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
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
|
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.
Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.
File: GrTableManager.cpp
Responsibility: Sharon Correll
Last reviewed: Not yet.
Description:
GrTableManager, which handles the interactions between passes, including the demand-pull
algorithm.
-------------------------------------------------------------------------------*//*:End Ignore*/
//:>********************************************************************************************
//:> Include files
//:>********************************************************************************************
#include "Main.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
#undef THIS_FILE
DEFINE_THIS_FILE
#ifndef _WIN32
#include <stdlib.h>
#include <math.h>
#endif
//:>********************************************************************************************
//:> Forward declarations
//:>********************************************************************************************
//:>********************************************************************************************
//:> Local Constants and static variables
//:>********************************************************************************************
namespace gr
{
//:>********************************************************************************************
//:> Methods
//:>********************************************************************************************
/*----------------------------------------------------------------------------------------------
Constructors and initializers
----------------------------------------------------------------------------------------------*/
EngineState::EngineState()
: m_ipassJustCalled(-1),
m_fStartLineContext(false),
m_fEndLineContext(false),
////m_pgg(NULL),
m_cslotPreSeg(0),
m_islotblkCurr(-1),
m_islotNext(kSlotBlockSize),
m_fInitialLB(false),
m_fFinalLB(false),
m_fInsertedLB(false),
m_fExceededSpace(false),
m_fHitHardBreak(false),
m_fRemovedWhtsp(false),
m_dxsShrinkPossible(GrSlotState::kNotYetSet),
m_islotPosNext(-1),
m_xsPosXNext(0),
m_ysPosYNext(0),
m_dxsTotWidthSoFar(0),
m_dxsVisWidthSoFar(0),
m_prgzpst(NULL),
m_prgpsstrm(NULL)
{
m_vslotblk.clear();
m_vprgnSlotVarLenBufs.clear();
}
void EngineState::Initialize(GrEngine * pgreng, GrTableManager * ptman)
{
m_ptman = ptman;
m_cpass = ptman->NumberOfPasses(); // duplicate for convenience
m_cUserDefn = pgreng->NumUserDefn();
m_cFeat = pgreng->NumFeat();
m_cCompPerLig = pgreng->NumCompPerLig();
if (m_prgzpst)
delete[] m_prgzpst;
m_prgzpst = new PassState[m_cpass];
ptman->StorePassStates(m_prgzpst);
}
/*----------------------------------------------------------------------------------------------
Destructors
----------------------------------------------------------------------------------------------*/
GrTableManager::~GrTableManager()
{
for (int ipass = 0; ipass < m_cpass; ++ipass)
delete Pass(ipass);
delete[] m_prgppass;
}
EngineState::~EngineState()
{
DestroySlotBlocks();
delete[] m_prgzpst;
if (m_prgpsstrm)
{
for (int isstrm = 0; isstrm < m_cpass; ++isstrm)
delete OutputStream(isstrm);
delete[] m_prgpsstrm;
}
}
/*----------------------------------------------------------------------------------------------
Delete all the slots.
----------------------------------------------------------------------------------------------*/
void EngineState::DestroySlotBlocks()
{
Assert(m_vslotblk.size() == m_vprgnSlotVarLenBufs.size());
for (size_t islotblk = 0; islotblk < m_vslotblk.size(); ++islotblk)
{
delete[] m_vslotblk[islotblk];
delete[] m_vprgnSlotVarLenBufs[islotblk];
}
m_vslotblk.clear();
m_vprgnSlotVarLenBufs.clear();
}
/*----------------------------------------------------------------------------------------------
Create the passes, and them fill them in by reading from the file stream.
----------------------------------------------------------------------------------------------*/
bool GrTableManager::CreateAndReadPasses(GrIStream & grstrm,
int fxdSilfVersion, int fxdRuleVersion,
int cpassFont, long lSubTableStart, int * rgnPassOffsets,
int ipassSub1Font, int ipassPos1Font, int ipassJust1Font, byte ipassPostBidiFont)
{
Assert(ipassSub1Font <= ipassJust1Font);
Assert(ipassJust1Font <= ipassPos1Font);
Assert(ipassPos1Font <= cpassFont);
if (ipassSub1Font > ipassJust1Font || ipassJust1Font > ipassPos1Font
|| ipassPos1Font > cpassFont)
{
return false; // bad table
}
// Adjusted indices based on the fact that we have a glyph generation pass that is
// pass 0, and possibly a bidi pass:
m_cpass = cpassFont + 1;
int ipassLB1, ipassSub1, ipassBidi, ipassPos1, ipassJust1;
ipassLB1 = 1;
ipassSub1 = ipassSub1Font + 1;
if (ipassPostBidiFont == 0xFF)
{
// No bidi pass.
m_fBidi = false;
// Add 1 below to account for the glyph-generation pass.
ipassPos1 = ipassPos1Font + 1;
ipassJust1 = ipassJust1Font + 1;
ipassBidi = ipassJust1;
}
else
{
m_fBidi = true;
m_cpass++;
Assert(ipassPostBidiFont == ipassJust1Font);
ipassJust1 = ipassJust1Font + 2;
ipassPos1 = ipassPos1Font + 2;
ipassBidi = ipassJust1 - 1;
}
// Always make at least one positioning pass, even if there are no rules.
if (ipassPos1 == m_cpass)
{
m_cpass++;
}
m_prgppass = new GrPass*[m_cpass + 1];
m_prgppass[0] = new GrGlyphGenPass(0);
int ipass = 1;
int ipassFont = 0;
m_cpassLB = 0;
m_ipassJust1 = 1;
m_ipassPos1 = 1;
for ( ; ipass < m_cpass; ipass++, ipassFont++)
{
if (ipass < ipassSub1)
{
m_prgppass[ipass] = new GrLineBreakPass(ipass);
m_prgppass[ipass]->ReadFromFont(grstrm, fxdSilfVersion, fxdRuleVersion,
lSubTableStart + rgnPassOffsets[ipassFont]);
m_cpassLB++;
m_ipassJust1++;
m_ipassPos1++;
}
else if (ipassSub1 <= ipass && ipass < ipassBidi)
{
m_prgppass[ipass] = new GrSubPass(ipass);
m_prgppass[ipass]->ReadFromFont(grstrm, fxdSilfVersion, fxdRuleVersion,
lSubTableStart + rgnPassOffsets[ipassFont]);
m_ipassJust1++;
m_ipassPos1++;
}
else if (ipassBidi == ipass && ipass < ipassJust1)
{
m_prgppass[ipass] = new GrBidiPass(ipass);
m_prgppass[ipass]->SetTopDirLevel(TopDirectionLevel());
ipassFont--; // no corresponding pass in font
m_ipassJust1++;
m_ipassPos1++;
}
else if (ipassJust1 <= ipass && ipass < ipassPos1)
{
// A justification pass is, in essence, a substitution pass.
m_prgppass[ipass] = new GrSubPass(ipass);
m_prgppass[ipass]->ReadFromFont(grstrm, fxdSilfVersion, fxdRuleVersion,
lSubTableStart + rgnPassOffsets[ipassFont]);
m_ipassPos1++;
}
else if (ipassPos1 <= ipass)
{
m_prgppass[ipass] = new GrPosPass(ipass);
if (ipassFont < cpassFont)
{
m_prgppass[ipass]->ReadFromFont(grstrm, fxdSilfVersion, fxdRuleVersion,
lSubTableStart + rgnPassOffsets[ipassFont]);
}
else
{
// No positioning pass in font: create a bogus one.
m_prgppass[ipass]->InitializeWithNoRules();
}
}
else
{
Assert(false);
return false; // bad table
}
}
return true;
}
/*----------------------------------------------------------------------------------------------
Create passes corresponding to an invalid font.
----------------------------------------------------------------------------------------------*/
void GrTableManager::CreateEmpty()
{
m_cpass = 2;
m_prgppass = new GrPass*[2];
m_prgppass[0] = new GrGlyphGenPass(0);
m_prgppass[1] = new GrPosPass(1);
m_prgppass[1]->InitializeWithNoRules();
m_ipassPos1 = 1;
m_cpassLB = 0;
m_fBidi = false;
m_engst.CreateEmpty();
}
void EngineState::CreateEmpty()
{
m_fStartLineContext = false;
m_fEndLineContext = false;
}
/*----------------------------------------------------------------------------------------------
Create the slot streams corresponding to the passes.
----------------------------------------------------------------------------------------------*/
void EngineState::CreateSlotStreams()
{
if (m_prgpsstrm)
return;
m_prgpsstrm = new GrSlotStream * [m_cpass];
for (int ipass = 0; ipass < m_cpass; ++ipass)
{
m_prgpsstrm[ipass] = new GrSlotStream(ipass);
}
}
/*----------------------------------------------------------------------------------------------
Store information about the pass-states in the pass objects.
----------------------------------------------------------------------------------------------*/
void GrTableManager::StorePassStates(PassState * rgzpst)
{
for (int ipass = 0; ipass < m_cpass; ipass++)
Pass(ipass)->SetPassState(rgzpst + ipass);
}
/*----------------------------------------------------------------------------------------------
Demand-driven loop: with goal of filling up final slot stream, loop backward and forward
filling up slot streams until final stream has reached physical capacity.
@param pgjus - justification agent; NULL if justification is of no interest
@param jmodi - (internal) justification mode
@param fStartLine, fEndLine
@param ichSegLim - known end of segment; -1 when using backtracking
@param dxWidthRequested - available width; when justifying, exact width desired
@param dxUnjustifiedWidth - when justifying, what the width would be normally
@param fNeedFinalBreak - true if the end of the segment needs to be a valid break point
@param fMoreText - true if char stream doesn't go up to text-stream lim
@param ichFontLim - end of the range that could be rendered by this font
@param fInfiniteWidth - used for "measured" segments
@param fWidthIsCharCount - kludge for test procedures: the width is a character count,
not a pixel count
@param lbPref - try for longest segment ending with this breakweight
- when justifying, lb to assume
@param lbMax - max (last resort) breakweight if no preferred break possible
@param ichwCallerBtLim - caller's backtrack lim; -1 if caller is not backtracking
@param twsh - how to handle trailing white-space
@param fParaRtl - overall paragraph direction
@param nDirDepth - direction depth of this segment
----------------------------------------------------------------------------------------------*/
void GrTableManager::Run(Segment * psegNew, Font * pfont,
GrCharStream * pchstrm, IGrJustifier * pgjus, int jmodi,
LayoutEnvironment & layout,
int ichSegLim,
float dxWidthRequested, float dxUnjustifiedWidth,
bool fNeedFinalBreak, bool fMoreText, int ichFontLim,
bool fInfiniteWidth, bool fWidthIsCharCount,
int ichwCallerBtLim,
int nDirDepth, SegEnd estJ)
{
bool fStartLine = layout.startOfLine();
bool fEndLine = layout.endOfLine();
LineBrk lbPref = layout.bestBreak();
LineBrk lbMax = layout.worstBreak();
TrWsHandling twsh = layout.trailingWs();
bool fParaRtl = layout.rightToLeft();
Segment * psegPrev = layout.prevSegment();
Segment * psegInit = layout.segmentForInit();
std::ostream * pstrmLog = layout.loggingStream();
m_engst.Initialize(Engine(), this); // do this after the tables have been fully read
m_engst.m_jmodi = jmodi;
m_engst.m_ipassJustCalled = -1;
m_engst.m_twsh = twsh;
m_engst.m_fParaRtl = fParaRtl;
m_engst.m_dxsShrinkPossible = GrSlotState::kNotYetSet;
m_fLogging = (pstrmLog != NULL);
int cbPrev;
byte * pbPrevSegDat = NULL;
if (psegPrev)
cbPrev = psegPrev->NextSegInitBuffer(&pbPrevSegDat);
else if (psegInit)
cbPrev = psegInit->ThisSegInitBuffer(&pbPrevSegDat);
else
cbPrev = 0;
if (pchstrm->IsEmpty())
{
m_engst.m_lbPrevEnd = klbNoBreak;
InitSegmentAsEmpty(psegNew, pfont, pchstrm, fStartLine, fEndLine);
psegNew->SetWidths(0, 0);
psegNew->SetUpOutputArrays(pfont, this, NULL, 0, 0, 0, twsh,
fParaRtl, nDirDepth, true);
psegNew->SetLayout(layout);
return;
}
int islotUnderBreak = -1; // position of inserted or final break point in underlying
// data; none so far (if an actual LB has been inserted,
// this is the index of that slot; otherwise it is the
// index of the last valid slot)
int islotSurfaceBreak = -1; // position of line break in surface stream; none so far
LineBrk lbBestToTry = lbPref;
LineBrk lbFound = klbNoBreak;
float dxMaxWidth = dxWidthRequested;
if (m_engst.m_jmodi == kjmodiJustify)
{
Assert(fInfiniteWidth);
lbBestToTry = klbClipBreak;
lbFound = lbPref;
}
float dxWidth;
std::vector<int> vnslotNeeded;
vnslotNeeded.resize(m_cpass);
m_engst.InitializeStreams(this, pchstrm, cbPrev, pbPrevSegDat, fNeedFinalBreak, &islotUnderBreak);
int ipassCurr;
int nNeedInput; // kNextPass: go on to the next pass
// kBacktrack: need to insert a line break
// > 0: need more input from previous pass
int cchwPostXlbContext = m_pgreng->PostXlbContext();
if (ichSegLim > -1)
{
// When we know exactly where the end of the segment will be, get as much as
// we need from the character stream, and then fill up the remaining streams.
// (3 below is an arbitrary number; we just want to get slightly more than
// we really need.)
vnslotNeeded[0] = ichSegLim - pchstrm->Min() + cchwPostXlbContext + 3;
for (int ipass = 1; ipass < m_cpass; ipass++)
vnslotNeeded[ipass] = 10000;
// Start at pass 0.
ipassCurr = 0;
}
else
{
// When we're doing line-breaking to find the end of the segment, use the
// demand-driven approach that tries to fill up the available space.
vnslotNeeded[m_cpass - 1] = 1;
// Start at the last pass. (It might be more efficient to start at the first pass and
// generate a small arbitrary number of glyphs.)
ipassCurr = m_cpass - 1;
}
while (ipassCurr < m_cpass)
{
if (ipassCurr == 0)
{
int nNeedNow = vnslotNeeded[0];
// Zeroth pass: generate glyph IDs from the underlying input.
nNeedInput = Pass(0)->ExtendGlyphIDOutput(this,
pchstrm, OutputStream(0), ichSegLim, cchwPostXlbContext, lbPref,
nNeedNow, fNeedFinalBreak, m_engst.m_twsh, &islotUnderBreak);
Assert(nNeedInput == kNextPass || nNeedInput == kBacktrack);
vnslotNeeded[0] -= nNeedNow;
}
else if (ipassCurr < m_cpass - 1)
{
// Middle pass.
int cslotNeedNow = vnslotNeeded[ipassCurr];
cslotNeedNow = max(1, cslotNeedNow);
int cslotGot;
Pass(ipassCurr)->ExtendOutput(this,
InputStream(ipassCurr), OutputStream(ipassCurr), cslotNeedNow, twsh,
&nNeedInput, &cslotGot, &islotUnderBreak);
vnslotNeeded[ipassCurr] -= cslotGot;
//if (nNeedInput != kNextPass && nNeedInput != kBacktrack)
// nNeedInput = max(nNeedInput, cslotNeedNow - cslotGot);
}
else // (ipassCurr == m_cpass - 1)
{
// Final pass: position and test allotted space.
float dxWidthWShrink = dxMaxWidth;
if (m_engst.m_dxsShrinkPossible != GrSlotState::kNotYetSet)
dxWidthWShrink += (int)m_engst.m_dxsShrinkPossible;
nNeedInput = Pass(ipassCurr)->ExtendFinalOutput(this,
InputStream(ipassCurr), OutputStream(ipassCurr),
dxWidthWShrink, fWidthIsCharCount, fInfiniteWidth,
(islotUnderBreak > -1), (fNeedFinalBreak && lbFound == klbNoBreak),
lbMax, twsh,
&islotSurfaceBreak, &dxWidth);
}
if (m_engst.m_jmodi == kjmodiJustify)
{
if (ipassCurr < m_ipassJust1 && !OutputStream(ipassCurr)->FullyWritten())
{
// When justifying, fill up all the streams up to the point where we need
// to interact with the justification routine.
// I think this code is obsolete.
int nStillNeed = 1000;
if ((ipassCurr == 1 && nNeedInput != kNextPass) || ipassCurr == 0)
nStillNeed = ichSegLim - (pchstrm->Pos() - pchstrm->Min())
+ cchwPostXlbContext + 3;
if (nStillNeed > 0)
{
if (nNeedInput == kNextPass)
ipassCurr++; // repeat this pass
nNeedInput = nStillNeed;
}
}
else if (nNeedInput == kNextPass && ipassCurr + 1 == m_ipassJust1)
{
CallJustifier(pgjus, ipassCurr, dxUnjustifiedWidth, dxWidthRequested, fEndLine);
}
}
DetermineShrink(pgjus, ipassCurr);
if (nNeedInput == kBacktrack)
{
// Final output has overflowed its space--find (or adjust) the break point.
bool fFoundBreak = Backtrack(&islotUnderBreak,
&lbBestToTry, lbMax, twsh, fMoreText, ichwCallerBtLim, &lbFound);
if (!fFoundBreak)
{
// Nothing will fit. Initialize the new segment just enough so that
// we can delete it.
InitSegmentToDelete(psegNew, pfont, pchstrm);
return;
}
}
else if (nNeedInput == kNextPass)
{
// This pass is sufficiently full--return to following pass.
ipassCurr++;
}
else
{
// Get more input from previous pass.
ipassCurr--;
vnslotNeeded[ipassCurr] = nNeedInput;
}
}
// At this point we have fully transduced all the text for this segment.
// Figure out why we broke the segment and what the caller is supposed to do about it.
SegEnd est;
if (m_engst.m_jmodi == kjmodiJustify)
{
// Don't change what was passed in.
est = estJ;
}
else if (m_engst.m_fRemovedWhtsp) // (islotUnderBreak > -1 && twsh == ktwshNoWs)
{
Assert(!m_engst.m_fInsertedLB);
est = kestMoreWhtsp;
}
else if (m_engst.m_fHitHardBreak)
{
est = kestHardBreak;
}
else if (!m_engst.m_fExceededSpace && !fMoreText)
// Rendered to the end of the original requested range.
est = kestNoMore;
// else if (!m_fExceededSpace && !fNextIsSameWs && fMoreText)
// // Writing system break; may be more room on the line. But see tweak below.
// *pest = kestWsBreak;
else if (m_engst.m_fExceededSpace)
{
// Exceeded available space, found a legal break.
if (twsh == ktwshNoWs && m_engst.m_fRemovedWhtsp)
est = kestMoreWhtsp;
else
est = kestMoreLines;
}
else if (twsh == ktwshOnlyWs)
{
est = kestOkayBreak; // but see tweak below
}
else
{
// Broke because of something like a font change. Determine whether this is a legal
// break or not.
// TODO SharonC: handle the situation where it is legal to break BEFORE the first
// character of the following segment.
Assert(fMoreText);
//Assert(fNextIsSameWs);
if (ichwCallerBtLim > -1)
{
Assert(m_engst.m_fInsertedLB);
}
else if (ichSegLim > -1)
{
// We stopped where caller said to.
Assert(fInfiniteWidth);
}
else
{
Assert(!m_engst.m_fInsertedLB);
Assert(islotUnderBreak == -1 || m_engst.m_fFinalLB);
}
int islotTmp = OutputStream(m_cpass - 1)->WritePos();
GrSlotState * pslotTmp;
if (m_engst.m_fFinalLB || m_engst.m_fInsertedLB)
pslotTmp = OutputStream(m_cpass-1)->SlotAt(islotTmp - 2);
else
pslotTmp = OutputStream(m_cpass-1)->SlotAt(islotTmp - 1);
if (abs(pslotTmp->BreakWeight()) <= lbPref)
est = kestOkayBreak;
else if (abs(pslotTmp->BreakWeight()) > lbMax)
est = kestBadBreak;
else if (OutputStream(m_cpassLB)->HasEarlierBetterBreak(islotUnderBreak, lbFound,
this->LBGlyphID()))
{
est = kestBadBreak;
}
else
est = kestOkayBreak;
// We no longer have the ability to know whether or not the next segment will use
// the same writing system.
//if (est == kestBadBreak && m_engst.m_fExceededSpace && !fNextIsSameWs && fMoreText)
// est = kestWsBreak;
}
// Make a segment out of it and return it.
int dichSegLen;
InitNewSegment(psegNew, pfont, pchstrm, pgjus,
islotUnderBreak, islotSurfaceBreak, fStartLine, fEndLine, ichFontLim, lbFound, est,
&dichSegLen);
if (psegNew->segmentTermination() == kestNothingFit)
{
return;
}
else if (psegNew->segmentTermination() == kestHardBreak &&
psegNew->startCharacter() == psegNew->stopCharacter())
{
// Empty segment caused by a hard-break.
m_engst.m_lbPrevEnd = klbNoBreak;
psegNew->SetWidths(0, 0);
psegNew->SetUpOutputArrays(pfont, this, NULL, 0, 0, 0, twsh,
fParaRtl, nDirDepth, true);
psegNew->SetLayout(layout);
return;
}
psegNew->RecordInitializationForThisSeg(cbPrev, pbPrevSegDat);
if ((est == kestNoMore || est == kestWsBreak) && twsh == ktwshOnlyWs &&
dichSegLen < pchstrm->Lim() - pchstrm->Min())
{
// If we are asking for white-space only, and we didn't use up everything in the font
// range, we could possibly have gotten an initial white-space only segment
// (as opposed to a writing system break).
est = kestOkayBreak;
psegNew->FixTermination(est);
}
// Do this before fixing up the associations below.
//if (m_pgreng->LoggingTransduction())
// WriteTransductionLog(pchstrm, *ppsegRet,
// cbPrev, pbPrevSegDat, pbNextSegDat, pcbNextSegDat);
WriteTransductionLog(pstrmLog, pchstrm, psegNew, cbPrev, pbPrevSegDat);
RecordAssocsAndOutput(pfont, psegNew, fWidthIsCharCount, twsh, fParaRtl, nDirDepth);
// Do this after fixing up the associations.
//if (m_pgreng->LoggingTransduction())
// WriteAssociationLog(pchstrm, *ppsegRet);
WriteAssociationLog(pstrmLog, pchstrm, psegNew);
psegNew->SetLayout(layout);
}
/*----------------------------------------------------------------------------------------------
Create a new segment to return.
@param pchstrm - input stream
@param pgjus - justification agent, in case this segment needs to be
stretched or shrunk
@param islotLBStreamBreak - position of inserted line break slot (which should be equal
to the position in the glyph generation stream);
-1 if no line-break was inserted (we rendered to the
end of the range)
@param islotSurfaceBreak - position of line break slot in final (surface) stream
@param fStartLine - does this segment start a line?
@param fEndLine - does this segment end a line?
@param ichFontLim - end of range that could be rendered by this font
@param lbEndFound - line break found; possibly adjusted here
@param est - why did the segment terminate
----------------------------------------------------------------------------------------------*/
void GrTableManager::InitNewSegment(Segment * psegNew,
Font * pfont, GrCharStream * pchstrm, IGrJustifier * pgjus,
int islotLBStreamBreak, int islotSurfaceBreak, bool fStartLine, bool fEndLine, int ichFontLim,
LineBrk lbEndFound, SegEnd est, int * pdichSegLen)
{
LineBrk lbStart = m_engst.m_lbPrevEnd;
LineBrk lbEnd = lbEndFound;
if (est == kestBadBreak)
lbEnd = klbLetterBreak;
int ichwMin = pchstrm->Min();
int ichwLim;
// int cslotPreSegStream0 = m_cslotPreSeg;
// if (m_fInitialLB && m_cpassLB > 0)
// {
// // Initial LB is included in m_cslotPreSeg, but is not in stream 0.
// // --Not really needed, because both islotLBStreamBreak and m_cslotPreSeg take
// // the initial LB into account if it is there.
// cslotPreSegStream0--;
// }
// No longer have to subtract this, because the chunk map takes it into
// consideration.
//int cslotPreInitialLB = m_cslotPreSeg - (m_fInitialLB ? 1 : 0);
//int ichwOldTmp;
if (m_engst.m_fInsertedLB)
{
Assert(islotLBStreamBreak > -1);
int ichwSegLim = m_engst.LbSlotToSegLim(islotLBStreamBreak, pchstrm, m_cpassLB);
ichwLim = ichwSegLim + pchstrm->Min(); // - cslotPreInitialLB;
// Old result, for debugging:
//ichwOldTmp = islotLBStreamBreak + pchstrm->Min() - m_cslotPreSeg;
}
else if (m_engst.m_fFinalLB || islotLBStreamBreak == -1)
ichwLim = pchstrm->Lim();
else
{
int ichwSegLim = m_engst.LbSlotToSegLim(islotLBStreamBreak, pchstrm, m_cpassLB);
ichwLim = ichwSegLim + pchstrm->Min(); // - cslotPreInitialLB;
// Old result, for debugging:
// islotLBStreamBreak is the last slot in the segment, not the LB glyph; hence +1.
//ichwOldTmp = islotLBStreamBreak + pchstrm->Min() - m_cslotPreSeg + 1;
}
*pdichSegLen = ichwLim - ichwMin;
if (ichwMin >= ichwLim)
{
if (est == kestHardBreak)
{
// Empty segment cause by a hard-break.
InitSegmentAsEmpty(psegNew, pfont, pchstrm, fStartLine, fEndLine);
psegNew->FixTermination(est);
}
else
{
// Invalid empty segment.
Assert(ichwMin == ichwLim);
InitSegmentToDelete(psegNew, pfont, pchstrm);
}
return;
}
Assert(psegNew);
psegNew->Initialize(pchstrm->TextSrc(),
ichwMin, ichwLim,
lbStart, lbEnd, est, fStartLine, fEndLine, m_pgreng->RightToLeft());
psegNew->SetEngine(m_pgreng);
psegNew->SetFont(pfont);
psegNew->SetJustifier(pgjus);
psegNew->SetFaceName(m_pgreng->FaceName(), m_pgreng->BaseFaceName());
bool fNextSegNeedsContext =
!(est == kestNoMore || est == kestWsBreak
|| ichwLim >= pchstrm->Lim() || ichwLim >= ichFontLim);
InitializeForNextSeg(psegNew, islotLBStreamBreak, islotSurfaceBreak, lbEnd,
fNextSegNeedsContext, pchstrm);
psegNew->SetPreContext(0 - m_pgreng->PreXlbContext());
}
/*----------------------------------------------------------------------------------------------
The newly created segment is invalid, for instance, because we were backtracking and
couldn't find a valid break point. Initialize it just enough so that it can be
deleted by the client.
----------------------------------------------------------------------------------------------*/
void GrTableManager::InitSegmentToDelete(Segment * psegNew, Font * pfont,
GrCharStream * pchstrm)
{
psegNew->Initialize(pchstrm->TextSrc(), 0, 0,
klbClipBreak, klbClipBreak, kestNothingFit, false, false,
m_pgreng->RightToLeft());
psegNew->SetEngine(m_pgreng);
psegNew->SetFont(pfont);
psegNew->SetJustifier(NULL); // can't justify an empty segment
psegNew->SetFaceName(m_pgreng->FaceName(), m_pgreng->BaseFaceName());
psegNew->SetPreContext(0);
}
/*----------------------------------------------------------------------------------------------
Map from the given slot (which is generally the final LB slot) to the end of the segment,
taking into account the fact that a single slot may represent two 16-bit surrogates.
There are several approaches that seem reasonable but do NOT work. (I tried them!)
You can't calculate it directly from the length of the streams, because that doesn't take
into account the possible presence of surrogate pairs in the character stream. You can't
use the chunk map at the point of the LB, because if a rule spanned a LB the chunk won't
have ended there and so there won't be any valid information.
What seems to work is to figure out the slot(s) in the zeroth stream using the associations
and then use the segment offset for this slot. This should be reliable because there is
always a one-to-one correspondence between glyphs in the line-break passes (ie, no
insertions or deletions), so the associations should be dependable and straightforward.
----------------------------------------------------------------------------------------------*/
int EngineState::LbSlotToSegLim(int islotLB, GrCharStream * pchstrm, int cpassLB)
{
Assert(islotLB < OutputStream(cpassLB)->WritePos());
// Figure out the corresponding characters in the zeroth stream. These should be straight-
// forward to figure out because no substitutions are permitted in the LB passes.
// I do the slots before and after the LB, if possible, as a sanity check.
GrSlotStream * psstrmLB = OutputStream(cpassLB);
GrSlotState * pslotPreLB = (m_fFinalLB || m_fInsertedLB) ?
psstrmLB->SlotAt(islotLB - 1) :
psstrmLB->SlotAt(islotLB);
GrSlotState * pslotPostLB = (psstrmLB->WritePos() > islotLB + 1) ?
psstrmLB->SlotAt(islotLB + 1) :
NULL;
int ichwPreLB = pslotPreLB->AfterAssoc();
int ichwSegLim;
if (pslotPostLB)
{
ichwSegLim = pslotPostLB->BeforeAssoc();
Assert(ichwPreLB + 1 == ichwSegLim
|| ichwPreLB + 2 == ichwSegLim); // in case of a surrogate
}
else
{
ichwSegLim = ichwPreLB + 1;
while (!pchstrm->AtUnicodeCharBoundary(ichwSegLim))
ichwSegLim++;
Assert(pchstrm->AtUnicodeCharBoundary(ichwSegLim));
}
// Old buggy routine that doesn't handle the case where a rule (and hence the chunk map) did
// not end at the LB character:
// Map backwards through the LB pass streams, if any.
//int islotAdjusted = islotLB + 1; // after the LB
//for (int ipass = m_cpassLB; ipass > 0; ipass--)
//{
// islotAdjusted = ChunkInPrev(ipass, islotAdjusted, pchstrm);
//}
//
//int ichwSegLim = ChunkInPrev(0, islotAdjusted, pchstrm);
// Mapping from 32-bit to 16-bit should only increase the index. Except that there
// may be a LB in the slot stream that is not in the character stream, so it could be
// one less.
Assert(ichwSegLim + m_cslotPreSeg >= islotLB - 1);
return ichwSegLim;
}
/*----------------------------------------------------------------------------------------------
Return the chunk mapping into the previous stream, taking into account the fact that it
is not recorded for the write-position.
----------------------------------------------------------------------------------------------*/
int GrTableManager::ChunkInPrev(int ipass, int islot, GrCharStream * pchstrm)
{
GrSlotStream * psstrmOut = OutputStream(ipass);
GrSlotStream * psstrmIn = (ipass == 0) ? NULL : InputStream(ipass);
int islotTmp = islot;
int islotAdjusted;
do {
if (islot >= psstrmOut->WritePos())
{
Assert(islot == psstrmOut->WritePos());
islotAdjusted = (ipass == 0) ? pchstrm->SegOffset() : psstrmIn->ReadPos();
Assert(islotAdjusted > -1);
}
else
islotAdjusted = psstrmOut->ChunkInPrev(islotTmp);
islotTmp--;
} while (islotAdjusted == -1);
return islotAdjusted;
}
/*----------------------------------------------------------------------------------------------
Create a new segment corresponding to an empty string.
@param pchstrm - input stream
@param fStartLine - does this segment start a line?
@param fEndLine - does this segment end a line?
----------------------------------------------------------------------------------------------*/
void GrTableManager::InitSegmentAsEmpty(Segment * psegNew, Font * pfont,
GrCharStream * pchstrm, bool fStartLine, bool fEndLine)
{
LineBrk lbStart = m_engst.m_lbPrevEnd;
LineBrk lbEnd = klbNoBreak;
int ichwMin = 0;
int ichwLim = 0;
Assert(psegNew);
psegNew->Initialize(pchstrm->TextSrc(), ichwMin, ichwLim,
lbStart, lbEnd, kestNoMore, fStartLine, fEndLine, m_pgreng->RightToLeft());
psegNew->SetEngine(m_pgreng);
psegNew->SetFont(pfont);
psegNew->SetJustifier(NULL); // can't justify an empty segment
psegNew->SetFaceName(m_pgreng->FaceName(), m_pgreng->BaseFaceName());
byte pbNextSegDat[256];
int cbNextSegDat;
int * pcbNextSegDat = &cbNextSegDat;
// Initialization for (theoretical) next segment.
byte * pb = pbNextSegDat;
*pb++ = byte(lbEnd);
*pb++ = kdircNeutral;
*pb++ = kdircNeutral;
*pb++ = 0;
for (int ipass = 0; ipass < m_cpass; ipass++)
*pb++ = 0;
*pcbNextSegDat = 0;
psegNew->RecordInitializationForNextSeg(*pcbNextSegDat, pbNextSegDat);
psegNew->SetPreContext(0);
}
/*----------------------------------------------------------------------------------------------
Find a line-break and unwind the passes. Return true if we were able to
backtrack successfully (which may have meant using a less-than-optimal line-break).
Return false if no line-break could be found (because maxBreakWeight did not allow
letter breaks or clipping--generally because there was already something on this
line).
@param pislotPrevBreak - position of previously-created break, -1 if none;
this is the index of the line-break glyph if any,
or the index of the last slot in the segment;
adjusted to contain the position of the newly
found break
@param plbMin - minimum (best) line break weight to try
@param lbMax - maximum (worst possible) line break weight
@param twsh - how to handle trailing white-space
@param fMoreText - true if char stream doesn't go up to original lim
@param ichwCallerBtLim - caller's backtrack lim; -1 if caller is not backtracking
@param plbFound - kind of line-break created
----------------------------------------------------------------------------------------------*/
bool GrTableManager::Backtrack(int * pislotPrevBreak,
LineBrk * plbMin, LineBrk lbMax, TrWsHandling twsh, bool fMoreText,
int ichwCallerBtLim,
LineBrk * plbFound)
{
int islotStartTry;
//if (*pislotPrevBreak == ichwCallerBtLim - 1)
//{
// islotStartTry = *pislotPrevBreak;
//}
//else
if (*pislotPrevBreak == -1)
{
// If no line break has been found so far, figure out where to start trying based
// on where the final stream choked.
GrSlotStream * psstrmFinal = OutputStream(m_cpass-1);
if ((islotStartTry
= m_engst.TraceStreamZeroPos(psstrmFinal->WritePos()-1, TopDirectionLevel()))
== -1)
{
// Just start looking at the end of what's been generated.
islotStartTry = OutputStream(m_cpassLB)->ReadPos() - 1;
}
}
else
{
// Start just before previous line break.
if (m_engst.m_fInsertedLB || m_engst.m_fFinalLB)
islotStartTry = *pislotPrevBreak - 2; // skip inserted LB and previous slot
else
islotStartTry = *pislotPrevBreak - 1; // skip previous slot
}
if (ichwCallerBtLim > -1 && islotStartTry > ichwCallerBtLim - 1)
{
islotStartTry = ichwCallerBtLim - 1;
}
// Determine if we want to insert an actual line-break "glyph". Don't do that if
// we are omitting trailing white space or we are at a writing-system or font break.
bool fInsertLB;
if (twsh == ktwshNoWs)
// omitting trailing white-space
fInsertLB = false;
else if (twsh == ktwshOnlyWs)
// trailing white-space segment
fInsertLB = true;
else if (ichwCallerBtLim > -1)
// backtracking
fInsertLB = true;
else if (*pislotPrevBreak > -1)
// no longer at the edge of the writing system or font
fInsertLB = true;
else if (!fMoreText)
// at final edge of total range to render
fInsertLB = true;
else
fInsertLB = false;
// Try to insert a line-break in the output of the (final) line-break pass (if any, or
// the output of the glyph-generation pass). First try the preferred (strictest)
// break weight and gradually relax.
LineBrk lb = *plbMin;
Assert(*plbMin <= lbMax);
GrSlotStream * psstrmLB = OutputStream(m_cpassLB);
islotStartTry = min(islotStartTry, psstrmLB->WritePos() - 1);
int islotNewBreak = -1;
while (lb <= lbMax)
{
LineBrk lbNextToTry;
if (fInsertLB)
{
islotNewBreak = psstrmLB->InsertLineBreak(this,
*pislotPrevBreak, m_engst.m_fInsertedLB, islotStartTry,
lb, twsh, m_engst.m_cslotPreSeg, &lbNextToTry);
}
else
{
islotNewBreak = psstrmLB->MakeSegmentBreak(this,
*pislotPrevBreak, m_engst.m_fInsertedLB, islotStartTry,
lb, twsh, m_engst.m_cslotPreSeg, &lbNextToTry);
}
if (islotNewBreak > -1)
break;
if (lb >= lbMax)
break;
lb = IncLineBreak(lb); // relax the break weight
}
if (islotNewBreak == -1)
{
return false;
}
// We've successfully inserted a line break.
if (fInsertLB)
m_engst.m_fInsertedLB = true;
m_engst.m_fFinalLB = false;
UnwindAndReinit(islotNewBreak);
*pislotPrevBreak = islotNewBreak;
*plbMin = lb; // return the best break we were able to find, so we don't keep trying
// for a better one when we know now that we can't find it
*plbFound = lb;
return true;
}
/*----------------------------------------------------------------------------------------------
This method is called after backtracking or removing trailing white-space.
Unwind the following slot streams as necessary depending on where the change was made.
@param islotNewBreak - in output of (final) line-break pass
----------------------------------------------------------------------------------------------*/
void GrTableManager::UnwindAndReinit(int islotNewBreak)
{
OutputStream(m_cpassLB)->ZapCalculatedDirLevels(islotNewBreak);
// Mark the passes before the line-break pass as fully written, so that if there
// isn't a line-break glyph to intercept, we don't keep trying to get more input
// from them.
int ipass;
for (ipass = 1; ipass < m_cpassLB + 1; ++ipass)
{
InputStream(ipass)->MarkFullyWritten();
}
// Unwind the passes to the changed positions.
// Note that we don't need to unwind pass 0, since all it does is generate glyph IDs.
// Also we don't need to unwind the line-break passes, because conceptually they happen
// before the break is inserted.
int islotChgPos = islotNewBreak;
bool fFirst = true;
for (ipass = m_cpassLB + 1 ; ipass < m_cpass ; ++ipass)
{
islotChgPos =
Pass(ipass)->Unwind(this, islotChgPos, InputStream(ipass), OutputStream(ipass),
fFirst);
fFirst = false;
}
// For anything that may have been skipped in the final output:
OutputStream(m_cpass - 1)->SetReadPos(0);
OutputStream(m_cpass - 1)->SetReadPosMax(0);
Pass(m_cpass - 1)->UndoResyncSkip();
OutputStream(m_cpass - 1)->ClearSlotsSkippedToResync();
m_engst.InitPosCache();
m_engst.m_dxsShrinkPossible = GrSlotState::kNotYetSet; // recalculate it
}
/*----------------------------------------------------------------------------------------------
Store information in the newly created segment that will allow us to reinitalize
the streams in order to process the next segment, appropriately handling any
cross-line-boundary contextuals.
The information required is the number of glyphs in the first stream that must
be reprocessed, and a skip-offset between each stream indicating the beginning
of chunk boundaries.
In most cases, where there are no cross-line-boundary contextuals, all these numbers
will be zero.
The buffer generated is of the following format (all are 1 byte, unsigned):
end breakweight
previous strong directionality code
previous terminator dir code
restart backup
skip offsets for each pass
This format must match what is generated by InitializeStreams.
@param pseg - newly created segment
@param islotUnderBreak - position of line break inserted in underlying stream (output
of glyph-generation pass--stream 0); -1 if we rendered to
the end of the range
@param islotSurfaceBreak - position of line break in surface stream;
-1 if we rendered to the end of the range
@param lbEnd - kind of line-break at the end of this segment
@param fNextSegNeedsContext - true if we need to remember the context for the next seg
@param pchstrm - character stream input for this segment
@param cbNextMax - amount of space available in the buffer
@param pbNextSegDat - buffer for initializing next segment
@param pcbNextSegDat - amount of space used in buffer
----------------------------------------------------------------------------------------------*/
void GrTableManager::InitializeForNextSeg(Segment * pseg,
int islotUnderBreak, int islotSurfaceBreak, LineBrk lbEnd,
bool fNextSegNeedsContext, GrCharStream * pchstrm)
{
byte pbNextSegDat[256];
int cbNextSegDat;
int * pcbNextSegDat = &cbNextSegDat;
std::vector<int> vcslotSkipOffsets;
vcslotSkipOffsets.resize(m_cpass);
byte * pb = pbNextSegDat;
if (!fNextSegNeedsContext)
{
// No contextualization between this segment and the next, whatever kind of renderer
// it might use.
*pcbNextSegDat = 0;
return;
}
gid16 chwLB = LBGlyphID();
int islotUnderLim = islotUnderBreak;
if (!m_engst.m_fInsertedLB && !m_engst.m_fFinalLB)
// Then islotUnderBreak is the position of the last slot in the segment; increment
// to get the lim in stream zero.
islotUnderLim++;
int islotSurfaceLim = islotSurfaceBreak;
if (islotSurfaceLim == -1)
islotSurfaceLim = OutputStream(m_cpass - 1)->FinalSegLim();
*pb++ = byte(lbEnd);
// Find previous strong and terminator directionality codes.
GrSlotStream * psstrm = OutputStream(m_cpass - 1);
DirCode dircStrong = kdircNeutral;
DirCode dircTerm = kdircNeutral;
int islot;
for (islot = islotSurfaceLim; islot-- > 0; )
{
GrSlotState * pslot = psstrm->SlotAt(islot);
DirCode dirc = pslot->Directionality();
if (dircTerm == kdircNeutral && dirc == kdircEuroTerm)
dircTerm = dirc;
if (StrongDir(dirc))
{
dircStrong = dirc;
break;
}
}
*pb++ = byte(dircStrong);
*pb++ = byte(dircTerm);
// Record how much of each pass to redo in the next segment.
// The basic algorithm is described in the "WR Data Transform Engine" document.
// But here we are doing a variation on what is described there. We are calculating
// the backup locations for BOTH the rule start locations and the necessary pre-contexts.
// It is the precontext values that are recorded for use by the following segment. But we
// also calculate the positions for the rule-start locations to make sure there is enough
// pre-context at each pass.
int islotRS = islotSurfaceLim; // rule-start positions
int islotPC = islotRS; // to handle pre-context positions
// If line-breaks are irrelevant to this font, we don't need to bother.
bool fGiveUp = !m_pgreng->LineBreakFlag();
int ipass;
for (ipass = m_cpass - 1; ipass >= 0 && !fGiveUp; --ipass)
{
GrSlotStream * psstrmIn = (ipass == 0) ? NULL : InputStream(ipass);
GrSlotStream * psstrmOut = OutputStream(ipass);
int islotBackupMin = (ipass == 0) ? 0 : psstrmOut->SegMin();
if (ipass >= m_cpassLB && m_engst.m_fInitialLB)
islotBackupMin++; // how far we can back up--not before the start of this segment
int islotPrevRS = 0; // corresponding slot in the previous stream for rule-start position
int islotPrevPC = 0; // same for pre-context position
// Initially we have to include at least this many prior slots:
int cslotSkipOffset = 0;
if (islotRS == psstrmOut->WritePos())
{
islotPrevRS = (ipass == 0) ? pchstrm->Pos() : psstrmIn->ReadPos();
}
else
{
// Ignore the final line-break, because it is explicitly inserted during
// the (final) line-break pass.
// if (ipass == m_cpassLB && psstrm->SlotAt(islotRS)->IsFinalLineBreak(chwLB))
// {
// Assert(islotRS > 0);
// islotRS--;
// }
// Ignore the chunk in the pass that generated the line break that corresponds
// to the inserted line break being chunked with the previous glyph--we don't
// want to treat that as a cross-line-boundary contextual!
if (ipass == m_cpassLB && psstrmOut->SlotAt(islotRS)->IsFinalLineBreak(chwLB))
{
// Assert(psstrm->ChunkInPrev(islotRS) == -1);
if (islotRS + 1 == psstrmOut->WritePos())
islotPrevRS = (ipass == 0) ? pchstrm->SegOffset() : psstrmIn->ReadPos();
else
islotPrevRS = psstrmOut->ChunkInPrev(islotRS + 1);
}
else
{
// Back up to the beginning of a chunk.
while (islotRS >= islotBackupMin &&
(islotPrevRS = psstrmOut->ChunkInPrev(islotRS)) == -1)
{
--islotRS;
//++cslotSkipOffset;
}
}
}
if (islotPrevRS == -1 || islotRS < islotBackupMin) // hit the start of the segment
{
// In order to get enough context to be sure to run the same rules again,
// we'd have to back up into the previous segment. Too complicated, so give up.
fGiveUp = true;
break;
}
int cslotPreContext = Pass(ipass)->MaxRulePreContext();
LBackupPC:
if (islotPC == psstrmOut->WritePos())
{
islotPrevPC = (ipass == 0) ? pchstrm->Pos() : psstrmIn->ReadPos();
}
else
{
if (ipass == m_cpassLB && psstrmOut->SlotAt(islotPC)->IsFinalLineBreak(chwLB))
{
if (islotPC+1 == psstrmOut->WritePos())
islotPrevPC = (ipass == 0) ? pchstrm->SegOffset() : psstrmIn->ReadPos();
else
islotPrevPC = psstrmOut->ChunkInPrev(islotPC+1);
}
else
{
// Back up to the beginning of a chunk; also make sure there are enough slots
// to handle the required pre-context for this pass.
while (islotPC >= islotBackupMin &&
((islotPrevPC = psstrmOut->ChunkInPrev(islotPC)) == -1 ||
islotPrevRS - islotPrevPC < cslotPreContext))
{
--islotPC;
++cslotSkipOffset;
}
}
}
if (islotPC >= islotBackupMin && islotPrevRS - islotPrevPC < cslotPreContext)
{
--islotPC;
++cslotSkipOffset;
goto LBackupPC;
}
if (islotPrevPC == -1 || islotPC < islotBackupMin) // hit the start of the segment
{
fGiveUp = true;
break;
}
vcslotSkipOffsets[ipass] = cslotSkipOffset;
islotRS = islotPrevRS;
islotPC = islotPrevPC;
if (ipass == 0)
{
// Convert from underlying chars to stream-zero slots.
islotRS += m_engst.m_cslotPreSeg;
islotPC += m_engst.m_cslotPreSeg;
}
}
// The following should be the case unless they tried to position contextually across
// the line break boundary, which is a very strange thing to do! - no longer true,
// because the pre-context is now included in this count, and it normal to have
// a pre-context in the final positioning pass.
//Assert(vcslotSkipOffsets[m_cpass-1] == 0);
// Also no adjustments are made by the glyph-generation pass, so this should always
// be true:
Assert(vcslotSkipOffsets[0] == 0);
// The number of slots in the underlying input previous to the line-break
// that must be reprocessed:
int cslotPreLB = islotUnderLim - islotPC;
if (fGiveUp)
{
// We can't reasonably provide enough context, so don't try to do it at all.
cslotPreLB = 0;
for (ipass = 0; ipass < m_cpass; ipass++)
vcslotSkipOffsets[ipass] = 0;
}
Assert(cslotPreLB >= 0);
Assert(cslotPreLB < 0xFF);
*pb++ = byte(cslotPreLB);
for (ipass = 0; ipass < m_cpass; ipass++)
*pb++ = byte(vcslotSkipOffsets[ipass]);
*pcbNextSegDat = 4 + m_cpass;
pseg->RecordInitializationForNextSeg(*pcbNextSegDat, pbNextSegDat);
}
/*----------------------------------------------------------------------------------------------
Calculate the associations, and record the output slots in the segment.
----------------------------------------------------------------------------------------------*/
void GrTableManager::RecordAssocsAndOutput(Font * pfont,
Segment * pseg, bool fWidthIsCharCount,
TrWsHandling twsh, bool fParaRtl, int nDirDepth)
{
int cchwUnderlying = pseg->stopCharacter() - pseg->startCharacter();
GrSlotStream * psstrmFinal = OutputStream(m_cpass-1);
int csloutSurface = psstrmFinal->WritePos() - psstrmFinal->IndexOffset(); // # of output slots
psstrmFinal->SetNeutralAssociations(LBGlyphID());
float xsTotalWidth, xsVisWidth;
#ifdef OLD_TEST_STUFF
if (fWidthIsCharCount)
xsTotalWidth = xsVisWidth = 0;
else
#endif // OLD_TEST_STUFF
// Make sure the final positions are set for every glyph.
CalcPositionsUpTo(m_cpass-1, reinterpret_cast<GrSlotState *>(NULL),
&xsTotalWidth, &xsVisWidth);
pseg->SetWidths(xsVisWidth, xsTotalWidth);
pseg->SetUpOutputArrays(pfont, this, psstrmFinal, cchwUnderlying, csloutSurface, LBGlyphID(),
twsh, fParaRtl, nDirDepth);
// Set underlying-to-surface assocations in the segment.
CalculateAssociations(pseg, csloutSurface);
}
/*----------------------------------------------------------------------------------------------
Calculate the underlying-to-surface associations and ligature mappings.
Assumes the arrays have been properly initialized.
----------------------------------------------------------------------------------------------*/
void GrTableManager::CalculateAssociations(Segment * pseg, int csloutSurface)
{
GrSlotStream * psstrmFinal = OutputStream(m_cpass-1);
std::vector<int> vichwAssocs;
std::vector<int> vichwComponents;
for (int islot = psstrmFinal->IndexOffset(); islot < psstrmFinal->WritePos(); islot++)
{
GrSlotState * pslot = psstrmFinal->SlotAt(islot);
int islout = islot - psstrmFinal->IndexOffset();
if (!pslot->IsLineBreak(LBGlyphID()))
{
vichwAssocs.clear();
pslot->AllAssocs(vichwAssocs);
size_t iichw;
for (iichw = 0; iichw < vichwAssocs.size(); ++iichw)
{
pseg->RecordSurfaceAssoc(vichwAssocs[iichw], islout, 0);
}
vichwComponents.clear();
if (pslot->HasComponents())
pslot->AllComponentRefs(vichwComponents);
for (iichw = 0; iichw < vichwComponents.size(); iichw++)
{
pseg->RecordLigature(vichwComponents[iichw], islout, iichw);
}
}
}
AdjustAssocsForOverlaps(pseg);
pseg->CleanUpAssocsVectors();
// pseg->AdjustForOverlapsWithPrevSeg(); // for any characters officially in the
// previous segment but rendered in this one,
// or vice versa
// TODO SharonC: for all characters that are right-to-left in the underlying stream,
// reverse the before and after flags in the segment.
}
/*----------------------------------------------------------------------------------------------
For any slots associated with characters in the new segment but not rendered in the
segment, adjust the underlying-to-surface mappings accordingly. Ie, set the before
mapping to kNegInfinity for slots at the beginning of the streams, and set the
after mapping to kPosInfinity for slots at the end of the streams.
TODO SharonC: Rework this method more carefully to handle each slot exactly once. The current
approach could possibly, for instance, process a slot that is inserted in one pass
and deleted in the next.
----------------------------------------------------------------------------------------------*/
void GrTableManager::AdjustAssocsForOverlaps(Segment * pseg)
{
if (!m_engst.m_fInitialLB && !m_engst.m_fFinalLB && !m_engst.m_fInsertedLB)
// no cross-line contextualization possible
return;
gid16 chwLB = LBGlyphID();
std::vector<int> vichwAssocs;
// Process any slots output by any substitution pass (or later) on either side of the
// line-breaks. These are the relevant slots that are rendered in the previous and
// following segments.
for (int ipass = m_cpass; ipass-- > m_cpassLB + 1; )
{
GrSlotStream * psstrm = OutputStream(ipass);
int islotMin = (ipass == m_cpass - 1) ? psstrm->IndexOffset() : 0;
if (m_engst.m_fInitialLB)
{
for (int islot = islotMin; ; islot++)
{
GrSlotState * pslot = psstrm->SlotAt(islot);
if (pslot->IsInitialLineBreak(chwLB))
break;
Assert(!pslot->IsFinalLineBreak(chwLB));
if (pslot->PassModified() != ipass)
continue;
vichwAssocs.clear();
pslot->AllAssocs(vichwAssocs);
for (size_t iichw = 0; iichw < vichwAssocs.size(); ++iichw)
pseg->MarkSlotInPrevSeg(vichwAssocs[iichw], islot);
}
}
if (m_engst.m_fFinalLB || m_engst.m_fInsertedLB && ipass > m_cpassLB)
{
for (int islot = psstrm->WritePos(); islot-- > islotMin ; )
{
GrSlotState * pslot = psstrm->SlotAt(islot);
if (pslot->IsFinalLineBreak(chwLB))
break;
Assert(!pslot->IsInitialLineBreak(chwLB));
if (pslot->PassModified() != ipass)
continue;
vichwAssocs.clear();
pslot->AllAssocs(vichwAssocs);
for (size_t iichw = 0; iichw < vichwAssocs.size(); ++iichw)
pseg->MarkSlotInNextSeg(vichwAssocs[iichw], islot);
}
}
}
}
/*----------------------------------------------------------------------------------------------
Initialize all the streams. In particular, process the information about the
skip-offsets to handle segments other than the first.
@param cbPrev - number of bytes in the pbPrevSegDat buffer
@param pbPrevSegDat - buffer in the following format (all are 1 byte, unsigned):
prev seg's end breakweight
prev seg's previous strong directionality code
prev seg's previous terminator dir code
restart backup
skip offsets for each pass
This format must match what is generated by InitializeForNextSeg.
----------------------------------------------------------------------------------------------*/
void EngineState::InitializeStreams(GrTableManager * ptman,
GrCharStream * pchstrm,
int cbPrev, byte * pbPrevSegDat, bool fNeedFinalBreak, int * pislotFinalBreak)
{
int cpassLB = ptman->NumberOfLbPasses();
int ipassPos1 = ptman->FirstPosPass();
CreateSlotStreams();
int cslotBackup = 0;
InitForNewSegment(ptman);
if (cbPrev == 0)
{
m_lbPrevEnd = klbNoBreak;
m_dircInitialStrongDir = kdircNeutral;
m_dircInitialTermDir = kdircNeutral;
for (int ipass = 0; ipass < m_cpass; ++ipass)
{
OutputStream(ipass)->Initialize(ipassPos1, false);
m_prgzpst[ipass].SetResyncSkip(0);
if (ptman->LoggingTransduction())
m_prgzpst[ipass].InitializeLogInfo();
}
}
else
{
Assert(cbPrev >= 4);
byte * pb = pbPrevSegDat;
m_lbPrevEnd = LineBrk(*pb++);
// Directionality codes from the previous segment.
m_dircInitialStrongDir = DirCode((int)*pb++);
m_dircInitialTermDir = DirCode((int)*pb++);
cslotBackup = (int)*pb++;
Assert((cbPrev == 4 || cslotBackup == 0) || cbPrev == m_cpass + 4);
for (int ipass = 0; ipass < m_cpass; ++ipass)
{
OutputStream(ipass)->Initialize(ipassPos1, true);
if (cbPrev == 4)
m_prgzpst[ipass].SetResyncSkip(0); // different writing system
else
m_prgzpst[ipass].SetResyncSkip(*pb++);
if (ptman->LoggingTransduction())
m_prgzpst[ipass].InitializeLogInfo();
}
pchstrm->Backup(cslotBackup);
}
if (cbPrev > 0 || pchstrm->StartLine())
{
// Go ahead and run the initial line-break passes (if any), and then insert
// a line-break at the appropriate place where the new segment will begin.
int cslotToGet = cslotBackup;
LGetMore:
ptman->Pass(0)->ExtendGlyphIDOutput(ptman, pchstrm, OutputStream(0),
-1, 0, klbWordBreak,
cslotToGet, fNeedFinalBreak, m_twsh, pislotFinalBreak);
OutputStream(0)->SetSegMin(cslotBackup);
for (int ipass = 1; ipass <= cpassLB; ipass++)
{
if (cslotToGet > 0)
{
int nRet = kNextPass;
int cslotGot;
ptman->Pass(ipass)->ExtendOutput(ptman,
InputStream(ipass), OutputStream(ipass),
cslotToGet, m_twsh, &nRet, &cslotGot, pislotFinalBreak);
if (nRet != kNextPass)
{
cslotToGet = 1;
goto LGetMore;
}
}
OutputStream(ipass)->SetSegMin(cslotBackup);
}
Assert(OutputStream(cpassLB)->WritePos() >= cslotBackup);
m_cslotPreSeg = cslotBackup;
if (pchstrm->StartLine())
{
OutputStream(cpassLB)->AppendLineBreak(ptman, pchstrm, m_lbPrevEnd,
(ptman->RightToLeft() ? kdircRlb : kdircLlb), cslotBackup, true, -1);
SetInitialLB();
m_cslotPreSeg++;
if (cpassLB > 0 && *pislotFinalBreak > -1)
// See corresponding kludge in GrPass::ExtendGlyphIDOutput.
*pislotFinalBreak += 1;
}
else
{
OutputStream(cpassLB)->SetSegMin(cslotBackup);
}
OutputStream(cpassLB)->CalcIndexOffset(ptman);
}
else
{
Assert(cslotBackup == 0);
OutputStream(0)->SetSegMin(0);
m_cslotPreSeg = 0;
}
}
/*----------------------------------------------------------------------------------------------
Reinitialize as we start to generate a new segment.
----------------------------------------------------------------------------------------------*/
void EngineState::InitForNewSegment(GrTableManager * ptman)
{
// Set up a fresh batch of slots.
DestroySlotBlocks();
m_islotblkCurr = -1;
m_islotNext = kSlotBlockSize;
m_fInitialLB = false;
m_fFinalLB = false;
m_fInsertedLB = false;
m_fExceededSpace = false;
m_fHitHardBreak = false;
m_fRemovedWhtsp = false;
InitPosCache();
//////m_pgg = pgg;
for (int ipass = 0; ipass < m_cpass; ipass++)
m_prgzpst[ipass].InitForNewSegment(ipass, ptman->Pass(ipass)->MaxRuleContext());
}
/*----------------------------------------------------------------------------------------------
Calculate the position in stream zero of the given slot in the final stream,
based on the associations. (This will be the same as the position at the end of the
line-break passes, which is really what we want.)
@param islotFinal - index of the final slot in the final stream
----------------------------------------------------------------------------------------------*/
int EngineState::TraceStreamZeroPos(int islotFinal, int nTopDir)
{
GrSlotStream * psstrmFinal = OutputStream(m_cpass - 1);
if (psstrmFinal->WritePos() == 0)
return -1;
GrSlotState * pslot = psstrmFinal->SlotAt(islotFinal);
// If we're in the middle of reordered bidi stuff, just use the end of the stream.
if (pslot->DirLevel() > nTopDir)
return -1;
int islot = pslot->BeforeAssoc();
if (islot == kPosInfinity || islot < 0)
return -1;
return islot + m_cslotPreSeg;
}
/*----------------------------------------------------------------------------------------------
Return the next higher (more relaxed, less desirable) line break weight.
----------------------------------------------------------------------------------------------*/
LineBrk GrTableManager::IncLineBreak(LineBrk lb)
{
Assert(lb != klbClipBreak);
return (LineBrk)((int)lb + 1);
}
/*----------------------------------------------------------------------------------------------
Return a pointer to a slot that is near the current position in the given stream.
Return NULL if no slots have been output at all.
This is used for initializing features of line-break slots.
@param ipassArg - pass at which to start looking
@param islot - slot to look for; -1 if considering the current position
----------------------------------------------------------------------------------------------*/
GrSlotState * EngineState::AnAdjacentSlot(int ipassArg, int islot)
{
int ipass = ipassArg;
while (ipass >= 0)
{
GrSlotStream * psstrm = OutputStream(ipass);
if (psstrm->NumberOfSlots() > 0)
{
if (islot == -1)
{
if (psstrm->AtEnd())
return psstrm->SlotAt(psstrm->WritePos() - 1);
else
return psstrm->Peek();
}
else
{
if (psstrm->WritePos() <= islot)
return psstrm->SlotAt(psstrm->WritePos() - 1);
else
return psstrm->SlotAt(islot);
}
}
ipass--;
}
return NULL;
}
/*----------------------------------------------------------------------------------------------
Simple access methods.
----------------------------------------------------------------------------------------------*/
GrEngine * GrTableManager::Engine()
{
return m_pgreng;
}
EngineState * GrTableManager::State()
{
return &m_engst;
}
GrEngine * EngineState::Engine()
{
return m_ptman->Engine();
}
GrTableManager * EngineState::TableManager()
{
return m_ptman;
}
/*----------------------------------------------------------------------------------------------
Memory management for slots
----------------------------------------------------------------------------------------------*/
// standard for pass 0 slots
void EngineState::NewSlot(
gid16 gID, GrFeatureValues fval, int ipass, int ichwSegOffset, int nUnicode,
GrSlotState ** ppslotRet)
{
NextSlot(ppslotRet);
(*ppslotRet)->Initialize(gID, Engine(), fval, ipass, ichwSegOffset, nUnicode);
}
// line-break slots
void EngineState::NewSlot(
gid16 gID, GrSlotState * pslotFeat, int ipass, int ichwSegOffset,
GrSlotState ** ppslotRet)
{
NextSlot(ppslotRet);
(*ppslotRet)->Initialize(gID, Engine(), pslotFeat, ipass, ichwSegOffset);
}
// for inserting new slots after pass 0 (under-pos and unicode are irrelevant)
void EngineState::NewSlot(
gid16 gID, GrSlotState * pslotFeat, int ipass,
GrSlotState ** ppslotRet)
{
NextSlot(ppslotRet);
(*ppslotRet)->Initialize(gID, Engine(), pslotFeat, ipass);
}
// for making a new version of the slot initialized from the old
void EngineState::NewSlotCopy(GrSlotState * pslotCopyFrom, int ipass,
GrSlotState ** ppslotRet)
{
NextSlot(ppslotRet);
(*ppslotRet)->InitializeFrom(pslotCopyFrom, ipass);
}
void EngineState::NextSlot(GrSlotState ** ppslotRet)
{
int cnExtraPerSlot = m_cUserDefn + (m_cCompPerLig * 2) + m_cFeat;
if (m_islotNext >= kSlotBlockSize)
{
// Allocate a new block of slots
GrSlotState * slotblkNew = new GrSlotState[kSlotBlockSize];
u_intslot * prgnSlotBuf = new u_intslot[kSlotBlockSize * cnExtraPerSlot];
m_vslotblk.push_back(slotblkNew);
m_vprgnSlotVarLenBufs.push_back(prgnSlotBuf);
m_islotblkCurr++;
m_islotNext = 0;
Assert((unsigned)m_islotblkCurr == m_vslotblk.size()-1);
}
*ppslotRet = m_vslotblk[m_islotblkCurr] + m_islotNext;
(*ppslotRet)->BasicInitialize(m_cUserDefn, m_cCompPerLig, m_cFeat,
(m_vprgnSlotVarLenBufs[m_islotblkCurr] + (m_islotNext * cnExtraPerSlot)));
m_islotNext++;
}
/*----------------------------------------------------------------------------------------------
Convert the number of font design units into source device logical units.
----------------------------------------------------------------------------------------------*/
float GrTableManager::EmToLogUnits(int m)
{
return m_engst.EmToLogUnits(m);
}
float EngineState::EmToLogUnits(int m)
{
if (m == 0)
return 0;
float xysFontEmSquare;
m_pfont->getFontMetrics(NULL, NULL, &xysFontEmSquare);
// mEmUnits should be equal to the design units in the font's em square
int mEmUnits = Engine()->GetFontEmUnits();
if (mEmUnits <= 0)
{
Warn("Failed to obtain font em-units");
return (float)m;
}
return GrEngine::GrIFIMulDiv(m, xysFontEmSquare, mEmUnits);
}
/*----------------------------------------------------------------------------------------------
Convert the source device coordinates to font design units.
----------------------------------------------------------------------------------------------*/
int GrTableManager::LogToEmUnits(float xys)
{
return m_engst.LogToEmUnits(xys);
}
int EngineState::LogToEmUnits(float xys)
{
if (xys == 0)
return 0;
float xysFontEmSquare;
m_pfont->getFontMetrics(NULL, NULL, &xysFontEmSquare);
// mEmUnits should be equal to the design units in the font's em square
int mEmUnits = Engine()->GetFontEmUnits();
if (mEmUnits < 0)
{
Warn("Failed to obtain font em-units");
return (int)xys;
}
return GrEngine::GrFIFMulDiv(xys, mEmUnits, xysFontEmSquare);
}
/*----------------------------------------------------------------------------------------------
Find the coordinates for a given glyph point.
Return true on success, false otherwise.
----------------------------------------------------------------------------------------------*/
bool GrTableManager::GPointToXY(gid16 chwGlyphID, int nGPoint, float * pxs, float * pys)
{
return m_engst.GPointToXY(chwGlyphID, nGPoint, pxs, pys);
}
bool EngineState::GPointToXY(gid16 chwGlyphID, int nGPoint, float * pxs, float * pys)
{
*pxs = INT_MIN;
*pys = INT_MIN;
Point pointRet;
m_pfont->getGlyphPoint(chwGlyphID, nGPoint, pointRet);
*pxs = pointRet.x;
*pys = pointRet.y;
return true;
}
/*----------------------------------------------------------------------------------------------
Call the justification routines.
----------------------------------------------------------------------------------------------*/
void GrTableManager::CallJustifier(IGrJustifier * pgjus, int ipassCurr,
float dxUnjustified, float dxJustified, bool fEndLine)
{
// Indicates the stream the justification routines will read and modify:
m_engst.m_ipassJustCalled = ipassCurr;
int iGlyphMin = OutputStream(ipassCurr)->SegMin();
int iGlyphLim = OutputStream(ipassCurr)->SegLimIfKnown();
if (iGlyphLim == -1)
iGlyphLim = OutputStream(ipassCurr)->WritePos();
GrSlotStream * psstrm = OutputStream(ipassCurr);
if (m_pgreng->BasicJustification() && fEndLine)
UnstretchTrailingWs(psstrm, iGlyphLim);
pgjus->adjustGlyphWidths(m_pgreng, iGlyphMin, iGlyphLim, dxUnjustified, dxJustified);
// After returning, reading and modifying slot attributes is not permitted:
m_engst.m_ipassJustCalled = -1;
}
/*----------------------------------------------------------------------------------------------
For the basic justification approach, remove stretch from the trailing whitespace.
----------------------------------------------------------------------------------------------*/
void GrTableManager::UnstretchTrailingWs(GrSlotStream * psstrm, int iGlyphLim)
{
int islot = iGlyphLim - 1;
while (islot >= 0)
{
GrSlotState * pslot = psstrm->SlotAt(islot);
if (pslot->IsLineBreak(LBGlyphID()))
{ } // keep looking for the trailing space
else if (pslot->IsSpace(this))
pslot->SetJStretch(0); // TODO: do this at level 0
else
// something other than space--quit
return;
islot--;
}
}
/*----------------------------------------------------------------------------------------------
Determine how much shrink is permissible.
This method is really used only for testing the shrink mechanism, since at this time
we don't allow low-end justification to do any shrinking.
----------------------------------------------------------------------------------------------*/
void GrTableManager::DetermineShrink(IGrJustifier * pgjus, int ipassCurr)
{
if (m_engst.m_dxsShrinkPossible != GrSlotState::kNotYetSet)
return;
if (m_engst.m_jmodi != kjmodiCanShrink || pgjus == NULL)
{
m_engst.m_dxsShrinkPossible = 0;
return;
}
if (ipassCurr != m_ipassJust1 - 1)
return;
GrSlotStream * psstrm = OutputStream(ipassCurr);
if (!psstrm->FullyWritten())
return;
// Normally just answer 0. To test shrinking, turn on the code below.
m_engst.m_dxsShrinkPossible = 0;
#if TEST_SHRINK
int mTotal = 0;
int mShrink = 0;
int islotLim = psstrm->SegLimIfKnown();
if (islotLim == -1)
islotLim = psstrm->WritePos();
for ( ; islotLim >= 0; islotLim--)
{
// Trailing white space is not shrinkable.
GrSlotState * pslot = psstrm->SlotAt(islotLim - 1);
if (pslot->IsLineBreak(LBGlyphID()))
continue;
if (!pslot->IsSpace(this))
break;
}
int islot;
for (islot = psstrm->SegMin(); islot < islotLim; islot++)
{
GrSlotState * pslot = psstrm->SlotAt(islot);
if (pslot->IsLineBreak(LBGlyphID()))
continue;
mTotal += pslot->AdvanceX(this);
mShrink += pslot->JShrink();
}
// Say that we are allowed to shrink up to 10% of the width of the segment.
mShrink = min(mShrink, mTotal/10);
m_engst.m_dxsShrinkPossible = EmToLogUnits(mShrink);
#endif // TEST_SHRINK
}
/*----------------------------------------------------------------------------------------------
Return the attribute of the given slot, back to the justification routine. Return
logical units.
----------------------------------------------------------------------------------------------*/
GrResult EngineState::GetGlyphAttrForJustification(int iGlyph, int jgat, int nLevel,
float * pValueRet)
{
GrResult res;
int valueRetInt = 0;
switch(jgat)
{
case kjgatWeight:
case kjgatStretchInSteps:
case kjgatBreak:
res = GetGlyphAttrForJustification(iGlyph, jgat, nLevel, &valueRetInt);
*pValueRet = (float)valueRetInt;
return res;
default:
break;
}
if (m_ipassJustCalled == -1)
return kresUnexpected;
if (nLevel != 1)
return kresInvalidArg;
GrSlotStream * psstrm = OutputStream(m_ipassJustCalled);
if (iGlyph < -1 || iGlyph >= psstrm->WritePos())
return kresInvalidArg;
GrSlotState * pslot = psstrm->SlotAt(iGlyph);
// bool fConvertUnits = true;
switch(jgat)
{
case kjgatStretch:
*pValueRet = EmToLogUnits(pslot->JStretch());
break;
case kjgatShrink:
*pValueRet = EmToLogUnits(pslot->JShrink());
break;
case kjgatStep:
*pValueRet = EmToLogUnits(pslot->JStep());
break;
default:
return kresNotImpl;
}
return kresOk;
}
GrResult EngineState::GetGlyphAttrForJustification(int iGlyph, int jgat, int nLevel,
int * pValueRet)
{
GrResult res;
float valueRetFloat;
switch(jgat)
{
case kjgatStretch:
case kjgatShrink:
case kjgatStep:
res = GetGlyphAttrForJustification(iGlyph, jgat, nLevel, &valueRetFloat);
*pValueRet = GrEngine::RoundFloat(valueRetFloat);
return res;
default:
break;
}
if (m_ipassJustCalled == -1)
return kresUnexpected;
if (nLevel != 1)
return kresInvalidArg;
GrSlotStream * psstrm = OutputStream(m_ipassJustCalled);
if (iGlyph < -1 || iGlyph >= psstrm->WritePos())
return kresInvalidArg;
GrSlotState * pslot = psstrm->SlotAt(iGlyph);
int mStretch, mStep;
//float xsStretch, xsStep;
switch(jgat)
{
case kjgatWeight:
*pValueRet = pslot->JWeight();
break;
case kjgatStretchInSteps:
// ???? should this be calculated in terms of logical units??
// Probably doesn't matter, since the logical units are floating point, so the
// level of precision is about the same.
mStretch = pslot->JStretch();
mStep = pslot->JStep();
if (mStep == 0)
return kresUnexpected;
*pValueRet = int(mStretch / mStep); // round down
break;
//case kjgatStretchInSteps:
// xsStretch = EmToLogUnits(pslot->JStretch());
// xsStep = EmToLogUnits(pslot->JStep());
// if (xsStep == 0)
// return kresUnexpected;
// *pValueRet = int(xsStretch / xsStep); // round down
// break;
case kjgatBreak:
*pValueRet = pslot->BreakWeight();
break;
default:
return kresNotImpl;
}
return kresOk;
}
/*----------------------------------------------------------------------------------------------
Set the attribute of the given slot, for the justification routine.
----------------------------------------------------------------------------------------------*/
GrResult EngineState::SetGlyphAttrForJustification(int iGlyph, int jgat, int nLevel,
float value)
{
GrResult res;
int valueInt;
switch(jgat)
{
case kjgatWeight:
case kjgatStretchInSteps:
case kjgatBreak:
valueInt = (int)value;
res = SetGlyphAttrForJustification(iGlyph, jgat, nLevel, valueInt);
return res;
default:
break;
}
if (m_ipassJustCalled == -1)
return kresUnexpected;
if (nLevel != 1)
return kresInvalidArg;
GrSlotStream * psstrm = OutputStream(m_ipassJustCalled);
if (iGlyph < -1 || iGlyph >= psstrm->WritePos())
return kresInvalidArg;
GrSlotState * pslot = psstrm->SlotAt(iGlyph);
int mValue = LogToEmUnits(value);
mValue = min(mValue, 0xFFFF); // truncate to an unsigned short
// Review: do we really want to allow them to set stretch/shrink/step/weight? The normal
// thing for them to do is set width.
switch(jgat)
{
case kjgatStretch:
pslot->SetJStretch(mValue);
break;
case kjgatShrink:
pslot->SetJShrink(mValue);
break;
case kjgatStep:
pslot->SetJStep(mValue);
break;
case kjgatWidth:
pslot->SetJWidth(mValue);
break;
default:
return kresNotImpl;
}
return kresOk;
}
GrResult EngineState::SetGlyphAttrForJustification(int iGlyph, int jgat, int nLevel,
int value)
{
GrResult res;
float valueFloat;
switch(jgat)
{
case kjgatStretch:
case kjgatShrink:
case kjgatStep:
case kjgatWidth:
valueFloat = (float)value;
res = SetGlyphAttrForJustification(iGlyph, jgat, nLevel, valueFloat);
return res;
default:
break;
}
if (m_ipassJustCalled == -1)
return kresUnexpected;
if (nLevel != 1)
return kresInvalidArg;
GrSlotStream * psstrm = OutputStream(m_ipassJustCalled);
if (iGlyph < -1 || iGlyph >= psstrm->WritePos())
return kresInvalidArg;
GrSlotState * pslot = psstrm->SlotAt(iGlyph);
int mValue, cSteps;
switch(jgat)
{
case kjgatWeight:
pslot->SetJWeight(value);
break;
case kjgatWidthInSteps:
cSteps = value;
mValue = pslot->JStep();
if (mValue == 0)
return kresUnexpected;
pslot->SetJWidth(mValue * cSteps);
break;
default:
return kresNotImpl;
}
return kresOk;
}
//:>--------------------------------------------------------------------------------------------
//:> Functions to forward to the engine.
//:>------------------------------------------------------------------------------------------*/
gid16 GrTableManager::GetGlyphIDFromUnicode(int nUnicode)
{
return m_pgreng->GetGlyphIDFromUnicode(nUnicode);
}
/*--------------------------------------------------------------------------------------------*/
gid16 GrTableManager::ActualGlyphForOutput(utf16 chw)
{
return m_pgreng->ActualGlyphForOutput(chw);
}
/*--------------------------------------------------------------------------------------------*/
GrGlyphTable * GrTableManager::GlyphTable()
{
return m_pgreng->GlyphTable();
}
/*--------------------------------------------------------------------------------------------*/
gid16 GrTableManager::LBGlyphID()
{
return m_pgreng->LBGlyphID();
}
/*--------------------------------------------------------------------------------------------*/
gid16 GrTableManager::GetClassGlyphIDAt(int nClass, int nIndex)
{
return m_pgreng->GetClassGlyphIDAt(nClass, nIndex);
}
/*--------------------------------------------------------------------------------------------*/
int GrTableManager::GetIndexInGlyphClass(int nClass, gid16 chwGlyphID)
{
return m_pgreng->GetIndexInGlyphClass(nClass, chwGlyphID);
}
/*--------------------------------------------------------------------------------------------*/
size_t GrTableManager::NumberOfGlyphsInClass(int nClass)
{
return m_pgreng->NumberOfGlyphsInClass(nClass);
}
/*--------------------------------------------------------------------------------------------*/
void GrTableManager::SetSlotAttrsFromGlyphAttrs(GrSlotState * pslot)
{
m_pgreng->SetSlotAttrsFromGlyphAttrs(pslot);
}
/*--------------------------------------------------------------------------------------------*/
int GrTableManager::NumFeat()
{
return m_pgreng->NumFeat();
}
/*--------------------------------------------------------------------------------------------*/
int GrTableManager::DefaultForFeatureAt(int ifeat)
{
return m_pgreng->DefaultForFeatureAt(ifeat);
}
/*--------------------------------------------------------------------------------------------*/
void GrTableManager::DefaultsForLanguage(isocode lgcode,
std::vector<featid> & vnFeats, std::vector<int> & vnValues)
{
return m_pgreng->DefaultsForLanguage(lgcode, vnFeats, vnValues);
}
/*--------------------------------------------------------------------------------------------*/
GrFeature * GrTableManager::FeatureWithID(featid nID, int * pifeat)
{
return m_pgreng->FeatureWithID(nID, pifeat);
}
/*--------------------------------------------------------------------------------------------*/
GrFeature * GrTableManager::Feature(int ifeat)
{
return m_pgreng->Feature(ifeat);
}
/*--------------------------------------------------------------------------------------------*/
bool GrTableManager::RightToLeft()
{
if (m_engst.WhiteSpaceOnly())
return m_engst.m_fParaRtl;
else
return m_pgreng->RightToLeft();
}
/*--------------------------------------------------------------------------------------------*/
int GrTableManager::TopDirectionLevel()
{
return m_pgreng->TopDirectionLevel();
}
/*--------------------------------------------------------------------------------------------*/
float GrTableManager::VerticalOffset()
{
return m_pgreng->VerticalOffset();
}
/*--------------------------------------------------------------------------------------------*/
int GrTableManager::NumUserDefn()
{
return m_pgreng->NumUserDefn();
}
/*--------------------------------------------------------------------------------------------*/
int GrTableManager::NumCompPerLig()
{
return m_pgreng->NumCompPerLig();
}
/*--------------------------------------------------------------------------------------------*/
int GrTableManager::ComponentIndexForGlyph(gid16 chwGlyphID, int nCompID)
{
return m_pgreng->ComponentIndexForGlyph(chwGlyphID, nCompID);
}
/*--------------------------------------------------------------------------------------------*/
int GrTableManager::GlyphAttrValue(gid16 chwGlyphID, int nAttrID)
{
return m_pgreng->GlyphAttrValue(chwGlyphID, nAttrID);
}
/*--------------------------------------------------------------------------------------------*/
bool GrTableManager::LoggingTransduction()
{
return m_fLogging;
//return m_pgreng->LoggingTransduction();
}
/*----------------------------------------------------------------------------------------------
Calculate the positions of the glyphs up to the given slot, within the output of the given
pass. If the slot is NULL, go all the way to the end of what has been generated.
@param ipass - index of pass needing positioning; normally this will be the
final pass, but it could be another if positions are
requested by the rules themselves
@param pslotLast - last slot that needs to be positioned, or NULL
@param pxsWidth - return the total width used so far
@param psxVisibleWidth - return the visible width so far
MOVE to EngineState
----------------------------------------------------------------------------------------------*/
void GrTableManager::CalcPositionsUpTo(int ipass, GrSlotState * pslotLast,
float * pxsWidth, float * pxsVisibleWidth)
{
Assert(ipass >= m_ipassPos1 - 1);
int isstrm = ipass;
GrSlotStream * psstrm = OutputStream(isstrm);
Assert(psstrm->GotIndexOffset());
if (psstrm->WritePos() <= psstrm->IndexOffset())
{
Assert(psstrm->WritePos() == 0);
*pxsWidth = 0;
*pxsVisibleWidth = 0;
return;
}
if (!pslotLast)
{
pslotLast = psstrm->SlotAt(psstrm->WritePos() - 1);
}
Assert(pslotLast);
// First set positions of all base slots.
int islot = psstrm->IndexOffset();
GrSlotState * pslot;
float xs = 0;
float ys = VerticalOffset();
*pxsWidth = 0;
*pxsVisibleWidth = 0;
bool fLast = false;
bool fLastBase = false;
bool fFakeItalic = m_pgreng->FakeItalic();
float fakeItalicRatio = 0;
if (fFakeItalic)
fakeItalicRatio = State()->GetFont()->fakeItalicRatio();
bool fBasicJust = m_pgreng->BasicJustification();
// Figure out how to know when to stop.
// We need to calculate up to the base of the last leaf slot, if it happens
// to be later in the stream than the last actual slot passed in.
if (!psstrm->HasSlotAtPosPassIndex(pslotLast->AttachRootPosPassIndex()))
return;
GrSlotState * pslotLastBase = pslotLast->Base(psstrm);
if (ipass == m_cpass - 1 && m_engst.m_islotPosNext > -1)
{
// For final pass, initialize from cache of previous calculations.
islot = m_engst.m_islotPosNext;
xs = m_engst.m_xsPosXNext;
ys = m_engst.m_ysPosYNext;
*pxsWidth = m_engst.m_dxsTotWidthSoFar;
*pxsVisibleWidth = m_engst.m_dxsVisWidthSoFar;
if (psstrm->SlotsPresent() <= islot)
return;
if (pslotLastBase->PosPassIndex() == GrSlotAbstract::kNotYetSet)
{
Assert(false); // I think this case is handled above
return; // can't position this slot yet; its base has not been processed
}
if (pslotLastBase->PosPassIndex() + psstrm->IndexOffset() < islot)
{
fLastBase = true;
if (pslotLast == pslotLastBase)
fLast = true;
}
}
std::vector<GrSlotState *> vpslotAttached;
bool fRtl = RightToLeft();
while (!fLast || !fLastBase)
{
Assert(islot < psstrm->SlotsPresent());
pslot = (isstrm == ipass) ? psstrm->SlotAt(islot) : psstrm->OutputSlotAt(islot);
if (!pslot->IsBase())
{
// This slot is attached to another; it will be positioned strictly
// relative to that one. This happens in the loop below.
vpslotAttached.push_back(pslot);
}
else
{
// Base character.
bool fLB = pslot->IsLineBreak(LBGlyphID());
if (InternalJustificationMode() == kjmodiJustify && fBasicJust
&& ipass == m_cpass - 1 && !fLB)
{
m_engst.AddJWidthToAdvance(psstrm, &pslot, islot, &pslotLast, &pslotLastBase);
}
// Make sure the metrics are the complete ones.
pslot->CalcCompositeMetrics(this, psstrm, kPosInfinity, true);
float xsInc = pslot->GlyphXOffset(psstrm, fakeItalicRatio);
float ysInc = pslot->GlyphYOffset(psstrm);
float xsAdvX = pslot->ClusterAdvWidth(psstrm);
float ysAdvY = (fLB) ?
0 :
EmToLogUnits(pslot->AdvanceY(this));
if (fRtl)
{
xs -= xsAdvX;
ys -= ysAdvY;
pslot->SetXPos(xs + xsInc);
pslot->SetYPos(ys + ysInc);
}
else
{
pslot->SetXPos(xs + xsInc);
pslot->SetYPos(ys + ysInc);
xs += xsAdvX;
ys += ysAdvY;
}
*pxsWidth = max(*pxsWidth, fabsf(xs));
if (!IsWhiteSpace(pslot))
*pxsVisibleWidth = *pxsWidth;
if (isstrm == m_cpass - 1)
{
// For the final output pass, cache the results of the calculation so far.
// Only do this for slots that have actually been processed by the final
// pass, because others may have intermediate values that may be changed
// later.
m_engst.m_islotPosNext = pslot->PosPassIndex() + psstrm->IndexOffset() + 1;
m_engst.m_xsPosXNext = xs;
m_engst.m_ysPosYNext = ys;
m_engst.m_dxsTotWidthSoFar = *pxsWidth;
m_engst.m_dxsVisWidthSoFar = *pxsVisibleWidth;
}
}
// Need to have calculated both the last leaf and the last base (if they happen to
// be different); if so, we're done.
if (pslot == pslotLast)
fLast = true;
if (pslot == pslotLastBase)
fLastBase = true;
islot++;
}
Assert(fLast);
Assert(fLastBase);
// Now set positions of non-base slots, relative to their bases.
for (size_t ipslot = 0; ipslot < vpslotAttached.size(); ipslot++)
{
GrSlotState * pslot = vpslotAttached[ipslot];
GrSlotState * pslotBase = pslot->Base(psstrm);
if (pslotBase->XPosition() == kNegInfinity || pslotBase->YPosition() == kNegInfinity)
{
Assert(false);
continue;
}
float xsCluster = pslotBase->XPosition() - pslotBase->GlyphXOffset(psstrm, fakeItalicRatio);
float ysCluster = pslotBase->YPosition() - pslotBase->GlyphYOffset(psstrm);
float xsInc = pslot->GlyphXOffset(psstrm, fakeItalicRatio);
float ysInc = pslot->GlyphYOffset(psstrm);
pslot->SetXPos(xsCluster + xsInc);
pslot->SetYPos(ysCluster + ysInc);
// My theory is that we don't need to adjust *pxsWidth here, because the width of
// any non-base slots should be factored into the advance width of their cluster
// base, which was handled above.
}
}
/*----------------------------------------------------------------------------------------------
Return true if the given slot is white space.
----------------------------------------------------------------------------------------------*/
bool GrTableManager::IsWhiteSpace(GrSlotState * pslot)
{
if (pslot->IsLineBreak(LBGlyphID()))
return true;
return pslot->IsSpace(this);
}
/*----------------------------------------------------------------------------------------------
When doing basic justification, apply the value of justify.width to the advance width.
This happens during the final positioning routine.
----------------------------------------------------------------------------------------------*/
void EngineState::AddJWidthToAdvance(GrSlotStream * psstrm, GrSlotState ** ppslot, int islot,
GrSlotState ** ppslotLast, GrSlotState ** ppslotLastBase)
{
Assert(psstrm->OutputOfPass() == m_cpass - 1); // only used for final stream
if ((*ppslot)->JWidth() > 0)
{
if ((*ppslot)->PassModified() != m_cpass - 1)
{
// Replace the slot with a new slot, so it gets marked properly with the
// pass in which it was modified. (Otherwise the debug log gets screwed up.)
GrSlotState * pslotNew;
NewSlotCopy((*ppslot), m_cpass - 1, &pslotNew);
psstrm->PutSlotAt(pslotNew, islot);
if (*ppslot == *ppslotLast)
*ppslotLast = pslotNew;
if (*ppslot == *ppslotLastBase)
*ppslotLastBase = pslotNew;
*ppslot = pslotNew;
}
(*ppslot)->AddJWidthToAdvance(TableManager());
}
}
//:>********************************************************************************************
//:> Debuggers and utilities
//:>********************************************************************************************
//:Ignore
#if 0
void GrTableManager::TempDebug()
{
for (int ipass = 0; ipass < m_cpass; ++ipass)
{
int wpos = OutputStream(ipass)->WritePos();
int rpos = OutputStream(ipass)->ReadPos();
GrSlotState * wslot = OutputStream(ipass)->LastSlotWritten();
GrSlotState * rslot = OutputStream(ipass)->LastSlotRead();
}
}
#endif
/*----------------------------------------------------------------------------------------------
Given a character index in the original string, locate the corresponding line-break slot
in the final surface stream. The character index must be one before which there is a
line-break (seg lim), or the beginning or the end of the string.
TODO SharonC: fix the bugs
----------------------------------------------------------------------------------------------*/
int GrTableManager::SurfaceLineBreakSlot(int ichw, GrCharStream * pchstrm, bool fInitial)
{
if (ichw == 0)
{
Assert(fInitial);
return -1; // no initial line break
}
if (ichw == pchstrm->Lim())
{
Assert(!fInitial);
return -1; // no terminating line break
}
int islotIn = pchstrm->SegOffset(ichw);
Assert(islotIn >= 0);
islotIn += (fInitial? m_engst.m_cslotPreSeg - 1: m_engst.m_cslotPreSeg);
gid16 chwLBGlyphID = LBGlyphID();
// Now islotIn is the position in stream 0;
for (int ipass = 1; ipass < m_cpass; ++ipass)
{
int islotChunkMin;
int islotChunkLim;
GrSlotStream * psstrmOut = OutputStream(ipass);
GrSlotStream * psstrmIn = InputStream(ipass);
if (fInitial)
islotIn = max(islotIn, psstrmIn->PreChunk());
int islotT = psstrmIn->ChunkInNextMin(islotIn);
islotChunkMin = psstrmIn->ChunkInNext(islotT);
islotChunkMin = (islotChunkMin == -1)? 0: islotChunkMin;
islotT = psstrmIn->ChunkInNextLim(islotIn);
Assert(islotT <= psstrmIn->ReadPos());
if (islotT == psstrmIn->ReadPos())
islotChunkLim = psstrmOut->WritePos();
else
islotChunkLim = psstrmIn->ChunkInNext(islotT);
int islotOut;
for (islotOut = islotChunkMin; islotOut < islotChunkLim; ++islotOut)
{
gid16 chw = psstrmOut->GlyphIDAt(islotOut);
if (chw == chwLBGlyphID)
break;
}
Assert(psstrmOut->GlyphIDAt(islotOut) == chwLBGlyphID);
islotIn = islotOut;
}
return islotIn;
}
/*----------------------------------------------------------------------------------------------
Generate a debugger string showing the chunk boundaries for the given stream.
----------------------------------------------------------------------------------------------*/
std::wstring GrTableManager::ChunkDebugString(int ipass)
{
GrSlotStream * psstrm = OutputStream(ipass);
std::wstring stuRet;
//utf16 chwN = '\\';
//utf16 chwR = '^';
gid16 chwLBGlyphID = LBGlyphID();
for (int islot = 0; islot < psstrm->WritePos(); islot++)
{
if (psstrm->ChunkInPrev(islot) != -1)
{
if (psstrm->ChunkInNext(islot) != -1)
stuRet.append(L">");
else
stuRet.append(L"\\");
}
else if (psstrm->ChunkInNext(islot) != -1)
stuRet.append(L"/");
else
stuRet.append(L" ");
// if (islot == psstrm->ReadPos())
// stuRet.Append(&chwR, 1);
wchar_t chw = psstrm->SlotAt(islot)->GlyphID();
if (chw == chwLBGlyphID)
stuRet.append(L"#");
else
stuRet.append(&chw, 1);
}
return stuRet;
}
} // namespace gr
//:End Ignore
|