src/pdfmake_textract.c

filter: Statementstmt 87.3% 1004 / 1150 Branchbran 61.4% 686 / 1116 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 74.5% 1690 / 2266 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/form.xs ↑ Summary src/pdfmake_writer.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 87 pdfmake_textract_options_t pdfmake_textract_default_options(void) {
75 pdfmake_textract_options_t opts;
76 87 opts.word_gap_factor = 0.3;
77 87 opts.line_tolerance = 0.5;
78 87 opts.block_leading = 1.5;
79 87 opts.include_invisible = 1;
80 87 return opts;
81 }
82  
83 87 pdfmake_textract_result_t *pdfmake_textract_new(pdfmake_arena_t *arena) {
84 87 pdfmake_textract_result_t *r = calloc(1, sizeof(*r));
85 87 ▶ if (r) {
Branch: 1 branch
BranchTrueFalseTotal
if (r) { 87 0 87
86 87 r->arena = arena;
87 87 r->include_invisible = 1; /* default: include OCR text */
88 87 r->current_mcid = -1; /* Phase 12: no active MCID */
89 }
90 87 return r;
91 }
92  
93 775 static void free_words(pdfmake_text_word_t *words, size_t len) {
94 size_t i;
95 3683 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 2908 775 3683
96 2908 free(words[i].glyphs);
97 }
98 775 free(words);
99 775 }
100  
101 316 static void free_lines(pdfmake_text_line_t *lines, size_t len) {
102 size_t i;
103 1091 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 775 316 1091
104 775 free_words(lines[i].words, lines[i].len);
105 }
106 316 free(lines);
107 316 }
108  
109 87 void pdfmake_textract_free(pdfmake_textract_result_t *result) {
110 size_t i;
111 87 ▶ if (!result) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return; 0 87 87
112 403 ▶ for (i = 0; i < result->len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < result->len; i++) { 316 87 403
113 316 free_lines(result->blocks[i].lines, result->blocks[i].len);
114 }
115 87 free(result->blocks);
116 87 free(result->raw_glyphs);
117 87 free(result->font_cache);
118 87 free(result->struct_map);
119 87 free(result);
120 }
121  
122 87 void pdfmake_textract_set_reader(pdfmake_textract_result_t *result,
123 struct pdfmake_reader *reader) {
124 87 ▶ if (!result) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return; 0 87 87
125 87 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 10691 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 19934 ▶ for (i = 0; i < r->font_cache_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < r->font_cache_len; i++) { 19773 161 19934
140 19773 ▶ if (r->font_cache[i].font_dict == font_dict) return &r->font_cache[i];
Branch: 1 branch
BranchTrueFalseTotal
if (r->font_cache[i].font_dict == font_dict) return &r->font_cache[i]; 10530 9243 19773
141 }
142 161 return NULL;
143 }
144  
145 161 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 161 ▶ if (r->font_cache_len >= r->font_cache_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->font_cache_len >= r->font_cache_cap) { 101 60 161
150 101 ▶ 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 87 101
151 101 pdfmake_resolved_font_t *n = realloc(
152 101 r->font_cache, new_cap * sizeof(*n));
153 101 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 101 101
154 101 r->font_cache = n;
155 101 r->font_cache_cap = new_cap;
156 }
157 161 slot = &r->font_cache[r->font_cache_len++];
158 161 memset(slot, 0, sizeof(*slot));
159 161 slot->font_dict = font_dict;
160 161 slot->std14_id = -1;
161 161 return slot;
162 }
163  
164 /* Resolve the ToUnicode CMap for a font dict. Returns NULL if not present
165 * or if resolution fails. Caches the result (including NULL) per-font. */
166 3563 static pdfmake_cmap_t *resolve_to_unicode(pdfmake_textract_result_t *r,
167 pdfmake_resolved_font_t *rf)
168 {
169 uint32_t key;
170 pdfmake_obj_t *tu;
171 pdfmake_buf_t buf;
172 pdfmake_err_t err;
173 3563 ▶ if (rf->to_unicode_tried) return rf->to_unicode;
Branch: 1 branch
BranchTrueFalseTotal
if (rf->to_unicode_tried) return rf->to_unicode; 3402 161 3563
174 161 rf->to_unicode_tried = 1;
175  
176 161 ▶ if (!r->reader || !rf->font_dict || !r->arena) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!r->reader || !rf->font_dict || !r->arena) return NULL; 161 0 161
if (!r->reader || !rf->font_dict || !r->arena) return NULL; 161 0 161
if (!r->reader || !rf->font_dict || !r->arena) return NULL; 0 161 161
177  
178 161 key = pdfmake_arena_intern_name(r->arena, "ToUnicode", 9);
179 161 tu = pdfmake_dict_get(rf->font_dict, key);
180 161 ▶ if (!tu || tu->kind != PDFMAKE_REF) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!tu || tu->kind != PDFMAKE_REF) return NULL; 20 141 161
if (!tu || tu->kind != PDFMAKE_REF) return NULL; 0 20 20
181  
182 20 pdfmake_buf_init(&buf);
183 20 err = pdfmake_reader_resolve_stream(
184 20 r->reader, tu->as.ref.num, tu->as.ref.gen, &buf);
185 20 ▶ if (err != PDFMAKE_OK || buf.len == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (err != PDFMAKE_OK || buf.len == 0) { 20 0 20
if (err != PDFMAKE_OK || buf.len == 0) { 0 20 20
186 0 pdfmake_buf_free(&buf);
187 0 return NULL;
188 }
189  
190 20 rf->to_unicode = pdfmake_cmap_parse(r->arena, buf.data, buf.len);
191 20 pdfmake_buf_free(&buf);
192 20 return rf->to_unicode;
193 }
194  
195 /* Resolve /Widths or /W for the font, plus font descriptor metrics. */
196 47 static void resolve_widths(pdfmake_textract_result_t *r,
197 pdfmake_resolved_font_t *rf)
198 {
199 47 ▶ if (rf->widths_resolved) return;
Branch: 1 branch
BranchTrueFalseTotal
if (rf->widths_resolved) return; 0 47 47
200 47 rf->widths_resolved = 1;
201  
202 47 ▶ if (!rf->font_dict || !r->arena) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rf->font_dict || !r->arena) { 47 0 47
if (!rf->font_dict || !r->arena) { 0 47 47
203 0 pdfmake_font_widths_init(&rf->widths);
204 0 return;
205 }
206  
207 47 ▶ if (rf->is_cid) {
Branch: 1 branch
BranchTrueFalseTotal
if (rf->is_cid) { 2 45 47
208 2 pdfmake_font_widths_from_cid(r->arena, rf->font_dict, &rf->widths);
209 } else {
210 45 pdfmake_font_widths_from_simple(r->arena, rf->font_dict, &rf->widths);
211 }
212  
213 /* Phase 6: overlay with TTF metrics from /FontFile2 if present.
214 * For simple fonts this requires the resolved /Encoding first; callers
215 * arrange the sequence in resolve_font(). */
216 47 ▶ if (r->reader) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->reader) { 47 0 47
217 47 const uint32_t *byte_to_uni = NULL;
218 47 ▶ if (!rf->is_cid && rf->encoding_resolved) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rf->is_cid && rf->encoding_resolved) { 45 2 47
if (!rf->is_cid && rf->encoding_resolved) { 45 0 45
219 45 byte_to_uni = rf->encoding.map;
220 }
221 47 pdfmake_font_widths_enhance_with_ttf(
222 r->arena,
223 (struct pdfmake_reader *)r->reader,
224 rf->font_dict,
225 rf->is_cid,
226 byte_to_uni,
227 &rf->widths);
228 }
229 }
230  
231 /* Resolve the /Encoding entry for a simple font. Populates rf->encoding.
232 * Std14 fonts without an explicit /Encoding default to WinAnsi in practice;
233 * other Type1 fonts default to StandardEncoding. */
234 159 static void resolve_encoding(pdfmake_textract_result_t *r,
235 pdfmake_resolved_font_t *rf)
236 {
237 uint32_t enc_key;
238 pdfmake_obj_t *enc_obj;
239 pdfmake_obj_t *resolved;
240 const pdfmake_std14_data_t *d;
241 159 ▶ if (rf->encoding_resolved) return;
Branch: 1 branch
BranchTrueFalseTotal
if (rf->encoding_resolved) return; 0 159 159
242 159 rf->encoding_resolved = 1;
243  
244 159 ▶ if (!rf->font_dict || !r->arena) {
Branch: 2 branches
BranchTrueFalseTotal
if (!rf->font_dict || !r->arena) { 159 0 159
if (!rf->font_dict || !r->arena) { 0 159 159
245 0 pdfmake_font_encoding_init_winansi(&rf->encoding);
246 0 return;
247 }
248  
249 159 enc_key = pdfmake_arena_intern_name(r->arena, "Encoding", 8);
250 159 enc_obj = pdfmake_dict_get(rf->font_dict, enc_key);
251  
252 /* Resolve indirect reference if needed */
253 159 ▶ if (enc_obj && enc_obj->kind == PDFMAKE_REF && r->reader) {
Branch: 3 branches
BranchTrueFalseTotal
if (enc_obj && enc_obj->kind == PDFMAKE_REF && r->reader) { 35 124 159
if (enc_obj && enc_obj->kind == PDFMAKE_REF && r->reader) { 14 21 35
if (enc_obj && enc_obj->kind == PDFMAKE_REF && r->reader) { 14 0 14
254 /* We need to resolve the ref through the parser; expose a tiny helper */
255 /* For now, the parser returns the concrete obj via the reader's parser */
256 14 resolved = pdfmake_parser_resolve(
257 14 ((pdfmake_reader_t *)r->reader)->parser, enc_obj->as.ref);
258 14 ▶ if (resolved) enc_obj = resolved;
Branch: 1 branch
BranchTrueFalseTotal
if (resolved) enc_obj = resolved; 14 0 14
259 }
260  
261 159 ▶ if (enc_obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (enc_obj) { 35 124 159
262 35 pdfmake_font_encoding_from_dict(r->arena, enc_obj, &rf->encoding);
263 } else {
264 /* No /Encoding: pick a sane default based on Std14 kind */
265 124 ▶ if (rf->std14_id >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rf->std14_id >= 0) { 79 45 124
266 /* Symbol and ZapfDingbats have their own encoding */
267 79 d = pdfmake_std14_get((pdfmake_std14_id_t)rf->std14_id);
268 79 ▶ if (d && d->name) {
Branch: 2 branches
BranchTrueFalseTotal
if (d && d->name) { 79 0 79
if (d && d->name) { 79 0 79
269 158 ▶ if (strcmp(d->name, "Symbol") == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(d->name, "Symbol") == 0) 0 79 79
270 0 pdfmake_font_encoding_init_symbol(&rf->encoding);
271 79 ▶ else if (strcmp(d->name, "ZapfDingbats") == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(d->name, "ZapfDingbats") == 0) 0 79 79
272 0 pdfmake_font_encoding_init_zapfdingbats(&rf->encoding);
273 else
274 79 pdfmake_font_encoding_init_standard(&rf->encoding);
275 } else {
276 0 pdfmake_font_encoding_init_standard(&rf->encoding);
277 }
278 } else {
279 /* Non-Std14 with no /Encoding: StandardEncoding per §9.6.5 */
280 45 pdfmake_font_encoding_init_standard(&rf->encoding);
281 }
282 }
283 }
284  
285 /* If `obj` is an indirect reference, resolve it through the reader's
286 * parser; otherwise return it unchanged. Returns NULL only when an
287 * attempted resolve failed — direct NULL/DICT/etc. inputs pass through. */
288 static PDFMAKE_INLINE pdfmake_obj_t *
289 10695 textract_follow_ref(pdfmake_textract_result_t *r, pdfmake_obj_t *obj)
290 {
291 10695 ▶ if (obj && obj->kind == PDFMAKE_REF && r->reader) {
Branch: 3 branches
BranchTrueFalseTotal
if (obj && obj->kind == PDFMAKE_REF && r->reader) { 10695 0 10695
if (obj && obj->kind == PDFMAKE_REF && r->reader) { 10692 3 10695
if (obj && obj->kind == PDFMAKE_REF && r->reader) { 10692 0 10692
292 10692 pdfmake_reader_t *rd = (pdfmake_reader_t *)r->reader;
293 10692 ▶ 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); 10692 0 10692
294 }
295 3 return obj;
296 }
297  
298 /* For Type0/CID fonts, derive writing mode and default vertical advance:
299 * - /Encoding: name ending in "-V" → vertical; a CMap stream with
300 * /WMode 1 overrides (§9.7.5.2).
301 * - When vertical, /DescendantFonts[0]/DW2 = [v_origin_y v_advance]
302 * replaces the -1000 spec default for default_v_advance. */
303 static void
304 2 resolve_cid_font_modes(pdfmake_textract_result_t *r,
305 pdfmake_obj_t *font_dict,
306 pdfmake_resolved_font_t *rf)
307 {
308 uint32_t enc_key;
309 pdfmake_obj_t *enc;
310 const char *nm;
311 size_t ln;
312 pdfmake_dict_t *sd;
313 pdfmake_obj_t sd_obj;
314 uint32_t wm_key;
315 pdfmake_obj_t *wm;
316 uint32_t df_key;
317 pdfmake_obj_t *df;
318 pdfmake_obj_t *desc;
319 uint32_t dw2_key;
320 pdfmake_obj_t *dw2;
321 pdfmake_obj_t *a;
322  
323 2 enc_key = pdfmake_arena_intern_name(r->arena, "Encoding", 8);
324 2 enc = textract_follow_ref(r, pdfmake_dict_get(font_dict, enc_key));
325  
326 2 ▶ if (enc && enc->kind == PDFMAKE_NAME) {
Branch: 2 branches
BranchTrueFalseTotal
if (enc && enc->kind == PDFMAKE_NAME) { 2 0 2
if (enc && enc->kind == PDFMAKE_NAME) { 2 0 2
327 2 nm = pdfmake_get_name_bytes(r->arena, enc);
328 2 ▶ ln = nm ? strlen(nm) : 0;
Branch: 1 branch
BranchTrueFalseTotal
ln = nm ? strlen(nm) : 0; 2 0 2
329 2 ▶ if (ln >= 2 && nm[ln - 2] == '-' &&
Branch: 2 branches
BranchTrueFalseTotal
if (ln >= 2 && nm[ln - 2] == '-' && 2 0 2
if (ln >= 2 && nm[ln - 2] == '-' && 2 0 2
330 2 ▶ (nm[ln - 1] == 'V' || nm[ln - 1] == 'v')) {
Branch: 2 branches
BranchTrueFalseTotal
(nm[ln - 1] == 'V' || nm[ln - 1] == 'v')) { 1 1 2
(nm[ln - 1] == 'V' || nm[ln - 1] == 'v')) { 0 1 1
331 1 rf->wmode = 1;
332 }
333 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
334 0 sd = pdfmake_stream_dict(enc);
335 0 ▶ if (sd) {
Branch: 1 branch
BranchTrueFalseTotal
if (sd) { 0 0 0
336 0 sd_obj.kind = PDFMAKE_DICT;
337 0 sd_obj.as.dict = sd;
338 0 wm_key = pdfmake_arena_intern_name(r->arena, "WMode", 5);
339 0 wm = pdfmake_dict_get(&sd_obj, wm_key);
340 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
341 }
342 }
343 2 ▶ if (!rf->wmode) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rf->wmode) return; 1 1 2
344  
345 1 df_key = pdfmake_arena_intern_name(r->arena, "DescendantFonts", 15);
346 1 df = textract_follow_ref(r, pdfmake_dict_get(font_dict, df_key));
347 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
348  
349 1 desc = textract_follow_ref(r, pdfmake_array_get(df, 0));
350 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
351  
352 1 dw2_key = pdfmake_arena_intern_name(r->arena, "DW2", 3);
353 1 dw2 = pdfmake_dict_get(desc, dw2_key);
354 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
355 1 a = pdfmake_array_get(dw2, 1);
356 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
357 }
358 }
359  
360 /* Resolve the font dict into a cached resolved_font. NULL if dict is NULL. */
361 10691 static pdfmake_resolved_font_t *resolve_font(
362 pdfmake_textract_result_t *r, pdfmake_obj_t *font_dict)
363 {
364 pdfmake_resolved_font_t *rf;
365 uint32_t bf_key;
366 pdfmake_obj_t *bf;
367 const char *name;
368 uint32_t sub_key;
369 pdfmake_obj_t *sub;
370 const char *st;
371 10691 ▶ if (!font_dict || !r->arena) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!font_dict || !r->arena) return NULL; 10691 0 10691
if (!font_dict || !r->arena) return NULL; 0 10691 10691
372  
373 /* Follow indirect references — resources often hold refs, not inline dicts */
374 10691 font_dict = textract_follow_ref(r, font_dict);
375 10691 ▶ if (!font_dict || font_dict->kind != PDFMAKE_DICT) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!font_dict || font_dict->kind != PDFMAKE_DICT) return NULL; 10691 0 10691
if (!font_dict || font_dict->kind != PDFMAKE_DICT) return NULL; 0 10691 10691
376  
377 10691 rf = font_cache_find(r, font_dict);
378 10691 ▶ if (rf) return rf;
Branch: 1 branch
BranchTrueFalseTotal
if (rf) return rf; 10530 161 10691
379 161 rf = font_cache_add(r, font_dict);
380 161 ▶ if (!rf) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!rf) return NULL; 0 161 161
381  
382 /* Std14 lookup */
383 161 bf_key = pdfmake_arena_intern_name(r->arena, "BaseFont", 8);
384 161 bf = pdfmake_dict_get(font_dict, bf_key);
385 161 ▶ if (bf && bf->kind == PDFMAKE_NAME) {
Branch: 2 branches
BranchTrueFalseTotal
if (bf && bf->kind == PDFMAKE_NAME) { 161 0 161
if (bf && bf->kind == PDFMAKE_NAME) { 161 0 161
386 161 name = pdfmake_get_name_bytes(r->arena, bf);
387 161 ▶ if (name) rf->std14_id = pdfmake_std14_lookup(name);
Branch: 1 branch
BranchTrueFalseTotal
if (name) rf->std14_id = pdfmake_std14_lookup(name); 161 0 161
388 }
389  
390 /* Type0/CID detection */
391 161 sub_key = pdfmake_arena_intern_name(r->arena, "Subtype", 7);
392 161 sub = pdfmake_dict_get(font_dict, sub_key);
393 161 ▶ if (sub && sub->kind == PDFMAKE_NAME) {
Branch: 2 branches
BranchTrueFalseTotal
if (sub && sub->kind == PDFMAKE_NAME) { 161 0 161
if (sub && sub->kind == PDFMAKE_NAME) { 161 0 161
394 161 st = pdfmake_get_name_bytes(r->arena, sub);
395 161 ▶ if (st && strcmp(st, "Type0") == 0) rf->is_cid = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (st && strcmp(st, "Type0") == 0) rf->is_cid = 1; 161 0 161
if (st && strcmp(st, "Type0") == 0) rf->is_cid = 1; 2 159 161
396 }
397  
398 /* Phase 14: writing mode + default vertical advance (Type0 only). */
399 161 rf->wmode = 0;
400 161 rf->default_v_advance = -1000; /* spec default for vertical */
401 161 ▶ 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); 2 159 161
402  
403 /* Resolve /Encoding for simple fonts only; CID fonts use ToUnicode */
404 161 ▶ if (!rf->is_cid) resolve_encoding(r, rf);
Branch: 1 branch
BranchTrueFalseTotal
if (!rf->is_cid) resolve_encoding(r, rf); 159 2 161
405  
406 /* Resolve widths + descriptor metrics for all non-Std14 fonts */
407 161 ▶ if (rf->std14_id < 0) resolve_widths(r, rf);
Branch: 1 branch
BranchTrueFalseTotal
if (rf->std14_id < 0) resolve_widths(r, rf); 47 114 161
408  
409 161 return rf;
410 }
411  
412 /*============================================================================
413 * Raw glyph collection
414 *==========================================================================*/
415  
416 19009 static int result_push_glyph(pdfmake_textract_result_t *r,
417 const pdfmake_text_glyph_t *g) {
418 19009 ▶ if (r->raw_len >= r->raw_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->raw_len >= r->raw_cap) { 177 18832 19009
419 177 ▶ 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; 90 87 177
420 177 pdfmake_text_glyph_t *new_arr = realloc(r->raw_glyphs,
421 new_cap * sizeof(pdfmake_text_glyph_t));
422 177 ▶ if (!new_arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!new_arr) return 0; 0 177 177
423 177 r->raw_glyphs = new_arr;
424 177 r->raw_cap = new_cap;
425 }
426 19009 r->raw_glyphs[r->raw_len++] = *g;
427 19009 return 1;
428 }
429  
430 /*============================================================================
431 * Visitor: on_text_show callback
432 *
433 * Called for each Tj/TJ/' /" operator. Decodes bytes, computes glyph
434 * positions in user space, emits raw glyphs.
435 *==========================================================================*/
436  
437 /* Legacy: resolve the Standard 14 font ID from the graphics state font dict.
438 * Superseded by resolve_font() + font cache. Kept for potential reuse. */
439 __attribute__((unused))
440 0 static int resolve_std14(const pdfmake_gstate_t *gs, pdfmake_arena_t *arena) {
441 uint32_t basefont_key;
442 pdfmake_obj_t *basefont;
443 const char *name;
444 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
445 0 ▶ if (gs->font->kind != PDFMAKE_DICT) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (gs->font->kind != PDFMAKE_DICT) return -1; 0 0 0
446  
447 /* Look up /BaseFont in the font dictionary */
448 0 basefont_key = pdfmake_arena_intern_name(arena, "BaseFont", 8);
449 0 basefont = pdfmake_dict_get(gs->font, basefont_key);
450 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
451  
452 0 name = pdfmake_get_name_bytes(arena, basefont);
453 0 ▶ if (!name) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) return -1; 0 0 0
454  
455 0 return pdfmake_std14_lookup(name);
456 }
457  
458 /* Horizontal + vertical advance for a glyph, plus a reliability flag that
459 * feeds the downstream word-boundary heuristic. */
460 typedef struct {
461 double horizontal; /* x-advance in text space (Tj/TJ direction) */
462 double vertical; /* y-advance magnitude for vertical writing */
463 int reliable; /* 1 iff horizontal came from real font metrics */
464 } textract_advance_t;
465  
466 /* Map one character code to its Unicode codepoint(s). Returns the count
467 * (always ≥ 1). Ligature-style ToUnicode mappings may yield several
468 * codepoints (e.g. "fi" → U+0066 U+0069); uni_list must hold at least
469 * PDFMAKE_CMAP_MAX_UNI entries. On failure we emit U+FFFD. */
470 static PDFMAKE_INLINE size_t
471 19007 textract_code_to_unicode(const pdfmake_resolved_font_t *rf,
472 pdfmake_cmap_t *to_uni,
473 int is_cid, uint32_t code,
474 uint32_t *uni_list)
475 {
476 19007 ▶ if (to_uni) {
Branch: 1 branch
BranchTrueFalseTotal
if (to_uni) { 107 18900 19007
477 107 size_t cnt = 0;
478 107 ▶ 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) { 107 0 107
if (pdfmake_cmap_lookup(to_uni, code, uni_list, &cnt) && cnt > 0) { 107 0 107
479 107 return cnt;
480 }
481 0 uni_list[0] = 0xFFFD;
482 0 return 1;
483 }
484 18900 ▶ if (!is_cid && rf && rf->encoding_resolved) {
Branch: 3 branches
BranchTrueFalseTotal
if (!is_cid && rf && rf->encoding_resolved) { 18900 0 18900
if (!is_cid && rf && rf->encoding_resolved) { 18900 0 18900
if (!is_cid && rf && rf->encoding_resolved) { 18900 0 18900
485 18900 uint32_t cp = rf->encoding.map[code & 0xFF];
486 18900 ▶ uni_list[0] = cp ? cp : 0xFFFD;
Branch: 1 branch
BranchTrueFalseTotal
uni_list[0] = cp ? cp : 0xFFFD; 18900 0 18900
487 18900 return 1;
488 }
489 0 ▶ if (!is_cid) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_cid) { 0 0 0
490 0 uni_list[0] = pdfmake_winansi_to_unicode((uint8_t)code);
491 0 return 1;
492 }
493 /* CID fallback: use the raw code. */
494 0 uni_list[0] = code;
495 0 return 1;
496 }
497  
498 /* Compute the glyph advance (both horizontal and vertical) in text-space
499 * units, trying /Widths first, then Std14 AFM, then a 0.5-em / 0.25-em
500 * placeholder. Vertical advance prefers the font's default_v_advance
501 * (CIDFont DW2) when available. */
502 static PDFMAKE_INLINE textract_advance_t
503 19007 textract_glyph_advance(const pdfmake_resolved_font_t *rf,
504 int std14_id, int is_cid,
505 uint32_t code, uint32_t unicode,
506 double font_size)
507 {
508 19007 textract_advance_t adv = { 0.0, 0.0, 0 };
509  
510 19007 int16_t w1000 = 0;
511 19007 ▶ if (rf && rf->widths_resolved) {
Branch: 2 branches
BranchTrueFalseTotal
if (rf && rf->widths_resolved) { 19007 0 19007
if (rf && rf->widths_resolved) { 6353 12654 19007
512 6353 w1000 = pdfmake_font_widths_lookup(&rf->widths, code);
513 }
514 19007 ▶ if (w1000 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (w1000 > 0) { 6353 12654 19007
515 6353 adv.horizontal = (double)w1000 / 1000.0 * font_size;
516 6353 adv.reliable = 1;
517 25308 ▶ } else if (std14_id >= 0 && !is_cid) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (std14_id >= 0 && !is_cid) { 12654 0 12654
} else if (std14_id >= 0 && !is_cid) { 12654 0 12654
518 12654 int w = pdfmake_std14_width((pdfmake_std14_id_t)std14_id, unicode);
519 12654 ▶ if (w > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (w > 0) { 12654 0 12654
520 12654 adv.horizontal = (double)w / 1000.0 * font_size;
521 12654 adv.reliable = 1;
522 } else {
523 0 adv.horizontal = 0.25 * font_size;
524 }
525 } else {
526 0 adv.horizontal = 0.5 * font_size;
527 }
528  
529 /* Default vertical advance falls back to horizontal magnitude. CJK
530 * fonts with DW2/W2 override this via rf->default_v_advance (negative
531 * because vertical flow is downward in text space). */
532 19007 adv.vertical = adv.horizontal;
533 19007 ▶ if (rf) {
Branch: 1 branch
BranchTrueFalseTotal
if (rf) { 19007 0 19007
534 19007 ▶ double mag = (double)(rf->default_v_advance < 0
Branch: 1 branch
BranchTrueFalseTotal
double mag = (double)(rf->default_v_advance < 0 19007 0 19007
535 19007 ? -rf->default_v_advance : rf->default_v_advance);
536 19007 ▶ if (mag > 0) adv.vertical = mag / 1000.0 * font_size;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > 0) adv.vertical = mag / 1000.0 * font_size; 19007 0 19007
537 }
538 19007 return adv;
539 }
540  
541 /* Transform a text-space point through the text matrix and CTM to final
542 * user/device coordinates. */
543 static PDFMAKE_INLINE void
544 19007 textract_text_to_device(const double *tm, const double *ctm,
545 double gx, double gy, double *fx, double *fy)
546 {
547 19007 double ux = tm[0] * gx + tm[2] * gy + tm[4];
548 19007 double uy = tm[1] * gx + tm[3] * gy + tm[5];
549 19007 *fx = ctm[0] * ux + ctm[2] * uy + ctm[4];
550 19007 *fy = ctm[1] * ux + ctm[3] * uy + ctm[5];
551 19007 }
552  
553 3564 static void textract_on_text_show(void *ctx,
554 const pdfmake_gstate_t *gs,
555 const uint8_t *bytes,
556 size_t len)
557 {
558 3564 pdfmake_textract_result_t *result = (pdfmake_textract_result_t *)ctx;
559 double font_size;
560 double char_space;
561 double word_space;
562 double h_scale;
563 double rise;
564 pdfmake_resolved_font_t *rf;
565 int std14_id;
566 int is_cid;
567 int is_vertical;
568 pdfmake_cmap_t *to_uni;
569 double ascent_ratio;
570 double descent_ratio;
571 const pdfmake_std14_data_t *data;
572 double sx;
573 double sy;
574 double effective_size;
575 double ascent;
576 double descent;
577 double accum_x;
578 size_t pos;
579 uint32_t code;
580 size_t code_width;
581 uint32_t uni_list[PDFMAKE_CMAP_MAX_UNI];
582 size_t uni_list_n;
583 uint32_t unicode;
584 textract_advance_t glyph_adv;
585 double gx;
586 double gy;
587 double fx;
588 double fy;
589 double adv;
590 double sub_adv;
591 pdfmake_text_glyph_t g;
592 double ws;
593 size_t k;
594 3565 ▶ if (!gs || !bytes || len == 0) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!gs || !bytes || len == 0) return; 3564 0 3564
if (!gs || !bytes || len == 0) return; 3564 0 3564
if (!gs || !bytes || len == 0) return; 0 3564 3564
595  
596 /* Phase 11: skip invisible text (Tr=3) when the caller opted out.
597 * The interpreter still advances text_matrix via get_string_advance,
598 * so positioning of subsequent visible glyphs stays correct. */
599 3564 ▶ if (!result->include_invisible &&
Branch: 1 branch
BranchTrueFalseTotal
if (!result->include_invisible && 3 3561 3564
600 3 ▶ gs->render_mode == PDFMAKE_RENDER_INVISIBLE) {
Branch: 1 branch
BranchTrueFalseTotal
gs->render_mode == PDFMAKE_RENDER_INVISIBLE) { 1 2 3
601 1 return;
602 }
603  
604 3563 font_size = gs->font_size;
605 3563 char_space = gs->char_space;
606 3563 word_space = gs->word_space;
607 3563 h_scale = gs->h_scale / 100.0; /* Tz is percentage */
608 3563 rise = gs->rise;
609  
610 /* Resolve the font (cached per-font dict) */
611 3563 rf = resolve_font(result, gs->font);
612 3563 ▶ std14_id = rf ? rf->std14_id : -1;
Branch: 1 branch
BranchTrueFalseTotal
std14_id = rf ? rf->std14_id : -1; 3563 0 3563
613 3563 ▶ is_cid = rf ? rf->is_cid : 0;
Branch: 1 branch
BranchTrueFalseTotal
is_cid = rf ? rf->is_cid : 0; 3563 0 3563
614 3563 ▶ is_vertical = rf ? rf->wmode : 0; /* Phase 14 */
Branch: 1 branch
BranchTrueFalseTotal
is_vertical = rf ? rf->wmode : 0; /* Phase 14 */ 3563 0 3563
615 3563 ▶ to_uni = rf ? resolve_to_unicode(result, rf) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
to_uni = rf ? resolve_to_unicode(result, rf) : NULL; 3563 0 3563
616  
617 /* Font metrics for ascent/descent.
618 * Priority: Std14 AFM data → /FontDescriptor → 0.8/-0.2 fallback. */
619 3563 ascent_ratio = 0.8;
620 3563 descent_ratio = -0.2;
621 3563 ▶ if (std14_id >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (std14_id >= 0) { 1014 2549 3563
622 1014 data = pdfmake_std14_get((pdfmake_std14_id_t)std14_id);
623 1014 ▶ if (data) {
Branch: 1 branch
BranchTrueFalseTotal
if (data) { 1014 0 1014
624 1014 ascent_ratio = data->metrics.ascent / 1000.0;
625 1014 descent_ratio = data->metrics.descent / 1000.0;
626 }
627 2549 ▶ } else if (rf && rf->widths_resolved) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (rf && rf->widths_resolved) { 2549 0 2549
} else if (rf && rf->widths_resolved) { 2549 0 2549
628 2549 ▶ 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; 0 2549 2549
629 2549 ▶ 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; 0 2549 2549
630 }
631 /* Effective font sizes in user space (account for text matrix scaling).
632 * sx scales horizontally (advances); sy scales vertically (ascent/descent). */
633 3563 sx = sqrt(gs->text_matrix[0] * gs->text_matrix[0] +
634 3563 gs->text_matrix[1] * gs->text_matrix[1]);
635 3563 sy = sqrt(gs->text_matrix[2] * gs->text_matrix[2] +
636 3563 gs->text_matrix[3] * gs->text_matrix[3]);
637 3563 ▶ if (sy == 0) sy = sx;
Branch: 1 branch
BranchTrueFalseTotal
if (sy == 0) sy = sx; 0 3563 3563
638 3563 effective_size = font_size * sx;
639 3563 ascent = ascent_ratio * font_size * sy;
640 3563 descent = descent_ratio * font_size * sy;
641  
642 /* Accumulated displacement through the string. For horizontal text
643 * (WMode 0) this is the x-offset in text space; for vertical text
644 * (WMode 1) it's the y-offset (negative because glyphs flow downward). */
645 3563 accum_x = 0.0; /* reused for both axes, name kept for symmetry */
646  
647 3563 pos = 0;
648 22570 ▶ while (pos < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < len) { 19007 3563 22570
649 /* Extract the character code (1 or 2 bytes depending on font type).
650 * CID fonts are 2-byte by default; simple fonts are 1-byte. */
651 19007 ▶ if (is_cid && pos + 1 < len) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_cid && pos + 1 < len) { 7 19000 19007
if (is_cid && pos + 1 < len) { 7 0 7
652 7 code = ((uint32_t)bytes[pos] << 8) | bytes[pos + 1];
653 7 code_width = 2;
654 } else {
655 19000 code = bytes[pos];
656 19000 code_width = 1;
657 }
658  
659 /* Phase 9: ligature-style mappings may yield multiple codepoints
660 * for a single glyph; emit one sub-glyph per codepoint, sharing
661 * the glyph's bounding box (advance split uniformly). */
662 19007 uni_list_n = textract_code_to_unicode(rf, to_uni, is_cid, code, uni_list);
663 19007 unicode = uni_list[0];
664  
665 19007 glyph_adv = textract_glyph_advance(
666 rf, std14_id, is_cid, code, unicode, font_size);
667  
668 19007 pos += code_width;
669  
670 /* Glyph origin in text space — for horizontal writing the pen
671 * travels along +x; for vertical writing it travels along -y.
672 * `rise` still adjusts the cross axis. */
673 19007 ▶ gx = is_vertical ? rise : accum_x;
Branch: 1 branch
BranchTrueFalseTotal
gx = is_vertical ? rise : accum_x; 3 19004 19007
674 19007 ▶ gy = is_vertical ? accum_x : rise;
Branch: 1 branch
BranchTrueFalseTotal
gy = is_vertical ? accum_x : rise; 3 19004 19007
675  
676 19007 textract_text_to_device(gs->text_matrix, gs->ctm, gx, gy, &fx, &fy);
677  
678 /* Bounding box — for horizontal, extent runs along x; for vertical
679 * it runs along -y (each glyph sits below the previous one). */
680 19007 ▶ adv = (is_vertical ? glyph_adv.vertical : glyph_adv.horizontal) * sx * h_scale;
Branch: 1 branch
BranchTrueFalseTotal
adv = (is_vertical ? glyph_adv.vertical : glyph_adv.horizontal) * sx * h_scale; 3 19004 19007
681  
682 /* Phase 9: emit one glyph per decoded codepoint. */
683 19007 ▶ 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; 19007 0 19007
684 38016 ▶ for (k = 0; k < uni_list_n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < uni_list_n; k++) { 19009 19007 38016
685 19009 g.unicode = uni_list[k];
686 19009 ▶ if (is_vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_vertical) { 3 19006 19009
687 /* Vertical: each sub-glyph occupies a horizontal strip whose
688 * height is sub_adv and width is effective_size (1 em). */
689 3 double top = fy - sub_adv * (double)k;
690 3 double bottom = fy - sub_adv * (double)(k + 1);
691 3 g.x0 = fx - effective_size * 0.5;
692 3 g.x1 = fx + effective_size * 0.5;
693 3 g.y0 = bottom;
694 3 g.y1 = top;
695 } else {
696 19006 g.x0 = fx + sub_adv * (double)k;
697 19006 g.y0 = fy + descent;
698 19006 g.x1 = fx + sub_adv * (double)(k + 1);
699 19006 g.y1 = fy + ascent;
700 }
701 19009 g.advance = sub_adv;
702 19009 g.font_size = effective_size;
703 19009 g.reliable_advance = (uint8_t)glyph_adv.reliable;
704 19009 g.mcid = result->current_mcid; /* Phase 12 */
705 19009 g.vertical = (uint8_t)is_vertical; /* Phase 14 */
706  
707 19009 ▶ if (!result_push_glyph(result, &g)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!result_push_glyph(result, &g)) break; 0 19009 19009
708 }
709  
710 /* Advance text position: §9.4.4. Horizontal moves along +x by the
711 * glyph's horizontal advance; vertical moves along -y by the
712 * vertical advance. Word-spacing only triggers on a space. */
713 19007 ▶ ws = (unicode == 0x20) ? word_space : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
ws = (unicode == 0x20) ? word_space : 0.0; 1979 17028 19007
714 19007 ▶ if (is_vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_vertical) { 3 19004 19007
715 3 accum_x -= (glyph_adv.vertical + char_space + ws) * h_scale;
716 } else {
717 19004 accum_x += (glyph_adv.horizontal + char_space + ws) * h_scale;
718 }
719 }
720 }
721  
722 /* Phase 8: compute the real text-space advance for a string so the
723 * interpreter can keep text_matrix in sync with our glyph positions.
724 * Mirrors the advance logic from textract_on_text_show. */
725 3564 static double textract_get_string_advance(void *ctx,
726 const pdfmake_gstate_t *gs,
727 const uint8_t *bytes, size_t len)
728 {
729 3564 pdfmake_textract_result_t *result = (pdfmake_textract_result_t *)ctx;
730 pdfmake_resolved_font_t *rf;
731 int std14_id;
732 int is_cid;
733 int is_vertical;
734 double font_size;
735 double char_space;
736 double word_space;
737 double accum;
738 size_t pos;
739 uint32_t code;
740 size_t code_w;
741 int16_t w1000;
742 double glyph_advance;
743 uint32_t uni;
744 int w;
745 double mag;
746 int is_space;
747 double ws;
748 3564 ▶ if (!gs || !bytes || len == 0) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!gs || !bytes || len == 0) return 0; 3564 0 3564
if (!gs || !bytes || len == 0) return 0; 3564 0 3564
if (!gs || !bytes || len == 0) return 0; 0 3564 3564
749  
750 3564 rf = resolve_font(result, gs->font);
751 3564 ▶ std14_id = rf ? rf->std14_id : -1;
Branch: 1 branch
BranchTrueFalseTotal
std14_id = rf ? rf->std14_id : -1; 3564 0 3564
752 3564 ▶ is_cid = rf ? rf->is_cid : 0;
Branch: 1 branch
BranchTrueFalseTotal
is_cid = rf ? rf->is_cid : 0; 3564 0 3564
753 3564 ▶ is_vertical = rf ? rf->wmode : 0;
Branch: 1 branch
BranchTrueFalseTotal
is_vertical = rf ? rf->wmode : 0; 3564 0 3564
754 3564 font_size = gs->font_size;
755 3564 char_space = gs->char_space;
756 3564 word_space = gs->word_space;
757  
758 3564 accum = 0;
759 3564 pos = 0;
760 22582 ▶ while (pos < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pos < len) { 19018 3564 22582
761 19018 ▶ if (is_cid && pos + 1 < len) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_cid && pos + 1 < len) { 7 19011 19018
if (is_cid && pos + 1 < len) { 7 0 7
762 7 code = ((uint32_t)bytes[pos] << 8) | bytes[pos + 1];
763 7 code_w = 2;
764 } else {
765 19011 code = bytes[pos];
766 19011 code_w = 1;
767 }
768  
769 /* Lookup advance in 1/1000 em, same priority order as on_text_show */
770 19018 w1000 = 0;
771 19018 ▶ if (rf && rf->widths_resolved) {
Branch: 2 branches
BranchTrueFalseTotal
if (rf && rf->widths_resolved) { 19018 0 19018
if (rf && rf->widths_resolved) { 6353 12665 19018
772 6353 w1000 = pdfmake_font_widths_lookup(&rf->widths, code);
773 }
774 19018 ▶ if (w1000 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (w1000 > 0) { 6353 12665 19018
775 6353 glyph_advance = (double)w1000 / 1000.0 * font_size;
776 12665 ▶ } else if (std14_id >= 0 && !is_cid) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (std14_id >= 0 && !is_cid) { 12665 0 12665
} else if (std14_id >= 0 && !is_cid) { 12665 0 12665
777 /* Resolve unicode via encoding for the Std14 lookup */
778 12665 ▶ uni = rf && rf->encoding_resolved
Branch: 1 branch
BranchTrueFalseTotal
uni = rf && rf->encoding_resolved 12665 0 12665
779 12665 ? rf->encoding.map[code & 0xFF]
780 25330 ▶ : (uint32_t)code;
Branch: 1 branch
BranchTrueFalseTotal
: (uint32_t)code; 12665 0 12665
781 12665 w = pdfmake_std14_width((pdfmake_std14_id_t)std14_id, uni);
782 12665 glyph_advance = (w > 0) ? (double)w / 1000.0 * font_size
783 12665 ▶ : 0.25 * font_size;
Branch: 1 branch
BranchTrueFalseTotal
: 0.25 * font_size; 12665 0 12665
784 } else {
785 0 glyph_advance = 0.5 * font_size;
786 }
787  
788 /* In vertical mode use the magnitude of /DW2 second element as the
789 * advance; the sign is absorbed by the text-matrix branch in the
790 * interpreter, which treats the returned value as a signed
791 * displacement along -y. */
792 19018 ▶ if (is_vertical && rf) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_vertical && rf) { 3 19015 19018
if (is_vertical && rf) { 3 0 3
793 3 ▶ mag = (double)(rf->default_v_advance < 0
Branch: 1 branch
BranchTrueFalseTotal
mag = (double)(rf->default_v_advance < 0 3 0 3
794 3 ? -rf->default_v_advance : rf->default_v_advance);
795 3 ▶ if (mag > 0) glyph_advance = mag / 1000.0 * font_size;
Branch: 1 branch
BranchTrueFalseTotal
if (mag > 0) glyph_advance = mag / 1000.0 * font_size; 3 0 3
796 }
797  
798 /* Add spacing contributions (matches on_text_show) */
799 19018 is_space = (code == 0x20);
800 19018 ▶ ws = is_space ? word_space : 0.0;
Branch: 1 branch
BranchTrueFalseTotal
ws = is_space ? word_space : 0.0; 1934 17084 19018
801 19018 accum += glyph_advance + char_space + ws;
802  
803 19018 pos += code_w;
804 }
805 3564 return accum;
806 }
807  
808 /* Phase 12: BDC / BMC — push the current MCID on entry to a marked-content
809 * item. MCID comes from the /MCID integer in the properties dict (if any).
810 * BMC has no properties dict so it never carries an MCID. */
811 3 static void textract_on_mc_begin(void *ctx,
812 const pdfmake_gstate_t *gs,
813 uint32_t tag,
814 pdfmake_obj_t *properties)
815 {
816 3 pdfmake_textract_result_t *result = (pdfmake_textract_result_t *)ctx;
817 3 int32_t mcid = -1;
818 (void)gs;
819 (void)tag;
820 3 ▶ if (!result) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return; 0 3 3
821  
822 3 ▶ if (properties && properties->kind == PDFMAKE_DICT && result->arena) {
Branch: 3 branches
BranchTrueFalseTotal
if (properties && properties->kind == PDFMAKE_DICT && result->arena) { 3 0 3
if (properties && properties->kind == PDFMAKE_DICT && result->arena) { 3 0 3
if (properties && properties->kind == PDFMAKE_DICT && result->arena) { 3 0 3
823 3 uint32_t mcid_key = pdfmake_arena_intern_name(result->arena, "MCID", 4);
824 3 pdfmake_obj_t *v = pdfmake_dict_get(properties, mcid_key);
825 3 ▶ if (v && v->kind == PDFMAKE_INT) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && v->kind == PDFMAKE_INT) { 3 0 3
if (v && v->kind == PDFMAKE_INT) { 3 0 3
826 3 mcid = (int32_t)v->as.i;
827 }
828 }
829  
830 3 ▶ if (result->mcid_depth <
Branch: 1 branch
BranchTrueFalseTotal
if (result->mcid_depth < 3 0 3
831 (int)(sizeof(result->mcid_stack)/sizeof(result->mcid_stack[0]))) {
832 3 result->mcid_stack[result->mcid_depth++] = result->current_mcid;
833 }
834 /* Nested marked content without its own /MCID inherits the parent's. */
835 3 ▶ if (mcid >= 0) result->current_mcid = mcid;
Branch: 1 branch
BranchTrueFalseTotal
if (mcid >= 0) result->current_mcid = mcid; 3 0 3
836 }
837  
838 3 static void textract_on_mc_end(void *ctx, const pdfmake_gstate_t *gs)
839 {
840 3 pdfmake_textract_result_t *result = (pdfmake_textract_result_t *)ctx;
841 (void)gs;
842 3 ▶ if (!result) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return; 0 3 3
843 3 ▶ if (result->mcid_depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (result->mcid_depth > 0) { 3 0 3
844 3 result->current_mcid = result->mcid_stack[--result->mcid_depth];
845 } else {
846 0 result->current_mcid = -1;
847 }
848 }
849  
850 /* Phase 14: report the current font's writing mode so the interpreter can
851 * advance text_matrix on the right axis. */
852 3564 static int textract_is_vertical(void *ctx, const pdfmake_gstate_t *gs) {
853 3564 pdfmake_textract_result_t *result = (pdfmake_textract_result_t *)ctx;
854 pdfmake_resolved_font_t *rf;
855 3564 ▶ if (!result || !gs) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!result || !gs) return 0; 3564 0 3564
if (!result || !gs) return 0; 0 3564 3564
856 3564 rf = resolve_font(result, gs->font);
857 3564 ▶ return rf ? rf->wmode : 0;
Branch: 1 branch
BranchTrueFalseTotal
return rf ? rf->wmode : 0; 3564 0 3564
858 }
859  
860 87 pdfmake_visitor_t pdfmake_textract_visitor(pdfmake_textract_result_t *result) {
861 pdfmake_visitor_t v;
862 87 memset(&v, 0, sizeof(v));
863 87 v.ctx = result;
864 87 v.on_text_show = textract_on_text_show;
865 87 v.get_string_advance = textract_get_string_advance;
866 87 v.on_marked_content_begin = textract_on_mc_begin;
867 87 v.on_marked_content_end = textract_on_mc_end;
868 87 v.is_vertical_writing = textract_is_vertical; /* Phase 14 */
869 87 return v;
870 }
871  
872 /*============================================================================
873 * Aggregation: raw glyphs → words → lines → blocks
874 *==========================================================================*/
875  
876 /* qsort() has no ctx param — use a file-static pointer for column info
877 * during the sort. pdfmake_textract_aggregate is single-threaded so this
878 * is safe. */
879 static const double *g_sort_column_splits = NULL;
880 static int g_sort_column_count = 0;
881  
882 99328 static int glyph_column(const pdfmake_text_glyph_t *g) {
883 99328 int col = 0;
884 int i;
885 128562 ▶ for (i = 0; i < g_sort_column_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < g_sort_column_count; i++) { 99328 29234 128562
886 99328 ▶ if (g->x0 >= g_sort_column_splits[i]) col++;
Branch: 1 branch
BranchTrueFalseTotal
if (g->x0 >= g_sort_column_splits[i]) col++; 29234 70094 99328
887 70094 else break;
888 }
889 99328 return col;
890 }
891  
892 /* Compare glyphs by column, then y descending, then x ascending. */
893 99891 static int cmp_glyph_position(const void *a, const void *b) {
894 99891 const pdfmake_text_glyph_t *ga = (const pdfmake_text_glyph_t *)a;
895 99891 const pdfmake_text_glyph_t *gb = (const pdfmake_text_glyph_t *)b;
896 double col_tol;
897 double dx_cx;
898 double dy2;
899 int ca;
900 int cb;
901 double dy;
902 double dx;
903  
904 /* Phase 14: vertical glyphs sort right-to-left, top-to-bottom so
905 * reading order matches CJK conventions. */
906 99891 ▶ if (ga->vertical && gb->vertical) {
Branch: 2 branches
BranchTrueFalseTotal
if (ga->vertical && gb->vertical) { 2 99889 99891
if (ga->vertical && gb->vertical) { 2 0 2
907 /* Group into columns by x (tolerance = font size). Right-most
908 * column comes first. */
909 2 col_tol = ga->font_size * 0.5;
910 2 dx_cx = gb->x0 - ga->x0;
911 2 ▶ if (fabs(dx_cx) > col_tol) {
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(dx_cx) > col_tol) { 0 2 2
912 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
913 }
914 /* Same column: higher y first (top-to-bottom). */
915 2 dy2 = gb->y0 - ga->y0;
916 2 ▶ if (dy2 < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (dy2 < 0) return -1; 2 0 2
917 0 ▶ if (dy2 > 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (dy2 > 0) return 1; 0 0 0
918 0 return 0;
919 }
920 /* If one is vertical and the other horizontal, emit vertical last so
921 * the horizontal reading order isn't interleaved. (Truly mixed pages
922 * are rare; the user can filter per glyph->vertical if they need to.) */
923 99889 ▶ if (ga->vertical != gb->vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (ga->vertical != gb->vertical) { 0 99889 99889
924 0 ▶ return ga->vertical ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
return ga->vertical ? 1 : -1; 0 0 0
925 }
926  
927 /* Phase 10: columns first so column 1 flows fully before column 2 */
928 99889 ▶ if (g_sort_column_count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (g_sort_column_count > 0) { 49664 50225 99889
929 49664 ca = glyph_column(ga);
930 49664 cb = glyph_column(gb);
931 49664 ▶ if (ca != cb) return ca - cb;
Branch: 1 branch
BranchTrueFalseTotal
if (ca != cb) return ca - cb; 6226 43438 49664
932 }
933  
934 /* Group by baseline (y0): higher y first (PDF y increases upward) */
935 93663 dy = gb->y0 - ga->y0;
936 93663 ▶ if (fabs(dy) > ga->font_size * 0.3) {
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(dy) > ga->font_size * 0.3) { 44409 49254 93663
937 44409 ▶ return dy > 0 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
return dy > 0 ? 1 : -1; 11307 33102 44409
938 }
939 /* Same line: left to right */
940 49254 dx = ga->x0 - gb->x0;
941 49254 ▶ if (dx < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (dx < 0) return -1; 49254 0 49254
942 0 ▶ if (dx > 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (dx > 0) return 1; 0 0 0
943 0 return 0;
944 }
945  
946 /* Detect column splits via largest-gap analysis on x0 values.
947 *
948 * Algorithm: take all unique-ish glyph x0 values, sort them, look for
949 * contiguous runs where no glyph starts. The widest such run in the page
950 * interior (not at extremes) is a likely gutter; split at its midpoint.
951 * Recurse on each half to find multi-column layouts up to 8 total columns.
952 *
953 * Thresholds:
954 * - min gutter width: 20pt (so we don't split on letter spacing)
955 * - gutter must be in middle 70% of x-range (not at margins)
956 * - only split if the two sides each contain ≥ 4 glyphs
957 */
958 /* Histogram bin size for column-gutter detection, in user-space points */
959 #define COLUMN_BIN_SIZE 5.0
960  
961 /* Distinct baselines among glyphs lying within [x_from, x_to], capped — the
962 * caller only ever asks "more than one?".
963 *
964 * The occupancy threshold below counts a bin as empty unless two glyphs cover
965 * it, so on a sparse page the letters of one big heading read as whitespace and
966 * a 20pt stretch between two of them looks like a gutter. A real gutter has
967 * text on both sides of it on several lines; one heading's worth of glyphs
968 * spans a single baseline, which is what tells the two apart. */
969 #define COLUMN_BASELINE_TOL 1.0
970 #define COLUMN_BASELINE_MAX 8
971  
972 38 static int count_baselines(const pdfmake_text_glyph_t *glyphs, size_t n,
973 double x_from, double x_to)
974 {
975 double seen[COLUMN_BASELINE_MAX];
976 38 int count = 0;
977 size_t i;
978 int j;
979  
980 7274 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 7258 16 7274
981 7258 const pdfmake_text_glyph_t *g = &glyphs[i];
982 7258 ▶ if (g->x0 < x_from || g->x1 > x_to) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (g->x0 < x_from || g->x1 > x_to) continue; 2688 4570 7258
if (g->x0 < x_from || g->x1 > x_to) continue; 268 2420 2688
983 11464 ▶ for (j = 0; j < count; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) 11220 244 11464
984 11220 ▶ if (fabs(seen[j] - g->y0) <= COLUMN_BASELINE_TOL) break;
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(seen[j] - g->y0) <= COLUMN_BASELINE_TOL) break; 2176 9044 11220
985 2420 ▶ if (j < count) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (j < count) continue; 2176 244 2420
986 244 ▶ if (count >= COLUMN_BASELINE_MAX) return count;
Branch: 1 branch
BranchTrueFalseTotal
if (count >= COLUMN_BASELINE_MAX) return count; 22 222 244
987 222 seen[count++] = g->y0;
988 }
989 16 return count;
990 }
991  
992 77 static void detect_column_splits_in_range(
993 const pdfmake_text_glyph_t *glyphs, size_t n,
994 double x_lo, double x_hi,
995 double *out_splits, int *out_count, int max_count)
996 {
997 size_t n_bins;
998 uint32_t *hist;
999 size_t i;
1000 uint32_t max_cnt;
1001 uint32_t empty_thr;
1002 size_t safe_lo_bin;
1003 size_t safe_hi_bin;
1004 size_t best_run;
1005 size_t best_start;
1006 size_t cur_run;
1007 size_t cur_start;
1008 size_t mid;
1009 size_t min_gutter_bins;
1010 size_t active_left;
1011 size_t active_right;
1012 double gutter_lo;
1013 double gutter_hi;
1014 77 ▶ if (*out_count >= max_count) return;
Branch: 1 branch
BranchTrueFalseTotal
if (*out_count >= max_count) return; 0 77 77
1015 77 ▶ if (x_hi - x_lo < 100) return;
Branch: 1 branch
BranchTrueFalseTotal
if (x_hi - x_lo < 100) return; 1 76 77
1016  
1017 76 n_bins = (size_t)((x_hi - x_lo) / COLUMN_BIN_SIZE) + 1;
1018 76 ▶ if (n_bins < 16 || n_bins > 10000) return;
Branch: 2 branches
BranchTrueFalseTotal
if (n_bins < 16 || n_bins > 10000) return; 76 0 76
if (n_bins < 16 || n_bins > 10000) return; 0 76 76
1019  
1020 76 hist = calloc(n_bins, sizeof(uint32_t));
1021 76 ▶ if (!hist) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hist) return; 0 76 76
1022  
1023 /* Populate histogram by glyph bbox coverage (not just x0) */
1024 35347 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 35271 76 35347
1025 35271 const pdfmake_text_glyph_t *g = &glyphs[i];
1026 double a;
1027 double b;
1028 size_t ba;
1029 size_t bb;
1030 35271 ▶ if (g->x1 < x_lo || g->x0 > x_hi) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (g->x1 < x_lo || g->x0 > x_hi) continue; 29280 5991 35271
if (g->x1 < x_lo || g->x0 > x_hi) continue; 2388 26892 29280
1031 26892 ▶ a = g->x0 > x_lo ? g->x0 : x_lo;
Branch: 1 branch
BranchTrueFalseTotal
a = g->x0 > x_lo ? g->x0 : x_lo; 25963 929 26892
1032 26892 ▶ b = g->x1 < x_hi ? g->x1 : x_hi;
Branch: 1 branch
BranchTrueFalseTotal
b = g->x1 < x_hi ? g->x1 : x_hi; 26777 115 26892
1033 26892 ▶ if (b <= a) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (b <= a) continue; 0 26892 26892
1034 26892 ba = (size_t)((a - x_lo) / COLUMN_BIN_SIZE);
1035 26892 bb = (size_t)((b - x_lo) / COLUMN_BIN_SIZE);
1036 26892 ▶ if (bb >= n_bins) bb = n_bins - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (bb >= n_bins) bb = n_bins - 1; 0 26892 26892
1037 {
1038 size_t k;
1039 81858 ▶ for (k = ba; k <= bb; k++) hist[k]++;
Branch: 1 branch
BranchTrueFalseTotal
for (k = ba; k <= bb; k++) hist[k]++; 54966 26892 81858
1040 }
1041 }
1042  
1043 76 max_cnt = 0;
1044 4549 ▶ for (i = 0; i < n_bins; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_bins; i++) 4473 76 4549
1045 4473 ▶ if (hist[i] > max_cnt) max_cnt = hist[i];
Branch: 1 branch
BranchTrueFalseTotal
if (hist[i] > max_cnt) max_cnt = hist[i]; 266 4207 4473
1046 76 ▶ if (max_cnt < 4) { free(hist); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (max_cnt < 4) { free(hist); return; } 13 63 76
1047  
1048 /* "Near-empty" = less than 15% of peak density. Higher than 2% because
1049 * full-width headers/footers contribute thin residual occupancy across
1050 * the gutter, and we don't want those to mask real column gaps. */
1051 63 empty_thr = max_cnt / 6 + 1;
1052  
1053 /* Only consider gutters whose midpoint is in the middle 70% of range */
1054 63 safe_lo_bin = n_bins * 15 / 100;
1055 63 safe_hi_bin = n_bins * 85 / 100;
1056  
1057 63 best_run = 0;
1058 63 best_start = 0;
1059 63 cur_run = 0;
1060 63 cur_start = 0;
1061 4045 ▶ for (i = 0; i < n_bins; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_bins; i++) { 3982 63 4045
1062 3982 ▶ if (hist[i] < empty_thr) {
Branch: 1 branch
BranchTrueFalseTotal
if (hist[i] < empty_thr) { 1555 2427 3982
1063 1555 ▶ if (cur_run == 0) cur_start = i;
Branch: 1 branch
BranchTrueFalseTotal
if (cur_run == 0) cur_start = i; 191 1364 1555
1064 1555 cur_run++;
1065 } else {
1066 2427 ▶ if (cur_run > best_run) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_run > best_run) { 70 2357 2427
1067 70 mid = cur_start + cur_run / 2;
1068 70 ▶ if (mid >= safe_lo_bin && mid <= safe_hi_bin) {
Branch: 2 branches
BranchTrueFalseTotal
if (mid >= safe_lo_bin && mid <= safe_hi_bin) { 69 1 70
if (mid >= safe_lo_bin && mid <= safe_hi_bin) { 69 0 69
1069 69 best_run = cur_run;
1070 69 best_start = cur_start;
1071 }
1072 }
1073 2427 cur_run = 0;
1074 }
1075 }
1076 63 ▶ if (cur_run > best_run) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_run > best_run) { 48 15 63
1077 48 mid = cur_start + cur_run / 2;
1078 48 ▶ if (mid >= safe_lo_bin && mid <= safe_hi_bin) {
Branch: 2 branches
BranchTrueFalseTotal
if (mid >= safe_lo_bin && mid <= safe_hi_bin) { 48 0 48
if (mid >= safe_lo_bin && mid <= safe_hi_bin) { 25 23 48
1079 25 best_run = cur_run;
1080 25 best_start = cur_start;
1081 }
1082 }
1083  
1084 63 min_gutter_bins = (size_t)(20.0 / COLUMN_BIN_SIZE); /* 20pt */
1085 63 ▶ if (best_run >= min_gutter_bins) {
Branch: 1 branch
BranchTrueFalseTotal
if (best_run >= min_gutter_bins) { 47 16 63
1086 47 active_left = 0;
1087 47 active_right = 0;
1088 1391 ▶ for (i = 0; i < best_start; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < best_start; i++) 1344 47 1391
1089 1344 ▶ if (hist[i] >= empty_thr) active_left++;
Branch: 1 branch
BranchTrueFalseTotal
if (hist[i] >= empty_thr) active_left++; 1221 123 1344
1090 725 ▶ for (i = best_start + best_run; i < n_bins; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = best_start + best_run; i < n_bins; i++) 678 47 725
1091 678 ▶ if (hist[i] >= empty_thr) active_right++;
Branch: 1 branch
BranchTrueFalseTotal
if (hist[i] >= empty_thr) active_right++; 249 429 678
1092  
1093 47 gutter_lo = x_lo + best_start * COLUMN_BIN_SIZE;
1094 47 gutter_hi = x_lo + (best_start + best_run) * COLUMN_BIN_SIZE;
1095  
1096 66 ▶ if (active_left >= 4 && active_right >= 4 &&
Branch: 1 branch
BranchTrueFalseTotal
if (active_left >= 4 && active_right >= 4 && 44 3 47
1097 38 ▶ count_baselines(glyphs, n, x_lo, gutter_lo) >= 2 &&
Branch: 1 branch
BranchTrueFalseTotal
count_baselines(glyphs, n, x_lo, gutter_lo) >= 2 && 15 4 19
1098 19 count_baselines(glyphs, n, gutter_hi, x_hi) >= 2) {
1099 15 double mid_x = x_lo + (best_start + best_run / 2.0) * COLUMN_BIN_SIZE;
1100  
1101 15 int pos = *out_count;
1102 15 ▶ while (pos > 0 && out_splits[pos - 1] > mid_x) {
Branch: 2 branches
BranchTrueFalseTotal
while (pos > 0 && out_splits[pos - 1] > mid_x) { 0 15 15
while (pos > 0 && out_splits[pos - 1] > mid_x) { 0 0 0
1103 0 out_splits[pos] = out_splits[pos - 1]; pos--;
1104 }
1105 15 out_splits[pos] = mid_x;
1106 15 (*out_count)++;
1107  
1108 15 free(hist);
1109 /* Recurse into each half to find further column splits */
1110 15 detect_column_splits_in_range(glyphs, n,
1111 x_lo, mid_x, out_splits, out_count, max_count);
1112 15 detect_column_splits_in_range(glyphs, n,
1113 mid_x, x_hi, out_splits, out_count, max_count);
1114 15 return;
1115 }
1116 }
1117  
1118 48 free(hist);
1119 }
1120  
1121 87 static void detect_column_splits(pdfmake_textract_result_t *result) {
1122 double x_lo;
1123 double x_hi;
1124 size_t i;
1125 87 result->column_split_count = 0;
1126 87 ▶ if (result->raw_len < 16) return;
Branch: 1 branch
BranchTrueFalseTotal
if (result->raw_len < 16) return; 32 55 87
1127  
1128 55 x_lo = 1e18;
1129 55 x_hi = -1e18;
1130 18716 ▶ for (i = 0; i < result->raw_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < result->raw_len; i++) { 18661 55 18716
1131 18661 ▶ 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; 76 18585 18661
1132 18661 ▶ 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; 1625 17036 18661
1133 }
1134 55 ▶ if (x_hi - x_lo < 100) return;
Branch: 1 branch
BranchTrueFalseTotal
if (x_hi - x_lo < 100) return; 8 47 55
1135  
1136 47 detect_column_splits_in_range(result->raw_glyphs, result->raw_len,
1137 47 x_lo, x_hi, result->column_splits, &result->column_split_count,
1138 (int)(sizeof(result->column_splits) / sizeof(double)));
1139 }
1140  
1141 17030 static void update_bbox_from_glyph(double *x0, double *y0, double *x1, double *y1,
1142 const pdfmake_text_glyph_t *g) {
1143 17030 ▶ if (g->x0 < *x0) *x0 = g->x0;
Branch: 1 branch
BranchTrueFalseTotal
if (g->x0 < *x0) *x0 = g->x0; 2908 14122 17030
1144 17030 ▶ if (g->y0 < *y0) *y0 = g->y0;
Branch: 1 branch
BranchTrueFalseTotal
if (g->y0 < *y0) *y0 = g->y0; 2910 14120 17030
1145 17030 ▶ if (g->x1 > *x1) *x1 = g->x1;
Branch: 1 branch
BranchTrueFalseTotal
if (g->x1 > *x1) *x1 = g->x1; 17028 2 17030
1146 17030 ▶ if (g->y1 > *y1) *y1 = g->y1;
Branch: 1 branch
BranchTrueFalseTotal
if (g->y1 > *y1) *y1 = g->y1; 2908 14122 17030
1147 17030 }
1148  
1149 17030 static int word_push_glyph(pdfmake_text_word_t *w, const pdfmake_text_glyph_t *g) {
1150 17030 ▶ if (w->len >= w->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (w->len >= w->cap) { 3365 13665 17030
1151 3365 ▶ 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; 457 2908 3365
1152 3365 pdfmake_text_glyph_t *arr = realloc(w->glyphs, new_cap * sizeof(*arr));
1153 3365 ▶ if (!arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!arr) return 0; 0 3365 3365
1154 3365 w->glyphs = arr;
1155 3365 w->cap = new_cap;
1156 }
1157 17030 w->glyphs[w->len++] = *g;
1158 17030 return 1;
1159 }
1160  
1161 2908 static int line_push_word(pdfmake_text_line_t *l, const pdfmake_text_word_t *w) {
1162 2908 ▶ if (l->len >= l->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->len >= l->cap) { 859 2049 2908
1163 859 ▶ 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; 84 775 859
1164 859 pdfmake_text_word_t *arr = realloc(l->words, new_cap * sizeof(*arr));
1165 859 ▶ if (!arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!arr) return 0; 0 859 859
1166 859 l->words = arr;
1167 859 l->cap = new_cap;
1168 }
1169 2908 l->words[l->len++] = *w;
1170 2908 return 1;
1171 }
1172  
1173 775 static int block_push_line(pdfmake_text_block_t *b, const pdfmake_text_line_t *l) {
1174 775 ▶ if (b->len >= b->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->len >= b->cap) { 351 424 775
1175 351 ▶ 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 316 351
1176 351 pdfmake_text_line_t *arr = realloc(b->lines, new_cap * sizeof(*arr));
1177 351 ▶ if (!arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!arr) return 0; 0 351 351
1178 351 b->lines = arr;
1179 351 b->cap = new_cap;
1180 }
1181 775 b->lines[b->len++] = *l;
1182 775 return 1;
1183 }
1184  
1185 316 static int result_push_block(pdfmake_textract_result_t *r, const pdfmake_text_block_t *b) {
1186 316 ▶ if (r->len >= r->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->len >= r->cap) { 127 189 316
1187 127 ▶ 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; 40 87 127
1188 127 pdfmake_text_block_t *arr = realloc(r->blocks, new_cap * sizeof(*arr));
1189 127 ▶ if (!arr) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!arr) return 0; 0 127 127
1190 127 r->blocks = arr;
1191 127 r->cap = new_cap;
1192 }
1193 316 r->blocks[r->len++] = *b;
1194 316 return 1;
1195 }
1196  
1197 87 pdfmake_err_t pdfmake_textract_aggregate(
1198 pdfmake_textract_result_t *result,
1199 const pdfmake_textract_options_t *options)
1200 {
1201 pdfmake_textract_options_t opts;
1202 pdfmake_text_word_t *words;
1203 size_t words_len;
1204 size_t words_cap;
1205 pdfmake_text_word_t cur_word;
1206 pdfmake_text_line_t *lines;
1207 size_t lines_len;
1208 size_t lines_cap;
1209 pdfmake_text_line_t cur_line;
1210 pdfmake_text_block_t cur_block;
1211 size_t i;
1212 87 ▶ if (!result || result->raw_len == 0) return PDFMAKE_OK;
Branch: 2 branches
BranchTrueFalseTotal
if (!result || result->raw_len == 0) return PDFMAKE_OK; 87 0 87
if (!result || result->raw_len == 0) return PDFMAKE_OK; 0 87 87
1213  
1214 87 ▶ opts = options ? *options : pdfmake_textract_default_options();
Branch: 1 branch
BranchTrueFalseTotal
opts = options ? *options : pdfmake_textract_default_options(); 87 0 87
1215  
1216 /* Phase 10: detect column splits before sorting so reading order is
1217 * column-major (fill column 1 top-to-bottom, then column 2, ...). */
1218 87 detect_column_splits(result);
1219 87 g_sort_column_splits = result->column_splits;
1220 87 g_sort_column_count = result->column_split_count;
1221  
1222 /* Sort glyphs into reading order */
1223 87 qsort(result->raw_glyphs, result->raw_len, sizeof(pdfmake_text_glyph_t),
1224 cmp_glyph_position);
1225  
1226 /* Don't leak static pointers past this function */
1227 87 g_sort_column_splits = NULL;
1228 87 g_sort_column_count = 0;
1229  
1230 /* Phase 5: Kern-aware word grouping.
1231 *
1232 * We split into a new word when ONE of:
1233 * - previous glyph is a space (U+0020)
1234 * - current glyph is a space (low priority)
1235 * - baseline jumped significantly (implies new line)
1236 * - horizontal gap exceeds a context-sensitive threshold
1237 *
1238 * Key insight from Phase 5: the gap threshold must be wider when the
1239 * previous glyph's advance is a fallback (i.e., we didn't know the real
1240 * width). In that case a "real-looking" Td that's actually intra-word
1241 * spacing (author-driven font-change mid-word in subsetted fonts) looks
1242 * like a big visible gap. Using ~0.9 em here keeps "Data" glued together
1243 * across font changes while still catching inter-word spaces.
1244 *
1245 * When we trust the advance, we use the tighter 0.3-em threshold —
1246 * that's what PDF typesetters use as a natural space width.
1247 *
1248 * We never split on negative or zero gaps; those come from kerning
1249 * overlap in TJ arrays and always represent intra-word positioning.
1250 */
1251 87 words = NULL;
1252 87 words_len = 0;
1253 87 words_cap = 0;
1254  
1255 87 memset(&cur_word, 0, sizeof(cur_word));
1256 87 cur_word.x0 = cur_word.y0 = 1e18;
1257 87 cur_word.x1 = cur_word.y1 = -1e18;
1258 87 cur_word.mcid = -1;
1259  
1260 19096 ▶ for (i = 0; i < result->raw_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < result->raw_len; i++) { 19009 87 19096
1261 19009 pdfmake_text_glyph_t *g = &result->raw_glyphs[i];
1262  
1263 19009 int new_word = 0;
1264 19009 ▶ if (cur_word.len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_word.len == 0) { 2052 16957 19009
1265 2052 new_word = 0; /* first glyph */
1266 } else {
1267 16957 pdfmake_text_glyph_t *prev = &cur_word.glyphs[cur_word.len - 1];
1268  
1269 16957 ▶ if (g->vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (g->vertical) { 2 16955 16957
1270 /* Phase 14: vertical word — split on column change (x jump)
1271 * or on a large vertical gap. */
1272 2 double col_diff = fabs(g->x0 - prev->x0);
1273 /* prev->y0 is the bottom; g->y1 is the top of the new glyph.
1274 * In top-to-bottom flow, gap = prev->y0 - g->y1 > 0. */
1275 2 double v_gap = prev->y0 - g->y1;
1276  
1277 2 ▶ if (col_diff > g->font_size * 0.5) {
Branch: 1 branch
BranchTrueFalseTotal
if (col_diff > g->font_size * 0.5) { 0 2 2
1278 0 new_word = 1;
1279 }
1280 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
1281 0 new_word = 1;
1282 }
1283 } else {
1284 16955 double gap = g->x0 - prev->x1;
1285 16955 double baseline_diff = fabs(g->y0 - prev->y0);
1286  
1287 /* Rule 1: baseline change = new line = new word */
1288 16955 ▶ if (baseline_diff > opts.line_tolerance * g->font_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (baseline_diff > opts.line_tolerance * g->font_size) { 563 16392 16955
1289 563 new_word = 1;
1290 }
1291  
1292 /* Rule 2: explicit space character = word boundary */
1293 16955 ▶ if (!new_word &&
Branch: 1 branch
BranchTrueFalseTotal
if (!new_word && 16392 563 16955
1294 16392 ▶ (prev->unicode == 0x20 || g->unicode == 0x20)) {
Branch: 2 branches
BranchTrueFalseTotal
(prev->unicode == 0x20 || g->unicode == 0x20)) { 16392 0 16392
(prev->unicode == 0x20 || g->unicode == 0x20)) { 1906 14486 16392
1295 1906 new_word = 1;
1296 }
1297  
1298 /* Rule 3: gap threshold based on advance reliability.
1299 * - Reliable advances: tight ~0.3 em threshold
1300 * - Fallback advances: generous ~0.9 em threshold
1301 * - Negative/zero gaps: never split (kerning) */
1302 16955 ▶ if (!new_word && gap > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!new_word && gap > 0) { 14486 2469 16955
if (!new_word && gap > 0) { 6372 8114 14486
1303 double threshold;
1304 6372 ▶ if (prev->reliable_advance) {
Branch: 1 branch
BranchTrueFalseTotal
if (prev->reliable_advance) { 6372 0 6372
1305 6372 threshold = opts.word_gap_factor * g->font_size;
1306 } else {
1307 /* Unreliable widths: need a bigger gap to call it a space */
1308 0 threshold = 0.9 * g->font_size;
1309 }
1310 6372 ▶ if (gap > threshold) new_word = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (gap > threshold) new_word = 1; 366 6006 6372
1311 }
1312 }
1313 }
1314  
1315 19009 ▶ if (new_word && cur_word.len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (new_word && cur_word.len > 0) { 2835 16174 19009
if (new_word && cur_word.len > 0) { 2835 0 2835
1316 /* Flush current word */
1317 2835 ▶ if (words_len >= words_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (words_len >= words_cap) { 147 2688 2835
1318 147 ▶ words_cap = words_cap == 0 ? 16 : words_cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
words_cap = words_cap == 0 ? 16 : words_cap * 2; 66 81 147
1319 147 words = realloc(words, words_cap * sizeof(*words));
1320 147 ▶ if (!words) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!words) return PDFMAKE_ENOMEM; 0 147 147
1321 }
1322 2835 words[words_len++] = cur_word;
1323 2835 memset(&cur_word, 0, sizeof(cur_word));
1324 2835 cur_word.x0 = cur_word.y0 = 1e18;
1325 2835 cur_word.x1 = cur_word.y1 = -1e18;
1326 2835 cur_word.mcid = -1;
1327 }
1328  
1329 /* Skip spaces (don't include in words) */
1330 19009 ▶ if (g->unicode == 0x20) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (g->unicode == 0x20) continue; 1979 17030 19009
1331  
1332 /* Phase 12: first glyph sets the word's MCID */
1333 17030 ▶ if (cur_word.len == 0) cur_word.mcid = g->mcid;
Branch: 1 branch
BranchTrueFalseTotal
if (cur_word.len == 0) cur_word.mcid = g->mcid; 2908 14122 17030
1334  
1335 17030 word_push_glyph(&cur_word, g);
1336 17030 update_bbox_from_glyph(&cur_word.x0, &cur_word.y0,
1337 &cur_word.x1, &cur_word.y1, g);
1338 }
1339 /* Flush last word */
1340 87 ▶ if (cur_word.len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_word.len > 0) { 73 14 87
1341 73 ▶ if (words_len >= words_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (words_len >= words_cap) { 7 66 73
1342 7 ▶ words_cap = words_cap == 0 ? 16 : words_cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
words_cap = words_cap == 0 ? 16 : words_cap * 2; 1 6 7
1343 7 words = realloc(words, words_cap * sizeof(*words));
1344 }
1345 73 ▶ if (words) words[words_len++] = cur_word;
Branch: 1 branch
BranchTrueFalseTotal
if (words) words[words_len++] = cur_word; 73 0 73
1346 }
1347  
1348 87 ▶ if (words_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (words_len == 0) { 0 87 87
1349 0 free(words);
1350 0 return PDFMAKE_OK;
1351 }
1352  
1353 /* Phase 2: Group words into lines (same baseline) */
1354 87 lines = NULL;
1355 87 lines_len = 0;
1356 87 lines_cap = 0;
1357  
1358 87 memset(&cur_line, 0, sizeof(cur_line));
1359 87 cur_line.x0 = cur_line.y0 = 1e18;
1360 87 cur_line.x1 = cur_line.y1 = -1e18;
1361  
1362 2995 ▶ for (i = 0; i < words_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < words_len; i++) { 2908 87 2995
1363 2908 pdfmake_text_word_t *w = &words[i];
1364  
1365 /* Phase 14: detect whether this word is vertical by checking its
1366 * first glyph. Line grouping then compares x (column) instead of
1367 * baseline y. */
1368 2908 ▶ int w_vertical = (w->len > 0 && w->glyphs[0].vertical);
Branch: 2 branches
BranchTrueFalseTotal
int w_vertical = (w->len > 0 && w->glyphs[0].vertical); 2908 0 2908
int w_vertical = (w->len > 0 && w->glyphs[0].vertical); 1 2907 2908
1369  
1370 2908 int new_line = 0;
1371 2908 ▶ if (cur_line.len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_line.len == 0) { 87 2821 2908
1372 87 cur_line.baseline_y = w->y0;
1373 } else {
1374 2821 double ref_size = cur_line.words[0].glyphs[0].font_size;
1375 2821 ▶ if (w_vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (w_vertical) { 0 2821 2821
1376 /* Same column = same vertical line. */
1377 0 double col_diff = fabs(w->x0 - cur_line.words[0].x0);
1378 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
1379 } else {
1380 2821 double baseline_diff = fabs(w->y0 - cur_line.baseline_y);
1381 2821 ▶ if (baseline_diff > opts.line_tolerance * ref_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (baseline_diff > opts.line_tolerance * ref_size) { 688 2133 2821
1382 688 new_line = 1;
1383 }
1384 }
1385 }
1386  
1387 2908 ▶ if (new_line && cur_line.len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (new_line && cur_line.len > 0) { 688 2220 2908
if (new_line && cur_line.len > 0) { 688 0 688
1388 688 ▶ if (lines_len >= lines_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (lines_len >= lines_cap) { 90 598 688
1389 90 ▶ lines_cap = lines_cap == 0 ? 8 : lines_cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
lines_cap = lines_cap == 0 ? 8 : lines_cap * 2; 47 43 90
1390 90 lines = realloc(lines, lines_cap * sizeof(*lines));
1391 90 ▶ if (!lines) { free(words); return PDFMAKE_ENOMEM; }
Branch: 1 branch
BranchTrueFalseTotal
if (!lines) { free(words); return PDFMAKE_ENOMEM; } 0 90 90
1392 }
1393 688 lines[lines_len++] = cur_line;
1394 688 memset(&cur_line, 0, sizeof(cur_line));
1395 688 cur_line.x0 = cur_line.y0 = 1e18;
1396 688 cur_line.x1 = cur_line.y1 = -1e18;
1397 688 cur_line.baseline_y = w->y0;
1398 }
1399  
1400 2908 line_push_word(&cur_line, w);
1401 2908 ▶ 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; 775 2133 2908
1402 2908 ▶ 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; 793 2115 2908
1403 2908 ▶ 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; 2866 42 2908
1404 2908 ▶ 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; 778 2130 2908
1405 }
1406 87 ▶ if (cur_line.len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_line.len > 0) { 87 0 87
1407 87 ▶ if (lines_len >= lines_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (lines_len >= lines_cap) { 45 42 87
1408 45 ▶ lines_cap = lines_cap == 0 ? 8 : lines_cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
lines_cap = lines_cap == 0 ? 8 : lines_cap * 2; 1 44 45
1409 45 lines = realloc(lines, lines_cap * sizeof(*lines));
1410 }
1411 87 ▶ if (lines) lines[lines_len++] = cur_line;
Branch: 1 branch
BranchTrueFalseTotal
if (lines) lines[lines_len++] = cur_line; 87 0 87
1412 }
1413 87 free(words); /* words are now owned by lines */
1414  
1415 /* Phase 3: Group lines into blocks */
1416 87 memset(&cur_block, 0, sizeof(cur_block));
1417 87 cur_block.x0 = cur_block.y0 = 1e18;
1418 87 cur_block.x1 = cur_block.y1 = -1e18;
1419  
1420 862 ▶ for (i = 0; i < lines_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < lines_len; i++) { 775 87 862
1421 775 pdfmake_text_line_t *l = &lines[i];
1422  
1423 775 int new_block = 0;
1424 775 ▶ if (cur_block.len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_block.len > 0) { 688 87 775
1425 688 pdfmake_text_line_t *prev = &cur_block.lines[cur_block.len - 1];
1426 688 double ref_size = prev->words[0].glyphs[0].font_size;
1427 1376 ▶ int l_vertical = (l->words[0].len > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
int l_vertical = (l->words[0].len > 0 && 688 0 688
1428 688 ▶ l->words[0].glyphs[0].vertical);
Branch: 1 branch
BranchTrueFalseTotal
l->words[0].glyphs[0].vertical); 0 688 688
1429 double gap;
1430 688 ▶ if (l_vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if (l_vertical) { 0 688 688
1431 /* Vertical: block boundary when columns are far apart in x. */
1432 0 gap = fabs(prev->words[0].x0 - l->words[0].x0);
1433 } else {
1434 688 gap = fabs(prev->baseline_y - l->baseline_y);
1435 }
1436 688 ▶ if (gap > opts.block_leading * ref_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (gap > opts.block_leading * ref_size) { 229 459 688
1437 229 new_block = 1;
1438 }
1439 }
1440  
1441 775 ▶ if (new_block && cur_block.len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (new_block && cur_block.len > 0) { 229 546 775
if (new_block && cur_block.len > 0) { 229 0 229
1442 229 result_push_block(result, &cur_block);
1443 229 memset(&cur_block, 0, sizeof(cur_block));
1444 229 cur_block.x0 = cur_block.y0 = 1e18;
1445 229 cur_block.x1 = cur_block.y1 = -1e18;
1446 }
1447  
1448 775 block_push_line(&cur_block, l);
1449 775 ▶ 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; 319 456 775
1450 775 ▶ 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; 775 0 775
1451 775 ▶ 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; 373 402 775
1452 775 ▶ 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; 316 459 775
1453 }
1454 87 ▶ if (cur_block.len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cur_block.len > 0) { 87 0 87
1455 87 result_push_block(result, &cur_block);
1456 }
1457 87 free(lines); /* lines are now owned by blocks */
1458  
1459 87 return PDFMAKE_OK;
1460 }
1461  
1462 /*============================================================================
1463 * Convenience: run extraction in one call
1464 *==========================================================================*/
1465  
1466 87 pdfmake_err_t pdfmake_textract_run(
1467 pdfmake_interp_t *interp,
1468 const uint8_t *content, size_t content_len,
1469 const pdfmake_textract_options_t *options,
1470 pdfmake_textract_result_t *result)
1471 {
1472 pdfmake_visitor_t visitor;
1473 pdfmake_err_t err;
1474 87 ▶ if (!interp || !content || !result) return PDFMAKE_EINVAL;
Branch: 3 branches
BranchTrueFalseTotal
if (!interp || !content || !result) return PDFMAKE_EINVAL; 87 0 87
if (!interp || !content || !result) return PDFMAKE_EINVAL; 87 0 87
if (!interp || !content || !result) return PDFMAKE_EINVAL; 0 87 87
1475  
1476 /* Propagate per-run options that the visitor needs to see during
1477 * interpretation (aggregate-phase options stay in `options`). */
1478 87 ▶ if (options) {
Branch: 1 branch
BranchTrueFalseTotal
if (options) { 87 0 87
1479 87 result->include_invisible = options->include_invisible;
1480 }
1481  
1482 /* Set up visitor */
1483 87 visitor = pdfmake_textract_visitor(result);
1484 87 pdfmake_interp_set_visitor(interp, &visitor);
1485  
1486 /* Interpret content stream */
1487 87 err = pdfmake_interpret(interp, content, content_len);
1488 87 ▶ if (err != PDFMAKE_OK) return err;
Branch: 1 branch
BranchTrueFalseTotal
if (err != PDFMAKE_OK) return err; 0 87 87
1489  
1490 /* Aggregate */
1491 87 return pdfmake_textract_aggregate(result, options);
1492 }
1493  
1494 /*============================================================================
1495 * Phase 12: Structure tree resolution
1496 *
1497 * Walks a /StructTreeRoot subtree and populates result->struct_map with one
1498 * entry per MCID encountered, pairing it with the /S role of the nearest
1499 * enclosing StructElem. The resulting flat map is consulted after extraction
1500 * so each word can be tagged with its structure role.
1501 *==========================================================================*/
1502  
1503 3 static int struct_map_push(pdfmake_textract_result_t *r, int32_t mcid,
1504 uint32_t role_id)
1505 {
1506 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
1507 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
1508 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
1509 1 pdfmake_struct_map_entry_t *n = realloc(
1510 1 r->struct_map, new_cap * sizeof(*n));
1511 1 ▶ if (!n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return 0; 0 1 1
1512 1 r->struct_map = n;
1513 1 r->struct_map_cap = new_cap;
1514 }
1515 3 r->struct_map[r->struct_map_len].mcid = mcid;
1516 3 r->struct_map[r->struct_map_len].role_id = role_id;
1517 3 r->struct_map_len++;
1518 3 return 1;
1519 }
1520  
1521 /* Recursively walk a StructElem dict or its /K entry. role_id is the /S of
1522 * the nearest enclosing StructElem. page_dict is the target page (NULL to
1523 * accept all pages). */
1524 7 static void walk_struct_node(pdfmake_textract_result_t *r,
1525 pdfmake_obj_t *node,
1526 uint32_t role_id,
1527 pdfmake_obj_t *page_dict,
1528 int depth)
1529 {
1530 size_t i;
1531 size_t n;
1532 pdfmake_obj_t *item;
1533 uint32_t type_key;
1534 pdfmake_obj_t *type_v;
1535 const char *type_name;
1536 uint32_t mcid_key;
1537 pdfmake_obj_t *mcid_v;
1538 uint32_t pg_key;
1539 pdfmake_obj_t *pg;
1540 pdfmake_reader_t *rd;
1541 pdfmake_obj_t *resolved;
1542 uint32_t s_key;
1543 pdfmake_obj_t *s_v;
1544 uint32_t this_role;
1545 uint32_t k_key;
1546 pdfmake_obj_t *k;
1547 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
1548  
1549 /* Follow indirect references */
1550 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
1551 3 pdfmake_reader_t *rd = (pdfmake_reader_t *)r->reader;
1552 3 ▶ if (!rd->parser) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!rd->parser) return; 0 3 3
1553 3 node = pdfmake_parser_resolve(rd->parser, node->as.ref);
1554 3 ▶ if (!node) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!node) return; 0 3 3
1555 }
1556  
1557 /* Integer leaf: this MCID belongs to role_id */
1558 7 ▶ if (node->kind == PDFMAKE_INT) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->kind == PDFMAKE_INT) { 3 4 7
1559 3 struct_map_push(r, (int32_t)node->as.i, role_id);
1560 3 return;
1561 }
1562  
1563 /* Array: recurse into each kid, keeping the current role_id */
1564 4 ▶ if (node->kind == PDFMAKE_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->kind == PDFMAKE_ARRAY) { 1 3 4
1565 1 n = pdfmake_array_len(node);
1566 4 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 1 4
1567 3 item = pdfmake_array_get(node, i);
1568 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
1569 }
1570 1 return;
1571 }
1572  
1573 3 ▶ if (node->kind != PDFMAKE_DICT) return;
Branch: 1 branch
BranchTrueFalseTotal
if (node->kind != PDFMAKE_DICT) return; 0 3 3
1574  
1575 /* MCR dict: { /Type /MCR, /Pg <page>, /MCID n } — ignore /Pg for now
1576 * since per-page extraction already limits us to one page's content. */
1577 3 type_key = pdfmake_arena_intern_name(r->arena, "Type", 4);
1578 3 type_v = pdfmake_dict_get(node, type_key);
1579 3 type_name = NULL;
1580 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
1581 3 type_name = pdfmake_get_name_bytes(r->arena, type_v);
1582 }
1583  
1584 3 mcid_key = pdfmake_arena_intern_name(r->arena, "MCID", 4);
1585 3 mcid_v = pdfmake_dict_get(node, mcid_key);
1586  
1587 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
1588 0 pg_key = pdfmake_arena_intern_name(r->arena, "Pg", 2);
1589 0 pg = pdfmake_dict_get(node, pg_key);
1590 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
1591 /* Skip MCRs that target a different page */
1592 0 rd = (pdfmake_reader_t *)r->reader;
1593 0 resolved = rd->parser
1594 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
1595 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
1596 }
1597 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
1598 0 struct_map_push(r, (int32_t)mcid_v->as.i, role_id);
1599 }
1600 0 return;
1601 }
1602  
1603 /* OBJR (object reference to an annotation) — no MCID, skip */
1604 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
1605  
1606 /* StructElem: adopt its /S as the current role, then recurse into /K */
1607 3 s_key = pdfmake_arena_intern_name(r->arena, "S", 1);
1608 3 s_v = pdfmake_dict_get(node, s_key);
1609 3 this_role = role_id;
1610 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
1611  
1612 /* /Pg filter: if this StructElem binds a specific page and it doesn't
1613 * match ours, we can still recurse — children may override /Pg. */
1614 3 k_key = pdfmake_arena_intern_name(r->arena, "K", 1);
1615 3 k = pdfmake_dict_get(node, k_key);
1616 3 ▶ if (!k) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!k) return; 0 3 3
1617  
1618 /* /K can be an integer (MCID), a dict (StructElem/MCR/OBJR), a ref, or
1619 * an array of any of the above. walk_struct_node handles all cases. */
1620 3 walk_struct_node(r, k, this_role, page_dict, depth + 1);
1621 }
1622  
1623 1 pdfmake_err_t pdfmake_textract_resolve_struct_tree(
1624 pdfmake_textract_result_t *result,
1625 pdfmake_obj_t *struct_root,
1626 pdfmake_obj_t *page_dict)
1627 {
1628 pdfmake_reader_t *rd;
1629 uint32_t k_key;
1630 pdfmake_obj_t *k;
1631 1 ▶ if (!result) return PDFMAKE_EINVAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return PDFMAKE_EINVAL; 0 1 1
1632 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
1633  
1634 /* Follow an indirect reference to the StructTreeRoot dict */
1635 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
1636 1 rd = (pdfmake_reader_t *)result->reader;
1637 1 ▶ if (rd->parser) {
Branch: 1 branch
BranchTrueFalseTotal
if (rd->parser) { 1 0 1
1638 1 struct_root = pdfmake_parser_resolve(rd->parser, struct_root->as.ref);
1639 1 ▶ if (!struct_root) return PDFMAKE_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (!struct_root) return PDFMAKE_OK; 0 1 1
1640 }
1641 }
1642 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
1643  
1644 /* Start at /K of the StructTreeRoot; role 0 means "none" (children will
1645 * overwrite as soon as they hit a StructElem with /S). */
1646 1 k_key = pdfmake_arena_intern_name(result->arena, "K", 1);
1647 1 k = pdfmake_dict_get(struct_root, k_key);
1648 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
1649  
1650 1 return PDFMAKE_OK;
1651 }
1652  
1653 5 uint32_t pdfmake_textract_role_for_mcid(
1654 const pdfmake_textract_result_t *result, int32_t mcid)
1655 {
1656 size_t i;
1657 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
1658 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
1659 11 ▶ if (result->struct_map[i].mcid == mcid) {
Branch: 1 branch
BranchTrueFalseTotal
if (result->struct_map[i].mcid == mcid) { 5 6 11
1660 5 return result->struct_map[i].role_id;
1661 }
1662 }
1663 0 return 0;
1664 }
1665  
1666 /*============================================================================
1667 * Phase 13 — Annotation + form field text extraction
1668 *==========================================================================*/
1669  
1670 /* Decode a PDF "text string" (§7.9.2) to UTF-8, arena-allocated.
1671 * Handles UTF-16BE (with BOM FE FF) and UTF-8-with-BOM (EF BB BF); everything
1672 * else is treated as PDFDocEncoding, which matches ISO-8859-1 for the ASCII
1673 * subset used by every real-world annotation we've seen. */
1674 8 static const char *decode_pdf_text(pdfmake_arena_t *arena,
1675 const uint8_t *b, size_t n)
1676 {
1677 size_t i;
1678 pdfmake_buf_t out;
1679 char *s;
1680 uint32_t cp;
1681 uint32_t lo;
1682 uint8_t c;
1683 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
1684  
1685 /* UTF-16BE BOM */
1686 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
1687 0 pdfmake_buf_init(&out);
1688 0 s = NULL;
1689 0 ▶ for (i = 2; i + 1 < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i + 1 < n; i += 2) { 0 0 0
1690 0 cp = ((uint32_t)b[i] << 8) | b[i + 1];
1691 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
1692 0 lo = ((uint32_t)b[i + 2] << 8) | b[i + 3];
1693 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
1694 0 cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00);
1695 0 i += 2;
1696 }
1697 }
1698 0 ▶ if (cp < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x80) { 0 0 0
1699 0 pdfmake_buf_append_byte(&out, (uint8_t)cp);
1700 0 ▶ } else if (cp < 0x800) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cp < 0x800) { 0 0 0
1701 0 pdfmake_buf_append_byte(&out, 0xC0 | (cp >> 6));
1702 0 pdfmake_buf_append_byte(&out, 0x80 | (cp & 0x3F));
1703 0 ▶ } else if (cp < 0x10000) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cp < 0x10000) { 0 0 0
1704 0 pdfmake_buf_append_byte(&out, 0xE0 | (cp >> 12));
1705 0 pdfmake_buf_append_byte(&out, 0x80 | ((cp >> 6) & 0x3F));
1706 0 pdfmake_buf_append_byte(&out, 0x80 | (cp & 0x3F));
1707 } else {
1708 0 pdfmake_buf_append_byte(&out, 0xF0 | (cp >> 18));
1709 0 pdfmake_buf_append_byte(&out, 0x80 | ((cp >> 12) & 0x3F));
1710 0 pdfmake_buf_append_byte(&out, 0x80 | ((cp >> 6) & 0x3F));
1711 0 pdfmake_buf_append_byte(&out, 0x80 | (cp & 0x3F));
1712 }
1713 }
1714 0 s = pdfmake_arena_alloc(arena, out.len + 1);
1715 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
1716 0 pdfmake_buf_free(&out);
1717 0 return s;
1718 }
1719  
1720 /* UTF-8 with BOM (PDF 2.0) */
1721 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
1722 0 b += 3; n -= 3;
1723 }
1724  
1725 /* PDFDocEncoding → promote high-bit bytes to 2-byte UTF-8. */
1726 8 pdfmake_buf_init(&out);
1727 106 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 98 8 106
1728 98 c = b[i];
1729 98 ▶ if (c < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { 98 0 98
1730 98 pdfmake_buf_append_byte(&out, c);
1731 } else {
1732 0 pdfmake_buf_append_byte(&out, 0xC0 | (c >> 6));
1733 0 pdfmake_buf_append_byte(&out, 0x80 | (c & 0x3F));
1734 }
1735 }
1736 8 s = pdfmake_arena_alloc(arena, out.len + 1);
1737 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
1738 8 pdfmake_buf_free(&out);
1739 8 return s;
1740 }
1741  
1742 /* Pull a PDF text string out of a dict (resolving one level of indirection).
1743 * Returns NULL if the key is missing or not a string. */
1744 8 static const char *dict_get_text(pdfmake_reader_t *rd,
1745 pdfmake_arena_t *arena,
1746 pdfmake_obj_t *dict,
1747 const char *name)
1748 {
1749 uint32_t key;
1750 pdfmake_obj_t *v;
1751 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
1752 8 key = pdfmake_arena_intern_name(arena, name, strlen(name));
1753 8 v = pdfmake_dict_get(dict, key);
1754 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
1755 0 v = pdfmake_parser_resolve(rd->parser, v->as.ref);
1756 }
1757 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
1758 7 return decode_pdf_text(arena, v->as.str.bytes, v->as.str.len);
1759 }
1760  
1761 /* Resolve a dict entry that's allowed to be an indirect reference. */
1762 1 static pdfmake_obj_t *dict_get_resolved(pdfmake_reader_t *rd,
1763 pdfmake_arena_t *arena,
1764 pdfmake_obj_t *dict,
1765 const char *name)
1766 {
1767 uint32_t key;
1768 pdfmake_obj_t *v;
1769 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
1770 1 key = pdfmake_arena_intern_name(arena, name, strlen(name));
1771 1 v = pdfmake_dict_get(dict, key);
1772 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
1773 1 v = pdfmake_parser_resolve(rd->parser, v->as.ref);
1774 }
1775 1 return v;
1776 }
1777  
1778 2 pdfmake_annot_text_list_t *pdfmake_annot_text_list_new(pdfmake_arena_t *arena) {
1779 2 pdfmake_annot_text_list_t *l = calloc(1, sizeof(*l));
1780 2 ▶ if (l) l->arena = arena;
Branch: 1 branch
BranchTrueFalseTotal
if (l) l->arena = arena; 2 0 2
1781 2 return l;
1782 }
1783  
1784 2 void pdfmake_annot_text_list_free(pdfmake_annot_text_list_t *list) {
1785 2 ▶ if (!list) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!list) return; 0 2 2
1786 2 free(list->items);
1787 2 free(list);
1788 }
1789  
1790 3 static int annot_list_push(pdfmake_annot_text_list_t *l,
1791 const pdfmake_annot_text_t *rec)
1792 {
1793 3 ▶ if (l->len >= l->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (l->len >= l->cap) { 1 2 3
1794 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
1795 1 pdfmake_annot_text_t *n = realloc(l->items, ncap * sizeof(*n));
1796 1 ▶ if (!n) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return 0; 0 1 1
1797 1 l->items = n; l->cap = ncap;
1798 }
1799 3 l->items[l->len++] = *rec;
1800 3 return 1;
1801 }
1802  
1803 /* Pull a 4-element numeric array (Rect / FreeText BBox) into out[4].
1804 * Zeros the array if the entry is missing or malformed. */
1805 3 static void dict_get_rect(pdfmake_obj_t *dict, uint32_t key, double out[4]) {
1806 int i;
1807 pdfmake_obj_t *v;
1808 pdfmake_obj_t *n;
1809 3 out[0] = out[1] = out[2] = out[3] = 0;
1810 3 v = pdfmake_dict_get(dict, key);
1811 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
1812 3 ▶ if (pdfmake_array_len(v) < 4) return;
Branch: 1 branch
BranchTrueFalseTotal
if (pdfmake_array_len(v) < 4) return; 0 3 3
1813 15 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 12 3 15
1814 12 n = pdfmake_array_get(v, i);
1815 12 ▶ if (n) out[i] = pdfmake_get_number(n);
Branch: 1 branch
BranchTrueFalseTotal
if (n) out[i] = pdfmake_get_number(n); 12 0 12
1816 }
1817 }
1818  
1819 /* Walk one page's /Annots array. */
1820 2 static void collect_page_annots(pdfmake_reader_t *rd,
1821 pdfmake_annot_text_list_t *out,
1822 pdfmake_reader_page_t *page,
1823 size_t page_index)
1824 {
1825 size_t i;
1826 pdfmake_arena_t *arena;
1827 uint32_t annots_key;
1828 pdfmake_obj_t *annots;
1829 uint32_t rect_key;
1830 size_t n;
1831 pdfmake_obj_t *a;
1832 uint32_t sub_key;
1833 pdfmake_obj_t *sub;
1834 const char *kind;
1835 const char *nm;
1836 const char *contents;
1837 const char *author;
1838 const char *subject;
1839 pdfmake_annot_text_t rec;
1840 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
1841 2 arena = rd->parser->doc->arena;
1842  
1843 2 annots_key = pdfmake_arena_intern_name(arena, "Annots", 6);
1844 2 annots = pdfmake_dict_get(page->page_dict, annots_key);
1845 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
1846 0 annots = pdfmake_parser_resolve(rd->parser, annots->as.ref);
1847 }
1848 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
1849  
1850 1 rect_key = pdfmake_arena_intern_name(arena, "Rect", 4);
1851 1 n = pdfmake_array_len(annots);
1852 4 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 1 4
1853 3 a = pdfmake_array_get(annots, i);
1854 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
1855 3 a = pdfmake_parser_resolve(rd->parser, a->as.ref);
1856 }
1857 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
1858  
1859 /* Subtype decides the kind label; we surface every textual annot. */
1860 3 sub_key = pdfmake_arena_intern_name(arena, "Subtype", 7);
1861 3 sub = pdfmake_dict_get(a, sub_key);
1862 3 kind = "Annot";
1863 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
1864 3 nm = pdfmake_get_name_bytes(arena, sub);
1865 3 ▶ if (nm) kind = nm;
Branch: 1 branch
BranchTrueFalseTotal
if (nm) kind = nm; 3 0 3
1866 }
1867  
1868 /* Skip Link/Widget/PrinterMark etc. that never carry user text.
1869 * Widgets are surfaced via the AcroForm walker instead. */
1870 3 ▶ if (strcmp(kind, "Link") == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(kind, "Link") == 0 || 3 0 3
1871 3 ▶ strcmp(kind, "Widget") == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
strcmp(kind, "Widget") == 0 || 2 1 3
1872 2 ▶ strcmp(kind, "PrinterMark") == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
strcmp(kind, "PrinterMark") == 0 || 2 0 2
1873 2 ▶ strcmp(kind, "TrapNet") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
strcmp(kind, "TrapNet") == 0) { 0 2 2
1874 1 continue;
1875 }
1876  
1877 2 contents = dict_get_text(rd, arena, a, "Contents");
1878 2 author = dict_get_text(rd, arena, a, "T");
1879 2 subject = dict_get_text(rd, arena, a, "Subj");
1880  
1881 /* Even with no /Contents we emit the record if there's author or
1882 * subject text — a Popup without Contents still tells you who
1883 * highlighted what. */
1884 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
1885  
1886 2 memset(&rec, 0, sizeof(rec));
1887 2 rec.kind = pdfmake_arena_strdup(arena, kind);
1888 2 rec.page_index = page_index;
1889 2 dict_get_rect(a, rect_key, rec.rect);
1890 2 ▶ rec.text = contents ? contents : "";
Branch: 1 branch
BranchTrueFalseTotal
rec.text = contents ? contents : ""; 2 0 2
1891 2 rec.author = author;
1892 2 rec.subject = subject;
1893 2 annot_list_push(out, &rec);
1894 }
1895 }
1896  
1897 /* Recursively walk /AcroForm /Fields; /Kids trees inherit qualified names
1898 * via dot-joining (§12.7.3.2). */
1899 1 static void collect_form_fields(pdfmake_reader_t *rd,
1900 pdfmake_annot_text_list_t *out,
1901 pdfmake_obj_t *field,
1902 const char *parent_name,
1903 int depth)
1904 {
1905 size_t i;
1906 size_t pi;
1907 pdfmake_arena_t *arena;
1908 const char *t;
1909 const char *full_name;
1910 size_t pl;
1911 size_t tl;
1912 char *buf;
1913 uint32_t v_key;
1914 pdfmake_obj_t *v;
1915 const char *value;
1916 const char *nm;
1917 const char *tooltip;
1918 pdfmake_annot_text_t rec;
1919 uint32_t rect_key;
1920 pdfmake_obj_t *p;
1921 uint32_t kids_key;
1922 pdfmake_obj_t *kids;
1923 size_t n;
1924 pdfmake_obj_t *kid;
1925 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
1926 1 arena = rd->parser->doc->arena;
1927 1 ▶ if (field->kind == PDFMAKE_REF) {
Branch: 1 branch
BranchTrueFalseTotal
if (field->kind == PDFMAKE_REF) { 1 0 1
1928 1 field = pdfmake_parser_resolve(rd->parser, field->as.ref);
1929 }
1930 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
1931  
1932 /* Build fully qualified name = parent.T joined with this.T */
1933 1 t = dict_get_text(rd, arena, field, "T");
1934 1 full_name = t;
1935 1 ▶ if (parent_name && t) {
Branch: 2 branches
BranchTrueFalseTotal
if (parent_name && t) { 0 1 1
if (parent_name && t) { 0 0 0
1936 0 pl = strlen(parent_name);
1937 0 tl = strlen(t);
1938 0 buf = pdfmake_arena_alloc(arena, pl + 1 + tl + 1);
1939 0 ▶ if (buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (buf) { 0 0 0
1940 0 memcpy(buf, parent_name, pl);
1941 0 buf[pl] = '.';
1942 0 memcpy(buf + pl + 1, t, tl);
1943 0 buf[pl + 1 + tl] = 0;
1944 0 full_name = buf;
1945 }
1946 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
1947 0 full_name = parent_name;
1948 }
1949  
1950 /* Emit a record for leaf fields that carry a /V (value). /V can be a
1951 * string (text, choice) or a name (button state). Skip anything else. */
1952 1 v_key = pdfmake_arena_intern_name(arena, "V", 1);
1953 1 v = pdfmake_dict_get(field, v_key);
1954 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
1955 0 v = pdfmake_parser_resolve(rd->parser, v->as.ref);
1956 }
1957 1 value = NULL;
1958 1 ▶ if (v) {
Branch: 1 branch
BranchTrueFalseTotal
if (v) { 1 0 1
1959 1 ▶ if (v->kind == PDFMAKE_STR) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->kind == PDFMAKE_STR) { 1 0 1
1960 1 value = decode_pdf_text(arena, v->as.str.bytes, v->as.str.len);
1961 0 ▶ } else if (v->kind == PDFMAKE_NAME) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (v->kind == PDFMAKE_NAME) { 0 0 0
1962 0 nm = pdfmake_get_name_bytes(arena, v);
1963 0 ▶ if (nm) value = pdfmake_arena_strdup(arena, nm);
Branch: 1 branch
BranchTrueFalseTotal
if (nm) value = pdfmake_arena_strdup(arena, nm); 0 0 0
1964 }
1965 }
1966  
1967 1 tooltip = dict_get_text(rd, arena, field, "TU");
1968  
1969 /* Fields may be pure intermediaries (have /Kids but no /V/T/TU). We
1970 * only emit when there's something to surface. */
1971 1 ▶ if (value || tooltip) {
Branch: 2 branches
BranchTrueFalseTotal
if (value || tooltip) { 0 1 1
if (value || tooltip) { 0 0 0
1972 1 memset(&rec, 0, sizeof(rec));
1973 1 rec.kind = "FormField";
1974 1 rec.page_index = (size_t)-1; /* not page-anchored by default */
1975 1 ▶ rec.text = value ? value : "";
Branch: 1 branch
BranchTrueFalseTotal
rec.text = value ? value : ""; 1 0 1
1976 1 rec.subject = tooltip;
1977 1 rec.field_name = full_name;
1978  
1979 /* If this field is a single-widget field (/Subtype /Widget) it
1980 * carries a /Rect; otherwise rect stays zero. */
1981 1 rect_key = pdfmake_arena_intern_name(arena, "Rect", 4);
1982 1 dict_get_rect(field, rect_key, rec.rect);
1983  
1984 /* /P may point at the owning page — use it to stamp page_index. */
1985 1 p = dict_get_resolved(rd, arena, field, "P");
1986 1 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 1 0 1
1987 1 ▶ for (pi = 0; pi < rd->page_count; pi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (pi = 0; pi < rd->page_count; pi++) { 1 0 1
1988 1 ▶ if (rd->pages[pi].page_dict == p) {
Branch: 1 branch
BranchTrueFalseTotal
if (rd->pages[pi].page_dict == p) { 1 0 1
1989 1 rec.page_index = pi;
1990 1 break;
1991 }
1992 }
1993 }
1994  
1995 1 annot_list_push(out, &rec);
1996 }
1997  
1998 1 kids_key = pdfmake_arena_intern_name(arena, "Kids", 4);
1999 1 kids = pdfmake_dict_get(field, kids_key);
2000 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
2001 0 kids = pdfmake_parser_resolve(rd->parser, kids->as.ref);
2002 }
2003 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
2004 0 n = pdfmake_array_len(kids);
2005 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
2006 0 kid = pdfmake_array_get(kids, i);
2007 0 collect_form_fields(rd, out, kid, full_name, depth + 1);
2008 }
2009 }
2010 }
2011  
2012 2 pdfmake_err_t pdfmake_textract_annotations(
2013 pdfmake_reader_t *reader,
2014 pdfmake_annot_text_list_t *out)
2015 {
2016 size_t i;
2017 pdfmake_arena_t *arena;
2018 uint32_t af_key;
2019 pdfmake_obj_t *af;
2020 uint32_t fields_key;
2021 pdfmake_obj_t *fields;
2022 size_t n;
2023 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
2024 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
2025  
2026 2 arena = reader->parser->doc->arena;
2027  
2028 /* Per-page /Annots */
2029 4 ▶ for (i = 0; i < reader->page_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < reader->page_count; i++) { 2 2 4
2030 2 collect_page_annots(reader, out, &reader->pages[i], i);
2031 }
2032  
2033 /* Document-level /AcroForm /Fields */
2034 2 ▶ if (reader->catalog) {
Branch: 1 branch
BranchTrueFalseTotal
if (reader->catalog) { 2 0 2
2035 2 af_key = pdfmake_arena_intern_name(arena, "AcroForm", 8);
2036 2 af = pdfmake_dict_get(reader->catalog, af_key);
2037 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
2038 1 af = pdfmake_parser_resolve(reader->parser, af->as.ref);
2039 }
2040 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
2041 1 fields_key = pdfmake_arena_intern_name(arena, "Fields", 6);
2042 1 fields = pdfmake_dict_get(af, fields_key);
2043 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
2044 0 fields = pdfmake_parser_resolve(reader->parser, fields->as.ref);
2045 }
2046 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
2047 1 n = pdfmake_array_len(fields);
2048 2 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 1 2
2049 1 collect_form_fields(reader, out,
2050 pdfmake_array_get(fields, i), NULL, 0);
2051 }
2052 }
2053 }
2054 }
2055  
2056 2 return PDFMAKE_OK;
2057 }
2058  
2059 /* Forward declaration — definition appears later under UTF-8 output. */
2060 static size_t encode_utf8(uint32_t cp, char *buf);
2061  
2062 /*============================================================================
2063 * Phase 15 — Table detection
2064 *
2065 * A page's words already come out sorted into rows via the aggregator, so
2066 * detection reduces to finding a maximal run of contiguous rows whose
2067 * column x-positions align within a small tolerance. Cells are then
2068 * populated by assigning each word to the column whose center it's closest
2069 * to, and joining the texts of any words that land in the same cell.
2070 *==========================================================================*/
2071  
2072 2 pdfmake_textract_table_opts_t pdfmake_textract_table_default_opts(void) {
2073 pdfmake_textract_table_opts_t o;
2074 2 o.min_rows = 3;
2075 2 o.min_cols = 2;
2076 2 o.x_tolerance = 5.0;
2077 2 o.row_tolerance = 0.5;
2078 2 return o;
2079 }
2080  
2081 2 pdfmake_textract_table_list_t *pdfmake_textract_table_list_new(pdfmake_arena_t *arena) {
2082 2 pdfmake_textract_table_list_t *l = calloc(1, sizeof(*l));
2083 2 ▶ if (l) l->arena = arena;
Branch: 1 branch
BranchTrueFalseTotal
if (l) l->arena = arena; 2 0 2
2084 2 return l;
2085 }
2086  
2087 2 void pdfmake_textract_table_list_free(pdfmake_textract_table_list_t *list) {
2088 size_t i;
2089 2 ▶ if (!list) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!list) return; 0 2 2
2090 3 ▶ for (i = 0; i < list->len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < list->len; i++) { 1 2 3
2091 1 free(list->items[i].cells);
2092 1 free(list->items[i].cell_x0);
2093 1 free(list->items[i].cell_y0);
2094 1 free(list->items[i].cell_x1);
2095 1 free(list->items[i].cell_y1);
2096 }
2097 2 free(list->items);
2098 2 free(list);
2099 }
2100  
2101 /* A logical table row built from raw glyphs. Each entry is one cell
2102 * (= a group of contiguous glyphs with no large x-gap between them). */
2103 typedef struct {
2104 double x0, x1; /* cell horizontal extent */
2105 double y0, y1; /* cell vertical extent */
2106 char *text; /* arena-owned UTF-8 */
2107 } tcell_t;
2108  
2109 typedef struct {
2110 tcell_t *cells;
2111 size_t ncells;
2112 size_t cap;
2113 double baseline;
2114 double font_size;
2115 } trow_t;
2116  
2117 17 static void trow_push(trow_t *r, const tcell_t *c) {
2118 17 ▶ if (r->ncells >= r->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->ncells >= r->cap) { 9 8 17
2119 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
2120 9 tcell_t *n = realloc(r->cells, nc * sizeof(*n));
2121 9 ▶ if (!n) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return; 0 9 9
2122 9 r->cells = n; r->cap = nc;
2123 }
2124 17 r->cells[r->ncells++] = *c;
2125 }
2126  
2127 /* Sort raw glyphs by y desc, then x asc — independent of the column-split
2128 * based sort used by the main aggregator. */
2129 591 static int cmp_glyph_row_major(const void *a, const void *b) {
2130 591 const pdfmake_text_glyph_t *ga = a;
2131 591 const pdfmake_text_glyph_t *gb = b;
2132 double dy;
2133 double tol;
2134 double dx;
2135 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
2136 591 dy = gb->y0 - ga->y0;
2137 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
2138 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
2139 389 dx = ga->x0 - gb->x0;
2140 389 ▶ if (dx < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (dx < 0) return -1; 389 0 389
2141 0 ▶ if (dx > 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (dx > 0) return 1; 0 0 0
2142 0 return 0;
2143 }
2144  
2145 2 pdfmake_err_t pdfmake_textract_detect_tables(
2146 const pdfmake_textract_result_t *result,
2147 const pdfmake_textract_table_opts_t *options,
2148 pdfmake_textract_table_list_t *out)
2149 {
2150 size_t i;
2151 size_t j;
2152 size_t k;
2153 size_t r;
2154 size_t c;
2155 size_t m;
2156 pdfmake_textract_table_opts_t opts;
2157 pdfmake_text_glyph_t *glyphs;
2158 size_t row_cap;
2159 size_t nrows;
2160 trow_t *rows;
2161 size_t gi;
2162 double ry;
2163 double rfs;
2164 size_t rend;
2165 size_t nc;
2166 trow_t *tmp;
2167 trow_t *rrow;
2168 size_t cstart;
2169 int split;
2170 double gap;
2171 tcell_t cell;
2172 pdfmake_buf_t tb;
2173 char utf8_tmp[4];
2174 size_t n;
2175 size_t ncols;
2176 int ok;
2177 double dx;
2178 size_t run;
2179 pdfmake_textract_table_t t;
2180 const trow_t *rp;
2181 const tcell_t *cellp;
2182 size_t idx;
2183 pdfmake_textract_table_t *nt;
2184 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
2185 2 ▶ opts = options ? *options : pdfmake_textract_table_default_opts();
Branch: 1 branch
BranchTrueFalseTotal
opts = options ? *options : pdfmake_textract_table_default_opts(); 0 2 2
2186 2 ▶ if (result->raw_len == 0) return PDFMAKE_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (result->raw_len == 0) return PDFMAKE_OK; 0 2 2
2187  
2188 /* Work on a private copy of the raw glyphs so we can re-sort without
2189 * touching the main extraction state (which was sorted by the Phase 10
2190 * column-aware comparator that would split rows across columns). */
2191 2 glyphs = malloc(result->raw_len * sizeof(*glyphs));
2192 2 ▶ if (!glyphs) return PDFMAKE_ENOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!glyphs) return PDFMAKE_ENOMEM; 0 2 2
2193 2 memcpy(glyphs, result->raw_glyphs, result->raw_len * sizeof(*glyphs));
2194 2 qsort(glyphs, result->raw_len, sizeof(*glyphs), cmp_glyph_row_major);
2195  
2196 /* 1) Cluster glyphs into rows by y. Within a row, group consecutive
2197 * glyphs into cells split on "big" horizontal gaps (≥ 1.5 em).
2198 * That keeps "Apple" together while splitting it from "12" sitting
2199 * at the next column origin. */
2200 2 row_cap = 64;
2201 2 nrows = 0;
2202 2 rows = calloc(row_cap, sizeof(trow_t));
2203 2 ▶ if (!rows) { free(glyphs); return PDFMAKE_ENOMEM; }
Branch: 1 branch
BranchTrueFalseTotal
if (!rows) { free(glyphs); return PDFMAKE_ENOMEM; } 0 2 2
2204  
2205 2 gi = 0;
2206 11 ▶ while (gi < result->raw_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (gi < result->raw_len) { 9 2 11
2207 9 ry = glyphs[gi].y0;
2208 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
2209 9 ▶ if (glyphs[gi].vertical) { gi++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (glyphs[gi].vertical) { gi++; continue; } 0 9 9
2210  
2211 /* Rows go until baseline jump > row_tolerance × fs. */
2212 9 rend = gi + 1;
2213 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
2214 174 ▶ fabs(glyphs[rend].y0 - ry) <= rfs * opts.row_tolerance) {
Branch: 1 branch
BranchTrueFalseTotal
fabs(glyphs[rend].y0 - ry) <= rfs * opts.row_tolerance) { 167 7 174
2215 167 rend++;
2216 }
2217  
2218 /* Grow rows */
2219 9 ▶ if (nrows >= row_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (nrows >= row_cap) { 0 9 9
2220 0 nc = row_cap * 2;
2221 0 tmp = realloc(rows, nc * sizeof(trow_t));
2222 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
2223 0 rows = tmp;
2224 0 memset(rows + row_cap, 0, (nc - row_cap) * sizeof(trow_t));
2225 0 row_cap = nc;
2226 }
2227 9 rrow = &rows[nrows++];
2228 9 rrow->baseline = ry;
2229 9 rrow->font_size = rfs;
2230  
2231 /* Walk glyphs[gi..rend-1] and split into cells on large x gaps. */
2232 9 cstart = gi;
2233 185 ▶ for (k = gi + 1; k <= rend; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = gi + 1; k <= rend; k++) { 176 9 185
2234 176 split = (k == rend);
2235 176 ▶ if (!split) {
Branch: 1 branch
BranchTrueFalseTotal
if (!split) { 167 9 176
2236 167 gap = glyphs[k].x0 - glyphs[k - 1].x1;
2237 167 ▶ if (gap > 1.5 * rfs) split = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (gap > 1.5 * rfs) split = 1; 8 159 167
2238 }
2239 176 ▶ if (split) {
Branch: 1 branch
BranchTrueFalseTotal
if (split) { 17 159 176
2240 /* Emit a cell from cstart..k-1 */
2241 17 memset(&cell, 0, sizeof(cell));
2242 17 pdfmake_buf_init(&tb);
2243 17 cell.x0 = glyphs[cstart].x0;
2244 17 cell.x1 = glyphs[k - 1].x1;
2245 17 cell.y0 = glyphs[cstart].y0;
2246 17 cell.y1 = glyphs[cstart].y1;
2247 193 ▶ for (j = cstart; j < k; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = cstart; j < k; j++) { 176 17 193
2248 176 n = encode_utf8(glyphs[j].unicode, utf8_tmp);
2249 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
2250 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
2251 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
2252 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
2253 352 ▶ for (m = 0; m < n; m++)
Branch: 1 branch
BranchTrueFalseTotal
for (m = 0; m < n; m++) 176 176 352
2254 176 pdfmake_buf_append_byte(&tb, utf8_tmp[m]);
2255 }
2256 17 cell.text = pdfmake_arena_alloc(out->arena, tb.len + 1);
2257 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
2258 17 pdfmake_buf_free(&tb);
2259 17 trow_push(rrow, &cell);
2260 17 cstart = k;
2261 }
2262 }
2263 9 gi = rend;
2264 }
2265  
2266 /* 2) Find maximal runs of consecutive rows with matching column layout.
2267 * "Same layout" = same cell count AND every cell-origin within
2268 * opts.x_tolerance. */
2269 2 i = 0;
2270 8 ▶ while (i < nrows) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < nrows) { 6 2 8
2271 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
2272 1 ncols = rows[i].ncells;
2273  
2274 1 j = i + 1;
2275 4 ▶ while (j < nrows) {
Branch: 1 branch
BranchTrueFalseTotal
while (j < nrows) { 4 0 4
2276 4 ▶ if (rows[j].ncells != ncols) break;
Branch: 1 branch
BranchTrueFalseTotal
if (rows[j].ncells != ncols) break; 1 3 4
2277 3 ok = 1;
2278 12 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 9 3 12
2279 9 dx = rows[i].cells[c].x0 - rows[j].cells[c].x0;
2280 9 ▶ if (dx < 0) dx = -dx;
Branch: 1 branch
BranchTrueFalseTotal
if (dx < 0) dx = -dx; 0 9 9
2281 9 ▶ if (dx > opts.x_tolerance) { ok = 0; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (dx > opts.x_tolerance) { ok = 0; break; } 0 9 9
2282 }
2283 3 ▶ if (!ok) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) break; 0 3 3
2284 3 j++;
2285 }
2286  
2287 1 run = j - i;
2288 1 ▶ if (run < opts.min_rows) { i = j; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (run < opts.min_rows) { i = j; continue; } 0 1 1
2289  
2290 /* 3) Emit the table. */
2291 1 memset(&t, 0, sizeof(t));
2292 1 t.rows = run;
2293 1 t.cols = ncols;
2294 1 t.cells = calloc(run * ncols, sizeof(*t.cells));
2295 1 t.cell_x0 = calloc(run * ncols, sizeof(double));
2296 1 t.cell_y0 = calloc(run * ncols, sizeof(double));
2297 1 t.cell_x1 = calloc(run * ncols, sizeof(double));
2298 1 t.cell_y1 = calloc(run * ncols, sizeof(double));
2299 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
2300 0 free(t.cells); free(t.cell_x0); free(t.cell_y0);
2301 0 free(t.cell_x1); free(t.cell_y1);
2302 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
2303 0 free(rows); free(glyphs);
2304 0 return PDFMAKE_ENOMEM;
2305 }
2306  
2307 1 t.x0 = t.y0 = 1e18; t.x1 = t.y1 = -1e18;
2308 5 ▶ for (r = 0; r < run; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < run; r++) { 4 1 5
2309 4 rp = &rows[i + r];
2310 16 ▶ for (c = 0; c < ncols; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < ncols; c++) { 12 4 16
2311 12 cellp = &rp->cells[c];
2312 12 idx = r * ncols + c;
2313 12 ▶ t.cells[idx] = cellp->text ? cellp->text : "";
Branch: 1 branch
BranchTrueFalseTotal
t.cells[idx] = cellp->text ? cellp->text : ""; 12 0 12
2314 12 t.cell_x0[idx] = cellp->x0;
2315 12 t.cell_y0[idx] = cellp->y0;
2316 12 t.cell_x1[idx] = cellp->x1;
2317 12 t.cell_y1[idx] = cellp->y1;
2318 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
2319 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
2320 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
2321 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
2322 }
2323 }
2324  
2325 1 ▶ if (out->len >= out->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (out->len >= out->cap) { 1 0 1
2326 1 ▶ nc = out->cap == 0 ? 4 : out->cap * 2;
Branch: 1 branch
BranchTrueFalseTotal
nc = out->cap == 0 ? 4 : out->cap * 2; 0 1 1
2327 1 nt = realloc(out->items, nc * sizeof(*nt));
2328 1 ▶ if (!nt) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nt) { 0 1 1
2329 0 free(t.cells); free(t.cell_x0); free(t.cell_y0);
2330 0 free(t.cell_x1); free(t.cell_y1);
2331 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
2332 0 free(rows); free(glyphs);
2333 0 return PDFMAKE_ENOMEM;
2334 }
2335 1 out->items = nt; out->cap = nc;
2336 }
2337 1 out->items[out->len++] = t;
2338  
2339 1 i = j;
2340 }
2341  
2342 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
2343 2 free(rows);
2344 2 free(glyphs);
2345 2 return PDFMAKE_OK;
2346 }
2347  
2348 /*============================================================================
2349 * UTF-8 output
2350 *==========================================================================*/
2351  
2352 /* Encode a single Unicode codepoint to UTF-8, return bytes written */
2353 1158 static size_t encode_utf8(uint32_t cp, char *buf) {
2354 1158 ▶ if (cp < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp < 0x80) { 1152 6 1158
2355 1152 buf[0] = (char)cp;
2356 1152 return 1;
2357 6 ▶ } else if (cp < 0x800) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cp < 0x800) { 0 6 6
2358 0 buf[0] = (char)(0xC0 | (cp >> 6));
2359 0 buf[1] = (char)(0x80 | (cp & 0x3F));
2360 0 return 2;
2361 6 ▶ } else if (cp < 0x10000) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cp < 0x10000) { 6 0 6
2362 6 buf[0] = (char)(0xE0 | (cp >> 12));
2363 6 buf[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
2364 6 buf[2] = (char)(0x80 | (cp & 0x3F));
2365 6 return 3;
2366 0 ▶ } else if (cp < 0x110000) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (cp < 0x110000) { 0 0 0
2367 0 buf[0] = (char)(0xF0 | (cp >> 18));
2368 0 buf[1] = (char)(0x80 | ((cp >> 12) & 0x3F));
2369 0 buf[2] = (char)(0x80 | ((cp >> 6) & 0x3F));
2370 0 buf[3] = (char)(0x80 | (cp & 0x3F));
2371 0 return 4;
2372 }
2373 0 return 0;
2374 }
2375  
2376 4 size_t pdfmake_textract_to_utf8(
2377 const pdfmake_textract_result_t *result,
2378 char *buf, size_t buf_cap)
2379 {
2380 size_t bi;
2381 size_t li;
2382 size_t wi;
2383 size_t gi;
2384 size_t k;
2385 size_t written;
2386 const pdfmake_text_block_t *block;
2387 const pdfmake_text_line_t *line;
2388 const pdfmake_text_word_t *word;
2389 size_t n;
2390 char tmp[4];
2391 4 ▶ if (!result) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!result) return 0; 0 4 4
2392  
2393 4 written = 0;
2394  
2395 19 ▶ for (bi = 0; bi < result->len; bi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (bi = 0; bi < result->len; bi++) { 15 4 19
2396 15 block = &result->blocks[bi];
2397 37 ▶ for (li = 0; li < block->len; li++) {
Branch: 1 branch
BranchTrueFalseTotal
for (li = 0; li < block->len; li++) { 22 15 37
2398 22 line = &block->lines[li];
2399 216 ▶ for (wi = 0; wi < line->len; wi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (wi = 0; wi < line->len; wi++) { 194 22 216
2400 194 word = &line->words[wi];
2401 194 ▶ if (wi > 0 && written < buf_cap) {
Branch: 2 branches
BranchTrueFalseTotal
if (wi > 0 && written < buf_cap) { 172 22 194
if (wi > 0 && written < buf_cap) { 172 0 172
2402 172 buf[written++] = ' '; /* space between words */
2403 }
2404 1176 ▶ for (gi = 0; gi < word->len; gi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (gi = 0; gi < word->len; gi++) { 982 194 1176
2405 982 n = encode_utf8(word->glyphs[gi].unicode, tmp);
2406 1976 ▶ for (k = 0; k < n && written < buf_cap; k++) {
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < n && written < buf_cap; k++) { 994 982 1976
for (k = 0; k < n && written < buf_cap; k++) { 994 0 994
2407 994 buf[written++] = tmp[k];
2408 }
2409 }
2410 }
2411 /* Newline between lines */
2412 22 ▶ if (li + 1 < block->len && written < buf_cap) {
Branch: 2 branches
BranchTrueFalseTotal
if (li + 1 < block->len && written < buf_cap) { 7 15 22
if (li + 1 < block->len && written < buf_cap) { 7 0 7
2413 7 buf[written++] = '\n';
2414 }
2415 }
2416 /* Double newline between blocks */
2417 15 ▶ if (bi + 1 < result->len && written + 1 < buf_cap) {
Branch: 2 branches
BranchTrueFalseTotal
if (bi + 1 < result->len && written + 1 < buf_cap) { 11 4 15
if (bi + 1 < result->len && written + 1 < buf_cap) { 11 0 11
2418 11 buf[written++] = '\n';
2419 11 buf[written++] = '\n';
2420 }
2421 }
2422  
2423 /* Null-terminate if room */
2424 4 ▶ if (written < buf_cap) buf[written] = '\0';
Branch: 1 branch
BranchTrueFalseTotal
if (written < buf_cap) buf[written] = '\0'; 4 0 4
2425  
2426 4 return written;
2427 }
« xs/form.xs ↑ Summary src/pdfmake_writer.c »