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