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
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
|
//*****************************************************************************
//
// string.c - Routines for drawing text.
//
// Copyright (c) 2007-2014 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 2.1.0.12573 of the Tiva Graphics Library.
//
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "grlib/grlib.h"
//*****************************************************************************
//
//! \addtogroup primitives_api
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
// The character printed by GrStringDraw in place of any character in the
// string which does not appear in the font. When using a font which does not
// include this character, a space is left instead.
//
//*****************************************************************************
#define ABSENT_CHAR_REPLACEMENT '.'
//*****************************************************************************
//
//! Determines the width of a string.
//!
//! \param pContext is a pointer to the drawing context to use.
//! \param pcString is the string in question.
//! \param i32Length is the length of the string.
//!
//! This function determines the width of a string (or portion of the string)
//! when drawn with a particular font. The \e i32Length parameter allows a
//! portion of the string to be examined without having to insert a NULL
//! character at the stopping point (would not be possible if the string was
//! located in flash); specifying a length of -1 will cause the width of the
//! entire string to be computed.
//!
//! \return Returns the width of the string in pixels.
//
//*****************************************************************************
#ifdef GRLIB_REMOVE_WIDE_FONT_SUPPORT
int32_t
GrStringWidthGet(const tContext *pContext, const char *pcString,
int32_t i32Length)
{
const uint16_t *pui16Offset;
const uint8_t *pui8Data;
uint8_t ui8First, ui8Last, ui8Absent;
int32_t i32Width;
//
// Check the arguments.
//
ASSERT(pContext);
ASSERT(pcString);
//
// This function doesn't support wide character fonts or wrapped fonts.
//
ASSERT(!(pContext->psFont->ui8Format &&
(FONT_FMT_WRAPPED | FONT_WIDE_MARKER)));
//
// Get some pointers to relevant information in the font to make things
// easier, and give the compiler a hint about extraneous loads that it can
// avoid.
//
if(pContext->psFont->ui8Format & FONT_EX_MARKER)
{
tFontEx *psFont;
psFont = (tFontEx *)(pContext->psFont);
pui8Data = psFont->pui8Data;
pui16Offset = psFont->pui16Offset;
ui8First = psFont->ui8First;
ui8Last = psFont->ui8Last;
//
// Does the default absent character replacement exist in the font?
//
if((ABSENT_CHAR_REPLACEMENT >= ui8First) &&
(ABSENT_CHAR_REPLACEMENT <= ui8Last))
{
//
// Yes - use the standard character when an absent character is
// found.
//
ui8Absent = ABSENT_CHAR_REPLACEMENT;
}
else
{
//
// The default absent character is not present in the font so use
// the first character (we only use its width here) instead.
//
ui8Absent = psFont->ui8First;
}
}
else
{
pui8Data = pContext->psFont->pui8Data;
pui16Offset = pContext->psFont->pui16Offset;
ui8First = 32;
ui8Last = 126;
ui8Absent = ABSENT_CHAR_REPLACEMENT;
}
//
// Loop through the characters in the string.
//
for(i32Width = 0; *pcString && i32Length; pcString++, i32Length--)
{
//
// Get a pointer to the font data for the next character from the
// string. If there is not a glyph for the next character, replace it
// with a ".".
//
if((*pcString >= ui8First) && (*pcString <= ui8Last))
{
//
// Add the width of this character as drawn with the given font.
//
i32Width += pui8Data[pui16Offset[*pcString - ui8First] + 1];
}
else
{
//
// This character does not exist in the font so replace it with
// a '.' instead. This matches the approach taken in GrStringDraw
// and ensures that the width returned here represents the
// rendered dimension of the string.
//
i32Width += pui8Data[pui16Offset[ui8Absent - ui8First] + 1];
}
}
//
// Return the width of the string.
//
return(i32Width);
}
#else
int32_t
GrStringWidthGet(const tContext *pContext, const char *pcString,
int32_t i32Length)
{
const uint8_t *pui8Data;
uint8_t ui8Width, ui8Height, ui8Baseline, ui8Format;
uint32_t ui32Count, ui32Char, ui32Skip;
int32_t i32Width;
//
// Check the arguments.
//
ASSERT(pContext);
ASSERT(pcString);
//
// Initialize our string length.
//
i32Width = 0;
//
// Set the maximum number of characters we should render. Note that the
// value -1 is used to indicate that the function should render until it
// hits the end of the string so casting it to a uint32_t here is
// fine since this says keep rendering for 2^32 characters. We are very
// unlikely to ever be passed a string this long.
//
ui32Count = (uint32_t)i32Length;
//
// Loop through each character in the string.
//
while(ui32Count)
{
//
// Get the next character to render.
//
ui32Char = GrStringNextCharGet(pContext, pcString, ui32Count,
&ui32Skip);
//
// If we ran out of characters to render, drop out of the loop.
//
if(!ui32Char)
{
break;
}
//
// Get information on this glyph.
//
pui8Data = GrFontGlyphDataGet(pContext->psFont, ui32Char, &ui8Width);
//
// Does the glyph exist?
//
if(!pui8Data)
{
//
// No - get the absent character replacement information.
//
pui8Data = GrFontGlyphDataGet(pContext->psFont,
ABSENT_CHAR_REPLACEMENT, &ui8Width);
//
// Does this character exist in the font?
//
if(!pui8Data)
{
//
// No - look for the ASCII/Unicode space character.
//
pui8Data = GrFontGlyphDataGet(pContext->psFont, 0x20,
&ui8Width);
//
// Does this exist?
//
if(!pui8Data)
{
//
// No - give up and just pad with a character cell of space.
//
GrFontInfoGet(pContext->psFont, &ui8Format, &ui8Width,
&ui8Height, &ui8Baseline);
}
}
}
//
// Increment our string length.
//
i32Width += (int32_t)ui8Width;
//
// Move on to the next character.
//
pcString += ui32Skip;
ui32Count -= ui32Skip;
}
//
// Return the width of the string.
//
return(i32Width);
}
#endif
//*****************************************************************************
//
//! Draws a string.
//!
//! \param pContext is a pointer to the drawing context to use.
//! \param pcString is a pointer to the string to be drawn.
//! \param i32Length is the number of characters from the string that should be
//! drawn on the screen.
//! \param i32X is the X coordinate of the upper left corner of the string
//! position on the screen.
//! \param i32Y is the Y coordinate of the upper left corner of the string
//! position on the screen.
//! \param bOpaque is true of the background of each character should be drawn
//! and false if it should not (leaving the background as is).
//!
//! This function draws a string of text on the screen. The \e i32Length
//! parameter allows a portion of the string to be examined without having to
//! insert a NULL character at the stopping point (which would not be possible
//! if the string was located in flash); specifying a length of -1 will cause
//! the entire string to be rendered (subject to clipping).
//!
//! \return None.
//
//*****************************************************************************
#ifdef GRLIB_REMOVE_WIDE_FONT_SUPPORT
//
// This version of GrStringDraw supports the original tFont and tFontEx ASCII
// and ISO8859 (8 bit) fonts only.
//
void
GrStringDraw(const tContext *pContext, const char *pcString, int32_t i32Length,
int32_t i32X, int32_t i32Y, uint32_t bOpaque)
{
int32_t i32Idx, i32X0, i32Y0, i32Count, i32Off, i32On, i32Bit;
const uint8_t *pui8Data;
const uint8_t *pui8Glyphs;
const uint16_t *pui16Offset;
uint8_t ui8First, ui8Last, ui8Absent;
tContext i16Con;
//
// Check the arguments.
//
ASSERT(pContext);
ASSERT(pcString);
//
// This function doesn't support wide character fonts or wrapped fonts.
//
ASSERT(!(pContext->psFont->ui8Format &&
(FONT_FMT_WRAPPED | FONT_WIDE_MARKER)));
//
// Copy the drawing context into a local structure that can be modified.
//
i16Con = *pContext;
//
// Extract various parameters from the font depending upon whether it's
// in the tFont or tFontEx format.
//
if(pContext->psFont->ui8Format & FONT_EX_MARKER)
{
tFontEx *psFont;
psFont = (tFontEx *)(pContext->psFont);
pui8Glyphs = psFont->pui8Data;
pui16Offset = psFont->pui16Offset;
ui8First = psFont->ui8First;
ui8Last = psFont->ui8Last;
//
// Does the default absent character replacement exist in the font?
//
if((ABSENT_CHAR_REPLACEMENT >= ui8First) &&
(ABSENT_CHAR_REPLACEMENT <= ui8Last))
{
//
// Yes - use the standard character when an absent character is
// found.
//
ui8Absent = ABSENT_CHAR_REPLACEMENT;
}
else
{
//
// The default absent character is not present in the font so use
// the first character instead.
//
ui8Absent = psFont->ui8First;
}
}
else
{
pui8Glyphs = pContext->psFont->pui8Data;
pui16Offset = pContext->psFont->pui16Offset;
ui8First = 32;
ui8Last = 126;
ui8Absent = ABSENT_CHAR_REPLACEMENT;
}
//
// Loop through the characters in the string.
//
while(*pcString && i32Length--)
{
//
// Stop drawing the string if the right edge of the clipping region has
// been exceeded.
//
if(i32X > i16Con.sClipRegion.i16XMax)
{
break;
}
//
// Get a pointer to the font data for the next character from the
// string. If there is not a glyph for the next character, replace it
// with the "absent" character (usually '.').
//
if((*pcString >= ui8First) && (*pcString <= ui8Last))
{
pui8Data = (pui8Glyphs + pui16Offset[*pcString - ui8First]);
}
else
{
pui8Data = (pui8Glyphs + pui16Offset[ui8Absent - ui8First]);
}
pcString++;
//
// See if the entire character is to the left of the clipping region.
//
if((i32X + pui8Data[1]) < i16Con.sClipRegion.i16XMin)
{
//
// Increment the X coordinate by the width of the character.
//
i32X += pui8Data[1];
//
// Go to the next character in the string.
//
continue;
}
//
// Loop through the bytes in the encoded data for this glyph.
//
for(i32Idx = 2, i32X0 = 0, i32Bit = 0, i32Y0 = 0;
i32Idx < pui8Data[0]; )
{
//
// See if the bottom of the clipping region has been exceeded.
//
if((i32Y + i32Y0) > i16Con.sClipRegion.i16YMax)
{
//
// Stop drawing this character.
//
break;
}
//
// See if the font is uncompressed.
//
if((i16Con.psFont->ui8Format & ~FONT_EX_MARKER) ==
FONT_FMT_UNCOMPRESSED)
{
//
// Count the number of off pixels from this position in the
// glyph image.
//
for(i32Off = 0; i32Idx < pui8Data[0]; )
{
//
// Get the number of zero pixels at this position.
//
i32Count = NumLeadingZeros(pui8Data[i32Idx] <<
(24 + i32Bit));
//
// If there were more than 8, then it is a "false" result
// since it counted beyond the end of the current byte.
// Therefore, simply limit it to the number of pixels
// remaining in this byte.
//
if(i32Count > 8)
{
i32Count = 8 - i32Bit;
}
//
// Increment the number of off pixels.
//
i32Off += i32Count;
//
// Increment the bit position within the byte.
//
i32Bit += i32Count;
//
// See if the end of the byte has been reached.
//
if(i32Bit == 8)
{
//
// Advance to the next byte and continue counting off
// pixels.
//
i32Bit = 0;
i32Idx++;
}
else
{
//
// Since the end of the byte was not reached, there
// must be an on pixel. Therefore, stop counting off
// pixels.
//
break;
}
}
//
// Count the number of on pixels from this position in the
// glyph image.
//
for(i32On = 0; i32Idx < pui8Data[0]; )
{
//
// Get the number of one pixels at this location (by
// inverting the data and counting the number of zeros).
//
i32Count = NumLeadingZeros(~(pui8Data[i32Idx] <<
(24 + i32Bit)));
//
// If there were more than 8, then it is a "false" result
// since it counted beyond the end of the current byte.
// Therefore, simply limit it to the number of pixels
// remaining in this byte.
//
if(i32Count > 8)
{
i32Count = 8 - i32Bit;
}
//
// Increment the number of on pixels.
//
i32On += i32Count;
//
// Increment the bit position within the byte.
//
i32Bit += i32Count;
//
// See if the end of the byte has been reached.
//
if(i32Bit == 8)
{
//
// Advance to the next byte and continue counting on
// pixels.
//
i32Bit = 0;
i32Idx++;
}
else
{
//
// Since the end of the byte was not reached, there
// must be an off pixel. Therefore, stop counting on
// pixels.
//
break;
}
}
}
//
// Otherwise, the font is compressed with a pixel RLE scheme.
//
else
{
//
// See if this is a byte that encodes some on and off pixels.
//
if(pui8Data[i32Idx])
{
//
// Extract the number of off pixels.
//
i32Off = (pui8Data[i32Idx] >> 4) & 15;
//
// Extract the number of on pixels.
//
i32On = pui8Data[i32Idx] & 15;
//
// Skip past this encoded byte.
//
i32Idx++;
}
//
// Otherwise, see if this is a repeated on pixel byte.
//
else if(pui8Data[i32Idx + 1] & 0x80)
{
//
// There are no off pixels in this encoding.
//
i32Off = 0;
//
// Extract the number of on pixels.
//
i32On = (pui8Data[i32Idx + 1] & 0x7f) * 8;
//
// Skip past these two encoded bytes.
//
i32Idx += 2;
}
//
// Otherwise, this is a repeated off pixel byte.
//
else
{
//
// Extract the number of off pixels.
//
i32Off = pui8Data[i32Idx + 1] * 8;
//
// There are no on pixels in this encoding.
//
i32On = 0;
//
// Skip past these two encoded bytes.
//
i32Idx += 2;
}
}
//
// Loop while there are any off pixels.
//
while(i32Off)
{
//
// See if the bottom of the clipping region has been exceeded.
//
if((i32Y + i32Y0) > i16Con.sClipRegion.i16YMax)
{
//
// Ignore the remainder of the on pixels.
//
break;
}
//
// See if there is more than one on pixel that will fit onto
// the current row.
//
if((i32Off > 1) && ((i32X0 + 1) < pui8Data[1]))
{
//
// Determine the number of on pixels that will fit on this
// row.
//
i32Count = (((i32X0 + i32Off) > pui8Data[1]) ?
pui8Data[1] - i32X0 : i32Off);
//
// If this row is within the clipping region, draw a
// horizontal line that corresponds to the sequence of on
// pixels.
//
if(((i32Y + i32Y0) >= i16Con.sClipRegion.i16YMin) &&
bOpaque)
{
i16Con.ui32Foreground = pContext->ui32Background;
GrLineDrawH(&i16Con, i32X + i32X0, i32X + i32X0 +
i32Count - 1, i32Y + i32Y0);
}
//
// Decrement the count of on pixels by the number on this
// row.
//
i32Off -= i32Count;
//
// Increment the X offset by the number of on pixels.
//
i32X0 += i32Count;
}
//
// Otherwise, there is only a single on pixel that can be
// drawn.
//
else
{
//
// If this pixel is within the clipping region, then draw
// it.
//
if(((i32X + i32X0) >= i16Con.sClipRegion.i16XMin) &&
((i32X + i32X0) <= i16Con.sClipRegion.i16XMax) &&
((i32Y + i32Y0) >= i16Con.sClipRegion.i16YMin) &&
bOpaque)
{
DpyPixelDraw(pContext->psDisplay, i32X + i32X0,
i32Y + i32Y0, pContext->ui32Background);
}
//
// Decrement the count of on pixels.
//
i32Off--;
//
// Increment the X offset.
//
i32X0++;
}
//
// See if the X offset has reached the right side of the
// character glyph.
//
if(i32X0 == pui8Data[1])
{
//
// Increment the Y offset.
//
i32Y0++;
//
// Reset the X offset to the left side of the character
// glyph.
//
i32X0 = 0;
}
}
//
// Loop while there are any on pixels.
//
while(i32On)
{
//
// See if the bottom of the clipping region has been exceeded.
//
if((i32Y + i32Y0) > i16Con.sClipRegion.i16YMax)
{
//
// Ignore the remainder of the on pixels.
//
break;
}
//
// See if there is more than one on pixel that will fit onto
// the current row.
//
if((i32On > 1) && ((i32X0 + 1) < pui8Data[1]))
{
//
// Determine the number of on pixels that will fit on this
// row.
//
i32Count = (((i32X0 + i32On) > pui8Data[1]) ?
pui8Data[1] - i32X0 : i32On);
//
// If this row is within the clipping region, draw a
// horizontal line that corresponds to the sequence of on
// pixels.
//
if((i32Y + i32Y0) >= i16Con.sClipRegion.i16YMin)
{
i16Con.ui32Foreground = pContext->ui32Foreground;
GrLineDrawH(&i16Con, i32X + i32X0, i32X + i32X0 +
i32Count - 1, i32Y + i32Y0);
}
//
// Decrement the count of on pixels by the number on this
// row.
//
i32On -= i32Count;
//
// Increment the X offset by the number of on pixels.
//
i32X0 += i32Count;
}
//
// Otherwise, there is only a single on pixel that can be
// drawn.
//
else
{
//
// If this pixel is within the clipping region, then draw
// it.
//
if(((i32X + i32X0) >= i16Con.sClipRegion.i16XMin) &&
((i32X + i32X0) <= i16Con.sClipRegion.i16XMax) &&
((i32Y + i32Y0) >= i16Con.sClipRegion.i16YMin))
{
DpyPixelDraw(pContext->psDisplay, i32X + i32X0,
i32Y + i32Y0, pContext->ui32Foreground);
}
//
// Decrement the count of on pixels.
//
i32On--;
//
// Increment the X offset.
//
i32X0++;
}
//
// See if the X offset has reached the right side of the
// character glyph.
//
if(i32X0 == pui8Data[1])
{
//
// Increment the Y offset.
//
i32Y0++;
//
// Reset the X offset to the left side of the character
// glyph.
//
i32X0 = 0;
}
}
}
//
// Increment the X coordinate by the width of the character.
//
i32X += pui8Data[1];
}
}
#else
//
// This version of GrStringDraw supports the original tFont and tFontEx ASCII
// and ISO8859 (8 bit) fonts along with new wide character set, relocatable
// fonts. Support for source and font codepages is also included.
//
void
GrStringDraw(const tContext *pContext, const char *pcString, int32_t i32Length,
int32_t i32X, int32_t i32Y, uint32_t bOpaque)
{
ASSERT(pContext);
ASSERT(pContext->pfnStringRenderer);
//
// Call the currently registered string rendering function.
//
pContext->pfnStringRenderer(pContext, pcString, i32Length, i32X, i32Y,
bOpaque);
}
//*****************************************************************************
//
//! The default text string rendering function.
//!
//! \param pContext is a pointer to the drawing context to use.
//! \param pcString is a pointer to the string to be drawn.
//! \param i32Length is the number of characters from the string that should be
//! drawn on the screen.
//! \param i32X is the X coordinate of the upper left corner of the string
//! position on the screen.
//! \param i32Y is the Y coordinate of the upper left corner of the string
//! position on the screen.
//! \param bOpaque is true of the background of each character should be drawn
//! and false if it should not (leaving the background as is).
//!
//! This function acts as the default string rendering function called by
//! GrStringDraw() if no language-specific renderer is registered. It draws a
//! string of text on the screen using the text orientation currently set in
//! the graphics context. The \e i32Length parameter allows a portion of the
//! string to be examined without having to insert a NULL character at the
//! stopping point (which would not be possible if the string was located in
//! flash); specifying a length of -1 will cause the entire string to be
//! rendered (subject to clipping).
//!
//! Applications are not expected to call this function directly but should
//! call GrStringDraw() instead. This function is provided as an aid to
//! language-specific renders which may call it to render parts of a string
//! at particular positions after dealing with any language-specific layout
//! issues such as, for example, inserting left-to-right numbers within a
//! right-to-left Arabic string.
//!
//! \return None.
//
//*****************************************************************************
void
GrDefaultStringRenderer(const tContext *pContext, const char *pcString,
int32_t i32Length, int32_t i32X, int32_t i32Y,
bool bOpaque)
{
uint8_t ui8Format, ui8Width, ui8MaxWidth, ui8Height, ui8Baseline;
uint32_t ui32Char, ui32Count, ui32Skip;
const uint8_t *pui8Data;
//
// Check the arguments.
//
ASSERT(pContext);
ASSERT(pcString);
//
// Get information on the font we are rendering the text in.
//
GrFontInfoGet(pContext->psFont, &ui8Format, &ui8MaxWidth, &ui8Height,
&ui8Baseline);
//
// If the string is completely outside the clipping region, don't even
// start rendering it.
//
if((i32Y > pContext->sClipRegion.i16YMax) ||
((i32Y + ui8Height) < pContext->sClipRegion.i16YMin))
{
return;
}
//
// Set the maximum number of characters we should render. Note that the
// value -1 is used to indicate that the function should render until it
// hits the end of the string so casting it to an uint32_t here is
// fine since this says keep rendering for 2^32 characters. We are very
// unlikely to ever be passed a string this long.
//
ui32Count = (uint32_t)i32Length;
//
// Loop through each character in the string.
//
while(ui32Count)
{
//
// Get the next character to render.
//
ui32Char = GrStringNextCharGet(pContext, pcString, ui32Count,
&ui32Skip);
//
// If we ran out of characters to render, return immediately.
//
if(!ui32Char)
{
return;
}
//
// If we are already outside the clipping region, exit early.
//
if(i32X >= pContext->sClipRegion.i16XMax)
{
return;
}
//
// Get the glyph data pointer for this character.
//
pui8Data = GrFontGlyphDataGet(pContext->psFont, ui32Char, &ui8Width);
//
// Does this glyph exist in the font?
//
if(!pui8Data)
{
//
// Look for the character we are supposed to use in place of absent
// glyphs.
//
pui8Data = GrFontGlyphDataGet(pContext->psFont,
ABSENT_CHAR_REPLACEMENT, &ui8Width);
//
// Does this glyph exist in the font?
//
if(!pui8Data)
{
//
// Last chance - look for the space character.
//
pui8Data = GrFontGlyphDataGet(pContext->psFont, ' ',
&ui8Width);
}
}
//
// Did we find something to render?
//
if(pui8Data)
{
GrFontGlyphRender(pContext, pui8Data, i32X, i32Y,
(ui8Format & FONT_FMT_PIXEL_RLE) ? true : false,
bOpaque);
i32X += ui8Width;
}
else
{
//
// Leave a space in place of the undefined glyph.
//
i32X += ui8MaxWidth;
}
//
// Move on to the next character.
//
pcString += ui32Skip;
ui32Count -= ui32Skip;
}
}
//*****************************************************************************
//
//! Returns the codepoint of the first character in a string.
//!
//! \param pContext points to the graphics context in use.
//! \param pcString points to the first byte of the string from which the
//! next character is to be parsed.
//! \param ui32Count provides the number of bytes in the pcString buffer.
//! \param pui32Skip points to storage which will be written with the number of
//! bytes that must be skipped in the string buffer to move past the
//! current character.
//!
//! This function is used to walk through a string extracting one character at
//! a time. The input string is assumed to be encoded using the currently-
//! selected string codepage (as set via a call to the GrStringCodepageSet()
//! function). The value returned is the codepoint of the first character in
//! the string as mapped into the current font's codepage. This may be passed
//! to the GrFontGlyphDataGet() function to retrieve the glyph data for the
//! character.
//!
//! Since variable length encoding schemes such as UTF-8 are supported, this
//! function also returns information on the size of the character that has
//! been parsed, allowing the caller to increment the string pointer by the
//! relevant amount before querying the next character in the string.
//!
//! \return Returns the font codepoint representing the first character in the
//! string or 0 if no valid character was found.
//!
//*****************************************************************************
uint32_t
GrStringNextCharGet(const tContext *pContext, const char *pcString,
uint32_t ui32Count, uint32_t *pui32Skip)
{
ASSERT(pContext);
ASSERT(pcString);
ASSERT(pui32Skip);
//
// If the string is empty, return immediately.
//
if(!ui32Count)
{
return(0);
}
//
// Has a codepage mapping table been registered for this context?
//
if(pContext->pCodePointMapTable)
{
//
// Yes - use the relevant mapping function
//
return(pContext->pCodePointMapTable[pContext->ui8CodePointMap].
pfnMapChar(pcString, ui32Count, pui32Skip));
}
else
{
//
// No codepage mapping table has been registered so fall back on the
// assumption that we are using ASCII or ISO8859-1 for both the
// string and font codepages (i.e. the legacy case).
//
*pui32Skip = 1;
return((uint32_t)*pcString);
}
}
//*****************************************************************************
//
//! Renders a single character glyph on the display at a given position.
//!
//! \param pContext points to the graphics context in use.
//! \param pui8Data points to the first byte of data for the glyph to be
//! rendered.
//! \param i32X is the X coordinate of the top left pixel of the glyph.
//! \param i32Y is the Y coordinate of the top left pixel of the glyph.
//! \param bCompressed is \b true if the data pointed to by \b pui8Data is in
//! compressed format or \b false if uncompressed.
//! \param bOpaque is \b true of background pixels are to be written or \b
//! false if only foreground pixels are drawn.
//!
//! This function is included as an aid to language-specific string rendering
//! functions. Applications are expected to render strings and characters
//! using calls to GrStringDraw or GrStringDrawCentered and should not call
//! this function directly.
//!
//! A string rendering function may call this low level API to place a single
//! character glyph on the display at a particular position. The rendered
//! glyph is subject to the clipping rectangle currently set in the passed
//! graphics context. Rendering colors are also taken from the context
//! structure. Glyph data pointed to by \b pui8Data should be retrieved using
//! a call to GrFontGlyphDataGet().
//!
//! \return None.
//
//*****************************************************************************
void
GrFontGlyphRender(const tContext *pContext, const uint8_t *pui8Data,
int32_t i32X, int32_t i32Y, bool bCompressed,
bool bOpaque)
{
int32_t i32Idx, i32X0, i32Y0, i32Count, i32Off, i32On, i32Bit;
int32_t i32ClipX1, i32ClipX2;
//
// Check the arguments.
//
ASSERT(pContext);
ASSERT(pui8Data);
//
// Stop drawing the string if the right edge of the clipping region has
// been exceeded.
//
if(i32X > pContext->sClipRegion.i16XMax)
{
return;
}
//
// See if the entire character is to the left of the clipping region.
//
if((i32X + pui8Data[1]) < pContext->sClipRegion.i16XMin)
{
return;
}
//
// Loop through the bytes in the encoded data for this glyph.
//
for(i32Idx = 2, i32X0 = 0, i32Bit = 0, i32Y0 = 0; i32Idx < pui8Data[0]; )
{
//
// See if the bottom of the clipping region has been exceeded.
//
if((i32Y + i32Y0) > pContext->sClipRegion.i16YMax)
{
//
// Stop drawing this character.
//
break;
}
//
// See if the font is uncompressed.
//
if(!bCompressed)
{
//
// Count the number of off pixels from this position in the
// glyph image.
//
for(i32Off = 0; i32Idx < pui8Data[0]; )
{
//
// Get the number of zero pixels at this position.
//
i32Count = NumLeadingZeros(pui8Data[i32Idx] << (24 + i32Bit));
//
// If there were more than 8, then it is a "false" result
// since it counted beyond the end of the current byte.
// Therefore, simply limit it to the number of pixels
// remaining in this byte.
//
if(i32Count > 8)
{
i32Count = 8 - i32Bit;
}
//
// Increment the number of off pixels.
//
i32Off += i32Count;
//
// Increment the bit position within the byte.
//
i32Bit += i32Count;
//
// See if the end of the byte has been reached.
//
if(i32Bit == 8)
{
//
// Advance to the next byte and continue counting off
// pixels.
//
i32Bit = 0;
i32Idx++;
}
else
{
//
// Since the end of the byte was not reached, there
// must be an on pixel. Therefore, stop counting off
// pixels.
//
break;
}
}
//
// Count the number of on pixels from this position in the
// glyph image.
//
for(i32On = 0; i32Idx < pui8Data[0]; )
{
//
// Get the number of one pixels at this location (by
// inverting the data and counting the number of zeros).
//
i32Count = NumLeadingZeros(~(pui8Data[i32Idx] <<
(24 + i32Bit)));
//
// If there were more than 8, then it is a "false" result
// since it counted beyond the end of the current byte.
// Therefore, simply limit it to the number of pixels
// remaining in this byte.
//
if(i32Count > 8)
{
i32Count = 8 - i32Bit;
}
//
// Increment the number of on pixels.
//
i32On += i32Count;
//
// Increment the bit position within the byte.
//
i32Bit += i32Count;
//
// See if the end of the byte has been reached.
//
if(i32Bit == 8)
{
//
// Advance to the next byte and continue counting on
// pixels.
//
i32Bit = 0;
i32Idx++;
}
else
{
//
// Since the end of the byte was not reached, there
// must be an off pixel. Therefore, stop counting on
// pixels.
//
break;
}
}
}
//
// Otherwise, the font is compressed with a pixel RLE scheme.
//
else
{
//
// See if this is a byte that encodes some on and off pixels.
//
if(pui8Data[i32Idx])
{
//
// Extract the number of off pixels.
//
i32Off = (pui8Data[i32Idx] >> 4) & 15;
//
// Extract the number of on pixels.
//
i32On = pui8Data[i32Idx] & 15;
//
// Skip past this encoded byte.
//
i32Idx++;
}
//
// Otherwise, see if this is a repeated on pixel byte.
//
else if(pui8Data[i32Idx + 1] & 0x80)
{
//
// There are no off pixels in this encoding.
//
i32Off = 0;
//
// Extract the number of on pixels.
//
i32On = (pui8Data[i32Idx + 1] & 0x7f) * 8;
//
// Skip past these two encoded bytes.
//
i32Idx += 2;
}
//
// Otherwise, this is a repeated off pixel byte.
//
else
{
//
// Extract the number of off pixels.
//
i32Off = pui8Data[i32Idx + 1] * 8;
//
// There are no on pixels in this encoding.
//
i32On = 0;
//
// Skip past these two encoded bytes.
//
i32Idx += 2;
}
}
//
// Loop while there are any off pixels.
//
while(i32Off)
{
//
// See if the bottom of the clipping region has been exceeded.
//
if((i32Y + i32Y0) > pContext->sClipRegion.i16YMax)
{
//
// Ignore the remainder of the on pixels.
//
break;
}
//
// See if there is more than one on pixel that will fit onto
// the current row.
//
if((i32Off > 1) && ((i32X0 + 1) < pui8Data[1]))
{
//
// Determine the number of on pixels that will fit on this
// row.
//
i32Count = (((i32X0 + i32Off) > pui8Data[1]) ?
pui8Data[1] - i32X0 : i32Off);
//
// If this row is within the clipping region, draw a
// horizontal line that corresponds to the sequence of on
// pixels.
//
if(((i32Y + i32Y0) >= pContext->sClipRegion.i16YMin) &&
bOpaque)
{
if((i32X + i32X0) < pContext->sClipRegion.i16XMin)
{
i32ClipX1 = pContext->sClipRegion.i16XMin;
}
else
{
i32ClipX1 = i32X + i32X0;
}
if((i32X + i32X0 + i32Count - 1) >
pContext->sClipRegion.i16XMax)
{
i32ClipX2 = pContext->sClipRegion.i16XMax;
}
else
{
i32ClipX2 = i32X + i32X0 + i32Count - 1;
}
DpyLineDrawH(pContext->psDisplay, i32ClipX1, i32ClipX2,
i32Y + i32Y0, pContext->ui32Background);
}
//
// Decrement the count of on pixels by the number on this
// row.
//
i32Off -= i32Count;
//
// Increment the X offset by the number of on pixels.
//
i32X0 += i32Count;
}
//
// Otherwise, there is only a single on pixel that can be
// drawn.
//
else
{
//
// If this pixel is within the clipping region, then draw
// it.
//
if(((i32X + i32X0) >= pContext->sClipRegion.i16XMin) &&
((i32X + i32X0) <= pContext->sClipRegion.i16XMax) &&
((i32Y + i32Y0) >= pContext->sClipRegion.i16YMin) &&
bOpaque)
{
DpyPixelDraw(pContext->psDisplay, i32X + i32X0,
i32Y + i32Y0, pContext->ui32Background);
}
//
// Decrement the count of on pixels.
//
i32Off--;
//
// Increment the X offset.
//
i32X0++;
}
//
// See if the X offset has reached the right side of the
// character glyph.
//
if(i32X0 == pui8Data[1])
{
//
// Increment the Y offset.
//
i32Y0++;
//
// Reset the X offset to the left side of the character
// glyph.
//
i32X0 = 0;
}
}
//
// Loop while there are any on pixels.
//
while(i32On)
{
//
// See if the bottom of the clipping region has been exceeded.
//
if((i32Y + i32Y0) > pContext->sClipRegion.i16YMax)
{
//
// Ignore the remainder of the on pixels.
//
break;
}
//
// See if there is more than one on pixel that will fit onto
// the current row.
//
if((i32On > 1) && ((i32X0 + 1) < pui8Data[1]))
{
//
// Determine the number of on pixels that will fit on this
// row.
//
i32Count = (((i32X0 + i32On) > pui8Data[1]) ?
pui8Data[1] - i32X0 : i32On);
//
// If this row is within the clipping region, draw a
// horizontal line that corresponds to the sequence of on
// pixels.
//
if((i32Y + i32Y0) >= pContext->sClipRegion.i16YMin)
{
if((i32X + i32X0) >= pContext->sClipRegion.i16XMin)
{
i32ClipX1 = i32X + i32X0;
}
else
{
i32ClipX1 = pContext->sClipRegion.i16XMin;
}
if((i32X + i32X0 + i32Count - 1) >
pContext->sClipRegion.i16XMax)
{
i32ClipX2 = pContext->sClipRegion.i16XMax;
}
else
{
i32ClipX2 = i32X + i32X0 + i32Count - 1;
}
DpyLineDrawH(pContext->psDisplay, i32ClipX1, i32ClipX2,
i32Y + i32Y0, pContext->ui32Foreground);
}
//
// Decrement the count of on pixels by the number on this
// row.
//
i32On -= i32Count;
//
// Increment the X offset by the number of on pixels.
//
i32X0 += i32Count;
}
//
// Otherwise, there is only a single on pixel that can be
// drawn.
//
else
{
//
// If this pixel is within the clipping region, then draw
// it.
//
if(((i32X + i32X0) >= pContext->sClipRegion.i16XMin) &&
((i32X + i32X0) <= pContext->sClipRegion.i16XMax) &&
((i32Y + i32Y0) >= pContext->sClipRegion.i16YMin))
{
DpyPixelDraw(pContext->psDisplay, i32X + i32X0,
i32Y + i32Y0, pContext->ui32Foreground);
}
//
// Decrement the count of on pixels.
//
i32On--;
//
// Increment the X offset.
//
i32X0++;
}
//
// See if the X offset has reached the right side of the
// character glyph.
//
if(i32X0 == pui8Data[1])
{
//
// Increment the Y offset.
//
i32Y0++;
//
// Reset the X offset to the left side of the character
// glyph.
//
i32X0 = 0;
}
}
}
}
//*****************************************************************************
//
//! Retrieves header information from a font.
//!
//! \param psFont points to the font whose information is to be queried.
//! \param pui8Format points to storage which will be written with the font
//! format.
//! \param pui8MaxWidth points to storage which will be written with the
//! maximum character width for the font in pixels.
//! \param pui8Height points to storage which will be written with the height
//! of the font character cell in pixels.
//! \param pui8Baseline points to storage which will be written with the font
//! baseline offset in pixels.
//!
//! This function may be used to retrieve information about a given font. The
//! \e psFont parameter may point to any supported font format including wrapped
//! fonts described using a \e tFontWrapper structure (with the pointer cast
//! to a tFont pointer).
//!
//! \return None.
//
//*****************************************************************************
void
GrFontInfoGet(const tFont *psFont, uint8_t *pui8Format, uint8_t *pui8MaxWidth,
uint8_t *pui8Height, uint8_t *pui8Baseline)
{
//
// Parameter sanity checks.
//
ASSERT(psFont);
ASSERT(pui8Format);
ASSERT(pui8MaxWidth);
ASSERT(pui8Height);
ASSERT(pui8Baseline);
//
// Is this a wrapped font?
//
if(psFont->ui8Format & FONT_FMT_WRAPPED)
{
tFontWrapper *psFontWrapper;
//
// Yes - get a pointer to the relevant header type and call the
// access function to retrieve the font information.
//
psFontWrapper = (tFontWrapper *)psFont;
psFontWrapper->pFuncs->pfnFontInfoGet(psFontWrapper->pui8FontId,
pui8Format, pui8MaxWidth,
pui8Height, pui8Baseline);
}
else
{
//
// This is not a wrapped font so we can read the information directly
// from the font structure passed.
//
*pui8Format = psFont->ui8Format;
*pui8MaxWidth = psFont->ui8MaxWidth;
*pui8Height = psFont->ui8Height;
*pui8Baseline = psFont->ui8Baseline;
}
}
//*****************************************************************************
//
//! Gets the baseline of a font.
//!
//! \param psFont is a pointer to the font to query.
//!
//! This function determines the baseline position of a font. The baseline is
//! the offset between the top of the font and the bottom of the capital
//! letters. The only font data that exists below the baseline are the
//! descenders on some lower-case letters (such as ``y'').
//!
//! \return Returns the baseline of the font, in pixels.
//
//*****************************************************************************
uint32_t
GrFontBaselineGet(const tFont *psFont)
{
uint8_t ui8Format, ui8Width, ui8Height, ui8Baseline;
ASSERT(psFont);
if(psFont->ui8Format != FONT_FMT_WRAPPED)
{
return((uint32_t)(psFont->ui8Baseline));
}
else
{
tFontWrapper *pWrap;
pWrap = (tFontWrapper *)psFont;
pWrap->pFuncs->pfnFontInfoGet(pWrap->pui8FontId, &ui8Format, &ui8Width,
&ui8Height, &ui8Baseline);
return((uint32_t)ui8Baseline);
}
}
//*****************************************************************************
//
//! Gets the height of a font.
//!
//! \param psFont is a pointer to the font to query.
//!
//! This function determines the height of a font. The height is the offset
//! between the top of the font and the bottom of the font, including any
//! ascenders and descenders.
//!
//! \return Returns the height of the font, in pixels.
//
//*****************************************************************************
uint32_t
GrFontHeightGet(const tFont *psFont)
{
uint8_t ui8Format, ui8Width, ui8Height, ui8Baseline;
ASSERT(psFont);
if(psFont->ui8Format != FONT_FMT_WRAPPED)
{
return(psFont->ui8Height);
}
else
{
tFontWrapper *pWrap;
pWrap = (tFontWrapper *)psFont;
pWrap->pFuncs->pfnFontInfoGet(pWrap->pui8FontId, &ui8Format, &ui8Width,
&ui8Height, &ui8Baseline);
return((uint32_t)ui8Height);
}
}
//*****************************************************************************
//
//! Gets the maximum width of a font.
//!
//! \param psFont is a pointer to the font to query.
//!
//! This function determines the maximum width of a font. The maximum width is
//! the width of the widest individual character in the font.
//!
//! \return Returns the maximum width of the font, in pixels.
//
//*****************************************************************************
uint32_t
GrFontMaxWidthGet(const tFont *psFont)
{
uint8_t ui8Format, ui8Width, ui8Height, ui8Baseline;
ASSERT(psFont);
if(psFont->ui8Format != FONT_FMT_WRAPPED)
{
return(psFont->ui8MaxWidth);
}
else
{
tFontWrapper *pWrap;
pWrap = (tFontWrapper *)psFont;
pWrap->pFuncs->pfnFontInfoGet(pWrap->pui8FontId, &ui8Format, &ui8Width,
&ui8Height, &ui8Baseline);
return((uint32_t)ui8Width);
}
}
//*****************************************************************************
//
// Retrieves a pointer to the data for a specific glyph in a tFont or tFontEx
// font.
//
// \param psFont points to the font whose glyph is to be queried.
// \param ui32CodePoint idenfities the specific glyph whose data is being
// queried.
// \param pui8Width points to storage which will be written with the
// width of the requested glyph in pixels.
//
// This function may be used to retrieve the pixel data for a particular glyph
// in a font described using a tFont or tFontEx type.
//
// \return Returns a pointer to the data for the requested glyph or NULL if
// the glyph does not exist in the font.
//
//*****************************************************************************
static const uint8_t *
FontGlyphDataGet(const tFont *psFont, uint32_t ui32CodePoint,
uint8_t *pui8Width)
{
const uint8_t *pui8Glyphs;
const uint16_t *pui16Offset;
uint8_t ui8First, ui8Last;
const tFontEx *psFontEx;
const uint8_t *pui8Data;
//
// Extract various parameters from the font depending upon whether it's
// in the tFont or tFontEx format.
//
if(psFont->ui8Format & FONT_EX_MARKER)
{
psFontEx = (const tFontEx *)psFont;
pui8Glyphs = psFontEx->pui8Data;
pui16Offset = psFontEx->pui16Offset;
ui8First = psFontEx->ui8First;
ui8Last = psFontEx->ui8Last;
}
else
{
pui8Glyphs = psFont->pui8Data;
pui16Offset = psFont->pui16Offset;
ui8First = 32;
ui8Last = 126;
}
//
// Does the codepoint passed exist in the font?
//
if((ui32CodePoint >= ui8First) && (ui32CodePoint <= ui8Last))
{
//
// Yes - return a pointer to the glyph data for the character.
//
pui8Data = pui8Glyphs + pui16Offset[ui32CodePoint - ui8First];
*pui8Width = pui8Data[1];
return(pui8Data);
}
else
{
//
// No - the glyph doesn't exist so return NULL to indicate this.
//
return(0);
}
}
//*****************************************************************************
//
// Retrieves a pointer to the data for a specific glyph in a tFontWide font.
//
// \param psFont points to the font whose glyph is to be queried.
// \param ui32CodePoint idenfities the specific glyph whose data is being
// queried.
// \param pui8Width points to storage which will be written with the
// width of the requested glyph in pixels.
//
// This function may be used to retrieve the pixel data for a particular glyph
// in a font described using a tFontWide type.
//
// \return Returns a pointer to the data for the requested glyph or NULL if
// the glyph does not exist in the font.
//
//*****************************************************************************
static const uint8_t *
FontWideGlyphDataGet(const tFontWide *psFont, uint32_t ui32CodePoint,
uint8_t *pui8Width)
{
const uint8_t *pui8Data;
tFontBlock *pBlock;
uint32_t *pui32OffsetTable;
uint32_t ui32Loop;
uint32_t ui32Offset;
//
// Get a pointer to the first block description in the font.
//
pBlock = (tFontBlock *)(psFont + 1);
//
// Run through the blocks of the font looking for the one that contains
// our codepoint.
//
for(ui32Loop = 0; ui32Loop < psFont->ui16NumBlocks; ui32Loop++)
{
//
// Does the codepoint lie within this block?
//
if((ui32CodePoint >= pBlock[ui32Loop].ui32StartCodepoint) &&
(ui32CodePoint < (pBlock[ui32Loop].ui32StartCodepoint +
pBlock[ui32Loop].ui32NumCodepoints)))
{
//
// Yes - drop out of the loop early.
//
break;
}
}
//
// Did we find the block?
//
if(ui32Loop == psFont->ui16NumBlocks)
{
//
// No - return NULL to indicate that the character wasn't found.
//
return(0);
}
//
// Get the offset to the glyph data via the block's offset table.
//
pui32OffsetTable = (uint32_t *)((uint8_t *)psFont +
pBlock[ui32Loop].ui32GlyphTableOffset);
ui32Offset = pui32OffsetTable[ui32CodePoint -
pBlock[ui32Loop].ui32StartCodepoint];
//
// Is the offset non-zero? Zero offset indicates that the glyph is not
// encoded in the font.
//
if(ui32Offset)
{
//
// The offset is not 0 so this glyph does exist. Return a pointer to
// its data.
//
pui8Data = (const uint8_t *)pui32OffsetTable + ui32Offset;
*pui8Width = pui8Data[1];
return(pui8Data);
}
else
{
//
// The glyph offset was 0 so this implies that the glyph does not
// exist. Return NULL to indicate this.
//
return(0);
}
}
//*****************************************************************************
//
//! Retrieves a pointer to the data for a specific font glyph.
//!
//! \param psFont points to the font whose glyph is to be queried.
//! \param ui32CodePoint idenfities the specific glyph whose data is being
//! queried.
//! \param pui8Width points to storage which will be written with the
//! width of the requested glyph in pixels.
//!
//! This function may be used to retrieve the pixel data for a particular glyph
//! in a font. The pointer returned may be passed to GrFontGlyphRender to
//! draw the glyph on the display. The format of the data may be determined
//! from the font format returned via a call to GrFontInfoGet().
//!
//! \return Returns a pointer to the data for the requested glyph or NULL if
//! the glyph does not exist in the font.
//
//*****************************************************************************
const uint8_t *
GrFontGlyphDataGet(const tFont *psFont, uint32_t ui32CodePoint,
uint8_t *pui8Width)
{
ASSERT(psFont);
ASSERT(pui8Width);
//
// What type of font are we dealing with here?
//
if(psFont->ui8Format == FONT_FMT_WRAPPED)
{
tFontWrapper *psFontWrapper;
//
// This is a wrapped font so call the access function required to get
// the required information.
//
psFontWrapper = (tFontWrapper *)psFont;
return(psFontWrapper->pFuncs->
pfnFontGlyphDataGet(psFontWrapper->pui8FontId, ui32CodePoint,
pui8Width));
}
else if (psFont->ui8Format & FONT_WIDE_MARKER)
{
//
// This is a wide character set font so call the relevant function to
// retrieve the glyph data pointer.
//
return(FontWideGlyphDataGet((const tFontWide *)psFont, ui32CodePoint,
pui8Width));
}
else
{
//
// This is an 8 bit font so call the relevant function to retrieve the
// glyph data pointer.
//
return(FontGlyphDataGet(psFont, ui32CodePoint, pui8Width));
}
}
//*****************************************************************************
//
//! Returns the codepage supported by the given font.
//!
//! \param psFont points to the font whose codepage is to be returned.
//!
//! This function returns the codepage supported by the font whose pointer is
//! passed. The codepage defines the mapping between a given character code
//! and the glyph that represents it. Standard codepages are identified by
//! labels of the form \b CODEPAGE_xxxx. Fonts may also be encoded using
//! application specific codepages with values of 0x8000 or higher.
//!
//! \return Returns the font codepage identifier.
//!
//*****************************************************************************
uint16_t
GrFontCodepageGet(const tFont *psFont)
{
ASSERT(psFont);
//
// Is this a wide character set font?
//
if(psFont->ui8Format & FONT_WIDE_MARKER)
{
//
// Yes - read the font codepage from the header.
//
return(((tFontWide *)psFont)->ui16Codepage);
}
else if(psFont->ui8Format & FONT_FMT_WRAPPED)
{
//
// This is a wrapper-based font so call the access function to get
// its codepage.
//
ASSERT(((tFontWrapper *)psFont)->pFuncs->pfnFontCodepageGet);
return(((tFontWrapper *)psFont)->pFuncs->
pfnFontCodepageGet(((tFontWrapper *)psFont)->pui8FontId));
}
else
{
//
// No - this is an old format font so just return ISO8859-1. This is
// compatible with ASCII so should be benign.
//
return(CODEPAGE_ISO8859_1);
}
}
//*****************************************************************************
//
// Determines which codepoint mapping function to use based on the current
// source codepage and font selection in the context.
//
//*****************************************************************************
static int32_t
UpdateContextCharMapping(tContext *pContext)
{
uint32_t ui32Loop;
uint16_t ui16FontCodepage;
//
// Make sure we have a font selected.
//
if(!pContext->psFont)
{
//
// No font is yet selected so we can't determine the codepage map to
// use.
//
return(-1);
}
//
// Get the current font's codepage.
//
ui16FontCodepage = GrFontCodepageGet(pContext->psFont);
//
// Look through the codepage mapping functions we have been given and
// find an appropriate one.
//
for(ui32Loop = 0; ui32Loop < pContext->ui8NumCodePointMaps; ui32Loop++)
{
if((pContext->pCodePointMapTable[ui32Loop].ui16SrcCodepage ==
pContext->ui16Codepage) &&
(pContext->pCodePointMapTable[ui32Loop].ui16FontCodepage ==
ui16FontCodepage))
{
//
// We found a suitable mapping function so remember it.
//
pContext->ui8CodePointMap = (uint8_t)ui32Loop;
return((int32_t)ui32Loop);
}
}
//
// If we get here, no suitable mapping function could be found. Set things
// up to use the first mapping function (even though it's not right).
//
pContext->ui8CodePointMap = 0;
return(-1);
}
//*****************************************************************************
//
//! Returns the number of blocks of character encoded by a font.
//!
//! \param psFont is a pointer to the font which is to be queried.
//!
//! This function may be used to query the number of contiguous blocks of
//! codepoints (characters) encoded by a given font. This is primarily of use
//! to applications which wish to parse fonts directly to, for example, display
//! all glyphs in the font. It is unlikely that applications which wish to
//! display text strings would need to call this function.
//!
//! The \e psFont parameter may point to any supported font format including
//! wrapped fonts described using the \e tFontWrapper structure (assuming, of
//! course, that the structure pointer is cast to a \e tFont pointer).
//!
//! \return Returns the number of blocks of codepoints within the font.
//
//*****************************************************************************
uint16_t
GrFontNumBlocksGet(const tFont *psFont)
{
ASSERT(psFont);
//
// Is this a wide character set font?
//
if(psFont->ui8Format & FONT_WIDE_MARKER)
{
//
// Yes - read the number of blocks from the header.
//
return(((tFontWide *)psFont)->ui16NumBlocks);
}
else if(psFont->ui8Format & FONT_FMT_WRAPPED)
{
//
// This is a wrapper-based font so call the access function to get
// the information.
//
ASSERT(((tFontWrapper *)psFont)->pFuncs->pfnFontNumBlocksGet);
return(((tFontWrapper *)psFont)->pFuncs->
pfnFontNumBlocksGet(((tFontWrapper *)psFont)->pui8FontId));
}
else
{
//
// No - this is an old format font so it only supports a single block
// of characters.
//
return(1);
}
}
//*****************************************************************************
//
//! Returns the number of blocks of character encoded by a font.
//!
//! \param psFont is a pointer to the font which is to be queried.
//! \param ui16BlockIndex is the index of the codepoint block to be queried.
//! \param pui32Start points to storage which is written with the codepoint
//! number of the first glyph in the block.
//!
//! This function may be used to query the contents of a particular block of
//! codepoints (characters) encoded by a given font. This is primarily of use
//! to applications which wish to parse fonts directly to, for example, display
//! all glyphs in the font. It is unlikely that applications which wish to
//! display text strings would need to call this function.
//!
//! The number of blocks in the font may be queried by calling
//! GrFontNumBlocksGet(). The \e ui16BlockIndex selects a block and valid
//! values are from, 0 to the number of blocks in the font - 1.
//!
//! The \e pui32Start pointer is written with the codepoint number of the
//! first glyph in the given block. It is assumed that each block contains
//! a contiguous block of glyphs so the actual codepoints represented in the
//! block will be from \e *pui32Start to (\e *pui32Start + return value - 1).
//!
//! The \e psFont parameter may point to any supported font format including
//! wrapped fonts described using the \e tFontWrapper structure (assuming, of
//! course, that the structure pointer is cast to a \e tFont pointer).
//!
//! \return Returns the number of blocks of codepoints within the block.
//
//*****************************************************************************
uint32_t
GrFontBlockCodepointsGet(const tFont *psFont, uint16_t ui16BlockIndex,
uint32_t *pui32Start)
{
ASSERT(psFont);
ASSERT(pui32Start);
//
// Is this a wide character set font?
//
if(psFont->ui8Format & FONT_WIDE_MARKER)
{
tFontWide *psFontWide;
tFontBlock *pBlock;
//
// This is a wide character set font. Is the block index valid?
//
psFontWide = (tFontWide *)psFont;
if(ui16BlockIndex >= psFontWide->ui16NumBlocks)
{
//
// The block number is invalid so return 0 to indicate the error.
//
return(0);
}
//
// Yes - find the relevant block table and extract the required
// information from it.
//
pBlock = (tFontBlock *)(psFontWide + 1);
*pui32Start = pBlock[ui16BlockIndex].ui32StartCodepoint;
return(pBlock[ui16BlockIndex].ui32NumCodepoints);
}
else if(psFont->ui8Format & FONT_FMT_WRAPPED)
{
//
// This is a wrapper-based font so call the access function to get
// its codepage.
//
ASSERT(((tFontWrapper *)psFont)->pFuncs->pfnFontBlockCodepointsGet);
return(((tFontWrapper *)psFont)->pFuncs->
pfnFontBlockCodepointsGet(((tFontWrapper *)psFont)->pui8FontId,
ui16BlockIndex, pui32Start));
}
else
{
//
// No - this is an old format font so it only supports a single block
// of
//
if(ui16BlockIndex != 0)
{
//
// An invalid block number was passed so return 0 to indicate an
// error.
//
return(0);
}
//
// We were passed a valid block number (0) so return the start
// codepoint and number of characters. Is this an extended font or
// the original ASCII-only flavor?
//
if(psFont->ui8Format & FONT_EX_MARKER)
{
tFontEx *psFontEx;
//
// It's an extended font so read the character range from the
// header.
//
psFontEx = (tFontEx *)psFont;
*pui32Start = (uint32_t)psFontEx->ui8First;
return((uint32_t)(psFontEx->ui8Last - psFontEx->ui8First + 1));
}
else
{
//
// This is an ASCII font so it supports a fixed set of characters.
//
*pui32Start = 0x20;
return(96);
}
}
}
//*****************************************************************************
//
//! Provides GrLib with a table of source/font codepage mapping functions.
//!
//! \param pContext is a pointer to the context to modify.
//! \param pCodePointMapTable points to an array of structures each defining
//! the mapping from a source text codepage to a destination font codepage.
//! \param ui8NumMaps provides the number of entries in the \e
//! pCodePointMapTable array.
//!
//! This function provides GrLib with a set of functions that can be used to
//! map text encoded in a particular codepage to one other codepage. These
//! functions are used to allow GrLib to parse text strings and display the
//! correct glyphs from the font. The mapping function used by the library
//! will depend upon the source text codepage set using a call to
//! GrStringCodepageSet() and the context's font, set using GrContextFontSet().
//!
//! If no conversion function is available to map from the selected source
//! codepage to the font's codepage, GrLib use the first conversion function
//! provided in the codepoint map table and the displayed text will likely be
//! incorrect.
//!
//! If this call is not made, GrLib assumes ISO8859-1 encoding for both the
//! source text and font to maintain backwards compatibility for applications
//! which were developed prior to the introduction of international character
//! set support.
//!
//! \return None.
//
//*****************************************************************************
void
GrCodepageMapTableSet(tContext *pContext, tCodePointMap *pCodePointMapTable,
uint8_t ui8NumMaps)
{
ASSERT(pContext);
ASSERT(pCodePointMapTable);
ASSERT(ui8NumMaps);
//
// Remember the table details.
//
pContext->pCodePointMapTable = pCodePointMapTable;
pContext->ui8NumCodePointMaps = ui8NumMaps;
//
// Update the character mapping to ensure that we show the right glyphs.
//
UpdateContextCharMapping(pContext);
}
//*****************************************************************************
//
//! Sets the source text codepage to be used.
//!
//! \param pContext is a pointer to the context to modify.
//! \param ui16Codepage is the identifier of the codepage for the text that the
//! application will pass on future calls to the GrStringDraw() and
//! GrStringDrawCentered() functions.
//!
//! This function sets the codepage that will be used when rendering text
//! via future calls to GrStringDraw() or GrStringDrawCentered(). The codepage
//! defines the mapping between specific numbers used to define characters and
//! the actual character glyphs displayed. By default, GrLib assumes text
//! passed is encoded using the ISO8859-1 which supports ASCII and western
//! European character sets. Applications wishing to use multi-byte character
//! sets or alphabets other than those supported by ISO8859-1 should set an
//! appropriate codepage such as UTF-8.
//!
//! It is important to ensure that your application makes use of fonts which
//! support the required codepage or that you have supplied GrLib with a
//! codepage mapping function that allows translation of your chosen text
//! codepage into the codepage supported by the fonts in use. Several
//! mapping functions for commonly-used codepages are provided and others can
//! be written easily to support different text and font codepage combinations.
//! Codepage mapping functions are provided to GrLib in a table passed as a
//! parameter to the function GrCodepageMapTableSet().
//!
//! \return None.
//
//*****************************************************************************
int32_t
GrStringCodepageSet(tContext *pContext, uint16_t ui16Codepage)
{
ASSERT(pContext);
//
// Remember the codepage to be used.
//
pContext->ui16Codepage = ui16Codepage;
//
// Update the character mapping to ensure that we show the right glyphs.
//
return(UpdateContextCharMapping(pContext));
}
//*****************************************************************************
//
//! Sets the font to be used.
//!
//! \param pContext is a pointer to the drawing context to modify.
//! \param psFont is a pointer to the font to be used.
//!
//! This function sets the font to be used for string drawing operations in the
//! specified drawing context.
//!
//! \return None.
//
//*****************************************************************************
void
GrContextFontSet(tContext *pContext, const tFont *psFont)
{
ASSERT(pContext);
ASSERT(psFont);
//
// Remember the font to be used.
//
pContext->psFont = psFont;
//
// Update the character mapping to ensure that we show the right glyphs.
//
UpdateContextCharMapping(pContext);
}
#endif
//*****************************************************************************
//
// Definitions and variables used by the decompression routine for the string
// table.
//
//*****************************************************************************
#define SC_MAX_INDEX 2047
#define SC_IS_NULL 0x0000ffff
#define SC_GET_LEN(v) ((v) >> (32 - 5))
#define SC_GET_INDEX(v) (((v) >> 16) & SC_MAX_INDEX)
#define SC_GET_OFF(v) ((v) & SC_IS_NULL)
#define SC_FLAG_COMPRESSED 0x00008000
#define SC_OFFSET_M 0x00007fff
//*****************************************************************************
//
// The globals that hold the shortcuts to various locations and values in the
// table.
//
//*****************************************************************************
static const uint32_t *g_pui32StringTable;
static const uint16_t *g_pui16LanguageTable;
static const uint8_t *g_pui8StringData;
static uint16_t g_ui16Language;
static uint16_t g_ui16NumLanguages;
static uint16_t g_ui16NumStrings;
//*****************************************************************************
//
//! This function sets the location of the current string table.
//!
//! \param pvTable is a pointer to a string table that was generated by the
//! string compression utility.
//!
//! This function is used to set the string table to use for strings in an
//! application. This string table is created by the string compression
//! utility. This function is used to swap out multiple string tables if the
//! application requires more than one table. It does not allow using more
//! than one string table at a time.
//!
//! \return None.
//
//*****************************************************************************
void
GrStringTableSet(const void *pvTable)
{
//
// Save the number of languages and number of strings.
//
g_ui16NumStrings = ((uint16_t *)pvTable)[0];
g_ui16NumLanguages = ((uint16_t *)pvTable)[1];
//
// Save a pointer to the Language Identifier table.
//
g_pui16LanguageTable = (uint16_t *)pvTable + 2;
//
// Save a pointer to the String Index table.
//
g_pui32StringTable = (uint32_t *)(g_pui16LanguageTable +
g_ui16NumLanguages);
//
// Save a pointer to the String Data.
//
g_pui8StringData = (uint8_t *)(g_pui32StringTable +
(g_ui16NumStrings * g_ui16NumLanguages));
}
//*****************************************************************************
//
//! This function sets the current language for strings returned by the
//! GrStringGet() function.
//!
//! \param ui16LangID is one of the language identifiers provided in the string
//! table.
//!
//! This function is used to set the language identifier for the strings
//! returned by the GrStringGet() function. The \e ui16LangID parameter should
//! match one of the identifiers that was included in the string table. These
//! are provided in a header file in the graphics library and must match the
//! values that were passed through the sting compression utility.
//!
//! \return This function returns 0 if the language was not found and a
//! non-zero value if the laguage was found.
//
//*****************************************************************************
uint32_t
GrStringLanguageSet(uint16_t ui16LangID)
{
int32_t i32Lang;
//
// Search for the requested language.
//
for(i32Lang = 0; i32Lang < g_ui16NumLanguages; i32Lang++)
{
//
// Once found, break out and save the new language.
//
if(g_pui16LanguageTable[i32Lang] == ui16LangID)
{
break;
}
}
//
// Only accept the language if it was found, otherwise continue using
// previous language.
//
if(i32Lang != g_ui16NumLanguages)
{
g_ui16Language = i32Lang;
return(1);
}
return(0);
}
//*****************************************************************************
//
//! This function returns a string from the current string table.
//!
//! \param i32Index is the index of the string to retrieve.
//! \param pcData is the pointer to the buffer to store the string into.
//! \param ui32Size is the size of the buffer provided by pcData.
//!
//! This function will return a string from the string table in the language
//! set by the GrStringLanguageSet() function. The value passed in \e iIndex
//! parameter is the string that is being requested and will be returned in
//! the buffer provided in the \e pcData parameter. The amount of data
//! returned will be limited by the ui32Size parameter.
//!
//! \return Returns the number of valid bytes returned in the \e pcData buffer.
//
//*****************************************************************************
uint32_t
GrStringGet(int32_t i32Index, char *pcData, uint32_t ui32Size)
{
uint32_t ui32Len, ui32Offset, ui32SubCode[16];
int32_t i32Pos, i32Idx, i32Bit, i32Skip, i32Buf;
uint8_t *pui8BufferOut;
const uint8_t *pui8String;
ASSERT(i32Index < g_ui16NumStrings);
ASSERT(pcData != 0);
//
// Initialize the output buffer state.
//
i32Pos = 0;
pui8BufferOut = 0;
//
// if built up from another string, we need to process that
// this could nest multiple layers, so we follow in
//
ui32SubCode[i32Pos] = g_pui32StringTable[(g_ui16Language *
g_ui16NumStrings) + i32Index];
if(SC_GET_LEN(ui32SubCode[i32Pos]))
{
//
// recurse down
//
while(i32Pos < 16)
{
//
// Copy over the partial (if any) from a previous string.
//
i32Idx = SC_GET_INDEX(ui32SubCode[i32Pos++]);
ui32SubCode[i32Pos] = g_pui32StringTable[(g_ui16Language *
g_ui16NumStrings) +
i32Idx];
if(!SC_GET_LEN(ui32SubCode[i32Pos]))
{
//
// not linked, just string
//
break;
}
}
}
//
// Now work backwards out.
//
i32Idx = 0;
//
// Build up the string in pieces.
//
while(i32Pos >= 0)
{
//
// Get the offset in string table.
//
ui32Offset = SC_GET_OFF(ui32SubCode[i32Pos]);
if(ui32Offset == SC_IS_NULL)
{
//
// An empty string.
//
pcData[i32Idx] = 0;
}
else if(ui32Offset & SC_FLAG_COMPRESSED)
{
//
// This is a compressed string so initialize the pointer to the
// compressed data.
//
pui8String = g_pui8StringData + (ui32Offset & SC_OFFSET_M);
//
// Initialize the bit variables.
//
i32Bit = 0;
i32Skip = 0;
//
// Make a pointer to the current buffer out location.
//
pui8BufferOut = (uint8_t *)pcData + i32Idx;
//
// If the out buffer is beyond the maximum size then just break
// out and return what we have so far.
//
if((char *)pui8BufferOut > (pcData + ui32Size))
{
break;
}
//
// Now build up real string by decompressing bits.
//
if(!SC_GET_LEN(ui32SubCode[i32Pos]) &&
SC_GET_INDEX(ui32SubCode[i32Pos]))
{
i32Skip = SC_GET_INDEX(ui32SubCode[i32Pos]);
if(i32Pos)
{
ui32Len = SC_GET_LEN(ui32SubCode[i32Pos - 1]);
}
else
{
ui32Len = (i32Skip & 0x3f);
}
i32Skip >>= 6;
i32Idx += ui32Len;
ui32Len += i32Skip;
}
else if(i32Pos)
{
//
// Get the length of the partial string.
//
ui32Len = SC_GET_LEN(ui32SubCode[i32Pos - 1]) - i32Idx;
i32Idx += ui32Len;
}
else if(!SC_GET_LEN(ui32SubCode[0]) &&
SC_GET_INDEX(ui32SubCode[0]))
{
ui32Len = SC_GET_INDEX(ui32SubCode[0]);
i32Skip = ui32Len >> 6;
ui32Len = (ui32Len & 0x3f) + i32Skip;
}
else
{
//
// Arbitrary as null character ends the string.
//
ui32Len = 1024;
}
for(; ui32Len; ui32Len--)
{
//
// Packed 6 bits for each char
//
*pui8BufferOut = (*pui8String >> i32Bit) & 0x3f;
if(i32Bit >= 2)
{
*pui8BufferOut |= (*++pui8String << (8 - i32Bit)) & 0x3f;
}
i32Bit = (i32Bit + 6) & 0x7;
if(!*pui8BufferOut)
{
//
// end of string
//
break;
}
if(i32Skip)
{
i32Skip--;
continue;
}
//
// Put back removed bit
//
*pui8BufferOut |= 0x40;
//
// Now look for a few special chars we mapped up into other
// characters.
//
if(*pui8BufferOut == '`')
{
*pui8BufferOut = ' ';
}
else if(*pui8BufferOut == '~')
{
*pui8BufferOut = '-';
}
else if(*pui8BufferOut == 0x7f)
{
*pui8BufferOut = '.';
}
else if(*pui8BufferOut == '\\')
{
*pui8BufferOut = ':';
}
//
// Increment the pointer and break out if the pointer is now
// beyond the end of the buffer provided.
//
pui8BufferOut++;
if((char *)pui8BufferOut >= (pcData + ui32Size))
{
break;
}
}
}
else if(i32Pos)
{
//
// Part of another string
//
ui32Len = SC_GET_LEN(ui32SubCode[i32Pos - 1]) - i32Idx;
//
// Prevent this copy from going beyond the end of the buffer
// provided.
//
if((i32Idx + ui32Len) > ui32Size)
{
ui32Len = ui32Size - i32Idx;
}
//
// Copy this portion of the string to the output buffer.
//
for(i32Buf = 0; i32Buf < ui32Len; i32Buf++)
{
pcData[i32Idx + i32Buf] = g_pui8StringData[ui32Offset +
i32Buf];
}
i32Idx += ui32Len;
}
else if(SC_GET_INDEX(ui32SubCode[0]) && !SC_GET_LEN(ui32SubCode[0]))
{
//
// Copy this portion of the string to the output buffer.
//
for(i32Buf = 0; i32Buf < SC_GET_INDEX(ui32SubCode[0]); i32Buf++)
{
if((i32Idx + i32Buf) < ui32Size)
{
pcData[i32Idx + i32Buf] = g_pui8StringData[ui32Offset +
i32Buf];
}
else
{
break;
}
}
}
else
{
//
// Now copy the last piece of the string.
//
for(i32Buf = 0; i32Buf < (ui32Size - i32Idx); i32Buf++)
{
//
// Copy the string to the output buffer.
//
pcData[i32Idx + i32Buf] = g_pui8StringData[ui32Offset +
i32Buf];
//
// If a null is hit then terminate the copy.
//
if(pcData[i32Idx + i32Buf] == 0)
{
break;
}
}
//
// If we had not copied any characters before hitting this case,
// initialize the output pointer (this keeps the code at the end of
// the function that returns the length happy). This will be the
// case if we are using an uncompressed string table.
//
if(!pui8BufferOut)
{
pui8BufferOut = (uint8_t *)pcData + (i32Idx + i32Buf);
}
}
i32Pos--;
}
//
// Return the number of bytes copied into the output buffer.
//
if(pui8BufferOut)
{
ui32Len = ((uint32_t)pui8BufferOut - (uint32_t)pcData);
//
// Null terminate the string if there is room.
//
if(ui32Len < ui32Size)
{
pcData[ui32Len] = 0;
}
}
else
{
ui32Len = 0;
}
return(ui32Len);
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
|