src/pdfmake_textract.c

filter: Statementstmt 88.4% 1399 / 1581 Branchbran 63.6% 1089 / 1712 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.5% 2488 / 3293 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/pdfmake_page.c ↑ Summary src/pdfmake_cid_unicode.c »
1 /*
2 * pdfmake_textract.c — Text extraction with coordinates.
3 *
4 * Pipeline: interpreter → visitor → raw glyphs → words → lines → blocks
5 *
6 * §9.4 Text objects, §9.10 Extraction of text content
7 */
8  
9 #include "pdfmake_textract.h"
10 #include "pdfmake_font.h"
11 #include "pdfmake_interpreter.h"
12 #include "pdfmake_cmap.h"
13 #include "pdfmake_reader.h"
14 #include "pdfmake_parser.h"
15 #include "pdfmake_buf.h"
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdio.h>
19 #include <math.h>
20  
21 /*============================================================================
22 * WinAnsi encoding → Unicode mapping (§D.1)
23 *==========================================================================*/
24  
25 /* Bytes 0x00–0x7F map to U+0000–U+007F (ASCII).
26 * Bytes 0x80–0x9F have special mappings.
27 * Bytes 0xA0–0xFF map to U+00A0–U+00FF (Latin-1 supplement).
28 */
29 static const uint32_t winansi_0x80[32] = {
30 0x20AC, /* 0x80: Euro sign */
31 0xFFFD, /* 0x81: undefined */
32 0x201A, /* 0x82: single low-9 quotation mark */
33 0x0192, /* 0x83: latin small f with hook */
34 0x201E, /* 0x84: double low-9 quotation mark */
35 0x2026, /* 0x85: horizontal ellipsis */
36 0x2020, /* 0x86: dagger */
37 0x2021, /* 0x87: double dagger */
38 0x02C6, /* 0x88: modifier letter circumflex accent */
39 0x2030, /* 0x89: per mille sign */
40 0x0160, /* 0x8A: latin capital S with caron */
41 0x2039, /* 0x8B: single left-pointing angle quotation */
42 0x0152, /* 0x8C: latin capital ligature OE */
43 0xFFFD, /* 0x8D: undefined */
44 0x017D, /* 0x8E: latin capital Z with caron */
45 0xFFFD, /* 0x8F: undefined */
46 0xFFFD, /* 0x90: undefined */
47 0x2018, /* 0x91: left single quotation mark */
48 0x2019, /* 0x92: right single quotation mark */
49 0x201C, /* 0x93: left double quotation mark */
50 0x201D, /* 0x94: right double quotation mark */
51 0x2022, /* 0x95: bullet */
52 0x2013, /* 0x96: en dash */
53 0x2014, /* 0x97: em dash */
54 0x02DC, /* 0x98: small tilde */
55 0x2122, /* 0x99: trade mark sign */
56 0x0161, /* 0x9A: latin small s with caron */
57 0x203A, /* 0x9B: single right-pointing angle quotation */
58 0x0153, /* 0x9C: latin small ligature oe */
59 0xFFFD, /* 0x9D: undefined */
60 0x017E, /* 0x9E: latin small z with caron */
61 0x0178, /* 0x9F: latin capital Y with diaeresis */
62 };
63  
64 0 uint32_t pdfmake_winansi_to_unicode(uint8_t byte) {
65 0 ▶ if (byte < 0x80) return (uint32_t)byte;
Branch: 1 branch
BranchTrueFalseTotal
if (byte < 0x80) return (uint32_t)byte; 0 0 0
66 0 ▶ if (byte < 0xA0) return winansi_0x80[byte - 0x80];
Branch: 1 branch
BranchTrueFalseTotal
if (byte < 0xA0) return winansi_0x80[byte - 0x80]; 0 0 0
67 0 return (uint32_t)byte; /* 0xA0–0xFF: same as Unicode */
68 }
69  
70 /*============================================================================
71 * Result management
72 *==========================================================================*/
73  
74 110 pdfmake_textract_options_t pdfmake_textract_default_options(void) {
75 pdfmake_textract_options_t opts;
76 110 opts.word_gap_factor = 0.3;
77 110 opts.line_tolerance = 0.5;
78 110 opts.block_leading = 1.5;
79 110 opts.include_invisible = 1;
80 110 return opts;
81 }
82  
83 110 pdfmake_textract_result_t *pdfmake_textract_new(pdfmake_arena_t *arena) {
84 110 pdfmake_textract_result_t *r = calloc(1, sizeof(*r));
85 110 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 110 0 110
86 110 r->arena = arena;
87 110 r->include_invisible = 1; /* default: include OCR text */
88 110 r->current_mcid = -1; /* Phase 12: no active MCID */
89 }
90 110 return r;
91 }
92  
93 802 static void free_words(pdfmake_text_word_t *words, size_t len) {
94 size_t i;
95 3800 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 2998 802 3800
96 2998 free(words[i].glyphs);
97 }
98 802 free(words);
99 802 }
100  
101 342 static void free_lines(pdfmake_text_line_t *lines, size_t len) {
102 size_t i;
103 1144 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 802 342 1144
104 802 free_words(lines[i].words, lines[i].len);
105 }
106 342 free(lines);
107 342 }
108  
109 110 void pdfmake_textract_free(pdfmake_textract_result_t *result) {
110 size_t i;
111 110 ▶ if (!result) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return; 0 110 110
112 452 ▶ for (i = 0; i < result->len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < result->len; i++) { 342 110 452
113 342 free_lines(result->blocks[i].lines, result->blocks[i].len);
114 }
115 110 free(result->blocks);
116 110 free(result->raw_glyphs);
117 110 free(result->font_cache);
118 110 free(result->struct_map);
119 110 free(result);
120 }
121  
122 110 void pdfmake_textract_set_reader(pdfmake_textract_result_t *result,
123 struct pdfmake_reader *reader) {
124 110 ▶ if (!result) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return; 0 110 110
125 110 result->reader = reader;
126 }
127  
128 /*============================================================================
129 * Font resolution cache
130 *
131 * Lazily resolves per-font metadata (ToUnicode CMap, Type0/CID flag, Std14 ID)
132 * on first use. Subsequent glyphs from the same font dict hit the cache.
133 *==========================================================================*/
134  
135 10922 static pdfmake_resolved_font_t *font_cache_find(
136 pdfmake_textract_result_t *r, pdfmake_obj_t *font_dict)
137 {
138 size_t i;
139 20189 ▶ for (i = 0; i < r->font_cache_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r->font_cache_len; i++) { 20002 187 20189
140 20002 ▶ if (r->font_cache[i].source_dict == font_dict) return &r->font_cache[i];
Branch: 1 branch
BranchTrueFalseTotal
if (r->font_cache[i].source_dict == font_dict) return &r->font_cache[i]; 10735 9267 20002
141 }
142 187 return NULL;
143 }
144  
145 187 static pdfmake_resolved_font_t *font_cache_add(
146 pdfmake_textract_result_t *r, pdfmake_obj_t *font_dict)
147 {
148 pdfmake_resolved_font_t *slot;
149 187 ▶ if (r->font_cache_len >= r->font_cache_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->font_cache_len >= r->font_cache_cap) { 124 63 187
150 124 ▶ size_t new_cap = r->font_cache_cap == 0 ? 4 : r->font_cache_cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = r->font_cache_cap == 0 ? 4 : r->font_cache_cap * 2; 14 110 124
151 124 pdfmake_resolved_font_t *n = realloc(
152 124 r->font_cache, new_cap * sizeof(*n));
153 124 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 124 124
154 124 r->font_cache = n;
155 124 r->font_cache_cap = new_cap;
156 }
157 187 slot = &r->font_cache[r->font_cache_len++];
158 187 memset(slot, 0, sizeof(*slot));
159 187 slot->source_dict = font_dict;
160 187 slot->font_dict = font_dict;
161 187 slot->std14_id = -1;
162 187 return slot;
163 }
164  
165 /* Resolve the ToUnicode CMap for a font dict. Returns NULL if not present
166 * or if resolution fails. Caches the result (including NULL) per-font. */
167 7281 static pdfmake_cmap_t *resolve_to_unicode(pdfmake_textract_result_t *r,
168 pdfmake_resolved_font_t *rf)
169 {
170 uint32_t key;
171 pdfmake_obj_t *tu;
172 pdfmake_buf_t buf;
173 pdfmake_err_t err;
174 7281 ▶ if (rf->to_unicode_tried) return rf->to_unicode;
Branch: 1 branch
BranchTrueFalseTotal
if (rf->to_unicode_tried) return rf->to_unicode; 7094 187 7281
175 187 rf->to_unicode_tried = 1;
176  
177 187 ▶ if (!r->reader || !rf->font_dict || !r->arena) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!r->reader || !rf->font_dict || !r->arena) return NULL; 187 0 187
if (!r->reader || !rf->font_dict || !r->arena) return NULL; 187 0 187
if (!r->reader || !rf->font_dict || !r->arena) return NULL; 0 187 187
178  
179 187 key = pdfmake_arena_intern_name(r->arena, "ToUnicode", 9);
180 187 tu = pdfmake_dict_get(rf->font_dict, key);
181 187 ▶ if (!tu || tu->kind != PDFMAKE_REF) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!tu || tu->kind != PDFMAKE_REF) return NULL; 22 165 187
if (!tu || tu->kind != PDFMAKE_REF) return NULL; 0 22 22
182  
183 22 pdfmake_buf_init(&buf);
184 22 err = pdfmake_reader_resolve_stream(
185 22 r->reader, tu->as.ref.num, tu->as.ref.gen, &buf);
186 22 ▶ if (err != PDFMAKE_OK || buf.len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (err != PDFMAKE_OK || buf.len == 0) { 22 0 22
if (err != PDFMAKE_OK || buf.len == 0) { 0 22 22
187 0 pdfmake_buf_free(&buf);
188 0 return NULL;
189 }
190  
191 22 rf->to_unicode = pdfmake_cmap_parse(r->arena, buf.data, buf.len);
192 22 pdfmake_buf_free(&buf);
193 22 return rf->to_unicode;
194 }
195  
196 /* A Type 3 font gives its widths in glyph space, which /FontMatrix maps
197 * to text space (§9.6.4). Every other font's glyph space is a thousandth
198 * of an em, so the table is rescaled to that. */
199 49 static void type3_scale_widths(pdfmake_textract_result_t *r,
200 pdfmake_resolved_font_t *rf)
201 {
202 uint32_t key;
203 pdfmake_obj_t *node;
204 const char *subtype;
205 49 double scale = 0.001;
206 int16_t *scaled;
207 size_t count, i;
208  
209 49 ▶ if (!rf->widths.table) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rf->widths.table) return; 0 49 49
210 49 key = pdfmake_arena_intern_name(r->arena, "Subtype", 7);
211 49 node = pdfmake_dict_get(rf->font_dict, key);
212 49 ▶ if (!node || node->kind != PDFMAKE_NAME) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!node || node->kind != PDFMAKE_NAME) return; 49 0 49
if (!node || node->kind != PDFMAKE_NAME) return; 0 49 49
213 49 subtype = pdfmake_get_name_bytes(r->arena, node);
214 49 ▶ if (!subtype || strcmp(subtype, "Type3") != 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!subtype || strcmp(subtype, "Type3") != 0) return; 49 0 49
if (!subtype || strcmp(subtype, "Type3") != 0) return; 48 1 49
215  
216 1 key = pdfmake_arena_intern_name(r->arena, "FontMatrix", 10);
217 1 node = pdfmake_dict_get(rf->font_dict, key);
218 1 ▶ if (node && node->kind == PDFMAKE_ARRAY && pdfmake_array_len(node) >= 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (node && node->kind == PDFMAKE_ARRAY && pdfmake_array_len(node) >= 1) { 1 0 1
if (node && node->kind == PDFMAKE_ARRAY && pdfmake_array_len(node) >= 1) { 1 0 1
if (node && node->kind == PDFMAKE_ARRAY && pdfmake_array_len(node) >= 1) { 1 0 1
219 1 double first = pdfmake_get_number(pdfmake_array_get(node, 0));
220 1 ▶ if (first != 0) scale = fabs(first);
Branch: 1 branch
BranchTrueFalseTotal
if (first != 0) scale = fabs(first); 1 0 1
221 }
222 1 ▶ if (scale == 0.001) return;
Branch: 1 branch
BranchTrueFalseTotal
if (scale == 0.001) return; 0 1 1
223  
224 1 count = (size_t)(rf->widths.last_char - rf->widths.first_char) + 1;
225 1 scaled = pdfmake_arena_alloc(r->arena, count * sizeof(*scaled));
226 1 ▶ if (!scaled) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!scaled) return; 0 1 1
227 3 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 2 1 3
228 2 double width = rf->widths.table[i] * scale * 1000.0;
229 2 ▶ if (width > 32767) width = 32767;
Branch: 1 branch
BranchTrueFalseTotal
if (width > 32767) width = 32767; 0 2 2
230 2 ▶ if (width < -32767) width = -32767;
Branch: 1 branch
BranchTrueFalseTotal
if (width < -32767) width = -32767; 0 2 2
231 2 ▶ scaled[i] = (int16_t)(width < 0 ? width - 0.5 : width + 0.5);
Branch: 1 branch
BranchTrueFalseTotal
scaled[i] = (int16_t)(width < 0 ? width - 0.5 : width + 0.5); 0 2 2
232 }
233 1 rf->widths.table = scaled;
234 }
235  
236 /* Resolve /Widths or /W for the font, plus font descriptor metrics. */
237 60 static void resolve_widths(pdfmake_textract_result_t *r,
238 pdfmake_resolved_font_t *rf)
239 {
240 60 ▶ if (rf->widths_resolved) return;
Branch: 1 branch
BranchTrueFalseTotal
if (rf->widths_resolved) return; 0 60 60
241 60 rf->widths_resolved = 1;
242  
243 60 ▶ if (!rf->font_dict || !r->arena) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rf->font_dict || !r->arena) { 60 0 60
if (!rf->font_dict || !r->arena) { 0 60 60
244 0 pdfmake_font_widths_init(&rf->widths);
245 0 return;
246 }
247  
248 60 ▶ if (rf->is_cid) {
Branch: 1 branch
BranchTrueFalseTotal
if (rf->is_cid) { 11 49 60
249 11 pdfmake_font_widths_from_cid(r->arena, rf->font_dict, &rf->widths);
250 } else {
251 49 pdfmake_font_widths_from_simple(r->arena, rf->font_dict, &rf->widths);
252 49 type3_scale_widths(r, rf);
253 }
254  
255 /* Phase 6: overlay with TTF metrics from /FontFile2 if present.
256 * For simple fonts this requires the resolved /Encoding first; callers
257 * arrange the sequence in resolve_font(). */
258 60 ▶ if (r->reader) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->reader) { 60 0 60
259 60 const uint32_t *byte_to_uni = NULL;
260 60 ▶ if (!rf->is_cid && rf->encoding_resolved) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rf->is_cid && rf->encoding_resolved) { 49 11 60
if (!rf->is_cid && rf->encoding_resolved) { 49 0 49
261 49 byte_to_uni = rf->encoding.map;
262 }
263 60 pdfmake_font_widths_enhance_with_ttf(
264 r->arena,
265 (struct pdfmake_reader *)r->reader,
266 rf->font_dict,
267 rf->is_cid,
268 byte_to_uni,
269 &rf->widths);
270 }
271 }
272  
273 /* Resolve the /Encoding entry for a simple font. Populates rf->encoding.
274 * Std14 fonts without an explicit /Encoding default to WinAnsi in practice;
275 * other Type1 fonts default to StandardEncoding. */
276 176 static void resolve_encoding(pdfmake_textract_result_t *r,
277 pdfmake_resolved_font_t *rf)
278 {
279 uint32_t enc_key;
280 pdfmake_obj_t *enc_obj;
281 pdfmake_obj_t *resolved;
282 const pdfmake_std14_data_t *d;
283 176 ▶ if (rf->encoding_resolved) return;
Branch: 1 branch
BranchTrueFalseTotal
if (rf->encoding_resolved) return; 0 176 176
284 176 rf->encoding_resolved = 1;
285  
286 176 ▶ if (!rf->font_dict || !r->arena) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rf->font_dict || !r->arena) { 176 0 176
if (!rf->font_dict || !r->arena) { 0 176 176
287 0 pdfmake_font_encoding_init_winansi(&rf->encoding);
288 0 return;
289 }
290  
291 176 enc_key = pdfmake_arena_intern_name(r->arena, "Encoding", 8);
292 176 enc_obj = pdfmake_dict_get(rf->font_dict, enc_key);
293  
294 /* Resolve indirect reference if needed */
295 176 ▶ if (enc_obj && enc_obj->kind == PDFMAKE_REF && r->reader) {
Branch: 3 branches
BranchTrueFalseTotal
if (enc_obj && enc_obj->kind == PDFMAKE_REF && r->reader) { 48 128 176
if (enc_obj && enc_obj->kind == PDFMAKE_REF && r->reader) { 0 48 48
if (enc_obj && enc_obj->kind == PDFMAKE_REF && r->reader) { 0 0 0
296 /* We need to resolve the ref through the parser; expose a tiny helper */
297 /* For now, the parser returns the concrete obj via the reader's parser */
298 0 resolved = pdfmake_parser_resolve(
299 0 ((pdfmake_reader_t *)r->reader)->parser, enc_obj->as.ref);
300 0 ▶ if (resolved) enc_obj = resolved;
Branch: 1 branch
BranchTrueFalseTotal
if (resolved) enc_obj = resolved; 0 0 0
301 }
302  
303 176 ▶ if (enc_obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc_obj) { 48 128 176
304 48 pdfmake_font_encoding_from_dict(r->arena, enc_obj, &rf->encoding);
305 } else {
306 /* No /Encoding: pick a sane default based on Std14 kind */
307 128 ▶ if (rf->std14_id >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rf->std14_id >= 0) { 81 47 128
308 /* Symbol and ZapfDingbats have their own encoding */
309 81 d = pdfmake_std14_get((pdfmake_std14_id_t)rf->std14_id);
310 81 ▶ if (d && d->name) {
Branch: 2 branches
BranchTrueFalseTotal
if (d && d->name) { 81 0 81
if (d && d->name) { 81 0 81
311 162 ▶ if (strcmp(d->name, "Symbol") == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(d->name, "Symbol") == 0) 0 81 81
312 0 pdfmake_font_encoding_init_symbol(&rf->encoding);
313 81 ▶ else if (strcmp(d->name, "ZapfDingbats") == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(d->name, "ZapfDingbats") == 0) 0 81 81
314 0 pdfmake_font_encoding_init_zapfdingbats(&rf->encoding);
315 else
316 81 pdfmake_font_encoding_init_standard(&rf->encoding);
317 } else {
318 0 pdfmake_font_encoding_init_standard(&rf->encoding);
319 }
320 } else {
321 /* Non-Std14 with no /Encoding: StandardEncoding per §9.6.5 */
322 47 pdfmake_font_encoding_init_standard(&rf->encoding);
323 }
324 }
325 }
326  
327 /* If `obj` is an indirect reference, resolve it through the reader's
328 * parser; otherwise return it unchanged. Returns NULL only when an
329 * attempted resolve failed — direct NULL/DICT/etc. inputs pass through. */
330 static PDFMAKE_INLINE pdfmake_obj_t *
331 11150 textract_follow_ref(pdfmake_textract_result_t *r, pdfmake_obj_t *obj)
332 {
333 11150 ▶ if (obj && obj->kind == PDFMAKE_REF && r->reader) {
Branch: 3 branches
BranchTrueFalseTotal
if (obj && obj->kind == PDFMAKE_REF && r->reader) { 11150 0 11150
if (obj && obj->kind == PDFMAKE_REF && r->reader) { 11082 68 11150
if (obj && obj->kind == PDFMAKE_REF && r->reader) { 11082 0 11082
334 11082 pdfmake_reader_t *rd = (pdfmake_reader_t *)r->reader;
335 11082 ▶ if (rd->parser) return pdfmake_parser_resolve(rd->parser, obj->as.ref);
Branch: 1 branch
BranchTrueFalseTotal
if (rd->parser) return pdfmake_parser_resolve(rd->parser, obj->as.ref); 11082 0 11082
336 }
337 68 return obj;
338 }
339  
340 /* Copy a font dictionary with every indirect reference inside it replaced
341 * by the object it names, so /DescendantFonts, /FontDescriptor, /W,
342 * /Widths and /Encoding read the same whether a producer wrote them inline
343 * or as references. Streams stay references: their readers decode them by
344 * object number. /Parent, /Resources and /CharProcs are not followed. */
345 #define TEXTRACT_FLATTEN_DEPTH 8
346  
347 static pdfmake_obj_t
348 4668 textract_flatten(pdfmake_textract_result_t *r, pdfmake_obj_t *obj, int depth)
349 {
350 pdfmake_obj_t out;
351  
352 4668 ▶ if (!obj) return pdfmake_null();
Branch: 1 branch
BranchTrueFalseTotal
if (!obj) return pdfmake_null(); 0 4668 4668
353 4668 ▶ if (depth > TEXTRACT_FLATTEN_DEPTH) return *obj;
Branch: 1 branch
BranchTrueFalseTotal
if (depth > TEXTRACT_FLATTEN_DEPTH) return *obj; 0 4668 4668
354  
355 4668 ▶ if (obj->kind == PDFMAKE_REF) {
Branch: 1 branch
BranchTrueFalseTotal
if (obj->kind == PDFMAKE_REF) { 160 4508 4668
356 160 pdfmake_obj_t *target = textract_follow_ref(r, obj);
357 160 ▶ if (!target || target == obj || target->kind == PDFMAKE_STREAM)
Branch: 3 branches
BranchTrueFalseTotal
if (!target || target == obj || target->kind == PDFMAKE_STREAM) 160 0 160
if (!target || target == obj || target->kind == PDFMAKE_STREAM) 160 0 160
if (!target || target == obj || target->kind == PDFMAKE_STREAM) 69 91 160
358 69 return *obj;
359 91 return textract_flatten(r, target, depth + 1);
360 }
361  
362 4508 ▶ if (obj->kind == PDFMAKE_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (obj->kind == PDFMAKE_ARRAY) { 155 4353 4508
363 155 size_t i, n = pdfmake_array_len(obj);
364 155 out = pdfmake_array_new(r->arena);
365 155 ▶ if (out.kind != PDFMAKE_ARRAY) return *obj;
Branch: 1 branch
BranchTrueFalseTotal
if (out.kind != PDFMAKE_ARRAY) return *obj; 0 155 155
366 3095 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2940 155 3095
367 2940 pdfmake_array_push(r->arena, &out,
368 textract_flatten(r, pdfmake_array_get(obj, i), depth + 1));
369 }
370 155 return out;
371 }
372  
373 4353 ▶ if (obj->kind == PDFMAKE_DICT) {
Branch: 1 branch
BranchTrueFalseTotal
if (obj->kind == PDFMAKE_DICT) { 271 4082 4353
374 pdfmake_dict_iter_t iter;
375 271 uint32_t parent = pdfmake_arena_intern_name(r->arena, "Parent", 6);
376 271 uint32_t resources = pdfmake_arena_intern_name(r->arena, "Resources", 9);
377 271 uint32_t char_procs = pdfmake_arena_intern_name(r->arena, "CharProcs", 9);
378 271 out = pdfmake_dict_new(r->arena);
379 271 ▶ if (out.kind != PDFMAKE_DICT) return *obj;
Branch: 1 branch
BranchTrueFalseTotal
if (out.kind != PDFMAKE_DICT) return *obj; 0 271 271
380 271 pdfmake_dict_iter_init(&iter, obj);
381 1723 ▶ while (pdfmake_dict_iter_next(&iter)) {
Branch: 1 branch
BranchTrueFalseTotal
while (pdfmake_dict_iter_next(&iter)) { 1452 271 1723
382 1452 uint32_t key = iter.current_key;
383 1452 ▶ if (key == parent || key == resources || key == char_procs) {
Branch: 3 branches
BranchTrueFalseTotal
if (key == parent || key == resources || key == char_procs) { 1452 0 1452
if (key == parent || key == resources || key == char_procs) { 1451 1 1452
if (key == parent || key == resources || key == char_procs) { 1 1450 1451
384 2 pdfmake_dict_set(r->arena, &out, key, *iter.current_value);
385 } else {
386 1450 pdfmake_dict_set(r->arena, &out, key,
387 textract_flatten(r, iter.current_value, depth + 1));
388 }
389 }
390 271 return out;
391 }
392  
393 4082 return *obj;
394 }
395  
396 /* For Type0/CID fonts, derive writing mode and default vertical advance:
397 * - /Encoding: name ending in "-V" → vertical; a CMap stream with
398 * /WMode 1 overrides (§9.7.5.2).
399 * - When vertical, /DescendantFonts[0]/DW2 = [v_origin_y v_advance]
400 * replaces the -1000 spec default for default_v_advance. */
401 static void
402 11 resolve_cid_font_modes(pdfmake_textract_result_t *r,
403 pdfmake_obj_t *font_dict,
404 pdfmake_resolved_font_t *rf)
405 {
406 uint32_t enc_key;
407 pdfmake_obj_t *enc;
408 const char *nm;
409 size_t ln;
410 pdfmake_dict_t *sd;
411 pdfmake_obj_t sd_obj;
412 uint32_t wm_key;
413 pdfmake_obj_t *wm;
414 uint32_t df_key;
415 pdfmake_obj_t *df;
416 pdfmake_obj_t *desc;
417 uint32_t dw2_key;
418 pdfmake_obj_t *dw2;
419 pdfmake_obj_t *a;
420  
421 11 enc_key = pdfmake_arena_intern_name(r->arena, "Encoding", 8);
422 11 enc = textract_follow_ref(r, pdfmake_dict_get(font_dict, enc_key));
423  
424 11 ▶ if (enc && enc->kind == PDFMAKE_NAME) {
Branch: 2 branches
BranchTrueFalseTotal
if (enc && enc->kind == PDFMAKE_NAME) { 11 0 11
if (enc && enc->kind == PDFMAKE_NAME) { 11 0 11
425 11 nm = pdfmake_get_name_bytes(r->arena, enc);
426 11 ▶ ln = nm ? strlen(nm) : 0;
Branch: 1 branch
BranchTrueFalseTotal
ln = nm ? strlen(nm) : 0; 11 0 11
427 11 ▶ if (ln >= 2 && nm[ln - 2] == '-' &&
Branch: 2 branches
BranchTrueFalseTotal
if (ln >= 2 && nm[ln - 2] == '-' && 11 0 11
if (ln >= 2 && nm[ln - 2] == '-' && 11 0 11
428 11 ▶ (nm[ln - 1] == 'V' || nm[ln - 1] == 'v')) {
Branch: 2 branches
BranchTrueFalseTotal
(nm[ln - 1] == 'V' || nm[ln - 1] == 'v')) { 10 1 11
(nm[ln - 1] == 'V' || nm[ln - 1] == 'v')) { 0 10 10
429 1 rf->wmode = 1;
430 }
431 0 ▶ } else if (enc && enc->kind == PDFMAKE_STREAM) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (enc && enc->kind == PDFMAKE_STREAM) { 0 0 0
} else if (enc && enc->kind == PDFMAKE_STREAM) { 0 0 0
432 0 sd = pdfmake_stream_dict(enc);
433 0 ▶ if (sd) {
Branch: 1 branch
BranchTrueFalseTotal
if (sd) { 0 0 0
434 0 sd_obj.kind = PDFMAKE_DICT;
435 0 sd_obj.as.dict = sd;
436 0 wm_key = pdfmake_arena_intern_name(r->arena, "WMode", 5);
437 0 wm = pdfmake_dict_get(&sd_obj, wm_key);
438 0 ▶ if (wm && wm->kind == PDFMAKE_INT && wm->as.i == 1) rf->wmode = 1;
Branch: 3 branches
BranchTrueFalseTotal
if (wm && wm->kind == PDFMAKE_INT && wm->as.i == 1) rf->wmode = 1; 0 0 0
if (wm && wm->kind == PDFMAKE_INT && wm->as.i == 1) rf->wmode = 1; 0 0 0
if (wm && wm->kind == PDFMAKE_INT && wm->as.i == 1) rf->wmode = 1; 0 0 0
439 }
440 }
441 11 ▶ if (!rf->wmode) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rf->wmode) return; 10 1 11
442  
443 1 df_key = pdfmake_arena_intern_name(r->arena, "DescendantFonts", 15);
444 1 df = textract_follow_ref(r, pdfmake_dict_get(font_dict, df_key));
445 1 ▶ if (!df || df->kind != PDFMAKE_ARRAY || pdfmake_array_len(df) == 0) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!df || df->kind != PDFMAKE_ARRAY || pdfmake_array_len(df) == 0) return; 1 0 1
if (!df || df->kind != PDFMAKE_ARRAY || pdfmake_array_len(df) == 0) return; 1 0 1
if (!df || df->kind != PDFMAKE_ARRAY || pdfmake_array_len(df) == 0) return; 0 1 1
446  
447 1 desc = textract_follow_ref(r, pdfmake_array_get(df, 0));
448 1 ▶ if (!desc || desc->kind != PDFMAKE_DICT) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!desc || desc->kind != PDFMAKE_DICT) return; 1 0 1
if (!desc || desc->kind != PDFMAKE_DICT) return; 0 1 1
449  
450 1 dw2_key = pdfmake_arena_intern_name(r->arena, "DW2", 3);
451 1 dw2 = pdfmake_dict_get(desc, dw2_key);
452 1 ▶ if (dw2 && dw2->kind == PDFMAKE_ARRAY && pdfmake_array_len(dw2) >= 2) {
Branch: 3 branches
BranchTrueFalseTotal
if (dw2 && dw2->kind == PDFMAKE_ARRAY && pdfmake_array_len(dw2) >= 2) { 1 0 1
if (dw2 && dw2->kind == PDFMAKE_ARRAY && pdfmake_array_len(dw2) >= 2) { 1 0 1
if (dw2 && dw2->kind == PDFMAKE_ARRAY && pdfmake_array_len(dw2) >= 2) { 1 0 1
453 1 a = pdfmake_array_get(dw2, 1);
454 1 ▶ if (a) rf->default_v_advance = (int16_t)pdfmake_get_number(a);
Branch: 1 branch
BranchTrueFalseTotal
if (a) rf->default_v_advance = (int16_t)pdfmake_get_number(a); 1 0 1
455 }
456 }
457  
458 /* Name the Adobe CJK collection in /DescendantFonts[0]/CIDSystemInfo. */
459 11 static int cid_collection_of(pdfmake_textract_result_t *r, pdfmake_obj_t *font_dict)
460 {
461 uint32_t key;
462 pdfmake_obj_t *node;
463  
464 11 key = pdfmake_arena_intern_name(r->arena, "DescendantFonts", 15);
465 11 node = textract_follow_ref(r, pdfmake_dict_get(font_dict, key));
466 11 ▶ if (!node || node->kind != PDFMAKE_ARRAY || pdfmake_array_len(node) == 0)
Branch: 3 branches
BranchTrueFalseTotal
if (!node || node->kind != PDFMAKE_ARRAY || pdfmake_array_len(node) == 0) 11 0 11
if (!node || node->kind != PDFMAKE_ARRAY || pdfmake_array_len(node) == 0) 11 0 11
if (!node || node->kind != PDFMAKE_ARRAY || pdfmake_array_len(node) == 0) 0 11 11
467 0 return PDFMAKE_CID_COLLECTION_NONE;
468 11 node = textract_follow_ref(r, pdfmake_array_get(node, 0));
469 11 ▶ if (!node || node->kind != PDFMAKE_DICT) return PDFMAKE_CID_COLLECTION_NONE;
Branch: 2 branches
BranchTrueFalseTotal
if (!node || node->kind != PDFMAKE_DICT) return PDFMAKE_CID_COLLECTION_NONE; 11 0 11
if (!node || node->kind != PDFMAKE_DICT) return PDFMAKE_CID_COLLECTION_NONE; 0 11 11
470  
471 11 key = pdfmake_arena_intern_name(r->arena, "CIDSystemInfo", 13);
472 11 node = textract_follow_ref(r, pdfmake_dict_get(node, key));
473 11 ▶ if (!node || node->kind != PDFMAKE_DICT) return PDFMAKE_CID_COLLECTION_NONE;
Branch: 2 branches
BranchTrueFalseTotal
if (!node || node->kind != PDFMAKE_DICT) return PDFMAKE_CID_COLLECTION_NONE; 11 0 11
if (!node || node->kind != PDFMAKE_DICT) return PDFMAKE_CID_COLLECTION_NONE; 0 11 11
474  
475 11 key = pdfmake_arena_intern_name(r->arena, "Ordering", 8);
476 11 node = textract_follow_ref(r, pdfmake_dict_get(node, key));
477 11 ▶ if (!node || node->kind != PDFMAKE_STR) return PDFMAKE_CID_COLLECTION_NONE;
Branch: 2 branches
BranchTrueFalseTotal
if (!node || node->kind != PDFMAKE_STR) return PDFMAKE_CID_COLLECTION_NONE; 11 0 11
if (!node || node->kind != PDFMAKE_STR) return PDFMAKE_CID_COLLECTION_NONE; 0 11 11
478  
479 11 return pdfmake_cid_collection_by_ordering(
480 11 (const char *)node->as.str.bytes, node->as.str.len);
481 }
482  
483 /* Say what a character code is under a Type0 font's /Encoding. */
484 11 static int cid_code_kind_of(pdfmake_textract_result_t *r, pdfmake_obj_t *font_dict)
485 {
486 11 uint32_t key = pdfmake_arena_intern_name(r->arena, "Encoding", 8);
487 11 pdfmake_obj_t *enc = textract_follow_ref(r, pdfmake_dict_get(font_dict, key));
488 const char *name;
489  
490 11 ▶ if (!enc) return PDFMAKE_CID_CODE_IS_CID;
Branch: 1 branch
BranchTrueFalseTotal
if (!enc) return PDFMAKE_CID_CODE_IS_CID; 0 11 11
491 11 ▶ if (enc->kind != PDFMAKE_NAME) return PDFMAKE_CID_CODE_IS_UNKNOWN;
Branch: 1 branch
BranchTrueFalseTotal
if (enc->kind != PDFMAKE_NAME) return PDFMAKE_CID_CODE_IS_UNKNOWN; 0 11 11
492 11 name = pdfmake_get_name_bytes(r->arena, enc);
493 11 ▶ if (!name) return PDFMAKE_CID_CODE_IS_UNKNOWN;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return PDFMAKE_CID_CODE_IS_UNKNOWN; 0 11 11
494 11 ▶ if (strcmp(name, "Identity-H") == 0 || strcmp(name, "Identity-V") == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (strcmp(name, "Identity-H") == 0 || strcmp(name, "Identity-V") == 0) 3 8 11
if (strcmp(name, "Identity-H") == 0 || strcmp(name, "Identity-V") == 0) 1 2 3
495 9 return PDFMAKE_CID_CODE_IS_CID;
496 2 ▶ if (strncmp(name, "Uni", 3) == 0
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(name, "Uni", 3) == 0 1 1 2
497 1 ▶ && (strstr(name, "-UCS2-") || strstr(name, "-UTF16-")))
Branch: 2 branches
BranchTrueFalseTotal
&& (strstr(name, "-UCS2-") || strstr(name, "-UTF16-"))) 0 1 1
&& (strstr(name, "-UCS2-") || strstr(name, "-UTF16-"))) 0 0 0
498 1 return PDFMAKE_CID_CODE_IS_UTF16;
499 1 return PDFMAKE_CID_CODE_IS_UNKNOWN;
500 }
501  
502 /* Resolve the font dict into a cached resolved_font. NULL if dict is NULL. */
503  
504 10922 static pdfmake_resolved_font_t *resolve_font(
505 pdfmake_textract_result_t *r, pdfmake_obj_t *font_dict)
506 {
507 pdfmake_resolved_font_t *rf;
508 uint32_t bf_key;
509 pdfmake_obj_t *bf;
510 const char *name;
511 uint32_t sub_key;
512 pdfmake_obj_t *sub;
513 const char *st;
514 10922 ▶ if (!font_dict || !r->arena) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!font_dict || !r->arena) return NULL; 10922 0 10922
if (!font_dict || !r->arena) return NULL; 0 10922 10922
515  
516 /* Follow indirect references — resources often hold refs, not inline dicts */
517 10922 font_dict = textract_follow_ref(r, font_dict);
518 10922 ▶ if (!font_dict || font_dict->kind != PDFMAKE_DICT) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!font_dict || font_dict->kind != PDFMAKE_DICT) return NULL; 10922 0 10922
if (!font_dict || font_dict->kind != PDFMAKE_DICT) return NULL; 0 10922 10922
519  
520 10922 rf = font_cache_find(r, font_dict);
521 10922 ▶ if (rf) return rf;
Branch: 1 branch
BranchTrueFalseTotal
if (rf) return rf; 10735 187 10922
522 187 rf = font_cache_add(r, font_dict);
523 187 ▶ if (!rf) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!rf) return NULL; 0 187 187
524  
525 187 ▶ if (r->reader) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->reader) { 187 0 187
526 187 pdfmake_obj_t *flat = pdfmake_arena_alloc(r->arena, sizeof(*flat));
527 187 ▶ if (flat) {
Branch: 1 branch
BranchTrueFalseTotal
if (flat) { 187 0 187
528 187 *flat = textract_flatten(r, font_dict, 0);
529 187 ▶ if (flat->kind == PDFMAKE_DICT) {
Branch: 1 branch
BranchTrueFalseTotal
if (flat->kind == PDFMAKE_DICT) { 187 0 187
530 187 rf->font_dict = flat;
531 187 font_dict = flat;
532 }
533 }
534 }
535  
536 /* Std14 lookup */
537 187 bf_key = pdfmake_arena_intern_name(r->arena, "BaseFont", 8);
538 187 bf = pdfmake_dict_get(font_dict, bf_key);
539 187 ▶ if (bf && bf->kind == PDFMAKE_NAME) {
Branch: 2 branches
BranchTrueFalseTotal
if (bf && bf->kind == PDFMAKE_NAME) { 186 1 187
if (bf && bf->kind == PDFMAKE_NAME) { 186 0 186
540 186 name = pdfmake_get_name_bytes(r->arena, bf);
541 186 ▶ if (name) rf->std14_id = pdfmake_std14_lookup(name);
Branch: 1 branch
BranchTrueFalseTotal
if (name) rf->std14_id = pdfmake_std14_lookup(name); 186 0 186
542 }
543  
544 /* Type0/CID detection */
545 187 sub_key = pdfmake_arena_intern_name(r->arena, "Subtype", 7);
546 187 sub = pdfmake_dict_get(font_dict, sub_key);
547 187 ▶ if (sub && sub->kind == PDFMAKE_NAME) {
Branch: 2 branches
BranchTrueFalseTotal
if (sub && sub->kind == PDFMAKE_NAME) { 187 0 187
if (sub && sub->kind == PDFMAKE_NAME) { 187 0 187
548 187 st = pdfmake_get_name_bytes(r->arena, sub);
549 187 ▶ if (st && strcmp(st, "Type0") == 0) rf->is_cid = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (st && strcmp(st, "Type0") == 0) rf->is_cid = 1; 187 0 187
if (st && strcmp(st, "Type0") == 0) rf->is_cid = 1; 11 176 187
550 }
551  
552 /* Phase 14: writing mode + default vertical advance (Type0 only). */
553 187 rf->wmode = 0;
554 187 rf->default_v_advance = -1000; /* spec default for vertical */
555 187 ▶ if (rf->is_cid) resolve_cid_font_modes(r, font_dict, rf);
Branch: 1 branch
BranchTrueFalseTotal
if (rf->is_cid) resolve_cid_font_modes(r, font_dict, rf); 11 176 187
556 187 ▶ if (rf->is_cid) {
Branch: 1 branch
BranchTrueFalseTotal
if (rf->is_cid) { 11 176 187
557 11 rf->cid_collection = cid_collection_of(r, font_dict);
558 11 rf->cid_code = cid_code_kind_of(r, font_dict);
559 }
560  
561 /* Resolve /Encoding for simple fonts only; CID fonts use ToUnicode */
562 187 ▶ if (!rf->is_cid) resolve_encoding(r, rf);
Branch: 1 branch
BranchTrueFalseTotal
if (!rf->is_cid) resolve_encoding(r, rf); 176 11 187
563  
564 /* Resolve widths + descriptor metrics for all non-Std14 fonts */
565 187 ▶ if (rf->std14_id < 0) resolve_widths(r, rf);
Branch: 1 branch
BranchTrueFalseTotal
if (rf->std14_id < 0) resolve_widths(r, rf); 60 127 187
566  
567 187 return rf;
568 }
569  
570 /*============================================================================
571 * Raw glyph collection
572 *==========================================================================*/
573  
574 19643 static int result_push_glyph(pdfmake_textract_result_t *r,
575 const pdfmake_text_glyph_t *g) {
576 19643 ▶ if (r->raw_len >= r->raw_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->raw_len >= r->raw_cap) { 203 19440 19643
577 203 ▶ size_t new_cap = r->raw_cap == 0 ? 64 : r->raw_cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = r->raw_cap == 0 ? 64 : r->raw_cap * 2; 93 110 203
578 203 pdfmake_text_glyph_t *new_arr = realloc(r->raw_glyphs,
579 new_cap * sizeof(pdfmake_text_glyph_t));
580 203 ▶ if (!new_arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!new_arr) return 0; 0 203 203
581 203 r->raw_glyphs = new_arr;
582 203 r->raw_cap = new_cap;
583 }
584 19643 r->raw_glyphs[r->raw_len++] = *g;
585 19643 return 1;
586 }
587  
588 /*============================================================================
589 * Visitor: on_text_show callback
590 *
591 * Called for each Tj/TJ/' /" operator. Decodes bytes, computes glyph
592 * positions in user space, emits raw glyphs.
593 *==========================================================================*/
594  
595 /* Legacy: resolve the Standard 14 font ID from the graphics state font dict.
596 * Superseded by resolve_font() + font cache. Kept for potential reuse. */
597 __attribute__((unused))
598 0 static int resolve_std14(const pdfmake_gstate_t *gs, pdfmake_arena_t *arena) {
599 uint32_t basefont_key;
600 pdfmake_obj_t *basefont;
601 const char *name;
602 0 ▶ if (!gs->font || !arena) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!gs->font || !arena) return -1; 0 0 0
if (!gs->font || !arena) return -1; 0 0 0
603 0 ▶ if (gs->font->kind != PDFMAKE_DICT) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (gs->font->kind != PDFMAKE_DICT) return -1; 0 0 0
604  
605 /* Look up /BaseFont in the font dictionary */
606 0 basefont_key = pdfmake_arena_intern_name(arena, "BaseFont", 8);
607 0 basefont = pdfmake_dict_get(gs->font, basefont_key);
608 0 ▶ if (!basefont || basefont->kind != PDFMAKE_NAME) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!basefont || basefont->kind != PDFMAKE_NAME) return -1; 0 0 0
if (!basefont || basefont->kind != PDFMAKE_NAME) return -1; 0 0 0
609  
610 0 name = pdfmake_get_name_bytes(arena, basefont);
611 0 ▶ if (!name) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return -1; 0 0 0
612  
613 0 return pdfmake_std14_lookup(name);
614 }
615  
616 /* Horizontal + vertical advance for a glyph, plus a reliability flag that
617 * feeds the downstream word-boundary heuristic. */
618 typedef struct {
619 double horizontal; /* x-advance in text space (Tj/TJ direction) */
620 double vertical; /* y-advance magnitude for vertical writing */
621 int reliable; /* 1 iff horizontal came from real font metrics */
622 } textract_advance_t;
623  
624 /* Map one character code to its Unicode codepoint(s). Returns the count
625 * (always ≥ 1). Ligature-style ToUnicode mappings may yield several
626 * codepoints (e.g. "fi" → U+0066 U+0069); uni_list must hold at least
627 * PDFMAKE_CMAP_MAX_UNI entries. On failure we emit U+FFFD. */
628 static PDFMAKE_INLINE size_t
629 39259 textract_code_to_unicode(const pdfmake_resolved_font_t *rf,
630 pdfmake_cmap_t *to_uni,
631 int is_cid, uint32_t code,
632 uint32_t *uni_list)
633 {
634 39259 ▶ if (to_uni) {
Branch: 1 branch
BranchTrueFalseTotal
if (to_uni) { 240 39019 39259
635 240 size_t cnt = 0;
636 240 ▶ if (pdfmake_cmap_lookup(to_uni, code, uni_list, &cnt) && cnt > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (pdfmake_cmap_lookup(to_uni, code, uni_list, &cnt) && cnt > 0) { 240 0 240
if (pdfmake_cmap_lookup(to_uni, code, uni_list, &cnt) && cnt > 0) { 240 0 240
637 240 return cnt;
638 }
639 0 uni_list[0] = 0xFFFD;
640 0 return 1;
641 }
642 39019 ▶ if (!is_cid && rf && rf->encoding_resolved) {
Branch: 3 branches
BranchTrueFalseTotal
if (!is_cid && rf && rf->encoding_resolved) { 38965 54 39019
if (!is_cid && rf && rf->encoding_resolved) { 38965 0 38965
if (!is_cid && rf && rf->encoding_resolved) { 38965 0 38965
643 38965 uint32_t cp = rf->encoding.map[code & 0xFF];
644 38965 ▶ uni_list[0] = cp ? cp : 0xFFFD;
Branch: 1 branch
BranchTrueFalseTotal
uni_list[0] = cp ? cp : 0xFFFD; 38965 0 38965
645 38965 return 1;
646 }
647 54 ▶ if (!is_cid) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_cid) { 0 54 54
648 0 uni_list[0] = pdfmake_winansi_to_unicode((uint8_t)code);
649 0 return 1;
650 }
651 /* A CID font from an Adobe CJK collection. Under an Identity encoding
652 * the code is the CID and the collection's table gives its text.
653 * Under a Uni... CMap the code is the UTF-16 unit itself. Any other
654 * CMap needs its own code table, so the text is reported unknown. */
655 54 ▶ if (rf && rf->cid_collection != PDFMAKE_CID_COLLECTION_NONE) {
Branch: 2 branches
BranchTrueFalseTotal
if (rf && rf->cid_collection != PDFMAKE_CID_COLLECTION_NONE) { 54 0 54
if (rf && rf->cid_collection != PDFMAKE_CID_COLLECTION_NONE) { 50 4 54
656 50 size_t count = 0;
657 50 ▶ if (rf->cid_code == PDFMAKE_CID_CODE_IS_CID) {
Branch: 1 branch
BranchTrueFalseTotal
if (rf->cid_code == PDFMAKE_CID_CODE_IS_CID) { 42 8 50
658 42 count = pdfmake_cid_to_unicode(rf->cid_collection, code,
659 uni_list, PDFMAKE_CMAP_MAX_UNI);
660 8 ▶ } else if (rf->cid_code == PDFMAKE_CID_CODE_IS_UTF16
Branch: 1 branch
BranchTrueFalseTotal
} else if (rf->cid_code == PDFMAKE_CID_CODE_IS_UTF16 4 4 8
661 4 ▶ && code != 0 && (code < 0xD800 || code > 0xDFFF)) {
Branch: 3 branches
BranchTrueFalseTotal
&& code != 0 && (code < 0xD800 || code > 0xDFFF)) { 4 0 4
&& code != 0 && (code < 0xD800 || code > 0xDFFF)) { 0 4 4
&& code != 0 && (code < 0xD800 || code > 0xDFFF)) { 0 0 0
662 4 uni_list[0] = code;
663 4 count = 1;
664 }
665 50 ▶ if (count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count == 0) { 6 44 50
666 6 uni_list[0] = 0xFFFD;
667 6 count = 1;
668 }
669 50 return count;
670 }
671 /* CID fallback: use the raw code. */
672 4 uni_list[0] = code;
673 4 return 1;
674 }
675  
676 /* Compute the glyph advance (both horizontal and vertical) in text-space
677 * units, trying /Widths first, then Std14 AFM, then a 0.5-em / 0.25-em
678 * placeholder. Vertical advance prefers the font's default_v_advance
679 * (CIDFont DW2) when available. */
680 static PDFMAKE_INLINE textract_advance_t
681 39259 textract_glyph_advance(const pdfmake_resolved_font_t *rf,
682 int std14_id, int is_cid,
683 uint32_t code, uint32_t unicode,
684 double font_size)
685 {
686 39259 textract_advance_t adv = { 0.0, 0.0, 0 };
687  
688 /* A font that states its widths is believed, a stated 0 included:
689 * that is a combining mark, and giving it a guessed width instead
690 * pushes the rest of the line along. A CID font always states one,
691 * through /W or /DW. A simple font states one for the codes inside
692 * /FirstChar to /LastChar. */
693 39259 int stated = 0;
694 39259 ▶ if (rf && rf->widths_resolved) {
Branch: 2 branches
BranchTrueFalseTotal
if (rf && rf->widths_resolved) { 39259 0 39259
if (rf && rf->widths_resolved) { 12798 26461 39259
695 12798 ▶ if (is_cid) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_cid) { 68 12730 12798
696 68 stated = 1;
697 12730 ▶ } else if (rf->widths.table && code >= rf->widths.first_char
Branch: 2 branches
BranchTrueFalseTotal
} else if (rf->widths.table && code >= rf->widths.first_char 12730 0 12730
} else if (rf->widths.table && code >= rf->widths.first_char 12730 0 12730
698 12730 ▶ && code <= rf->widths.last_char) {
Branch: 1 branch
BranchTrueFalseTotal
&& code <= rf->widths.last_char) { 12730 0 12730
699 12730 stated = 1;
700 }
701 }
702 39259 ▶ if (stated) {
Branch: 1 branch
BranchTrueFalseTotal
if (stated) { 12798 26461 39259
703 12798 int16_t w1000 = pdfmake_font_widths_lookup(&rf->widths, code);
704 12798 adv.horizontal = (double)w1000 / 1000.0 * font_size;
705 12798 adv.reliable = 1;
706 52922 ▶ } else if (std14_id >= 0 && !is_cid) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (std14_id >= 0 && !is_cid) { 26461 0 26461
} else if (std14_id >= 0 && !is_cid) { 26461 0 26461
707 26461 int w = pdfmake_std14_width((pdfmake_std14_id_t)std14_id, unicode);
708 26461 ▶ if (w > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (w > 0) { 26461 0 26461
709 26461 adv.horizontal = (double)w / 1000.0 * font_size;
710 26461 adv.reliable = 1;
711 } else {
712 0 adv.horizontal = 0.25 * font_size;
713 }
714 } else {
715 0 adv.horizontal = 0.5 * font_size;
716 }
717  
718 /* Default vertical advance falls back to horizontal magnitude. CJK
719 * fonts with DW2/W2 override this via rf->default_v_advance (negative
720 * because vertical flow is downward in text space). */
721 39259 adv.vertical = adv.horizontal;
722 39259 ▶ if (rf) {
Branch: 1 branch
BranchTrueFalseTotal
if (rf) { 39259 0 39259
723 39259 ▶ double mag = (double)(rf->default_v_advance < 0
Branch: 1 branch
BranchTrueFalseTotal
double mag = (double)(rf->default_v_advance < 0 39259 0 39259
724 39259 ? -rf->default_v_advance : rf->default_v_advance);
725 39259 ▶ if (mag > 0) adv.vertical = mag / 1000.0 * font_size;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > 0) adv.vertical = mag / 1000.0 * font_size; 39259 0 39259
726 }
727 39259 return adv;
728 }
729  
730 /* Transform a text-space point through the text matrix and CTM to final
731 * user/device coordinates. */
732 static PDFMAKE_INLINE void
733 98200 textract_text_to_device(const double *tm, const double *ctm,
734 double gx, double gy, double *fx, double *fy)
735 {
736 98200 double ux = tm[0] * gx + tm[2] * gy + tm[4];
737 98200 double uy = tm[1] * gx + tm[3] * gy + tm[5];
738 98200 *fx = ctm[0] * ux + ctm[2] * uy + ctm[4];
739 98200 *fy = ctm[1] * ux + ctm[3] * uy + ctm[5];
740 98200 }
741  
742 3641 static void textract_on_text_show(void *ctx,
743 const pdfmake_gstate_t *gs,
744 const uint8_t *bytes,
745 size_t len)
746 {
747 3641 pdfmake_textract_result_t *result = (pdfmake_textract_result_t *)ctx;
748 double font_size;
749 double char_space;
750 double word_space;
751 double h_scale;
752 double rise;
753 pdfmake_resolved_font_t *rf;
754 int std14_id;
755 int is_cid;
756 int is_vertical;
757 pdfmake_cmap_t *to_uni;
758 double ascent_ratio;
759 double descent_ratio;
760 const pdfmake_std14_data_t *data;
761 double trm[6];
762 double theta;
763 double cos_theta;
764 double sin_theta;
765 int angle;
766 double sx;
767 double sy;
768 double effective_size;
769 double ascent;
770 double descent;
771 double accum_x;
772 size_t pos;
773 uint32_t code;
774 size_t code_width;
775 uint32_t uni_list[PDFMAKE_CMAP_MAX_UNI];
776 size_t uni_list_n;
777 uint32_t unicode;
778 textract_advance_t glyph_adv;
779 double gx;
780 double gy;
781 double fx;
782 double fy;
783 double adv;
784 double sub_adv;
785 pdfmake_text_glyph_t g;
786 double ws;
787 size_t k;
788 3642 ▶ if (!gs || !bytes || len == 0) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!gs || !bytes || len == 0) return; 3641 0 3641
if (!gs || !bytes || len == 0) return; 3641 0 3641
if (!gs || !bytes || len == 0) return; 0 3641 3641
789  
790 /* Phase 11: skip invisible text (Tr=3) when the caller opted out.
791 * The interpreter still advances text_matrix via get_string_advance,
792 * so positioning of subsequent visible glyphs stays correct. */
793 3641 ▶ if (!result->include_invisible &&
Branch: 1 branch
BranchTrueFalseTotal
if (!result->include_invisible && 3 3638 3641
794 3 ▶ gs->render_mode == PDFMAKE_RENDER_INVISIBLE) {
Branch: 1 branch
BranchTrueFalseTotal
gs->render_mode == PDFMAKE_RENDER_INVISIBLE) { 1 2 3
795 1 return;
796 }
797  
798 3640 font_size = gs->font_size;
799 3640 char_space = gs->char_space;
800 3640 word_space = gs->word_space;
801 3640 h_scale = gs->h_scale / 100.0; /* Tz is percentage */
802 3640 rise = gs->rise;
803  
804 /* Resolve the font (cached per-font dict) */
805 3640 rf = resolve_font(result, gs->font);
806 3640 ▶ std14_id = rf ? rf->std14_id : -1;
Branch: 1 branch
BranchTrueFalseTotal
std14_id = rf ? rf->std14_id : -1; 3640 0 3640
807 3640 ▶ is_cid = rf ? rf->is_cid : 0;
Branch: 1 branch
BranchTrueFalseTotal
is_cid = rf ? rf->is_cid : 0; 3640 0 3640
808 3640 ▶ is_vertical = rf ? rf->wmode : 0; /* Phase 14 */
Branch: 1 branch
BranchTrueFalseTotal
is_vertical = rf ? rf->wmode : 0; /* Phase 14 */ 3640 0 3640
809 3640 ▶ to_uni = rf ? resolve_to_unicode(result, rf) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
to_uni = rf ? resolve_to_unicode(result, rf) : NULL; 3640 0 3640
810  
811 /* Font metrics for ascent/descent.
812 * Priority: Std14 AFM data → /FontDescriptor → 0.8/-0.2 fallback. */
813 3640 ascent_ratio = 0.8;
814 3640 descent_ratio = -0.2;
815 3640 ▶ if (std14_id >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (std14_id >= 0) { 1071 2569 3640
816 1071 data = pdfmake_std14_get((pdfmake_std14_id_t)std14_id);
817 1071 ▶ if (data) {
Branch: 1 branch
BranchTrueFalseTotal
if (data) { 1071 0 1071
818 1071 ascent_ratio = data->metrics.ascent / 1000.0;
819 1071 descent_ratio = data->metrics.descent / 1000.0;
820 }
821 2569 ▶ } else if (rf && rf->widths_resolved) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (rf && rf->widths_resolved) { 2569 0 2569
} else if (rf && rf->widths_resolved) { 2569 0 2569
822 2569 ▶ if (rf->widths.ascent) ascent_ratio = rf->widths.ascent / 1000.0;
Branch: 1 branch
BranchTrueFalseTotal
if (rf->widths.ascent) ascent_ratio = rf->widths.ascent / 1000.0; 2549 20 2569
823 2569 ▶ if (rf->widths.descent) descent_ratio = rf->widths.descent / 1000.0;
Branch: 1 branch
BranchTrueFalseTotal
if (rf->widths.descent) descent_ratio = rf->widths.descent / 1000.0; 2549 20 2569
824 }
825 /* Text space to page space is the text matrix then the CTM. Every
826 * glyph box is built in text space and carried through both, so a
827 * scaled, flipped or rotated CTM lands the box where the glyph is
828 * painted. */
829 3640 pdfmake_matrix_multiply(trm, gs->text_matrix, gs->ctm);
830 3640 sx = sqrt(trm[0] * trm[0] + trm[1] * trm[1]);
831 3640 sy = sqrt(trm[2] * trm[2] + trm[3] * trm[3]);
832 3640 ▶ if (sy == 0) sy = sx;
Branch: 1 branch
BranchTrueFalseTotal
if (sy == 0) sy = sx; 0 3640 3640
833 3640 effective_size = fabs(font_size) * sy;
834 3640 ▶ theta = is_vertical ? 0.0 : atan2(trm[1], trm[0]);
Branch: 1 branch
BranchTrueFalseTotal
theta = is_vertical ? 0.0 : atan2(trm[1], trm[0]); 3639 1 3640
835 3640 cos_theta = cos(theta);
836 3640 sin_theta = sin(theta);
837 3640 angle = (int)floor(theta * 180.0 / 3.14159265358979323846 + 0.5);
838 3640 ▶ if (angle <= -180) angle += 360;
Branch: 1 branch
BranchTrueFalseTotal
if (angle <= -180) angle += 360; 0 3640 3640
839 3640 ascent = ascent_ratio * font_size;
840 3640 descent = descent_ratio * font_size;
841  
842 /* Displacement through the string in text space: along +x for
843 * horizontal writing, along -y for vertical. */
844 3640 accum_x = 0.0;
845  
846 3640 pos = 0;
847 23264 ▶ while (pos < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < len) { 19624 3640 23264
848 /* CID fonts are 2-byte by default; simple fonts are 1-byte. */
849 19624 ▶ if (is_cid && pos + 1 < len) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_cid && pos + 1 < len) { 34 19590 19624
if (is_cid && pos + 1 < len) { 34 0 34
850 34 code = ((uint32_t)bytes[pos] << 8) | bytes[pos + 1];
851 34 code_width = 2;
852 } else {
853 19590 code = bytes[pos];
854 19590 code_width = 1;
855 }
856  
857 /* A ligature mapping yields several codepoints for one glyph:
858 * emit one sub-glyph per codepoint, the advance split evenly. */
859 19624 uni_list_n = textract_code_to_unicode(rf, to_uni, is_cid, code, uni_list);
860 19624 unicode = uni_list[0];
861  
862 19624 glyph_adv = textract_glyph_advance(
863 rf, std14_id, is_cid, code, unicode, font_size);
864  
865 19624 pos += code_width;
866  
867 19624 ▶ gx = is_vertical ? 0.0 : accum_x;
Branch: 1 branch
BranchTrueFalseTotal
gx = is_vertical ? 0.0 : accum_x; 19621 3 19624
868 19624 ▶ gy = is_vertical ? accum_x : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
gy = is_vertical ? accum_x : 0.0; 3 19621 19624
869  
870 19624 adv = is_vertical ? glyph_adv.vertical
871 19624 ▶ : glyph_adv.horizontal * h_scale;
Branch: 1 branch
BranchTrueFalseTotal
: glyph_adv.horizontal * h_scale; 3 19621 19624
872 19624 ▶ sub_adv = (uni_list_n > 0) ? adv / (double)uni_list_n : adv;
Branch: 1 branch
BranchTrueFalseTotal
sub_adv = (uni_list_n > 0) ? adv / (double)uni_list_n : adv; 19624 0 19624
873  
874 39264 ▶ for (k = 0; k < uni_list_n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < uni_list_n; k++) { 19640 19624 39264
875 double tx0, tx1, ty0, ty1, origin_x, origin_y;
876 double corner_x[4], corner_y[4];
877 int corner;
878  
879 19640 ▶ if (is_vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_vertical) { 3 19637 19640
880 3 tx0 = gx - font_size * 0.5;
881 3 tx1 = gx + font_size * 0.5;
882 3 ty1 = gy + rise - sub_adv * (double)k;
883 3 ty0 = gy + rise - sub_adv * (double)(k + 1);
884 3 origin_x = gx;
885 3 origin_y = ty1;
886 } else {
887 19637 tx0 = gx + sub_adv * (double)k;
888 19637 tx1 = gx + sub_adv * (double)(k + 1);
889 19637 ty0 = gy + rise + descent;
890 19637 ty1 = gy + rise + ascent;
891 19637 origin_x = tx0;
892 19637 origin_y = gy + rise;
893 }
894  
895 19640 corner_x[0] = tx0; corner_y[0] = ty0;
896 19640 corner_x[1] = tx1; corner_y[1] = ty0;
897 19640 corner_x[2] = tx1; corner_y[2] = ty1;
898 19640 corner_x[3] = tx0; corner_y[3] = ty1;
899 98200 ▶ for (corner = 0; corner < 4; corner++) {
Branch: 1 branch
BranchTrueFalseTotal
for (corner = 0; corner < 4; corner++) { 78560 19640 98200
900 78560 textract_text_to_device(gs->text_matrix, gs->ctm,
901 corner_x[corner], corner_y[corner], &fx, &fy);
902 78560 ▶ if (corner == 0 || fx < g.x0) g.x0 = fx;
Branch: 2 branches
BranchTrueFalseTotal
if (corner == 0 || fx < g.x0) g.x0 = fx; 58920 19640 78560
if (corner == 0 || fx < g.x0) g.x0 = fx; 8 58912 58920
903 78560 ▶ if (corner == 0 || fx > g.x1) g.x1 = fx;
Branch: 2 branches
BranchTrueFalseTotal
if (corner == 0 || fx > g.x1) g.x1 = fx; 58920 19640 78560
if (corner == 0 || fx > g.x1) g.x1 = fx; 19631 39289 58920
904 78560 ▶ if (corner == 0 || fy < g.y0) g.y0 = fy;
Branch: 2 branches
BranchTrueFalseTotal
if (corner == 0 || fy < g.y0) g.y0 = fy; 58920 19640 78560
if (corner == 0 || fy < g.y0) g.y0 = fy; 0 58920 58920
905 78560 ▶ if (corner == 0 || fy > g.y1) g.y1 = fy;
Branch: 2 branches
BranchTrueFalseTotal
if (corner == 0 || fy > g.y1) g.y1 = fy; 58920 19640 78560
if (corner == 0 || fy > g.y1) g.y1 = fy; 19640 39280 58920
906 }
907 19640 textract_text_to_device(gs->text_matrix, gs->ctm,
908 origin_x, origin_y, &fx, &fy);
909  
910 19640 g.unicode = uni_list[k];
911 19640 g.baseline = fy;
912 19640 ▶ g.advance = fabs(sub_adv) * (is_vertical ? sy : sx);
Branch: 1 branch
BranchTrueFalseTotal
g.advance = fabs(sub_adv) * (is_vertical ? sy : sx); 3 19637 19640
913 19640 g.font_size = effective_size;
914 19640 g.reliable_advance = (uint8_t)glyph_adv.reliable;
915 19640 g.mcid = result->current_mcid;
916 19640 g.vertical = (uint8_t)is_vertical;
917 19640 g.angle = (int16_t)angle;
918 19640 g.along0 = fx * cos_theta + fy * sin_theta;
919 19640 g.along1 = g.along0 + g.advance;
920 19640 g.across = fy * cos_theta - fx * sin_theta;
921 19640 g.line = 0;
922  
923 19640 ▶ if (!result_push_glyph(result, &g)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!result_push_glyph(result, &g)) break; 0 19640 19640
924 }
925  
926 /* §9.4.4: word spacing applies to the single-byte code 32 only. */
927 19624 ▶ ws = (code_width == 1 && code == 0x20) ? word_space : 0.0;
Branch: 2 branches
BranchTrueFalseTotal
ws = (code_width == 1 && code == 0x20) ? word_space : 0.0; 19590 34 19624
ws = (code_width == 1 && code == 0x20) ? word_space : 0.0; 1999 17591 19590
928 19624 ▶ if (is_vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_vertical) { 3 19621 19624
929 3 accum_x -= glyph_adv.vertical + char_space + ws;
930 } else {
931 19621 accum_x += (glyph_adv.horizontal + char_space + ws) * h_scale;
932 }
933 }
934 }
935  
936 /* Phase 8: compute the real text-space advance for a string so the
937 * interpreter can keep text_matrix in sync with our glyph positions.
938 * Mirrors the advance logic from textract_on_text_show. */
939 3641 static double textract_get_string_advance(void *ctx,
940 const pdfmake_gstate_t *gs,
941 const uint8_t *bytes, size_t len)
942 {
943 3641 pdfmake_textract_result_t *result = (pdfmake_textract_result_t *)ctx;
944 pdfmake_resolved_font_t *rf;
945 int std14_id;
946 int is_cid;
947 int is_vertical;
948 double font_size;
949 double char_space;
950 double word_space;
951 double accum;
952 size_t pos;
953 uint32_t code;
954 size_t code_w;
955 pdfmake_cmap_t *to_uni;
956 uint32_t uni_list[PDFMAKE_CMAP_MAX_UNI];
957 textract_advance_t glyph_adv;
958 double glyph_advance;
959 int is_space;
960 double ws;
961 3641 ▶ if (!gs || !bytes || len == 0) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!gs || !bytes || len == 0) return 0; 3641 0 3641
if (!gs || !bytes || len == 0) return 0; 3641 0 3641
if (!gs || !bytes || len == 0) return 0; 0 3641 3641
962  
963 3641 rf = resolve_font(result, gs->font);
964 3641 ▶ std14_id = rf ? rf->std14_id : -1;
Branch: 1 branch
BranchTrueFalseTotal
std14_id = rf ? rf->std14_id : -1; 3641 0 3641
965 3641 ▶ is_cid = rf ? rf->is_cid : 0;
Branch: 1 branch
BranchTrueFalseTotal
is_cid = rf ? rf->is_cid : 0; 3641 0 3641
966 3641 ▶ is_vertical = rf ? rf->wmode : 0;
Branch: 1 branch
BranchTrueFalseTotal
is_vertical = rf ? rf->wmode : 0; 3641 0 3641
967 3641 ▶ to_uni = rf ? resolve_to_unicode(result, rf) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
to_uni = rf ? resolve_to_unicode(result, rf) : NULL; 3641 0 3641
968 3641 font_size = gs->font_size;
969 3641 char_space = gs->char_space;
970 3641 word_space = gs->word_space;
971  
972 3641 accum = 0;
973 3641 pos = 0;
974 23276 ▶ while (pos < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < len) { 19635 3641 23276
975 19635 ▶ if (is_cid && pos + 1 < len) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_cid && pos + 1 < len) { 34 19601 19635
if (is_cid && pos + 1 < len) { 34 0 34
976 34 code = ((uint32_t)bytes[pos] << 8) | bytes[pos + 1];
977 34 code_w = 2;
978 } else {
979 19601 code = bytes[pos];
980 19601 code_w = 1;
981 }
982  
983 /* The same advance on_text_show gives the glyph. */
984 19635 textract_code_to_unicode(rf, to_uni, is_cid, code, uni_list);
985 19635 glyph_adv = textract_glyph_advance(
986 rf, std14_id, is_cid, code, uni_list[0], font_size);
987 19635 ▶ glyph_advance = is_vertical ? glyph_adv.vertical : glyph_adv.horizontal;
Branch: 1 branch
BranchTrueFalseTotal
glyph_advance = is_vertical ? glyph_adv.vertical : glyph_adv.horizontal; 3 19632 19635
988  
989 /* Add spacing contributions (matches on_text_show) */
990 19635 ▶ is_space = (code_w == 1 && code == 0x20);
Branch: 2 branches
BranchTrueFalseTotal
is_space = (code_w == 1 && code == 0x20); 19601 34 19635
is_space = (code_w == 1 && code == 0x20); 1999 17602 19601
991 19635 ▶ ws = is_space ? word_space : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
ws = is_space ? word_space : 0.0; 1999 17636 19635
992 19635 accum += glyph_advance + char_space + ws;
993  
994 19635 pos += code_w;
995 }
996 3641 return accum;
997 }
998  
999 /* Strong right-to-left letters: Hebrew, Arabic and their presentation
1000 * forms. */
1001 4115 static int is_rtl_codepoint(uint32_t cp) {
1002 110 ▶ return (cp >= 0x0590 && cp <= 0x08FF)
Branch: 1 branch
BranchTrueFalseTotal
return (cp >= 0x0590 && cp <= 0x08FF) 105 5 110
1003 4110 ▶ || (cp >= 0xFB1D && cp <= 0xFDFF)
Branch: 2 branches
BranchTrueFalseTotal
|| (cp >= 0xFB1D && cp <= 0xFDFF) 8 4102 4110
|| (cp >= 0xFB1D && cp <= 0xFDFF) 8 0 8
1004 8230 ▶ || (cp >= 0xFE70 && cp <= 0xFEFF);
Branch: 3 branches
BranchTrueFalseTotal
|| (cp >= 0xFE70 && cp <= 0xFEFF); 110 4005 4115
|| (cp >= 0xFE70 && cp <= 0xFEFF); 7 4103 4110
|| (cp >= 0xFE70 && cp <= 0xFEFF); 0 7 7
1005 }
1006  
1007 8 static int actual_hex_digit(uint8_t c) {
1008 8 ▶ if (c >= '0' && c <= '9') return c - '0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c - '0'; 8 0 8
if (c >= '0' && c <= '9') return c - '0'; 2 6 8
1009 6 ▶ if (c >= 'a' && c <= 'f') return c - 'a' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 0 6 6
if (c >= 'a' && c <= 'f') return c - 'a' + 10; 0 0 0
1010 6 ▶ if (c >= 'A' && c <= 'F') return c - 'A' + 10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 6 0 6
if (c >= 'A' && c <= 'F') return c - 'A' + 10; 6 0 6
1011 0 return -1;
1012 }
1013  
1014 /* Decode an /ActualText string operand to codepoints: UTF-16BE behind a
1015 * FE FF mark, UTF-8 behind EF BB BF, one byte per character otherwise.
1016 * Returns the arena-held codepoints and sets *out_len, or NULL when the
1017 * operand is not a string. An empty string is a non-NULL result of length
1018 * zero: it means the span has no text. */
1019 6 static uint32_t *actual_text_decode(pdfmake_arena_t *arena,
1020 const pdfmake_obj_t *str, size_t *out_len)
1021 {
1022 const uint8_t *bytes;
1023 6 size_t len, i, n = 0;
1024 uint32_t *out;
1025  
1026 6 *out_len = 0;
1027 6 ▶ if (!str || str->kind != PDFMAKE_STR) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!str || str->kind != PDFMAKE_STR) return NULL; 3 3 6
if (!str || str->kind != PDFMAKE_STR) return NULL; 0 3 3
1028 3 bytes = str->as.str.bytes;
1029 3 len = str->as.str.len;
1030  
1031 3 ▶ if (str->as.str.hex) {
Branch: 1 branch
BranchTrueFalseTotal
if (str->as.str.hex) { 1 2 3
1032 1 uint8_t *raw = pdfmake_arena_alloc(arena, len / 2 + 1);
1033 1 size_t written = 0;
1034 1 int high = -1;
1035 1 ▶ if (!raw) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!raw) return NULL; 0 1 1
1036 9 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 8 1 9
1037 8 int digit = actual_hex_digit(bytes[i]);
1038 8 ▶ if (digit < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (digit < 0) continue; 0 8 8
1039 8 ▶ if (high < 0) { high = digit; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (high < 0) { high = digit; continue; } 4 4 8
1040 4 raw[written++] = (uint8_t)((high << 4) | digit);
1041 4 high = -1;
1042 }
1043 1 ▶ if (high >= 0) raw[written++] = (uint8_t)(high << 4);
Branch: 1 branch
BranchTrueFalseTotal
if (high >= 0) raw[written++] = (uint8_t)(high << 4); 0 1 1
1044 1 bytes = raw;
1045 1 len = written;
1046 }
1047  
1048 3 out = pdfmake_arena_alloc(arena, (len + 1) * sizeof(*out));
1049 3 ▶ if (!out) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return NULL; 0 3 3
1050  
1051 3 ▶ if (len >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) {
Branch: 3 branches
BranchTrueFalseTotal
if (len >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) { 2 1 3
if (len >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) { 1 1 2
if (len >= 2 && bytes[0] == 0xFE && bytes[1] == 0xFF) { 1 0 1
1052 2 ▶ for (i = 2; i + 1 < len; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < len; i += 2) { 1 1 2
1053 1 uint32_t unit = ((uint32_t)bytes[i] << 8) | bytes[i + 1];
1054 1 ▶ if (unit >= 0xD800 && unit <= 0xDBFF && i + 3 < len) {
Branch: 3 branches
BranchTrueFalseTotal
if (unit >= 0xD800 && unit <= 0xDBFF && i + 3 < len) { 0 1 1
if (unit >= 0xD800 && unit <= 0xDBFF && i + 3 < len) { 0 0 0
if (unit >= 0xD800 && unit <= 0xDBFF && i + 3 < len) { 0 0 0
1055 0 uint32_t low = ((uint32_t)bytes[i + 2] << 8) | bytes[i + 3];
1056 0 ▶ if (low >= 0xDC00 && low <= 0xDFFF) {
Branch: 2 branches
BranchTrueFalseTotal
if (low >= 0xDC00 && low <= 0xDFFF) { 0 0 0
if (low >= 0xDC00 && low <= 0xDFFF) { 0 0 0
1057 0 unit = 0x10000 + ((unit - 0xD800) << 10) + (low - 0xDC00);
1058 0 i += 2;
1059 }
1060 }
1061 1 out[n++] = unit;
1062 }
1063 2 ▶ } else if (len >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
Branch: 4 branches
BranchTrueFalseTotal
} else if (len >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { 1 1 2
} else if (len >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { 0 1 1
} else if (len >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { 0 0 0
} else if (len >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) { 0 0 0
1064 0 i = 3;
1065 0 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 0 0 0
1066 0 uint8_t lead = bytes[i];
1067 0 uint32_t cp = lead;
1068 0 ▶ int extra = lead >= 0xF0 ? 3 : lead >= 0xE0 ? 2 : lead >= 0xC0 ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
int extra = lead >= 0xF0 ? 3 : lead >= 0xE0 ? 2 : lead >= 0xC0 ? 1 : 0; 0 0 0
int extra = lead >= 0xF0 ? 3 : lead >= 0xE0 ? 2 : lead >= 0xC0 ? 1 : 0; 0 0 0
1069 0 ▶ if (extra) cp = lead & (0x3F >> extra);
Branch: 1 branch
BranchTrueFalseTotal
if (extra) cp = lead & (0x3F >> extra); 0 0 0
1070 0 i++;
1071 0 ▶ while (extra-- > 0 && i < len && (bytes[i] & 0xC0) == 0x80)
Branch: 3 branches
BranchTrueFalseTotal
while (extra-- > 0 && i < len && (bytes[i] & 0xC0) == 0x80) 0 0 0
while (extra-- > 0 && i < len && (bytes[i] & 0xC0) == 0x80) 0 0 0
while (extra-- > 0 && i < len && (bytes[i] & 0xC0) == 0x80) 0 0 0
1072 0 cp = (cp << 6) | (bytes[i++] & 0x3F);
1073 0 out[n++] = cp;
1074 }
1075 } else {
1076 6 ▶ for (i = 0; i < len; i++) out[n++] = bytes[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) out[n++] = bytes[i]; 4 2 6
1077 }
1078 3 *out_len = n;
1079 3 return out;
1080 }
1081  
1082 /* Replace the glyphs a span painted with the text its /ActualText gives.
1083 * The same number of characters keeps every glyph's own box. Any other
1084 * count shares the span's extent evenly between the characters.
1085 *
1086 * The text is in reading order and the boxes run left to right, so
1087 * right-to-left text is laid over them from the far end: the word it
1088 * lands in is turned to reading order later, with every other
1089 * right-to-left word. A span whose glyphs were painted right to left is
1090 * already in reading order and is left as it is. */
1091 3 static void actual_text_apply(pdfmake_textract_result_t *r)
1092 {
1093 3 size_t start = r->actual_start;
1094 3 ▶ size_t painted = r->raw_len > start ? r->raw_len - start : 0;
Branch: 1 branch
BranchTrueFalseTotal
size_t painted = r->raw_len > start ? r->raw_len - start : 0; 3 0 3
1095 3 size_t count = r->actual_len;
1096 pdfmake_text_glyph_t first, *glyphs;
1097 double x0, y0, x1, y1, along0, along1;
1098 size_t i;
1099 3 int rtl = 0;
1100  
1101 5 ▶ if (painted == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (painted == 0) return; 0 3 3
1102 3 glyphs = r->raw_glyphs + start;
1103  
1104 8 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 5 3 8
1105 5 ▶ if (is_rtl_codepoint(r->actual_text[i])) { rtl = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (is_rtl_codepoint(r->actual_text[i])) { rtl = 1; break; } 0 5 5
1106 }
1107  
1108 3 ▶ if (count == painted) {
Branch: 1 branch
BranchTrueFalseTotal
if (count == painted) { 2 1 3
1109 2 ▶ if (rtl && glyphs[painted - 1].along0 < glyphs[0].along0) rtl = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (rtl && glyphs[painted - 1].along0 < glyphs[0].along0) rtl = 0; 0 2 2
if (rtl && glyphs[painted - 1].along0 < glyphs[0].along0) rtl = 0; 0 0 0
1110 4 ▶ for (i = 0; i < count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) 2 2 4
1111 2 ▶ glyphs[i].unicode = r->actual_text[rtl ? count - 1 - i : i];
Branch: 1 branch
BranchTrueFalseTotal
glyphs[i].unicode = r->actual_text[rtl ? count - 1 - i : i]; 0 2 2
1112 2 return;
1113 }
1114  
1115 1 first = glyphs[0];
1116 1 x0 = first.x0; y0 = first.y0; x1 = first.x1; y1 = first.y1;
1117 1 along0 = first.along0; along1 = first.along1;
1118 1 ▶ for (i = 1; i < painted; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < painted; i++) { 0 1 1
1119 0 ▶ if (glyphs[i].x0 < x0) x0 = glyphs[i].x0;
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[i].x0 < x0) x0 = glyphs[i].x0; 0 0 0
1120 0 ▶ if (glyphs[i].y0 < y0) y0 = glyphs[i].y0;
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[i].y0 < y0) y0 = glyphs[i].y0; 0 0 0
1121 0 ▶ if (glyphs[i].x1 > x1) x1 = glyphs[i].x1;
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[i].x1 > x1) x1 = glyphs[i].x1; 0 0 0
1122 0 ▶ if (glyphs[i].y1 > y1) y1 = glyphs[i].y1;
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[i].y1 > y1) y1 = glyphs[i].y1; 0 0 0
1123 0 ▶ if (glyphs[i].along0 < along0) along0 = glyphs[i].along0;
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[i].along0 < along0) along0 = glyphs[i].along0; 0 0 0
1124 0 ▶ if (glyphs[i].along1 > along1) along1 = glyphs[i].along1;
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[i].along1 > along1) along1 = glyphs[i].along1; 0 0 0
1125 }
1126  
1127 1 r->raw_len = start;
1128 4 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 3 1 4
1129 3 pdfmake_text_glyph_t g = first;
1130 3 double from = (double)i / (double)count;
1131 3 double to = (double)(i + 1) / (double)count;
1132 3 ▶ g.unicode = r->actual_text[rtl ? count - 1 - i : i];
Branch: 1 branch
BranchTrueFalseTotal
g.unicode = r->actual_text[rtl ? count - 1 - i : i]; 0 3 3
1133 3 g.y0 = y0;
1134 3 g.y1 = y1;
1135 3 ▶ if (first.angle == 0 && !first.vertical) {
Branch: 2 branches
BranchTrueFalseTotal
if (first.angle == 0 && !first.vertical) { 3 0 3
if (first.angle == 0 && !first.vertical) { 3 0 3
1136 3 g.x0 = x0 + (x1 - x0) * from;
1137 3 g.x1 = x0 + (x1 - x0) * to;
1138 } else {
1139 0 g.x0 = x0;
1140 0 g.x1 = x1;
1141 }
1142 3 g.along0 = along0 + (along1 - along0) * from;
1143 3 g.along1 = along0 + (along1 - along0) * to;
1144 3 g.advance = g.along1 - g.along0;
1145 3 ▶ if (!result_push_glyph(r, &g)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!result_push_glyph(r, &g)) break; 0 3 3
1146 }
1147 }
1148  
1149 /* Phase 12: BDC / BMC — push the current MCID on entry to a marked-content
1150 * item. MCID comes from the /MCID integer in the properties dict (if any).
1151 * BMC has no properties dict so it never carries an MCID. */
1152 6 static void textract_on_mc_begin(void *ctx,
1153 const pdfmake_gstate_t *gs,
1154 uint32_t tag,
1155 pdfmake_obj_t *properties)
1156 {
1157 6 pdfmake_textract_result_t *result = (pdfmake_textract_result_t *)ctx;
1158 6 int32_t mcid = -1;
1159 (void)gs;
1160 (void)tag;
1161 6 ▶ if (!result) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return; 0 6 6
1162  
1163 6 result->mc_depth++;
1164 6 ▶ if (properties && properties->kind == PDFMAKE_DICT && result->arena) {
Branch: 3 branches
BranchTrueFalseTotal
if (properties && properties->kind == PDFMAKE_DICT && result->arena) { 6 0 6
if (properties && properties->kind == PDFMAKE_DICT && result->arena) { 6 0 6
if (properties && properties->kind == PDFMAKE_DICT && result->arena) { 6 0 6
1165 6 uint32_t mcid_key = pdfmake_arena_intern_name(result->arena, "MCID", 4);
1166 6 pdfmake_obj_t *v = pdfmake_dict_get(properties, mcid_key);
1167 6 ▶ if (v && v->kind == PDFMAKE_INT) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && v->kind == PDFMAKE_INT) { 3 3 6
if (v && v->kind == PDFMAKE_INT) { 3 0 3
1168 3 mcid = (int32_t)v->as.i;
1169 }
1170 6 ▶ if (result->actual_depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (result->actual_depth == 0) { 6 0 6
1171 6 uint32_t actual_key = pdfmake_arena_intern_name(
1172 result->arena, "ActualText", 10);
1173 6 size_t actual_len = 0;
1174 6 uint32_t *actual = actual_text_decode(result->arena,
1175 6 pdfmake_dict_get(properties, actual_key), &actual_len);
1176 6 ▶ if (actual) {
Branch: 1 branch
BranchTrueFalseTotal
if (actual) { 3 3 6
1177 3 result->actual_depth = result->mc_depth;
1178 3 result->actual_start = result->raw_len;
1179 3 result->actual_text = actual;
1180 3 result->actual_len = actual_len;
1181 }
1182 }
1183 }
1184  
1185 6 ▶ if (result->mcid_depth <
Branch: 1 branch
BranchTrueFalseTotal
if (result->mcid_depth < 6 0 6
1186 (int)(sizeof(result->mcid_stack)/sizeof(result->mcid_stack[0]))) {
1187 6 result->mcid_stack[result->mcid_depth++] = result->current_mcid;
1188 }
1189 /* Nested marked content without its own /MCID inherits the parent's. */
1190 6 ▶ if (mcid >= 0) result->current_mcid = mcid;
Branch: 1 branch
BranchTrueFalseTotal
if (mcid >= 0) result->current_mcid = mcid; 3 3 6
1191 }
1192  
1193 6 static void textract_on_mc_end(void *ctx, const pdfmake_gstate_t *gs)
1194 {
1195 6 pdfmake_textract_result_t *result = (pdfmake_textract_result_t *)ctx;
1196 (void)gs;
1197 6 ▶ if (!result) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return; 0 6 6
1198 6 ▶ if (result->actual_depth > 0 && result->actual_depth == result->mc_depth) {
Branch: 2 branches
BranchTrueFalseTotal
if (result->actual_depth > 0 && result->actual_depth == result->mc_depth) { 3 3 6
if (result->actual_depth > 0 && result->actual_depth == result->mc_depth) { 3 0 3
1199 3 actual_text_apply(result);
1200 3 result->actual_depth = 0;
1201 }
1202 6 ▶ if (result->mc_depth > 0) result->mc_depth--;
Branch: 1 branch
BranchTrueFalseTotal
if (result->mc_depth > 0) result->mc_depth--; 6 0 6
1203 6 ▶ if (result->mcid_depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (result->mcid_depth > 0) { 6 0 6
1204 6 result->current_mcid = result->mcid_stack[--result->mcid_depth];
1205 } else {
1206 0 result->current_mcid = -1;
1207 }
1208 }
1209  
1210 /* Phase 14: report the current font's writing mode so the interpreter can
1211 * advance text_matrix on the right axis. */
1212 3641 static int textract_is_vertical(void *ctx, const pdfmake_gstate_t *gs) {
1213 3641 pdfmake_textract_result_t *result = (pdfmake_textract_result_t *)ctx;
1214 pdfmake_resolved_font_t *rf;
1215 3641 ▶ if (!result || !gs) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!result || !gs) return 0; 3641 0 3641
if (!result || !gs) return 0; 0 3641 3641
1216 3641 rf = resolve_font(result, gs->font);
1217 3641 ▶ return rf ? rf->wmode : 0;
Branch: 1 branch
BranchTrueFalseTotal
return rf ? rf->wmode : 0; 3641 0 3641
1218 }
1219  
1220 110 pdfmake_visitor_t pdfmake_textract_visitor(pdfmake_textract_result_t *result) {
1221 pdfmake_visitor_t v;
1222 110 memset(&v, 0, sizeof(v));
1223 110 v.ctx = result;
1224 110 v.on_text_show = textract_on_text_show;
1225 110 v.get_string_advance = textract_get_string_advance;
1226 110 v.on_marked_content_begin = textract_on_mc_begin;
1227 110 v.on_marked_content_end = textract_on_mc_end;
1228 110 v.is_vertical_writing = textract_is_vertical; /* Phase 14 */
1229 110 return v;
1230 }
1231  
1232 /*============================================================================
1233 * Aggregation: raw glyphs → words → lines → blocks
1234 *==========================================================================*/
1235  
1236 /* qsort() has no ctx param — use a file-static pointer for column info
1237 * during the sort. pdfmake_textract_aggregate is single-threaded so this
1238 * is safe. */
1239 static const double *g_sort_column_splits = NULL;
1240 static int g_sort_column_count = 0;
1241  
1242 19631 static int glyph_column(const pdfmake_text_glyph_t *g) {
1243 19631 int col = 0;
1244 int i;
1245 22177 ▶ for (i = 0; i < g_sort_column_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < g_sort_column_count; i++) { 8889 13288 22177
1246 8889 ▶ if (g->x0 >= g_sort_column_splits[i]) col++;
Branch: 1 branch
BranchTrueFalseTotal
if (g->x0 >= g_sort_column_splits[i]) col++; 2546 6343 8889
1247 6343 else break;
1248 }
1249 19631 return col;
1250 }
1251  
1252 /* Compare glyphs by column, then y descending, then x ascending. */
1253 98793 static int cmp_glyph_position(const void *a, const void *b) {
1254 98793 const pdfmake_text_glyph_t *ga = (const pdfmake_text_glyph_t *)a;
1255 98793 const pdfmake_text_glyph_t *gb = (const pdfmake_text_glyph_t *)b;
1256 double col_tol;
1257 double dx_cx;
1258 double dy2;
1259 double dy;
1260 double dx;
1261  
1262 /* Phase 14: vertical glyphs sort right-to-left, top-to-bottom so
1263 * reading order matches CJK conventions. */
1264 98793 ▶ if (ga->vertical && gb->vertical) {
Branch: 2 branches
BranchTrueFalseTotal
if (ga->vertical && gb->vertical) { 2 98791 98793
if (ga->vertical && gb->vertical) { 2 0 2
1265 /* Group into columns by x (tolerance = font size). Right-most
1266 * column comes first. */
1267 2 col_tol = ga->font_size * 0.5;
1268 2 dx_cx = gb->x0 - ga->x0;
1269 2 ▶ if (fabs(dx_cx) > col_tol) {
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(dx_cx) > col_tol) { 0 2 2
1270 0 ▶ return dx_cx > 0 ? -1 : 1; /* larger x first */
Branch: 1 branch
BranchTrueFalseTotal
return dx_cx > 0 ? -1 : 1; /* larger x first */ 0 0 0
1271 }
1272 /* Same column: higher y first (top-to-bottom). */
1273 2 dy2 = gb->y0 - ga->y0;
1274 2 ▶ if (dy2 < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (dy2 < 0) return -1; 2 0 2
1275 0 ▶ if (dy2 > 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (dy2 > 0) return 1; 0 0 0
1276 0 return 0;
1277 }
1278 /* If one is vertical and the other horizontal, emit vertical last so
1279 * the horizontal reading order isn't interleaved. (Truly mixed pages
1280 * are rare; the user can filter per glyph->vertical if they need to.) */
1281 98791 ▶ if (ga->vertical != gb->vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (ga->vertical != gb->vertical) { 0 98791 98791
1282 0 ▶ return ga->vertical ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
return ga->vertical ? 1 : -1; 0 0 0
1283 }
1284  
1285 /* Upright text first, then each rotation in turn. */
1286 98791 ▶ if (ga->angle != gb->angle) {
Branch: 1 branch
BranchTrueFalseTotal
if (ga->angle != gb->angle) { 0 98791 98791
1287 0 ▶ if (ga->angle == 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ga->angle == 0) return -1; 0 0 0
1288 0 ▶ if (gb->angle == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (gb->angle == 0) return 1; 0 0 0
1289 0 return ga->angle - gb->angle;
1290 }
1291  
1292 /* Top line first, then along the line. The comparison is exact so the
1293 * order is a strict one; assign_lines() applies the tolerance. */
1294 98791 dy = gb->across - ga->across;
1295 98791 ▶ if (dy > 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (dy > 0) return 1; 13593 85198 98791
1296 85198 ▶ if (dy < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (dy < 0) return -1; 33796 51402 85198
1297 51402 dx = ga->along0 - gb->along0;
1298 51402 ▶ if (dx < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (dx < 0) return -1; 51397 5 51402
1299 5 ▶ if (dx > 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (dx > 0) return 1; 4 1 5
1300 1 return 0;
1301 }
1302  
1303 50428 static int cmp_glyph_along(const void *a, const void *b) {
1304 50428 const pdfmake_text_glyph_t *ga = (const pdfmake_text_glyph_t *)a;
1305 50428 const pdfmake_text_glyph_t *gb = (const pdfmake_text_glyph_t *)b;
1306 50428 ▶ if (ga->along0 < gb->along0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ga->along0 < gb->along0) return -1; 50222 206 50428
1307 206 ▶ if (ga->along0 > gb->along0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ga->along0 > gb->along0) return 1; 205 1 206
1308 1 ▶ if (ga->across > gb->across) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (ga->across > gb->across) return -1; 0 1 1
1309 1 ▶ if (ga->across < gb->across) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ga->across < gb->across) return 1; 0 1 1
1310 1 return 0;
1311 }
1312  
1313 /* A stretch of one line that reads as a unit: the whole line, or the part
1314 * of it inside one column. */
1315 typedef struct {
1316 size_t start;
1317 size_t len;
1318 int group; /* rotation, in sort order */
1319 int column;
1320 size_t order; /* position among the segments before reordering */
1321 } line_segment_t;
1322  
1323 2377 static int cmp_line_segment(const void *a, const void *b) {
1324 2377 const line_segment_t *sa = (const line_segment_t *)a;
1325 2377 const line_segment_t *sb = (const line_segment_t *)b;
1326 2377 ▶ if (sa->group != sb->group) return sa->group - sb->group;
Branch: 1 branch
BranchTrueFalseTotal
if (sa->group != sb->group) return sa->group - sb->group; 0 2377 2377
1327 2377 ▶ if (sa->column != sb->column) return sa->column - sb->column;
Branch: 1 branch
BranchTrueFalseTotal
if (sa->column != sb->column) return sa->column - sb->column; 620 1757 2377
1328 1757 ▶ if (sa->order < sb->order) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (sa->order < sb->order) return -1; 1757 0 1757
1329 0 ▶ if (sa->order > sb->order) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (sa->order > sb->order) return 1; 0 0 0
1330 0 return 0;
1331 }
1332  
1333 /* A line breaks at a column boundary only where the text itself has a gap
1334 * this many font sizes wide. A heading or a sentence that runs across the
1335 * boundary with ordinary word spacing stays whole. */
1336 #define COLUMN_BREAK_GAP 0.9
1337  
1338 /* Give every glyph a line number and put the lines in reading order.
1339 *
1340 * The glyphs arrive sorted by rotation and baseline. A glyph joins the
1341 * line above it when its baseline is close to that of the line's anchor,
1342 * the largest glyph seen on that line so far. Close is measured in the
1343 * font size of the larger of the two, which keeps a superscript or a
1344 * subscript on the line of the text it belongs to: up to `tolerance` font
1345 * sizes above the larger glyph's baseline, and SUBSCRIPT_REACH of that
1346 * below it. The reach below is the shorter one because a subscript drops
1347 * less than a superscript rises, and because the text under a large
1348 * heading starts not far below its baseline and is a line of its own.
1349 * Each line is then cut into column segments, and the segments are ordered
1350 * column by column. */
1351 #define SUBSCRIPT_REACH 0.6
1352  
1353 110 static void assign_lines(pdfmake_text_glyph_t *glyphs, size_t n, double tolerance)
1354 {
1355 110 size_t i, count = 0, start = 0, segment_count = 0, out = 0;
1356 110 uint32_t provisional = 0;
1357 110 double anchor_across = 0, anchor_size = 0;
1358 line_segment_t *segments;
1359 pdfmake_text_glyph_t *ordered;
1360 110 int group = 0;
1361  
1362 19749 ▶ while (count < n && !glyphs[count].vertical) count++;
Branch: 2 branches
BranchTrueFalseTotal
while (count < n && !glyphs[count].vertical) count++; 19640 109 19749
while (count < n && !glyphs[count].vertical) count++; 19639 1 19640
1363 113 ▶ for (i = count; i < n; i++) glyphs[i].line = 0xFFFFFFFFu;
Branch: 1 branch
BranchTrueFalseTotal
for (i = count; i < n; i++) glyphs[i].line = 0xFFFFFFFFu; 3 110 113
1364 110 ▶ if (count == 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (count == 0) return; 1 109 110
1365  
1366 /* Baseline clustering: line holds a provisional line number. */
1367 19748 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 19639 109 19748
1368 19639 pdfmake_text_glyph_t *g = &glyphs[i];
1369 19639 int fresh = (i == 0);
1370 19639 ▶ if (!fresh) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fresh) { 19530 109 19639
1371 /* How far the smaller of the two sits above the baseline of
1372 * the larger, in the larger one's font size. */
1373 19530 int larger = g->font_size >= anchor_size;
1374 19530 ▶ double size = larger ? g->font_size : anchor_size;
Branch: 1 branch
BranchTrueFalseTotal
double size = larger ? g->font_size : anchor_size; 19364 166 19530
1375 38894 double rise = larger ? anchor_across - g->across
1376 19530 ▶ : g->across - anchor_across;
Branch: 1 branch
BranchTrueFalseTotal
: g->across - anchor_across; 19364 166 19530
1377 19530 fresh = g->angle != glyphs[i - 1].angle
1378 19530 ▶ || rise > tolerance * size
Branch: 1 branch
BranchTrueFalseTotal
|| rise > tolerance * size 19016 514 19530
1379 39060 ▶ || rise < -SUBSCRIPT_REACH * tolerance * size;
Branch: 2 branches
BranchTrueFalseTotal
|| rise < -SUBSCRIPT_REACH * tolerance * size; 19530 0 19530
|| rise < -SUBSCRIPT_REACH * tolerance * size; 112 18904 19016
1380 }
1381 19639 ▶ if (fresh) {
Branch: 1 branch
BranchTrueFalseTotal
if (fresh) { 735 18904 19639
1382 735 ▶ if (i > start)
Branch: 1 branch
BranchTrueFalseTotal
if (i > start) 626 109 735
1383 626 qsort(glyphs + start, i - start, sizeof(*glyphs), cmp_glyph_along);
1384 735 ▶ if (i > 0) provisional++;
Branch: 1 branch
BranchTrueFalseTotal
if (i > 0) provisional++; 626 109 735
1385 735 start = i;
1386 735 anchor_across = g->across;
1387 735 anchor_size = g->font_size;
1388 18904 ▶ } else if (g->font_size > anchor_size) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (g->font_size > anchor_size) { 43 18861 18904
1389 43 anchor_across = g->across;
1390 43 anchor_size = g->font_size;
1391 }
1392 19639 g->line = provisional;
1393 }
1394 109 qsort(glyphs + start, count - start, sizeof(*glyphs), cmp_glyph_along);
1395  
1396 109 segments = malloc(count * sizeof(*segments));
1397 109 ordered = malloc(count * sizeof(*ordered));
1398 109 ▶ if (!segments || !ordered) {
Branch: 2 branches
BranchTrueFalseTotal
if (!segments || !ordered) { 109 0 109
if (!segments || !ordered) { 0 109 109
1399 0 free(segments);
1400 0 free(ordered);
1401 0 return;
1402 }
1403  
1404 19748 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 19639 109 19748
1405 19639 pdfmake_text_glyph_t *g = &glyphs[i];
1406 19639 ▶ int column = g->angle == 0 ? glyph_column(g) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int column = g->angle == 0 ? glyph_column(g) : 0; 19631 8 19639
1407 19639 int fresh = (i == 0);
1408 19639 ▶ if (!fresh) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fresh) { 19530 109 19639
1409 19530 pdfmake_text_glyph_t *prev = &glyphs[i - 1];
1410 19530 ▶ if (g->angle != prev->angle) {
Branch: 1 branch
BranchTrueFalseTotal
if (g->angle != prev->angle) { 0 19530 19530
1411 0 group++;
1412 0 fresh = 1;
1413 19530 ▶ } else if (g->line != prev->line) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (g->line != prev->line) { 626 18904 19530
1414 626 fresh = 1;
1415 18904 ▶ } else if (column != segments[segment_count - 1].column
Branch: 1 branch
BranchTrueFalseTotal
} else if (column != segments[segment_count - 1].column 596 18308 18904
1416 596 ▶ && g->along0 - prev->along1 > COLUMN_BREAK_GAP * g->font_size) {
Branch: 1 branch
BranchTrueFalseTotal
&& g->along0 - prev->along1 > COLUMN_BREAK_GAP * g->font_size) { 139 457 596
1417 139 fresh = 1;
1418 }
1419 }
1420 19639 ▶ if (fresh) {
Branch: 1 branch
BranchTrueFalseTotal
if (fresh) { 874 18765 19639
1421 874 segments[segment_count].start = i;
1422 874 segments[segment_count].len = 0;
1423 874 segments[segment_count].group = group;
1424 874 segments[segment_count].column = column;
1425 874 segments[segment_count].order = segment_count;
1426 874 segment_count++;
1427 }
1428 19639 segments[segment_count - 1].len++;
1429 }
1430  
1431 109 qsort(segments, segment_count, sizeof(*segments), cmp_line_segment);
1432 983 ▶ for (i = 0; i < segment_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < segment_count; i++) { 874 109 983
1433 size_t k;
1434 20513 ▶ for (k = 0; k < segments[i].len; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < segments[i].len; k++) { 19639 874 20513
1435 19639 ordered[out] = glyphs[segments[i].start + k];
1436 19639 ordered[out].line = (uint32_t)i;
1437 19639 out++;
1438 }
1439 }
1440 109 memcpy(glyphs, ordered, count * sizeof(*glyphs));
1441 109 free(segments);
1442 109 free(ordered);
1443 }
1444  
1445 /* Characters that separate words and are never part of one. */
1446 59981 static int is_space_codepoint(uint32_t cp) {
1447 53882 ▶ return cp == 0x20 || cp == 0xA0 || (cp >= 0x09 && cp <= 0x0D)
Branch: 3 branches
BranchTrueFalseTotal
return cp == 0x20 || cp == 0xA0 || (cp >= 0x09 && cp <= 0x0D) 53879 3 53882
return cp == 0x20 || cp == 0xA0 || (cp >= 0x09 && cp <= 0x0D) 53879 0 53879
return cp == 0x20 || cp == 0xA0 || (cp >= 0x09 && cp <= 0x0D) 53879 0 53879
1448 53879 ▶ || cp == 0x1680 || (cp >= 0x2000 && cp <= 0x200B)
Branch: 3 branches
BranchTrueFalseTotal
|| cp == 0x1680 || (cp >= 0x2000 && cp <= 0x200B) 53879 0 53879
|| cp == 0x1680 || (cp >= 0x2000 && cp <= 0x200B) 261 53618 53879
|| cp == 0x1680 || (cp >= 0x2000 && cp <= 0x200B) 261 0 261
1449 53879 ▶ || cp == 0x2028 || cp == 0x2029 || cp == 0x202F || cp == 0x205F
Branch: 4 branches
BranchTrueFalseTotal
|| cp == 0x2028 || cp == 0x2029 || cp == 0x202F || cp == 0x205F 53879 0 53879
|| cp == 0x2028 || cp == 0x2029 || cp == 0x202F || cp == 0x205F 53879 0 53879
|| cp == 0x2028 || cp == 0x2029 || cp == 0x202F || cp == 0x205F 53879 0 53879
|| cp == 0x2028 || cp == 0x2029 || cp == 0x202F || cp == 0x205F 53879 0 53879
1450 113863 ▶ || cp == 0x3000 || cp == 0xFEFF;
Branch: 3 branches
BranchTrueFalseTotal
|| cp == 0x3000 || cp == 0xFEFF; 53882 6099 59981
|| cp == 0x3000 || cp == 0xFEFF; 53879 0 53879
|| cp == 0x3000 || cp == 0xFEFF; 0 53879 53879
1451 }
1452  
1453 /* Put each space back between the two glyphs it was drawn between.
1454 *
1455 * The glyphs are still in the order the page painted them. A producer
1456 * that kerns with TJ or resets the text matrix after a space can leave
1457 * the space glyph inside the next word, where sorting by position would
1458 * make it break that word and join the two it was meant to part. A space
1459 * on the same line as both its neighbours, and outside the stretch
1460 * between their origins, is moved to the middle of that stretch. */
1461 110 static void settle_spaces(pdfmake_text_glyph_t *glyphs, size_t n, double tolerance)
1462 {
1463 size_t i;
1464 19532 ▶ for (i = 1; i + 1 < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < n; i++) { 19422 110 19532
1465 19422 pdfmake_text_glyph_t *space = &glyphs[i];
1466 19422 const pdfmake_text_glyph_t *before = &glyphs[i - 1];
1467 19422 const pdfmake_text_glyph_t *after = &glyphs[i + 1];
1468 double low, high, reach;
1469  
1470 19422 ▶ if (!is_space_codepoint(space->unicode) || space->vertical) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!is_space_codepoint(space->unicode) || space->vertical) continue; 2017 17405 19422
if (!is_space_codepoint(space->unicode) || space->vertical) continue; 0 2017 2017
1471 2017 ▶ if (is_space_codepoint(before->unicode) || is_space_codepoint(after->unicode))
Branch: 2 branches
BranchTrueFalseTotal
if (is_space_codepoint(before->unicode) || is_space_codepoint(after->unicode)) 1969 48 2017
if (is_space_codepoint(before->unicode) || is_space_codepoint(after->unicode)) 20 1949 1969
1472 68 continue;
1473 1949 ▶ if (before->vertical || after->vertical) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (before->vertical || after->vertical) continue; 1949 0 1949
if (before->vertical || after->vertical) continue; 0 1949 1949
1474 1949 ▶ if (before->angle != space->angle || after->angle != space->angle) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (before->angle != space->angle || after->angle != space->angle) continue; 1949 0 1949
if (before->angle != space->angle || after->angle != space->angle) continue; 0 1949 1949
1475 1949 reach = tolerance * space->font_size;
1476 1949 ▶ if (fabs(before->across - space->across) > reach
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(before->across - space->across) > reach 1938 11 1949
1477 1949 ▶ || fabs(after->across - space->across) > reach) continue;
Branch: 1 branch
BranchTrueFalseTotal
|| fabs(after->across - space->across) > reach) continue; 100 1838 1938
1478  
1479 1838 ▶ low = before->along0 < after->along0 ? before->along0 : after->along0;
Branch: 1 branch
BranchTrueFalseTotal
low = before->along0 < after->along0 ? before->along0 : after->along0; 1838 0 1838
1480 1838 ▶ high = before->along0 < after->along0 ? after->along0 : before->along0;
Branch: 1 branch
BranchTrueFalseTotal
high = before->along0 < after->along0 ? after->along0 : before->along0; 1838 0 1838
1481 1838 ▶ if (space->along0 > low && space->along0 < high) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (space->along0 > low && space->along0 < high) continue; 1838 0 1838
if (space->along0 > low && space->along0 < high) continue; 1837 1 1838
1482 1 space->along0 = (low + high) / 2.0;
1483 1 space->along1 = space->along0;
1484 }
1485 110 }
1486  
1487 /* Nonspacing marks: accents, vowel points and signs that sit on a base
1488 * letter. The ranges cover the general combining blocks and the marks of
1489 * Hebrew, Arabic, Devanagari and Thai. */
1490 19534 static int is_mark_codepoint(uint32_t cp) {
1491 19534 ▶ if (cp < 0x0300) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x0300) return 0; 19459 75 19534
1492 return (cp <= 0x036F)
1493 74 ▶ || (cp >= 0x0483 && cp <= 0x0489)
Branch: 2 branches
BranchTrueFalseTotal
|| (cp >= 0x0483 && cp <= 0x0489) 73 1 74
|| (cp >= 0x0483 && cp <= 0x0489) 73 0 73
1494 74 ▶ || (cp >= 0x0591 && cp <= 0x05BD) || cp == 0x05BF
Branch: 3 branches
BranchTrueFalseTotal
|| (cp >= 0x0591 && cp <= 0x05BD) || cp == 0x05BF 73 1 74
|| (cp >= 0x0591 && cp <= 0x05BD) || cp == 0x05BF 73 0 73
|| (cp >= 0x0591 && cp <= 0x05BD) || cp == 0x05BF 74 0 74
1495 74 ▶ || cp == 0x05C1 || cp == 0x05C2 || cp == 0x05C4 || cp == 0x05C5
Branch: 4 branches
BranchTrueFalseTotal
|| cp == 0x05C1 || cp == 0x05C2 || cp == 0x05C4 || cp == 0x05C5 74 0 74
|| cp == 0x05C1 || cp == 0x05C2 || cp == 0x05C4 || cp == 0x05C5 74 0 74
|| cp == 0x05C1 || cp == 0x05C2 || cp == 0x05C4 || cp == 0x05C5 74 0 74
|| cp == 0x05C1 || cp == 0x05C2 || cp == 0x05C4 || cp == 0x05C5 74 0 74
1496 74 ▶ || cp == 0x05C7
Branch: 1 branch
BranchTrueFalseTotal
|| cp == 0x05C7 74 0 74
1497 74 ▶ || (cp >= 0x0610 && cp <= 0x061A) || (cp >= 0x064B && cp <= 0x065F)
Branch: 4 branches
BranchTrueFalseTotal
|| (cp >= 0x0610 && cp <= 0x061A) || (cp >= 0x064B && cp <= 0x065F) 70 4 74
|| (cp >= 0x0610 && cp <= 0x061A) || (cp >= 0x064B && cp <= 0x065F) 70 0 70
|| (cp >= 0x0610 && cp <= 0x061A) || (cp >= 0x064B && cp <= 0x065F) 70 4 74
|| (cp >= 0x0610 && cp <= 0x061A) || (cp >= 0x064B && cp <= 0x065F) 70 0 70
1498 74 ▶ || cp == 0x0670 || (cp >= 0x06D6 && cp <= 0x06DC)
Branch: 3 branches
BranchTrueFalseTotal
|| cp == 0x0670 || (cp >= 0x06D6 && cp <= 0x06DC) 74 0 74
|| cp == 0x0670 || (cp >= 0x06D6 && cp <= 0x06DC) 70 4 74
|| cp == 0x0670 || (cp >= 0x06D6 && cp <= 0x06DC) 70 0 70
1499 74 ▶ || (cp >= 0x06DF && cp <= 0x06E4) || cp == 0x06E7 || cp == 0x06E8
Branch: 4 branches
BranchTrueFalseTotal
|| (cp >= 0x06DF && cp <= 0x06E4) || cp == 0x06E7 || cp == 0x06E8 70 4 74
|| (cp >= 0x06DF && cp <= 0x06E4) || cp == 0x06E7 || cp == 0x06E8 70 0 70
|| (cp >= 0x06DF && cp <= 0x06E4) || cp == 0x06E7 || cp == 0x06E8 74 0 74
|| (cp >= 0x06DF && cp <= 0x06E4) || cp == 0x06E7 || cp == 0x06E8 74 0 74
1500 74 ▶ || (cp >= 0x06EA && cp <= 0x06ED)
Branch: 2 branches
BranchTrueFalseTotal
|| (cp >= 0x06EA && cp <= 0x06ED) 70 4 74
|| (cp >= 0x06EA && cp <= 0x06ED) 70 0 70
1501 74 ▶ || (cp >= 0x0900 && cp <= 0x0902) || cp == 0x093A || cp == 0x093C
Branch: 4 branches
BranchTrueFalseTotal
|| (cp >= 0x0900 && cp <= 0x0902) || cp == 0x093A || cp == 0x093C 70 4 74
|| (cp >= 0x0900 && cp <= 0x0902) || cp == 0x093A || cp == 0x093C 70 0 70
|| (cp >= 0x0900 && cp <= 0x0902) || cp == 0x093A || cp == 0x093C 74 0 74
|| (cp >= 0x0900 && cp <= 0x0902) || cp == 0x093A || cp == 0x093C 74 0 74
1502 74 ▶ || (cp >= 0x0941 && cp <= 0x0948) || cp == 0x094D
Branch: 3 branches
BranchTrueFalseTotal
|| (cp >= 0x0941 && cp <= 0x0948) || cp == 0x094D 70 4 74
|| (cp >= 0x0941 && cp <= 0x0948) || cp == 0x094D 70 0 70
|| (cp >= 0x0941 && cp <= 0x0948) || cp == 0x094D 74 0 74
1503 74 ▶ || (cp >= 0x0951 && cp <= 0x0957) || cp == 0x0962 || cp == 0x0963
Branch: 4 branches
BranchTrueFalseTotal
|| (cp >= 0x0951 && cp <= 0x0957) || cp == 0x0962 || cp == 0x0963 70 4 74
|| (cp >= 0x0951 && cp <= 0x0957) || cp == 0x0962 || cp == 0x0963 70 0 70
|| (cp >= 0x0951 && cp <= 0x0957) || cp == 0x0962 || cp == 0x0963 74 0 74
|| (cp >= 0x0951 && cp <= 0x0957) || cp == 0x0962 || cp == 0x0963 74 0 74
1504 74 ▶ || cp == 0x0E31 || (cp >= 0x0E34 && cp <= 0x0E3A)
Branch: 3 branches
BranchTrueFalseTotal
|| cp == 0x0E31 || (cp >= 0x0E34 && cp <= 0x0E3A) 74 0 74
|| cp == 0x0E31 || (cp >= 0x0E34 && cp <= 0x0E3A) 70 4 74
|| cp == 0x0E31 || (cp >= 0x0E34 && cp <= 0x0E3A) 70 0 70
1505 74 ▶ || (cp >= 0x0E47 && cp <= 0x0E4E)
Branch: 2 branches
BranchTrueFalseTotal
|| (cp >= 0x0E47 && cp <= 0x0E4E) 70 4 74
|| (cp >= 0x0E47 && cp <= 0x0E4E) 70 0 70
1506 74 ▶ || (cp >= 0x1AB0 && cp <= 0x1AFF) || (cp >= 0x1DC0 && cp <= 0x1DFF)
Branch: 4 branches
BranchTrueFalseTotal
|| (cp >= 0x1AB0 && cp <= 0x1AFF) || (cp >= 0x1DC0 && cp <= 0x1DFF) 70 4 74
|| (cp >= 0x1AB0 && cp <= 0x1AFF) || (cp >= 0x1DC0 && cp <= 0x1DFF) 70 0 70
|| (cp >= 0x1AB0 && cp <= 0x1AFF) || (cp >= 0x1DC0 && cp <= 0x1DFF) 70 4 74
|| (cp >= 0x1AB0 && cp <= 0x1AFF) || (cp >= 0x1DC0 && cp <= 0x1DFF) 70 0 70
1507 149 ▶ || (cp >= 0x20D0 && cp <= 0x20FF) || (cp >= 0xFE20 && cp <= 0xFE2F);
Branch: 5 branches
BranchTrueFalseTotal
|| (cp >= 0x20D0 && cp <= 0x20FF) || (cp >= 0xFE20 && cp <= 0xFE2F); 74 1 75
|| (cp >= 0x20D0 && cp <= 0x20FF) || (cp >= 0xFE20 && cp <= 0xFE2F); 25 49 74
|| (cp >= 0x20D0 && cp <= 0x20FF) || (cp >= 0xFE20 && cp <= 0xFE2F); 25 0 25
|| (cp >= 0x20D0 && cp <= 0x20FF) || (cp >= 0xFE20 && cp <= 0xFE2F); 4 70 74
|| (cp >= 0x20D0 && cp <= 0x20FF) || (cp >= 0xFE20 && cp <= 0xFE2F); 0 4 4
1508 }
1509  
1510 2 static int same_line_glyphs(const pdfmake_text_glyph_t *a,
1511 const pdfmake_text_glyph_t *b, double tolerance)
1512 {
1513 2 ▶ double size = a->font_size > b->font_size ? a->font_size : b->font_size;
Branch: 1 branch
BranchTrueFalseTotal
double size = a->font_size > b->font_size ? a->font_size : b->font_size; 0 2 2
1514 2 ▶ return !a->vertical && !b->vertical && a->angle == b->angle
Branch: 2 branches
BranchTrueFalseTotal
return !a->vertical && !b->vertical && a->angle == b->angle 2 0 2
return !a->vertical && !b->vertical && a->angle == b->angle 2 0 2
1515 4 ▶ && fabs(a->across - b->across) <= tolerance * size;
Branch: 2 branches
BranchTrueFalseTotal
&& fabs(a->across - b->across) <= tolerance * size; 2 0 2
&& fabs(a->across - b->across) <= tolerance * size; 2 0 2
1516 }
1517  
1518 /* Keep each mark beside the letter it was painted with.
1519 *
1520 * The glyphs are still in the order the page painted them, where a mark
1521 * follows its base. Its own origin says little: producers place a mark
1522 * over the middle of the base, past its end, or over the next letter, so
1523 * sorting by position can carry it into the wrong cluster. A mark is given
1524 * a position a hair past its base, on the side the text is heading, so it
1525 * sorts next to the base and reads after it whichever way the run was
1526 * drawn. */
1527 #define MARK_STEP 1e-4
1528  
1529 110 static void settle_marks(pdfmake_text_glyph_t *glyphs, size_t n, double tolerance)
1530 {
1531 size_t i;
1532 19642 ▶ for (i = 1; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < n; i++) { 19532 110 19642
1533 19532 pdfmake_text_glyph_t *mark = &glyphs[i];
1534 const pdfmake_text_glyph_t *base;
1535 19532 size_t b = i, next = i + 1, rank;
1536 19532 double heading = 1.0;
1537  
1538 19532 ▶ if (!is_mark_codepoint(mark->unicode) || mark->vertical) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!is_mark_codepoint(mark->unicode) || mark->vertical) continue; 1 19531 19532
if (!is_mark_codepoint(mark->unicode) || mark->vertical) continue; 0 1 1
1539 1 ▶ while (b > 0 && is_mark_codepoint(glyphs[b - 1].unicode)) b--;
Branch: 2 branches
BranchTrueFalseTotal
while (b > 0 && is_mark_codepoint(glyphs[b - 1].unicode)) b--; 1 0 1
while (b > 0 && is_mark_codepoint(glyphs[b - 1].unicode)) b--; 0 1 1
1540 1 ▶ if (b == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b == 0) continue; 0 1 1
1541 1 base = &glyphs[b - 1];
1542 1 rank = i - b + 1;
1543 1 ▶ if (is_space_codepoint(base->unicode)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (is_space_codepoint(base->unicode)) continue; 0 1 1
1544 1 ▶ if (!same_line_glyphs(base, mark, tolerance)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!same_line_glyphs(base, mark, tolerance)) continue; 0 1 1
1545  
1546 1 ▶ while (next < n && is_mark_codepoint(glyphs[next].unicode)) next++;
Branch: 2 branches
BranchTrueFalseTotal
while (next < n && is_mark_codepoint(glyphs[next].unicode)) next++; 1 0 1
while (next < n && is_mark_codepoint(glyphs[next].unicode)) next++; 0 1 1
1547 1 ▶ if (next < n && same_line_glyphs(base, &glyphs[next], tolerance)) {
Branch: 2 branches
BranchTrueFalseTotal
if (next < n && same_line_glyphs(base, &glyphs[next], tolerance)) { 1 0 1
if (next < n && same_line_glyphs(base, &glyphs[next], tolerance)) { 1 0 1
1548 1 ▶ if (glyphs[next].along0 < base->along0) heading = -1.0;
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[next].along0 < base->along0) heading = -1.0; 0 1 1
1549 0 ▶ } else if (b >= 2 && same_line_glyphs(base, &glyphs[b - 2], tolerance)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (b >= 2 && same_line_glyphs(base, &glyphs[b - 2], tolerance)) { 0 0 0
} else if (b >= 2 && same_line_glyphs(base, &glyphs[b - 2], tolerance)) { 0 0 0
1550 0 ▶ if (base->along0 < glyphs[b - 2].along0) heading = -1.0;
Branch: 1 branch
BranchTrueFalseTotal
if (base->along0 < glyphs[b - 2].along0) heading = -1.0; 0 0 0
1551 }
1552  
1553 1 ▶ if (heading > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (heading > 0) { 1 0 1
1554 1 ▶ double end = mark->along1 > base->along1 ? mark->along1 : base->along1;
Branch: 1 branch
BranchTrueFalseTotal
double end = mark->along1 > base->along1 ? mark->along1 : base->along1; 1 0 1
1555 1 mark->along0 = base->along0 + MARK_STEP * (double)rank;
1556 1 mark->along1 = end;
1557 } else {
1558 0 mark->along0 = base->along0 - MARK_STEP * (double)rank;
1559 0 mark->along1 = mark->along0;
1560 }
1561 }
1562 110 }
1563  
1564 3821 static int is_ltr_letter(uint32_t cp) {
1565 3821 ▶ return (cp >= 'A' && cp <= 'Z') || (cp >= 'a' && cp <= 'z')
Branch: 3 branches
BranchTrueFalseTotal
return (cp >= 'A' && cp <= 'Z') || (cp >= 'a' && cp <= 'z') 2182 779 2961
return (cp >= 'A' && cp <= 'Z') || (cp >= 'a' && cp <= 'z') 1984 1058 3042
return (cp >= 'A' && cp <= 'Z') || (cp >= 'a' && cp <= 'z') 62 1922 1984
1566 1120 ▶ || (cp >= 0x00C0 && cp <= 0x058F) || (cp >= 0x0900 && cp <= 0x1FFF)
Branch: 4 branches
BranchTrueFalseTotal
|| (cp >= 0x00C0 && cp <= 0x058F) || (cp >= 0x0900 && cp <= 0x1FFF) 62 1058 1120
|| (cp >= 0x00C0 && cp <= 0x058F) || (cp >= 0x0900 && cp <= 0x1FFF) 61 1 62
|| (cp >= 0x00C0 && cp <= 0x058F) || (cp >= 0x0900 && cp <= 0x1FFF) 57 1062 1119
|| (cp >= 0x00C0 && cp <= 0x058F) || (cp >= 0x0900 && cp <= 0x1FFF) 57 0 57
1567 7642 ▶ || (cp >= 0x2E80 && cp <= 0xD7FF);
Branch: 3 branches
BranchTrueFalseTotal
|| (cp >= 0x2E80 && cp <= 0xD7FF); 2961 860 3821
|| (cp >= 0x2E80 && cp <= 0xD7FF); 12 1107 1119
|| (cp >= 0x2E80 && cp <= 0xD7FF); 7 5 12
1568 }
1569  
1570 /* A word is right-to-left when it has such letters and no left-to-right
1571 * ones. Its glyphs were gathered in visual order, so reading order is the
1572 * reverse. */
1573 2998 static int word_is_rtl(const pdfmake_text_word_t *w) {
1574 size_t i;
1575 2998 int rtl = 0;
1576 4110 ▶ for (i = 0; i < w->len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w->len; i++) { 3821 289 4110
1577 3821 ▶ if (is_ltr_letter(w->glyphs[i].unicode)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (is_ltr_letter(w->glyphs[i].unicode)) return 0; 2709 1112 3821
1578 1112 ▶ if (is_rtl_codepoint(w->glyphs[i].unicode)) rtl = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (is_rtl_codepoint(w->glyphs[i].unicode)) rtl = 1; 4 1108 1112
1579 }
1580 289 return rtl;
1581 }
1582  
1583 2998 static void word_to_reading_order(pdfmake_text_word_t *w) {
1584 size_t i;
1585 2998 ▶ if (!word_is_rtl(w)) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!word_is_rtl(w)) return; 2997 1 2998
1586 3 ▶ for (i = 0; i < w->len / 2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < w->len / 2; i++) { 2 1 3
1587 2 pdfmake_text_glyph_t swap = w->glyphs[i];
1588 2 w->glyphs[i] = w->glyphs[w->len - 1 - i];
1589 2 w->glyphs[w->len - 1 - i] = swap;
1590 }
1591 }
1592  
1593 /* Reverse each run of right-to-left words so the line reads in order. */
1594 802 static void line_to_reading_order(pdfmake_text_line_t *l) {
1595 802 size_t i = 0;
1596 3800 ▶ while (i < l->len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < l->len) { 2998 802 3800
1597 2998 size_t j = i, a, b;
1598 5996 ▶ while (j < l->len && l->words[j].len > 0
Branch: 1 branch
BranchTrueFalseTotal
while (j < l->len && l->words[j].len > 0 2998 0 2998
1599 5996 ▶ && is_rtl_codepoint(l->words[j].glyphs[0].unicode)) j++;
Branch: 2 branches
BranchTrueFalseTotal
&& is_rtl_codepoint(l->words[j].glyphs[0].unicode)) j++; 2998 1 2999
&& is_rtl_codepoint(l->words[j].glyphs[0].unicode)) j++; 1 2997 2998
1600 2998 ▶ if (j == i) { i++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (j == i) { i++; continue; } 2997 1 2998
1601 1 ▶ for (a = i, b = j - 1; a < b; a++, b--) {
Branch: 1 branch
BranchTrueFalseTotal
for (a = i, b = j - 1; a < b; a++, b--) { 0 1 1
1602 0 pdfmake_text_word_t swap = l->words[a];
1603 0 l->words[a] = l->words[b];
1604 0 l->words[b] = swap;
1605 }
1606 1 i = j;
1607 }
1608 802 }
1609  
1610 /* Detect column splits via largest-gap analysis on x0 values.
1611 *
1612 * Algorithm: take all unique-ish glyph x0 values, sort them, look for
1613 * contiguous runs where no glyph starts. The widest such run in the page
1614 * interior (not at extremes) is a likely gutter; split at its midpoint.
1615 * Recurse on each half to find multi-column layouts up to 8 total columns.
1616 *
1617 * Thresholds:
1618 * - min gutter width: 20pt (so we don't split on letter spacing)
1619 * - gutter must be in middle 70% of x-range (not at margins)
1620 * - only split if the two sides each contain ≥ 4 glyphs
1621 */
1622 /* Histogram bin size for column-gutter detection, in user-space points */
1623 #define COLUMN_BIN_SIZE 5.0
1624  
1625 /* Distinct baselines among glyphs lying within [x_from, x_to], capped — the
1626 * caller only ever asks "more than one?".
1627 *
1628 * The occupancy threshold below counts a bin as empty unless two glyphs cover
1629 * it, so on a sparse page the letters of one big heading read as whitespace and
1630 * a 20pt stretch between two of them looks like a gutter. A real gutter has
1631 * text on both sides of it on several lines; one heading's worth of glyphs
1632 * spans a single baseline, which is what tells the two apart. */
1633 #define COLUMN_BASELINE_TOL 1.0
1634 #define COLUMN_BASELINE_MAX 8
1635  
1636 42 static int count_baselines(const pdfmake_text_glyph_t *glyphs, size_t n,
1637 double x_from, double x_to)
1638 {
1639 double seen[COLUMN_BASELINE_MAX];
1640 42 int count = 0;
1641 size_t i;
1642 int j;
1643  
1644 7948 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7930 18 7948
1645 7930 const pdfmake_text_glyph_t *g = &glyphs[i];
1646 7930 ▶ if (g->x0 < x_from || g->x1 > x_to) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (g->x0 < x_from || g->x1 > x_to) continue; 3212 4718 7930
if (g->x0 < x_from || g->x1 > x_to) continue; 454 2758 3212
1647 13048 ▶ for (j = 0; j < count; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) 12783 265 13048
1648 12783 ▶ if (fabs(seen[j] - g->baseline) <= COLUMN_BASELINE_TOL) break;
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(seen[j] - g->baseline) <= COLUMN_BASELINE_TOL) break; 2493 10290 12783
1649 2758 ▶ if (j < count) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (j < count) continue; 2493 265 2758
1650 265 ▶ if (count >= COLUMN_BASELINE_MAX) return count;
Branch: 1 branch
BranchTrueFalseTotal
if (count >= COLUMN_BASELINE_MAX) return count; 24 241 265
1651 241 seen[count++] = g->baseline;
1652 }
1653 18 return count;
1654 }
1655  
1656 81 static void detect_column_splits_in_range(
1657 const pdfmake_text_glyph_t *glyphs, size_t n,
1658 double x_lo, double x_hi,
1659 double *out_splits, int *out_count, int max_count)
1660 {
1661 size_t n_bins;
1662 uint32_t *hist;
1663 size_t i;
1664 uint32_t max_cnt;
1665 uint32_t empty_thr;
1666 size_t safe_lo_bin;
1667 size_t safe_hi_bin;
1668 size_t best_run;
1669 size_t best_start;
1670 size_t cur_run;
1671 size_t cur_start;
1672 size_t mid;
1673 size_t min_gutter_bins;
1674 size_t active_left;
1675 size_t active_right;
1676 double gutter_lo;
1677 double gutter_hi;
1678 81 ▶ if (*out_count >= max_count) return;
Branch: 1 branch
BranchTrueFalseTotal
if (*out_count >= max_count) return; 0 81 81
1679 81 ▶ if (x_hi - x_lo < 100) return;
Branch: 1 branch
BranchTrueFalseTotal
if (x_hi - x_lo < 100) return; 1 80 81
1680  
1681 80 n_bins = (size_t)((x_hi - x_lo) / COLUMN_BIN_SIZE) + 1;
1682 80 ▶ if (n_bins < 16 || n_bins > 10000) return;
Branch: 2 branches
BranchTrueFalseTotal
if (n_bins < 16 || n_bins > 10000) return; 80 0 80
if (n_bins < 16 || n_bins > 10000) return; 0 80 80
1683  
1684 80 hist = calloc(n_bins, sizeof(uint32_t));
1685 80 ▶ if (!hist) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hist) return; 0 80 80
1686  
1687 /* Populate histogram by glyph bbox coverage (not just x0) */
1688 36619 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 36539 80 36619
1689 36539 const pdfmake_text_glyph_t *g = &glyphs[i];
1690 double a;
1691 double b;
1692 size_t ba;
1693 size_t bb;
1694 36539 ▶ if (g->x1 < x_lo || g->x0 > x_hi) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (g->x1 < x_lo || g->x0 > x_hi) continue; 30284 6255 36539
if (g->x1 < x_lo || g->x0 > x_hi) continue; 2546 27738 30284
1695 27738 ▶ a = g->x0 > x_lo ? g->x0 : x_lo;
Branch: 1 branch
BranchTrueFalseTotal
a = g->x0 > x_lo ? g->x0 : x_lo; 26802 936 27738
1696 27738 ▶ b = g->x1 < x_hi ? g->x1 : x_hi;
Branch: 1 branch
BranchTrueFalseTotal
b = g->x1 < x_hi ? g->x1 : x_hi; 27617 121 27738
1697 27738 ▶ if (b <= a) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b <= a) continue; 0 27738 27738
1698 27738 ba = (size_t)((a - x_lo) / COLUMN_BIN_SIZE);
1699 27738 bb = (size_t)((b - x_lo) / COLUMN_BIN_SIZE);
1700 27738 ▶ if (bb >= n_bins) bb = n_bins - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (bb >= n_bins) bb = n_bins - 1; 0 27738 27738
1701 {
1702 size_t k;
1703 84386 ▶ for (k = ba; k <= bb; k++) hist[k]++;
Branch: 1 branch
BranchTrueFalseTotal
for (k = ba; k <= bb; k++) hist[k]++; 56648 27738 84386
1704 }
1705 }
1706  
1707 80 max_cnt = 0;
1708 4799 ▶ for (i = 0; i < n_bins; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_bins; i++) 4719 80 4799
1709 4719 ▶ if (hist[i] > max_cnt) max_cnt = hist[i];
Branch: 1 branch
BranchTrueFalseTotal
if (hist[i] > max_cnt) max_cnt = hist[i]; 292 4427 4719
1710 80 ▶ if (max_cnt < 4) { free(hist); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (max_cnt < 4) { free(hist); return; } 13 67 80
1711  
1712 /* "Near-empty" = less than 15% of peak density. Higher than 2% because
1713 * full-width headers/footers contribute thin residual occupancy across
1714 * the gutter, and we don't want those to mask real column gaps. */
1715 67 empty_thr = max_cnt / 6 + 1;
1716  
1717 /* Only consider gutters whose midpoint is in the middle 70% of range */
1718 67 safe_lo_bin = n_bins * 15 / 100;
1719 67 safe_hi_bin = n_bins * 85 / 100;
1720  
1721 67 best_run = 0;
1722 67 best_start = 0;
1723 67 cur_run = 0;
1724 67 cur_start = 0;
1725 4295 ▶ for (i = 0; i < n_bins; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_bins; i++) { 4228 67 4295
1726 4228 ▶ if (hist[i] < empty_thr) {
Branch: 1 branch
BranchTrueFalseTotal
if (hist[i] < empty_thr) { 1640 2588 4228
1727 1640 ▶ if (cur_run == 0) cur_start = i;
Branch: 1 branch
BranchTrueFalseTotal
if (cur_run == 0) cur_start = i; 202 1438 1640
1728 1640 cur_run++;
1729 } else {
1730 2588 ▶ if (cur_run > best_run) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_run > best_run) { 75 2513 2588
1731 75 mid = cur_start + cur_run / 2;
1732 75 ▶ if (mid >= safe_lo_bin && mid <= safe_hi_bin) {
Branch: 2 branches
BranchTrueFalseTotal
if (mid >= safe_lo_bin && mid <= safe_hi_bin) { 72 3 75
if (mid >= safe_lo_bin && mid <= safe_hi_bin) { 72 0 72
1733 72 best_run = cur_run;
1734 72 best_start = cur_start;
1735 }
1736 }
1737 2588 cur_run = 0;
1738 }
1739 }
1740 67 ▶ if (cur_run > best_run) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_run > best_run) { 49 18 67
1741 49 mid = cur_start + cur_run / 2;
1742 49 ▶ if (mid >= safe_lo_bin && mid <= safe_hi_bin) {
Branch: 2 branches
BranchTrueFalseTotal
if (mid >= safe_lo_bin && mid <= safe_hi_bin) { 49 0 49
if (mid >= safe_lo_bin && mid <= safe_hi_bin) { 25 24 49
1743 25 best_run = cur_run;
1744 25 best_start = cur_start;
1745 }
1746 }
1747  
1748 67 min_gutter_bins = (size_t)(20.0 / COLUMN_BIN_SIZE); /* 20pt */
1749 67 ▶ if (best_run >= min_gutter_bins) {
Branch: 1 branch
BranchTrueFalseTotal
if (best_run >= min_gutter_bins) { 50 17 67
1750 50 active_left = 0;
1751 50 active_right = 0;
1752 1451 ▶ for (i = 0; i < best_start; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < best_start; i++) 1401 50 1451
1753 1401 ▶ if (hist[i] >= empty_thr) active_left++;
Branch: 1 branch
BranchTrueFalseTotal
if (hist[i] >= empty_thr) active_left++; 1277 124 1401
1754 796 ▶ for (i = best_start + best_run; i < n_bins; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = best_start + best_run; i < n_bins; i++) 746 50 796
1755 746 ▶ if (hist[i] >= empty_thr) active_right++;
Branch: 1 branch
BranchTrueFalseTotal
if (hist[i] >= empty_thr) active_right++; 306 440 746
1756  
1757 50 gutter_lo = x_lo + best_start * COLUMN_BIN_SIZE;
1758 50 gutter_hi = x_lo + (best_start + best_run) * COLUMN_BIN_SIZE;
1759  
1760 71 ▶ if (active_left >= 4 && active_right >= 4 &&
Branch: 1 branch
BranchTrueFalseTotal
if (active_left >= 4 && active_right >= 4 && 46 4 50
1761 42 ▶ count_baselines(glyphs, n, x_lo, gutter_lo) >= 2 &&
Branch: 1 branch
BranchTrueFalseTotal
count_baselines(glyphs, n, x_lo, gutter_lo) >= 2 && 16 5 21
1762 21 count_baselines(glyphs, n, gutter_hi, x_hi) >= 2) {
1763 16 double mid_x = x_lo + (best_start + best_run / 2.0) * COLUMN_BIN_SIZE;
1764  
1765 16 int pos = *out_count;
1766 16 ▶ while (pos > 0 && out_splits[pos - 1] > mid_x) {
Branch: 2 branches
BranchTrueFalseTotal
while (pos > 0 && out_splits[pos - 1] > mid_x) { 0 16 16
while (pos > 0 && out_splits[pos - 1] > mid_x) { 0 0 0
1767 0 out_splits[pos] = out_splits[pos - 1]; pos--;
1768 }
1769 16 out_splits[pos] = mid_x;
1770 16 (*out_count)++;
1771  
1772 16 free(hist);
1773 /* Recurse into each half to find further column splits */
1774 16 detect_column_splits_in_range(glyphs, n,
1775 x_lo, mid_x, out_splits, out_count, max_count);
1776 16 detect_column_splits_in_range(glyphs, n,
1777 mid_x, x_hi, out_splits, out_count, max_count);
1778 16 return;
1779 }
1780 }
1781  
1782 51 free(hist);
1783 }
1784  
1785 110 static void detect_column_splits(pdfmake_textract_result_t *result) {
1786 double x_lo;
1787 double x_hi;
1788 size_t i;
1789 110 result->column_split_count = 0;
1790 110 ▶ if (result->raw_len < 16) return;
Branch: 1 branch
BranchTrueFalseTotal
if (result->raw_len < 16) return; 51 59 110
1791  
1792 59 x_lo = 1e18;
1793 59 x_hi = -1e18;
1794 19184 ▶ for (i = 0; i < result->raw_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < result->raw_len; i++) { 19125 59 19184
1795 19125 ▶ if (result->raw_glyphs[i].x0 < x_lo) x_lo = result->raw_glyphs[i].x0;
Branch: 1 branch
BranchTrueFalseTotal
if (result->raw_glyphs[i].x0 < x_lo) x_lo = result->raw_glyphs[i].x0; 81 19044 19125
1796 19125 ▶ if (result->raw_glyphs[i].x1 > x_hi) x_hi = result->raw_glyphs[i].x1;
Branch: 1 branch
BranchTrueFalseTotal
if (result->raw_glyphs[i].x1 > x_hi) x_hi = result->raw_glyphs[i].x1; 1706 17419 19125
1797 }
1798 59 ▶ if (x_hi - x_lo < 100) return;
Branch: 1 branch
BranchTrueFalseTotal
if (x_hi - x_lo < 100) return; 10 49 59
1799  
1800 49 detect_column_splits_in_range(result->raw_glyphs, result->raw_len,
1801 49 x_lo, x_hi, result->column_splits, &result->column_split_count,
1802 (int)(sizeof(result->column_splits) / sizeof(double)));
1803 }
1804  
1805 17593 static void update_bbox_from_glyph(double *x0, double *y0, double *x1, double *y1,
1806 const pdfmake_text_glyph_t *g) {
1807 17593 ▶ if (g->x0 < *x0) *x0 = g->x0;
Branch: 1 branch
BranchTrueFalseTotal
if (g->x0 < *x0) *x0 = g->x0; 2998 14595 17593
1808 17593 ▶ if (g->y0 < *y0) *y0 = g->y0;
Branch: 1 branch
BranchTrueFalseTotal
if (g->y0 < *y0) *y0 = g->y0; 3012 14581 17593
1809 17593 ▶ if (g->x1 > *x1) *x1 = g->x1;
Branch: 1 branch
BranchTrueFalseTotal
if (g->x1 > *x1) *x1 = g->x1; 17586 7 17593
1810 17593 ▶ if (g->y1 > *y1) *y1 = g->y1;
Branch: 1 branch
BranchTrueFalseTotal
if (g->y1 > *y1) *y1 = g->y1; 3002 14591 17593
1811 17593 }
1812  
1813 17593 static int word_push_glyph(pdfmake_text_word_t *w, const pdfmake_text_glyph_t *g) {
1814 17593 ▶ if (w->len >= w->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->len >= w->cap) { 3467 14126 17593
1815 3467 ▶ size_t new_cap = w->cap == 0 ? 8 : w->cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = w->cap == 0 ? 8 : w->cap * 2; 469 2998 3467
1816 3467 pdfmake_text_glyph_t *arr = realloc(w->glyphs, new_cap * sizeof(*arr));
1817 3467 ▶ if (!arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!arr) return 0; 0 3467 3467
1818 3467 w->glyphs = arr;
1819 3467 w->cap = new_cap;
1820 }
1821 17593 w->glyphs[w->len++] = *g;
1822 17593 return 1;
1823 }
1824  
1825 2998 static int line_push_word(pdfmake_text_line_t *l, const pdfmake_text_word_t *w) {
1826 2998 ▶ if (l->len >= l->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->len >= l->cap) { 887 2111 2998
1827 887 ▶ size_t new_cap = l->cap == 0 ? 8 : l->cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = l->cap == 0 ? 8 : l->cap * 2; 85 802 887
1828 887 pdfmake_text_word_t *arr = realloc(l->words, new_cap * sizeof(*arr));
1829 887 ▶ if (!arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!arr) return 0; 0 887 887
1830 887 l->words = arr;
1831 887 l->cap = new_cap;
1832 }
1833 2998 l->words[l->len++] = *w;
1834 2998 return 1;
1835 }
1836  
1837 802 static int block_push_line(pdfmake_text_block_t *b, const pdfmake_text_line_t *l) {
1838 802 ▶ if (b->len >= b->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->len >= b->cap) { 377 425 802
1839 377 ▶ size_t new_cap = b->cap == 0 ? 8 : b->cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = b->cap == 0 ? 8 : b->cap * 2; 35 342 377
1840 377 pdfmake_text_line_t *arr = realloc(b->lines, new_cap * sizeof(*arr));
1841 377 ▶ if (!arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!arr) return 0; 0 377 377
1842 377 b->lines = arr;
1843 377 b->cap = new_cap;
1844 }
1845 802 b->lines[b->len++] = *l;
1846 802 return 1;
1847 }
1848  
1849 342 static int result_push_block(pdfmake_textract_result_t *r, const pdfmake_text_block_t *b) {
1850 342 ▶ if (r->len >= r->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->len >= r->cap) { 142 200 342
1851 142 ▶ size_t new_cap = r->cap == 0 ? 4 : r->cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = r->cap == 0 ? 4 : r->cap * 2; 32 110 142
1852 142 pdfmake_text_block_t *arr = realloc(r->blocks, new_cap * sizeof(*arr));
1853 142 ▶ if (!arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!arr) return 0; 0 142 142
1854 142 r->blocks = arr;
1855 142 r->cap = new_cap;
1856 }
1857 342 r->blocks[r->len++] = *b;
1858 342 return 1;
1859 }
1860  
1861 110 pdfmake_err_t pdfmake_textract_aggregate(
1862 pdfmake_textract_result_t *result,
1863 const pdfmake_textract_options_t *options)
1864 {
1865 pdfmake_textract_options_t opts;
1866 pdfmake_text_word_t *words;
1867 size_t words_len;
1868 size_t words_cap;
1869 pdfmake_text_word_t cur_word;
1870 pdfmake_text_line_t *lines;
1871 size_t lines_len;
1872 size_t lines_cap;
1873 pdfmake_text_line_t cur_line;
1874 110 double cur_line_size = 0;
1875 pdfmake_text_block_t cur_block;
1876 size_t i;
1877 110 ▶ if (!result || result->raw_len == 0) return PDFMAKE_OK;
Branch: 2 branches
BranchTrueFalseTotal
if (!result || result->raw_len == 0) return PDFMAKE_OK; 110 0 110
if (!result || result->raw_len == 0) return PDFMAKE_OK; 0 110 110
1878  
1879 110 ▶ opts = options ? *options : pdfmake_textract_default_options();
Branch: 1 branch
BranchTrueFalseTotal
opts = options ? *options : pdfmake_textract_default_options(); 110 0 110
1880  
1881 /* Phase 10: detect column splits before sorting so reading order is
1882 * column-major (fill column 1 top-to-bottom, then column 2, ...). */
1883 110 detect_column_splits(result);
1884 110 g_sort_column_splits = result->column_splits;
1885 110 g_sort_column_count = result->column_split_count;
1886  
1887 110 settle_spaces(result->raw_glyphs, result->raw_len, opts.line_tolerance);
1888 110 settle_marks(result->raw_glyphs, result->raw_len, opts.line_tolerance);
1889  
1890 /* Sort glyphs into reading order */
1891 110 qsort(result->raw_glyphs, result->raw_len, sizeof(pdfmake_text_glyph_t),
1892 cmp_glyph_position);
1893 110 assign_lines(result->raw_glyphs, result->raw_len, opts.line_tolerance);
1894  
1895 /* Don't leak static pointers past this function */
1896 110 g_sort_column_splits = NULL;
1897 110 g_sort_column_count = 0;
1898  
1899 /* Phase 5: Kern-aware word grouping.
1900 *
1901 * We split into a new word when ONE of:
1902 * - previous glyph is a space (U+0020)
1903 * - current glyph is a space (low priority)
1904 * - baseline jumped significantly (implies new line)
1905 * - horizontal gap exceeds a context-sensitive threshold
1906 *
1907 * Key insight from Phase 5: the gap threshold must be wider when the
1908 * previous glyph's advance is a fallback (i.e., we didn't know the real
1909 * width). In that case a "real-looking" Td that's actually intra-word
1910 * spacing (author-driven font-change mid-word in subsetted fonts) looks
1911 * like a big visible gap. Using ~0.9 em here keeps "Data" glued together
1912 * across font changes while still catching inter-word spaces.
1913 *
1914 * When we trust the advance, we use the tighter 0.3-em threshold —
1915 * that's what PDF typesetters use as a natural space width.
1916 *
1917 * We never split on negative or zero gaps; those come from kerning
1918 * overlap in TJ arrays and always represent intra-word positioning.
1919 */
1920 110 words = NULL;
1921 110 words_len = 0;
1922 110 words_cap = 0;
1923  
1924 110 memset(&cur_word, 0, sizeof(cur_word));
1925 110 cur_word.x0 = cur_word.y0 = 1e18;
1926 110 cur_word.x1 = cur_word.y1 = -1e18;
1927 110 cur_word.mcid = -1;
1928  
1929 19752 ▶ for (i = 0; i < result->raw_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < result->raw_len; i++) { 19642 110 19752
1930 19642 pdfmake_text_glyph_t *g = &result->raw_glyphs[i];
1931  
1932 19642 int new_word = 0;
1933 19642 ▶ if (cur_word.len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_word.len == 0) { 2141 17501 19642
1934 2141 new_word = 0; /* first glyph */
1935 } else {
1936 17501 pdfmake_text_glyph_t *prev = &cur_word.glyphs[cur_word.len - 1];
1937  
1938 17501 ▶ if (g->vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (g->vertical) { 2 17499 17501
1939 /* Phase 14: vertical word — split on column change (x jump)
1940 * or on a large vertical gap. */
1941 2 double col_diff = fabs(g->x0 - prev->x0);
1942 /* prev->y0 is the bottom; g->y1 is the top of the new glyph.
1943 * In top-to-bottom flow, gap = prev->y0 - g->y1 > 0. */
1944 2 double v_gap = prev->y0 - g->y1;
1945  
1946 2 ▶ if (col_diff > g->font_size * 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if (col_diff > g->font_size * 0.5) { 0 2 2
1947 0 new_word = 1;
1948 }
1949 2 ▶ if (!new_word && v_gap > opts.word_gap_factor * g->font_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (!new_word && v_gap > opts.word_gap_factor * g->font_size) { 2 0 2
if (!new_word && v_gap > opts.word_gap_factor * g->font_size) { 0 2 2
1950 0 new_word = 1;
1951 }
1952 } else {
1953 17499 double gap = g->along0 - prev->along1;
1954  
1955 /* Rule 1: a new line is a new word */
1956 17499 ▶ if (g->line != prev->line) {
Branch: 1 branch
BranchTrueFalseTotal
if (g->line != prev->line) { 561 16938 17499
1957 561 new_word = 1;
1958 }
1959  
1960 /* The same glyph painted twice in one place is one glyph:
1961 * synthetic bold and shadows are drawn this way. */
1962 17499 ▶ if (!new_word && g->unicode == prev->unicode
Branch: 2 branches
BranchTrueFalseTotal
if (!new_word && g->unicode == prev->unicode 16938 561 17499
if (!new_word && g->unicode == prev->unicode 418 16520 16938
1963 418 ▶ && fabs(g->along0 - prev->along0) < 0.06 * g->font_size
Branch: 1 branch
BranchTrueFalseTotal
&& fabs(g->along0 - prev->along0) < 0.06 * g->font_size 4 414 418
1964 4 ▶ && fabs(g->across - prev->across) < 0.06 * g->font_size) {
Branch: 1 branch
BranchTrueFalseTotal
&& fabs(g->across - prev->across) < 0.06 * g->font_size) { 4 0 4
1965 4 continue;
1966 }
1967  
1968 /* Rule 2: explicit space character = word boundary */
1969 34429 if (!new_word &&
1970 16934 is_space_codepoint(g->unicode)) {
1971 1972 new_word = 1;
1972 }
1973  
1974 /* Rule 3: gap threshold based on advance reliability.
1975 * - Reliable advances: tight ~0.3 em threshold
1976 * - Fallback advances: generous ~0.9 em threshold
1977 * - Negative/zero gaps: never split (kerning) */
1978 17495 ▶ if (!new_word && gap > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!new_word && gap > 0) { 14962 2533 17495
if (!new_word && gap > 0) { 6421 8541 14962
1979 double threshold;
1980 6421 ▶ if (prev->reliable_advance) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev->reliable_advance) { 6421 0 6421
1981 6421 threshold = opts.word_gap_factor * g->font_size;
1982 } else {
1983 /* Unreliable widths: need a bigger gap to call it a space */
1984 0 threshold = 0.9 * g->font_size;
1985 }
1986 6421 ▶ if (gap > threshold) new_word = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (gap > threshold) new_word = 1; 369 6052 6421
1987 }
1988 }
1989 }
1990  
1991 19638 ▶ if (new_word && cur_word.len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (new_word && cur_word.len > 0) { 2902 16736 19638
if (new_word && cur_word.len > 0) { 2902 0 2902
1992 /* Flush current word */
1993 2902 ▶ if (words_len >= words_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (words_len >= words_cap) { 165 2737 2902
1994 165 ▶ words_cap = words_cap == 0 ? 16 : words_cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
words_cap = words_cap == 0 ? 16 : words_cap * 2; 69 96 165
1995 165 words = realloc(words, words_cap * sizeof(*words));
1996 165 ▶ if (!words) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!words) return PDFMAKE_ENOMEM; 0 165 165
1997 }
1998 2902 word_to_reading_order(&cur_word);
1999 2902 words[words_len++] = cur_word;
2000 2902 memset(&cur_word, 0, sizeof(cur_word));
2001 2902 cur_word.x0 = cur_word.y0 = 1e18;
2002 2902 cur_word.x1 = cur_word.y1 = -1e18;
2003 2902 cur_word.mcid = -1;
2004 }
2005  
2006 /* Skip spaces (don't include in words) */
2007 19638 ▶ if (is_space_codepoint(g->unicode)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (is_space_codepoint(g->unicode)) continue; 2045 17593 19638
2008  
2009 /* Phase 12: first glyph sets the word's MCID and its baseline */
2010 17593 ▶ if (cur_word.len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_word.len == 0) { 2998 14595 17593
2011 2998 cur_word.mcid = g->mcid;
2012 2998 cur_word.baseline = g->baseline;
2013 }
2014  
2015 17593 word_push_glyph(&cur_word, g);
2016 17593 update_bbox_from_glyph(&cur_word.x0, &cur_word.y0,
2017 &cur_word.x1, &cur_word.y1, g);
2018 }
2019 /* Flush last word */
2020 110 ▶ if (cur_word.len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_word.len > 0) { 96 14 110
2021 96 ▶ if (words_len >= words_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (words_len >= words_cap) { 15 81 96
2022 15 ▶ words_cap = words_cap == 0 ? 16 : words_cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
words_cap = words_cap == 0 ? 16 : words_cap * 2; 1 14 15
2023 15 words = realloc(words, words_cap * sizeof(*words));
2024 }
2025 96 ▶ if (words) {
Branch: 1 branch
BranchTrueFalseTotal
if (words) { 96 0 96
2026 96 word_to_reading_order(&cur_word);
2027 96 words[words_len++] = cur_word;
2028 }
2029 }
2030  
2031 110 ▶ if (words_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (words_len == 0) { 0 110 110
2032 0 free(words);
2033 0 return PDFMAKE_OK;
2034 }
2035  
2036 /* Phase 2: Group words into lines (same baseline) */
2037 110 lines = NULL;
2038 110 lines_len = 0;
2039 110 lines_cap = 0;
2040  
2041 110 memset(&cur_line, 0, sizeof(cur_line));
2042 110 cur_line.x0 = cur_line.y0 = 1e18;
2043 110 cur_line.x1 = cur_line.y1 = -1e18;
2044  
2045 3108 ▶ for (i = 0; i < words_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < words_len; i++) { 2998 110 3108
2046 2998 pdfmake_text_word_t *w = &words[i];
2047  
2048 /* Phase 14: detect whether this word is vertical by checking its
2049 * first glyph. Line grouping then compares x (column) instead of
2050 * baseline y. */
2051 2998 ▶ int w_vertical = (w->len > 0 && w->glyphs[0].vertical);
Branch: 2 branches
BranchTrueFalseTotal
int w_vertical = (w->len > 0 && w->glyphs[0].vertical); 2998 0 2998
int w_vertical = (w->len > 0 && w->glyphs[0].vertical); 1 2997 2998
2052  
2053 2998 int new_line = 0;
2054 2998 ▶ if (cur_line.len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_line.len == 0) { 110 2888 2998
2055 110 cur_line.baseline_y = w->baseline;
2056 } else {
2057 2888 double ref_size = cur_line.words[0].glyphs[0].font_size;
2058 2888 ▶ if (w_vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (w_vertical) { 0 2888 2888
2059 /* Same column = same vertical line. */
2060 0 double col_diff = fabs(w->x0 - cur_line.words[0].x0);
2061 0 ▶ if (col_diff > ref_size * 0.5) new_line = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (col_diff > ref_size * 0.5) new_line = 1; 0 0 0
2062 } else {
2063 2888 ▶ if (w->glyphs[0].line != cur_line.words[0].glyphs[0].line) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->glyphs[0].line != cur_line.words[0].glyphs[0].line) { 692 2196 2888
2064 692 new_line = 1;
2065 2196 ▶ } else if (w->glyphs[0].font_size > cur_line_size) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (w->glyphs[0].font_size > cur_line_size) { 39 2157 2196
2066 39 cur_line.baseline_y = w->baseline;
2067 }
2068 }
2069 }
2070  
2071 2998 ▶ if (new_line && cur_line.len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (new_line && cur_line.len > 0) { 692 2306 2998
if (new_line && cur_line.len > 0) { 692 0 692
2072 692 ▶ if (lines_len >= lines_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (lines_len >= lines_cap) { 99 593 692
2073 99 ▶ lines_cap = lines_cap == 0 ? 8 : lines_cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
lines_cap = lines_cap == 0 ? 8 : lines_cap * 2; 49 50 99
2074 99 lines = realloc(lines, lines_cap * sizeof(*lines));
2075 99 ▶ if (!lines) { free(words); return PDFMAKE_ENOMEM; }
Branch: 1 branch
BranchTrueFalseTotal
if (!lines) { free(words); return PDFMAKE_ENOMEM; } 0 99 99
2076 }
2077 692 line_to_reading_order(&cur_line);
2078 692 lines[lines_len++] = cur_line;
2079 692 memset(&cur_line, 0, sizeof(cur_line));
2080 692 cur_line.x0 = cur_line.y0 = 1e18;
2081 692 cur_line.x1 = cur_line.y1 = -1e18;
2082 692 cur_line.baseline_y = w->baseline;
2083 }
2084  
2085 2998 ▶ if (cur_line.len == 0 || w->glyphs[0].font_size > cur_line_size)
Branch: 2 branches
BranchTrueFalseTotal
if (cur_line.len == 0 || w->glyphs[0].font_size > cur_line_size) 2196 802 2998
if (cur_line.len == 0 || w->glyphs[0].font_size > cur_line_size) 39 2157 2196
2086 841 cur_line_size = w->glyphs[0].font_size;
2087 2998 line_push_word(&cur_line, w);
2088 2998 ▶ if (w->x0 < cur_line.x0) cur_line.x0 = w->x0;
Branch: 1 branch
BranchTrueFalseTotal
if (w->x0 < cur_line.x0) cur_line.x0 = w->x0; 802 2196 2998
2089 2998 ▶ if (w->y0 < cur_line.y0) cur_line.y0 = w->y0;
Branch: 1 branch
BranchTrueFalseTotal
if (w->y0 < cur_line.y0) cur_line.y0 = w->y0; 820 2178 2998
2090 2998 ▶ if (w->x1 > cur_line.x1) cur_line.x1 = w->x1;
Branch: 1 branch
BranchTrueFalseTotal
if (w->x1 > cur_line.x1) cur_line.x1 = w->x1; 2996 2 2998
2091 2998 ▶ if (w->y1 > cur_line.y1) cur_line.y1 = w->y1;
Branch: 1 branch
BranchTrueFalseTotal
if (w->y1 > cur_line.y1) cur_line.y1 = w->y1; 846 2152 2998
2092 }
2093 110 ▶ if (cur_line.len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_line.len > 0) { 110 0 110
2094 110 ▶ if (lines_len >= lines_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (lines_len >= lines_cap) { 61 49 110
2095 61 ▶ lines_cap = lines_cap == 0 ? 8 : lines_cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
lines_cap = lines_cap == 0 ? 8 : lines_cap * 2; 1 60 61
2096 61 lines = realloc(lines, lines_cap * sizeof(*lines));
2097 }
2098 110 ▶ if (lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (lines) { 110 0 110
2099 110 line_to_reading_order(&cur_line);
2100 110 lines[lines_len++] = cur_line;
2101 }
2102 }
2103 110 free(words); /* words are now owned by lines */
2104  
2105 /* Phase 3: Group lines into blocks */
2106 110 memset(&cur_block, 0, sizeof(cur_block));
2107 110 cur_block.x0 = cur_block.y0 = 1e18;
2108 110 cur_block.x1 = cur_block.y1 = -1e18;
2109  
2110 912 ▶ for (i = 0; i < lines_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < lines_len; i++) { 802 110 912
2111 802 pdfmake_text_line_t *l = &lines[i];
2112  
2113 802 int new_block = 0;
2114 802 ▶ if (cur_block.len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_block.len > 0) { 692 110 802
2115 692 pdfmake_text_line_t *prev = &cur_block.lines[cur_block.len - 1];
2116 692 double ref_size = prev->words[0].glyphs[0].font_size;
2117 1384 ▶ int l_vertical = (l->words[0].len > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
int l_vertical = (l->words[0].len > 0 && 692 0 692
2118 692 ▶ l->words[0].glyphs[0].vertical);
Branch: 1 branch
BranchTrueFalseTotal
l->words[0].glyphs[0].vertical); 0 692 692
2119 double gap;
2120 692 ▶ if (l_vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (l_vertical) { 0 692 692
2121 /* Vertical: block boundary when columns are far apart in x. */
2122 0 gap = fabs(prev->words[0].x0 - l->words[0].x0);
2123 } else {
2124 692 gap = fabs(prev->baseline_y - l->baseline_y);
2125 }
2126 692 ▶ if (gap > opts.block_leading * ref_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (gap > opts.block_leading * ref_size) { 232 460 692
2127 232 new_block = 1;
2128 }
2129 692 ▶ if (prev->words[0].glyphs[0].angle != l->words[0].glyphs[0].angle) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev->words[0].glyphs[0].angle != l->words[0].glyphs[0].angle) { 0 692 692
2130 0 new_block = 1;
2131 }
2132 }
2133  
2134 802 ▶ if (new_block && cur_block.len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (new_block && cur_block.len > 0) { 232 570 802
if (new_block && cur_block.len > 0) { 232 0 232
2135 232 result_push_block(result, &cur_block);
2136 232 memset(&cur_block, 0, sizeof(cur_block));
2137 232 cur_block.x0 = cur_block.y0 = 1e18;
2138 232 cur_block.x1 = cur_block.y1 = -1e18;
2139 }
2140  
2141 802 block_push_line(&cur_block, l);
2142 802 ▶ if (l->x0 < cur_block.x0) cur_block.x0 = l->x0;
Branch: 1 branch
BranchTrueFalseTotal
if (l->x0 < cur_block.x0) cur_block.x0 = l->x0; 346 456 802
2143 802 ▶ if (l->y0 < cur_block.y0) cur_block.y0 = l->y0;
Branch: 1 branch
BranchTrueFalseTotal
if (l->y0 < cur_block.y0) cur_block.y0 = l->y0; 802 0 802
2144 802 ▶ if (l->x1 > cur_block.x1) cur_block.x1 = l->x1;
Branch: 1 branch
BranchTrueFalseTotal
if (l->x1 > cur_block.x1) cur_block.x1 = l->x1; 401 401 802
2145 802 ▶ if (l->y1 > cur_block.y1) cur_block.y1 = l->y1;
Branch: 1 branch
BranchTrueFalseTotal
if (l->y1 > cur_block.y1) cur_block.y1 = l->y1; 342 460 802
2146 }
2147 110 ▶ if (cur_block.len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_block.len > 0) { 110 0 110
2148 110 result_push_block(result, &cur_block);
2149 }
2150 110 free(lines); /* lines are now owned by blocks */
2151  
2152 110 return PDFMAKE_OK;
2153 }
2154  
2155 /*============================================================================
2156 * Convenience: run extraction in one call
2157 *==========================================================================*/
2158  
2159 110 pdfmake_err_t pdfmake_textract_run(
2160 pdfmake_interp_t *interp,
2161 const uint8_t *content, size_t content_len,
2162 const pdfmake_textract_options_t *options,
2163 pdfmake_textract_result_t *result)
2164 {
2165 pdfmake_visitor_t visitor;
2166 pdfmake_err_t err;
2167 110 ▶ if (!interp || !content || !result) return PDFMAKE_EINVAL;
Branch: 3 branches
BranchTrueFalseTotal
if (!interp || !content || !result) return PDFMAKE_EINVAL; 110 0 110
if (!interp || !content || !result) return PDFMAKE_EINVAL; 110 0 110
if (!interp || !content || !result) return PDFMAKE_EINVAL; 0 110 110
2168  
2169 /* Propagate per-run options that the visitor needs to see during
2170 * interpretation (aggregate-phase options stay in `options`). */
2171 110 ▶ if (options) {
Branch: 1 branch
BranchTrueFalseTotal
if (options) { 110 0 110
2172 110 result->include_invisible = options->include_invisible;
2173 }
2174  
2175 /* Set up visitor */
2176 110 visitor = pdfmake_textract_visitor(result);
2177 110 pdfmake_interp_set_visitor(interp, &visitor);
2178  
2179 /* Interpret content stream */
2180 110 err = pdfmake_interpret(interp, content, content_len);
2181 110 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 110 110
2182  
2183 /* Aggregate */
2184 110 return pdfmake_textract_aggregate(result, options);
2185 }
2186  
2187 /*============================================================================
2188 * Phase 12: Structure tree resolution
2189 *
2190 * Walks a /StructTreeRoot subtree and populates result->struct_map with one
2191 * entry per MCID encountered, pairing it with the /S role of the nearest
2192 * enclosing StructElem. The resulting flat map is consulted after extraction
2193 * so each word can be tagged with its structure role.
2194 *==========================================================================*/
2195  
2196 3 static int struct_map_push(pdfmake_textract_result_t *r, int32_t mcid,
2197 uint32_t role_id)
2198 {
2199 3 ▶ if (mcid < 0 || role_id == 0) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (mcid < 0 || role_id == 0) return 1; 3 0 3
if (mcid < 0 || role_id == 0) return 1; 0 3 3
2200 3 ▶ if (r->struct_map_len >= r->struct_map_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->struct_map_len >= r->struct_map_cap) { 1 2 3
2201 1 ▶ size_t new_cap = r->struct_map_cap == 0 ? 16 : r->struct_map_cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
size_t new_cap = r->struct_map_cap == 0 ? 16 : r->struct_map_cap * 2; 0 1 1
2202 1 pdfmake_struct_map_entry_t *n = realloc(
2203 1 r->struct_map, new_cap * sizeof(*n));
2204 1 ▶ if (!n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return 0; 0 1 1
2205 1 r->struct_map = n;
2206 1 r->struct_map_cap = new_cap;
2207 }
2208 3 r->struct_map[r->struct_map_len].mcid = mcid;
2209 3 r->struct_map[r->struct_map_len].role_id = role_id;
2210 3 r->struct_map_len++;
2211 3 return 1;
2212 }
2213  
2214 /* Recursively walk a StructElem dict or its /K entry. role_id is the /S of
2215 * the nearest enclosing StructElem. page_dict is the target page (NULL to
2216 * accept all pages). */
2217 7 static void walk_struct_node(pdfmake_textract_result_t *r,
2218 pdfmake_obj_t *node,
2219 uint32_t role_id,
2220 pdfmake_obj_t *page_dict,
2221 int depth)
2222 {
2223 size_t i;
2224 size_t n;
2225 pdfmake_obj_t *item;
2226 uint32_t type_key;
2227 pdfmake_obj_t *type_v;
2228 const char *type_name;
2229 uint32_t mcid_key;
2230 pdfmake_obj_t *mcid_v;
2231 uint32_t pg_key;
2232 pdfmake_obj_t *pg;
2233 pdfmake_reader_t *rd;
2234 pdfmake_obj_t *resolved;
2235 uint32_t s_key;
2236 pdfmake_obj_t *s_v;
2237 uint32_t this_role;
2238 uint32_t k_key;
2239 pdfmake_obj_t *k;
2240 7 ▶ if (!node || depth > 32) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!node || depth > 32) return; 7 0 7
if (!node || depth > 32) return; 0 7 7
2241  
2242 /* Follow indirect references */
2243 7 ▶ if (node->kind == PDFMAKE_REF && r->reader) {
Branch: 2 branches
BranchTrueFalseTotal
if (node->kind == PDFMAKE_REF && r->reader) { 3 4 7
if (node->kind == PDFMAKE_REF && r->reader) { 3 0 3
2244 3 pdfmake_reader_t *rd = (pdfmake_reader_t *)r->reader;
2245 3 ▶ if (!rd->parser) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rd->parser) return; 0 3 3
2246 3 node = pdfmake_parser_resolve(rd->parser, node->as.ref);
2247 3 ▶ if (!node) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!node) return; 0 3 3
2248 }
2249  
2250 /* Integer leaf: this MCID belongs to role_id */
2251 7 ▶ if (node->kind == PDFMAKE_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->kind == PDFMAKE_INT) { 3 4 7
2252 3 struct_map_push(r, (int32_t)node->as.i, role_id);
2253 3 return;
2254 }
2255  
2256 /* Array: recurse into each kid, keeping the current role_id */
2257 4 ▶ if (node->kind == PDFMAKE_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->kind == PDFMAKE_ARRAY) { 1 3 4
2258 1 n = pdfmake_array_len(node);
2259 4 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 1 4
2260 3 item = pdfmake_array_get(node, i);
2261 3 ▶ if (item) walk_struct_node(r, item, role_id, page_dict, depth + 1);
Branch: 1 branch
BranchTrueFalseTotal
if (item) walk_struct_node(r, item, role_id, page_dict, depth + 1); 3 0 3
2262 }
2263 1 return;
2264 }
2265  
2266 3 ▶ if (node->kind != PDFMAKE_DICT) return;
Branch: 1 branch
BranchTrueFalseTotal
if (node->kind != PDFMAKE_DICT) return; 0 3 3
2267  
2268 /* MCR dict: { /Type /MCR, /Pg <page>, /MCID n } — ignore /Pg for now
2269 * since per-page extraction already limits us to one page's content. */
2270 3 type_key = pdfmake_arena_intern_name(r->arena, "Type", 4);
2271 3 type_v = pdfmake_dict_get(node, type_key);
2272 3 type_name = NULL;
2273 3 ▶ if (type_v && type_v->kind == PDFMAKE_NAME) {
Branch: 2 branches
BranchTrueFalseTotal
if (type_v && type_v->kind == PDFMAKE_NAME) { 3 0 3
if (type_v && type_v->kind == PDFMAKE_NAME) { 3 0 3
2274 3 type_name = pdfmake_get_name_bytes(r->arena, type_v);
2275 }
2276  
2277 3 mcid_key = pdfmake_arena_intern_name(r->arena, "MCID", 4);
2278 3 mcid_v = pdfmake_dict_get(node, mcid_key);
2279  
2280 3 ▶ if (type_name && strcmp(type_name, "MCR") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (type_name && strcmp(type_name, "MCR") == 0) { 3 0 3
if (type_name && strcmp(type_name, "MCR") == 0) { 0 3 3
2281 0 pg_key = pdfmake_arena_intern_name(r->arena, "Pg", 2);
2282 0 pg = pdfmake_dict_get(node, pg_key);
2283 0 ▶ if (pg && page_dict && pg->kind == PDFMAKE_REF && r->reader) {
Branch: 4 branches
BranchTrueFalseTotal
if (pg && page_dict && pg->kind == PDFMAKE_REF && r->reader) { 0 0 0
if (pg && page_dict && pg->kind == PDFMAKE_REF && r->reader) { 0 0 0
if (pg && page_dict && pg->kind == PDFMAKE_REF && r->reader) { 0 0 0
if (pg && page_dict && pg->kind == PDFMAKE_REF && r->reader) { 0 0 0
2284 /* Skip MCRs that target a different page */
2285 0 rd = (pdfmake_reader_t *)r->reader;
2286 0 resolved = rd->parser
2287 0 ▶ ? pdfmake_parser_resolve(rd->parser, pg->as.ref) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? pdfmake_parser_resolve(rd->parser, pg->as.ref) : NULL; 0 0 0
2288 0 ▶ if (resolved && resolved != page_dict) return;
Branch: 2 branches
BranchTrueFalseTotal
if (resolved && resolved != page_dict) return; 0 0 0
if (resolved && resolved != page_dict) return; 0 0 0
2289 }
2290 0 ▶ if (mcid_v && mcid_v->kind == PDFMAKE_INT) {
Branch: 2 branches
BranchTrueFalseTotal
if (mcid_v && mcid_v->kind == PDFMAKE_INT) { 0 0 0
if (mcid_v && mcid_v->kind == PDFMAKE_INT) { 0 0 0
2291 0 struct_map_push(r, (int32_t)mcid_v->as.i, role_id);
2292 }
2293 0 return;
2294 }
2295  
2296 /* OBJR (object reference to an annotation) — no MCID, skip */
2297 3 ▶ if (type_name && strcmp(type_name, "OBJR") == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (type_name && strcmp(type_name, "OBJR") == 0) return; 3 0 3
if (type_name && strcmp(type_name, "OBJR") == 0) return; 0 3 3
2298  
2299 /* StructElem: adopt its /S as the current role, then recurse into /K */
2300 3 s_key = pdfmake_arena_intern_name(r->arena, "S", 1);
2301 3 s_v = pdfmake_dict_get(node, s_key);
2302 3 this_role = role_id;
2303 3 ▶ if (s_v && s_v->kind == PDFMAKE_NAME) this_role = s_v->as.name.id;
Branch: 2 branches
BranchTrueFalseTotal
if (s_v && s_v->kind == PDFMAKE_NAME) this_role = s_v->as.name.id; 3 0 3
if (s_v && s_v->kind == PDFMAKE_NAME) this_role = s_v->as.name.id; 3 0 3
2304  
2305 /* /Pg filter: if this StructElem binds a specific page and it doesn't
2306 * match ours, we can still recurse — children may override /Pg. */
2307 3 k_key = pdfmake_arena_intern_name(r->arena, "K", 1);
2308 3 k = pdfmake_dict_get(node, k_key);
2309 3 ▶ if (!k) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!k) return; 0 3 3
2310  
2311 /* /K can be an integer (MCID), a dict (StructElem/MCR/OBJR), a ref, or
2312 * an array of any of the above. walk_struct_node handles all cases. */
2313 3 walk_struct_node(r, k, this_role, page_dict, depth + 1);
2314 }
2315  
2316 1 pdfmake_err_t pdfmake_textract_resolve_struct_tree(
2317 pdfmake_textract_result_t *result,
2318 pdfmake_obj_t *struct_root,
2319 pdfmake_obj_t *page_dict)
2320 {
2321 pdfmake_reader_t *rd;
2322 uint32_t k_key;
2323 pdfmake_obj_t *k;
2324 1 ▶ if (!result) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return PDFMAKE_EINVAL; 0 1 1
2325 1 ▶ if (!struct_root || !result->arena) return PDFMAKE_OK; /* no-op */
Branch: 2 branches
BranchTrueFalseTotal
if (!struct_root || !result->arena) return PDFMAKE_OK; /* no-op */ 1 0 1
if (!struct_root || !result->arena) return PDFMAKE_OK; /* no-op */ 0 1 1
2326  
2327 /* Follow an indirect reference to the StructTreeRoot dict */
2328 1 ▶ if (struct_root->kind == PDFMAKE_REF && result->reader) {
Branch: 2 branches
BranchTrueFalseTotal
if (struct_root->kind == PDFMAKE_REF && result->reader) { 1 0 1
if (struct_root->kind == PDFMAKE_REF && result->reader) { 1 0 1
2329 1 rd = (pdfmake_reader_t *)result->reader;
2330 1 ▶ if (rd->parser) {
Branch: 1 branch
BranchTrueFalseTotal
if (rd->parser) { 1 0 1
2331 1 struct_root = pdfmake_parser_resolve(rd->parser, struct_root->as.ref);
2332 1 ▶ if (!struct_root) return PDFMAKE_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (!struct_root) return PDFMAKE_OK; 0 1 1
2333 }
2334 }
2335 1 ▶ if (struct_root->kind != PDFMAKE_DICT) return PDFMAKE_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (struct_root->kind != PDFMAKE_DICT) return PDFMAKE_OK; 0 1 1
2336  
2337 /* Start at /K of the StructTreeRoot; role 0 means "none" (children will
2338 * overwrite as soon as they hit a StructElem with /S). */
2339 1 k_key = pdfmake_arena_intern_name(result->arena, "K", 1);
2340 1 k = pdfmake_dict_get(struct_root, k_key);
2341 1 ▶ if (k) walk_struct_node(result, k, 0, page_dict, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (k) walk_struct_node(result, k, 0, page_dict, 0); 1 0 1
2342  
2343 1 return PDFMAKE_OK;
2344 }
2345  
2346 5 uint32_t pdfmake_textract_role_for_mcid(
2347 const pdfmake_textract_result_t *result, int32_t mcid)
2348 {
2349 size_t i;
2350 5 ▶ if (!result || mcid < 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!result || mcid < 0) return 0; 5 0 5
if (!result || mcid < 0) return 0; 0 5 5
2351 11 ▶ for (i = 0; i < result->struct_map_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < result->struct_map_len; i++) { 11 0 11
2352 11 ▶ if (result->struct_map[i].mcid == mcid) {
Branch: 1 branch
BranchTrueFalseTotal
if (result->struct_map[i].mcid == mcid) { 5 6 11
2353 5 return result->struct_map[i].role_id;
2354 }
2355 }
2356 0 return 0;
2357 }
2358  
2359 /*============================================================================
2360 * Phase 13 — Annotation + form field text extraction
2361 *==========================================================================*/
2362  
2363 /* Decode a PDF "text string" (§7.9.2) to UTF-8, arena-allocated.
2364 * Handles UTF-16BE (with BOM FE FF) and UTF-8-with-BOM (EF BB BF); everything
2365 * else is treated as PDFDocEncoding, which matches ISO-8859-1 for the ASCII
2366 * subset used by every real-world annotation we've seen. */
2367 8 static const char *decode_pdf_text(pdfmake_arena_t *arena,
2368 const uint8_t *b, size_t n)
2369 {
2370 size_t i;
2371 pdfmake_buf_t out;
2372 char *s;
2373 uint32_t cp;
2374 uint32_t lo;
2375 uint8_t c;
2376 8 ▶ if (!arena || !b) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!arena || !b) return NULL; 8 0 8
if (!arena || !b) return NULL; 0 8 8
2377  
2378 /* UTF-16BE BOM */
2379 8 ▶ if (n >= 2 && b[0] == 0xFE && b[1] == 0xFF) {
Branch: 3 branches
BranchTrueFalseTotal
if (n >= 2 && b[0] == 0xFE && b[1] == 0xFF) { 8 0 8
if (n >= 2 && b[0] == 0xFE && b[1] == 0xFF) { 0 8 8
if (n >= 2 && b[0] == 0xFE && b[1] == 0xFF) { 0 0 0
2380 0 pdfmake_buf_init(&out);
2381 0 s = NULL;
2382 0 ▶ for (i = 2; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < n; i += 2) { 0 0 0
2383 0 cp = ((uint32_t)b[i] << 8) | b[i + 1];
2384 0 ▶ if (cp >= 0xD800 && cp <= 0xDBFF && i + 3 < n) {
Branch: 3 branches
BranchTrueFalseTotal
if (cp >= 0xD800 && cp <= 0xDBFF && i + 3 < n) { 0 0 0
if (cp >= 0xD800 && cp <= 0xDBFF && i + 3 < n) { 0 0 0
if (cp >= 0xD800 && cp <= 0xDBFF && i + 3 < n) { 0 0 0
2385 0 lo = ((uint32_t)b[i + 2] << 8) | b[i + 3];
2386 0 ▶ if (lo >= 0xDC00 && lo <= 0xDFFF) {
Branch: 2 branches
BranchTrueFalseTotal
if (lo >= 0xDC00 && lo <= 0xDFFF) { 0 0 0
if (lo >= 0xDC00 && lo <= 0xDFFF) { 0 0 0
2387 0 cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00);
2388 0 i += 2;
2389 }
2390 }
2391 0 ▶ if (cp < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x80) { 0 0 0
2392 0 pdfmake_buf_append_byte(&out, (uint8_t)cp);
2393 0 ▶ } else if (cp < 0x800) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cp < 0x800) { 0 0 0
2394 0 pdfmake_buf_append_byte(&out, 0xC0 | (cp >> 6));
2395 0 pdfmake_buf_append_byte(&out, 0x80 | (cp & 0x3F));
2396 0 ▶ } else if (cp < 0x10000) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cp < 0x10000) { 0 0 0
2397 0 pdfmake_buf_append_byte(&out, 0xE0 | (cp >> 12));
2398 0 pdfmake_buf_append_byte(&out, 0x80 | ((cp >> 6) & 0x3F));
2399 0 pdfmake_buf_append_byte(&out, 0x80 | (cp & 0x3F));
2400 } else {
2401 0 pdfmake_buf_append_byte(&out, 0xF0 | (cp >> 18));
2402 0 pdfmake_buf_append_byte(&out, 0x80 | ((cp >> 12) & 0x3F));
2403 0 pdfmake_buf_append_byte(&out, 0x80 | ((cp >> 6) & 0x3F));
2404 0 pdfmake_buf_append_byte(&out, 0x80 | (cp & 0x3F));
2405 }
2406 }
2407 0 s = pdfmake_arena_alloc(arena, out.len + 1);
2408 0 ▶ if (s) { memcpy(s, out.data, out.len); s[out.len] = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (s) { memcpy(s, out.data, out.len); s[out.len] = 0; } 0 0 0
2409 0 pdfmake_buf_free(&out);
2410 0 return s;
2411 }
2412  
2413 /* UTF-8 with BOM (PDF 2.0) */
2414 8 ▶ if (n >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) {
Branch: 4 branches
BranchTrueFalseTotal
if (n >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) { 8 0 8
if (n >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) { 0 8 8
if (n >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) { 0 0 0
if (n >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF) { 0 0 0
2415 0 b += 3; n -= 3;
2416 }
2417  
2418 /* PDFDocEncoding → promote high-bit bytes to 2-byte UTF-8. */
2419 8 pdfmake_buf_init(&out);
2420 106 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 98 8 106
2421 98 c = b[i];
2422 98 ▶ if (c < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { 98 0 98
2423 98 pdfmake_buf_append_byte(&out, c);
2424 } else {
2425 0 pdfmake_buf_append_byte(&out, 0xC0 | (c >> 6));
2426 0 pdfmake_buf_append_byte(&out, 0x80 | (c & 0x3F));
2427 }
2428 }
2429 8 s = pdfmake_arena_alloc(arena, out.len + 1);
2430 8 ▶ if (s) { memcpy(s, out.data, out.len); s[out.len] = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (s) { memcpy(s, out.data, out.len); s[out.len] = 0; } 8 0 8
2431 8 pdfmake_buf_free(&out);
2432 8 return s;
2433 }
2434  
2435 /* Pull a PDF text string out of a dict (resolving one level of indirection).
2436 * Returns NULL if the key is missing or not a string. */
2437 8 static const char *dict_get_text(pdfmake_reader_t *rd,
2438 pdfmake_arena_t *arena,
2439 pdfmake_obj_t *dict,
2440 const char *name)
2441 {
2442 uint32_t key;
2443 pdfmake_obj_t *v;
2444 8 ▶ if (!dict || dict->kind != PDFMAKE_DICT) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!dict || dict->kind != PDFMAKE_DICT) return NULL; 8 0 8
if (!dict || dict->kind != PDFMAKE_DICT) return NULL; 0 8 8
2445 8 key = pdfmake_arena_intern_name(arena, name, strlen(name));
2446 8 v = pdfmake_dict_get(dict, key);
2447 8 ▶ if (v && v->kind == PDFMAKE_REF && rd && rd->parser) {
Branch: 4 branches
BranchTrueFalseTotal
if (v && v->kind == PDFMAKE_REF && rd && rd->parser) { 7 1 8
if (v && v->kind == PDFMAKE_REF && rd && rd->parser) { 0 7 7
if (v && v->kind == PDFMAKE_REF && rd && rd->parser) { 0 0 0
if (v && v->kind == PDFMAKE_REF && rd && rd->parser) { 0 0 0
2448 0 v = pdfmake_parser_resolve(rd->parser, v->as.ref);
2449 }
2450 8 ▶ if (!v || v->kind != PDFMAKE_STR) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || v->kind != PDFMAKE_STR) return NULL; 7 1 8
if (!v || v->kind != PDFMAKE_STR) return NULL; 0 7 7
2451 7 return decode_pdf_text(arena, v->as.str.bytes, v->as.str.len);
2452 }
2453  
2454 /* Resolve a dict entry that's allowed to be an indirect reference. */
2455 1 static pdfmake_obj_t *dict_get_resolved(pdfmake_reader_t *rd,
2456 pdfmake_arena_t *arena,
2457 pdfmake_obj_t *dict,
2458 const char *name)
2459 {
2460 uint32_t key;
2461 pdfmake_obj_t *v;
2462 1 ▶ if (!dict || dict->kind != PDFMAKE_DICT) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!dict || dict->kind != PDFMAKE_DICT) return NULL; 1 0 1
if (!dict || dict->kind != PDFMAKE_DICT) return NULL; 0 1 1
2463 1 key = pdfmake_arena_intern_name(arena, name, strlen(name));
2464 1 v = pdfmake_dict_get(dict, key);
2465 1 ▶ if (v && v->kind == PDFMAKE_REF && rd && rd->parser) {
Branch: 4 branches
BranchTrueFalseTotal
if (v && v->kind == PDFMAKE_REF && rd && rd->parser) { 1 0 1
if (v && v->kind == PDFMAKE_REF && rd && rd->parser) { 1 0 1
if (v && v->kind == PDFMAKE_REF && rd && rd->parser) { 1 0 1
if (v && v->kind == PDFMAKE_REF && rd && rd->parser) { 1 0 1
2466 1 v = pdfmake_parser_resolve(rd->parser, v->as.ref);
2467 }
2468 1 return v;
2469 }
2470  
2471 2 pdfmake_annot_text_list_t *pdfmake_annot_text_list_new(pdfmake_arena_t *arena) {
2472 2 pdfmake_annot_text_list_t *l = calloc(1, sizeof(*l));
2473 2 ▶ if (l) l->arena = arena;
Branch: 1 branch
BranchTrueFalseTotal
if (l) l->arena = arena; 2 0 2
2474 2 return l;
2475 }
2476  
2477 2 void pdfmake_annot_text_list_free(pdfmake_annot_text_list_t *list) {
2478 2 ▶ if (!list) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!list) return; 0 2 2
2479 2 free(list->items);
2480 2 free(list);
2481 }
2482  
2483 3 static int annot_list_push(pdfmake_annot_text_list_t *l,
2484 const pdfmake_annot_text_t *rec)
2485 {
2486 3 ▶ if (l->len >= l->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->len >= l->cap) { 1 2 3
2487 1 ▶ size_t ncap = l->cap == 0 ? 8 : l->cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
size_t ncap = l->cap == 0 ? 8 : l->cap * 2; 0 1 1
2488 1 pdfmake_annot_text_t *n = realloc(l->items, ncap * sizeof(*n));
2489 1 ▶ if (!n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return 0; 0 1 1
2490 1 l->items = n; l->cap = ncap;
2491 }
2492 3 l->items[l->len++] = *rec;
2493 3 return 1;
2494 }
2495  
2496 /* Pull a 4-element numeric array (Rect / FreeText BBox) into out[4].
2497 * Zeros the array if the entry is missing or malformed. */
2498 3 static void dict_get_rect(pdfmake_obj_t *dict, uint32_t key, double out[4]) {
2499 int i;
2500 pdfmake_obj_t *v;
2501 pdfmake_obj_t *n;
2502 3 out[0] = out[1] = out[2] = out[3] = 0;
2503 3 v = pdfmake_dict_get(dict, key);
2504 3 ▶ if (!v || v->kind != PDFMAKE_ARRAY) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || v->kind != PDFMAKE_ARRAY) return; 3 0 3
if (!v || v->kind != PDFMAKE_ARRAY) return; 0 3 3
2505 3 ▶ if (pdfmake_array_len(v) < 4) return;
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_array_len(v) < 4) return; 0 3 3
2506 15 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 12 3 15
2507 12 n = pdfmake_array_get(v, i);
2508 12 ▶ if (n) out[i] = pdfmake_get_number(n);
Branch: 1 branch
BranchTrueFalseTotal
if (n) out[i] = pdfmake_get_number(n); 12 0 12
2509 }
2510 }
2511  
2512 /* Walk one page's /Annots array. */
2513 2 static void collect_page_annots(pdfmake_reader_t *rd,
2514 pdfmake_annot_text_list_t *out,
2515 pdfmake_reader_page_t *page,
2516 size_t page_index)
2517 {
2518 size_t i;
2519 pdfmake_arena_t *arena;
2520 uint32_t annots_key;
2521 pdfmake_obj_t *annots;
2522 uint32_t rect_key;
2523 size_t n;
2524 pdfmake_obj_t *a;
2525 uint32_t sub_key;
2526 pdfmake_obj_t *sub;
2527 const char *kind;
2528 const char *nm;
2529 const char *contents;
2530 const char *author;
2531 const char *subject;
2532 pdfmake_annot_text_t rec;
2533 3 ▶ if (!page || !page->page_dict) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!page || !page->page_dict) return; 2 0 2
if (!page || !page->page_dict) return; 0 2 2
2534 2 arena = rd->parser->doc->arena;
2535  
2536 2 annots_key = pdfmake_arena_intern_name(arena, "Annots", 6);
2537 2 annots = pdfmake_dict_get(page->page_dict, annots_key);
2538 2 ▶ if (annots && annots->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (annots && annots->kind == PDFMAKE_REF) { 1 1 2
if (annots && annots->kind == PDFMAKE_REF) { 0 1 1
2539 0 annots = pdfmake_parser_resolve(rd->parser, annots->as.ref);
2540 }
2541 2 ▶ if (!annots || annots->kind != PDFMAKE_ARRAY) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!annots || annots->kind != PDFMAKE_ARRAY) return; 1 1 2
if (!annots || annots->kind != PDFMAKE_ARRAY) return; 0 1 1
2542  
2543 1 rect_key = pdfmake_arena_intern_name(arena, "Rect", 4);
2544 1 n = pdfmake_array_len(annots);
2545 4 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 1 4
2546 3 a = pdfmake_array_get(annots, i);
2547 3 ▶ if (a && a->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (a && a->kind == PDFMAKE_REF) { 3 0 3
if (a && a->kind == PDFMAKE_REF) { 3 0 3
2548 3 a = pdfmake_parser_resolve(rd->parser, a->as.ref);
2549 }
2550 3 ▶ if (!a || a->kind != PDFMAKE_DICT) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!a || a->kind != PDFMAKE_DICT) continue; 3 0 3
if (!a || a->kind != PDFMAKE_DICT) continue; 0 3 3
2551  
2552 /* Subtype decides the kind label; we surface every textual annot. */
2553 3 sub_key = pdfmake_arena_intern_name(arena, "Subtype", 7);
2554 3 sub = pdfmake_dict_get(a, sub_key);
2555 3 kind = "Annot";
2556 3 ▶ if (sub && sub->kind == PDFMAKE_NAME) {
Branch: 2 branches
BranchTrueFalseTotal
if (sub && sub->kind == PDFMAKE_NAME) { 3 0 3
if (sub && sub->kind == PDFMAKE_NAME) { 3 0 3
2557 3 nm = pdfmake_get_name_bytes(arena, sub);
2558 3 ▶ if (nm) kind = nm;
Branch: 1 branch
BranchTrueFalseTotal
if (nm) kind = nm; 3 0 3
2559 }
2560  
2561 /* Skip Link/Widget/PrinterMark etc. that never carry user text.
2562 * Widgets are surfaced via the AcroForm walker instead. */
2563 3 ▶ if (strcmp(kind, "Link") == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(kind, "Link") == 0 || 3 0 3
2564 3 ▶ strcmp(kind, "Widget") == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
strcmp(kind, "Widget") == 0 || 2 1 3
2565 2 ▶ strcmp(kind, "PrinterMark") == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
strcmp(kind, "PrinterMark") == 0 || 2 0 2
2566 2 ▶ strcmp(kind, "TrapNet") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
strcmp(kind, "TrapNet") == 0) { 0 2 2
2567 1 continue;
2568 }
2569  
2570 2 contents = dict_get_text(rd, arena, a, "Contents");
2571 2 author = dict_get_text(rd, arena, a, "T");
2572 2 subject = dict_get_text(rd, arena, a, "Subj");
2573  
2574 /* Even with no /Contents we emit the record if there's author or
2575 * subject text — a Popup without Contents still tells you who
2576 * highlighted what. */
2577 2 ▶ if (!contents && !author && !subject) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!contents && !author && !subject) continue; 0 2 2
if (!contents && !author && !subject) continue; 0 0 0
if (!contents && !author && !subject) continue; 0 0 0
2578  
2579 2 memset(&rec, 0, sizeof(rec));
2580 2 rec.kind = pdfmake_arena_strdup(arena, kind);
2581 2 rec.page_index = page_index;
2582 2 dict_get_rect(a, rect_key, rec.rect);
2583 2 ▶ rec.text = contents ? contents : "";
Branch: 1 branch
BranchTrueFalseTotal
rec.text = contents ? contents : ""; 2 0 2
2584 2 rec.author = author;
2585 2 rec.subject = subject;
2586 2 annot_list_push(out, &rec);
2587 }
2588 }
2589  
2590 /* Recursively walk /AcroForm /Fields; /Kids trees inherit qualified names
2591 * via dot-joining (§12.7.3.2). */
2592 1 static void collect_form_fields(pdfmake_reader_t *rd,
2593 pdfmake_annot_text_list_t *out,
2594 pdfmake_obj_t *field,
2595 const char *parent_name,
2596 int depth)
2597 {
2598 size_t i;
2599 size_t pi;
2600 pdfmake_arena_t *arena;
2601 const char *t;
2602 const char *full_name;
2603 size_t pl;
2604 size_t tl;
2605 char *buf;
2606 uint32_t v_key;
2607 pdfmake_obj_t *v;
2608 const char *value;
2609 const char *nm;
2610 const char *tooltip;
2611 pdfmake_annot_text_t rec;
2612 uint32_t rect_key;
2613 pdfmake_obj_t *p;
2614 uint32_t kids_key;
2615 pdfmake_obj_t *kids;
2616 size_t n;
2617 pdfmake_obj_t *kid;
2618 1 ▶ if (!field || depth > 32) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!field || depth > 32) return; 1 0 1
if (!field || depth > 32) return; 0 1 1
2619 1 arena = rd->parser->doc->arena;
2620 1 ▶ if (field->kind == PDFMAKE_REF) {
Branch: 1 branch
BranchTrueFalseTotal
if (field->kind == PDFMAKE_REF) { 1 0 1
2621 1 field = pdfmake_parser_resolve(rd->parser, field->as.ref);
2622 }
2623 1 ▶ if (!field || field->kind != PDFMAKE_DICT) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!field || field->kind != PDFMAKE_DICT) return; 1 0 1
if (!field || field->kind != PDFMAKE_DICT) return; 0 1 1
2624  
2625 /* Build fully qualified name = parent.T joined with this.T */
2626 1 t = dict_get_text(rd, arena, field, "T");
2627 1 full_name = t;
2628 1 ▶ if (parent_name && t) {
Branch: 2 branches
BranchTrueFalseTotal
if (parent_name && t) { 0 1 1
if (parent_name && t) { 0 0 0
2629 0 pl = strlen(parent_name);
2630 0 tl = strlen(t);
2631 0 buf = pdfmake_arena_alloc(arena, pl + 1 + tl + 1);
2632 0 ▶ if (buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf) { 0 0 0
2633 0 memcpy(buf, parent_name, pl);
2634 0 buf[pl] = '.';
2635 0 memcpy(buf + pl + 1, t, tl);
2636 0 buf[pl + 1 + tl] = 0;
2637 0 full_name = buf;
2638 }
2639 1 ▶ } else if (parent_name && !t) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (parent_name && !t) { 0 1 1
} else if (parent_name && !t) { 0 0 0
2640 0 full_name = parent_name;
2641 }
2642  
2643 /* Emit a record for leaf fields that carry a /V (value). /V can be a
2644 * string (text, choice) or a name (button state). Skip anything else. */
2645 1 v_key = pdfmake_arena_intern_name(arena, "V", 1);
2646 1 v = pdfmake_dict_get(field, v_key);
2647 1 ▶ if (v && v->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && v->kind == PDFMAKE_REF) { 1 0 1
if (v && v->kind == PDFMAKE_REF) { 0 1 1
2648 0 v = pdfmake_parser_resolve(rd->parser, v->as.ref);
2649 }
2650 1 value = NULL;
2651 1 ▶ if (v) {
Branch: 1 branch
BranchTrueFalseTotal
if (v) { 1 0 1
2652 1 ▶ if (v->kind == PDFMAKE_STR) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->kind == PDFMAKE_STR) { 1 0 1
2653 1 value = decode_pdf_text(arena, v->as.str.bytes, v->as.str.len);
2654 0 ▶ } else if (v->kind == PDFMAKE_NAME) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (v->kind == PDFMAKE_NAME) { 0 0 0
2655 0 nm = pdfmake_get_name_bytes(arena, v);
2656 0 ▶ if (nm) value = pdfmake_arena_strdup(arena, nm);
Branch: 1 branch
BranchTrueFalseTotal
if (nm) value = pdfmake_arena_strdup(arena, nm); 0 0 0
2657 }
2658 }
2659  
2660 1 tooltip = dict_get_text(rd, arena, field, "TU");
2661  
2662 /* Fields may be pure intermediaries (have /Kids but no /V/T/TU). We
2663 * only emit when there's something to surface. */
2664 1 ▶ if (value || tooltip) {
Branch: 2 branches
BranchTrueFalseTotal
if (value || tooltip) { 0 1 1
if (value || tooltip) { 0 0 0
2665 1 memset(&rec, 0, sizeof(rec));
2666 1 rec.kind = "FormField";
2667 1 rec.page_index = (size_t)-1; /* not page-anchored by default */
2668 1 ▶ rec.text = value ? value : "";
Branch: 1 branch
BranchTrueFalseTotal
rec.text = value ? value : ""; 1 0 1
2669 1 rec.subject = tooltip;
2670 1 rec.field_name = full_name;
2671  
2672 /* If this field is a single-widget field (/Subtype /Widget) it
2673 * carries a /Rect; otherwise rect stays zero. */
2674 1 rect_key = pdfmake_arena_intern_name(arena, "Rect", 4);
2675 1 dict_get_rect(field, rect_key, rec.rect);
2676  
2677 /* /P may point at the owning page — use it to stamp page_index. */
2678 1 p = dict_get_resolved(rd, arena, field, "P");
2679 1 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 1 0 1
2680 1 ▶ for (pi = 0; pi < rd->page_count; pi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pi = 0; pi < rd->page_count; pi++) { 1 0 1
2681 1 ▶ if (rd->pages[pi].page_dict == p) {
Branch: 1 branch
BranchTrueFalseTotal
if (rd->pages[pi].page_dict == p) { 1 0 1
2682 1 rec.page_index = pi;
2683 1 break;
2684 }
2685 }
2686 }
2687  
2688 1 annot_list_push(out, &rec);
2689 }
2690  
2691 1 kids_key = pdfmake_arena_intern_name(arena, "Kids", 4);
2692 1 kids = pdfmake_dict_get(field, kids_key);
2693 1 ▶ if (kids && kids->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (kids && kids->kind == PDFMAKE_REF) { 0 1 1
if (kids && kids->kind == PDFMAKE_REF) { 0 0 0
2694 0 kids = pdfmake_parser_resolve(rd->parser, kids->as.ref);
2695 }
2696 1 ▶ if (kids && kids->kind == PDFMAKE_ARRAY) {
Branch: 2 branches
BranchTrueFalseTotal
if (kids && kids->kind == PDFMAKE_ARRAY) { 0 1 1
if (kids && kids->kind == PDFMAKE_ARRAY) { 0 0 0
2697 0 n = pdfmake_array_len(kids);
2698 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
2699 0 kid = pdfmake_array_get(kids, i);
2700 0 collect_form_fields(rd, out, kid, full_name, depth + 1);
2701 }
2702 }
2703 }
2704  
2705 2 pdfmake_err_t pdfmake_textract_annotations(
2706 pdfmake_reader_t *reader,
2707 pdfmake_annot_text_list_t *out)
2708 {
2709 size_t i;
2710 pdfmake_arena_t *arena;
2711 uint32_t af_key;
2712 pdfmake_obj_t *af;
2713 uint32_t fields_key;
2714 pdfmake_obj_t *fields;
2715 size_t n;
2716 2 ▶ if (!reader || !out) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!reader || !out) return PDFMAKE_EINVAL; 2 0 2
if (!reader || !out) return PDFMAKE_EINVAL; 0 2 2
2717 2 ▶ if (!reader->parser || !reader->parser->doc) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!reader->parser || !reader->parser->doc) return PDFMAKE_EINVAL; 2 0 2
if (!reader->parser || !reader->parser->doc) return PDFMAKE_EINVAL; 0 2 2
2718  
2719 2 arena = reader->parser->doc->arena;
2720  
2721 /* Per-page /Annots */
2722 4 ▶ for (i = 0; i < reader->page_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < reader->page_count; i++) { 2 2 4
2723 2 collect_page_annots(reader, out, &reader->pages[i], i);
2724 }
2725  
2726 /* Document-level /AcroForm /Fields */
2727 2 ▶ if (reader->catalog) {
Branch: 1 branch
BranchTrueFalseTotal
if (reader->catalog) { 2 0 2
2728 2 af_key = pdfmake_arena_intern_name(arena, "AcroForm", 8);
2729 2 af = pdfmake_dict_get(reader->catalog, af_key);
2730 2 ▶ if (af && af->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (af && af->kind == PDFMAKE_REF) { 1 1 2
if (af && af->kind == PDFMAKE_REF) { 1 0 1
2731 1 af = pdfmake_parser_resolve(reader->parser, af->as.ref);
2732 }
2733 2 ▶ if (af && af->kind == PDFMAKE_DICT) {
Branch: 2 branches
BranchTrueFalseTotal
if (af && af->kind == PDFMAKE_DICT) { 1 1 2
if (af && af->kind == PDFMAKE_DICT) { 1 0 1
2734 1 fields_key = pdfmake_arena_intern_name(arena, "Fields", 6);
2735 1 fields = pdfmake_dict_get(af, fields_key);
2736 1 ▶ if (fields && fields->kind == PDFMAKE_REF) {
Branch: 2 branches
BranchTrueFalseTotal
if (fields && fields->kind == PDFMAKE_REF) { 1 0 1
if (fields && fields->kind == PDFMAKE_REF) { 0 1 1
2737 0 fields = pdfmake_parser_resolve(reader->parser, fields->as.ref);
2738 }
2739 1 ▶ if (fields && fields->kind == PDFMAKE_ARRAY) {
Branch: 2 branches
BranchTrueFalseTotal
if (fields && fields->kind == PDFMAKE_ARRAY) { 1 0 1
if (fields && fields->kind == PDFMAKE_ARRAY) { 1 0 1
2740 1 n = pdfmake_array_len(fields);
2741 2 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 1 2
2742 1 collect_form_fields(reader, out,
2743 pdfmake_array_get(fields, i), NULL, 0);
2744 }
2745 }
2746 }
2747 }
2748  
2749 2 return PDFMAKE_OK;
2750 }
2751  
2752 /* Forward declaration — definition appears later under UTF-8 output. */
2753 static size_t encode_utf8(uint32_t cp, char *buf);
2754  
2755 /*============================================================================
2756 * Phase 15 — Table detection
2757 *
2758 * A page's words already come out sorted into rows via the aggregator, so
2759 * detection reduces to finding a maximal run of contiguous rows whose
2760 * column x-positions align within a small tolerance. Cells are then
2761 * populated by assigning each word to the column whose center it's closest
2762 * to, and joining the texts of any words that land in the same cell.
2763 *==========================================================================*/
2764  
2765 2 pdfmake_textract_table_opts_t pdfmake_textract_table_default_opts(void) {
2766 pdfmake_textract_table_opts_t o;
2767 2 o.min_rows = 3;
2768 2 o.min_cols = 2;
2769 2 o.x_tolerance = 5.0;
2770 2 o.row_tolerance = 0.5;
2771 2 return o;
2772 }
2773  
2774 2 pdfmake_textract_table_list_t *pdfmake_textract_table_list_new(pdfmake_arena_t *arena) {
2775 2 pdfmake_textract_table_list_t *l = calloc(1, sizeof(*l));
2776 2 ▶ if (l) l->arena = arena;
Branch: 1 branch
BranchTrueFalseTotal
if (l) l->arena = arena; 2 0 2
2777 2 return l;
2778 }
2779  
2780 2 void pdfmake_textract_table_list_free(pdfmake_textract_table_list_t *list) {
2781 size_t i;
2782 2 ▶ if (!list) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!list) return; 0 2 2
2783 3 ▶ for (i = 0; i < list->len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < list->len; i++) { 1 2 3
2784 1 free(list->items[i].cells);
2785 1 free(list->items[i].cell_x0);
2786 1 free(list->items[i].cell_y0);
2787 1 free(list->items[i].cell_x1);
2788 1 free(list->items[i].cell_y1);
2789 }
2790 2 free(list->items);
2791 2 free(list);
2792 }
2793  
2794 /* A logical table row built from raw glyphs. Each entry is one cell
2795 * (= a group of contiguous glyphs with no large x-gap between them). */
2796 typedef struct {
2797 double x0, x1; /* cell horizontal extent */
2798 double y0, y1; /* cell vertical extent */
2799 char *text; /* arena-owned UTF-8 */
2800 } tcell_t;
2801  
2802 typedef struct {
2803 tcell_t *cells;
2804 size_t ncells;
2805 size_t cap;
2806 double baseline;
2807 double font_size;
2808 } trow_t;
2809  
2810 17 static void trow_push(trow_t *r, const tcell_t *c) {
2811 17 ▶ if (r->ncells >= r->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->ncells >= r->cap) { 9 8 17
2812 9 ▶ size_t nc = r->cap == 0 ? 4 : r->cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
size_t nc = r->cap == 0 ? 4 : r->cap * 2; 0 9 9
2813 9 tcell_t *n = realloc(r->cells, nc * sizeof(*n));
2814 9 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 9 9
2815 9 r->cells = n; r->cap = nc;
2816 }
2817 17 r->cells[r->ncells++] = *c;
2818 }
2819  
2820 /* Sort raw glyphs by y desc, then x asc — independent of the column-split
2821 * based sort used by the main aggregator. */
2822 591 static int cmp_glyph_row_major(const void *a, const void *b) {
2823 591 const pdfmake_text_glyph_t *ga = a;
2824 591 const pdfmake_text_glyph_t *gb = b;
2825 double dy;
2826 double tol;
2827 double dx;
2828 591 ▶ if (ga->vertical != gb->vertical) return ga->vertical ? 1 : -1;
Branch: 2 branches
BranchTrueFalseTotal
if (ga->vertical != gb->vertical) return ga->vertical ? 1 : -1; 0 591 591
if (ga->vertical != gb->vertical) return ga->vertical ? 1 : -1; 0 0 0
2829 591 dy = gb->y0 - ga->y0;
2830 591 ▶ tol = (ga->font_size > 0 ? ga->font_size : 10.0) * 0.3;
Branch: 1 branch
BranchTrueFalseTotal
tol = (ga->font_size > 0 ? ga->font_size : 10.0) * 0.3; 591 0 591
2831 591 ▶ if (fabs(dy) > tol) return dy > 0 ? 1 : -1;
Branch: 2 branches
BranchTrueFalseTotal
if (fabs(dy) > tol) return dy > 0 ? 1 : -1; 202 389 591
if (fabs(dy) > tol) return dy > 0 ? 1 : -1; 51 151 202
2832 389 dx = ga->x0 - gb->x0;
2833 389 ▶ if (dx < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (dx < 0) return -1; 389 0 389
2834 0 ▶ if (dx > 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (dx > 0) return 1; 0 0 0
2835 0 return 0;
2836 }
2837  
2838 2 pdfmake_err_t pdfmake_textract_detect_tables(
2839 const pdfmake_textract_result_t *result,
2840 const pdfmake_textract_table_opts_t *options,
2841 pdfmake_textract_table_list_t *out)
2842 {
2843 size_t i;
2844 size_t j;
2845 size_t k;
2846 size_t r;
2847 size_t c;
2848 size_t m;
2849 pdfmake_textract_table_opts_t opts;
2850 pdfmake_text_glyph_t *glyphs;
2851 size_t row_cap;
2852 size_t nrows;
2853 trow_t *rows;
2854 size_t gi;
2855 double ry;
2856 double rfs;
2857 size_t rend;
2858 size_t nc;
2859 trow_t *tmp;
2860 trow_t *rrow;
2861 size_t cstart;
2862 int split;
2863 double gap;
2864 tcell_t cell;
2865 pdfmake_buf_t tb;
2866 char utf8_tmp[4];
2867 size_t n;
2868 size_t ncols;
2869 int ok;
2870 double dx;
2871 size_t run;
2872 pdfmake_textract_table_t t;
2873 const trow_t *rp;
2874 const tcell_t *cellp;
2875 size_t idx;
2876 pdfmake_textract_table_t *nt;
2877 2 ▶ if (!result || !out) return PDFMAKE_EINVAL;
Branch: 2 branches
BranchTrueFalseTotal
if (!result || !out) return PDFMAKE_EINVAL; 2 0 2
if (!result || !out) return PDFMAKE_EINVAL; 0 2 2
2878 2 ▶ opts = options ? *options : pdfmake_textract_table_default_opts();
Branch: 1 branch
BranchTrueFalseTotal
opts = options ? *options : pdfmake_textract_table_default_opts(); 0 2 2
2879 2 ▶ if (result->raw_len == 0) return PDFMAKE_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (result->raw_len == 0) return PDFMAKE_OK; 0 2 2
2880  
2881 /* Work on a private copy of the raw glyphs so we can re-sort without
2882 * touching the main extraction state (which was sorted by the Phase 10
2883 * column-aware comparator that would split rows across columns). */
2884 2 glyphs = malloc(result->raw_len * sizeof(*glyphs));
2885 2 ▶ if (!glyphs) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!glyphs) return PDFMAKE_ENOMEM; 0 2 2
2886 2 memcpy(glyphs, result->raw_glyphs, result->raw_len * sizeof(*glyphs));
2887 2 qsort(glyphs, result->raw_len, sizeof(*glyphs), cmp_glyph_row_major);
2888  
2889 /* 1) Cluster glyphs into rows by y. Within a row, group consecutive
2890 * glyphs into cells split on "big" horizontal gaps (≥ 1.5 em).
2891 * That keeps "Apple" together while splitting it from "12" sitting
2892 * at the next column origin. */
2893 2 row_cap = 64;
2894 2 nrows = 0;
2895 2 rows = calloc(row_cap, sizeof(trow_t));
2896 2 ▶ if (!rows) { free(glyphs); return PDFMAKE_ENOMEM; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rows) { free(glyphs); return PDFMAKE_ENOMEM; } 0 2 2
2897  
2898 2 gi = 0;
2899 11 ▶ while (gi < result->raw_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (gi < result->raw_len) { 9 2 11
2900 9 ry = glyphs[gi].baseline;
2901 9 ▶ rfs = glyphs[gi].font_size > 0 ? glyphs[gi].font_size : 10.0;
Branch: 1 branch
BranchTrueFalseTotal
rfs = glyphs[gi].font_size > 0 ? glyphs[gi].font_size : 10.0; 9 0 9
2902 9 ▶ if (glyphs[gi].vertical) { gi++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[gi].vertical) { gi++; continue; } 0 9 9
2903  
2904 /* Rows go until baseline jump > row_tolerance × fs. */
2905 9 rend = gi + 1;
2906 176 ▶ while (rend < result->raw_len && !glyphs[rend].vertical &&
Branch: 2 branches
BranchTrueFalseTotal
while (rend < result->raw_len && !glyphs[rend].vertical && 174 2 176
while (rend < result->raw_len && !glyphs[rend].vertical && 174 0 174
2907 174 ▶ fabs(glyphs[rend].baseline - ry) <= rfs * opts.row_tolerance) {
Branch: 1 branch
BranchTrueFalseTotal
fabs(glyphs[rend].baseline - ry) <= rfs * opts.row_tolerance) { 167 7 174
2908 167 rend++;
2909 }
2910  
2911 /* Grow rows */
2912 9 ▶ if (nrows >= row_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows >= row_cap) { 0 9 9
2913 0 nc = row_cap * 2;
2914 0 tmp = realloc(rows, nc * sizeof(trow_t));
2915 0 ▶ if (!tmp) { free(rows); free(glyphs); return PDFMAKE_ENOMEM; }
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp) { free(rows); free(glyphs); return PDFMAKE_ENOMEM; } 0 0 0
2916 0 rows = tmp;
2917 0 memset(rows + row_cap, 0, (nc - row_cap) * sizeof(trow_t));
2918 0 row_cap = nc;
2919 }
2920 9 rrow = &rows[nrows++];
2921 9 rrow->baseline = ry;
2922 9 rrow->font_size = rfs;
2923  
2924 /* Walk glyphs[gi..rend-1] and split into cells on large x gaps. */
2925 9 cstart = gi;
2926 185 ▶ for (k = gi + 1; k <= rend; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = gi + 1; k <= rend; k++) { 176 9 185
2927 176 split = (k == rend);
2928 176 ▶ if (!split) {
Branch: 1 branch
BranchTrueFalseTotal
if (!split) { 167 9 176
2929 167 gap = glyphs[k].x0 - glyphs[k - 1].x1;
2930 167 ▶ if (gap > 1.5 * rfs) split = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (gap > 1.5 * rfs) split = 1; 8 159 167
2931 }
2932 176 ▶ if (split) {
Branch: 1 branch
BranchTrueFalseTotal
if (split) { 17 159 176
2933 /* Emit a cell from cstart..k-1 */
2934 17 memset(&cell, 0, sizeof(cell));
2935 17 pdfmake_buf_init(&tb);
2936 17 cell.x0 = glyphs[cstart].x0;
2937 17 cell.x1 = glyphs[k - 1].x1;
2938 17 cell.y0 = glyphs[cstart].y0;
2939 17 cell.y1 = glyphs[cstart].y1;
2940 193 ▶ for (j = cstart; j < k; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = cstart; j < k; j++) { 176 17 193
2941 176 n = encode_utf8(glyphs[j].unicode, utf8_tmp);
2942 176 ▶ if (glyphs[j].x0 < cell.x0) cell.x0 = glyphs[j].x0;
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[j].x0 < cell.x0) cell.x0 = glyphs[j].x0; 0 176 176
2943 176 ▶ if (glyphs[j].x1 > cell.x1) cell.x1 = glyphs[j].x1;
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[j].x1 > cell.x1) cell.x1 = glyphs[j].x1; 0 176 176
2944 176 ▶ if (glyphs[j].y0 < cell.y0) cell.y0 = glyphs[j].y0;
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[j].y0 < cell.y0) cell.y0 = glyphs[j].y0; 0 176 176
2945 176 ▶ if (glyphs[j].y1 > cell.y1) cell.y1 = glyphs[j].y1;
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[j].y1 > cell.y1) cell.y1 = glyphs[j].y1; 0 176 176
2946 352 ▶ for (m = 0; m < n; m++)
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; m < n; m++) 176 176 352
2947 176 pdfmake_buf_append_byte(&tb, utf8_tmp[m]);
2948 }
2949 17 cell.text = pdfmake_arena_alloc(out->arena, tb.len + 1);
2950 17 ▶ if (cell.text) { memcpy(cell.text, tb.data, tb.len); cell.text[tb.len] = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (cell.text) { memcpy(cell.text, tb.data, tb.len); cell.text[tb.len] = 0; } 17 0 17
2951 17 pdfmake_buf_free(&tb);
2952 17 trow_push(rrow, &cell);
2953 17 cstart = k;
2954 }
2955 }
2956 9 gi = rend;
2957 }
2958  
2959 /* 2) Find maximal runs of consecutive rows with matching column layout.
2960 * "Same layout" = same cell count AND every cell-origin within
2961 * opts.x_tolerance. */
2962 2 i = 0;
2963 8 ▶ while (i < nrows) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < nrows) { 6 2 8
2964 6 ▶ if (rows[i].ncells < opts.min_cols) { i++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (rows[i].ncells < opts.min_cols) { i++; continue; } 5 1 6
2965 1 ncols = rows[i].ncells;
2966  
2967 1 j = i + 1;
2968 4 ▶ while (j < nrows) {
Branch: 1 branch
BranchTrueFalseTotal
while (j < nrows) { 4 0 4
2969 4 ▶ if (rows[j].ncells != ncols) break;
Branch: 1 branch
BranchTrueFalseTotal
if (rows[j].ncells != ncols) break; 1 3 4
2970 3 ok = 1;
2971 12 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 9 3 12
2972 9 dx = rows[i].cells[c].x0 - rows[j].cells[c].x0;
2973 9 ▶ if (dx < 0) dx = -dx;
Branch: 1 branch
BranchTrueFalseTotal
if (dx < 0) dx = -dx; 0 9 9
2974 9 ▶ if (dx > opts.x_tolerance) { ok = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (dx > opts.x_tolerance) { ok = 0; break; } 0 9 9
2975 }
2976 3 ▶ if (!ok) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) break; 0 3 3
2977 3 j++;
2978 }
2979  
2980 1 run = j - i;
2981 1 ▶ if (run < opts.min_rows) { i = j; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (run < opts.min_rows) { i = j; continue; } 0 1 1
2982  
2983 /* 3) Emit the table. */
2984 1 memset(&t, 0, sizeof(t));
2985 1 t.rows = run;
2986 1 t.cols = ncols;
2987 1 t.cells = calloc(run * ncols, sizeof(*t.cells));
2988 1 t.cell_x0 = calloc(run * ncols, sizeof(double));
2989 1 t.cell_y0 = calloc(run * ncols, sizeof(double));
2990 1 t.cell_x1 = calloc(run * ncols, sizeof(double));
2991 1 t.cell_y1 = calloc(run * ncols, sizeof(double));
2992 1 ▶ if (!t.cells || !t.cell_x0 || !t.cell_y0 || !t.cell_x1 || !t.cell_y1) {
Branch: 5 branches
BranchTrueFalseTotal
if (!t.cells || !t.cell_x0 || !t.cell_y0 || !t.cell_x1 || !t.cell_y1) { 1 0 1
if (!t.cells || !t.cell_x0 || !t.cell_y0 || !t.cell_x1 || !t.cell_y1) { 1 0 1
if (!t.cells || !t.cell_x0 || !t.cell_y0 || !t.cell_x1 || !t.cell_y1) { 1 0 1
if (!t.cells || !t.cell_x0 || !t.cell_y0 || !t.cell_x1 || !t.cell_y1) { 1 0 1
if (!t.cells || !t.cell_x0 || !t.cell_y0 || !t.cell_x1 || !t.cell_y1) { 0 1 1
2993 0 free(t.cells); free(t.cell_x0); free(t.cell_y0);
2994 0 free(t.cell_x1); free(t.cell_y1);
2995 0 ▶ for (k = 0; k < nrows; k++) free(rows[k].cells);
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nrows; k++) free(rows[k].cells); 0 0 0
2996 0 free(rows); free(glyphs);
2997 0 return PDFMAKE_ENOMEM;
2998 }
2999  
3000 1 t.x0 = t.y0 = 1e18; t.x1 = t.y1 = -1e18;
3001 5 ▶ for (r = 0; r < run; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < run; r++) { 4 1 5
3002 4 rp = &rows[i + r];
3003 16 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 12 4 16
3004 12 cellp = &rp->cells[c];
3005 12 idx = r * ncols + c;
3006 12 ▶ t.cells[idx] = cellp->text ? cellp->text : "";
Branch: 1 branch
BranchTrueFalseTotal
t.cells[idx] = cellp->text ? cellp->text : ""; 12 0 12
3007 12 t.cell_x0[idx] = cellp->x0;
3008 12 t.cell_y0[idx] = cellp->y0;
3009 12 t.cell_x1[idx] = cellp->x1;
3010 12 t.cell_y1[idx] = cellp->y1;
3011 12 ▶ if (cellp->x0 < t.x0) t.x0 = cellp->x0;
Branch: 1 branch
BranchTrueFalseTotal
if (cellp->x0 < t.x0) t.x0 = cellp->x0; 1 11 12
3012 12 ▶ if (cellp->y0 < t.y0) t.y0 = cellp->y0;
Branch: 1 branch
BranchTrueFalseTotal
if (cellp->y0 < t.y0) t.y0 = cellp->y0; 4 8 12
3013 12 ▶ if (cellp->x1 > t.x1) t.x1 = cellp->x1;
Branch: 1 branch
BranchTrueFalseTotal
if (cellp->x1 > t.x1) t.x1 = cellp->x1; 3 9 12
3014 12 ▶ if (cellp->y1 > t.y1) t.y1 = cellp->y1;
Branch: 1 branch
BranchTrueFalseTotal
if (cellp->y1 > t.y1) t.y1 = cellp->y1; 1 11 12
3015 }
3016 }
3017  
3018 1 ▶ if (out->len >= out->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (out->len >= out->cap) { 1 0 1
3019 1 ▶ nc = out->cap == 0 ? 4 : out->cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
nc = out->cap == 0 ? 4 : out->cap * 2; 0 1 1
3020 1 nt = realloc(out->items, nc * sizeof(*nt));
3021 1 ▶ if (!nt) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nt) { 0 1 1
3022 0 free(t.cells); free(t.cell_x0); free(t.cell_y0);
3023 0 free(t.cell_x1); free(t.cell_y1);
3024 0 ▶ for (k = 0; k < nrows; k++) free(rows[k].cells);
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nrows; k++) free(rows[k].cells); 0 0 0
3025 0 free(rows); free(glyphs);
3026 0 return PDFMAKE_ENOMEM;
3027 }
3028 1 out->items = nt; out->cap = nc;
3029 }
3030 1 out->items[out->len++] = t;
3031  
3032 1 i = j;
3033 }
3034  
3035 11 ▶ for (k = 0; k < nrows; k++) free(rows[k].cells);
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nrows; k++) free(rows[k].cells); 9 2 11
3036 2 free(rows);
3037 2 free(glyphs);
3038 2 return PDFMAKE_OK;
3039 }
3040  
3041 /*============================================================================
3042 * UTF-8 output
3043 *==========================================================================*/
3044  
3045 /* Encode a single Unicode codepoint to UTF-8, return bytes written */
3046 1171 static size_t encode_utf8(uint32_t cp, char *buf) {
3047 1171 ▶ if (cp < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x80) { 1164 7 1171
3048 1164 buf[0] = (char)cp;
3049 1164 return 1;
3050 7 ▶ } else if (cp < 0x800) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cp < 0x800) { 1 6 7
3051 1 buf[0] = (char)(0xC0 | (cp >> 6));
3052 1 buf[1] = (char)(0x80 | (cp & 0x3F));
3053 1 return 2;
3054 6 ▶ } else if (cp < 0x10000) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cp < 0x10000) { 6 0 6
3055 6 buf[0] = (char)(0xE0 | (cp >> 12));
3056 6 buf[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
3057 6 buf[2] = (char)(0x80 | (cp & 0x3F));
3058 6 return 3;
3059 0 ▶ } else if (cp < 0x110000) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cp < 0x110000) { 0 0 0
3060 0 buf[0] = (char)(0xF0 | (cp >> 18));
3061 0 buf[1] = (char)(0x80 | ((cp >> 12) & 0x3F));
3062 0 buf[2] = (char)(0x80 | ((cp >> 6) & 0x3F));
3063 0 buf[3] = (char)(0x80 | (cp & 0x3F));
3064 0 return 4;
3065 }
3066 0 return 0;
3067 }
3068  
3069 5 size_t pdfmake_textract_to_utf8(
3070 const pdfmake_textract_result_t *result,
3071 char *buf, size_t buf_cap)
3072 {
3073 size_t bi;
3074 size_t li;
3075 size_t wi;
3076 size_t gi;
3077 size_t k;
3078 size_t written;
3079 const pdfmake_text_block_t *block;
3080 const pdfmake_text_line_t *line;
3081 const pdfmake_text_word_t *word;
3082 size_t n;
3083 char tmp[4];
3084 5 ▶ if (!result) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return 0; 0 5 5
3085  
3086 5 written = 0;
3087  
3088 21 ▶ for (bi = 0; bi < result->len; bi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (bi = 0; bi < result->len; bi++) { 16 5 21
3089 16 block = &result->blocks[bi];
3090 39 ▶ for (li = 0; li < block->len; li++) {
Branch: 1 branch
BranchTrueFalseTotal
for (li = 0; li < block->len; li++) { 23 16 39
3091 23 line = &block->lines[li];
3092 220 ▶ for (wi = 0; wi < line->len; wi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (wi = 0; wi < line->len; wi++) { 197 23 220
3093 197 word = &line->words[wi];
3094 197 ▶ if (wi > 0 && written < buf_cap) {
Branch: 2 branches
BranchTrueFalseTotal
if (wi > 0 && written < buf_cap) { 174 23 197
if (wi > 0 && written < buf_cap) { 174 0 174
3095 174 buf[written++] = ' '; /* space between words */
3096 }
3097 1192 ▶ for (gi = 0; gi < word->len; gi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (gi = 0; gi < word->len; gi++) { 995 197 1192
3098 995 n = encode_utf8(word->glyphs[gi].unicode, tmp);
3099 2003 ▶ for (k = 0; k < n && written < buf_cap; k++) {
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < n && written < buf_cap; k++) { 1008 995 2003
for (k = 0; k < n && written < buf_cap; k++) { 1008 0 1008
3100 1008 buf[written++] = tmp[k];
3101 }
3102 }
3103 }
3104 /* Newline between lines */
3105 23 ▶ if (li + 1 < block->len && written < buf_cap) {
Branch: 2 branches
BranchTrueFalseTotal
if (li + 1 < block->len && written < buf_cap) { 7 16 23
if (li + 1 < block->len && written < buf_cap) { 7 0 7
3106 7 buf[written++] = '\n';
3107 }
3108 }
3109 /* Double newline between blocks */
3110 16 ▶ if (bi + 1 < result->len && written + 1 < buf_cap) {
Branch: 2 branches
BranchTrueFalseTotal
if (bi + 1 < result->len && written + 1 < buf_cap) { 11 5 16
if (bi + 1 < result->len && written + 1 < buf_cap) { 11 0 11
3111 11 buf[written++] = '\n';
3112 11 buf[written++] = '\n';
3113 }
3114 }
3115  
3116 /* Null-terminate if room */
3117 5 ▶ if (written < buf_cap) buf[written] = '\0';
Branch: 1 branch
BranchTrueFalseTotal
if (written < buf_cap) buf[written] = '\0'; 5 0 5
3118  
3119 5 return written;
3120 }
« src/pdfmake_page.c ↑ Summary src/pdfmake_cid_unicode.c »