src/pdfmake_ttf_subset.c

filter: Statementstmt 87.6% 241 / 275 Branchbran 60.1% 77 / 128 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.9% 318 / 403 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/pdfmake_redact_rewrite.c ↑ Summary xs/markup_style.xs »
1 /*
2 * pdfmake_ttf_subset.c - TrueType font subsetter
3 *
4 * Creates a minimal TrueType font containing only the glyphs used.
5 * Required tables: head, hhea, maxp, loca, glyf, hmtx, cmap, post
6 *
7 * Reference: OpenType/TrueType specification
8 */
9  
10 #include "pdfmake_font.h"
11 #include "pdfmake_internal.h"
12 #include <string.h>
13 #include <stdlib.h>
14  
15 /*============================================================================
16 * Table tag helpers
17 *==========================================================================*/
18  
19 #define TAG(a,b,c,d) (((uint32_t)(a)<<24)|((uint32_t)(b)<<16)|((uint32_t)(c)<<8)|(d))
20  
21 /*============================================================================
22 * Calculate TTF table checksum
23 *==========================================================================*/
24  
25 27 static uint32_t ttf_checksum(const uint8_t *data, size_t len) {
26 27 uint32_t sum = 0;
27 27 size_t nwords = (len + 3) / 4;
28 size_t i;
29
30 586 ▶ for (i = 0; i < nwords; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nwords; i++) { 559 27 586
31 559 uint32_t word = 0;
32 559 size_t offset = i * 4;
33
34 559 ▶ if (offset < len) word |= (uint32_t)data[offset] << 24;
Branch: 1 branch
BranchTrueFalseTotal
if (offset < len) word |= (uint32_t)data[offset] << 24; 559 0 559
35 559 ▶ if (offset + 1 < len) word |= (uint32_t)data[offset + 1] << 16;
Branch: 1 branch
BranchTrueFalseTotal
if (offset + 1 < len) word |= (uint32_t)data[offset + 1] << 16; 559 0 559
36 559 ▶ if (offset + 2 < len) word |= (uint32_t)data[offset + 2] << 8;
Branch: 1 branch
BranchTrueFalseTotal
if (offset + 2 < len) word |= (uint32_t)data[offset + 2] << 8; 551 8 559
37 559 ▶ if (offset + 3 < len) word |= (uint32_t)data[offset + 3];
Branch: 1 branch
BranchTrueFalseTotal
if (offset + 3 < len) word |= (uint32_t)data[offset + 3]; 551 8 559
38
39 559 sum += word;
40 }
41
42 27 return sum;
43 }
44  
45 /*============================================================================
46 * Build glyph remapping table
47 *==========================================================================*/
48  
49 typedef struct {
50 uint16_t *old_to_new; /* old_glyph_id -> new_glyph_id */
51 uint16_t *new_to_old; /* new_glyph_id -> old_glyph_id */
52 uint16_t new_count;
53 } glyph_map_t;
54  
55 6 static glyph_map_t *build_glyph_map(pdfmake_ttf_t *ttf) {
56 6 glyph_map_t *map = calloc(1, sizeof(glyph_map_t));
57 6 ▶ if (!map) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!map) return NULL; 0 6 6
58
59 6 map->old_to_new = calloc(ttf->num_glyphs, sizeof(uint16_t));
60 6 map->new_to_old = calloc(ttf->num_glyphs, sizeof(uint16_t));
61 6 ▶ if (!map->old_to_new || !map->new_to_old) {
Branch: 2 branches
BranchTrueFalseTotal
if (!map->old_to_new || !map->new_to_old) { 6 0 6
if (!map->old_to_new || !map->new_to_old) { 0 6 6
62 0 free(map->old_to_new);
63 0 free(map->new_to_old);
64 0 free(map);
65 0 return NULL;
66 }
67
68 /* Always include glyph 0 (.notdef) */
69 {
70 int i;
71 6 map->old_to_new[0] = 0;
72 6 map->new_to_old[0] = 0;
73 6 map->new_count = 1;
74
75 /* Add used glyphs in order */
76 30 ▶ for (i = 1; i < ttf->num_glyphs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < ttf->num_glyphs; i++) { 24 6 30
77 24 size_t byte = i / 8;
78 24 uint8_t bit = 1 << (i % 8);
79
80 24 ▶ if (ttf->used_glyphs[byte] & bit) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->used_glyphs[byte] & bit) { 14 10 24
81 14 map->old_to_new[i] = map->new_count;
82 14 map->new_to_old[map->new_count] = i;
83 14 map->new_count++;
84 }
85 }
86 }
87
88 6 return map;
89 }
90  
91 6 static void free_glyph_map(glyph_map_t *map) {
92 6 ▶ if (!map) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!map) return; 0 6 6
93 6 free(map->old_to_new);
94 6 free(map->new_to_old);
95 6 free(map);
96 }
97  
98 /*============================================================================
99 * Get glyph data location from loca table
100 *==========================================================================*/
101  
102 20 static int get_glyph_location(pdfmake_ttf_t *ttf, uint16_t glyph_id,
103 uint32_t *offset, uint32_t *length) {
104 const uint8_t *loca;
105 uint32_t off1, off2;
106
107 20 ▶ if (glyph_id >= ttf->num_glyphs) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (glyph_id >= ttf->num_glyphs) return 0; 0 20 20
108
109 20 loca = ttf->data + ttf->loca.offset;
110
111 20 ▶ if (ttf->index_to_loc_format == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->index_to_loc_format == 0) { 20 0 20
112 /* Short format: offsets are words, multiply by 2 */
113 20 off1 = pdfmake_read_be16(loca + glyph_id * 2) * 2;
114 20 off2 = pdfmake_read_be16(loca + (glyph_id + 1) * 2) * 2;
115 } else {
116 /* Long format: offsets are dwords */
117 0 off1 = pdfmake_read_be32(loca + glyph_id * 4);
118 0 off2 = pdfmake_read_be32(loca + (glyph_id + 1) * 4);
119 }
120
121 20 *offset = off1;
122 20 *length = off2 - off1;
123 20 return 1;
124 }
125  
126 /*============================================================================
127 * Build subset glyf table
128 *==========================================================================*/
129  
130 typedef struct {
131 uint8_t *data;
132 size_t len;
133 uint32_t *offsets; /* num_glyphs + 1 offsets for loca */
134 } glyf_result_t;
135  
136 3 static glyf_result_t *build_subset_glyf(pdfmake_ttf_t *ttf, glyph_map_t *map) {
137 3 glyf_result_t *result = calloc(1, sizeof(glyf_result_t));
138 3 ▶ if (!result) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return NULL; 0 3 3
139
140 3 result->offsets = calloc(map->new_count + 1, sizeof(uint32_t));
141 3 ▶ if (!result->offsets) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result->offsets) { 0 3 3
142 0 free(result);
143 0 return NULL;
144 }
145
146 /* First pass: calculate total size */
147 {
148 3 size_t total = 0;
149 uint16_t i;
150 13 ▶ for (i = 0; i < map->new_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < map->new_count; i++) { 10 3 13
151 10 uint16_t old_id = map->new_to_old[i];
152 uint32_t off, len;
153 10 ▶ if (get_glyph_location(ttf, old_id, &off, &len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_glyph_location(ttf, old_id, &off, &len)) { 10 0 10
154 /* Align to 2 bytes (short loca format) or 4 bytes (long) */
155 10 total += len;
156 10 ▶ if (len % 2) total++; /* Pad to even */
Branch: 1 branch
BranchTrueFalseTotal
if (len % 2) total++; /* Pad to even */ 0 10 10
157 }
158 }
159
160 3 ▶ result->data = calloc(1, total > 0 ? total : 1);
Branch: 1 branch
BranchTrueFalseTotal
result->data = calloc(1, total > 0 ? total : 1); 3 0 3
161 }
162 3 ▶ if (!result->data) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result->data) { 0 3 3
163 0 free(result->offsets);
164 0 free(result);
165 0 return NULL;
166 }
167
168 /* Second pass: copy glyph data */
169 {
170 3 size_t pos = 0;
171 uint16_t i;
172 13 ▶ for (i = 0; i < map->new_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < map->new_count; i++) { 10 3 13
173 uint16_t old_id;
174 uint32_t off, len;
175 10 result->offsets[i] = pos;
176
177 10 old_id = map->new_to_old[i];
178 10 ▶ if (get_glyph_location(ttf, old_id, &off, &len) && len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (get_glyph_location(ttf, old_id, &off, &len) && len > 0) { 10 0 10
if (get_glyph_location(ttf, old_id, &off, &len) && len > 0) { 7 3 10
179 7 memcpy(result->data + pos, ttf->data + ttf->glyf.offset + off, len);
180 7 pos += len;
181 7 ▶ if (len % 2) pos++; /* Pad to even */
Branch: 1 branch
BranchTrueFalseTotal
if (len % 2) pos++; /* Pad to even */ 0 7 7
182 }
183 }
184 3 result->offsets[map->new_count] = pos;
185 3 result->len = pos;
186 }
187
188 3 return result;
189 }
190  
191 3 static void free_glyf_result(glyf_result_t *r) {
192 3 ▶ if (!r) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!r) return; 0 3 3
193 3 free(r->data);
194 3 free(r->offsets);
195 3 free(r);
196 }
197  
198 /*============================================================================
199 * Build subset loca table
200 *==========================================================================*/
201  
202 3 static uint8_t *build_subset_loca(glyph_map_t *map, glyf_result_t *glyf,
203 int short_format, size_t *out_len) {
204 3 size_t count = map->new_count + 1;
205 size_t len;
206 uint8_t *data;
207
208 3 ▶ if (short_format) {
Branch: 1 branch
BranchTrueFalseTotal
if (short_format) { 3 0 3
209 size_t i;
210 3 len = count * 2;
211 3 data = calloc(1, len);
212 3 ▶ if (!data) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!data) return NULL; 0 3 3
213
214 16 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 13 3 16
215 13 pdfmake_write_be16(data + i * 2, glyf->offsets[i] / 2);
216 }
217 } else {
218 size_t i;
219 0 len = count * 4;
220 0 data = calloc(1, len);
221 0 ▶ if (!data) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!data) return NULL; 0 0 0
222
223 0 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 0 0 0
224 0 pdfmake_write_be32(data + i * 4, glyf->offsets[i]);
225 }
226 }
227
228 3 *out_len = len;
229 3 return data;
230 }
231  
232 /*============================================================================
233 * Build subset hmtx table
234 *==========================================================================*/
235  
236 3 static uint8_t *build_subset_hmtx(pdfmake_ttf_t *ttf, glyph_map_t *map,
237 size_t *out_len) {
238 /* Each glyph gets full metric (advance + lsb) */
239 3 size_t len = map->new_count * 4;
240 3 uint8_t *data = calloc(1, len);
241 const uint8_t *hmtx;
242 uint16_t i;
243 3 ▶ if (!data) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!data) return NULL; 0 3 3
244
245 3 hmtx = ttf->data + ttf->hmtx.offset;
246
247 13 ▶ for (i = 0; i < map->new_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < map->new_count; i++) { 10 3 13
248 10 uint16_t old_id = map->new_to_old[i];
249 uint16_t advance, lsb;
250
251 10 ▶ if (old_id < ttf->num_h_metrics) {
Branch: 1 branch
BranchTrueFalseTotal
if (old_id < ttf->num_h_metrics) { 10 0 10
252 10 advance = pdfmake_read_be16(hmtx + old_id * 4);
253 10 lsb = pdfmake_read_be16(hmtx + old_id * 4 + 2);
254 } else {
255 /* Last advance, variable lsb */
256 size_t lsb_offset;
257 0 advance = pdfmake_read_be16(hmtx + (ttf->num_h_metrics - 1) * 4);
258 0 lsb_offset = ttf->num_h_metrics * 4 +
259 0 (old_id - ttf->num_h_metrics) * 2;
260 0 ▶ if (ttf->hmtx.offset + lsb_offset + 2 <= ttf->data_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->hmtx.offset + lsb_offset + 2 <= ttf->data_len) { 0 0 0
261 0 lsb = pdfmake_read_be16(hmtx + lsb_offset);
262 } else {
263 0 lsb = 0;
264 }
265 }
266
267 10 pdfmake_write_be16(data + i * 4, advance);
268 10 pdfmake_write_be16(data + i * 4 + 2, lsb);
269 }
270
271 3 *out_len = len;
272 3 return data;
273 }
274  
275 /*============================================================================
276 * Build minimal cmap table (format 4 or format 12)
277 *
278 * For PDF embedding, we typically use Identity-H/Identity-V with CIDFont,
279 * so cmap maps GID to GID directly. But for simple TrueType, we need
280 * a proper Unicode cmap.
281 *==========================================================================*/
282  
283 3 static uint8_t *build_subset_cmap_format4(glyph_map_t *map, size_t *out_len) {
284 /* Build a simple format 4 cmap for the subset */
285 /* For PDF embedding with /Identity-H, glyphs are accessed by GID directly */
286 /* We create a minimal cmap that maps GID -> GID */
287
288 /* Simplified: single segment 0 to num_glyphs-1 */
289 3 uint16_t seg_count = 2; /* One real segment + terminator */
290 3 size_t len = 4 + 8 + 14 + seg_count * 8; /* header + encoding record + format 4 */
291
292 3 uint8_t *data = calloc(1, len);
293 3 ▶ if (!data) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!data) return NULL; 0 3 3
294
295 /* cmap header */
296 3 pdfmake_write_be16(data, 0); /* version */
297 3 pdfmake_write_be16(data + 2, 1); /* numTables */
298
299 /* Encoding record: platform 3 (Windows), encoding 1 (Unicode BMP) */
300 3 pdfmake_write_be16(data + 4, 3); /* platformID */
301 3 pdfmake_write_be16(data + 6, 1); /* encodingID */
302 3 pdfmake_write_be32(data + 8, 12); /* offset to subtable */
303
304 /* Format 4 subtable */
305 {
306 3 uint8_t *fmt = data + 12;
307 3 uint16_t fmt_len = 14 + seg_count * 8;
308 /* Segment arrays */
309 uint8_t *endCode;
310 uint8_t *reservedPad;
311 uint8_t *startCode;
312 uint8_t *idDelta;
313 uint8_t *idRangeOffset;
314
315 3 pdfmake_write_be16(fmt, 4); /* format */
316 3 pdfmake_write_be16(fmt + 2, fmt_len); /* length */
317 3 pdfmake_write_be16(fmt + 4, 0); /* language */
318 3 pdfmake_write_be16(fmt + 6, seg_count * 2); /* segCountX2 */
319 3 pdfmake_write_be16(fmt + 8, 2); /* searchRange */
320 3 pdfmake_write_be16(fmt + 10, 0); /* entrySelector */
321 3 pdfmake_write_be16(fmt + 12, 2); /* rangeShift */
322
323 3 endCode = fmt + 14;
324 3 reservedPad = endCode + seg_count * 2;
325 3 startCode = reservedPad + 2;
326 3 idDelta = startCode + seg_count * 2;
327 3 idRangeOffset = idDelta + seg_count * 2;
328
329 /* Segment 0: 0 to num_glyphs-1 -> direct mapping (delta=0) */
330 3 pdfmake_write_be16(endCode, map->new_count - 1);
331 3 pdfmake_write_be16(startCode, 0);
332 3 pdfmake_write_be16(idDelta, 0);
333 3 pdfmake_write_be16(idRangeOffset, 0);
334
335 /* Terminator segment (required): 0xFFFF */
336 3 pdfmake_write_be16(endCode + 2, 0xFFFF);
337 3 pdfmake_write_be16(reservedPad, 0);
338 3 pdfmake_write_be16(startCode + 2, 0xFFFF);
339 3 pdfmake_write_be16(idDelta + 2, 1);
340 3 pdfmake_write_be16(idRangeOffset + 2, 0);
341 }
342
343 3 *out_len = len;
344 3 return data;
345 }
346  
347 /*============================================================================
348 * Copy and adjust head table
349 *==========================================================================*/
350  
351 3 static uint8_t *build_subset_head(pdfmake_ttf_t *ttf, int short_loca,
352 size_t *out_len) {
353 uint8_t *data;
354 3 ▶ if (ttf->head.length < 54) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ttf->head.length < 54) return NULL; 0 3 3
355
356 3 data = malloc(ttf->head.length);
357 3 ▶ if (!data) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!data) return NULL; 0 3 3
358
359 3 memcpy(data, ttf->data + ttf->head.offset, ttf->head.length);
360
361 /* Update indexToLocFormat */
362 3 pdfmake_write_be16(data + 50, short_loca ? 0 : 1);
363
364 /* Clear checkSumAdjustment (will be updated later) */
365 3 pdfmake_write_be32(data + 8, 0);
366
367 3 *out_len = ttf->head.length;
368 3 return data;
369 }
370  
371 /*============================================================================
372 * Copy and adjust maxp table
373 *==========================================================================*/
374  
375 3 static uint8_t *build_subset_maxp(pdfmake_ttf_t *ttf, glyph_map_t *map,
376 size_t *out_len) {
377 3 uint8_t *data = malloc(ttf->maxp.length);
378 3 ▶ if (!data) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!data) return NULL; 0 3 3
379
380 3 memcpy(data, ttf->data + ttf->maxp.offset, ttf->maxp.length);
381
382 /* Update numGlyphs */
383 3 pdfmake_write_be16(data + 4, map->new_count);
384
385 3 *out_len = ttf->maxp.length;
386 3 return data;
387 }
388  
389 /*============================================================================
390 * Copy and adjust hhea table
391 *==========================================================================*/
392  
393 3 static uint8_t *build_subset_hhea(pdfmake_ttf_t *ttf, glyph_map_t *map,
394 size_t *out_len) {
395 3 uint8_t *data = malloc(ttf->hhea.length);
396 3 ▶ if (!data) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!data) return NULL; 0 3 3
397
398 3 memcpy(data, ttf->data + ttf->hhea.offset, ttf->hhea.length);
399
400 /* Update numberOfHMetrics - all glyphs have full metrics */
401 3 pdfmake_write_be16(data + 34, map->new_count);
402
403 3 *out_len = ttf->hhea.length;
404 3 return data;
405 }
406  
407 /*============================================================================
408 * Build minimal post table
409 *==========================================================================*/
410  
411 3 static uint8_t *build_subset_post(size_t *out_len) {
412 /* Create minimal version 3.0 post table (no glyph names) */
413 3 size_t len = 32;
414 3 uint8_t *data = calloc(1, len);
415 3 ▶ if (!data) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!data) return NULL; 0 3 3
416
417 3 pdfmake_write_be32(data, 0x00030000); /* version 3.0 */
418 /* italicAngle, underlinePosition, etc. default to 0 */
419
420 3 *out_len = len;
421 3 return data;
422 }
423  
424 /*============================================================================
425 * Assemble final subset TTF
426 *==========================================================================*/
427  
428 3 pdfmake_err_t pdfmake_ttf_subset(const pdfmake_ttf_t *ttf, pdfmake_buf_t *out_buf) {
429 /* Cast away const for internal processing - we don't modify the source */
430 pdfmake_ttf_t *mutable_ttf;
431 glyph_map_t *map;
432 glyf_result_t *glyf;
433 int short_loca;
434 size_t loca_len, hmtx_len, cmap_len, head_len, maxp_len, hhea_len, post_len;
435 uint8_t *loca, *hmtx, *cmap, *head, *maxp, *hhea, *post;
436 3 const int num_tables = 8; /* head, hhea, maxp, loca, glyf, hmtx, cmap, post */
437 size_t header_size;
438 /* Align each table to 4 bytes */
439 #define ALIGN4(x) (((x) + 3) & ~3)
440 size_t tables_size;
441 size_t total_size;
442 uint8_t *result;
443 int power, selector;
444 uint8_t *dir;
445 size_t offset;
446 int i;
447 uint32_t file_checksum;
448 uint32_t head_offset;
449 struct table_entry {
450 uint32_t tag;
451 uint8_t *data;
452 size_t len;
453 } tables[8];
454
455 3 ▶ if (!ttf || !out_buf) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!ttf || !out_buf) return PDFMAKE_EINVAL; 3 0 3
if (!ttf || !out_buf) return PDFMAKE_EINVAL; 0 3 3
456
457 3 mutable_ttf = (pdfmake_ttf_t *)ttf;
458
459 3 map = build_glyph_map(mutable_ttf);
460 3 ▶ if (!map) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!map) return PDFMAKE_ENOMEM; 0 3 3
461
462 /* Build subset tables */
463 3 glyf = build_subset_glyf(mutable_ttf, map);
464 3 ▶ if (!glyf) { free_glyph_map(map); return PDFMAKE_ENOMEM; }
Branch: 1 branch
BranchTrueFalseTotal
if (!glyf) { free_glyph_map(map); return PDFMAKE_ENOMEM; } 0 3 3
465
466 /* Determine if we can use short loca format */
467 3 short_loca = (glyf->len / 2) <= 0xFFFF;
468
469 3 loca = build_subset_loca(map, glyf, short_loca, &loca_len);
470 3 hmtx = build_subset_hmtx(mutable_ttf, map, &hmtx_len);
471 3 cmap = build_subset_cmap_format4(map, &cmap_len);
472 3 head = build_subset_head(mutable_ttf, short_loca, &head_len);
473 3 maxp = build_subset_maxp(mutable_ttf, map, &maxp_len);
474 3 hhea = build_subset_hhea(mutable_ttf, map, &hhea_len);
475 3 post = build_subset_post(&post_len);
476
477 3 ▶ if (!loca || !hmtx || !cmap || !head || !maxp || !hhea || !post) {
Branch: 7 branches
BranchTrueFalseTotal
if (!loca || !hmtx || !cmap || !head || !maxp || !hhea || !post) { 3 0 3
if (!loca || !hmtx || !cmap || !head || !maxp || !hhea || !post) { 3 0 3
if (!loca || !hmtx || !cmap || !head || !maxp || !hhea || !post) { 3 0 3
if (!loca || !hmtx || !cmap || !head || !maxp || !hhea || !post) { 3 0 3
if (!loca || !hmtx || !cmap || !head || !maxp || !hhea || !post) { 3 0 3
if (!loca || !hmtx || !cmap || !head || !maxp || !hhea || !post) { 3 0 3
if (!loca || !hmtx || !cmap || !head || !maxp || !hhea || !post) { 0 3 3
478 0 free(loca); free(hmtx); free(cmap); free(head);
479 0 free(maxp); free(hhea); free(post);
480 0 free_glyf_result(glyf);
481 0 free_glyph_map(map);
482 0 return PDFMAKE_ENOMEM;
483 }
484
485 /* Calculate file size */
486 3 header_size = 12 + num_tables * 16;
487
488 3 tables_size = ALIGN4(head_len) + ALIGN4(hhea_len) + ALIGN4(maxp_len) +
489 3 ALIGN4(cmap_len) + ALIGN4(hmtx_len) + ALIGN4(loca_len) +
490 3 ALIGN4(glyf->len) + ALIGN4(post_len);
491
492 3 total_size = header_size + tables_size;
493
494 3 result = calloc(1, total_size);
495 3 ▶ if (!result) {
Branch: 1 branch
BranchTrueFalseTotal
if (!result) { 0 3 3
496 0 free(loca); free(hmtx); free(cmap); free(head);
497 0 free(maxp); free(hhea); free(post);
498 0 free_glyf_result(glyf);
499 0 free_glyph_map(map);
500 0 return PDFMAKE_ENOMEM;
501 }
502
503 /* Write offset table header */
504 3 pdfmake_write_be32(result, 0x00010000); /* sfntVersion */
505 3 pdfmake_write_be16(result + 4, num_tables);
506
507 /* searchRange, entrySelector, rangeShift */
508 3 power = 1; selector = 0;
509 12 ▶ while (power * 2 <= num_tables) { power *= 2; selector++; }
Branch: 1 branch
BranchTrueFalseTotal
while (power * 2 <= num_tables) { power *= 2; selector++; } 9 3 12
510 3 pdfmake_write_be16(result + 6, power * 16);
511 3 pdfmake_write_be16(result + 8, selector);
512 3 pdfmake_write_be16(result + 10, num_tables * 16 - power * 16);
513
514 /* Table directory entries */
515 3 tables[0].tag = TAG('c','m','a','p'); tables[0].data = cmap; tables[0].len = cmap_len;
516 3 tables[1].tag = TAG('g','l','y','f'); tables[1].data = glyf->data; tables[1].len = glyf->len;
517 3 tables[2].tag = TAG('h','e','a','d'); tables[2].data = head; tables[2].len = head_len;
518 3 tables[3].tag = TAG('h','h','e','a'); tables[3].data = hhea; tables[3].len = hhea_len;
519 3 tables[4].tag = TAG('h','m','t','x'); tables[4].data = hmtx; tables[4].len = hmtx_len;
520 3 tables[5].tag = TAG('l','o','c','a'); tables[5].data = loca; tables[5].len = loca_len;
521 3 tables[6].tag = TAG('m','a','x','p'); tables[6].data = maxp; tables[6].len = maxp_len;
522 3 tables[7].tag = TAG('p','o','s','t'); tables[7].data = post; tables[7].len = post_len;
523
524 3 dir = result + 12;
525 3 offset = header_size;
526
527 27 ▶ for (i = 0; i < num_tables; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_tables; i++) { 24 3 27
528 24 pdfmake_write_be32(dir, tables[i].tag);
529 24 pdfmake_write_be32(dir + 4, ttf_checksum(tables[i].data, tables[i].len));
530 24 pdfmake_write_be32(dir + 8, offset);
531 24 pdfmake_write_be32(dir + 12, tables[i].len);
532
533 24 memcpy(result + offset, tables[i].data, tables[i].len);
534 24 offset += ALIGN4(tables[i].len);
535 24 dir += 16;
536 }
537
538 /* Update head checkSumAdjustment */
539 3 file_checksum = ttf_checksum(result, total_size);
540 3 head_offset = 0;
541 9 ▶ for (i = 0; i < num_tables; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_tables; i++) { 9 0 9
542 9 ▶ if (tables[i].tag == TAG('h','e','a','d')) {
Branch: 1 branch
BranchTrueFalseTotal
if (tables[i].tag == TAG('h','e','a','d')) { 3 6 9
543 3 head_offset = pdfmake_read_be32(result + 12 + i * 16 + 8);
544 3 break;
545 }
546 }
547 3 pdfmake_write_be32(result + head_offset + 8, 0xB1B0AFBA - file_checksum);
548
549 /* Copy to output buffer */
550 3 pdfmake_buf_append(out_buf, result, total_size);
551
552 /* Cleanup */
553 3 free(result);
554 3 free(loca); free(hmtx); free(cmap); free(head);
555 3 free(maxp); free(hhea); free(post);
556 3 free_glyf_result(glyf);
557 3 free_glyph_map(map);
558
559 3 return PDFMAKE_OK;
560
561 #undef ALIGN4
562 }
563  
564 /*============================================================================
565 * CIDToGIDMap for an embedded subset
566 *
567 * The subsetter compacts the used glyphs to 0..n-1, so a glyph the content
568 * stream names by its id in the ORIGINAL font is at a different id in the
569 * embedded one. Writing /CIDToGIDMap /Identity against a renumbered subset
570 * draws the wrong glyphs; this is the stream that maps one to the other.
571 *
572 * CIDs are original glyph ids, so the map is 2 bytes per CID up to the
573 * highest one in use. It is nearly all zeroes and flate-compresses to
574 * almost nothing. Built from build_glyph_map so it cannot disagree with the
575 * numbering the subset itself was written with.
576 *==========================================================================*/
577  
578 3 pdfmake_err_t pdfmake_ttf_subset_cidtogid(const pdfmake_ttf_t *ttf,
579 pdfmake_buf_t *out_buf) {
580 glyph_map_t *map;
581 uint16_t highest;
582 int i;
583 uint8_t pair[2];
584  
585 3 ▶ if (!ttf || !out_buf) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!ttf || !out_buf) return PDFMAKE_EINVAL; 3 0 3
if (!ttf || !out_buf) return PDFMAKE_EINVAL; 0 3 3
586  
587 3 map = build_glyph_map((pdfmake_ttf_t *)ttf);
588 3 ▶ if (!map) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!map) return PDFMAKE_ENOMEM; 0 3 3
589  
590 3 highest = 0;
591 15 ▶ for (i = 1; i < ttf->num_glyphs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < ttf->num_glyphs; i++) { 12 3 15
592 12 size_t byte = i / 8;
593 12 uint8_t bit = 1 << (i % 8);
594 12 ▶ if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit))
Branch: 2 branches
BranchTrueFalseTotal
if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit)) 12 0 12
if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit)) 7 5 12
595 7 highest = (uint16_t)i;
596 }
597  
598 13 ▶ for (i = 0; i <= (int)highest; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= (int)highest; i++) { 10 3 13
599 10 uint16_t new_gid = map->old_to_new[i];
600 /* old_to_new is zero for unused glyphs, which is .notdef — correct. */
601 10 pair[0] = (uint8_t)((new_gid >> 8) & 0xFF);
602 10 pair[1] = (uint8_t)(new_gid & 0xFF);
603 10 ▶ if (pdfmake_buf_append(out_buf, pair, 2) != PDFMAKE_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_buf_append(out_buf, pair, 2) != PDFMAKE_OK) { 0 10 10
604 0 free_glyph_map(map);
605 0 return PDFMAKE_ENOMEM;
606 }
607 }
608  
609 3 free_glyph_map(map);
610 3 return PDFMAKE_OK;
611 }
« src/pdfmake_redact_rewrite.c ↑ Summary xs/markup_style.xs »