Coverage Report

Created: 2025-09-04 06:50

/src/mupdf/source/pdf/pdf-resources.c
Line
Count
Source (jump to first uncovered line)
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
0
{
30
0
  pdf_drop_obj(ctx, obj);
31
0
}
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
0
{
41
0
  pdf_obj *res;
42
43
0
  if (!doc->resources.fonts)
44
0
    doc->resources.fonts = fz_new_hash_table(ctx, 4096, sizeof(*key), -1, pdf_drop_obj_as_void);
45
46
0
  memset(key, 0, sizeof(*key));
47
0
  fz_font_digest(ctx, item, key->digest);
48
49
0
  key->type = type;
50
0
  key->encoding = encoding;
51
0
  key->local_xref = doc->local_xref_nesting > 0;
52
53
0
  res = fz_hash_find(ctx, doc->resources.fonts, (void *)key);
54
0
  if (res)
55
0
    pdf_keep_obj(ctx, res);
56
0
  return res;
57
0
}
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
0
{
81
0
  pdf_obj *res = fz_hash_insert(ctx, doc->resources.fonts, (void *)key, obj);
82
0
  if (res)
83
0
    fz_warn(ctx, "warning: font resource already present");
84
0
  else
85
0
    res = pdf_keep_obj(ctx, obj);
86
0
  return pdf_keep_obj(ctx, res);
87
0
}
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
0
{
102
0
  pdf_font_resource_key *key = key_;
103
0
  if (key->local_xref)
104
0
  {
105
0
    pdf_drop_obj(ctx, val);
106
0
    return 1;
107
0
  }
108
0
  return 0;
109
0
}
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
12
{
125
12
  if (doc->resources.fonts)
126
0
    fz_hash_filter(ctx, doc->resources.fonts, NULL, purge_local_font_resource);
127
12
  if (doc->resources.colorspaces)
128
0
    fz_hash_filter(ctx, doc->resources.colorspaces, NULL, purge_local_colorspace_resource);
129
12
}
130
131
void
132
pdf_drop_resource_tables(fz_context *ctx, pdf_document *doc)
133
12
{
134
12
  if (doc)
135
12
  {
136
12
    fz_drop_hash_table(ctx, doc->resources.colorspaces);
137
12
    fz_drop_hash_table(ctx, doc->resources.fonts);
138
12
  }
139
12
}