1 /****************************************************************************
5 * TrueType font driver implementation (body).
7 * Copyright (C) 1996-2020 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.
19 #include <freetype/internal/ftdebug.h>
20 #include <freetype/internal/ftstream.h>
21 #include <freetype/internal/sfnt.h>
22 #include <freetype/internal/services/svfntfmt.h>
24 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
25 #include <freetype/ftmm.h>
26 #include <freetype/internal/services/svmm.h>
27 #include <freetype/internal/services/svmetric.h>
30 #include <freetype/internal/services/svtteng.h>
31 #include <freetype/internal/services/svttglyf.h>
32 #include <freetype/internal/services/svprop.h>
33 #include <freetype/ftdriver.h>
39 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
46 /**************************************************************************
48 * The macro FT_COMPONENT is used in trace mode. It is an implicit
49 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
50 * messages during execution.
53 #define FT_COMPONENT ttdriver
61 tt_property_set( FT_Module module, /* TT_Driver */
62 const char* property_name,
64 FT_Bool value_is_string )
66 FT_Error error = FT_Err_Ok;
67 TT_Driver driver = (TT_Driver)module;
69 #ifndef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES
70 FT_UNUSED( value_is_string );
74 if ( !ft_strcmp( property_name, "interpreter-version" ) )
76 FT_UInt interpreter_version;
79 #ifdef FT_CONFIG_OPTION_ENVIRONMENT_PROPERTIES
80 if ( value_is_string )
82 const char* s = (const char*)value;
85 interpreter_version = (FT_UInt)ft_strtol( s, NULL, 10 );
90 FT_UInt* iv = (FT_UInt*)value;
93 interpreter_version = *iv;
96 if ( interpreter_version == TT_INTERPRETER_VERSION_35
97 #ifdef TT_SUPPORT_SUBPIXEL_HINTING_INFINALITY
98 || interpreter_version == TT_INTERPRETER_VERSION_38
100 #ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
101 || interpreter_version == TT_INTERPRETER_VERSION_40
104 driver->interpreter_version = interpreter_version;
106 error = FT_ERR( Unimplemented_Feature );
111 FT_TRACE0(( "tt_property_set: missing property `%s'\n",
113 return FT_THROW( Missing_Property );
118 tt_property_get( FT_Module module, /* TT_Driver */
119 const char* property_name,
122 FT_Error error = FT_Err_Ok;
123 TT_Driver driver = (TT_Driver)module;
125 FT_UInt interpreter_version = driver->interpreter_version;
128 if ( !ft_strcmp( property_name, "interpreter-version" ) )
130 FT_UInt* val = (FT_UInt*)value;
133 *val = interpreter_version;
138 FT_TRACE0(( "tt_property_get: missing property `%s'\n",
140 return FT_THROW( Missing_Property );
144 FT_DEFINE_SERVICE_PROPERTIESREC(
145 tt_service_properties,
147 (FT_Properties_SetFunc)tt_property_set, /* set_property */
148 (FT_Properties_GetFunc)tt_property_get /* get_property */
152 /*************************************************************************/
153 /*************************************************************************/
154 /*************************************************************************/
157 /**** F A C E S ****/
160 /*************************************************************************/
161 /*************************************************************************/
162 /*************************************************************************/
165 /**************************************************************************
171 * A driver method used to return the kerning vector between two
172 * glyphs of the same face.
176 * A handle to the source face object.
179 * The index of the left glyph in the kern pair.
182 * The index of the right glyph in the kern pair.
186 * The kerning vector. This is in font units for
187 * scalable formats, and in pixels for fixed-sizes
191 * FreeType error code. 0 means success.
194 * Only horizontal layouts (left-to-right & right-to-left) are
195 * supported by this function. Other layouts, or more sophisticated
196 * kernings, are out of scope of this method (the basic driver
197 * interface is meant to be simple).
199 * They can be implemented by format-specific interfaces.
202 tt_get_kerning( FT_Face ttface, /* TT_Face */
207 TT_Face face = (TT_Face)ttface;
208 SFNT_Service sfnt = (SFNT_Service)face->sfnt;
215 kerning->x = sfnt->get_kerning( face, left_glyph, right_glyph );
222 tt_get_advances( FT_Face ttface,
229 TT_Face face = (TT_Face)ttface;
232 /* XXX: TODO: check for sbits */
234 if ( flags & FT_LOAD_VERTICAL_LAYOUT )
236 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
237 /* no fast retrieval for blended MM fonts without VVAR table */
238 if ( ( FT_IS_NAMED_INSTANCE( ttface ) || FT_IS_VARIATION( ttface ) ) &&
239 !( face->variation_support & TT_FACE_FLAG_VAR_VADVANCE ) )
240 return FT_THROW( Unimplemented_Feature );
243 for ( nn = 0; nn < count; nn++ )
249 /* since we don't need `tsb', we use zero for `yMax' parameter */
250 TT_Get_VMetrics( face, start + nn, 0, &tsb, &ah );
256 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
257 /* no fast retrieval for blended MM fonts without HVAR table */
258 if ( ( FT_IS_NAMED_INSTANCE( ttface ) || FT_IS_VARIATION( ttface ) ) &&
259 !( face->variation_support & TT_FACE_FLAG_VAR_HADVANCE ) )
260 return FT_THROW( Unimplemented_Feature );
263 for ( nn = 0; nn < count; nn++ )
269 TT_Get_HMetrics( face, start + nn, &lsb, &aw );
278 /*************************************************************************/
279 /*************************************************************************/
280 /*************************************************************************/
283 /**** S I Z E S ****/
286 /*************************************************************************/
287 /*************************************************************************/
288 /*************************************************************************/
291 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
294 tt_size_select( FT_Size size,
295 FT_ULong strike_index )
297 TT_Face ttface = (TT_Face)size->face;
298 TT_Size ttsize = (TT_Size)size;
299 FT_Error error = FT_Err_Ok;
302 ttsize->strike_index = strike_index;
304 if ( FT_IS_SCALABLE( size->face ) )
306 /* use the scaled metrics, even when tt_size_reset fails */
307 FT_Select_Metrics( size->face, strike_index );
309 tt_size_reset( ttsize, 0 ); /* ignore return value */
313 SFNT_Service sfnt = (SFNT_Service)ttface->sfnt;
314 FT_Size_Metrics* size_metrics = &size->metrics;
317 error = sfnt->load_strike_metrics( ttface,
321 ttsize->strike_index = 0xFFFFFFFFUL;
327 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
331 tt_size_request( FT_Size size,
332 FT_Size_Request req )
334 TT_Size ttsize = (TT_Size)size;
335 FT_Error error = FT_Err_Ok;
338 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
340 if ( FT_HAS_FIXED_SIZES( size->face ) )
342 TT_Face ttface = (TT_Face)size->face;
343 SFNT_Service sfnt = (SFNT_Service)ttface->sfnt;
344 FT_ULong strike_index;
347 error = sfnt->set_sbit_strike( ttface, req, &strike_index );
350 ttsize->strike_index = 0xFFFFFFFFUL;
352 return tt_size_select( size, strike_index );
355 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
357 FT_Request_Metrics( size->face, req );
359 if ( FT_IS_SCALABLE( size->face ) )
361 error = tt_size_reset( ttsize, 0 );
363 #ifdef TT_USE_BYTECODE_INTERPRETER
364 /* for the `MPS' bytecode instruction we need the point size */
368 ttsize->metrics->x_ppem > ttsize->metrics->y_ppem
369 ? req->horiResolution
370 : req->vertResolution;
373 /* if we don't have a resolution value, assume 72dpi */
374 if ( req->type == FT_SIZE_REQUEST_TYPE_SCALES ||
378 ttsize->point_size = FT_MulDiv( ttsize->ttmetrics.ppem,
389 /**************************************************************************
395 * A driver method used to load a glyph within a given glyph slot.
399 * A handle to the target slot object where the glyph
403 * A handle to the source face size at which the glyph
404 * must be scaled, loaded, etc.
407 * The index of the glyph in the font file.
410 * A flag indicating what to load for this glyph. The
411 * FT_LOAD_XXX constants can be used to control the
412 * glyph loading process (e.g., whether the outline
413 * should be scaled, whether to load bitmaps or not,
414 * whether to hint the outline, etc).
417 * FreeType error code. 0 means success.
420 tt_glyph_load( FT_GlyphSlot ttslot, /* TT_GlyphSlot */
421 FT_Size ttsize, /* TT_Size */
423 FT_Int32 load_flags )
425 TT_GlyphSlot slot = (TT_GlyphSlot)ttslot;
426 TT_Size size = (TT_Size)ttsize;
427 FT_Face face = ttslot->face;
432 return FT_THROW( Invalid_Slot_Handle );
435 return FT_THROW( Invalid_Size_Handle );
438 return FT_THROW( Invalid_Face_Handle );
440 #ifdef FT_CONFIG_OPTION_INCREMENTAL
441 if ( glyph_index >= (FT_UInt)face->num_glyphs &&
442 !face->internal->incremental_interface )
444 if ( glyph_index >= (FT_UInt)face->num_glyphs )
446 return FT_THROW( Invalid_Argument );
448 if ( load_flags & FT_LOAD_NO_HINTING )
450 /* both FT_LOAD_NO_HINTING and FT_LOAD_NO_AUTOHINT */
451 /* are necessary to disable hinting for tricky fonts */
453 if ( FT_IS_TRICKY( face ) )
454 load_flags &= ~FT_LOAD_NO_HINTING;
456 if ( load_flags & FT_LOAD_NO_AUTOHINT )
457 load_flags |= FT_LOAD_NO_HINTING;
460 if ( load_flags & ( FT_LOAD_NO_RECURSE | FT_LOAD_NO_SCALE ) )
462 load_flags |= FT_LOAD_NO_BITMAP | FT_LOAD_NO_SCALE;
464 if ( !FT_IS_TRICKY( face ) )
465 load_flags |= FT_LOAD_NO_HINTING;
468 /* use hinted metrics only if we load a glyph with hinting */
469 size->metrics = ( load_flags & FT_LOAD_NO_HINTING )
471 : &size->hinted_metrics;
473 /* now fill in the glyph slot with outline/bitmap/layered */
474 error = TT_Load_Glyph( size, slot, glyph_index, load_flags );
476 /* force drop-out mode to 2 - irrelevant now */
477 /* slot->outline.dropout_mode = 2; */
483 /*************************************************************************/
484 /*************************************************************************/
485 /*************************************************************************/
488 /**** D R I V E R I N T E R F A C E ****/
491 /*************************************************************************/
492 /*************************************************************************/
493 /*************************************************************************/
495 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
497 FT_DEFINE_SERVICE_MULTIMASTERSREC(
498 tt_service_gx_multi_masters,
500 (FT_Get_MM_Func) NULL, /* get_mm */
501 (FT_Set_MM_Design_Func) NULL, /* set_mm_design */
502 (FT_Set_MM_Blend_Func) TT_Set_MM_Blend, /* set_mm_blend */
503 (FT_Get_MM_Blend_Func) TT_Get_MM_Blend, /* get_mm_blend */
504 (FT_Get_MM_Var_Func) TT_Get_MM_Var, /* get_mm_var */
505 (FT_Set_Var_Design_Func) TT_Set_Var_Design, /* set_var_design */
506 (FT_Get_Var_Design_Func) TT_Get_Var_Design, /* get_var_design */
507 (FT_Set_Instance_Func) TT_Set_Named_Instance, /* set_instance */
508 (FT_Set_MM_WeightVector_Func)NULL, /* set_mm_weightvector */
509 (FT_Get_MM_WeightVector_Func)NULL, /* get_mm_weightvector */
511 (FT_Get_Var_Blend_Func) tt_get_var_blend, /* get_var_blend */
512 (FT_Done_Blend_Func) tt_done_blend /* done_blend */
515 FT_DEFINE_SERVICE_METRICSVARIATIONSREC(
516 tt_service_metrics_variations,
518 (FT_HAdvance_Adjust_Func)tt_hadvance_adjust, /* hadvance_adjust */
519 (FT_LSB_Adjust_Func) NULL, /* lsb_adjust */
520 (FT_RSB_Adjust_Func) NULL, /* rsb_adjust */
522 (FT_VAdvance_Adjust_Func)tt_vadvance_adjust, /* vadvance_adjust */
523 (FT_TSB_Adjust_Func) NULL, /* tsb_adjust */
524 (FT_BSB_Adjust_Func) NULL, /* bsb_adjust */
525 (FT_VOrg_Adjust_Func) NULL, /* vorg_adjust */
527 (FT_Metrics_Adjust_Func) tt_apply_mvar /* metrics_adjust */
530 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
533 static const FT_Service_TrueTypeEngineRec tt_service_truetype_engine =
535 #ifdef TT_USE_BYTECODE_INTERPRETER
537 FT_TRUETYPE_ENGINE_TYPE_PATENTED
539 #else /* !TT_USE_BYTECODE_INTERPRETER */
541 FT_TRUETYPE_ENGINE_TYPE_NONE
543 #endif /* TT_USE_BYTECODE_INTERPRETER */
547 FT_DEFINE_SERVICE_TTGLYFREC(
548 tt_service_truetype_glyf,
550 (TT_Glyf_GetLocationFunc)tt_face_get_location /* get_location */
554 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
555 FT_DEFINE_SERVICEDESCREC6(
558 FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_TRUETYPE,
559 FT_SERVICE_ID_MULTI_MASTERS, &tt_service_gx_multi_masters,
560 FT_SERVICE_ID_METRICS_VARIATIONS, &tt_service_metrics_variations,
561 FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine,
562 FT_SERVICE_ID_TT_GLYF, &tt_service_truetype_glyf,
563 FT_SERVICE_ID_PROPERTIES, &tt_service_properties )
565 FT_DEFINE_SERVICEDESCREC4(
568 FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_TRUETYPE,
569 FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine,
570 FT_SERVICE_ID_TT_GLYF, &tt_service_truetype_glyf,
571 FT_SERVICE_ID_PROPERTIES, &tt_service_properties )
575 FT_CALLBACK_DEF( FT_Module_Interface )
576 tt_get_interface( FT_Module driver, /* TT_Driver */
577 const char* tt_interface )
580 FT_Module_Interface result;
585 result = ft_service_list_lookup( tt_services, tt_interface );
591 library = driver->library;
595 /* only return the default interface from the SFNT module */
596 sfntd = FT_Get_Module( library, "sfnt" );
599 sfnt = (SFNT_Service)( sfntd->clazz->module_interface );
601 return sfnt->get_interface( driver, tt_interface );
608 /* The FT_DriverInterface structure is defined in ftdriver.h. */
610 #ifdef TT_USE_BYTECODE_INTERPRETER
611 #define TT_HINTER_FLAG FT_MODULE_DRIVER_HAS_HINTER
613 #define TT_HINTER_FLAG 0
616 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
617 #define TT_SIZE_SELECT tt_size_select
619 #define TT_SIZE_SELECT 0
625 FT_MODULE_FONT_DRIVER |
626 FT_MODULE_DRIVER_SCALABLE |
629 sizeof ( TT_DriverRec ),
631 "truetype", /* driver name */
632 0x10000L, /* driver version == 1.0 */
633 0x20000L, /* driver requires FreeType 2.0 or above */
635 NULL, /* module-specific interface */
637 tt_driver_init, /* FT_Module_Constructor module_init */
638 tt_driver_done, /* FT_Module_Destructor module_done */
639 tt_get_interface, /* FT_Module_Requester get_interface */
641 sizeof ( TT_FaceRec ),
642 sizeof ( TT_SizeRec ),
643 sizeof ( FT_GlyphSlotRec ),
645 tt_face_init, /* FT_Face_InitFunc init_face */
646 tt_face_done, /* FT_Face_DoneFunc done_face */
647 tt_size_init, /* FT_Size_InitFunc init_size */
648 tt_size_done, /* FT_Size_DoneFunc done_size */
649 tt_slot_init, /* FT_Slot_InitFunc init_slot */
650 NULL, /* FT_Slot_DoneFunc done_slot */
652 tt_glyph_load, /* FT_Slot_LoadFunc load_glyph */
654 tt_get_kerning, /* FT_Face_GetKerningFunc get_kerning */
655 NULL, /* FT_Face_AttachFunc attach_file */
656 tt_get_advances, /* FT_Face_GetAdvancesFunc get_advances */
658 tt_size_request, /* FT_Size_RequestFunc request_size */
659 TT_SIZE_SELECT /* FT_Size_SelectFunc select_size */