Commit 23ce5d4575c4802ea05d9eaee4da526f802ab3b8

David Turner 2002-02-22T18:28:11

adding new & experimental charmap processor

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
diff --git a/src/sfnt/ttcmap0.c b/src/sfnt/ttcmap0.c
new file mode 100644
index 0000000..9f3b6c4
--- /dev/null
+++ b/src/sfnt/ttcmap0.c
@@ -0,0 +1,1248 @@
+/***************************************************************************/
+/*                                                                         */
+/*  ttcmap.c                                                               */
+/*                                                                         */
+/*    TrueType character mapping table (cmap) support (body).              */
+/*                                                                         */
+/*  Copyright 1996-2001 by                                                 */
+/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
+/*                                                                         */
+/*  This file is part of the FreeType project, and may only be used,       */
+/*  modified, and distributed under the terms of the FreeType project      */
+/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
+/*  this file you indicate that you have read the license and              */
+/*  understand and accept it fully.                                        */
+/*                                                                         */
+/***************************************************************************/
+
+
+#include <ft2build.h>
+#include FT_INTERNAL_DEBUG_H
+#include "ttload.h"
+#include "ttcmap.h"
+
+#include "sferrors.h"
+
+  /*************************************************************************/
+  /*                                                                       */
+  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
+  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
+  /* messages during execution.                                            */
+  /*                                                                       */
+#undef  FT_COMPONENT
+#define FT_COMPONENT  trace_ttcmap
+
+
+
+#define  TT_PEEK_Short   FT_PEEK_SHORT
+#define  TT_PEEK_UShort  FT_PEEK16_UBE
+#define  TT_PEEK_Long    FT_PEEK32_BE
+#define  TT_PEEK_ULong   FT_PEEK32_UBE
+
+#define  TT_NEXT_Short   FT_NEXT_SHORT_BE
+#define  TT_NEXT_UShort  FT_NEXT_USHORT_BE
+#define  TT_NEXT_Long    FT_NEXT_LONG_BE
+#define  TT_NEXT_ULong   FT_NEXT_ULONG_BE
+
+ /************************************************************************/
+ /************************************************************************/
+ /*****                                                              *****/
+ /*****                          FORMAT 0                            *****/
+ /*****                                                              *****/
+ /************************************************************************/
+ /************************************************************************/
+
+ /*************************************************************************
+  *
+  *  TABLE OVERVIEW:
+  *  ---------------
+  *
+  *    NAME        OFFSET         TYPE          DESCRIPTION
+  *
+  *    format      0              USHORT        must be 0
+  *    length      2              USHORT        table length in bytes
+  *    language    4              USHORT        Mac language code
+  *    glyph_ids   6              BYTE[256]     array of glyph indices
+  *                262
+  */
+
+#ifdef TT_CONFIG_CMAP_FORMAT_0
+
+  static void
+  tt_cmap0_validate( FT_Byte*      table,
+                     FT_Validator  valid )
+  {
+    FT_Byte*  p      = table + 2;  /* skip format */
+    FT_UInt   length = TT_NEXT_USHORT(p);
+
+    if ( table + length > valid->limit || length < 262 )
+      TOO_SHORT;
+
+    /* check glyph indices whenever necessary */
+    if ( valid->level >= FT_VALIDATE_TIGHT )
+    {
+      FT_UInt  n, index;
+
+      for ( n = 0; n < 256; n++ )
+      {
+        index = *p++;
+        if ( index >= valid->num_glyphs )
+          INVALID_DATA;
+      }
+    }
+  }
+
+
+  static FT_UInt
+  tt_cmap0_char_index( FT_Byte*   table,
+                       FT_ULong   char_code )
+  {
+    return  ( char_code < 256 ? table[6+char_code] : 0 );
+  }
+
+
+  static FT_ULong
+  tt_cmap0_char_next( FT_Byte*  table,
+                      FT_ULong  char_code,
+                      FT_UInt   *agindex )
+  {
+    FT_ULong  result = 0;
+    FT_UInt   gindex = 0;
+
+    table += 6;  /* go to glyph ids */
+    while ( ++char_code < 256 )
+    {
+      gindex = table[char_code];
+      if ( gindex != 0 )
+      {
+        result = char_code;
+        break;
+      }
+    }
+
+    if ( agindex )
+      *agindex = gindex;
+
+    return result;
+  }
+
+  static const TT_Cmap_ClassRec  tt_cmap0_class_rec =
+  {
+    (TT_CMap_ValidateFunc)  tt_cmap0_validate,
+    (TT_CMap_CharIndexFunc) tt_cmap0_char_index,
+    (TT_CMap_CharNextFunc)  tt_cmap0_char_next
+  };
+
+#endif /* TT_CONFIG_CMAP_FORMAT_0 */
+
+
+ /************************************************************************/
+ /************************************************************************/
+ /*****                                                              *****/
+ /*****                          FORMAT 2                            *****/
+ /*****                                                              *****/
+ /*****  This is used for certain CJK encodings that encode text     *****/
+ /*****  in a mixed 8/16 bits along the following lines:             *****/
+ /*****                                                              *****/
+ /*****  * certain byte values correspond to an 8-bit character code *****/
+ /*****    (typicall in the range 0..127 for ASCII compatibility)    *****/
+ /*****                                                              *****/
+ /*****  * certain byte values signal the first byte of a 2-byte     *****/
+ /*****    character code (but these values are also valid as the    *****/
+ /*****    second byte of a 2-byte character)                        *****/
+ /*****                                                              *****/
+ /*****  the following charmap lookup and iteration function all     *****/
+ /*****  assume that the value "charcode" correspond to following:   *****/
+ /*****                                                              *****/
+ /*****     - for one byte characters, "charcode" is simply the      *****/
+ /*****       character code                                         *****/
+ /*****                                                              *****/
+ /*****     - for two byte characters, "charcode" is the 2-byte      *****/
+ /*****       character code in big endian format. More exactly:     *****/
+ /*****                                                              *****/
+ /*****           (charcode >> 8)    is the first byte value         *****/
+ /*****           (charcode & 0xFF)  is the second byte value        *****/
+ /*****                                                              *****/
+ /*****   note that not all values of "charcode" are valid           *****/
+ /*****   according to these rules, and the function moderately      *****/
+ /*****   check the arguments..                                      *****/
+ /*****                                                              *****/
+ /************************************************************************/
+ /************************************************************************/
+
+ /*************************************************************************
+  *
+  *  TABLE OVERVIEW:
+  *  ---------------
+  *
+  *    NAME        OFFSET         TYPE            DESCRIPTION
+  *
+  *    format      0              USHORT          must be 2
+  *    length      2              USHORT          table length in bytes
+  *    language    4              USHORT          Mac language code
+  *    keys        6              USHORT[256]     sub-header keys
+  *    subs        518            SUBHEAD[NSUBS]  sub-headers array
+  *    glyph_ids   518+NSUB*8     USHORT[]        glyph id array
+  *    
+  *   the 'keys' table is used to map charcode high-bytes to sub-headers.
+  *   the value of 'NSUBS' is the number of sub-headers defined in the
+  *   table and is computed by finding the maximum of the 'keys' table.
+  *
+  *   note that for any N, keys[n] is a byte offset within the subs table,
+  *   i.e. it is the corresponding sub-header index multiplied by 8.
+  *
+  *   each sub-header has the following format:
+  *
+  *    NAME        OFFSET         TYPE            DESCRIPTION
+  *
+  *    first       0              USHORT          first valid low-byte
+  *    count       2              USHORT          number of valid low-bytes
+  *    delta       4              SHORT           see below
+  *    offset      6              USHORT          see below
+  *
+  *  a sub-header defines, for each high-byte, the range of valid low-bytes
+  *  within the charmap. note that the range defined by 'first' and 'count'
+  *  must be completely included in the interval [0..255] according to the
+  *  specification
+  *
+  *  if a character code is contained within a given sub-header, then mapping
+  *  it to a glyph index is done as follows:
+  *
+  *  * the value of 'offset' is read. this is a _byte_ distance from the
+  *    location of the 'offset' field itself into a slice of the 'glyph_ids'
+  *    table. Let's call it 'slice' (it's a USHORT[] too)
+  *
+  *  * the value 'slice[ char.lo - first ]' is read. If it is 0, there is
+  *    no glyph for the charcode. Otherwise, the value of 'delta' is added
+  *    to it (modulo 65536) to form a new glyph index
+  *
+  *  it is up to the validation routine to check that all offsets fall within
+  *  the glyph ids table (and not within the 'subs' table itself or outside
+  *  of the CMap).
+  */
+
+#ifdef TT_CONFIG_CMAP_FORMAT_2
+
+  static void
+  tt_cmap2_validate( FT_Byte*      table,
+                     FT_Validator  valid )
+  {
+    FT_Byte*  p      = table + 2;  /* skip format */
+    FT_UInt   length = PEEK_UShort(p);
+    FT_UInt   n, max_subs;
+    FT_Byte*  keys;        /* keys table */
+    FT_Byte*  subs;        /* sub-headers */
+    FT_Byte*  glyph_ids;   /* glyph id array */
+
+
+    if ( table + length > valid->limit || length < 6+512 )
+      TOO_SHORT;
+
+    keys = table + 6;
+
+    /* parse keys to compute sub-headers count */
+    p = keys;
+    for ( n = 0; n < 256; n++ )
+    {
+      FT_UInt  index = TT_NEXT_USHORT(p);
+
+      /* value must be multiple of 8 */
+      if ( valid->level >= FT_VALIDATE_PARANOID && ( index & 7 ) != 0 )
+        INVALID_DATA;
+
+      index >>= 3;
+
+      if ( index > max_subs )
+        max_subs = index;
+    }
+
+    subs      = p;
+    glyph_ids = subs + (max_subs + 1)*8;
+    if ( glyph_ids > valid->limit )
+      TOO_SHORT;
+
+    /* parse sub-headers */
+    for ( n = 0; n <= max_subs; n++ )
+    {
+      FT_UInt   first_code, code_count, offset;
+      FT_Int    delta;
+      FT_Byte*  ids;
+
+
+      first_code = TT_NEXT_USHORT(p);
+      code_count = TT_NEXT_USHORT(p);
+      delta      = TT_NEXT_SHORT(p);
+      offset     = TT_NEXT_USHORT(p);
+
+      /* check range within 0..255 */
+      if ( valid->level >= FT_VALIDATE_PARANOID )
+      {
+        if ( first_code >= 256 || first_code + code_count > 256 )
+          INVALID_DATA;
+      }
+
+      /* check offset */
+      if ( offset != 0 )
+      {
+        ids = p - 2 + offset;
+        if ( ids < glyph_ids || ids + code_count*2 > table + length )
+          INVALID_DATA;
+
+        /* check glyph ids */
+        if ( valid->level >= FT_VALIDATE_TIGHT )
+        {
+          FT_Byte*  limit = p + code_count*2;
+          FT_UInt   index;
+
+          for ( ; p < limit; )
+          {
+            index = TT_NEXT_USHORT(p);
+            if ( index != 0 )
+            {
+              index = (index + delta) & 0xFFFFU;
+              if ( index >= valid->num_glyphs )
+                INVALID_GLYPH_ID
+            }
+          }
+        }
+      }
+    }
+  }
+
+
+  /* return sub header corresponding to a given character code */
+  /* NULL on invalid charcode..                                */
+  static FT_Byte*
+  tt_cmap2_get_subheader( FT_Byte*  table,
+                          FT_ULong  char_code )
+  {
+    FT_Byte*  result = NULL;
+
+    if ( char_code < 0x10000 )
+    {
+      FT_UInt  char_lo = (FT_UInt)( char_code & 0xFF );
+      FT_UInt  char_hi = (FT_UInt)( char_code >> 8 );
+      FT_Byte* p       = table + 6;  /* keys table */
+      FT_Byte* subs    = p + 512;    /* subheaders table */
+      FT_Byte* sub;
+
+
+      if ( char_hi == 0 )
+      {
+        /* an 8-bit character code -- we use subHeader 0 in this case */
+        /* to test wheteher the character code is in the charmap      */
+        /*                                                            */
+        sub = subs;  /* jump to first sub-header */
+
+        /* check that the sub-header for this byte is 0, which */
+        /* indicates that it's really a valid one-byte value   */
+        /* Otherwise, return 0                                 */
+        /*                                                     */
+        p  += char_lo*2;
+        if ( PEEK_UShort(p) != 0 )
+          goto Exit;
+      }
+      else
+      {
+        /* a 16-bit character code */
+        p  += char_hi*2;                   /* jump to key entry */
+        sub = subs + PEEK_UShort(p);       /* jump to sub-header */
+
+        /* check that the hi byte isn't a valid one-byte value */
+        if ( sub == subs )
+          goto Exit;
+      }
+      result = sub;
+    }
+  Exit:
+    return result;
+  }
+
+
+  static FT_UInt
+  tt_cmap2_char_index( FT_Byte*   table,
+                       FT_ULong   char_code )
+  {
+    FT_UInt   result  = 0;
+    FT_Byte*  subheader;
+
+    subheader = tt_cmap2_get_subheader( table, char_code );
+    if ( subheader )
+    {
+      FT_Byte*  p     = subheader;
+      FT_UInt   index = (FT_UInt)(char_code & 0xFF);
+      FT_UInt   start, count;
+      FT_Int    delta;
+      FT_UInt   offset;
+
+      start  = TT_NEXT_USHORT(p);
+      count  = TT_NEXT_USHORT(p);
+      delta  = TT_NEXT_SHORT(p);
+      offset = PEEK_UShort(p);
+
+      index -= start;
+      if ( index < count && offset != 0 )
+      {
+        p    += offset + 2*index;
+        index = PEEK_UShort(p);
+
+        if ( index == 0 )
+          goto Exit;
+
+        result = (FT_UInt)( index + delta ) & 0xFFFFU;
+      }
+    }
+
+  Exit:
+    return result;
+  }
+
+
+ /* return first valid charcode in a format 2 sub-header */
+  static FT_ULong
+  tt_cmap2_subheader_first( FT_Byte*  subheader,
+                            FT_UInt   char_hi,
+                            FT_UInt  *agindex )
+  {
+    FT_ULong  result    = 0;
+    FT_UInt   n, gindex = 0;
+    FT_Byte*  p         = subheader;
+
+    FT_UInt   start  = TT_NEXT_USHORT(p);
+    FT_UInt   count  = TT_NEXT_USHORT(p);
+
+    if ( count > 0 )
+    {
+      FT_Int    delta  = TT_NEXT_SHORT(p);
+      FT_UInt   offset = TT_NEXT_USHORT(p);
+
+      if ( offset == 0 )
+      {
+        /* simple difference, compute directly */
+        result = char_hi*256 + start;
+        gindex = (FT_UInt)( start + delta ) & 0xFFFFU;
+      }
+      else
+      {
+        FT_UInt  i, index;
+
+        /* parse glyph id table for non-0 indices */
+        p += offset - 2;
+        for (; i < count; i++ )
+        {
+          index = TT_NEXT_USHORT(p);
+          if ( index != 0 )
+          {
+            result = char_hi*256 + start + i;
+            gindex = (FT_UInt)(index + delta) & 0xFFFFU;
+            break;
+          }
+        }
+      }
+    }
+
+    if ( agindex )
+      *agindex = gindex;
+
+    return result;
+  }
+
+
+  static FT_UInt
+  tt_cmap2_char_next( FT_Byte*  table,
+                      FT_ULong  char_code,
+                      FT_UInt   *agindex )
+  {
+    FT_UInt   result    = 0;
+    FT_UInt   n, gindex = 0;
+    FT_Byte*  subheader;
+    FT_Byte*  p;
+
+    ++char_code;
+    for (;;)
+    {
+      subheader = tt_cmap2_get_subheader( table, char_code );
+      if ( subheader )
+      {
+        FT_Byte*  p       = subheader;
+        FT_UInt   start   = TT_NEXT_USHORT(p);
+        FT_UInt   count   = TT_NEXT_USHORT(p);
+        FT_Int    delta   = TT_NEXT_SHORT(p);
+        FT_UInt   offset  = PEEK_UShort(p);
+        FT_UInt   char_lo = (FT_UInt)( char_code & 0xFF );
+        FT_UInt   pos, index;
+
+        if ( offset == 0 )
+          goto Next_SubHeader:
+
+        if ( char_lo < start )
+        {
+          char_lo = start;
+          pos     = 0;
+        }
+        else
+          pos = (FT_UInt)( char_lo - start );
+
+        p        += offset + pos*2;
+        char_code = (char_code & -256) + char_lo;
+
+        for ( ; pos < count; pos++, char_code++ )
+        {
+          index = TT_NEXT_USHORT(p);
+
+          if ( index != 0 )
+          {
+            gindex = ( index + delta ) & 0xFFFFU;
+            if ( gindex != 0 )
+            {
+              result = char_code;
+              goto Exit;
+            }
+          }
+        }
+      }
+
+      /* jump to next sub-header, i.e. higher byte value */
+    Next_SubHeader:
+      char_code = (char_code & -256) + 256;
+      if ( char_code >= 0x10000U )
+        break;
+    }
+
+  Exit:
+    if ( agindex )
+      *agindex = gindex;
+
+    return result;
+  }
+
+  static const TT_Cmap_ClassRec  tt_cmap2_class_rec =
+  {
+    (TT_CMap_ValidateFunc)  tt_cmap2_validate,
+    (TT_CMap_CharIndexFunc) tt_cmap2_char_index,
+    (TT_CMap_CharNextFunc)  tt_cmap2_char_next
+  };
+
+#endif /* TT_CONFIG_CMAP_FORMAT_2 */
+
+
+ /************************************************************************/
+ /************************************************************************/
+ /*****                                                              *****/
+ /*****                          FORMAT 4                            *****/
+ /*****                                                              *****/
+ /************************************************************************/
+ /************************************************************************/
+
+#ifdef TT_CONFIG_CMAP_FORMAT_4
+
+  static void
+  tt_cmap4_validate( FT_Byte*      table,
+                     FT_Validator  valid )
+  {
+    FT_Byte*  p      = table + 2;  /* skip format */
+    FT_UInt   length = TT_NEXT_USHORT(p);
+    FT_Byte   *ends, *starts, *offsets, *glyph_ids;
+    FT_UInt   n, num_segs;
+
+    if ( table + length > valid->limit || length < 16 )
+      TOO_SHORT;
+
+    p += 2;  /* skip language */
+
+    num_segs = TT_NEXT_USHORT(p);   /* read segCountX2 */
+
+    if ( valid->level >= FT_VALIDATE_PARANOID )
+    {
+      /* check that we have an even value here */
+      if ( num_segs & 1 )
+        INVALID_DATA;
+    }
+
+    num_segs /= 2;
+
+    /* check the search parameters - even though we never use them */
+    /*                                                             */
+    if ( valid->level >= FT_VALIDATE_PARANOID )
+    {
+      /* check the values of 'searchRange', 'entrySelector', 'rangeShift' */
+      FT_UInt  search_range   = TT_NEXT_USHORT(p);
+      FT_UInt  entry_selector = TT_NEXT_USHORT(p);
+      FT_UInt  range_shift    = TT_NEXT_USHORT(p);
+
+      if ( (search_range | range_shift) & 1 )  /* must be even values */
+        INVALID_DATA;
+
+      search_range /= 2;
+      range_shift  /= 2;
+
+      /* 'search range' is the greatest power of 2 that is <= num_segs */
+
+      if ( search_range > num_segs                ||
+           search_range*2 < num_segs              ||
+           search_range + range_shift != num_segs ||
+           search_range != (1 << entry_selector)  )
+        INVALID_DATA;
+    }
+    else
+      p += 6;
+
+    ends      = p;
+    starts    = ends + num_segs*2 + 2;
+    offsets   = starts + num_segs*4;
+    glyph_ids = offsets + num_segs*2;
+
+    if ( glyph_ids >= table + length )
+      TOO_SHORT;
+
+    /* check last segment, its end count must be FFFF */
+    if ( valid->level >= FT_VALIDATE_PARANOID )
+    {
+      p = ends + (num_segs-1)*2;
+      if ( PEEK_UShort(p) != 0xFFFFU )
+        INVALID_DATA;
+    }
+
+    /* check that segments are sorted in increasing order and do not overlap */
+    /* check also the offsets..                                              */
+    {
+      FT_UInt  start, end, last = 0,offset, n;
+
+      for ( n = 0; n < num_segs; n++ )
+      {
+        p = starts + n*2;  start  = PEEK_UShort(p);
+        p = ends   + n*2;  end    = PEEK_UShort(p);
+        p = offsets + n*2; offset = PEEK_UShort(p);
+
+        if ( end > start )
+          INVALID_DATA;
+
+        if ( n > 0 && start <= last )
+          INVALID_DATA;
+
+        if ( offset )
+        {
+          p += offset;  /* start of glyph id array */
+
+          /* check that we point within the glyph ids table only */
+          if ( p < glyph_ids || p + (end - start + 1) > table + length )
+            INVALID_DATA;
+
+          /* XXXX: check glyph ids !! */
+        }
+        last = end;
+      }
+    }
+  }
+
+
+
+  static FT_UInt
+  tt_cmap4_char_index( FT_Byte*  table,
+                       FT_ULong  char_code )
+  {
+    FT_UInt   result = 0;
+
+    if ( char_code < 0x10000U )
+    {
+      FT_Byte*  p;
+      FT_UInt   start, end, index, num_segs2;
+      FT_Int    delta, segment;
+      FT_UInt   code = (FT_UInt)char_code;
+
+      p         = table + 6;
+      num_segs2 = PEEK_UShort(p);
+
+      p = table + 14;               /* ends table   */
+      q = table + 16 + num_segs2;   /* starts table */
+
+      for ( n = 0; n < num_segs2; n += 2 )
+      {
+        FT_UInt  end   = TT_NEXT_USHORT(p);
+        FT_UInt  start = TT_NEXT_USHORT(q);
+
+        if ( code < start )
+          break;
+
+        if ( code <= end )
+        {
+          index = (FT_UInt)( char_code - start );
+
+          p  = q + num_segs2 - 2; delta  = PEEK_Short(p);
+          p += num_segs2;         offset = PEEK_UShort(p);
+
+          if ( offset != 0 )
+          {
+            p    += offset + 2*index;
+            index = PEEK_UShort(p);
+          }
+
+          if ( index != 0 )
+            result = (FT_UInt)( index + delta ) & 0xFFFFU;
+        }
+      }
+    }
+    return result;
+  }
+
+
+
+  static FT_ULong
+  tt_cmap4_char_next( FT_Byte*  table,
+                      FT_ULong  char_code,
+                      FT_UInt  *agindex )
+  {
+    FT_ULong  result = 0;
+    FT_UInt   gindex = 0;
+    FT_Byte*  p;
+    FT_UInt   code, num_segs2;
+
+    ++char_code;
+    if ( char_code >= 0x10000U )
+      goto Exit;
+
+    code      = (FT_UInt)char_code;
+    p         = table + 6;
+    num_segs2 = PEEK_UShort(p) & -2;  /* ensure even-ness */
+
+    for (;;)
+    {
+      FT_UInt   start, end, index, n;
+      FT_Int    delta;
+
+      p = table + 14;              /* ends table  */
+      q = table + 16 + num_segs2;  /* starts table */
+
+      for ( n = 0; n < num_segs2; n += 2 )
+      {
+        FT_UInt  end   = TT_NEXT_USHORT(p);
+        FT_UInt  start = TT_NEXT_USHORT(q);
+
+        if ( code < start )
+          code = start;
+
+        if ( code <= end )
+        {
+          p  = q + num_segs2 - 2; delta  = PEEK_Short(p);
+          p += num_segs2;         offset = PEEK_UShort(p);
+
+          if ( offset != 0 )
+          {
+            /* parse the glyph ids array for non-0 index */
+            p += offset + (code - start)*2;
+            while ( code <= end )
+            {
+              gindex = TT_NEXT_USHORT(p);
+              if ( gindex != 0 )
+              {
+                gindex = (FT_UInt)( gindex + delta ) & 0xFFFFU;
+                if ( gindex != 0 )
+                  break;
+              }
+              code++;
+            }
+          }
+          else
+            gindex = (FT_UInt)( code + delta ) & 0xFFFFU;
+
+          if ( gindex == 0 )
+            break;
+
+          result = code;
+          goto Exit;
+        }
+      }
+
+      /* loop to next trial charcode */
+      if ( code >= 0xFFFFU )
+        break;
+
+      code++;
+    }
+    return result;
+
+  Exit:
+    if ( agindex )
+      *agindex = gindex;
+
+    return result;
+  }
+
+  static const TT_Cmap_ClassRec  tt_cmap4_class_rec =
+  {
+    (TT_CMap_ValidateFunc)  tt_cmap4_validate,
+    (TT_CMap_CharIndexFunc) tt_cmap4_char_index,
+    (TT_CMap_CharNextFunc)  tt_cmap4_char_next
+  };
+
+#endif /* TT_CONFIG_CMAP_FORMAT_4 */
+
+ /************************************************************************/
+ /************************************************************************/
+ /*****                                                              *****/
+ /*****                          FORMAT 6                            *****/
+ /*****                                                              *****/
+ /************************************************************************/
+ /************************************************************************/
+
+#ifdef TT_CONFIG_CMAP_FORMAT_6
+
+  static void
+  tt_cmap6_validate( FT_Byte*      table,
+                     FT_Validator  valid )
+  {
+    FT_Byte*  p = table + 2;
+    FT_UInt   length, start, count;
+
+    if ( table + 10 > valid->limit )
+      INVALID_TOO_SHORT;
+
+    length = TT_NEXT_USHORT(p);
+    p     += 2;  /* skip language */
+    start  = TT_NEXT_USHORT(p);
+    count  = TT_NEXT_USHORT(p);
+
+    if ( table + length > valid->limit || length < 10 + count*2 )
+      INVALID_TOO_SHORT;
+
+    /* check glyph indices */
+    if ( valid->level >= FT_VALIDATE_TIGHT )
+    {
+      FT_UInt  gindex;
+
+      for ( ; count > 0; count-- )
+      {
+        gindex = TT_NEXT_USHORT(p);
+        if ( gindex >= valid->num_glyphs )
+          INVALID_GLYPH_ID;
+      }
+    }
+  }
+
+
+  static FT_UInt
+  tt_cmap6_char_index( FT_Byte*   table,
+                       FT_ULong   char_code )
+  {
+    FT_UInt   result = 0;
+    FT_Byte*  p      = table + 6;
+    FT_UInt   start  = TT_NEXT_USHORT(p);
+    FT_UInt   count  = TT_NEXT_USHORT(p);
+    FT_UInt   index  = (FT_UInt)( char_code - start );
+
+    if ( index < count )
+    {
+      p += 2*index;
+      result = PEEK_UShort(p);
+    }
+    return result;
+  }
+
+
+  static FT_ULong
+  tt_cmap6_char_next( FT_Byte*    table,
+                      FT_ULong    char_code,
+                      FT_UInt    *agindex )
+  {
+    FT_ULong  result = 0;
+    FT_UInt   gindex = 0;
+    FT_Byte*  p      = table + 6;
+    FT_UInt   start  = TT_NEXT_USHORT(p);
+    FT_UInt   count  = TT_NEXT_USHORT(p);
+    FT_UInt   code, index;
+
+    char_code++;
+    if ( char_code >= 0x10000U )
+      goto Exit;
+
+    if ( char_code < start )
+      char_code = start;
+
+    index = (FT_UInt)( char_code - start );
+    p    += 2*index;
+
+    for ( ; index < count; index++ )
+    {
+      gindex = TT_NEXT_USHORT(p);
+      if ( gindex != 0 )
+      {
+        result = char_code;
+        break;
+      }
+      char_code++;
+    }
+
+  Exit:
+    if ( agindex )
+      *agindex = gindex;
+
+    return result;
+  }
+
+  static const TT_Cmap_ClassRec  tt_cmap6_class_rec =
+  {
+    (TT_CMap_ValidateFunc)  tt_cmap6_validate,
+    (TT_CMap_CharIndexFunc) tt_cmap6_char_index,
+    (TT_CMap_CharNextFunc)  tt_cmap6_char_next
+  };
+
+#endif /* TT_CONFIG_CMAP_FORMAT_6 */
+
+
+ /************************************************************************/
+ /************************************************************************/
+ /*****                                                              *****/
+ /*****                          FORMAT 8                            *****/
+ /*****                                                              *****/
+ /*****  It's hard to completely understand what the OpenType        *****/
+ /*****  spec says about this format, but here are my conclusion     *****/
+ /*****                                                              *****/
+ /*****  the purpose of this format is to easily map UTF-16 text     *****/
+ /*****  to glyph indices. Basically, the 'char_code' must be in     *****/
+ /*****  one of the following formats:                               *****/
+ /*****                                                              *****/
+ /*****    - a 16-bit value that isn't part of the Unicode           *****/
+ /*****      Surrogates Area (i.e. U+D800-U+DFFF)                    *****/
+ /*****                                                              *****/
+ /*****    - a 32-bit value, made of two surrogate values, i.e.      *****/
+ /*****      if "char_code = (char_hi << 16) | char_lo", then        *****/
+ /*****      both 'char_hi' and 'char_lo' must be in the Surrogates  *****/
+ /*****      Area.                                                   *****/
+ /*****                                                              *****/
+ /*****  The 'is32' table embedded in the charmap indicates          *****/
+ /*****  wether a given 16-bit value is in the surrogates area       *****/
+ /*****  or not..                                                    *****/
+ /*****                                                              *****/
+ /*****  so, for any given "char_code", we can assert the following  *****/
+ /*****                                                              *****/
+ /*****   if 'char_hi == 0' then we must have 'is32[char_lo] == 0'   *****/
+ /*****                                                              *****/
+ /*****   if 'char_hi != 0' then we must have both                   *****/
+ /*****   'is32[char_hi] != 0' and 'is32[char_lo] != 0'              *****/
+ /*****                                                              *****/
+ /*****                                                              *****/
+ /************************************************************************/
+ /************************************************************************/
+
+#ifdef TT_CONFIG_CMAP_FORMAT_8
+
+  static void
+  tt_cmap8_validate( FT_Byte*      table,
+                     FT_Validator  valid )
+  {
+    FT_Byte*   p = table + 4;
+    FT_Byte*   is32;
+    FT_ULong   length;
+    FT_ULong   num_groups;
+
+    if ( table + 16 + 8192 > valid->limit )
+      INVALID_TOO_SHORT;
+
+    length = TT_NEXT_ULONG(p);
+    if ( table + length > valid->limit || length < 16 + 8192 )
+      INVALID_TOO_SHORT;
+
+    is32       = p + 4;        /* skip language     */
+    p          = is32 + 8192;  /* skip 'is32' array */
+    num_groups = TT_NEXT_ULONG(p);
+
+    if ( p + num_groups*12 > valid->limit )
+      INVALID_TOO_SHORT;
+
+    /* check groups, they must be in increasing order */
+    {
+      FT_ULong  n, start, end, start_id, count, last = 0;
+
+      for ( n = 0; n < num_groups; n++ )
+      {
+        FT_Bytes* q;
+        FT_UInt   hi, lo;
+
+        start    = TT_NEXT_ULONG(p);
+        end      = TT_NEXT_ULONG(p);
+        start_id = TT_NEXT_ULONG(p);
+
+        if ( start > end )
+          INVALID_DATA;
+
+        if ( n > 0 && start <= last )
+          INVALID_DATA;
+
+        if ( valid->level >= FT_VALIDATE_TIGHT )
+        {
+          if ( start_id + end - start >= valid->num_glyphs )
+            INVALID_GLYPH_ID;
+
+          count = (FT_ULong)(end - start + 1);
+
+          if ( start & ~0xFFFFU )
+          {
+            /* start_hi != 0, check that is32[i] is 1 for each i in */
+            /* the 'hi' and 'lo' of the range [start..end]          */
+            for ( ; count > 0; count--, start++ )
+            {
+              hi = (FT_UInt)(start >> 16);
+              lo = (FT_UInt)(start & 0xFFFFU);
+  
+              if ( is32[ hi >> 3 ] & (0x80 >> (hi & 7)) == 0 )
+                INVALID_DATA;
+  
+              if ( is32[ lo >> 3 ] & (0x80 >> (lo & 7)) == 0 )
+                INVALID_DATA;
+            }
+          }
+          else
+          {
+            /* start_hi == 0, check that is32[i] is 0 for each i in */
+            /* the range [start..end]                               */
+  
+            /* end_hi cannot be != 0 !! */
+            if ( end & ~0xFFFFU )
+              INVALID_DATA;
+  
+            for ( ; count > 0; count--, start++ )
+            {
+              lo = (FT_UInt)(start & 0xFFFFU);
+  
+              if ( is32[ lo >> 3 ] & (0x80 >> (lo & 7)) != 0 )
+                INVALID_DATA;
+            }
+          }
+        }
+      }
+    }
+  }
+
+
+  static FT_UInt
+  tt_cmap8_char_index( FT_Byte*   table,
+                       FT_ULong   char_code )
+  {
+    FT_UInt   result     = 0;
+    FT_Byte*  p          = table + 12 + 8192;
+    FT_ULong  num_groups = TT_NEXT_ULONG(p);
+    FT_ULong  n, start, end, start_id;
+    
+    for ( ; num_groups > 0; num_groups-- )
+    {
+      start    = TT_NEXT_ULONG(p);
+      end      = TT_NEXT_ULONG(p);
+      start_id = TT_NEXT_ULONG(p);
+      
+      if ( char_code < start )
+        break;
+        
+      if ( char_code <= end )
+      {
+        result = start_id + char_code - start;
+        break;
+      }
+    }
+    return result;
+  }
+
+
+  static FT_ULong
+  tt_cmap8_char_next( FT_Byte*   table,
+                      FT_ULong   char_code,
+                      FT_UInt   *agindex )
+  {
+    FT_ULong   result     = 0;
+    FT_UInt    gindex     = 0;
+    FT_Byte*   p          = table + 12 + 8192;
+    FT_ULong   num_groups = TT_NEXT_USHORT(p);
+    FT_ULong   n, start, end, start_id;
+    
+    ++char_code;
+    p = table + 16 + 8192;
+    
+    for ( n = 0; n < num_groups++; n++ )
+    {
+      start    = TT_NEXT_ULONG(p);
+      end      = TT_NEXT_ULONG(p);
+      start_id = TT_NEXT_ULONG(p);
+      
+      if ( char_code < start )
+        char_code = start;
+      
+      if ( char_code <= end )
+      {
+        gindex = (FT_UInt)(char_code - start + start_id);
+        if ( gindex != 0 )
+        {
+          result = char_code;
+          goto Exit;
+        }
+      }
+    }
+    
+  Exit:
+    if ( agindex )
+      *agindex = gindex;
+    
+    return result;
+  }                      
+
+
+  static const TT_Cmap_ClassRec  tt_cmap8_class_rec =
+  {
+    (TT_CMap_ValidateFunc)  tt_cmap8_validate,
+    (TT_CMap_CharIndexFunc) tt_cmap8_char_index,
+    (TT_CMap_CharNextFunc)  tt_cmap8_char_next
+  };
+
+#endif /* TT_CONFIG_CMAP_FORMAT_8 */
+
+ /************************************************************************/
+ /************************************************************************/
+ /*****                                                              *****/
+ /*****                          FORMAT 10                           *****/
+ /*****                                                              *****/
+ /************************************************************************/
+ /************************************************************************/
+
+#ifdef TT_CONFIG_CMAP_FORMAT_10
+
+  static void
+  tt_cmap10_validate( FT_Byte*      table,
+                      FT_Validator  valid )
+  {
+    FT_Byte*  p = table + 2;
+    FT_ULong  length, start, count;
+
+    if ( table + 20 > valid->limit )
+      INVALID_TOO_SHORT;
+
+    length = TT_NEXT_USHORT(p);
+    p     += 4;  /* skip language */
+    start  = TT_NEXT_ULONG(p);
+    count  = TT_NEXT_ULONG(p);
+
+    if ( table + length > valid->limit || length < 20 + count*2 )
+      INVALID_TOO_SHORT;
+
+    /* check glyph indices */
+    if ( valid->level >= FT_VALIDATE_TIGHT )
+    {
+      FT_UInt  gindex;
+
+      for ( ; count > 0; count-- )
+      {
+        gindex = TT_NEXT_USHORT(p);
+        if ( gindex >= valid->num_glyphs )
+          INVALID_GLYPH_ID;
+      }
+    }
+  }
+
+
+  static FT_UInt
+  tt_cmap10_char_index( FT_Byte*   table,
+                        FT_ULong   char_code )
+  {
+    FT_UInt   result = 0;
+    FT_Byte*  p      = table + 12;
+    FT_ULong  start  = TT_NEXT_ULONG(p);
+    FT_ULong  count  = TT_NEXT_ULONG(p);
+    FT_ULong  index  = (FT_ULong)( char_code - start );
+
+    if ( index < count )
+    {
+      p += 2*index;
+      result = PEEK_UShort(p);
+    }
+    return result;
+  }
+
+
+  static FT_ULong
+  tt_cmap10_char_next( FT_Byte*    table,
+                       FT_ULong    char_code,
+                       FT_UInt    *agindex )
+  {
+    FT_ULong  result = 0;
+    FT_UInt   gindex = 0;
+    FT_Byte*  p      = table + 12;
+    FT_ULong  start  = TT_NEXT_ULONG(p);
+    FT_ULong  count  = TT_NEXT_ULONG(p);
+    FT_ULong  index;
+
+    char_code++;
+    if ( char_code >= 0x10000U )
+      goto Exit;
+
+    if ( char_code < start )
+      char_code = start;
+
+    index = (FT_ULong)( char_code - start );
+    p    += 2*index;
+
+    for ( ; index < count; index++ )
+    {
+      gindex = TT_NEXT_USHORT(p);
+      if ( gindex != 0 )
+      {
+        result = char_code;
+        break;
+      }
+      char_code++;
+    }
+
+  Exit:
+    if ( agindex )
+      *agindex = gindex;
+
+    return result;
+  }
+
+  static const TT_Cmap_ClassRec  tt_cmap10_class_rec =
+  {
+    (TT_CMap_ValidateFunc)  tt_cmap10_validate,
+    (TT_CMap_CharIndexFunc) tt_cmap10_char_index,
+    (TT_CMap_CharNextFunc)  tt_cmap10_char_next
+  };
+
+#endif /* TT_CONFIG_CMAP_FORMAT_10 */
+
+
+ /************************************************************************/
+ /************************************************************************/
+ /*****                                                              *****/
+ /*****                          FORMAT 12                           *****/
+ /*****                                                              *****/
+ /************************************************************************/
+ /************************************************************************/
+
+#ifdef TT_CONFIG_CMAP_FORMAT_12
+
+  static void
+  tt_cmap12_validate( FT_Byte*      table,
+                      FT_Validator  valid )
+  {
+  }                      
+
+
+  static FT_UInt
+  tt_cmap12_char_index( FT_Byte*   table,
+                        FT_ULong   char_code )
+  {
+  }                        
+
+
+  static FT_ULong
+  tt_cmap12_char_next( FT_Byte*   table,
+                       FT_ULong   char_code,
+                       FT_UInt   *agindex )
+  {
+  }                       
+
+
+  static const TT_Cmap_ClassRec  tt_cmap12_class_rec =
+  {
+    (TT_CMap_ValidateFunc)  tt_cmap12_validate,
+    (TT_CMap_CharIndexFunc) tt_cmap12_char_index,
+    (TT_CMap_CharNextFunc)  tt_cmap12_char_next
+  };
+
+#endif /* TT_CONFIG_CMAP_FORMAT_12 */
+
diff --git a/src/sfnt/ttcmap0.h b/src/sfnt/ttcmap0.h
new file mode 100644
index 0000000..338ae2f
--- /dev/null
+++ b/src/sfnt/ttcmap0.h
@@ -0,0 +1,45 @@
+/***************************************************************************/
+/*                                                                         */
+/*  ttcmap.h                                                               */
+/*                                                                         */
+/*    TrueType character mapping table (cmap) support (specification).     */
+/*                                                                         */
+/*  Copyright 1996-2001 by                                                 */
+/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
+/*                                                                         */
+/*  This file is part of the FreeType project, and may only be used,       */
+/*  modified, and distributed under the terms of the FreeType project      */
+/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
+/*  this file you indicate that you have read the license and              */
+/*  understand and accept it fully.                                        */
+/*                                                                         */
+/***************************************************************************/
+
+
+#ifndef __TTCMAP_H__
+#define __TTCMAP_H__
+
+
+#include <ft2build.h>
+#include FT_INTERNAL_TRUETYPE_TYPES_H
+#include FT_INTERNAL_OBJECTS_H
+
+
+FT_BEGIN_HEADER
+
+  FT_LOCAL FT_Error
+  TT_CharMap_Load( TT_Face        face,
+                   TT_CMapTable*  cmap,
+                   FT_Stream      input );
+
+  FT_LOCAL FT_Error
+  TT_CharMap_Free( TT_Face        face,
+                   TT_CMapTable*  cmap );
+
+
+FT_END_HEADER
+
+#endif /* __TTCMAP_H__ */
+
+
+/* END */