1 /***************************************************************************/
5 /* TrueType 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_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 FT_Size_Metrics* metrics2 = &size->metrics;
195 TT_Face face = (TT_Face)size->root.face;
196 FT_Long dim_x, dim_y;
199 *metrics2 = *metrics;
201 /* This bit flag, when set, indicates that the pixel size must be */
202 /* truncated to an integer. Nearly all TrueType fonts have this */
203 /* bit set, as hinting won't work really well otherwise. */
205 if ( ( face->header.Flags & 8 ) != 0 )
207 /* we need to use rounding in the following computations. Otherwise,
208 * the resulting hinted outlines will be very slightly distorted
210 dim_x = ( ( char_width * horz_resolution + (36+32*72) ) / 72 ) & -64;
211 dim_y = ( ( char_height * vert_resolution + (36+32*72) ) / 72 ) & -64;
215 dim_x = ( ( char_width * horz_resolution + 36 ) / 72 );
216 dim_y = ( ( char_height * vert_resolution + 36 ) / 72 );
219 /* we only modify "metrics2", not "metrics", so these changes have */
220 /* no effect on the result of the auto-hinter when it is used */
222 metrics2->x_ppem = (FT_UShort)( dim_x >> 6 );
223 metrics2->y_ppem = (FT_UShort)( dim_y >> 6 );
224 metrics2->x_scale = FT_DivFix( dim_x, face->root.units_per_EM );
225 metrics2->y_scale = FT_DivFix( dim_y, face->root.units_per_EM );
227 size->ttmetrics.valid = FALSE;
228 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
229 size->strike_index = 0xFFFF;
232 return tt_size_reset( size );
236 /*************************************************************************/
239 /* Set_Pixel_Sizes */
242 /* A driver method used to reset a size's character sizes (horizontal */
243 /* and vertical) expressed in integer pixels. */
246 /* pixel_width :: The character width expressed in integer pixels. */
248 /* pixel_height :: The character height expressed in integer pixels. */
251 /* size :: A handle to the target size object. */
254 /* FreeType error code. 0 means success. */
257 Set_Pixel_Sizes( TT_Size size,
259 FT_UInt pixel_height )
261 FT_UNUSED( pixel_width );
262 FT_UNUSED( pixel_height );
264 /* many things have been pre-computed by the base layer */
266 size->metrics = size->root.metrics;
267 size->ttmetrics.valid = FALSE;
268 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
269 size->strike_index = 0xFFFF;
272 return tt_size_reset( size );
276 /*************************************************************************/
282 /* A driver method used to load a glyph within a given glyph slot. */
285 /* slot :: A handle to the target slot object where the glyph */
286 /* will be loaded. */
288 /* size :: A handle to the source face size at which the glyph */
289 /* must be scaled, loaded, etc. */
291 /* glyph_index :: The index of the glyph in the font file. */
293 /* load_flags :: A flag indicating what to load for this glyph. The */
294 /* FTLOAD_??? constants can be used to control the */
295 /* glyph loading process (e.g., whether the outline */
296 /* should be scaled, whether to load bitmaps or not, */
297 /* whether to hint the outline, etc). */
300 /* FreeType error code. 0 means success. */
303 Load_Glyph( TT_GlyphSlot slot,
305 FT_UShort glyph_index,
306 FT_Int32 load_flags )
312 return TT_Err_Invalid_Slot_Handle;
314 /* check whether we want a scaled outline or bitmap */
316 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_NO_HINTING;
318 if ( load_flags & FT_LOAD_NO_SCALE )
321 /* reset the size object if necessary */
324 /* these two object must have the same parent */
325 if ( size->root.face != slot->face )
326 return TT_Err_Invalid_Face_Handle;
328 if ( !size->ttmetrics.valid )
330 if ( FT_SET_ERROR( tt_size_reset( size ) ) )
335 /* now load the glyph outline if necessary */
336 error = TT_Load_Glyph( size, slot, glyph_index, load_flags );
338 /* force drop-out mode to 2 - irrelevant now */
339 /* slot->outline.dropout_mode = 2; */
345 /*************************************************************************/
346 /*************************************************************************/
347 /*************************************************************************/
350 /**** D R I V E R I N T E R F A C E ****/
353 /*************************************************************************/
354 /*************************************************************************/
355 /*************************************************************************/
358 static FT_Module_Interface
359 tt_get_interface( TT_Driver driver,
360 const char* tt_interface )
362 FT_Module sfntd = FT_Get_Module( driver->root.root.library,
367 /* only return the default interface from the SFNT module */
370 sfnt = (SFNT_Service)( sfntd->clazz->module_interface );
372 return sfnt->get_interface( FT_MODULE( driver ), tt_interface );
379 /* The FT_DriverInterface structure is defined in ftdriver.h. */
381 FT_CALLBACK_TABLE_DEF
382 const FT_Driver_ClassRec tt_driver_class =
385 ft_module_font_driver |
386 ft_module_driver_scalable |
387 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
388 ft_module_driver_has_hinter,
393 sizeof ( TT_DriverRec ),
395 "truetype", /* driver name */
396 0x10000L, /* driver version == 1.0 */
397 0x20000L, /* driver requires FreeType 2.0 or above */
399 (void*)0, /* driver specific interface */
401 (FT_Module_Constructor)tt_driver_init,
402 (FT_Module_Destructor) tt_driver_done,
403 (FT_Module_Requester) tt_get_interface,
406 sizeof ( TT_FaceRec ),
407 sizeof ( TT_SizeRec ),
408 sizeof ( FT_GlyphSlotRec ),
411 (FT_Face_InitFunc) tt_face_init,
412 (FT_Face_DoneFunc) tt_face_done,
413 (FT_Size_InitFunc) tt_size_init,
414 (FT_Size_DoneFunc) tt_size_done,
415 (FT_Slot_InitFunc) 0,
416 (FT_Slot_DoneFunc) 0,
418 (FT_Size_ResetPointsFunc) Set_Char_Sizes,
419 (FT_Size_ResetPixelsFunc) Set_Pixel_Sizes,
420 (FT_Slot_LoadFunc) Load_Glyph,
422 (FT_Face_GetKerningFunc) Get_Kerning,
423 (FT_Face_AttachFunc) 0,
424 (FT_Face_GetAdvancesFunc) 0