/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 | 525 | { |
30 | 525 | pdf_drop_obj(ctx, obj); |
31 | 525 | } |
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 | 4.43k | { |
41 | 4.43k | pdf_obj *res; |
42 | | |
43 | 4.43k | if (!doc->resources.fonts) |
44 | 287 | doc->resources.fonts = fz_new_hash_table(ctx, 4096, sizeof(*key), -1, pdf_drop_obj_as_void); |
45 | | |
46 | 4.43k | memset(key, 0, sizeof(*key)); |
47 | 4.43k | fz_font_digest(ctx, item, key->digest); |
48 | | |
49 | 4.43k | key->type = type; |
50 | 4.43k | key->encoding = encoding; |
51 | 4.43k | key->local_xref = doc->local_xref_nesting > 0; |
52 | | |
53 | 4.43k | res = fz_hash_find(ctx, doc->resources.fonts, (void *)key); |
54 | 4.43k | if (res) |
55 | 3.90k | pdf_keep_obj(ctx, res); |
56 | 4.43k | return res; |
57 | 4.43k | } |
58 | | |
59 | | pdf_obj * |
60 | | pdf_insert_font_resource(fz_context *ctx, pdf_document *doc, pdf_font_resource_key *key, pdf_obj *obj) |
61 | 531 | { |
62 | 531 | pdf_obj *res = fz_hash_insert(ctx, doc->resources.fonts, (void *)key, obj); |
63 | 531 | if (res) |
64 | 0 | fz_warn(ctx, "warning: font resource already present"); |
65 | 531 | else |
66 | 531 | res = pdf_keep_obj(ctx, obj); |
67 | 531 | return pdf_keep_obj(ctx, res); |
68 | 531 | } |
69 | | |
70 | | static int purge_local_font_resource(fz_context *ctx, void *state, void *key_, int keylen, void *val) |
71 | 6 | { |
72 | 6 | pdf_font_resource_key *key = key_; |
73 | 6 | if (key->local_xref) |
74 | 6 | { |
75 | 6 | pdf_drop_obj(ctx, val); |
76 | 6 | return 1; |
77 | 6 | } |
78 | 0 | return 0; |
79 | 6 | } |
80 | | |
81 | | void |
82 | | pdf_purge_local_font_resources(fz_context *ctx, pdf_document *doc) |
83 | 11.3k | { |
84 | 11.3k | if (doc->resources.fonts) |
85 | 6 | fz_hash_filter(ctx, doc->resources.fonts, NULL, purge_local_font_resource); |
86 | 11.3k | } |
87 | | |
88 | | void |
89 | | pdf_drop_resource_tables(fz_context *ctx, pdf_document *doc) |
90 | 11.8k | { |
91 | 11.8k | if (doc) |
92 | 11.8k | { |
93 | 11.8k | fz_drop_hash_table(ctx, doc->resources.fonts); |
94 | 11.8k | } |
95 | 11.8k | } |