1 /***************************************************************************/
5 /* PSNames module implementation (body). */
7 /* Copyright 1996-2001 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
20 #include FT_INTERNAL_POSTSCRIPT_NAMES_H
21 #include FT_INTERNAL_OBJECTS_H
28 #include <stdlib.h> /* for qsort() */
29 #include <string.h> /* for strcmp(), strncpy() */
32 #ifndef FT_CONFIG_OPTION_NO_POSTSCRIPT_NAMES
35 #ifdef FT_CONFIG_OPTION_ADOBE_GLYPH_LIST
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. */
43 PS_Unicode_Value( const char* glyph_name )
46 char first = glyph_name[0];
50 /* if the name begins with `uni', then the glyph name may be a */
51 /* hard-coded unicode character code. */
52 if ( glyph_name[0] == 'u' &&
53 glyph_name[1] == 'n' &&
54 glyph_name[2] == 'i' )
56 /* determine whether the next four characters following are */
59 /* XXX: Add code to deal with ligatures, i.e. glyph names like */
60 /* `uniXXXXYYYYZZZZ'... */
64 const char* p = glyph_name + 3;
67 for ( count = 4; count > 0; count--, p++ )
73 d = (unsigned char)c - '0';
76 d = (unsigned char)c - 'A';
83 /* exit if a non-uppercase hexadecimal character was found */
87 value = ( value << 4 ) + d;
93 /* look for a non-initial dot in the glyph name in order to */
94 /* sort-out variants like `A.swash', `e.final', etc. */
102 while ( *p && *p != '.' )
105 len = (int)( p - glyph_name );
107 if ( *p && len < 64 )
109 strncpy( temp, glyph_name, len );
115 /* now, look up the glyph in the Adobe Glyph List */
116 for ( n = 0; n < NUM_ADOBE_GLYPHS; n++ )
118 const char* name = sid_standard_names[n];
121 if ( first == name[0] && strcmp( glyph_name, name ) == 0 )
122 return ps_names_to_unicode[n];
125 /* not found, there is probably no Unicode value for this glyph name */
130 /* qsort callback to sort the unicode map */
131 FT_CALLBACK_DEF( int )
132 compare_uni_maps( const void* a,
135 PS_UniMap* map1 = (PS_UniMap*)a;
136 PS_UniMap* map2 = (PS_UniMap*)b;
139 return ( map1->unicode - map2->unicode );
143 /* Builds a table that maps Unicode values to glyph indices */
145 PS_Build_Unicode_Table( FT_Memory memory,
147 const char** glyph_names,
153 /* we first allocate the table */
157 if ( !ALLOC_ARRAY( table->maps, num_glyphs, PS_UniMap ) )
167 for ( n = 0; n < num_glyphs; n++ )
169 const char* gname = glyph_names[n];
174 uni_char = PS_Unicode_Value( gname );
176 if ( uni_char != 0 && uni_char != 0xFFFF )
178 map->unicode = uni_char;
179 map->glyph_index = n;
185 /* now, compress the table a bit */
186 count = (FT_UInt)( map - table->maps );
188 if ( count > 0 && REALLOC( table->maps,
189 num_glyphs * sizeof ( PS_UniMap ),
190 count * sizeof ( PS_UniMap ) ) )
197 error = PSnames_Err_Invalid_Argument; /* no unicode chars here! */
200 /* sort the table in increasing order of unicode values */
201 qsort( table->maps, count, sizeof ( PS_UniMap ), compare_uni_maps );
203 table->num_maps = count;
211 PS_Lookup_Unicode( PS_Unicodes* table,
214 PS_UniMap *min, *max, *mid;
217 /* perform a binary search on the table */
220 max = min + table->num_maps - 1;
224 mid = min + ( max - min ) / 2;
225 if ( mid->unicode == unicode )
226 return mid->glyph_index;
231 if ( mid->unicode < unicode )
241 #endif /* FT_CONFIG_OPTION_ADOBE_GLYPH_LIST */
245 PS_Macintosh_Name( FT_UInt name_index )
247 if ( name_index >= 258 )
250 return ps_glyph_names[mac_standard_names[name_index]];
255 PS_Standard_Strings( FT_UInt sid )
257 return ( sid < NUM_SID_GLYPHS ? sid_standard_names[sid] : 0 );
262 const PSNames_Interface psnames_interface =
264 #ifdef FT_CONFIG_OPTION_ADOBE_GLYPH_LIST
266 (PS_Unicode_Value_Func) PS_Unicode_Value,
267 (PS_Build_Unicodes_Func) PS_Build_Unicode_Table,
268 (PS_Lookup_Unicode_Func) PS_Lookup_Unicode,
276 #endif /* FT_CONFIG_OPTION_ADOBE_GLYPH_LIST */
278 (PS_Macintosh_Name_Func) PS_Macintosh_Name,
279 (PS_Adobe_Std_Strings_Func)PS_Standard_Strings,
281 t1_standard_encoding,
286 #endif /* !FT_CONFIG_OPTION_NO_POSTSCRIPT_NAMES */
289 FT_CALLBACK_TABLE_DEF
290 const FT_Module_Class psnames_module_class =
292 0, /* this is not a font driver, nor a renderer */
293 sizeof( FT_ModuleRec ),
295 "psnames", /* driver name */
296 0x10000L, /* driver version */
297 0x20000L, /* driver requires FreeType 2 or above */
299 #ifdef FT_CONFIG_OPTION_NO_POSTSCRIPT_NAMES
302 (void*)&psnames_interface, /* module specific interface */
305 (FT_Module_Constructor)0,
306 (FT_Module_Destructor) 0,
307 (FT_Module_Requester) 0