1/***************************************************************************/
2/*                                                                         */
3/*  cffcmap.c                                                              */
4/*                                                                         */
5/*    CFF character mapping table (cmap) support (body).                   */
6/*                                                                         */
7/*  Copyright 2002-2015 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 <ft2build.h>
20#include FT_INTERNAL_DEBUG_H
21#include "cffcmap.h"
22#include "cffload.h"
23
24#include "cfferrs.h"
25
26
27  /*************************************************************************/
28  /*************************************************************************/
29  /*****                                                               *****/
30  /*****           CFF STANDARD (AND EXPERT) ENCODING CMAPS            *****/
31  /*****                                                               *****/
32  /*************************************************************************/
33  /*************************************************************************/
34
35  FT_CALLBACK_DEF( FT_Error )
36  cff_cmap_encoding_init( CFF_CMapStd  cmap,
37                          FT_Pointer   pointer )
38  {
39    TT_Face       face     = (TT_Face)FT_CMAP_FACE( cmap );
40    CFF_Font      cff      = (CFF_Font)face->extra.data;
41    CFF_Encoding  encoding = &cff->encoding;
42
43    FT_UNUSED( pointer );
44
45
46    cmap->gids  = encoding->codes;
47
48    return 0;
49  }
50
51
52  FT_CALLBACK_DEF( void )
53  cff_cmap_encoding_done( CFF_CMapStd  cmap )
54  {
55    cmap->gids  = NULL;
56  }
57
58
59  FT_CALLBACK_DEF( FT_UInt )
60  cff_cmap_encoding_char_index( CFF_CMapStd  cmap,
61                                FT_UInt32    char_code )
62  {
63    FT_UInt  result = 0;
64
65
66    if ( char_code < 256 )
67      result = cmap->gids[char_code];
68
69    return result;
70  }
71
72
73  FT_CALLBACK_DEF( FT_UInt32 )
74  cff_cmap_encoding_char_next( CFF_CMapStd   cmap,
75                               FT_UInt32    *pchar_code )
76  {
77    FT_UInt    result    = 0;
78    FT_UInt32  char_code = *pchar_code;
79
80
81    *pchar_code = 0;
82
83    if ( char_code < 255 )
84    {
85      FT_UInt  code = (FT_UInt)(char_code + 1);
86
87
88      for (;;)
89      {
90        if ( code >= 256 )
91          break;
92
93        result = cmap->gids[code];
94        if ( result != 0 )
95        {
96          *pchar_code = code;
97          break;
98        }
99
100        code++;
101      }
102    }
103    return result;
104  }
105
106
107  FT_DEFINE_CMAP_CLASS(cff_cmap_encoding_class_rec,
108    sizeof ( CFF_CMapStdRec ),
109
110    (FT_CMap_InitFunc)     cff_cmap_encoding_init,
111    (FT_CMap_DoneFunc)     cff_cmap_encoding_done,
112    (FT_CMap_CharIndexFunc)cff_cmap_encoding_char_index,
113    (FT_CMap_CharNextFunc) cff_cmap_encoding_char_next,
114
115    NULL, NULL, NULL, NULL, NULL
116  )
117
118
119  /*************************************************************************/
120  /*************************************************************************/
121  /*****                                                               *****/
122  /*****              CFF SYNTHETIC UNICODE ENCODING CMAP              *****/
123  /*****                                                               *****/
124  /*************************************************************************/
125  /*************************************************************************/
126
127  FT_CALLBACK_DEF( const char* )
128  cff_sid_to_glyph_name( TT_Face  face,
129                         FT_UInt  idx )
130  {
131    CFF_Font     cff     = (CFF_Font)face->extra.data;
132    CFF_Charset  charset = &cff->charset;
133    FT_UInt      sid     = charset->sids[idx];
134
135
136    return cff_index_get_sid_string( cff, sid );
137  }
138
139
140  FT_CALLBACK_DEF( FT_Error )
141  cff_cmap_unicode_init( PS_Unicodes  unicodes,
142                         FT_Pointer   pointer )
143  {
144    TT_Face             face    = (TT_Face)FT_CMAP_FACE( unicodes );
145    FT_Memory           memory  = FT_FACE_MEMORY( face );
146    CFF_Font            cff     = (CFF_Font)face->extra.data;
147    CFF_Charset         charset = &cff->charset;
148    FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)cff->psnames;
149
150    FT_UNUSED( pointer );
151
152
153    /* can't build Unicode map for CID-keyed font */
154    /* because we don't know glyph names.         */
155    if ( !charset->sids )
156      return FT_THROW( No_Unicode_Glyph_Name );
157
158    return psnames->unicodes_init( memory,
159                                   unicodes,
160                                   cff->num_glyphs,
161                                   (PS_GetGlyphNameFunc)&cff_sid_to_glyph_name,
162                                   (PS_FreeGlyphNameFunc)NULL,
163                                   (FT_Pointer)face );
164  }
165
166
167  FT_CALLBACK_DEF( void )
168  cff_cmap_unicode_done( PS_Unicodes  unicodes )
169  {
170    FT_Face    face   = FT_CMAP_FACE( unicodes );
171    FT_Memory  memory = FT_FACE_MEMORY( face );
172
173
174    FT_FREE( unicodes->maps );
175    unicodes->num_maps = 0;
176  }
177
178
179  FT_CALLBACK_DEF( FT_UInt )
180  cff_cmap_unicode_char_index( PS_Unicodes  unicodes,
181                               FT_UInt32    char_code )
182  {
183    TT_Face             face    = (TT_Face)FT_CMAP_FACE( unicodes );
184    CFF_Font            cff     = (CFF_Font)face->extra.data;
185    FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)cff->psnames;
186
187
188    return psnames->unicodes_char_index( unicodes, char_code );
189  }
190
191
192  FT_CALLBACK_DEF( FT_UInt32 )
193  cff_cmap_unicode_char_next( PS_Unicodes  unicodes,
194                              FT_UInt32   *pchar_code )
195  {
196    TT_Face             face    = (TT_Face)FT_CMAP_FACE( unicodes );
197    CFF_Font            cff     = (CFF_Font)face->extra.data;
198    FT_Service_PsCMaps  psnames = (FT_Service_PsCMaps)cff->psnames;
199
200
201    return psnames->unicodes_char_next( unicodes, pchar_code );
202  }
203
204
205  FT_DEFINE_CMAP_CLASS(cff_cmap_unicode_class_rec,
206    sizeof ( PS_UnicodesRec ),
207
208    (FT_CMap_InitFunc)     cff_cmap_unicode_init,
209    (FT_CMap_DoneFunc)     cff_cmap_unicode_done,
210    (FT_CMap_CharIndexFunc)cff_cmap_unicode_char_index,
211    (FT_CMap_CharNextFunc) cff_cmap_unicode_char_next,
212
213    NULL, NULL, NULL, NULL, NULL
214  )
215
216/* END */
217