Coverage Report

Created: 2024-07-05 06:13

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