summaryrefslogtreecommitdiff
blob: 4425de3dbe6ab0f49c1b7a0ea2a5ce024e5965db (plain)
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
#include "../include/extract.h"
#include "../include/extract_alloc.h"

#include "astring.h"
#include "document.h"
#include "mem.h"
#include "outf.h"

#include <assert.h>
#include <float.h>
#include <math.h>
#include <stdio.h>


static char_t* span_char_first(span_t* span)
{
    assert(span->chars_num > 0);
    return &span->chars[0];
}

static span_t* s_line_span_first(line_t* line)
{
    return extract_line_span_first(line);
}

/* Returns first char_t in a line. */
static char_t* line_item_first(line_t* line)
{
    span_t* span = s_line_span_first(line);
    return span_char_first(span);
}

/* Returns last char_t in a line. */
static char_t* line_item_last(line_t* line)
{
    span_t* span = extract_line_span_last(line);
    return extract_span_char_last(span);
}

static point_t char_to_point(const char_t* char_)
{
    point_t ret;
    ret.x = char_->x;
    ret.y = char_->y;
    return ret;
}

const char* extract_matrix_string(const matrix_t* matrix)
{
    static char ret[5][64];
    static int i = 0;
    i = (i + 1) % 5;
    snprintf(ret[i], sizeof(ret[i]), "{%f %f %f %f %f %f}",
            matrix->a,
            matrix->b,
            matrix->c,
            matrix->d,
            matrix->e,
            matrix->f
            );
    return ret[i];
}

/* Returns total width of span. */
static double span_adv_total(span_t* span)
{
    double dx = extract_span_char_last(span)->x - span_char_first(span)->x;
    double dy = extract_span_char_last(span)->y - span_char_first(span)->y;
    /* We add on the advance of the last item; this avoids us returning zero if
    there's only one item. */
    double adv = extract_span_char_last(span)->adv * extract_matrix_expansion(span->trm);
    return sqrt(dx*dx + dy*dy) + adv;
}

/* Returns distance between end of <a> and beginning of <b>. */
static double spans_adv(
        span_t* a_span,
        char_t* a,
        char_t* b
        )
{
    double delta_x = b->x - a->x;
    double delta_y = b->y - a->y;
    double s = sqrt( delta_x*delta_x + delta_y*delta_y);
    double a_size = a->adv * extract_matrix_expansion(a_span->trm);
    s -= a_size;
    return s;
}

static double span_angle(span_t* span)
{
    double ret = atan2(-span->ctm.c, span->ctm.a);
    if (0)
    {
        /* This is an attempt to take into account the trm matrix when looking
        at spans, because for agstat.pdf vertical text seems to be achieved
        by making trm rotate by 90 degrees. But it messes up the ordering of
        rotated paragraphs in Python2.pdf so is disabled for now. */
        matrix_t m = extract_multiply_matrix_matrix(span->trm, span->ctm);
        point_t dir;
        double ret;
        dir.x = span->flags.wmode ? 0 : 1;
        dir.y = span->flags.wmode ? 1 : 0;
        dir = extract_multiply_matrix_point(m, dir);
        ret = atan2(dir.y, dir.x);
        return ret;
    }
    /* Assume ctm is a rotation matix. */
    outfx("ctm.a=%f ctm.b=%f ret=%f", span->ctm.a, span->ctm.b, ret);
    return ret;
    /* Not sure whether this is right. Inclined text seems to be done by
    setting the ctm matrix, so not really sure what trm matrix does. This code
    assumes that it also inclines text, but maybe it only rotates individual
    glyphs? */
    /*if (span->wmode == 0) {
        return atan2(span->trm.b, span->trm.a);
    }
    else {
        return atan2(span->trm.d, span->trm.c);
    }*/
}

static double span_angle2(span_t* span)
{
    if (span->chars_num > 1)
    {
        double dx = span->chars[span->chars_num-1].x - span->chars[0].x;
        double dy = span->chars[span->chars_num-1].y - span->chars[0].y;
        double ret1 = span_angle(span);
        double ret2 = atan2(-dy, dx);
        if (fabs(ret2 - ret1) > 0.01)
        {
            outf("### ret1=%f ret2=%f: %s", ret1, ret2, extract_span_string(NULL, span));
        }
    }
    return span_angle(span);
}

/* Returns static string containing brief info about span_t. */
static const char* span_string2(extract_alloc_t* alloc, span_t* span)
{
    static extract_astring_t ret = {0};
    int i;
    extract_astring_free(alloc, &ret);
    extract_astring_catc(alloc, &ret, '"');
    for (i=0; i<span->chars_num; ++i) {
        extract_astring_catc(alloc, &ret, (char) span->chars[i].ucs);
    }
    extract_astring_catc(alloc, &ret, '"');
    return ret.chars;
}

/* Returns angle of <line>. */
static double line_angle(line_t* line)
{
    /* All spans in a line must have same angle, so just use the first span. */
    assert(line->spans_num > 0);
    return span_angle(line->spans[0]);
}

/* Returns static string containing brief info about line_t. */
static const char* line_string2(extract_alloc_t* alloc, line_t* line)
{
    static extract_astring_t ret = {0};
    char    buffer[256];
    int i;
    extract_astring_free(alloc, &ret);
    snprintf(buffer, sizeof(buffer), "line x=%f y=%f spans_num=%i:",
            line->spans[0]->chars[0].x,
            line->spans[0]->chars[0].y,
            line->spans_num
            );
    extract_astring_cat(alloc, &ret, buffer);
    for (i=0; i<line->spans_num; ++i) {
        extract_astring_cat(alloc, &ret, " ");
        extract_astring_cat(alloc, &ret, span_string2(alloc, line->spans[i]));
    }
    return ret.chars;
}

/* Array of pointers to lines that are aligned and adjacent to each other so as
to form a paragraph. */
static const char* paragraph_string(extract_alloc_t* alloc, paragraph_t* paragraph)
{
    static extract_astring_t ret = {0};
    extract_astring_free(alloc, &ret);
    extract_astring_cat(alloc, &ret, "paragraph: ");
    if (paragraph->lines_num) {
        extract_astring_cat(alloc, &ret, line_string2(alloc, paragraph->lines[0]));
        if (paragraph->lines_num > 1) {
            extract_astring_cat(alloc, &ret, "..");
            extract_astring_cat(
                    alloc,
                    &ret,
                    line_string2(alloc, paragraph->lines[paragraph->lines_num-1])
                    );
        }
    }
    return ret.chars;
}

/* Returns first line in paragraph. */
static line_t* paragraph_line_first(const paragraph_t* paragraph)
{
    assert(paragraph->lines_num);
    return paragraph->lines[0];
}

/* Returns last line in paragraph. */
static line_t* paragraph_line_last(const paragraph_t* paragraph)
{
    assert(paragraph->lines_num);
    return paragraph->lines[ paragraph->lines_num-1];
}



/* Things for direct conversion of text spans into lines and paragraphs. */

/* Returns 1 if lines have same wmode and are at the same angle, else 0.

todo: allow small epsilon? */
static int lines_are_compatible(
        line_t* a,
        line_t* b,
        double  angle_a,
        int     verbose
        )
{
    if (a == b) return 0;
    if (!a->spans || !b->spans) return 0;
    if (s_line_span_first(a)->flags.wmode != s_line_span_first(b)->flags.wmode) {
        return 0;
    }
    if (extract_matrix_cmp4(
            &s_line_span_first(a)->ctm,
            &s_line_span_first(b)->ctm
            )) {
        if (verbose) {
            outf("ctm's differ:");
            outf("    %f %f %f %f %f %f",
                    s_line_span_first(a)->ctm.a,
                    s_line_span_first(a)->ctm.b,
                    s_line_span_first(a)->ctm.c,
                    s_line_span_first(a)->ctm.d,
                    s_line_span_first(a)->ctm.e,
                    s_line_span_first(a)->ctm.f
                    );
            outf("    %f %f %f %f %f %f",
                    s_line_span_first(b)->ctm.a,
                    s_line_span_first(b)->ctm.b,
                    s_line_span_first(b)->ctm.c,
                    s_line_span_first(b)->ctm.d,
                    s_line_span_first(b)->ctm.e,
                    s_line_span_first(b)->ctm.f
                    );
        }
        return 0;
    }
    {
        double angle_b = span_angle(s_line_span_first(b));
        if (angle_b != angle_a) {
            outfx("%s:%i: angles differ");
            return 0;
        }
    }
    return 1;
}


static const unsigned ucs_NONE = ((unsigned) -1);

static int s_span_inside_rects(
        extract_alloc_t* alloc,
        span_t* span,
        rect_t* rects,
        int rects_num,
        span_t* o_span
        )
/* Returns with <o_span> containing char_t's from <span> that are inside
rects[], and *span modified to remove any char_t's that we have moved to
<o_span>.

May return with span->chars_num == 0, in which case the caller must remove the
span (including freeing .font_name), because lots of code assumes that there
are no empty spans. */
{
    int c;
    *o_span = *span;
    extract_strdup(alloc, span->font_name, &o_span->font_name);
    o_span->chars = NULL;
    o_span->chars_num = 0;
    for (c=0; c<span->chars_num; ++c)
    {
        /* For now we just look at whether span's (x, y) is within any
        rects[]. We could instead try to find character's bounding box etc. */
        char_t* char_ = &span->chars[c];
        int r;
        for (r=0; r<rects_num; ++r)
        {
            rect_t* rect = &rects[r];
            if (1
                    && char_->x >= rect->min.x
                    && char_->x < rect->max.x
                    && char_->y >= rect->min.y
                    && char_->y < rect->max.y
                    )
            {
                if (extract_span_append_c(alloc, o_span, char_->ucs))   return -1;
                /* Coverity warns, but o_span must have at least one item. */
                /* coverity[var_deref_op] */
                *extract_span_char_last(o_span) = *char_;
                char_->ucs = ucs_NONE; /* Mark for removal below, so it is not used again. */
                break;
            }
        }
    }

    /* Remove any char_t's that we've used. */
    {
        int cc = 0;
        for (c=0; c<span->chars_num; ++c)
        {
            char_t* char_ = &span->chars[c];
            if (char_->ucs != ucs_NONE)
            {
                span->chars[cc] = span->chars[c];
                cc += 1;
            }
        }
        /* This might set span->chars_num to zero; our caller needs to remove
        the span - lots of code assumes that all spans contain at least one
        character. */
        span->chars_num = cc;
    }

    if (o_span->chars_num)
    {
        //outf0("  span: %s", extract_span_string(alloc, span));
        outf("o_span: %s", extract_span_string(alloc, o_span));
    }
    return 0;
}

/* Creates representation of span_t's that consists of a list of line_t's, with
each line_t contains pointers to a list of span_t's.

We only join spans that are at the same angle and are aligned.

On entry:
    Original value of *o_lines and *o_lines_num are ignored.

    <spans> points to array of <spans_num> span_t*'s, each pointing to
    a span_t.

On exit:
    If we succeed, we return 0, with *o_lines pointing to array of *o_lines_num
    line_t*'s, each pointing to a line_t.
    
    If <rects_num> is zero, each of these line_t's will contain pointers to
    items in <spans>; otherwise each of the line_t's will contain new spans
    which should be freed by the caller (spans are not necessarily wholy inside
    or outside rects[] so we need to create new spams).

    Otherwise we return -1 with errno set. *o_lines and *o_lines_num are
    undefined.
*/
static int make_lines(
        extract_alloc_t*    alloc,
        span_t**            spans,
        int*                spans_num,
        rect_t*             rects,
        int                 rects_num,
        line_t***           o_lines,
        int*                o_lines_num
        )
{
    int ret = -1;

    /* Make a line_t for each span. Then we will join some of these line_t's
    together before returning. */
    int         lines_num = 0;
    line_t**    lines = NULL;
    int         a;
    int         num_compatible;
    int         num_joins;
    span_t*     span = NULL;
    
    if (rects_num)
    {
        /* Make <lines> contain new span_t's and char_t's that are inside rects[]. */
        for (a=0; a<*spans_num; ++a)
        {
            if (spans[a]->chars_num == 0)   continue; /* In case used for table, */
            if (extract_realloc(alloc, &span, sizeof(*span))) goto end;
            extract_span_init(span);
            if (s_span_inside_rects(alloc, spans[a], rects, rects_num, span))
            {
                goto end;
            }
            if (span->chars_num)
            {
                if (extract_realloc(alloc, &lines, sizeof(*lines) * (lines_num + 1))) goto end;
                if (extract_malloc(alloc, &lines[lines_num], sizeof(line_t))) goto end;
                lines_num += 1;
                if (extract_malloc(alloc, &lines[lines_num-1]->spans, sizeof(span_t*) * 1)) goto end;
                lines[lines_num-1]->spans[0] = span;
                lines[lines_num-1]->spans_num = 1;
                span = NULL;
            }
            else
            {
                extract_span_free(alloc, &span);
            }
            
            if (!spans[a]->chars_num)
            {
                /* All characters in this span are inside table, so remove
                entire span, otherwise the same characters will end up being
                output outside the table also. */
                extract_span_free(alloc, &spans[a]);
                memmove(&spans[a], &spans[a+1], sizeof(*spans) * ((*spans_num) - (a+1)));
                *spans_num -= 1;
                a -= 1;
            }
        }
    }
    else
    {
        /* Make <lines> be a copy of <spans>. */
        lines_num = *spans_num;
        if (extract_malloc(alloc, &lines, sizeof(*lines) * lines_num)) goto end;

        /* Ensure we can clean up after error. */
        for (a=0; a<lines_num; ++a) {
            lines[a] = NULL;
        }
        for (a=0; a<lines_num; ++a) {
            if (extract_malloc(alloc, &lines[a], sizeof(line_t))) goto end;
            lines[a]->spans_num = 0;
            if (extract_malloc(alloc, &lines[a]->spans, sizeof(span_t*) * 1)) goto end;
            lines[a]->spans_num = 1;
            lines[a]->spans[0] = spans[a];
            /* Ensure that spans[] can be safely freed now we've moved it into lines[]. */
            spans[a] = NULL;
            outfx("initial line a=%i: %s", a, line_string(lines[a]));
        }
    }
    
    num_compatible = 0;

    /* For each line, look for nearest aligned line, and append if found. */
    num_joins = 0;
    for (a=0; a<lines_num; ++a) {
        int b;
        int verbose = 0;
        int nearest_line_b = -1;
        double nearest_adv = 0;
        line_t* nearest_line = NULL;
        span_t* span_a;
        double angle_a;
        
        line_t* line_a = lines[a];
        if (!line_a) {
            continue;
        }

        if (0 && a < 1) verbose = 1;
        outfx("looking at line_a=%s", line_string2(alloc, line_a));

        span_a = extract_line_span_last(line_a);
        angle_a = span_angle(span_a);
        if (verbose) outf("a=%i angle_a=%f ctm=%s: %s",
                a,
                angle_a * 180/pi,
                extract_matrix_string(&span_a->ctm),
                line_string2(alloc, line_a)
                );

        for (b=0; b<lines_num; ++b) {
            line_t* line_b = lines[b];
            if (!line_b) {
                continue;
            }
            if (b == a) {
                continue;
            }
            if (verbose) {
                outf("a=%i b=%i: nearest_line_b=%i nearest_adv=%f",
                        a,
                        b,
                        nearest_line_b,
                        nearest_adv
                        );
                outf("    line_a=%s", line_string2(alloc, line_a));
                outf("    line_b=%s", line_string2(alloc, line_b));
            }
            if (!lines_are_compatible(line_a, line_b, angle_a, 0*verbose)) {
                if (verbose) outf("not compatible");
                continue;
            }

            num_compatible += 1;
            {
                /* Find angle between last glyph of span_a and first glyph of
                span_b. This detects whether the lines are lined up with each other
                (as opposed to being at the same angle but in different lines). */
                span_t* span_b = s_line_span_first(line_b);
                double dx = span_char_first(span_b)->x - extract_span_char_last(span_a)->x;
                double dy = span_char_first(span_b)->y - extract_span_char_last(span_a)->y;
                double angle_a_b = atan2(-dy, dx);
                const double angle_tolerance_deg = 1;
                if (verbose) {
                    outf("delta=(%f %f) alast=(%f %f) bfirst=(%f %f): angle_a=%f angle_a_b=%f",
                            dx,
                            dy,
                            extract_span_char_last(span_a)->x,
                            extract_span_char_last(span_a)->y,
                            span_char_first(span_b)->x,
                            span_char_first(span_b)->y,
                            angle_a * 180 / pi,
                            angle_a_b * 180 / pi
                            );
                }
                /* Might want to relax this when we test on non-horizontal lines.
                */
                if (fabs(angle_a_b - angle_a) * 180 / pi <= angle_tolerance_deg) {
                    /* Find distance between end of line_a and beginning of line_b. */
                    double adv = spans_adv(
                            span_a,
                            extract_span_char_last(span_a),
                            span_char_first(span_b)
                            );
                    if (verbose) outf("nearest_adv=%f. angle_a_b=%f adv=%f",
                            nearest_adv,
                            angle_a_b,
                            adv
                            );
                    if (!nearest_line || adv < nearest_adv) {
                        nearest_line = line_b;
                        nearest_adv = adv;
                        nearest_line_b = b;
                    }
                }
                else {
                    if (verbose) outf(
                            "angle beyond tolerance: span_a last=(%f,%f) span_b first=(%f,%f) angle_a_b=%g angle_a=%g span_a.trm{a=%f b=%f}",
                            extract_span_char_last(span_a)->x,
                            extract_span_char_last(span_a)->y,
                            span_char_first(span_b)->x,
                            span_char_first(span_b)->y,
                            angle_a_b * 180 / pi,
                            angle_a * 180 / pi,
                            span_a->trm.a,
                            span_a->trm.b
                            );
                }
            }
        }

        if (nearest_line) {
            /* line_a and nearest_line are aligned so we can move line_b's
            spans on to the end of line_a. */
            double average_adv;
            span_t* span_b = s_line_span_first(nearest_line);
            b = nearest_line_b;
            if (verbose) outf("found nearest line. a=%i b=%i", a, b);

            /* Find average advance of the two adjacent spans in the two
            lines we are considering joining, so that we can decide whether
            the distance between them is large enough to merit joining with
            a space character). */
            average_adv = (
                    (span_adv_total(span_a) + span_adv_total(span_b))
                    /
                    (double) (span_a->chars_num + span_b->chars_num)
                    );

            if (0 && nearest_adv > 5 * average_adv)
            {
                continue;
            }
            
            if (1
                    && extract_span_char_last(span_a)->ucs != ' '
                    && span_char_first(span_b)->ucs != ' '
                    ) {
                int insert_space = (nearest_adv > 0.25 * average_adv);
                if (insert_space) {
                    /* Append space to span_a before concatenation. */
                    char_t* item;
                    if (verbose) {
                        outf("(inserted space) nearest_adv=%f average_adv=%f",
                                nearest_adv,
                                average_adv
                                );
                        outf("    a: %s", extract_span_string(alloc, span_a));
                        outf("    b: %s", extract_span_string(alloc, span_b));
                    }
                    if (extract_realloc2(
                            alloc,
                            &span_a->chars,
                            sizeof(char_t) * span_a->chars_num,
                            sizeof(char_t) * (span_a->chars_num + 1)
                            )) goto end;
                    item = &span_a->chars[span_a->chars_num];
                    span_a->chars_num += 1;
                    extract_bzero(item, sizeof(*item));
                    item->ucs = ' ';
                    item->adv = nearest_adv;
                    /* This is a hack to give our extra space a vaguely useful
                    (x,y) coordinate - this can be used later on when ordering
                    paragraphs. We could try to be more accurate by adding
                    item[-1]'s .adv suitably transformed by .wmode, .ctm and
                    .trm. */
                    item->x = item[-1].x;
                    item->y = item[-1].y;
                }

                if (verbose) {
                    outf("Joining spans a=%i b=%i:", a, b);
                    outf("    %s", span_string2(alloc, span_a));
                    outf("    %s", span_string2(alloc, span_b));
                }
                if (0) {
                    /* Show details about what we're joining. */
                    outf(
                            "joining line insert_space=%i a=%i (y=%f) to line b=%i (y=%f). nearest_adv=%f average_adv=%f",
                            insert_space,
                            a,
                            extract_span_char_last(span_a)->y,
                            b,
                            span_char_first(span_b)->y,
                            nearest_adv,
                            average_adv
                            );
                    outf("a: %s", extract_span_string(alloc, span_a));
                    outf("b: %s", extract_span_string(alloc, span_b));
                }
            }

            /* We might end up with two adjacent spaces here. But removing a
            space could result in an empty line_t, which could break various
            assumptions elsewhere. */

            if (verbose) {
                outf("Joining spans a=%i b=%i:", a, b);
                outf("    %s", span_string2(alloc, span_a));
                outf("    %s", span_string2(alloc, span_b));
            }
            if (extract_realloc2(
                    alloc,
                    &line_a->spans,
                    sizeof(span_t*) * line_a->spans_num,
                    sizeof(span_t*) * (line_a->spans_num + nearest_line->spans_num)
                    )) goto end;
            {
                int k;
                for (k=0; k<nearest_line->spans_num; ++k) {
                    line_a->spans[ line_a->spans_num + k] = nearest_line->spans[k];
                }
            }
            line_a->spans_num += nearest_line->spans_num;

            /* Ensure that we ignore nearest_line from now on. */
            extract_free(alloc, &nearest_line->spans);
            extract_free(alloc, &nearest_line);
            outfx("setting line[b=%i] to NULL", b);
            lines[b] = NULL;

            num_joins += 1;

            if (b > a) {
                /* We haven't yet tried appending any spans to nearest_line, so
                the new extended line_a needs checking again. */
                a -= 1;
            }
            outfx("num_joins=%i new line is:\n    %s", num_joins, line_string2(line_a));
        }
    }

    {
        /* Remove empty lines left behind after we appended pairs of lines. */
        int from;
        int to;
        int lines_num_old;
        for (from=0, to=0; from<lines_num; ++from) {
            if (lines[from]) {
                outfx("final line from=%i: %s",
                        from,
                        lines[from] ? line_string(lines[from]) : "NULL"
                        );
                lines[to] = lines[from];
                to += 1;
            }
        }
        lines_num_old = lines_num;
        lines_num = to;
        if (extract_realloc2(
                alloc,
                &lines,
                sizeof(line_t*) * lines_num_old,
                sizeof(line_t*) * lines_num
                )) {
            /* Should always succeed because we're not increasing allocation size. */
            goto end;
        }
    }

    *o_lines = lines;
    *o_lines_num = lines_num;
    ret = 0;

    outf("Turned %i spans into %i lines. num_compatible=%i",
            *spans_num,
            lines_num,
            num_compatible
            );

    end:
    if (ret) {
        /* Free everything. */
        extract_span_free(alloc, &span);
        if (lines) {
            for (a=0; a<lines_num; ++a) {
                if (lines[a])
                {
                    int s;
                    for (s=0; s<lines[a]->spans_num; ++s)
                    {
                        extract_span_free(alloc, &lines[a]->spans[s]);
                    }
                    extract_free(alloc, &lines[a]->spans);
                }
                extract_free(alloc, &lines[a]);
            }
        }
        extract_free(alloc, &lines);
    }
    return ret;
}


/* Returns max font size of all span_t's in an line_t. */
static double line_font_size_max(line_t* line)
{
    double  size_max = 0;
    int i;
    for (i=0; i<line->spans_num; ++i) {
        span_t* span = line->spans[i];
        /* fixme: <size> should be double, which changes some output. */
        double size = extract_matrix_expansion(span->trm);
        if (size > size_max) {
            size_max = size;
        }
    }
    return size_max;
}



/* Find distance between parallel lines line_a and line_b, both at <angle>.

        _-R
     _-
    A------------_P
     \        _-
      \    _B
       \_-
        Q

A is (ax, ay)
B is (bx, by)
APB and PAR are both <angle>.

AR and QBP are parallel, and are the lines of text a and b
respectively.

AQB is a right angle. We need to find AQ.
*/
static double line_distance_y( double ax, double ay, double bx, double by, double angle)
{
    double dx = bx - ax;
    double dy = by - ay;

    return dx * sin(angle) + dy * cos(angle);
}

/* Returns distance QB in above diagram. */
static double line_distance_x( double ax, double ay, double bx, double by, double angle)
{
    double dx = bx - ax;
    double dy = by - ay;

    return dx * cos(angle) - dy * sin(angle);
}

static double line_distance_xp(point_t a, point_t b, double angle)
{
    return line_distance_x(a.x, a.y, b.x, b.y, angle);
}

static int lines_overlap(point_t a_left, point_t a_right, point_t b_left, point_t b_right, double angle)
{
    if (line_distance_xp(a_left, b_right, angle) < 0)  return 0;
    if (line_distance_xp(a_right, b_left, angle) >= 0) return 0;
    return 1;
}


/* A comparison function for use with qsort(), for sorting paragraphs within a
page. */
static int paragraphs_cmp(const void* a, const void* b)
{
    const paragraph_t* const* a_paragraph = a;
    const paragraph_t* const* b_paragraph = b;
    line_t* a_line = paragraph_line_first(*a_paragraph);
    line_t* b_line = paragraph_line_first(*b_paragraph);

    span_t* a_span = s_line_span_first(a_line);
    span_t* b_span = s_line_span_first(b_line);

    if (0)
    {
        double a_angle = span_angle2(a_span);
        double b_angle = span_angle2(b_span);
        if (fabs(a_angle - b_angle) > 0.01)
        {
            outf0("angles differ: a_angle=%f b_angle=%f", a_angle, b_angle);
            outf0("a_span: %s", extract_span_string(NULL, a_span));
            outf0("b_span: %s", extract_span_string(NULL, b_span));
            if (a_angle - b_angle > 3.14/2) {
                /* Give up if more than 90 deg. */
                return 0;
            }
            if (a_angle > b_angle)  return 1;
            if (a_angle < b_angle)  return -1;
            return 0;
        }
    }
    if (1)
    {
        /* If ctm matrices differ, always return this diff first. Note that we
        ignore .e and .f because if data is from ghostscript then .e and .f
        vary for each span, and we don't care about these differences. */
        int d = extract_matrix_cmp4(&a_span->ctm, &b_span->ctm);
        if (d)
        {
            outf("extract_matrix_cmp4() returned non-zero.");
            outf("a_span->ctm=%s trm=%s: %s",
                    extract_matrix_string(&a_span->ctm),
                    extract_matrix_string(&a_span->trm),
                    extract_span_string(NULL, a_span)
                    );
            outf("b_span->ctm=%s trm=%s: %s",
                    extract_matrix_string(&b_span->ctm),
                    extract_matrix_string(&a_span->trm),
                    extract_span_string(NULL, b_span)
                    );
            return d;
        }
    }

    {
        double a_angle = line_angle(a_line);
        double b_angle = line_angle(b_line);
        if (fabs(a_angle - b_angle) > 3.14/2) {
            /* Give up if more than 90 deg. */
            return 0;
        }
        {
            double angle = (a_angle + b_angle) / 2;
            double ax = line_item_first(a_line)->x;
            double ay = line_item_first(a_line)->y;
            double bx = line_item_first(b_line)->x;
            double by = line_item_first(b_line)->y;
            double distance = line_distance_y(ax, ay, bx, by, angle);
            if (distance > 0)   return -1;
            if (distance < 0)   return +1;
        }
    }
    return 0;
}


/* Creates a representation of line_t's that consists of a list of
paragraph_t's.

We only join lines that are at the same angle and are adjacent.

On entry:
    Original value of *o_paragraphs and *o_paragraphs_num are ignored.

    <lines> points to array of <lines_num> line_t*'s, each pointing to
    a line_t.

On exit:
    On sucess, returns zero, *o_paragraphs points to array of *o_paragraphs_num
    paragraph_t*'s, each pointing to an paragraph_t. In the
    array, paragraph_t's with same angle are sorted.

    On failure, returns -1 with errno set. *o_paragraphs and *o_paragraphs_num
    are undefined.
*/
static int make_paragraphs(
        extract_alloc_t*    alloc, 
        line_t**            lines,
        int                 lines_num,
        paragraph_t***      o_paragraphs,
        int*                o_paragraphs_num
        )
{
    int ret = -1;
    int a;
    int num_joins;
    paragraph_t** paragraphs = NULL;

    /* Start off with a paragraph_t for each line_t. */
    int paragraphs_num = lines_num;
    if (extract_malloc(alloc, &paragraphs, sizeof(*paragraphs) * paragraphs_num)) goto end;
    /* Ensure we can clean up after error when setting up. */
    for (a=0; a<paragraphs_num; ++a) {
        paragraphs[a] = NULL;
    }
    /* Set up initial paragraphs. */
    for (a=0; a<paragraphs_num; ++a) {
        if (extract_malloc(alloc, &paragraphs[a], sizeof(paragraph_t))) goto end;
        paragraphs[a]->lines_num = 0;
        if (extract_malloc(alloc, &paragraphs[a]->lines, sizeof(line_t*) * 1)) goto end;
        paragraphs[a]->lines_num = 1;
        paragraphs[a]->lines[0] = lines[a];
    }

    /* Now join paragraphs together where possible. */
    num_joins = 0;
    for (a=0; a<paragraphs_num; ++a) {
        paragraph_t* nearest_paragraph = NULL;
        int nearest_paragraph_b = -1;
        double nearest_paragraph_distance = -1;
        line_t* line_a;
        double angle_a;
        int verbose;
        int b;
        
        paragraph_t* paragraph_a = paragraphs[a];
        if (!paragraph_a) {
            /* This paragraph is empty - already been appended to a different
            paragraph. */
            continue;
        }

        assert(paragraph_a->lines_num > 0);
        line_a = paragraph_line_last(paragraph_a);
        angle_a = line_angle(line_a);
        verbose = 0;

        /* Look for nearest paragraph_t that could be appended to
        paragraph_a. */
        for (b=0; b<paragraphs_num; ++b) {
            paragraph_t* paragraph_b = paragraphs[b];
            line_t* line_b;
            if (!paragraph_b) {
                /* This paragraph is empty - already been appended to a different
                paragraph. */
                continue;
            }
            line_b = paragraph_line_first(paragraph_b);
            if (!lines_are_compatible(line_a, line_b, angle_a, 0)) {
                continue;
            }

            {
                double ax = line_item_last(line_a)->x;
                double ay = line_item_last(line_a)->y;
                double bx = line_item_first(line_b)->x;
                double by = line_item_first(line_b)->y;
                double distance = line_distance_y(ax, ay, bx, by, angle_a);
                if (verbose) {
                    outf(
                            "angle_a=%f a=(%f %f) b=(%f %f) delta=(%f %f) distance=%f:",
                            angle_a * 180 / pi,
                            ax, ay,
                            bx, by,
                            bx - ax,
                            by - ay,
                            distance
                            );
                    outf("    line_a=%s", line_string2(alloc, line_a));
                    outf("    line_b=%s", line_string2(alloc, line_b));
                }
                if (distance > 0)
                {
                    if (nearest_paragraph_distance == -1
                            || distance < nearest_paragraph_distance)
                    {
                        int ok = 1;
                        if (0)
                        {
                            /* Check whether lines overlap horizontally. */
                            point_t a_left = char_to_point(line_item_first(line_a));
                            point_t b_left = char_to_point(line_item_first(line_b));
                            point_t a_right = char_to_point(line_item_last(line_a));
                            point_t b_right = char_to_point(line_item_last(line_b));

                            if (!lines_overlap(a_left, a_right, b_left, b_right, angle_a))
                            {
                                outf("Not joining lines because not overlapping.");
                                ok = 0;
                            }
                        }

                        if (ok)
                        {
                            if (verbose) {
                                outf("updating nearest. distance=%f:", distance);
                                outf("    line_a=%s", line_string2(alloc, line_a));
                                outf("    line_b=%s", line_string2(alloc, line_b));
                            }

                            nearest_paragraph_distance = distance;
                            nearest_paragraph_b = b;
                            nearest_paragraph = paragraph_b;
                        }
                    }
                }
            }
        }

        if (nearest_paragraph) {
            double line_b_size = line_font_size_max(
                    paragraph_line_first(nearest_paragraph)
                    );
            line_t* line_b = paragraph_line_first(nearest_paragraph);
            (void) line_b; /* Only used in outfx(). */
            if (nearest_paragraph_distance < 1.4 * line_b_size) {
                /* Paragraphs are close together vertically compared to maximum
                font size of first line in second paragraph, so we'll join them
                into a single paragraph. */
                span_t* a_span;
                int a_lines_num_new;
                if (verbose) {
                    outf(
                            "joing paragraphs. a=(%f,%f) b=(%f,%f) nearest_paragraph_distance=%f line_b_size=%f",
                            line_item_last(line_a)->x,
                            line_item_last(line_a)->y,
                            line_item_first(line_b)->x,
                            line_item_first(line_b)->y,
                            nearest_paragraph_distance,
                            line_b_size
                            );
                    outf("    %s", paragraph_string(alloc, paragraph_a));
                    outf("    %s", paragraph_string(alloc, nearest_paragraph));
                    outf("paragraph_a ctm=%s",
                            extract_matrix_string(&paragraph_a->lines[0]->spans[0]->ctm)
                            );
                    outf("paragraph_a trm=%s",
                            extract_matrix_string(&paragraph_a->lines[0]->spans[0]->trm)
                            );
                }
                /* Join these two paragraph_t's. */
                a_span = extract_line_span_last(line_a);
                if (extract_span_char_last(a_span)->ucs == '-'
                        || extract_span_char_last(a_span)->ucs == 0x2212 /* unicode dash */
                        )
                {
                    /* remove trailing '-' at end of prev line. char_t doesn't
                    contain any malloc-heap pointers so this doesn't leak. */
                    a_span->chars_num -= 1;
                }
                else if (extract_span_char_last(a_span)->ucs == ' ')
                {
                }
                else if (extract_span_char_last(a_span)->ucs == '/')
                {
                }
                else
                {
                    /* Insert space before joining adjacent lines. */
                    char_t* c_prev;
                    char_t* c;
                    if (extract_span_append_c(alloc, extract_line_span_last(line_a), ' ')) goto end;
                    c_prev = &a_span->chars[ a_span->chars_num-2];
                    c = &a_span->chars[ a_span->chars_num-1];
                    c->x = c_prev->x + c_prev->adv * a_span->ctm.a;
                    c->y = c_prev->y + c_prev->adv * a_span->ctm.c;
                }

                a_lines_num_new = paragraph_a->lines_num + nearest_paragraph->lines_num;
                if (extract_realloc2(
                        alloc,
                        &paragraph_a->lines,
                        sizeof(line_t*) * paragraph_a->lines_num,
                        sizeof(line_t*) * a_lines_num_new
                        )) goto end;
                {
                    int i;
                    for (i=0; i<nearest_paragraph->lines_num; ++i) {
                        paragraph_a->lines[paragraph_a->lines_num + i]
                            = nearest_paragraph->lines[i];
                    }
                }
                paragraph_a->lines_num = a_lines_num_new;

                /* Ensure that we skip nearest_paragraph in future. */
                extract_free(alloc, &nearest_paragraph->lines);
                extract_free(alloc, &nearest_paragraph);
                paragraphs[nearest_paragraph_b] = NULL;

                num_joins += 1;
                outfx(
                        "have joined paragraph a=%i to nearest_paragraph_b=%i. num_joins=%i.",
                        a,
                        nearest_paragraph_b,
                        num_joins
                        );

                if (nearest_paragraph_b > a) {
                    /* We haven't yet tried appending any paragraphs to
                    nearest_paragraph_b, so the new extended paragraph_a needs
                    checking again. */
                    a -= 1;
                }
            }
            else {
                outfx(
                        "Not joining paragraphs. nearest_paragraph_distance=%f line_b_size=%f",
                        nearest_paragraph_distance,
                        line_b_size
                        );
            }
        }
    }

    {
        /* Remove empty paragraphs. */
        int from;
        int to;
        int paragraphs_num_old;
        for (from=0, to=0; from<paragraphs_num; ++from) {
            if (paragraphs[from]) {
                paragraphs[to] = paragraphs[from];
                to += 1;
            }
        }
        outfx("paragraphs_num=%i => %i", paragraphs_num, to);
        paragraphs_num_old = paragraphs_num;
        paragraphs_num = to;
        if (extract_realloc2(
                alloc,
                &paragraphs,
                sizeof(paragraph_t*) * paragraphs_num_old,
                sizeof(paragraph_t*) * paragraphs_num
                )) {
            /* Should always succeed because we're not increasing allocation size, but
            can fail with memento squeeze. */
            goto end;
        }
    }

    /* Sort paragraphs so they appear in correct order, using paragraphs_cmp().
    */
    qsort(paragraphs, paragraphs_num, sizeof(paragraph_t*), paragraphs_cmp);

    *o_paragraphs = paragraphs;
    *o_paragraphs_num = paragraphs_num;
    ret = 0;
    outf("Turned %i lines into %i paragraphs", lines_num, paragraphs_num);

    end:

    if (ret)
    {
        if (paragraphs)
        {
            for (a=0; a<paragraphs_num; ++a)
            {
                if (paragraphs[a])   extract_free(alloc, &paragraphs[a]->lines);
                extract_free(alloc, &paragraphs[a]);
            }
        }
        extract_free(alloc, &paragraphs);
    }
    return ret;
}

static int s_join_page_rects(
        extract_alloc_t*    alloc,
        extract_page_t*     page,
        rect_t*             rects,
        int                 rects_num,
        line_t***           lines,
        int*                lines_num,
        paragraph_t***      paragraphs,
        int*                paragraphs_num
        )
/* Extracts text that is inside any of rects[0..rects_num], or all text if
rects_num is zero. */
{
    if (make_lines(
            alloc,
            page->spans,
            &page->spans_num,
            rects,
            rects_num,
            lines,
            lines_num
            )) return -1;
    if (make_paragraphs(
            alloc,
            *lines,
            *lines_num,
            paragraphs,
            paragraphs_num
            )) return -1;
    
    return 0;
}


static int tablelines_compare_x(const void* a, const void* b)
/* Compares two tableline_t's rectangles using x as primary key. */
{
    const tableline_t*  aa = a;
    const tableline_t*  bb = b;
    if (aa->rect.min.x > bb->rect.min.x)    return +1;
    if (aa->rect.min.x < bb->rect.min.x)    return -1;
    if (aa->rect.min.y > bb->rect.min.y)    return +1;
    if (aa->rect.min.y < bb->rect.min.y)    return -1;
    return 0;
}

static int tablelines_compare_y(const void* a, const void* b)
/* Compares two tableline_t's rectangles using y as primary key. */
{
    const tableline_t*  aa = a;
    const tableline_t*  bb = b;
    if (aa->rect.min.y > bb->rect.min.y)    return +1;
    if (aa->rect.min.y < bb->rect.min.y)    return -1;
    if (aa->rect.min.x > bb->rect.min.x)    return +1;
    if (aa->rect.min.x < bb->rect.min.x)    return -1;
    return 0;
}

static int table_find_y_range(extract_alloc_t* alloc, tablelines_t* all, double y_min, double y_max,
        tablelines_t* out)
/* Makes <out> to contain all lines in <all> with y coordinate in the range
y_min..y_max. */
{
    int i;
    for (i=0; i<all->tablelines_num; ++i)
    {
        if (all->tablelines[i].rect.min.y >= y_min && all->tablelines[i].rect.min.y < y_max)
        {
            if (extract_realloc(alloc, &out->tablelines, sizeof(*out->tablelines) * (out->tablelines_num + 1))) return -1;
            out->tablelines[out->tablelines_num] = all->tablelines[i];
            out->tablelines_num += 1;
        }
        else
        {
            outf("Excluding line because outside y=%f..%f: %s", y_min, y_max, extract_rect_string(&all->tablelines[i].rect));
        }
    }
    return 0;
}


static int overlap(double a_min, double a_max, double b_min, double b_max)
/* Returns one if a_min..a_max significantly overlapps b_min..b_max, otherwise
zero. */
{
    double overlap;
    int ret0;
    int ret1;
    assert(a_min < a_max);
    assert(b_min < b_max);
    if (b_min < a_min)  b_min = a_min;
    if (b_max > a_max)  b_max = a_max;
    if (b_max < b_min)  b_max = b_min;
    overlap = (b_max - b_min) / (a_max - a_min);
    ret0 = overlap > 0.2;
    ret1 = overlap > 0.8;
    if (ret0 != ret1)
    {
        if (0) outf0("warning, unclear overlap=%f: a=%f..%f b=%f..%f", overlap, a_min, a_max, b_min, b_max);
    }
    return overlap > 0.8;
}

void extract_cell_init(cell_t* cell)
{
    cell->rect.min.x = 0;
    cell->rect.min.y = 0;
    cell->rect.max.x = 0;
    cell->rect.max.y = 0;
    cell->above = 0;
    cell->left = 0;
    cell->extend_right = 0;
    cell->extend_down = 0;
    cell->lines = NULL;
    cell->lines_num = 0;
    cell->paragraphs = NULL;
    cell->paragraphs_num = 0;
}


static int table_find_extend(cell_t** cells, int cells_num_x, int cells_num_y)
{    
    /* Find cell extensions to right and down by looking at cells' .left and
    .above flags.
    
    For example for adjacent cells ABC..., we extend A to include cells BC..
    until we reach a cell with .left set to one.
    
    ABCDE
    FGHIJ
    KLMNO
    
    When looking to extend cell A, we only look at cells in the same column or
    same row, (i.e. in the above example we look at BCDE and FK, and not at
    GHIJ and LMNO).

    For example if BCDE have no left lines and FK have no above lines, we
    ignore any lines in GHIJ and LMNO and make A extend to the entire 3x4
    box. Having found this box, we set .above=0 and .left to 0 in all enclosed
    cells, which simplifies html table generation code.
    */
    int y;
    for (y=0; y<cells_num_y; ++y)
    {
        int x;
        for (x=0; x<cells_num_x; ++x)
        {
            cell_t* cell = cells[y * cells_num_x + x];
            outf("xy=(%i %i) above=%i left=%i", x, y, cell->above, cell->left);
            if (cell->left && cell->above)
            {
                /* See how far this cell extends to right and down. */
                int xx;
                int yy;
                for (xx=x+1; xx<cells_num_x; ++xx)
                {
                    if (cells[y * cells_num_x + xx]->left)  break;
                }
                cell->extend_right = xx - x;
                cell->rect.max.x = cells[y * cells_num_x + xx-1]->rect.max.x;
                for (yy=y+1; yy<cells_num_y; ++yy)
                {
                    if (cells[yy * cells_num_x + x]->above) break;
                }
                cell->extend_down = yy - y;
                cell->rect.max.y = cells[(yy-1) * cells_num_x + x]->rect.max.y;
                
                /* Clear .above and .left in enclosed cells. */
                for (xx = x; xx < x + cell->extend_right; ++xx)
                {
                    int yy;
                    for (yy = y; yy < y + cell->extend_down; ++yy)
                    {
                        cell_t* cell2 = cells[cells_num_x * yy  + xx];
                        if ( xx==x && yy==y)
                        {}
                        else
                        {
                            if (xx==x)
                            {
                                cell2->extend_right = cell->extend_right;
                            }
                            cell2->above = 0;
                            /* We set .left to 1 for left-most cells - e.g. F
                            and K in the above diagram; this allows us to
                            generate correct html without lots of recursing
                            looking for extend_down in earlier cells. */
                            cell2->left = (xx == x);
                            outf("xy=(%i %i) xxyy=(%i %i) have set cell2->above=%i left=%i",
                                    x, y, xx, yy, cell2->above, cell2->left
                                    );
                        }
                    }
                }
            }
        }
    }
    return 0;
}


static int table_find_cells_text(extract_alloc_t* alloc, extract_page_t* page,
        cell_t** cells, int cells_num_x, int cells_num_y)
/* Sets each cell to contain the text that is within the cell's boundary. We
remove any found text from the page. */
{
    /* Find text within each cell. We don't attempt to handle images within
    cells. */
    int e = -1;
    int i;
    int cells_num = cells_num_x * cells_num_y;
    for (i=0; i<cells_num; ++i)
    {
        cell_t* cell = cells[i];
        if (!cell->above || !cell->left) continue;
        if (s_join_page_rects(
                alloc,
                page,
                &cell->rect,
                1 /*rects_num*/,
                &cell->lines,
                &cell->lines_num,
                &cell->paragraphs,
                &cell->paragraphs_num
                )) return -1;
    }
    
    /* Append the table we have found to page->tables[]. */
    if (extract_realloc(alloc, &page->tables, sizeof(*page->tables) * (page->tables_num + 1))) goto end;
    if (extract_malloc(alloc, &page->tables[page->tables_num], sizeof(*page->tables[page->tables_num]))) goto end;
    page->tables[page->tables_num]->pos.x = cells[0]->rect.min.x;
    page->tables[page->tables_num]->pos.y = cells[0]->rect.min.y;
    page->tables[page->tables_num]->cells = cells;
    page->tables[page->tables_num]->cells_num_x = cells_num_x;
    page->tables[page->tables_num]->cells_num_y = cells_num_y;
    page->tables_num += 1;
    
    if (0)
    {
        /* For debugging. */
        int y;
        outf0("table:\n");
        for (y=0; y<cells_num_y; ++y)
        {
            int x;
            for (x=0; x<cells_num_x; ++x)
            {
                cell_t* cell = cells[cells_num_x * y + x];
                fprintf(stderr, "    %c%c x=%i y=% 3i 3i w=%i h=%i",
                        cell->left ? '|' : ' ',
                        cell->above ? '-' : ' ',
                        x,
                        y,
                        cell->extend_right,
                        cell->extend_down
                        );
            }
            fprintf(stderr, "\n");
        }
        
    }
    
    e = 0;
    end:
    return e;
}


static int table_find(extract_alloc_t* alloc, extract_page_t* page, double y_min, double y_max)
/* Finds single table made from lines whose y coordinates are in the range
y_min..y_max. */
{
    tablelines_t* all_h = &page->tablelines_horizontal;
    tablelines_t* all_v = &page->tablelines_vertical;
    int e = -1;
    int i;
    
    /* Find subset of vertical and horizontal lines that are within range
    y_min..y_max, and sort by y coordinate. */
    tablelines_t    tl_h = {NULL, 0};
    tablelines_t    tl_v = {NULL, 0};
    cell_t**    cells = NULL;
    int         cells_num = 0;
    int         cells_num_x = 0;
    int         cells_num_y = 0;
    int x;
    int y;

    outf("y=(%f %f)", y_min, y_max);
    
    if (table_find_y_range(alloc, all_h, y_min, y_max, &tl_h)) goto end;
    if (table_find_y_range(alloc, all_v, y_min, y_max, &tl_v)) goto end;
    /* Suppress false coverity warning - qsort() does not dereference null
    pointer if nmemb is zero. */
    /* coverity[var_deref_model] */
    qsort(tl_v.tablelines, tl_v.tablelines_num, sizeof(*tl_v.tablelines), tablelines_compare_x);
    
    if (0)
    {
        /* Show raw lines info. */
        outf0("all_h->tablelines_num=%i tl_h.tablelines_num=%i", all_h->tablelines_num, tl_h.tablelines_num);
        for (i=0; i<tl_h.tablelines_num; ++i)
        {
            outf0("    %i: %s", i, extract_rect_string(&tl_h.tablelines[i].rect));
        }

        outf0("all_v->tablelines_num=%i tl_v.tablelines_num=%i", all_v->tablelines_num, tl_v.tablelines_num);
        for (i=0; i<tl_v.tablelines_num; ++i)
        {
            outf0("    %i: %s", i, extract_rect_string(&tl_v.tablelines[i].rect));
        }
    }
    /* Find the cells defined by the vertical and horizontal lines.

    It seems that lines can be disjoint, e.g. what looks like a single
    horizontal line could be made up of multiple lines all with the same
    y coordinate, so we use i_next and j_next to skip these sublines when
    iterating. */
    cells = NULL;
    cells_num = 0;
    cells_num_x = 0;
    cells_num_y = 0;
    for (i=0; i<tl_h.tablelines_num; )
    {
        int i_next;
        int j;
        for (i_next=i+1; i_next<tl_h.tablelines_num; ++i_next)
        {
            if (tl_h.tablelines[i_next].rect.min.y - tl_h.tablelines[i].rect.min.y > 5) break;
        }
        if (i_next == tl_h.tablelines_num)
        {
            /* Ignore last row of points - cells need another row below. */
            break;
        }
        cells_num_y += 1;
        
        for (j=0; j<tl_v.tablelines_num; )
        {
            int j_next;
            int ii;
            int jj;
            cell_t* cell;
            
            for (j_next = j+1; j_next<tl_v.tablelines_num; ++j_next)
            {
                if (tl_v.tablelines[j_next].rect.min.x - tl_v.tablelines[j].rect.min.x > 0.5) break;
            }
            outf("i=%i j=%i tl_v.tablelines[j].rect=%s", i, j, extract_rect_string(&tl_v.tablelines[j].rect));
            
            if (j_next == tl_v.tablelines_num) break;
                        
            if (extract_realloc(alloc, &cells, sizeof(*cells) * (cells_num+1))) goto end;
            if (extract_malloc(alloc, &cells[cells_num], sizeof(*cells[cells_num]))) goto end;
            cell = cells[cells_num];
            cells_num += 1;
            if (i==0)   cells_num_x += 1;
            
            cell->rect.min.x = tl_v.tablelines[j].rect.min.x;
            cell->rect.min.y = tl_h.tablelines[i].rect.min.y;
            cell->rect.max.x = (j_next < tl_v.tablelines_num) ? tl_v.tablelines[j_next].rect.min.x : cell->rect.min.x;
            cell->rect.max.y = (i_next < tl_h.tablelines_num) ? tl_h.tablelines[i_next].rect.min.y : cell->rect.min.y;
            cell->above = (i==0);
            cell->left = (j==0);
            cell->extend_right = 1;
            cell->extend_down = 1;
            cell->lines = NULL;
            cell->lines_num = 0;
            cell->paragraphs = NULL;
            cell->paragraphs_num = 0;
            
            /* Set cell->above if there is a horizontal line above the cell. */
            outf("Looking to set above for i=%i j=%i rect=%s", i, j, extract_rect_string(&cell->rect));
            for (ii = i; ii < i_next; ++ii)
            {
                tableline_t* h = &tl_h.tablelines[ii];
                if (overlap(
                        cell->rect.min.x,
                        cell->rect.max.x,
                        h->rect.min.x,
                        h->rect.max.x
                        ))
                {
                    cell->above = 1;
                    break;
                }
            }
            
            /* Set cell->left if there is a vertical line to the left of the cell. */
            for (jj = j; jj < j_next; ++jj)
            {
                tableline_t* v = &tl_v.tablelines[jj];
                if (overlap(
                        cell->rect.min.y,
                        cell->rect.max.y,
                        v->rect.min.y,
                        v->rect.max.y
                        ))
                {
                    cell->left = 1;
                    break;
                }
            }
            
            j = j_next;
        }
        
        i = i_next;
    }
    
    assert(cells_num == cells_num_x * cells_num_y);
    
    /* Remove cols and rows where no cells have .above and .left - these
    will not appear. It also avoids spurious empty columns when table uses
    closely-spaced double lines as separators. */
    for (x=0; x<cells_num_x; ++x)
    {
        int has_cells = 0;
        for (y=0; y<cells_num_y; ++y)
        {
            cell_t* cell = cells[y * cells_num_x + x];
            if (cell->above && cell->left)
            {
                has_cells = 1;
                break;
            }
        }
        if (!has_cells)
        {
            /* Remove column <x>. */
            int j = 0;
            outf("Removing column %i. cells_num=%i cells_num_x=%i cells_num_y=%i", x, cells_num, cells_num_x, cells_num_y);
            for (i=0; i<cells_num; ++i)
            {
                if (i % cells_num_x == x)
                {
                    extract_cell_free(alloc, &cells[i]);
                    continue;
                }
                cells[j] = cells[i];
                j += 1;
            }
            cells_num -= cells_num_y;
            cells_num_x -= 1;
        }
    }
    
    if (cells_num == 0)
    {
        e = 0;
        goto end;
    }

    if (table_find_extend(cells, cells_num_x, cells_num_y)) goto end;
    
    if (table_find_cells_text(alloc, page, cells, cells_num_x, cells_num_y)) goto end;
    
    e = 0;
    end:
    extract_free(alloc, &tl_h.tablelines);
    extract_free(alloc, &tl_v.tablelines);
    if (e)
    {
        for (i=0; i<cells_num; ++i)
        {
            extract_cell_free(alloc, &cells[i]);
        }
        extract_free(alloc, &cells);
    }
    return e;
}


static int extract_page_tables_find_lines(
        extract_alloc_t*    alloc,
        extract_page_t*     page
        )
/* Finds tables in <page> by looking for lines in page->tablelines_horizontal
and page->tablelines_vertical that look like table dividers.

Any text found inside tables is removed from page->spans[].
*/
{
    double miny;
    double maxy;
    double margin = 1;
    int iv;
    int ih;
    outf("page->tablelines_horizontal.tablelines_num=%i", page->tablelines_horizontal.tablelines_num);
    outf("page->tablelines_vertical.tablelines_num=%i", page->tablelines_vertical.tablelines_num);
    
    /* Sort all lines by y coordinate. */
    qsort(
            page->tablelines_horizontal.tablelines,
            page->tablelines_horizontal.tablelines_num,
            sizeof(*page->tablelines_horizontal.tablelines),
            tablelines_compare_y
            );
    qsort(
            page->tablelines_vertical.tablelines,
            page->tablelines_vertical.tablelines_num,
            sizeof(*page->tablelines_vertical.tablelines),
            tablelines_compare_y
            );
    
    if (0)
    {
        /* Show info about lines. */
        int i;
        outf0("tablelines_horizontal:");
        for (i=0; i<page->tablelines_horizontal.tablelines_num; ++i)
        {
            outf0("    color=%f: %s",
                    page->tablelines_horizontal.tablelines[i].color,
                    extract_rect_string(&page->tablelines_horizontal.tablelines[i].rect)
                    );
        }
        outf0("tablelines_vertical:");
        for (i=0; i<page->tablelines_vertical.tablelines_num; ++i)
        {
            outf0("    color=%f: %s",
                    page->tablelines_vertical.tablelines[i].color,
                    extract_rect_string(&page->tablelines_vertical.tablelines[i].rect)
                    );
        }
    }
    
    /* Look for completely separate vertical regions that define different
    tables, by looking for vertical gaps between the rects of each
    horizontal/vertical line. */
    maxy = -DBL_MAX;
    miny = -DBL_MAX;
    iv = 0;
    ih = 0;
    for(;;)
    {
        tableline_t* tlv = NULL;
        tableline_t* tlh = NULL;
        tableline_t* tl;
        if (iv < page->tablelines_vertical.tablelines_num)
        {
            tlv = &page->tablelines_vertical.tablelines[iv];
        }
        /* We only consider horizontal lines that are not white. This is a bit
        of a cheat to get the right behaviour with twotables_2.pdf. */
        while (ih < page->tablelines_horizontal.tablelines_num)
        {
            if (page->tablelines_horizontal.tablelines[ih].color == 1)
            {
                /* Ignore white horizontal lines. */
                ++ih;
            }
            else
            {
                tlh = &page->tablelines_horizontal.tablelines[ih];
                break;
            }
        }
        if (tlv && tlh)
        {
            tl = (tlv->rect.min.y < tlh->rect.min.y) ? tlv : tlh;
        }
        else if (tlv) tl = tlv;
        else if (tlh) tl = tlh;
        else break;
        if (tl == tlv)  iv += 1;
        else ih += 1;
        if (tl->rect.min.y > maxy + margin)
        {
            if (maxy > miny)
            {
                outf("New table. maxy=%f miny=%f", maxy, miny);
                /* Find table. */
                table_find(alloc, page, miny - margin, maxy + margin);
            }
            miny = tl->rect.min.y;
        }
        if (tl->rect.max.y > maxy)  maxy = tl->rect.max.y;
    }
    
    /* Find last table. */
    table_find(alloc, page, miny - margin, maxy + margin);
    
    return 0;
}


static void show_tables(table_t** tables, int tables_num)
/* For debugging only. */
{
    int i;
    outf0("tables_num=%i", tables_num);
    for (i=0; i<tables_num; ++i)
    {
        table_t* table = tables[i];
        int y;
        outf0("table %i: cells_num_y=%i cells_num_x=%i", i, table->cells_num_y, table->cells_num_x);
        for (y=0; y<table->cells_num_y; ++y)
        {
            int x;
            for (x=0; x<table->cells_num_x; ++x)
            {
                cell_t* cell = table->cells[table->cells_num_x * y + x];
                outf0("cell: y=% 3i x=% 3i: left=%i above=%i rect=%s",
                        y, x, cell->left, cell->above, extract_rect_string(&cell->rect));
            }
        }
    }
}

static int extract_page_tables_find(
        extract_alloc_t*    alloc,
        extract_page_t*     page
        )
/* Find tables in <page>.

At the moment this only calls extract_page_tables_find_lines(), but in future
will call other functions that find tables in different ways, e.g. by analysing
an image of a page, or looking for blocks of whitespace in between chunks of
text. */
{
    if (extract_page_tables_find_lines(alloc, page)) return -1;

    if (0)
    {
        outf0("=== tables from extract_page_tables_find_lines():");
        show_tables(page->tables, page->tables_num);
    }

    return 0;
}

static int extract_document_join_page(
        extract_alloc_t*    alloc,
        extract_page_t*     page
        )
/* Finds tables and paragraphs on <page>. */
{
    /* Find tables on this page first. This will remove text that is within
    tables from page->spans, so that text doesn't appearing more than once in
    the final output. */
    if (extract_page_tables_find(alloc, page)) return -1;

    /* Now join remaining spans into lines and paragraphs. */
    if (s_join_page_rects(
            alloc,
            page,
            NULL /*rects*/,
            0 /*rects_num*/,
            &page->lines,
            &page->lines_num,
            &page->paragraphs,
            &page->paragraphs_num
            ))
    {
        outf0("s_join_page_rects failed. page->spans_num=%i page->lines_num=%i page->paragraphs_num=%i",
                page->spans_num,
                page->lines_num,
                page->paragraphs_num
                );
        return -1;
    }
    
    return 0;
}


int extract_document_join(extract_alloc_t* alloc, document_t* document)
{
    /* For each page in <document> we find tables and join spans into lines and paragraphs.

    A line is a list of spans that are at the same angle and on the same
    line. A paragraph is a list of lines that are at the same angle and close
    together.
    */
    int p;
    for (p=0; p<document->pages_num; ++p) {
        extract_page_t* page = document->pages[p];
        
        outf("processing page %i: num_spans=%i", p, page->spans_num);
        if (extract_document_join_page(alloc, page)) return -1;
    }

    return 0;
}