pdf_pattern.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "fitz-internal.h"
  2. #include "mupdf-internal.h"
  3. pdf_pattern *
  4. pdf_keep_pattern(fz_context *ctx, pdf_pattern *pat)
  5. {
  6. return (pdf_pattern *)fz_keep_storable(ctx, &pat->storable);
  7. }
  8. void
  9. pdf_drop_pattern(fz_context *ctx, pdf_pattern *pat)
  10. {
  11. fz_drop_storable(ctx, &pat->storable);
  12. }
  13. static void
  14. pdf_free_pattern_imp(fz_context *ctx, fz_storable *pat_)
  15. {
  16. pdf_pattern *pat = (pdf_pattern *)pat_;
  17. if (pat->resources)
  18. pdf_drop_obj(pat->resources);
  19. if (pat->contents)
  20. pdf_drop_obj(pat->contents);
  21. fz_free(ctx, pat);
  22. }
  23. static unsigned int
  24. pdf_pattern_size(pdf_pattern *pat)
  25. {
  26. if (pat == NULL)
  27. return 0;
  28. return sizeof(*pat);
  29. }
  30. pdf_pattern *
  31. pdf_load_pattern(pdf_document *xref, pdf_obj *dict)
  32. {
  33. pdf_pattern *pat;
  34. pdf_obj *obj;
  35. fz_context *ctx = xref->ctx;
  36. if ((pat = pdf_find_item(ctx, pdf_free_pattern_imp, dict)))
  37. {
  38. return pat;
  39. }
  40. pat = fz_malloc_struct(ctx, pdf_pattern);
  41. FZ_INIT_STORABLE(pat, 1, pdf_free_pattern_imp);
  42. pat->resources = NULL;
  43. pat->contents = NULL;
  44. /* Store pattern now, to avoid possible recursion if objects refer back to this one */
  45. pdf_store_item(ctx, dict, pat, pdf_pattern_size(pat));
  46. pat->ismask = pdf_to_int(pdf_dict_gets(dict, "PaintType")) == 2;
  47. pat->xstep = pdf_to_real(pdf_dict_gets(dict, "XStep"));
  48. pat->ystep = pdf_to_real(pdf_dict_gets(dict, "YStep"));
  49. obj = pdf_dict_gets(dict, "BBox");
  50. pdf_to_rect(ctx, obj, &pat->bbox);
  51. obj = pdf_dict_gets(dict, "Matrix");
  52. if (obj)
  53. pdf_to_matrix(ctx, obj, &pat->matrix);
  54. else
  55. pat->matrix = fz_identity;
  56. pat->resources = pdf_dict_gets(dict, "Resources");
  57. if (pat->resources)
  58. pdf_keep_obj(pat->resources);
  59. fz_try(ctx)
  60. {
  61. pat->contents = pdf_keep_obj(dict);
  62. }
  63. fz_catch(ctx)
  64. {
  65. pdf_remove_item(ctx, pdf_free_pattern_imp, dict);
  66. pdf_drop_pattern(ctx, pat);
  67. fz_throw(ctx, "cannot load pattern stream (%d %d R)", pdf_to_num(dict), pdf_to_gen(dict));
  68. }
  69. return pat;
  70. }