1 /****************************************************************************
2  *
3  * cffcmap.c
4  *
5  *   CFF character mapping table (cmap) support (body).
6  *
7  * Copyright (C) 2002-2020 by
8  * David Turner, Robert Wilhelm, and Werner Lemberg.
9  *
10  * This file is part of the FreeType project, and may only be used,
11  * modified, and distributed under the terms of the FreeType project
12  * license, LICENSE.TXT.  By continuing to use, modify, or distribute
13  * this file you indicate that you have read the license and
14  * understand and accept it fully.
15  *
16  */
17 
18 
19 #include <freetype/internal/ftdebug.h>
20 #include "cffcmap.h"
21 #include "cffload.h"
22 
23 #include "cfferrs.h"
24 
25 
26   /*************************************************************************/
27   /*************************************************************************/
28   /*****                                                               *****/
29   /*****           CFF STANDARD (AND EXPERT) ENCODING CMAPS            *****/
30   /*****                                                               *****/
31   /*************************************************************************/
32   /*************************************************************************/
33 
34   FT_CALLBACK_DEF( FT_Error )
cff_cmap_encoding_init(CFF_CMapStd cmap,FT_Pointer pointer)35   cff_cmap_encoding_init( CFF_CMapStd  cmap,
36                           FT_Pointer   pointer )
37   {
38     TT_Face       face     = (TT_Face)FT_CMAP_FACE( cmap );
39     CFF_Font      cff      = (CFF_Font)face->extra.data;
40     CFF_Encoding  encoding = &cff->encoding;
41 
42     FT_UNUSED( pointer );
43 
44 
45     cmap->gids  = encoding->codes;
46 
47     return 0;
48   }
49 
50 
51   FT_CALLBACK_DEF( void )
cff_cmap_encoding_done(CFF_CMapStd cmap)52   cff_cmap_encoding_done( CFF_CMapStd  cmap )
53   {
54     cmap->gids  = NULL;
55   }
56 
57 
58   FT_CALLBACK_DEF( FT_UInt )
cff_cmap_encoding_char_index(CFF_CMapStd cmap,FT_UInt32 char_code)59   cff_cmap_encoding_char_index( CFF_CMapStd  cmap,
60                                 FT_UInt32    char_code )
61   {
62     FT_UInt  result = 0;
63 
64 
65     if ( char_code < 256 )
66       result = cmap->gids[char_code];
67 
68     return result;
69   }
70 
71 
72   FT_CALLBACK_DEF( FT_UInt32 )
cff_cmap_encoding_char_next(CFF_CMapStd cmap,FT_UInt32 * pchar_code)73   cff_cmap_encoding_char_next( CFF_CMapStd   cmap,
74                                FT_UInt32    *pchar_code )
75   {
76     FT_UInt    result    = 0;
77     FT_UInt32  char_code = *pchar_code;
78 
79 
80     *pchar_code = 0;
81 
82     if ( char_code < 255 )
83     {
84       FT_UInt  code = (FT_UInt)(char_code + 1);
85 
86 
87       for (;;)
88       {
89         if ( code >= 256 )
90           break;
91 
92         result = cmap->gids[code];
93         if ( result != 0 )
94         {
95           *pchar_code = code;
96           break;
97         }
98 
99         code++;
100       }
101     }
102     return result;
103   }
104 
105 
106   FT_DEFINE_CMAP_CLASS(
107     cff_cmap_encoding_class_rec,
108 
109     sizeof ( CFF_CMapStdRec ),
110 
111     (FT_CMap_InitFunc)     cff_cmap_encoding_init,        /* init       */
112     (FT_CMap_DoneFunc)     cff_cmap_encoding_done,        /* done       */
113     (FT_CMap_CharIndexFunc)cff_cmap_encoding_char_index,  /* char_index */
114     (FT_CMap_CharNextFunc) cff_cmap_encoding_char_next,   /* char_next  */
115 
116     (FT_CMap_CharVarIndexFunc)    NULL,  /* char_var_index   */
117     (FT_CMap_CharVarIsDefaultFunc)NULL,  /* char_var_default */
118     (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
119     (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
120     (FT_CMap_VariantCharListFunc) NULL   /* variantchar_list */
121   )
122 
123 
124   /*************************************************************************/
125   /*************************************************************************/
126   /*****                                                               *****/
127   /*****              CFF SYNTHETIC UNICODE ENCODING CMAP              *****/
128   /*****                                                               *****/
129   /*************************************************************************/
130   /*************************************************************************/
131 
FT_CALLBACK_DEF(const char *)132   FT_CALLBACK_DEF( const char* )
133   cff_sid_to_glyph_name( TT_Face  face,
134                          FT_UInt  idx )
135   {
136     CFF_Font     cff     = (CFF_Font)face->extra.data;
137     CFF_Charset  charset = &cff->charset;
138     FT_UInt      sid     = charset->sids[idx];
139 
140 
141     return cff_index_get_sid_string( cff, sid );
142   }
143 
144 
145   FT_CALLBACK_DEF( FT_Error )
cff_cmap_unicode_init(PS_Unicodes unicodes,FT_Pointer pointer)146   cff_cmap_unicode_init( PS_Unicodes  unicodes,
147                          FT_Pointer   pointer )
148   {
149     TT_Face             face    = (TT_Face)FT_CMAP_FACE( unicodes );
150     FT_Memory           memory  = FT_FACE_MEMORY( face );
151     CFF_Font            cff     = (CFF_Font)face->extra.data;
152     CFF_Charset         charset = &cff->charset;
153     FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)cff->psnames;
154 
155     FT_UNUSED( pointer );
156 
157 
158     /* can't build Unicode map for CID-keyed font */
159     /* because we don't know glyph names.         */
160     if ( !charset->sids )
161       return FT_THROW( No_Unicode_Glyph_Name );
162 
163     if ( !psnames->unicodes_init )
164       return FT_THROW( Unimplemented_Feature );
165 
166     return psnames->unicodes_init( memory,
167                                    unicodes,
168                                    cff->num_glyphs,
169                                    (PS_GetGlyphNameFunc)&cff_sid_to_glyph_name,
170                                    (PS_FreeGlyphNameFunc)NULL,
171                                    (FT_Pointer)face );
172   }
173 
174 
175   FT_CALLBACK_DEF( void )
cff_cmap_unicode_done(PS_Unicodes unicodes)176   cff_cmap_unicode_done( PS_Unicodes  unicodes )
177   {
178     FT_Face    face   = FT_CMAP_FACE( unicodes );
179     FT_Memory  memory = FT_FACE_MEMORY( face );
180 
181 
182     FT_FREE( unicodes->maps );
183     unicodes->num_maps = 0;
184   }
185 
186 
187   FT_CALLBACK_DEF( FT_UInt )
cff_cmap_unicode_char_index(PS_Unicodes unicodes,FT_UInt32 char_code)188   cff_cmap_unicode_char_index( PS_Unicodes  unicodes,
189                                FT_UInt32    char_code )
190   {
191     TT_Face             face    = (TT_Face)FT_CMAP_FACE( unicodes );
192     CFF_Font            cff     = (CFF_Font)face->extra.data;
193     FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)cff->psnames;
194 
195 
196     return psnames->unicodes_char_index( unicodes, char_code );
197   }
198 
199 
200   FT_CALLBACK_DEF( FT_UInt32 )
cff_cmap_unicode_char_next(PS_Unicodes unicodes,FT_UInt32 * pchar_code)201   cff_cmap_unicode_char_next( PS_Unicodes  unicodes,
202                               FT_UInt32   *pchar_code )
203   {
204     TT_Face             face    = (TT_Face)FT_CMAP_FACE( unicodes );
205     CFF_Font            cff     = (CFF_Font)face->extra.data;
206     FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)cff->psnames;
207 
208 
209     return psnames->unicodes_char_next( unicodes, pchar_code );
210   }
211 
212 
213   FT_DEFINE_CMAP_CLASS(
214     cff_cmap_unicode_class_rec,
215 
216     sizeof ( PS_UnicodesRec ),
217 
218     (FT_CMap_InitFunc)     cff_cmap_unicode_init,        /* init       */
219     (FT_CMap_DoneFunc)     cff_cmap_unicode_done,        /* done       */
220     (FT_CMap_CharIndexFunc)cff_cmap_unicode_char_index,  /* char_index */
221     (FT_CMap_CharNextFunc) cff_cmap_unicode_char_next,   /* char_next  */
222 
223     (FT_CMap_CharVarIndexFunc)    NULL,  /* char_var_index   */
224     (FT_CMap_CharVarIsDefaultFunc)NULL,  /* char_var_default */
225     (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
226     (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
227     (FT_CMap_VariantCharListFunc) NULL   /* variantchar_list */
228   )
229 
230 
231 /* END */
232