pdf-resources.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "mupdf/pdf.h"
  24. #include <string.h>
  25. static void pdf_drop_obj_as_void(fz_context *ctx, void *obj)
  26. {
  27. pdf_drop_obj(ctx, obj);
  28. }
  29. /* We do need to come up with an effective way to see what is already in the
  30. * file to avoid adding to what is already there. This is avoided for pdfwrite
  31. * as we check as we add each font. For adding text to an existing file though
  32. * it may be more problematic. */
  33. pdf_obj *
  34. pdf_find_font_resource(fz_context *ctx, pdf_document *doc, int type, int encoding, fz_font *item, pdf_font_resource_key *key)
  35. {
  36. pdf_obj *res;
  37. if (!doc->resources.fonts)
  38. doc->resources.fonts = fz_new_hash_table(ctx, 4096, sizeof(*key), -1, pdf_drop_obj_as_void);
  39. memset(key, 0, sizeof(*key));
  40. fz_font_digest(ctx, item, key->digest);
  41. key->type = type;
  42. key->encoding = encoding;
  43. key->local_xref = doc->local_xref_nesting > 0;
  44. res = fz_hash_find(ctx, doc->resources.fonts, (void *)key);
  45. if (res)
  46. pdf_keep_obj(ctx, res);
  47. return res;
  48. }
  49. pdf_obj *
  50. pdf_find_colorspace_resource(fz_context *ctx, pdf_document *doc, fz_colorspace *item, pdf_colorspace_resource_key *key)
  51. {
  52. pdf_obj *res;
  53. if (!doc->resources.colorspaces)
  54. doc->resources.colorspaces = fz_new_hash_table(ctx, 4096, sizeof(*key), -1, pdf_drop_obj_as_void);
  55. memset(key, 0, sizeof(*key));
  56. fz_colorspace_digest(ctx, item, key->digest);
  57. key->local_xref = doc->local_xref_nesting > 0;
  58. res = fz_hash_find(ctx, doc->resources.colorspaces, (void *)key);
  59. if (res)
  60. pdf_keep_obj(ctx, res);
  61. return res;
  62. }
  63. pdf_obj *
  64. pdf_insert_font_resource(fz_context *ctx, pdf_document *doc, pdf_font_resource_key *key, pdf_obj *obj)
  65. {
  66. pdf_obj *res = fz_hash_insert(ctx, doc->resources.fonts, (void *)key, obj);
  67. if (res)
  68. fz_warn(ctx, "warning: font resource already present");
  69. else
  70. res = pdf_keep_obj(ctx, obj);
  71. return pdf_keep_obj(ctx, res);
  72. }
  73. pdf_obj *
  74. pdf_insert_colorspace_resource(fz_context *ctx, pdf_document *doc, pdf_colorspace_resource_key *key, pdf_obj *obj)
  75. {
  76. pdf_obj *res = fz_hash_insert(ctx, doc->resources.colorspaces, (void *)key, obj);
  77. if (res)
  78. fz_warn(ctx, "warning: colorspace resource already present");
  79. else
  80. res = pdf_keep_obj(ctx, obj);
  81. return pdf_keep_obj(ctx, res);
  82. }
  83. static int purge_local_font_resource(fz_context *ctx, void *state, void *key_, int keylen, void *val)
  84. {
  85. pdf_font_resource_key *key = key_;
  86. if (key->local_xref)
  87. {
  88. pdf_drop_obj(ctx, val);
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. static int purge_local_colorspace_resource(fz_context *ctx, void *state, void *key_, int keylen, void *val)
  94. {
  95. pdf_colorspace_resource_key *key = key_;
  96. if (key->local_xref)
  97. {
  98. pdf_drop_obj(ctx, val);
  99. return 1;
  100. }
  101. return 0;
  102. }
  103. void
  104. pdf_purge_local_resources(fz_context *ctx, pdf_document *doc)
  105. {
  106. if (doc->resources.fonts)
  107. fz_hash_filter(ctx, doc->resources.fonts, NULL, purge_local_font_resource);
  108. if (doc->resources.colorspaces)
  109. fz_hash_filter(ctx, doc->resources.colorspaces, NULL, purge_local_colorspace_resource);
  110. }
  111. void
  112. pdf_drop_resource_tables(fz_context *ctx, pdf_document *doc)
  113. {
  114. if (doc)
  115. {
  116. fz_drop_hash_table(ctx, doc->resources.colorspaces);
  117. fz_drop_hash_table(ctx, doc->resources.fonts);
  118. }
  119. }