-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdftext.c
685 lines (608 loc) · 16.1 KB
/
pdftext.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/*
* pdftext.c
*
* convert pdf to text or rich text (roff, html, tex)
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <poppler.h>
#include "pdfrects.h"
#include "pdftext.h"
/*
* known output formats
*/
struct format format_roff = {
".ti 1\n", "\n",
"",
"\\fR", "\\fI", "\\fB", "\\f[BI]",
"", "", "", "",
FALSE,
"\\", "\\[char46]", "<", ">", "&"
};
struct format format_html = {
"\n<p>", "</p>\n",
"",
"", "", "", "",
"<i>", "</i>", "<b>", "</b>",
TRUE,
"\\", ".", "<", ">", "&"
};
struct format format_tex = {
"", "\n\n",
"",
"\\rm ", "\\it ", "\\bf ", "\\bf ", /* FIXME: bold+italic */
"", "", "", "",
FALSE,
"\\backslash ", ".", "<", ">", "\\& "
};
struct format format_textfont = {
"", "\n",
"\\[%s]",
"", "", "", "",
"", "", "", "",
FALSE,
"\\\\", ".", "<", ">", "&"
};
struct format format_text = {
"", "\n",
"",
"", "", "", "",
"", "", "", "",
FALSE,
"\\", ".", "<", ">", "&"
};
/*
* print reason for a paragraph break
*/
gboolean debugpar = FALSE;
void dnewpar(FILE *fd, char *why) {
if (debugpar)
fputs(why, fd);
}
void delement(FILE *fd, char *what, int num) {
if (debugpar)
fprintf(fd, what, num);
}
/*
* start or end a font face
* start TRUE to start the new face, FALSE to end the previous
* reset TRUE to temporarily end or restore all active faces
*/
void face(FILE *fd, gboolean start, gboolean reset,
gboolean *italic, gboolean *bold,
PopplerTextAttributes *attr, struct format *format) {
gboolean newitalic, newbold;
if (reset && ! format->reset)
return;
newitalic = NULL != strstr(attr->font_name, "Italic");
newbold = NULL != strstr(attr->font_name, "Bold");
/* font name */
if (start && ! reset && *format->fontname != '\0')
fprintf(fd, format->fontname, attr->font_name);
/* font start, no end except for resets */
if (start) {
if (! newitalic && ! newbold)
fputs(format->plain, fd);
else if (newitalic && ! newbold)
fputs(format->italic, fd);
else if (! newitalic && newbold)
fputs(format->bold, fd);
if (newitalic && newbold)
fputs(format->bolditalic, fd);
}
if (! start && reset)
fputs(format->plain, fd);
/* font start-end */
if (! start) {
if (*bold && newbold == reset)
fputs(format->boldend, fd);
if (*italic && newitalic == reset)
fputs(format->italicend, fd);
}
else {
if (*italic == reset && newitalic)
fputs(format->italicbegin, fd);
if (*bold == reset && newbold)
fputs(format->boldbegin, fd);
}
/* update current font */
if (start && ! reset) {
*italic = newitalic;
*bold = newbold;
}
}
/*
* show a single character
* rest character not written (hyphen at end of line) or NONE
*/
void showcharacter(FILE *fd, char *cur, char *next, char *rest,
gboolean newpar, char hyphen, struct format *format) {
char *ligatures[] = {
"ff", "\xef\xac\x80",
"fi", "\xef\xac\x81",
"fl", "\xef\xac\x82",
"ffi", "\xef\xac\x83",
"ffl", "\xef\xac\x84",
"st", "\xef\xac\x85",
"st", "\xef\xac\x86",
NULL, NULL
};
int l;
*rest = NONE;
if (*cur == '\\')
fputs(format->backslash, fd);
else if (newpar && *cur == '.')
fputs(format->firstdot, fd);
else if (*cur == '<')
fputs(format->less, fd);
else if (*cur == '>')
fputs(format->greater, fd);
else if (*cur == '&')
fputs(format->and, fd);
else if (*cur == hyphen && (*next == '\0' || *next == '\n'))
*rest = '-';
else {
for (l = 0; ligatures[l] != NULL; l += 2) {
if ((int) strlen(ligatures[l + 1]) != next - cur)
continue;
if (! memcmp(ligatures[l + 1], cur, next - cur)) {
fputs(ligatures[l], fd);
return;
}
}
fwrite(cur, 1, next - cur, fd);
}
}
/*
* check if a line is short
*/
gboolean isshortline(PopplerRectangle crect, gdouble left, gdouble right,
struct measure *measure) {
return crect.x2 - left < (right - left) * measure->rightreturn / 100;
}
/*
* check start of new column
*/
gboolean newcolumn(gdouble y, PopplerRectangle crect,
gdouble left, PopplerRectangle *tr,
struct measure *measure) {
return
crect.x1 - left > (tr->x2 - tr->x1) * measure->newcolumnx / 100
&&
y - crect.y1 > (tr->y2 - tr->y1) * measure->newcolumny / 100;
}
/*
* box-not-found error
*/
void boxnotfound(char *cur, PopplerRectangle *crect, RectangleList *textarea) {
fprintf(stderr, "error: cannot find text rectangle\n");
fprintf(stderr, "character: %c (%d)\n", *cur, *cur);
fprintf(stderr, "rectangle:\n");
rectangle_print(stderr, crect);
fprintf(stderr, "\n");
fprintf(stderr, "text area:\n");
rectanglelist_print(stderr, textarea);
exit(EXIT_FAILURE);
}
/*
* data for processing the characters
*/
struct scandata {
gboolean newpar; // last paragraph in previous box/page is over
char prev; // unprinted char, possibly NONE or START
gboolean italic; // current font is italic
gboolean bold; // current font is bold
gboolean newface; // font changed
};
/*
* start processing a page (no end needed)
*/
void startpage(struct scandata *scanpage) {
scanpage->italic = FALSE;
scanpage->bold = FALSE;
scanpage->newface = TRUE;
}
/*
* show the characters of a page contained in a box
* zone only characters in this box are shown
* NULL = all characters in page
* textarea the blocks of text in the page
* may also be the whole page or its bounding box
* text, attrlist, rects, nrects
* characters and their fonts and positions
* detectcolumns whether to detect the start of a new column during the
* scan, by comparing the position of the current
* character with that of the current column and previous
* character; only used when textarea=page
*/
void showregion(FILE *fd, PopplerRectangle *zone, RectangleList *textarea,
char *text, GList *attrlist,
PopplerRectangle *rects, guint nrects,
struct measure *measure, struct format *format,
struct scandata *scandata, gboolean detectcolumn) {
char *cur, *next;
int count;
gdouble left, y;
GList *attrelem;
int ti = -1;
PopplerRectangle *tr;
PopplerTextAttributes *attr;
gboolean startcolumn, shortline, newline;
PopplerRectangle crect;
guint r;
/* cycle over (utf-8) characters in page */
shortline = FALSE;
startcolumn = TRUE;
attrelem = attrlist;
attr = (PopplerTextAttributes *) (attrelem->data);
for (cur = text, count = 0; *cur; cur = next, count++) {
crect = rects[count];
next = g_utf8_next_char(cur);
if (zone != NULL && ! rectangle_contain(zone, &crect))
continue;
/* text rectangle this char is in */
if (ti != -1 && rectangle_contain(tr, &crect))
newline = FALSE;
else {
ti = rectanglelist_contain(textarea, &crect);
delement(fd, "[BLOCK %d]", ti);
if (ti != -1)
tr = &textarea->rect[ti];
else if (*cur == ' ') {
dnewpar(fd, "_SPACE_");
tr = &crect;
}
else
boxnotfound(cur, &crect, textarea);
left = tr->x1;
y = tr->y1 - measure->newline - 1;
newline = TRUE;
}
/* explicit end of line */
if (*cur == '\n' || newline) {
if (shortline) {
dnewpar(fd, "[S]");
scandata->newpar = TRUE;
}
else {
if (scandata->prev == '-')
dnewpar(fd, "[-]");
else
dnewpar(fd, "[]");
scandata->prev =
scandata->prev == '-' ||
scandata->prev == START ?
NONE : ' ';
}
}
/* real character */
if (*cur != '\n') {
/* new column */
if (detectcolumn &&
newcolumn(y, crect, left, tr, measure))
startcolumn = TRUE;
if (detectcolumn && startcolumn) {
dnewpar(fd, "[COLUMN]");
left = 10000;
y = 10000;
for (r = MAX(measure->headfooter, count);
r + measure->headfooter < nrects;
r++) {
left = MIN(left, rects[r].x1);
y = MIN(y, rects[r].y1);
}
if (left == 10000)
y = 0; /* few chars, force newpar */
y -= measure->newline + 1;
startcolumn = FALSE;
}
/* y increase */
if (crect.y1 - y > measure->newline) {
if (crect.y1 - y > measure->newpar) {
dnewpar(fd, "[V]");
fputs(format->parend, fd);
fputs(format->parstart, fd);
scandata->newpar = TRUE;
}
y = crect.y1;
if (crect.x1 - left > measure->indent) {
dnewpar(fd, "[I]");
scandata->newpar = TRUE;
}
}
/* new paragraph */
if (scandata->newpar) {
face(fd, FALSE, TRUE,
&scandata->italic, &scandata->bold,
attr, format);
if (scandata->prev != START)
fputs(format->parend, fd);
fputs(format->parstart, fd);
face(fd, TRUE, TRUE,
&scandata->italic, &scandata->bold,
attr, format);
}
else if (scandata->prev > START)
fprintf(fd, "%c", scandata->prev);
/* start a new font face */
if (scandata->newface && *cur != ' ') {
face(fd, TRUE, FALSE,
&scandata->italic, &scandata->bold,
attr, format);
scandata->newface = FALSE;
}
/* print character */
showcharacter(fd, cur, next,
&scandata->prev, scandata->newpar,
measure->hyphen, format);
/* update status variables */
shortline = isshortline(crect, left, tr->x2, measure);
scandata->newpar = FALSE;
}
/* end of text with current font; read next */
if (count == attr->end_index -
(g_unichar_isspace(*next) ? 1 : 0)) {
attrelem = g_list_next(attrelem);
if (! attrelem) {
face(fd, FALSE, TRUE,
&scandata->italic, &scandata->bold,
attr, format);
break;
}
attr = (PopplerTextAttributes *) (attrelem->data);
face(fd, FALSE, FALSE,
&scandata->italic, &scandata->bold,
attr, format);
scandata->newface = TRUE;
}
}
/* shortline at end */
if (shortline) {
dnewpar(fd, "[E]");
scandata->newpar = TRUE;
}
}
/*
* show the characters in a page
*/
void showpage(FILE *fd, PopplerPage *page, PopplerRectangle *zone,
int method, int order,
struct measure *measure, struct format *format,
struct scandata *scandata) {
char *text;
GList *attrlist;
PopplerRectangle *rects, *tr, *region;
guint nrects;
RectangleList *textarea;
gint r;
void (*sort[])(RectangleList *, PopplerPage *) = {
rectanglelist_quicksort,
rectanglelist_twosort,
rectanglelist_charsort
};
/* initalize output font */
startpage(scandata);
/* get page content */
text = poppler_page_get_text(page);
attrlist = poppler_page_get_text_attributes(page);
if (! text || ! attrlist)
return; /* no text in page */
if (! poppler_page_get_text_layout(page, &rects, &nrects))
return; /* no text in page */
/* analyze text */
switch (method) {
case 0:
tr = poppler_rectangle_new();
poppler_page_get_crop_box(page, tr);
textarea = rectanglelist_new(1);
rectanglelist_add(textarea, tr);
showregion(fd, zone, textarea, text, attrlist, rects, nrects,
measure, format, scandata, TRUE);
poppler_rectangle_free(tr);
break;
case 1:
tr = rectanglelist_boundingbox(page);
textarea = rectanglelist_new(1);
rectanglelist_add(textarea, tr);
showregion(fd, zone, textarea, text, attrlist, rects, nrects,
measure, format, scandata, FALSE);
poppler_rectangle_free(tr);
break;
case 2:
textarea = rectanglelist_textarea_distance(page,
measure->blockdistance);
showregion(fd, zone, textarea, text, attrlist, rects, nrects,
measure, format, scandata, FALSE);
break;
case 3:
textarea = rectanglelist_textarea_distance(page,
measure->blockdistance);
sort[order](textarea, page);
region = poppler_rectangle_new();
for (r = 0; r < textarea->num; r++) {
delement(fd, "[=== BLOCK %d]", r);
if (zone == NULL) {
showregion(fd, &textarea->rect[r], textarea,
text, attrlist, rects, nrects,
measure, format, scandata, FALSE);
continue;
}
if (! rectangle_overlap(zone, &textarea->rect[r]))
continue;
rectangle_intersect(region, zone, &textarea->rect[r]);
showregion(fd, region, textarea,
text, attrlist, rects, nrects,
measure, format, scandata, FALSE);
}
poppler_rectangle_free(region);
break;
case 4:
measure->rightreturn = -1;
measure->indent = 100000;
measure->hyphen = '\0';
textarea = rectanglelist_rows(page, measure->blockdistance);
region = poppler_rectangle_new();
for (r = 0; r < textarea->num; r++) {
delement(fd, "[=== BLOCK %d]", r);
if (zone == NULL) {
showregion(fd, &textarea->rect[r], textarea,
text, attrlist, rects, nrects,
measure, format, scandata, FALSE);
fprintf(fd, "\n");
continue;
}
if (! rectangle_overlap(zone, &textarea->rect[r]))
continue;
rectangle_intersect(region, zone, &textarea->rect[r]);
showregion(fd, region, textarea,
text, attrlist, rects, nrects,
measure, format, scandata, FALSE);
fprintf(fd, "\n");
}
poppler_rectangle_free(region);
break;
default:
fprintf(stderr, "no such conversion method: %d\n", method);
exit(EXIT_FAILURE);
}
poppler_page_free_text_attributes(attrlist);
g_free(rects);
free(text);
}
/*
* start processing a document
*/
void startdocument(FILE *fd,
int method, struct measure *measure, struct format *format,
struct scandata *scandata) {
(void)fd;
(void)method;
(void)measure;
(void)format;
scandata->newpar = FALSE;
scandata->prev = START;
}
/*
* end a document
*/
void enddocument(FILE *fd,
int method, struct measure *measure, struct format *format,
struct scandata *scandata) {
(void)method;
(void)measure;
if (scandata->prev != START)
fputs(format->parend, fd);
}
/*
* show some pages of a pdf document
*/
void showdocumentpart(FILE *fd, PopplerDocument *doc, int first, int last,
PopplerRectangle *zone,
int method, int order,
struct measure *measure, struct format *format) {
struct scandata scandata;
int npage;
PopplerPage *page;
gdouble h;
if (first < 0)
first = poppler_document_get_n_pages(doc) + first;
if (last < 0)
last = poppler_document_get_n_pages(doc) + last;
if (first < 0)
first = 0;
if (last >= poppler_document_get_n_pages(doc))
last = poppler_document_get_n_pages(doc) - 1;
if (zone != NULL && zone->x1 == -100 && zone->x2 == -100) {
h = zone->y2;
poppler_rectangle_free(zone);
debugfrequent = 0;
zone = rectanglevector_main(doc, NULL,
h, measure->blockdistance);
}
startdocument(fd, method, measure, format, &scandata);
for (npage = first; npage <= last; npage++) {
page = poppler_document_get_page(doc, npage);
delement(fd, "[PAGE %d]", npage);
showpage(fd, page, zone,
method, order, measure, format, &scandata);
g_object_unref(page);
}
enddocument(fd, method, measure, format, &scandata);
}
/*
* show a pdf document
*/
void showdocument(FILE *fd, PopplerDocument *doc, PopplerRectangle *zone,
int method, int order,
struct measure *measure, struct format *format) {
showdocumentpart(fd, doc, 0, -1, zone, method, order, measure, format);
}
/*
* show a pdf file
*/
void showfile(FILE *fd, char *filename, int first, int last,
PopplerRectangle *zone,
int method, int order,
struct measure *measure, struct format *format) {
char *uri;
PopplerDocument *doc;
uri = filenametouri(filename);
doc = poppler_document_new_from_file(uri, NULL, NULL);
free(uri);
if (doc == NULL) {
printf("error opening file %s\n", filename);
exit(EXIT_FAILURE);
}
showdocumentpart(fd, doc, first, last, zone,
method, order, measure, format);
}
/*
* parse a string into a struct format
*/
struct format *parseformat(char *s) {
struct format *f;
char *c, *t;
c = strdup(s);
f = malloc(sizeof(struct format));
memset(f, 0, sizeof(struct format));
if (! (f->parstart = strsep(&c, ",")))
goto parse_error;
if (! (f->parend = strsep(&c, ",")))
goto parse_error;
if (! (f->fontname = strsep(&c, ",")))
goto parse_error;
if (! (f->plain = strsep(&c, ",")))
goto parse_error;
if (! (f->italic = strsep(&c, ",")))
goto parse_error;
if (! (f->bold = strsep(&c, ",")))
goto parse_error;
if (! (f->bolditalic = strsep(&c, ",")))
goto parse_error;
if (! (f->italicbegin = strsep(&c, ",")))
goto parse_error;
if (! (f->italicend = strsep(&c, ",")))
goto parse_error;
if (! (f->boldbegin = strsep(&c, ",")))
goto parse_error;
if (! (f->boldend = strsep(&c, ",")))
goto parse_error;
if (! (t = strsep(&c, ",")))
goto parse_error;
f->reset = ! strcmp(t, "true");
if (! (f->backslash = strsep(&c, ",")))
goto parse_error;
if (! (f->firstdot = strsep(&c, ",")))
goto parse_error;
if (! (f->less = strsep(&c, ",")))
goto parse_error;
if (! (f->greater = strsep(&c, ",")))
goto parse_error;
if (! (f->and = strsep(&c, ",")))
goto parse_error;
return f;
parse_error:
free(f);
return NULL;
}