Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 1 | /***************************************************************************/ |
| 2 | /* */ |
| 3 | /* psmodule.c */ |
| 4 | /* */ |
| 5 | /* PSNames module implementation (body). */ |
| 6 | /* */ |
| 7 | /* Copyright 1996-2000 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 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 19 | #include <freetype/internal/psnames.h> |
| 20 | #include <freetype/internal/ftobjs.h> |
| 21 | #include <psmodule.h> |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 22 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 24 | #include <string.h> /* for strcmp(), strncpy() */ |
| 25 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 26 | |
| 27 | #ifndef FT_CONFIG_OPTION_NO_POSTSCRIPT_NAMES |
| 28 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 29 | |
| 30 | /* see the python script `freetype2/docs/glnames.py' which is used */ |
| 31 | /* to generate the following tables... */ |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 32 | #include <pstables.h> |
| 33 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 34 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 35 | #ifdef FT_CONFIG_OPTION_ADOBE_GLYPH_LIST |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 36 | |
| 37 | |
| 38 | /* return the Unicode value corresponding to a given glyph. Note that */ |
| 39 | /* we do deal with glyph variants by detecting a non-initial dot in */ |
| 40 | /* the name, as in `A.swash' or `e.final', etc. */ |
| 41 | /* */ |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 42 | static |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 43 | FT_ULong PS_Unicode_Value( const char* glyph_name ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 44 | { |
| 45 | FT_Int n; |
| 46 | char first = glyph_name[0]; |
| 47 | char temp[64]; |
| 48 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 49 | |
| 50 | /* if the name begins with `uni', then the glyph name may be a */ |
| 51 | /* hard-coded unicode character code. */ |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 52 | if ( glyph_name[0] == 'u' && |
| 53 | glyph_name[1] == 'n' && |
| 54 | glyph_name[2] == 'i' ) |
| 55 | { |
| 56 | /* determine wether the following characters are hexadecimal */ |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 57 | FT_Int count; |
| 58 | FT_ULong value = 0; |
| 59 | const char* p = glyph_name + 4; |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 60 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 61 | |
| 62 | for ( count = 4; count > 0; count--, p++ ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 63 | { |
| 64 | char c = *p; |
| 65 | unsigned char d; |
| 66 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 67 | |
| 68 | d = (unsigned char)c - '0'; |
| 69 | if ( d >= 10 ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 70 | { |
| 71 | d = (unsigned char)c - 'A'; |
| 72 | if ( d >= 6 ) |
| 73 | d = 16; |
| 74 | else |
| 75 | d += 10; |
| 76 | } |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 77 | /* exit if a non-uppercase-hexadecimal character was found */ |
| 78 | if ( d >= 16 ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 79 | break; |
| 80 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 81 | value = ( value << 4 ) + d; |
| 82 | if ( count == 0 ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 83 | return value; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* look for a non-initial dot in the glyph name in order to */ |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 88 | /* sort-out variants like `A.swash', `e.final', etc. */ |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 89 | { |
| 90 | const char* p; |
| 91 | int len; |
| 92 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 93 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 94 | p = glyph_name; |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 95 | while ( *p && *p != '.' ) |
| 96 | p++; |
| 97 | len = p - glyph_name; |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 98 | |
| 99 | if ( *p && len < 64 ) |
| 100 | { |
| 101 | strncpy( temp, glyph_name, len ); |
| 102 | temp[len] = 0; |
| 103 | glyph_name = temp; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* now, lookup the glyph in the Adobe Glyph List */ |
| 108 | for ( n = 0; n < NUM_ADOBE_GLYPHS; n++ ) |
| 109 | { |
| 110 | const char* name = t1_standard_glyphs[n]; |
| 111 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 112 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 113 | if ( first == name[0] && strcmp( glyph_name, name ) == 0 ) |
| 114 | return names_to_unicode[n]; |
| 115 | } |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 116 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 117 | /* not found, there is probably no Unicode value for this glyph name */ |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 122 | /* qsort callback to sort the unicode map */ |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 123 | static |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 124 | int compare_uni_maps( const void* a, |
| 125 | const void* b ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 126 | { |
| 127 | PS_UniMap* map1 = (PS_UniMap*)a; |
| 128 | PS_UniMap* map2 = (PS_UniMap*)b; |
| 129 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 130 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 131 | return ( map1->unicode < map2->unicode ? -1 : |
| 132 | map1->unicode > map2->unicode ? 1 : 0 ); |
| 133 | } |
| 134 | |
| 135 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 136 | /* Builds a table that maps Unicode values to glyph indices */ |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 137 | static |
| 138 | FT_Error PS_Build_Unicode_Table( FT_Memory memory, |
| 139 | FT_UInt num_glyphs, |
| 140 | const char** glyph_names, |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 141 | PS_Unicodes* table ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 142 | { |
| 143 | FT_Error error; |
| 144 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 145 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 146 | /* we first allocate the table */ |
| 147 | table->num_maps = 0; |
| 148 | table->maps = 0; |
| 149 | |
| 150 | if ( !ALLOC_ARRAY( table->maps, num_glyphs, PS_UniMap ) ) |
| 151 | { |
| 152 | FT_UInt n; |
| 153 | FT_UInt count; |
| 154 | PS_UniMap* map; |
| 155 | FT_ULong uni_char; |
| 156 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 157 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 158 | map = table->maps; |
| 159 | for ( n = 0; n < num_glyphs; n++ ) |
| 160 | { |
| 161 | const char* gname = glyph_names[n]; |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 162 | |
| 163 | |
| 164 | if ( gname ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 165 | { |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 166 | uni_char = PS_Unicode_Value( gname ); |
| 167 | if ( uni_char && uni_char != 0xFFFF ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 168 | { |
| 169 | map->unicode = uni_char; |
| 170 | map->glyph_index = n; |
| 171 | map++; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /* now, compress the table a bit */ |
| 177 | count = map - table->maps; |
| 178 | if ( count > 0 && REALLOC( table->maps, |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 179 | num_glyphs * sizeof ( PS_UniMap ), |
| 180 | count * sizeof ( PS_UniMap ) ) ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 181 | count = 0; |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 182 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 183 | if ( count == 0 ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 184 | { |
| 185 | FREE( table->maps ); |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 186 | if ( !error ) |
| 187 | error = FT_Err_Invalid_Argument; /* no unicode chars here! */ |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 188 | } |
| 189 | else |
| 190 | /* sort the table in increasing order of unicode values */ |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 191 | qsort( table->maps, count, sizeof ( PS_UniMap ), compare_uni_maps ); |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 192 | |
| 193 | table->num_maps = count; |
| 194 | } |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 195 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 196 | return error; |
| 197 | } |
| 198 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 199 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 200 | static |
| 201 | FT_UInt PS_Lookup_Unicode( PS_Unicodes* table, |
| 202 | FT_ULong unicode ) |
| 203 | { |
| 204 | PS_UniMap *min, *max, *mid; |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 205 | |
| 206 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 207 | /* perform a binary search on the table */ |
| 208 | min = table->maps; |
| 209 | max = min + table->num_maps - 1; |
| 210 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 211 | while ( min <= max ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 212 | { |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 213 | mid = min + ( max - min ) / 2; |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 214 | if ( mid->unicode == unicode ) |
| 215 | return mid->glyph_index; |
| 216 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 217 | if ( min == max ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 218 | break; |
| 219 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 220 | if ( mid->unicode < unicode ) |
| 221 | min = mid + 1; |
| 222 | else |
| 223 | max = mid - 1; |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | return 0xFFFF; |
| 227 | } |
| 228 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 229 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 230 | #endif /* FT_CONFIG_OPTION_ADOBE_GLYPH_LIST */ |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 231 | |
| 232 | |
| 233 | static |
| 234 | const char* PS_Macintosh_Name( FT_UInt name_index ) |
| 235 | { |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 236 | if ( name_index >= 258 ) |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 237 | name_index = 0; |
| 238 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 239 | return standard_glyph_names[mac_standard_names[name_index]]; |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 243 | static |
| 244 | const char* PS_Standard_Strings( FT_UInt sid ) |
| 245 | { |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 246 | return ( sid < NUM_STD_GLYPHS ? t1_standard_glyphs[sid] : 0 ); |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 250 | static const PSNames_Interface psnames_interface = |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 251 | { |
| 252 | #ifdef FT_CONFIG_OPTION_ADOBE_GLYPH_LIST |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 253 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 254 | (PS_Unicode_Value_Func) PS_Unicode_Value, |
| 255 | (PS_Build_Unicodes_Func) PS_Build_Unicode_Table, |
| 256 | (PS_Lookup_Unicode_Func) PS_Lookup_Unicode, |
| 257 | |
| 258 | #else |
| 259 | |
| 260 | 0, |
| 261 | 0, |
| 262 | 0, |
| 263 | |
| 264 | #endif /* FT_CONFIG_OPTION_ADOBE_GLYPH_LIST */ |
| 265 | |
| 266 | (PS_Macintosh_Name_Func) PS_Macintosh_Name, |
| 267 | (PS_Adobe_Std_Strings_Func)PS_Standard_Strings, |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 268 | |
| 269 | t1_standard_encoding, |
| 270 | t1_expert_encoding |
| 271 | }; |
| 272 | |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 273 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 274 | #endif /* !FT_CONFIG_OPTION_NO_POSTSCRIPT_NAMES */ |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 275 | |
| 276 | |
| 277 | const FT_Module_Class psnames_module_class = |
| 278 | { |
| 279 | 0, /* this is not a font driver, nor a renderer */ |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 280 | sizeof( FT_ModuleRec ), |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 281 | |
| 282 | "psnames", /* driver name */ |
| 283 | 100, /* driver version */ |
| 284 | 200, /* driver requires FreeType 2 or above */ |
| 285 | |
| 286 | #ifdef FT_CONFIG_OPTION_NO_POSTSCRIPT_NAMES |
| 287 | 0, |
| 288 | #else |
| 289 | (void*)&psnames_interface, /* module specific interface */ |
| 290 | #endif |
| 291 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 292 | (FT_Module_Constructor)0, |
| 293 | (FT_Module_Destructor) 0, |
| 294 | (FT_Module_Requester) 0 |
David Turner | aa4c28f | 2000-06-22 00:27:15 +0000 | [diff] [blame] | 295 | }; |
| 296 | |
Werner Lemberg | a929ba9 | 2000-06-25 06:47:11 +0000 | [diff] [blame^] | 297 | |
| 298 | /* END */ |