1 /***************************************************************************/
5 /* TrueType font driver implementation (body). */
7 /* Copyright 1996-2016 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_SERVICE_FONT_FORMAT_H
25 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
26 #include FT_MULTIPLE_MASTERS_H
27 #include FT_SERVICE_MULTIPLE_MASTERS_H
28 #include FT_SERVICE_METRICS_VARIATIONS_H
31 #include FT_SERVICE_TRUETYPE_ENGINE_H
32 #include FT_SERVICE_TRUETYPE_GLYF_H
33 #include FT_SERVICE_PROPERTIES_H
34 #include FT_TRUETYPE_DRIVER_H
40 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
48 /*************************************************************************/
50 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
51 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
52 /* messages during execution. */
55 #define FT_COMPONENT trace_ttdriver
63 tt_property_set( FT_Module module
, /* TT_Driver */
64 const char* property_name
,
66 FT_Bool value_is_string
)
68 FT_Error error
= FT_Err_Ok
;
69 TT_Driver driver
= (TT_Driver
)module
;
71 #ifndef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES
72 FT_UNUSED( value_is_string
);
76 if ( !ft_strcmp( property_name
, "interpreter-version" ) )
78 FT_UInt interpreter_version
;
81 #ifdef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES
82 if ( value_is_string
)
84 const char* s
= (const char*)value
;
87 interpreter_version
= (FT_UInt
)ft_strtol( s
, NULL
, 10 );
92 FT_UInt
* iv
= (FT_UInt
*)value
;
95 interpreter_version
= *iv
;
98 if ( interpreter_version
== TT_INTERPRETER_VERSION_35
99 #ifdef TT_SUPPORT_SUBPIXEL_HINTING_INFINALITY
100 || interpreter_version
== TT_INTERPRETER_VERSION_38
102 #ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
103 || interpreter_version
== TT_INTERPRETER_VERSION_40
106 driver
->interpreter_version
= interpreter_version
;
108 error
= FT_ERR( Unimplemented_Feature
);
113 FT_TRACE0(( "tt_property_set: missing property `%s'\n",
115 return FT_THROW( Missing_Property
);
120 tt_property_get( FT_Module module
, /* TT_Driver */
121 const char* property_name
,
124 FT_Error error
= FT_Err_Ok
;
125 TT_Driver driver
= (TT_Driver
)module
;
127 FT_UInt interpreter_version
= driver
->interpreter_version
;
130 if ( !ft_strcmp( property_name
, "interpreter-version" ) )
132 FT_UInt
* val
= (FT_UInt
*)value
;
135 *val
= interpreter_version
;
140 FT_TRACE0(( "tt_property_get: missing property `%s'\n",
142 return FT_THROW( Missing_Property
);
146 FT_DEFINE_SERVICE_PROPERTIESREC(
147 tt_service_properties
,
149 (FT_Properties_SetFunc
)tt_property_set
, /* set_property */
150 (FT_Properties_GetFunc
)tt_property_get
/* get_property */
154 /*************************************************************************/
155 /*************************************************************************/
156 /*************************************************************************/
159 /**** F A C E S ****/
162 /*************************************************************************/
163 /*************************************************************************/
164 /*************************************************************************/
167 /*************************************************************************/
173 /* A driver method used to return the kerning vector between two */
174 /* glyphs of the same face. */
177 /* face :: A handle to the source face object. */
179 /* left_glyph :: The index of the left glyph in the kern pair. */
181 /* right_glyph :: The index of the right glyph in the kern pair. */
184 /* kerning :: The kerning vector. This is in font units for */
185 /* scalable formats, and in pixels for fixed-sizes */
189 /* FreeType error code. 0 means success. */
192 /* Only horizontal layouts (left-to-right & right-to-left) are */
193 /* supported by this function. Other layouts, or more sophisticated */
194 /* kernings, are out of scope of this method (the basic driver */
195 /* interface is meant to be simple). */
197 /* They can be implemented by format-specific interfaces. */
200 tt_get_kerning( FT_Face ttface
, /* TT_Face */
205 TT_Face face
= (TT_Face
)ttface
;
206 SFNT_Service sfnt
= (SFNT_Service
)face
->sfnt
;
213 kerning
->x
= sfnt
->get_kerning( face
, left_glyph
, right_glyph
);
220 tt_get_advances( FT_Face ttface
,
227 TT_Face face
= (TT_Face
)ttface
;
230 /* XXX: TODO: check for sbits */
232 if ( flags
& FT_LOAD_VERTICAL_LAYOUT
)
234 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
235 /* no fast retrieval for blended MM fonts without VVAR table */
236 if ( !face
->is_default_instance
&&
237 !( face
->variation_support
& TT_FACE_FLAG_VAR_VADVANCE
) )
238 return FT_THROW( Unimplemented_Feature
);
241 for ( nn
= 0; nn
< count
; nn
++ )
247 /* since we don't need `tsb', we use zero for `yMax' parameter */
248 TT_Get_VMetrics( face
, start
+ nn
, 0, &tsb
, &ah
);
254 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
255 /* no fast retrieval for blended MM fonts without HVAR table */
256 if ( !face
->is_default_instance
&&
257 !( face
->variation_support
& TT_FACE_FLAG_VAR_HADVANCE
) )
258 return FT_THROW( Unimplemented_Feature
);
261 for ( nn
= 0; nn
< count
; nn
++ )
267 TT_Get_HMetrics( face
, start
+ nn
, &lsb
, &aw
);
276 /*************************************************************************/
277 /*************************************************************************/
278 /*************************************************************************/
281 /**** S I Z E S ****/
284 /*************************************************************************/
285 /*************************************************************************/
286 /*************************************************************************/
289 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
292 tt_size_select( FT_Size size
,
293 FT_ULong strike_index
)
295 TT_Face ttface
= (TT_Face
)size
->face
;
296 TT_Size ttsize
= (TT_Size
)size
;
297 FT_Error error
= FT_Err_Ok
;
300 ttsize
->strike_index
= strike_index
;
302 if ( FT_IS_SCALABLE( size
->face
) )
304 /* use the scaled metrics, even when tt_size_reset fails */
305 FT_Select_Metrics( size
->face
, strike_index
);
307 tt_size_reset( ttsize
); /* ignore return value */
311 SFNT_Service sfnt
= (SFNT_Service
)ttface
->sfnt
;
312 FT_Size_Metrics
* metrics
= &size
->metrics
;
315 error
= sfnt
->load_strike_metrics( ttface
, strike_index
, metrics
);
317 ttsize
->strike_index
= 0xFFFFFFFFUL
;
323 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
327 tt_size_request( FT_Size size
,
328 FT_Size_Request req
)
330 TT_Size ttsize
= (TT_Size
)size
;
331 FT_Error error
= FT_Err_Ok
;
334 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
336 if ( FT_HAS_FIXED_SIZES( size
->face
) )
338 TT_Face ttface
= (TT_Face
)size
->face
;
339 SFNT_Service sfnt
= (SFNT_Service
)ttface
->sfnt
;
340 FT_ULong strike_index
;
343 error
= sfnt
->set_sbit_strike( ttface
, req
, &strike_index
);
346 ttsize
->strike_index
= 0xFFFFFFFFUL
;
348 return tt_size_select( size
, strike_index
);
351 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
353 FT_Request_Metrics( size
->face
, req
);
355 if ( FT_IS_SCALABLE( size
->face
) )
357 error
= tt_size_reset( ttsize
);
358 ttsize
->root
.metrics
= ttsize
->metrics
;
360 #ifdef TT_USE_BYTECODE_INTERPRETER
361 /* for the `MPS' bytecode instruction we need the point size */
363 FT_UInt resolution
= ttsize
->metrics
.x_ppem
> ttsize
->metrics
.y_ppem
364 ? req
->horiResolution
365 : req
->vertResolution
;
368 /* if we don't have a resolution value, assume 72dpi */
369 if ( req
->type
== FT_SIZE_REQUEST_TYPE_SCALES
||
373 ttsize
->point_size
= FT_MulDiv( ttsize
->ttmetrics
.ppem
,
384 /*************************************************************************/
390 /* A driver method used to load a glyph within a given glyph slot. */
393 /* slot :: A handle to the target slot object where the glyph */
394 /* will be loaded. */
396 /* size :: A handle to the source face size at which the glyph */
397 /* must be scaled, loaded, etc. */
399 /* glyph_index :: The index of the glyph in the font file. */
401 /* load_flags :: A flag indicating what to load for this glyph. The */
402 /* FT_LOAD_XXX constants can be used to control the */
403 /* glyph loading process (e.g., whether the outline */
404 /* should be scaled, whether to load bitmaps or not, */
405 /* whether to hint the outline, etc). */
408 /* FreeType error code. 0 means success. */
411 tt_glyph_load( FT_GlyphSlot ttslot
, /* TT_GlyphSlot */
412 FT_Size ttsize
, /* TT_Size */
414 FT_Int32 load_flags
)
416 TT_GlyphSlot slot
= (TT_GlyphSlot
)ttslot
;
417 TT_Size size
= (TT_Size
)ttsize
;
418 FT_Face face
= ttslot
->face
;
423 return FT_THROW( Invalid_Slot_Handle
);
426 return FT_THROW( Invalid_Size_Handle
);
429 return FT_THROW( Invalid_Face_Handle
);
431 #ifdef FT_CONFIG_OPTION_INCREMENTAL
432 if ( glyph_index
>= (FT_UInt
)face
->num_glyphs
&&
433 !face
->internal
->incremental_interface
)
435 if ( glyph_index
>= (FT_UInt
)face
->num_glyphs
)
437 return FT_THROW( Invalid_Argument
);
439 if ( load_flags
& FT_LOAD_NO_HINTING
)
441 /* both FT_LOAD_NO_HINTING and FT_LOAD_NO_AUTOHINT */
442 /* are necessary to disable hinting for tricky fonts */
444 if ( FT_IS_TRICKY( face
) )
445 load_flags
&= ~FT_LOAD_NO_HINTING
;
447 if ( load_flags
& FT_LOAD_NO_AUTOHINT
)
448 load_flags
|= FT_LOAD_NO_HINTING
;
451 if ( load_flags
& ( FT_LOAD_NO_RECURSE
| FT_LOAD_NO_SCALE
) )
453 load_flags
|= FT_LOAD_NO_BITMAP
| FT_LOAD_NO_SCALE
;
455 if ( !FT_IS_TRICKY( face
) )
456 load_flags
|= FT_LOAD_NO_HINTING
;
459 /* now load the glyph outline if necessary */
460 error
= TT_Load_Glyph( size
, slot
, glyph_index
, load_flags
);
462 /* force drop-out mode to 2 - irrelevant now */
463 /* slot->outline.dropout_mode = 2; */
469 /*************************************************************************/
470 /*************************************************************************/
471 /*************************************************************************/
474 /**** D R I V E R I N T E R F A C E ****/
477 /*************************************************************************/
478 /*************************************************************************/
479 /*************************************************************************/
481 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
483 FT_DEFINE_SERVICE_MULTIMASTERSREC(
484 tt_service_gx_multi_masters
,
486 (FT_Get_MM_Func
) NULL
, /* get_mm */
487 (FT_Set_MM_Design_Func
) NULL
, /* set_mm_design */
488 (FT_Set_MM_Blend_Func
) TT_Set_MM_Blend
, /* set_mm_blend */
489 (FT_Get_MM_Blend_Func
) TT_Get_MM_Blend
, /* get_mm_blend */
490 (FT_Get_MM_Var_Func
) TT_Get_MM_Var
, /* get_mm_var */
491 (FT_Set_Var_Design_Func
)TT_Set_Var_Design
, /* set_var_design */
492 (FT_Get_Var_Design_Func
)TT_Get_Var_Design
, /* get_var_design */
494 (FT_Get_Var_Blend_Func
) tt_get_var_blend
, /* get_var_blend */
495 (FT_Done_Blend_Func
) tt_done_blend
/* done_blend */
498 FT_DEFINE_SERVICE_METRICSVARIATIONSREC(
499 tt_service_metrics_variations
,
501 (FT_HAdvance_Adjust_Func
)tt_hadvance_adjust
, /* hadvance_adjust */
502 (FT_LSB_Adjust_Func
) NULL
, /* lsb_adjust */
503 (FT_RSB_Adjust_Func
) NULL
, /* rsb_adjust */
505 (FT_VAdvance_Adjust_Func
)NULL
, /* vadvance_adjust */
506 (FT_TSB_Adjust_Func
) NULL
, /* tsb_adjust */
507 (FT_BSB_Adjust_Func
) NULL
, /* bsb_adjust */
508 (FT_VOrg_Adjust_Func
) NULL
, /* vorg_adjust */
510 (FT_Metrics_Adjust_Func
) NULL
/* metrics_adjust */
513 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
516 static const FT_Service_TrueTypeEngineRec tt_service_truetype_engine
=
518 #ifdef TT_USE_BYTECODE_INTERPRETER
520 FT_TRUETYPE_ENGINE_TYPE_PATENTED
522 #else /* !TT_USE_BYTECODE_INTERPRETER */
524 FT_TRUETYPE_ENGINE_TYPE_NONE
526 #endif /* TT_USE_BYTECODE_INTERPRETER */
530 FT_DEFINE_SERVICE_TTGLYFREC(
531 tt_service_truetype_glyf
,
533 (TT_Glyf_GetLocationFunc
)tt_face_get_location
/* get_location */
537 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
538 FT_DEFINE_SERVICEDESCREC6(
541 FT_SERVICE_ID_FONT_FORMAT
, FT_FONT_FORMAT_TRUETYPE
,
542 FT_SERVICE_ID_MULTI_MASTERS
, &TT_SERVICE_GX_MULTI_MASTERS_GET
,
543 FT_SERVICE_ID_METRICS_VARIATIONS
, &TT_SERVICE_METRICS_VARIATIONS_GET
,
544 FT_SERVICE_ID_TRUETYPE_ENGINE
, &tt_service_truetype_engine
,
545 FT_SERVICE_ID_TT_GLYF
, &TT_SERVICE_TRUETYPE_GLYF_GET
,
546 FT_SERVICE_ID_PROPERTIES
, &TT_SERVICE_PROPERTIES_GET
)
548 FT_DEFINE_SERVICEDESCREC4(
551 FT_SERVICE_ID_FONT_FORMAT
, FT_FONT_FORMAT_TRUETYPE
,
552 FT_SERVICE_ID_TRUETYPE_ENGINE
, &tt_service_truetype_engine
,
553 FT_SERVICE_ID_TT_GLYF
, &TT_SERVICE_TRUETYPE_GLYF_GET
,
554 FT_SERVICE_ID_PROPERTIES
, &TT_SERVICE_PROPERTIES_GET
)
558 FT_CALLBACK_DEF( FT_Module_Interface
)
559 tt_get_interface( FT_Module driver
, /* TT_Driver */
560 const char* tt_interface
)
563 FT_Module_Interface result
;
568 /* TT_SERVICES_GET dereferences `library' in PIC mode */
569 #ifdef FT_CONFIG_OPTION_PIC
572 library
= driver
->library
;
577 result
= ft_service_list_lookup( TT_SERVICES_GET
, tt_interface
);
581 #ifndef FT_CONFIG_OPTION_PIC
584 library
= driver
->library
;
589 /* only return the default interface from the SFNT module */
590 sfntd
= FT_Get_Module( library
, "sfnt" );
593 sfnt
= (SFNT_Service
)( sfntd
->clazz
->module_interface
);
595 return sfnt
->get_interface( driver
, tt_interface
);
602 /* The FT_DriverInterface structure is defined in ftdriver.h. */
604 #ifdef TT_USE_BYTECODE_INTERPRETER
605 #define TT_HINTER_FLAG FT_MODULE_DRIVER_HAS_HINTER
607 #define TT_HINTER_FLAG 0
610 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
611 #define TT_SIZE_SELECT tt_size_select
613 #define TT_SIZE_SELECT 0
619 FT_MODULE_FONT_DRIVER
|
620 FT_MODULE_DRIVER_SCALABLE
|
623 sizeof ( TT_DriverRec
),
625 "truetype", /* driver name */
626 0x10000L
, /* driver version == 1.0 */
627 0x20000L
, /* driver requires FreeType 2.0 or above */
629 NULL
, /* module-specific interface */
631 tt_driver_init
, /* FT_Module_Constructor module_init */
632 tt_driver_done
, /* FT_Module_Destructor module_done */
633 tt_get_interface
, /* FT_Module_Requester get_interface */
635 sizeof ( TT_FaceRec
),
636 sizeof ( TT_SizeRec
),
637 sizeof ( FT_GlyphSlotRec
),
639 tt_face_init
, /* FT_Face_InitFunc init_face */
640 tt_face_done
, /* FT_Face_DoneFunc done_face */
641 tt_size_init
, /* FT_Size_InitFunc init_size */
642 tt_size_done
, /* FT_Size_DoneFunc done_size */
643 tt_slot_init
, /* FT_Slot_InitFunc init_slot */
644 NULL
, /* FT_Slot_DoneFunc done_slot */
646 tt_glyph_load
, /* FT_Slot_LoadFunc load_glyph */
648 tt_get_kerning
, /* FT_Face_GetKerningFunc get_kerning */
649 NULL
, /* FT_Face_AttachFunc attach_file */
650 tt_get_advances
, /* FT_Face_GetAdvancesFunc get_advances */
652 tt_size_request
, /* FT_Size_RequestFunc request_size */
653 TT_SIZE_SELECT
/* FT_Size_SelectFunc select_size */