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
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
|
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include <algorithm>
#include <cctype>
#include <cwctype>
#include <memory>
#include <utility>
#include <vector>
#include "core/fpdfapi/fpdf_font/include/cpdf_font.h"
#include "core/fpdfapi/fpdf_page/include/cpdf_form.h"
#include "core/fpdfapi/fpdf_page/include/cpdf_formobject.h"
#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
#include "core/fpdfapi/fpdf_page/include/cpdf_pageobject.h"
#include "core/fpdfapi/fpdf_page/include/cpdf_textobject.h"
#include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h"
#include "core/fpdfapi/fpdf_parser/include/cpdf_string.h"
#include "core/fpdftext/include/cpdf_linkextract.h"
#include "core/fpdftext/include/cpdf_textpage.h"
#include "core/fpdftext/include/cpdf_textpagefind.h"
#include "core/fpdftext/unicodenormalizationdata.h"
#include "core/fxcrt/fx_bidi.h"
#include "core/fxcrt/include/fx_ext.h"
#include "core/fxcrt/include/fx_ucd.h"
#include "third_party/base/stl_util.h"
#define FPDFTEXT_MATCHCASE 0x00000001
#define FPDFTEXT_MATCHWHOLEWORD 0x00000002
#define FPDFTEXT_CONSECUTIVE 0x00000004
#define FPDFTEXT_CHAR_ERROR -1
#define FPDFTEXT_CHAR_NORMAL 0
#define FPDFTEXT_CHAR_GENERATED 1
#define FPDFTEXT_CHAR_UNUNICODE 2
#define FPDFTEXT_CHAR_HYPHEN 3
#define FPDFTEXT_CHAR_PIECE 4
#define TEXT_BLANK_CHAR L' '
#define TEXT_LINEFEED_CHAR L'\n'
#define TEXT_RETURN_CHAR L'\r'
#define TEXT_EMPTY L""
#define TEXT_BLANK L" "
#define TEXT_RETURN_LINEFEED L"\r\n"
#define TEXT_LINEFEED L"\n"
#define TEXT_CHARRATIO_GAPDELTA 0.070
namespace {
const FX_FLOAT kDefaultFontSize = 1.0f;
const uint16_t* const g_UnicodeData_Normalization_Maps[5] = {
nullptr, g_UnicodeData_Normalization_Map1, g_UnicodeData_Normalization_Map2,
g_UnicodeData_Normalization_Map3, g_UnicodeData_Normalization_Map4};
FX_BOOL IsIgnoreSpaceCharacter(FX_WCHAR curChar) {
if (curChar < 255) {
return FALSE;
}
if ((curChar >= 0x0600 && curChar <= 0x06FF) ||
(curChar >= 0xFE70 && curChar <= 0xFEFF) ||
(curChar >= 0xFB50 && curChar <= 0xFDFF) ||
(curChar >= 0x0400 && curChar <= 0x04FF) ||
(curChar >= 0x0500 && curChar <= 0x052F) ||
(curChar >= 0xA640 && curChar <= 0xA69F) ||
(curChar >= 0x2DE0 && curChar <= 0x2DFF) || curChar == 8467 ||
(curChar >= 0x2000 && curChar <= 0x206F)) {
return FALSE;
}
return TRUE;
}
FX_FLOAT NormalizeThreshold(FX_FLOAT threshold) {
if (threshold < 300) {
return threshold / 2.0f;
}
if (threshold < 500) {
return threshold / 4.0f;
}
if (threshold < 700) {
return threshold / 5.0f;
}
return threshold / 6.0f;
}
FX_FLOAT CalculateBaseSpace(const CPDF_TextObject* pTextObj,
const CFX_Matrix& matrix) {
FX_FLOAT baseSpace = 0.0;
const int nItems = pTextObj->CountItems();
if (pTextObj->m_TextState.GetObject()->m_CharSpace && nItems >= 3) {
FX_BOOL bAllChar = TRUE;
FX_FLOAT spacing = matrix.TransformDistance(
pTextObj->m_TextState.GetObject()->m_CharSpace);
baseSpace = spacing;
for (int i = 0; i < nItems; i++) {
CPDF_TextObjectItem item;
pTextObj->GetItemInfo(i, &item);
if (item.m_CharCode == (uint32_t)-1) {
FX_FLOAT fontsize_h = pTextObj->m_TextState.GetFontSizeH();
FX_FLOAT kerning = -fontsize_h * item.m_OriginX / 1000;
baseSpace = std::min(baseSpace, kerning + spacing);
bAllChar = FALSE;
}
}
if (baseSpace < 0.0 || (nItems == 3 && !bAllChar)) {
baseSpace = 0.0;
}
}
return baseSpace;
}
FX_STRSIZE Unicode_GetNormalization(FX_WCHAR wch, FX_WCHAR* pDst) {
wch = wch & 0xFFFF;
FX_WCHAR wFind = g_UnicodeData_Normalization[wch];
if (!wFind) {
if (pDst) {
*pDst = wch;
}
return 1;
}
if (wFind >= 0x8000) {
wch = wFind - 0x8000;
wFind = 1;
} else {
wch = wFind & 0x0FFF;
wFind >>= 12;
}
const uint16_t* pMap = g_UnicodeData_Normalization_Maps[wFind];
if (pMap == g_UnicodeData_Normalization_Map4) {
pMap = g_UnicodeData_Normalization_Map4 + wch;
wFind = (FX_WCHAR)(*pMap++);
} else {
pMap += wch;
}
if (pDst) {
FX_WCHAR n = wFind;
while (n--) {
*pDst++ = *pMap++;
}
}
return (FX_STRSIZE)wFind;
}
} // namespace
CPDF_TextPage::CPDF_TextPage(const CPDF_Page* pPage, FPDFText_Direction flags)
: m_pPage(pPage),
m_parserflag(flags),
m_pPreTextObj(nullptr),
m_bIsParsed(false),
m_TextlineDir(-1),
m_CurlineRect(0, 0, 0, 0) {
m_TextBuf.EstimateSize(0, 10240);
pPage->GetDisplayMatrix(m_DisplayMatrix, 0, 0, (int)pPage->GetPageWidth(),
(int)pPage->GetPageHeight(), 0);
}
bool CPDF_TextPage::IsControlChar(const PAGECHAR_INFO& charInfo) {
switch (charInfo.m_Unicode) {
case 0x2:
case 0x3:
case 0x93:
case 0x94:
case 0x96:
case 0x97:
case 0x98:
case 0xfffe:
return charInfo.m_Flag != FPDFTEXT_CHAR_HYPHEN;
default:
return false;
}
}
void CPDF_TextPage::ParseTextPage() {
m_bIsParsed = false;
m_TextBuf.Clear();
m_CharList.clear();
m_pPreTextObj = NULL;
ProcessObject();
m_bIsParsed = true;
m_CharIndex.clear();
int nCount = pdfium::CollectionSize<int>(m_CharList);
if (nCount) {
m_CharIndex.push_back(0);
}
for (int i = 0; i < nCount; i++) {
int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
FX_BOOL bNormal = FALSE;
const PAGECHAR_INFO& charinfo = m_CharList[i];
if (charinfo.m_Flag == FPDFTEXT_CHAR_GENERATED) {
bNormal = TRUE;
} else if (charinfo.m_Unicode == 0 || IsControlChar(charinfo)) {
bNormal = FALSE;
} else {
bNormal = TRUE;
}
if (bNormal) {
if (indexSize % 2) {
m_CharIndex.push_back(1);
} else {
if (indexSize <= 0) {
continue;
}
m_CharIndex[indexSize - 1] += 1;
}
} else {
if (indexSize % 2) {
if (indexSize <= 0) {
continue;
}
m_CharIndex[indexSize - 1] = i + 1;
} else {
m_CharIndex.push_back(i + 1);
}
}
}
int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
if (indexSize % 2) {
m_CharIndex.erase(m_CharIndex.begin() + indexSize - 1);
}
}
int CPDF_TextPage::CountChars() const {
return pdfium::CollectionSize<int>(m_CharList);
}
int CPDF_TextPage::CharIndexFromTextIndex(int TextIndex) const {
int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
int count = 0;
for (int i = 0; i < indexSize; i += 2) {
count += m_CharIndex[i + 1];
if (count > TextIndex)
return TextIndex - count + m_CharIndex[i + 1] + m_CharIndex[i];
}
return -1;
}
int CPDF_TextPage::TextIndexFromCharIndex(int CharIndex) const {
int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
int count = 0;
for (int i = 0; i < indexSize; i += 2) {
count += m_CharIndex[i + 1];
if (m_CharIndex[i + 1] + m_CharIndex[i] > CharIndex) {
if (CharIndex - m_CharIndex[i] < 0)
return -1;
return CharIndex - m_CharIndex[i] + count - m_CharIndex[i + 1];
}
}
return -1;
}
std::vector<CFX_FloatRect> CPDF_TextPage::GetRectArray(int start,
int nCount) const {
if (start < 0 || nCount == 0 || !m_bIsParsed)
return std::vector<CFX_FloatRect>();
if (nCount + start > pdfium::CollectionSize<int>(m_CharList) ||
nCount == -1) {
nCount = pdfium::CollectionSize<int>(m_CharList) - start;
}
std::vector<CFX_FloatRect> rectArray;
CPDF_TextObject* pCurObj = nullptr;
CFX_FloatRect rect;
int curPos = start;
FX_BOOL flagNewRect = TRUE;
while (nCount--) {
PAGECHAR_INFO info_curchar = m_CharList[curPos++];
if (info_curchar.m_Flag == FPDFTEXT_CHAR_GENERATED) {
continue;
}
if (info_curchar.m_CharBox.Width() < 0.01 ||
info_curchar.m_CharBox.Height() < 0.01) {
continue;
}
if (!pCurObj) {
pCurObj = info_curchar.m_pTextObj;
}
if (pCurObj != info_curchar.m_pTextObj) {
rectArray.push_back(rect);
pCurObj = info_curchar.m_pTextObj;
flagNewRect = TRUE;
}
if (flagNewRect) {
FX_FLOAT orgX = info_curchar.m_OriginX, orgY = info_curchar.m_OriginY;
CFX_Matrix matrix, matrix_reverse;
info_curchar.m_pTextObj->GetTextMatrix(&matrix);
matrix.Concat(info_curchar.m_Matrix);
matrix_reverse.SetReverse(matrix);
matrix_reverse.Transform(orgX, orgY);
rect.left = info_curchar.m_CharBox.left;
rect.right = info_curchar.m_CharBox.right;
if (pCurObj->GetFont()->GetTypeDescent()) {
rect.bottom = orgY +
pCurObj->GetFont()->GetTypeDescent() *
pCurObj->GetFontSize() / 1000;
FX_FLOAT xPosTemp = orgX;
matrix.Transform(xPosTemp, rect.bottom);
} else {
rect.bottom = info_curchar.m_CharBox.bottom;
}
if (pCurObj->GetFont()->GetTypeAscent()) {
rect.top =
orgY +
pCurObj->GetFont()->GetTypeAscent() * pCurObj->GetFontSize() / 1000;
FX_FLOAT xPosTemp =
orgX +
GetCharWidth(info_curchar.m_CharCode, pCurObj->GetFont()) *
pCurObj->GetFontSize() / 1000;
matrix.Transform(xPosTemp, rect.top);
} else {
rect.top = info_curchar.m_CharBox.top;
}
flagNewRect = FALSE;
rect = info_curchar.m_CharBox;
rect.Normalize();
} else {
info_curchar.m_CharBox.Normalize();
if (rect.left > info_curchar.m_CharBox.left) {
rect.left = info_curchar.m_CharBox.left;
}
if (rect.right < info_curchar.m_CharBox.right) {
rect.right = info_curchar.m_CharBox.right;
}
if (rect.top < info_curchar.m_CharBox.top) {
rect.top = info_curchar.m_CharBox.top;
}
if (rect.bottom > info_curchar.m_CharBox.bottom) {
rect.bottom = info_curchar.m_CharBox.bottom;
}
}
}
rectArray.push_back(rect);
return rectArray;
}
int CPDF_TextPage::GetIndexAtPos(CFX_FloatPoint point,
FX_FLOAT xTolerance,
FX_FLOAT yTolerance) const {
if (!m_bIsParsed)
return -3;
int pos = 0;
int NearPos = -1;
double xdif = 5000;
double ydif = 5000;
while (pos < pdfium::CollectionSize<int>(m_CharList)) {
PAGECHAR_INFO charinfo = m_CharList[pos];
CFX_FloatRect charrect = charinfo.m_CharBox;
if (charrect.Contains(point.x, point.y)) {
break;
}
if (xTolerance > 0 || yTolerance > 0) {
CFX_FloatRect charRectExt;
charrect.Normalize();
charRectExt.left = charrect.left - xTolerance / 2;
charRectExt.right = charrect.right + xTolerance / 2;
charRectExt.top = charrect.top + yTolerance / 2;
charRectExt.bottom = charrect.bottom - yTolerance / 2;
if (charRectExt.Contains(point.x, point.y)) {
double curXdif, curYdif;
curXdif = FXSYS_fabs(point.x - charrect.left) <
FXSYS_fabs(point.x - charrect.right)
? FXSYS_fabs(point.x - charrect.left)
: FXSYS_fabs(point.x - charrect.right);
curYdif = FXSYS_fabs(point.y - charrect.bottom) <
FXSYS_fabs(point.y - charrect.top)
? FXSYS_fabs(point.y - charrect.bottom)
: FXSYS_fabs(point.y - charrect.top);
if (curYdif + curXdif < xdif + ydif) {
ydif = curYdif;
xdif = curXdif;
NearPos = pos;
}
}
}
++pos;
}
return pos < pdfium::CollectionSize<int>(m_CharList) ? pos : NearPos;
}
CFX_WideString CPDF_TextPage::GetTextByRect(const CFX_FloatRect& rect) const {
if (!m_bIsParsed)
return CFX_WideString();
FX_FLOAT posy = 0;
bool IsContainPreChar = false;
bool IsAddLineFeed = false;
CFX_WideString strText;
for (const auto& charinfo : m_CharList) {
if (IsRectIntersect(rect, charinfo.m_CharBox)) {
if (FXSYS_fabs(posy - charinfo.m_OriginY) > 0 && !IsContainPreChar &&
IsAddLineFeed) {
posy = charinfo.m_OriginY;
if (strText.GetLength() > 0) {
strText += L"\r\n";
}
}
IsContainPreChar = true;
IsAddLineFeed = false;
if (charinfo.m_Unicode) {
strText += charinfo.m_Unicode;
}
} else if (charinfo.m_Unicode == 32) {
if (IsContainPreChar && charinfo.m_Unicode) {
strText += charinfo.m_Unicode;
IsContainPreChar = false;
IsAddLineFeed = false;
}
} else {
IsContainPreChar = false;
IsAddLineFeed = true;
}
}
return strText;
}
std::vector<CFX_FloatRect> CPDF_TextPage::GetRectsArrayByRect(
const CFX_FloatRect& rect) const {
if (!m_bIsParsed)
return std::vector<CFX_FloatRect>();
CFX_FloatRect curRect;
std::vector<CFX_FloatRect> result;
bool flagNewRect = true;
CPDF_TextObject* pCurObj = nullptr;
for (auto info_curchar : m_CharList) {
if (info_curchar.m_Flag == FPDFTEXT_CHAR_GENERATED) {
continue;
}
if (!IsRectIntersect(rect, info_curchar.m_CharBox)) {
continue;
}
if (!pCurObj) {
pCurObj = info_curchar.m_pTextObj;
}
if (pCurObj != info_curchar.m_pTextObj) {
result.push_back(curRect);
pCurObj = info_curchar.m_pTextObj;
flagNewRect = true;
}
if (flagNewRect) {
curRect = info_curchar.m_CharBox;
curRect.Normalize();
flagNewRect = false;
} else {
info_curchar.m_CharBox.Normalize();
curRect.left = std::min(curRect.left, info_curchar.m_CharBox.left);
curRect.bottom = std::min(curRect.bottom, info_curchar.m_CharBox.bottom);
curRect.right = std::max(curRect.right, info_curchar.m_CharBox.right);
curRect.top = std::max(curRect.top, info_curchar.m_CharBox.top);
}
}
result.push_back(curRect);
return result;
}
int CPDF_TextPage::GetIndexAtPos(FX_FLOAT x,
FX_FLOAT y,
FX_FLOAT xTolerance,
FX_FLOAT yTolerance) const {
CFX_FloatPoint point(x, y);
return GetIndexAtPos(point, xTolerance, yTolerance);
}
void CPDF_TextPage::GetCharInfo(int index, FPDF_CHAR_INFO* info) const {
if (!m_bIsParsed)
return;
if (index < 0 || index >= pdfium::CollectionSize<int>(m_CharList))
return;
const PAGECHAR_INFO& charinfo = m_CharList[index];
info->m_Charcode = charinfo.m_CharCode;
info->m_OriginX = charinfo.m_OriginX;
info->m_OriginY = charinfo.m_OriginY;
info->m_Unicode = charinfo.m_Unicode;
info->m_Flag = charinfo.m_Flag;
info->m_CharBox = charinfo.m_CharBox;
info->m_pTextObj = charinfo.m_pTextObj;
if (charinfo.m_pTextObj && charinfo.m_pTextObj->GetFont()) {
info->m_FontSize = charinfo.m_pTextObj->GetFontSize();
} else {
info->m_FontSize = kDefaultFontSize;
}
info->m_Matrix.Copy(charinfo.m_Matrix);
}
void CPDF_TextPage::CheckMarkedContentObject(int32_t& start,
int32_t& nCount) const {
PAGECHAR_INFO charinfo = m_CharList[start];
PAGECHAR_INFO charinfo2 = m_CharList[start + nCount - 1];
if (FPDFTEXT_CHAR_PIECE != charinfo.m_Flag &&
FPDFTEXT_CHAR_PIECE != charinfo2.m_Flag) {
return;
}
if (FPDFTEXT_CHAR_PIECE == charinfo.m_Flag) {
PAGECHAR_INFO charinfo1 = charinfo;
int startIndex = start;
while (FPDFTEXT_CHAR_PIECE == charinfo1.m_Flag &&
charinfo1.m_Index == charinfo.m_Index) {
startIndex--;
if (startIndex < 0) {
break;
}
charinfo1 = m_CharList[startIndex];
}
startIndex++;
start = startIndex;
}
if (FPDFTEXT_CHAR_PIECE == charinfo2.m_Flag) {
PAGECHAR_INFO charinfo3 = charinfo2;
int endIndex = start + nCount - 1;
while (FPDFTEXT_CHAR_PIECE == charinfo3.m_Flag &&
charinfo3.m_Index == charinfo2.m_Index) {
endIndex++;
if (endIndex >= pdfium::CollectionSize<int>(m_CharList)) {
break;
}
charinfo3 = m_CharList[endIndex];
}
endIndex--;
nCount = endIndex - start + 1;
}
}
CFX_WideString CPDF_TextPage::GetPageText(int start, int nCount) const {
if (!m_bIsParsed || nCount == 0)
return L"";
if (start < 0)
start = 0;
if (nCount == -1) {
nCount = pdfium::CollectionSize<int>(m_CharList) - start;
return m_TextBuf.AsStringC().Mid(start, m_TextBuf.AsStringC().GetLength());
}
if (nCount <= 0 || m_CharList.empty()) {
return L"";
}
if (nCount + start > pdfium::CollectionSize<int>(m_CharList) - 1) {
nCount = pdfium::CollectionSize<int>(m_CharList) - start;
}
if (nCount <= 0) {
return L"";
}
CheckMarkedContentObject(start, nCount);
int startindex = 0;
PAGECHAR_INFO charinfo = m_CharList[start];
int startOffset = 0;
while (charinfo.m_Index == -1) {
startOffset++;
if (startOffset > nCount ||
start + startOffset >= pdfium::CollectionSize<int>(m_CharList)) {
return L"";
}
charinfo = m_CharList[start + startOffset];
}
startindex = charinfo.m_Index;
charinfo = m_CharList[start + nCount - 1];
int nCountOffset = 0;
while (charinfo.m_Index == -1) {
nCountOffset++;
if (nCountOffset >= nCount) {
return L"";
}
charinfo = m_CharList[start + nCount - nCountOffset - 1];
}
nCount = start + nCount - nCountOffset - startindex;
if (nCount <= 0) {
return L"";
}
return m_TextBuf.AsStringC().Mid(startindex, nCount);
}
int CPDF_TextPage::CountRects(int start, int nCount) {
if (!m_bIsParsed || start < 0)
return -1;
if (nCount == -1 ||
nCount + start > pdfium::CollectionSize<int>(m_CharList)) {
nCount = pdfium::CollectionSize<int>(m_CharList) - start;
}
m_SelRects = GetRectArray(start, nCount);
return pdfium::CollectionSize<int>(m_SelRects);
}
void CPDF_TextPage::GetRect(int rectIndex,
FX_FLOAT& left,
FX_FLOAT& top,
FX_FLOAT& right,
FX_FLOAT& bottom) const {
if (!m_bIsParsed)
return;
if (rectIndex < 0 || rectIndex >= pdfium::CollectionSize<int>(m_SelRects))
return;
left = m_SelRects[rectIndex].left;
top = m_SelRects[rectIndex].top;
right = m_SelRects[rectIndex].right;
bottom = m_SelRects[rectIndex].bottom;
}
int CPDF_TextPage::CountBoundedSegments(FX_FLOAT left,
FX_FLOAT top,
FX_FLOAT right,
FX_FLOAT bottom,
FX_BOOL bContains) {
m_Segments.RemoveAll();
if (!m_bIsParsed)
return -1;
CFX_FloatRect rect(left, bottom, right, top);
rect.Normalize();
FPDF_SEGMENT segment;
segment.m_Start = 0;
segment.m_nCount = 0;
int pos = 0;
int segmentStatus = 0;
FX_BOOL IsContainPreChar = FALSE;
for (const auto& charinfo : m_CharList) {
if (bContains && rect.Contains(charinfo.m_CharBox)) {
if (segmentStatus == 0 || segmentStatus == 2) {
segment.m_Start = pos;
segment.m_nCount = 1;
segmentStatus = 1;
} else if (segmentStatus == 1) {
segment.m_nCount++;
}
IsContainPreChar = TRUE;
} else if (!bContains &&
(IsRectIntersect(rect, charinfo.m_CharBox) ||
rect.Contains(charinfo.m_OriginX, charinfo.m_OriginY))) {
if (segmentStatus == 0 || segmentStatus == 2) {
segment.m_Start = pos;
segment.m_nCount = 1;
segmentStatus = 1;
} else if (segmentStatus == 1) {
segment.m_nCount++;
}
IsContainPreChar = TRUE;
} else if (charinfo.m_Unicode == 32) {
if (IsContainPreChar == TRUE) {
if (segmentStatus == 0 || segmentStatus == 2) {
segment.m_Start = pos;
segment.m_nCount = 1;
segmentStatus = 1;
} else if (segmentStatus == 1) {
segment.m_nCount++;
}
IsContainPreChar = FALSE;
} else {
if (segmentStatus == 1) {
segmentStatus = 2;
m_Segments.Add(segment);
segment.m_Start = 0;
segment.m_nCount = 0;
}
}
} else {
if (segmentStatus == 1) {
segmentStatus = 2;
m_Segments.Add(segment);
segment.m_Start = 0;
segment.m_nCount = 0;
}
IsContainPreChar = FALSE;
}
pos++;
}
if (segmentStatus == 1) {
segmentStatus = 2;
m_Segments.Add(segment);
segment.m_Start = 0;
segment.m_nCount = 0;
}
return m_Segments.GetSize();
}
int32_t CPDF_TextPage::FindTextlineFlowDirection() {
const int32_t nPageWidth = static_cast<int32_t>(m_pPage->GetPageWidth());
const int32_t nPageHeight = static_cast<int32_t>(m_pPage->GetPageHeight());
std::vector<uint8_t> nHorizontalMask(nPageWidth);
std::vector<uint8_t> nVerticalMask(nPageHeight);
uint8_t* pDataH = nHorizontalMask.data();
uint8_t* pDataV = nVerticalMask.data();
int32_t index = 0;
FX_FLOAT fLineHeight = 0.0f;
if (m_pPage->GetPageObjectList()->empty())
return -1;
for (auto& pPageObj : *m_pPage->GetPageObjectList()) {
if (!pPageObj || !pPageObj->IsText())
continue;
int32_t minH =
(int32_t)pPageObj->m_Left < 0 ? 0 : (int32_t)pPageObj->m_Left;
int32_t maxH = (int32_t)pPageObj->m_Right > nPageWidth
? nPageWidth
: (int32_t)pPageObj->m_Right;
int32_t minV =
(int32_t)pPageObj->m_Bottom < 0 ? 0 : (int32_t)pPageObj->m_Bottom;
int32_t maxV = (int32_t)pPageObj->m_Top > nPageHeight
? nPageHeight
: (int32_t)pPageObj->m_Top;
if (minH >= maxH || minV >= maxV)
continue;
FXSYS_memset(pDataH + minH, 1, maxH - minH);
FXSYS_memset(pDataV + minV, 1, maxV - minV);
if (fLineHeight <= 0.0f)
fLineHeight = pPageObj->m_Top - pPageObj->m_Bottom;
}
int32_t nStartH = 0;
int32_t nEndH = 0;
FX_FLOAT nSumH = 0.0f;
for (index = 0; index < nPageWidth; index++) {
if (1 == nHorizontalMask[index])
break;
}
nStartH = index;
for (index = nPageWidth; index > 0; index--) {
if (1 == nHorizontalMask[index - 1])
break;
}
nEndH = index;
for (index = nStartH; index < nEndH; index++) {
nSumH += nHorizontalMask[index];
}
nSumH /= nEndH - nStartH;
int32_t nStartV = 0;
int32_t nEndV = 0;
FX_FLOAT nSumV = 0.0f;
for (index = 0; index < nPageHeight; index++) {
if (1 == nVerticalMask[index])
break;
}
nStartV = index;
for (index = nPageHeight; index > 0; index--) {
if (1 == nVerticalMask[index - 1])
break;
}
nEndV = index;
for (index = nStartV; index < nEndV; index++) {
nSumV += nVerticalMask[index];
}
nSumV /= nEndV - nStartV;
if ((nEndV - nStartV) < (int32_t)(2 * fLineHeight)) {
return 0;
}
if ((nEndH - nStartH) < (int32_t)(2 * fLineHeight)) {
return 1;
}
if (nSumH > 0.8f) {
return 0;
}
if (nSumH - nSumV > 0.0f) {
return 0;
}
if (nSumV - nSumH > 0.0f) {
return 1;
}
return -1;
}
void CPDF_TextPage::ProcessObject() {
if (m_pPage->GetPageObjectList()->empty())
return;
m_TextlineDir = FindTextlineFlowDirection();
const CPDF_PageObjectList* pObjList = m_pPage->GetPageObjectList();
for (auto it = pObjList->begin(); it != pObjList->end(); ++it) {
if (CPDF_PageObject* pObj = it->get()) {
if (pObj->IsText()) {
CFX_Matrix matrix;
ProcessTextObject(pObj->AsText(), matrix, pObjList, it);
} else if (pObj->IsForm()) {
CFX_Matrix formMatrix(1, 0, 0, 1, 0, 0);
ProcessFormObject(pObj->AsForm(), formMatrix);
}
}
}
for (int i = 0; i < m_LineObj.GetSize(); i++)
ProcessTextObject(m_LineObj.GetAt(i));
m_LineObj.RemoveAll();
CloseTempLine();
}
void CPDF_TextPage::ProcessFormObject(CPDF_FormObject* pFormObj,
const CFX_Matrix& formMatrix) {
CPDF_PageObjectList* pObjectList = pFormObj->m_pForm->GetPageObjectList();
if (pObjectList->empty())
return;
CFX_Matrix curFormMatrix;
curFormMatrix.Copy(pFormObj->m_FormMatrix);
curFormMatrix.Concat(formMatrix);
for (auto it = pObjectList->begin(); it != pObjectList->end(); ++it) {
if (CPDF_PageObject* pPageObj = it->get()) {
if (pPageObj->IsText()) {
ProcessTextObject(pPageObj->AsText(), curFormMatrix, pObjectList, it);
} else if (pPageObj->IsForm()) {
ProcessFormObject(pPageObj->AsForm(), curFormMatrix);
}
}
}
}
int CPDF_TextPage::GetCharWidth(uint32_t charCode, CPDF_Font* pFont) const {
if (charCode == CPDF_Font::kInvalidCharCode)
return 0;
if (int w = pFont->GetCharWidthF(charCode))
return w;
CFX_ByteString str;
pFont->AppendChar(str, charCode);
if (int w = pFont->GetStringWidth(str.c_str(), 1))
return w;
return pFont->GetCharBBox(charCode).Width();
}
void CPDF_TextPage::AddCharInfoByLRDirection(FX_WCHAR wChar,
PAGECHAR_INFO info) {
if (!IsControlChar(info)) {
info.m_Index = m_TextBuf.GetLength();
if (wChar >= 0xFB00 && wChar <= 0xFB06) {
FX_WCHAR* pDst = NULL;
FX_STRSIZE nCount = Unicode_GetNormalization(wChar, pDst);
if (nCount >= 1) {
pDst = FX_Alloc(FX_WCHAR, nCount);
Unicode_GetNormalization(wChar, pDst);
for (int nIndex = 0; nIndex < nCount; nIndex++) {
PAGECHAR_INFO info2 = info;
info2.m_Unicode = pDst[nIndex];
info2.m_Flag = FPDFTEXT_CHAR_PIECE;
m_TextBuf.AppendChar(info2.m_Unicode);
m_CharList.push_back(info2);
}
FX_Free(pDst);
return;
}
}
m_TextBuf.AppendChar(wChar);
} else {
info.m_Index = -1;
}
m_CharList.push_back(info);
}
void CPDF_TextPage::AddCharInfoByRLDirection(FX_WCHAR wChar,
PAGECHAR_INFO info) {
if (!IsControlChar(info)) {
info.m_Index = m_TextBuf.GetLength();
wChar = FX_GetMirrorChar(wChar, TRUE, FALSE);
FX_WCHAR* pDst = NULL;
FX_STRSIZE nCount = Unicode_GetNormalization(wChar, pDst);
if (nCount >= 1) {
pDst = FX_Alloc(FX_WCHAR, nCount);
Unicode_GetNormalization(wChar, pDst);
for (int nIndex = 0; nIndex < nCount; nIndex++) {
PAGECHAR_INFO info2 = info;
info2.m_Unicode = pDst[nIndex];
info2.m_Flag = FPDFTEXT_CHAR_PIECE;
m_TextBuf.AppendChar(info2.m_Unicode);
m_CharList.push_back(info2);
}
FX_Free(pDst);
return;
}
info.m_Unicode = wChar;
m_TextBuf.AppendChar(info.m_Unicode);
} else {
info.m_Index = -1;
}
m_CharList.push_back(info);
}
void CPDF_TextPage::CloseTempLine() {
if (m_TempCharList.empty())
return;
CFX_WideString str = m_TempTextBuf.AsStringC();
FX_BOOL bPrevSpace = FALSE;
for (int i = 0; i < str.GetLength(); i++) {
if (str.GetAt(i) != ' ') {
bPrevSpace = FALSE;
continue;
}
if (bPrevSpace) {
m_TempTextBuf.Delete(i, 1);
m_TempCharList.erase(m_TempCharList.begin() + i);
str.Delete(i);
i--;
}
bPrevSpace = TRUE;
}
CFX_BidiString bidi(str);
if (m_parserflag == FPDFText_Direction::Right)
bidi.SetOverallDirectionRight();
CFX_BidiChar::Direction eCurrentDirection = bidi.OverallDirection();
for (const auto& segment : bidi) {
if (segment.direction == CFX_BidiChar::RIGHT ||
(segment.direction == CFX_BidiChar::NEUTRAL &&
eCurrentDirection == CFX_BidiChar::RIGHT)) {
eCurrentDirection = CFX_BidiChar::RIGHT;
for (int m = segment.start + segment.count; m > segment.start; --m)
AddCharInfoByRLDirection(bidi.CharAt(m - 1), m_TempCharList[m - 1]);
} else {
eCurrentDirection = CFX_BidiChar::LEFT;
for (int m = segment.start; m < segment.start + segment.count; m++)
AddCharInfoByLRDirection(bidi.CharAt(m), m_TempCharList[m]);
}
}
m_TempCharList.clear();
m_TempTextBuf.Delete(0, m_TempTextBuf.GetLength());
}
void CPDF_TextPage::ProcessTextObject(
CPDF_TextObject* pTextObj,
const CFX_Matrix& formMatrix,
const CPDF_PageObjectList* pObjList,
CPDF_PageObjectList::const_iterator ObjPos) {
CFX_FloatRect re(pTextObj->m_Left, pTextObj->m_Bottom, pTextObj->m_Right,
pTextObj->m_Top);
if (FXSYS_fabs(pTextObj->m_Right - pTextObj->m_Left) < 0.01f) {
return;
}
int count = m_LineObj.GetSize();
PDFTEXT_Obj Obj;
Obj.m_pTextObj = pTextObj;
Obj.m_formMatrix = formMatrix;
if (count == 0) {
m_LineObj.Add(Obj);
return;
}
if (IsSameAsPreTextObject(pTextObj, pObjList, ObjPos)) {
return;
}
PDFTEXT_Obj prev_Obj = m_LineObj.GetAt(count - 1);
CPDF_TextObjectItem item;
int nItem = prev_Obj.m_pTextObj->CountItems();
prev_Obj.m_pTextObj->GetItemInfo(nItem - 1, &item);
FX_FLOAT prev_width =
GetCharWidth(item.m_CharCode, prev_Obj.m_pTextObj->GetFont()) *
prev_Obj.m_pTextObj->GetFontSize() / 1000;
CFX_Matrix prev_matrix;
prev_Obj.m_pTextObj->GetTextMatrix(&prev_matrix);
prev_width = FXSYS_fabs(prev_width);
prev_matrix.Concat(prev_Obj.m_formMatrix);
prev_width = prev_matrix.TransformDistance(prev_width);
pTextObj->GetItemInfo(0, &item);
FX_FLOAT this_width = GetCharWidth(item.m_CharCode, pTextObj->GetFont()) *
pTextObj->GetFontSize() / 1000;
this_width = FXSYS_fabs(this_width);
CFX_Matrix this_matrix;
pTextObj->GetTextMatrix(&this_matrix);
this_width = FXSYS_fabs(this_width);
this_matrix.Concat(formMatrix);
this_width = this_matrix.TransformDistance(this_width);
FX_FLOAT threshold =
prev_width > this_width ? prev_width / 4 : this_width / 4;
FX_FLOAT prev_x = prev_Obj.m_pTextObj->GetPosX(),
prev_y = prev_Obj.m_pTextObj->GetPosY();
prev_Obj.m_formMatrix.Transform(prev_x, prev_y);
m_DisplayMatrix.Transform(prev_x, prev_y);
FX_FLOAT this_x = pTextObj->GetPosX(), this_y = pTextObj->GetPosY();
formMatrix.Transform(this_x, this_y);
m_DisplayMatrix.Transform(this_x, this_y);
if (FXSYS_fabs(this_y - prev_y) > threshold * 2) {
for (int i = 0; i < count; i++) {
ProcessTextObject(m_LineObj.GetAt(i));
}
m_LineObj.RemoveAll();
m_LineObj.Add(Obj);
return;
}
int i = 0;
for (i = count - 1; i >= 0; i--) {
PDFTEXT_Obj prev_Obj = m_LineObj.GetAt(i);
CFX_Matrix prev_matrix;
prev_Obj.m_pTextObj->GetTextMatrix(&prev_matrix);
FX_FLOAT Prev_x = prev_Obj.m_pTextObj->GetPosX(),
Prev_y = prev_Obj.m_pTextObj->GetPosY();
prev_Obj.m_formMatrix.Transform(Prev_x, Prev_y);
m_DisplayMatrix.Transform(Prev_x, Prev_y);
if (this_x >= Prev_x) {
if (i == count - 1) {
m_LineObj.Add(Obj);
} else {
m_LineObj.InsertAt(i + 1, Obj);
}
break;
}
}
if (i < 0) {
m_LineObj.InsertAt(0, Obj);
}
}
FPDFText_MarkedContent CPDF_TextPage::PreMarkedContent(PDFTEXT_Obj Obj) {
CPDF_TextObject* pTextObj = Obj.m_pTextObj;
CPDF_ContentMarkData* pMarkData =
(CPDF_ContentMarkData*)pTextObj->m_ContentMark.GetObject();
if (!pMarkData)
return FPDFText_MarkedContent::Pass;
int nContentMark = pMarkData->CountItems();
if (nContentMark < 1)
return FPDFText_MarkedContent::Pass;
CFX_WideString actText;
FX_BOOL bExist = FALSE;
CPDF_Dictionary* pDict = NULL;
int n = 0;
for (n = 0; n < nContentMark; n++) {
CPDF_ContentMarkItem& item = pMarkData->GetItem(n);
if (item.GetParamType() == CPDF_ContentMarkItem::ParamType::None)
continue;
pDict = item.GetParam();
CPDF_String* temp =
ToString(pDict ? pDict->GetObjectBy("ActualText") : nullptr);
if (temp) {
bExist = TRUE;
actText = temp->GetUnicodeText();
}
}
if (!bExist)
return FPDFText_MarkedContent::Pass;
if (m_pPreTextObj) {
CPDF_ContentMarkData* pPreMarkData =
(CPDF_ContentMarkData*)m_pPreTextObj->m_ContentMark.GetObject();
if (pPreMarkData && pPreMarkData->CountItems() == n &&
pDict == pPreMarkData->GetItem(n - 1).GetParam()) {
return FPDFText_MarkedContent::Done;
}
}
FX_STRSIZE nItems = actText.GetLength();
if (nItems < 1)
return FPDFText_MarkedContent::Pass;
CPDF_Font* pFont = pTextObj->GetFont();
bExist = FALSE;
for (FX_STRSIZE i = 0; i < nItems; i++) {
if (pFont->CharCodeFromUnicode(actText.GetAt(i)) !=
CPDF_Font::kInvalidCharCode) {
bExist = TRUE;
break;
}
}
if (!bExist)
return FPDFText_MarkedContent::Pass;
bExist = FALSE;
for (FX_STRSIZE i = 0; i < nItems; i++) {
FX_WCHAR wChar = actText.GetAt(i);
if ((wChar > 0x80 && wChar < 0xFFFD) || (wChar <= 0x80 && isprint(wChar))) {
bExist = TRUE;
break;
}
}
if (!bExist)
return FPDFText_MarkedContent::Done;
return FPDFText_MarkedContent::Delay;
}
void CPDF_TextPage::ProcessMarkedContent(PDFTEXT_Obj Obj) {
CPDF_TextObject* pTextObj = Obj.m_pTextObj;
CPDF_ContentMarkData* pMarkData =
(CPDF_ContentMarkData*)pTextObj->m_ContentMark.GetObject();
if (!pMarkData)
return;
int nContentMark = pMarkData->CountItems();
if (nContentMark < 1)
return;
CFX_WideString actText;
CPDF_Dictionary* pDict = NULL;
for (int n = 0; n < nContentMark; n++) {
CPDF_ContentMarkItem& item = pMarkData->GetItem(n);
if (item.GetParamType() == CPDF_ContentMarkItem::ParamType::None)
continue;
pDict = item.GetParam();
if (pDict)
actText = pDict->GetUnicodeTextBy("ActualText");
}
FX_STRSIZE nItems = actText.GetLength();
if (nItems < 1)
return;
CPDF_Font* pFont = pTextObj->GetFont();
CFX_Matrix formMatrix = Obj.m_formMatrix;
CFX_Matrix matrix;
pTextObj->GetTextMatrix(&matrix);
matrix.Concat(formMatrix);
FX_FLOAT fPosX = pTextObj->GetPosX();
FX_FLOAT fPosY = pTextObj->GetPosY();
int nCharInfoIndex = m_TextBuf.GetLength();
CFX_FloatRect charBox;
charBox.top = pTextObj->m_Top;
charBox.left = pTextObj->m_Left;
charBox.right = pTextObj->m_Right;
charBox.bottom = pTextObj->m_Bottom;
for (FX_STRSIZE k = 0; k < nItems; k++) {
FX_WCHAR wChar = actText.GetAt(k);
if (wChar <= 0x80 && !isprint(wChar)) {
wChar = 0x20;
}
if (wChar >= 0xFFFD) {
continue;
}
PAGECHAR_INFO charinfo;
charinfo.m_OriginX = fPosX;
charinfo.m_OriginY = fPosY;
charinfo.m_Index = nCharInfoIndex;
charinfo.m_Unicode = wChar;
charinfo.m_CharCode = pFont->CharCodeFromUnicode(wChar);
charinfo.m_Flag = FPDFTEXT_CHAR_PIECE;
charinfo.m_pTextObj = pTextObj;
charinfo.m_CharBox.top = charBox.top;
charinfo.m_CharBox.left = charBox.left;
charinfo.m_CharBox.right = charBox.right;
charinfo.m_CharBox.bottom = charBox.bottom;
charinfo.m_Matrix.Copy(matrix);
m_TempTextBuf.AppendChar(wChar);
m_TempCharList.push_back(charinfo);
}
}
void CPDF_TextPage::FindPreviousTextObject() {
if (m_TempCharList.empty() && m_CharList.empty())
return;
PAGECHAR_INFO preChar =
m_TempCharList.empty() ? m_CharList.back() : m_TempCharList.back();
if (preChar.m_pTextObj)
m_pPreTextObj = preChar.m_pTextObj;
}
void CPDF_TextPage::SwapTempTextBuf(int32_t iCharListStartAppend,
int32_t iBufStartAppend) {
int32_t i = iCharListStartAppend;
int32_t j = pdfium::CollectionSize<int32_t>(m_TempCharList) - 1;
for (; i < j; i++, j--) {
std::swap(m_TempCharList[i], m_TempCharList[j]);
std::swap(m_TempCharList[i].m_Index, m_TempCharList[j].m_Index);
}
FX_WCHAR* pTempBuffer = m_TempTextBuf.GetBuffer();
i = iBufStartAppend;
j = m_TempTextBuf.GetLength() - 1;
for (; i < j; i++, j--) {
std::swap(pTempBuffer[i], pTempBuffer[j]);
}
}
FX_BOOL CPDF_TextPage::IsRightToLeft(const CPDF_TextObject* pTextObj,
const CPDF_Font* pFont,
int nItems) const {
CFX_WideString str;
for (int32_t i = 0; i < nItems; i++) {
CPDF_TextObjectItem item;
pTextObj->GetItemInfo(i, &item);
if (item.m_CharCode == (uint32_t)-1) {
continue;
}
CFX_WideString wstrItem = pFont->UnicodeFromCharCode(item.m_CharCode);
FX_WCHAR wChar = wstrItem.GetAt(0);
if ((wstrItem.IsEmpty() || wChar == 0) && item.m_CharCode) {
wChar = (FX_WCHAR)item.m_CharCode;
}
if (wChar)
str += wChar;
}
return CFX_BidiString(str).OverallDirection() == CFX_BidiChar::RIGHT;
}
void CPDF_TextPage::ProcessTextObject(PDFTEXT_Obj Obj) {
CPDF_TextObject* pTextObj = Obj.m_pTextObj;
if (FXSYS_fabs(pTextObj->m_Right - pTextObj->m_Left) < 0.01f) {
return;
}
CFX_Matrix formMatrix = Obj.m_formMatrix;
CPDF_Font* pFont = pTextObj->GetFont();
CFX_Matrix matrix;
pTextObj->GetTextMatrix(&matrix);
matrix.Concat(formMatrix);
FPDFText_MarkedContent ePreMKC = PreMarkedContent(Obj);
if (ePreMKC == FPDFText_MarkedContent::Done) {
m_pPreTextObj = pTextObj;
m_perMatrix.Copy(formMatrix);
return;
}
int result = 0;
if (m_pPreTextObj) {
result = ProcessInsertObject(pTextObj, formMatrix);
if (2 == result) {
m_CurlineRect =
CFX_FloatRect(Obj.m_pTextObj->m_Left, Obj.m_pTextObj->m_Bottom,
Obj.m_pTextObj->m_Right, Obj.m_pTextObj->m_Top);
} else {
m_CurlineRect.Union(
CFX_FloatRect(Obj.m_pTextObj->m_Left, Obj.m_pTextObj->m_Bottom,
Obj.m_pTextObj->m_Right, Obj.m_pTextObj->m_Top));
}
PAGECHAR_INFO generateChar;
if (result == 1) {
if (GenerateCharInfo(TEXT_BLANK_CHAR, generateChar)) {
if (!formMatrix.IsIdentity()) {
generateChar.m_Matrix.Copy(formMatrix);
}
m_TempTextBuf.AppendChar(TEXT_BLANK_CHAR);
m_TempCharList.push_back(generateChar);
}
} else if (result == 2) {
CloseTempLine();
if (m_TextBuf.GetSize()) {
if (GenerateCharInfo(TEXT_RETURN_CHAR, generateChar)) {
m_TextBuf.AppendChar(TEXT_RETURN_CHAR);
if (!formMatrix.IsIdentity()) {
generateChar.m_Matrix.Copy(formMatrix);
}
m_CharList.push_back(generateChar);
}
if (GenerateCharInfo(TEXT_LINEFEED_CHAR, generateChar)) {
m_TextBuf.AppendChar(TEXT_LINEFEED_CHAR);
if (!formMatrix.IsIdentity()) {
generateChar.m_Matrix.Copy(formMatrix);
}
m_CharList.push_back(generateChar);
}
}
} else if (result == 3) {
int32_t nChars = pTextObj->CountChars();
if (nChars == 1) {
CPDF_TextObjectItem item;
pTextObj->GetCharInfo(0, &item);
CFX_WideString wstrItem =
pTextObj->GetFont()->UnicodeFromCharCode(item.m_CharCode);
if (wstrItem.IsEmpty()) {
wstrItem += (FX_WCHAR)item.m_CharCode;
}
FX_WCHAR curChar = wstrItem.GetAt(0);
if (0x2D == curChar || 0xAD == curChar) {
return;
}
}
while (m_TempTextBuf.GetSize() > 0 &&
m_TempTextBuf.AsStringC().GetAt(m_TempTextBuf.GetLength() - 1) ==
0x20) {
m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1);
m_TempCharList.pop_back();
}
PAGECHAR_INFO* charinfo = &m_TempCharList.back();
m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1);
charinfo->m_Unicode = 0x2;
charinfo->m_Flag = FPDFTEXT_CHAR_HYPHEN;
m_TempTextBuf.AppendChar(0xfffe);
}
} else {
m_CurlineRect =
CFX_FloatRect(Obj.m_pTextObj->m_Left, Obj.m_pTextObj->m_Bottom,
Obj.m_pTextObj->m_Right, Obj.m_pTextObj->m_Top);
}
if (ePreMKC == FPDFText_MarkedContent::Delay) {
ProcessMarkedContent(Obj);
m_pPreTextObj = pTextObj;
m_perMatrix.Copy(formMatrix);
return;
}
m_pPreTextObj = pTextObj;
m_perMatrix.Copy(formMatrix);
int nItems = pTextObj->CountItems();
FX_FLOAT baseSpace = CalculateBaseSpace(pTextObj, matrix);
const FX_BOOL bR2L = IsRightToLeft(pTextObj, pFont, nItems);
const FX_BOOL bIsBidiAndMirrorInverse =
bR2L && (matrix.a * matrix.d - matrix.b * matrix.c) < 0;
int32_t iBufStartAppend = m_TempTextBuf.GetLength();
int32_t iCharListStartAppend =
pdfium::CollectionSize<int32_t>(m_TempCharList);
FX_FLOAT spacing = 0;
for (int i = 0; i < nItems; i++) {
CPDF_TextObjectItem item;
PAGECHAR_INFO charinfo;
charinfo.m_OriginX = 0;
charinfo.m_OriginY = 0;
pTextObj->GetItemInfo(i, &item);
if (item.m_CharCode == (uint32_t)-1) {
CFX_WideString str = m_TempTextBuf.AsStringC();
if (str.IsEmpty()) {
str = m_TextBuf.AsStringC();
}
if (str.IsEmpty() || str.GetAt(str.GetLength() - 1) == TEXT_BLANK_CHAR) {
continue;
}
FX_FLOAT fontsize_h = pTextObj->m_TextState.GetFontSizeH();
spacing = -fontsize_h * item.m_OriginX / 1000;
continue;
}
FX_FLOAT charSpace = pTextObj->m_TextState.GetObject()->m_CharSpace;
if (charSpace > 0.001) {
spacing += matrix.TransformDistance(charSpace);
} else if (charSpace < -0.001) {
spacing -= matrix.TransformDistance(FXSYS_fabs(charSpace));
}
spacing -= baseSpace;
if (spacing && i > 0) {
int last_width = 0;
FX_FLOAT fontsize_h = pTextObj->m_TextState.GetFontSizeH();
uint32_t space_charcode = pFont->CharCodeFromUnicode(' ');
FX_FLOAT threshold = 0;
if (space_charcode != CPDF_Font::kInvalidCharCode) {
threshold = fontsize_h * pFont->GetCharWidthF(space_charcode) / 1000;
}
if (threshold > fontsize_h / 3) {
threshold = 0;
} else {
threshold /= 2;
}
if (threshold == 0) {
threshold = fontsize_h;
int this_width = FXSYS_abs(GetCharWidth(item.m_CharCode, pFont));
threshold = this_width > last_width ? (FX_FLOAT)this_width
: (FX_FLOAT)last_width;
threshold = NormalizeThreshold(threshold);
threshold = fontsize_h * threshold / 1000;
}
if (threshold && (spacing && spacing >= threshold)) {
charinfo.m_Unicode = TEXT_BLANK_CHAR;
charinfo.m_Flag = FPDFTEXT_CHAR_GENERATED;
charinfo.m_pTextObj = pTextObj;
charinfo.m_Index = m_TextBuf.GetLength();
m_TempTextBuf.AppendChar(TEXT_BLANK_CHAR);
charinfo.m_CharCode = CPDF_Font::kInvalidCharCode;
charinfo.m_Matrix.Copy(formMatrix);
matrix.Transform(item.m_OriginX, item.m_OriginY, charinfo.m_OriginX,
charinfo.m_OriginY);
charinfo.m_CharBox =
CFX_FloatRect(charinfo.m_OriginX, charinfo.m_OriginY,
charinfo.m_OriginX, charinfo.m_OriginY);
m_TempCharList.push_back(charinfo);
}
if (item.m_CharCode == CPDF_Font::kInvalidCharCode) {
continue;
}
}
spacing = 0;
CFX_WideString wstrItem = pFont->UnicodeFromCharCode(item.m_CharCode);
FX_BOOL bNoUnicode = FALSE;
FX_WCHAR wChar = wstrItem.GetAt(0);
if ((wstrItem.IsEmpty() || wChar == 0) && item.m_CharCode) {
if (wstrItem.IsEmpty()) {
wstrItem += (FX_WCHAR)item.m_CharCode;
} else {
wstrItem.SetAt(0, (FX_WCHAR)item.m_CharCode);
}
bNoUnicode = TRUE;
}
charinfo.m_Index = -1;
charinfo.m_CharCode = item.m_CharCode;
if (bNoUnicode) {
charinfo.m_Flag = FPDFTEXT_CHAR_UNUNICODE;
} else {
charinfo.m_Flag = FPDFTEXT_CHAR_NORMAL;
}
charinfo.m_pTextObj = pTextObj;
charinfo.m_OriginX = 0, charinfo.m_OriginY = 0;
matrix.Transform(item.m_OriginX, item.m_OriginY, charinfo.m_OriginX,
charinfo.m_OriginY);
FX_RECT rect =
charinfo.m_pTextObj->GetFont()->GetCharBBox(charinfo.m_CharCode);
charinfo.m_CharBox.top =
rect.top * pTextObj->GetFontSize() / 1000 + item.m_OriginY;
charinfo.m_CharBox.left =
rect.left * pTextObj->GetFontSize() / 1000 + item.m_OriginX;
charinfo.m_CharBox.right =
rect.right * pTextObj->GetFontSize() / 1000 + item.m_OriginX;
charinfo.m_CharBox.bottom =
rect.bottom * pTextObj->GetFontSize() / 1000 + item.m_OriginY;
if (fabsf(charinfo.m_CharBox.top - charinfo.m_CharBox.bottom) < 0.01f) {
charinfo.m_CharBox.top =
charinfo.m_CharBox.bottom + pTextObj->GetFontSize();
}
if (fabsf(charinfo.m_CharBox.right - charinfo.m_CharBox.left) < 0.01f) {
charinfo.m_CharBox.right =
charinfo.m_CharBox.left + pTextObj->GetCharWidth(charinfo.m_CharCode);
}
matrix.TransformRect(charinfo.m_CharBox);
charinfo.m_Matrix.Copy(matrix);
if (wstrItem.IsEmpty()) {
charinfo.m_Unicode = 0;
m_TempCharList.push_back(charinfo);
m_TempTextBuf.AppendChar(0xfffe);
continue;
} else {
int nTotal = wstrItem.GetLength();
FX_BOOL bDel = FALSE;
const int count =
std::min(pdfium::CollectionSize<int>(m_TempCharList), 7);
FX_FLOAT threshold = charinfo.m_Matrix.TransformXDistance(
(FX_FLOAT)TEXT_CHARRATIO_GAPDELTA * pTextObj->GetFontSize());
for (int n = pdfium::CollectionSize<int>(m_TempCharList);
n > pdfium::CollectionSize<int>(m_TempCharList) - count; n--) {
const PAGECHAR_INFO& charinfo1 = m_TempCharList[n - 1];
if (charinfo1.m_CharCode == charinfo.m_CharCode &&
charinfo1.m_pTextObj->GetFont() == charinfo.m_pTextObj->GetFont() &&
FXSYS_fabs(charinfo1.m_OriginX - charinfo.m_OriginX) < threshold &&
FXSYS_fabs(charinfo1.m_OriginY - charinfo.m_OriginY) < threshold) {
bDel = TRUE;
break;
}
}
if (!bDel) {
for (int nIndex = 0; nIndex < nTotal; nIndex++) {
charinfo.m_Unicode = wstrItem.GetAt(nIndex);
if (charinfo.m_Unicode) {
charinfo.m_Index = m_TextBuf.GetLength();
m_TempTextBuf.AppendChar(charinfo.m_Unicode);
} else {
m_TempTextBuf.AppendChar(0xfffe);
}
m_TempCharList.push_back(charinfo);
}
} else if (i == 0) {
CFX_WideString str = m_TempTextBuf.AsStringC();
if (!str.IsEmpty() &&
str.GetAt(str.GetLength() - 1) == TEXT_BLANK_CHAR) {
m_TempTextBuf.Delete(m_TempTextBuf.GetLength() - 1, 1);
m_TempCharList.pop_back();
}
}
}
}
if (bIsBidiAndMirrorInverse) {
SwapTempTextBuf(iCharListStartAppend, iBufStartAppend);
}
}
int32_t CPDF_TextPage::GetTextObjectWritingMode(
const CPDF_TextObject* pTextObj) {
int32_t nChars = pTextObj->CountChars();
if (nChars == 1) {
return m_TextlineDir;
}
CPDF_TextObjectItem first, last;
pTextObj->GetCharInfo(0, &first);
pTextObj->GetCharInfo(nChars - 1, &last);
CFX_Matrix textMatrix;
pTextObj->GetTextMatrix(&textMatrix);
textMatrix.TransformPoint(first.m_OriginX, first.m_OriginY);
textMatrix.TransformPoint(last.m_OriginX, last.m_OriginY);
FX_FLOAT dX = FXSYS_fabs(last.m_OriginX - first.m_OriginX);
FX_FLOAT dY = FXSYS_fabs(last.m_OriginY - first.m_OriginY);
if (dX <= 0.0001f && dY <= 0.0001f) {
return -1;
}
CFX_VectorF v(dX, dY);
v.Normalize();
if (v.y <= 0.0872f) {
return v.x <= 0.0872f ? m_TextlineDir : 0;
}
if (v.x <= 0.0872f) {
return 1;
}
return m_TextlineDir;
}
FX_BOOL CPDF_TextPage::IsHyphen(FX_WCHAR curChar) {
CFX_WideString strCurText = m_TempTextBuf.AsStringC();
if (strCurText.GetLength() == 0) {
strCurText = m_TextBuf.AsStringC();
}
FX_STRSIZE nCount = strCurText.GetLength();
int nIndex = nCount - 1;
FX_WCHAR wcTmp = strCurText.GetAt(nIndex);
while (wcTmp == 0x20 && nIndex <= nCount - 1 && nIndex >= 0) {
wcTmp = strCurText.GetAt(--nIndex);
}
if (0x2D == wcTmp || 0xAD == wcTmp) {
if (--nIndex > 0) {
FX_WCHAR preChar = strCurText.GetAt((nIndex));
if (((preChar >= L'A' && preChar <= L'Z') ||
(preChar >= L'a' && preChar <= L'z')) &&
((curChar >= L'A' && curChar <= L'Z') ||
(curChar >= L'a' && curChar <= L'z'))) {
return TRUE;
}
}
const PAGECHAR_INFO* preInfo;
if (!m_TempCharList.empty()) {
preInfo = &m_TempCharList.back();
} else if (!m_CharList.empty()) {
preInfo = &m_CharList.back();
} else {
return FALSE;
}
if (FPDFTEXT_CHAR_PIECE == preInfo->m_Flag &&
(0xAD == preInfo->m_Unicode || 0x2D == preInfo->m_Unicode)) {
return TRUE;
}
}
return FALSE;
}
int CPDF_TextPage::ProcessInsertObject(const CPDF_TextObject* pObj,
const CFX_Matrix& formMatrix) {
FindPreviousTextObject();
FX_BOOL bNewline = FALSE;
int WritingMode = GetTextObjectWritingMode(pObj);
if (WritingMode == -1) {
WritingMode = GetTextObjectWritingMode(m_pPreTextObj);
}
CFX_FloatRect this_rect(pObj->m_Left, pObj->m_Bottom, pObj->m_Right,
pObj->m_Top);
CFX_FloatRect prev_rect(m_pPreTextObj->m_Left, m_pPreTextObj->m_Bottom,
m_pPreTextObj->m_Right, m_pPreTextObj->m_Top);
CPDF_TextObjectItem PrevItem, item;
int nItem = m_pPreTextObj->CountItems();
m_pPreTextObj->GetItemInfo(nItem - 1, &PrevItem);
pObj->GetItemInfo(0, &item);
CFX_WideString wstrItem =
pObj->GetFont()->UnicodeFromCharCode(item.m_CharCode);
if (wstrItem.IsEmpty()) {
wstrItem += (FX_WCHAR)item.m_CharCode;
}
FX_WCHAR curChar = wstrItem.GetAt(0);
if (WritingMode == 0) {
if (this_rect.Height() > 4.5 && prev_rect.Height() > 4.5) {
FX_FLOAT top =
this_rect.top < prev_rect.top ? this_rect.top : prev_rect.top;
FX_FLOAT bottom = this_rect.bottom > prev_rect.bottom ? this_rect.bottom
: prev_rect.bottom;
if (bottom >= top) {
if (IsHyphen(curChar)) {
return 3;
}
return 2;
}
}
} else if (WritingMode == 1) {
if (this_rect.Width() > pObj->GetFontSize() * 0.1f &&
prev_rect.Width() > m_pPreTextObj->GetFontSize() * 0.1f) {
FX_FLOAT left = this_rect.left > m_CurlineRect.left ? this_rect.left
: m_CurlineRect.left;
FX_FLOAT right = this_rect.right < m_CurlineRect.right
? this_rect.right
: m_CurlineRect.right;
if (right <= left) {
if (IsHyphen(curChar)) {
return 3;
}
return 2;
}
}
}
FX_FLOAT last_pos = PrevItem.m_OriginX;
int nLastWidth = GetCharWidth(PrevItem.m_CharCode, m_pPreTextObj->GetFont());
FX_FLOAT last_width = nLastWidth * m_pPreTextObj->GetFontSize() / 1000;
last_width = FXSYS_fabs(last_width);
int nThisWidth = GetCharWidth(item.m_CharCode, pObj->GetFont());
FX_FLOAT this_width = nThisWidth * pObj->GetFontSize() / 1000;
this_width = FXSYS_fabs(this_width);
FX_FLOAT threshold =
last_width > this_width ? last_width / 4 : this_width / 4;
CFX_Matrix prev_matrix, prev_reverse;
m_pPreTextObj->GetTextMatrix(&prev_matrix);
prev_matrix.Concat(m_perMatrix);
prev_reverse.SetReverse(prev_matrix);
FX_FLOAT x = pObj->GetPosX();
FX_FLOAT y = pObj->GetPosY();
formMatrix.Transform(x, y);
prev_reverse.Transform(x, y);
if (last_width < this_width) {
threshold = prev_reverse.TransformDistance(threshold);
}
CFX_FloatRect rect1(m_pPreTextObj->m_Left, pObj->m_Bottom,
m_pPreTextObj->m_Right, pObj->m_Top);
CFX_FloatRect rect2(m_pPreTextObj->m_Left, m_pPreTextObj->m_Bottom,
m_pPreTextObj->m_Right, m_pPreTextObj->m_Top);
CFX_FloatRect rect3 = rect1;
rect1.Intersect(rect2);
if (WritingMode == 0) {
if ((rect1.IsEmpty() && rect2.Height() > 5 && rect3.Height() > 5) ||
((y > threshold * 2 || y < threshold * -3) &&
(FXSYS_fabs(y) < 1 ? FXSYS_fabs(x) < FXSYS_fabs(y) : TRUE))) {
bNewline = TRUE;
if (nItem > 1) {
CPDF_TextObjectItem tempItem;
m_pPreTextObj->GetItemInfo(0, &tempItem);
CFX_Matrix m;
m_pPreTextObj->GetTextMatrix(&m);
if (PrevItem.m_OriginX > tempItem.m_OriginX &&
m_DisplayMatrix.a > 0.9 && m_DisplayMatrix.b < 0.1 &&
m_DisplayMatrix.c < 0.1 && m_DisplayMatrix.d < -0.9 && m.b < 0.1 &&
m.c < 0.1) {
CFX_FloatRect re(0, m_pPreTextObj->m_Bottom, 1000,
m_pPreTextObj->m_Top);
if (re.Contains(pObj->GetPosX(), pObj->GetPosY())) {
bNewline = FALSE;
} else {
CFX_FloatRect re(0, pObj->m_Bottom, 1000, pObj->m_Top);
if (re.Contains(m_pPreTextObj->GetPosX(),
m_pPreTextObj->GetPosY())) {
bNewline = FALSE;
}
}
}
}
}
}
if (bNewline)
return IsHyphen(curChar) ? 3 : 2;
int32_t nChars = pObj->CountChars();
if (nChars == 1 && (0x2D == curChar || 0xAD == curChar) &&
IsHyphen(curChar)) {
return 3;
}
CFX_WideString PrevStr =
m_pPreTextObj->GetFont()->UnicodeFromCharCode(PrevItem.m_CharCode);
FX_WCHAR preChar = PrevStr.GetAt(PrevStr.GetLength() - 1);
CFX_Matrix matrix;
pObj->GetTextMatrix(&matrix);
matrix.Concat(formMatrix);
threshold = (FX_FLOAT)(nLastWidth > nThisWidth ? nLastWidth : nThisWidth);
threshold = threshold > 400
? (threshold < 700
? threshold / 4
: (threshold > 800 ? threshold / 6 : threshold / 5))
: (threshold / 2);
if (nLastWidth >= nThisWidth) {
threshold *= FXSYS_fabs(m_pPreTextObj->GetFontSize());
} else {
threshold *= FXSYS_fabs(pObj->GetFontSize());
threshold = matrix.TransformDistance(threshold);
threshold = prev_reverse.TransformDistance(threshold);
}
threshold /= 1000;
if ((threshold < 1.4881 && threshold > 1.4879) ||
(threshold < 1.39001 && threshold > 1.38999)) {
threshold *= 1.5;
}
if (FXSYS_fabs(last_pos + last_width - x) > threshold && curChar != L' ' &&
preChar != L' ') {
if (curChar != L' ' && preChar != L' ') {
if ((x - last_pos - last_width) > threshold ||
(last_pos - x - last_width) > threshold) {
return 1;
}
if (x < 0 && (last_pos - x - last_width) > threshold) {
return 1;
}
if ((x - last_pos - last_width) > this_width ||
(x - last_pos - this_width) > last_width) {
return 1;
}
}
}
return 0;
}
FX_BOOL CPDF_TextPage::IsSameTextObject(CPDF_TextObject* pTextObj1,
CPDF_TextObject* pTextObj2) {
if (!pTextObj1 || !pTextObj2) {
return FALSE;
}
CFX_FloatRect rcPreObj(pTextObj2->m_Left, pTextObj2->m_Bottom,
pTextObj2->m_Right, pTextObj2->m_Top);
CFX_FloatRect rcCurObj(pTextObj1->m_Left, pTextObj1->m_Bottom,
pTextObj1->m_Right, pTextObj1->m_Top);
if (rcPreObj.IsEmpty() && rcCurObj.IsEmpty()) {
FX_FLOAT dbXdif = FXSYS_fabs(rcPreObj.left - rcCurObj.left);
size_t nCount = m_CharList.size();
if (nCount >= 2) {
PAGECHAR_INFO perCharTemp = m_CharList[nCount - 2];
FX_FLOAT dbSpace = perCharTemp.m_CharBox.Width();
if (dbXdif > dbSpace) {
return FALSE;
}
}
}
if (!rcPreObj.IsEmpty() || !rcCurObj.IsEmpty()) {
rcPreObj.Intersect(rcCurObj);
if (rcPreObj.IsEmpty()) {
return FALSE;
}
if (FXSYS_fabs(rcPreObj.Width() - rcCurObj.Width()) >
rcCurObj.Width() / 2) {
return FALSE;
}
if (pTextObj2->GetFontSize() != pTextObj1->GetFontSize()) {
return FALSE;
}
}
int nPreCount = pTextObj2->CountItems();
int nCurCount = pTextObj1->CountItems();
if (nPreCount != nCurCount) {
return FALSE;
}
// If both objects have no items, consider them same.
if (!nPreCount)
return TRUE;
CPDF_TextObjectItem itemPer = {0, 0.0f, 0.0f};
CPDF_TextObjectItem itemCur = {0, 0.0f, 0.0f};
for (int i = 0; i < nPreCount; i++) {
pTextObj2->GetItemInfo(i, &itemPer);
pTextObj1->GetItemInfo(i, &itemCur);
if (itemCur.m_CharCode != itemPer.m_CharCode) {
return FALSE;
}
}
if (FXSYS_fabs(pTextObj1->GetPosX() - pTextObj2->GetPosX()) >
GetCharWidth(itemPer.m_CharCode, pTextObj2->GetFont()) *
pTextObj2->GetFontSize() / 1000 * 0.9 ||
FXSYS_fabs(pTextObj1->GetPosY() - pTextObj2->GetPosY()) >
std::max(std::max(rcPreObj.Height(), rcPreObj.Width()),
pTextObj2->GetFontSize()) /
8) {
return FALSE;
}
return TRUE;
}
FX_BOOL CPDF_TextPage::IsSameAsPreTextObject(
CPDF_TextObject* pTextObj,
const CPDF_PageObjectList* pObjList,
CPDF_PageObjectList::const_iterator iter) {
int i = 0;
while (i < 5 && iter != pObjList->begin()) {
--iter;
CPDF_PageObject* pOtherObj = iter->get();
if (pOtherObj == pTextObj || !pOtherObj->IsText())
continue;
if (IsSameTextObject(pOtherObj->AsText(), pTextObj))
return TRUE;
++i;
}
return FALSE;
}
FX_BOOL CPDF_TextPage::GenerateCharInfo(FX_WCHAR unicode, PAGECHAR_INFO& info) {
const PAGECHAR_INFO* preChar;
if (!m_TempCharList.empty()) {
preChar = &m_TempCharList.back();
} else if (!m_CharList.empty()) {
preChar = &m_CharList.back();
} else {
return FALSE;
}
info.m_Index = m_TextBuf.GetLength();
info.m_Unicode = unicode;
info.m_pTextObj = NULL;
info.m_CharCode = CPDF_Font::kInvalidCharCode;
info.m_Flag = FPDFTEXT_CHAR_GENERATED;
int preWidth = 0;
if (preChar->m_pTextObj && preChar->m_CharCode != -1) {
preWidth =
GetCharWidth(preChar->m_CharCode, preChar->m_pTextObj->GetFont());
}
FX_FLOAT fFontSize = preChar->m_pTextObj ? preChar->m_pTextObj->GetFontSize()
: preChar->m_CharBox.Height();
if (!fFontSize)
fFontSize = kDefaultFontSize;
info.m_OriginX = preChar->m_OriginX + preWidth * (fFontSize) / 1000;
info.m_OriginY = preChar->m_OriginY;
info.m_CharBox = CFX_FloatRect(info.m_OriginX, info.m_OriginY, info.m_OriginX,
info.m_OriginY);
return TRUE;
}
FX_BOOL CPDF_TextPage::IsRectIntersect(const CFX_FloatRect& rect1,
const CFX_FloatRect& rect2) {
CFX_FloatRect rect = rect1;
rect.Intersect(rect2);
return !rect.IsEmpty();
}
CPDF_TextPageFind::CPDF_TextPageFind(const CPDF_TextPage* pTextPage)
: m_pTextPage(pTextPage),
m_flags(0),
m_findNextStart(-1),
m_findPreStart(-1),
m_bMatchCase(FALSE),
m_bMatchWholeWord(FALSE),
m_resStart(0),
m_resEnd(-1),
m_IsFind(FALSE) {
m_strText = m_pTextPage->GetPageText();
int nCount = pTextPage->CountChars();
if (nCount) {
m_CharIndex.push_back(0);
}
for (int i = 0; i < nCount; i++) {
FPDF_CHAR_INFO info;
pTextPage->GetCharInfo(i, &info);
int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
if (info.m_Flag == FPDFTEXT_CHAR_NORMAL ||
info.m_Flag == FPDFTEXT_CHAR_GENERATED) {
if (indexSize % 2) {
m_CharIndex.push_back(1);
} else {
if (indexSize <= 0) {
continue;
}
m_CharIndex[indexSize - 1] += 1;
}
} else {
if (indexSize % 2) {
if (indexSize <= 0) {
continue;
}
m_CharIndex[indexSize - 1] = i + 1;
} else {
m_CharIndex.push_back(i + 1);
}
}
}
int indexSize = pdfium::CollectionSize<int>(m_CharIndex);
if (indexSize % 2) {
m_CharIndex.erase(m_CharIndex.begin() + indexSize - 1);
}
}
int CPDF_TextPageFind::GetCharIndex(int index) const {
return m_pTextPage->CharIndexFromTextIndex(index);
}
FX_BOOL CPDF_TextPageFind::FindFirst(const CFX_WideString& findwhat,
int flags,
int startPos) {
if (!m_pTextPage) {
return FALSE;
}
if (m_strText.IsEmpty() || m_bMatchCase != (flags & FPDFTEXT_MATCHCASE)) {
m_strText = m_pTextPage->GetPageText();
}
CFX_WideString findwhatStr = findwhat;
m_findWhat = findwhatStr;
m_flags = flags;
m_bMatchCase = flags & FPDFTEXT_MATCHCASE;
if (m_strText.IsEmpty()) {
m_IsFind = FALSE;
return TRUE;
}
FX_STRSIZE len = findwhatStr.GetLength();
if (!m_bMatchCase) {
findwhatStr.MakeLower();
m_strText.MakeLower();
}
m_bMatchWholeWord = flags & FPDFTEXT_MATCHWHOLEWORD;
m_findNextStart = startPos;
if (startPos == -1) {
m_findPreStart = m_strText.GetLength() - 1;
} else {
m_findPreStart = startPos;
}
m_csFindWhatArray.clear();
int i = 0;
while (i < len) {
if (findwhatStr.GetAt(i) != ' ') {
break;
}
i++;
}
if (i < len) {
ExtractFindWhat(findwhatStr);
} else {
m_csFindWhatArray.push_back(findwhatStr);
}
if (m_csFindWhatArray.empty()) {
return FALSE;
}
m_IsFind = TRUE;
m_resStart = 0;
m_resEnd = -1;
return TRUE;
}
FX_BOOL CPDF_TextPageFind::FindNext() {
if (!m_pTextPage) {
return FALSE;
}
m_resArray.clear();
if (m_findNextStart == -1) {
return FALSE;
}
if (m_strText.IsEmpty()) {
m_IsFind = FALSE;
return m_IsFind;
}
int strLen = m_strText.GetLength();
if (m_findNextStart > strLen - 1) {
m_IsFind = FALSE;
return m_IsFind;
}
int nCount = pdfium::CollectionSize<int>(m_csFindWhatArray);
int nResultPos = 0;
int nStartPos = 0;
nStartPos = m_findNextStart;
FX_BOOL bSpaceStart = FALSE;
for (int iWord = 0; iWord < nCount; iWord++) {
CFX_WideString csWord = m_csFindWhatArray[iWord];
if (csWord.IsEmpty()) {
if (iWord == nCount - 1) {
FX_WCHAR strInsert = m_strText.GetAt(nStartPos);
if (strInsert == TEXT_LINEFEED_CHAR || strInsert == TEXT_BLANK_CHAR ||
strInsert == TEXT_RETURN_CHAR || strInsert == 160) {
nResultPos = nStartPos + 1;
break;
}
iWord = -1;
} else if (iWord == 0) {
bSpaceStart = TRUE;
}
continue;
}
int endIndex;
nResultPos = m_strText.Find(csWord.c_str(), nStartPos);
if (nResultPos == -1) {
m_IsFind = FALSE;
return m_IsFind;
}
endIndex = nResultPos + csWord.GetLength() - 1;
if (iWord == 0) {
m_resStart = nResultPos;
}
FX_BOOL bMatch = TRUE;
if (iWord != 0 && !bSpaceStart) {
int PreResEndPos = nStartPos;
int curChar = csWord.GetAt(0);
CFX_WideString lastWord = m_csFindWhatArray[iWord - 1];
int lastChar = lastWord.GetAt(lastWord.GetLength() - 1);
if (nStartPos == nResultPos &&
!(IsIgnoreSpaceCharacter(lastChar) ||
IsIgnoreSpaceCharacter(curChar))) {
bMatch = FALSE;
}
for (int d = PreResEndPos; d < nResultPos; d++) {
FX_WCHAR strInsert = m_strText.GetAt(d);
if (strInsert != TEXT_LINEFEED_CHAR && strInsert != TEXT_BLANK_CHAR &&
strInsert != TEXT_RETURN_CHAR && strInsert != 160) {
bMatch = FALSE;
break;
}
}
} else if (bSpaceStart) {
if (nResultPos > 0) {
FX_WCHAR strInsert = m_strText.GetAt(nResultPos - 1);
if (strInsert != TEXT_LINEFEED_CHAR && strInsert != TEXT_BLANK_CHAR &&
strInsert != TEXT_RETURN_CHAR && strInsert != 160) {
bMatch = FALSE;
m_resStart = nResultPos;
} else {
m_resStart = nResultPos - 1;
}
}
}
if (m_bMatchWholeWord && bMatch) {
bMatch = IsMatchWholeWord(m_strText, nResultPos, endIndex);
}
nStartPos = endIndex + 1;
if (!bMatch) {
iWord = -1;
if (bSpaceStart) {
nStartPos = m_resStart + m_csFindWhatArray[1].GetLength();
} else {
nStartPos = m_resStart + m_csFindWhatArray[0].GetLength();
}
}
}
m_resEnd = nResultPos + m_csFindWhatArray.back().GetLength() - 1;
m_IsFind = TRUE;
int resStart = GetCharIndex(m_resStart);
int resEnd = GetCharIndex(m_resEnd);
m_resArray = m_pTextPage->GetRectArray(resStart, resEnd - resStart + 1);
if (m_flags & FPDFTEXT_CONSECUTIVE) {
m_findNextStart = m_resStart + 1;
m_findPreStart = m_resEnd - 1;
} else {
m_findNextStart = m_resEnd + 1;
m_findPreStart = m_resStart - 1;
}
return m_IsFind;
}
FX_BOOL CPDF_TextPageFind::FindPrev() {
if (!m_pTextPage) {
return FALSE;
}
m_resArray.clear();
if (m_strText.IsEmpty() || m_findPreStart < 0) {
m_IsFind = FALSE;
return m_IsFind;
}
CPDF_TextPageFind findEngine(m_pTextPage);
FX_BOOL ret = findEngine.FindFirst(m_findWhat, m_flags);
if (!ret) {
m_IsFind = FALSE;
return m_IsFind;
}
int order = -1, MatchedCount = 0;
while (ret) {
ret = findEngine.FindNext();
if (ret) {
int order1 = findEngine.GetCurOrder();
int MatchedCount1 = findEngine.GetMatchedCount();
if (((order1 + MatchedCount1) - 1) > m_findPreStart) {
break;
}
order = order1;
MatchedCount = MatchedCount1;
}
}
if (order == -1) {
m_IsFind = FALSE;
return m_IsFind;
}
m_resStart = m_pTextPage->TextIndexFromCharIndex(order);
m_resEnd = m_pTextPage->TextIndexFromCharIndex(order + MatchedCount - 1);
m_IsFind = TRUE;
m_resArray = m_pTextPage->GetRectArray(order, MatchedCount);
if (m_flags & FPDFTEXT_CONSECUTIVE) {
m_findNextStart = m_resStart + 1;
m_findPreStart = m_resEnd - 1;
} else {
m_findNextStart = m_resEnd + 1;
m_findPreStart = m_resStart - 1;
}
return m_IsFind;
}
void CPDF_TextPageFind::ExtractFindWhat(const CFX_WideString& findwhat) {
if (findwhat.IsEmpty()) {
return;
}
int index = 0;
while (1) {
CFX_WideString csWord = TEXT_EMPTY;
int ret =
ExtractSubString(csWord, findwhat.c_str(), index, TEXT_BLANK_CHAR);
if (csWord.IsEmpty()) {
if (ret) {
m_csFindWhatArray.push_back(L"");
index++;
continue;
} else {
break;
}
}
int pos = 0;
while (pos < csWord.GetLength()) {
CFX_WideString curStr = csWord.Mid(pos, 1);
FX_WCHAR curChar = csWord.GetAt(pos);
if (IsIgnoreSpaceCharacter(curChar)) {
if (pos > 0 && curChar == 0x2019) {
pos++;
continue;
}
if (pos > 0) {
m_csFindWhatArray.push_back(csWord.Mid(0, pos));
}
m_csFindWhatArray.push_back(curStr);
if (pos == csWord.GetLength() - 1) {
csWord.clear();
break;
}
csWord = csWord.Right(csWord.GetLength() - pos - 1);
pos = 0;
continue;
}
pos++;
}
if (!csWord.IsEmpty()) {
m_csFindWhatArray.push_back(csWord);
}
index++;
}
}
FX_BOOL CPDF_TextPageFind::IsMatchWholeWord(const CFX_WideString& csPageText,
int startPos,
int endPos) {
FX_WCHAR char_left = 0;
FX_WCHAR char_right = 0;
int char_count = endPos - startPos + 1;
if (char_count < 1) {
return FALSE;
}
if (char_count == 1 && csPageText.GetAt(startPos) > 255) {
return TRUE;
}
if (startPos - 1 >= 0) {
char_left = csPageText.GetAt(startPos - 1);
}
if (startPos + char_count < csPageText.GetLength()) {
char_right = csPageText.GetAt(startPos + char_count);
}
if ((char_left > 'A' && char_left < 'a') ||
(char_left > 'a' && char_left < 'z') ||
(char_left > 0xfb00 && char_left < 0xfb06) || std::iswdigit(char_left) ||
(char_right > 'A' && char_right < 'a') ||
(char_right > 'a' && char_right < 'z') ||
(char_right > 0xfb00 && char_right < 0xfb06) ||
std::iswdigit(char_right)) {
return FALSE;
}
if (!(('A' > char_left || char_left > 'Z') &&
('a' > char_left || char_left > 'z') &&
('A' > char_right || char_right > 'Z') &&
('a' > char_right || char_right > 'z'))) {
return FALSE;
}
if (char_count > 0) {
if (csPageText.GetAt(startPos) >= L'0' &&
csPageText.GetAt(startPos) <= L'9' && char_left >= L'0' &&
char_left <= L'9') {
return FALSE;
}
if (csPageText.GetAt(endPos) >= L'0' && csPageText.GetAt(endPos) <= L'9' &&
char_right >= L'0' && char_right <= L'9') {
return FALSE;
}
}
return TRUE;
}
FX_BOOL CPDF_TextPageFind::ExtractSubString(CFX_WideString& rString,
const FX_WCHAR* lpszFullString,
int iSubString,
FX_WCHAR chSep) {
if (!lpszFullString) {
return FALSE;
}
while (iSubString--) {
lpszFullString = wcschr(lpszFullString, chSep);
if (!lpszFullString) {
rString.clear();
return FALSE;
}
lpszFullString++;
while (*lpszFullString == chSep) {
lpszFullString++;
}
}
const FX_WCHAR* lpchEnd = wcschr(lpszFullString, chSep);
int nLen = lpchEnd ? (int)(lpchEnd - lpszFullString)
: (int)FXSYS_wcslen(lpszFullString);
ASSERT(nLen >= 0);
FXSYS_memcpy(rString.GetBuffer(nLen), lpszFullString,
nLen * sizeof(FX_WCHAR));
rString.ReleaseBuffer();
return TRUE;
}
CFX_WideString CPDF_TextPageFind::MakeReverse(const CFX_WideString& str) {
CFX_WideString str2;
str2.clear();
int nlen = str.GetLength();
for (int i = nlen - 1; i >= 0; i--) {
str2 += str.GetAt(i);
}
return str2;
}
int CPDF_TextPageFind::GetCurOrder() const {
return GetCharIndex(m_resStart);
}
int CPDF_TextPageFind::GetMatchedCount() const {
int resStart = GetCharIndex(m_resStart);
int resEnd = GetCharIndex(m_resEnd);
return resEnd - resStart + 1;
}
CPDF_LinkExtract::CPDF_LinkExtract(const CPDF_TextPage* pTextPage)
: m_pTextPage(pTextPage) {}
CPDF_LinkExtract::~CPDF_LinkExtract() {
}
void CPDF_LinkExtract::ExtractLinks() {
m_LinkArray.clear();
if (!m_pTextPage->IsParsed())
return;
m_strPageText = m_pTextPage->GetPageText(0, -1);
if (m_strPageText.IsEmpty())
return;
ParseLink();
}
void CPDF_LinkExtract::ParseLink() {
int start = 0, pos = 0;
int TotalChar = m_pTextPage->CountChars();
while (pos < TotalChar) {
FPDF_CHAR_INFO pageChar;
m_pTextPage->GetCharInfo(pos, &pageChar);
if (pageChar.m_Flag == FPDFTEXT_CHAR_GENERATED ||
pageChar.m_Unicode == 0x20 || pos == TotalChar - 1) {
int nCount = pos - start;
if (pos == TotalChar - 1) {
nCount++;
}
CFX_WideString strBeCheck;
strBeCheck = m_pTextPage->GetPageText(start, nCount);
if (strBeCheck.GetLength() > 5) {
while (strBeCheck.GetLength() > 0) {
FX_WCHAR ch = strBeCheck.GetAt(strBeCheck.GetLength() - 1);
if (ch == L')' || ch == L',' || ch == L'>' || ch == L'.') {
strBeCheck = strBeCheck.Mid(0, strBeCheck.GetLength() - 1);
nCount--;
} else {
break;
}
}
if (nCount > 5 &&
(CheckWebLink(strBeCheck) || CheckMailLink(strBeCheck))) {
m_LinkArray.push_back({start, nCount, strBeCheck});
}
}
start = ++pos;
} else {
pos++;
}
}
}
bool CPDF_LinkExtract::CheckWebLink(CFX_WideString& strBeCheck) {
CFX_WideString str = strBeCheck;
str.MakeLower();
if (str.Find(L"http://www.") != -1) {
strBeCheck = strBeCheck.Right(str.GetLength() - str.Find(L"http://www."));
return true;
}
if (str.Find(L"http://") != -1) {
strBeCheck = strBeCheck.Right(str.GetLength() - str.Find(L"http://"));
return true;
}
if (str.Find(L"https://www.") != -1) {
strBeCheck = strBeCheck.Right(str.GetLength() - str.Find(L"https://www."));
return true;
}
if (str.Find(L"https://") != -1) {
strBeCheck = strBeCheck.Right(str.GetLength() - str.Find(L"https://"));
return true;
}
if (str.Find(L"www.") != -1) {
strBeCheck = strBeCheck.Right(str.GetLength() - str.Find(L"www."));
strBeCheck = L"http://" + strBeCheck;
return true;
}
return false;
}
bool CPDF_LinkExtract::CheckMailLink(CFX_WideString& str) {
int aPos = str.Find(L'@');
// Invalid when no '@'.
if (aPos < 1)
return false;
// Check the local part.
int pPos = aPos; // Used to track the position of '@' or '.'.
for (int i = aPos - 1; i >= 0; i--) {
FX_WCHAR ch = str.GetAt(i);
if (ch == L'_' || ch == L'-' || FXSYS_iswalnum(ch))
continue;
if (ch != L'.' || i == pPos - 1 || i == 0) {
if (i == aPos - 1) {
// There is '.' or invalid char before '@'.
return FALSE;
}
// End extracting for other invalid chars, '.' at the beginning, or
// consecutive '.'.
int removed_len = i == pPos - 1 ? i + 2 : i + 1;
str = str.Right(str.GetLength() - removed_len);
break;
}
// Found a valid '.'.
pPos = i;
}
// Check the domain name part.
aPos = str.Find(L'@');
if (aPos < 1)
return false;
str.TrimRight(L'.');
// At least one '.' in domain name, but not at the beginning.
// TODO(weili): RFC5322 allows domain names to be a local name without '.'.
// Check whether we should remove this check.
int ePos = str.Find(L'.', aPos + 1);
if (ePos == -1 || ePos == aPos + 1)
return false;
// Validate all other chars in domain name.
int nLen = str.GetLength();
pPos = 0; // Used to track the position of '.'.
for (int i = aPos + 1; i < nLen; i++) {
FX_WCHAR wch = str.GetAt(i);
if (wch == L'-' || FXSYS_iswalnum(wch))
continue;
if (wch != L'.' || i == pPos + 1) {
// Domain name should end before invalid char.
int host_end = i == pPos + 1 ? i - 2 : i - 1;
if (pPos > 0 && host_end - aPos >= 3) {
// Trim the ending invalid chars if there is at least one '.' and name.
str = str.Left(host_end + 1);
break;
}
return false;
}
pPos = i;
}
if (str.Find(L"mailto:") == -1)
str = L"mailto:" + str;
return true;
}
CFX_WideString CPDF_LinkExtract::GetURL(size_t index) const {
return index < m_LinkArray.size() ? m_LinkArray[index].m_strUrl : L"";
}
std::vector<CFX_FloatRect> CPDF_LinkExtract::GetRects(size_t index) const {
if (index >= m_LinkArray.size())
return std::vector<CFX_FloatRect>();
return m_pTextPage->GetRectArray(m_LinkArray[index].m_Start,
m_LinkArray[index].m_Count);
}
|