1 /***************************************************************************/
5 /* TrueType font driver 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_DEBUG_H
21 #include FT_INTERNAL_STREAM_H
22 #include FT_INTERNAL_SFNT_H
23 #include FT_TRUETYPE_IDS_H
31 /*************************************************************************/
33 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
34 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
35 /* messages during execution. */
38 #define FT_COMPONENT trace_ttdriver
41 /*************************************************************************/
42 /*************************************************************************/
43 /*************************************************************************/
49 /*************************************************************************/
50 /*************************************************************************/
51 /*************************************************************************/
55 #define PAIR_TAG( left, right ) ( ( (FT_ULong)left << 16 ) | \
59 /*************************************************************************/
65 /* A driver method used to return the kerning vector between two */
66 /* glyphs of the same face. */
69 /* face :: A handle to the source face object. */
71 /* left_glyph :: The index of the left glyph in the kern pair. */
73 /* right_glyph :: The index of the right glyph in the kern pair. */
76 /* kerning :: The kerning vector. This is in font units for */
77 /* scalable formats, and in pixels for fixed-sizes */
81 /* FreeType error code. 0 means success. */
84 /* Only horizontal layouts (left-to-right & right-to-left) are */
85 /* supported by this function. Other layouts, or more sophisticated */
86 /* kernings, are out of scope of this method (the basic driver */
87 /* interface is meant to be simple). */
89 /* They can be implemented by format-specific interfaces. */
92 Get_Kerning( TT_Face face,
101 return TT_Err_Invalid_Face_Handle;
106 if ( face->kern_pairs )
108 /* there are some kerning pairs in this font file! */
109 FT_ULong search_tag = PAIR_TAG( left_glyph, right_glyph );
114 right = face->num_kern_pairs - 1;
116 while ( left <= right )
118 FT_Int middle = left + ( ( right - left ) >> 1 );
122 pair = face->kern_pairs + middle;
123 cur_pair = PAIR_TAG( pair->left, pair->right );
125 if ( cur_pair == search_tag )
128 if ( cur_pair < search_tag )
139 kerning->x = pair->value;
147 /*************************************************************************/
148 /*************************************************************************/
149 /*************************************************************************/
152 /**** S I Z E S ****/
155 /*************************************************************************/
156 /*************************************************************************/
157 /*************************************************************************/
160 /*************************************************************************/
166 /* A driver method used to reset a size's character sizes (horizontal */
167 /* and vertical) expressed in fractional points. */
170 /* char_width :: The character width expressed in 26.6 */
171 /* fractional points. */
173 /* char_height :: The character height expressed in 26.6 */
174 /* fractional points. */
176 /* horz_resolution :: The horizontal resolution of the output device. */
178 /* vert_resolution :: The vertical resolution of the output device. */
181 /* size :: A handle to the target size object. */
184 /* FreeType error code. 0 means success. */
187 Set_Char_Sizes( TT_Size size,
188 FT_F26Dot6 char_width,
189 FT_F26Dot6 char_height,
190 FT_UInt horz_resolution,
191 FT_UInt vert_resolution )
193 FT_Size_Metrics* metrics = &size->root.metrics;
194 TT_Face face = (TT_Face)size->root.face;
195 FT_Long dim_x, dim_y;
198 /* This bit flag, when set, indicates that the pixel size must be */
199 /* truncated to an integer. Nearly all TrueType fonts have this */
200 /* bit set, as hinting won't work really well otherwise. */
202 /* However, for those rare fonts who do not set it, we override */
203 /* the default computations performed by the base layer. I */
204 /* really don't know whether this is useful, but hey, that's the */
207 if ( ( face->header.Flags & 8 ) == 0 )
209 /* Compute pixel sizes in 26.6 units */
210 dim_x = ( char_width * horz_resolution ) / 72;
211 dim_y = ( char_height * vert_resolution ) / 72;
213 metrics->x_scale = FT_DivFix( dim_x, face->root.units_per_EM );
214 metrics->y_scale = FT_DivFix( dim_y, face->root.units_per_EM );
216 metrics->x_ppem = (FT_UShort)( dim_x >> 6 );
217 metrics->y_ppem = (FT_UShort)( dim_y >> 6 );
220 size->ttmetrics.valid = FALSE;
221 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
222 size->strike_index = 0xFFFF;
225 return TT_Reset_Size( size );
229 /*************************************************************************/
232 /* Set_Pixel_Sizes */
235 /* A driver method used to reset a size's character sizes (horizontal */
236 /* and vertical) expressed in integer pixels. */
239 /* pixel_width :: The character width expressed in integer pixels. */
241 /* pixel_height :: The character height expressed in integer pixels. */
244 /* size :: A handle to the target size object. */
247 /* FreeType error code. 0 means success. */
250 Set_Pixel_Sizes( TT_Size size,
252 FT_UInt pixel_height )
254 FT_UNUSED( pixel_width );
255 FT_UNUSED( pixel_height );
257 /* many things have been pre-computed by the base layer */
259 size->ttmetrics.valid = FALSE;
260 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
261 size->strike_index = 0xFFFF;
264 return TT_Reset_Size( size );
268 /*************************************************************************/
274 /* A driver method used to load a glyph within a given glyph slot. */
277 /* slot :: A handle to the target slot object where the glyph */
278 /* will be loaded. */
280 /* size :: A handle to the source face size at which the glyph */
281 /* must be scaled, loaded, etc. */
283 /* glyph_index :: The index of the glyph in the font file. */
285 /* load_flags :: A flag indicating what to load for this glyph. The */
286 /* FTLOAD_??? constants can be used to control the */
287 /* glyph loading process (e.g., whether the outline */
288 /* should be scaled, whether to load bitmaps or not, */
289 /* whether to hint the outline, etc). */
292 /* FreeType error code. 0 means success. */
295 Load_Glyph( TT_GlyphSlot slot,
297 FT_UShort glyph_index,
304 return TT_Err_Invalid_Slot_Handle;
306 /* check whether we want a scaled outline or bitmap */
308 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
310 if ( load_flags & FT_LOAD_NO_SCALE )
313 /* reset the size object if necessary */
316 /* these two object must have the same parent */
317 if ( size->root.face != slot->face )
318 return TT_Err_Invalid_Face_Handle;
320 if ( !size->ttmetrics.valid )
322 if ( FT_SET_ERROR( TT_Reset_Size( size ) ) )
327 /* now load the glyph outline if necessary */
328 error = TT_Load_Glyph( size, slot, glyph_index, load_flags );
330 /* force drop-out mode to 2 - irrelevant now */
331 /* slot->outline.dropout_mode = 2; */
337 /*************************************************************************/
338 /*************************************************************************/
339 /*************************************************************************/
342 /**** C H A R A C T E R M A P P I N G S ****/
345 /*************************************************************************/
346 /*************************************************************************/
347 /*************************************************************************/
349 /*************************************************************************/
355 /* Uses a charmap to return a given character code's glyph index. */
358 /* charmap :: A handle to the source charmap object. */
359 /* charcode :: The character code. */
362 /* Glyph index. 0 means `undefined character code'. */
365 Get_Char_Index( TT_CharMap charmap,
373 cmap = &charmap->cmap;
374 face = (TT_Face)charmap->root.face;
376 /* Load table if needed */
379 SFNT_Interface* sfnt = (SFNT_Interface*)face->sfnt;
382 error = sfnt->load_charmap( face, cmap, face->root.stream );
389 if ( cmap->get_index )
390 return cmap->get_index( cmap, charcode );
396 /*************************************************************************/
397 /*************************************************************************/
398 /*************************************************************************/
401 /**** D R I V E R I N T E R F A C E ****/
404 /*************************************************************************/
405 /*************************************************************************/
406 /*************************************************************************/
409 static FT_Module_Interface
410 tt_get_interface( TT_Driver driver,
411 const char* interface )
413 FT_Module sfntd = FT_Get_Module( driver->root.root.library,
415 SFNT_Interface* sfnt;
418 /* only return the default interface from the SFNT module */
421 sfnt = (SFNT_Interface*)( sfntd->clazz->module_interface );
423 return sfnt->get_interface( FT_MODULE( driver ), interface );
430 /* The FT_DriverInterface structure is defined in ftdriver.h. */
432 FT_CALLBACK_TABLE_DEF
433 const FT_Driver_Class tt_driver_class =
436 ft_module_font_driver |
437 ft_module_driver_scalable |
438 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
439 ft_module_driver_has_hinter,
444 sizeof ( TT_DriverRec ),
446 "truetype", /* driver name */
447 0x10000L, /* driver version == 1.0 */
448 0x20000L, /* driver requires FreeType 2.0 or above */
450 (void*)0, /* driver specific interface */
452 (FT_Module_Constructor)TT_Init_Driver,
453 (FT_Module_Destructor) TT_Done_Driver,
454 (FT_Module_Requester) tt_get_interface,
457 sizeof ( TT_FaceRec ),
458 sizeof ( TT_SizeRec ),
459 sizeof ( FT_GlyphSlotRec ),
462 (FTDriver_initFace) TT_Init_Face,
463 (FTDriver_doneFace) TT_Done_Face,
464 (FTDriver_initSize) TT_Init_Size,
465 (FTDriver_doneSize) TT_Done_Size,
466 (FTDriver_initGlyphSlot)0,
467 (FTDriver_doneGlyphSlot)0,
469 (FTDriver_setCharSizes) Set_Char_Sizes,
470 (FTDriver_setPixelSizes)Set_Pixel_Sizes,
471 (FTDriver_loadGlyph) Load_Glyph,
472 (FTDriver_getCharIndex) Get_Char_Index,
474 (FTDriver_getKerning) Get_Kerning,
475 (FTDriver_attachFile) 0,
476 (FTDriver_getAdvances) 0
480 #ifdef FT_CONFIG_OPTION_DYNAMIC_DRIVERS
483 /*************************************************************************/
489 /* This function is used when compiling the TrueType driver as a */
490 /* shared library (`.DLL' or `.so'). It will be used by the */
491 /* high-level library of FreeType to retrieve the address of the */
492 /* driver's generic interface. */
494 /* It shouldn't be implemented in a static build, as each driver must */
495 /* have the same function as an exported entry point. */
498 /* The address of the TrueType's driver generic interface. The */
499 /* format-specific interface can then be retrieved through the method */
500 /* interface->get_format_interface. */
502 FT_EXPORT_DEF( const FT_Driver_Class* )
503 getDriverClass( void )
505 return &tt_driver_class;
509 #endif /* CONFIG_OPTION_DYNAMIC_DRIVERS */