src/pdfmake_tounicode.c

filter: Statementstmt 60.1% 74 / 123 Branchbran 44.9% 44 / 98 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 53.3% 118 / 221 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/outline.xs ↑ Summary src/pdfmake_tag.c »
1 /*
2 * pdfmake_tounicode.c - ToUnicode CMap generation
3 *
4 * Generates a ToUnicode CMap stream that maps glyph codes to Unicode
5 * codepoints, enabling text extraction and copy/paste from PDFs.
6 *
7 * Reference: PDF spec ยง9.10.3, Adobe Technical Note #5014 (CMap)
8 */
9  
10 #include "pdfmake_font.h"
11 #include "pdfmake_buf.h"
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15  
16 /*============================================================================
17 * ToUnicode CMap structure
18 *
19 * The CMap maps character codes (glyph IDs for CIDFont) to Unicode.
20 * We use bfchar entries for individual mappings and bfrange for
21 * contiguous ranges.
22 *==========================================================================*/
23  
24 typedef struct {
25 uint16_t glyph_id;
26 uint32_t unicode;
27 } glyph_unicode_t;
28  
29 /*============================================================================
30 * Build glyph->Unicode mapping from reverse cmap lookup
31 *==========================================================================*/
32  
33 5 static int compare_gu(const void *a, const void *b) {
34 5 const glyph_unicode_t *ga = a;
35 5 const glyph_unicode_t *gb = b;
36 5 ▶ if (ga->glyph_id < gb->glyph_id) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ga->glyph_id < gb->glyph_id) return -1; 5 0 5
37 0 ▶ if (ga->glyph_id > gb->glyph_id) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ga->glyph_id > gb->glyph_id) return 1; 0 0 0
38 0 return 0;
39 }
40  
41 /*============================================================================
42 * Generate ToUnicode CMap for font
43 *==========================================================================*/
44  
45 3 pdfmake_err_t pdfmake_tounicode_generate(const pdfmake_font_t *font,
46 pdfmake_buf_t *out_buf) {
47 pdfmake_ttf_t *ttf;
48 size_t used_count;
49 int i;
50 size_t byte;
51 uint8_t bit;
52 glyph_unicode_t *mappings;
53 size_t map_count;
54 uint32_t cp;
55 uint16_t gid;
56 int exists;
57 size_t j;
58 size_t mi;
59 size_t range_start;
60 size_t range_len;
61 size_t next;
62 uint16_t gid_start;
63 uint16_t gid_end;
64 uint32_t unicode_start;
65 size_t batch_count;
66 size_t batch_start;
67 size_t possible_range;
68 uint32_t unicode;
69  
70 3 ▶ if (!font || !out_buf) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!font || !out_buf) return PDFMAKE_EINVAL; 3 0 3
if (!font || !out_buf) return PDFMAKE_EINVAL; 0 3 3
71
72 /* For Standard 14 fonts, use WinAnsi ToUnicode */
73 3 ▶ if (font->type == PDFMAKE_FONT_TYPE1) {
Branch: 1 branch
BranchTrueFalseTotal
if (font->type == PDFMAKE_FONT_TYPE1) { 0 3 3
74 /* WinAnsi to Unicode mapping (codes 128-159 differ from Latin-1) */
75 static const uint16_t winansi_unicode[] = {
76 /* 128-143 */
77 0x20AC, 0x0081, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
78 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0x008D, 0x017D, 0x008F,
79 /* 144-159 */
80 0x0090, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
81 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, 0x009D, 0x017E, 0x0178,
82 };
83
84 /* CMap header */
85 0 pdfmake_buf_appendf(out_buf,
86 "/CIDInit /ProcSet findresource begin\n"
87 "12 dict begin\n"
88 "begincmap\n"
89 "/CIDSystemInfo <<\n"
90 " /Registry (Adobe)\n"
91 " /Ordering (UCS)\n"
92 " /Supplement 0\n"
93 ">> def\n"
94 "/CMapName /Adobe-Identity-UCS def\n"
95 "/CMapType 2 def\n"
96 "1 begincodespacerange\n"
97 "<00> <FF>\n"
98 "endcodespacerange\n"
99 );
100
101 /* Range 32-127: direct ASCII mapping */
102 0 pdfmake_buf_appendf(out_buf,
103 "2 beginbfrange\n"
104 "<20> <7E> <0020>\n"
105 "<A0> <FF> <00A0>\n"
106 "endbfrange\n"
107 );
108
109 /* Individual mappings for 128-159 (WinAnsi special) */
110 0 pdfmake_buf_appendf(out_buf, "32 beginbfchar\n");
111
112 0 ▶ for (i = 0; i < 32; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 32; i++) { 0 0 0
113 0 pdfmake_buf_appendf(out_buf, "<%02X> <%04X>\n",
114 0 128 + i, winansi_unicode[i]);
115 }
116
117 0 pdfmake_buf_appendf(out_buf, "endbfchar\n");
118
119 /* CMap trailer */
120 0 pdfmake_buf_appendf(out_buf,
121 "endcmap\n"
122 "CMapName currentdict /CMap defineresource pop\n"
123 "end\n"
124 "end\n"
125 );
126
127 0 return PDFMAKE_OK;
128 }
129
130 /* For TrueType fonts, build from used glyphs */
131 3 ▶ if (font->type != PDFMAKE_FONT_TRUETYPE || !font->ttf) {
Branch: 2 branches
BranchTrueFalseTotal
if (font->type != PDFMAKE_FONT_TRUETYPE || !font->ttf) { 3 0 3
if (font->type != PDFMAKE_FONT_TRUETYPE || !font->ttf) { 0 3 3
132 0 return PDFMAKE_EINVAL;
133 }
134
135 3 ttf = font->ttf;
136
137 /* Count used glyphs */
138 3 used_count = 0;
139 15 ▶ for (i = 1; i < ttf->num_glyphs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < ttf->num_glyphs; i++) { 12 3 15
140 12 byte = i / 8;
141 12 bit = 1 << (i % 8);
142 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
143 7 used_count++;
144 }
145 }
146
147 3 ▶ if (used_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (used_count == 0) { 0 3 3
148 0 return PDFMAKE_OK; /* Nothing to map */
149 }
150
151 /* Build glyph->unicode mapping by scanning used codepoints */
152 /* We need to reverse the cmap lookup - scan common Unicode ranges */
153 3 mappings = calloc(used_count, sizeof(glyph_unicode_t));
154 3 ▶ if (!mappings) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!mappings) return PDFMAKE_ENOMEM; 0 3 3
155
156 3 map_count = 0;
157
158 /* Scan BMP range */
159 109 ▶ for (cp = 0x20; cp < 0x10000 && map_count < used_count; cp++) {
Branch: 2 branches
BranchTrueFalseTotal
for (cp = 0x20; cp < 0x10000 && map_count < used_count; cp++) { 109 0 109
for (cp = 0x20; cp < 0x10000 && map_count < used_count; cp++) { 106 3 109
160 106 gid = pdfmake_ttf_cmap_lookup(ttf, cp);
161 106 ▶ if (gid > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (gid > 0) { 7 99 106
162 7 byte = gid / 8;
163 7 bit = 1 << (gid % 8);
164 7 ▶ if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit)) { 7 0 7
if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit)) { 7 0 7
165 /* Check if we already have this glyph */
166 7 exists = 0;
167 14 ▶ for (j = 0; j < map_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < map_count; j++) { 7 7 14
168 7 ▶ if (mappings[j].glyph_id == gid) {
Branch: 1 branch
BranchTrueFalseTotal
if (mappings[j].glyph_id == gid) { 0 7 7
169 0 exists = 1;
170 0 break;
171 }
172 }
173 7 ▶ if (!exists) {
Branch: 1 branch
BranchTrueFalseTotal
if (!exists) { 7 0 7
174 7 mappings[map_count].glyph_id = gid;
175 7 mappings[map_count].unicode = cp;
176 7 map_count++;
177 }
178 }
179 }
180 }
181
182 3 ▶ if (map_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (map_count == 0) { 0 3 3
183 0 free(mappings);
184 0 return PDFMAKE_OK;
185 }
186
187 /* Sort by glyph ID */
188 3 qsort(mappings, map_count, sizeof(glyph_unicode_t), compare_gu);
189
190 /* CMap header */
191 3 pdfmake_buf_appendf(out_buf,
192 "/CIDInit /ProcSet findresource begin\n"
193 "12 dict begin\n"
194 "begincmap\n"
195 "/CIDSystemInfo <<\n"
196 " /Registry (Adobe)\n"
197 " /Ordering (UCS)\n"
198 " /Supplement 0\n"
199 ">> def\n"
200 "/CMapName /Adobe-Identity-UCS def\n"
201 "/CMapType 2 def\n"
202 "1 begincodespacerange\n"
203 "<0000> <FFFF>\n"
204 "endcodespacerange\n"
205 );
206
207 /* Emit mappings */
208 3 mi = 0;
209 6 ▶ while (mi < map_count) {
Branch: 1 branch
BranchTrueFalseTotal
while (mi < map_count) { 3 3 6
210 /* Try to find a range */
211 3 range_start = mi;
212 3 range_len = 1;
213
214 7 ▶ while (range_start + range_len < map_count) {
Branch: 1 branch
BranchTrueFalseTotal
while (range_start + range_len < map_count) { 4 3 7
215 4 next = range_start + range_len;
216 4 ▶ if (mappings[next].glyph_id == mappings[range_start].glyph_id + range_len &&
Branch: 1 branch
BranchTrueFalseTotal
if (mappings[next].glyph_id == mappings[range_start].glyph_id + range_len && 4 0 4
217 4 ▶ mappings[next].unicode == mappings[range_start].unicode + range_len) {
Branch: 1 branch
BranchTrueFalseTotal
mappings[next].unicode == mappings[range_start].unicode + range_len) { 4 0 4
218 4 range_len++;
219 } else {
220 break;
221 }
222 }
223
224 3 ▶ if (range_len >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (range_len >= 3) { 1 2 3
225 /* Emit as range */
226 1 pdfmake_buf_appendf(out_buf, "1 beginbfrange\n");
227
228 1 gid_start = mappings[range_start].glyph_id;
229 1 gid_end = gid_start + range_len - 1;
230 1 unicode_start = mappings[range_start].unicode;
231
232 1 pdfmake_buf_appendf(out_buf, "<%04X> <%04X> <%04X>\n",
233 gid_start, gid_end, (unsigned)unicode_start);
234
235 1 pdfmake_buf_appendf(out_buf, "endbfrange\n");
236 1 mi += range_len;
237 } else {
238 /* Emit individual characters */
239 2 batch_count = 0;
240 2 batch_start = mi;
241
242 5 ▶ while (mi < map_count && batch_count < 100) {
Branch: 2 branches
BranchTrueFalseTotal
while (mi < map_count && batch_count < 100) { 3 2 5
while (mi < map_count && batch_count < 100) { 3 0 3
243 3 possible_range = 1;
244 4 ▶ while (mi + possible_range < map_count) {
Branch: 1 branch
BranchTrueFalseTotal
while (mi + possible_range < map_count) { 1 3 4
245 1 next = mi + possible_range;
246 1 ▶ if (mappings[next].glyph_id == mappings[mi].glyph_id + possible_range &&
Branch: 1 branch
BranchTrueFalseTotal
if (mappings[next].glyph_id == mappings[mi].glyph_id + possible_range && 1 0 1
247 1 ▶ mappings[next].unicode == mappings[mi].unicode + possible_range) {
Branch: 1 branch
BranchTrueFalseTotal
mappings[next].unicode == mappings[mi].unicode + possible_range) { 1 0 1
248 1 possible_range++;
249 } else {
250 break;
251 }
252 }
253
254 3 ▶ if (possible_range >= 3) break;
Branch: 1 branch
BranchTrueFalseTotal
if (possible_range >= 3) break; 0 3 3
255
256 3 batch_count++;
257 3 mi++;
258 }
259
260 2 ▶ if (batch_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (batch_count > 0) { 2 0 2
261 2 pdfmake_buf_appendf(out_buf, "%zu beginbfchar\n", batch_count);
262
263 5 ▶ for (j = 0; j < batch_count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < batch_count; j++) { 3 2 5
264 3 gid = mappings[batch_start + j].glyph_id;
265 3 unicode = mappings[batch_start + j].unicode;
266 3 pdfmake_buf_appendf(out_buf, "<%04X> <%04X>\n", gid, (unsigned)unicode);
267 }
268
269 2 pdfmake_buf_appendf(out_buf, "endbfchar\n");
270 }
271 }
272 }
273
274 /* CMap trailer */
275 3 pdfmake_buf_appendf(out_buf,
276 "endcmap\n"
277 "CMapName currentdict /CMap defineresource pop\n"
278 "end\n"
279 "end\n"
280 );
281
282 3 free(mappings);
283 3 return PDFMAKE_OK;
284 }
285  
286 /*============================================================================
287 * Generate CID width array (W entry for CIDFont)
288 *
289 * Format: [ c [w1 w2 ...] c [w1 w2 ...] ... ]
290 * or: [ c_first c_last w ]
291 *==========================================================================*/
292  
293 0 pdfmake_err_t pdfmake_cid_widths(const pdfmake_font_t *font,
294 pdfmake_buf_t *out_buf) {
295 pdfmake_ttf_t *ttf;
296 size_t used_count;
297 int i;
298 size_t byte;
299 uint8_t bit;
300 int in_group;
301 uint16_t advance;
302  
303 0 ▶ if (!font || !out_buf) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!font || !out_buf) return PDFMAKE_EINVAL; 0 0 0
if (!font || !out_buf) return PDFMAKE_EINVAL; 0 0 0
304
305 0 ▶ if (font->type != PDFMAKE_FONT_TRUETYPE || !font->ttf) {
Branch: 2 branches
BranchTrueFalseTotal
if (font->type != PDFMAKE_FONT_TRUETYPE || !font->ttf) { 0 0 0
if (font->type != PDFMAKE_FONT_TRUETYPE || !font->ttf) { 0 0 0
306 0 return PDFMAKE_EINVAL;
307 }
308
309 0 ttf = font->ttf;
310
311 /* Collect used glyphs in order */
312 0 used_count = 0;
313 0 ▶ for (i = 0; i < ttf->num_glyphs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ttf->num_glyphs; i++) { 0 0 0
314 0 byte = i / 8;
315 0 bit = 1 << (i % 8);
316 0 ▶ if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit)) { 0 0 0
if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit)) { 0 0 0
317 0 used_count++;
318 }
319 }
320
321 0 ▶ if (used_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (used_count == 0) { 0 0 0
322 0 pdfmake_buf_appendf(out_buf, "[]");
323 0 return PDFMAKE_OK;
324 }
325
326 0 pdfmake_buf_appendf(out_buf, "[");
327
328 /* Group consecutive glyphs */
329 0 in_group = 0;
330
331 0 ▶ for (i = 0; i < ttf->num_glyphs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ttf->num_glyphs; i++) { 0 0 0
332 0 byte = i / 8;
333 0 bit = 1 << (i % 8);
334
335 0 ▶ if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit)) { 0 0 0
if (ttf->used_glyphs && (ttf->used_glyphs[byte] & bit)) { 0 0 0
336 0 ▶ if (!in_group) {
Branch: 1 branch
BranchTrueFalseTotal
if (!in_group) { 0 0 0
337 0 in_group = 1;
338 0 pdfmake_buf_appendf(out_buf, " %d [", i);
339 }
340
341 0 advance = pdfmake_ttf_glyph_advance(ttf, i);
342 0 pdfmake_buf_appendf(out_buf, " %d", advance);
343 0 ▶ } else if (in_group) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (in_group) { 0 0 0
344 0 pdfmake_buf_appendf(out_buf, " ]");
345 0 in_group = 0;
346 }
347 }
348
349 0 ▶ if (in_group) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_group) { 0 0 0
350 0 pdfmake_buf_appendf(out_buf, " ]");
351 }
352
353 0 pdfmake_buf_appendf(out_buf, " ]");
354
355 0 return PDFMAKE_OK;
356 }
« xs/outline.xs ↑ Summary src/pdfmake_tag.c »