Commit adf828ff953b0db3492ddf7678fdcefe34a5058b

Werner Lemberg 2006-01-11T10:08:49

Fix Savannah bug #15056 and use pscmap service in psaux module. * include/freetype/internal/services/svpscmap.h (PS_UniMap): Use FT_UInt32 for `glyph_index'. (PS_Unicodes_InitFunc): Use FT_String for `glyph_names'. (PS_Unicodes_CharIndexFunc): Use FT_UInt32 for `unicode'. (PS_Unicodes_CharNextFunc): Make second argument a pointer to FT_UInt32. * src/psnames/psmodule.c (VARIANT_BIT, BASE_GLYPH): New macros. (ps_unicode_value): Set VARIANT_BIT in return value if glyph is a variant glyph (this is, it has non-leading `.' in its name). (compare_uni_maps): Sort base glyphs before variant glyphs. (ps_unicodes_init): Use FT_String for `glyph_names' argument. Reallocate only if number of used entries is much smaller. Updated to handle variant glyphs. (ps_unicodes_char_index, ps_unicodes_char_next): Prefer base glyphs over variant glyphs. Simplify code. * src/psaux/t1cmap.c (t1_cmap_uni_pair_compare): Removed. (t1_cmap_unicode_init, t1_cmap_unicode_char_index, t1_cmap_unicode_char_next): Use pscmap service. (t1_cmap_unicode_done): Updated. * src/psaux/t1cmap.h (T1_CMapUniPair): Removed. (T1_CMapUnicode): Use PS_Unicodes structure.

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
diff --git a/ChangeLog b/ChangeLog
index ba03a7d..f720b4e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,22 +1,53 @@
-2006-01-11  suzuki toshiya <mpsuzuki@hiroshima-u.ac.jp>
+2006-01-10  Werner Lemberg  <wl@gnu.org>
 
-	Jumbo patch to fix "deprecated" warning to cross-build for
-	Tiger on Intel, the issue is reported by Sean McBride
-	<sean@rogue-research.com> on 2005-08-24.
+	Fix Savannah bug #15056 and use pscmap service in psaux module.
 
-	* src/base/ftmac.c: Heavy change to build without deprecated
-	Carbon functions on Tiger.
+	* include/freetype/internal/services/svpscmap.h (PS_UniMap): Use
+	FT_UInt32 for `glyph_index'.
+	(PS_Unicodes_InitFunc): Use FT_String for `glyph_names'.
+	(PS_Unicodes_CharIndexFunc): Use FT_UInt32 for `unicode'.
+	(PS_Unicodes_CharNextFunc): Make second argument a pointer to
+	FT_UInt32.
 
-	* builds/unix/configure.ac: Add options and autochecks for
-	Carbon functions availabilities, for MacOS X.
+	* src/psnames/psmodule.c (VARIANT_BIT, BASE_GLYPH): New macros.
+	(ps_unicode_value): Set VARIANT_BIT in return value if glyph is a
+	variant glyph (this is, it has non-leading `.' in its name).
+	(compare_uni_maps): Sort base glyphs before variant glyphs.
+	(ps_unicodes_init): Use FT_String for `glyph_names' argument.
+	Reallocate only if number of used entries is much smaller.
+	Updated to handle variant glyphs.
+	(ps_unicodes_char_index, ps_unicodes_char_next): Prefer base glyphs
+	over variant glyphs.
+	Simplify code.
 
-	* builds/mac/ascii2mpw.py: Add convertor for character "\305".
+	* src/psaux/t1cmap.c (t1_cmap_uni_pair_compare): Removed.
+	(t1_cmap_unicode_init, t1_cmap_unicode_char_index,
+	t1_cmap_unicode_char_next): Use pscmap service.
+	(t1_cmap_unicode_done): Updated.
+
+	* src/psaux/t1cmap.h (T1_CMapUniPair): Removed.
+	(T1_CMapUnicode): Use PS_Unicodes structure.
+
+2006-01-11  suzuki toshiya  <mpsuzuki@hiroshima-u.ac.jp>
+
+	Jumbo patch to fix `deprecated' warning of cross-build for Tiger on
+	Intel, as reported by Sean McBride <sean@rogue-research.com> on
+	2005-08-24.
+
+	* src/base/ftmac.c: Heavy change to build without deprecated Carbon
+	functions on Tiger.
+
+	* builds/unix/configure.ac: Add options and autochecks for Carbon
+	functions availabilities, for MacOS X.
+
+	* builds/mac/ascii2mpw.py: Add converter for character `\305'.
 	* builds/mac/FreeType.m68k_{far|cfm}.make.txt: Add conditional
-	macros to avoid unavailable functions. And ftmac.c must be
-	compiled without "-strict ansi", because it disables cpp macro
-	to use ToolBox system call.
-	* builds/mac/FreeType.ppc_{classic|carbon}.make.txt: Add
-	conditional macros to avoid unavailable functions.
+	macros to avoid unavailable functions.
+	ftmac.c must be compiled without `-strict ansi', because it disables
+	cpp macro to use ToolBox system call.
+
+	* builds/mac/FreeType.ppc_{classic|carbon}.make.txt: Add conditional
+	macros to avoid unavailable functions.
 
 	* builds/mac/README: Detailed notes on function availabilities.
 
diff --git a/include/freetype/internal/services/svpscmap.h b/include/freetype/internal/services/svpscmap.h
index ade96ef..74d57f9 100644
--- a/include/freetype/internal/services/svpscmap.h
+++ b/include/freetype/internal/services/svpscmap.h
@@ -4,7 +4,7 @@
 /*                                                                         */
 /*    The FreeType PostScript charmap service (specification).             */
 /*                                                                         */
-/*  Copyright 2003 by                                                      */
+/*  Copyright 2003, 2006 by                                                */
 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
@@ -27,7 +27,7 @@ FT_BEGIN_HEADER
 
 
   /*
-   *  Adobe glyph name to unicode value
+   *  Adobe glyph name to unicode value.
    */
   typedef FT_UInt32
   (*PS_Unicode_ValueFunc)( const char*  glyph_name );
@@ -58,8 +58,8 @@ FT_BEGIN_HEADER
    */
   typedef struct  PS_UniMap_
   {
-    FT_UInt  unicode;
-    FT_UInt  glyph_index;
+    FT_UInt32  unicode;      /* bit 31 set: is glyph variant */
+    FT_UInt    glyph_index;
 
   } PS_UniMap;
 
@@ -75,16 +75,16 @@ FT_BEGIN_HEADER
   typedef FT_Error
   (*PS_Unicodes_InitFunc)( FT_Memory     memory,
                            FT_UInt       num_glyphs,
-                           const char**  glyph_names,
+                           FT_String**   glyph_names,
                            PS_Unicodes*  unicodes );
 
   typedef FT_UInt
   (*PS_Unicodes_CharIndexFunc)( PS_Unicodes*  unicodes,
-                                FT_UInt       unicode );
+                                FT_UInt32     unicode );
 
   typedef FT_ULong
   (*PS_Unicodes_CharNextFunc)( PS_Unicodes*  unicodes,
-                               FT_ULong      unicode );
+                               FT_UInt32    *unicode );
 
 
   FT_DEFINE_SERVICE( PsCMaps )
diff --git a/src/psaux/t1cmap.c b/src/psaux/t1cmap.c
index dcd99be..a0e25e2 100644
--- a/src/psaux/t1cmap.c
+++ b/src/psaux/t1cmap.c
@@ -4,7 +4,7 @@
 /*                                                                         */
 /*    Type 1 character map support (body).                                 */
 /*                                                                         */
-/*  Copyright 2002, 2003 by                                                */
+/*  Copyright 2002, 2003, 2006 by                                          */
 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
@@ -257,105 +257,30 @@
   /*************************************************************************/
   /*************************************************************************/
 
-  FT_CALLBACK_DEF( FT_Int )
-  t1_cmap_uni_pair_compare( const void*  pair1,
-                            const void*  pair2 )
-  {
-    FT_UInt32  u1 = ((T1_CMapUniPair)pair1)->unicode;
-    FT_UInt32  u2 = ((T1_CMapUniPair)pair2)->unicode;
-
-
-    if ( u1 < u2 )
-      return -1;
-
-    if ( u1 > u2 )
-      return +1;
-
-    return 0;
-  }
-
-
   FT_CALLBACK_DEF( FT_Error )
   t1_cmap_unicode_init( T1_CMapUnicode  cmap )
   {
-    FT_Error            error;
-    FT_UInt             count;
     T1_Face             face    = (T1_Face)FT_CMAP_FACE( cmap );
     FT_Memory           memory  = FT_FACE_MEMORY( face );
     FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)face->psnames;
 
 
-    cmap->num_pairs = 0;
-    cmap->pairs     = NULL;
-
-    count = face->type1.num_glyphs;
-
-    if ( !FT_NEW_ARRAY( cmap->pairs, count ) )
-    {
-      FT_UInt         n, new_count;
-      T1_CMapUniPair  pair;
-      FT_UInt32       uni_code;
-
-
-      pair = cmap->pairs;
-      for ( n = 0; n < count; n++ )
-      {
-        const char*  gname = face->type1.glyph_names[n];
-
-
-        /* build unsorted pair table by matching glyph names */
-        if ( gname )
-        {
-          uni_code = psnames->unicode_value( gname );
-
-          if ( uni_code != 0 )
-          {
-            pair->unicode = uni_code;
-            pair->gindex  = n;
-            pair++;
-          }
-        }
-      }
-
-      new_count = (FT_UInt)( pair - cmap->pairs );
-      if ( new_count == 0 )
-      {
-        /* there are no unicode characters in here! */
-        FT_FREE( cmap->pairs );
-        error = PSaux_Err_Invalid_Argument;
-      }
-      else
-      {
-        /* re-allocate if the new array is much smaller than the original */
-        /* one                                                            */
-        if ( new_count != count && new_count < count / 2 )
-        {
-          (void)FT_RENEW_ARRAY( cmap->pairs, count, new_count );
-          error = 0;
-        }
-
-        /* sort the pairs table to allow efficient binary searches */
-        ft_qsort( cmap->pairs,
-                  new_count,
-                  sizeof ( T1_CMapUniPairRec ),
-                  t1_cmap_uni_pair_compare );
-
-        cmap->num_pairs = new_count;
-      }
-    }
-
-    return error;
+    return psnames->unicodes_init( memory,
+                                   face->type1.num_glyphs,
+                                   face->type1.glyph_names,
+                                   &cmap->unicodes );
   }
 
 
   FT_CALLBACK_DEF( void )
   t1_cmap_unicode_done( T1_CMapUnicode  cmap )
   {
-    FT_Face    face   = FT_CMAP_FACE(cmap);
-    FT_Memory  memory = FT_FACE_MEMORY(face);
+    FT_Face    face   = FT_CMAP_FACE( cmap );
+    FT_Memory  memory = FT_FACE_MEMORY( face );
 
-    FT_FREE( cmap->pairs );
-    cmap->num_pairs = 0;
+
+    FT_FREE( cmap->unicodes.maps );
+    cmap->unicodes.num_maps = 0;
   }
 
 
@@ -363,26 +288,11 @@
   t1_cmap_unicode_char_index( T1_CMapUnicode  cmap,
                               FT_UInt32       char_code )
   {
-    FT_UInt         min = 0;
-    FT_UInt         max = cmap->num_pairs;
-    FT_UInt         mid;
-    T1_CMapUniPair  pair;
-
-
-    while ( min < max )
-    {
-      mid  = min + ( max - min ) / 2;
-      pair = cmap->pairs + mid;
+    T1_Face             face    = (T1_Face)FT_CMAP_FACE( cmap );
+    FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)face->psnames;
 
-      if ( pair->unicode == char_code )
-        return pair->gindex;
 
-      if ( pair->unicode < char_code )
-        min = mid + 1;
-      else
-        max = mid;
-    }
-    return 0;
+    return psnames->unicodes_char_index( &cmap->unicodes, char_code );
   }
 
 
@@ -390,54 +300,11 @@
   t1_cmap_unicode_char_next( T1_CMapUnicode  cmap,
                              FT_UInt32      *pchar_code )
   {
-    FT_UInt    result    = 0;
-    FT_UInt32  char_code = *pchar_code + 1;
-
-
-  Restart:
-    {
-      FT_UInt         min = 0;
-      FT_UInt         max = cmap->num_pairs;
-      FT_UInt         mid;
-      T1_CMapUniPair  pair;
-
-
-      while ( min < max )
-      {
-        mid  = min + ( ( max - min ) >> 1 );
-        pair = cmap->pairs + mid;
-
-        if ( pair->unicode == char_code )
-        {
-          result = pair->gindex;
-          if ( result != 0 )
-            goto Exit;
-
-          char_code++;
-          goto Restart;
-        }
-
-        if ( pair->unicode < char_code )
-          min = mid+1;
-        else
-          max = mid;
-      }
-
-      /* we didn't find it, but we have a pair just above it */
-      char_code = 0;
+    T1_Face             face    = (T1_Face)FT_CMAP_FACE( cmap );
+    FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)face->psnames;
 
-      if ( min < cmap->num_pairs )
-      {
-        pair   = cmap->pairs + min;
-        result = pair->gindex;
-        if ( result != 0 )
-          char_code = pair->unicode;
-      }
-    }
 
-  Exit:
-    *pchar_code = char_code;
-    return result;
+    return psnames->unicodes_char_next( &cmap->unicodes, pchar_code );
   }
 
 
diff --git a/src/psaux/t1cmap.h b/src/psaux/t1cmap.h
index aa93d74..5382efc 100644
--- a/src/psaux/t1cmap.h
+++ b/src/psaux/t1cmap.h
@@ -4,7 +4,7 @@
 /*                                                                         */
 /*    Type 1 character map support (specification).                        */
 /*                                                                         */
-/*  Copyright 2002, 2003 by                                                */
+/*  Copyright 2002, 2003, 2006 by                                          */
 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
@@ -89,22 +89,13 @@ FT_BEGIN_HEADER
   /*************************************************************************/
   /*************************************************************************/
 
-  /* unicode (syntehtic) cmaps */
+  /* unicode (synthetic) cmaps */
   typedef struct T1_CMapUnicodeRec_*  T1_CMapUnicode;
 
-  typedef struct  T1_CMapUniPairRec_
-  {
-    FT_UInt32  unicode;
-    FT_UInt    gindex;
-
-  } T1_CMapUniPairRec, *T1_CMapUniPair;
-
-
   typedef struct  T1_CMapUnicodeRec_
   {
-    FT_CMapRec      cmap;
-    FT_UInt         num_pairs;
-    T1_CMapUniPair  pairs;
+    FT_CMapRec   cmap;
+    PS_Unicodes  unicodes;
 
   } T1_CMapUnicodeRec;
 
diff --git a/src/psnames/psmodule.c b/src/psnames/psmodule.c
index ba9deae..db8b25f 100644
--- a/src/psnames/psmodule.c
+++ b/src/psnames/psmodule.c
@@ -4,7 +4,7 @@
 /*                                                                         */
 /*    PSNames module implementation (body).                                */
 /*                                                                         */
-/*  Copyright 1996-2001, 2002, 2003, 2005 by                               */
+/*  Copyright 1996-2001, 2002, 2003, 2005, 2006 by                         */
 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
@@ -32,9 +32,14 @@
 #ifdef FT_CONFIG_OPTION_ADOBE_GLYPH_LIST
 
 
+#define VARIANT_BIT         ( 1L << 31 )
+#define BASE_GLYPH( code )  ( (code) & ~VARIANT_BIT )
+
+
   /* Return the Unicode value corresponding to a given glyph.  Note that */
   /* we do deal with glyph variants by detecting a non-initial dot in    */
-  /* the name, as in `A.swash' or `e.final'.                             */
+  /* the name, as in `A.swash' or `e.final'; in this case, the           */
+  /* VARIANT_BIT is set in the return value.                             */
   /*                                                                     */
   static FT_UInt32
   ps_unicode_value( const char*  glyph_name )
@@ -72,7 +77,9 @@
             d += 10;
         }
 
-        /* exit if a non-uppercase hexadecimal character was found */
+        /* Exit if a non-uppercase hexadecimal character was found   */
+        /* -- this also catches character codes below `0' since such */
+        /* negative numbers cast to `unsigned int' are far too big.  */
         if ( d >= 16 )
           break;
 
@@ -80,8 +87,13 @@
       }
 
       /* there must be exactly four hex digits */
-      if ( ( *p == '\0' || *p == '.' ) && count == 0 )
-        return value;
+      if ( count == 0 )
+      {
+        if ( *p == '\0' )
+          return value;
+        if ( *p == '.' )
+          return value ^ VARIANT_BIT;
+      }
     }
 
     /* If the name begins with `u', followed by four to six uppercase */
@@ -115,12 +127,17 @@
         value = ( value << 4 ) + d;
       }
 
-      if ( ( *p == '\0' || *p == '.' ) && count <= 2 )
-        return value;
+      if ( count <= 2 )
+      {
+        if ( *p == '\0' )
+          return value;
+        if ( *p == '.' )
+          return value ^ VARIANT_BIT;
+      }
     }
 
-    /* look for a non-initial dot in the glyph name in order to */
-    /* sort-out variants like `A.swash', `e.final', etc.        */
+    /* Look for a non-initial dot in the glyph name in order to */
+    /* find variants like `A.swash', `e.final', etc.            */
     {
       const char*  p   = glyph_name;
       const char*  dot = NULL;
@@ -128,15 +145,18 @@
 
       for ( ; *p; p++ )
       {
-        if ( *p == '.' && p > glyph_name && !dot )
+        if ( *p == '.' && p > glyph_name )
+        {
           dot = p;
+          break;
+        }
       }
 
+      /* now look up the glyph in the Adobe Glyph List */
       if ( !dot )
-        dot = p;
-
-      /* now, look up the glyph in the Adobe Glyph List */
-      return ft_get_adobe_glyph_index( glyph_name, dot );
+        return ft_get_adobe_glyph_index( glyph_name, p );
+      else
+        return ft_get_adobe_glyph_index( glyph_name, dot ) ^ VARIANT_BIT;
     }
   }
 
@@ -148,17 +168,23 @@
   {
     PS_UniMap*  map1 = (PS_UniMap*)a;
     PS_UniMap*  map2 = (PS_UniMap*)b;
+    FT_UInt32   unicode1 = BASE_GLYPH( map1->unicode );
+    FT_UInt32   unicode2 = BASE_GLYPH( map2->unicode );
 
 
-    return ( map1->unicode - map2->unicode );
+    /* sort base glyphs before glyph variants */
+    if ( unicode1 == unicode2 )
+      return map1->unicode - map2->unicode;
+    else
+      return unicode1 - unicode2;
   }
 
 
-  /* Builds a table that maps Unicode values to glyph indices */
+  /* Build a table that maps Unicode values to glyph indices. */
   static FT_Error
   ps_unicodes_init( FT_Memory     memory,
                     FT_UInt       num_glyphs,
-                    const char**  glyph_names,
+                    FT_String**   glyph_names,
                     PS_Unicodes*  table )
   {
     FT_Error  error;
@@ -187,32 +213,37 @@
         {
           uni_char = ps_unicode_value( gname );
 
-          if ( uni_char != 0 && uni_char != 0xFFFFL )
+          if ( BASE_GLYPH( uni_char ) != 0 )
           {
-            map->unicode     = (FT_UInt)uni_char;
+            map->unicode     = uni_char;
             map->glyph_index = n;
             map++;
           }
         }
       }
 
-      /* now, compress the table a bit */
+      /* now compress the table a bit */
       count = (FT_UInt)( map - table->maps );
 
-      if ( count > 0 && FT_REALLOC( table->maps,
-                                    num_glyphs * sizeof ( PS_UniMap ),
-                                    count * sizeof ( PS_UniMap ) ) )
-        count = 0;
-
       if ( count == 0 )
       {
         FT_FREE( table->maps );
         if ( !error )
-          error = PSnames_Err_Invalid_Argument;  /* no unicode chars here! */
+          error = PSnames_Err_Invalid_Argument;  /* No unicode chars here! */
+      }
+      else {
+        /* Reallocate if the number of used entries is much smaller. */
+        if ( count < num_glyphs / 2 )
+        {
+          (void)FT_RENEW_ARRAY( table->maps, num_glyphs, count );
+          error = PSnames_Err_Ok;
+        }
+
+        /* Sort the table in increasing order of unicode values, */
+        /* taking care of glyph variants.                        */
+        ft_qsort( table->maps, count, sizeof ( PS_UniMap ),
+                  compare_uni_maps );
       }
-      else
-        /* sort the table in increasing order of unicode values */
-        ft_qsort( table->maps, count, sizeof ( PS_UniMap ), compare_uni_maps );
 
       table->num_maps = count;
     }
@@ -223,74 +254,105 @@
 
   static FT_UInt
   ps_unicodes_char_index( PS_Unicodes*  table,
-                          FT_ULong      unicode )
+                          FT_UInt32     unicode )
   {
-    PS_UniMap  *min, *max, *mid;
+    PS_UniMap  *min, *max, *mid, *result = NULL;
 
 
-    /* perform a binary search on the table */
+    /* Perform a binary search on the table. */
 
     min = table->maps;
     max = min + table->num_maps - 1;
 
     while ( min <= max )
     {
-      mid = min + ( max - min ) / 2;
+      FT_UInt32  base_glyph;
+
+
+      mid = min + ( ( max - min ) >> 1 );
+
       if ( mid->unicode == unicode )
-        return mid->glyph_index;
+      {
+        result = mid;
+        break;
+      }
+
+      base_glyph = BASE_GLYPH( mid->unicode );
+
+      if ( base_glyph == unicode )
+        result = mid; /* remember match but continue search for base glyph */
 
       if ( min == max )
         break;
 
-      if ( mid->unicode < unicode )
+      if ( base_glyph < unicode )
         min = mid + 1;
       else
         max = mid - 1;
     }
 
-    return 0xFFFFU;
+    if ( result )
+      return result->glyph_index;
+    else
+      return 0;
   }
 
 
   static FT_ULong
   ps_unicodes_char_next( PS_Unicodes*  table,
-                         FT_ULong      unicode )
+                         FT_UInt32    *unicode )
   {
-    PS_UniMap  *min, *max, *mid;
+    FT_UInt    result    = 0;
+    FT_UInt32  char_code = *unicode + 1;
 
 
-    unicode++;
-    /* perform a binary search on the table */
+    {
+      FT_UInt     min = 0;
+      FT_UInt     max = table->num_maps;
+      FT_UInt     mid;
+      PS_UniMap*  map;
+      FT_UInt32   base_glyph;
 
-    min = table->maps;
-    max = min + table->num_maps - 1;
 
-    while ( min <= max )
-    {
-      mid = min + ( max - min ) / 2;
-      if ( mid->unicode == unicode )
-        return unicode;
+      while ( min < max )
+      {
+        mid = min + ( ( max - min ) >> 1 );
+        map = table->maps + mid;
 
-      if ( min == max )
-        break;
+        if ( map->unicode == char_code )
+        {
+          result = map->glyph_index;
+          goto Exit;
+        }
 
-      if ( mid->unicode < unicode )
-        min = mid + 1;
-      else
-        max = mid - 1;
-    }
+        base_glyph = BASE_GLYPH( map->unicode );
 
-    if ( max < table->maps )
-      max = table->maps;
+        if ( base_glyph == char_code )
+          result = map->glyph_index;
 
-    while ( max < table->maps + table->num_maps )
-    {
-      if ( unicode < max->unicode )
-        return max->unicode;
-      max++;
+        if ( base_glyph < char_code )
+          min = mid + 1;
+        else
+          max = mid;
+      }
+
+      if ( result )
+        goto Exit;               /* we have a variant glyph */
+
+      /* we didn't find it; check whether we have a map just above it */
+      char_code = 0;
+
+      if ( min < table->num_maps )
+      {
+        map    = table->maps + min;
+        result = map->glyph_index;
+        char_code = map->unicode;
+      }
     }
 
-    return 0;
+  Exit:
+    *unicode = char_code;
+    return result;
   }