Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/source/pdf/pdf-resources.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 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 | |
| 23 #include "mupdf/fitz.h" | |
| 24 #include "mupdf/pdf.h" | |
| 25 | |
| 26 #include <string.h> | |
| 27 | |
| 28 static void pdf_drop_obj_as_void(fz_context *ctx, void *obj) | |
| 29 { | |
| 30 pdf_drop_obj(ctx, obj); | |
| 31 } | |
| 32 | |
| 33 /* We do need to come up with an effective way to see what is already in the | |
| 34 * file to avoid adding to what is already there. This is avoided for pdfwrite | |
| 35 * as we check as we add each font. For adding text to an existing file though | |
| 36 * it may be more problematic. */ | |
| 37 | |
| 38 pdf_obj * | |
| 39 pdf_find_font_resource(fz_context *ctx, pdf_document *doc, int type, int encoding, fz_font *item, pdf_font_resource_key *key) | |
| 40 { | |
| 41 pdf_obj *res; | |
| 42 | |
| 43 if (!doc->resources.fonts) | |
| 44 doc->resources.fonts = fz_new_hash_table(ctx, 4096, sizeof(*key), -1, pdf_drop_obj_as_void); | |
| 45 | |
| 46 memset(key, 0, sizeof(*key)); | |
| 47 fz_font_digest(ctx, item, key->digest); | |
| 48 | |
| 49 key->type = type; | |
| 50 key->encoding = encoding; | |
| 51 key->local_xref = doc->local_xref_nesting > 0; | |
| 52 | |
| 53 res = fz_hash_find(ctx, doc->resources.fonts, (void *)key); | |
| 54 if (res) | |
| 55 pdf_keep_obj(ctx, res); | |
| 56 return res; | |
| 57 } | |
| 58 | |
| 59 pdf_obj * | |
| 60 pdf_find_colorspace_resource(fz_context *ctx, pdf_document *doc, fz_colorspace *item, pdf_colorspace_resource_key *key) | |
| 61 { | |
| 62 pdf_obj *res; | |
| 63 | |
| 64 if (!doc->resources.colorspaces) | |
| 65 doc->resources.colorspaces = fz_new_hash_table(ctx, 4096, sizeof(*key), -1, pdf_drop_obj_as_void); | |
| 66 | |
| 67 memset(key, 0, sizeof(*key)); | |
| 68 fz_colorspace_digest(ctx, item, key->digest); | |
| 69 | |
| 70 key->local_xref = doc->local_xref_nesting > 0; | |
| 71 | |
| 72 res = fz_hash_find(ctx, doc->resources.colorspaces, (void *)key); | |
| 73 if (res) | |
| 74 pdf_keep_obj(ctx, res); | |
| 75 return res; | |
| 76 } | |
| 77 | |
| 78 pdf_obj * | |
| 79 pdf_insert_font_resource(fz_context *ctx, pdf_document *doc, pdf_font_resource_key *key, pdf_obj *obj) | |
| 80 { | |
| 81 pdf_obj *res = fz_hash_insert(ctx, doc->resources.fonts, (void *)key, obj); | |
| 82 if (res) | |
| 83 fz_warn(ctx, "warning: font resource already present"); | |
| 84 else | |
| 85 res = pdf_keep_obj(ctx, obj); | |
| 86 return pdf_keep_obj(ctx, res); | |
| 87 } | |
| 88 | |
| 89 pdf_obj * | |
| 90 pdf_insert_colorspace_resource(fz_context *ctx, pdf_document *doc, pdf_colorspace_resource_key *key, pdf_obj *obj) | |
| 91 { | |
| 92 pdf_obj *res = fz_hash_insert(ctx, doc->resources.colorspaces, (void *)key, obj); | |
| 93 if (res) | |
| 94 fz_warn(ctx, "warning: colorspace resource already present"); | |
| 95 else | |
| 96 res = pdf_keep_obj(ctx, obj); | |
| 97 return pdf_keep_obj(ctx, res); | |
| 98 } | |
| 99 | |
| 100 static int purge_local_font_resource(fz_context *ctx, void *state, void *key_, int keylen, void *val) | |
| 101 { | |
| 102 pdf_font_resource_key *key = key_; | |
| 103 if (key->local_xref) | |
| 104 { | |
| 105 pdf_drop_obj(ctx, val); | |
| 106 return 1; | |
| 107 } | |
| 108 return 0; | |
| 109 } | |
| 110 | |
| 111 static int purge_local_colorspace_resource(fz_context *ctx, void *state, void *key_, int keylen, void *val) | |
| 112 { | |
| 113 pdf_colorspace_resource_key *key = key_; | |
| 114 if (key->local_xref) | |
| 115 { | |
| 116 pdf_drop_obj(ctx, val); | |
| 117 return 1; | |
| 118 } | |
| 119 return 0; | |
| 120 } | |
| 121 | |
| 122 void | |
| 123 pdf_purge_local_resources(fz_context *ctx, pdf_document *doc) | |
| 124 { | |
| 125 if (doc->resources.fonts) | |
| 126 fz_hash_filter(ctx, doc->resources.fonts, NULL, purge_local_font_resource); | |
| 127 if (doc->resources.colorspaces) | |
| 128 fz_hash_filter(ctx, doc->resources.colorspaces, NULL, purge_local_colorspace_resource); | |
| 129 } | |
| 130 | |
| 131 void | |
| 132 pdf_drop_resource_tables(fz_context *ctx, pdf_document *doc) | |
| 133 { | |
| 134 if (doc) | |
| 135 { | |
| 136 fz_drop_hash_table(ctx, doc->resources.colorspaces); | |
| 137 fz_drop_hash_table(ctx, doc->resources.fonts); | |
| 138 } | |
| 139 } |
