Commit e3f41982a2391d2fcb61b8883af98aa4e5b82df1

Werner Lemberg 2003-10-16T15:48:39

Completely revised Type 42 parser. It now handles both fonts produced with ttftot42 (tested version 0.3.1) and TrueTypeToType42.ps (tested version May 2001; it is necessary to fix the broken header comment to be `%!PS-TrueTypeFont...'). * src/type42/t42objs.c (T42_GlyphSlot_Load): Change fourth parameter to `FT_UInt'. * src/type42/t42objs.h: Updated. * src/type42/t42parse.h (T42_ParserRec): Change type of `in_memory' to FT_Bool. (T42_Loader): Change type of `num_chars' and `num_glyphs' to FT_UInt. Add `swap_table' element. * src/type42/t42parse.c (T42_KEYWORD_COUNT, T1_ToFixed, T1_ToCoordArray, T1_ToTokenArray): Removed. (T1_ToBytes): New macro. (t42_is_alpha, t42_hexval): Removed. (t42_is_space): Handle `\0'. (t42_parse_encoding): Updated to use new PostScript parser routines from psaux. Handle `/Encoding [ ... ]' also. (T42_Load_Status): New enumeration. (t42_parse_sfnts): Updated to use new PostScript parser routines from psaux. (t42_parse_charstrings): Updated to use new PostScript parser routines from psaux. Handle `/CharStrings << ... >>' also. Don't expect that /.notdef is the first element in dictionary. Copy code from type1 module to handle this. (t42_parse_dict): Updated to use new PostScript parser routines from psaux. Remove code for synthetic fonts (which can't occur in Type 42 fonts). (t42_loader_done): Release `swap_table'. * src/psaux/psobjs.c (skip_string): Increase `cur' properly. * src/type1/t1load.c (parse_charstrings): Make test for `.notdef' faster.

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
diff --git a/ChangeLog b/ChangeLog
index 6a8eca1..bb9e846 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,47 @@
-2003-10-15:  Graham Asher  <graham.asher@btinternet.com>
+2003-10-16  Werner Lemberg  <wl@gnu.org>
+
+	Completely revised Type 42 parser.  It now handles both fonts
+	produced with ttftot42 (tested version 0.3.1) and
+	TrueTypeToType42.ps (tested version May 2001; it is necessary to
+	fix the broken header comment to be `%!PS-TrueTypeFont...').
+
+	* src/type42/t42objs.c (T42_GlyphSlot_Load): Change fourth
+	parameter to `FT_UInt'.
+	* src/type42/t42objs.h: Updated.
+
+	* src/type42/t42parse.h (T42_ParserRec): Change type of `in_memory'
+	to FT_Bool.
+	(T42_Loader): Change type of `num_chars' and `num_glyphs' to
+	FT_UInt.
+	Add `swap_table' element.
+	* src/type42/t42parse.c (T42_KEYWORD_COUNT, T1_ToFixed,
+	T1_ToCoordArray, T1_ToTokenArray): Removed.
+	(T1_ToBytes): New macro.
+	(t42_is_alpha, t42_hexval): Removed.
+	(t42_is_space): Handle `\0'.
+	(t42_parse_encoding): Updated to use new PostScript parser routines
+	from psaux.
+	Handle `/Encoding [ ... ]' also.
+	(T42_Load_Status): New enumeration.
+	(t42_parse_sfnts): Updated to use new PostScript parser routines
+	from psaux.
+	(t42_parse_charstrings): Updated to use new PostScript parser
+	routines from psaux.
+	Handle `/CharStrings << ... >>' also.
+	Don't expect that /.notdef is the first element in dictionary.  Copy
+	code from type1 module to handle this.
+	(t42_parse_dict): Updated to use new PostScript parser routines
+	from psaux.
+	Remove code for synthetic fonts (which can't occur in Type 42
+	fonts).
+	(t42_loader_done): Release `swap_table'.
+
+	* src/psaux/psobjs.c (skip_string): Increase `cur' properly.
+
+	* src/type1/t1load.c (parse_charstrings): Make test for `.notdef'
+	faster.
+
+2003-10-15  Graham Asher  <graham.asher@btinternet.com>
 
 	* src/autohint/ahglobal.c (blue_chars), src/winfonts/winfnt.c
 	(fnt_cmap_class_rec, fnt_cmap_class), src/bdf/bdflib.c (empty,
diff --git a/src/psaux/psobjs.c b/src/psaux/psobjs.c
index adc9772..c7f2601 100644
--- a/src/psaux/psobjs.c
+++ b/src/psaux/psobjs.c
@@ -410,9 +410,7 @@
     FT_Byte*  limit = parser->limit;
 
 
-    cur++;
-
-    while ( cur < limit )
+    while ( ++cur < limit )
     {
       int  d;
 
diff --git a/src/type1/t1load.c b/src/type1/t1load.c
index e563d63..0aee92e 100644
--- a/src/type1/t1load.c
+++ b/src/type1/t1load.c
@@ -1190,9 +1190,7 @@
 
       /* the format is simple:                    */
       /*   `/glyphname' + binary data             */
-      /*                                          */
-      /* note that we stop when we find a `def'   */
-      /*                                          */
+
       T1_Skip_Spaces( parser );
 
       cur = parser->root.cursor;
@@ -1236,8 +1234,9 @@
         /* add a trailing zero to the name table */
         name_table->elements[n][len] = '\0';
 
-        /* record index of /.notdef              */
-        if ( ft_strcmp( (const char*)".notdef",
+        /* record index of /.notdef */
+        if ( *cur == '.'                                              &&
+             ft_strcmp( ".notdef",
                         (const char*)(name_table->elements[n]) ) == 0 )
         {
           notdef_index = n;
@@ -1274,7 +1273,7 @@
 
     loader->num_glyphs = n;
 
-    /* if /.notdef is found but does not occupy index 0, do our magic.      */
+    /* if /.notdef is found but does not occupy index 0, do our magic. */
     if ( ft_strcmp( (const char*)".notdef",
                     (const char*)name_table->elements[0] ) &&
          notdef_found                                      )
diff --git a/src/type42/t42drivr.c b/src/type42/t42drivr.c
index faa891e..b33c92c 100644
--- a/src/type42/t42drivr.c
+++ b/src/type42/t42drivr.c
@@ -24,10 +24,9 @@
   /* 2) Incremental fonts making use of the GlyphDirectory keyword         */
   /*    will be loaded, but the rendering will be using the TrueType       */
   /*    tables.                                                            */
-  /* 3) The sfnts array is expected to be ASCII, not binary.               */
-  /* 4) As for Type1 fonts, CDevProc is not supported.                     */
-  /* 5) The Metrics dictionary is not supported.                           */
-  /* 6) AFM metrics are not supported.                                     */
+  /* 3) As for Type1 fonts, CDevProc is not supported.                     */
+  /* 4) The Metrics dictionary is not supported.                           */
+  /* 5) AFM metrics are not supported.                                     */
   /*                                                                       */
   /* In other words, this driver supports Type42 fonts derived from        */
   /* TrueType fonts in a non-CID manner, as done by usual conversion       */
diff --git a/src/type42/t42objs.c b/src/type42/t42objs.c
index e5a5748..c4ce3fe 100644
--- a/src/type42/t42objs.c
+++ b/src/type42/t42objs.c
@@ -53,7 +53,8 @@
     if ( error )
       goto Exit;
 
-    error = t42_parse_dict( face, &loader, parser->base_dict, parser->base_len );
+    error = t42_parse_dict( face, &loader,
+                            parser->base_dict, parser->base_len );
 
     if ( type1->font_type != 42 )
     {
@@ -65,7 +66,8 @@
     /* to the Type1 data                                    */
     type1->num_glyphs = loader.num_glyphs;
 
-    if ( !loader.charstrings.init ) {
+    if ( !loader.charstrings.init )
+    {
       FT_ERROR(( "T42_Open_Face: no charstrings array in face!\n" ));
       error = T42_Err_Invalid_File_Format;
     }
@@ -121,8 +123,10 @@
               if ( ft_strcmp( (const char*)".notdef",
                               (const char*)glyph_name ) != 0 )
               {
-                if ( charcode < min_char ) min_char = charcode;
-                if ( charcode > max_char ) max_char = charcode;
+                if ( charcode < min_char )
+                  min_char = charcode;
+                if ( charcode > max_char )
+                  max_char = charcode;
               }
               break;
             }
@@ -149,12 +153,12 @@
                  FT_Int         num_params,
                  FT_Parameter*  params )
   {
-    FT_Error         error;
+    FT_Error            error;
     FT_Service_PsNames  psnames;
-    PSAux_Service    psaux;
-    FT_Face          root  = (FT_Face)&face->root;
-    T1_Font          type1 = &face->type1;
-    PS_FontInfo      info  = &type1->font_info;
+    PSAux_Service       psaux;
+    FT_Face             root  = (FT_Face)&face->root;
+    T1_Font             type1 = &face->type1;
+    PS_FontInfo         info  = &type1->font_info;
 
     FT_UNUSED( num_params );
     FT_UNUSED( params );
@@ -189,7 +193,7 @@
       goto Exit;
     }
 
-    /* Now, load the font program into the face object */
+    /* Now load the font program into the face object */
 
     /* Init the face object fields */
     /* Now set up root face fields */
@@ -350,7 +354,7 @@
 
 #if 0
         /* Select default charmap */
-        if (root->num_charmaps)
+        if ( root->num_charmaps )
           root->charmap = root->charmaps[0];
 #endif
       }
@@ -452,8 +456,6 @@
   }
 
 
-
-
   FT_LOCAL_DEF( FT_Error )
   T42_Size_Init( T42_Size  size )
   {
@@ -601,7 +603,7 @@
   FT_LOCAL_DEF( FT_Error )
   T42_GlyphSlot_Load( FT_GlyphSlot  glyph,
                       FT_Size       size,
-                      FT_Int        glyph_index,
+                      FT_UInt       glyph_index,
                       FT_Int32      load_flags )
   {
     FT_Error         error;
diff --git a/src/type42/t42objs.h b/src/type42/t42objs.h
index 17f5894..c155951 100644
--- a/src/type42/t42objs.h
+++ b/src/type42/t42objs.h
@@ -102,7 +102,7 @@ FT_BEGIN_HEADER
   FT_LOCAL( FT_Error )
   T42_GlyphSlot_Load( FT_GlyphSlot  glyph,
                       FT_Size       size,
-                      FT_Int        glyph_index,
+                      FT_UInt       glyph_index,
                       FT_Int32      load_flags );
 
   FT_LOCAL( void )
diff --git a/src/type42/t42parse.c b/src/type42/t42parse.c
index 7ce4707..f3b4088 100644
--- a/src/type42/t42parse.c
+++ b/src/type42/t42parse.c
@@ -93,10 +93,6 @@
   };
 
 
-#define T42_KEYWORD_COUNT                                          \
-          ( sizeof ( t42_keywords ) / sizeof ( t42_keywords[0] ) )
-
-
 #define T1_Add_Table( p, i, o, l )  (p)->funcs.add( (p), i, o, l )
 #define T1_Done_Table( p )          \
           do                        \
@@ -114,17 +110,15 @@
 #define T1_Skip_Spaces( p )    (p)->root.funcs.skip_spaces( &(p)->root )
 #define T1_Skip_PS_Token( p )  (p)->root.funcs.skip_PS_token( &(p)->root )
 
-#define T1_ToInt( p )       (p)->root.funcs.to_int( &(p)->root )
-#define T1_ToFixed( p, t )  (p)->root.funcs.to_fixed( &(p)->root, t )
+#define T1_ToInt( p )                          \
+          (p)->root.funcs.to_int( &(p)->root )
+#define T1_ToBytes( p, b, m, n, d )                          \
+          (p)->root.funcs.to_bytes( &(p)->root, b, m, n, d )
 
-#define T1_ToCoordArray( p, m, c )                           \
-          (p)->root.funcs.to_coord_array( &(p)->root, m, c )
 #define T1_ToFixedArray( p, m, f, t )                           \
           (p)->root.funcs.to_fixed_array( &(p)->root, m, f, t )
 #define T1_ToToken( p, t )                          \
           (p)->root.funcs.to_token( &(p)->root, t )
-#define T1_ToTokenArray( p, t, m, c )                           \
-          (p)->root.funcs.to_token_array( &(p)->root, t, m, c )
 
 #define T1_Load_Field( p, f, o, m, pf )                         \
           (p)->root.funcs.load_field( &(p)->root, f, o, m, pf )
@@ -195,9 +189,9 @@
     }
 
     /* Now check font format; we must see `%!PS-TrueTypeFont' */
-    if (size <= 17                                    ||
-        ( ft_strncmp( (const char*)parser->base_dict,
-                      "%!PS-TrueTypeFont", 17) )      )
+    if ( size <= 17                                    ||
+         ( ft_strncmp( (const char*)parser->base_dict,
+                       "%!PS-TrueTypeFont", 17 ) )     )
       error = T42_Err_Unknown_File_Format;
     else
     {
@@ -229,23 +223,11 @@
 
 
   static int
-  t42_is_alpha( FT_Byte  c )
-  {
-    /* Note: we must accept "+" as a valid character, as it is used in */
-    /*       embedded type1 fonts in PDF documents.                    */
-    /*                                                                 */
-    return ( ft_isalnum( c ) ||
-             c == '.'        ||
-             c == '_'        ||
-             c == '-'        ||
-             c == '+'        );
-  }
-
-
-  static int
   t42_is_space( FT_Byte  c )
   {
-    return ( c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' );
+    return ( c == ' '  || c == '\t'              ||
+             c == '\r' || c == '\n' || c == '\f' ||
+             c == '\0'                           );
   }
 
 
@@ -288,8 +270,8 @@
     matrix->yy = temp[3];
 
     /* note that the offsets must be expressed in integer font units */
-    offset->x  = temp[4] >> 16;
-    offset->y  = temp[5] >> 16;
+    offset->x = temp[4] >> 16;
+    offset->y = temp[5] >> 16;
   }
 
 
@@ -297,49 +279,53 @@
   t42_parse_encoding( T42_Face    face,
                       T42_Loader  loader )
   {
-    T42_Parser     parser = &loader->parser;
-    FT_Byte*       cur    = parser->root.cursor;
-    FT_Byte*       limit  = parser->root.limit;
+    T42_Parser  parser = &loader->parser;
+    FT_Byte*    cur;
+    FT_Byte*    limit  = parser->root.limit;
 
     PSAux_Service  psaux  = (PSAux_Service)face->psaux;
 
 
-    /* skip whitespace */
-    while ( t42_is_space( *cur ) )
+    T1_Skip_Spaces( parser );
+    cur = parser->root.cursor;
+
+    if ( cur >= limit )
     {
-      cur++;
-      if ( cur >= limit )
-      {
-        FT_ERROR(( "t42_parse_encoding: out of bounds!\n" ));
-        parser->root.error = T42_Err_Invalid_File_Format;
-        return;
-      }
+      FT_ERROR(( "t42_parse_encoding: out of bounds!\n" ));
+      parser->root.error = T42_Err_Invalid_File_Format;
+      return;
     }
 
-    /* if we have a number, then the encoding is an array, */
-    /* and we must load it now                             */
-    if ( ft_isdigit( *cur ) )
+    /* if we have a number or `[', the encoding is an array, */
+    /* and we must load it now                               */
+    if ( ft_isdigit( *cur ) || *cur == '[' )
     {
-      T1_Encoding  encode     = &face->type1.encoding;
-      FT_Int       count, n;
-      PS_Table     char_table = &loader->encoding_table;
-      FT_Memory    memory     = parser->root.memory;
+      T1_Encoding  encode          = &face->type1.encoding;
+      FT_UInt      count, n;
+      PS_Table     char_table      = &loader->encoding_table;
+      FT_Memory    memory          = parser->root.memory;
       FT_Error     error;
+      FT_Bool      only_immediates = 0;
 
 
-      if ( encode->char_index )
-        /*  with synthetic fonts, it's possible we get here twice  */
-        return;
+      /* read the number of entries in the encoding; should be 256 */
+      if ( *cur == '[' )
+      {
+        count           = 256;
+        only_immediates = 1;
+        parser->root.cursor++;
+      }
+      else
+        count = (FT_UInt)T1_ToInt( parser );
 
-      /* read the number of entries in the encoding, should be 256 */
-      count = (FT_Int)T1_ToInt( parser );
-      if ( parser->root.error )
+      T1_Skip_Spaces( parser );
+      if ( parser->root.cursor >= limit )
         return;
 
       /* we use a T1_Table to store our charnames */
       loader->num_chars = encode->num_chars = count;
-      if ( FT_NEW_ARRAY( encode->char_index, count ) ||
-           FT_NEW_ARRAY( encode->char_name,  count ) ||
+      if ( FT_NEW_ARRAY( encode->char_index, count )     ||
+           FT_NEW_ARRAY( encode->char_name,  count )     ||
            FT_SET_ERROR( psaux->ps_table_funcs->init(
                            char_table, count, memory ) ) )
       {
@@ -356,86 +342,97 @@
         T1_Add_Table( char_table, n, notdef, 8 );
       }
 
-      /* Now, we will need to read a record of the form         */
-      /* ... charcode /charname ... for each entry in our table */
+      /* Now we need to read records of the form                */
+      /*                                                        */
+      /*   ... charcode /charname ...                           */
+      /*                                                        */
+      /* for each entry in our table.                           */
       /*                                                        */
       /* We simply look for a number followed by an immediate   */
       /* name.  Note that this ignores correctly the sequence   */
-      /* that is often seen in type1 fonts:                     */
+      /* that is often seen in type42 fonts:                    */
       /*                                                        */
       /*   0 1 255 { 1 index exch /.notdef put } for dup        */
       /*                                                        */
       /* used to clean the encoding array before anything else. */
       /*                                                        */
-      /* We stop when we encounter a `def'.                     */
+      /* Alternatively, if the array is directly given as       */
+      /*                                                        */
+      /*   /Encoding [ ... ]                                    */
+      /*                                                        */
+      /* we only read immediates.                               */
 
-      cur   = parser->root.cursor;
-      limit = parser->root.limit;
-      n     = 0;
+      n = 0;
 
-      for ( ; cur < limit; )
+      while ( parser->root.cursor < limit )
       {
-        FT_Byte  c;
-
-
-        c = *cur;
+        T1_Skip_Spaces( parser );
+        cur = parser->root.cursor;
 
-        /* we stop when we encounter a `def' */
-        if ( c == 'd' && cur + 3 < limit )
+        /* we stop when we encounter `def' or `]' */
+        if ( *cur == 'd' && cur + 3 < limit )
         {
-          if ( cur[1] == 'e'       &&
-               cur[2] == 'f'       &&
-               t42_is_space( cur[-1] ) &&
-               t42_is_space( cur[3] )  )
+          if ( cur[1] == 'e'          &&
+               cur[2] == 'f'          &&
+               t42_is_space( cur[3] ) )
           {
             FT_TRACE6(( "encoding end\n" ));
+            cur += 3;
             break;
           }
         }
+        if ( *cur == ']' )
+        {
+          FT_TRACE6(( "encoding end\n" ));
+          cur++;
+          break;
+        }
 
-        /* otherwise, we must find a number before anything else */
-        if ( ft_isdigit( c ) )
+        /* check whether we've found an entry */
+        if ( ft_isdigit( *cur ) || only_immediates )
         {
           FT_Int  charcode;
 
 
-          parser->root.cursor = cur;
-          charcode = (FT_Int)T1_ToInt( parser );
-          cur      = parser->root.cursor;
+          if ( only_immediates )
+            charcode = n;
+          else
+          {
+            charcode = (FT_Int)T1_ToInt( parser );
+            T1_Skip_Spaces( parser );
+          }
 
-          /* skip whitespace */
-          while ( cur < limit && t42_is_space( *cur ) )
-            cur++;
+          cur = parser->root.cursor;
 
-          if ( cur < limit && *cur == '/' )
+          if ( *cur == '/' && cur + 2 < limit && n < count )
           {
-            /* bingo, we have an immediate name -- it must be a */
-            /* character name                                   */
-            FT_Byte*    cur2 = cur + 1;
             FT_PtrDist  len;
 
 
-            while ( cur2 < limit && t42_is_alpha( *cur2 ) )
-              cur2++;
+            cur++;
+
+            parser->root.cursor = cur;
+            T1_Skip_PS_Token( parser );
 
-            len = cur2 - cur - 1;
+            len = parser->root.cursor - cur;
 
             parser->root.error = T1_Add_Table( char_table, charcode,
-                                               cur + 1, len + 1 );
+                                               cur, len + 1 );
             char_table->elements[charcode][len] = '\0';
             if ( parser->root.error )
               return;
 
-            cur = cur2;
+            n++;
           }
         }
         else
-          cur++;
+          T1_Skip_PS_Token( parser );
       }
 
       face->type1.encoding_type  = T1_ENCODING_TYPE_ARRAY;
       parser->root.cursor        = cur;
     }
+
     /* Otherwise, we should have either `StandardEncoding', */
     /* `ExpertEncoding', or `ISOLatin1Encoding'             */
     else
@@ -452,7 +449,8 @@
                 ft_strncmp( (const char*)cur, "ISOLatin1Encoding", 17 ) == 0 )
         face->type1.encoding_type = T1_ENCODING_TYPE_ISOLATIN1;
 
-      else {
+      else
+      {
         FT_ERROR(( "t42_parse_encoding: invalid token!\n" ));
         parser->root.error = T42_Err_Invalid_File_Format;
       }
@@ -460,34 +458,13 @@
   }
 
 
-  static FT_UInt
-  t42_hexval( FT_Byte  v )
+  typedef enum
   {
-    FT_UInt  d;
+    BEFORE_START,
+    BEFORE_TABLE_DIR,
+    OTHER_TABLES
 
-    d = (FT_UInt)( v - 'A' );
-    if ( d < 6 )
-    {
-      d += 10;
-      goto Exit;
-    }
-
-    d = (FT_UInt)( v - 'a' );
-    if ( d < 6 )
-    {
-      d += 10;
-      goto Exit;
-    }
-
-    d = (FT_UInt)( v - '0' );
-    if ( d < 10 )
-      goto Exit;
-
-    d = 0;
-
-  Exit:
-    return d;
-  }
+  } T42_Load_Status;
 
 
   static void
@@ -496,154 +473,176 @@
   {
     T42_Parser  parser = &loader->parser;
     FT_Memory   memory = parser->root.memory;
-    FT_Byte*    cur    = parser->root.cursor;
+    FT_Byte*    cur;
     FT_Byte*    limit  = parser->root.limit;
     FT_Error    error;
-    FT_Int      num_tables = 0, status;
-    FT_ULong    count, ttf_size = 0, string_size = 0;
-    FT_Bool     in_string  = 0;
-    FT_Byte     v = 0;
+    FT_Int      num_tables = 0;
+    FT_ULong    count, ttf_size = 0;
 
+    FT_Long     n, string_size, old_string_size, real_size;
+    FT_Byte*    string_buf = NULL;
+    FT_Bool     alloc      = 0;
 
-    /* The format is `/sfnts [ <...> <...> ... ] def' */
+    T42_Load_Status  status;
 
-    while ( t42_is_space( *cur ) )
-      cur++;
 
-    if (*cur++ == '[')
-    {
-      status = 0;
-      count = 0;
-    }
-    else
+    /* The format is                                */
+    /*                                              */
+    /*   /sfnts [ <hexstring> <hexstring> ... ] def */
+    /*                                              */
+    /* or                                           */
+    /*                                              */
+    /*   /sfnts [                                   */
+    /*      <num_bin_bytes> RD <binary data>        */
+    /*      <num_bin_bytes> RD <binary data>        */
+    /*      ...                                     */
+    /*   ] def                                      */
+    /*                                              */
+    /* with exactly one space after the `RD' token. */
+
+    T1_Skip_Spaces( parser );
+
+    if ( parser->root.cursor >= limit || *parser->root.cursor++ != '[' )
     {
       FT_ERROR(( "t42_parse_sfnts: can't find begin of sfnts vector!\n" ));
       error = T42_Err_Invalid_File_Format;
       goto Fail;
     }
 
-    while ( cur < limit - 2 )
+    T1_Skip_Spaces( parser );
+    status          = BEFORE_START;
+    old_string_size = 0;
+    count           = 0;
+
+    while ( parser->root.cursor < limit )
     {
-      while ( t42_is_space( *cur ) )
-        cur++;
+      cur = parser->root.cursor;
 
-      switch ( *cur )
+      if ( *cur == ']' )
       {
-      case ']':
-        parser->root.cursor = cur++;
-        return;
+        parser->root.cursor++;
+        goto Exit;
+      }
 
-      case '<':
-        in_string   = 1;
-        string_size = 0;
-        cur++;
-        continue;
+      else if ( *cur == '<' )
+      {
+        T1_Skip_PS_Token( parser );
+        if ( parser->root.error )
+          goto Exit;
 
-      case '>':
-        if ( !in_string )
-        {
-          FT_ERROR(( "t42_parse_sfnts: found unpaired `>'!\n" ));
-          error = T42_Err_Invalid_File_Format;
+        /* don't include delimiters */
+        string_size = (FT_Long)( ( parser->root.cursor - cur - 2 + 1 ) / 2 );
+        if ( FT_REALLOC( string_buf, old_string_size, string_size ) )
           goto Fail;
-        }
 
-        /* A string can have, as a last byte,         */
-        /* a zero byte for padding.  If so, ignore it */
-        if ( ( v == 0 ) && ( string_size % 2 == 1 ) )
-          count--;
-        in_string = 0;
-        cur++;
-        continue;
+        alloc = 1;
 
-      case '%':
-        if ( !in_string )
-        {
-          /* Comment found; skip till end of line */
-          while ( *cur != '\n' )
-            cur++;
-          continue;
-        }
-        else
-        {
-          FT_ERROR(( "t42_parse_sfnts: found `%' in string!\n" ));
-          error = T42_Err_Invalid_File_Format;
-          goto Fail;
-        }
+        parser->root.cursor = cur;
+        (void)T1_ToBytes( parser, string_buf, string_size, &real_size, 1 );
+        old_string_size = string_size;
+        string_size = real_size;
+      }
+
+      else if ( ft_isdigit( *cur ) )
+      {
+        string_size = T1_ToInt( parser );
+
+        T1_Skip_PS_Token( parser );             /* `RD' */
 
-      default:
-        if ( !ft_isxdigit( *cur ) || !ft_isxdigit( *(cur + 1) ) )
+        string_buf = parser->root.cursor + 1;   /* one space after `RD' */
+
+        parser->root.cursor += string_size + 1;
+        if ( parser->root.cursor >= limit )
         {
-          FT_ERROR(( "t42_parse_sfnts: found non-hex characters in string" ));
+          FT_ERROR(( "t42_parse_sfnts: too many binary data!\n" ));
           error = T42_Err_Invalid_File_Format;
           goto Fail;
         }
-
-        v = (FT_Byte)( 16 * t42_hexval( cur[0] ) + t42_hexval( cur[1] ) );
-        cur += 2;
-        string_size++;
       }
 
-      switch ( status )
+      /* A string can have a trailing zero byte for padding.  Ignore it. */
+      if ( string_buf[string_size - 1] == 0 && ( string_size % 2 == 1 ) )
+        string_size--;
+
+      for ( n = 0; n < string_size; n++ )
       {
-      case 0: /* The '[' was read, so load offset table, 12 bytes */
-        if ( count < 12 )
+        switch ( status )
         {
-          face->ttf_data[count++] = v;
-          continue;
-        }
-        else
-        {
-          num_tables = 16 * face->ttf_data[4] + face->ttf_data[5];
-          status     = 1;
-          ttf_size   = 12 + 16 * num_tables;
+        case BEFORE_START:
+          /* load offset table, 12 bytes */
+          if ( count < 12 )
+          {
+            face->ttf_data[count++] = string_buf[n];
+            continue;
+          }
+          else
+          {
+            num_tables = 16 * face->ttf_data[4] + face->ttf_data[5];
+            status     = BEFORE_TABLE_DIR;
+            ttf_size   = 12 + 16 * num_tables;
 
-          if ( FT_REALLOC( face->ttf_data, 12, ttf_size ) )
-            goto Fail;
-        }
-        /* No break, fall-through */
+            if ( FT_REALLOC( face->ttf_data, 12, ttf_size ) )
+              goto Fail;
+          }
+          /* fall through */
 
-      case 1: /* The offset table is read; read now the table directory */
-        if ( count < ttf_size )
-        {
-          face->ttf_data[count++] = v;
-          continue;
-        }
-        else
-        {
-          int      i;
-          FT_ULong len;
+        case BEFORE_TABLE_DIR:
+          /* the offset table is read; read the table directory */
+          if ( count < ttf_size )
+          {
+            face->ttf_data[count++] = string_buf[n];
+            continue;
+          }
+          else
+          {
+            int       i;
+            FT_ULong  len;
 
 
-          for ( i = 0; i < num_tables; i++ )
-          {
-            FT_Byte*  p = face->ttf_data + 12 + 16*i + 12;
+            for ( i = 0; i < num_tables; i++ )
+            {
+              FT_Byte*  p = face->ttf_data + 12 + 16 * i + 12;
 
-            len = FT_PEEK_ULONG( p );
 
-            /* Pad to a 4-byte boundary length */
-            ttf_size += ( len + 3 ) & ~3;
-          }
+              len = FT_PEEK_ULONG( p );
+
+              /* Pad to a 4-byte boundary length */
+              ttf_size += ( len + 3 ) & ~3;
+            }
+
+            status         = OTHER_TABLES;
+            face->ttf_size = ttf_size;
 
-          status         = 2;
-          face->ttf_size = ttf_size;
+            if ( FT_REALLOC( face->ttf_data, 12 + 16 * num_tables,
+                             ttf_size + 1 ) )
+              goto Fail;
+          }
+          /* fall through */
 
-          if ( FT_REALLOC( face->ttf_data, 12 + 16 * num_tables,
-                           ttf_size + 1 ) )
+        case OTHER_TABLES:
+          /* all other tables are just copied */
+          if ( count >= ttf_size )
+          {
+            FT_ERROR(( "t42_parse_sfnts: too many binary data!\n" ));
+            error = T42_Err_Invalid_File_Format;
             goto Fail;
+          }
+          face->ttf_data[count++] = string_buf[n];
         }
-        /* No break, fall-through */
-
-      case 2: /* We are reading normal tables; just swallow them */
-        face->ttf_data[count++] = v;
-
       }
+
+      T1_Skip_Spaces( parser );
     }
 
-    /* If control reaches this point, the format was not valid */
+    /* if control reaches this point, the format was not valid */
     error = T42_Err_Invalid_File_Format;
 
   Fail:
     parser->root.error = error;
+
+  Exit:
+    if ( alloc )
+      FT_FREE( string_buf );
   }
 
 
@@ -651,22 +650,75 @@
   t42_parse_charstrings( T42_Face    face,
                          T42_Loader  loader )
   {
-    T42_Parser     parser     = &loader->parser;
-    PS_Table       code_table = &loader->charstrings;
-    PS_Table       name_table = &loader->glyph_names;
-    FT_Memory      memory     = parser->root.memory;
+    T42_Parser     parser       = &loader->parser;
+    PS_Table       code_table   = &loader->charstrings;
+    PS_Table       name_table   = &loader->glyph_names;
+    PS_Table       swap_table   = &loader->swap_table;
+    FT_Memory      memory       = parser->root.memory;
     FT_Error       error;
 
-    PSAux_Service  psaux      = (PSAux_Service)face->psaux;
+    PSAux_Service  psaux        = (PSAux_Service)face->psaux;
 
     FT_Byte*       cur;
-    FT_Byte*       limit      = parser->root.limit;
-    FT_Int         n;
+    FT_Byte*       limit        = parser->root.limit;
+    FT_UInt        n;
+    FT_UInt        notdef_index = 0;
+    FT_Byte        notdef_found = 0;
 
 
-    loader->num_glyphs = (FT_Int)T1_ToInt( parser );
-    if ( parser->root.error )
-      return;
+    T1_Skip_Spaces( parser );
+
+    if ( parser->root.cursor >= limit )
+    {
+      FT_ERROR(( "t42_parse_charstrings: out of bounds!\n" ));
+      error = T42_Err_Invalid_File_Format;
+      goto Fail;
+    }
+
+    if ( ft_isdigit( *parser->root.cursor ) )
+    {
+      loader->num_glyphs = (FT_UInt)T1_ToInt( parser );
+      if ( parser->root.error )
+        return;
+    }
+    else if ( *parser->root.cursor == '<' )
+    {
+      /* We have `<< ... >>'.  Count the number of `/' in the dictionary */
+      /* to get its size.                                                */
+      FT_UInt  count = 0;
+
+
+      T1_Skip_PS_Token( parser );
+      T1_Skip_Spaces( parser );
+      cur = parser->root.cursor;
+
+      while ( parser->root.cursor < limit )
+      {
+        if ( *parser->root.cursor == '/' )
+          count++;
+        else if ( *parser->root.cursor == '>' )
+        {
+          loader->num_glyphs  = count;
+          parser->root.cursor = cur;        /* rewind */
+          break;
+        }
+        T1_Skip_PS_Token( parser );
+        T1_Skip_Spaces( parser );
+      }
+    }
+    else
+    {
+      FT_ERROR(( "t42_parse_charstrings: invalid token!\n" ));
+      error = T42_Err_Invalid_File_Format;
+      goto Fail;
+    }
+
+    if ( parser->root.cursor >= limit )
+    {
+      FT_ERROR(( "t42_parse_charstrings: out of bounds!\n" ));
+      error = T42_Err_Invalid_File_Format;
+      goto Fail;
+    }
 
     /* initialize tables */
 
@@ -682,57 +734,82 @@
     if ( error )
       goto Fail;
 
+    /* Initialize table for swapping index notdef_index and */
+    /* index 0 names and codes (if necessary).              */
+
+    error = psaux->ps_table_funcs->init( swap_table, 4, memory );
+    if ( error )
+      goto Fail;
+
     n = 0;
 
     for (;;)
     {
-      /* the format is simple:                    */
-      /*   `/glyphname' + index + def             */
-      /*                                          */
-      /* note that we stop when we find an `end'  */
-      /*                                          */
+      /* The format is simple:                   */
+      /*   `/glyphname' + index [+ def]          */
+
       T1_Skip_Spaces( parser );
 
       cur = parser->root.cursor;
       if ( cur >= limit )
         break;
 
-      /* we stop when we find an `end' keyword */
-      if ( *cur   == 'e'   &&
-           cur + 3 < limit &&
-           cur[1] == 'n'   &&
-           cur[2] == 'd'   )
+      /* We stop when we find an `end' keyword or '>' */
+      if ( *cur   == 'e'          &&
+           cur + 3 < limit        &&
+           cur[1] == 'n'          &&
+           cur[2] == 'd'          &&
+           t42_is_space( cur[3] ) )
+        break;
+      if ( *cur == '>' )
         break;
 
-      if ( *cur != '/' )
-        T1_Skip_PS_Token( parser );
-      else
+      T1_Skip_PS_Token( parser );
+
+      if ( *cur == '/' )
       {
-        FT_Byte*  cur2 = cur + 1;
-        FT_Int    len;
+        FT_PtrDist  len;
 
 
-        while ( cur2 < limit && t42_is_alpha( *cur2 ) )
-          cur2++;
-        len = (FT_Int)( cur2 - cur - 1 );
+        if ( cur + 1 >= limit )
+        {
+          FT_ERROR(( "t42_parse_charstrings: out of bounds!\n" ));
+          error = T42_Err_Invalid_File_Format;
+          goto Fail;
+        }
 
-        error = T1_Add_Table( name_table, n, cur + 1, len + 1 );
+        cur++;                              /* skip `/' */
+        len = parser->root.cursor - cur;
+
+        error = T1_Add_Table( name_table, n, cur, len + 1 );
         if ( error )
           goto Fail;
 
         /* add a trailing zero to the name table */
         name_table->elements[n][len] = '\0';
 
-        parser->root.cursor = cur2;
+        /* record index of /.notdef */
+        if ( *cur == '.'                                              &&
+             ft_strcmp( ".notdef",
+                        (const char*)(name_table->elements[n]) ) == 0 )
+        {
+          notdef_index = n;
+          notdef_found = 1;
+        }
+
         T1_Skip_Spaces( parser );
 
-        cur2 = cur = parser->root.cursor;
-        if ( cur >= limit )
-          break;
+        cur = parser->root.cursor;
 
-        while ( cur2 < limit && t42_is_alpha( *cur2 ) )
-          cur2++;
-        len = (FT_Int)( cur2 - cur );
+        (void)T1_ToInt( parser );
+        if ( parser->root.cursor >= limit )
+        {
+          FT_ERROR(( "t42_parse_charstrings: out of bounds!\n" ));
+          error = T42_Err_Invalid_File_Format;
+          goto Fail;
+        }
+
+        len = parser->root.cursor - cur;
 
         error = T1_Add_Table( code_table, n, cur, len + 1 );
         if ( error )
@@ -746,15 +823,79 @@
       }
     }
 
-    /* Index 0 must be a .notdef element */
-    if ( ft_strcmp( (char *)name_table->elements[0], ".notdef" ) )
+    loader->num_glyphs = n;
+
+    if ( !notdef_found )
     {
-      FT_ERROR(( "t42_parse_charstrings: Index 0 is not `.notdef'!\n" ));
+      FT_ERROR(( "t42_parse_charstrings: no /.notdef glyph!\n" ));
       error = T42_Err_Invalid_File_Format;
       goto Fail;
     }
 
-    loader->num_glyphs = n;
+    /* if /.notdef does not occupy index 0, do our magic. */
+    if ( ft_strcmp( (const char*)".notdef",
+                    (const char*)name_table->elements[0] ) )
+    {
+      /* Swap glyph in index 0 with /.notdef glyph.  First, add index 0  */
+      /* name and code entries to swap_table.  Then place notdef_index   */
+      /* name and code entries into swap_table.  Then swap name and code */
+      /* entries at indices notdef_index and 0 using values stored in    */
+      /* swap_table.                                                     */
+
+      /* Index 0 name */
+      error = T1_Add_Table( swap_table, 0,
+                            name_table->elements[0],
+                            name_table->lengths [0] );
+      if ( error )
+        goto Fail;
+
+      /* Index 0 code */
+      error = T1_Add_Table( swap_table, 1,
+                            code_table->elements[0],
+                            code_table->lengths [0] );
+      if ( error )
+        goto Fail;
+
+      /* Index notdef_index name */
+      error = T1_Add_Table( swap_table, 2,
+                            name_table->elements[notdef_index],
+                            name_table->lengths [notdef_index] );
+      if ( error )
+        goto Fail;
+
+      /* Index notdef_index code */
+      error = T1_Add_Table( swap_table, 3,
+                            code_table->elements[notdef_index],
+                            code_table->lengths [notdef_index] );
+      if ( error )
+        goto Fail;
+
+      error = T1_Add_Table( name_table, notdef_index,
+                            swap_table->elements[0],
+                            swap_table->lengths [0] );
+      if ( error )
+        goto Fail;
+
+      error = T1_Add_Table( code_table, notdef_index,
+                            swap_table->elements[1],
+                            swap_table->lengths [1] );
+      if ( error )
+        goto Fail;
+
+      error = T1_Add_Table( name_table, 0,
+                            swap_table->elements[2],
+                            swap_table->lengths [2] );
+      if ( error )
+        goto Fail;
+
+      error = T1_Add_Table( code_table, 0,
+                            swap_table->elements[3],
+                            swap_table->lengths [3] );
+      if ( error )
+        goto Fail;
+
+    }
+
     return;
 
   Fail:
@@ -774,14 +915,16 @@
 
 
     /* if the keyword has a dedicated callback, call it */
-    if ( field->type == T1_FIELD_TYPE_CALLBACK ) {
+    if ( field->type == T1_FIELD_TYPE_CALLBACK )
+    {
       field->reader( (FT_Face)face, loader );
       error = loader->parser.root.error;
       goto Exit;
     }
 
-    /* now, the keyword is either a simple field, or a table of fields; */
-    /* we are now going to take care of it                              */
+    /* now the keyword is either a simple field or a table of fields; */
+    /* we are now going to take care of it                            */
+
     switch ( field->location )
     {
     case T1_FIELD_LOCATION_FONT_INFO:
@@ -818,28 +961,27 @@
                   FT_Long     size )
   {
     T42_Parser  parser     = &loader->parser;
-    FT_Byte*    cur        = base;
-    FT_Byte*    limit      = cur + size;
-    FT_UInt     n_keywords = (FT_UInt)( sizeof ( t42_keywords ) /
-                                        sizeof ( t42_keywords[0] ) );
-
-    FT_Byte     keyword_flags[T42_KEYWORD_COUNT];
-
-    {
-      FT_UInt  n;
+    FT_Byte*    limit;
+    int         n_keywords = sizeof ( t42_keywords ) /
+                               sizeof ( t42_keywords[0] );
 
 
-      for ( n = 0; n < T42_KEYWORD_COUNT; n++ )
-        keyword_flags[n] = 0;
-    }
-
     parser->root.cursor = base;
     parser->root.limit  = base + size;
-    parser->root.error  = 0;
+    parser->root.error  = T42_Err_Ok;
+
+    limit = parser->root.limit;
+
+    T1_Skip_Spaces( parser );
 
-    for ( ; cur < limit; cur++ )
+    while ( parser->root.cursor < limit )
     {
-      /* look for `FontDirectory', which causes problems on some fonts */
+      FT_Byte*  cur;
+
+
+      cur = parser->root.cursor;
+
+      /* look for `FontDirectory' which causes problems for some fonts */
       if ( *cur == 'F' && cur + 25 < limit                    &&
            ft_strncmp( (char*)cur, "FontDirectory", 13 ) == 0 )
       {
@@ -847,13 +989,21 @@
 
 
         /* skip the `FontDirectory' keyword */
-        cur += 13;
-        cur2 = cur;
+        T1_Skip_PS_Token( parser );
+        T1_Skip_Spaces  ( parser );
+        cur = cur2 = parser->root.cursor;
 
-        /* lookup the `known' keyword */
-        while ( cur < limit && *cur != 'k'           &&
-                ft_strncmp( (char*)cur, "known", 5 ) )
-          cur++;
+        /* look up the `known' keyword */
+        while ( cur < limit )
+        {
+          if ( *cur == 'k' && cur + 5 < limit             &&
+                ft_strncmp( (char*)cur, "known", 5 ) == 0 )
+            break;
+
+          T1_Skip_PS_Token( parser );
+          T1_Skip_Spaces  ( parser );
+          cur = parser->root.cursor;
+        }
 
         if ( cur < limit )
         {
@@ -861,34 +1011,37 @@
 
 
           /* skip the `known' keyword and the token following it */
-          cur += 5;
-          loader->parser.root.cursor = cur;
-          T1_ToToken( &loader->parser, &token );
+          T1_Skip_PS_Token( parser );
+          T1_ToToken( parser, &token );
 
           /* if the last token was an array, skip it! */
           if ( token.type == T1_TOKEN_TYPE_ARRAY )
             cur2 = parser->root.cursor;
         }
-        cur = cur2;
+        parser->root.cursor = cur2;
       }
+
       /* look for immediates */
       else if ( *cur == '/' && cur + 2 < limit )
       {
-        FT_Byte*  cur2;
-        FT_UInt    i, len;
+        FT_PtrDist  len;
 
 
         cur++;
-        cur2 = cur;
-        while ( cur2 < limit && t42_is_alpha( *cur2 ) )
-          cur2++;
 
-        len  = (FT_UInt)( cur2 - cur );
-        if ( len > 0 && len < 22 ) /* XXX What shall it this 22? */
+        parser->root.cursor = cur;
+        T1_Skip_PS_Token( parser );
+
+        len = parser->root.cursor - cur;
+
+        if ( len > 0 && len < 22 && parser->root.cursor < limit )
         {
-          /* now, compare the immediate name to the keyword table */
+          int  i;
+
 
-          /* Loop through all known keywords */
+          /* now compare the immediate name to the keyword table */
+
+          /* loop through all known keywords */
           for ( i = 0; i < n_keywords; i++ )
           {
             T1_Field  keyword = (T1_Field)&t42_keywords[i];
@@ -898,32 +1051,27 @@
             if ( !name )
               continue;
 
-            if ( ( len == ft_strlen( (const char *)name ) ) &&
-                 ( ft_memcmp( cur, name, len ) == 0 )       )
+            if ( cur[0] == name[0]                      && 
+                 len == ft_strlen( (const char *)name ) &&
+                 ft_memcmp( cur, name, len ) == 0       )
             {
               /* we found it -- run the parsing callback! */
-              parser->root.cursor = cur2;
-              T1_Skip_Spaces( parser );
-
-              /* only record the first instance of each field/keyword */
-              /* to deal with synthetic fonts correctly               */
-              if ( keyword_flags[i] == 0 )
-              {
-                parser->root.error = t42_load_keyword(face,
-                                                      loader,
-                                                      keyword );
-                if ( parser->root.error )
-                  return parser->root.error;
-              }
-              keyword_flags[i] = 1;
-
-              cur = parser->root.cursor;
+              parser->root.error = t42_load_keyword( face,
+                                                     loader,
+                                                     keyword );
+              if ( parser->root.error )
+                return parser->root.error;
               break;
             }
           }
         }
       }
+      else
+        T1_Skip_PS_Token( parser );
+
+      T1_Skip_Spaces( parser );
     }
+
     return parser->root.error;
   }
 
@@ -955,6 +1103,7 @@
     T1_Release_Table( &loader->encoding_table );
     T1_Release_Table( &loader->charstrings );
     T1_Release_Table( &loader->glyph_names );
+    T1_Release_Table( &loader->swap_table );
 
     /* finalize parser */
     t42_parser_done( parser );
diff --git a/src/type42/t42parse.h b/src/type42/t42parse.h
index 7339aa6..f77ec4a 100644
--- a/src/type42/t42parse.h
+++ b/src/type42/t42parse.h
@@ -33,7 +33,7 @@ FT_BEGIN_HEADER
     FT_Byte*      base_dict;
     FT_Long       base_len;
 
-    FT_Byte       in_memory;
+    FT_Bool       in_memory;
 
   } T42_ParserRec, *T42_Parser;
 
@@ -42,13 +42,14 @@ FT_BEGIN_HEADER
   {
     T42_ParserRec  parser;          /* parser used to read the stream */
 
-    FT_Int         num_chars;       /* number of characters in encoding */
+    FT_UInt        num_chars;       /* number of characters in encoding */
     PS_TableRec    encoding_table;  /* PS_Table used to store the       */
                                     /* encoding character names         */
 
-    FT_Int         num_glyphs;
+    FT_UInt        num_glyphs;
     PS_TableRec    glyph_names;
     PS_TableRec    charstrings;
+    PS_TableRec    swap_table;      /* For moving .notdef glyph to index 0. */
 
   } T42_LoaderRec, *T42_Loader;