src/pdfmake_font_widths.c

filter: Statementstmt 87.5% 246 / 281 Branchbran 58.8% 153 / 260 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.7% 399 / 541 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/pdfmake_types.c ↑ Summary xs/form.xs »
1 /*
2 * pdfmake_font_widths.c — Glyph-advance + descriptor metrics resolution.
3 *
4 * PDF spec references:
5 * §9.2.4 Simple font /FirstChar, /LastChar, /Widths
6 * §9.7.4.3 CID /W array (formats 1 and 2), /DW default width
7 * §9.8.1 Font descriptor /Ascent, /Descent, /CapHeight, /XHeight
8 */
9  
10 #include "pdfmake_font_widths.h"
11 #include "pdfmake_reader.h"
12 #include "pdfmake_parser.h"
13 #include "pdfmake_font.h"
14 #include "pdfmake_arena.h"
15 #include "pdfmake_buf.h"
16 #include <string.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19  
20 /*============================================================================
21 * Helpers
22 *==========================================================================*/
23  
24 2823 static int16_t int_of(const pdfmake_obj_t *o, int16_t fallback) {
25 2823 ▶ if (!o) return fallback;
Branch: 1 branch
BranchTrueFalseTotal
if (!o) return fallback; 47 2776 2823
26 2776 ▶ if (o->kind == PDFMAKE_INT) return (int16_t)o->as.i;
Branch: 1 branch
BranchTrueFalseTotal
if (o->kind == PDFMAKE_INT) return (int16_t)o->as.i; 2776 0 2776
27 0 ▶ if (o->kind == PDFMAKE_REAL) return (int16_t)o->as.r;
Branch: 1 branch
BranchTrueFalseTotal
if (o->kind == PDFMAKE_REAL) return (int16_t)o->as.r; 0 0 0
28 0 return fallback;
29 }
30  
31 135 static int32_t int32_of(const pdfmake_obj_t *o, int32_t fallback) {
32 135 ▶ if (!o) return fallback;
Branch: 1 branch
BranchTrueFalseTotal
if (!o) return fallback; 0 135 135
33 135 ▶ if (o->kind == PDFMAKE_INT) return (int32_t)o->as.i;
Branch: 1 branch
BranchTrueFalseTotal
if (o->kind == PDFMAKE_INT) return (int32_t)o->as.i; 135 0 135
34 0 ▶ if (o->kind == PDFMAKE_REAL) return (int32_t)o->as.r;
Branch: 1 branch
BranchTrueFalseTotal
if (o->kind == PDFMAKE_REAL) return (int32_t)o->as.r; 0 0 0
35 0 return fallback;
36 }
37  
38 /*============================================================================
39 * Descriptor metrics
40 *==========================================================================*/
41  
42 47 static void fill_descriptor_metrics(pdfmake_arena_t *arena,
43 pdfmake_obj_t *descriptor,
44 pdfmake_font_widths_t *out)
45 {
46 uint32_t ascent_k;
47 uint32_t descent_k;
48 uint32_t cap_k;
49 uint32_t xh_k;
50 uint32_t mw_k;
51 pdfmake_obj_t *mw;
52 47 ▶ if (!descriptor || descriptor->kind != PDFMAKE_DICT) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!descriptor || descriptor->kind != PDFMAKE_DICT) return; 47 0 47
if (!descriptor || descriptor->kind != PDFMAKE_DICT) return; 0 47 47
53  
54 47 ascent_k = pdfmake_arena_intern_name(arena, "Ascent", 6);
55 47 descent_k = pdfmake_arena_intern_name(arena, "Descent", 7);
56 47 cap_k = pdfmake_arena_intern_name(arena, "CapHeight", 9);
57 47 xh_k = pdfmake_arena_intern_name(arena, "XHeight", 7);
58 47 mw_k = pdfmake_arena_intern_name(arena, "MissingWidth", 12);
59  
60 47 out->ascent = int_of(pdfmake_dict_get(descriptor, ascent_k), out->ascent);
61 47 out->descent = int_of(pdfmake_dict_get(descriptor, descent_k), out->descent);
62 47 out->cap_height = int_of(pdfmake_dict_get(descriptor, cap_k), out->cap_height);
63 47 out->x_height = int_of(pdfmake_dict_get(descriptor, xh_k), out->x_height);
64  
65 47 mw = pdfmake_dict_get(descriptor, mw_k);
66 47 ▶ if (mw) out->default_width = int_of(mw, out->default_width);
Branch: 1 branch
BranchTrueFalseTotal
if (mw) out->default_width = int_of(mw, out->default_width); 0 47 47
67 }
68  
69 /*============================================================================
70 * Simple font /Widths
71 *==========================================================================*/
72  
73 60 void pdfmake_font_widths_init(pdfmake_font_widths_t *w) {
74 60 memset(w, 0, sizeof(*w));
75 60 }
76  
77 49 int pdfmake_font_widths_from_simple(
78 pdfmake_arena_t *arena,
79 pdfmake_obj_t *font_dict,
80 pdfmake_font_widths_t *out)
81 {
82 uint32_t first_k;
83 uint32_t last_k;
84 uint32_t widths_k;
85 uint32_t fd_k;
86 pdfmake_obj_t *fd;
87 pdfmake_obj_t *first;
88 pdfmake_obj_t *last;
89 pdfmake_obj_t *widths;
90 int32_t fc;
91 int32_t lc;
92 size_t n;
93 size_t have;
94 int16_t *table;
95 size_t i;
96  
97 49 pdfmake_font_widths_init(out);
98 49 ▶ if (!font_dict || font_dict->kind != PDFMAKE_DICT || !arena) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!font_dict || font_dict->kind != PDFMAKE_DICT || !arena) return -1; 49 0 49
if (!font_dict || font_dict->kind != PDFMAKE_DICT || !arena) return -1; 49 0 49
if (!font_dict || font_dict->kind != PDFMAKE_DICT || !arena) return -1; 0 49 49
99  
100 49 first_k = pdfmake_arena_intern_name(arena, "FirstChar", 9);
101 49 last_k = pdfmake_arena_intern_name(arena, "LastChar", 8);
102 49 widths_k = pdfmake_arena_intern_name(arena, "Widths", 6);
103 49 fd_k = pdfmake_arena_intern_name(arena, "FontDescriptor", 14);
104  
105 /* Always pull descriptor metrics even if /Widths is absent */
106 49 fd = pdfmake_dict_get(font_dict, fd_k);
107 49 ▶ if (fd && fd->kind == PDFMAKE_DICT) {
Branch: 2 branches
BranchTrueFalseTotal
if (fd && fd->kind == PDFMAKE_DICT) { 46 3 49
if (fd && fd->kind == PDFMAKE_DICT) { 46 0 46
108 46 fill_descriptor_metrics(arena, fd, out);
109 }
110  
111 49 first = pdfmake_dict_get(font_dict, first_k);
112 49 last = pdfmake_dict_get(font_dict, last_k);
113 49 widths = pdfmake_dict_get(font_dict, widths_k);
114  
115 49 ▶ if (!first || !last || !widths) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!first || !last || !widths) return -1; 49 0 49
if (!first || !last || !widths) return -1; 49 0 49
if (!first || !last || !widths) return -1; 0 49 49
116 49 ▶ if (widths->kind != PDFMAKE_ARRAY) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (widths->kind != PDFMAKE_ARRAY) return -1; 0 49 49
117  
118 49 fc = int32_of(first, -1);
119 49 lc = int32_of(last, -1);
120 49 ▶ if (fc < 0 || lc < fc || fc > 0xFFFF || lc > 0xFFFF) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (fc < 0 || lc < fc || fc > 0xFFFF || lc > 0xFFFF) return -1; 49 0 49
if (fc < 0 || lc < fc || fc > 0xFFFF || lc > 0xFFFF) return -1; 49 0 49
if (fc < 0 || lc < fc || fc > 0xFFFF || lc > 0xFFFF) return -1; 49 0 49
if (fc < 0 || lc < fc || fc > 0xFFFF || lc > 0xFFFF) return -1; 0 49 49
121  
122 49 n = (size_t)(lc - fc + 1);
123 49 have = pdfmake_array_len(widths);
124 49 ▶ if (have < n) n = have;
Branch: 1 branch
BranchTrueFalseTotal
if (have < n) n = have; 0 49 49
125  
126 49 table = pdfmake_arena_alloc(arena, n * sizeof(int16_t));
127 49 ▶ if (!table) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!table) return -1; 0 49 49
128  
129 2641 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2592 49 2641
130 2592 pdfmake_obj_t *w = pdfmake_array_get(widths, i);
131 2592 table[i] = int_of(w, 0);
132 }
133  
134 49 out->table = table;
135 49 out->first_char = (uint16_t)fc;
136 49 out->last_char = (uint16_t)(fc + n - 1);
137 49 return 0;
138 }
139  
140 /*============================================================================
141 * CID font /W array
142 *
143 * Two entry formats, mixed freely in one array:
144 * 1. first [ w1 w2 w3 ... ] codes first..first+n-1 have widths wi
145 * 2. first last w codes first..last all have width w
146 *==========================================================================*/
147  
148 32 static int add_range(pdfmake_arena_t *arena,
149 pdfmake_width_range_t **list, size_t *count, size_t *cap,
150 uint32_t lo, uint32_t hi, int16_t w)
151 {
152 pdfmake_width_range_t *r;
153 32 ▶ if (*count >= *cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (*count >= *cap) { 10 22 32
154 10 ▶ size_t new_cap = *cap ? *cap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = *cap ? *cap * 2 : 16; 0 10 10
155 10 pdfmake_width_range_t *n = pdfmake_arena_alloc(
156 arena, new_cap * sizeof(pdfmake_width_range_t));
157 10 ▶ if (!n) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return -1; 0 10 10
158 10 ▶ if (*list && *count > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (*list && *count > 0) { 0 10 10
if (*list && *count > 0) { 0 0 0
159 0 memcpy(n, *list, *count * sizeof(pdfmake_width_range_t));
160 }
161 10 *list = n;
162 10 *cap = new_cap;
163 }
164 32 r = &(*list)[(*count)++];
165 32 r->lo = lo;
166 32 r->hi = hi;
167 32 r->width = w;
168 32 return 0;
169 }
170  
171 28 static int range_cmp(const void *a, const void *b) {
172 28 const pdfmake_width_range_t *ra = a;
173 28 const pdfmake_width_range_t *rb = b;
174 28 ▶ if (ra->lo < rb->lo) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ra->lo < rb->lo) return -1; 28 0 28
175 0 ▶ if (ra->lo > rb->lo) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ra->lo > rb->lo) return 1; 0 0 0
176 0 return 0;
177 }
178  
179 10 static int parse_w_array(pdfmake_arena_t *arena,
180 pdfmake_obj_t *w_arr,
181 pdfmake_font_widths_t *out)
182 {
183 pdfmake_width_range_t *list;
184 size_t count, cap;
185 size_t n;
186 size_t i;
187 10 ▶ if (!w_arr || w_arr->kind != PDFMAKE_ARRAY) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!w_arr || w_arr->kind != PDFMAKE_ARRAY) return -1; 10 0 10
if (!w_arr || w_arr->kind != PDFMAKE_ARRAY) return -1; 0 10 10
188  
189 10 list = NULL;
190 10 count = 0; cap = 0;
191 10 n = pdfmake_array_len(w_arr);
192  
193 10 i = 0;
194 38 ▶ while (i < n) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < n) { 28 10 38
195 28 pdfmake_obj_t *first = pdfmake_array_get(w_arr, i++);
196 28 int32_t fc = int32_of(first, -1);
197 pdfmake_obj_t *next;
198 28 ▶ if (fc < 0 || i >= n) break;
Branch: 2 branches
BranchTrueFalseTotal
if (fc < 0 || i >= n) break; 28 0 28
if (fc < 0 || i >= n) break; 28 0 28
199  
200 28 next = pdfmake_array_get(w_arr, i);
201 28 ▶ if (!next) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!next) break; 0 28 28
202  
203 28 ▶ if (next->kind == PDFMAKE_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (next->kind == PDFMAKE_ARRAY) { 19 9 28
204 /* Format 1: first [ w w w ... ] */
205 19 size_t m = pdfmake_array_len(next);
206 size_t j;
207 /* Expand as consecutive single-code ranges for uniform lookup.
208 * Could compress runs of equal widths, but not worth the complexity. */
209 42 ▶ for (j = 0; j < m; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < m; j++) { 23 19 42
210 23 int16_t w = int_of(pdfmake_array_get(next, j), 0);
211 23 ▶ if (add_range(arena, &list, &count, &cap,
Branch: 1 branch
BranchTrueFalseTotal
if (add_range(arena, &list, &count, &cap, 0 23 23
212 (uint32_t)(fc + j), (uint32_t)(fc + j), w) < 0) {
213 0 return -1;
214 }
215 }
216 19 i++;
217 } else {
218 /* Format 2: first last w */
219 9 int32_t lc = int32_of(next, fc);
220 int16_t w;
221 9 ▶ if (lc < fc) lc = fc;
Branch: 1 branch
BranchTrueFalseTotal
if (lc < fc) lc = fc; 0 9 9
222 9 ▶ if (i + 1 >= n) break;
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 >= n) break; 0 9 9
223 9 w = int_of(pdfmake_array_get(w_arr, i + 1), 0);
224 9 ▶ if (add_range(arena, &list, &count, &cap,
Branch: 1 branch
BranchTrueFalseTotal
if (add_range(arena, &list, &count, &cap, 0 9 9
225 (uint32_t)fc, (uint32_t)lc, w) < 0) {
226 0 return -1;
227 }
228 9 i += 2;
229 }
230 }
231  
232 10 ▶ if (count > 1) qsort(list, count, sizeof(*list), range_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 1) qsort(list, count, sizeof(*list), range_cmp); 10 0 10
233 10 out->ranges = list;
234 10 out->range_count = count;
235 10 return 0;
236 }
237  
238 11 int pdfmake_font_widths_from_cid(
239 pdfmake_arena_t *arena,
240 pdfmake_obj_t *font_dict,
241 pdfmake_font_widths_t *out)
242 {
243 uint32_t desc_k;
244 pdfmake_obj_t *desc_arr;
245 11 pdfmake_obj_t *cidfont = NULL;
246 uint32_t w_k;
247 uint32_t dw_k;
248 uint32_t fd_k;
249 pdfmake_obj_t *dw;
250 pdfmake_obj_t *fd;
251 pdfmake_obj_t *w_arr;
252  
253 11 pdfmake_font_widths_init(out);
254 11 out->default_width = 1000; /* Per §9.7.4.3: /DW defaults to 1000 */
255  
256 11 ▶ if (!font_dict || font_dict->kind != PDFMAKE_DICT || !arena) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!font_dict || font_dict->kind != PDFMAKE_DICT || !arena) return -1; 11 0 11
if (!font_dict || font_dict->kind != PDFMAKE_DICT || !arena) return -1; 11 0 11
if (!font_dict || font_dict->kind != PDFMAKE_DICT || !arena) return -1; 0 11 11
257  
258 /* For Type0 fonts, widths live on /DescendantFonts[0] */
259 11 desc_k = pdfmake_arena_intern_name(arena, "DescendantFonts", 15);
260 11 desc_arr = pdfmake_dict_get(font_dict, desc_k);
261 11 ▶ if (desc_arr && desc_arr->kind == PDFMAKE_ARRAY && pdfmake_array_len(desc_arr) > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (desc_arr && desc_arr->kind == PDFMAKE_ARRAY && pdfmake_array_len(desc_arr) > 0) { 11 0 11
if (desc_arr && desc_arr->kind == PDFMAKE_ARRAY && pdfmake_array_len(desc_arr) > 0) { 11 0 11
if (desc_arr && desc_arr->kind == PDFMAKE_ARRAY && pdfmake_array_len(desc_arr) > 0) { 11 0 11
262 11 cidfont = pdfmake_array_get(desc_arr, 0);
263 }
264 11 ▶ if (!cidfont) cidfont = font_dict; /* Might already be the CIDFont */
Branch: 1 branch
BranchTrueFalseTotal
if (!cidfont) cidfont = font_dict; /* Might already be the CIDFont */ 0 11 11
265 11 ▶ if (cidfont->kind != PDFMAKE_DICT) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (cidfont->kind != PDFMAKE_DICT) return -1; 0 11 11
266  
267 11 w_k = pdfmake_arena_intern_name(arena, "W", 1);
268 11 dw_k = pdfmake_arena_intern_name(arena, "DW", 2);
269 11 fd_k = pdfmake_arena_intern_name(arena, "FontDescriptor", 14);
270  
271 11 dw = pdfmake_dict_get(cidfont, dw_k);
272 11 ▶ if (dw) out->default_width = int_of(dw, out->default_width);
Branch: 1 branch
BranchTrueFalseTotal
if (dw) out->default_width = int_of(dw, out->default_width); 11 0 11
273  
274 11 fd = pdfmake_dict_get(cidfont, fd_k);
275 11 ▶ if (fd && fd->kind == PDFMAKE_DICT) {
Branch: 2 branches
BranchTrueFalseTotal
if (fd && fd->kind == PDFMAKE_DICT) { 1 10 11
if (fd && fd->kind == PDFMAKE_DICT) { 1 0 1
276 1 fill_descriptor_metrics(arena, fd, out);
277 }
278  
279 11 w_arr = pdfmake_dict_get(cidfont, w_k);
280 11 ▶ if (w_arr) parse_w_array(arena, w_arr, out);
Branch: 1 branch
BranchTrueFalseTotal
if (w_arr) parse_w_array(arena, w_arr, out); 10 1 11
281  
282 11 return 0;
283 }
284  
285 /*============================================================================
286 * Lookup
287 *==========================================================================*/
288  
289 12798 int16_t pdfmake_font_widths_lookup(const pdfmake_font_widths_t *w,
290 uint32_t code)
291 {
292 12798 ▶ if (!w) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!w) return 0; 0 12798 12798
293  
294 /* Simple font table */
295 12798 ▶ if (w->table && code >= w->first_char && code <= w->last_char) {
Branch: 3 branches
BranchTrueFalseTotal
if (w->table && code >= w->first_char && code <= w->last_char) { 12730 68 12798
if (w->table && code >= w->first_char && code <= w->last_char) { 12730 0 12730
if (w->table && code >= w->first_char && code <= w->last_char) { 12730 0 12730
296 12730 int16_t v = w->table[code - w->first_char];
297 12730 ▶ if (v != 0) return v;
Branch: 1 branch
BranchTrueFalseTotal
if (v != 0) return v; 12728 2 12730
298 }
299  
300 /* CID range list — binary search */
301 70 ▶ if (w->ranges && w->range_count > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (w->ranges && w->range_count > 0) { 62 8 70
if (w->ranges && w->range_count > 0) { 62 0 62
302 62 size_t lo = 0, hi = w->range_count;
303 /* Find last range with range.lo <= code */
304 190 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 128 62 190
305 128 size_t mid = (lo + hi) / 2;
306 128 ▶ if (w->ranges[mid].lo <= code) lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (w->ranges[mid].lo <= code) lo = mid + 1; 112 16 128
307 16 else hi = mid;
308 }
309 62 ▶ if (lo > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (lo > 0) { 62 0 62
310 62 const pdfmake_width_range_t *r = &w->ranges[lo - 1];
311 62 ▶ if (code >= r->lo && code <= r->hi) return r->width;
Branch: 2 branches
BranchTrueFalseTotal
if (code >= r->lo && code <= r->hi) return r->width; 62 0 62
if (code >= r->lo && code <= r->hi) return r->width; 20 42 62
312 }
313 }
314  
315 50 return w->default_width;
316 }
317  
318 /*============================================================================
319 * Phase 6: Enhance widths from embedded /FontFile2 TTF data.
320 *==========================================================================*/
321  
322 /* Resolve /FontFile2 (or /FontFile3) stream from a font descriptor.
323 * Returns the decoded stream bytes (owned by `out_buf`) on success. */
324 47 static int fetch_font_file_stream(
325 struct pdfmake_reader *reader,
326 pdfmake_arena_t *arena,
327 pdfmake_obj_t *descriptor,
328 pdfmake_buf_t *out_buf)
329 {
330 uint32_t ff2_k;
331 uint32_t ff3_k;
332 pdfmake_obj_t *ref;
333 47 ▶ if (!descriptor || descriptor->kind != PDFMAKE_DICT) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!descriptor || descriptor->kind != PDFMAKE_DICT) return -1; 47 0 47
if (!descriptor || descriptor->kind != PDFMAKE_DICT) return -1; 0 47 47
334  
335 47 ff2_k = pdfmake_arena_intern_name(arena, "FontFile2", 9);
336 47 ff3_k = pdfmake_arena_intern_name(arena, "FontFile3", 9);
337 47 ref = pdfmake_dict_get(descriptor, ff2_k);
338 47 ▶ if (!ref) ref = pdfmake_dict_get(descriptor, ff3_k);
Branch: 1 branch
BranchTrueFalseTotal
if (!ref) ref = pdfmake_dict_get(descriptor, ff3_k); 1 46 47
339 47 ▶ if (!ref || ref->kind != PDFMAKE_REF) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!ref || ref->kind != PDFMAKE_REF) return -1; 46 1 47
if (!ref || ref->kind != PDFMAKE_REF) return -1; 0 46 46
340  
341 46 return pdfmake_reader_resolve_stream(reader, ref->as.ref.num,
342 46 ref->as.ref.gen, out_buf) == PDFMAKE_OK
343 46 ▶ ? 0 : -1;
Branch: 1 branch
BranchTrueFalseTotal
? 0 : -1; 46 0 46
344 }
345  
346 /* Parse /CIDToGIDMap. For /Identity (or missing), returns NULL and sets
347 * is_identity=1. For a stream, returns a uint16_t[num_cids] array in arena. */
348 1 static uint16_t *resolve_cid_to_gid_map(
349 pdfmake_arena_t *arena,
350 struct pdfmake_reader *reader,
351 pdfmake_obj_t *cidfont_dict,
352 size_t *out_count,
353 int *out_is_identity)
354 {
355 uint32_t k;
356 pdfmake_obj_t *m;
357 pdfmake_buf_t buf;
358 size_t count;
359 uint16_t *map;
360 const uint8_t *d;
361 size_t i;
362  
363 1 *out_count = 0;
364 1 *out_is_identity = 0;
365  
366 1 k = pdfmake_arena_intern_name(arena, "CIDToGIDMap", 11);
367 1 m = pdfmake_dict_get(cidfont_dict, k);
368 1 ▶ if (!m) { *out_is_identity = 1; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!m) { *out_is_identity = 1; return NULL; } 0 1 1
369  
370 1 ▶ if (m->kind == PDFMAKE_NAME) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->kind == PDFMAKE_NAME) { 0 1 1
371 /* /Identity is the only named form */
372 0 *out_is_identity = 1;
373 0 return NULL;
374 }
375  
376 1 ▶ if (m->kind != PDFMAKE_REF) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (m->kind != PDFMAKE_REF) return NULL; 0 1 1
377  
378 1 ▶ if (pdfmake_buf_init(&buf) != PDFMAKE_OK) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_init(&buf) != PDFMAKE_OK) return NULL; 0 1 1
379 1 ▶ if (pdfmake_reader_resolve_stream(reader, m->as.ref.num, m->as.ref.gen, &buf)
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_reader_resolve_stream(reader, m->as.ref.num, m->as.ref.gen, &buf) 0 1 1
380 != PDFMAKE_OK) {
381 0 pdfmake_buf_free(&buf);
382 0 return NULL;
383 }
384  
385 /* Stream is a sequence of big-endian 2-byte GIDs indexed by CID */
386 1 count = pdfmake_buf_len(&buf) / 2;
387 1 ▶ if (count == 0) { pdfmake_buf_free(&buf); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (count == 0) { pdfmake_buf_free(&buf); return NULL; } 0 1 1
388  
389 1 map = pdfmake_arena_alloc(arena, count * sizeof(uint16_t));
390 1 ▶ if (!map) { pdfmake_buf_free(&buf); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!map) { pdfmake_buf_free(&buf); return NULL; } 0 1 1
391  
392 1 d = pdfmake_buf_data(&buf);
393 6 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 5 1 6
394 5 map[i] = ((uint16_t)d[i * 2] << 8) | d[i * 2 + 1];
395 }
396 1 pdfmake_buf_free(&buf);
397 1 *out_count = count;
398 1 return map;
399 }
400  
401 60 int pdfmake_font_widths_enhance_with_ttf(
402 pdfmake_arena_t *arena,
403 struct pdfmake_reader *reader,
404 pdfmake_obj_t *font_dict,
405 int is_cid,
406 const uint32_t *byte_to_unicode,
407 pdfmake_font_widths_t *out)
408 {
409 pdfmake_obj_t *descriptor_owner;
410 uint32_t fd_k;
411 pdfmake_obj_t *descriptor;
412 pdfmake_buf_t ttf_buf;
413 pdfmake_ttf_t *ttf;
414  
415 60 ▶ if (!arena || !reader || !font_dict || !out) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (!arena || !reader || !font_dict || !out) return -1; 60 0 60
if (!arena || !reader || !font_dict || !out) return -1; 60 0 60
if (!arena || !reader || !font_dict || !out) return -1; 60 0 60
if (!arena || !reader || !font_dict || !out) return -1; 0 60 60
416  
417 /* Find the font dict that owns the descriptor. For CID fonts, that's the
418 * descendant CIDFont, not the Type0 wrapper. */
419 60 descriptor_owner = font_dict;
420 60 ▶ if (is_cid) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_cid) { 11 49 60
421 11 uint32_t df_k = pdfmake_arena_intern_name(arena, "DescendantFonts", 15);
422 11 pdfmake_obj_t *df_arr = pdfmake_dict_get(font_dict, df_k);
423 22 ▶ if (df_arr && df_arr->kind == PDFMAKE_ARRAY &&
Branch: 1 branch
BranchTrueFalseTotal
if (df_arr && df_arr->kind == PDFMAKE_ARRAY && 11 0 11
424 11 pdfmake_array_len(df_arr) > 0) {
425 11 pdfmake_obj_t *cidfont = pdfmake_array_get(df_arr, 0);
426 11 ▶ if (cidfont && cidfont->kind == PDFMAKE_DICT) {
Branch: 2 branches
BranchTrueFalseTotal
if (cidfont && cidfont->kind == PDFMAKE_DICT) { 11 0 11
if (cidfont && cidfont->kind == PDFMAKE_DICT) { 11 0 11
427 11 descriptor_owner = cidfont;
428 }
429 }
430 }
431  
432 60 fd_k = pdfmake_arena_intern_name(arena, "FontDescriptor", 14);
433 60 descriptor = pdfmake_dict_get(descriptor_owner, fd_k);
434 60 ▶ if (!descriptor || descriptor->kind != PDFMAKE_DICT) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!descriptor || descriptor->kind != PDFMAKE_DICT) return -1; 47 13 60
if (!descriptor || descriptor->kind != PDFMAKE_DICT) return -1; 0 47 47
435  
436 47 ▶ if (pdfmake_buf_init(&ttf_buf) != PDFMAKE_OK) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_init(&ttf_buf) != PDFMAKE_OK) return -1; 0 47 47
437 47 ▶ if (fetch_font_file_stream(reader, arena, descriptor, &ttf_buf) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fetch_font_file_stream(reader, arena, descriptor, &ttf_buf) != 0) { 1 46 47
438 1 pdfmake_buf_free(&ttf_buf);
439 1 return -1;
440 }
441  
442 46 ttf = pdfmake_ttf_parse(arena,
443 pdfmake_buf_data(&ttf_buf),
444 pdfmake_buf_len(&ttf_buf));
445 /* Note: pdfmake_ttf_parse may reference the input buffer internally.
446 * We must keep ttf_buf alive for the lifetime of the TTF object. */
447 46 ▶ if (!ttf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ttf) { 0 46 46
448 0 pdfmake_buf_free(&ttf_buf);
449 0 return -1;
450 }
451  
452 /* Pull better ascent/descent from OS/2 if available */
453 46 ▶ if (ttf->has_os2 && ttf->units_per_em) {
Branch: 2 branches
BranchTrueFalseTotal
if (ttf->has_os2 && ttf->units_per_em) { 45 1 46
if (ttf->has_os2 && ttf->units_per_em) { 45 0 45
454 45 out->ascent = (int16_t)((int32_t)ttf->s_typo_ascender * 1000 / ttf->units_per_em);
455 45 out->descent = (int16_t)((int32_t)ttf->s_typo_descender * 1000 / ttf->units_per_em);
456 45 ▶ if (ttf->s_cap_height)
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->s_cap_height) 0 45 45
457 0 out->cap_height = (int16_t)((int32_t)ttf->s_cap_height * 1000 / ttf->units_per_em);
458 45 ▶ if (ttf->s_x_height)
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->s_x_height) 0 45 45
459 0 out->x_height = (int16_t)((int32_t)ttf->s_x_height * 1000 / ttf->units_per_em);
460 1 ▶ } else if (ttf->units_per_em) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ttf->units_per_em) { 1 0 1
461 1 out->ascent = (int16_t)((int32_t)ttf->ascender * 1000 / ttf->units_per_em);
462 1 out->descent = (int16_t)((int32_t)ttf->descender * 1000 / ttf->units_per_em);
463 }
464  
465 46 ▶ if (is_cid) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_cid) { 1 45 46
466 /* Build a range list: CID -> advance via /CIDToGIDMap + hmtx.
467 * Only populate CIDs that the map actually covers. */
468 1 size_t cid_count = 0;
469 1 int is_identity = 0;
470 uint16_t *cidmap;
471 size_t new_cap;
472 pdfmake_width_range_t *merged;
473 size_t out_count;
474 size_t cid;
475  
476 1 cidmap = resolve_cid_to_gid_map(arena, reader, descriptor_owner,
477 &cid_count, &is_identity);
478  
479 /* For Identity mapping, treat GID == CID up to num_glyphs. */
480 1 ▶ if (is_identity) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_identity) { 0 1 1
481 0 cid_count = ttf->num_glyphs;
482 }
483 1 ▶ if (!is_identity && !cidmap) {
Branch: 2 branches
BranchTrueFalseTotal
if (!is_identity && !cidmap) { 1 0 1
if (!is_identity && !cidmap) { 0 1 1
484 0 pdfmake_buf_free(&ttf_buf);
485 0 return 0; /* metrics updated, widths unchanged */
486 }
487  
488 /* Append TTF-derived widths to the existing range list, where they
489 * fill gaps. For simplicity, emit one range per CID — the lookup
490 * code binary-searches so O(log n) either way.
491 *
492 * Skip CIDs already covered by the PDF's /W array (PDF values win). */
493 1 new_cap = out->range_count + cid_count;
494 1 merged = pdfmake_arena_alloc(
495 arena, new_cap * sizeof(*merged));
496 1 ▶ if (!merged) { pdfmake_buf_free(&ttf_buf); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!merged) { pdfmake_buf_free(&ttf_buf); return 0; } 0 1 1
497  
498 1 ▶ if (out->ranges && out->range_count)
Branch: 2 branches
BranchTrueFalseTotal
if (out->ranges && out->range_count) 1 0 1
if (out->ranges && out->range_count) 1 0 1
499 1 memcpy(merged, out->ranges, out->range_count * sizeof(*merged));
500 1 out_count = out->range_count;
501  
502 6 ▶ for (cid = 0; cid < cid_count; cid++) {
Branch: 1 branch
BranchTrueFalseTotal
for (cid = 0; cid < cid_count; cid++) { 5 1 6
503 5 ▶ uint16_t gid = is_identity ? (uint16_t)cid : cidmap[cid];
Branch: 1 branch
BranchTrueFalseTotal
uint16_t gid = is_identity ? (uint16_t)cid : cidmap[cid]; 0 5 5
504 uint16_t w;
505 int covered;
506 size_t j;
507 5 ▶ if (gid == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (gid == 0) continue; 1 4 5
508 4 w = pdfmake_ttf_glyph_advance(ttf, gid);
509 4 ▶ if (w == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (w == 0) continue; 0 4 4
510  
511 /* Check if CID is already in existing ranges */
512 4 covered = 0;
513 14 ▶ for (j = 0; j < out->range_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < out->range_count; j++) { 14 0 14
514 14 ▶ if (cid >= out->ranges[j].lo && cid <= out->ranges[j].hi) {
Branch: 2 branches
BranchTrueFalseTotal
if (cid >= out->ranges[j].lo && cid <= out->ranges[j].hi) { 14 0 14
if (cid >= out->ranges[j].lo && cid <= out->ranges[j].hi) { 4 10 14
515 4 covered = 1;
516 4 break;
517 }
518 }
519 4 ▶ if (covered) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (covered) continue; 4 0 4
520  
521 0 merged[out_count].lo = (uint32_t)cid;
522 0 merged[out_count].hi = (uint32_t)cid;
523 0 merged[out_count].width = (int16_t)w;
524 0 out_count++;
525 }
526  
527 /* Sort merged ranges for binary search */
528 1 ▶ if (out_count > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (out_count > 1) { 1 0 1
529 1 qsort(merged, out_count, sizeof(*merged), range_cmp);
530 }
531 1 out->ranges = merged;
532 1 out->range_count = out_count;
533 } else {
534 /* Simple font: populate a 256-entry table indexed by byte code.
535 * charcode -> Unicode (from /Encoding) -> GID (from TTF cmap)
536 * -> advance (from hmtx)
537 *
538 * Only overwrite entries that are currently 0 (unset by /Widths) —
539 * PDF's declared /Widths array takes precedence. */
540 int16_t *table;
541 uint16_t first;
542 uint16_t last;
543 size_t tbl_len;
544 int code;
545 45 ▶ if (!byte_to_unicode) {
Branch: 1 branch
BranchTrueFalseTotal
if (!byte_to_unicode) { 0 45 45
546 0 pdfmake_buf_free(&ttf_buf);
547 0 return 0;
548 }
549  
550 45 table = out->table;
551 45 first = out->first_char;
552 45 last = out->last_char;
553 45 ▶ tbl_len = table ? (size_t)(last - first + 1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
tbl_len = table ? (size_t)(last - first + 1) : 0; 45 0 45
554  
555 /* If no existing table, allocate full 256-entry one */
556 45 ▶ if (!table) {
Branch: 1 branch
BranchTrueFalseTotal
if (!table) { 0 45 45
557 0 table = pdfmake_arena_alloc(arena, 256 * sizeof(int16_t));
558 0 ▶ if (!table) { pdfmake_buf_free(&ttf_buf); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!table) { pdfmake_buf_free(&ttf_buf); return 0; } 0 0 0
559 0 memset(table, 0, 256 * sizeof(int16_t));
560 0 first = 0;
561 0 last = 255;
562 0 tbl_len = 256;
563 45 ▶ } else if (first > 0 || last < 255) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (first > 0 || last < 255) { 0 45 45
} else if (first > 0 || last < 255) { 0 0 0
564 /* Expand to full 256-entry so we can cover the full byte range */
565 45 int16_t *expanded = pdfmake_arena_alloc(arena, 256 * sizeof(int16_t));
566 size_t i;
567 45 ▶ if (!expanded) { pdfmake_buf_free(&ttf_buf); return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!expanded) { pdfmake_buf_free(&ttf_buf); return 0; } 0 45 45
568 45 memset(expanded, 0, 256 * sizeof(int16_t));
569 2563 ▶ for (i = 0; i < tbl_len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tbl_len; i++) 2518 45 2563
570 2518 expanded[first + i] = table[i];
571 45 table = expanded;
572 45 first = 0;
573 45 last = 255;
574 45 tbl_len = 256;
575 }
576  
577 /* Fill gaps from TTF */
578 11565 ▶ for (code = 0; code < 256; code++) {
Branch: 1 branch
BranchTrueFalseTotal
for (code = 0; code < 256; code++) { 11520 45 11565
579 uint32_t uni;
580 uint16_t gid;
581 uint16_t w;
582 11520 ▶ if (table[code] != 0) continue; /* honour PDF /Widths */
Branch: 1 branch
BranchTrueFalseTotal
if (table[code] != 0) continue; /* honour PDF /Widths */ 1001 10519 11520
583 10519 uni = byte_to_unicode[code];
584 10519 ▶ if (uni == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (uni == 0) continue; 4812 5707 10519
585 5707 gid = pdfmake_ttf_cmap_lookup(ttf, uni);
586 5707 ▶ if (gid == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (gid == 0) continue; 5707 0 5707
587 0 w = pdfmake_ttf_glyph_advance(ttf, gid);
588 0 ▶ if (w == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (w == 0) continue; 0 0 0
589 0 table[code] = (int16_t)w;
590 }
591  
592 45 out->table = table;
593 45 out->first_char = first;
594 45 out->last_char = last;
595 }
596  
597 /* NB: ttf_buf backs the ttf->data pointer. Keeping it leaked-into-arena
598 * isn't possible (buf uses malloc), but the TTF parse copied nothing.
599 * For the simple font path we've already consumed all data via lookups,
600 * so freeing here is safe.
601 *
602 * Actually: pdfmake_ttf_cmap_lookup walks ttf->data, so for the CID path
603 * we might have read partial data if lookups happen after this function
604 * returns. We therefore arena-copy the buffer first.
605 *
606 * Currently all TTF reads happen inside this function, so freeing is
607 * safe. If that changes, hoist the memcpy-to-arena into parse above. */
608 46 pdfmake_buf_free(&ttf_buf);
609 46 return 0;
610 }
« src/pdfmake_types.c ↑ Summary xs/form.xs »