1 /****************************************************************************
5 * TrueType font driver implementation (body).
7 * Copyright (C) 1996-2019 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.
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
40 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
47 /**************************************************************************
49 * The macro FT_COMPONENT is used in trace mode. It is an implicit
50 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
51 * messages during execution.
54 #define FT_COMPONENT ttdriver
62 tt_property_set( FT_Module module, /* TT_Driver */
63 const char* property_name,
65 FT_Bool value_is_string )
67 FT_Error error = FT_Err_Ok;
68 TT_Driver driver = (TT_Driver)module;
70 #ifndef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES
71 FT_UNUSED( value_is_string );
75 if ( !ft_strcmp( property_name, "interpreter-version" ) )
77 FT_UInt interpreter_version;
80 #ifdef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES
81 if ( value_is_string )
83 const char* s = (const char*)value;
86 interpreter_version = (FT_UInt)ft_strtol( s, NULL, 10 );
91 FT_UInt* iv = (FT_UInt*)value;
94 interpreter_version = *iv;
97 if ( interpreter_version == TT_INTERPRETER_VERSION_35
98 #ifdef TT_SUPPORT_SUBPIXEL_HINTING_INFINALITY
99 || interpreter_version == TT_INTERPRETER_VERSION_38
101 #ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
102 || interpreter_version == TT_INTERPRETER_VERSION_40
105 driver->interpreter_version = interpreter_version;
107 error = FT_ERR( Unimplemented_Feature );
112 FT_TRACE0(( "tt_property_set: missing property `%s'\n",
114 return FT_THROW( Missing_Property );
119 tt_property_get( FT_Module module, /* TT_Driver */
120 const char* property_name,
123 FT_Error error = FT_Err_Ok;
124 TT_Driver driver = (TT_Driver)module;
126 FT_UInt interpreter_version = driver->interpreter_version;
129 if ( !ft_strcmp( property_name, "interpreter-version" ) )
131 FT_UInt* val = (FT_UInt*)value;
134 *val = interpreter_version;
139 FT_TRACE0(( "tt_property_get: missing property `%s'\n",
141 return FT_THROW( Missing_Property );
145 FT_DEFINE_SERVICE_PROPERTIESREC(
146 tt_service_properties,
148 (FT_Properties_SetFunc)tt_property_set, /* set_property */
149 (FT_Properties_GetFunc)tt_property_get /* get_property */
153 /*************************************************************************/
154 /*************************************************************************/
155 /*************************************************************************/
158 /**** F A C E S ****/
161 /*************************************************************************/
162 /*************************************************************************/
163 /*************************************************************************/
166 /**************************************************************************
172 * A driver method used to return the kerning vector between two
173 * glyphs of the same face.
177 * A handle to the source face object.
180 * The index of the left glyph in the kern pair.
183 * The index of the right glyph in the kern pair.
187 * The kerning vector. This is in font units for
188 * scalable formats, and in pixels for fixed-sizes
192 * FreeType error code. 0 means success.
195 * Only horizontal layouts (left-to-right & right-to-left) are
196 * supported by this function. Other layouts, or more sophisticated
197 * kernings, are out of scope of this method (the basic driver
198 * interface is meant to be simple).
200 * They can be implemented by format-specific interfaces.
203 tt_get_kerning( FT_Face ttface, /* TT_Face */
208 TT_Face face = (TT_Face)ttface;
209 SFNT_Service sfnt = (SFNT_Service)face->sfnt;
216 kerning->x = sfnt->get_kerning( face, left_glyph, right_glyph );
223 tt_get_advances( FT_Face ttface,
230 TT_Face face = (TT_Face)ttface;
233 /* XXX: TODO: check for sbits */
235 if ( flags & FT_LOAD_VERTICAL_LAYOUT )
237 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
238 /* no fast retrieval for blended MM fonts without VVAR table */
239 if ( ( FT_IS_NAMED_INSTANCE( ttface ) || FT_IS_VARIATION( ttface ) ) &&
240 !( face->variation_support & TT_FACE_FLAG_VAR_VADVANCE ) )
241 return FT_THROW( Unimplemented_Feature );
244 for ( nn = 0; nn < count; nn++ )
250 /* since we don't need `tsb', we use zero for `yMax' parameter */
251 TT_Get_VMetrics( face, start + nn, 0, &tsb, &ah );
257 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
258 /* no fast retrieval for blended MM fonts without HVAR table */
259 if ( ( FT_IS_NAMED_INSTANCE( ttface ) || FT_IS_VARIATION( ttface ) ) &&
260 !( face->variation_support & TT_FACE_FLAG_VAR_HADVANCE ) )
261 return FT_THROW( Unimplemented_Feature );
264 for ( nn = 0; nn < count; nn++ )
270 TT_Get_HMetrics( face, start + nn, &lsb, &aw );
279 /*************************************************************************/
280 /*************************************************************************/
281 /*************************************************************************/
284 /**** S I Z E S ****/
287 /*************************************************************************/
288 /*************************************************************************/
289 /*************************************************************************/
292 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
295 tt_size_select( FT_Size size,
296 FT_ULong strike_index )
298 TT_Face ttface = (TT_Face)size->face;
299 TT_Size ttsize = (TT_Size)size;
300 FT_Error error = FT_Err_Ok;
303 ttsize->strike_index = strike_index;
305 if ( FT_IS_SCALABLE( size->face ) )
307 /* use the scaled metrics, even when tt_size_reset fails */
308 FT_Select_Metrics( size->face, strike_index );
310 tt_size_reset( ttsize, 0 ); /* ignore return value */
314 SFNT_Service sfnt = (SFNT_Service)ttface->sfnt;
315 FT_Size_Metrics* size_metrics = &size->metrics;
318 error = sfnt->load_strike_metrics( ttface,
322 ttsize->strike_index = 0xFFFFFFFFUL;
328 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
332 tt_size_request( FT_Size size,
333 FT_Size_Request req )
335 TT_Size ttsize = (TT_Size)size;
336 FT_Error error = FT_Err_Ok;
339 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
341 if ( FT_HAS_FIXED_SIZES( size->face ) )
343 TT_Face ttface = (TT_Face)size->face;
344 SFNT_Service sfnt = (SFNT_Service)ttface->sfnt;
345 FT_ULong strike_index;
348 error = sfnt->set_sbit_strike( ttface, req, &strike_index );
351 ttsize->strike_index = 0xFFFFFFFFUL;
353 return tt_size_select( size, strike_index );
356 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
358 FT_Request_Metrics( size->face, req );
360 if ( FT_IS_SCALABLE( size->face ) )
362 error = tt_size_reset( ttsize, 0 );
364 #ifdef TT_USE_BYTECODE_INTERPRETER
365 /* for the `MPS' bytecode instruction we need the point size */
369 ttsize->metrics->x_ppem > ttsize->metrics->y_ppem
370 ? req->horiResolution
371 : req->vertResolution;
374 /* if we don't have a resolution value, assume 72dpi */
375 if ( req->type == FT_SIZE_REQUEST_TYPE_SCALES ||
379 ttsize->point_size = FT_MulDiv( ttsize->ttmetrics.ppem,
390 /**************************************************************************
396 * A driver method used to load a glyph within a given glyph slot.
400 * A handle to the target slot object where the glyph
404 * A handle to the source face size at which the glyph
405 * must be scaled, loaded, etc.
408 * The index of the glyph in the font file.
411 * A flag indicating what to load for this glyph. The
412 * FT_LOAD_XXX constants can be used to control the
413 * glyph loading process (e.g., whether the outline
414 * should be scaled, whether to load bitmaps or not,
415 * whether to hint the outline, etc).
418 * FreeType error code. 0 means success.
421 tt_glyph_load( FT_GlyphSlot ttslot, /* TT_GlyphSlot */
422 FT_Size ttsize, /* TT_Size */
424 FT_Int32 load_flags )
426 TT_GlyphSlot slot = (TT_GlyphSlot)ttslot;
427 TT_Size size = (TT_Size)ttsize;
428 FT_Face face = ttslot->face;
433 return FT_THROW( Invalid_Slot_Handle );
436 return FT_THROW( Invalid_Size_Handle );
439 return FT_THROW( Invalid_Face_Handle );
441 #ifdef FT_CONFIG_OPTION_INCREMENTAL
442 if ( glyph_index >= (FT_UInt)face->num_glyphs &&
443 !face->internal->incremental_interface )
445 if ( glyph_index >= (FT_UInt)face->num_glyphs )
447 return FT_THROW( Invalid_Argument );
449 if ( load_flags & FT_LOAD_NO_HINTING )
451 /* both FT_LOAD_NO_HINTING and FT_LOAD_NO_AUTOHINT */
452 /* are necessary to disable hinting for tricky fonts */
454 if ( FT_IS_TRICKY( face ) )
455 load_flags &= ~FT_LOAD_NO_HINTING;
457 if ( load_flags & FT_LOAD_NO_AUTOHINT )
458 load_flags |= FT_LOAD_NO_HINTING;
461 if ( load_flags & ( FT_LOAD_NO_RECURSE | FT_LOAD_NO_SCALE ) )
463 load_flags |= FT_LOAD_NO_BITMAP | FT_LOAD_NO_SCALE;
465 if ( !FT_IS_TRICKY( face ) )
466 load_flags |= FT_LOAD_NO_HINTING;
469 /* use hinted metrics only if we load a glyph with hinting */
470 size->metrics = ( load_flags & FT_LOAD_NO_HINTING )
472 : &size->hinted_metrics;
474 /* now fill in the glyph slot with outline/bitmap/layered */
475 error = TT_Load_Glyph( size, slot, glyph_index, load_flags );
477 /* force drop-out mode to 2 - irrelevant now */
478 /* slot->outline.dropout_mode = 2; */
484 /*************************************************************************/
485 /*************************************************************************/
486 /*************************************************************************/
489 /**** D R I V E R I N T E R F A C E ****/
492 /*************************************************************************/
493 /*************************************************************************/
494 /*************************************************************************/
496 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
498 FT_DEFINE_SERVICE_MULTIMASTERSREC(
499 tt_service_gx_multi_masters,
501 (FT_Get_MM_Func) NULL, /* get_mm */
502 (FT_Set_MM_Design_Func) NULL, /* set_mm_design */
503 (FT_Set_MM_Blend_Func) TT_Set_MM_Blend, /* set_mm_blend */
504 (FT_Get_MM_Blend_Func) TT_Get_MM_Blend, /* get_mm_blend */
505 (FT_Get_MM_Var_Func) TT_Get_MM_Var, /* get_mm_var */
506 (FT_Set_Var_Design_Func) TT_Set_Var_Design, /* set_var_design */
507 (FT_Get_Var_Design_Func) TT_Get_Var_Design, /* get_var_design */
508 (FT_Set_Instance_Func) TT_Set_Named_Instance, /* set_instance */
509 (FT_Set_MM_WeightVector_Func)NULL, /* set_mm_weightvector */
510 (FT_Get_MM_WeightVector_Func)NULL, /* get_mm_weightvector */
512 (FT_Get_Var_Blend_Func) tt_get_var_blend, /* get_var_blend */
513 (FT_Done_Blend_Func) tt_done_blend /* done_blend */
516 FT_DEFINE_SERVICE_METRICSVARIATIONSREC(
517 tt_service_metrics_variations,
519 (FT_HAdvance_Adjust_Func)tt_hadvance_adjust, /* hadvance_adjust */
520 (FT_LSB_Adjust_Func) NULL, /* lsb_adjust */
521 (FT_RSB_Adjust_Func) NULL, /* rsb_adjust */
523 (FT_VAdvance_Adjust_Func)tt_vadvance_adjust, /* vadvance_adjust */
524 (FT_TSB_Adjust_Func) NULL, /* tsb_adjust */
525 (FT_BSB_Adjust_Func) NULL, /* bsb_adjust */
526 (FT_VOrg_Adjust_Func) NULL, /* vorg_adjust */
528 (FT_Metrics_Adjust_Func) tt_apply_mvar /* metrics_adjust */
531 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
534 static const FT_Service_TrueTypeEngineRec tt_service_truetype_engine =
536 #ifdef TT_USE_BYTECODE_INTERPRETER
538 FT_TRUETYPE_ENGINE_TYPE_PATENTED
540 #else /* !TT_USE_BYTECODE_INTERPRETER */
542 FT_TRUETYPE_ENGINE_TYPE_NONE
544 #endif /* TT_USE_BYTECODE_INTERPRETER */
548 FT_DEFINE_SERVICE_TTGLYFREC(
549 tt_service_truetype_glyf,
551 (TT_Glyf_GetLocationFunc)tt_face_get_location /* get_location */
555 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
556 FT_DEFINE_SERVICEDESCREC6(
559 FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_TRUETYPE,
560 FT_SERVICE_ID_MULTI_MASTERS, &tt_service_gx_multi_masters,
561 FT_SERVICE_ID_METRICS_VARIATIONS, &tt_service_metrics_variations,
562 FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine,
563 FT_SERVICE_ID_TT_GLYF, &tt_service_truetype_glyf,
564 FT_SERVICE_ID_PROPERTIES, &tt_service_properties )
566 FT_DEFINE_SERVICEDESCREC4(
569 FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_TRUETYPE,
570 FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine,
571 FT_SERVICE_ID_TT_GLYF, &tt_service_truetype_glyf,
572 FT_SERVICE_ID_PROPERTIES, &tt_service_properties )
576 FT_CALLBACK_DEF( FT_Module_Interface )
577 tt_get_interface( FT_Module driver, /* TT_Driver */
578 const char* tt_interface )
581 FT_Module_Interface result;
586 result = ft_service_list_lookup( tt_services, tt_interface );
592 library = driver->library;
596 /* only return the default interface from the SFNT module */
597 sfntd = FT_Get_Module( library, "sfnt" );
600 sfnt = (SFNT_Service)( sfntd->clazz->module_interface );
602 return sfnt->get_interface( driver, tt_interface );
609 /* The FT_DriverInterface structure is defined in ftdriver.h. */
611 #ifdef TT_USE_BYTECODE_INTERPRETER
612 #define TT_HINTER_FLAG FT_MODULE_DRIVER_HAS_HINTER
614 #define TT_HINTER_FLAG 0
617 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
618 #define TT_SIZE_SELECT tt_size_select
620 #define TT_SIZE_SELECT 0
626 FT_MODULE_FONT_DRIVER |
627 FT_MODULE_DRIVER_SCALABLE |
630 sizeof ( TT_DriverRec ),
632 "truetype", /* driver name */
633 0x10000L, /* driver version == 1.0 */
634 0x20000L, /* driver requires FreeType 2.0 or above */
636 NULL, /* module-specific interface */
638 tt_driver_init, /* FT_Module_Constructor module_init */
639 tt_driver_done, /* FT_Module_Destructor module_done */
640 tt_get_interface, /* FT_Module_Requester get_interface */
642 sizeof ( TT_FaceRec ),
643 sizeof ( TT_SizeRec ),
644 sizeof ( FT_GlyphSlotRec ),
646 tt_face_init, /* FT_Face_InitFunc init_face */
647 tt_face_done, /* FT_Face_DoneFunc done_face */
648 tt_size_init, /* FT_Size_InitFunc init_size */
649 tt_size_done, /* FT_Size_DoneFunc done_size */
650 tt_slot_init, /* FT_Slot_InitFunc init_slot */
651 NULL, /* FT_Slot_DoneFunc done_slot */
653 tt_glyph_load, /* FT_Slot_LoadFunc load_glyph */
655 tt_get_kerning, /* FT_Face_GetKerningFunc get_kerning */
656 NULL, /* FT_Face_AttachFunc attach_file */
657 tt_get_advances, /* FT_Face_GetAdvancesFunc get_advances */
659 tt_size_request, /* FT_Size_RequestFunc request_size */
660 TT_SIZE_SELECT /* FT_Size_SelectFunc select_size */