comparison mupdf-source/source/pdf/pdf-metrics.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-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 <stdlib.h>
27
28 void
29 pdf_set_font_wmode(fz_context *ctx, pdf_font_desc *font, int wmode)
30 {
31 font->wmode = wmode;
32 }
33
34 void
35 pdf_set_default_hmtx(fz_context *ctx, pdf_font_desc *font, int w)
36 {
37 font->dhmtx.w = w;
38 }
39
40 void
41 pdf_set_default_vmtx(fz_context *ctx, pdf_font_desc *font, int y, int w)
42 {
43 font->dvmtx.y = y;
44 font->dvmtx.w = w;
45 }
46
47 void
48 pdf_add_hmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int w)
49 {
50 if (font->hmtx_len + 1 >= font->hmtx_cap)
51 {
52 int new_cap = font->hmtx_cap + 16;
53 font->hmtx = fz_realloc_array(ctx, font->hmtx, new_cap, pdf_hmtx);
54 font->hmtx_cap = new_cap;
55 }
56
57 font->hmtx[font->hmtx_len].lo = lo;
58 font->hmtx[font->hmtx_len].hi = hi;
59 font->hmtx[font->hmtx_len].w = w;
60 font->hmtx_len++;
61 }
62
63 void
64 pdf_add_vmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int x, int y, int w)
65 {
66 if (font->vmtx_len + 1 >= font->vmtx_cap)
67 {
68 int new_cap = font->vmtx_cap + 16;
69 font->vmtx = fz_realloc_array(ctx, font->vmtx, new_cap, pdf_vmtx);
70 font->vmtx_cap = new_cap;
71 }
72
73 font->vmtx[font->vmtx_len].lo = lo;
74 font->vmtx[font->vmtx_len].hi = hi;
75 font->vmtx[font->vmtx_len].x = x;
76 font->vmtx[font->vmtx_len].y = y;
77 font->vmtx[font->vmtx_len].w = w;
78 font->vmtx_len++;
79 }
80
81 static int cmph(const void *a0, const void *b0)
82 {
83 pdf_hmtx *a = (pdf_hmtx*)a0;
84 pdf_hmtx *b = (pdf_hmtx*)b0;
85 return a->lo - b->lo;
86 }
87
88 static int cmpv(const void *a0, const void *b0)
89 {
90 pdf_vmtx *a = (pdf_vmtx*)a0;
91 pdf_vmtx *b = (pdf_vmtx*)b0;
92 return a->lo - b->lo;
93 }
94
95 void
96 pdf_end_hmtx(fz_context *ctx, pdf_font_desc *font)
97 {
98 if (!font->hmtx)
99 return;
100 qsort(font->hmtx, font->hmtx_len, sizeof(pdf_hmtx), cmph);
101 font->size += font->hmtx_cap * sizeof(pdf_hmtx);
102 }
103
104 void
105 pdf_end_vmtx(fz_context *ctx, pdf_font_desc *font)
106 {
107 if (!font->vmtx)
108 return;
109 qsort(font->vmtx, font->vmtx_len, sizeof(pdf_vmtx), cmpv);
110 font->size += font->vmtx_cap * sizeof(pdf_vmtx);
111 }
112
113 pdf_hmtx
114 pdf_lookup_hmtx(fz_context *ctx, pdf_font_desc *font, int cid)
115 {
116 int l = 0;
117 int r = font->hmtx_len - 1;
118 int m;
119
120 if (!font->hmtx)
121 goto notfound;
122
123 while (l <= r)
124 {
125 m = (l + r) >> 1;
126 if (cid < font->hmtx[m].lo)
127 r = m - 1;
128 else if (cid > font->hmtx[m].hi)
129 l = m + 1;
130 else
131 return font->hmtx[m];
132 }
133
134 notfound:
135 return font->dhmtx;
136 }
137
138 pdf_vmtx
139 pdf_lookup_vmtx(fz_context *ctx, pdf_font_desc *font, int cid)
140 {
141 pdf_hmtx h;
142 pdf_vmtx v;
143 int l = 0;
144 int r = font->vmtx_len - 1;
145 int m;
146
147 if (!font->vmtx)
148 goto notfound;
149
150 while (l <= r)
151 {
152 m = (l + r) >> 1;
153 if (cid < font->vmtx[m].lo)
154 r = m - 1;
155 else if (cid > font->vmtx[m].hi)
156 l = m + 1;
157 else
158 return font->vmtx[m];
159 }
160
161 notfound:
162 h = pdf_lookup_hmtx(ctx, font, cid);
163 v = font->dvmtx;
164 v.x = h.w / 2;
165 return v;
166 }