1 /***************************************************************************/
5 /* PSNames module implementation (body). */
7 /* Copyright 1996-2001, 2002, 2003, 2005, 2006, 2007 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_OBJECTS_H
21 #include FT_SERVICE_POSTSCRIPT_CMAPS_H
29 #ifndef FT_CONFIG_OPTION_NO_POSTSCRIPT_NAMES
32 #ifdef FT_CONFIG_OPTION_ADOBE_GLYPH_LIST
35 #define VARIANT_BIT 0x80000000UL
36 #define BASE_GLYPH( code ) ( (code) & ~VARIANT_BIT )
39 /* Return the Unicode value corresponding to a given glyph. Note that */
40 /* we do deal with glyph variants by detecting a non-initial dot in */
41 /* the name, as in `A.swash' or `e.final'; in this case, the */
42 /* VARIANT_BIT is set in the return value. */
45 ps_unicode_value( const char* glyph_name
)
47 /* If the name begins with `uni', then the glyph name may be a */
48 /* hard-coded unicode character code. */
49 if ( glyph_name
[0] == 'u' &&
50 glyph_name
[1] == 'n' &&
51 glyph_name
[2] == 'i' )
53 /* determine whether the next four characters following are */
56 /* XXX: Add code to deal with ligatures, i.e. glyph names like */
57 /* `uniXXXXYYYYZZZZ'... */
61 const char* p
= glyph_name
+ 3;
64 for ( count
= 4; count
> 0; count
--, p
++ )
70 d
= (unsigned char)c
- '0';
73 d
= (unsigned char)c
- 'A';
80 /* Exit if a non-uppercase hexadecimal character was found */
81 /* -- this also catches character codes below `0' since such */
82 /* negative numbers cast to `unsigned int' are far too big. */
86 value
= ( value
<< 4 ) + d
;
89 /* there must be exactly four hex digits */
95 return value
| VARIANT_BIT
;
99 /* If the name begins with `u', followed by four to six uppercase */
100 /* hexadecimal digits, it is a hard-coded unicode character code. */
101 if ( glyph_name
[0] == 'u' )
105 const char* p
= glyph_name
+ 1;
108 for ( count
= 6; count
> 0; count
--, p
++ )
114 d
= (unsigned char)c
- '0';
117 d
= (unsigned char)c
- 'A';
127 value
= ( value
<< 4 ) + d
;
135 return value
| VARIANT_BIT
;
139 /* Look for a non-initial dot in the glyph name in order to */
140 /* find variants like `A.swash', `e.final', etc. */
142 const char* p
= glyph_name
;
143 const char* dot
= NULL
;
148 if ( *p
== '.' && p
> glyph_name
)
155 /* now look up the glyph in the Adobe Glyph List */
157 return ft_get_adobe_glyph_index( glyph_name
, p
);
159 return ft_get_adobe_glyph_index( glyph_name
, dot
) | VARIANT_BIT
;
164 /* ft_qsort callback to sort the unicode map */
165 FT_CALLBACK_DEF( int )
166 compare_uni_maps( const void* a
,
169 PS_UniMap
* map1
= (PS_UniMap
*)a
;
170 PS_UniMap
* map2
= (PS_UniMap
*)b
;
171 FT_UInt32 unicode1
= BASE_GLYPH( map1
->unicode
);
172 FT_UInt32 unicode2
= BASE_GLYPH( map2
->unicode
);
175 /* sort base glyphs before glyph variants */
176 if ( unicode1
== unicode2
)
177 return map1
->unicode
- map2
->unicode
;
179 return unicode1
- unicode2
;
183 /* Build a table that maps Unicode values to glyph indices. */
185 ps_unicodes_init( FT_Memory memory
,
188 PS_GetGlyphNameFunc get_glyph_name
,
189 PS_FreeGlyphNameFunc free_glyph_name
,
190 FT_Pointer glyph_data
)
195 /* we first allocate the table */
199 if ( !FT_NEW_ARRAY( table
->maps
, num_glyphs
) )
209 for ( n
= 0; n
< num_glyphs
; n
++ )
211 const char* gname
= get_glyph_name( glyph_data
, n
);
216 uni_char
= ps_unicode_value( gname
);
218 if ( BASE_GLYPH( uni_char
) != 0 )
220 map
->unicode
= uni_char
;
221 map
->glyph_index
= n
;
225 if ( free_glyph_name
)
226 free_glyph_name( glyph_data
, gname
);
230 /* now compress the table a bit */
231 count
= (FT_UInt
)( map
- table
->maps
);
235 FT_FREE( table
->maps
);
237 error
= PSnames_Err_Invalid_Argument
; /* No unicode chars here! */
240 /* Reallocate if the number of used entries is much smaller. */
241 if ( count
< num_glyphs
/ 2 )
243 (void)FT_RENEW_ARRAY( table
->maps
, num_glyphs
, count
);
244 error
= PSnames_Err_Ok
;
247 /* Sort the table in increasing order of unicode values, */
248 /* taking care of glyph variants. */
249 ft_qsort( table
->maps
, count
, sizeof ( PS_UniMap
),
253 table
->num_maps
= count
;
261 ps_unicodes_char_index( PS_Unicodes table
,
264 PS_UniMap
*min
, *max
, *mid
, *result
= NULL
;
267 /* Perform a binary search on the table. */
270 max
= min
+ table
->num_maps
- 1;
274 FT_UInt32 base_glyph
;
277 mid
= min
+ ( ( max
- min
) >> 1 );
279 if ( mid
->unicode
== unicode
)
285 base_glyph
= BASE_GLYPH( mid
->unicode
);
287 if ( base_glyph
== unicode
)
288 result
= mid
; /* remember match but continue search for base glyph */
293 if ( base_glyph
< unicode
)
300 return result
->glyph_index
;
307 ps_unicodes_char_next( PS_Unicodes table
,
311 FT_UInt32 char_code
= *unicode
+ 1;
316 FT_UInt max
= table
->num_maps
;
319 FT_UInt32 base_glyph
;
324 mid
= min
+ ( ( max
- min
) >> 1 );
325 map
= table
->maps
+ mid
;
327 if ( map
->unicode
== char_code
)
329 result
= map
->glyph_index
;
333 base_glyph
= BASE_GLYPH( map
->unicode
);
335 if ( base_glyph
== char_code
)
336 result
= map
->glyph_index
;
338 if ( base_glyph
< char_code
)
345 goto Exit
; /* we have a variant glyph */
347 /* we didn't find it; check whether we have a map just above it */
350 if ( min
< table
->num_maps
)
352 map
= table
->maps
+ min
;
353 result
= map
->glyph_index
;
354 char_code
= BASE_GLYPH( map
->unicode
);
359 *unicode
= char_code
;
364 #endif /* FT_CONFIG_OPTION_ADOBE_GLYPH_LIST */
368 ps_get_macintosh_name( FT_UInt name_index
)
370 if ( name_index
>= FT_NUM_MAC_NAMES
)
373 return ft_standard_glyph_names
+ ft_mac_names
[name_index
];
378 ps_get_standard_strings( FT_UInt sid
)
380 if ( sid
>= FT_NUM_SID_NAMES
)
383 return ft_standard_glyph_names
+ ft_sid_names
[sid
];
388 const FT_Service_PsCMapsRec pscmaps_interface
=
390 #ifdef FT_CONFIG_OPTION_ADOBE_GLYPH_LIST
392 (PS_Unicode_ValueFunc
) ps_unicode_value
,
393 (PS_Unicodes_InitFunc
) ps_unicodes_init
,
394 (PS_Unicodes_CharIndexFunc
)ps_unicodes_char_index
,
395 (PS_Unicodes_CharNextFunc
) ps_unicodes_char_next
,
404 #endif /* FT_CONFIG_OPTION_ADOBE_GLYPH_LIST */
406 (PS_Macintosh_NameFunc
) ps_get_macintosh_name
,
407 (PS_Adobe_Std_StringsFunc
) ps_get_standard_strings
,
409 t1_standard_encoding
,
414 static const FT_ServiceDescRec pscmaps_services
[] =
416 { FT_SERVICE_ID_POSTSCRIPT_CMAPS
, &pscmaps_interface
},
422 psnames_get_service( FT_Module module
,
423 const char* service_id
)
427 return ft_service_list_lookup( pscmaps_services
, service_id
);
430 #endif /* !FT_CONFIG_OPTION_NO_POSTSCRIPT_NAMES */
434 FT_CALLBACK_TABLE_DEF
435 const FT_Module_Class psnames_module_class
=
437 0, /* this is not a font driver, nor a renderer */
438 sizeof ( FT_ModuleRec
),
440 "psnames", /* driver name */
441 0x10000L
, /* driver version */
442 0x20000L
, /* driver requires FreeType 2 or above */
444 #ifdef FT_CONFIG_OPTION_NO_POSTSCRIPT_NAMES
446 (FT_Module_Constructor
)0,
447 (FT_Module_Destructor
) 0,
448 (FT_Module_Requester
) 0
450 (void*)&pscmaps_interface
, /* module specific interface */
451 (FT_Module_Constructor
)0,
452 (FT_Module_Destructor
) 0,
453 (FT_Module_Requester
) psnames_get_service