1 /***************************************************************************/
5 /* OpenType font driver implementation (body). */
7 /* Copyright 1996-2001, 2002 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_FREETYPE_H
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_INTERNAL_SFNT_H
24 #include FT_TRUETYPE_IDS_H
25 #include FT_INTERNAL_POSTSCRIPT_NAMES_H
34 /*************************************************************************/
36 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
37 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
38 /* messages during execution. */
41 #define FT_COMPONENT trace_cffdriver
44 /*************************************************************************/
45 /*************************************************************************/
46 /*************************************************************************/
52 /*************************************************************************/
53 /*************************************************************************/
54 /*************************************************************************/
58 #define PAIR_TAG( left, right ) ( ( (FT_ULong)left << 16 ) | \
62 /*************************************************************************/
68 /* A driver method used to return the kerning vector between two */
69 /* glyphs of the same face. */
72 /* face :: A handle to the source face object. */
74 /* left_glyph :: The index of the left glyph in the kern pair. */
76 /* right_glyph :: The index of the right glyph in the kern pair. */
79 /* kerning :: The kerning vector. This is in font units for */
80 /* scalable formats, and in pixels for fixed-sizes */
84 /* FreeType error code. 0 means success. */
87 /* Only horizontal layouts (left-to-right & right-to-left) are */
88 /* supported by this function. Other layouts, or more sophisticated */
89 /* kernings, are out of scope of this method (the basic driver */
90 /* interface is meant to be simple). */
92 /* They can be implemented by format-specific interfaces. */
95 Get_Kerning( TT_Face face,
104 return CFF_Err_Invalid_Face_Handle;
109 if ( face->kern_pairs )
111 /* there are some kerning pairs in this font file! */
112 FT_ULong search_tag = PAIR_TAG( left_glyph, right_glyph );
117 right = face->num_kern_pairs - 1;
119 while ( left <= right )
121 FT_Long middle = left + ( ( right - left ) >> 1 );
125 pair = face->kern_pairs + middle;
126 cur_pair = PAIR_TAG( pair->left, pair->right );
128 if ( cur_pair == search_tag )
131 if ( cur_pair < search_tag )
142 kerning->x = pair->value;
150 /*************************************************************************/
156 /* A driver method used to load a glyph within a given glyph slot. */
159 /* slot :: A handle to the target slot object where the glyph */
160 /* will be loaded. */
162 /* size :: A handle to the source face size at which the glyph */
163 /* must be scaled, loaded, etc. */
165 /* glyph_index :: The index of the glyph in the font file. */
167 /* load_flags :: A flag indicating what to load for this glyph. The */
168 /* FTLOAD_??? constants can be used to control the */
169 /* glyph loading process (e.g., whether the outline */
170 /* should be scaled, whether to load bitmaps or not, */
171 /* whether to hint the outline, etc). */
174 /* FreeType error code. 0 means success. */
177 Load_Glyph( CFF_GlyphSlot slot,
179 FT_UShort glyph_index,
180 FT_Int32 load_flags )
186 return CFF_Err_Invalid_Slot_Handle;
188 /* check whether we want a scaled outline or bitmap */
190 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
192 if ( load_flags & FT_LOAD_NO_SCALE )
195 /* reset the size object if necessary */
198 /* these two object must have the same parent */
199 if ( size->face != slot->root.face )
200 return CFF_Err_Invalid_Face_Handle;
203 /* now load the glyph outline if necessary */
204 error = cff_slot_load( slot, size, glyph_index, load_flags );
206 /* force drop-out mode to 2 - irrelevant now */
207 /* slot->outline.dropout_mode = 2; */
213 /*************************************************************************/
214 /*************************************************************************/
215 /*************************************************************************/
218 /**** C H A R A C T E R M A P P I N G S ****/
221 /*************************************************************************/
222 /*************************************************************************/
223 /*************************************************************************/
226 cff_get_glyph_name( CFF_Face face,
231 CFF_Font font = (CFF_Font)face->extra.data;
232 FT_Memory memory = FT_FACE_MEMORY( face );
235 PSNames_Service psnames;
239 psnames = (PSNames_Service)FT_Get_Module_Interface(
240 face->root.driver->root.library, "psnames" );
244 FT_ERROR(( "cff_get_glyph_name:" ));
245 FT_ERROR(( " cannot open CFF & CEF fonts\n" ));
247 FT_ERROR(( " without the `PSNames' module\n" ));
248 error = CFF_Err_Unknown_File_Format;
252 /* first, locate the sid in the charset table */
253 sid = font->charset.sids[glyph_index];
255 /* now, lookup the name itself */
256 gname = cff_index_get_sid_string( &font->string_index, sid, psnames );
258 if ( buffer_max > 0 )
260 FT_UInt len = (FT_UInt)ft_strlen( gname );
263 if ( len >= buffer_max )
264 len = buffer_max - 1;
266 FT_MEM_COPY( buffer, gname, len );
267 ((FT_Byte*)buffer)[len] = 0;
279 /*************************************************************************/
282 /* cff_get_name_index */
285 /* Uses the psnames module and the CFF font's charset to to return a */
286 /* a given glyph name's glyph index. */
289 /* face :: A handle to the source face object. */
291 /* glyph_name :: The glyph name. */
294 /* Glyph index. 0 means `undefined character code'. */
297 cff_get_name_index( CFF_Face face,
298 FT_String* glyph_name )
302 PSNames_Service psnames;
303 FT_Memory memory = FT_FACE_MEMORY( face );
310 cff = (CFF_FontRec *)face->extra.data;
311 charset = &cff->charset;
313 psnames = (PSNames_Service)FT_Get_Module_Interface(
314 face->root.driver->root.library, "psnames" );
316 for ( i = 0; i < cff->num_glyphs; i++ )
318 sid = charset->sids[i];
321 name = cff_index_get_name( &cff->string_index, sid - 391 );
323 name = (FT_String *)psnames->adobe_std_strings( sid );
325 result = ft_strcmp( glyph_name, name );
338 /*************************************************************************/
339 /*************************************************************************/
340 /*************************************************************************/
343 /**** D R I V E R I N T E R F A C E ****/
346 /*************************************************************************/
347 /*************************************************************************/
348 /*************************************************************************/
350 static FT_Module_Interface
351 cff_get_interface( CFF_Driver driver,
352 const char* module_interface )
357 #ifndef FT_CONFIG_OPTION_NO_GLYPH_NAMES
359 if ( ft_strcmp( (const char*)module_interface, "glyph_name" ) == 0 )
360 return (FT_Module_Interface)cff_get_glyph_name;
362 if ( ft_strcmp( (const char*)module_interface, "name_index" ) == 0 )
363 return (FT_Module_Interface)cff_get_name_index;
367 /* we simply pass our request to the `sfnt' module */
368 sfnt = FT_Get_Module( driver->root.root.library, "sfnt" );
370 return sfnt ? sfnt->clazz->get_interface( sfnt, module_interface ) : 0;
374 /* The FT_DriverInterface structure is defined in ftdriver.h. */
376 FT_CALLBACK_TABLE_DEF
377 const FT_Driver_ClassRec cff_driver_class =
379 /* begin with the FT_Module_Class fields */
381 ft_module_font_driver |
382 ft_module_driver_scalable |
383 ft_module_driver_has_hinter,
385 sizeof( CFF_DriverRec ),
390 0, /* module-specific interface */
392 (FT_Module_Constructor)cff_driver_init,
393 (FT_Module_Destructor) cff_driver_done,
394 (FT_Module_Requester) cff_get_interface,
397 /* now the specific driver fields */
398 sizeof( TT_FaceRec ),
399 sizeof( FT_SizeRec ),
400 sizeof( CFF_GlyphSlotRec ),
402 (FT_Face_InitFunc) cff_face_init,
403 (FT_Face_DoneFunc) cff_face_done,
404 (FT_Size_InitFunc) cff_size_init,
405 (FT_Size_DoneFunc) cff_size_done,
406 (FT_Slot_InitFunc) cff_slot_init,
407 (FT_Slot_DoneFunc) cff_slot_done,
409 (FT_Size_ResetPointsFunc)cff_size_reset,
410 (FT_Size_ResetPixelsFunc)cff_size_reset,
412 (FT_Slot_LoadFunc) Load_Glyph,
414 (FT_Face_GetKerningFunc) Get_Kerning,
415 (FT_Face_AttachFunc) 0,
416 (FT_Face_GetAdvancesFunc)0,