Commit b36d4a53e9c019b47f0655855fbe4084f01af425

Werner Lemberg 2003-12-12T15:38:39

* src/cff/cffdrivr.c (cff_get_glyph_name): Improve error message. (cff_get_name_index): Return if no PSNames service is available. (cff_ps_has_glyph_names): Handle CID-keyed fonts correctly. * src/cff/cfftypes.h (CFF_CharsetRec): New field `cids', used for CID-keyed fonts. This is the inverse mapping of `sids'. * src/cff/cffload.c (cff_charset_load): New argument `invert'. Initialize charset->cids if `invert' is set. (cff_font_load): In call to cff_charset_load, set `invert' to true for CID-keyed fonts. * src/cff/cffgload.c (cff_slot_load): Handle glyph index as CID and map it to the real glyph index. * docs/CHANGES: Updated.

diff --git a/ChangeLog b/ChangeLog
index 8133b07..7757f7a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,19 @@
+2003-12-12  Werner Lemberg  <wl@gnu.org>
+
+	* src/cff/cffdrivr.c (cff_get_glyph_name): Improve error message.
+	(cff_get_name_index): Return if no PSNames service is available.
+	(cff_ps_has_glyph_names): Handle CID-keyed fonts correctly.
+	* src/cff/cfftypes.h (CFF_CharsetRec): New field `cids', used for
+	CID-keyed fonts.  This is the inverse mapping of `sids'.
+	* src/cff/cffload.c (cff_charset_load): New argument `invert'.
+	Initialize charset->cids if `invert' is set.
+	(cff_font_load): In call to cff_charset_load, set `invert' to true
+	for CID-keyed fonts.
+	* src/cff/cffgload.c (cff_slot_load): Handle glyph index as CID
+	and map it to the real glyph index.
+
+	* docs/CHANGES: Updated.
+
 2003-12-11  Werner Lemberg  <wl@gnu.org>
 
 	* src/cff/cffobjs.c (cff_face_init): Don't set
diff --git a/docs/CHANGES b/docs/CHANGES
index a99b16c..c6a8499 100644
--- a/docs/CHANGES
+++ b/docs/CHANGES
@@ -10,6 +10,10 @@ LATEST CHANGES BETWEEN 2.1.8 and 2.1.7
       rounded,  making  them virtually  unusable  if  not loaded  with
       FT_LOAD_LINEAR_DESIGN.
 
+    - Indexing CID-keyed CFF fonts is now working:  The glyph index is
+      correctly  treated as a CID,  similar to  FreeType's CID  driver
+      module.  Note that CID CMaps support is still missing.
+
 
   II. IMPORTANT CHANGES
 
diff --git a/include/freetype/internal/ftgloadr.h b/include/freetype/internal/ftgloadr.h
index 284f4e3..abc56e2 100644
--- a/include/freetype/internal/ftgloadr.h
+++ b/include/freetype/internal/ftgloadr.h
@@ -110,15 +110,15 @@ FT_BEGIN_HEADER
   FT_BASE( void )
   FT_GlyphLoader_Rewind( FT_GlyphLoader  loader );
 
-  /* check that there is enough room to add 'n_points' and 'n_contours' */
-  /* to the glyph loader                                                */
+  /* check that there is enough space to add `n_points' and `n_contours' */
+  /* to the glyph loader                                                 */
   FT_BASE( FT_Error )
   FT_GlyphLoader_CheckPoints( FT_GlyphLoader  loader,
                               FT_UInt         n_points,
                               FT_UInt         n_contours );
 
-  /* check that there is enough room to add 'n_subs' sub-glyphs to */
-  /* a glyph loader                                                */
+  /* check that there is enough space to add `n_subs' sub-glyphs to */
+  /* a glyph loader                                                 */
   FT_BASE( FT_Error )
   FT_GlyphLoader_CheckSubGlyphs( FT_GlyphLoader  loader,
                                  FT_UInt         n_subs );
diff --git a/src/cff/cffdrivr.c b/src/cff/cffdrivr.c
index 9916485..d44f1a5 100644
--- a/src/cff/cffdrivr.c
+++ b/src/cff/cffdrivr.c
@@ -236,7 +236,7 @@
     if ( !psnames )
     {
       FT_ERROR(( "cff_get_glyph_name:" ));
-      FT_ERROR(( " cannot open CFF & CEF fonts\n" ));
+      FT_ERROR(( " cannot get glyph name from CFF & CEF fonts\n" ));
       FT_ERROR(( "                   " ));
       FT_ERROR(( " without the `PSNames' module\n" ));
       error = CFF_Err_Unknown_File_Format;
@@ -287,6 +287,9 @@
     charset = &cff->charset;
 
     FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
+    if ( !psnames )
+      return 0;
+
     for ( i = 0; i < cff->num_glyphs; i++ )
     {
       sid = charset->sids[i];
@@ -324,8 +327,7 @@
   static FT_Int
   cff_ps_has_glyph_names( FT_Face  face )
   {
-    FT_UNUSED( face );
-    return 1;
+    return face->face_flags & FT_FACE_FLAG_GLYPH_NAMES;
   }
 
 
diff --git a/src/cff/cffgload.c b/src/cff/cffgload.c
index 63db54a..0388b75 100644
--- a/src/cff/cffgload.c
+++ b/src/cff/cffgload.c
@@ -398,7 +398,7 @@
   }
 
 
-  /* check that there is enough room for `count' more points */
+  /* check that there is enough space for `count' more points */
   static FT_Error
   check_points( CFF_Builder*  builder,
                 FT_Int        count )
@@ -451,7 +451,7 @@
   }
 
 
-  /* check room for a new contour, then add it */
+  /* check space for a new contour, then add it */
   static FT_Error
   cff_builder_add_contour( CFF_Builder*  builder )
   {
@@ -2315,6 +2315,11 @@
       FT_ULong  charstring_len;
 
 
+      /* in a CID-keyed font, consider `glyph_index' as a CID and map */
+      /* it immediately to the real glyph_index                       */
+      if ( cff->top_font.font_dict.cid_registry != 0xFFFFU )
+        glyph_index = cff->charset.cids[glyph_index];
+
       cff_decoder_init( &decoder, face, size, glyph, hinting,
                         FT_LOAD_TARGET_MODE(load_flags) );
 
diff --git a/src/cff/cffload.c b/src/cff/cffload.c
index dcfe99a..68bac3e 100644
--- a/src/cff/cffload.c
+++ b/src/cff/cffload.c
@@ -1511,7 +1511,8 @@
                     FT_UInt      num_glyphs,
                     FT_Stream    stream,
                     FT_ULong     base_offset,
-                    FT_ULong     offset )
+                    FT_ULong     offset,
+                    FT_Bool      invert )
   {
     FT_Memory  memory = stream->memory;
     FT_Error   error  = 0;
@@ -1611,8 +1612,8 @@
       case 0:
         if ( num_glyphs > 229 )
         {
-          FT_ERROR(("cff_charset_load: implicit charset larger than\n"
-                    "predefined charset (Adobe ISO-Latin)!\n" ));
+          FT_ERROR(( "cff_charset_load: implicit charset larger than\n"
+                     "predefined charset (Adobe ISO-Latin)!\n" ));
           error = CFF_Err_Invalid_File_Format;
           goto Exit;
         }
@@ -1671,17 +1672,36 @@
       }
     }
 
-  Exit:
+    /* we have to invert the `sids' array for CID-keyed fonts */
+    if ( invert )
+    {
+      FT_UInt    i;
+      FT_UShort  max_cid = 0;
+
+
+      for ( i = 0; i < num_glyphs; i++ )
+        if ( charset->sids[i] > max_cid )
+          max_cid = charset->sids[i];
+      max_cid++;
+
+      if ( FT_NEW_ARRAY( charset->cids, max_cid ) )
+        goto Exit;
+      FT_MEM_ZERO( charset->cids, sizeof ( FT_UShort ) * max_cid );
 
+      for ( i = 0; i < num_glyphs; i++ )
+        charset->cids[charset->sids[i]] = i;
+    }
+
+  Exit:
     /* Clean up if there was an error. */
     if ( error )
-      if ( charset->sids )
-      {
-        FT_FREE( charset->sids );
-        charset->format = 0;
-        charset->offset = 0;
-        charset->sids   = 0;
-      }
+    {
+      FT_FREE( charset->sids );
+      FT_FREE( charset->cids );
+      charset->format = 0;
+      charset->offset = 0;
+      charset->sids   = 0;
+    }
 
     return error;
   }
@@ -2225,7 +2245,8 @@
     if ( font->num_glyphs > 0 )
     {
       error = cff_charset_load( &font->charset, font->num_glyphs, stream,
-                                base_offset, dict->charset_offset );
+                                base_offset, dict->charset_offset,
+                                dict->cid_registry != 0xFFFFU );
       if ( error )
         goto Exit;
 
diff --git a/src/cff/cffobjs.c b/src/cff/cffobjs.c
index 45b5830..15540b7 100644
--- a/src/cff/cffobjs.c
+++ b/src/cff/cffobjs.c
@@ -281,7 +281,7 @@
     if ( FT_STREAM_SEEK( 0 ) )
       goto Exit;
 
-    /* check that we have a valid OpenType file */
+    /* check whether we have a valid OpenType file */
     error = sfnt->init_face( stream, face, face_index, num_params, params );
     if ( !error )
     {
@@ -312,7 +312,7 @@
       }
       else
       {
-        /* load the `cmap' table by hand */
+        /* load the `cmap' table explicitly */
         error = sfnt->load_charmaps( face, stream );
         if ( error )
           goto Exit;
@@ -322,7 +322,7 @@
         /* FreeType 2                                                */
       }
 
-      /* now, load the CFF part of the file */
+      /* now load the CFF part of the file */
       error = face->goto_table( face, TTAG_CFF, stream, 0 );
       if ( error )
         goto Exit;
diff --git a/src/cff/cfftypes.h b/src/cff/cfftypes.h
index d7a7cb0..83b7d19 100644
--- a/src/cff/cfftypes.h
+++ b/src/cff/cfftypes.h
@@ -82,7 +82,8 @@ FT_BEGIN_HEADER
     FT_ULong    offset;
 
     FT_UShort*  sids;
-
+    FT_UShort*  cids;       /* the inverse mapping of `sids'; only needed */
+                            /* for CID-keyed fonts                        */
   } CFF_CharsetRec, *CFF_Charset;
 
 
diff --git a/src/pfr/pfrgload.c b/src/pfr/pfrgload.c
index cb5c60b..fdaf253 100644
--- a/src/pfr/pfrgload.c
+++ b/src/pfr/pfrgload.c
@@ -4,7 +4,7 @@
 /*                                                                         */
 /*    FreeType PFR glyph loader (body).                                    */
 /*                                                                         */
-/*  Copyright 2002 by                                                      */
+/*  Copyright 2002, 2003 by                                                */
 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
 /*                                                                         */
 /*  This file is part of the FreeType project, and may only be used,       */
@@ -198,7 +198,7 @@
     /* indicate that a new contour has started */
     glyph->path_begun = 1;
 
-    /* check that there is room for a new contour and a new point */
+    /* check that there is space for a new contour and a new point */
     error = FT_GlyphLoader_CheckPoints( loader, 1, 1 );
     if ( !error )
       /* add new start point */
diff --git a/src/psaux/psobjs.c b/src/psaux/psobjs.c
index 7f49530..b244675 100644
--- a/src/psaux/psobjs.c
+++ b/src/psaux/psobjs.c
@@ -1670,7 +1670,7 @@
   }
 
 
-  /* check room for a new contour, then add it */
+  /* check space for a new contour, then add it */
   FT_LOCAL_DEF( FT_Error )
   t1_builder_add_contour( T1_Builder  builder )
   {
diff --git a/src/type1/t1load.c b/src/type1/t1load.c
index 886e6cf..59cf2b9 100644
--- a/src/type1/t1load.c
+++ b/src/type1/t1load.c
@@ -1191,9 +1191,9 @@
     if ( parser->root.error )
       return;
 
-    /* initialize tables, leaving room for addition of .notdef, */
-    /* if necessary, and a few other glyphs to handle buggy     */
-    /* fonts which have more glyphs than specified.             */
+    /* initialize tables, leaving space for addition of .notdef, */
+    /* if necessary, and a few other glyphs to handle buggy      */
+    /* fonts which have more glyphs than specified.              */
 
     error = psaux->ps_table_funcs->init(
               code_table, loader->num_glyphs + 1 + TABLE_EXTEND, memory );