src/pdfmake_ttf.c

filter: Statementstmt 75.5% 164 / 217 Branchbran 42.2% 76 / 180 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 60.4% 240 / 397 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/pdfmake_linear.c ↑ Summary src/pdfmake_content.c »
1 /*
2 * pdfmake_ttf.c - TrueType font parser implementation
3 *
4 * Parses TrueType/OpenType fonts, extracts metrics, provides cmap lookup,
5 * and supports subsetting for PDF embedding.
6 *
7 * Reference: OpenType specification (Microsoft), TrueType reference (Apple)
8 */
9  
10 #include "pdfmake_font.h"
11 #include "pdfmake_arena.h"
12 #include "pdfmake_internal.h" /* pdfmake_read_be16 / be32 / sbe16 */
13 #include <string.h>
14 #include <stdlib.h>
15  
16 /*============================================================================
17 * TTF table tags
18 *==========================================================================*/
19  
20 #define TAG(a,b,c,d) (((uint32_t)(a)<<24)|((uint32_t)(b)<<16)|((uint32_t)(c)<<8)|(d))
21  
22 #define TAG_CMAP TAG('c','m','a','p')
23 #define TAG_GLYF TAG('g','l','y','f')
24 #define TAG_HEAD TAG('h','e','a','d')
25 #define TAG_HHEA TAG('h','h','e','a')
26 #define TAG_HMTX TAG('h','m','t','x')
27 #define TAG_LOCA TAG('l','o','c','a')
28 #define TAG_MAXP TAG('m','a','x','p')
29 #define TAG_NAME TAG('n','a','m','e')
30 #define TAG_OS2 TAG('O','S','/','2')
31 #define TAG_POST TAG('p','o','s','t')
32 #define TAG_CVT TAG('c','v','t',' ')
33 #define TAG_FPGM TAG('f','p','g','m')
34 #define TAG_PREP TAG('p','r','e','p')
35  
36 /*============================================================================
37 * TTF parsing - table directory
38 *==========================================================================*/
39  
40 52 static int ttf_locate_table(const uint8_t *data, size_t data_len,
41 uint16_t num_tables, uint32_t tag,
42 uint32_t *offset, uint32_t *length) {
43 const uint8_t *p;
44 uint16_t i;
45 (void)data_len;
46 52 p = data + 12; /* Skip offset table header */
47
48 340 ▶ for (i = 0; i < num_tables; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_tables; i++) { 324 16 340
49 324 uint32_t t = pdfmake_read_be32(p);
50 324 ▶ if (t == tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (t == tag) { 36 288 324
51 36 *offset = pdfmake_read_be32(p + 8);
52 36 *length = pdfmake_read_be32(p + 12);
53 36 return 1;
54 }
55 288 p += 16;
56 }
57 16 return 0;
58 }
59  
60 /*============================================================================
61 * TTF parsing main entry
62 *==========================================================================*/
63  
64 4 pdfmake_ttf_t *pdfmake_ttf_parse(pdfmake_arena_t *arena,
65 const uint8_t *data, size_t len) {
66 uint32_t sfnt_version;
67 uint16_t num_tables;
68 pdfmake_ttf_t *ttf;
69 size_t bitmap_size;
70  
71 4 ▶ if (!arena || !data || len < 12) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!arena || !data || len < 12) return NULL; 4 0 4
if (!arena || !data || len < 12) return NULL; 4 0 4
if (!arena || !data || len < 12) return NULL; 0 4 4
72
73 4 sfnt_version = pdfmake_read_be32(data);
74
75 /* Check for valid TrueType signature */
76 4 ▶ if (sfnt_version != 0x00010000 && sfnt_version != TAG('t','r','u','e') &&
Branch: 3 branches
BranchTrueFalseTotal
if (sfnt_version != 0x00010000 && sfnt_version != TAG('t','r','u','e') && 0 4 4
if (sfnt_version != 0x00010000 && sfnt_version != TAG('t','r','u','e') && 0 0 0
if (sfnt_version != 0x00010000 && sfnt_version != TAG('t','r','u','e') && 0 0 0
77 sfnt_version != TAG('O','T','T','O')) {
78 0 return NULL;
79 }
80
81 4 num_tables = pdfmake_read_be16(data + 4);
82 4 ▶ if (len < 12 + num_tables * 16) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 12 + num_tables * 16) return NULL; 0 4 4
83
84 4 ttf = pdfmake_arena_alloc(arena, sizeof(pdfmake_ttf_t));
85 4 ▶ if (!ttf) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!ttf) return NULL; 0 4 4
86 4 memset(ttf, 0, sizeof(pdfmake_ttf_t));
87
88 4 ttf->data = data;
89 4 ttf->data_len = len;
90
91 /* Locate tables */
92 #define LOCATE(member, tag_val) \
93 ttf_locate_table(data, len, num_tables, tag_val, &ttf->member.offset, &ttf->member.length)
94
95 4 LOCATE(head, TAG_HEAD);
96 4 LOCATE(hhea, TAG_HHEA);
97 4 LOCATE(hmtx, TAG_HMTX);
98 4 LOCATE(maxp, TAG_MAXP);
99 4 LOCATE(cmap, TAG_CMAP);
100 4 LOCATE(glyf, TAG_GLYF);
101 4 LOCATE(loca, TAG_LOCA);
102 4 LOCATE(name, TAG_NAME);
103 4 LOCATE(post, TAG_POST);
104 4 LOCATE(os2, TAG_OS2);
105 4 LOCATE(cvt, TAG_CVT);
106 4 LOCATE(fpgm, TAG_FPGM);
107 4 LOCATE(prep, TAG_PREP);
108
109 #undef LOCATE
110
111 /* Parse head */
112 4 ▶ if (ttf->head.length >= 54) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->head.length >= 54) { 4 0 4
113 4 const uint8_t *p = data + ttf->head.offset;
114 4 ttf->units_per_em = pdfmake_read_be16(p + 18);
115 4 ttf->x_min = pdfmake_read_sbe16(p + 36);
116 4 ttf->y_min = pdfmake_read_sbe16(p + 38);
117 4 ttf->x_max = pdfmake_read_sbe16(p + 40);
118 4 ttf->y_max = pdfmake_read_sbe16(p + 42);
119 4 ttf->mac_style = pdfmake_read_sbe16(p + 44);
120 4 ttf->index_to_loc_format = pdfmake_read_sbe16(p + 50);
121 }
122
123 /* Parse maxp */
124 4 ▶ if (ttf->maxp.length >= 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->maxp.length >= 6) { 4 0 4
125 4 ttf->num_glyphs = pdfmake_read_be16(data + ttf->maxp.offset + 4);
126 }
127
128 /* Parse hhea */
129 4 ▶ if (ttf->hhea.length >= 36) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->hhea.length >= 36) { 4 0 4
130 4 const uint8_t *p = data + ttf->hhea.offset;
131 4 ttf->ascender = pdfmake_read_sbe16(p + 4);
132 4 ttf->descender = pdfmake_read_sbe16(p + 6);
133 4 ttf->line_gap = pdfmake_read_sbe16(p + 8);
134 4 ttf->num_h_metrics = pdfmake_read_be16(p + 34);
135 }
136
137 /* Parse OS/2 if present */
138 4 ▶ if (ttf->os2.length >= 78) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->os2.length >= 78) { 0 4 4
139 0 const uint8_t *p = data + ttf->os2.offset;
140 0 ttf->has_os2 = 1;
141 0 ttf->us_weight_class = pdfmake_read_be16(p + 4);
142 0 ttf->us_width_class = pdfmake_read_be16(p + 6);
143 0 ttf->fs_selection = pdfmake_read_be16(p + 62);
144
145 0 ▶ if (ttf->os2.length >= 72) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->os2.length >= 72) { 0 0 0
146 0 ttf->s_typo_ascender = pdfmake_read_sbe16(p + 68);
147 0 ttf->s_typo_descender = pdfmake_read_sbe16(p + 70);
148 0 ttf->s_typo_line_gap = pdfmake_read_sbe16(p + 72);
149 }
150 0 ▶ if (ttf->os2.length >= 90) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->os2.length >= 90) { 0 0 0
151 0 ttf->s_x_height = pdfmake_read_sbe16(p + 86);
152 0 ttf->s_cap_height = pdfmake_read_sbe16(p + 88);
153 }
154 }
155
156 /* Find best cmap subtable */
157 4 ▶ if (ttf->cmap.length >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->cmap.length >= 4) { 4 0 4
158 4 const uint8_t *p = data + ttf->cmap.offset;
159 4 uint16_t num_subtables = pdfmake_read_be16(p + 2);
160 uint16_t i;
161
162 4 p += 4;
163 8 ▶ for (i = 0; i < num_subtables && ttf->cmap.offset + 4 + i*8 + 8 <= len; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < num_subtables && ttf->cmap.offset + 4 + i*8 + 8 <= len; i++) { 4 4 8
for (i = 0; i < num_subtables && ttf->cmap.offset + 4 + i*8 + 8 <= len; i++) { 4 0 4
164 4 uint16_t platform = pdfmake_read_be16(p);
165 4 uint16_t encoding = pdfmake_read_be16(p + 2);
166 4 uint32_t subtable_offset = pdfmake_read_be32(p + 4);
167
168 4 ▶ if (ttf->cmap.offset + subtable_offset + 2 <= len) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->cmap.offset + subtable_offset + 2 <= len) { 4 0 4
169 4 uint16_t format = pdfmake_read_be16(data + ttf->cmap.offset + subtable_offset);
170
171 /* Prefer format 12 (full Unicode) then format 4 (BMP) */
172 4 ▶ if (format == 12 && ((platform == 3 && encoding == 10) ||
Branch: 4 branches
BranchTrueFalseTotal
if (format == 12 && ((platform == 3 && encoding == 10) || 0 4 4
if (format == 12 && ((platform == 3 && encoding == 10) || 0 0 0
if (format == 12 && ((platform == 3 && encoding == 10) || 0 0 0
if (format == 12 && ((platform == 3 && encoding == 10) || 0 0 0
173 0 ▶ (platform == 0 && encoding >= 3))) {
Branch: 1 branch
BranchTrueFalseTotal
(platform == 0 && encoding >= 3))) { 0 0 0
174 0 ttf->cmap_format = 12;
175 0 ttf->cmap_offset = ttf->cmap.offset + subtable_offset;
176 4 ▶ } else if (format == 4 && ttf->cmap_format != 12 &&
Branch: 3 branches
BranchTrueFalseTotal
} else if (format == 4 && ttf->cmap_format != 12 && 4 0 4
} else if (format == 4 && ttf->cmap_format != 12 && 4 0 4
} else if (format == 4 && ttf->cmap_format != 12 && 4 0 4
177 4 ▶ ((platform == 3 && encoding == 1) ||
Branch: 2 branches
BranchTrueFalseTotal
((platform == 3 && encoding == 1) || 0 4 4
((platform == 3 && encoding == 1) || 0 0 0
178 0 ▶ (platform == 0 && encoding <= 3))) {
Branch: 1 branch
BranchTrueFalseTotal
(platform == 0 && encoding <= 3))) { 0 0 0
179 4 ttf->cmap_format = 4;
180 4 ttf->cmap_offset = ttf->cmap.offset + subtable_offset;
181 }
182 }
183 4 p += 8;
184 }
185 }
186
187 /* Allocate used glyph bitmap */
188 4 bitmap_size = (ttf->num_glyphs + 7) / 8;
189 4 ttf->used_glyphs = pdfmake_arena_alloc(arena, bitmap_size);
190 4 ▶ if (ttf->used_glyphs) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->used_glyphs) { 4 0 4
191 4 memset(ttf->used_glyphs, 0, bitmap_size);
192 }
193 4 ttf->used_count = 0;
194
195 4 return ttf;
196 }
197  
198 /*============================================================================
199 * cmap format 4 decoder (BMP)
200 *==========================================================================*/
201  
202 131 static uint16_t cmap_format4_lookup(const pdfmake_ttf_t *ttf, uint32_t codepoint) {
203 const uint8_t *p;
204 uint16_t length, seg_count_x2, seg_count;
205 const uint8_t *end_codes, *start_codes, *id_deltas, *id_range_offsets;
206 uint16_t lo, hi;
207 uint16_t end_code, start_code;
208 int16_t id_delta;
209 uint16_t id_range_offset;
210 uint16_t glyph_id;
211 131 ▶ if (codepoint > 0xFFFF) return 0; /* Format 4 only handles BMP */
Branch: 1 branch
BranchTrueFalseTotal
if (codepoint > 0xFFFF) return 0; /* Format 4 only handles BMP */ 0 131 131
212
213 131 p = ttf->data + ttf->cmap_offset;
214
215 131 length = pdfmake_read_be16(p + 2);
216 131 seg_count_x2 = pdfmake_read_be16(p + 6);
217 131 seg_count = seg_count_x2 / 2;
218
219 131 ▶ if (ttf->cmap_offset + length > ttf->data_len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->cmap_offset + length > ttf->data_len) return 0; 0 131 131
220
221 131 end_codes = p + 14;
222 131 start_codes = end_codes + seg_count_x2 + 2;
223 131 id_deltas = start_codes + seg_count_x2;
224 131 id_range_offsets = id_deltas + seg_count_x2;
225
226 /* Binary search for segment */
227 131 lo = 0; hi = seg_count;
228 393 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 262 131 393
229 262 uint16_t mid = lo + (hi - lo) / 2;
230 262 uint16_t mid_end = pdfmake_read_be16(end_codes + mid * 2);
231 262 ▶ if (mid_end < codepoint) {
Branch: 1 branch
BranchTrueFalseTotal
if (mid_end < codepoint) { 0 262 262
232 0 lo = mid + 1;
233 } else {
234 262 hi = mid;
235 }
236 }
237
238 131 ▶ if (lo >= seg_count) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (lo >= seg_count) return 0; 0 131 131
239
240 131 end_code = pdfmake_read_be16(end_codes + lo * 2);
241 131 start_code = pdfmake_read_be16(start_codes + lo * 2);
242
243 131 ▶ if (codepoint < start_code || codepoint > end_code) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (codepoint < start_code || codepoint > end_code) return 0; 28 103 131
if (codepoint < start_code || codepoint > end_code) return 0; 0 28 28
244
245 28 id_delta = pdfmake_read_sbe16(id_deltas + lo * 2);
246 28 id_range_offset = pdfmake_read_be16(id_range_offsets + lo * 2);
247
248 28 ▶ if (id_range_offset == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (id_range_offset == 0) { 28 0 28
249 28 glyph_id = (uint16_t)((codepoint + id_delta) & 0xFFFF);
250 } else {
251 0 const uint8_t *glyph_addr = id_range_offsets + lo * 2 + id_range_offset +
252 0 (codepoint - start_code) * 2;
253 0 ▶ if (glyph_addr >= ttf->data + ttf->data_len - 1) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (glyph_addr >= ttf->data + ttf->data_len - 1) return 0; 0 0 0
254 0 glyph_id = pdfmake_read_be16(glyph_addr);
255 0 ▶ if (glyph_id != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (glyph_id != 0) { 0 0 0
256 0 glyph_id = (uint16_t)((glyph_id + id_delta) & 0xFFFF);
257 }
258 }
259
260 28 return glyph_id;
261 }
262  
263 /*============================================================================
264 * cmap format 12 decoder (full Unicode)
265 *==========================================================================*/
266  
267 0 static uint16_t cmap_format12_lookup(const pdfmake_ttf_t *ttf, uint32_t codepoint) {
268 0 const uint8_t *p = ttf->data + ttf->cmap_offset;
269 const uint8_t *groups;
270 uint32_t length, num_groups;
271 uint32_t lo, hi;
272 uint32_t start_code, end_code, start_glyph;
273
274 0 length = pdfmake_read_be32(p + 4);
275 0 num_groups = pdfmake_read_be32(p + 12);
276
277 0 ▶ if (ttf->cmap_offset + length > ttf->data_len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->cmap_offset + length > ttf->data_len) return 0; 0 0 0
278 0 ▶ if (length < 16 + num_groups * 12) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (length < 16 + num_groups * 12) return 0; 0 0 0
279
280 0 groups = p + 16;
281
282 /* Binary search */
283 0 lo = 0; hi = num_groups;
284 0 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 0 0 0
285 0 uint32_t mid = lo + (hi - lo) / 2;
286 0 uint32_t mid_end = pdfmake_read_be32(groups + mid * 12 + 4);
287 0 ▶ if (mid_end < codepoint) {
Branch: 1 branch
BranchTrueFalseTotal
if (mid_end < codepoint) { 0 0 0
288 0 lo = mid + 1;
289 } else {
290 0 hi = mid;
291 }
292 }
293
294 0 ▶ if (lo >= num_groups) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (lo >= num_groups) return 0; 0 0 0
295
296 0 start_code = pdfmake_read_be32(groups + lo * 12);
297 0 end_code = pdfmake_read_be32(groups + lo * 12 + 4);
298 0 start_glyph = pdfmake_read_be32(groups + lo * 12 + 8);
299
300 0 ▶ if (codepoint < start_code || codepoint > end_code) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (codepoint < start_code || codepoint > end_code) return 0; 0 0 0
if (codepoint < start_code || codepoint > end_code) return 0; 0 0 0
301
302 0 return (uint16_t)(start_glyph + (codepoint - start_code));
303 }
304  
305 /*============================================================================
306 * cmap lookup (public API)
307 *==========================================================================*/
308  
309 131 uint16_t pdfmake_ttf_cmap_lookup(const pdfmake_ttf_t *ttf, uint32_t codepoint) {
310 131 ▶ if (!ttf || ttf->cmap_offset == 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!ttf || ttf->cmap_offset == 0) return 0; 131 0 131
if (!ttf || ttf->cmap_offset == 0) return 0; 0 131 131
311
312 131 ▶ if (ttf->cmap_format == 12) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->cmap_format == 12) { 0 131 131
313 0 return cmap_format12_lookup(ttf, codepoint);
314 131 ▶ } else if (ttf->cmap_format == 4) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ttf->cmap_format == 4) { 131 0 131
315 131 return cmap_format4_lookup(ttf, codepoint);
316 }
317
318 0 return 0;
319 }
320  
321 /*============================================================================
322 * Glyph advance width lookup
323 *==========================================================================*/
324  
325 27 uint16_t pdfmake_ttf_glyph_advance(const pdfmake_ttf_t *ttf, uint16_t glyph_id) {
326 const uint8_t *hmtx;
327 uint16_t advance;
328 27 ▶ if (!ttf || glyph_id >= ttf->num_glyphs) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!ttf || glyph_id >= ttf->num_glyphs) return 0; 27 0 27
if (!ttf || glyph_id >= ttf->num_glyphs) return 0; 0 27 27
329 27 ▶ if (ttf->hmtx.offset == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->hmtx.offset == 0) return 0; 0 27 27
330
331 27 hmtx = ttf->data + ttf->hmtx.offset;
332
333 27 ▶ if (glyph_id < ttf->num_h_metrics) {
Branch: 1 branch
BranchTrueFalseTotal
if (glyph_id < ttf->num_h_metrics) { 27 0 27
334 27 advance = pdfmake_read_be16(hmtx + glyph_id * 4);
335 } else {
336 0 advance = pdfmake_read_be16(hmtx + (ttf->num_h_metrics - 1) * 4);
337 }
338
339 /* Convert from font units to 1/1000 em */
340 27 ▶ if (ttf->units_per_em == 0) return advance;
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->units_per_em == 0) return advance; 0 27 27
341 27 return (advance * 1000) / ttf->units_per_em;
342 }
343  
344 /*============================================================================
345 * PostScript name (name table, nameID 6)
346 *
347 * The /BaseFont of an embedded font. Windows records it UTF-16BE and Mac
348 * ASCII; either way the PostScript name is restricted to printable ASCII
349 * minus the ten PostScript delimiters, so the high byte of a Windows record
350 * is dropped and anything outside that set is rejected rather than copied
351 * into a /Name where it would need escaping.
352 *==========================================================================*/
353  
354 #define TTF_PS_NAME_MAX 63
355  
356 40 static int ps_name_char_ok(uint8_t c) {
357 40 ▶ if (c < 33 || c > 126) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c < 33 || c > 126) return 0; 40 0 40
if (c < 33 || c > 126) return 0; 0 40 40
358 40 ▶ switch (c) {
Branch: 1 branch
BranchTrueFalseTotal
switch (c) { 0 40 40
359 0 case '(': case ')': case '<': case '>': case '[': case ']':
360 case '{': case '}': case '/': case '%': case '#':
361 0 return 0;
362 40 default:
363 40 return 1;
364 }
365 }
366  
367 4 const char *pdfmake_ttf_postscript_name(const pdfmake_ttf_t *ttf,
368 pdfmake_arena_t *arena) {
369 const uint8_t *tbl;
370 const uint8_t *recs;
371 uint16_t count;
372 uint16_t string_off;
373 uint16_t i;
374 char buf[TTF_PS_NAME_MAX + 1];
375 char best[TTF_PS_NAME_MAX + 1];
376 4 int best_score = -1;
377  
378 4 ▶ if (!ttf || !arena || !ttf->data) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!ttf || !arena || !ttf->data) return NULL; 4 0 4
if (!ttf || !arena || !ttf->data) return NULL; 4 0 4
if (!ttf || !arena || !ttf->data) return NULL; 0 4 4
379 4 ▶ if (ttf->name.length < 6) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->name.length < 6) return NULL; 0 4 4
380 4 ▶ if (ttf->name.offset > ttf->data_len) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->name.offset > ttf->data_len) return NULL; 0 4 4
381 4 ▶ if (ttf->name.length > ttf->data_len - ttf->name.offset) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->name.length > ttf->data_len - ttf->name.offset) return NULL; 0 4 4
382  
383 4 tbl = ttf->data + ttf->name.offset;
384 4 count = pdfmake_read_be16(tbl + 2);
385 4 string_off = pdfmake_read_be16(tbl + 4);
386 4 recs = tbl + 6;
387  
388 /* Each record is 12 bytes; stop short rather than walk off the table. */
389 4 ▶ if ((size_t)count * 12 + 6 > ttf->name.length) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)count * 12 + 6 > ttf->name.length) { 0 4 4
390 0 count = (uint16_t)((ttf->name.length - 6) / 12);
391 }
392  
393 4 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 4 0 4
394 4 const uint8_t *r = recs + (size_t)i * 12;
395 4 uint16_t platform = pdfmake_read_be16(r);
396 4 uint16_t name_id = pdfmake_read_be16(r + 6);
397 4 uint16_t len = pdfmake_read_be16(r + 8);
398 4 uint16_t off = pdfmake_read_be16(r + 10);
399 const uint8_t *s;
400 int wide;
401 int score;
402 size_t step;
403 size_t n;
404 size_t out;
405  
406 4 ▶ if (name_id != 6) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (name_id != 6) continue; 0 4 4
407 /* 3 = Windows (UTF-16BE), 0 = Unicode (UTF-16BE), 1 = Mac (ASCII) */
408 4 ▶ if (platform == 3) { wide = 1; score = 2; }
Branch: 1 branch
BranchTrueFalseTotal
if (platform == 3) { wide = 1; score = 2; } 4 0 4
409 0 ▶ else if (platform == 1) { wide = 0; score = 1; }
Branch: 1 branch
BranchTrueFalseTotal
else if (platform == 1) { wide = 0; score = 1; } 0 0 0
410 0 ▶ else if (platform == 0) { wide = 1; score = 0; }
Branch: 1 branch
BranchTrueFalseTotal
else if (platform == 0) { wide = 1; score = 0; } 0 0 0
411 0 else continue;
412 4 ▶ if (score <= best_score) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (score <= best_score) continue; 0 4 4
413  
414 /* Offsets are relative to the table's string storage. */
415 4 ▶ if ((size_t)string_off + off + len > ttf->name.length) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)string_off + off + len > ttf->name.length) continue; 0 4 4
416 4 s = tbl + string_off + off;
417  
418 4 ▶ step = wide ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
step = wide ? 2 : 1; 4 0 4
419 4 ▶ if (len < step) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (len < step) continue; 0 4 4
420 4 n = len / step;
421 4 ▶ if (n > TTF_PS_NAME_MAX) n = TTF_PS_NAME_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > TTF_PS_NAME_MAX) n = TTF_PS_NAME_MAX; 0 4 4
422  
423 44 ▶ for (out = 0; out < n; out++) {
Branch: 1 branch
BranchTrueFalseTotal
for (out = 0; out < n; out++) { 40 4 44
424 40 const uint8_t *cp = s + out * step;
425 40 ▶ if (wide && cp[0] != 0) break; /* not Latin-1: give up */
Branch: 2 branches
BranchTrueFalseTotal
if (wide && cp[0] != 0) break; /* not Latin-1: give up */ 40 0 40
if (wide && cp[0] != 0) break; /* not Latin-1: give up */ 0 40 40
426 40 ▶ if (!ps_name_char_ok(cp[wide])) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!ps_name_char_ok(cp[wide])) break; 0 40 40
427 40 buf[out] = (char)cp[wide];
428 }
429 4 ▶ if (out == 0 || out != n) continue; /* partial name is no name */
Branch: 2 branches
BranchTrueFalseTotal
if (out == 0 || out != n) continue; /* partial name is no name */ 4 0 4
if (out == 0 || out != n) continue; /* partial name is no name */ 0 4 4
430  
431 4 buf[out] = '\0';
432 4 memcpy(best, buf, out + 1);
433 4 best_score = score;
434 4 ▶ if (score == 2) break; /* Windows record wins */
Branch: 1 branch
BranchTrueFalseTotal
if (score == 2) break; /* Windows record wins */ 4 0 4
435 }
436  
437 4 ▶ if (best_score < 0) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (best_score < 0) return NULL; 0 4 4
438 4 return pdfmake_arena_strdup(arena, best);
439 }
440  
441 /*============================================================================
442 * Mark glyph as used (for subsetting)
443 *==========================================================================*/
444  
445 11 void pdfmake_ttf_mark_glyph(pdfmake_ttf_t *ttf, uint16_t glyph_id) {
446 size_t byte;
447 uint8_t bit;
448 11 ▶ if (!ttf || !ttf->used_glyphs) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!ttf || !ttf->used_glyphs) return; 11 0 11
if (!ttf || !ttf->used_glyphs) return; 0 11 11
449 11 ▶ if (glyph_id >= ttf->num_glyphs) return;
Branch: 1 branch
BranchTrueFalseTotal
if (glyph_id >= ttf->num_glyphs) return; 0 11 11
450
451 11 byte = glyph_id / 8;
452 11 bit = 1 << (glyph_id % 8);
453
454 11 ▶ if (!(ttf->used_glyphs[byte] & bit)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(ttf->used_glyphs[byte] & bit)) { 10 1 11
455 10 ttf->used_glyphs[byte] |= bit;
456 10 ttf->used_count++;
457 }
458 }
« src/pdfmake_linear.c ↑ Summary src/pdfmake_content.c »