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
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
|
/****************************************************************************
**
** ???
**
** Copyright (C) 2003-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the kernel module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file. Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/
#include "tqtglobaldefines.h"
#ifdef USE_QT4
#include <Qt/qdatastream.h>
#endif // USE_QT4
#ifdef USE_QT4
#else // USE_QT4
#include "tqfontengine_p.h"
#endif // USE_QT4
// #define FONTENGINE_DEBUG
#include <tqwidget.h>
#include <tqcstring.h>
#include <tqtextcodec.h>
#include "tqbitmap.h"
#include "tqfontdatabase.h"
#include "tqpaintdevice.h"
#include "tqpaintdevicemetrics.h"
#include "tqpainter.h"
#include "tqimage.h"
#include "tqt_x11_p.h"
#include "tqfont.h"
#include "tqtextengine_p.h"
#include <private/tqunicodetables_p.h>
#ifdef USE_QT4
#include "tqfontengine_p.h"
#endif // USE_QT4
#include <limits.h>
#ifdef USE_QT4
#else // USE_QT4
// defined in qfontdatbase_x11.cpp
extern int qt_mib_for_xlfd_encoding( const char *encoding );
extern int qt_xlfd_encoding_id( const char *encoding );
extern void qt_draw_transformed_rect( TQPainter *p, int x, int y, int w, int h, bool fill );
static void drawLines( TQPainter *p, TQFontEngine *fe, int baseline, int x1, int w, int textFlags )
{
int lw = fe->lineThickness();
if ( textFlags & TQt::Underline ) {
int pos = fe->underlinePosition();
qt_draw_transformed_rect( p, x1, baseline+pos, w, lw, TRUE );
}
if ( textFlags & TQt::Overline ) {
int pos = fe->ascent()+1;
if ( !pos ) pos = 1;
qt_draw_transformed_rect( p, x1, baseline-pos, w, lw, TRUE );
}
if ( textFlags & TQt::StrikeOut ) {
int pos = fe->ascent()/3;
if ( !pos ) pos = 1;
qt_draw_transformed_rect( p, x1, baseline-pos, w, lw, TRUE );
}
}
inline static void qSafeXDestroyImage( XImage *x )
{
if ( x->data ) {
free( x->data );
x->data = 0;
}
XDestroyImage( x );
}
extern bool qt_xForm_helper( const TQWMatrix &trueMat, int xoffset,
int type, int depth,
uchar *dptr, int dbpl, int p_inc, int dHeight,
uchar *sptr, int sbpl, int sWidth, int sHeight
);
static TQBitmap transform(Display *dpy, const TQBitmap &source, int xoff, int yoff, int w, int h, const TQWMatrix &matrix)
{
int ws = source.width();
int hs = source.height();
bool invertible;
TQWMatrix mat = matrix.invert( &invertible ); // invert matrix
if (!invertible )
return TQBitmap();
mat.translate(xoff, yoff);
XImage *xi = XGetImage(dpy, source.handle(), 0, 0, ws, hs, AllPlanes, XYPixmap);
if ( !xi )
return TQBitmap();
int sbpl = xi->bytes_per_line;
uchar *sptr = (uchar *)xi->data;
int dbpl = (w+7)/8;
int dbytes = dbpl*h;
uchar *dptr = (uchar *)malloc( dbytes ); // create buffer for bits
memset( dptr, 0, dbytes );
int type = xi->bitmap_bit_order == MSBFirst ? TQT_XFORM_TYPE_MSBFIRST : TQT_XFORM_TYPE_LSBFIRST;
int xbpl, p_inc;
xbpl = (w+7)/8;
p_inc = dbpl - xbpl;
bool ok = qt_xForm_helper( mat, xi->xoffset, type, 1, dptr, xbpl, p_inc, h, sptr, sbpl, ws, hs );
qSafeXDestroyImage(xi);
TQBitmap bm;
if (ok) {
bm = TQBitmap( w, h, dptr, TQImage::systemBitOrder() != TQImage::BigEndian );
} else {
#if defined(TQT_CHECK_RANGE)
qWarning( "TQFontEngineXft::tranform: xform failed");
#endif
}
free( dptr );
return bm;
}
static void drawScaled(int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags,
Display *dpy, GC gc, TQPaintDevice *pdev, TQFontEngine *fe,
const TQWMatrix &xmat, float scale)
{
// font doesn't support transformations, need to do it by hand
int w = tqRound(si->width/scale);
int h = tqRound((si->ascent + si->descent + 1)/scale);
if (w == 0 || h == 0)
return;
TQWMatrix mat1 = xmat;
mat1.scale(scale, scale);
w += h; // add some pixels to width because of italic correction
TQBitmap bm( w, h, TRUE ); // create bitmap
TQPainter paint;
paint.begin( &bm ); // draw text in bitmap
fe->draw( &paint, 0, si->ascent/scale, engine, si, textFlags );
paint.end();
TQRect pdevRect;
if (pdev->devType() == TQInternal::Widget)
pdevRect = ((TQWidget *)pdev)->rect();
else if (pdev->devType() == TQInternal::Pixmap)
pdevRect = ((TQPixmap *)pdev)->rect();
else
return;
TQRect br = mat1.mapRect(TQRect(x, y - si->ascent, w, h));
TQRect br2 = br & pdevRect;
if (br2.width() <= 0 || br2.height() <= 0
|| br2.width() >= 32768 || br2.height() >= 32768)
return;
TQWMatrix mat = TQT_TQWMATRIX_OBJECT(TQPixmap::trueMatrix( mat1, w, h ));
TQBitmap wx_bm = ::transform(dpy, bm, br2.x() - br.x(), br2.y() - br.y(), br2.width(), br2.height(), mat);
if ( wx_bm.isNull() )
return;
x = br2.x();
y = br2.y();
TQt::HANDLE hd = pdev->handle();
XSetFillStyle( dpy, gc, FillStippled );
XSetStipple( dpy, gc, wx_bm.handle() );
XSetTSOrigin( dpy, gc, x, y );
XFillRectangle( dpy, hd, gc, x, y, wx_bm.width(), wx_bm.height() );
XSetTSOrigin( dpy, gc, 0, 0 );
XSetFillStyle( dpy, gc, FillSolid );
}
TQFontEngine::~TQFontEngine()
{
}
int TQFontEngine::lineThickness() const
{
// ad hoc algorithm
int score = fontDef.weight * fontDef.pixelSize;
int lw = score / 700;
// looks better with thicker line for small pointsizes
if ( lw < 2 && score >= 1050 ) lw = 2;
if ( lw == 0 ) lw = 1;
return lw;
}
int TQFontEngine::underlinePosition() const
{
int pos = ( ( lineThickness() * 2 ) + 3 ) / 6;
return pos ? pos : 1;
}
// ------------------------------------------------------------------
// The box font engine
// ------------------------------------------------------------------
TQFontEngineBox::TQFontEngineBox( int size )
: _size( size )
{
cache_cost = sizeof( TQFontEngineBox );
}
TQFontEngineBox::~TQFontEngineBox()
{
}
TQFontEngine::Error TQFontEngineBox::stringToCMap( const TQChar *, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool ) const
{
if ( *nglyphs < len ) {
*nglyphs = len;
return OutOfMemory;
}
memset( glyphs, 0, len * sizeof( glyph_t ) );
*nglyphs = len;
if ( advances ) {
for ( int i = 0; i < len; i++ )
*(advances++) = _size;
}
return NoError;
}
void TQFontEngineBox::draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags )
{
Display *dpy = TQPaintDevice::x11AppDisplay();
TQt::HANDLE hd = p->tqdevice()->handle();
GC gc = p->gc;
#ifdef FONTENGINE_DEBUG
p->save();
p->setBrush( TQt::white );
glyph_metrics_t ci = boundingBox( glyphs, offsets, numGlyphs );
p->drawRect( x + ci.x, y + ci.y, ci.width, ci.height );
p->drawRect( x + ci.x, y + 50 + ci.y, ci.width, ci.height );
qDebug("bounding rect=%d %d (%d/%d)", ci.x, ci.y, ci.width, ci.height );
p->restore();
int xp = x;
int yp = y;
#endif
GlyphAttributes *glyphAttributes = engine->glyphAttributes( si );
if ( p->txop > TQPainter::TxTranslate ) {
int xp = x;
int yp = _size + 2;
int s = _size - 3;
for (int k = 0; k < si->num_glyphs; k++) {
if (!glyphAttributes[k].zeroWidth)
qt_draw_transformed_rect( p, xp, yp, s, s, FALSE );
xp += _size;
}
} else {
if ( p->txop == TQPainter::TxTranslate )
p->map( x, y, &x, &y );
XRectangle rects[64];
int gl = 0;
while (gl < si->num_glyphs) {
int toDraw = TQMIN(64, si->num_glyphs-gl);
int adv = toDraw*_size;
if (x + adv < SHRT_MAX && x > SHRT_MIN) {
int ng = 0;
for (int k = 0; k < toDraw; k++) {
if (!glyphAttributes[gl + k].zeroWidth) {
rects[ng].x = x + (k * _size);
rects[ng].y = y - _size + 2;
rects[ng].width = rects[k].height = _size - 3;
++ng;
}
}
XDrawRectangles(dpy, hd, gc, rects, ng);
}
gl += toDraw;
x += adv;
}
}
if ( textFlags != 0 )
drawLines( p, this, y, x, si->num_glyphs*_size, textFlags );
#ifdef FONTENGINE_DEBUG
x = xp;
y = yp;
p->save();
p->setPen( TQt::red );
for ( int i = 0; i < numGlyphs; i++ ) {
glyph_metrics_t ci = boundingBox( glyphs[i] );
x += offsets[i].x;
y += offsets[i].y;
p->drawRect( x + ci.x, y + 50 + ci.y, ci.width, ci.height );
qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offset=(%d/%d)", i, ci.x, ci.y, ci.width, ci.height,
ci.xoff, ci.yoff, offsets[i].x, offsets[i].y );
x += ci.xoff;
y += ci.yoff;
}
p->restore();
#endif
}
glyph_metrics_t TQFontEngineBox::boundingBox( const glyph_t *, const advance_t *, const qoffset_t *, int numGlyphs )
{
glyph_metrics_t overall;
overall.x = overall.y = 0;
overall.width = _size*numGlyphs;
overall.height = _size;
overall.xoff = overall.width;
overall.yoff = 0;
return overall;
}
glyph_metrics_t TQFontEngineBox::boundingBox( glyph_t )
{
return glyph_metrics_t( 0, _size, _size, _size, _size, 0 );
}
int TQFontEngineBox::ascent() const
{
return _size;
}
int TQFontEngineBox::descent() const
{
return 0;
}
int TQFontEngineBox::leading() const
{
int l = tqRound( _size * 0.15 );
return (l > 0) ? l : 1;
}
int TQFontEngineBox::maxCharWidth() const
{
return _size;
}
int TQFontEngineBox::cmap() const
{
return -1;
}
const char *TQFontEngineBox::name() const
{
return "null";
}
bool TQFontEngineBox::canRender( const TQChar *, int )
{
return TRUE;
}
TQFontEngine::Type TQFontEngineBox::type() const
{
return Box;
}
// ------------------------------------------------------------------
// Xlfd cont engine
// ------------------------------------------------------------------
static inline XCharStruct *charStruct( XFontStruct *xfs, uint ch )
{
XCharStruct *xcs = 0;
unsigned char r = ch>>8;
unsigned char c = ch&0xff;
if ( r >= xfs->min_byte1 &&
r <= xfs->max_byte1 &&
c >= xfs->min_char_or_byte2 &&
c <= xfs->max_char_or_byte2) {
if ( !xfs->per_char )
xcs = &(xfs->min_bounds);
else {
xcs = xfs->per_char + ((r - xfs->min_byte1) *
(xfs->max_char_or_byte2 -
xfs->min_char_or_byte2 + 1)) +
(c - xfs->min_char_or_byte2);
if (xcs->width == 0 && xcs->ascent == 0 && xcs->descent == 0)
xcs = 0;
}
}
return xcs;
}
TQFontEngineXLFD::TQFontEngineXLFD( XFontStruct *fs, const char *name, int mib )
: _fs( fs ), _name( name ), _codec( 0 ), _scale( 1. ), _cmap( mib )
{
if ( _cmap ) _codec = TQTextCodec::codecForMib( _cmap );
cache_cost = (((fs->max_byte1 - fs->min_byte1) *
(fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1)) +
fs->max_char_or_byte2 - fs->min_char_or_byte2);
cache_cost = ((fs->max_bounds.ascent + fs->max_bounds.descent) *
(fs->max_bounds.width * cache_cost / 8));
lbearing = SHRT_MIN;
rbearing = SHRT_MIN;
#if 1
// Server side transformations do not seem to work correctly for
// all types of fonts (for example, it works for bdf/pcf fonts,
// but not for ttf). It also seems to be extermely server
// dependent. The best thing is to just disable server side
// transformations until either server support matures or we
// figure out a better way to do it.
xlfd_transformations = XlfdTrUnsupported;
#else
xlfd_transformations = XlfdTrUnknown;
// Hummingbird's Exceed X server will substitute 'fixed' for any
// known fonts, and it doesn't seem to support transformations, so
// we should never try to use xlfd transformations with it
if (strstr(ServerVendor(TQPaintDevice::x11AppDisplay()), "Hummingbird"))
xlfd_transformations = XlfdTrUnsupported;
#endif
}
TQFontEngineXLFD::~TQFontEngineXLFD()
{
XFreeFont( TQPaintDevice::x11AppDisplay(), _fs );
_fs = 0;
TransformedFont *trf = transformed_fonts;
while ( trf ) {
XUnloadFont( TQPaintDevice::x11AppDisplay(), trf->xlfd_font );
TransformedFont *tmp = trf;
trf = trf->next;
delete tmp;
}
}
TQFontEngine::Error TQFontEngineXLFD::stringToCMap( const TQChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const
{
if ( *nglyphs < len ) {
*nglyphs = len;
return OutOfMemory;
}
if ( _codec ) {
bool haveNbsp = FALSE;
for ( int i = 0; i < len; i++ )
if ( str[i].tqunicode() == 0xa0 ) {
haveNbsp = TRUE;
break;
}
TQChar *chars = (TQChar *)str;
if ( haveNbsp || mirrored ) {
chars = (TQChar *)malloc( len*sizeof(TQChar) );
for ( int i = 0; i < len; i++ )
chars[i] = (str[i].tqunicode() == 0xa0 ? 0x20 :
(mirrored ? ::mirroredChar(str[i]).tqunicode() : str[i].tqunicode()));
}
_codec->fromUnicodeInternal( chars, glyphs, len );
if (chars != str)
free( chars );
} else {
glyph_t *g = glyphs + len;
const TQChar *c = str + len;
if ( mirrored ) {
while ( c != str )
*(--g) = (--c)->tqunicode() == 0xa0 ? 0x20 : ::mirroredChar(*c).tqunicode();
} else {
while ( c != str )
*(--g) = (--c)->tqunicode() == 0xa0 ? 0x20 : c->tqunicode();
}
}
*nglyphs = len;
if ( advances ) {
glyph_t *g = glyphs + len;
advance_t *a = advances + len;
XCharStruct *xcs;
// inlined for better perfomance
if ( !_fs->per_char ) {
xcs = &_fs->min_bounds;
while ( a != advances )
*(--a) = xcs->width;
}
else if ( !_fs->max_byte1 ) {
XCharStruct *base = _fs->per_char - _fs->min_char_or_byte2;
while ( g-- != glyphs ) {
unsigned int gl = *g;
xcs = (gl >= _fs->min_char_or_byte2 && gl <= _fs->max_char_or_byte2) ?
base + gl : 0;
*(--a) = (!xcs || (!xcs->width && !xcs->ascent && !xcs->descent)) ? _fs->ascent : xcs->width;
}
}
else {
while ( g != glyphs ) {
xcs = charStruct( _fs, *(--g) );
*(--a) = (xcs ? xcs->width : _fs->ascent);
}
}
if ( _scale != 1. ) {
for ( int i = 0; i < len; i++ )
advances[i] = tqRound(advances[i]*_scale);
}
}
return NoError;
}
#if defined(TQ_C_CALLBACKS)
extern "C" {
#endif
static bool x_font_load_error = FALSE;
static int x_font_errorhandler(Display *, XErrorEvent *)
{
x_font_load_error = TRUE;
return 0;
}
#if defined(TQ_C_CALLBACKS)
}
#endif
void TQFontEngineXLFD::draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags )
{
if ( !si->num_glyphs )
return;
// qDebug("TQFontEngineXLFD::draw( %d, %d, numglyphs=%d", x, y, si->num_glyphs );
Display *dpy = TQPaintDevice::x11AppDisplay();
TQt::HANDLE hd = p->tqdevice()->handle();
GC gc = p->gc;
bool transform = FALSE;
int xorig = x;
int yorig = y;
TQt::HANDLE font_id = _fs->fid;
if ( p->txop > TQPainter::TxTranslate || _scale < 0.9999 || _scale > 1.0001 ) {
bool degenerate = TQABS( p->m11()*p->m22() - p->m12()*p->m21() ) < 0.01;
if ( !degenerate && xlfd_transformations != XlfdTrUnsupported ) {
// need a transformed font from the server
TQCString xlfd_transformed = _name;
int field = 0;
char *data = xlfd_transformed.data();
int pos = 0;
while ( field < 7 ) {
if ( data[pos] == '-' )
field++;
pos++;
}
int endPos = pos;
while ( data[endPos] != '-' )
endPos++;
float size = xlfd_transformed.mid( pos, endPos-pos ).toInt();
float mat[4];
mat[0] = p->m11()*size*_scale;
mat[1] = -p->m12()*size*_scale;
mat[2] = -p->m21()*size*_scale;
mat[3] = p->m22()*size*_scale;
// check if we have it cached
TransformedFont *trf = transformed_fonts;
TransformedFont *prev = 0;
int i = 0;
while ( trf ) {
if ( trf->xx == mat[0] &&
trf->xy == mat[1] &&
trf->yx == mat[2] &&
trf->yy == mat[3] )
break;
TransformedFont *tmp = trf;
trf = trf->next;
if (i > 10) {
XUnloadFont( TQPaintDevice::x11AppDisplay(), tmp->xlfd_font );
delete tmp;
prev->next = trf;
} else {
prev = tmp;
}
++i;
}
if ( trf ) {
if ( prev ) {
// move to beginning of list
prev->next = trf->next;
trf->next = transformed_fonts;
transformed_fonts = trf;
}
font_id = trf->xlfd_font;
} else {
TQCString matrix="[";
for ( int i = 0; i < 4; i++ ) {
float f = mat[i];
if ( f < 0 ) {
matrix += '~';
f = -f;
}
matrix += TQString::number( f, 'f', 5 ).latin1();
matrix += ' ';
}
matrix += ']';
//qDebug("m: %2.2f %2.2f %2.2f %2.2f, matrix=%s", p->m11(), p->m12(), p->m21(), p->m22(), matrix.data());
xlfd_transformed.tqreplace( pos, endPos-pos, matrix );
x_font_load_error = FALSE;
XErrorHandler old_handler = XSetErrorHandler( x_font_errorhandler );
font_id = XLoadFont( dpy, xlfd_transformed.data() );
XSync( dpy, FALSE );
XSetErrorHandler( old_handler );
if ( x_font_load_error ) {
//qDebug( "couldn't load transformed font" );
font_id = _fs->fid;
xlfd_transformations = XlfdTrUnsupported;
} else {
TransformedFont *trf = new TransformedFont;
trf->xx = mat[0];
trf->xy = mat[1];
trf->yx = mat[2];
trf->yy = mat[3];
trf->xlfd_font = font_id;
trf->next = transformed_fonts;
transformed_fonts = trf;
}
}
}
if ( degenerate || xlfd_transformations == XlfdTrUnsupported ) {
// XServer or font don't support server side transformations, need to do it by hand
float tmp = _scale;
_scale = 1.;
drawScaled(x, y, engine, si, textFlags, dpy, p->gc, p->tqdevice(), this, p->xmat, tmp);
_scale = tmp;
return;
}
transform = TRUE;
} else if ( p->txop == TQPainter::TxTranslate ) {
p->map( x, y, &x, &y );
}
XSetFont(dpy, gc, font_id);
#ifdef FONTENGINE_DEBUG
p->save();
p->setBrush( TQt::white );
glyph_metrics_t ci = boundingBox( glyphs, advances, offsets, si->num_glyphs );
p->drawRect( x + ci.x, y + ci.y, ci.width, ci.height );
p->drawRect( x + ci.x, y + 100 + ci.y, ci.width, ci.height );
qDebug("bounding rect=%d %d (%d/%d)", ci.x, ci.y, ci.width, ci.height );
p->restore();
int xp = x;
int yp = y;
#endif
glyph_t *glyphs = engine->glyphs( si );
advance_t *advances = engine->advances( si );
qoffset_t *offsets = engine->offsets( si );
XChar2b ch[256];
XChar2b *chars = ch;
if ( si->num_glyphs > 255 )
chars = (XChar2b *)malloc( si->num_glyphs*sizeof(XChar2b) );
for (int i = 0; i < si->num_glyphs; i++) {
chars[i].byte1 = glyphs[i] >> 8;
chars[i].byte2 = glyphs[i] & 0xff;
}
int xpos = x;
GlyphAttributes *glyphAttributes = engine->glyphAttributes( si );
if ( si->analysis.bidiLevel % 2 ) {
int i = si->num_glyphs;
while( i-- ) {
advance_t adv = advances[i];
// qDebug("advance = %d/%d", adv.x, adv.y );
x += adv;
glyph_metrics_t gi = boundingBox( glyphs[i] );
int xp = x-offsets[i].x-gi.xoff;
int yp = y+offsets[i].y-gi.yoff;
if ( transform )
p->map( xp, yp, &xp, &yp );
if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN)
XDrawString16(dpy, hd, gc, xp, yp, chars+i, 1 );
}
} else {
if ( transform || si->hasPositioning ) {
int i = 0;
while( i < si->num_glyphs ) {
int xp = x+offsets[i].x;
int yp = y+offsets[i].y;
if ( transform )
p->map( xp, yp, &xp, &yp );
if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN)
XDrawString16(dpy, hd, gc, xp, yp, chars+i, 1 );
advance_t adv = advances[i];
// qDebug("advance = %d/%d", adv.x, adv.y );
x += adv;
i++;
}
} else {
// we can take a shortcut
int gl = 0;
while (gl < si->num_glyphs) {
int toDraw = TQMIN(64, si->num_glyphs-gl);
int adv = 0;
for (int i = gl; i < gl+toDraw; ++i)
adv += advances[i];
if (x + adv < SHRT_MAX && x > SHRT_MIN)
XDrawString16(dpy, hd, gc, x, y, chars+gl, toDraw);
gl += toDraw;
x += adv;
}
}
}
if ( chars != ch )
free( chars );
if ( textFlags != 0 )
drawLines( p, this, yorig, xorig, x-xpos, textFlags );
#ifdef FONTENGINE_DEBUG
x = xp;
y = yp;
p->save();
p->setPen( TQt::red );
for ( int i = 0; i < si->num_glyphs; i++ ) {
glyph_metrics_t ci = boundingBox( glyphs[i] );
p->drawRect( x + ci.x + offsets[i].x, y + 100 + ci.y + offsets[i].y, ci.width, ci.height );
qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offs=(%d/%d) advance=(%d/%d)", i, ci.x, ci.y, ci.width, ci.height,
ci.xoff, ci.yoff, offsets[i].x, offsets[i].y,
advances[i].x, advances[i].y);
x += advances[i].x;
y += advances[i].y;
}
p->restore();
#endif
}
glyph_metrics_t TQFontEngineXLFD::boundingBox( const glyph_t *glyphs, const advance_t *advances, const qoffset_t *offsets, int numGlyphs )
{
int i;
glyph_metrics_t overall;
int ymax = 0;
int xmax = 0;
for (i = 0; i < numGlyphs; i++) {
XCharStruct *xcs = charStruct( _fs, glyphs[i] );
if (xcs) {
int x = overall.xoff + offsets[i].x - xcs->lbearing;
int y = overall.yoff + offsets[i].y - xcs->ascent;
overall.x = TQMIN( overall.x, x );
overall.y = TQMIN( overall.y, y );
xmax = TQMAX( xmax, overall.xoff + offsets[i].x + xcs->rbearing );
ymax = TQMAX( ymax, y + xcs->ascent + xcs->descent );
overall.xoff += tqRound(advances[i]/_scale);
} else {
int size = _fs->ascent;
overall.x = TQMIN(overall.x, overall.xoff );
overall.y = TQMIN(overall.y, overall.yoff - size );
ymax = TQMAX( ymax, overall.yoff );
overall.xoff += size;
xmax = TQMAX( xmax, overall.xoff );
}
}
overall.height = ymax - overall.y;
overall.width = xmax - overall.x;
if ( _scale != 1. ) {
overall.x = tqRound(overall.x * _scale);
overall.y = tqRound(overall.y * _scale);
overall.height = tqRound(overall.height * _scale);
overall.width = tqRound(overall.width * _scale);
overall.xoff = tqRound(overall.xoff * _scale);
overall.yoff = tqRound(overall.yoff * _scale);
}
return overall;
}
glyph_metrics_t TQFontEngineXLFD::boundingBox( glyph_t glyph )
{
glyph_metrics_t gm;
// ### scale missing!
XCharStruct *xcs = charStruct( _fs, glyph );
if (xcs) {
gm = glyph_metrics_t( xcs->lbearing, -xcs->ascent, xcs->rbearing- xcs->lbearing, xcs->ascent + xcs->descent, xcs->width, 0 );
} else {
int size = _fs->ascent;
gm = glyph_metrics_t( 0, size, size, size, size, 0 );
}
if ( _scale != 1. ) {
gm.x = tqRound(gm.x * _scale);
gm.y = tqRound(gm.y * _scale);
gm.height = tqRound(gm.height * _scale);
gm.width = tqRound(gm.width * _scale);
gm.xoff = tqRound(gm.xoff * _scale);
gm.yoff = tqRound(gm.yoff * _scale);
}
return gm;
}
int TQFontEngineXLFD::ascent() const
{
return tqRound(_fs->ascent*_scale);
}
int TQFontEngineXLFD::descent() const
{
return tqRound((_fs->descent-1)*_scale);
}
int TQFontEngineXLFD::leading() const
{
int l = tqRound((TQMIN(_fs->ascent, _fs->max_bounds.ascent)
+ TQMIN(_fs->descent, _fs->max_bounds.descent)) * _scale * 0.15 );
return (l > 0) ? l : 1;
}
int TQFontEngineXLFD::maxCharWidth() const
{
return tqRound(_fs->max_bounds.width*_scale);
}
// Loads the font for the specified script
static inline int maxIndex(XFontStruct *f) {
return (((f->max_byte1 - f->min_byte1) *
(f->max_char_or_byte2 - f->min_char_or_byte2 + 1)) +
f->max_char_or_byte2 - f->min_char_or_byte2);
}
int TQFontEngineXLFD::minLeftBearing() const
{
if ( lbearing == SHRT_MIN ) {
if ( _fs->per_char ) {
XCharStruct *cs = _fs->per_char;
int nc = maxIndex(_fs) + 1;
int mx = cs->lbearing;
for (int c = 1; c < nc; c++) {
// ignore the bearings for characters whose ink is
// completely outside the normal bounding box
if ((cs[c].lbearing <= 0 && cs[c].rbearing <= 0) ||
(cs[c].lbearing >= cs[c].width && cs[c].rbearing >= cs[c].width))
continue;
int nmx = cs[c].lbearing;
if (nmx < mx)
mx = nmx;
}
((TQFontEngineXLFD *)this)->lbearing = mx;
} else
((TQFontEngineXLFD *)this)->lbearing = _fs->min_bounds.lbearing;
}
return tqRound (lbearing*_scale);
}
int TQFontEngineXLFD::minRightBearing() const
{
if ( rbearing == SHRT_MIN ) {
if ( _fs->per_char ) {
XCharStruct *cs = _fs->per_char;
int nc = maxIndex(_fs) + 1;
int mx = cs->rbearing;
for (int c = 1; c < nc; c++) {
// ignore the bearings for characters whose ink is
// completely outside the normal bounding box
if ((cs[c].lbearing <= 0 && cs[c].rbearing <= 0) ||
(cs[c].lbearing >= cs[c].width && cs[c].rbearing >= cs[c].width))
continue;
int nmx = cs[c].rbearing;
if (nmx < mx)
mx = nmx;
}
((TQFontEngineXLFD *)this)->rbearing = mx;
} else
((TQFontEngineXLFD *)this)->rbearing = _fs->min_bounds.rbearing;
}
return tqRound (rbearing*_scale);
}
int TQFontEngineXLFD::cmap() const
{
return _cmap;
}
const char *TQFontEngineXLFD::name() const
{
return _name;
}
bool TQFontEngineXLFD::canRender( const TQChar *string, int len )
{
glyph_t glyphs[256];
int nglyphs = 255;
glyph_t *g = glyphs;
if ( stringToCMap( string, len, g, 0, &nglyphs, FALSE ) == OutOfMemory ) {
g = (glyph_t *)malloc( nglyphs*sizeof(glyph_t) );
stringToCMap( string, len, g, 0, &nglyphs, FALSE );
}
bool allExist = TRUE;
for ( int i = 0; i < nglyphs; i++ ) {
if ( !g[i] || !charStruct( _fs, g[i] ) ) {
allExist = FALSE;
break;
}
}
if ( g != glyphs )
free( g );
return allExist;
}
void TQFontEngineXLFD::setScale( double scale )
{
_scale = scale;
}
TQFontEngine::Type TQFontEngineXLFD::type() const
{
return XLFD;
}
// ------------------------------------------------------------------
// LatinXLFD engine
// ------------------------------------------------------------------
static const int engine_array_inc = 4;
TQFontEngineLatinXLFD::TQFontEngineLatinXLFD( XFontStruct *xfs, const char *name,
int mib )
{
_engines = new TQFontEngine*[ engine_array_inc ];
_engines[0] = new TQFontEngineXLFD( xfs, name, mib );
_count = 1;
cache_cost = _engines[0]->cache_cost;
memset( glyphIndices, 0, sizeof( glyphIndices ) );
memset( glyphAdvances, 0, sizeof( glyphAdvances ) );
euroIndex = 0;
euroAdvance = 0;
}
TQFontEngineLatinXLFD::~TQFontEngineLatinXLFD()
{
for ( int i = 0; i < _count; ++i ) {
delete _engines[i];
_engines[i] = 0;
}
delete [] _engines;
_engines = 0;
}
void TQFontEngineLatinXLFD::tqfindEngine( const TQChar &ch )
{
if ( ch.tqunicode() == 0 ) return;
static const char *alternate_encodings[] = {
"iso8859-1",
"iso8859-2",
"iso8859-3",
"iso8859-4",
"iso8859-9",
"iso8859-10",
"iso8859-13",
"iso8859-14",
"iso8859-15",
"hp-roman8"
};
static const int mib_count = sizeof( alternate_encodings ) / sizeof( const char * );
// see if one of the above mibs can map the char we want
TQTextCodec *codec = 0;
int which = -1;
int i;
for ( i = 0; i < mib_count; ++i ) {
const int mib = qt_mib_for_xlfd_encoding( alternate_encodings[i] );
bool skip = FALSE;
for ( int e = 0; e < _count; ++e ) {
if ( _engines[e]->cmap() == mib ) {
skip = TRUE;
break;
}
}
if ( skip ) continue;
codec = TQTextCodec::codecForMib( mib );
if ( codec && codec->canEncode( ch ) ) {
which = i;
break;
}
}
if ( ! codec || which == -1 )
return;
const int enc_id = qt_xlfd_encoding_id( alternate_encodings[which] );
TQFontDef req = fontDef;
TQFontEngine *engine = TQFontDatabase::tqfindFont( TQFont::Latin, 0, req, enc_id );
if ( ! engine ) {
req.family = TQString::null;
engine = TQFontDatabase::tqfindFont( TQFont::Latin, 0, req, enc_id );
if ( ! engine ) return;
}
engine->setScale( scale() );
if ( ! ( _count % engine_array_inc ) ) {
// grow the engines array
TQFontEngine **old = _engines;
int new_size =
( ( ( _count+engine_array_inc ) / engine_array_inc ) * engine_array_inc );
_engines = new TQFontEngine*[new_size];
for ( i = 0; i < _count; ++i )
_engines[i] = old[i];
delete [] old;
}
_engines[_count] = engine;
const int hi = _count << 8;
++_count;
unsigned short chars[0x201];
glyph_t glyphs[0x201];
advance_t advances[0x201];
for ( i = 0; i < 0x200; ++i )
chars[i] = i;
chars[0x200] = 0x20ac;
int glyphCount = 0x201;
engine->stringToCMap( (const TQChar *) chars, 0x201, glyphs, advances, &glyphCount, FALSE );
// merge member data with the above
for ( i = 0; i < 0x200; ++i ) {
if ( glyphIndices[i] != 0 || glyphs[i] == 0 ) continue;
glyphIndices[i] = glyphs[i] >= 0x2100 ? glyphs[i] : hi | glyphs[i];
glyphAdvances[i] = advances[i];
}
if (!euroIndex && glyphs[0x200]) {
euroIndex = hi | glyphs[0x200];
euroAdvance = advances[0x200];
}
}
TQFontEngine::Error
TQFontEngineLatinXLFD::stringToCMap( const TQChar *str, int len, glyph_t *glyphs,
advance_t *advances, int *nglyphs, bool mirrored ) const
{
if ( *nglyphs < len ) {
*nglyphs = len;
return OutOfMemory;
}
int i;
bool missing = FALSE;
const TQChar *c = str+len;
glyph_t *g = glyphs+len;
if ( advances ) {
int asc = ascent();
advance_t *a = advances+len;
if ( mirrored ) {
while ( c != str ) {
--c;
--g;
--a;
if ( c->tqunicode() < 0x200 ) {
unsigned short ch = ::mirroredChar(*c).tqunicode();
*g = glyphIndices[ch];
*a = glyphAdvances[ch];
} else {
if ( c->tqunicode() == 0x20ac ) {
*g = euroIndex;
*a = euroAdvance;
} else {
*g = 0;
*a = asc;
}
}
missing = ( missing || ( *g == 0 ) );
}
} else {
while ( c != str ) {
--c;
--g;
--a;
if ( c->tqunicode() < 0x200 ) {
*g = glyphIndices[c->tqunicode()];
*a = glyphAdvances[c->tqunicode()];
} else {
if ( c->tqunicode() == 0x20ac ) {
*g = euroIndex;
*a = euroAdvance;
} else {
*g = 0;
*a = asc;
}
}
missing = ( missing || ( *g == 0 ) );
}
}
} else {
if ( mirrored ) {
while ( c != str ) {
--c;
--g;
*g = ( ( c->tqunicode() < 0x200 ) ? glyphIndices[::mirroredChar(*c).tqunicode()]
: (c->tqunicode() == 0x20ac) ? euroIndex : 0 );
missing = ( missing || ( *g == 0 ) );
}
} else {
while ( c != str ) {
--c;
--g;
*g = ( ( c->tqunicode() < 0x200 ) ? glyphIndices[c->tqunicode()]
: (c->tqunicode() == 0x20ac) ? euroIndex : 0 );
missing = ( missing || ( *g == 0 ) );
}
}
}
if ( missing ) {
for ( i = 0; i < len; ++i ) {
unsigned short uc = str[i].tqunicode();
if ( glyphs[i] != 0 || (uc >= 0x200 && uc != 0x20ac) )
continue;
TQFontEngineLatinXLFD *that = (TQFontEngineLatinXLFD *) this;
that->tqfindEngine( str[i] );
glyphs[i] = (uc == 0x20ac ? euroIndex : that->glyphIndices[uc]);
if ( advances )
advances[i] = (uc == 0x20ac ? euroAdvance : glyphAdvances[uc]);
}
}
*nglyphs = len;
return NoError;
}
void TQFontEngineLatinXLFD::draw( TQPainter *p, int x, int y, const TQTextEngine *engine,
const TQScriptItem *si, int textFlags )
{
if ( !si->num_glyphs ) return;
glyph_t *glyphs = engine->glyphs( si );
advance_t *advances = engine->advances( si );
int which = glyphs[0] >> 8;
if (which > 0x20)
which = 0;
int start = 0;
int end, i;
for ( end = 0; end < si->num_glyphs; ++end ) {
int e = glyphs[end] >> 8;
if (e > 0x20)
e = 0;
if ( e == which ) continue;
// set the high byte to zero
if (which != 0) {
for ( i = start; i < end; ++i )
glyphs[i] = glyphs[i] & 0xff;
}
// draw the text
TQScriptItem si2 = *si;
si2.glyph_data_offset = si->glyph_data_offset + start;
si2.num_glyphs = end - start;
_engines[which]->draw( p, x, y, engine, &si2, textFlags );
// reset the high byte for all glyphs and advance to the next sub-string
const int hi = which << 8;
for ( i = start; i < end; ++i ) {
glyphs[i] = hi | glyphs[i];
x += advances[i];
}
// change engine
start = end;
which = e;
}
// set the high byte to zero
if (which != 0) {
for ( i = start; i < end; ++i )
glyphs[i] = glyphs[i] & 0xff;
}
// draw the text
TQScriptItem si2 = *si;
si2.glyph_data_offset = si->glyph_data_offset + start;
si2.num_glyphs = end - start;
_engines[which]->draw( p, x, y, engine, &si2, textFlags );
// reset the high byte for all glyphs
if (which != 0) {
const int hi = which << 8;
for ( i = start; i < end; ++i )
glyphs[i] = hi | glyphs[i];
}
}
glyph_metrics_t TQFontEngineLatinXLFD::boundingBox( const glyph_t *glyphs_const,
const advance_t *advances,
const qoffset_t *offsets,
int numGlyphs )
{
if ( numGlyphs <= 0 ) return glyph_metrics_t();
glyph_metrics_t overall;
glyph_t *glyphs = (glyph_t *) glyphs_const;
int which = glyphs[0] >> 8;
if (which > 0x20)
which = 0;
int start = 0;
int end, i;
for ( end = 0; end < numGlyphs; ++end ) {
int e = glyphs[end] >> 8;
if (e > 0x20)
e = 0;
if ( e == which ) continue;
// set the high byte to zero
if (which != 0) {
for ( i = start; i < end; ++i )
glyphs[i] = glyphs[i] & 0xff;
}
// merge the bounding box for this run
const glyph_metrics_t gm =
_engines[which]->boundingBox( glyphs + start,
advances + start,
offsets + start,
end - start );
overall.x = TQMIN( overall.x, gm.x );
overall.y = TQMIN( overall.y, gm.y );
overall.width = overall.xoff + gm.width;
overall.height = TQMAX( overall.height + overall.y, gm.height + gm.y ) -
TQMIN( overall.y, gm.y );
overall.xoff += gm.xoff;
overall.yoff += gm.yoff;
// reset the high byte for all glyphs
if (which != 0) {
const int hi = which << 8;
for ( i = start; i < end; ++i )
glyphs[i] = hi | glyphs[i];
}
// change engine
start = end;
which = e;
}
// set the high byte to zero
if (which != 0) {
for ( i = start; i < end; ++i )
glyphs[i] = glyphs[i] & 0xff;
}
// merge the bounding box for this run
const glyph_metrics_t gm =
_engines[which]->boundingBox( glyphs + start,
advances + start,
offsets + start,
end - start );
overall.x = TQMIN( overall.x, gm.x );
overall.y = TQMIN( overall.y, gm.y );
overall.width = overall.xoff + gm.width;
overall.height = TQMAX( overall.height + overall.y, gm.height + gm.y ) -
TQMIN( overall.y, gm.y );
overall.xoff += gm.xoff;
overall.yoff += gm.yoff;
// reset the high byte for all glyphs
if (which != 0) {
const int hi = which << 8;
for ( i = start; i < end; ++i )
glyphs[i] = hi | glyphs[i];
}
return overall;
}
glyph_metrics_t TQFontEngineLatinXLFD::boundingBox( glyph_t glyph )
{
int engine = glyph >> 8;
if (engine > 0x20)
engine = 0;
TQ_ASSERT( engine < _count );
return _engines[engine]->boundingBox( engine > 0 ? glyph & 0xff : glyph );
}
int TQFontEngineLatinXLFD::ascent() const
{
return _engines[0]->ascent();
}
int TQFontEngineLatinXLFD::descent() const
{
return _engines[0]->descent();
}
int TQFontEngineLatinXLFD::leading() const
{
return _engines[0]->leading();
}
int TQFontEngineLatinXLFD::maxCharWidth() const
{
return _engines[0]->maxCharWidth();
}
int TQFontEngineLatinXLFD::minLeftBearing() const
{
return _engines[0]->minLeftBearing();
}
int TQFontEngineLatinXLFD::minRightBearing() const
{
return _engines[0]->minRightBearing();
}
const char *TQFontEngineLatinXLFD::name() const
{
return _engines[0]->name();
}
bool TQFontEngineLatinXLFD::canRender( const TQChar *string, int len )
{
bool all = TRUE;
int i;
for ( i = 0; i < len; ++i ) {
if ( string[i].tqunicode() >= 0x200 ||
glyphIndices[string[i].tqunicode()] == 0 ) {
if (string[i].tqunicode() != 0x20ac || euroIndex == 0)
all = FALSE;
break;
}
}
if ( all )
return TRUE;
all = TRUE;
for ( i = 0; i < len; ++i ) {
if ( string[i].tqunicode() >= 0x200 ) {
if (string[i].tqunicode() == 0x20ac) {
if (euroIndex)
continue;
tqfindEngine(string[i]);
if (euroIndex)
continue;
}
all = FALSE;
break;
}
if ( glyphIndices[string[i].tqunicode()] != 0 ) continue;
tqfindEngine( string[i] );
if ( glyphIndices[string[i].tqunicode()] == 0 ) {
all = FALSE;
break;
}
}
return all;
}
void TQFontEngineLatinXLFD::setScale( double scale )
{
int i;
for ( i = 0; i < _count; ++i )
_engines[i]->setScale( scale );
unsigned short chars[0x200];
for ( i = 0; i < 0x200; ++i )
chars[i] = i;
int glyphCount = 0x200;
_engines[0]->stringToCMap( (const TQChar *)chars, 0x200,
glyphIndices, glyphAdvances, &glyphCount, FALSE );
}
// ------------------------------------------------------------------
// Xft cont engine
// ------------------------------------------------------------------
// #define FONTENGINE_DEBUG
#ifndef TQT_NO_XFTFREETYPE
class TQ_HackPaintDevice : public TQPaintDevice
{
public:
inline TQ_HackPaintDevice() : TQPaintDevice( 0 ) {}
inline XftDraw *xftDrawHandle() const {
return (XftDraw *)rendhd;
}
};
#ifdef TQT_XFT2
static inline void getGlyphInfo( XGlyphInfo *xgi, XftFont *font, int glyph )
{
FT_UInt x = glyph;
XftGlyphExtents( TQPaintDevice::x11AppDisplay(), font, &x, 1, xgi );
}
#else
static inline XftFontStruct *getFontStruct( XftFont *font )
{
if (font->core)
return 0;
return font->u.ft.font;
}
static inline void getGlyphInfo(XGlyphInfo *xgi, XftFont *font, int glyph)
{
XftTextExtents32(TQPaintDevice::x11AppDisplay(), font, (XftChar32 *) &glyph, 1, xgi);
}
#endif // TQT_XFT2
static inline FT_Face lockFTFace( XftFont *font )
{
#ifdef TQT_XFT2
return XftLockFace( font );
#else
if (font->core) return 0;
return font->u.ft.font->face;
#endif // TQT_XFT2
}
static inline void unlockFTFace( XftFont *font )
{
#ifdef TQT_XFT2
XftUnlockFace( font );
#else
TQ_UNUSED( font );
#endif // TQT_XFT2
}
TQFontEngineXft::TQFontEngineXft( XftFont *font, XftPattern *pattern, int cmap )
: _font( font ), _pattern( pattern ), _openType( 0 ), _cmap( cmap )
{
_face = lockFTFace( _font );
#ifndef TQT_XFT2
XftFontStruct *xftfs = getFontStruct( _font );
if ( xftfs ) {
// dirty hack: we set the charmap in the Xftfreetype to -1, so
// XftFreetype assumes no encoding and really draws glyph
// indices. The FT_Face still has the Unicode encoding to we
// can convert from Unicode to glyph index
xftfs->charmap = -1;
}
#else
_cmap = -1;
// Xft maps Unicode and adobe roman for us.
for (int i = 0; i < _face->num_charmaps; ++i) {
FT_CharMap cm = _face->charmaps[i];
// qDebug("font has charmap %x", cm->encoding);
if (cm->encoding == ft_encoding_adobe_custom
|| cm->encoding == ft_encoding_symbol) {
// qDebug("font has adobe custom or ms symbol charmap");
_cmap = i;
break;
}
}
#endif // TQT_XFT2
cache_cost = _font->height * _font->max_advance_width *
( _face ? _face->num_glyphs : 1024 );
// if the Xft font is not antialiased, it uses bitmaps instead of
// 8-bit alpha maps... adjust the cache_cost to reflect this
Bool antialiased = TRUE;
if ( XftPatternGetBool( pattern, XFT_ANTIALIAS,
0, &antialiased ) == XftResultMatch &&
! antialiased ) {
cache_cost /= 8;
}
lbearing = SHRT_MIN;
rbearing = SHRT_MIN;
memset( widthCache, 0, sizeof(widthCache) );
memset( cmapCache, 0, sizeof(cmapCache) );
}
TQFontEngineXft::~TQFontEngineXft()
{
delete _openType;
unlockFTFace( _font );
XftFontClose( TQPaintDevice::x11AppDisplay(),_font );
XftPatternDestroy( _pattern );
_font = 0;
_pattern = 0;
TransformedFont *trf = transformed_fonts;
while ( trf ) {
XftFontClose( TQPaintDevice::x11AppDisplay(), trf->xft_font );
TransformedFont *tmp = trf;
trf = trf->next;
delete tmp;
}
}
#ifdef TQT_XFT2
static glyph_t getAdobeCharIndex(XftFont *font, int cmap, uint ucs4)
{
FT_Face _face = XftLockFace( font );
FT_Set_Charmap(_face, _face->charmaps[cmap]);
glyph_t g = FT_Get_Char_Index(_face, ucs4);
XftUnlockFace(font);
return g;
}
#endif
TQFontEngine::Error TQFontEngineXft::stringToCMap( const TQChar *str, int len, glyph_t *glyphs, advance_t *advances, int *nglyphs, bool mirrored ) const
{
if ( *nglyphs < len ) {
*nglyphs = len;
return OutOfMemory;
}
#ifdef TQT_XFT2
if (_cmap != -1) {
for ( int i = 0; i < len; ++i ) {
unsigned short uc = str[i].tqunicode();
if (mirrored)
uc = ::mirroredChar(str[i]).tqunicode();
glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0;
if ( !glyphs[i] ) {
glyph_t glyph = XftCharIndex(0, _font, uc);
if (!glyph)
glyph = getAdobeCharIndex(_font, _cmap, uc);
glyphs[i] = glyph;
if ( uc < cmapCacheSize )
((TQFontEngineXft *)this)->cmapCache[uc] = glyph;
}
}
} else if ( mirrored ) {
for ( int i = 0; i < len; ++i ) {
unsigned short uc = ::mirroredChar(str[i]).tqunicode();
glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0;
if ( !glyphs[i] ) {
if (uc == 0xa0)
uc = 0x20;
glyph_t glyph = XftCharIndex(0, _font, uc);
glyphs[i] = glyph;
if ( uc < cmapCacheSize )
((TQFontEngineXft *)this)->cmapCache[uc] = glyph;
}
}
} else {
for ( int i = 0; i < len; ++i ) {
unsigned short uc = str[i].tqunicode();
glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0;
if ( !glyphs[i] ) {
if (uc == 0xa0)
uc = 0x20;
glyph_t glyph = XftCharIndex(0, _font, uc);
glyphs[i] = glyph;
if ( uc < cmapCacheSize )
((TQFontEngineXft *)this)->cmapCache[uc] = glyph;
}
}
}
if ( advances ) {
for ( int i = 0; i < len; i++ ) {
FT_UInt glyph = *(glyphs + i);
advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0;
if ( !advances[i] ) {
XGlyphInfo gi;
XftGlyphExtents( TQPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi );
advances[i] = gi.xOff;
if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 )
((TQFontEngineXft *)this)->widthCache[glyph] = gi.xOff;
}
}
if ( _scale != 1. ) {
for ( int i = 0; i < len; i++ )
advances[i] = tqRound(advances[i]*_scale);
}
}
#else
if ( !_face ) {
if ( mirrored ) {
for ( int i = 0; i < len; i++ )
glyphs[i] = ::mirroredChar(str[i]).tqunicode();
} else {
for ( int i = 0; i < len; i++ )
glyphs[i] = str[i].tqunicode();
}
} else {
if ( _cmap == 1 ) {
// symbol font
for ( int i = 0; i < len; i++ ) {
unsigned short uc = str[i].tqunicode();
glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0;
if ( !glyphs[i] ) {
glyph_t glyph = FT_Get_Char_Index( _face, uc );
if(!glyph && uc < 0x100)
glyph = FT_Get_Char_Index( _face, uc+0xf000 );
glyphs[i] = glyph;
if ( uc < cmapCacheSize )
((TQFontEngineXft *)this)->cmapCache[uc] = glyph;
}
}
} else if ( mirrored ) {
for ( int i = 0; i < len; i++ ) {
unsigned short uc = ::mirroredChar(str[i]).tqunicode();
glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0;
if ( !glyphs[i] ) {
glyph_t glyph = FT_Get_Char_Index( _face, uc );
glyphs[i] = glyph;
if ( uc < cmapCacheSize )
((TQFontEngineXft *)this)->cmapCache[uc] = glyph;
}
}
} else {
for ( int i = 0; i < len; i++ ) {
unsigned short uc = str[i].tqunicode();
glyphs[i] = uc < cmapCacheSize ? cmapCache[uc] : 0;
if ( !glyphs[i] ) {
glyph_t glyph = FT_Get_Char_Index( _face, uc );
glyphs[i] = glyph;
if ( uc < cmapCacheSize )
((TQFontEngineXft *)this)->cmapCache[uc] = glyph;
}
}
}
}
if ( advances ) {
for ( int i = 0; i < len; i++ ) {
XftChar16 glyph = *(glyphs + i);
advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0;
if ( !advances[i] ) {
XGlyphInfo gi;
XftTextExtents16(TQPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi);
advances[i] = gi.xOff;
if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 )
((TQFontEngineXft *)this)->widthCache[glyph] = gi.xOff;
}
}
if ( _scale != 1. ) {
for ( int i = 0; i < len; i++ )
advances[i] = tqRound(advances[i]*_scale);
}
}
#endif // TQT_XFT2
*nglyphs = len;
return NoError;
}
void TQFontEngineXft::recalcAdvances( int len, glyph_t *glyphs, advance_t *advances )
{
#ifdef TQT_XFT2
for ( int i = 0; i < len; i++ ) {
FT_UInt glyph = *(glyphs + i);
advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0;
if ( !advances[i] ) {
XGlyphInfo gi;
XftGlyphExtents( TQPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi );
advances[i] = gi.xOff;
if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 )
((TQFontEngineXft *)this)->widthCache[glyph] = gi.xOff;
}
if ( _scale != 1. ) {
for ( int i = 0; i < len; i++ )
advances[i] = tqRound(advances[i]*_scale);
}
}
#else
for ( int i = 0; i < len; i++ ) {
XftChar16 glyph = *(glyphs + i);
advances[i] = (glyph < widthCacheSize) ? widthCache[glyph] : 0;
if ( !advances[i] ) {
XGlyphInfo gi;
XftTextExtents16(TQPaintDevice::x11AppDisplay(), _font, &glyph, 1, &gi);
advances[i] = gi.xOff;
if ( glyph < widthCacheSize && gi.xOff > 0 && gi.xOff < 0x100 )
((TQFontEngineXft *)this)->widthCache[glyph] = gi.xOff;
}
}
if ( _scale != 1. ) {
for ( int i = 0; i < len; i++ )
advances[i] = tqRound(advances[i]*_scale);
}
#endif // TQT_XFT2
}
//#define FONTENGINE_DEBUG
void TQFontEngineXft::draw( TQPainter *p, int x, int y, const TQTextEngine *engine, const TQScriptItem *si, int textFlags )
{
if ( !si->num_glyphs )
return;
Display *dpy = TQPaintDevice::x11AppDisplay();
int xorig = x;
int yorig = y;
GlyphAttributes *glyphAttributes = engine->glyphAttributes( si );
XftFont *fnt = _font;
bool transform = FALSE;
if ( p->txop >= TQPainter::TxScale || p->rop != TQt::CopyROP || _scale < 0.9999 || _scale > 1.001) {
bool can_scale = (_face->face_flags & FT_FACE_FLAG_SCALABLE) && p->rop == TQt::CopyROP;
double size = (p->m11()*p->m22() - p->m12()*p->m21())*_scale*_scale*fontDef.pixelSize*fontDef.pixelSize;
if (size > 256*256 || _scale < .9999 || _scale > 1.001)
can_scale = FALSE;
if (!can_scale) {
// font doesn't support transformations, need to do it by hand
float tmp = _scale;
_scale = 1.;
drawScaled(x, y, engine, si, textFlags, dpy, p->gc, p->tqdevice(), this, p->xmat, tmp);
_scale = tmp;
return;
}
XftMatrix *mat = 0;
XftPatternGetMatrix( _pattern, XFT_MATRIX, 0, &mat );
XftMatrix m2;
m2.xx = p->m11()*_scale;
m2.xy = -p->m21()*_scale;
m2.yx = -p->m12()*_scale;
m2.yy = p->m22()*_scale;
// check if we have it cached
TransformedFont *trf = transformed_fonts;
TransformedFont *prev = 0;
int i = 0;
while ( trf ) {
if ( trf->xx == (float)m2.xx &&
trf->xy == (float)m2.xy &&
trf->yx == (float)m2.yx &&
trf->yy == (float)m2.yy )
break;
TransformedFont *tmp = trf;
trf = trf->next;
if (i > 10) {
XftFontClose( TQPaintDevice::x11AppDisplay(), tmp->xft_font );
delete tmp;
prev->next = trf;
} else {
prev = tmp;
}
++i;
}
if ( trf ) {
if ( prev ) {
// move to beginning of list
prev->next = trf->next;
trf->next = transformed_fonts;
transformed_fonts = trf;
}
fnt = trf->xft_font;
} else {
if ( mat )
XftMatrixMultiply( &m2, &m2, mat );
XftPattern *pattern = XftPatternDuplicate( _pattern );
XftPatternDel( pattern, XFT_MATRIX );
XftPatternAddMatrix( pattern, XFT_MATRIX, &m2 );
fnt = XftFontOpenPattern( dpy, pattern );
#ifndef TQT_XFT2
XftFontStruct *xftfs = getFontStruct( fnt );
if ( xftfs ) {
// dirty hack: we set the charmap in the Xftfreetype to -1, so
// XftFreetype assumes no encoding and really draws glyph
// indices. The FT_Face still has the Unicode encoding to we
// can convert from Unicode to glyph index
xftfs->charmap = -1;
}
#endif // TQT_XFT2
TransformedFont *trf = new TransformedFont;
trf->xx = (float)m2.xx;
trf->xy = (float)m2.xy;
trf->yx = (float)m2.yx;
trf->yy = (float)m2.yy;
trf->xft_font = fnt;
trf->next = transformed_fonts;
transformed_fonts = trf;
}
transform = TRUE;
} else if ( p->txop == TQPainter::TxTranslate ) {
p->map( x, y, &x, &y );
}
glyph_t *glyphs = engine->glyphs( si );
advance_t *advances = engine->advances( si );
qoffset_t *offsets = engine->offsets( si );
const TQColor &pen = p->cpen.color();
XftDraw *draw = ((TQ_HackPaintDevice *)p->pdev)->xftDrawHandle();
XftColor col;
col.color.red = pen.red () | pen.red() << 8;
col.color.green = pen.green () | pen.green() << 8;
col.color.blue = pen.blue () | pen.blue() << 8;
col.color.alpha = 0xffff;
col.pixel = pen.pixel();
#ifdef FONTENGINE_DEBUG
qDebug("===== drawing %d glyphs reverse=%s ======", si->num_glyphs, si->analysis.bidiLevel % 2?"TRUE":"FALSE" );
p->save();
p->setBrush( TQt::white );
glyph_metrics_t ci = boundingBox( glyphs, advances, offsets, si->num_glyphs );
p->drawRect( x + ci.x, y + ci.y, ci.width, ci.height );
p->drawRect( x + ci.x, y + 100 + ci.y, ci.width, ci.height );
qDebug("bounding rect=%d %d (%d/%d)", ci.x, ci.y, ci.width, ci.height );
p->restore();
int yp = y;
int xp = x;
#endif
if ( textFlags != 0 )
drawLines( p, this, yorig, xorig, si->width, textFlags );
if ( si->isSpace )
return;
if ( transform || si->hasPositioning ) {
if ( si->analysis.bidiLevel % 2 ) {
int i = si->num_glyphs;
while( i-- ) {
int xp = x + offsets[i].x;
int yp = y + offsets[i].y;
if ( transform )
p->map( xp, yp, &xp, &yp );
#ifdef TQT_XFT2
FT_UInt glyph = *(glyphs + i);
if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN)
XftDrawGlyphs( draw, &col, fnt, xp, yp, &glyph, 1 );
#else
if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN)
XftDrawString16( draw, &col, fnt, xp, yp, (XftChar16 *) (glyphs+i), 1);
#endif // TQT_XFT2
#ifdef FONTENGINE_DEBUG
glyph_metrics_t gi = boundingBox( glyphs[i] );
p->drawRect( x+offsets[i].x+gi.x, y+offsets[i].y+100+gi.y, gi.width, gi.height );
p->drawLine( x+offsets[i].x, y + 150 + 5*i , x+offsets[i].x+advances[i], y + 150 + 5*i );
p->drawLine( x+offsets[i].x, y + 152 + 5*i , x+offsets[i].x+gi.xoff, y + 152 + 5*i );
qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offs=(%d/%d) advance=%d", i, gi.x, gi.y, gi.width, gi.height,
gi.xoff, gi.yoff, offsets[i].x, offsets[i].y, advances[i]);
#endif
x += advances[i];
}
} else {
int i = 0;
while ( i < si->num_glyphs ) {
int xp = x + offsets[i].x;
int yp = y + offsets[i].y;
if ( transform )
p->map( xp, yp, &xp, &yp );
#ifdef TQT_XFT2
FT_UInt glyph = *(glyphs + i);
if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN)
XftDrawGlyphs( draw, &col, fnt, xp, yp, &glyph, 1 );
#else
if (!glyphAttributes[i].zeroWidth && xp < SHRT_MAX && xp > SHRT_MIN)
XftDrawString16( draw, &col, fnt, xp, yp, (XftChar16 *) (glyphs+i), 1 );
#endif // TQT_XFT2
// qDebug("advance = %d/%d", adv.x, adv.y );
x += advances[i];
i++;
}
}
} else {
// Xft has real trouble drawing the glyphs on their own.
// Drawing them as one string increases performance significantly.
#ifdef TQT_XFT2
// #### we should use a different method anyways on Xft2
FT_UInt g[64];
int gl = 0;
while (gl < si->num_glyphs) {
int toDraw = TQMIN(64, si->num_glyphs-gl);
int adv = 0;
if ( si->analysis.bidiLevel % 2 ) {
for ( int i = 0; i < toDraw; i++ ) {
g[i] = glyphs[si->num_glyphs-1-(gl+i)];
adv += advances[si->num_glyphs-1-(gl+i)];
}
} else {
for ( int i = 0; i < toDraw; i++ ) {
g[i] = glyphs[gl+i];
adv += advances[gl+i];
}
}
if (x + adv < SHRT_MAX && x > SHRT_MIN)
XftDrawGlyphs( draw, &col, fnt, x, y, g, toDraw );
gl += toDraw;
x += adv;
}
#else
XftChar16 g[64];
int gl = 0;
while (gl < si->num_glyphs) {
int toDraw = TQMIN(64, si->num_glyphs-gl);
int adv = 0;
if ( si->analysis.bidiLevel % 2 ) {
for ( int i = 0; i < toDraw; i++ ) {
g[i] = glyphs[si->num_glyphs-1-(gl+i)];
adv += advances[si->num_glyphs-1-(gl+i)];
}
} else {
for ( int i = 0; i < toDraw; i++ ) {
g[i] = glyphs[gl+i];
adv += advances[gl+i];
}
}
if (x + adv < SHRT_MAX && x > SHRT_MIN)
XftDrawString16( draw, &col, fnt, x, y, g, toDraw );
gl += toDraw;
x += adv;
}
#endif // TQT_XFT2
}
#ifdef FONTENGINE_DEBUG
if ( !si->analysis.bidiLevel % 2 ) {
x = xp;
y = yp;
p->save();
p->setPen( TQt::red );
for ( int i = 0; i < si->num_glyphs; i++ ) {
glyph_metrics_t ci = boundingBox( glyphs[i] );
p->drawRect( x + ci.x + offsets[i].x, y + 100 + ci.y + offsets[i].y, ci.width, ci.height );
qDebug("bounding ci[%d]=%d %d (%d/%d) / %d %d offs=(%d/%d) advance=%d", i, ci.x, ci.y, ci.width, ci.height,
ci.xoff, ci.yoff, offsets[i].x, offsets[i].y, advances[i]);
x += advances[i];
}
p->restore();
}
#endif
}
glyph_metrics_t TQFontEngineXft::boundingBox( const glyph_t *glyphs, const advance_t *advances, const qoffset_t *offsets, int numGlyphs )
{
XGlyphInfo xgi;
glyph_metrics_t overall;
int ymax = 0;
int xmax = 0;
if (_scale != 1) {
for (int i = 0; i < numGlyphs; i++) {
getGlyphInfo( &xgi, _font, glyphs[i] );
int x = overall.xoff + offsets[i].x - xgi.x;
int y = overall.yoff + offsets[i].y - xgi.y;
overall.x = TQMIN( overall.x, x );
overall.y = TQMIN( overall.y, y );
xmax = TQMAX( xmax, x + xgi.width );
ymax = TQMAX( ymax, y + xgi.height );
overall.xoff += tqRound(advances[i]/_scale);
}
overall.x = tqRound(overall.x * _scale);
overall.y = tqRound(overall.y * _scale);
overall.xoff = tqRound(overall.xoff * _scale);
overall.yoff = tqRound(overall.yoff * _scale);
xmax = tqRound(xmax * _scale);
ymax = tqRound(ymax * _scale);
} else {
for (int i = 0; i < numGlyphs; i++) {
getGlyphInfo( &xgi, _font, glyphs[i] );
int x = overall.xoff + offsets[i].x - xgi.x;
int y = overall.yoff + offsets[i].y - xgi.y;
overall.x = TQMIN( overall.x, x );
overall.y = TQMIN( overall.y, y );
xmax = TQMAX( xmax, x + xgi.width );
ymax = TQMAX( ymax, y + xgi.height );
overall.xoff += advances[i];
}
}
overall.height = ymax - overall.y;
overall.width = xmax - overall.x;
return overall;
}
glyph_metrics_t TQFontEngineXft::boundingBox( glyph_t glyph )
{
XGlyphInfo xgi;
getGlyphInfo( &xgi, _font, glyph );
glyph_metrics_t gm = glyph_metrics_t( -xgi.x, -xgi.y, xgi.width, xgi.height, xgi.xOff, -xgi.yOff );
if ( _scale != 1. ) {
gm.x = tqRound(gm.x * _scale);
gm.y = tqRound(gm.y * _scale);
gm.height = tqRound(gm.height * _scale);
gm.width = tqRound(gm.width * _scale);
gm.xoff = tqRound(gm.xoff * _scale);
gm.yoff = tqRound(gm.yoff * _scale);
}
return gm;
}
int TQFontEngineXft::ascent() const
{
return tqRound(_font->ascent*_scale);
}
int TQFontEngineXft::descent() const
{
return tqRound((_font->descent-1)*_scale);
}
// #### use Freetype to determine this
int TQFontEngineXft::leading() const
{
int l = tqRound(TQMIN( _font->height - (_font->ascent + _font->descent),
((_font->ascent + _font->descent) >> 4)*_scale ));
return (l > 0) ? l : 1;
}
// #### use Freetype to determine this
int TQFontEngineXft::lineThickness() const
{
// ad hoc algorithm
int score = fontDef.weight * fontDef.pixelSize;
int lw = score / 700;
// looks better with thicker line for small pointsizes
if ( lw < 2 && score >= 1050 ) lw = 2;
if ( lw == 0 ) lw = 1;
return lw;
}
// #### use Freetype to determine this
int TQFontEngineXft::underlinePosition() const
{
int pos = ( ( lineThickness() * 2 ) + 3 ) / 6;
return pos ? pos : 1;
}
int TQFontEngineXft::maxCharWidth() const
{
return tqRound(_font->max_advance_width*_scale);
}
static const ushort char_table[] = {
40,
67,
70,
75,
86,
88,
89,
91,
102,
114,
124,
127,
205,
645,
884,
922,
1070,
12386
};
static const int char_table_entries = sizeof(char_table)/sizeof(ushort);
int TQFontEngineXft::minLeftBearing() const
{
if ( lbearing == SHRT_MIN )
minRightBearing(); // calculates both
return lbearing;
}
int TQFontEngineXft::minRightBearing() const
{
if ( rbearing == SHRT_MIN ) {
TQFontEngineXft *that = (TQFontEngineXft *)this;
that->lbearing = that->rbearing = 0;
TQChar *ch = (TQChar *)char_table;
glyph_t glyphs[char_table_entries];
int ng = char_table_entries;
stringToCMap(ch, char_table_entries, glyphs, 0, &ng, FALSE);
while (--ng) {
if (glyphs[ng]) {
glyph_metrics_t gi = that->boundingBox( glyphs[ng] );
if (gi.xoff) {
that->lbearing = TQMIN(lbearing, gi.x);
that->rbearing = TQMIN(rbearing, gi.xoff - gi.x - gi.width);
}
}
}
}
return rbearing;
}
int TQFontEngineXft::cmap() const
{
return _cmap;
}
const char *TQFontEngineXft::name() const
{
return "xft";
}
void TQFontEngineXft::setScale( double scale )
{
_scale = scale;
}
bool TQFontEngineXft::canRender( const TQChar *string, int len )
{
bool allExist = TRUE;
#ifdef TQT_XFT2
if (_cmap != -1) {
for ( int i = 0; i < len; i++ ) {
if (!XftCharExists(0, _font, string[i].tqunicode())
&& getAdobeCharIndex(_font, _cmap, string[i].tqunicode()) == 0) {
allExist = FALSE;
break;
}
}
} else {
for ( int i = 0; i < len; i++ ) {
if (!XftCharExists(0, _font, string[i].tqunicode())) {
allExist = FALSE;
break;
}
}
}
#else
glyph_t glyphs[256];
int nglyphs = 255;
glyph_t *g = glyphs;
if ( stringToCMap( string, len, g, 0, &nglyphs, FALSE ) == OutOfMemory ) {
g = (glyph_t *)malloc( nglyphs*sizeof(glyph_t) );
stringToCMap( string, len, g, 0, &nglyphs, FALSE );
}
for ( int i = 0; i < nglyphs; i++ ) {
if ( !XftGlyphExists(TQPaintDevice::x11AppDisplay(), _font, g[i]) ) {
allExist = FALSE;
break;
}
}
if ( g != glyphs )
free( g );
#endif // TQT_XFT2
return allExist;
}
TQOpenType *TQFontEngineXft::openType() const
{
// qDebug("openTypeIface requested!");
if ( _openType )
return _openType;
if ( !_face || ! FT_IS_SFNT( _face ) )
return 0;
TQFontEngineXft *that = (TQFontEngineXft *)this;
that->_openType = new TQOpenType(that);
return _openType;
}
TQFontEngine::Type TQFontEngineXft::type() const
{
return Xft;
}
#endif
// --------------------------------------------------------------------------------------------------------------------
// Open type support
// --------------------------------------------------------------------------------------------------------------------
#ifndef TQT_NO_XFTFREETYPE
#include "tqscriptengine_p.h"
//#define OT_DEBUG
#ifdef OT_DEBUG
static inline char *tag_to_string(FT_ULong tag)
{
static char string[5];
string[0] = (tag >> 24)&0xff;
string[1] = (tag >> 16)&0xff;
string[2] = (tag >> 8)&0xff;
string[3] = tag&0xff;
string[4] = 0;
return string;
}
#endif
#define DefaultLangSys 0xffff
#define DefaultScript FT_MAKE_TAG('D', 'F', 'L', 'T')
enum {
RequiresGsub = 1,
RequiresGpos = 2
};
struct OTScripts {
unsigned int tag;
int flags;
};
static const OTScripts ot_scripts [] = {
// // European Alphabetic Scripts
// Latin,
{ FT_MAKE_TAG( 'l', 'a', 't', 'n' ), 0 },
// Greek,
{ FT_MAKE_TAG( 'g', 'r', 'e', 'k' ), 0 },
// Cyrillic,
{ FT_MAKE_TAG( 'c', 'y', 'r', 'l' ), 0 },
// Armenian,
{ FT_MAKE_TAG( 'a', 'r', 'm', 'n' ), 0 },
// Georgian,
{ FT_MAKE_TAG( 'g', 'e', 'o', 'r' ), 0 },
// Runic,
{ FT_MAKE_TAG( 'r', 'u', 'n', 'r' ), 0 },
// Ogham,
{ FT_MAKE_TAG( 'o', 'g', 'a', 'm' ), 0 },
// SpacingModifiers,
{ FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 },
// CombiningMarks,
{ FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 },
// // Middle Eastern Scripts
// Hebrew,
{ FT_MAKE_TAG( 'h', 'e', 'b', 'r' ), 1 },
// Arabic,
{ FT_MAKE_TAG( 'a', 'r', 'a', 'b' ), 1 },
// Syriac,
{ FT_MAKE_TAG( 's', 'y', 'r', 'c' ), 1 },
// Thaana,
{ FT_MAKE_TAG( 't', 'h', 'a', 'a' ), 1 },
// // South and Southeast Asian Scripts
// Devanagari,
{ FT_MAKE_TAG( 'd', 'e', 'v', 'a' ), 1 },
// Bengali,
{ FT_MAKE_TAG( 'b', 'e', 'n', 'g' ), 1 },
// Gurmukhi,
{ FT_MAKE_TAG( 'g', 'u', 'r', 'u' ), 1 },
// Gujarati,
{ FT_MAKE_TAG( 'g', 'u', 'j', 'r' ), 1 },
// Oriya,
{ FT_MAKE_TAG( 'o', 'r', 'y', 'a' ), 1 },
// Tamil,
{ FT_MAKE_TAG( 't', 'a', 'm', 'l' ), 1 },
// Telugu,
{ FT_MAKE_TAG( 't', 'e', 'l', 'u' ), 1 },
// Kannada,
{ FT_MAKE_TAG( 'k', 'n', 'd', 'a' ), 1 },
// Malayalam,
{ FT_MAKE_TAG( 'm', 'l', 'y', 'm' ), 1 },
// Sinhala,
// ### could not tqfind any OT specs on this
{ FT_MAKE_TAG( 's', 'i', 'n', 'h' ), 1 },
// Thai,
{ FT_MAKE_TAG( 't', 'h', 'a', 'i' ), 1 },
// Lao,
{ FT_MAKE_TAG( 'l', 'a', 'o', ' ' ), 1 },
// Tibetan,
{ FT_MAKE_TAG( 't', 'i', 'b', 't' ), 1 },
// Myanmar,
{ FT_MAKE_TAG( 'm', 'y', 'm', 'r' ), 1 },
// Khmer,
{ FT_MAKE_TAG( 'k', 'h', 'm', 'r' ), 1 },
// // East Asian Scripts
// Han,
{ FT_MAKE_TAG( 'h', 'a', 'n', 'i' ), 0 },
// Hiragana,
{ FT_MAKE_TAG( 'k', 'a', 'n', 'a' ), 0 },
// Katakana,
{ FT_MAKE_TAG( 'k', 'a', 'n', 'a' ), 0 },
// Hangul,
{ FT_MAKE_TAG( 'h', 'a', 'n', 'g' ), 1 },
// Bopomofo,
{ FT_MAKE_TAG( 'b', 'o', 'p', 'o' ), 0 },
// Yi,
{ FT_MAKE_TAG( 'y', 'i', ' ', ' ' ), 0 },
// // Additional Scripts
// Ethiopic,
{ FT_MAKE_TAG( 'e', 't', 'h', 'i' ), 0 },
// Cherokee,
{ FT_MAKE_TAG( 'c', 'h', 'e', 'r' ), 0 },
// CanadianAboriginal,
{ FT_MAKE_TAG( 'c', 'a', 'n', 's' ), 0 },
// Mongolian,
{ FT_MAKE_TAG( 'm', 'o', 'n', 'g' ), 0 },
// // Symbols
// CurrencySymbols,
{ FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 },
// LetterlikeSymbols,
{ FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 },
// NumberForms,
{ FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 },
// MathematicalOperators,
{ FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 },
// TechnicalSymbols,
{ FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 },
// GeometricSymbols,
{ FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 },
// MiscellaneousSymbols,
{ FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 },
// EnclosedAndSquare,
{ FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 },
// Braille,
{ FT_MAKE_TAG( 'b', 'r', 'a', 'i' ), 0 },
// Unicode, should be used
{ FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 }
// ### where are these?
// { FT_MAKE_TAG( 'b', 'y', 'z', 'm' ), 0 },
// { FT_MAKE_TAG( 'D', 'F', 'L', 'T' ), 0 },
// ### Hangul Jamo
// { FT_MAKE_TAG( 'j', 'a', 'm', 'o' ), 0 },
};
TQOpenType::TQOpenType(TQFontEngineXft *fe)
: fontEngine(fe), gdef(0), gsub(0), gpos(0), current_script(0)
{
face = fe->face();
otl_buffer_new(face->memory, &otl_buffer);
tmpAttributes = 0;
tmpLogClusters = 0;
FT_Error error;
if ((error = TT_Load_GDEF_Table(face, &gdef))) {
#ifdef OT_DEBUG
qDebug("error loading gdef table: %d", error);
#endif
gdef = 0;
}
if ((error = TT_Load_GSUB_Table(face, &gsub, gdef))) {
gsub = 0;
#ifdef OT_DEBUG
if (error != FT_Err_Table_Missing) {
qDebug("error loading gsub table: %d", error);
} else {
qDebug("face doesn't have a gsub table");
}
#endif
}
if ((error = TT_Load_GPOS_Table(face, &gpos, gdef))) {
gpos = 0;
#ifdef OT_DEBUG
qDebug("error loading gpos table: %d", error);
#endif
}
for (uint i = 0; i < TQFont::NScripts; ++i)
supported_scripts[i] = checkScript(i);
}
TQOpenType::~TQOpenType()
{
if (gpos)
TT_Done_GPOS_Table(gpos);
if (gsub)
TT_Done_GSUB_Table(gsub);
if (gdef)
TT_Done_GDEF_Table(gdef);
if (otl_buffer)
otl_buffer_free(otl_buffer);
if (tmpAttributes)
free(tmpAttributes);
if (tmpLogClusters)
free(tmpLogClusters);
}
bool TQOpenType::checkScript(unsigned int script)
{
assert(script < TQFont::NScripts);
uint tag = ot_scripts[script].tag;
int requirements = ot_scripts[script].flags;
if (requirements & RequiresGsub) {
if (!gsub)
return FALSE;
FT_UShort script_index;
FT_Error error = TT_GSUB_Select_Script(gsub, tag, &script_index);
if (error) {
#ifdef OT_DEBUG
qDebug("could not select script %d in GSub table: %d", (int)script, error);
#endif
return FALSE;
}
}
if (requirements & RequiresGpos) {
if (!gpos)
return FALSE;
FT_UShort script_index;
FT_Error error = TT_GPOS_Select_Script(gpos, script, &script_index);
if (error) {
#ifdef OT_DEBUG
qDebug("could not select script in gpos table: %d", error);
#endif
return FALSE;
}
}
return TRUE;
}
void TQOpenType::selectScript(unsigned int script, const Features *features)
{
if (current_script == script)
return;
assert(script < TQFont::NScripts);
// tqfind script in our list of supported scripts.
uint tag = ot_scripts[script].tag;
if (gsub && features) {
#ifdef OT_DEBUG
{
TTO_FeatureList featurelist = gsub->FeatureList;
int numfeatures = featurelist.FeatureCount;
qDebug("gsub table has %d features", numfeatures);
for(int i = 0; i < numfeatures; i++) {
TTO_FeatureRecord *r = featurelist.FeatureRecord + i;
qDebug(" feature '%s'", tag_to_string(r->FeatureTag));
}
}
#endif
TT_GSUB_Clear_Features(gsub);
FT_UShort script_index;
FT_Error error = TT_GSUB_Select_Script(gsub, tag, &script_index);
if (!error) {
#ifdef OT_DEBUG
qDebug("script %s has script index %d", tag_to_string(script), script_index);
#endif
while (features->tag) {
FT_UShort feature_index;
error = TT_GSUB_Select_Feature(gsub, features->tag, script_index, 0xffff, &feature_index);
if (!error) {
#ifdef OT_DEBUG
qDebug(" adding feature %s", tag_to_string(features->tag));
#endif
TT_GSUB_Add_Feature(gsub, feature_index, features->property);
}
++features;
}
}
}
if (gpos) {
TT_GPOS_Clear_Features(gpos);
FT_UShort script_index;
FT_Error error = TT_GPOS_Select_Script(gpos, tag, &script_index);
if (!error) {
#ifdef OT_DEBUG
{
TTO_FeatureList featurelist = gpos->FeatureList;
int numfeatures = featurelist.FeatureCount;
qDebug("gpos table has %d features", numfeatures);
for(int i = 0; i < numfeatures; i++) {
TTO_FeatureRecord *r = featurelist.FeatureRecord + i;
FT_UShort feature_index;
TT_GPOS_Select_Feature(gpos, r->FeatureTag, script_index, 0xffff, &feature_index);
qDebug(" feature '%s'", tag_to_string(r->FeatureTag));
}
}
#endif
FT_ULong *feature_tag_list;
error = TT_GPOS_Query_Features(gpos, script_index, 0xffff, &feature_tag_list);
if (!error) {
while (*feature_tag_list) {
FT_UShort feature_index;
error = TT_GPOS_Select_Feature(gpos, *feature_tag_list, script_index, 0xffff, &feature_index);
if (!error)
TT_GPOS_Add_Feature(gpos, feature_index, PositioningProperties);
++feature_tag_list;
}
}
}
}
current_script = script;
}
#ifdef OT_DEBUG
static void dump_string(OTL_Buffer buffer)
{
for (uint i = 0; i < buffer->in_length; ++i) {
qDebug(" %x: cluster=%d", buffer->in_string[i].gindex, buffer->in_string[i].cluster);
}
}
#endif
extern void qt_heuristicPosition(TQShaperItem *item);
bool TQOpenType::tqshape(TQShaperItem *item, const unsigned int *properties)
{
length = item->num_glyphs;
otl_buffer_clear(otl_buffer);
tmpAttributes = (GlyphAttributes *) realloc(tmpAttributes, length*sizeof(GlyphAttributes));
tmpLogClusters = (unsigned int *) realloc(tmpLogClusters, length*sizeof(unsigned int));
for (int i = 0; i < length; ++i) {
otl_buffer_add_glyph(otl_buffer, item->glyphs[i], properties ? properties[i] : 0, i);
tmpAttributes[i] = item->attributes[i];
tmpLogClusters[i] = item->log_clusters[i];
}
#ifdef OT_DEBUG
qDebug("-----------------------------------------");
// qDebug("log clusters before shaping:");
// for (int j = 0; j < length; j++)
// qDebug(" log[%d] = %d", j, item->log_clusters[j]);
qDebug("original glyphs: %p", item->glyphs);
for (int i = 0; i < length; ++i)
qDebug(" glyph=%4x", otl_buffer->in_string[i].gindex);
// dump_string(otl_buffer);
#endif
loadFlags = FT_LOAD_DEFAULT;
if (gsub) {
uint error = TT_GSUB_Apply_String(gsub, otl_buffer);
if (error && error != TTO_Err_Not_Covered)
return false;
}
#ifdef OT_DEBUG
// qDebug("log clusters before shaping:");
// for (int j = 0; j < length; j++)
// qDebug(" log[%d] = %d", j, item->log_clusters[j]);
qDebug("tqshaped glyphs:");
for (int i = 0; i < length; ++i)
qDebug(" glyph=%4x", otl_buffer->in_string[i].gindex);
qDebug("-----------------------------------------");
// dump_string(otl_buffer);
#endif
return true;
}
bool TQOpenType::positionAndAdd(TQShaperItem *item, bool doLogClusters)
{
if (gpos) {
#ifdef TQ_WS_X11
TQ_ASSERT(fontEngine->type() == TQFontEngine::Xft);
face = lockFTFace(static_cast<TQFontEngineXft *>(fontEngine)->font());
#endif
memset(otl_buffer->positions, 0, otl_buffer->in_length*sizeof(OTL_PositionRec));
// #### check that passing "FALSE,FALSE" is correct
TT_GPOS_Apply_String(face, gpos, loadFlags, otl_buffer, FALSE, FALSE);
#ifdef TQ_WS_X11
unlockFTFace(static_cast<TQFontEngineXft *>(fontEngine)->font());
#endif
}
// make sure we have enough space to write everything back
if (item->num_glyphs < (int)otl_buffer->in_length) {
item->num_glyphs = otl_buffer->in_length;
return FALSE;
}
for (unsigned int i = 0; i < otl_buffer->in_length; ++i) {
item->glyphs[i] = otl_buffer->in_string[i].gindex;
item->attributes[i] = tmpAttributes[otl_buffer->in_string[i].cluster];
if (i && otl_buffer->in_string[i].cluster == otl_buffer->in_string[i-1].cluster)
item->attributes[i].clusterStart = FALSE;
}
item->num_glyphs = otl_buffer->in_length;
if (doLogClusters) {
// we can't do this for indic, as we pass the stuf in syllables and it's easier to do it in the tqshaper.
unsigned short *logClusters = item->log_clusters;
int clusterStart = 0;
int oldCi = 0;
for (unsigned int i = 0; i < otl_buffer->in_length; ++i) {
int ci = otl_buffer->in_string[i].cluster;
// qDebug(" ci[%d] = %d mark=%d, cmb=%d, cs=%d",
// i, ci, glyphAttributes[i].mark, glyphAttributes[i].combiningClass, glyphAttributes[i].clusterStart);
if (!item->attributes[i].mark && item->attributes[i].clusterStart && ci != oldCi) {
for (int j = oldCi; j < ci; j++)
logClusters[j] = clusterStart;
clusterStart = i;
oldCi = ci;
}
}
for (int j = oldCi; j < length; j++)
logClusters[j] = clusterStart;
}
// calulate the advances for the tqshaped glyphs
// qDebug("unpositioned: ");
static_cast<TQFontEngineXft *>(item->font)->recalcAdvances(item->num_glyphs, item->glyphs, item->advances);
// positioning code:
if (gpos) {
float scale = item->font->scale();
OTL_Position positions = otl_buffer->positions;
// qDebug("positioned glyphs:");
for (unsigned int i = 0; i < otl_buffer->in_length; i++) {
// qDebug(" %d:\t orig advance: (%d/%d)\tadv=(%d/%d)\tpos=(%d/%d)\tback=%d\tnew_advance=%d", i,
// glyphs[i].advance.x.toInt(), glyphs[i].advance.y.toInt(),
// (int)(positions[i].x_advance >> 6), (int)(positions[i].y_advance >> 6),
// (int)(positions[i].x_pos >> 6), (int)(positions[i].y_pos >> 6),
// positions[i].back, positions[i].new_advance);
// ###### fix the case where we have y advances. How do we handle this in Uniscribe?????
if (positions[i].new_advance) {
item->advances[i] = item->flags & TQTextEngine::RightToLeft
? -tqRound((positions[i].x_advance >> 6)*scale)
: tqRound((positions[i].x_advance >> 6)*scale);
} else {
item->advances[i] += item->flags & TQTextEngine::RightToLeft
? -tqRound((positions[i].x_advance >> 6)*scale)
: tqRound((positions[i].x_advance >> 6)*scale);
}
int back = 0;
item->offsets[i].x = tqRound((positions[i].x_pos >> 6)*scale);
item->offsets[i].y = tqRound((positions[i].y_pos >> 6)*scale);
while (positions[i-back].back) {
back += positions[i - back].back;
item->offsets[i].x += tqRound((positions[i - back].x_pos >> 6)*scale);
item->offsets[i].y += tqRound((positions[i - back].y_pos >> 6)*scale);
}
item->offsets[i].y = -item->offsets[i].y;
back = positions[i].back;
if (item->flags & TQTextEngine::RightToLeft) {
while (back--) {
item->offsets[i].x -= item->advances[i-back];
}
} else {
while (back) {
item->offsets[i].x -= item->advances[i-back];
--back;
}
}
// qDebug(" ->\tadv=%d\tpos=(%d/%d)",
// glyphs[i].advance.x.toInt(), glyphs[i].offset.x.toInt(), glyphs[i].offset.y.toInt());
}
item->has_positioning = TRUE;
} else {
qt_heuristicPosition(item);
}
#ifdef OT_DEBUG
// if (doLogClusters) {
// qDebug("log clusters after shaping:");
// for (int j = 0; j < length; j++)
// qDebug(" log[%d] = %d", j, item->log_clusters[j]);
// }
qDebug("final glyphs:");
for (int i = 0; i < (int)otl_buffer->in_length; ++i)
qDebug(" glyph=%4x char_index=%d mark: %d cmp: %d, clusterStart: %d advance=%d offset=%d/%d",
item->glyphs[i], otl_buffer->in_string[i].cluster, item->attributes[i].mark,
item->attributes[i].combiningClass, item->attributes[i].clusterStart,
item->advances[i],
item->offsets[i].x, item->offsets[i].y);
qDebug("-----------------------------------------");
#endif
return TRUE;
}
#endif
#endif // USE_QT4
|