/src/mupdf/source/pdf/pdf-resources.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (C) 2004-2021 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 | 3.65k | { |
30 | 3.65k | pdf_drop_obj(ctx, obj); |
31 | 3.65k | } |
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 | 7.41k | { |
41 | 7.41k | pdf_obj *res; |
42 | | |
43 | 7.41k | if (!doc->resources.fonts) |
44 | 570 | doc->resources.fonts = fz_new_hash_table(ctx, 4096, sizeof(*key), -1, pdf_drop_obj_as_void); |
45 | | |
46 | 7.41k | memset(key, 0, sizeof(*key)); |
47 | 7.41k | fz_font_digest(ctx, item, key->digest); |
48 | | |
49 | 7.41k | key->type = type; |
50 | 7.41k | key->encoding = encoding; |
51 | 7.41k | key->local_xref = doc->local_xref_nesting > 0; |
52 | | |
53 | 7.41k | res = fz_hash_find(ctx, doc->resources.fonts, (void *)key); |
54 | 7.41k | if (res) |
55 | 3.76k | pdf_keep_obj(ctx, res); |
56 | 7.41k | return res; |
57 | 7.41k | } |
58 | | |
59 | | pdf_obj * |
60 | | pdf_find_colorspace_resource(fz_context *ctx, pdf_document *doc, fz_colorspace *item, pdf_colorspace_resource_key *key) |
61 | 0 | { |
62 | 0 | pdf_obj *res; |
63 | |
|
64 | 0 | if (!doc->resources.colorspaces) |
65 | 0 | doc->resources.colorspaces = fz_new_hash_table(ctx, 4096, sizeof(*key), -1, pdf_drop_obj_as_void); |
66 | |
|
67 | 0 | memset(key, 0, sizeof(*key)); |
68 | 0 | fz_colorspace_digest(ctx, item, key->digest); |
69 | |
|
70 | 0 | key->local_xref = doc->local_xref_nesting > 0; |
71 | |
|
72 | 0 | res = fz_hash_find(ctx, doc->resources.colorspaces, (void *)key); |
73 | 0 | if (res) |
74 | 0 | pdf_keep_obj(ctx, res); |
75 | 0 | return res; |
76 | 0 | } |
77 | | |
78 | | pdf_obj * |
79 | | pdf_insert_font_resource(fz_context *ctx, pdf_document *doc, pdf_font_resource_key *key, pdf_obj *obj) |
80 | 3.65k | { |
81 | 3.65k | pdf_obj *res = fz_hash_insert(ctx, doc->resources.fonts, (void *)key, obj); |
82 | 3.65k | if (res) |
83 | 0 | fz_warn(ctx, "warning: font resource already present"); |
84 | 3.65k | else |
85 | 3.65k | res = pdf_keep_obj(ctx, obj); |
86 | 3.65k | return pdf_keep_obj(ctx, res); |
87 | 3.65k | } |
88 | | |
89 | | pdf_obj * |
90 | | pdf_insert_colorspace_resource(fz_context *ctx, pdf_document *doc, pdf_colorspace_resource_key *key, pdf_obj *obj) |
91 | 0 | { |
92 | 0 | pdf_obj *res = fz_hash_insert(ctx, doc->resources.colorspaces, (void *)key, obj); |
93 | 0 | if (res) |
94 | 0 | fz_warn(ctx, "warning: colorspace resource already present"); |
95 | 0 | else |
96 | 0 | res = pdf_keep_obj(ctx, obj); |
97 | 0 | return pdf_keep_obj(ctx, res); |
98 | 0 | } |
99 | | |
100 | | static int purge_local_font_resource(fz_context *ctx, void *state, void *key_, int keylen, void *val) |
101 | 6 | { |
102 | 6 | pdf_font_resource_key *key = key_; |
103 | 6 | if (key->local_xref) |
104 | 6 | { |
105 | 6 | pdf_drop_obj(ctx, val); |
106 | 6 | return 1; |
107 | 6 | } |
108 | 0 | return 0; |
109 | 6 | } |
110 | | |
111 | | static int purge_local_colorspace_resource(fz_context *ctx, void *state, void *key_, int keylen, void *val) |
112 | 0 | { |
113 | 0 | pdf_colorspace_resource_key *key = key_; |
114 | 0 | if (key->local_xref) |
115 | 0 | { |
116 | 0 | pdf_drop_obj(ctx, val); |
117 | 0 | return 1; |
118 | 0 | } |
119 | 0 | return 0; |
120 | 0 | } |
121 | | |
122 | | void |
123 | | pdf_purge_local_resources(fz_context *ctx, pdf_document *doc) |
124 | 9.76k | { |
125 | 9.76k | if (doc->resources.fonts) |
126 | 6 | fz_hash_filter(ctx, doc->resources.fonts, NULL, purge_local_font_resource); |
127 | 9.76k | if (doc->resources.colorspaces) |
128 | 0 | fz_hash_filter(ctx, doc->resources.colorspaces, NULL, purge_local_colorspace_resource); |
129 | 9.76k | } |
130 | | |
131 | | void |
132 | | pdf_drop_resource_tables(fz_context *ctx, pdf_document *doc) |
133 | 10.3k | { |
134 | 10.3k | if (doc) |
135 | 10.3k | { |
136 | 10.3k | fz_drop_hash_table(ctx, doc->resources.fonts); |
137 | 10.3k | } |
138 | 10.3k | } |