Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

freetype: Update to 2.15.0

Rémi Verschelde302748c442c4
diff --git a/modules/freetype/SCsub b/modules/freetype/SCsub
index 1346d818eff..925f3e5d8e4 100644
--- a/modules/freetype/SCsub
+++ b/modules/freetype/SCsub
@@ -41,6 +41,7 @@ if env["builtin_freetype"]:
"src/cid/type1cid.c",
"src/gxvalid/gxvalid.c",
"src/gzip/ftgzip.c",
+ "src/hvf/hvf.c",
"src/lzw/ftlzw.c",
"src/otvalid/otvalid.c",
"src/pcf/pcf.c",
diff --git a/thirdparty/README.md b/thirdparty/README.md
index cbabe51a514..7af00e26a36 100644
--- a/thirdparty/README.md
+++ b/thirdparty/README.md
@@ -356,13 +356,13 @@ for UI.
## freetype
- Upstream: https://gitlab.freedesktop.org/freetype/freetype
-- Version: 2.14.3 (0a0221a1347e2f1e07c395263540026e9a0aa7c7, 2026)
+- Version: 2.15.0 (37515cf56ba3349656049edbed89ffc6f6466f2d, 2026)
- License: FreeType License (BSD-like)
Files extracted from upstream source:
- `src/` folder, minus the `dlg` and `tools` subfolders
- * These files can be removed: `.dat`, `.diff`, `.mk`, `.rc`, `README*`
+ * These files can be removed: `.cin`, `.dat`, `.diff`, `.hin`, `.mk`, `.rc`, `README*`
* In `src/gzip/`, keep only `ftgzip.c`
- `include/` folder, minus the `dlg` subfolder
- `LICENSE.TXT` and `docs/FTL.TXT`
diff --git a/thirdparty/freetype/include/freetype/config/ftmodule.h b/thirdparty/freetype/include/freetype/config/ftmodule.h
index b315baba8ae..028e52cc33c 100644
--- a/thirdparty/freetype/include/freetype/config/ftmodule.h
+++ b/thirdparty/freetype/include/freetype/config/ftmodule.h
@@ -15,6 +15,7 @@ FT_USE_MODULE( FT_Driver_ClassRec, tt_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, t1_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, cff_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, t1cid_driver_class )
+FT_USE_MODULE( FT_Driver_ClassRec, hvf_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, pfr_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, t42_driver_class )
FT_USE_MODULE( FT_Driver_ClassRec, winfnt_driver_class )
diff --git a/thirdparty/freetype/include/freetype/config/ftoption.h b/thirdparty/freetype/include/freetype/config/ftoption.h
index b857e0ebbd9..6d3d7dfaa57 100644
--- a/thirdparty/freetype/include/freetype/config/ftoption.h
+++ b/thirdparty/freetype/include/freetype/config/ftoption.h
@@ -334,6 +334,22 @@ FT_BEGIN_HEADER
/* #define FT_CONFIG_OPTION_USE_BROTLI */
+ /**************************************************************************
+ *
+ * HVF support.
+ *
+ * FreeType can use Apple's HVF (Hierarchical Variable Font) library
+ * to render glyphs from fonts containing 'hvgl' tables.
+ *
+ * Define this macro if you want to enable this 'feature'.
+ *
+ * If you use a build system like cmake or the `configure` script,
+ * options set by those programs have precedence, overwriting the value
+ * here with the configured one.
+ */
+/* #define FT_CONFIG_OPTION_HVF */
+
+
/**************************************************************************
*
* Glyph Postscript Names handling
@@ -423,8 +439,10 @@ FT_BEGIN_HEADER
/**************************************************************************
*
- * The size in bytes of the render pool used by the scan-line converter to
- * do all of its work.
+ * The size in bytes of the stack render pool used by the scan-line
+ * converters. Use this option to limit the stack usage. The memory
+ * requirements are proportional to size and complexity of a given glyph.
+ * FreeType's rasterizers switch to dynamic allocations when necessary.
*/
#define FT_RENDER_POOL_SIZE 16384L
@@ -651,6 +669,7 @@ FT_BEGIN_HEADER
#define TT_CONFIG_CMAP_FORMAT_12
#define TT_CONFIG_CMAP_FORMAT_13
#define TT_CONFIG_CMAP_FORMAT_14
+#define TT_CONFIG_CMAP_FORMAT_15
/*************************************************************************/
@@ -756,6 +775,21 @@ FT_BEGIN_HEADER
/* #define TT_CONFIG_OPTION_NO_BORING_EXPANSION */
+ /**************************************************************************
+ *
+ * Define `TT_CONFIG_OPTION_VARC` if you want to include support for
+ * variable composite glyphs (the 'VARC' table). This is part of the
+ * 'boring' OpenType specification expansions.
+ *
+ * https://github.com/harfbuzz/boring-expansion-spec/blob/main/VARC.md
+ *
+ * Variable composites enable more efficient encoding of variable glyphs
+ * by allowing glyphs to be composed from other glyphs with variable
+ * transformations and axis value overrides.
+ */
+#define TT_CONFIG_OPTION_VARC
+
+
/**************************************************************************
*
* Define `TT_CONFIG_OPTION_BDF` if you want to include support for an
diff --git a/thirdparty/freetype/include/freetype/freetype.h b/thirdparty/freetype/include/freetype/freetype.h
index 68a9e0c6bd2..0a330c7c3f6 100644
--- a/thirdparty/freetype/include/freetype/freetype.h
+++ b/thirdparty/freetype/include/freetype/freetype.h
@@ -4134,7 +4134,7 @@ FT_BEGIN_HEADER
* This function returns an error if the charmap is not part of the face
* (i.e., if it is not listed in the `face->charmaps` table).
*
- * It also fails if an OpenType type~14 charmap is selected (which
+ * It also fails if an OpenType type 14 or~15 charmap is selected (which
* doesn't map character codes to glyph indices at all).
*/
FT_EXPORT( FT_Error )
@@ -4700,7 +4700,7 @@ FT_BEGIN_HEADER
*
* @abstract:
* The FreeType~2 interface to Unicode Variation Sequences (UVS), using
- * the SFNT cmap format~14.
+ * the SFNT cmap formats 14 and~15.
*
* @description:
* Many characters, especially for CJK scripts, have variant forms. They
@@ -4730,9 +4730,10 @@ FT_BEGIN_HEADER
* U+9089, you have to write the character sequence `U+9089 U+E0100`.
*
* Adobe and MS decided to support both standardized and ideographic VS
- * with a new cmap subtable (format~14). It is an odd subtable because
- * it is not a mapping of input code points to glyphs, but contains lists
- * of all variations supported by the font.
+ * with a new cmap subtable (format~14, or format~15 for glyph indices
+ * above 65535). It is an odd subtable because it is not a mapping of
+ * input code points to glyphs, but contains lists of all variations
+ * supported by the font.
*
* A variation may be either 'default' or 'non-default' for a given font.
* A default variation is the one you will get for that code point if you
@@ -5168,8 +5169,8 @@ FT_BEGIN_HEADER
*
*/
#define FREETYPE_MAJOR 2
-#define FREETYPE_MINOR 14
-#define FREETYPE_PATCH 3
+#define FREETYPE_MINOR 15
+#define FREETYPE_PATCH 0
/**************************************************************************
diff --git a/thirdparty/freetype/include/freetype/ftimage.h b/thirdparty/freetype/include/freetype/ftimage.h
index 1fff76b20d9..61de54e48cb 100644
--- a/thirdparty/freetype/include/freetype/ftimage.h
+++ b/thirdparty/freetype/include/freetype/ftimage.h
@@ -471,7 +471,7 @@ FT_BEGIN_HEADER
/* */
-#define FT_CURVE_TAG( flag ) ( flag & 0x03 )
+#define FT_CURVE_TAG( flag ) ( ( flag & 0x01 ) ? 0x01 : ( flag & 0x03 ) )
/* see the `tags` field in `FT_Outline` for a description of the values */
#define FT_CURVE_TAG_ON 0x01
@@ -875,7 +875,7 @@ FT_BEGIN_HEADER
*/
typedef struct FT_Span_
{
- unsigned short x;
+ short x;
unsigned short len;
unsigned char coverage;
diff --git a/thirdparty/freetype/include/freetype/ftmoderr.h b/thirdparty/freetype/include/freetype/ftmoderr.h
index 7c1b1fd3c04..9010c9fc489 100644
--- a/thirdparty/freetype/include/freetype/ftmoderr.h
+++ b/thirdparty/freetype/include/freetype/ftmoderr.h
@@ -172,6 +172,7 @@
FT_MODERRDEF( Winfonts, 0x1500, "Windows FON/FNT module" )
FT_MODERRDEF( GXvalid, 0x1600, "GX validation module" )
FT_MODERRDEF( Sdf, 0x1700, "Signed distance field raster module" )
+ FT_MODERRDEF( HVF, 0x1800, "HVF module" )
#ifdef FT_MODERR_END_LIST
diff --git a/thirdparty/freetype/include/freetype/fttypes.h b/thirdparty/freetype/include/freetype/fttypes.h
index 97ddcde65aa..2f3ca05955a 100644
--- a/thirdparty/freetype/include/freetype/fttypes.h
+++ b/thirdparty/freetype/include/freetype/fttypes.h
@@ -25,8 +25,6 @@
#include <freetype/ftsystem.h>
#include <freetype/ftimage.h>
-#include <stddef.h>
-
FT_BEGIN_HEADER
diff --git a/thirdparty/freetype/include/freetype/internal/ftcalc.h b/thirdparty/freetype/include/freetype/internal/ftcalc.h
index 4c4943164a3..129366f3894 100644
--- a/thirdparty/freetype/include/freetype/internal/ftcalc.h
+++ b/thirdparty/freetype/include/freetype/internal/ftcalc.h
@@ -77,6 +77,10 @@ FT_BEGIN_HEADER
#endif /* FT_INT64 */
+#define ULABS( a ) ( a >= 0 ? (FT_ULong)a : 0UL - (FT_ULong)a )
+#define UABS( a ) ( a >= 0 ? (FT_UInt)a : 0U - (FT_UInt)a )
+
+
/**************************************************************************
*
* FT_MulDiv() and FT_MulFix() are declared in freetype.h.
diff --git a/thirdparty/freetype/include/freetype/internal/ftdebug.h b/thirdparty/freetype/include/freetype/internal/ftdebug.h
index 62b9ec2f79c..1baf5f385a8 100644
--- a/thirdparty/freetype/include/freetype/internal/ftdebug.h
+++ b/thirdparty/freetype/include/freetype/internal/ftdebug.h
@@ -33,6 +33,7 @@
#ifdef FT_DEBUG_LOGGING
#define DLG_STATIC
+#define DLG_LOG_LEVEL dlg_level_trace
#include <dlg/output.h>
#include <dlg/dlg.h>
diff --git a/thirdparty/freetype/include/freetype/internal/ftmmtypes.h b/thirdparty/freetype/include/freetype/internal/ftmmtypes.h
index a4db9c487b7..854798ad3c0 100644
--- a/thirdparty/freetype/include/freetype/internal/ftmmtypes.h
+++ b/thirdparty/freetype/include/freetype/internal/ftmmtypes.h
@@ -71,6 +71,13 @@ FT_BEGIN_HEADER
FT_UInt regionCount; /* total number of regions defined */
GX_VarRegion varRegionList;
+ /* Cache of per-region scalars for the current normalized coordinates. */
+ /* `regionScalars` holds `regionCount` values, `cachedCoords` the */
+ /* `axisCount` coordinates they were computed for; both are NULL until */
+ /* first use and recomputed whenever the coordinates change. */
+ FT_Fixed* regionScalars;
+ FT_Fixed* cachedCoords;
+
} GX_ItemVarStoreRec, *GX_ItemVarStore;
diff --git a/thirdparty/freetype/include/freetype/internal/ftobjs.h b/thirdparty/freetype/include/freetype/internal/ftobjs.h
index 4fa4991dd88..f97262ffa95 100644
--- a/thirdparty/freetype/include/freetype/internal/ftobjs.h
+++ b/thirdparty/freetype/include/freetype/internal/ftobjs.h
@@ -214,8 +214,8 @@ FT_BEGIN_HEADER
FT_CMap_CharIndexFunc char_index;
FT_CMap_CharNextFunc char_next;
- /* Subsequent entries are special ones for format 14 -- the variant */
- /* selector subtable which behaves like no other */
+ /* Subsequent entries are special ones for formats 14 and 15 -- */
+ /* the variant selector subtables which behave like no other */
FT_CMap_CharVarIndexFunc char_var_index;
FT_CMap_CharVarIsDefaultFunc char_var_default;
diff --git a/thirdparty/freetype/include/freetype/internal/ftserv.h b/thirdparty/freetype/include/freetype/internal/ftserv.h
index 9c6406c82d8..3b326627074 100644
--- a/thirdparty/freetype/include/freetype/internal/ftserv.h
+++ b/thirdparty/freetype/include/freetype/internal/ftserv.h
@@ -173,6 +173,7 @@ FT_BEGIN_HEADER
* FT_DEFINE_SERVICEDESCREC8
* FT_DEFINE_SERVICEDESCREC9
* FT_DEFINE_SERVICEDESCREC10
+ * FT_DEFINE_SERVICEDESCREC11
*
* @description:
* Used to initialize an array of FT_ServiceDescRec structures.
@@ -350,6 +351,34 @@ FT_BEGIN_HEADER
{ NULL, NULL } \
};
+#define FT_DEFINE_SERVICEDESCREC11( class_, \
+ serv_id_1, serv_data_1, \
+ serv_id_2, serv_data_2, \
+ serv_id_3, serv_data_3, \
+ serv_id_4, serv_data_4, \
+ serv_id_5, serv_data_5, \
+ serv_id_6, serv_data_6, \
+ serv_id_7, serv_data_7, \
+ serv_id_8, serv_data_8, \
+ serv_id_9, serv_data_9, \
+ serv_id_10, serv_data_10, \
+ serv_id_11, serv_data_11 ) \
+ static const FT_ServiceDescRec class_[] = \
+ { \
+ { serv_id_1, serv_data_1 }, \
+ { serv_id_2, serv_data_2 }, \
+ { serv_id_3, serv_data_3 }, \
+ { serv_id_4, serv_data_4 }, \
+ { serv_id_5, serv_data_5 }, \
+ { serv_id_6, serv_data_6 }, \
+ { serv_id_7, serv_data_7 }, \
+ { serv_id_8, serv_data_8 }, \
+ { serv_id_9, serv_data_9 }, \
+ { serv_id_10, serv_data_10 }, \
+ { serv_id_11, serv_data_11 }, \
+ { NULL, NULL } \
+ };
+
/*
* Parse a list of FT_ServiceDescRec descriptors and look for a specific
diff --git a/thirdparty/freetype/include/freetype/internal/fttrace.h b/thirdparty/freetype/include/freetype/internal/fttrace.h
index 798896cd0c8..22291ef7cfe 100644
--- a/thirdparty/freetype/include/freetype/internal/fttrace.h
+++ b/thirdparty/freetype/include/freetype/internal/fttrace.h
@@ -79,6 +79,7 @@ FT_TRACE_DEF( ttgxvar ) /* TrueType GX var handler (ttgxvar.c) */
FT_TRACE_DEF( ttinterp ) /* bytecode interpreter (ttinterp.c) */
FT_TRACE_DEF( ttobjs ) /* TT objects manager (ttobjs.c) */
FT_TRACE_DEF( ttpload ) /* TT data/program loader (ttpload.c) */
+FT_TRACE_DEF( ttvarc ) /* TT VARC handler (ttvarc.c) */
/* Type 1 driver components */
FT_TRACE_DEF( t1afm )
@@ -120,6 +121,11 @@ FT_TRACE_DEF( cidload )
FT_TRACE_DEF( cidobjs )
FT_TRACE_DEF( cidparse )
+ /* HVF driver component */
+FT_TRACE_DEF( hvfdrv )
+FT_TRACE_DEF( hvfobjs )
+FT_TRACE_DEF( hvfload )
+
/* Windows font component */
FT_TRACE_DEF( winfnt )
diff --git a/thirdparty/freetype/include/freetype/internal/services/svfntfmt.h b/thirdparty/freetype/include/freetype/internal/services/svfntfmt.h
index 770381da67a..91a6f007aa5 100644
--- a/thirdparty/freetype/include/freetype/internal/services/svfntfmt.h
+++ b/thirdparty/freetype/include/freetype/internal/services/svfntfmt.h
@@ -40,6 +40,7 @@ FT_BEGIN_HEADER
#define FT_FONT_FORMAT_TYPE_42 "Type 42"
#define FT_FONT_FORMAT_CID "CID Type 1"
#define FT_FONT_FORMAT_CFF "CFF"
+#define FT_FONT_FORMAT_HVF "HVF"
#define FT_FONT_FORMAT_PFR "PFR"
#define FT_FONT_FORMAT_WINFNT "Windows FNT"
diff --git a/thirdparty/freetype/include/freetype/internal/services/svttcmap.h b/thirdparty/freetype/include/freetype/internal/services/svttcmap.h
index 14921304ebf..d7ad8066b5e 100644
--- a/thirdparty/freetype/include/freetype/internal/services/svttcmap.h
+++ b/thirdparty/freetype/include/freetype/internal/services/svttcmap.h
@@ -52,7 +52,8 @@ FT_BEGIN_HEADER
* table), 2~(high-byte mapping through table), 4~(segment mapping to
* delta values), 6~(trimmed table mapping), 8~(mixed 16-bit and 32-bit
* coverage), 10~(trimmed array), 12~(segmented coverage), 13~(last
- * resort font), and 14 (Unicode Variation Sequences).
+ * resort font), 14~(Unicode Variation Sequences), and 15~(extended
+ * glyph repertoire Unicode Variation Sequences).
*/
typedef struct TT_CMapInfo_
{
diff --git a/thirdparty/freetype/include/freetype/internal/services/svvarc.h b/thirdparty/freetype/include/freetype/internal/services/svvarc.h
new file mode 100644
index 00000000000..753340904d6
--- /dev/null
+++ b/thirdparty/freetype/include/freetype/internal/services/svvarc.h
@@ -0,0 +1,87 @@
+/****************************************************************************
+ *
+ * svvarc.h
+ *
+ * The FreeType VARC service (specification).
+ *
+ * Copyright (C) 2026 by
+ * David Turner, Robert Wilhelm, Werner Lemberg, and Behdad Esfahbod.
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+
+#ifndef SVVARC_H_
+#define SVVARC_H_
+
+
+#include <freetype/internal/ftserv.h>
+
+
+FT_BEGIN_HEADER
+
+
+#define FT_SERVICE_ID_VARC "varc"
+
+
+ typedef FT_Error
+ (*FT_VARC_Init_Axes_Func)( FT_Face face,
+ FT_UInt axis_count );
+
+ typedef FT_Error
+ (*FT_VARC_Load_Func)( FT_Face face,
+ FT_Stream stream );
+
+ typedef void
+ (*FT_VARC_Done_Func)( FT_Face face );
+
+ typedef FT_Bool
+ (*FT_VARC_Has_Glyph_Func)( FT_Face face,
+ FT_UInt glyph_index );
+
+ typedef FT_Error
+ (*FT_VARC_Load_Glyph_Func)( FT_Face face,
+ FT_GlyphSlot glyph_slot,
+ FT_UInt glyph_index,
+ FT_Int32 load_flags );
+
+
+ FT_DEFINE_SERVICE( VARC )
+ {
+ FT_VARC_Init_Axes_Func init_axes;
+ FT_VARC_Load_Func load;
+ FT_VARC_Done_Func done;
+ FT_VARC_Has_Glyph_Func has_glyph;
+ FT_VARC_Load_Glyph_Func load_glyph;
+ };
+
+
+#define FT_DEFINE_SERVICE_VARCREC( class_, \
+ init_axes_, \
+ load_, \
+ done_, \
+ has_glyph_, \
+ load_glyph_ ) \
+ static const FT_Service_VARCRec class_ = \
+ { \
+ init_axes_, \
+ load_, \
+ done_, \
+ has_glyph_, \
+ load_glyph_ \
+ };
+
+ /* */
+
+
+FT_END_HEADER
+
+#endif /* SVVARC_H_ */
+
+
+/* END */
diff --git a/thirdparty/freetype/include/freetype/internal/tttypes.h b/thirdparty/freetype/include/freetype/internal/tttypes.h
index 65341a3a259..ad45da39b99 100644
--- a/thirdparty/freetype/include/freetype/internal/tttypes.h
+++ b/thirdparty/freetype/include/freetype/internal/tttypes.h
@@ -1130,11 +1130,29 @@ FT_BEGIN_HEADER
* The font's horizontal header ('hhea' table). This field also
* contains the associated horizontal metrics table ('hmtx').
*
+ * horz_metrics_count ::
+ * The number of horizontal metrics. This can be larger than the
+ * public `TT_HoriHeader.number_Of_HMetrics` field when the font uses
+ * the 'HHEA table (instead of 'hhea').
+ *
+ * horz_metrics_is_extended ::
+ * A boolean which is set when the font uses 'HHEA' and 'HMTX' tables
+ * (instead of 'hhea' and 'hmtx').
+ *
* max_profile ::
* The font's maximum profile table. Read on font opening. Note that
* some maximum values cannot be taken directly from this table. We
* thus define additional fields below to hold the computed maxima.
*
+ * maxp_num_glyphs ::
+ * The glyph count from the font's maximum profile table. This can be
+ * larger than the public `TT_MaxProfile.numGlyphs` field when the font
+ * uses the 24-bit 'MAXP' table.
+ *
+ * is_extended_glyf ::
+ * A boolean which is set when the font uses the 'GLYF' and 'LOCA'
+ * tables (extending 'glyf' and 'loca', respectively).
+ *
* vertical_info ::
* A boolean which is set when the font file contains vertical metrics.
* If not, the value of the 'vertical' field is undefined.
@@ -1145,6 +1163,15 @@ FT_BEGIN_HEADER
* IMPORTANT: The contents of this field is undefined if the
* `vertical_info` field is unset.
*
+ * vert_metrics_count ::
+ * The number of vertical metrics. This can be larger than the public
+ * `TT_VertHeader.number_Of_VMetrics` field when the font uses the
+ * 'VHEA' table (instead of 'vhea').
+ *
+ * vert_metrics_is_extended ::
+ * A boolean which is set when the font uses the 'VHEA' and 'VMTX'
+ * tables (instead of 'vhea' and 'vmtx').
+ *
* num_names ::
* The number of name records within this TrueType font.
*
@@ -1166,6 +1193,25 @@ FT_BEGIN_HEADER
* cmap_size ::
* The size in bytes of the `cmap_table` described above.
*
+ * dmap_table ::
+ * Address of the face's 'DMAP' SFNT table in memory (it's an extracted
+ * frame).
+ *
+ * dmap_size ::
+ * The size in bytes of the `dmap_table` described above.
+ *
+ * dmap_charmaps ::
+ * The array of 'DMAP' charmaps.
+ *
+ * num_dmap_charmaps ::
+ * The number of elements in `dmap_charmaps`.
+ *
+ * dmap_results ::
+ * An array used while merging a dmap with its corresponding cmap.
+ *
+ * dmap_max_results ::
+ * The current size of `dmap_results`.
+ *
* goto_table ::
* A function called by each TrueType table loader to position a
* stream's cursor to the start of a given table according to its tag.
@@ -1209,6 +1255,13 @@ FT_BEGIN_HEADER
* A pointer to the Metrics Variations service for this `TT_Face`'s
* driver.
*
+ * tt_varc ::
+ * A pointer for sharing the VARC services provided by this
+ * `TT_Face`'s driver.
+ *
+ * face_varc ::
+ * A pointer to handle VARC glyphs by this `TT_Face`'s driver.
+ *
* psaux ::
* A pointer to the PostScript Auxiliary service.
*
@@ -1408,6 +1461,26 @@ FT_BEGIN_HEADER
* svg ::
* A pointer to data related to the 'SVG' table. `NULL` if the table
* is not available.
+ *
+ * gpos_table ::
+ * A pointer to the 'GPOS' table. `NULL` if the table is not
+ * available.
+ *
+ * gpos_lookups_kerning ::
+ * An array of GPOS kerning lookups.
+ *
+ * num_gpos_lookups_kerning ::
+ * The number of GPOS kerning lookups.
+ *
+ * varc ::
+ * A pointer to data related to the 'VARC' table. `NULL` if the table
+ * is not available.
+ *
+ * varc_context ::
+ * A context to manage recursion in the 'VARC' table.
+ *
+ * varc_loading_components ::
+ * Set if component checks for 'VARC' table handling can be skipped.
*/
typedef struct TT_FaceRec_
{
@@ -1421,11 +1494,17 @@ FT_BEGIN_HEADER
TT_Header header; /* TrueType header table */
TT_HoriHeader horizontal; /* TrueType horizontal header */
+ FT_UInt32 horz_metrics_count;
+ FT_Bool horz_metrics_is_extended;
TT_MaxProfile max_profile;
+ FT_UInt maxp_num_glyphs;
+ FT_Bool is_extended_glyf;
FT_Bool vertical_info;
TT_VertHeader vertical; /* TT Vertical header, if present */
+ FT_UInt32 vert_metrics_count;
+ FT_Bool vert_metrics_is_extended;
FT_UShort num_names; /* number of name records */
TT_NameTableRec name_table; /* name table */
@@ -1436,6 +1515,13 @@ FT_BEGIN_HEADER
FT_Byte* cmap_table; /* extracted `cmap' table */
FT_ULong cmap_size;
+ FT_Byte* dmap_table; /* extracted 'DMAP' table */
+ FT_ULong dmap_size;
+ FT_CMap* dmap_charmaps;
+ FT_UInt num_dmap_charmaps;
+ FT_UInt32* dmap_results;
+ FT_UInt32 dmap_max_results;
+
TT_Loader_GotoTableFunc goto_table;
TT_Loader_StartGlyphFunc access_glyph_frame;
@@ -1468,6 +1554,16 @@ FT_BEGIN_HEADER
void* face_var; /* since 2.13.1 */
#endif
+#ifdef TT_CONFIG_OPTION_VARC
+ /* a typeless pointer to the FT_Service_VARCRec table used to */
+ /* share VARC handling from the `truetype' driver */
+ void* tt_varc; /* since 2.15 */
+
+ /* a typeless pointer to the FT_Service_VARCRec table used to */
+ /* handle VARC glyphs by this TT_Face's driver */
+ void* face_varc; /* since 2.15 */
+#endif
+
/* a typeless pointer to the PostScript Aux service */
void* psaux;
@@ -1553,7 +1649,7 @@ FT_BEGIN_HEADER
FT_ULong horz_metrics_size;
FT_ULong vert_metrics_size;
- FT_ULong num_locations; /* up to 0xFFFF + 1 */
+ FT_ULong num_locations; /* up to 0xFFFFFF + 1 */
FT_Byte* glyph_locations;
FT_Byte* hdmx_table;
@@ -1604,6 +1700,15 @@ FT_BEGIN_HEADER
FT_UInt num_gpos_lookups_kerning;
#endif
+#ifdef TT_CONFIG_OPTION_VARC
+ /* since 2.15 */
+ void* varc;
+ /* Active recursion context. */
+ void* varc_context;
+ /* Whether to skip VARC check for components. */
+ FT_Bool varc_loading_components;
+#endif
+
} TT_FaceRec;
diff --git a/thirdparty/freetype/include/freetype/tttags.h b/thirdparty/freetype/include/freetype/tttags.h
index 3be3e2ff905..7d431cea490 100644
--- a/thirdparty/freetype/include/freetype/tttags.h
+++ b/thirdparty/freetype/include/freetype/tttags.h
@@ -49,6 +49,7 @@ FT_BEGIN_HEADER
#define TTAG_CPAL FT_MAKE_TAG( 'C', 'P', 'A', 'L' )
#define TTAG_cvar FT_MAKE_TAG( 'c', 'v', 'a', 'r' )
#define TTAG_cvt FT_MAKE_TAG( 'c', 'v', 't', ' ' )
+#define TTAG_DMAP FT_MAKE_TAG( 'D', 'M', 'A', 'P' )
#define TTAG_DSIG FT_MAKE_TAG( 'D', 'S', 'I', 'G' )
#define TTAG_EBDT FT_MAKE_TAG( 'E', 'B', 'D', 'T' )
#define TTAG_EBLC FT_MAKE_TAG( 'E', 'B', 'L', 'C' )
@@ -60,23 +61,31 @@ FT_BEGIN_HEADER
#define TTAG_gasp FT_MAKE_TAG( 'g', 'a', 's', 'p' )
#define TTAG_GDEF FT_MAKE_TAG( 'G', 'D', 'E', 'F' )
#define TTAG_glyf FT_MAKE_TAG( 'g', 'l', 'y', 'f' )
+#define TTAG_GLYF FT_MAKE_TAG( 'G', 'L', 'Y', 'F' )
#define TTAG_GPOS FT_MAKE_TAG( 'G', 'P', 'O', 'S' )
#define TTAG_GSUB FT_MAKE_TAG( 'G', 'S', 'U', 'B' )
#define TTAG_gvar FT_MAKE_TAG( 'g', 'v', 'a', 'r' )
+#define TTAG_GVAR FT_MAKE_TAG( 'G', 'V', 'A', 'R' )
#define TTAG_HVAR FT_MAKE_TAG( 'H', 'V', 'A', 'R' )
#define TTAG_hdmx FT_MAKE_TAG( 'h', 'd', 'm', 'x' )
#define TTAG_head FT_MAKE_TAG( 'h', 'e', 'a', 'd' )
#define TTAG_hhea FT_MAKE_TAG( 'h', 'h', 'e', 'a' )
+#define TTAG_HHEA FT_MAKE_TAG( 'H', 'H', 'E', 'A' )
#define TTAG_hmtx FT_MAKE_TAG( 'h', 'm', 't', 'x' )
+#define TTAG_HMTX FT_MAKE_TAG( 'H', 'M', 'T', 'X' )
+#define TTAG_hvgl FT_MAKE_TAG( 'h', 'v', 'g', 'l' )
+#define TTAG_hvpm FT_MAKE_TAG( 'h', 'v', 'p', 'm' )
#define TTAG_JSTF FT_MAKE_TAG( 'J', 'S', 'T', 'F' )
#define TTAG_just FT_MAKE_TAG( 'j', 'u', 's', 't' )
#define TTAG_kern FT_MAKE_TAG( 'k', 'e', 'r', 'n' )
#define TTAG_lcar FT_MAKE_TAG( 'l', 'c', 'a', 'r' )
#define TTAG_loca FT_MAKE_TAG( 'l', 'o', 'c', 'a' )
+#define TTAG_LOCA FT_MAKE_TAG( 'L', 'O', 'C', 'A' )
#define TTAG_LTSH FT_MAKE_TAG( 'L', 'T', 'S', 'H' )
#define TTAG_LWFN FT_MAKE_TAG( 'L', 'W', 'F', 'N' )
#define TTAG_MATH FT_MAKE_TAG( 'M', 'A', 'T', 'H' )
#define TTAG_maxp FT_MAKE_TAG( 'm', 'a', 'x', 'p' )
+#define TTAG_MAXP FT_MAKE_TAG( 'M', 'A', 'X', 'P' )
#define TTAG_META FT_MAKE_TAG( 'M', 'E', 'T', 'A' )
#define TTAG_MMFX FT_MAKE_TAG( 'M', 'M', 'F', 'X' )
#define TTAG_MMSD FT_MAKE_TAG( 'M', 'M', 'S', 'D' )
@@ -104,7 +113,10 @@ FT_BEGIN_HEADER
#define TTAG_typ1 FT_MAKE_TAG( 't', 'y', 'p', '1' )
#define TTAG_VDMX FT_MAKE_TAG( 'V', 'D', 'M', 'X' )
#define TTAG_vhea FT_MAKE_TAG( 'v', 'h', 'e', 'a' )
+#define TTAG_VHEA FT_MAKE_TAG( 'V', 'H', 'E', 'A' )
#define TTAG_vmtx FT_MAKE_TAG( 'v', 'm', 't', 'x' )
+#define TTAG_VMTX FT_MAKE_TAG( 'V', 'M', 'T', 'X' )
+#define TTAG_VARC FT_MAKE_TAG( 'V', 'A', 'R', 'C' )
#define TTAG_VVAR FT_MAKE_TAG( 'V', 'V', 'A', 'R' )
#define TTAG_wOFF FT_MAKE_TAG( 'w', 'O', 'F', 'F' )
#define TTAG_wOF2 FT_MAKE_TAG( 'w', 'O', 'F', '2' )
diff --git a/thirdparty/freetype/src/autofit/afgsub.c b/thirdparty/freetype/src/autofit/afgsub.c
index af7b0f67982..4a9b9ef69fa 100644
--- a/thirdparty/freetype/src/autofit/afgsub.c
+++ b/thirdparty/freetype/src/autofit/afgsub.c
@@ -67,6 +67,8 @@
{
FT_UInt rangeCount = FT_NEXT_USHORT( p );
FT_Byte* limit = p + rangeCount * 6;
+ FT_UInt startGlyphID;
+ FT_UInt endGlyphID = 0;
if ( table_limit < limit )
@@ -74,14 +76,15 @@
while ( p < limit )
{
- FT_UInt startGlyphID = FT_NEXT_USHORT( p );
- FT_UInt endGlyphID = FT_NEXT_USHORT( p );
-
+ startGlyphID = FT_NEXT_USHORT( p );
+ if ( startGlyphID < endGlyphID )
+ return FALSE;
- if ( startGlyphID > endGlyphID )
+ endGlyphID = FT_NEXT_USHORT( p );
+ if ( endGlyphID < startGlyphID )
return FALSE;
- count += endGlyphID - startGlyphID + 1;
+ count += ++endGlyphID - startGlyphID;
/* We don't validate coverage indices. */
p += 2;
@@ -229,7 +232,8 @@
if ( lookupType == 7 )
{
/* Substitution extension. */
- FT_Byte* q = subtable;
+ FT_Byte* q = subtable;
+ FT_UInt32 offset;
if ( table_limit < q + 8 )
@@ -243,7 +247,11 @@
else if ( real_lookupType != FT_NEXT_USHORT( q ) )
return FALSE;
- subtable += FT_PEEK_ULONG( q );
+ offset = FT_PEEK_ULONG( q );
+ if ( offset > table_limit - table )
+ return FALSE;
+
+ subtable += offset;
}
else
real_lookupType = lookupType;
diff --git a/thirdparty/freetype/src/autofit/aflatin.c b/thirdparty/freetype/src/autofit/aflatin.c
index 73addf28eb4..3c97f91dd93 100644
--- a/thirdparty/freetype/src/autofit/aflatin.c
+++ b/thirdparty/freetype/src/autofit/aflatin.c
@@ -2783,8 +2783,8 @@
{
do
{
- point->y += movement;
- point = point->next;
+ point->y = ADD_LONG( point->y, movement );
+ point = point->next;
} while ( point != first_point );
}
@@ -3739,7 +3739,7 @@
pos = ADD_LONG( high_min_y, adjustment_amount );
if ( adjust_below_top && is_top_tilde )
- pos += high_height;
+ pos = ADD_LONG( pos, high_height );
if ( pos % 64 == 0 && tilde_height < 3 * 64 )
{
@@ -3892,7 +3892,7 @@
pos = SUB_LONG( low_max_y, adjustment_amount );
if ( adjust_above_bottom && is_bottom_tilde )
- pos -= low_height;
+ pos = SUB_LONG( pos, low_height );
if ( pos % 64 == 0 && tilde_height < 3 * 64 )
{
diff --git a/thirdparty/freetype/src/autofit/afranges.c b/thirdparty/freetype/src/autofit/afranges.c
index 4d0755decb2..c9f5e0a8aab 100644
--- a/thirdparty/freetype/src/autofit/afranges.c
+++ b/thirdparty/freetype/src/autofit/afranges.c
@@ -104,7 +104,8 @@
AF_UNIRANGE_REC( 0xFE7A, 0xFE7A ),
AF_UNIRANGE_REC( 0xFE7C, 0xFE7C ),
AF_UNIRANGE_REC( 0xFE7E, 0xFE7E ),
- AF_UNIRANGE_REC( 0x10EFD, 0x10EFF ),
+ AF_UNIRANGE_REC( 0x10ECB, 0x10ECF ),
+ AF_UNIRANGE_REC( 0x10EF0, 0x10EFF ),
AF_UNIRANGE_REC( 0, 0 )
};
@@ -118,7 +119,8 @@
const AF_Script_UniRangeRec af_armn_nonbase_uniranges[] =
{
- AF_UNIRANGE_REC( 0x0559, 0x055F ),
+ AF_UNIRANGE_REC( 0x0558, 0x055F ),
+ AF_UNIRANGE_REC( 0x058B, 0x058C ),
AF_UNIRANGE_REC( 0, 0 )
};
@@ -156,7 +158,8 @@
const AF_Script_UniRangeRec af_beng_uniranges[] =
{
- AF_UNIRANGE_REC( 0x0980, 0x09FF ), /* Bengali */
+ AF_UNIRANGE_REC( 0x0980, 0x09FF ), /* Bengali */
+ AF_UNIRANGE_REC( 0x11DF0, 0x11DFF ), /* Bengali Supplement */
AF_UNIRANGE_REC( 0, 0 )
};
@@ -168,6 +171,7 @@
AF_UNIRANGE_REC( 0x09CD, 0x09CD ),
AF_UNIRANGE_REC( 0x09E2, 0x09E3 ),
AF_UNIRANGE_REC( 0x09FE, 0x09FE ),
+ AF_UNIRANGE_REC( 0x11DF0, 0x11DF0 ),
AF_UNIRANGE_REC( 0, 0 )
};
@@ -470,7 +474,7 @@
AF_UNIRANGE_REC( 0x0591, 0x05BF ),
AF_UNIRANGE_REC( 0x05C1, 0x05C2 ),
AF_UNIRANGE_REC( 0x05C4, 0x05C5 ),
- AF_UNIRANGE_REC( 0x05C7, 0x05C7 ),
+ AF_UNIRANGE_REC( 0x05C7, 0x05C9 ),
AF_UNIRANGE_REC( 0xFB1E, 0xFB1E ),
AF_UNIRANGE_REC( 0, 0 )
};
@@ -586,7 +590,7 @@
AF_UNIRANGE_REC( 0xAB6A, 0xAB6F ), /* ... continued */
AF_UNIRANGE_REC( 0xFB00, 0xFB06 ), /* Alphab. Present. Forms (Latin Ligs) */
AF_UNIRANGE_REC( 0x1D400, 0x1D7FF ), /* Mathematical Alphanumeric Symbols */
- AF_UNIRANGE_REC( 0x1DF00, 0x1DFFF ), /* Latin Extended-G */
+ AF_UNIRANGE_REC( 0x1DF00, 0x1DFCC ), /* Latin Extended-G */
AF_UNIRANGE_REC( 0, 0 )
};
@@ -643,7 +647,8 @@
AF_UNIRANGE_REC( 0xA7F8, 0xA7F9 ), /* more modifier letters */
AF_UNIRANGE_REC( 0xAB5C, 0xAB5F ), /* more modifier letters */
AF_UNIRANGE_REC( 0xAB69, 0xAB69 ), /* modifier letter small turned w */
- AF_UNIRANGE_REC( 0x10780, 0x107FB ), /* Latin Extended-F */
+ AF_UNIRANGE_REC( 0x10780, 0x107BF ), /* Latin Extended-F */
+ AF_UNIRANGE_REC( 0x1DFCD, 0x1DFFF ), /* Latin Extended-G (subset) */
AF_UNIRANGE_REC( 0, 0 )
};
diff --git a/thirdparty/freetype/src/autofit/afshaper.c b/thirdparty/freetype/src/autofit/afshaper.c
index 5c9e5376ad0..3c0ffe09473 100644
--- a/thirdparty/freetype/src/autofit/afshaper.c
+++ b/thirdparty/freetype/src/autofit/afshaper.c
@@ -45,12 +45,16 @@
* that actually do the mapping job. Please check the OpenType
* specification for more details on features and lookups.
*
- * 2. Create glyph ID sets from the corresponding lookup sets.
+ * 2. Create glyph ID sets from the corresponding lookup sets. For
+ * default coverage, start with glyphs already assigned to the style
+ * and compute their substitution closure.
*
* 3. The glyph set corresponding to AF_COVERAGE_DEFAULT is computed
- * with all lookups specific to the OpenType script activated. It
- * relies on the order of AF_DEFINE_STYLE_CLASS entries so that
- * special coverages (like `oldstyle figures') don't get overwritten.
+ * with all lookups specific to the OpenType script activated. Only
+ * reachable glyphs are added, since a lookup can be shared by several
+ * scripts. It relies on the order of AF_DEFINE_STYLE_CLASS entries
+ * so that special coverages (like `oldstyle figures') don't get
+ * overwritten.
*
*/
@@ -198,14 +202,34 @@
count++;
#endif
- /* get output coverage of GSUB feature */
- hb( ot_layout_lookup_collect_glyphs )( face,
- HB_OT_TAG_GSUB,
- idx,
- NULL,
- NULL,
- NULL,
- gsub_glyphs );
+ if ( style_class->coverage != AF_COVERAGE_DEFAULT )
+ {
+ /* get output coverage of GSUB feature */
+ hb( ot_layout_lookup_collect_glyphs )( face,
+ HB_OT_TAG_GSUB,
+ idx,
+ NULL,
+ NULL,
+ NULL,
+ gsub_glyphs );
+ }
+ }
+
+ if ( style_class->coverage == AF_COVERAGE_DEFAULT )
+ {
+ FT_UInt i;
+
+
+ /* A script's lookups can also substitute glyphs of other scripts. */
+ /* Seed the closure with this style's glyphs so that their */
+ /* substitutes inherit the same style. */
+ for ( i = 0; i < globals->glyph_count; i++ )
+ if ( ( gstyles[i] & AF_STYLE_MASK ) == style_class->style )
+ hb( set_add )( gsub_glyphs, i );
+
+ hb( ot_layout_lookups_substitute_closure )( face,
+ gsub_lookups,
+ gsub_glyphs );
}
#ifdef FT_DEBUG_LEVEL_TRACE
diff --git a/thirdparty/freetype/src/autofit/ft-hb-decls.h b/thirdparty/freetype/src/autofit/ft-hb-decls.h
index 032d4d37a5e..056b53a191d 100644
--- a/thirdparty/freetype/src/autofit/ft-hb-decls.h
+++ b/thirdparty/freetype/src/autofit/ft-hb-decls.h
@@ -142,6 +142,11 @@ hb_ot_layout_lookup_would_substitute,(hb_face_t *face,
unsigned int glyphs_length,
hb_bool_t zero_context))
+HB_EXTERN(void,
+hb_ot_layout_lookups_substitute_closure,(hb_face_t *face,
+ const hb_set_t *lookups,
+ hb_set_t *glyphs /* OUT */))
+
HB_EXTERN(void,
hb_ot_tags_from_script_and_language,(hb_script_t script,
hb_language_t language,
diff --git a/thirdparty/freetype/src/autofit/hb-script-list.h b/thirdparty/freetype/src/autofit/hb-script-list.h
index 88e270d3479..1caa16aabe7 100644
--- a/thirdparty/freetype/src/autofit/hb-script-list.h
+++ b/thirdparty/freetype/src/autofit/hb-script-list.h
@@ -223,6 +223,13 @@ HB_END_DECLS
* @HB_SCRIPT_SUNUWAR: `Sunu`, Since: 10.0.0
* @HB_SCRIPT_TODHRI: `Todr`, Since: 10.0.0
* @HB_SCRIPT_TULU_TIGALARI: `Tutg`, Since: 10.0.0
+ * @HB_SCRIPT_BERIA_ERFE: `Berf`, Since: 11.5.0
+ * @HB_SCRIPT_SIDETIC: `Sidt`, Since: 11.5.0
+ * @HB_SCRIPT_TAI_YO: `Tayo`, Since: 11.5.0
+ * @HB_SCRIPT_TOLONG_SIKI: `Tols`, Since: 11.5.0
+ * @HB_SCRIPT_JURCHEN: `Jurc`, Since: 14.5.0
+ * @HB_SCRIPT_PROTO_CUNEIFORM: `Pcun`, Since: 14.5.0
+ * @HB_SCRIPT_SEAL: `Seal`, Since: 14.5.0
* @HB_SCRIPT_INVALID: No script set
*
* Data type for scripts. Each #hb_script_t's value is an #hb_tag_t corresponding
@@ -461,6 +468,21 @@ typedef enum
HB_SCRIPT_TODHRI = HB_TAG ('T','o','d','r'), /*16.0*/
HB_SCRIPT_TULU_TIGALARI = HB_TAG ('T','u','t','g'), /*16.0*/
+ /*
+ * Since 11.5.0
+ */
+ HB_SCRIPT_BERIA_ERFE = HB_TAG ('B','e','r','f'), /*17.0*/
+ HB_SCRIPT_SIDETIC = HB_TAG ('S','i','d','t'), /*17.0*/
+ HB_SCRIPT_TAI_YO = HB_TAG ('T','a','y','o'), /*17.0*/
+ HB_SCRIPT_TOLONG_SIKI = HB_TAG ('T','o','l','s'), /*17.0*/
+
+ /*
+ * Since: 14.5.0
+ */
+ HB_SCRIPT_JURCHEN = HB_TAG ('J','u','r','c'), /*18.0*/
+ HB_SCRIPT_PROTO_CUNEIFORM = HB_TAG ('P','c','u','n'), /*18.0*/
+ HB_SCRIPT_SEAL = HB_TAG ('S','e','a','l'), /*18.0*/
+
/* No script set. */
HB_SCRIPT_INVALID = HB_TAG_NONE,
@@ -480,5 +502,4 @@ typedef enum
} hb_script_t;
-
#endif /* HB_SCRIPT_LIST_H */
diff --git a/thirdparty/freetype/src/base/ftbbox.c b/thirdparty/freetype/src/base/ftbbox.c
index 4641ce27e82..4007a9314f0 100644
--- a/thirdparty/freetype/src/base/ftbbox.c
+++ b/thirdparty/freetype/src/base/ftbbox.c
@@ -291,11 +291,10 @@
/* It is called with either q2 or q3 positive, which is necessary */
/* for the peak to exist and avoids undefined FT_MSB. */
- shift = 27 - FT_MSB( (FT_UInt32)( FT_ABS( q1 ) |
- FT_ABS( q2 ) |
- FT_ABS( q3 ) |
- FT_ABS( q4 ) ) );
-
+ shift = 8 * sizeof ( FT_Pos ) - 5 - FT_MSB( ULABS( q1 ) |
+ ULABS( q2 ) |
+ ULABS( q3 ) |
+ ULABS( q4 ) );
if ( shift > 0 )
{
/* upscaling too much just wastes time */
diff --git a/thirdparty/freetype/src/base/ftbitmap.c b/thirdparty/freetype/src/base/ftbitmap.c
index f88cbc11e63..1606cbfa090 100644
--- a/thirdparty/freetype/src/base/ftbitmap.c
+++ b/thirdparty/freetype/src/base/ftbitmap.c
@@ -776,18 +776,12 @@
FT_Bitmap source_bitmap;
const FT_Bitmap* source;
- FT_Vector source_offset;
- FT_Vector target_offset;
-
FT_Bool free_source_bitmap = 0;
FT_Bool free_target_bitmap_on_error = 0;
- FT_Pos source_llx, source_lly, source_urx, source_ury;
- FT_Pos target_llx, target_lly, target_urx, target_ury;
- FT_Pos final_llx, final_lly, final_urx, final_ury;
+ FT_BBox sbx, tbx, fbx;
unsigned int final_rows, final_width;
- long x, y;
if ( !library || !target || !source_ || !atarget_offset )
@@ -811,110 +805,32 @@
if ( !( source_->width && source_->rows ) )
return FT_Err_Ok; /* nothing to do */
- /* assure integer pixel offsets */
- source_offset.x = FT_PIX_FLOOR( source_offset_.x );
- source_offset.y = FT_PIX_FLOOR( source_offset_.y );
- target_offset.x = FT_PIX_FLOOR( atarget_offset->x );
- target_offset.y = FT_PIX_FLOOR( atarget_offset->y );
+ /* get source bitmap dimensions assuming integer offsets */
+ sbx.xMin = ( source_offset_.x >> 6 );
+ sbx.yMin = ( source_offset_.y >> 6 ) - source_->rows;
+ sbx.xMax = ( source_offset_.x >> 6 ) + source_->width;
+ sbx.yMax = ( source_offset_.y >> 6 );
- /* get source bitmap dimensions */
- source_llx = source_offset.x;
- if ( FT_LONG_MIN + (FT_Pos)( source_->rows << 6 ) + 64 > source_offset.y )
- {
- FT_TRACE5((
- "FT_Bitmap_Blend: y coordinate overflow in source bitmap\n" ));
- return FT_THROW( Invalid_Argument );
- }
- source_lly = source_offset.y - ( source_->rows << 6 );
+ FT_TRACE5(( "FT_Bitmap_Blend:\n" ));
+ FT_TRACE5(( " source bitmap: (%ld, %ld) -- (%ld, %ld); %u x %u\n",
+ sbx.xMin, sbx.yMin, sbx.xMax, sbx.yMax,
+ source_->width, source_->rows ));
- if ( FT_LONG_MAX - (FT_Pos)( source_->width << 6 ) - 64 < source_llx )
+ /* sanity check */
+ if ( sbx.xMin > sbx.xMax || sbx.yMin > sbx.yMax ||
+ source_->width > 0x7FFFU || source_->rows > 0x7FFFU )
{
- FT_TRACE5((
- "FT_Bitmap_Blend: x coordinate overflow in source bitmap\n" ));
+ FT_TRACE5(( "FT_Bitmap_Blend: source dimension overflow\n" ));
return FT_THROW( Invalid_Argument );
}
- source_urx = source_llx + ( source_->width << 6 );
- source_ury = source_offset.y;
- /* get target bitmap dimensions */
- if ( target->width && target->rows )
- {
- target_llx = target_offset.x;
- if ( FT_LONG_MIN + (FT_Pos)( target->rows << 6 ) > target_offset.y )
- {
- FT_TRACE5((
- "FT_Bitmap_Blend: y coordinate overflow in target bitmap\n" ));
- return FT_THROW( Invalid_Argument );
- }
- target_lly = target_offset.y - ( target->rows << 6 );
-
- if ( FT_LONG_MAX - (FT_Pos)( target->width << 6 ) < target_llx )
- {
- FT_TRACE5((
- "FT_Bitmap_Blend: x coordinate overflow in target bitmap\n" ));
- return FT_THROW( Invalid_Argument );
- }
- target_urx = target_llx + ( target->width << 6 );
- target_ury = target_offset.y;
- }
- else
+ if ( !target->width || !target->rows )
{
- target_llx = FT_LONG_MAX;
- target_lly = FT_LONG_MAX;
- target_urx = FT_LONG_MIN;
- target_ury = FT_LONG_MIN;
- }
-
- /* compute final bitmap dimensions */
- final_llx = FT_MIN( source_llx, target_llx );
- final_lly = FT_MIN( source_lly, target_lly );
- final_urx = FT_MAX( source_urx, target_urx );
- final_ury = FT_MAX( source_ury, target_ury );
-
- final_width = ( final_urx - final_llx ) >> 6;
- final_rows = ( final_ury - final_lly ) >> 6;
-
-#ifdef FT_DEBUG_LEVEL_TRACE
- FT_TRACE5(( "FT_Bitmap_Blend:\n" ));
- FT_TRACE5(( " source bitmap: (%ld, %ld) -- (%ld, %ld); %u x %u\n",
- source_llx / 64, source_lly / 64,
- source_urx / 64, source_ury / 64,
- source_->width, source_->rows ));
-
- if ( target->width && target->rows )
- FT_TRACE5(( " target bitmap: (%ld, %ld) -- (%ld, %ld); %u x %u\n",
- target_llx / 64, target_lly / 64,
- target_urx / 64, target_ury / 64,
- target->width, target->rows ));
- else
FT_TRACE5(( " target bitmap: empty\n" ));
- if ( final_width && final_rows )
- FT_TRACE5(( " final bitmap: (%ld, %ld) -- (%ld, %ld); %u x %u\n",
- final_llx / 64, final_lly / 64,
- final_urx / 64, final_ury / 64,
- final_width, final_rows ));
- else
- FT_TRACE5(( " final bitmap: empty\n" ));
-#endif /* FT_DEBUG_LEVEL_TRACE */
-
- if ( !( final_width && final_rows ) )
- return FT_Err_Ok; /* nothing to do */
-
- /* for blending, set offset vector of final bitmap */
- /* temporarily to (0,0) */
- source_llx -= final_llx;
- source_lly -= final_lly;
-
- if ( target->width && target->rows )
- {
- target_llx -= final_llx;
- target_lly -= final_lly;
- }
+ final_width = source_->width;
+ final_rows = source_->rows;
- /* set up target bitmap */
- if ( target->pixel_mode == FT_PIXEL_MODE_NONE )
- {
/* create new empty bitmap */
target->width = final_width;
target->rows = final_rows;
@@ -926,72 +842,116 @@
return error;
free_target_bitmap_on_error = 1;
+
+ fbx = sbx;
}
- else if ( target->width != final_width ||
- target->rows != final_rows )
+ else
{
- /* adjust old bitmap to enlarged size */
- int pitch, new_pitch;
+ /* get target bitmap dimensions assuming integer offsets */
+ tbx.xMin = ( atarget_offset->x >> 6 );
+ tbx.yMin = ( atarget_offset->y >> 6 ) - target->rows;
+ tbx.xMax = ( atarget_offset->x >> 6 ) + target->width;
+ tbx.yMax = ( atarget_offset->y >> 6 );
- unsigned char* buffer = NULL;
+ FT_TRACE5(( " target bitmap: (%ld, %ld) -- (%ld, %ld); %u x %u\n",
+ tbx.xMin, tbx.yMin, tbx.xMax, tbx.yMax,
+ target->width, target->rows ));
+ /* sanity check */
+ if ( tbx.xMin > tbx.xMax || tbx.yMin > tbx.yMax )
+ {
+ FT_TRACE5(( "FT_Bitmap_Blend: targget dimension overflow\n" ));
+ return FT_THROW( Invalid_Argument );
+ }
- pitch = target->pitch;
+ /* compute final bitmap dimensions */
+ fbx.xMin = FT_MIN( sbx.xMin, tbx.xMin );
+ fbx.yMin = FT_MIN( sbx.yMin, tbx.yMin );
+ fbx.xMax = FT_MAX( sbx.xMax, tbx.xMax );
+ fbx.yMax = FT_MAX( sbx.yMax, tbx.yMax );
- if ( pitch < 0 )
- pitch = -pitch;
+ /* sanity check */
+ if ( fbx.xMin < -0x10000 || fbx.xMax >= 0x10000 ||
+ fbx.yMin < -0x10000 || fbx.yMax >= 0x10000 )
+ {
+ FT_TRACE5(( "FT_Bitmap_Blend: final dimension overflow\n" ));
+ return FT_THROW( Invalid_Argument );
+ }
- new_pitch = (int)final_width * 4;
+ final_width = fbx.xMax - fbx.xMin;
+ final_rows = fbx.yMax - fbx.yMin;
- /* TODO: provide an in-buffer solution for large bitmaps */
- /* to avoid allocation of a new buffer */
- if ( FT_ALLOC_MULT( buffer, final_rows, new_pitch ) )
- goto Error;
+ /* adjust target bitmap to enlarged size */
+ if ( target->width < final_width ||
+ target->rows < final_rows )
+ {
+ int pitch, new_pitch;
- /* copy data to new buffer */
- x = target_llx >> 6;
- y = target_lly >> 6;
+ unsigned char* buffer = NULL;
- /* the bitmap flow is from top to bottom, */
- /* but y is measured from bottom to top */
- if ( target->pitch < 0 )
- {
- /* XXX */
- }
- else
- {
- unsigned char* p =
- target->buffer;
- unsigned char* q =
- buffer +
- ( final_rows - y - target->rows ) * new_pitch +
- x * 4;
- unsigned char* limit_p =
- p + pitch * (int)target->rows;
+ pitch = target->pitch;
+
+ if ( pitch < 0 )
+ pitch = -pitch;
+
+ new_pitch = (int)final_width * 4;
+
+ /* TODO: provide an in-buffer solution for large bitmaps */
+ /* to avoid allocation of a new buffer */
+ if ( FT_ALLOC_MULT( buffer, final_rows, new_pitch ) )
+ goto Error;
+
+ /* copy data to new buffer */
- while ( p < limit_p )
+ /* the bitmap flow is from top to bottom, */
+ /* but y is measured from bottom to top */
+ if ( target->pitch < 0 )
{
- FT_MEM_COPY( q, p, pitch );
+ /* XXX */
+ }
+ else
+ {
+ FT_Pos x = tbx.xMin - fbx.xMin;
+ FT_Pos y = tbx.yMin - fbx.yMin;
+
+ unsigned char* p =
+ target->buffer;
+ unsigned char* q =
+ buffer +
+ ( final_rows - y - target->rows ) * new_pitch +
+ x * 4;
+ unsigned char* limit_p =
+ p + pitch * (int)target->rows;
+
- p += pitch;
- q += new_pitch;
+ while ( p < limit_p )
+ {
+ FT_MEM_COPY( q, p, pitch );
+
+ p += pitch;
+ q += new_pitch;
+ }
}
- }
- FT_FREE( target->buffer );
+ FT_FREE( target->buffer );
- target->width = final_width;
- target->rows = final_rows;
+ target->width = final_width;
+ target->rows = final_rows;
- if ( target->pitch < 0 )
- target->pitch = -new_pitch;
- else
- target->pitch = new_pitch;
+ if ( target->pitch < 0 )
+ target->pitch = -new_pitch;
+ else
+ target->pitch = new_pitch;
- target->buffer = buffer;
+ target->buffer = buffer;
+ }
}
+ FT_TRACE5(( " final bitmap: (%ld, %ld) -- (%ld, %ld); %u x %u\n",
+ fbx.xMin, fbx.yMin, fbx.xMax, fbx.yMax,
+ final_width, final_rows ));
+
/* adjust source bitmap if necessary */
if ( source_->pixel_mode != FT_PIXEL_MODE_GRAY )
{
@@ -1008,8 +968,6 @@
/* do blending; the code below returns pre-multiplied channels, */
/* similar to what FreeType gets from `CBDT' tables */
- x = source_llx >> 6;
- y = source_lly >> 6;
/* the bitmap flow is from top to bottom, */
/* but y is measured from bottom to top */
@@ -1019,6 +977,9 @@
}
else
{
+ FT_Pos x = sbx.xMin - fbx.xMin;
+ FT_Pos y = sbx.yMin - fbx.yMin;
+
unsigned char* p =
source->buffer;
unsigned char* q =
@@ -1064,8 +1025,8 @@
}
}
- atarget_offset->x = final_llx;
- atarget_offset->y = final_lly + ( final_rows << 6 );
+ atarget_offset->x = fbx.xMin * 64;
+ atarget_offset->y = fbx.yMax * 64;
Error:
if ( error && free_target_bitmap_on_error )
diff --git a/thirdparty/freetype/src/base/ftcalc.c b/thirdparty/freetype/src/base/ftcalc.c
index d233dba3d6b..c78d3b6da4b 100644
--- a/thirdparty/freetype/src/base/ftcalc.c
+++ b/thirdparty/freetype/src/base/ftcalc.c
@@ -714,9 +714,8 @@
FT_Matrix_Check( const FT_Matrix* matrix )
{
FT_Fixed xx, xy, yx, yy;
- FT_Fixed val;
+ FT_ULong val, abs_det, frob_sq;
FT_Int shift;
- FT_ULong temp1, temp2;
if ( !matrix )
@@ -726,16 +725,14 @@
xy = matrix->xy;
yx = matrix->yx;
yy = matrix->yy;
- val = FT_ABS( xx ) | FT_ABS( xy ) | FT_ABS( yx ) | FT_ABS( yy );
+ val = ULABS( xx ) | ULABS( xy ) | ULABS( yx ) | ULABS( yy );
/* we only handle non-zero 32-bit values */
- if ( !val || val > 0x7FFFFFFFL )
+ if ( !val || val > 0x7FFFFFFFUL )
return 0;
- /* Scale matrix to avoid the temp1 overflow, which is */
- /* more stringent than avoiding the temp2 overflow. */
-
- shift = FT_MSB( val ) - 12;
+ /* Scale matrix to avoid overflows */
+ shift = FT_MSB( val ) - 4 * sizeof ( FT_ULong ) + 2;
if ( shift > 0 )
{
@@ -745,11 +742,11 @@
yy >>= shift;
}
- temp1 = 32U * (FT_ULong)FT_ABS( xx * yy - xy * yx );
- temp2 = (FT_ULong)( xx * xx ) + (FT_ULong)( xy * xy ) +
- (FT_ULong)( yx * yx ) + (FT_ULong)( yy * yy );
+ abs_det = ULABS( xx * yy - xy * yx );
+ frob_sq = (FT_ULong)( xx * xx ) + (FT_ULong)( xy * xy ) +
+ (FT_ULong)( yx * yx ) + (FT_ULong)( yy * yy );
- if ( temp1 <= temp2 )
+ if ( abs_det <= frob_sq / 32 )
return 0;
return 1;
@@ -966,7 +963,7 @@
FT_Int result;
- if ( (FT_ULong)FT_ABS( in_x ) + (FT_ULong)FT_ABS( out_y ) <= 92681UL )
+ if ( ULABS( in_x ) + ULABS( out_y ) <= 92681UL )
{
z1.lo = (FT_UInt32)in_x * (FT_UInt32)out_y;
z1.hi = (FT_UInt32)( (FT_Int32)z1.lo >> 31 ); /* sign-expansion */
@@ -974,7 +971,7 @@
else
ft_multo64( (FT_UInt32)in_x, (FT_UInt32)out_y, &z1 );
- if ( (FT_ULong)FT_ABS( in_y ) + (FT_ULong)FT_ABS( out_x ) <= 92681UL )
+ if ( ULABS( in_y ) + ULABS( out_x ) <= 92681UL )
{
z2.lo = (FT_UInt32)in_y * (FT_UInt32)out_x;
z2.hi = (FT_UInt32)( (FT_Int32)z2.lo >> 31 ); /* sign-expansion */
diff --git a/thirdparty/freetype/src/base/ftobjs.c b/thirdparty/freetype/src/base/ftobjs.c
index f9f64c40fec..acefdf46fd9 100644
--- a/thirdparty/freetype/src/base/ftobjs.c
+++ b/thirdparty/freetype/src/base/ftobjs.c
@@ -42,6 +42,10 @@
#include <freetype/internal/services/svkern.h>
#include <freetype/internal/services/svtteng.h>
+#ifdef TT_CONFIG_OPTION_VARC
+#include <freetype/internal/services/svvarc.h>
+#endif
+
#include <freetype/ftdriver.h>
#ifdef FT_CONFIG_OPTION_MAC_FONTS
@@ -503,8 +507,8 @@
pbox.xMin < -0x1000000 || pbox.xMax >= 0x1000000 ||
pbox.yMin < -0x1000000 || pbox.yMax >= 0x1000000 ||
( slot->face &&
- ( width > 10 * slot->face->size->metrics.x_ppem ||
- height > 10 * slot->face->size->metrics.y_ppem ) ) );
+ ( width > 16 * slot->face->size->metrics.x_ppem ||
+ height > 16 * slot->face->size->metrics.y_ppem ) ) );
if ( ret )
FT_TRACE3(( "ft_glyphslot_preset_bitmap: [%ld %ld %ld %ld]\n",
@@ -788,7 +792,7 @@
internal->transform_flags = 0;
- if ( !matrix )
+ if ( !matrix || !FT_Matrix_Check( matrix ) )
{
internal->transform_matrix.xx = 0x10000L;
internal->transform_matrix.xy = 0;
@@ -1017,6 +1021,22 @@
}
}
+#ifdef TT_CONFIG_OPTION_VARC
+ /* Never auto-hint a VARC (variable composite) glyph: its components */
+ /* are already loaded unhinted, and the auto-fitter cannot process */
+ /* the assembled composite (it also re-enters variation-coordinate */
+ /* setting). Use the native hinter / unhinted load instead. */
+ if ( autohint && FT_IS_SFNT( face ) )
+ {
+ FT_Service_VARC varc;
+
+
+ FT_FACE_FIND_SERVICE( face, varc, VARC );
+ if ( varc && varc->has_glyph( face, glyph_index ) )
+ autohint = FALSE;
+ }
+#endif
+
if ( autohint )
{
FT_AutoHinter_Interface hinting;
@@ -1441,8 +1461,14 @@
{
if ( cur[0]->encoding == FT_ENCODING_UNICODE )
{
- face->charmap = cur[0];
- return FT_Err_Ok;
+ FT_Long format = FT_Get_CMap_Format( cur[0] );
+
+
+ if ( format != 14 && format != 15 )
+ {
+ face->charmap = cur[0];
+ return FT_Err_Ok;
+ }
}
}
@@ -1457,7 +1483,7 @@
*
* @Description:
* This function finds the variant selector charmap, if there is one.
- * There can only be one (platform=0, specific=5, format=14).
+ * There can only be one (platform=0, specific=5, format=14 or 15).
*/
static FT_CharMap
find_variant_selector_charmap( FT_Face face )
@@ -1480,9 +1506,14 @@
for ( cur = first; cur < end; cur++ )
{
if ( cur[0]->platform_id == TT_PLATFORM_APPLE_UNICODE &&
- cur[0]->encoding_id == TT_APPLE_ID_VARIANT_SELECTOR &&
- FT_Get_CMap_Format( cur[0] ) == 14 )
- return cur[0];
+ cur[0]->encoding_id == TT_APPLE_ID_VARIANT_SELECTOR )
+ {
+ FT_Long format = FT_Get_CMap_Format( cur[0] );
+
+
+ if ( format == 14 || format == 15 )
+ return cur[0];
+ }
}
return NULL;
@@ -3771,11 +3802,16 @@
for ( ; cur < limit; cur++ )
{
- if ( cur[0] == charmap &&
- FT_Get_CMap_Format ( charmap ) != 14 )
+ if ( cur[0] == charmap )
{
- face->charmap = cur[0];
- return FT_Err_Ok;
+ FT_Long format = FT_Get_CMap_Format( charmap );
+
+
+ if ( format != 14 && format != 15 )
+ {
+ face->charmap = cur[0];
+ return FT_Err_Ok;
+ }
}
}
@@ -4737,121 +4773,114 @@
FT_Error error = FT_Err_Ok;
FT_Face face = slot->face;
FT_Renderer renderer;
+ FT_ListNode node = NULL;
- switch ( slot->format )
+ /* try to render colored glyph layers as a special case */
+ if ( slot->internal->load_flags & FT_LOAD_COLOR &&
+ slot->format == FT_GLYPH_FORMAT_OUTLINE )
{
- default:
- if ( slot->internal->load_flags & FT_LOAD_COLOR )
- {
- FT_LayerIterator iterator;
+ FT_LayerIterator iterator;
- FT_UInt base_glyph = slot->glyph_index;
+ FT_UInt base_glyph = slot->glyph_index;
- FT_Bool have_layers;
- FT_UInt glyph_index;
- FT_UInt color_index;
+ FT_Bool have_layers;
+ FT_UInt glyph_index;
+ FT_UInt color_index;
- /* check whether we have colored glyph layers */
- iterator.p = NULL;
- have_layers = FT_Get_Color_Glyph_Layer( face,
- base_glyph,
- &glyph_index,
- &color_index,
- &iterator );
- if ( have_layers )
+ iterator.p = NULL;
+ have_layers = FT_Get_Color_Glyph_Layer( face,
+ base_glyph,
+ &glyph_index,
+ &color_index,
+ &iterator );
+ if ( have_layers )
+ {
+ error = FT_New_GlyphSlot( face, NULL );
+ if ( !error )
{
- error = FT_New_GlyphSlot( face, NULL );
- if ( !error )
- {
- TT_Face ttface = (TT_Face)face;
- SFNT_Service sfnt = (SFNT_Service)ttface->sfnt;
+ TT_Face ttface = (TT_Face)face;
+ SFNT_Service sfnt = (SFNT_Service)ttface->sfnt;
- do
- {
- FT_Int32 load_flags = slot->internal->load_flags;
+ do
+ {
+ FT_Int32 load_flags = slot->internal->load_flags;
- /* disable the `FT_LOAD_COLOR' flag to avoid recursion */
- /* right here in this function */
- load_flags &= ~FT_LOAD_COLOR;
+ /* disable the `FT_LOAD_COLOR' flag to avoid recursion */
+ /* right here in this function */
+ load_flags &= ~FT_LOAD_COLOR;
- /* render into the new `face->glyph' glyph slot */
- load_flags |= FT_LOAD_RENDER;
+ /* render into the new `face->glyph' glyph slot */
+ load_flags |= FT_LOAD_RENDER | FT_LOAD_NO_BITMAP;
- error = FT_Load_Glyph( face, glyph_index, load_flags );
- if ( error )
- break;
-
- /* blend new `face->glyph' into old `slot'; */
- /* at the first call, `slot' is still empty */
- error = sfnt->colr_blend( ttface,
- color_index,
- slot,
- face->glyph );
- if ( error )
- break;
-
- } while ( FT_Get_Color_Glyph_Layer( face,
- base_glyph,
- &glyph_index,
- &color_index,
- &iterator ) );
+ error = FT_Load_Glyph( face, glyph_index, load_flags );
+ if ( error )
+ break;
- if ( !error )
- slot->format = FT_GLYPH_FORMAT_BITMAP;
+ /* blend new `face->glyph' into old `slot'; */
+ /* at the first call, `slot' is still empty */
+ error = sfnt->colr_blend( ttface,
+ color_index,
+ slot,
+ face->glyph );
+ if ( error )
+ break;
- /* this call also restores `slot' as the glyph slot */
- FT_Done_GlyphSlot( face->glyph );
- }
+ } while ( FT_Get_Color_Glyph_Layer( face,
+ base_glyph,
+ &glyph_index,
+ &color_index,
+ &iterator ) );
if ( !error )
- return error;
+ slot->format = FT_GLYPH_FORMAT_BITMAP;
- /* Failed to do the colored layer. Draw outline instead. */
- slot->format = FT_GLYPH_FORMAT_OUTLINE;
+ /* this call also restores `slot' as the glyph slot */
+ FT_Done_GlyphSlot( face->glyph );
}
- }
- {
- FT_ListNode node = NULL;
+ if ( !error )
+ return error;
+ /* Failed to do the colored layer. Draw outline instead. */
+ slot->format = FT_GLYPH_FORMAT_OUTLINE;
+ }
+ }
- /* small shortcut for the very common case */
- if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
- {
- renderer = library->cur_renderer;
- node = library->renderers.head;
- }
- else
- renderer = FT_Lookup_Renderer( library, slot->format, &node );
+ /* small shortcut for the very common case */
+ if ( slot->format == FT_GLYPH_FORMAT_OUTLINE )
+ {
+ renderer = library->cur_renderer;
+ node = library->renderers.head;
+ }
+ else
+ renderer = FT_Lookup_Renderer( library, slot->format, &node );
- error = FT_ERR( Cannot_Render_Glyph );
- while ( renderer )
- {
- error = renderer->render( renderer, slot, render_mode, NULL );
- if ( !error ||
- FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
- break;
-
- /* FT_Err_Cannot_Render_Glyph is returned if the render mode */
- /* is unsupported by the current renderer for this glyph image */
- /* format. */
-
- /* now, look for another renderer that supports the same */
- /* format. */
- renderer = FT_Lookup_Renderer( library, slot->format, &node );
- }
+ error = FT_ERR( Cannot_Render_Glyph );
+ while ( renderer )
+ {
+ error = renderer->render( renderer, slot, render_mode, NULL );
+ if ( !error ||
+ FT_ERR_NEQ( error, Cannot_Render_Glyph ) )
+ break;
- /* it is not an error if we cannot render a bitmap glyph */
- if ( FT_ERR_EQ( error, Cannot_Render_Glyph ) &&
- slot->format == FT_GLYPH_FORMAT_BITMAP )
- error = FT_Err_Ok;
- }
+ /* `FT_Err_Cannot_Render_Glyph` is returned if the render mode */
+ /* is unsupported by the current renderer for this glyph image */
+ /* format. */
+
+ /* now, look for another renderer that supports the same */
+ /* format. */
+ renderer = FT_Lookup_Renderer( library, slot->format, &node );
}
+ /* it is not an error if we cannot render a bitmap glyph */
+ if ( FT_ERR_EQ( error, Cannot_Render_Glyph ) &&
+ slot->format == FT_GLYPH_FORMAT_BITMAP )
+ error = FT_Err_Ok;
+
#ifdef FT_DEBUG_LEVEL_TRACE
#undef FT_COMPONENT
diff --git a/thirdparty/freetype/src/base/ftoutln.c b/thirdparty/freetype/src/base/ftoutln.c
index a0a1549adaa..b37b53312fc 100644
--- a/thirdparty/freetype/src/base/ftoutln.c
+++ b/thirdparty/freetype/src/base/ftoutln.c
@@ -102,15 +102,14 @@
tags = outline->tags + first;
tag = FT_CURVE_TAG( tags[0] );
- /* A contour cannot start with a cubic control point! */
- if ( tag == FT_CURVE_TAG_CUBIC )
- goto Invalid_Outline;
-
/* check first point to determine origin */
- if ( tag == FT_CURVE_TAG_CONIC )
+ if ( tag != FT_CURVE_TAG_ON )
{
- /* first point is conic control. Yes, this happens. */
- if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
+ FT_Int last_tag = FT_CURVE_TAG( outline->tags[last] );
+
+
+ /* first point is a control point. Yes, this happens. */
+ if ( last_tag == FT_CURVE_TAG_ON )
{
/* start at last point if it is on the curve */
v_start = v_last;
@@ -118,13 +117,16 @@
}
else
{
- /* if both first and last points are conic, */
- /* start at their middle and record its position */
- /* for closure */
+ if ( last_tag != tag )
+ goto Invalid_Outline;
+
+ /* if both first and last points are controls, */
+ /* start at their middle and record its position */
+ /* for closure */
v_start.x = ( v_start.x + v_last.x ) / 2;
v_start.y = ( v_start.y + v_last.y ) / 2;
- /* v_last = v_start; */
+ /* v_last = v_start; */
}
point--;
tags--;
@@ -242,11 +244,24 @@
if ( point <= limit )
{
FT_Vector vec;
+ FT_Vector v_middle;
+ tag = FT_CURVE_TAG( tags[0] );
+
vec.x = SCALED( point->x );
vec.y = SCALED( point->y );
+ if ( tag == FT_CURVE_TAG_CUBIC )
+ {
+ v_middle.x = ( vec2.x + vec.x ) / 2;
+ v_middle.y = ( vec2.y + vec.y ) / 2;
+
+ vec = v_middle;
+ }
+ else if ( tag != FT_CURVE_TAG_ON )
+ goto Invalid_Outline;
+
FT_TRACE5(( " cubic to (%.2f, %.2f)"
" with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
(double)vec.x / 64,
@@ -258,6 +273,12 @@
error = func_interface->cubic_to( &vec1, &vec2, &vec, user );
if ( error )
goto Exit;
+
+ if ( tag == FT_CURVE_TAG_CUBIC )
+ {
+ point--;
+ tags--;
+ }
continue;
}
@@ -554,11 +575,15 @@
last = -1;
for ( n = 0; n < outline->n_contours; n++ )
{
- /* keep the first contour point as is and swap points around it */
- /* to guarantee that the cubic arches stay valid after reverse */
- first = last + 2;
+ first = last + 1;
last = outline->contours[n];
+ /* Keep the first point unless it is a cubic control. In that */
+ /* case, reverse the entire contour to preserve control pairs, */
+ /* including contours with no explicit on-curve points. */
+ if ( FT_CURVE_TAG( outline->tags[first] ) != FT_CURVE_TAG_CUBIC )
+ first++;
+
/* reverse point table */
{
FT_Vector* p = outline->points + first;
@@ -1050,12 +1075,17 @@
FT_EXPORT_DEF( FT_Orientation )
FT_Outline_Get_Orientation( FT_Outline* outline )
{
+ FT_Vector* points;
+ FT_Int c, n, first, last;
+
+#ifdef FT_INT64
+ FT_Int64 area = 0;
+#else
FT_BBox cbox = { 0, 0, 0, 0 };
FT_Int xshift, yshift;
- FT_Vector* points;
FT_Vector v_prev, v_cur;
- FT_Int c, n, first, last;
FT_Pos area = 0;
+#endif
if ( !outline || outline->n_points <= 0 )
@@ -1066,6 +1096,42 @@
/* cubic or quadratic curves, this test deals with the polygon */
/* only that is spanned up by the control points. */
+ points = outline->points;
+
+#ifdef FT_INT64
+
+ /* Single-pass shoelace. The 64-bit accumulator removes the need */
+ /* for the coordinate shift pre-pass (and its `FT_Outline_Get_CBox` */
+ /* traversal) that the 32-bit variant below uses. */
+ last = -1;
+ for ( c = 0; c < outline->n_contours; c++ )
+ {
+ FT_Pos prev_x, prev_y;
+
+
+ first = last + 1;
+ last = outline->contours[c];
+
+ prev_x = points[last].x;
+ prev_y = points[last].y;
+
+ for ( n = first; n <= last; n++ )
+ {
+ FT_Pos cur_x = points[n].x;
+ FT_Pos cur_y = points[n].y;
+
+
+ area = ADD_INT64( area,
+ MUL_INT64( SUB_INT64( cur_y, prev_y ),
+ ADD_INT64( cur_x, prev_x ) ) );
+
+ prev_x = cur_x;
+ prev_y = cur_y;
+ }
+ }
+
+#else /* !FT_INT64 */
+
FT_Outline_Get_CBox( outline, &cbox );
/* Handle collapsed outlines to avoid undefined FT_MSB. */
@@ -1084,8 +1150,6 @@
yshift = FT_MSB( (FT_UInt32)( cbox.yMax - cbox.yMin ) ) - 14;
yshift = FT_MAX( yshift, 0 );
- points = outline->points;
-
last = -1;
for ( c = 0; c < outline->n_contours; c++ )
{
@@ -1108,6 +1172,8 @@
}
}
+#endif /* FT_INT64 */
+
if ( area > 0 )
return FT_ORIENTATION_POSTSCRIPT;
else if ( area < 0 )
diff --git a/thirdparty/freetype/src/base/ftstroke.c b/thirdparty/freetype/src/base/ftstroke.c
index e599ecaf46f..cc93265b0ba 100644
--- a/thirdparty/freetype/src/base/ftstroke.c
+++ b/thirdparty/freetype/src/base/ftstroke.c
@@ -656,12 +656,11 @@
FT_UInt num_contours = 0;
FT_UInt count = border->num_points;
- FT_Vector* point = border->points;
FT_Byte* tags = border->tags;
FT_Int in_contour = 0;
- for ( ; count > 0; count--, num_points++, point++, tags++ )
+ for ( ; count > 0; count--, num_points++, tags++ )
{
if ( tags[0] & FT_STROKE_TAG_BEGIN )
{
@@ -1875,6 +1874,10 @@
goto Exit;
}
+ /* don't try to end the path if no segments have been generated */
+ if ( stroker->first_point )
+ goto Exit;
+
if ( stroker->subpath_open )
{
FT_StrokeBorder right = stroker->borders;
@@ -2095,15 +2098,14 @@
tags = outline->tags + first;
tag = FT_CURVE_TAG( tags[0] );
- /* A contour cannot start with a cubic control point! */
- if ( tag == FT_CURVE_TAG_CUBIC )
- goto Invalid_Outline;
-
/* check first point to determine origin */
- if ( tag == FT_CURVE_TAG_CONIC )
+ if ( tag != FT_CURVE_TAG_ON )
{
- /* First point is conic control. Yes, this happens. */
- if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
+ FT_Int last_tag = FT_CURVE_TAG( outline->tags[last] );
+
+
+ /* First point is a control point. Yes, this happens. */
+ if ( last_tag == FT_CURVE_TAG_ON )
{
/* start at last point if it is on the curve */
v_start = v_last;
@@ -2111,8 +2113,11 @@
}
else
{
- /* if both first and last points are conic, */
- /* start at their middle */
+ if ( last_tag != tag )
+ goto Invalid_Outline;
+
+ /* if both first and last points are controls, */
+ /* start at their middle */
v_start.x = ( v_start.x + v_last.x ) / 2;
v_start.y = ( v_start.y + v_last.y ) / 2;
}
@@ -2206,13 +2211,31 @@
if ( point <= limit )
{
FT_Vector vec;
+ FT_Vector v_middle;
+ tag = FT_CURVE_TAG( tags[0] );
vec = point[0];
+ if ( tag == FT_CURVE_TAG_CUBIC )
+ {
+ v_middle.x = ( vec2.x + vec.x ) / 2;
+ v_middle.y = ( vec2.y + vec.y ) / 2;
+
+ vec = v_middle;
+ }
+ else if ( tag != FT_CURVE_TAG_ON )
+ goto Invalid_Outline;
+
error = FT_Stroker_CubicTo( stroker, &vec1, &vec2, &vec );
if ( error )
goto Exit;
+
+ if ( tag == FT_CURVE_TAG_CUBIC )
+ {
+ point--;
+ tags--;
+ }
continue;
}
@@ -2226,13 +2249,9 @@
if ( error )
goto Exit;
- /* don't try to end the path if no segments have been generated */
- if ( !stroker->first_point )
- {
- error = FT_Stroker_EndSubPath( stroker );
- if ( error )
- goto Exit;
- }
+ error = FT_Stroker_EndSubPath( stroker );
+ if ( error )
+ goto Exit;
}
return FT_Err_Ok;
diff --git a/thirdparty/freetype/src/cff/cffdrivr.c b/thirdparty/freetype/src/cff/cffdrivr.c
index 18c7b879048..935548ea647 100644
--- a/thirdparty/freetype/src/cff/cffdrivr.c
+++ b/thirdparty/freetype/src/cff/cffdrivr.c
@@ -40,6 +40,10 @@
#include <freetype/internal/services/svmetric.h>
#endif
+#ifdef TT_CONFIG_OPTION_VARC
+#include <freetype/internal/services/svvarc.h>
+#endif
+
#include "cfferrs.h"
#include <freetype/internal/services/svfntfmt.h>
@@ -1085,6 +1089,111 @@
#endif
+#ifdef TT_CONFIG_OPTION_VARC
+
+ /*
+ * VARC SERVICE
+ *
+ * The implementation belongs to the `truetype' module. The CFF
+ * driver only forwards calls for OpenType/CFF faces, in the same way
+ * that it forwards variation-font operations.
+ */
+
+ FT_CALLBACK_DEF( FT_Error )
+ cff_varc_init_axes( FT_Face face,
+ FT_UInt axis_count )
+ {
+ CFF_Face cffface = (CFF_Face)face;
+ FT_Service_VARC varc = (FT_Service_VARC)cffface->tt_varc;
+
+
+ return varc && varc->init_axes
+ ? varc->init_axes( face, axis_count )
+ : FT_THROW( Missing_Module );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ cff_varc_load( FT_Face face,
+ FT_Stream stream )
+ {
+ CFF_Face cffface = (CFF_Face)face;
+ FT_Service_VARC varc = (FT_Service_VARC)cffface->tt_varc;
+
+
+ return varc ? varc->load( face, stream )
+ : FT_THROW( Missing_Module );
+ }
+
+
+ FT_CALLBACK_DEF( void )
+ cff_varc_done( FT_Face face )
+ {
+ CFF_Face cffface = (CFF_Face)face;
+ FT_Service_VARC varc = (FT_Service_VARC)cffface->tt_varc;
+
+
+ if ( varc )
+ varc->done( face );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Bool )
+ cff_varc_has_glyph( FT_Face face,
+ FT_UInt glyph_index )
+ {
+ CFF_Face cffface = (CFF_Face)face;
+ FT_Service_VARC varc = (FT_Service_VARC)cffface->tt_varc;
+
+
+ return FT_BOOL( varc && varc->has_glyph( face, glyph_index ) );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ cff_varc_load_glyph( FT_Face face,
+ FT_GlyphSlot glyph_slot,
+ FT_UInt glyph_index,
+ FT_Int32 load_flags )
+ {
+ CFF_Face cffface = (CFF_Face)face;
+ FT_Service_VARC varc = (FT_Service_VARC)cffface->tt_varc;
+ CFF_Font cff = (CFF_Font)cffface->extra.data;
+
+ FT_Error error;
+
+
+ if ( !varc )
+ return FT_THROW( Missing_Module );
+
+ if ( !FT_HAS_MULTIPLE_MASTERS( face ) &&
+ cff &&
+ cff->cff2 &&
+ cff->vstore.axisCount )
+ {
+ error = varc->init_axes( face, cff->vstore.axisCount );
+ if ( error )
+ return error;
+ }
+
+ return varc->load_glyph( face, glyph_slot,
+ glyph_index, load_flags );
+ }
+
+
+ FT_DEFINE_SERVICE_VARCREC(
+ cff_service_varc,
+
+ cff_varc_init_axes,
+ cff_varc_load,
+ cff_varc_done,
+ cff_varc_has_glyph,
+ cff_varc_load_glyph
+ )
+
+#endif /* TT_CONFIG_OPTION_VARC */
+
+
/*
* CFFLOAD SERVICE
*
@@ -1113,7 +1222,24 @@
/*************************************************************************/
/*************************************************************************/
-#if defined TT_CONFIG_OPTION_GX_VAR_SUPPORT
+#if defined( TT_CONFIG_OPTION_GX_VAR_SUPPORT ) && \
+ defined( TT_CONFIG_OPTION_VARC )
+ FT_DEFINE_SERVICEDESCREC11(
+ cff_services,
+
+ FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_CFF,
+ FT_SERVICE_ID_MULTI_MASTERS, &cff_service_multi_masters,
+ FT_SERVICE_ID_METRICS_VARIATIONS, &cff_service_metrics_variations,
+ FT_SERVICE_ID_VARC, &cff_service_varc,
+ FT_SERVICE_ID_POSTSCRIPT_INFO, &cff_service_ps_info,
+ FT_SERVICE_ID_POSTSCRIPT_FONT_NAME, &cff_service_ps_name,
+ FT_SERVICE_ID_GLYPH_DICT, &cff_service_glyph_dict,
+ FT_SERVICE_ID_TT_CMAP, &cff_service_get_cmap_info,
+ FT_SERVICE_ID_CID, &cff_service_cid_info,
+ FT_SERVICE_ID_PROPERTIES, &cff_service_properties,
+ FT_SERVICE_ID_CFF_LOAD, &cff_service_cff_load
+ )
+#elif defined( TT_CONFIG_OPTION_GX_VAR_SUPPORT )
FT_DEFINE_SERVICEDESCREC10(
cff_services,
@@ -1128,6 +1254,20 @@
FT_SERVICE_ID_PROPERTIES, &cff_service_properties,
FT_SERVICE_ID_CFF_LOAD, &cff_service_cff_load
)
+#elif defined( TT_CONFIG_OPTION_VARC )
+ FT_DEFINE_SERVICEDESCREC9(
+ cff_services,
+
+ FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_CFF,
+ FT_SERVICE_ID_VARC, &cff_service_varc,
+ FT_SERVICE_ID_POSTSCRIPT_INFO, &cff_service_ps_info,
+ FT_SERVICE_ID_POSTSCRIPT_FONT_NAME, &cff_service_ps_name,
+ FT_SERVICE_ID_GLYPH_DICT, &cff_service_glyph_dict,
+ FT_SERVICE_ID_TT_CMAP, &cff_service_get_cmap_info,
+ FT_SERVICE_ID_CID, &cff_service_cid_info,
+ FT_SERVICE_ID_PROPERTIES, &cff_service_properties,
+ FT_SERVICE_ID_CFF_LOAD, &cff_service_cff_load
+ )
#else
FT_DEFINE_SERVICEDESCREC8(
cff_services,
diff --git a/thirdparty/freetype/src/cff/cffgload.c b/thirdparty/freetype/src/cff/cffgload.c
index 0c986930cee..331c681b8ae 100644
--- a/thirdparty/freetype/src/cff/cffgload.c
+++ b/thirdparty/freetype/src/cff/cffgload.c
@@ -24,6 +24,10 @@
#include <freetype/ftoutln.h>
#include <freetype/ftdriver.h>
+#ifdef TT_CONFIG_OPTION_VARC
+#include <freetype/internal/services/svvarc.h>
+#endif
+
#include "cffload.h"
#include "cffgload.h"
@@ -235,7 +239,7 @@
return FT_THROW( Invalid_Argument );
}
}
- else if ( glyph_index >= cff->num_glyphs )
+ else if ( glyph_index >= (FT_ULong)face->root.num_glyphs )
return FT_THROW( Invalid_Argument );
#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
@@ -408,6 +412,76 @@
#endif /* FT_CONFIG_OPTION_SVG */
+#ifdef TT_CONFIG_OPTION_VARC
+
+ /* check for VARC glyphs */
+ {
+ FT_Service_VARC varc = (FT_Service_VARC)face->face_varc;
+
+
+ if ( face->varc &&
+ varc &&
+ varc->has_glyph( (FT_Face)face, glyph_index ) )
+ {
+ FT_TRACE3(( "Loading VARC glyph\n" ));
+
+ error = varc->load_glyph( (FT_Face)face,
+ (FT_GlyphSlot)glyph,
+ glyph_index,
+ load_flags );
+ if ( !error )
+ {
+ SFNT_Service sfnt = (SFNT_Service)face->sfnt;
+
+ FT_Short sb;
+ FT_UShort advanceX = 0;
+ FT_UShort advanceY = 0;
+ FT_BBox bbox;
+
+
+ FT_TRACE3(( "Successfully loaded VARC glyph\n" ));
+
+ /* The outline comes from the VARC table, so the advance and */
+ /* metrics must be supplied here (`get_metrics` applies */
+ /* HVAR/VVAR). */
+ sfnt->get_metrics( face, FALSE, glyph_index, &sb, &advanceX );
+ sfnt->get_metrics( face, TRUE, glyph_index, &sb, &advanceY );
+
+ glyph->root.linearHoriAdvance = advanceX;
+ glyph->root.linearVertAdvance = advanceY;
+
+ FT_Outline_Get_CBox( &glyph->root.outline, &bbox );
+ glyph->root.metrics.horiBearingX = bbox.xMin;
+ glyph->root.metrics.horiBearingY = bbox.yMax;
+ glyph->root.metrics.width = SUB_LONG( bbox.xMax, bbox.xMin );
+ glyph->root.metrics.height = SUB_LONG( bbox.yMax, bbox.yMin );
+
+ if ( ( load_flags & FT_LOAD_NO_SCALE ) || !size )
+ {
+ glyph->root.metrics.horiAdvance = advanceX;
+ glyph->root.metrics.vertAdvance = advanceY;
+ }
+ else
+ {
+ glyph->root.metrics.horiAdvance =
+ FT_MulFix( advanceX, size->root.metrics.x_scale );
+ glyph->root.metrics.vertAdvance =
+ FT_MulFix( advanceY, size->root.metrics.y_scale );
+ }
+
+ return FT_Err_Ok;
+ }
+
+ FT_TRACE3(( "Failed to load VARC glyph, falling back to CFF\n" ));
+ }
+ }
+
+#endif /* TT_CONFIG_OPTION_VARC */
+
+ /* A VARC-only glyph has no CFF2 fallback CharString. */
+ if ( glyph_index >= cff->num_glyphs )
+ return FT_THROW( Invalid_Argument );
+
/* top-level code ensures that FT_LOAD_NO_HINTING is set */
/* if FT_LOAD_NO_SCALE is active */
hinting = FT_BOOL( ( load_flags & FT_LOAD_NO_HINTING ) == 0 );
diff --git a/thirdparty/freetype/src/cff/cffload.c b/thirdparty/freetype/src/cff/cffload.c
index 9d067cfeeb8..cfd5317ff52 100644
--- a/thirdparty/freetype/src/cff/cffload.c
+++ b/thirdparty/freetype/src/cff/cffload.c
@@ -634,6 +634,12 @@
if ( error )
goto Exit;
+ if ( !bytes )
+ {
+ error = FT_THROW( Invalid_Table );
+ goto Exit;
+ }
+
if ( !FT_QALLOC( name, byte_len + 1 ) )
{
FT_MEM_COPY( name, bytes, byte_len );
diff --git a/thirdparty/freetype/src/cff/cffobjs.c b/thirdparty/freetype/src/cff/cffobjs.c
index c6ba35e2734..8efdd109f95 100644
--- a/thirdparty/freetype/src/cff/cffobjs.c
+++ b/thirdparty/freetype/src/cff/cffobjs.c
@@ -624,7 +624,13 @@
/* Note that this is only necessary for pure CFF and CEF fonts; */
/* SFNT based fonts use the `name' table instead. */
- cffface->num_glyphs = (FT_Long)cff->num_glyphs;
+ /* VARC can define glyphs beyond the CFF2 CharStrings INDEX. */
+#ifdef TT_CONFIG_OPTION_VARC
+ if ( !cff2 ||
+ !face->varc ||
+ cff->num_glyphs > face->max_profile.numGlyphs )
+#endif
+ cffface->num_glyphs = (FT_Long)cff->num_glyphs;
dict = &cff->top_font.font_dict;
diff --git a/thirdparty/freetype/src/cid/cidload.c b/thirdparty/freetype/src/cid/cidload.c
index 1a0e6b1561e..170ae2d1c7d 100644
--- a/thirdparty/freetype/src/cid/cidload.c
+++ b/thirdparty/freetype/src/cid/cidload.c
@@ -512,12 +512,14 @@
CID_FaceInfo cid = &face->cid;
FT_Memory memory = face->root.memory;
FT_Stream stream = face->cid_stream;
+ PSAux_Service psaux = (PSAux_Service)face->psaux;
FT_Error error;
+
FT_UInt n;
CID_Subrs subr;
FT_UInt max_offsets = 0;
- FT_ULong* offsets = NULL;
- PSAux_Service psaux = (PSAux_Service)face->psaux;
+ FT_ULong* offsets = NULL;
+ FT_ULong cum_len = 0;
if ( FT_NEW_ARRAY( face->subrs, cid->num_dicts ) )
@@ -584,6 +586,14 @@
/* now, compute the size of subrs charstrings, */
/* allocate, and read them */
data_len = offsets[num_subrs] - offsets[0];
+ cum_len += data_len;
+
+ if ( cum_len / 4 > stream->size )
+ {
+ FT_ERROR(( "cid_read_subrs: excessive overlaps\n" ));
+ error = FT_THROW( Invalid_File_Format );
+ goto Fail;
+ }
if ( FT_QNEW_ARRAY( subr->code, num_subrs + 1 ) ||
FT_QALLOC( subr->code[0], data_len ) )
diff --git a/thirdparty/freetype/src/hvf/hvf.c b/thirdparty/freetype/src/hvf/hvf.c
new file mode 100644
index 00000000000..a0e0a58ce10
--- /dev/null
+++ b/thirdparty/freetype/src/hvf/hvf.c
@@ -0,0 +1,27 @@
+/****************************************************************************
+ *
+ * hvf.c
+ *
+ * FreeType HVF driver component (body only).
+ *
+ * Copyright (C) 2025-2026 by
+ * Apple Inc.
+ * written by Deborah Goldsmith <[email protected]>
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+
+#define FT_MAKE_OPTION_SINGLE_OBJECT
+
+#include "hvfdrv.c"
+#include "hvfload.c"
+#include "hvfobjs.c"
+
+
+/* END */
diff --git a/thirdparty/freetype/src/hvf/hvfdrv.c b/thirdparty/freetype/src/hvf/hvfdrv.c
new file mode 100644
index 00000000000..b59cb55c93f
--- /dev/null
+++ b/thirdparty/freetype/src/hvf/hvfdrv.c
@@ -0,0 +1,723 @@
+/****************************************************************************
+ *
+ * hvfdrv.c
+ *
+ * HVF font driver implementation (body).
+ *
+ * Copyright (C) 2025-2026 by
+ * Apple Inc.
+ * written by Deborah Goldsmith <[email protected]>
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+
+#include <freetype/internal/ftdebug.h>
+#include <freetype/internal/ftstream.h>
+#include <freetype/internal/sfnt.h>
+#include <freetype/tttags.h>
+#include <freetype/internal/tttypes.h> /* For TT_Face */
+#include <freetype/internal/services/svfntfmt.h>
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+#include <freetype/ftmm.h>
+#include <freetype/internal/services/svmm.h>
+#include <freetype/internal/services/svmetric.h>
+#endif
+
+#include "hvfdrv.h"
+#include "hvfobjs.h"
+#include "hvfload.h"
+#include "hvferror.h"
+
+
+ /**************************************************************************
+ *
+ * The macro FT_COMPONENT is used in trace mode. It is an implicit
+ * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
+ * messages during execution.
+ */
+#undef FT_COMPONENT
+#define FT_COMPONENT hvfdrv
+
+
+#ifdef FT_CONFIG_OPTION_HVF
+
+ /**************************************************************************
+ *
+ * FACE MANAGEMENT
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * hvf_get_kerning
+ *
+ * @Description:
+ * Get kerning vector between two glyphs. Delegates to SFNT service
+ * since HVF fonts are TrueType/OpenType fonts.
+ *
+ * @Input:
+ * face ::
+ * A handle to the source face object.
+ *
+ * left_glyph ::
+ * The index of the left glyph in the kern pair.
+ *
+ * right_glyph ::
+ * The index of the right glyph in the kern pair.
+ *
+ * @Output:
+ * kerning ::
+ * The kerning vector in font units.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ hvf_get_kerning( FT_Face face,
+ FT_UInt left_glyph,
+ FT_UInt right_glyph,
+ FT_Vector* kerning )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ SFNT_Service sfnt = (SFNT_Service)tt_face->sfnt;
+
+
+ kerning->x = 0;
+ kerning->y = 0;
+
+ if ( sfnt )
+ {
+ /* Use 'kern' table if available since that can be faster; otherwise */
+ /* use GPOS kerning pairs if available. */
+ if ( tt_face->kern_avail_bits )
+ kerning->x = sfnt->get_kerning( tt_face,
+ left_glyph,
+ right_glyph );
+#ifdef TT_CONFIG_OPTION_GPOS_KERNING
+ else if ( tt_face->num_gpos_lookups_kerning )
+ kerning->x = sfnt->get_gpos_kerning( tt_face,
+ left_glyph,
+ right_glyph );
+#endif
+ }
+
+ return FT_Err_Ok;
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * hvf_get_advances
+ *
+ * @Description:
+ * Get advance widths/heights for a range of glyphs. Delegates to
+ * TrueType infrastructure since HVF fonts are TrueType/OpenType fonts.
+ *
+ * @Input:
+ * face ::
+ * A handle to the source face object.
+ *
+ * start ::
+ * The first glyph index.
+ *
+ * count ::
+ * The number of advance values to retrieve.
+ *
+ * flags ::
+ * Loading flags (vertical/horizontal layout).
+ *
+ * @Output:
+ * advances ::
+ * The advance values in 16.16 format.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ hvf_get_advances( FT_Face face,
+ FT_UInt start,
+ FT_UInt count,
+ FT_Int32 flags,
+ FT_Fixed *advances )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Bool horz;
+ FT_UInt nn;
+
+
+ if ( !FT_IS_SFNT( face ) )
+ return FT_THROW( Unimplemented_Feature );
+
+ horz = !( flags & FT_LOAD_VERTICAL_LAYOUT );
+
+ if ( horz )
+ {
+ /* Check availability of horizontal metrics. */
+ if ( !tt_face->horizontal.number_Of_HMetrics )
+ return FT_THROW( Unimplemented_Feature );
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+ /* No fast retrieval for blended MM fonts without 'HVAR' table. */
+ if ( ( FT_IS_NAMED_INSTANCE( face ) || FT_IS_VARIATION( face ) ) &&
+ !( tt_face->variation_support & TT_FACE_FLAG_VAR_HADVANCE ) )
+ return FT_THROW( Unimplemented_Feature );
+#endif
+ }
+ else /* vertical */
+ {
+ /* Check whether we have data from the 'vmtx' table at all. */
+ if ( !tt_face->vertical_info )
+ return FT_THROW( Unimplemented_Feature );
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+ /* No fast retrieval for blended MM fonts without 'VVAR' table. */
+ if ( ( FT_IS_NAMED_INSTANCE( face ) || FT_IS_VARIATION( face ) ) &&
+ !( tt_face->variation_support & TT_FACE_FLAG_VAR_VADVANCE ) )
+ return FT_THROW( Unimplemented_Feature );
+#endif
+ }
+
+ /* Proceed to fast advances. */
+ for ( nn = 0; nn < count; nn++ )
+ {
+ FT_UShort aw;
+ FT_Short dummy;
+
+
+ ( (SFNT_Service)tt_face->sfnt )->get_metrics( tt_face,
+ !horz,
+ start + nn,
+ &dummy,
+ &aw );
+
+ FT_TRACE5(( " idx %u: advance %s %d font unit%s\n",
+ start + nn,
+ horz ? "width" : "height",
+ aw,
+ aw == 1 ? "" : "s" ));
+ advances[nn] = aw;
+ }
+
+ return FT_Err_Ok;
+ }
+
+
+ /**************************************************************************
+ *
+ * SERVICE DEFINITIONS
+ *
+ */
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+
+ /* Service delegation functions following CFF module pattern. */
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_set_mm_blend( FT_Face face, /* HVF_Face */
+ FT_UInt num_coords,
+ FT_Fixed* coords )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+ HVF_Face hvf_face = (HVF_Face)face;
+
+ FT_Error error;
+
+
+ /* Call the underlying service. */
+ error = mm->set_mm_blend( face, num_coords, coords );
+
+ /* error == -1 means "no change"; early exit. */
+ if ( error == -1 )
+ return FT_Err_Ok;
+
+ /* If successful, refresh our cached HVF coordinates. */
+ if ( !error )
+ {
+ FT_Error refresh_error = hvf_refresh_axis_coordinates( hvf_face );
+
+
+#ifdef FT_DEBUG_LEVEL_TRACE
+ if ( refresh_error )
+ {
+ /* Don't fail the entire operation, just log the issue. */
+ FT_TRACE3(( "hvf_set_mm_blend:"
+ " failed to refresh HVF coordinates (error %d)\n",
+ refresh_error ));
+ }
+#else
+ FT_UNUSED( refresh_error );
+#endif
+ }
+
+ return error;
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_get_mm_blend( FT_Face face, /* HVF_Face */
+ FT_UInt num_coords,
+ FT_Fixed* coords )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ return mm->get_mm_blend( face, num_coords, coords );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_get_mm_var( FT_Face face, /* HVF_Face */
+ FT_MM_Var** master )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ return mm->get_mm_var( face, master );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_set_var_design( FT_Face face, /* HVF_Face */
+ FT_UInt num_coords,
+ FT_Fixed* coords )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+ HVF_Face hvf_face = (HVF_Face)face;
+
+ FT_Error error;
+
+
+ /* Call the underlying service. */
+ error = mm->set_var_design( face, num_coords, coords );
+
+ /* error == -1 means "no change"; early exit. */
+ if ( error == -1 )
+ return FT_Err_Ok;
+
+ /* If successful, refresh our cached HVF coordinates. */
+ if ( !error || error == -2 )
+ {
+ FT_Error refresh_error = hvf_refresh_axis_coordinates( hvf_face );
+
+
+#ifdef FT_DEBUG_LEVEL_TRACE
+ if ( refresh_error )
+ {
+ /* Don't fail the entire operation, just log the issue. */
+ FT_TRACE3(( "hvf_set_var_design:"
+ " failed to refresh HVF coordinates (error %d)\n",
+ refresh_error ));
+ }
+#else
+ FT_UNUSED( refresh_error );
+#endif
+ }
+
+ return error;
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_get_var_design( FT_Face face, /* HVF_Face */
+ FT_UInt num_coords,
+ FT_Fixed* coords )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ return mm->get_var_design( face, num_coords, coords );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_set_named_instance( FT_Face face, /* HVF_Face */
+ FT_UInt instance_index )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+ HVF_Face hvf_face = (HVF_Face)face;
+
+ FT_Error error;
+
+
+ /* Call the underlying service. */
+ error = mm->set_named_instance( face, instance_index );
+
+ /* error == -1 means "no change"; early exit. */
+ if ( error == -1 )
+ return FT_Err_Ok;
+
+ /* If successful, refresh our cached HVF coordinates. */
+ if ( !error )
+ {
+ FT_Error refresh_error = hvf_refresh_axis_coordinates( hvf_face );
+
+
+#ifdef FT_DEBUG_LEVEL_TRACE
+ if ( refresh_error )
+ {
+ /* Don't fail the entire operation, just log the issue. */
+ FT_TRACE3(( "hvf_set_named_instance:"
+ " failed to refresh HVF coordinates (error %d)\n",
+ refresh_error ));
+ }
+#else
+ FT_UNUSED( refresh_error );
+#endif
+ }
+
+ return error;
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_get_default_named_instance( FT_Face face, /* HVF_Face */
+ FT_UInt *instance_index )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ return mm->get_default_named_instance( face, instance_index );
+ }
+
+
+ FT_CALLBACK_DEF( void )
+ hvf_construct_ps_name( FT_Face face ) /* HVF_Face */
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ mm->construct_ps_name( face );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_load_delta_set_index_mapping( FT_Face face, /* HVF_Face */
+ FT_ULong offset,
+ GX_DeltaSetIdxMap map,
+ GX_ItemVarStore itemStore,
+ FT_ULong table_len )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ return mm->load_delta_set_idx_map( face, offset, map,
+ itemStore, table_len );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_load_item_variation_store( FT_Face face, /* HVF_Face */
+ FT_ULong offset,
+ GX_ItemVarStore itemStore )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ return mm->load_item_var_store( face, offset, itemStore );
+ }
+
+
+ FT_CALLBACK_DEF( FT_ItemVarDelta )
+ hvf_get_item_delta( FT_Face face, /* HVF_Face */
+ GX_ItemVarStore itemStore,
+ FT_UInt outerIndex,
+ FT_UInt innerIndex )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ return mm->get_item_delta( face, itemStore, outerIndex, innerIndex );
+ }
+
+
+ FT_CALLBACK_DEF( void )
+ hvf_done_item_variation_store( FT_Face face, /* HVF_Face */
+ GX_ItemVarStore itemStore )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ mm->done_item_var_store( face, itemStore );
+ }
+
+
+ FT_CALLBACK_DEF( void )
+ hvf_done_delta_set_index_map( FT_Face face, /* HVF_Face */
+ GX_DeltaSetIdxMap deltaSetIdxMap )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ mm->done_delta_set_idx_map( face, deltaSetIdxMap );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_get_var_blend( FT_Face face, /* HVF_Face */
+ FT_UInt* num_coords,
+ FT_Fixed** coords,
+ FT_Fixed** normalizedcoords,
+ FT_MM_Var** mm_var )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ return mm->get_var_blend( face, num_coords, coords,
+ normalizedcoords, mm_var );
+ }
+
+
+ FT_CALLBACK_DEF( void )
+ hvf_done_blend( FT_Face face ) /* HVF_Face */
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MultiMasters mm = (FT_Service_MultiMasters)tt_face->mm;
+
+
+ mm->done_blend( face );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_hadvance_adjust( FT_Face face, /* HVF_Face */
+ FT_UInt gindex,
+ FT_Int *avalue )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MetricsVariations var = (FT_Service_MetricsVariations)
+ tt_face->tt_var;
+
+
+ return var->hadvance_adjust( face, gindex, avalue );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ hvf_vadvance_adjust( FT_Face face, /* HVF_Face */
+ FT_UInt gindex,
+ FT_Int *avalue )
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MetricsVariations var = (FT_Service_MetricsVariations)
+ tt_face->tt_var;
+
+
+ return var->vadvance_adjust( face, gindex, avalue );
+ }
+
+
+ FT_CALLBACK_DEF( void )
+ hvf_apply_mvar( FT_Face face ) /* HVF_Face */
+ {
+ TT_Face tt_face = (TT_Face)face;
+ FT_Service_MetricsVariations var = (FT_Service_MetricsVariations)
+ tt_face->tt_var;
+
+
+ var->metrics_adjust( face );
+ }
+
+
+ FT_CALLBACK_DEF( void )
+ hvf_size_reset_height( FT_Size size )
+ {
+ /* Delegate to size's face services. */
+ TT_Face tt_face = (TT_Face)size->face;
+ FT_Service_MetricsVariations var = (FT_Service_MetricsVariations)
+ tt_face->tt_var;
+
+
+ if ( var->size_reset )
+ var->size_reset( size );
+ }
+
+
+ FT_DEFINE_SERVICE_MULTIMASTERSREC(
+ hvf_service_gx_multi_masters,
+
+ NULL, /* FT_Get_MM_Func get_mm */
+ NULL, /* FT_Set_MM_Design_Func set_mm_design */
+ hvf_set_mm_blend, /* FT_Set_MM_Blend_Func set_mm_blend */
+ hvf_get_mm_blend, /* FT_Get_MM_Blend_Func get_mm_blend */
+ hvf_get_mm_var, /* FT_Get_MM_Var_Func get_mm_var */
+ hvf_set_var_design, /* FT_Set_Var_Design_Func set_var_design */
+ hvf_get_var_design, /* FT_Get_Var_Design_Func get_var_design */
+ hvf_set_named_instance,
+ /* FT_Set_Named_Instance_Func set_named_instance */
+ hvf_get_default_named_instance,
+ /* FT_Get_Default_Named_Instance_Func get_default_named_instance */
+ NULL, /* FT_Set_MM_WeightVector_Func set_mm_weightvector */
+ NULL, /* FT_Get_MM_WeightVector_Func get_mm_weightvector */
+
+ hvf_construct_ps_name,
+ /* FT_Construct_PS_Name_Func construct_ps_name */
+ hvf_load_delta_set_index_mapping,
+ /* FT_Var_Load_Delta_Set_Idx_Map_Func load_delta_set_idx_map */
+ hvf_load_item_variation_store,
+ /* FT_Var_Load_Item_Var_Store_Func load_item_variation_store */
+ hvf_get_item_delta,
+ /* FT_Var_Get_Item_Delta_Func get_item_delta */
+ hvf_done_item_variation_store,
+ /* FT_Var_Done_Item_Var_Store_Func done_item_variation_store */
+ hvf_done_delta_set_index_map,
+ /* FT_Var_Done_Delta_Set_Idx_Map_Func done_delta_set_index_map */
+ hvf_get_var_blend, /* FT_Get_Var_Blend_Func get_var_blend */
+ hvf_done_blend /* FT_Done_Blend_Func done_blend */
+ )
+
+
+ FT_DEFINE_SERVICE_METRICSVARIATIONSREC(
+ hvf_service_metrics_variations,
+
+ hvf_hadvance_adjust, /* FT_HAdvance_Adjust_Func hadvance_adjust */
+ NULL, /* FT_LSB_Adjust_Func lsb_adjust */
+ NULL, /* FT_RSB_Adjust_Func rsb_adjust */
+
+ hvf_vadvance_adjust, /* FT_VAdvance_Adjust_Func vadvance_adjust */
+ NULL, /* FT_TSB_Adjust_Func tsb_adjust */
+ NULL, /* FT_BSB_Adjust_Func bsb_adjust */
+ NULL, /* FT_VOrg_Adjust_Func vorg_adjust */
+
+ hvf_apply_mvar, /* FT_Metrics_Adjust_Func metrics_adjust */
+ hvf_size_reset_height /* FT_Size_Reset_Func size_reset */
+ )
+
+#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+ FT_DEFINE_SERVICEDESCREC3(
+ hvf_services,
+
+ FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_HVF,
+ FT_SERVICE_ID_MULTI_MASTERS, &hvf_service_gx_multi_masters,
+ FT_SERVICE_ID_METRICS_VARIATIONS, &hvf_service_metrics_variations )
+#else
+ FT_DEFINE_SERVICEDESCREC1(
+ hvf_services,
+
+ FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_HVF )
+#endif
+
+
+ FT_CALLBACK_DEF( FT_Module_Interface )
+ hvf_get_interface( FT_Module driver, /* HVF_Driver */
+ const char* hvf_interface )
+ {
+ FT_Library library;
+ FT_Module_Interface result;
+ FT_Module sfntd;
+ SFNT_Service sfnt;
+
+
+ FT_TRACE5(( "hvf_get_interface: Looking up service %s\n",
+ hvf_interface ));
+
+ result = ft_service_list_lookup( hvf_services, hvf_interface );
+ if ( result )
+ return result;
+
+ FT_TRACE3(( "hvf_get_interface: Did not find service %s; falling back\n",
+ hvf_interface ));
+
+ if ( !driver )
+ return NULL;
+
+ library = driver->library;
+ if ( !library )
+ return NULL;
+
+ /* Only return the default interface from the SFNT module. */
+ sfntd = FT_Get_Module( library, "sfnt" );
+ if ( sfntd )
+ {
+ sfnt = (SFNT_Service)( sfntd->clazz->module_interface );
+ if ( sfnt )
+ return sfnt->get_interface( driver, hvf_interface );
+ }
+
+ return NULL;
+ }
+
+#endif /* FT_CONFIG_OPTION_HVF */
+
+
+ /**************************************************************************
+ *
+ * DRIVER INTERFACE
+ *
+ */
+
+#ifdef FT_CONFIG_OPTION_HVF
+#define PUT_HVF_MODULE( a ) a
+#else
+#define PUT_HVF_MODULE( a ) NULL
+#endif
+
+ FT_DEFINE_DRIVER(
+ hvf_driver_class,
+
+ FT_MODULE_FONT_DRIVER |
+ FT_MODULE_DRIVER_SCALABLE |
+ FT_MODULE_DRIVER_HAS_HINTER,
+
+ sizeof ( FT_DriverRec ),
+ "hvf", /* driver name */
+ 0x10000L, /* driver version == 1.0 */
+ 0x20000L, /* driver requires FreeType 2.0 or above */
+
+ NULL, /* module-specific interface */
+
+ NULL, /* FT_Module_Constructor module_init */
+ NULL, /* FT_Module_Destructor module_done */
+ PUT_HVF_MODULE( hvf_get_interface ),
+ /* FT_Module_Requester get_interface */
+
+ sizeof ( HVF_FaceRec ),
+ sizeof ( FT_SizeRec ),
+ sizeof ( FT_GlyphSlotRec ),
+
+ hvf_face_init, /* FT_Face_InitFunc init_face */
+ PUT_HVF_MODULE( hvf_face_done ), /* FT_Face_DoneFunc done_face */
+ NULL, /* FT_Size_InitFunc init_size */
+ NULL, /* FT_Size_DoneFunc done_size */
+ NULL, /* FT_Slot_InitFunc init_slot */
+ NULL, /* FT_Slot_DoneFunc done_slot */
+
+ hvf_slot_load_glyph, /* FT_Slot_LoadFunc load_glyph */
+
+ PUT_HVF_MODULE( hvf_get_kerning ),
+ /* FT_Face_GetKerningFunc get_kerning */
+ NULL, /* FT_Face_AttachFunc attach_file */
+ PUT_HVF_MODULE( hvf_get_advances ),
+ /* FT_Face_GetAdvancesFunc get_advances */
+
+ NULL, /* FT_Size_RequestFunc request_size */
+ NULL /* FT_Size_SelectFunc select_size */
+ )
+
+
+/* END */
diff --git a/thirdparty/freetype/src/hvf/hvfdrv.h b/thirdparty/freetype/src/hvf/hvfdrv.h
new file mode 100644
index 00000000000..c27d7fcaa19
--- /dev/null
+++ b/thirdparty/freetype/src/hvf/hvfdrv.h
@@ -0,0 +1,37 @@
+/****************************************************************************
+ *
+ * hvfdrv.h
+ *
+ * Apple Hierarchical Variable Font (HVF) driver interface
+ * (specification only).
+ *
+ * Copyright (C) 2025-2026 by
+ * Apple Inc.
+ * written by Deborah Goldsmith <[email protected]>
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+
+#ifndef HVFDRV_H_
+#define HVFDRV_H_
+
+
+#include <freetype/internal/ftdrv.h>
+
+
+FT_BEGIN_HEADER
+
+ FT_DECLARE_DRIVER( hvf_driver_class )
+
+FT_END_HEADER
+
+#endif /* HVFDRV_H_ */
+
+
+/* END */
diff --git a/thirdparty/freetype/src/hvf/hvferror.h b/thirdparty/freetype/src/hvf/hvferror.h
new file mode 100644
index 00000000000..6d474d292cf
--- /dev/null
+++ b/thirdparty/freetype/src/hvf/hvferror.h
@@ -0,0 +1,43 @@
+/****************************************************************************
+ *
+ * hvferror.h
+ *
+ * HVF error codes (specification only).
+ *
+ * Copyright (C) 2025-2026 by
+ * Apple Inc.
+ * written by Deborah Goldsmith <[email protected]>
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+
+ /**************************************************************************
+ *
+ * This file is used to define the HVF error enumeration constants.
+ *
+ */
+
+#ifndef HVFERROR_H_
+#define HVFERROR_H_
+
+#include <freetype/ftmoderr.h>
+
+#undef FTERRORS_H_
+
+#undef FT_ERR_PREFIX
+#define FT_ERR_PREFIX HVF_Err_
+#define FT_ERR_BASE FT_Mod_Err_HVF
+
+
+#include <freetype/fterrors.h>
+
+#endif /* HVFERROR_H_ */
+
+
+/* END */
diff --git a/thirdparty/freetype/src/hvf/hvfload.c b/thirdparty/freetype/src/hvf/hvfload.c
new file mode 100644
index 00000000000..a02d363c1da
--- /dev/null
+++ b/thirdparty/freetype/src/hvf/hvfload.c
@@ -0,0 +1,610 @@
+/****************************************************************************
+ *
+ * hvfload.c
+ *
+ * HVF glyph loading (body).
+ *
+ * Copyright (C) 2025-2026 by
+ * Apple Inc.
+ * written by Deborah Goldsmith <[email protected]>
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+
+#include <freetype/internal/ftdebug.h>
+#include <freetype/internal/ftstream.h>
+#include <freetype/internal/ftgloadr.h>
+#include <freetype/ftoutln.h>
+
+#include "hvfload.h"
+#include "hvfobjs.h"
+#include "hvferror.h"
+
+
+#undef FT_COMPONENT
+#define FT_COMPONENT hvfload
+
+
+#ifdef FT_CONFIG_OPTION_HVF
+
+ /**************************************************************************
+ *
+ * HVF Coordinate Conversion Macros
+ *
+ */
+
+ /* Convert HVF coordinates to FreeType 26.6 fixed point */
+ /* using a pre-calculated scale factor. */
+#define HVF_COORD_TO_FIXED( coord, scale_fixed ) \
+ ( (FT_F26Dot6)( (coord) * (scale_fixed) ) )
+
+ /* Pre-calculate scale factor for efficient coordinate conversion. */
+ /* FreeType scale factors are 16.16 fixed point; convert to floating */
+ /* point for HVF. */
+#define FT_SCALE_TO_HVF_SCALE_FIXED( scale, apply_scaling ) \
+ ( (apply_scaling) ? (HVFXYCoord)(scale) / 65536.0 \
+ : (HVFXYCoord)64.0 )
+
+ /* Cache management: clear cache after this many glyph loads. */
+ /* TODO: Future enhancement: make this a settable property on */
+ /* the HVF driver. */
+#define HVF_CACHE_CLEAR_COUNT 17
+
+
+ /**************************************************************************
+ *
+ * HVF axis values setup with TrueType integration
+ *
+ */
+ static FT_Error
+ hvf_set_variation_axes( HVF_Face face )
+ {
+ FT_Error error = FT_Err_Ok;
+
+ int axis_count;
+
+ FT_UInt i;
+ FT_UInt max_axes;
+
+ HVFAxisValue axis_value;
+
+
+ /* Only the HVF API calls need `@available` protection. */
+ if ( HVF_IS_AVAILABLE )
+ {
+ /* Get HVF axis count for current part. */
+ axis_count =
+ HVF_render_part_axis_count( (HVFPartRenderer*)face->renderer );
+ if ( axis_count <= 0 )
+ {
+ /* No axes for this part - this is normal for non-variable glyphs. */
+ return FT_Err_Ok;
+ }
+
+ /* Calculate maximum axes to set - only set axes that have */
+ /* pre-stored coordinates. */
+ /* */
+ /* Note: `HVF_set_render_part` already sets all axes to */
+ /* default (0.0), so we only need to override axes */
+ /* that have actual variation coordinates. */
+ max_axes = 0;
+ if ( face->axis_coords && face->num_axes > 0 )
+ {
+ max_axes = face->num_axes;
+ if ( max_axes > (FT_UInt)axis_count )
+ max_axes = (FT_UInt)axis_count;
+ }
+
+ /* Set only axes with pre-stored coordinates */
+ /* (`HVF_set_render_part` handles defaults). */
+ for ( i = 0; i < max_axes; i++ )
+ {
+ int hvf_result;
+
+
+ /* Use pre-converted HVF axis coordinate directly */
+ /* (no conversion needed). */
+ axis_value = ((HVFAxisValue*)face->axis_coords)[i];
+ FT_TRACE5(( "hvf_set_variation_axes:"
+ " axis %u = %f (pre-converted HVF coord)\n",
+ i, axis_value ));
+
+ /* Set the axis value in HVF renderer. */
+ hvf_result = HVF_set_axis_value( (HVFPartRenderer*)face->renderer,
+ (int)i,
+ axis_value );
+#ifdef FT_DEBUG_LEVEL_TRACE
+ if ( hvf_result != 0 )
+ {
+ /* Continue with other axes even if one fails. */
+ FT_TRACE1(( "hvf_set_variation_axes:"
+ " HVF_set_axis_value failed for axis %u (error %d)\n",
+ i, hvf_result ));
+ }
+#else
+ FT_UNUSED( hvf_result );
+#endif
+ }
+
+ /* Remaining axes already set to defaults by `HVF_set_render_part` - */
+ /* no action needed. */
+ FT_TRACE3(( "hvf_set_variation_axes:"
+ " configured %u of %d axis value%s\n"
+ " "
+ " using pre-converted HVF coordinates\n",
+ max_axes, axis_count, axis_count == 1 ? "" : "s" ));
+ }
+ else
+ {
+ FT_TRACE3(( "hvf_set_variation_axes:"
+ " HVF not available at runtime\n" ));
+ return FT_THROW( Unimplemented_Feature );
+ }
+
+ return error;
+ }
+
+
+ /**************************************************************************
+ *
+ * HVF instruction callback - using `FT_GlyphLoader`
+ *
+ */
+ static HVFPartRenderAction
+ hvf_render_callback( HVFPartRenderInstruction instruction,
+ const HVFPartRenderParameters* params,
+ void* user_data )
+ {
+ HVF_RenderContext* ctx = (HVF_RenderContext*)user_data;
+
+ FT_Error error;
+
+
+ switch ( instruction )
+ {
+ case HVFPartRenderInstructionBeginPart:
+ FT_TRACE5(( "hvf_render: BeginPart (part %zu)\n",
+ params->beginPart.partInfo.partId ));
+ return HVFPartRenderActionContinue;
+
+ case HVFPartRenderInstructionBeginPath:
+ FT_TRACE5(( "hvf_render: BeginPath\n" ));
+ ctx->path_begun = 1;
+ return HVFPartRenderActionContinue;
+
+ case HVFPartRenderInstructionAddPoint:
+ {
+ FT_Int n;
+
+
+ /* Move-to or line-to point with pre-calculated scaling factors. */
+ error = FT_GLYPHLOADER_CHECK_POINTS( ctx->loader, 1, 0 );
+ if ( error )
+ return HVFPartRenderActionStop;
+
+ n = ctx->outline->n_points;
+
+ ctx->outline->points[n].x =
+ HVF_COORD_TO_FIXED( params->addPoint.pt.x, ctx->x_scale_fixed );
+ ctx->outline->points[n].y =
+ HVF_COORD_TO_FIXED( params->addPoint.pt.y, ctx->y_scale_fixed );
+ ctx->outline->tags[n] = FT_CURVE_TAG_ON;
+ ctx->outline->n_points++;
+
+ FT_TRACE5(( "hvf_render: AddPoint (%.2f, %.2f)\n",
+ (double)ctx->outline->points[n].x / 64,
+ (double)ctx->outline->points[n].y / 64 ));
+ return HVFPartRenderActionContinue;
+ }
+
+ case HVFPartRenderInstructionAddQuad:
+ {
+ FT_Int n;
+
+
+ /* Quadratic curve with pre-calculated scaling factors. */
+ error = FT_GLYPHLOADER_CHECK_POINTS( ctx->loader, 2, 0 );
+ if ( error )
+ return HVFPartRenderActionStop;
+
+ n = ctx->outline->n_points;
+
+ /* Control point. */
+ ctx->outline->points[n].x =
+ HVF_COORD_TO_FIXED( params->addQuad.offpt.x, ctx->x_scale_fixed );
+ ctx->outline->points[n].y =
+ HVF_COORD_TO_FIXED( params->addQuad.offpt.y, ctx->y_scale_fixed );
+ ctx->outline->tags[n] = FT_CURVE_TAG_CONIC;
+
+ /* End point. */
+ ctx->outline->points[n + 1].x =
+ HVF_COORD_TO_FIXED( params->addQuad.onpt.x, ctx->x_scale_fixed );
+ ctx->outline->points[n + 1].y =
+ HVF_COORD_TO_FIXED( params->addQuad.onpt.y, ctx->y_scale_fixed );
+ ctx->outline->tags[n + 1] = FT_CURVE_TAG_ON;
+
+ ctx->outline->n_points += 2;
+
+ FT_TRACE5(( "hvf_render: AddQuad (%.2f,%.2f) (%.2f,%.2f)\n",
+ (double)ctx->outline->points[n].x / 64,
+ (double)ctx->outline->points[n].y / 64,
+ (double)ctx->outline->points[n + 1].x / 64,
+ (double)ctx->outline->points[n + 1].y / 64 ));
+ return HVFPartRenderActionContinue;
+ }
+
+ case HVFPartRenderInstructionAddCubic:
+ {
+ FT_Int n;
+
+
+ /* Cubic curve with pre-calculated scaling factors. */
+ error = FT_GLYPHLOADER_CHECK_POINTS( ctx->loader, 3, 0 );
+ if ( error )
+ return HVFPartRenderActionStop;
+
+ n = ctx->outline->n_points;
+
+ /* First control point */
+ ctx->outline->points[n].x =
+ HVF_COORD_TO_FIXED( params->addCubic.cp1.x, ctx->x_scale_fixed );
+ ctx->outline->points[n].y =
+ HVF_COORD_TO_FIXED( params->addCubic.cp1.y, ctx->y_scale_fixed );
+ ctx->outline->tags[n] = FT_CURVE_TAG_CUBIC;
+
+ /* Second control point */
+ ctx->outline->points[n + 1].x =
+ HVF_COORD_TO_FIXED( params->addCubic.cp2.x, ctx->x_scale_fixed );
+ ctx->outline->points[n + 1].y =
+ HVF_COORD_TO_FIXED( params->addCubic.cp2.y, ctx->y_scale_fixed );
+ ctx->outline->tags[n + 1] = FT_CURVE_TAG_CUBIC;
+
+ /* End point */
+ ctx->outline->points[n + 2].x =
+ HVF_COORD_TO_FIXED( params->addCubic.onpt.x, ctx->x_scale_fixed );
+ ctx->outline->points[n + 2].y =
+ HVF_COORD_TO_FIXED( params->addCubic.onpt.y, ctx->y_scale_fixed );
+ ctx->outline->tags[n + 2] = FT_CURVE_TAG_ON;
+
+ ctx->outline->n_points += 3;
+
+ FT_TRACE5(( "hvf_render:"
+ " AddCubic (%.2f,%.2f) (%.2f,%.2f) (%.2f,%.2f)\n",
+ (double)ctx->outline->points[n].x / 64,
+ (double)ctx->outline->points[n].y / 64,
+ (double)ctx->outline->points[n + 1].x / 64,
+ (double)ctx->outline->points[n + 1].y / 64,
+ (double)ctx->outline->points[n + 2].x / 64,
+ (double)ctx->outline->points[n + 2].y / 64 ));
+ return HVFPartRenderActionContinue;
+ }
+
+ case HVFPartRenderInstructionClosePath:
+ if ( ctx->path_begun && ctx->outline->n_points > 0 )
+ {
+ /* Check space for contour. */
+ error = FT_GLYPHLOADER_CHECK_POINTS( ctx->loader, 0, 1 );
+ if ( error )
+ return HVFPartRenderActionStop;
+
+ /* Close current contour. */
+ ctx->outline->contours[ctx->outline->n_contours++] =
+ (FT_UShort)( ctx->outline->n_points - 1 );
+
+ ctx->path_begun = 0;
+ }
+
+ FT_TRACE5(( "hvf_render: ClosePath\n" ));
+ return HVFPartRenderActionContinue;
+
+ case HVFPartRenderInstructionEndPath:
+ FT_TRACE5(( "hvf_render: EndPath\n" ));
+ return HVFPartRenderActionContinue;
+
+ case HVFPartRenderInstructionEndPart:
+ FT_TRACE5(( "hvf_render: EndPart (part %zu)\n",
+ params->endPart.partInfo.partId ));
+ return HVFPartRenderActionContinue;
+
+ case HVFPartRenderInstructionStop:
+ FT_TRACE5(( "hvf_render: Stop\n" ));
+ return HVFPartRenderActionStop;
+
+ default:
+ FT_TRACE1(( "hvf_render: Unknown instruction %d\n", instruction ));
+ return HVFPartRenderActionStop;
+ }
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * hvf_slot_load_glyph
+ *
+ * @Description:
+ * Load a glyph using HVF rendering with TrueType integration.
+ *
+ * @Input:
+ * glyph ::
+ * The glyph slot.
+ *
+ * size ::
+ * The size object.
+ *
+ * glyph_index ::
+ * The glyph index.
+ *
+ * load_flags ::
+ * Load flags.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ FT_LOCAL_DEF( FT_Error )
+ hvf_slot_load_glyph( FT_GlyphSlot glyph,
+ FT_Size size,
+ FT_UInt glyph_index,
+ FT_Int32 load_flags )
+ {
+ HVF_Face face = (HVF_Face)glyph->face;
+ FT_GlyphLoader loader = glyph->internal->loader;
+ HVF_RenderContext context;
+
+ FT_Error error = FT_Err_Ok;
+
+ FT_Bool apply_scaling = !( load_flags & FT_LOAD_NO_SCALE );
+
+
+ if ( !face->renderer )
+ {
+ FT_ERROR(( "hvf_slot_load_glyph: HVF renderer not initialized\n" ));
+ return FT_THROW( Invalid_Handle );
+ }
+
+ if ( glyph_index >= (FT_UInt)face->root.root.num_glyphs )
+ return FT_THROW( Invalid_Glyph_Index );
+
+ /* Initialize render context. */
+ context.loader = loader;
+ context.path_begun = 0;
+
+ /* Pre-calculate scaling factors based on `FT_LOAD_NO_SCALE` flag. */
+ context.x_scale_fixed =
+ FT_SCALE_TO_HVF_SCALE_FIXED( size->metrics.x_scale, apply_scaling );
+ context.y_scale_fixed =
+ FT_SCALE_TO_HVF_SCALE_FIXED( size->metrics.y_scale, apply_scaling );
+
+ FT_TRACE3(( "hvf_slot_load_glyph:"
+ " %s\n"
+ " "
+ " (x_scale_fixed=%.1f, y_scale_fixed=%.1f)\n",
+ apply_scaling ? "scaling enabled"
+ : "FT_LOAD_NO_SCALE - no scaling applied",
+ context.x_scale_fixed,
+ context.y_scale_fixed ));
+
+ /* Initialize glyph loader. */
+ FT_GlyphLoader_Rewind( loader );
+ context.outline = &loader->current.outline;
+
+ /* Only the HVF API calls need `@available` protection. */
+ if ( HVF_IS_AVAILABLE )
+ {
+ int hvf_result;
+
+
+ /* Configure HVF renderer for this glyph. */
+ hvf_result = HVF_set_render_part( (HVFPartRenderer*)face->renderer,
+ (HVFPartIndex)glyph_index );
+ if ( hvf_result != 0 )
+ {
+ FT_TRACE1(( "hvf_slot_load_glyph:"
+ " HVF_set_render_part failed (error %d)\n",
+ hvf_result ));
+ return FT_THROW( Invalid_Glyph_Index );
+ }
+
+ /* Set variation axis values for this glyph */
+ /* (using TT_Face infrastructure). */
+ error = hvf_set_variation_axes( face );
+#ifdef FT_DEBUG_LEVEL_TRACE
+ if ( error )
+ {
+ /* Continue with rendering even if axis setting fails. */
+ FT_TRACE1(( "hvf_slot_load_glyph:"
+ " hvf_set_variation_axes failed (error %d)\n", error ));
+ }
+#endif
+ error = FT_Err_Ok; /* axis failure is non-fatal */
+
+ /* Render the glyph using HVF. */
+ hvf_result = HVF_render_current_part( (HVFPartRenderer*)face->renderer,
+ hvf_render_callback,
+ &context );
+ if ( hvf_result != 0 )
+ {
+ FT_TRACE1(( "hvf_slot_load_glyph:"
+ " HVF_render_current_part failed (%d)\n", hvf_result ));
+ return FT_THROW( Invalid_Glyph_Index );
+ }
+ }
+ else
+ {
+ FT_TRACE3(( "hvf_slot_load_glyph: HVF not available at runtime\n" ));
+ return FT_THROW( Unimplemented_Feature );
+ }
+
+ /* Close any open path. */
+ if ( context.path_begun && context.outline->n_points > 0 )
+ {
+ /* Check space for contour. */
+ error = FT_GLYPHLOADER_CHECK_POINTS( loader, 0, 1 );
+ if ( !error )
+ {
+ context.outline->contours[context.outline->n_contours++] =
+ (FT_UShort)( context.outline->n_points - 1 );
+ }
+ }
+
+ /* Finalize glyph data with `FT_GlyphLoader`. */
+ FT_GlyphLoader_Add( loader );
+
+ /* Set up glyph slot. */
+ glyph->format = FT_GLYPH_FORMAT_OUTLINE;
+ glyph->outline = loader->base.outline;
+
+ /* Nearly all HVF glyphs have overlapping contours, */
+ /* since it is a stroke-based format */
+ glyph->outline.flags |= FT_OUTLINE_OVERLAP;
+
+ /* Set glyph metrics - get from TrueType infrastructure when possible. */
+ {
+ FT_BBox bbox;
+ FT_UShort advance_width = 0;
+ FT_Short left_bearing = 0;
+
+ TT_Face tt_face = (TT_Face)face;
+ SFNT_Service sfnt = (SFNT_Service)tt_face->sfnt;
+
+ FT_Bool has_vertical_info;
+ FT_Pos top; /* vertical top side bearing */
+ FT_Pos advance; /* vertical advance height */
+
+
+ /* Get horizontal metrics from TrueType tables if available. */
+ if ( sfnt && glyph_index < (FT_UInt)face->root.root.num_glyphs )
+ sfnt->get_metrics( tt_face, 0, glyph_index,
+ &left_bearing, &advance_width );
+ else
+ {
+ /* Fallback to reasonable defaults. */
+ advance_width = (FT_UShort)( size->metrics.x_ppem );
+ left_bearing = 0;
+ }
+
+ glyph->metrics.horiAdvance = advance_width;
+ glyph->linearHoriAdvance = advance_width;
+
+ /* Get vertical metrics from vmtx table or synthesize them, */
+ /* consistent with TrueType and CFF drivers for SFNT-based fonts. */
+ has_vertical_info =
+ FT_BOOL( tt_face->vertical_info &&
+ tt_face->vertical.number_Of_VMetrics > 0 );
+
+ if ( has_vertical_info )
+ {
+ FT_Short tsb = 0;
+ FT_UShort ah = 0;
+
+
+ sfnt->get_metrics( tt_face, 1, glyph_index, &tsb, &ah );
+
+ top = tsb;
+ advance = ah;
+ }
+ else
+ {
+ /* Use OS/2 or horizontal header for vertical advance. */
+ if ( tt_face->os2.version != 0xFFFFU )
+ advance = (FT_Pos)( tt_face->os2.sTypoAscender -
+ tt_face->os2.sTypoDescender );
+ else
+ advance = (FT_Pos)( tt_face->horizontal.Ascender -
+ tt_face->horizontal.Descender );
+
+ top = 0; /* computed after bbox is known */
+ }
+
+ glyph->linearVertAdvance = advance;
+
+ /* Scale table-derived metrics if needed. */
+ /* Outline coordinates are already in the target coordinate */
+ /* space (scaled in callback, or 26.6 font units for NO_SCALE) */
+ /* so bbox-derived metrics need no additional scaling. */
+ if ( apply_scaling )
+ {
+ FT_Fixed x_scale = size->metrics.x_scale;
+ FT_Fixed y_scale = size->metrics.y_scale;
+
+
+ glyph->metrics.horiAdvance = FT_MulFix( glyph->metrics.horiAdvance,
+ x_scale );
+ top = FT_MulFix( top, y_scale );
+ advance = FT_MulFix( advance, y_scale );
+ }
+
+ /* Compute width, height, and bearing metrics from the glyph */
+ /* outline bounding box, consistent with TrueType and CFF */
+ /* drivers. */
+ FT_Outline_Get_CBox( &glyph->outline, &bbox );
+
+ glyph->metrics.width = bbox.xMax - bbox.xMin;
+ glyph->metrics.height = bbox.yMax - bbox.yMin;
+ glyph->metrics.horiBearingX = bbox.xMin;
+ glyph->metrics.horiBearingY = bbox.yMax;
+
+ /* Finalize vertical metrics. */
+ /* vertBearingX centers the glyph horizontally for vertical */
+ /* layout, matching both TrueType and CFF drivers. */
+ /* When no vmtx data is available, vertBearingY centers the */
+ /* glyph vertically within the advance height. */
+ if ( !has_vertical_info )
+ top = ( advance - glyph->metrics.height ) / 2;
+
+ glyph->metrics.vertBearingX = glyph->metrics.horiBearingX -
+ glyph->metrics.horiAdvance / 2;
+ glyph->metrics.vertBearingY = top;
+ glyph->metrics.vertAdvance = advance;
+ }
+
+ /* Cache management - clear cache every */
+ /* `HVF_CACHE_CLEAR_COUNT` glyphs. */
+ face->cache_count++;
+ if ( face->cache_count >= HVF_CACHE_CLEAR_COUNT )
+ {
+ if ( HVF_IS_AVAILABLE )
+ {
+ HVF_clear_part_cache( (HVFPartRenderer*)face->renderer );
+ face->cache_count = 0;
+ FT_TRACE3(( "hvf_slot_load_glyph: cleared HVF cache\n" ));
+ }
+#ifdef FT_DEBUG_LEVEL_TRACE
+ else
+ FT_TRACE3(( "hvf_slot_load_glyph:"
+ " HVF not available for cache clearing\n" ));
+#endif
+ }
+
+ FT_TRACE2(( "hvf_slot_load_glyph: glyph %u loaded successfully\n",
+ glyph_index ));
+
+ return error;
+ }
+
+#else /* !FT_CONFIG_OPTION_HVF */
+
+ FT_LOCAL_DEF( FT_Error )
+ hvf_slot_load_glyph( FT_GlyphSlot glyph,
+ FT_Size size,
+ FT_UInt glyph_index,
+ FT_Int32 load_flags )
+ {
+ FT_UNUSED( glyph );
+ FT_UNUSED( size );
+ FT_UNUSED( glyph_index );
+ FT_UNUSED( load_flags );
+
+ return FT_THROW( Unimplemented_Feature );
+ }
+
+#endif /* !FT_CONFIG_OPTION_HVF */
+
+
+/* END */
diff --git a/thirdparty/freetype/src/hvf/hvfload.h b/thirdparty/freetype/src/hvf/hvfload.h
new file mode 100644
index 00000000000..0207a55e5ff
--- /dev/null
+++ b/thirdparty/freetype/src/hvf/hvfload.h
@@ -0,0 +1,42 @@
+/****************************************************************************
+ *
+ * hvfload.h
+ *
+ * HVF glyph loading (specification).
+ *
+ * Copyright (C) 2025-2026 by
+ * Apple Inc.
+ * written by Deborah Goldsmith <[email protected]>
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+
+#ifndef HVFLOAD_H_
+#define HVFLOAD_H_
+
+
+#include <freetype/freetype.h>
+#include <freetype/internal/ftobjs.h>
+
+
+FT_BEGIN_HEADER
+
+ FT_LOCAL( FT_Error )
+ hvf_slot_load_glyph( FT_GlyphSlot glyph,
+ FT_Size size,
+ FT_UInt glyph_index,
+ FT_Int32 load_flags );
+
+
+FT_END_HEADER
+
+#endif /* HVFLOAD_H_ */
+
+
+/* END */
diff --git a/thirdparty/freetype/src/hvf/hvfobjs.c b/thirdparty/freetype/src/hvf/hvfobjs.c
new file mode 100644
index 00000000000..96536b8ef15
--- /dev/null
+++ b/thirdparty/freetype/src/hvf/hvfobjs.c
@@ -0,0 +1,510 @@
+/****************************************************************************
+ *
+ * hvfobjs.c
+ *
+ * HVF objects manager (body).
+ *
+ * Copyright (C) 2025-2026 by
+ * Apple Inc.
+ * written by Deborah Goldsmith <[email protected]>
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+
+#include <freetype/internal/ftdebug.h>
+#include <freetype/internal/ftstream.h>
+#include <freetype/internal/sfnt.h>
+#include <freetype/tttags.h>
+#include <freetype/ftmm.h>
+
+#include "hvfobjs.h"
+#include "hvferror.h"
+
+
+ /**************************************************************************
+ *
+ * The macro FT_COMPONENT is used in trace mode. It is an implicit
+ * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
+ * messages during execution.
+ */
+#undef FT_COMPONENT
+#define FT_COMPONENT hvfobjs
+
+
+#ifdef FT_CONFIG_OPTION_HVF
+
+ /**************************************************************************
+ *
+ * FACE MANAGEMENT
+ *
+ */
+
+ static FT_Error
+ hvf_load_tables( HVF_Face face )
+ {
+ FT_Error error;
+ FT_Stream stream;
+ TT_Face tt_face = (TT_Face)face; /* Cast to access TT_Face members. */
+
+
+ /* Use the stream from TT_Face root. */
+ stream = FT_FACE_STREAM( face );
+
+ /* Load required 'hvgl' table using memory mapping. */
+ error = tt_face->goto_table( tt_face, TTAG_hvgl,
+ stream, &face->hvgl_size );
+ if ( error )
+ {
+ FT_TRACE2(( "hvf_load_tables: missing required 'hvgl' table\n" ));
+ return error;
+ }
+
+ /* Memory-map the 'hvgl' table data instead of copying it. */
+ if ( FT_FRAME_EXTRACT( face->hvgl_size, face->hvgl_data ) )
+ {
+ face->hvgl_size = 0;
+ FT_TRACE2(( "hvf_load_tables: failed to extract 'hvgl' table\n" ));
+ return FT_THROW( Invalid_Table );
+ }
+
+ FT_TRACE2(( "hvf_load_tables: memory-mapped 'hvgl' table (%lu bytes)\n",
+ face->hvgl_size ));
+
+ /* Load optional 'hvpm' table using memory mapping. */
+ error = tt_face->goto_table( tt_face, TTAG_hvpm,
+ stream, &face->hvpm_size );
+ if ( error )
+ {
+ face->hvpm_data = NULL; /* Ensure NULL for pointer check. */
+ face->hvpm_size = 0;
+ FT_TRACE2(( "hvf_load_tables: no 'hvpm' table found (optional)\n" ));
+ }
+ else
+ {
+ /* Memory-map the 'hvpm' table data instead of copying it. */
+ if ( FT_FRAME_EXTRACT( face->hvpm_size, face->hvpm_data ) )
+ {
+ face->hvpm_data = NULL;
+ face->hvpm_size = 0;
+ FT_TRACE2(( "hvf_load_tables: failed to extract 'hvpm' table\n" ));
+ }
+#ifdef FT_DEBUG_LEVEL_TRACE
+ else
+ FT_TRACE2(( "hvf_load_tables:"
+ " memory-mapped 'hvpm' table (%lu bytes)\n",
+ face->hvpm_size ));
+#endif
+ }
+
+ return FT_Err_Ok;
+ }
+
+
+ static FT_Error
+ hvf_init_renderer( HVF_Face face )
+ {
+ FT_Error error = FT_Err_Ok;
+ FT_Memory memory = FT_FACE_MEMORY( face );
+
+ size_t storage_size;
+
+
+ if ( !face->hvgl_data )
+ return FT_THROW( Invalid_Table );
+
+ /* Create HVF renderer - only once per face. */
+ if ( !face->renderer )
+ {
+ if ( HVF_IS_AVAILABLE )
+ {
+ int hvf_result;
+
+
+ /* Get required storage size. */
+ storage_size = HVF_part_renderer_storage_size();
+ if ( storage_size == 0 )
+ {
+ FT_TRACE1(( "hvf_init_renderer:"
+ " HVF_part_renderer_storage_size failed\n" ));
+ return FT_THROW( Invalid_Table );
+ }
+
+ /* Allocate storage for renderer. */
+ if ( FT_ALLOC( face->renderer, storage_size ) )
+ return error;
+
+ /* Initialize HVF renderer with 'hvgl' and optional 'hvpm' */
+ /* table data. */
+ hvf_result =
+ HVF_open_part_renderer( face->hvgl_data,
+ face->hvgl_size,
+ face->hvpm_data, /* NULL if missing */
+ face->hvpm_size, /* 0 if missing */
+ face->renderer,
+ storage_size );
+ if ( hvf_result != 0 )
+ {
+ FT_TRACE1(( "hvf_init_renderer:"
+ " HVF_open_part_renderer failed (error %d)\n",
+ hvf_result ));
+ FT_FREE( face->renderer );
+ return FT_THROW( Invalid_Table );
+ }
+
+ face->cache_count = 0;
+ FT_TRACE2(( "hvf_init_renderer:"
+ " HVF renderer initialized (%s hvpm)\n",
+ face->hvpm_data ? "with" : "without" ));
+ }
+ else
+ {
+ FT_TRACE3(( "hvf_init_renderer: HVF not available at runtime\n" ));
+ return FT_THROW( Invalid_Table );
+ }
+ }
+
+ return error;
+ }
+
+
+ static FT_Error
+ hvf_init_axis_coordinates( HVF_Face face )
+ {
+ FT_Error error = FT_Err_Ok;
+ FT_Memory memory = FT_FACE_MEMORY( face );
+ FT_MM_Var* mm_var = NULL;
+
+
+ /* Initialize axis data. */
+ face->num_axes = 0;
+ face->axis_coords = NULL;
+
+ /* Check whether this is a variable font and get axis information. */
+ error = FT_Get_MM_Var( (FT_Face)face, &mm_var );
+ if ( error )
+ {
+ /* Not a variable font or error getting variation info - */
+ /* that's fine. */
+ FT_TRACE3(( "hvf_init_axis_coordinates:"
+ " not a variable font or no MM info (error %x)\n",
+ error ));
+ return FT_Err_Ok;
+ }
+
+ if ( mm_var && mm_var->num_axis > 0 )
+ {
+ face->num_axes = mm_var->num_axis;
+
+ /* Allocate storage for HVF axis coordinates. */
+ if ( FT_QNEW_ARRAY( *(HVFAxisValue**)&face->axis_coords,
+ face->num_axes ) )
+ {
+ face->num_axes = 0;
+ goto Cleanup;
+ }
+
+ /* Initialize coordinates using the shared refresh function. */
+ error = hvf_refresh_axis_coordinates( face );
+ if ( error )
+ {
+ FT_FREE( face->axis_coords );
+ face->num_axes = 0;
+ goto Cleanup;
+ }
+ }
+
+ Cleanup:
+ if ( mm_var )
+ FT_Done_MM_Var( FT_FACE_LIBRARY( face ), mm_var );
+
+ return error;
+ }
+
+
+ FT_LOCAL_DEF( FT_Error )
+ hvf_refresh_axis_coordinates( HVF_Face face )
+ {
+ FT_Error error = FT_Err_Ok;
+ FT_Memory memory = FT_FACE_MEMORY( face );
+
+ FT_Fixed* ft_coords = NULL;
+ FT_UInt i;
+
+
+ /* Only refresh if we have cached coordinates. */
+ if ( !face->axis_coords || face->num_axes == 0 )
+ return FT_Err_Ok;
+
+ /* Allocate temporary storage for current FreeType coordinates. */
+ if ( FT_QNEW_ARRAY( ft_coords, face->num_axes ) )
+ return error;
+
+ /* Get current variation coordinates. */
+ error = FT_Get_Var_Blend_Coordinates( (FT_Face)face,
+ face->num_axes, ft_coords );
+ if ( error )
+ {
+ /* If we can't get coordinates, set to defaults. */
+ FT_ARRAY_ZERO( (HVFAxisValue*)face->axis_coords, face->num_axes );
+ FT_TRACE3(( "hvf_refresh_axis_coordinates:"
+ " could not get blend coordinates, using defaults\n" ));
+ error = FT_Err_Ok; /* Not fatal. */
+ }
+ else
+ {
+ /* Convert FreeType coordinates to HVF coordinates. */
+ for ( i = 0; i < face->num_axes; i++ )
+ {
+ ((HVFAxisValue*)face->axis_coords)[i] =
+ FT_COORD_TO_HVF_AXIS( ft_coords[i] );
+ FT_TRACE5(( "hvf_refresh_axis_coordinates:"
+ " axis %u: FT coord %ld -> HVF coord %f\n",
+ i, ft_coords[i],
+ ((HVFAxisValue*)face->axis_coords)[i] ));
+ }
+ FT_TRACE3(( "hvf_refresh_axis_coordinates:"
+ " refreshed %u axis coordinates\n", face->num_axes ));
+
+ /* Clear HVF renderer cache since axis coordinates changed. */
+ /* Cached parts were rendered with old axis values and are */
+ /* now invalid. */
+ if ( face->renderer )
+ {
+ if ( HVF_IS_AVAILABLE )
+ {
+ HVF_clear_part_cache( (HVFPartRenderer*)face->renderer );
+ face->cache_count = 0;
+ FT_TRACE4(( "hvf_refresh_axis_coordinates:"
+ " cleared HVF cache due to axis change\n" ));
+ }
+#ifdef FT_DEBUG_LEVEL_TRACE
+ else
+ FT_TRACE3(( "hvf_refresh_axis_coordinates:"
+ " HVF not available at runtime\n" ));
+#endif
+ }
+ }
+
+ /* Free temporary FreeType coordinate storage. */
+ FT_FREE( ft_coords );
+
+ return error;
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * hvf_face_init
+ *
+ * @Description:
+ * Initialize a given HVF face object.
+ *
+ * @Input:
+ * stream ::
+ * The source font stream.
+ *
+ * face_index ::
+ * The index of the font face in the resource.
+ *
+ * num_params ::
+ * Number of additional generic parameters. Ignored.
+ *
+ * params ::
+ * Additional generic parameters. Ignored.
+ *
+ * @InOut:
+ * face ::
+ * The newly built face object.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ FT_LOCAL_DEF( FT_Error )
+ hvf_face_init( FT_Stream stream,
+ FT_Face face,
+ FT_Int typeface_index,
+ FT_Int num_params,
+ FT_Parameter* parameters )
+ {
+ FT_Error error;
+
+ FT_Library library = FT_FACE_LIBRARY( face );
+ SFNT_Service sfnt;
+
+ TT_Face tt_face = (TT_Face)face; /* Cast to access TT_Face members. */
+
+
+ FT_TRACE2(( "HVF driver\n" ));
+
+ /* Check HVF availability at runtime (Apple platforms only). */
+ if ( HVF_IS_AVAILABLE )
+ ; /* `HVF_IS_AVAILABLE` must not be negated for strange reasons. */
+ else
+ {
+ FT_TRACE2(( "hvf_face_init: HVF not available at runtime\n" ));
+ return FT_THROW( Unknown_File_Format );
+ }
+
+ /* Check for SFNT wrapper. */
+ sfnt = (SFNT_Service)FT_Get_Module_Interface( library, "sfnt" );
+ if ( !sfnt )
+ {
+ FT_ERROR(( "hvf_face_init: cannot access 'sfnt' module\n" ));
+ error = FT_THROW( Missing_Module );
+ goto Exit;
+ }
+
+ /* Create input stream from resource. */
+ if ( FT_STREAM_SEEK( 0 ) )
+ goto Exit;
+
+ FT_TRACE2(( " " ));
+
+ /* Check that we have a valid HVF font. */
+ error = sfnt->init_face( stream, tt_face, typeface_index,
+ num_params, parameters );
+ if ( error )
+ {
+ FT_TRACE1(( "hvf_face_init: sfnt initialization failed (%d)\n",
+ error ));
+ goto Exit;
+ }
+
+ /* Verify this is actually an HVF font by checking for 'hvgl' table. */
+ error = tt_face->goto_table( tt_face, TTAG_hvgl, stream, NULL );
+ if ( error )
+ {
+ FT_TRACE1(( "hvf_face_init:"
+ " not an HVF font (missing 'hvgl' table)\n" ));
+ error = FT_THROW( Unknown_File_Format );
+ goto Exit;
+ }
+
+ /* Mark face as scalable. */
+ face->face_flags |= FT_FACE_FLAG_SCALABLE;
+
+ /* If we are performing a simple font format check, exit immediately. */
+ if ( typeface_index < 0 )
+ return FT_Err_Ok;
+
+ /* Load font directory. */
+ error = sfnt->load_face( stream, tt_face, typeface_index,
+ num_params, parameters );
+ if ( error )
+ goto Exit;
+
+ /* Load HVF-specific tables. */
+ error = hvf_load_tables( (HVF_Face)face );
+ if ( error )
+ goto Exit;
+
+ /* Initialize HVF renderer. */
+ error = hvf_init_renderer( (HVF_Face)face );
+ if ( error )
+ goto Exit;
+
+ /* Get variation axis information once during face initialization. */
+ error = hvf_init_axis_coordinates( (HVF_Face)face );
+ if ( error )
+ goto Exit;
+
+ FT_TRACE2(( "hvf_face_init: HVF face initialized successfully\n" ));
+
+ Exit:
+ return error;
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * hvf_face_done
+ *
+ * @Description:
+ * Finalize a given face object.
+ *
+ * @Input:
+ * face ::
+ * A pointer to the face object to destroy.
+ */
+ FT_LOCAL_DEF( void )
+ hvf_face_done( FT_Face face )
+ {
+ FT_Memory memory;
+ FT_Stream stream;
+
+ TT_Face tt_face;
+ HVF_Face hvf_face;
+
+ SFNT_Service sfnt;
+
+
+ if ( !face )
+ return;
+
+ memory = FT_FACE_MEMORY( face );
+ stream = FT_FACE_STREAM( face );
+
+ hvf_face = (HVF_Face)face;
+ tt_face = (TT_Face)face;
+
+ /* Use inherited SFNT service pointer */
+ sfnt = (SFNT_Service)tt_face->sfnt;
+
+ /* Clean up HVF renderer. */
+ if ( hvf_face->renderer )
+ {
+ if ( HVF_IS_AVAILABLE )
+ HVF_close_part_renderer( (HVFPartRenderer*)hvf_face->renderer );
+#ifdef FT_DEBUG_LEVEL_TRACE
+ else
+ FT_TRACE3(( "hvf_face_done: HVF not available at runtime\n" ));
+#endif
+
+ FT_FREE( hvf_face->renderer );
+ }
+
+ /* Release axis coordinates storage. */
+ if ( hvf_face->axis_coords )
+ FT_FREE( hvf_face->axis_coords );
+
+ /* Release memory-mapped table data. */
+ if ( hvf_face->hvgl_data )
+ FT_FRAME_RELEASE( hvf_face->hvgl_data );
+ if ( hvf_face->hvpm_data )
+ FT_FRAME_RELEASE( hvf_face->hvpm_data );
+
+ /* Clean up SFNT (this will clean up the TT_Face base class). */
+ if ( sfnt )
+ sfnt->done_face( tt_face );
+ }
+
+#else /* !FT_CONFIG_OPTION_HVF */
+
+ FT_LOCAL_DEF( FT_Error )
+ hvf_face_init( FT_Stream stream,
+ FT_Face face,
+ FT_Int typeface_index,
+ FT_Int num_params,
+ FT_Parameter* parameters )
+ {
+ FT_UNUSED( stream );
+ FT_UNUSED( face );
+ FT_UNUSED( typeface_index );
+ FT_UNUSED( num_params );
+ FT_UNUSED( parameters );
+
+ return FT_THROW( Unknown_File_Format );
+ }
+
+#endif /* !FT_CONFIG_OPTION_HVF */
+
+
+/* END */
diff --git a/thirdparty/freetype/src/hvf/hvfobjs.h b/thirdparty/freetype/src/hvf/hvfobjs.h
new file mode 100644
index 00000000000..ae9c90595e8
--- /dev/null
+++ b/thirdparty/freetype/src/hvf/hvfobjs.h
@@ -0,0 +1,154 @@
+/****************************************************************************
+ *
+ * hvfobjs.h
+ *
+ * HVF objects manager (specification).
+ *
+ * Copyright (C) 2025-2026 by
+ * Apple Inc.
+ * written by Deborah Goldsmith <[email protected]>
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+
+#ifndef HVFOBJS_H_
+#define HVFOBJS_H_
+
+#include <freetype/freetype.h>
+#include <freetype/ftglyph.h>
+#include <freetype/internal/ftobjs.h>
+#include <freetype/internal/tttypes.h> /* For TT_FaceRec. */
+
+
+FT_BEGIN_HEADER
+
+
+ /**************************************************************************
+ *
+ * @type:
+ * HVF_Face
+ *
+ * @description:
+ * A handle to an HVF face object. Always available for compatibility.
+ */
+ typedef struct HVF_FaceRec_* HVF_Face;
+
+
+ /**************************************************************************
+ *
+ * @struct:
+ * HVF_FaceRec
+ *
+ * @description:
+
+ * HVF face record. This structure inherits from `TT_FaceRec` instead of
+ * `FT_FaceRec` because HVF fonts are TrueType/OpenType fonts with
+ * additional HVF tables, and the HVF driver calls into SFNT functions
+ * that require `TT_Face` access.
+ *
+ * NOTE: Structure is always available with all fields always present.
+ * Only the implementation functions are conditionally compiled.
+ */
+ typedef struct HVF_FaceRec_
+ {
+ TT_FaceRec root; /* Inherit from `TT_FaceRec`. */
+
+ void* renderer; /* `HVFPartRenderer` storage or NULL. */
+
+ /* HVF-specific table data (memory-mapped). */
+ FT_Byte* hvgl_data; /* 'hvgl' table data */
+ FT_ULong hvgl_size;
+ FT_Byte* hvpm_data; /* 'hvpm' table data, NULL if not present. */
+ FT_ULong hvpm_size;
+
+ /* Cache management. */
+ FT_UInt cache_count; /* Clear cache every N glyphs. */
+
+ /* Variation axis data (allocated once in `hvf_face_init`). */
+ FT_UInt num_axes; /* Number of variation axes. */
+ void* axis_coords; /* Pre-converted HVF axis coordinates */
+ /* (HVFAxisValue* when configured). */
+
+ } HVF_FaceRec;
+
+
+ /* Conditional declarations - only when HVF is enabled. */
+#ifdef FT_CONFIG_OPTION_HVF
+
+#include <hvf/Scaler.h>
+
+ /* Runtime availability checking for Apple platforms. */
+#ifdef HVF_RUNTIME_AVAILABLE
+
+ /* Single macro that encapsulates the `__builtin_available` */
+ /* check for C/C++. */
+ /* */
+ /* BEWARE: Only use this as `if ( HVS_IS_AVAILABLE ) ...` */
+ /* Any deviation from this form (also including */
+ /* negation) causes `clang` to emit a warning. */
+#define HVF_IS_AVAILABLE __builtin_available( macOS 15.4, iOS 18.4, * )
+
+#else
+
+ /* Non-Apple platforms: No runtime check needed. */
+#define HVF_IS_AVAILABLE ( 1 )
+
+#endif /* HVF_RUNTIME_AVAILABLE */
+
+
+ /**************************************************************************
+ *
+ * @struct:
+ * HVF_RenderContext
+ *
+ * @description:
+ * Context for HVF rendering callbacks.
+ */
+ typedef struct HVF_RenderContext_
+ {
+ FT_GlyphLoader loader; /* Standard FreeType loader. */
+ FT_Outline* outline; /* Points to `loader->current.outline`. */
+ FT_Bool path_begun; /* Path state tracking. */
+
+ /* Pre-calculated scaling factors for efficient coordinate conversion. */
+ HVFXYCoord x_scale_fixed; /* x_scale * 65536.0 (or just 65536.0) */
+ HVFXYCoord y_scale_fixed; /* y_scale * 65536.0 (or just 65536.0) */
+
+ } HVF_RenderContext;
+
+
+ /* Function declarations. */
+ FT_LOCAL( void )
+ hvf_face_done( FT_Face face );
+
+ FT_LOCAL( FT_Error )
+ hvf_refresh_axis_coordinates( HVF_Face face );
+
+
+ /* Convert FreeType normalized coordinates to HVF axis values. */
+#define FT_COORD_TO_HVF_AXIS( coord ) \
+ ( (HVFAxisValue)(coord) / 65536.0 )
+
+#endif /* FT_CONFIG_OPTION_HVF */
+
+
+ FT_LOCAL( FT_Error )
+ hvf_face_init( FT_Stream stream,
+ FT_Face face,
+ FT_Int typeface_index,
+ FT_Int num_params,
+ FT_Parameter* parameters );
+
+
+FT_END_HEADER
+
+#endif /* HVFOBJS_H_ */
+
+
+/* END */
diff --git a/thirdparty/freetype/src/otvalid/otvmath.c b/thirdparty/freetype/src/otvalid/otvmath.c
index 80d78890bee..ccc4b89d296 100644
--- a/thirdparty/freetype/src/otvalid/otvmath.c
+++ b/thirdparty/freetype/src/otvalid/otvmath.c
@@ -147,8 +147,9 @@
cnt = FT_NEXT_USHORT( p );
- OTV_LIMIT_CHECK( 4 * cnt + 2 );
- table_size = 4 + 4 * cnt;
+ /* (4 * cnt) + (4 * (cnt + 1)) */
+ OTV_LIMIT_CHECK( 8 * cnt + 4 );
+ table_size = 6 + 8 * cnt;
/* Heights */
for ( i = 0; i < cnt; i++ )
diff --git a/thirdparty/freetype/src/pcf/pcfdrivr.c b/thirdparty/freetype/src/pcf/pcfdrivr.c
index 6d65c6909e8..11977c51edb 100644
--- a/thirdparty/freetype/src/pcf/pcfdrivr.c
+++ b/thirdparty/freetype/src/pcf/pcfdrivr.c
@@ -458,6 +458,7 @@ THE SOFTWARE.
PCF_Face face = (PCF_Face)size->face;
FT_Stream stream;
FT_Error error = FT_Err_Ok;
+ FT_Int width, height;
FT_Bitmap* bitmap = &slot->bitmap;
PCF_Metric metric;
FT_ULong bytes;
@@ -481,29 +482,36 @@ THE SOFTWARE.
metric = face->metrics + glyph_index;
- bitmap->rows = (unsigned int)( metric->ascent +
- metric->descent );
- bitmap->width = (unsigned int)( metric->rightSideBearing -
- metric->leftSideBearing );
+ height = metric->ascent + metric->descent;
+ width = metric->rightSideBearing - metric->leftSideBearing;
+
+ /* sanity checks against reasonable size */
+ if ( height < 0 || height > 4 * size->metrics.y_ppem )
+ height = 0;
+ if ( width < 0 || width > 4 * size->metrics.x_ppem )
+ width = 0;
+
+ bitmap->rows = (unsigned int)height;
+ bitmap->width = (unsigned int)width;
bitmap->num_grays = 1;
bitmap->pixel_mode = FT_PIXEL_MODE_MONO;
switch ( PCF_GLYPH_PAD( face->bitmapsFormat ) )
{
case 1:
- bitmap->pitch = (int)( ( bitmap->width + 7 ) >> 3 );
+ bitmap->pitch = ( width + 7 ) >> 3;
break;
case 2:
- bitmap->pitch = (int)( ( ( bitmap->width + 15 ) >> 4 ) << 1 );
+ bitmap->pitch = ( ( width + 15 ) >> 4 ) << 1;
break;
case 4:
- bitmap->pitch = (int)( ( ( bitmap->width + 31 ) >> 5 ) << 2 );
+ bitmap->pitch = ( ( width + 31 ) >> 5 ) << 2;
break;
case 8:
- bitmap->pitch = (int)( ( ( bitmap->width + 63 ) >> 6 ) << 3 );
+ bitmap->pitch = ( ( width + 63 ) >> 6 ) << 3;
break;
default:
@@ -517,9 +525,8 @@ THE SOFTWARE.
slot->metrics.horiAdvance = (FT_Pos)( metric->characterWidth * 64 );
slot->metrics.horiBearingX = (FT_Pos)( metric->leftSideBearing * 64 );
slot->metrics.horiBearingY = (FT_Pos)( metric->ascent * 64 );
- slot->metrics.width = (FT_Pos)( ( metric->rightSideBearing -
- metric->leftSideBearing ) * 64 );
- slot->metrics.height = (FT_Pos)( bitmap->rows * 64 );
+ slot->metrics.width = (FT_Pos)( width * 64 );
+ slot->metrics.height = (FT_Pos)( height * 64 );
ft_synthesize_vertical_metrics( &slot->metrics,
( face->accel.fontAscent +
@@ -529,12 +536,12 @@ THE SOFTWARE.
goto Exit;
/* XXX: to do: are there cases that need repadding the bitmap? */
- bytes = (FT_ULong)bitmap->pitch * bitmap->rows;
-
error = ft_glyphslot_alloc_bitmap( slot );
if ( error )
goto Exit;
+ bytes = (FT_ULong)bitmap->pitch * bitmap->rows;
+
if ( FT_STREAM_SEEK( metric->bits ) ||
FT_STREAM_READ( bitmap->buffer, bytes ) )
goto Exit;
diff --git a/thirdparty/freetype/src/pcf/pcfread.c b/thirdparty/freetype/src/pcf/pcfread.c
index 9c250c84649..49d5171630d 100644
--- a/thirdparty/freetype/src/pcf/pcfread.c
+++ b/thirdparty/freetype/src/pcf/pcfread.c
@@ -783,22 +783,6 @@ THE SOFTWARE.
if ( error )
break;
-
- /* sanity checks -- those values are used in `PCF_Glyph_Load' to */
- /* compute a glyph's bitmap dimensions, thus setting them to zero in */
- /* case of an error disables this particular glyph only */
- if ( metrics->rightSideBearing < metrics->leftSideBearing ||
- metrics->ascent < -metrics->descent )
- {
- metrics->characterWidth = 0;
- metrics->leftSideBearing = 0;
- metrics->rightSideBearing = 0;
- metrics->ascent = 0;
- metrics->descent = 0;
-
- FT_TRACE0(( "pcf_get_metrics:"
- " invalid metrics for glyph %lu\n", i ));
- }
}
if ( error )
diff --git a/thirdparty/freetype/src/pfr/pfrobjs.c b/thirdparty/freetype/src/pfr/pfrobjs.c
index 9fdd84a86d9..bf538e20e54 100644
--- a/thirdparty/freetype/src/pfr/pfrobjs.c
+++ b/thirdparty/freetype/src/pfr/pfrobjs.c
@@ -529,6 +529,8 @@
if ( extra > 0 )
{
+ /* `item->pair_count` is not a power of two; */
+ /* check value and adjust `base` if necessary */
p = base + extra * size;
if ( twobytes )
@@ -541,14 +543,15 @@
if ( cpair < pair )
{
- if ( twobyte_adj )
- p += 2;
+ if ( twobytes )
+ p -= 4;
else
- p++;
+ p -= 2;
base = p;
}
}
+ /* do a power-of-two binary search */
while ( probe > size )
{
probe >>= 1;
diff --git a/thirdparty/freetype/src/psaux/psintrp.c b/thirdparty/freetype/src/psaux/psintrp.c
index 7e3475e6f58..bb8efc8156f 100644
--- a/thirdparty/freetype/src/psaux/psintrp.c
+++ b/thirdparty/freetype/src/psaux/psintrp.c
@@ -415,7 +415,7 @@
/* Blend numOperands on the stack, */
/* store results into the first numBlends values, */
/* then pop remaining arguments. */
- static void
+ static FT_Error
cf2_doBlend( const CFF_Blend blend,
CF2_Stack opStack,
CF2_UInt numBlends )
@@ -423,10 +423,19 @@
CF2_UInt delta;
CF2_UInt base;
CF2_UInt i, j;
- CF2_UInt numOperands = (CF2_UInt)( numBlends * blend->lenBV );
+ CF2_UInt numOperands;
+ CF2_UInt count = cf2_stack_count( opStack );
+
+
+ if ( blend->lenBV == 0 ||
+ numBlends > count / blend->lenBV )
+ return FT_THROW( Stack_Underflow );
+ numOperands = numBlends * blend->lenBV; /* cannot overflow here */
+ if ( numOperands > count )
+ return FT_THROW( Stack_Underflow );
- base = cf2_stack_count( opStack ) - numOperands;
+ base = count - numOperands;
delta = base + numBlends;
FT_TRACE6(( " (" ));
@@ -455,6 +464,8 @@
/* leave only `numBlends' results on stack */
cf2_stack_pop( opStack, numOperands - numBlends );
+
+ return FT_Err_Ok;
}
@@ -769,13 +780,10 @@
/* do the blend */
numBlends = (FT_UInt)cf2_stack_popInt( opStack );
- if ( numBlends > stackSize )
- {
- lastError = FT_THROW( Invalid_Glyph_Format );
- goto exit;
- }
- cf2_doBlend( &font->blend, opStack, numBlends );
+ lastError = cf2_doBlend( &font->blend, opStack, numBlends );
+ if ( lastError )
+ goto exit;
font->blend.usedBV = TRUE;
}
@@ -985,8 +993,8 @@
FT_TRACE4(( "%s", op1 == cf2_cmdCALLGSUBR ? " callgsubr"
: " callsubr" ));
- if ( ( !font->isT1 && charstringIndex > CF2_MAX_SUBR ) ||
- ( font->isT1 && charstringIndex > T1_MAX_SUBRS_CALLS ) )
+ if ( ( !font->isT1 && charstringIndex >= CF2_MAX_SUBR ) ||
+ ( font->isT1 && charstringIndex >= T1_MAX_SUBRS_CALLS ) )
{
/* max subr plus one for charstring */
lastError = FT_THROW( Invalid_Glyph_Format );
diff --git a/thirdparty/freetype/src/psaux/psstack.c b/thirdparty/freetype/src/psaux/psstack.c
index 797486588a3..1360ba30dab 100644
--- a/thirdparty/freetype/src/psaux/psstack.c
+++ b/thirdparty/freetype/src/psaux/psstack.c
@@ -211,7 +211,7 @@
CF2_UInt idx,
CF2_Fixed val )
{
- if ( idx > cf2_stack_count( stack ) )
+ if ( idx >= cf2_stack_count( stack ) )
{
CF2_SET_ERROR( stack->error, Stack_Overflow );
return;
@@ -245,6 +245,7 @@
CF2_StackNumber last = { { 0 }, CF2_NumberInt };
CF2_Int start_idx, idx, i;
+ CF2_Int offset;
if ( count < 2 )
@@ -256,6 +257,8 @@
return;
}
+ offset = (CF2_Int)cf2_stack_count( stack ) - count;
+
/* before C99 it is implementation-defined whether */
/* the result of `%' is negative if the first operand */
/* is negative */
@@ -303,7 +306,7 @@
{
start_idx++;
idx = start_idx;
- last = stack->buffer[idx];
+ last = stack->buffer[idx + offset];
}
idx += shift;
@@ -312,9 +315,9 @@
else if ( idx < 0 )
idx += count;
- tmp = stack->buffer[idx];
- stack->buffer[idx] = last;
- last = tmp;
+ tmp = stack->buffer[idx + offset];
+ stack->buffer[idx + offset] = last;
+ last = tmp;
}
}
diff --git a/thirdparty/freetype/src/raster/ftraster.c b/thirdparty/freetype/src/raster/ftraster.c
index 4165800a1aa..e0121f6ee1a 100644
--- a/thirdparty/freetype/src/raster/ftraster.c
+++ b/thirdparty/freetype/src/raster/ftraster.c
@@ -1622,15 +1622,14 @@
tag = FT_CURVE_TAG( tags[0] );
- /* A contour cannot start with a cubic control point! */
- if ( tag == FT_CURVE_TAG_CUBIC )
- goto Invalid_Outline;
-
/* check first point to determine origin */
- if ( tag == FT_CURVE_TAG_CONIC )
+ if ( tag != FT_CURVE_TAG_ON )
{
- /* first point is conic control. Yes, this happens. */
- if ( FT_CURVE_TAG( ras.outline.tags[last] ) == FT_CURVE_TAG_ON )
+ UInt last_tag = FT_CURVE_TAG( ras.outline.tags[last] );
+
+
+ /* first point is a control point. Yes, this happens. */
+ if ( last_tag == FT_CURVE_TAG_ON )
{
/* start at last point if it is on the curve */
v_start = v_last;
@@ -1638,9 +1637,12 @@
}
else
{
- /* if both first and last points are conic, */
- /* start at their middle and record its position */
- /* for closure */
+ if ( last_tag != tag )
+ goto Invalid_Outline;
+
+ /* if both first and last points are controls, */
+ /* start at their middle and record its position */
+ /* for closure */
v_start.x = ( v_start.x + v_last.x ) / 2;
v_start.y = ( v_start.y + v_last.y ) / 2;
@@ -1755,14 +1757,30 @@
if ( point <= limit )
{
+ tag = FT_CURVE_TAG( tags[0] );
+
x3 = SCALED( point[0].x );
y3 = SCALED( point[0].y );
if ( flipped )
SWAP_( x3, y3 );
+ if ( tag == FT_CURVE_TAG_CUBIC )
+ {
+ x3 = ( x2 + x3 ) / 2;
+ y3 = ( y2 + y3 ) / 2;
+ }
+ else if ( tag != FT_CURVE_TAG_ON )
+ goto Invalid_Outline;
+
if ( Cubic_To( RAS_VARS x1, y1, x2, y2, x3, y3 ) )
goto Fail;
+
+ if ( tag == FT_CURVE_TAG_CUBIC )
+ {
+ point--;
+ tags--;
+ }
continue;
}
@@ -2447,9 +2465,8 @@
Int y_min,
Int y_max )
{
- Int y_mid;
- Int band_top = 0;
- Int band_stack[32]; /* enough to bisect 32-bit int bands */
+ Int band_stack[32]; /* enough to bisect 32-bit int bands */
+ Int* band = band_stack;
FT_TRACE6(( "%s pass [%d..%d]\n",
@@ -2476,10 +2493,8 @@
FT_TRACE6(( "band [%d..%d]: to be bisected\n",
y_min, y_max ));
- y_mid = ( y_min + y_max ) >> 1;
-
- band_stack[band_top++] = y_min;
- y_min = y_mid + 1;
+ *band++ = y_min;
+ y_min = ( y_min + y_max + 1 ) >> 1;
}
else
{
@@ -2490,11 +2505,11 @@
if ( ras.fProfile )
Draw_Sweep( RAS_VAR );
- if ( --band_top < 0 )
- break;
+ if ( band == band_stack )
+ break; /* done */
y_max = y_min - 1;
- y_min = band_stack[band_top];
+ y_min = *--band;
}
}
@@ -2517,11 +2532,7 @@
Render_Glyph( RAS_ARG )
{
FT_Error error;
- Long buffer[FT_MAX_BLACK_POOL];
-
- ras.buff = buffer;
- ras.sizeBuff = (&buffer)[1]; /* Points to right after buffer. */
Set_High_Precision( RAS_VARS ras.outline.flags &
FT_OUTLINE_HIGH_PRECISION );
@@ -2665,6 +2676,9 @@
const FT_Outline* outline = (const FT_Outline*)params->source;
const FT_Bitmap* target_map = params->target;
+ FT_ULong estimate;
+ int ret;
+
#ifndef FT_STATIC_RASTER
black_TWorker worker[1];
#endif
@@ -2712,7 +2726,36 @@
if ( ras.bPitch > 0 )
ras.bOrigin += ras.bTop * ras.bPitch;
- return Render_Glyph( RAS_VAR );
+ /* allocate memory based on empirical estimate from CJK fonts */
+ estimate = ( ras.bTop + ras.bRight ) * 8UL +
+ 80UL * sizeof ( TProfile ) / sizeof ( Long );
+ if ( estimate > FT_MAX_BLACK_POOL )
+ {
+ FT_Error error;
+ FT_Memory memory = (FT_Memory)((black_PRaster)raster)->memory;
+
+
+ if ( FT_QNEW_ARRAY( ras.buff, estimate ) )
+ ret = error;
+ else
+ {
+ ras.sizeBuff = ras.buff + estimate;
+ ret = Render_Glyph( RAS_VAR );
+ FT_FREE( ras.buff );
+ }
+ }
+ else
+ {
+ Long buffer[FT_MAX_BLACK_POOL]; /* stack allocation */
+
+
+ ras.buff = buffer;
+ ras.sizeBuff = (&buffer)[1]; /* Points to right after buffer. */
+
+ ret = Render_Glyph( RAS_VAR );
+ }
+
+ return ret;
}
diff --git a/thirdparty/freetype/src/sdf/ftsdf.c b/thirdparty/freetype/src/sdf/ftsdf.c
index e86b9959467..d9d43fcc41c 100644
--- a/thirdparty/freetype/src/sdf/ftsdf.c
+++ b/thirdparty/freetype/src/sdf/ftsdf.c
@@ -655,12 +655,6 @@
FT_Memory memory = shape->memory;
- if ( !to || !user )
- {
- error = FT_THROW( Invalid_Argument );
- goto Exit;
- }
-
FT_CALL( sdf_contour_new( memory, &contour ) );
contour->last_pos = *to;
@@ -679,21 +673,14 @@
sdf_line_to( const FT_26D6_Vec* to,
void* user )
{
- SDF_Shape* shape = ( SDF_Shape* )user;
- SDF_Edge* edge = NULL;
- SDF_Contour* contour = NULL;
-
- FT_Error error = FT_Err_Ok;
- FT_Memory memory = shape->memory;
+ SDF_Shape* shape = ( SDF_Shape* )user;
+ SDF_Edge* edge = NULL;
+ SDF_Contour* contour = shape->contours;
+ FT_Error error = FT_Err_Ok;
+ FT_Memory memory = shape->memory;
- if ( !to || !user )
- {
- error = FT_THROW( Invalid_Argument );
- goto Exit;
- }
- contour = shape->contours;
if ( contour->last_pos.x == to->x &&
contour->last_pos.y == to->y )
@@ -722,22 +709,14 @@
const FT_26D6_Vec* to,
void* user )
{
- SDF_Shape* shape = ( SDF_Shape* )user;
- SDF_Edge* edge = NULL;
- SDF_Contour* contour = NULL;
+ SDF_Shape* shape = ( SDF_Shape* )user;
+ SDF_Edge* edge = NULL;
+ SDF_Contour* contour = shape->contours;
FT_Error error = FT_Err_Ok;
FT_Memory memory = shape->memory;
- if ( !control_1 || !to || !user )
- {
- error = FT_THROW( Invalid_Argument );
- goto Exit;
- }
-
- contour = shape->contours;
-
/* If the control point coincides with any of the end points */
/* then it is a line and should be treated as one to avoid */
/* unnecessary complexity later in the algorithm. */
@@ -778,20 +757,12 @@
{
SDF_Shape* shape = ( SDF_Shape* )user;
SDF_Edge* edge = NULL;
- SDF_Contour* contour = NULL;
+ SDF_Contour* contour = shape->contours;
FT_Error error = FT_Err_Ok;
FT_Memory memory = shape->memory;
- if ( !control_2 || !control_1 || !to || !user )
- {
- error = FT_THROW( Invalid_Argument );
- goto Exit;
- }
-
- contour = shape->contours;
-
FT_CALL( sdf_edge_new( memory, &edge ) );
edge->edge_type = SDF_EDGE_CUBIC;
@@ -3643,10 +3614,13 @@
else
internal_params.overload_sign = 0;
- /* Make `contour->next` NULL so that there is */
- /* one contour in the list. Also hold the next */
- /* contour in a temporary variable so as to */
- /* restore the original value. */
+ /* Make `contour->next` NULL so that there is one */
+ /* contour in the list. Also hold the next contour */
+ /* in a temporary variable to continue the iteration */
+ /* afterwards, since the `sdf_generate_subdivision` */
+ /* call below frees the current `contour` (through */
+ /* `split_sdf_shape`) and it must no longer be */
+ /* dereferenced. */
temp_contour = contour->next;
contour->next = NULL;
@@ -3660,9 +3634,6 @@
spread,
&bitmaps[i] ) );
- /* Restore the original `next` variable. */
- contour->next = temp_contour;
-
/* Since `split_sdf_shape` deallocated the original */
/* contours list we need to assign the new value to */
/* the shape's contour. */
@@ -3679,7 +3650,10 @@
orientations[i] = SDF_ORIENTATION_CW;
}
- contour = contour->next;
+ /* Advance using the saved pointer; `contour` itself */
+ /* has been freed by `split_sdf_shape` and must not */
+ /* be dereferenced. */
+ contour = temp_contour;
}
/* assign the new contour list to `shape->contours` */
diff --git a/thirdparty/freetype/src/sfnt/sfobjs.c b/thirdparty/freetype/src/sfnt/sfobjs.c
index fc855cbb6b9..b894331935a 100644
--- a/thirdparty/freetype/src/sfnt/sfobjs.c
+++ b/thirdparty/freetype/src/sfnt/sfobjs.c
@@ -34,6 +34,10 @@
#include <freetype/internal/services/svmetric.h>
#endif
+#ifdef TT_CONFIG_OPTION_VARC
+#include <freetype/internal/services/svvarc.h>
+#endif
+
#include "sferrors.h"
#ifdef TT_CONFIG_OPTION_BDF
@@ -440,7 +444,10 @@
if ( tag == TTAG_ttcf )
{
- FT_Int n;
+ FT_ULong version;
+ FT_ULong count;
+ FT_Bool has_extended_offsets;
+ FT_Int n;
FT_TRACE3(( "sfnt_open_font: file is a collection\n" ));
@@ -448,10 +455,13 @@
if ( FT_STREAM_READ_FIELDS( ttc_header_fields, &face->ttc_header ) )
return error;
- FT_TRACE3(( " with %ld subfonts\n",
- face->ttc_header.count ));
+ version = (FT_ULong)face->ttc_header.version;
+ count = (FT_ULong)face->ttc_header.count;
- if ( face->ttc_header.count == 0 )
+ has_extended_offsets = ( version >> 16 == 1 || version >> 16 == 2 ) &&
+ ( version & 0xFFFF ) >= 1;
+
+ if ( !has_extended_offsets && face->ttc_header.count == 0 )
return FT_THROW( Invalid_Table );
/* a rough size estimate: let's conservatively assume that there */
@@ -459,7 +469,38 @@
/* 28 bytes), thus we have (at least) `12 + 4*count' bytes for the */
/* size of the TTC header plus `28*count' bytes for all subfont */
/* headers */
- if ( (FT_ULong)face->ttc_header.count > stream->size / ( 28 + 4 ) )
+ if ( count > stream->size / ( 28 + 4 ) )
+ return FT_THROW( Array_Too_Large );
+
+ if ( has_extended_offsets )
+ {
+ FT_TRACE3(( " with %lu compatibility subfonts\n",
+ count ));
+
+ if ( FT_STREAM_SKIP( count * 4L ) )
+ return error;
+
+ /* In version 2.1 the extended count and offsets follow the */
+ /* three version 2 DSIG fields. */
+ if ( version >> 16 == 2 && FT_STREAM_SKIP( 12 ) )
+ return error;
+
+ if ( FT_READ_ULONG( count ) )
+ return error;
+
+ if ( count > FT_LONG_MAX )
+ return FT_THROW( Array_Too_Large );
+
+ face->ttc_header.count = (FT_Long)count;
+ }
+
+ FT_TRACE3(( " with %ld subfonts\n",
+ face->ttc_header.count ));
+
+ if ( face->ttc_header.count == 0 )
+ return FT_THROW( Invalid_Table );
+
+ if ( count > stream->size / ( 28 + 4 ) )
return FT_THROW( Array_Too_Large );
/* now read the offsets of each font in the file */
@@ -557,6 +598,24 @@
0 );
#endif
+#ifdef TT_CONFIG_OPTION_VARC
+ if ( !face->tt_varc )
+ {
+ /* we want the VARC implementation from the `truetype' module only */
+ FT_Module tt_module = FT_Get_Module( library, "truetype" );
+
+
+ face->tt_varc = ft_module_get_service( tt_module,
+ FT_SERVICE_ID_VARC,
+ 0 );
+ }
+
+ if ( !face->face_varc )
+ face->face_varc = ft_module_get_service( &face->root.driver->root,
+ FT_SERVICE_ID_VARC,
+ 0 );
+#endif
+
FT_TRACE2(( "SFNT driver\n" ));
error = sfnt_open_font( stream,
@@ -719,10 +778,12 @@
FT_FREE( default_values );
FT_FREE( instance_values );
- /* we don't support Multiple Master CFFs yet; */
- /* note that `glyf' or `CFF2' have precedence */
- if ( face->goto_table( face, TTAG_glyf, stream, 0 ) &&
+ /* we don't support Multiple Master CFFs yet; */
+ /* note that 'GLYF', 'glyf', or 'CFF2' have precedence */
+ if ( face->goto_table( face, TTAG_GLYF, stream, 0 ) &&
+ face->goto_table( face, TTAG_glyf, stream, 0 ) &&
face->goto_table( face, TTAG_CFF2, stream, 0 ) &&
+ face->goto_table( face, TTAG_hvgl, stream, 0 ) &&
!face->goto_table( face, TTAG_CFF, stream, 0 ) )
num_instances = 0;
@@ -862,12 +923,20 @@
/* do we have outlines in there? */
#ifdef FT_CONFIG_OPTION_INCREMENTAL
+ face->is_extended_glyf =
+ FT_BOOL( tt_face_lookup_table( face, TTAG_GLYF ) );
+
has_outline = FT_BOOL( face->root.internal->incremental_interface ||
+ face->is_extended_glyf ||
tt_face_lookup_table( face, TTAG_glyf ) ||
tt_face_lookup_table( face, TTAG_CFF ) ||
tt_face_lookup_table( face, TTAG_CFF2 ) );
#else
- has_outline = FT_BOOL( tt_face_lookup_table( face, TTAG_glyf ) ||
+ face->is_extended_glyf =
+ FT_BOOL( tt_face_lookup_table( face, TTAG_GLYF ) );
+
+ has_outline = FT_BOOL( face->is_extended_glyf ||
+ tt_face_lookup_table( face, TTAG_glyf ) ||
tt_face_lookup_table( face, TTAG_CFF ) ||
tt_face_lookup_table( face, TTAG_CFF2 ) );
#endif
@@ -950,6 +1019,7 @@
get_glyph_metrics )
{
face->horizontal.number_Of_HMetrics = 0;
+ face->horz_metrics_count = 0;
error = FT_Err_Ok;
}
#endif
@@ -977,6 +1047,7 @@
get_glyph_metrics )
{
face->horizontal.number_Of_HMetrics = 0;
+ face->horz_metrics_count = 0;
error = FT_Err_Ok;
}
#endif
@@ -1024,6 +1095,27 @@
LOAD_( colr );
}
+#ifdef TT_CONFIG_OPTION_VARC
+ /* variable composite glyph support */
+ if ( face->face_varc )
+ {
+ FT_Service_VARC varc = (FT_Service_VARC)face->face_varc;
+
+
+ FT_TRACE2(( "'VARC' " ));
+ FT_TRACE3(( "-->\n" ));
+
+ error = varc->load( (FT_Face)face, stream );
+
+ FT_TRACE2(( "%s\n", ( !error )
+ ? "loaded"
+ : FT_ERR_EQ( error, Table_Missing )
+ ? "missing"
+ : "failed to load" ));
+ FT_TRACE3(( "\n" ));
+ }
+#endif
+
/* OpenType-SVG glyph support */
if ( sfnt->load_svg )
LOAD_( svg );
@@ -1037,7 +1129,7 @@
LOAD_( gpos );
#endif
- face->root.num_glyphs = face->max_profile.numGlyphs;
+ face->root.num_glyphs = face->maxp_num_glyphs;
/* Bit 8 of the `fsSelection' field in the `OS/2' table denotes */
/* a WWS-only font face. `WWS' stands for `weight', width', and */
@@ -1190,13 +1282,10 @@
tt_face_build_cmaps( face ); /* ignore errors */
-
/* set the encoding fields */
{
FT_Int m;
-#ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
- FT_Bool has_unicode = FALSE;
-#endif
+ FT_UInt n;
for ( m = 0; m < root->num_charmaps; m++ )
@@ -1206,38 +1295,58 @@
charmap->encoding = sfnt_find_encoding( charmap->platform_id,
charmap->encoding_id );
+ }
+
+ for ( n = 0; n < face->num_dmap_charmaps; n++ )
+ {
+ FT_CharMap charmap = &face->dmap_charmaps[n]->charmap;
-#ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
- if ( charmap->encoding == FT_ENCODING_UNICODE ||
- charmap->encoding == FT_ENCODING_MS_SYMBOL ) /* PUA */
- has_unicode = TRUE;
+ charmap->encoding = sfnt_find_encoding( charmap->platform_id,
+ charmap->encoding_id );
}
- /* synthesize Unicode charmap if one is missing */
- if ( !has_unicode &&
- root->face_flags & FT_FACE_FLAG_GLYPH_NAMES )
+ tt_face_build_dmaps( face ); /* ignore errors */
+
+#ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
{
- FT_CharMapRec cmaprec;
+ FT_Bool has_unicode = FALSE;
- cmaprec.face = root;
- cmaprec.platform_id = TT_PLATFORM_MICROSOFT;
- cmaprec.encoding_id = TT_MS_ID_UNICODE_CS;
- cmaprec.encoding = FT_ENCODING_UNICODE;
+ for ( m = 0; m < root->num_charmaps; m++ )
+ {
+ FT_CharMap charmap = root->charmaps[m];
- error = FT_CMap_New( (FT_CMap_Class)&tt_cmap_unicode_class_rec,
- NULL, &cmaprec, NULL );
- if ( error &&
- FT_ERR_NEQ( error, No_Unicode_Glyph_Name ) &&
- FT_ERR_NEQ( error, Unimplemented_Feature ) )
- goto Exit;
- error = FT_Err_Ok;
+ if ( charmap->encoding == FT_ENCODING_UNICODE ||
+ charmap->encoding == FT_ENCODING_MS_SYMBOL ) /* PUA */
+ has_unicode = TRUE;
+ }
+
+ /* synthesize Unicode charmap if one is missing */
+ if ( !has_unicode &&
+ root->face_flags & FT_FACE_FLAG_GLYPH_NAMES )
+ {
+ FT_CharMapRec cmaprec;
+
+
+ cmaprec.face = root;
+ cmaprec.platform_id = TT_PLATFORM_MICROSOFT;
+ cmaprec.encoding_id = TT_MS_ID_UNICODE_CS;
+ cmaprec.encoding = FT_ENCODING_UNICODE;
-#endif /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */
+ error = FT_CMap_New( (FT_CMap_Class)&tt_cmap_unicode_class_rec,
+ NULL, &cmaprec, NULL );
+ if ( error &&
+ FT_ERR_NEQ( error, No_Unicode_Glyph_Name ) &&
+ FT_ERR_NEQ( error, Unimplemented_Feature ) )
+ goto Exit;
+ error = FT_Err_Ok;
+ }
}
+
+#endif /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */
}
#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
@@ -1470,6 +1579,17 @@
sfnt->free_colr( face );
}
+#ifdef TT_CONFIG_OPTION_VARC
+ /* free VARC data */
+ if ( face->face_varc )
+ {
+ FT_Service_VARC varc = (FT_Service_VARC)face->face_varc;
+
+
+ varc->done( (FT_Face)face );
+ }
+#endif
+
#ifdef FT_CONFIG_OPTION_SVG
/* free SVG data */
if ( sfnt->free_svg )
@@ -1502,9 +1622,14 @@
FT_Stream stream = FT_FACE_STREAM( face );
- /* simply release the 'cmap' table frame */
+ tt_face_done_dmaps( face );
+
+ /* simply release the 'cmap' and 'DMAP' table frames */
FT_FRAME_RELEASE( face->cmap_table );
face->cmap_size = 0;
+
+ FT_FRAME_RELEASE( face->dmap_table );
+ face->dmap_size = 0;
}
face->horz_metrics_size = 0;
@@ -1549,6 +1674,11 @@
FT_FREE( face->palette );
face->sfnt = NULL;
+
+#ifdef TT_CONFIG_OPTION_VARC
+ face->tt_varc = NULL;
+ face->face_varc = NULL;
+#endif
}
diff --git a/thirdparty/freetype/src/sfnt/sfwoff.c b/thirdparty/freetype/src/sfnt/sfwoff.c
index b8a3030d9cb..02125e3cba5 100644
--- a/thirdparty/freetype/src/sfnt/sfwoff.c
+++ b/thirdparty/freetype/src/sfnt/sfwoff.c
@@ -310,8 +310,12 @@
woff_offset += woff.privLength;
}
- if ( sfnt_offset != woff.totalSfntSize ||
- woff_offset != woff.length )
+ /* Reject unrealistic size or compression factor */
+ if ( sfnt_offset > 0x4000000UL ||
+ ( sfnt_offset >> 6 ) > woff_offset ||
+ sfnt_offset != woff.totalSfntSize ||
+ woff_offset != woff.length )
+
{
FT_ERROR(( "woff_font_open: invalid `sfnt' table structure\n" ));
error = FT_THROW( Invalid_Table );
diff --git a/thirdparty/freetype/src/sfnt/ttbdf.c b/thirdparty/freetype/src/sfnt/ttbdf.c
index 9fa12c099f0..e4ece461369 100644
--- a/thirdparty/freetype/src/sfnt/ttbdf.c
+++ b/thirdparty/freetype/src/sfnt/ttbdf.c
@@ -216,8 +216,9 @@
case 0x00: /* string */
case 0x01: /* atoms */
/* check that the content is really 0-terminated */
- if ( value < bdf->strings_size &&
- ft_memchr( bdf->strings + value, 0, bdf->strings_size ) )
+ if ( value < bdf->strings_size &&
+ ft_memchr( bdf->strings + value, 0,
+ bdf->strings_size - value ) )
{
aprop->type = BDF_PROPERTY_TYPE_ATOM;
aprop->u.atom = (const char*)bdf->strings + value;
diff --git a/thirdparty/freetype/src/sfnt/ttcmap.c b/thirdparty/freetype/src/sfnt/ttcmap.c
index f5837e65e86..6fae7041b8e 100644
--- a/thirdparty/freetype/src/sfnt/ttcmap.c
+++ b/thirdparty/freetype/src/sfnt/ttcmap.c
@@ -17,6 +17,7 @@
#include <freetype/internal/ftdebug.h>
+#include <freetype/ttnameid.h>
#include "sferrors.h" /* must come before `ftvalid.h' */
@@ -2936,7 +2937,8 @@
* Ranges are sorted by `uniStart'.
*/
-#ifdef TT_CONFIG_CMAP_FORMAT_14
+#if defined( TT_CONFIG_CMAP_FORMAT_14 ) || \
+ defined( TT_CONFIG_CMAP_FORMAT_15 )
typedef struct TT_CMap14Rec_
{
@@ -3009,9 +3011,23 @@
}
- FT_CALLBACK_DEF( FT_Error )
- tt_cmap14_validate( FT_Byte* table,
- FT_Validator valid )
+ static FT_Bool
+ tt_cmap14_is_extended( FT_CMap cmap )
+ {
+ TT_CMap ttcmap = (TT_CMap)cmap;
+ TT_CMap_Class clazz = (TT_CMap_Class)( ttcmap->cmap_class
+ ? ttcmap->cmap_class
+ : cmap->clazz );
+
+
+ return FT_BOOL( clazz->format == 15 );
+ }
+
+
+ static FT_Error
+ tt_cmap14_validate_internal( FT_Byte* table,
+ FT_Validator valid,
+ FT_Bool is_extended )
{
FT_Byte* p;
FT_ULong length;
@@ -3092,9 +3108,10 @@
/* and the non-default table (these glyphs are specified here) */
if ( nondefOff != 0 )
{
- FT_Byte* ndp = table + nondefOff;
+ FT_Byte* ndp = table + nondefOff;
FT_ULong numMappings;
- FT_ULong i, lastUni = 0;
+ FT_UInt record_size = is_extended ? 6 : 5;
+ FT_ULong i, lastUni = 0;
if ( ndp + 4 > valid->limit )
@@ -3102,14 +3119,15 @@
numMappings = TT_NEXT_ULONG( ndp );
- /* numMappings * 5 > (FT_ULong)( valid->limit - ndp ) ? */
- if ( numMappings > ( (FT_ULong)( valid->limit - ndp ) ) / 5 )
+ if ( numMappings >
+ ( (FT_ULong)( valid->limit - ndp ) ) / record_size )
FT_INVALID_TOO_SHORT;
for ( i = 0; i < numMappings; i++ )
{
FT_ULong uni = TT_NEXT_UINT24( ndp );
- FT_ULong gid = TT_NEXT_USHORT( ndp );
+ FT_ULong gid = is_extended ? TT_NEXT_UINT24( ndp )
+ : TT_NEXT_USHORT( ndp );
if ( uni >= 0x110000UL ) /* end of Unicode */
@@ -3132,6 +3150,30 @@
}
+#ifdef TT_CONFIG_CMAP_FORMAT_14
+
+ FT_CALLBACK_DEF( FT_Error )
+ tt_cmap14_validate( FT_Byte* table,
+ FT_Validator valid )
+ {
+ return tt_cmap14_validate_internal( table, valid, FALSE );
+ }
+
+#endif /* TT_CONFIG_CMAP_FORMAT_14 */
+
+
+#ifdef TT_CONFIG_CMAP_FORMAT_15
+
+ FT_CALLBACK_DEF( FT_Error )
+ tt_cmap15_validate( FT_Byte* table,
+ FT_Validator valid )
+ {
+ return tt_cmap14_validate_internal( table, valid, TRUE );
+ }
+
+#endif /* TT_CONFIG_CMAP_FORMAT_15 */
+
+
FT_CALLBACK_DEF( FT_UInt )
tt_cmap14_char_index( FT_CMap cmap,
FT_UInt32 char_code )
@@ -3160,9 +3202,13 @@
tt_cmap14_get_info( FT_CharMap cmap,
TT_CMapInfo *cmap_info )
{
- FT_UNUSED( cmap );
+ TT_CMap ttcmap = (TT_CMap)cmap;
+ TT_CMap_Class clazz = (TT_CMap_Class)( ttcmap->cmap_class
+ ? ttcmap->cmap_class
+ : FT_CMAP( cmap )->clazz );
+
- cmap_info->format = 14;
+ cmap_info->format = clazz->format;
/* subtable 14 does not define a language field */
cmap_info->language = 0xFFFFFFFFUL;
@@ -3206,10 +3252,12 @@
static FT_UInt
tt_cmap14_char_map_nondef_binary( FT_Byte *base,
- FT_UInt32 char_code )
+ FT_UInt32 char_code,
+ FT_Bool is_extended )
{
FT_UInt32 numMappings = TT_PEEK_ULONG( base );
FT_UInt32 max, min;
+ FT_UInt record_size = is_extended ? 6 : 5;
min = 0;
@@ -3221,7 +3269,7 @@
while ( min < max )
{
FT_UInt32 mid = ( min + max ) >> 1;
- FT_Byte* p = base + 5 * mid;
+ FT_Byte* p = base + record_size * mid;
FT_UInt32 uni = (FT_UInt32)TT_NEXT_UINT24( p );
@@ -3230,7 +3278,7 @@
else if ( char_code > uni )
min = mid + 1;
else
- return TT_PEEK_USHORT( p );
+ return is_extended ? TT_PEEK_UINT24( p ) : TT_PEEK_USHORT( p );
}
return 0;
@@ -3278,6 +3326,7 @@
{
TT_CMap ttcmap = (TT_CMap)cmap;
TT_CMap ttucmap = (TT_CMap)ucmap;
+ FT_Bool is_extended = tt_cmap14_is_extended( cmap );
FT_Byte* p = tt_cmap14_find_variant( ttcmap->data + 6,
variantSelector );
@@ -3301,7 +3350,8 @@
if ( nondefOff != 0 )
return tt_cmap14_char_map_nondef_binary( ttcmap->data + nondefOff,
- charcode );
+ charcode,
+ is_extended );
return 0;
}
@@ -3313,6 +3363,7 @@
FT_UInt32 variantSelector )
{
TT_CMap ttcmap = (TT_CMap)cmap;
+ FT_Bool is_extended = tt_cmap14_is_extended( cmap );
FT_Byte* p = tt_cmap14_find_variant( ttcmap->data + 6,
variantSelector );
FT_ULong defOff;
@@ -3331,7 +3382,8 @@
if ( nondefOff != 0 &&
tt_cmap14_char_map_nondef_binary( ttcmap->data + nondefOff,
- charcode ) != 0 )
+ charcode,
+ is_extended ) != 0 )
return 0;
return -1;
@@ -3375,6 +3427,7 @@
FT_UInt32 count = cmap14->num_selectors;
FT_Byte* p = ttcmap->data + 10;
FT_UInt32* q;
+ FT_Bool is_extended = tt_cmap14_is_extended( cmap );
if ( tt_cmap14_ensure( cmap14, ( count + 1 ), memory ) )
@@ -3392,7 +3445,8 @@
charCode ) ) ||
( nondefOff != 0 &&
tt_cmap14_char_map_nondef_binary( ttcmap->data + nondefOff,
- charCode ) != 0 ) )
+ charCode,
+ is_extended ) != 0 ) )
{
q[0] = varSel;
q++;
@@ -3462,7 +3516,8 @@
static FT_UInt32*
tt_cmap14_get_nondef_chars( TT_CMap cmap,
FT_Byte *p,
- FT_Memory memory )
+ FT_Memory memory,
+ FT_Bool is_extended )
{
TT_CMap14 cmap14 = (TT_CMap14) cmap;
FT_UInt32 numMappings;
@@ -3479,7 +3534,7 @@
for ( i = 0; i < numMappings; i++ )
{
ret[i] = (FT_UInt32)TT_NEXT_UINT24( p );
- p += 2;
+ p += is_extended ? 3 : 2;
}
ret[i] = 0;
@@ -3495,6 +3550,7 @@
TT_CMap ttcmap = (TT_CMap)cmap;
FT_Byte *p = tt_cmap14_find_variant( ttcmap->data + 6,
variantSelector );
+ FT_Bool is_extended = tt_cmap14_is_extended( cmap );
FT_Int i;
FT_ULong defOff;
FT_ULong nondefOff;
@@ -3511,7 +3567,7 @@
if ( defOff == 0 )
return tt_cmap14_get_nondef_chars( ttcmap, ttcmap->data + nondefOff,
- memory );
+ memory, is_extended );
else if ( nondefOff == 0 )
return tt_cmap14_get_def_chars( ttcmap, ttcmap->data + defOff,
memory );
@@ -3543,7 +3599,7 @@
memory );
if ( dcnt == 0 )
return tt_cmap14_get_nondef_chars( ttcmap, ttcmap->data + nondefOff,
- memory );
+ memory, is_extended );
if ( tt_cmap14_ensure( cmap14, ( dcnt + numMappings + 1 ), memory ) )
return NULL;
@@ -3553,7 +3609,7 @@
dcnt = FT_NEXT_BYTE( dp );
di = 1;
nuni = (FT_UInt32)TT_NEXT_UINT24( p );
- p += 2;
+ p += is_extended ? 3 : 2;
ni = 1;
i = 0;
@@ -3583,7 +3639,7 @@
break;
nuni = (FT_UInt32)TT_NEXT_UINT24( p );
- p += 2;
+ p += is_extended ? 3 : 2;
}
}
@@ -3596,7 +3652,7 @@
while ( ni < numMappings )
{
ret[i++] = (FT_UInt32)TT_NEXT_UINT24( p );
- p += 2;
+ p += is_extended ? 3 : 2;
ni++;
}
}
@@ -3626,6 +3682,8 @@
}
+#ifdef TT_CONFIG_CMAP_FORMAT_14
+
FT_DEFINE_TT_CMAP(
tt_cmap14_class_rec,
@@ -3651,6 +3709,35 @@
#endif /* TT_CONFIG_CMAP_FORMAT_14 */
+#ifdef TT_CONFIG_CMAP_FORMAT_15
+
+ FT_DEFINE_TT_CMAP(
+ tt_cmap15_class_rec,
+
+ sizeof ( TT_CMap14Rec ),
+
+ (FT_CMap_InitFunc) tt_cmap14_init, /* init */
+ (FT_CMap_DoneFunc) tt_cmap14_done, /* done */
+ (FT_CMap_CharIndexFunc)tt_cmap14_char_index, /* char_index */
+ (FT_CMap_CharNextFunc) tt_cmap14_char_next, /* char_next */
+
+ /* Format 15 extension functions */
+ (FT_CMap_CharVarIndexFunc) tt_cmap14_char_var_index,
+ (FT_CMap_CharVarIsDefaultFunc)tt_cmap14_char_var_isdefault,
+ (FT_CMap_VariantListFunc) tt_cmap14_variants,
+ (FT_CMap_CharVariantListFunc) tt_cmap14_char_variants,
+ (FT_CMap_VariantCharListFunc) tt_cmap14_variant_chars,
+
+ 15,
+ (TT_CMap_ValidateFunc)tt_cmap15_validate, /* validate */
+ (TT_CMap_Info_GetFunc)tt_cmap14_get_info /* get_cmap_info */
+ )
+
+#endif /* TT_CONFIG_CMAP_FORMAT_15 */
+
+#endif /* TT_CONFIG_CMAP_FORMAT_14 || TT_CONFIG_CMAP_FORMAT_15 */
+
+
/*************************************************************************/
/*************************************************************************/
/***** *****/
@@ -3773,22 +3860,506 @@
};
- /* parse the `cmap' table and build the corresponding TT_CMap objects */
- /* in the current face */
- /* */
- FT_LOCAL_DEF( FT_Error )
- tt_face_build_cmaps( TT_Face face )
+ static void
+ tt_cmap_free( FT_CMap cmap )
+ {
+ FT_Memory memory = FT_FACE_MEMORY( cmap->charmap.face );
+
+
+ if ( cmap->clazz->done )
+ cmap->clazz->done( cmap );
+
+ FT_FREE( cmap );
+ }
+
+
+ static FT_Error
+ tt_cmap_new( FT_CMap_Class clazz,
+ FT_Pointer init_data,
+ FT_CharMap charmap,
+ FT_CMap *acmap )
+ {
+ FT_Error error = FT_Err_Ok;
+ FT_Memory memory = FT_FACE_MEMORY( charmap->face );
+ FT_CMap cmap = NULL;
+
+
+ if ( !FT_ALLOC( cmap, clazz->size ) )
+ {
+ cmap->charmap = *charmap;
+ cmap->clazz = clazz;
+
+ if ( clazz->init )
+ {
+ error = clazz->init( cmap, init_data );
+ if ( error )
+ goto Fail;
+ }
+ }
+
+ *acmap = cmap;
+ return error;
+
+ Fail:
+ tt_cmap_free( cmap );
+ *acmap = NULL;
+ return error;
+ }
+
+
+ static FT_Error
+ tt_face_add_dmap( TT_Face face,
+ FT_CMap_Class clazz,
+ FT_Pointer init_data,
+ FT_CharMap charmap,
+ FT_Int flags )
+ {
+ FT_Error error;
+ FT_Memory memory = FT_FACE_MEMORY( face );
+ FT_CMap cmap;
+ FT_UInt count = face->num_dmap_charmaps;
+
+
+ error = tt_cmap_new( clazz, init_data, charmap, &cmap );
+ if ( error )
+ return error;
+
+ ((TT_CMap)cmap)->flags = flags;
+
+ if ( FT_QRENEW_ARRAY( face->dmap_charmaps, count, count + 1 ) )
+ {
+ tt_cmap_free( cmap );
+ return error;
+ }
+
+ face->dmap_charmaps[count] = cmap;
+ face->num_dmap_charmaps = count + 1;
+
+ return FT_Err_Ok;
+ }
+
+
+ static FT_Bool
+ tt_cmap_is_variation( FT_CMap cmap )
+ {
+ TT_CMap ttcmap = (TT_CMap)cmap;
+ TT_CMap_Class clazz = (TT_CMap_Class)( ttcmap->cmap_class
+ ? ttcmap->cmap_class
+ : cmap->clazz );
+
+
+ return FT_BOOL( clazz->format == 14 || clazz->format == 15 );
+ }
+
+
+ static FT_CMap
+ tt_face_find_unicode_dmap( TT_Face face )
+ {
+ FT_CMap* first = face->dmap_charmaps;
+ FT_CMap* cur;
+
+
+ if ( !first )
+ return NULL;
+
+ cur = first + face->num_dmap_charmaps;
+ for ( ; --cur >= first; )
+ {
+ FT_CMap cmap = cur[0];
+
+
+ if ( !cmap ||
+ tt_cmap_is_variation( cmap ) ||
+ cmap->charmap.encoding != FT_ENCODING_UNICODE )
+ continue;
+
+ if ( ( cmap->charmap.platform_id == TT_PLATFORM_MICROSOFT &&
+ cmap->charmap.encoding_id == TT_MS_ID_UCS_4 ) ||
+ ( cmap->charmap.platform_id == TT_PLATFORM_APPLE_UNICODE &&
+ cmap->charmap.encoding_id == TT_APPLE_ID_UNICODE_32 ) ||
+ ( cmap->charmap.platform_id == TT_PLATFORM_APPLE_UNICODE &&
+ cmap->charmap.encoding_id == TT_APPLE_ID_FULL_UNICODE &&
+ ( (TT_CMap_Class)cmap->clazz )->format == 13 ) )
+ return cmap;
+ }
+
+ cur = first + face->num_dmap_charmaps;
+ for ( ; --cur >= first; )
+ {
+ FT_CMap cmap = cur[0];
+
+
+ if ( cmap &&
+ !tt_cmap_is_variation( cmap ) &&
+ cmap->charmap.encoding == FT_ENCODING_UNICODE )
+ return cmap;
+ }
+
+ return NULL;
+ }
+
+
+ static FT_CMap
+ tt_face_find_dmap( TT_Face face,
+ FT_CharMap charmap )
+ {
+ FT_CMap* first = face->dmap_charmaps;
+ FT_CMap* cur;
+
+
+ if ( !first )
+ return NULL;
+
+ if ( tt_cmap_is_variation( FT_CMAP( charmap ) ) )
+ {
+ cur = first + face->num_dmap_charmaps;
+ for ( ; --cur >= first; )
+ if ( cur[0] && tt_cmap_is_variation( cur[0] ) )
+ return cur[0];
+
+ return NULL;
+ }
+
+ if ( charmap->encoding == FT_ENCODING_UNICODE )
+ return tt_face_find_unicode_dmap( face );
+
+ cur = first + face->num_dmap_charmaps;
+ for ( ; --cur >= first; )
+ {
+ FT_CMap cmap = cur[0];
+
+
+ if ( cmap &&
+ !tt_cmap_is_variation( cmap ) &&
+ cmap->charmap.platform_id == charmap->platform_id &&
+ cmap->charmap.encoding_id == charmap->encoding_id )
+ return cmap;
+ }
+
+ cur = first + face->num_dmap_charmaps;
+ for ( ; --cur >= first; )
+ {
+ FT_CMap cmap = cur[0];
+
+
+ if ( cmap &&
+ !tt_cmap_is_variation( cmap ) &&
+ cmap->charmap.encoding == charmap->encoding )
+ return cmap;
+ }
+
+ return NULL;
+ }
+
+
+ FT_CALLBACK_DEF( void )
+ tt_cmap_dmap_done( FT_CMap cmap )
+ {
+ TT_CMap ttcmap = (TT_CMap)cmap;
+ FT_CMap_Class clazz = ttcmap->cmap_class;
+
+
+ cmap->clazz = clazz;
+ if ( clazz->done )
+ clazz->done( cmap );
+ }
+
+
+ FT_CALLBACK_DEF( FT_UInt )
+ tt_cmap_dmap_char_index( FT_CMap cmap,
+ FT_UInt32 char_code )
+ {
+ TT_CMap ttcmap = (TT_CMap)cmap;
+ FT_UInt result;
+
+
+ result = ttcmap->dmap->clazz->char_index( ttcmap->dmap, char_code );
+ if ( !result )
+ result = ttcmap->cmap_class->char_index( cmap, char_code );
+
+ return result;
+ }
+
+
+ FT_CALLBACK_DEF( FT_UInt )
+ tt_cmap_dmap_char_next( FT_CMap cmap,
+ FT_UInt32 *achar_code )
+ {
+ TT_CMap ttcmap = (TT_CMap)cmap;
+
+ FT_UInt32 dmap_char = *achar_code;
+ FT_UInt32 cmap_char = *achar_code;
+
+ FT_UInt dmap_glyph;
+ FT_UInt cmap_glyph;
+
+
+ dmap_glyph = ttcmap->dmap->clazz->char_next( ttcmap->dmap, &dmap_char );
+ cmap_glyph = ttcmap->cmap_class->char_next( cmap, &cmap_char );
+
+ if ( dmap_glyph && ( !cmap_glyph || dmap_char <= cmap_char ) )
+ {
+ *achar_code = dmap_char;
+ return dmap_glyph;
+ }
+
+ *achar_code = cmap_char;
+ return cmap_glyph;
+ }
+
+
+ FT_CALLBACK_DEF( FT_UInt )
+ tt_cmap_dmap_char_var_index( FT_CMap cmap,
+ FT_CMap unicode_cmap,
+ FT_UInt32 char_code,
+ FT_UInt32 variant_selector )
+ {
+ TT_CMap ttcmap = (TT_CMap)cmap;
+ FT_CMap_Class clazz = ttcmap->dmap->clazz;
+ FT_Int is_default;
+
+
+ if ( clazz->char_var_default )
+ {
+ is_default = clazz->char_var_default( ttcmap->dmap,
+ char_code,
+ variant_selector );
+ if ( is_default == 1 )
+ return unicode_cmap->clazz->char_index( unicode_cmap, char_code );
+ if ( is_default == 0 )
+ return clazz->char_var_index( ttcmap->dmap, unicode_cmap,
+ char_code, variant_selector );
+ }
+ else if ( clazz->char_var_index )
+ {
+ FT_UInt result = clazz->char_var_index( ttcmap->dmap,
+ unicode_cmap,
+ char_code,
+ variant_selector );
+
+
+ if ( result )
+ return result;
+ }
+
+ clazz = ttcmap->cmap_class;
+ if ( clazz->char_var_index )
+ return clazz->char_var_index( cmap, unicode_cmap,
+ char_code, variant_selector );
+
+ return 0;
+ }
+
+
+ FT_CALLBACK_DEF( FT_Int )
+ tt_cmap_dmap_char_var_isdefault( FT_CMap cmap,
+ FT_UInt32 char_code,
+ FT_UInt32 variant_selector )
+ {
+ TT_CMap ttcmap = (TT_CMap)cmap;
+ FT_CMap_Class clazz = ttcmap->dmap->clazz;
+ FT_Int result = -1;
+
+
+ if ( clazz->char_var_default )
+ result = clazz->char_var_default( ttcmap->dmap,
+ char_code, variant_selector );
+
+ clazz = ttcmap->cmap_class;
+ if ( result == -1 && clazz->char_var_default )
+ result = clazz->char_var_default( cmap, char_code,
+ variant_selector );
+
+ return result;
+ }
+
+
+ static FT_UInt32*
+ tt_cmap_dmap_merge( TT_CMap cmap,
+ FT_UInt32 *dmap_list,
+ FT_UInt32 *cmap_list )
+ {
+ TT_Face face = (TT_Face)cmap->cmap.charmap.face;
+ FT_Memory memory = FT_FACE_MEMORY( face );
+ FT_Error error = FT_Err_Ok;
+
+ FT_UInt32 dmap_count = 0;
+ FT_UInt32 cmap_count = 0;
+ FT_UInt32 old_max = face->dmap_max_results;
+
+ FT_UInt32 i = 0;
+ FT_UInt32 j = 0;
+ FT_UInt32 k = 0;
+
+
+ if ( dmap_list )
+ while ( dmap_list[dmap_count] )
+ dmap_count++;
+ if ( cmap_list )
+ while ( cmap_list[cmap_count] )
+ cmap_count++;
+
+ if ( !dmap_count && !cmap_count )
+ return NULL;
+
+ if ( dmap_count >= 0xFFFFFFFFUL - cmap_count )
+ return NULL;
+
+ if ( dmap_count + cmap_count + 1 > face->dmap_max_results )
+ {
+ if ( FT_QRENEW_ARRAY( face->dmap_results, old_max,
+ dmap_count + cmap_count + 1 ) )
+ return NULL;
+
+ face->dmap_max_results = dmap_count + cmap_count + 1;
+ }
+
+ while ( i < dmap_count && j < cmap_count )
+ {
+ if ( dmap_list[i] < cmap_list[j] )
+ face->dmap_results[k++] = dmap_list[i++];
+ else if ( cmap_list[j] < dmap_list[i] )
+ face->dmap_results[k++] = cmap_list[j++];
+ else
+ {
+ face->dmap_results[k++] = dmap_list[i++];
+ j++;
+ }
+ }
+
+ while ( i < dmap_count )
+ face->dmap_results[k++] = dmap_list[i++];
+ while ( j < cmap_count )
+ face->dmap_results[k++] = cmap_list[j++];
+
+ face->dmap_results[k] = 0;
+ return face->dmap_results;
+ }
+
+
+ FT_CALLBACK_DEF( FT_UInt32* )
+ tt_cmap_dmap_variants( FT_CMap cmap,
+ FT_Memory memory )
+ {
+ TT_CMap ttcmap = (TT_CMap)cmap;
+
+ FT_CMap_Class dclazz = ttcmap->dmap->clazz;
+ FT_CMap_Class cclazz = ttcmap->cmap_class;
+
+ FT_UInt32* dmap_list = NULL;
+ FT_UInt32* cmap_list = NULL;
+
+
+ if ( dclazz->variant_list )
+ dmap_list = dclazz->variant_list( ttcmap->dmap, memory );
+ if ( cclazz->variant_list )
+ cmap_list = cclazz->variant_list( cmap, memory );
+
+ return tt_cmap_dmap_merge( ttcmap, dmap_list, cmap_list );
+ }
+
+
+ FT_CALLBACK_DEF( FT_UInt32* )
+ tt_cmap_dmap_char_variants( FT_CMap cmap,
+ FT_Memory memory,
+ FT_UInt32 char_code )
+ {
+ TT_CMap ttcmap = (TT_CMap)cmap;
+
+ FT_CMap_Class dclazz = ttcmap->dmap->clazz;
+ FT_CMap_Class cclazz = ttcmap->cmap_class;
+
+ FT_UInt32* dmap_list = NULL;
+ FT_UInt32* cmap_list = NULL;
+
+
+ if ( dclazz->charvariant_list )
+ dmap_list = dclazz->charvariant_list( ttcmap->dmap, memory,
+ char_code );
+ if ( cclazz->charvariant_list )
+ cmap_list = cclazz->charvariant_list( cmap, memory, char_code );
+
+ return tt_cmap_dmap_merge( ttcmap, dmap_list, cmap_list );
+ }
+
+
+ FT_CALLBACK_DEF( FT_UInt32* )
+ tt_cmap_dmap_variant_chars( FT_CMap cmap,
+ FT_Memory memory,
+ FT_UInt32 variant_selector )
+ {
+ TT_CMap ttcmap = (TT_CMap)cmap;
+
+ FT_CMap_Class dclazz = ttcmap->dmap->clazz;
+ FT_CMap_Class cclazz = ttcmap->cmap_class;
+
+ FT_UInt32* dmap_list = NULL;
+ FT_UInt32* cmap_list = NULL;
+
+
+ if ( dclazz->variantchar_list )
+ dmap_list = dclazz->variantchar_list( ttcmap->dmap, memory,
+ variant_selector );
+ if ( cclazz->variantchar_list )
+ cmap_list = cclazz->variantchar_list( cmap, memory,
+ variant_selector );
+
+ return tt_cmap_dmap_merge( ttcmap, dmap_list, cmap_list );
+ }
+
+
+ FT_CALLBACK_DEF( FT_Error )
+ tt_cmap_dmap_get_info( FT_CharMap charmap,
+ TT_CMapInfo *cmap_info )
+ {
+ TT_CMap cmap = (TT_CMap)charmap;
+ TT_CMap_Class clazz = (TT_CMap_Class)cmap->cmap_class;
+
+
+ if ( clazz->get_cmap_info )
+ return clazz->get_cmap_info( charmap, cmap_info );
+
+ return FT_THROW( Invalid_CharMap_Format );
+ }
+
+
+ FT_DEFINE_TT_CMAP(
+ tt_cmap_dmap_class_rec,
+
+ sizeof ( TT_CMapRec ),
+
+ (FT_CMap_InitFunc) NULL, /* init */
+ (FT_CMap_DoneFunc) tt_cmap_dmap_done, /* done */
+ (FT_CMap_CharIndexFunc)tt_cmap_dmap_char_index, /* char_index */
+ (FT_CMap_CharNextFunc) tt_cmap_dmap_char_next, /* char_next */
+
+ (FT_CMap_CharVarIndexFunc) tt_cmap_dmap_char_var_index,
+ (FT_CMap_CharVarIsDefaultFunc)tt_cmap_dmap_char_var_isdefault,
+ (FT_CMap_VariantListFunc) tt_cmap_dmap_variants,
+ (FT_CMap_CharVariantListFunc) tt_cmap_dmap_char_variants,
+ (FT_CMap_VariantCharListFunc) tt_cmap_dmap_variant_chars,
+
+ 0,
+ (TT_CMap_ValidateFunc)NULL,
+ (TT_CMap_Info_GetFunc)tt_cmap_dmap_get_info
+ )
+
+
+ /* parse a `cmap'-shaped table and build the corresponding TT_CMap */
+ /* objects in the current face */
+ /* */
+ static FT_Error
+ tt_face_build_cmaps_from_table( TT_Face face,
+ FT_Byte* table,
+ FT_ULong table_size,
+ FT_Bool is_dmap )
{
- FT_Byte* const table = face->cmap_table;
FT_Byte* limit;
FT_UInt volatile num_cmaps;
- FT_Byte* volatile p = table;
- FT_Library library = FT_FACE_LIBRARY( face );
-
- FT_UNUSED( library );
+ FT_Byte* volatile p = table;
- if ( !p || face->cmap_size < 4 )
+ if ( !p || table_size < 4 )
return FT_THROW( Invalid_Table );
/* Version 1.8.3 of the OpenType specification contains the following */
@@ -3805,7 +4376,7 @@
num_cmaps = TT_NEXT_USHORT( p );
FT_TRACE4(( "tt_face_build_cmaps: %u cmaps\n", num_cmaps ));
- limit = table + face->cmap_size;
+ limit = table + table_size;
for ( ; num_cmaps > 0 && p + 8 <= limit; num_cmaps-- )
{
FT_CharMapRec charmap;
@@ -3818,7 +4389,7 @@
charmap.encoding = FT_ENCODING_NONE; /* will be filled later */
offset = TT_NEXT_ULONG( p );
- if ( offset && offset <= face->cmap_size - 2 )
+ if ( offset && offset <= table_size - 2 )
{
FT_Byte* volatile cmap = table + offset;
volatile FT_UInt format = TT_PEEK_USHORT( cmap );
@@ -3838,7 +4409,7 @@
ft_validator_init( FT_VALIDATOR( &valid ), cmap, limit,
FT_VALIDATE_DEFAULT );
- valid.num_glyphs = (FT_UInt)face->max_profile.numGlyphs;
+ valid.num_glyphs = face->maxp_num_glyphs;
if ( ft_setjmp( FT_VALIDATOR( &valid )->jump_buffer) == 0 )
{
@@ -3848,20 +4419,31 @@
if ( !valid.validator.error )
{
- FT_CMap ttcmap;
+ FT_CMap ttcmap;
+ FT_Error new_error;
/* It might make sense to store the single variation */
/* selector cmap somewhere special. But it would have to be */
/* in the public FT_FaceRec, and we can't change that. */
- if ( !FT_CMap_New( (FT_CMap_Class)clazz,
- cmap, &charmap, &ttcmap ) )
+ if ( is_dmap )
+ new_error = tt_face_add_dmap( face, (FT_CMap_Class)clazz,
+ cmap, &charmap,
+ (FT_Int)error );
+ else
+ new_error = FT_CMap_New( (FT_CMap_Class)clazz,
+ cmap, &charmap, &ttcmap );
+
+ if ( !new_error && !is_dmap )
{
/* it is simpler to directly set `flags' than adding */
/* a parameter to FT_CMap_New */
((TT_CMap)ttcmap)->flags = (FT_Int)error;
}
+
+ if ( new_error && FT_ERR_EQ( new_error, Out_Of_Memory ) )
+ return new_error;
}
else
{
@@ -3884,6 +4466,162 @@
}
+ /* parse the 'cmap' and 'DMAP' tables */
+ FT_LOCAL_DEF( FT_Error )
+ tt_face_build_cmaps( TT_Face face )
+ {
+ FT_Error error;
+
+
+ error = tt_face_build_cmaps_from_table( face,
+ face->cmap_table,
+ face->cmap_size,
+ FALSE );
+ if ( error || !face->dmap_table )
+ return error;
+
+ return tt_face_build_cmaps_from_table( face,
+ face->dmap_table,
+ face->dmap_size,
+ TRUE );
+ }
+
+
+ static FT_Error
+ tt_face_expose_dmap( TT_Face face,
+ FT_CMap dmap )
+ {
+ FT_Face root = FT_FACE( face );
+ FT_Memory memory = FT_FACE_MEMORY( face );
+ FT_Error error = FT_Err_Ok;
+
+ FT_Int count = root->num_charmaps;
+ FT_UInt i;
+
+
+ if ( FT_QRENEW_ARRAY( root->charmaps, count, count + 1 ) )
+ return error;
+
+ root->charmaps[count] = &dmap->charmap;
+ root->num_charmaps = count + 1;
+
+ for ( i = 0; i < face->num_dmap_charmaps; i++ )
+ if ( face->dmap_charmaps[i] == dmap )
+ {
+ face->dmap_charmaps[i] = NULL;
+ break;
+ }
+
+ return FT_Err_Ok;
+ }
+
+
+ FT_LOCAL_DEF( FT_Error )
+ tt_face_build_dmaps( TT_Face face )
+ {
+ FT_Face root = FT_FACE( face );
+ FT_Error error;
+
+ FT_Int num_cmaps = root->num_charmaps;
+ FT_Int i;
+ FT_UInt j;
+
+
+ if ( !face->num_dmap_charmaps )
+ return FT_Err_Ok;
+
+ /* Apply the best matching DMAP subtable to every selectable cmap. */
+ for ( i = 0; i < num_cmaps; i++ )
+ {
+ FT_CMap cmap = FT_CMAP( root->charmaps[i] );
+ FT_CMap dmap = tt_face_find_dmap( face, &cmap->charmap );
+
+
+ if ( dmap )
+ {
+ TT_CMap ttcmap = (TT_CMap)cmap;
+
+
+ ttcmap->cmap_class = cmap->clazz;
+ ttcmap->dmap = dmap;
+ cmap->clazz = (FT_CMap_Class)&tt_cmap_dmap_class_rec;
+ }
+ }
+
+ /* If DMAP supplies an encoding absent from cmap, expose it directly. */
+ for ( j = 0; j < face->num_dmap_charmaps; j++ )
+ {
+ FT_CMap dmap = face->dmap_charmaps[j];
+ FT_Bool found = FALSE;
+
+
+ if ( !dmap || tt_cmap_is_variation( dmap ) )
+ continue;
+
+ for ( i = 0; i < root->num_charmaps; i++ )
+ {
+ FT_CMap cmap = FT_CMAP( root->charmaps[i] );
+
+
+ if ( !tt_cmap_is_variation( cmap ) &&
+ cmap->charmap.encoding == dmap->charmap.encoding )
+ {
+ found = TRUE;
+ break;
+ }
+ }
+
+ if ( !found )
+ {
+ FT_CMap best = dmap;
+
+
+ if ( dmap->charmap.encoding == FT_ENCODING_UNICODE )
+ best = tt_face_find_unicode_dmap( face );
+
+ error = tt_face_expose_dmap( face, best );
+ if ( error )
+ return error;
+ }
+ }
+
+ /* A variation subtable is not selectable as an ordinary charmap. */
+ for ( i = 0; i < root->num_charmaps; i++ )
+ if ( tt_cmap_is_variation( FT_CMAP( root->charmaps[i] ) ) )
+ return FT_Err_Ok;
+
+ for ( j = 0; j < face->num_dmap_charmaps; j++ )
+ {
+ FT_CMap dmap = face->dmap_charmaps[j];
+
+
+ if ( dmap && tt_cmap_is_variation( dmap ) )
+ return tt_face_expose_dmap( face, dmap );
+ }
+
+ return FT_Err_Ok;
+ }
+
+
+ FT_LOCAL_DEF( void )
+ tt_face_done_dmaps( TT_Face face )
+ {
+ FT_Memory memory = FT_FACE_MEMORY( face );
+ FT_UInt i;
+
+
+ for ( i = 0; i < face->num_dmap_charmaps; i++ )
+ if ( face->dmap_charmaps[i] )
+ tt_cmap_free( face->dmap_charmaps[i] );
+
+ FT_FREE( face->dmap_charmaps );
+ face->num_dmap_charmaps = 0;
+
+ FT_FREE( face->dmap_results );
+ face->dmap_max_results = 0;
+ }
+
+
FT_LOCAL_DEF( FT_Error )
tt_get_cmap_info( FT_CharMap charmap,
TT_CMapInfo *cmap_info )
diff --git a/thirdparty/freetype/src/sfnt/ttcmap.h b/thirdparty/freetype/src/sfnt/ttcmap.h
index fdb34f525e3..4b8cbb3197a 100644
--- a/thirdparty/freetype/src/sfnt/ttcmap.h
+++ b/thirdparty/freetype/src/sfnt/ttcmap.h
@@ -32,9 +32,11 @@ FT_BEGIN_HEADER
typedef struct TT_CMapRec_
{
- FT_CMapRec cmap;
- FT_Byte* data; /* pointer to in-memory cmap table */
- FT_Int flags; /* for format 4 only */
+ FT_CMapRec cmap;
+ FT_Byte* data; /* pointer to in-memory cmap table */
+ FT_Int flags; /* for format 4 only */
+ FT_CMap_Class cmap_class; /* class before applying 'DMAP' */
+ FT_CMap dmap; /* overriding 'DMAP' subtable */
} TT_CMapRec, *TT_CMap;
@@ -112,6 +114,12 @@ FT_BEGIN_HEADER
FT_LOCAL( FT_Error )
tt_face_build_cmaps( TT_Face face );
+ FT_LOCAL( FT_Error )
+ tt_face_build_dmaps( TT_Face face );
+
+ FT_LOCAL( void )
+ tt_face_done_dmaps( TT_Face face );
+
/* used in tt-cmaps service */
FT_LOCAL( FT_Error )
tt_get_cmap_info( FT_CharMap charmap,
diff --git a/thirdparty/freetype/src/sfnt/ttcmapc.h b/thirdparty/freetype/src/sfnt/ttcmapc.h
index c17ac5bba15..a704690dae6 100644
--- a/thirdparty/freetype/src/sfnt/ttcmapc.h
+++ b/thirdparty/freetype/src/sfnt/ttcmapc.h
@@ -52,5 +52,9 @@
TTCMAPCITEM( tt_cmap14_class_rec )
#endif
+#ifdef TT_CONFIG_CMAP_FORMAT_15
+ TTCMAPCITEM( tt_cmap15_class_rec )
+#endif
+
/* END */
diff --git a/thirdparty/freetype/src/sfnt/ttcolr.c b/thirdparty/freetype/src/sfnt/ttcolr.c
index b22e279b346..2272ec3739a 100644
--- a/thirdparty/freetype/src/sfnt/ttcolr.c
+++ b/thirdparty/freetype/src/sfnt/ttcolr.c
@@ -85,6 +85,7 @@
FT_COLR_PAINTFORMAT_INTERNAL_VAR_SKEW = 29,
FT_COLR_PAINTFORMAT_INTERNAL_SKEW_CENTER = 30,
FT_COLR_PAINTFORMAT_INTERNAL_VAR_SKEW_CENTER = 31,
+ FT_COLR_PAINTFORMAT_INTERNAL_GLYPH2 = 33,
} FT_PaintFormat_Internal;
@@ -645,7 +646,9 @@
apaint->format = (FT_PaintFormat)FT_NEXT_BYTE( p );
- if ( apaint->format >= FT_COLR_PAINT_FORMAT_MAX )
+ if ( apaint->format >= FT_COLR_PAINT_FORMAT_MAX &&
+ (FT_PaintFormat_Internal)apaint->format !=
+ FT_COLR_PAINTFORMAT_INTERNAL_GLYPH2 )
return 0;
if ( apaint->format == FT_COLR_PAINTFORMAT_COLR_LAYERS )
@@ -879,12 +882,21 @@
return 1;
}
- if ( apaint->format == FT_COLR_PAINTFORMAT_GLYPH )
+ if ( apaint->format == FT_COLR_PAINTFORMAT_GLYPH ||
+ (FT_PaintFormat_Internal)apaint->format ==
+ FT_COLR_PAINTFORMAT_INTERNAL_GLYPH2 )
{
- ENSURE_READ_BYTES( 2 );
+ FT_Bool is_glyph2 = (FT_PaintFormat_Internal)apaint->format ==
+ FT_COLR_PAINTFORMAT_INTERNAL_GLYPH2;
+
+
+ ENSURE_READ_BYTES( ( is_glyph2 ? 3 : 2 ) );
apaint->u.glyph.paint.p = child_table_p;
apaint->u.glyph.paint.insert_root_transform = 0;
- apaint->u.glyph.glyphID = FT_NEXT_USHORT( p );
+ apaint->u.glyph.glyphID = is_glyph2 ? FT_NEXT_UOFF3( p )
+ : FT_NEXT_USHORT( p );
+
+ apaint->format = FT_COLR_PAINTFORMAT_GLYPH;
return 1;
}
@@ -1862,39 +1874,78 @@
alpha = face->palette[color_index].alpha;
}
- /* XXX Convert if srcSlot.bitmap is not grey? */
src = srcSlot->bitmap.buffer;
dst = dstSlot->bitmap.buffer +
dstSlot->bitmap.pitch * ( dstSlot->bitmap_top - srcSlot->bitmap_top ) +
4 * ( srcSlot->bitmap_left - dstSlot->bitmap_left );
- for ( y = 0; y < srcSlot->bitmap.rows; y++ )
+ switch ( srcSlot->bitmap.pixel_mode )
{
- for ( x = 0; x < srcSlot->bitmap.width; x++ )
- {
- int aa = src[x];
- int fa = alpha * aa / 255;
+ case FT_PIXEL_MODE_GRAY:
+ for ( y = 0; y < srcSlot->bitmap.rows; y++ )
+ {
+ for ( x = 0; x < srcSlot->bitmap.width; x++ )
+ {
+ int aa = src[x];
+ int fa = alpha * aa / 255;
- int fb = b * fa / 255;
- int fg = g * fa / 255;
- int fr = r * fa / 255;
+ int fb = b * fa / 255;
+ int fg = g * fa / 255;
+ int fr = r * fa / 255;
- int ba2 = 255 - fa;
+ int ba2 = 255 - fa;
- int bb = dst[4 * x + 0];
- int bg = dst[4 * x + 1];
- int br = dst[4 * x + 2];
- int ba = dst[4 * x + 3];
+ int bb = dst[4 * x + 0];
+ int bg = dst[4 * x + 1];
+ int br = dst[4 * x + 2];
+ int ba = dst[4 * x + 3];
+ dst[4 * x + 0] = (FT_Byte)( bb * ba2 / 255 + fb );
+ dst[4 * x + 1] = (FT_Byte)( bg * ba2 / 255 + fg );
+ dst[4 * x + 2] = (FT_Byte)( br * ba2 / 255 + fr );
+ dst[4 * x + 3] = (FT_Byte)( ba * ba2 / 255 + fa );
+ }
- dst[4 * x + 0] = (FT_Byte)( bb * ba2 / 255 + fb );
- dst[4 * x + 1] = (FT_Byte)( bg * ba2 / 255 + fg );
- dst[4 * x + 2] = (FT_Byte)( br * ba2 / 255 + fr );
- dst[4 * x + 3] = (FT_Byte)( ba * ba2 / 255 + fa );
- }
+ src += srcSlot->bitmap.pitch;
+ dst += dstSlot->bitmap.pitch;
+ }
+ break;
+
+ case FT_PIXEL_MODE_MONO:
+ {
+ int fb = b * alpha / 255;
+ int fg = g * alpha / 255;
+ int fr = r * alpha / 255;
+
+ int ba2 = 255 - alpha;
+
+ for ( y = 0; y < srcSlot->bitmap.rows; y++ )
+ {
+ for ( x = 0; x < srcSlot->bitmap.width; x++ )
+ {
+ if ( src[x >> 3] & ( 0x80 >> ( x & 7 ) ) )
+ {
+ int bb = dst[4 * x + 0];
+ int bg = dst[4 * x + 1];
+ int br = dst[4 * x + 2];
+ int ba = dst[4 * x + 3];
+
+ dst[4 * x + 0] = (FT_Byte)( bb * ba2 / 255 + fb );
+ dst[4 * x + 1] = (FT_Byte)( bg * ba2 / 255 + fg );
+ dst[4 * x + 2] = (FT_Byte)( br * ba2 / 255 + fr );
+ dst[4 * x + 3] = (FT_Byte)( ba * ba2 / 255 + alpha );
+ }
+ }
+
+ src += srcSlot->bitmap.pitch;
+ dst += dstSlot->bitmap.pitch;
+ }
+ }
+ break;
- src += srcSlot->bitmap.pitch;
- dst += dstSlot->bitmap.pitch;
+ default:
+ /* Reject pixel modes other than GRAY/MONO. */
+ return FT_Err_Invalid_Glyph_Format;
}
return FT_Err_Ok;
diff --git a/thirdparty/freetype/src/sfnt/ttload.c b/thirdparty/freetype/src/sfnt/ttload.c
index e17d07a971c..74d319e01f1 100644
--- a/thirdparty/freetype/src/sfnt/ttload.c
+++ b/thirdparty/freetype/src/sfnt/ttload.c
@@ -227,7 +227,9 @@
/* ignore in general). */
if ( table.Tag == TTAG_hmtx ||
- table.Tag == TTAG_vmtx )
+ table.Tag == TTAG_HMTX ||
+ table.Tag == TTAG_vmtx ||
+ table.Tag == TTAG_VMTX )
valid_entries++;
else
{
@@ -439,7 +441,9 @@
else if ( entry.Length > stream->size - entry.Offset )
{
if ( entry.Tag == TTAG_hmtx ||
- entry.Tag == TTAG_vmtx )
+ entry.Tag == TTAG_HMTX ||
+ entry.Tag == TTAG_vmtx ||
+ entry.Tag == TTAG_VMTX )
{
#ifdef FT_DEBUG_LEVEL_TRACE
FT_ULong old_length = entry.Length;
@@ -749,7 +753,8 @@
FT_Stream stream )
{
FT_Error error;
- TT_MaxProfile* maxProfile = &face->max_profile;
+ TT_MaxProfile* maxProfile = &face->max_profile;
+ FT_Bool is_extended = FALSE;
static const FT_Frame_Field maxp_fields[] =
{
@@ -782,12 +787,37 @@
};
- error = face->goto_table( face, TTAG_maxp, stream, 0 );
+ face->maxp_num_glyphs = 0;
+
+ error = face->goto_table( face, TTAG_MAXP, stream, 0 );
+ if ( !error )
+ is_extended = TRUE;
+ else if ( FT_ERR_EQ( error, Table_Missing ) )
+ error = face->goto_table( face, TTAG_maxp, stream, 0 );
+
if ( error )
goto Exit;
- if ( FT_STREAM_READ_FIELDS( maxp_fields, maxProfile ) )
- goto Exit;
+ if ( is_extended )
+ {
+ FT_UInt32 num_glyphs;
+
+
+ if ( FT_READ_LONG( maxProfile->version ) ||
+ FT_READ_UOFF3( num_glyphs ) )
+ goto Exit;
+
+ face->maxp_num_glyphs = num_glyphs;
+ maxProfile->numGlyphs = num_glyphs > 0xFFFFU ? 0xFFFFU
+ : (FT_UShort)num_glyphs;
+ }
+ else
+ {
+ if ( FT_STREAM_READ_FIELDS( maxp_fields, maxProfile ) )
+ goto Exit;
+
+ face->maxp_num_glyphs = maxProfile->numGlyphs;
+ }
maxProfile->maxPoints = 0;
maxProfile->maxContours = 0;
@@ -829,7 +859,7 @@
}
}
- FT_TRACE3(( "numGlyphs: %hu\n", maxProfile->numGlyphs ));
+ FT_TRACE3(( "numGlyphs: %u\n", face->maxp_num_glyphs ));
Exit:
return error;
@@ -1135,6 +1165,21 @@
if ( FT_FRAME_EXTRACT( face->cmap_size, face->cmap_table ) )
face->cmap_size = 0;
+ if ( error )
+ goto Exit;
+
+ error = face->goto_table( face, TTAG_DMAP, stream, &face->dmap_size );
+ if ( error )
+ {
+ face->dmap_size = 0;
+ if ( FT_ERR_EQ( error, Table_Missing ) )
+ error = FT_Err_Ok;
+ goto Exit;
+ }
+
+ if ( FT_FRAME_EXTRACT( face->dmap_size, face->dmap_table ) )
+ face->dmap_size = 0;
+
Exit:
return error;
}
diff --git a/thirdparty/freetype/src/sfnt/ttmtx.c b/thirdparty/freetype/src/sfnt/ttmtx.c
index 60dce87d841..e52df3f583e 100644
--- a/thirdparty/freetype/src/sfnt/ttmtx.c
+++ b/thirdparty/freetype/src/sfnt/ttmtx.c
@@ -81,13 +81,15 @@
if ( vertical )
{
- tag = TTAG_vmtx;
+ tag = face->vert_metrics_is_extended ? TTAG_VMTX
+ : TTAG_vmtx;
ptable_offset = &face->vert_metrics_offset;
ptable_size = &face->vert_metrics_size;
}
else
{
- tag = TTAG_hmtx;
+ tag = face->horz_metrics_is_extended ? TTAG_HMTX
+ : TTAG_hmtx;
ptable_offset = &face->horz_metrics_offset;
ptable_size = &face->horz_metrics_size;
}
@@ -132,13 +134,17 @@
{
FT_Error error;
TT_HoriHeader* header;
+ FT_UInt32* metrics_count;
+ FT_Bool* is_extended;
+ FT_ULong lower_tag;
+ FT_ULong upper_tag;
static const FT_Frame_Field metrics_header_fields[] =
{
#undef FT_STRUCTURE
#define FT_STRUCTURE TT_HoriHeader
- FT_FRAME_START( 36 ),
+ FT_FRAME_START( 34 ),
FT_FRAME_ULONG ( Version ),
FT_FRAME_SHORT ( Ascender ),
FT_FRAME_SHORT ( Descender ),
@@ -155,7 +161,6 @@
FT_FRAME_SHORT ( Reserved[2] ),
FT_FRAME_SHORT ( Reserved[3] ),
FT_FRAME_SHORT ( metric_Data_Format ),
- FT_FRAME_USHORT( number_Of_HMetrics ),
FT_FRAME_END
};
@@ -165,27 +170,71 @@
void *v = &face->vertical;
- error = face->goto_table( face, TTAG_vhea, stream, 0 );
+ header = (TT_HoriHeader*)v;
+ metrics_count = &face->vert_metrics_count;
+ is_extended = &face->vert_metrics_is_extended;
+ lower_tag = TTAG_vhea;
+ upper_tag = TTAG_VHEA;
+ }
+ else
+ {
+ header = &face->horizontal;
+ metrics_count = &face->horz_metrics_count;
+ is_extended = &face->horz_metrics_is_extended;
+ lower_tag = TTAG_hhea;
+ upper_tag = TTAG_HHEA;
+ }
+
+ *metrics_count = 0;
+ *is_extended = FALSE;
+
+ if ( face->is_extended_glyf )
+ {
+ error = face->goto_table( face, upper_tag, stream, 0 );
if ( error )
goto Fail;
- header = (TT_HoriHeader*)v;
+ *is_extended = TRUE;
}
else
{
- error = face->goto_table( face, TTAG_hhea, stream, 0 );
+ error = face->goto_table( face, lower_tag, stream, 0 );
+ if ( FT_ERR_EQ( error, Table_Missing ) )
+ {
+ error = face->goto_table( face, upper_tag, stream, 0 );
+ if ( !error )
+ *is_extended = TRUE;
+ }
if ( error )
goto Fail;
-
- header = &face->horizontal;
}
if ( FT_STREAM_READ_FIELDS( metrics_header_fields, header ) )
goto Fail;
+ if ( *is_extended )
+ {
+ if ( FT_READ_ULONG( *metrics_count ) )
+ goto Fail;
+ }
+ else
+ {
+ FT_UShort count;
+
+
+ if ( FT_READ_USHORT( count ) )
+ goto Fail;
+
+ *metrics_count = count;
+ }
+
+ header->number_Of_HMetrics = *metrics_count > 0xFFFFU
+ ? 0xFFFFU
+ : (FT_UShort)*metrics_count;
+
FT_TRACE3(( "Ascender: %5d\n", header->Ascender ));
FT_TRACE3(( "Descender: %5d\n", header->Descender ));
- FT_TRACE3(( "number_Of_Metrics: %5u\n", header->number_Of_HMetrics ));
+ FT_TRACE3(( "number_Of_Metrics: %5lu\n", (FT_ULong)*metrics_count ));
header->long_metrics = NULL;
header->short_metrics = NULL;
@@ -231,11 +280,10 @@
FT_Short *abearing,
FT_UShort *aadvance )
{
- FT_Error error;
- FT_Stream stream = face->root.stream;
- TT_HoriHeader* header;
- FT_ULong table_pos, table_size, table_end;
- FT_UShort k;
+ FT_Error error;
+ FT_Stream stream = face->root.stream;
+ FT_ULong table_pos, table_size, table_end;
+ FT_UInt32 k;
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
FT_Service_MetricsVariations var =
@@ -245,24 +293,19 @@
if ( vertical )
{
- void* v = &face->vertical;
-
-
- header = (TT_HoriHeader*)v;
table_pos = face->vert_metrics_offset;
table_size = face->vert_metrics_size;
+ k = face->vert_metrics_count;
}
else
{
- header = &face->horizontal;
table_pos = face->horz_metrics_offset;
table_size = face->horz_metrics_size;
+ k = face->horz_metrics_count;
}
table_end = table_pos + table_size;
- k = header->number_Of_HMetrics;
-
if ( k > 0 )
{
if ( gindex < (FT_UInt)k )
@@ -306,7 +349,9 @@
}
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
- if ( var && FT_IS_VARIATION( &face->root ) )
+ if ( var &&
+ ( FT_IS_VARIATION( &face->root ) ||
+ FT_IS_NAMED_INSTANCE( &face->root ) ) )
{
FT_Face f = FT_FACE( face );
FT_Int a = (FT_Int)*aadvance;
diff --git a/thirdparty/freetype/src/sfnt/ttpost.c b/thirdparty/freetype/src/sfnt/ttpost.c
index 98603e115ea..0e0bd0fd366 100644
--- a/thirdparty/freetype/src/sfnt/ttpost.c
+++ b/thirdparty/freetype/src/sfnt/ttpost.c
@@ -337,10 +337,10 @@
/* UNDOCUMENTED! The number of glyphs in this table can be smaller */
/* than the value in the maxp table (cf. cyberbit.ttf). */
- if ( post_len < 34 ||
- FT_STREAM_SKIP( 32 ) ||
- FT_READ_USHORT( num_glyphs ) ||
- num_glyphs > face->max_profile.numGlyphs ||
+ if ( post_len < 34 ||
+ FT_STREAM_SKIP( 32 ) ||
+ FT_READ_USHORT( num_glyphs ) ||
+ num_glyphs > face->maxp_num_glyphs ||
num_glyphs == 0 )
goto Exit;
@@ -422,7 +422,7 @@
if ( !face )
return FT_THROW( Invalid_Face_Handle );
- if ( idx >= (FT_UInt)face->max_profile.numGlyphs )
+ if ( idx >= face->maxp_num_glyphs )
return FT_THROW( Invalid_Glyph_Index );
#ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
@@ -463,7 +463,7 @@
/* version 1.0 is only valid with 258 glyphs */
else if ( format == 0x00010000L &&
- face->max_profile.numGlyphs == 258 )
+ face->maxp_num_glyphs == 258 )
*PSname = MAC_NAME( idx );
/* nothing to do for format == 0x00030000L */
diff --git a/thirdparty/freetype/src/sfnt/ttsbit.c b/thirdparty/freetype/src/sfnt/ttsbit.c
index 1655e40b681..d479b6a0b78 100644
--- a/thirdparty/freetype/src/sfnt/ttsbit.c
+++ b/thirdparty/freetype/src/sfnt/ttsbit.c
@@ -1602,9 +1602,10 @@
metrics->horiBearingY = (FT_Short)( originOffsetY + metrics->height );
metrics->vertBearingY = (FT_Short)originOffsetY;
- metrics->horiAdvance = (FT_UShort)( aadvance *
- face->root.size->metrics.x_ppem /
- face->header.Units_Per_EM );
+ metrics->horiAdvance =
+ (FT_UShort)FT_MulDiv( aadvance,
+ face->root.size->metrics.x_ppem,
+ face->header.Units_Per_EM );
if ( face->vertical_info )
tt_face_get_metrics( face, TRUE, glyph_index, &abearing, &aadvance );
@@ -1615,9 +1616,10 @@
aadvance = (FT_UShort)FT_ABS( face->horizontal.Ascender -
face->horizontal.Descender );
- metrics->vertAdvance = (FT_UShort)( aadvance *
- face->root.size->metrics.x_ppem /
- face->header.Units_Per_EM );
+ metrics->vertAdvance =
+ (FT_UShort)FT_MulDiv( aadvance,
+ face->root.size->metrics.x_ppem,
+ face->header.Units_Per_EM );
}
return error;
diff --git a/thirdparty/freetype/src/smooth/ftgrays.c b/thirdparty/freetype/src/smooth/ftgrays.c
index 0f7b63937d7..69805f08a27 100644
--- a/thirdparty/freetype/src/smooth/ftgrays.c
+++ b/thirdparty/freetype/src/smooth/ftgrays.c
@@ -68,11 +68,14 @@
*
* This renderer has the following advantages:
*
- * - It doesn't need an intermediate bitmap. Instead, one can supply a
- * callback function that will be called by the renderer to draw gray
- * spans on any target surface. You can thus do direct composition on
- * any kind of bitmap, provided that you give the renderer the right
- * callback.
+ * - It doesn't need an intermediate bitmap. Instead, linked lists
+ * of cells (pixels visited by an outline) are stored, which requires
+ * less memory and can be maintained on the stack for small glyphs.
+ *
+ * - One can supply a callback function that will be called by the
+ * renderer to draw gray spans on any target surface. You can thus
+ * do direct composition on any kind of bitmap, provided that you give
+ * the renderer the right callback.
*
* - A perfect anti-aliaser, i.e., it computes the _exact_ coverage on
* each pixel cell by straight segments.
@@ -464,7 +467,10 @@ typedef ptrdiff_t FT_PtrDist;
} TPixmap;
- /* maximum number of gray cells in the buffer */
+ /* Maximum number of gray cells in the stack buffer; */
+ /* for example, a buffer of 680 cells can accommodate */
+ /* a glyph with the taxicab perimeter of 680 pixels. */
+ /* A larger buffer can be allocated when necessary. */
#if FT_RENDER_POOL_SIZE > 2048
#define FT_MAX_GRAY_POOL ( FT_RENDER_POOL_SIZE / sizeof ( TCell ) )
#else
@@ -485,16 +491,14 @@ typedef ptrdiff_t FT_PtrDist;
typedef struct gray_TWorker_
{
- FT_BBox cbox;
-
- TCoord min_ex, max_ex; /* min and max integer pixel coordinates */
- TCoord min_ey, max_ey;
- TCoord count_ey; /* same as (max_ey - min_ey) */
+ TCoord min_ex, min_ey; /* min and max integer pixel coordinates */
+ TCoord max_ex, max_ey;
int error; /* pool overflow exception */
+ PCell buffer; /* buffer */
+ PCell cell_null; /* last cell, used as dumpster and limit */
PCell cell; /* current cell */
PCell cell_free; /* call allocation next free slot */
- PCell cell_null; /* last cell, used as dumpster and limit */
PCell* ycells; /* array of cell linked-lists; one per */
/* vertical coordinate in the current band */
@@ -581,14 +585,11 @@ typedef ptrdiff_t FT_PtrDist;
/* Note that if a cell is to the left of the clipping region, it is */
/* actually set to the (min_ex-1) horizontal position. */
- TCoord ey_index = ey - ras.min_ey;
-
-
- if ( ey_index < 0 || ey_index >= ras.count_ey || ex >= ras.max_ex )
+ if ( ey < ras.min_ey || ey >= ras.max_ey || ex >= ras.max_ex )
ras.cell = ras.cell_null;
else
{
- PCell* pcell = ras.ycells + ey_index;
+ PCell* pcell = ras.ycells + ( ey - ras.min_ey );
PCell cell;
@@ -1502,15 +1503,14 @@ typedef ptrdiff_t FT_PtrDist;
tags = outline->tags + first;
tag = FT_CURVE_TAG( tags[0] );
- /* A contour cannot start with a cubic control point! */
- if ( tag == FT_CURVE_TAG_CUBIC )
- goto Invalid_Outline;
-
/* check first point to determine origin */
- if ( tag == FT_CURVE_TAG_CONIC )
+ if ( tag != FT_CURVE_TAG_ON )
{
- /* first point is conic control. Yes, this happens. */
- if ( FT_CURVE_TAG( outline->tags[last] ) == FT_CURVE_TAG_ON )
+ char last_tag = FT_CURVE_TAG( outline->tags[last] );
+
+
+ /* first point is a control point. Yes, this happens. */
+ if ( last_tag == FT_CURVE_TAG_ON )
{
/* start at last point if it is on the curve */
v_start = v_last;
@@ -1518,9 +1518,12 @@ typedef ptrdiff_t FT_PtrDist;
}
else
{
- /* if both first and last points are conic, */
- /* start at their middle and record its position */
- /* for closure */
+ if ( last_tag != tag )
+ goto Invalid_Outline;
+
+ /* if both first and last points are controls, */
+ /* start at their middle and record its position */
+ /* for closure */
v_start.x = ( v_start.x + v_last.x ) / 2;
v_start.y = ( v_start.y + v_last.y ) / 2;
@@ -1636,11 +1639,24 @@ typedef ptrdiff_t FT_PtrDist;
if ( point <= limit )
{
FT_Vector vec;
+ FT_Vector v_middle;
+
+ tag = FT_CURVE_TAG( tags[0] );
vec.x = SCALED( point->x );
vec.y = SCALED( point->y );
+ if ( tag == FT_CURVE_TAG_CUBIC )
+ {
+ v_middle.x = ( vec2.x + vec.x ) / 2;
+ v_middle.y = ( vec2.y + vec.y ) / 2;
+
+ vec = v_middle;
+ }
+ else if ( tag != FT_CURVE_TAG_ON )
+ goto Invalid_Outline;
+
FT_TRACE5(( " cubic to (%.2f, %.2f)"
" with controls (%.2f, %.2f) and (%.2f, %.2f)\n",
vec.x / 64.0, vec.y / 64.0,
@@ -1649,6 +1665,12 @@ typedef ptrdiff_t FT_PtrDist;
error = func_interface->cubic_to( &vec1, &vec2, &vec, user );
if ( error )
goto Exit;
+
+ if ( tag == FT_CURVE_TAG_CUBIC )
+ {
+ point--;
+ tags--;
+ }
continue;
}
@@ -1804,7 +1826,7 @@ typedef ptrdiff_t FT_PtrDist;
FT_FILL_RULE( coverage, cover, fill );
span[n].coverage = (unsigned char)coverage;
- span[n].x = (unsigned short)x;
+ span[n].x = (short)x;
span[n].len = (unsigned short)( cell->x - x );
if ( ++n == FT_MAX_GRAY_SPANS )
@@ -1823,7 +1845,7 @@ typedef ptrdiff_t FT_PtrDist;
FT_FILL_RULE( coverage, area, fill );
span[n].coverage = (unsigned char)coverage;
- span[n].x = (unsigned short)cell->x;
+ span[n].x = (short)cell->x;
span[n].len = 1;
if ( ++n == FT_MAX_GRAY_SPANS )
@@ -1861,101 +1883,75 @@ typedef ptrdiff_t FT_PtrDist;
static int
gray_convert_glyph( RAS_ARG )
{
- TCell buffer[FT_MAX_GRAY_POOL];
- size_t height = (size_t)( ras.cbox.yMax - ras.cbox.yMin );
- size_t n = FT_MAX_GRAY_POOL / 8;
- TCoord y;
TCoord bands[32]; /* enough to accommodate bisections */
- TCoord* band;
+ TCoord* band = bands;
int continued = 0;
int error = Smooth_Err_Ok;
/* Initialize the null cell at the end of the poll. */
- ras.cell_null = buffer + FT_MAX_GRAY_POOL - 1;
ras.cell_null->x = CELL_MAX_X_VALUE;
ras.cell_null->area = 0;
ras.cell_null->cover = 0;
ras.cell_null->next = NULL;
/* set up vertical bands */
- ras.ycells = (PCell*)buffer;
-
- if ( height > n )
- {
- /* two divisions rounded up */
- n = ( height + n - 1 ) / n;
- height = ( height + n - 1 ) / n;
- }
+ ras.ycells = (PCell*)ras.buffer;
- for ( y = ras.cbox.yMin; y < ras.cbox.yMax; )
+ while ( 1 )
{
- ras.min_ey = y;
- y += height;
- ras.max_ey = FT_MIN( y, ras.cbox.yMax );
-
- ras.count_ey = ras.max_ey - ras.min_ey;
+ size_t n;
+ TCoord i, count = ras.max_ey - ras.min_ey;
- band = bands;
- band[1] = ras.cbox.xMin;
- band[0] = ras.cbox.xMax;
-
- do
- {
- TCoord i;
+ /* memory management: zero out and skip ycells */
+ for ( i = 0; i < count; ++i )
+ ras.ycells[i] = ras.cell_null;
- ras.min_ex = band[1];
- ras.max_ex = band[0];
+ n = ( (size_t)count * sizeof ( PCell ) + sizeof ( TCell ) - 1 )
+ / sizeof ( TCell );
- /* memory management: zero out and skip ycells */
- for ( i = 0; i < ras.count_ey; ++i )
- ras.ycells[i] = ras.cell_null;
+ ras.cell_free = ras.buffer + n;
+ ras.cell = ras.cell_null;
+ ras.error = Smooth_Err_Ok;
- n = ( (size_t)ras.count_ey * sizeof ( PCell ) + sizeof ( TCell ) - 1 )
- / sizeof ( TCell );
+ error = gray_convert_glyph_inner( RAS_VAR_ continued );
+ continued = 1;
- ras.cell_free = buffer + n;
- ras.cell = ras.cell_null;
- ras.error = Smooth_Err_Ok;
+ if ( !error )
+ {
+ if ( ras.render_span ) /* for FT_RASTER_FLAG_DIRECT only */
+ gray_sweep_direct( RAS_VAR );
+ else
+ gray_sweep( RAS_VAR );
- error = gray_convert_glyph_inner( RAS_VAR_ continued );
- continued = 1;
+ if ( band == bands )
+ break; /* done */
- if ( !error )
- {
- if ( ras.render_span ) /* for FT_RASTER_FLAG_DIRECT only */
- gray_sweep_direct( RAS_VAR );
- else
- gray_sweep( RAS_VAR );
- band--;
- continue;
- }
- else if ( error != Smooth_Err_Raster_Overflow )
- goto Exit;
+ ras.max_ey = ras.min_ey;
+ ras.min_ey = *--band;
+ continue;
+ }
+ else if ( error != Smooth_Err_Raster_Overflow )
+ goto Exit;
- /* render pool overflow; we will reduce the render band by half */
- i = ( band[0] - band[1] ) >> 1;
+ /* render pool overflow, reduce the render band by half */
+ count >>= 1;
- /* this should never happen even with tiny rendering pool */
- if ( i == 0 )
- {
- FT_TRACE7(( "gray_convert_glyph: rotten glyph\n" ));
- error = FT_THROW( Raster_Overflow );
- goto Exit;
- }
+ /* this happens only if the rendering pool is too small */
+ if ( count == 0 )
+ {
+ FT_TRACE7(( "gray_convert_glyph: rotten glyph\n" ));
+ error = FT_THROW( Raster_Overflow );
+ goto Exit;
+ }
- band++;
- band[1] = band[0];
- band[0] += i;
- } while ( band >= bands );
+ *band++ = ras.min_ey;
+ ras.min_ey += count;
}
Exit:
- ras.cell = ras.cell_free = ras.cell_null = NULL;
- ras.ycells = NULL;
-
return error;
}
@@ -1964,6 +1960,9 @@ typedef ptrdiff_t FT_PtrDist;
gray_raster_render( FT_Raster raster,
const FT_Raster_Params* params )
{
+ FT_ULong estimate;
+ int ret;
+
const FT_Outline* outline = (const FT_Outline*)params->source;
const FT_Bitmap* target_map = params->target;
@@ -2003,7 +2002,10 @@ typedef ptrdiff_t FT_PtrDist;
ras.render_span = (FT_Raster_Span_Func)params->gray_spans;
ras.render_span_data = params->user;
- ras.cbox = params->clip_box;
+ ras.min_ex = (TCoord)params->clip_box.xMin;
+ ras.min_ey = (TCoord)params->clip_box.yMin;
+ ras.max_ex = (TCoord)params->clip_box.xMax;
+ ras.max_ey = (TCoord)params->clip_box.yMax;
}
else
{
@@ -2029,17 +2031,45 @@ typedef ptrdiff_t FT_PtrDist;
ras.render_span = (FT_Raster_Span_Func)NULL;
ras.render_span_data = NULL;
- ras.cbox.xMin = 0;
- ras.cbox.yMin = 0;
- ras.cbox.xMax = (FT_Pos)target_map->width;
- ras.cbox.yMax = (FT_Pos)target_map->rows;
+ ras.min_ex = 0;
+ ras.min_ey = 0;
+ ras.max_ex = (TCoord)target_map->width;
+ ras.max_ey = (TCoord)target_map->rows;
}
/* exit if nothing to do */
- if ( ras.cbox.xMin >= ras.cbox.xMax || ras.cbox.yMin >= ras.cbox.yMax )
+ if ( ras.min_ex >= ras.max_ex || ras.min_ey >= ras.max_ey )
return Smooth_Err_Ok;
- return gray_convert_glyph( RAS_VAR );
+ /* allocate memory based on empirical estimate from CJK fonts */
+ estimate = ( ras.max_ex - ras.min_ex + ras.max_ey - ras.min_ey ) * 10UL;
+ if ( estimate > FT_MAX_GRAY_POOL )
+ {
+ FT_Error error;
+ FT_Memory memory = (FT_Memory)((gray_PRaster)raster)->memory;
+
+
+ if ( FT_QNEW_ARRAY( ras.buffer, estimate ) )
+ ret = error;
+ else
+ {
+ ras.cell_null = ras.buffer + estimate - 1;
+ ret = gray_convert_glyph( RAS_VAR );
+ FT_FREE( ras.buffer );
+ }
+ }
+ else
+ {
+ TCell buffer[FT_MAX_GRAY_POOL]; /* stack allocation */
+
+
+ ras.buffer = buffer;
+ ras.cell_null = ras.buffer + FT_MAX_GRAY_POOL - 1;
+
+ ret = gray_convert_glyph( RAS_VAR );
+ }
+
+ return ret;
}
diff --git a/thirdparty/freetype/src/smooth/ftsmooth.c b/thirdparty/freetype/src/smooth/ftsmooth.c
index 7378e6adc46..f5a3de65d80 100644
--- a/thirdparty/freetype/src/smooth/ftsmooth.c
+++ b/thirdparty/freetype/src/smooth/ftsmooth.c
@@ -124,7 +124,8 @@
for ( ; count--; spans++ )
- for ( dst = dst_line + spans->x * 3, w = spans->len; w--; dst += 3 )
+ for ( dst = dst_line + (unsigned short)spans->x * 3,
+ w = spans->len; w--; dst += 3 )
*dst = spans->coverage;
}
@@ -151,7 +152,7 @@
params.clip_box.xMin = 0;
params.clip_box.yMin = 0;
- params.clip_box.xMax = bitmap->width;
+ params.clip_box.xMax = bitmap->width / 3;
params.clip_box.yMax = bitmap->rows;
if ( bitmap->pitch < 0 )
@@ -290,7 +291,8 @@
for ( ; count--; spans++ )
- for ( dst = dst_line + spans->x, w = spans->len; w--; dst++ )
+ for ( dst = dst_line + (unsigned short)spans->x,
+ w = spans->len; w--; dst++ )
{
dst[0] += ( spans->coverage * target->wght[0] + 85 ) >> 8;
dst[1] += ( spans->coverage * target->wght[1] + 85 ) >> 8;
@@ -385,7 +387,8 @@
for ( ; count--; spans++ )
- for ( dst = dst_line + spans->x, w = spans->len; w--; dst++ )
+ for ( dst = dst_line + (unsigned short)spans->x,
+ w = spans->len; w--; dst++ )
{
dst[ 0] += ( spans->coverage * target->wght[0] + 85 ) >> 8;
dst[ pitch] += ( spans->coverage * target->wght[1] + 85 ) >> 8;
@@ -473,7 +476,7 @@
unsigned char* dst = target->origin - ( y / SCALE ) * target->pitch;
- unsigned short x;
+ unsigned int x, i;
unsigned int cover, sum;
@@ -487,8 +490,9 @@
cover = ( spans->coverage + SCALE * SCALE / 2 ) / ( SCALE * SCALE );
for ( x = 0; x < spans->len; x++ )
{
- sum = dst[( spans->x + x ) / SCALE] + cover;
- dst[( spans->x + x ) / SCALE] = (unsigned char)( sum - ( sum >> 8 ) );
+ i = ( (unsigned short)spans->x + x ) / SCALE;
+ sum = dst[i] + cover;
+ dst[i] = (unsigned char)( sum - ( sum >> 8 ) );
}
}
}
diff --git a/thirdparty/freetype/src/truetype/truetype.c b/thirdparty/freetype/src/truetype/truetype.c
index accec4597d7..631f004cb3d 100644
--- a/thirdparty/freetype/src/truetype/truetype.c
+++ b/thirdparty/freetype/src/truetype/truetype.c
@@ -24,6 +24,7 @@
#include "ttinterp.c"
#include "ttobjs.c" /* object manager */
#include "ttpload.c" /* tables loader */
+#include "ttvarc.c" /* variable composites */
/* END */
diff --git a/thirdparty/freetype/src/truetype/ttdriver.c b/thirdparty/freetype/src/truetype/ttdriver.c
index 90d6e810578..b9530b518bc 100644
--- a/thirdparty/freetype/src/truetype/ttdriver.c
+++ b/thirdparty/freetype/src/truetype/ttdriver.c
@@ -32,6 +32,10 @@
#include <freetype/internal/services/svprop.h>
#include <freetype/ftdriver.h>
+#ifdef TT_CONFIG_OPTION_VARC
+#include <freetype/internal/services/svvarc.h>
+#endif
+
#include "ttdriver.h"
#include "ttgload.h"
#include "ttpload.h"
@@ -40,6 +44,10 @@
#include "ttgxvar.h"
#endif
+#ifdef TT_CONFIG_OPTION_VARC
+#include "ttvarc.h"
+#endif
+
#include "tterrors.h"
@@ -456,15 +464,6 @@
FT_Error error;
- if ( !slot )
- return FT_THROW( Invalid_Slot_Handle );
-
- if ( !size )
- return FT_THROW( Invalid_Size_Handle );
-
- if ( !face )
- return FT_THROW( Invalid_Face_Handle );
-
#ifdef FT_CONFIG_OPTION_INCREMENTAL
if ( glyph_index >= (FT_UInt)face->num_glyphs &&
!face->internal->incremental_interface )
@@ -493,6 +492,39 @@
load_flags |= FT_LOAD_NO_HINTING;
}
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ /*
+ * DirectWrite treats ClearType symmetric grid fitting independently
+ * from symmetric smoothing. Resolve the grid-fitting decision from the
+ * same policy source that GETINFO uses for symmetric smoothing.
+ *
+ * Only native v40 horizontal-LCD hinting participates. Caller-disabled
+ * native hinting and the other interpreter/rendering modes are left
+ * alone.
+ */
+ if ( FT_LOAD_TARGET_MODE( load_flags ) == FT_RENDER_MODE_LCD &&
+ !( load_flags & FT_LOAD_NO_HINTING ) &&
+ !FT_IS_TRICKY( face ) )
+ {
+ TT_Driver driver = (TT_Driver)FT_FACE_DRIVER( face );
+
+
+ if ( driver->interpreter_version == TT_INTERPRETER_VERSION_40 )
+ {
+ FT_Bool grid_fit;
+
+
+ tt_face_get_cleartype_policy( (TT_Face)face,
+ size->metrics.y_ppem,
+ NULL,
+ &grid_fit );
+
+ if ( !grid_fit )
+ load_flags |= FT_LOAD_NO_HINTING;
+ }
+ }
+#endif
+
/* use hinted metrics only if we load a glyph with hinting */
ttsize->metrics = ( load_flags & FT_LOAD_NO_HINTING )
? &size->metrics
@@ -571,6 +603,21 @@
#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+#ifdef TT_CONFIG_OPTION_VARC
+
+ FT_DEFINE_SERVICE_VARCREC(
+ tt_service_varc,
+
+ tt_face_init_varc_axes,
+ tt_face_load_varc,
+ tt_face_free_varc,
+ tt_face_has_varc_glyph,
+ tt_face_load_varc_glyph
+ )
+
+#endif /* TT_CONFIG_OPTION_VARC */
+
+
static const FT_Service_TrueTypeEngineRec tt_service_truetype_engine =
{
#ifdef TT_USE_BYTECODE_INTERPRETER
@@ -592,7 +639,19 @@
)
-#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+#if defined( TT_CONFIG_OPTION_GX_VAR_SUPPORT ) && \
+ defined( TT_CONFIG_OPTION_VARC )
+ FT_DEFINE_SERVICEDESCREC7(
+ tt_services,
+
+ FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_TRUETYPE,
+ FT_SERVICE_ID_MULTI_MASTERS, &tt_service_gx_multi_masters,
+ FT_SERVICE_ID_METRICS_VARIATIONS, &tt_service_metrics_variations,
+ FT_SERVICE_ID_VARC, &tt_service_varc,
+ FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine,
+ FT_SERVICE_ID_TT_GLYF, &tt_service_truetype_glyf,
+ FT_SERVICE_ID_PROPERTIES, &tt_service_properties )
+#elif defined( TT_CONFIG_OPTION_GX_VAR_SUPPORT )
FT_DEFINE_SERVICEDESCREC6(
tt_services,
@@ -602,6 +661,15 @@
FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine,
FT_SERVICE_ID_TT_GLYF, &tt_service_truetype_glyf,
FT_SERVICE_ID_PROPERTIES, &tt_service_properties )
+#elif defined( TT_CONFIG_OPTION_VARC )
+ FT_DEFINE_SERVICEDESCREC5(
+ tt_services,
+
+ FT_SERVICE_ID_FONT_FORMAT, FT_FONT_FORMAT_TRUETYPE,
+ FT_SERVICE_ID_VARC, &tt_service_varc,
+ FT_SERVICE_ID_TRUETYPE_ENGINE, &tt_service_truetype_engine,
+ FT_SERVICE_ID_TT_GLYF, &tt_service_truetype_glyf,
+ FT_SERVICE_ID_PROPERTIES, &tt_service_properties )
#else
FT_DEFINE_SERVICEDESCREC4(
tt_services,
diff --git a/thirdparty/freetype/src/truetype/ttgload.c b/thirdparty/freetype/src/truetype/ttgload.c
index dcff7c3bb71..f665361646d 100644
--- a/thirdparty/freetype/src/truetype/ttgload.c
+++ b/thirdparty/freetype/src/truetype/ttgload.c
@@ -34,6 +34,10 @@
#include "ttgxvar.h"
#endif
+#ifdef TT_CONFIG_OPTION_VARC
+#include "ttvarc.h"
+#endif
+
#include "tterrors.h"
@@ -51,7 +55,7 @@
*
* Simple glyph flags.
*/
-#define ON_CURVE_POINT 0x01 /* same value as FT_CURVE_TAG_ON */
+#define ON_CURVE_POINT 0x01
#define X_SHORT_VECTOR 0x02
#define Y_SHORT_VECTOR 0x04
#define REPEAT_FLAG 0x08
@@ -60,6 +64,7 @@
#define Y_POSITIVE 0x20 /* two meanings depending on Y_SHORT_VECTOR */
#define SAME_Y 0x20
#define OVERLAP_SIMPLE 0x40 /* retained as FT_OUTLINE_OVERLAP */
+#define CUBIC_FLAG 0x80
/**************************************************************************
@@ -79,6 +84,7 @@
#define OVERLAP_COMPOUND 0x0400 /* retained as FT_OUTLINE_OVERLAP */
#define SCALED_COMPONENT_OFFSET 0x0800
#define UNSCALED_COMPONENT_OFFSET 0x1000
+#define GID_IS_24_BIT 0x2000
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
@@ -345,8 +351,8 @@
FT_Byte* limit = load->limit;
FT_GlyphLoader gloader = load->gloader;
FT_Outline* outline = &gloader->current.outline;
- FT_Int n_contours = load->n_contours;
- FT_Int n_points;
+ FT_UInt n_contours = (FT_UInt)load->n_contours;
+ FT_UInt n_points;
FT_UShort n_ins;
FT_Byte *flag, *flag_limit;
@@ -354,7 +360,6 @@
FT_Vector *vec, *vec_limit;
FT_Pos x, y;
FT_UShort *cont, *cont_limit;
- FT_Int last;
/* check that we can add the contours to the glyph */
@@ -363,27 +368,25 @@
goto Fail;
/* check space for contours array + instructions count */
- if ( n_contours >= 0xFFF || p + 2 * n_contours + 2 > limit )
+ if ( n_contours >= 0xFFFU || p + 2 * n_contours + 2 > limit )
goto Invalid_Outline;
/* reading the contours' endpoints & number of points */
cont = outline->contours;
cont_limit = cont + n_contours;
- last = -1;
+ n_points = 0;
for ( ; cont < cont_limit; cont++ )
{
*cont = FT_NEXT_USHORT( p );
- if ( *cont <= last )
+ if ( *cont < n_points )
goto Invalid_Outline;
- last = *cont;
+ n_points = *cont + 1U;
}
- n_points = last + 1;
-
- FT_TRACE5(( " # of points: %d\n", n_points ));
+ FT_TRACE5(( " # of points: %u\n", n_points ));
/* note that we will add four phantom points later */
error = FT_GLYPHLOADER_CHECK_POINTS( gloader, n_points + 4, 0 );
@@ -527,8 +530,9 @@
y += delta;
vec->y = y;
- /* the cast is for stupid compilers */
- *flag = (FT_Byte)( f & ON_CURVE_POINT );
+ *flag = (FT_Byte)( f & ON_CURVE_POINT );
+ if ( load->face->is_extended_glyf && ( f & CUBIC_FLAG ) )
+ *flag |= FT_CURVE_TAG_CUBIC;
}
outline->n_points = (FT_UShort)n_points;
@@ -570,7 +574,7 @@
if ( error )
goto Fail;
- /* check space */
+ /* check space for the flags and a 16-bit glyph ID */
if ( p + 4 > limit )
goto Invalid_Composite;
@@ -579,7 +583,16 @@
subglyph->arg1 = subglyph->arg2 = 0;
subglyph->flags = FT_NEXT_USHORT( p );
- subglyph->index = FT_NEXT_USHORT( p );
+ if ( loader->face->is_extended_glyf &&
+ ( subglyph->flags & GID_IS_24_BIT ) != 0 )
+ {
+ if ( p + 3 > limit )
+ goto Invalid_Composite;
+
+ subglyph->index = (FT_Int)FT_NEXT_UOFF3( p );
+ }
+ else
+ subglyph->index = FT_NEXT_USHORT( p );
/* we reject composites that have components */
/* with invalid glyph indices */
@@ -1148,7 +1161,15 @@
IS_HINTED( loader->load_flags ) )
{
#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
- if ( !loader->exec->backward_compatibility )
+ TT_Driver driver =
+ (TT_Driver)FT_FACE_DRIVER( (FT_Face)loader->face );
+
+
+ /* v40 approximates native ClearType's fine horizontal grid by */
+ /* leaving X offsets of components unrounded. */
+ if ( driver->interpreter_version != TT_INTERPRETER_VERSION_40 ||
+ loader->exec->mode == FT_RENDER_MODE_MONO ||
+ FT_IS_TRICKY( (FT_Face)loader->face ) )
#endif
x = FT_PIX_ROUND( x );
@@ -2539,6 +2560,84 @@
#endif /* FT_CONFIG_OPTION_SVG */
+#ifdef TT_CONFIG_OPTION_VARC
+
+ /* check for VARC glyphs */
+ if ( face->varc &&
+ tt_face_has_varc_glyph( (FT_Face)face, glyph_index ) )
+ {
+ FT_TRACE3(( "Loading VARC glyph\n" ));
+
+ error = tt_face_load_varc_glyph( (FT_Face)face,
+ (FT_GlyphSlot)glyph,
+ glyph_index,
+ load_flags );
+ if ( !error )
+ {
+ FT_Short left_bearing = 0;
+ FT_Short top_bearing = 0;
+ FT_UShort advance_width = 0;
+ FT_UShort advance_height = 0;
+
+
+ /* Get advance width and height. */
+ TT_Get_HMetrics( face, glyph_index,
+ &left_bearing,
+ &advance_width );
+ TT_Get_VMetrics( face, glyph_index,
+ 0,
+ &top_bearing,
+ &advance_height );
+
+ glyph->linearHoriAdvance = advance_width;
+ glyph->linearVertAdvance = advance_height;
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+ {
+ FT_Int advance = (FT_Int)advance_width;
+
+
+ tt_hadvance_adjust( (FT_Face)face, glyph_index, &advance );
+ advance_width = (FT_UShort)advance;
+ }
+#endif
+
+ {
+ FT_BBox bbox;
+
+
+ FT_Outline_Get_CBox( &glyph->outline, &bbox );
+
+ glyph->metrics.horiBearingX = bbox.xMin;
+ glyph->metrics.horiBearingY = bbox.yMax;
+ glyph->metrics.width = SUB_LONG( bbox.xMax, bbox.xMin );
+ glyph->metrics.height = SUB_LONG( bbox.yMax, bbox.yMin );
+ }
+
+ if ( load_flags & FT_LOAD_NO_SCALE )
+ {
+ /* Under NO_SCALE the advance is in font units (no 26.6 shift), */
+ /* like the normal glyf path (pp2.x - pp1.x). */
+ glyph->metrics.horiAdvance = advance_width;
+ glyph->metrics.vertAdvance = advance_height;
+ }
+ else
+ {
+ glyph->metrics.horiAdvance = FT_MulFix( advance_width,
+ size->metrics->x_scale );
+ glyph->metrics.vertAdvance = FT_MulFix( advance_height,
+ size->metrics->y_scale );
+ }
+
+ FT_TRACE3(( "Successfully loaded VARC glyph\n" ));
+ goto Exit;
+ }
+
+ FT_TRACE3(( "Failed to load VARC glyph, falling back to glyf\n" ));
+ }
+
+#endif /* TT_CONFIG_OPTION_VARC */
+
error = tt_loader_init( &loader, size, glyph, load_flags, FALSE );
if ( error )
goto Exit;
diff --git a/thirdparty/freetype/src/truetype/ttgxvar.c b/thirdparty/freetype/src/truetype/ttgxvar.c
index 5ab6e17119c..d3f8c30fa06 100644
--- a/thirdparty/freetype/src/truetype/ttgxvar.c
+++ b/thirdparty/freetype/src/truetype/ttgxvar.c
@@ -129,6 +129,12 @@
* stream ::
* The data stream.
*
+ * scratch ::
+ * A caller-provided scratch buffer for temporary storage.
+ *
+ * scratch_cap ::
+ * The size of `scratch`.
+ *
* @Output:
* point_cnt ::
* The number of points read. A zero value means that
@@ -140,8 +146,10 @@
* special value ALL_POINTS.
*/
static FT_UShort*
- ft_var_readpackedpoints( FT_Stream stream,
- FT_UInt *point_cnt )
+ ft_var_readpackedpoints( FT_Stream stream,
+ FT_UInt *point_cnt,
+ FT_UShort* scratch,
+ FT_UInt scratch_cap )
{
FT_UShort *points = NULL;
FT_UInt n;
@@ -166,7 +174,11 @@
n |= FT_GET_BYTE();
}
- if ( FT_QNEW_ARRAY( points, n ) )
+ /* Re-use `scratch` if it is large enough; this avoids */
+ /* a heap allocation per tuple in the common case. */
+ if ( scratch && n <= scratch_cap )
+ points = scratch;
+ else if ( FT_QNEW_ARRAY( points, n ) )
return NULL;
p = stream->cursor;
@@ -218,7 +230,8 @@
Fail:
FT_TRACE1(( "ft_var_readpackedpoints: invalid table\n" ));
- FT_FREE( points );
+ if ( points != scratch )
+ FT_FREE( points );
return NULL;
}
@@ -244,6 +257,12 @@
* delta_cnt ::
* The number of deltas to be read.
*
+ * scratch ::
+ * A caller-provided scratch buffer for temporary storage.
+ *
+ * scratch_cap ::
+ * The size of `scratch`.
+ *
* @Return:
* An array of FT_Fixed containing the deltas for the affected
* points. (This only gets the deltas for one dimension. It will
@@ -256,7 +275,9 @@
*/
static FT_Fixed*
ft_var_readpackeddeltas( FT_Stream stream,
- FT_UInt delta_cnt )
+ FT_UInt delta_cnt,
+ FT_Fixed* scratch,
+ FT_UInt scratch_cap )
{
FT_Fixed *deltas = NULL;
FT_UInt runcnt, cnt;
@@ -266,7 +287,11 @@
FT_Error error;
- if ( FT_QNEW_ARRAY( deltas, delta_cnt ) )
+ /* Re-use `scratch` if it is large enough; this avoids */
+ /* a heap allocation per tuple in the common case. */
+ if ( scratch && delta_cnt <= scratch_cap )
+ deltas = scratch;
+ else if ( FT_QNEW_ARRAY( deltas, delta_cnt ) )
return NULL;
p = stream->cursor;
@@ -314,7 +339,8 @@
Fail:
FT_TRACE1(( "ft_var_readpackeddeltas: invalid table\n" ));
- FT_FREE( deltas );
+ if ( deltas != scratch )
+ FT_FREE( deltas );
return NULL;
}
@@ -1175,11 +1201,12 @@
GX_ItemVarData varData;
- FT_UInt master;
- FT_Int64 returnValue = FT_INT64_ZERO;
- FT_UInt shift_base = 1;
- FT_UInt per_region_size;
- FT_Byte* bytes;
+ FT_UInt master;
+ FT_Int64 returnValue = FT_INT64_ZERO;
+ FT_UInt shift_base = 1;
+ FT_UInt per_region_size;
+ FT_Byte* bytes;
+ FT_Fixed* regionScalars;
if ( !ttface->blend || !ttface->blend->normalizedcoords )
@@ -1217,6 +1244,67 @@
bytes = varData->deltaSet + per_region_size * innerIndex;
+ /* The region scalars depend only on the normalized coordinates, which */
+ /* are constant while glyphs are loaded; yet this function is called */
+ /* once per item (e.g., per glyph for 'HVAR' advances). Cache the */
+ /* scalars per store and recompute only when the coordinates change. */
+ /* `regionScalars` stays NULL (falling back to recomputation) if the */
+ /* cache cannot be allocated. */
+ regionScalars = itemStore->regionScalars;
+ {
+ FT_Fixed* ncoords = ttface->blend->normalizedcoords;
+ FT_Bool fresh = FT_BOOL( regionScalars != NULL );
+ FT_UInt a;
+
+
+ if ( fresh )
+ {
+ for ( a = 0; a < itemStore->axisCount; a++ )
+ if ( itemStore->cachedCoords[a] != ncoords[a] )
+ {
+ fresh = FALSE;
+ break;
+ }
+ }
+
+ if ( !fresh )
+ {
+ FT_Memory memory = FT_FACE_MEMORY( face );
+ FT_Error error = FT_Err_Ok;
+
+ FT_UNUSED( error );
+
+
+ if ( !regionScalars )
+ {
+ if ( !FT_QNEW_ARRAY( itemStore->regionScalars,
+ itemStore->regionCount ) &&
+ !FT_QNEW_ARRAY( itemStore->cachedCoords,
+ itemStore->axisCount ) )
+ regionScalars = itemStore->regionScalars;
+ else
+ {
+ FT_FREE( itemStore->regionScalars );
+ FT_FREE( itemStore->cachedCoords );
+ }
+ }
+
+ if ( regionScalars )
+ {
+ FT_UInt r;
+
+
+ for ( r = 0; r < itemStore->regionCount; r++ )
+ regionScalars[r] = tt_calculate_scalar(
+ itemStore->varRegionList[r].axisList,
+ itemStore->axisCount,
+ ncoords );
+ for ( a = 0; a < itemStore->axisCount; a++ )
+ itemStore->cachedCoords[a] = ncoords[a];
+ }
+ }
+ }
+
/* outer loop steps through master designs to be blended */
for ( master = 0; master < varData->regionIdxCount; master++ )
{
@@ -1224,10 +1312,12 @@
GX_AxisCoords axis = itemStore->varRegionList[regionIndex].axisList;
- FT_Fixed scalar = tt_calculate_scalar(
- axis,
- itemStore->axisCount,
- ttface->blend->normalizedcoords );
+ FT_Fixed scalar = regionScalars
+ ? regionScalars[regionIndex]
+ : tt_calculate_scalar(
+ axis,
+ itemStore->axisCount,
+ ttface->blend->normalizedcoords );
if ( scalar )
@@ -1755,7 +1845,7 @@
FT_UShort axisCount;
FT_UShort globalCoordCount;
FT_ULong offsetToCoord;
- FT_UShort glyphCount;
+ FT_UInt32 glyphCount;
FT_UShort flags;
FT_ULong offsetToData;
@@ -1781,16 +1871,24 @@
static FT_Error
ft_var_load_gvar( TT_Face face )
{
- FT_Stream stream = FT_FACE_STREAM( face );
- FT_Memory memory = stream->memory;
- GX_Blend blend = face->blend;
- FT_Error error;
- FT_UInt i, j;
- FT_Byte* bytes;
- FT_ULong table_len;
- FT_ULong gvar_start;
- FT_ULong offsetToData;
- FT_ULong offsets_len;
+ FT_Error error;
+
+ FT_Stream stream = FT_FACE_STREAM( face );
+ FT_Memory memory = stream->memory;
+
+ GX_Blend blend = face->blend;
+ FT_Bool blend_created = FALSE;
+ FT_Bool is_extended = FALSE;
+
+ FT_UInt i, j;
+ FT_Byte* bytes;
+ FT_ULong table_len;
+ FT_ULong gvar_start;
+ FT_ULong header_size;
+
+ FT_ULong offsetToData;
+ FT_ULong offsets_len;
+
GX_GVar_Head gvar_head;
static const FT_Frame_Field gvar_fields[] =
@@ -1799,33 +1897,68 @@
#undef FT_STRUCTURE
#define FT_STRUCTURE GX_GVar_Head
- FT_FRAME_START( 20 ),
+ FT_FRAME_START( 12 ),
FT_FRAME_LONG ( version ),
FT_FRAME_USHORT( axisCount ),
FT_FRAME_USHORT( globalCoordCount ),
FT_FRAME_ULONG ( offsetToCoord ),
- FT_FRAME_USHORT( glyphCount ),
- FT_FRAME_USHORT( flags ),
- FT_FRAME_ULONG ( offsetToData ),
FT_FRAME_END
};
FT_TRACE2(( "GVAR " ));
- if ( FT_SET_ERROR( face->goto_table( face,
- TTAG_gvar,
- stream,
- &table_len ) ) )
+ error = face->goto_table( face,
+ face->is_extended_glyf ? TTAG_GVAR
+ : TTAG_gvar,
+ stream,
+ &table_len );
+ if ( FT_ERR_EQ( error, Table_Missing ) && !face->is_extended_glyf )
+ {
+ error = face->goto_table( face, TTAG_GVAR, stream, &table_len );
+ if ( !error )
+ is_extended = TRUE;
+ }
+ else if ( !error && face->is_extended_glyf )
+ is_extended = TRUE;
+
+ if ( error )
{
FT_TRACE2(( "is missing\n" ));
goto Exit;
}
+ header_size = is_extended ? 21 : 20;
+ if ( table_len < header_size )
+ {
+ error = FT_THROW( Invalid_Table );
+ goto Exit;
+ }
+
gvar_start = FT_STREAM_POS( );
if ( FT_STREAM_READ_FIELDS( gvar_fields, &gvar_head ) )
goto Exit;
+ if ( is_extended )
+ {
+ if ( FT_READ_UOFF3( gvar_head.glyphCount ) )
+ goto Exit;
+ }
+ else
+ {
+ FT_UShort glyph_count;
+
+
+ if ( FT_READ_USHORT( glyph_count ) )
+ goto Exit;
+
+ gvar_head.glyphCount = glyph_count;
+ }
+
+ if ( FT_READ_USHORT( gvar_head.flags ) ||
+ FT_READ_ULONG( gvar_head.offsetToData ) )
+ goto Exit;
+
if ( gvar_head.version != 0x00010000L )
{
FT_TRACE1(( "bad table version\n" ));
@@ -1833,7 +1966,25 @@
goto Exit;
}
- if ( gvar_head.axisCount != (FT_UShort)blend->mmvar->num_axis )
+ /*
+ * VARC can use normalized coordinates for component-internal axes in a
+ * static font, where 'gvar' is present without 'fvar'. Set up the
+ * private part of the blend machinery in that case; it deliberately has
+ * no `mmvar`, so the axes remain invisible through the public MM API.
+ */
+ if ( !blend )
+ {
+ if ( FT_SET_ERROR( tt_var_init_blend( FT_FACE( face ),
+ gvar_head.axisCount ) ) )
+ goto Exit;
+
+ blend = face->blend;
+ blend_created = TRUE;
+ }
+
+ blend->gvar_is_extended = is_extended;
+
+ if ( gvar_head.axisCount != (FT_UShort)blend->num_axis )
{
FT_TRACE1(( "ft_var_load_gvar:"
" number of axes in `gvar' and `cvar'\n" ));
@@ -1858,7 +2009,7 @@
( ( gvar_head.flags & 1 ) ? 4L : 2L );
/* rough sanity check */
- if (offsets_len > table_len )
+ if ( offsets_len > table_len - header_size )
{
FT_TRACE1(( "ft_var_load_gvar: invalid number of glyphs\n" ));
error = FT_THROW( Invalid_Table );
@@ -1999,14 +2150,32 @@
gvar_head.globalCoordCount ) )
goto Fail2;
- blend->tuplecount = gvar_head.globalCoordCount;
+ /* The tuple index in each glyph variation tuple header is only 12 */
+ /* bits wide (`GX_TI_TUPLE_INDEX_MASK`), so shared tuples with a */
+ /* higher index can never be referenced. Clamp the count to the */
+ /* largest addressable value, `GX_TI_TUPLE_INDEX_MASK + 1`. This */
+ /* also lets the fast path in `TT_Vary_Apply_Glyph_Deltas` rely on */
+ /* a set tuple flag forcing the masked value out of range. */
+ blend->tuplecount =
+ gvar_head.globalCoordCount > GX_TI_TUPLE_INDEX_MASK + 1
+ ? GX_TI_TUPLE_INDEX_MASK + 1
+ : gvar_head.globalCoordCount;
FT_TRACE5(( "\n" ));
FT_FRAME_EXIT();
}
+ if ( blend_created )
+ {
+ for ( i = 0; i < blend->tuplecount; i++ )
+ blend->tuplescalars[i] = (FT_Fixed)-0x20000L;
+ }
+
Exit:
+ if ( error && blend_created )
+ tt_done_blend( FT_FACE( face ) );
+
return error;
Fail2:
@@ -2019,6 +2188,70 @@
}
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_var_init_gvar
+ *
+ * @Description:
+ * Initialize the private 'gvar' blend state. This also supports the
+ * static-font case where VARC addresses component-internal axes and no
+ * 'fvar' table is present.
+ */
+ FT_LOCAL_DEF( FT_Error )
+ tt_var_init_gvar( FT_Face face )
+ {
+ TT_Face ttface = (TT_Face)face;
+
+
+ if ( ttface->blend && ttface->blend->glyphoffsets )
+ return FT_Err_Ok;
+
+ return ft_var_load_gvar( ttface );
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_var_init_blend
+ *
+ * @Description:
+ * Initialize a private normalized-coordinate vector without 'fvar'.
+ * CFF2 VARC glyphs use this with the axis count from their variation
+ * store; TrueType VARC glyphs get the count from 'gvar' above.
+ */
+ FT_LOCAL_DEF( FT_Error )
+ tt_var_init_blend( FT_Face face,
+ FT_UInt axis_count )
+ {
+ TT_Face ttface = (TT_Face)face;
+ FT_Memory memory = FT_FACE_MEMORY( face );
+ FT_Error error = FT_Err_Ok;
+ GX_Blend blend = ttface->blend;
+
+
+ if ( blend )
+ return blend->num_axis == axis_count
+ ? FT_Err_Ok
+ : FT_THROW( Invalid_Table );
+
+ if ( FT_NEW( blend ) )
+ return error;
+
+ blend->num_axis = axis_count;
+ ttface->blend = blend;
+
+ if ( FT_NEW_ARRAY( blend->normalizedcoords, axis_count ) )
+ {
+ FT_FREE( blend );
+ ttface->blend = NULL;
+ }
+
+ return error;
+ }
+
+
/**************************************************************************
*
* @Function:
@@ -2507,6 +2740,11 @@
FT_FRAME_END
};
+ /* A static VARC face can have a private blend initialized from 'gvar' */
+ /* without exposing variation axes through the public MM API. */
+ if ( ttface->blend && !ttface->blend->mmvar )
+ return FT_THROW( Table_Missing );
+
/* `num_instances` holds the number of all named instances including */
/* the default instance, which might be missing in the table of named */
/* instances (in 'fvar'). This value is validated in `sfobjs.c` and */
@@ -3453,9 +3691,7 @@
TT_Face ttface = (TT_Face)face;
FT_Error error = FT_Err_Ok;
GX_Blend blend;
- FT_MM_Var* mmvar;
- FT_Var_Axis* a;
- FT_UInt i, nc;
+ FT_UInt i;
if ( !ttface->blend )
@@ -3474,31 +3710,30 @@
return error;
}
- nc = num_coords;
if ( num_coords > blend->num_axis )
{
FT_TRACE2(( "TT_Get_Var_Design:"
" only using first %u of %u coordinates\n",
blend->num_axis, num_coords ));
- nc = blend->num_axis;
+
+ FT_ARRAY_ZERO( coords + blend->num_axis, num_coords - blend->num_axis );
+ num_coords = blend->num_axis;
}
- mmvar = blend->mmvar;
- a = mmvar->axis;
if ( ttface->doblend )
{
- for ( i = 0; i < nc; i++, a++ )
+ for ( i = 0; i < num_coords; i++ )
coords[i] = blend->coords[i];
}
else
{
- for ( i = 0; i < nc; i++, a++ )
+ FT_Var_Axis* a = blend->mmvar->axis;
+
+
+ for ( i = 0; i < num_coords; i++, a++ )
coords[i] = a->def;
}
- for ( ; i < num_coords; i++, a++ )
- coords[i] = a->def;
-
return FT_Err_Ok;
}
@@ -3804,7 +4039,8 @@
FT_Stream_SeekSet( stream, offsetToData );
- sharedpoints = ft_var_readpackedpoints( stream, &spoint_count );
+ sharedpoints = ft_var_readpackedpoints( stream, &spoint_count,
+ NULL, 0 );
offsetToData = FT_Stream_FTell( stream );
@@ -3879,7 +4115,8 @@
if ( tupleIndex & GX_TI_PRIVATE_POINT_NUMBERS )
{
- localpoints = ft_var_readpackedpoints( stream, &point_count );
+ localpoints = ft_var_readpackedpoints( stream, &point_count,
+ NULL, 0 );
points = localpoints;
}
else
@@ -3891,7 +4128,8 @@
deltas = ft_var_readpackeddeltas( stream,
point_count == 0 ? face->cvt_size
- : point_count );
+ : point_count,
+ NULL, 0 );
if ( !points || !deltas )
; /* failure, ignore it */
@@ -4025,13 +4263,13 @@
/* modeled after `af_iup_shift' */
static void
- tt_delta_shift( int p1,
- int p2,
- int ref,
+ tt_delta_shift( FT_UInt p1,
+ FT_UInt p2,
+ FT_UInt ref,
FT_Vector* in_points,
FT_Vector* out_points )
{
- int p;
+ FT_UInt p;
FT_Vector delta;
@@ -4062,14 +4300,14 @@
/* modeled after `af_iup_interp', `_iup_worker_interpolate', and */
/* `Ins_IUP' with spec differences in handling ill-defined cases. */
static void
- tt_delta_interpolate( int p1,
- int p2,
- int ref1,
- int ref2,
+ tt_delta_interpolate( FT_UInt p1,
+ FT_UInt p2,
+ FT_UInt ref1,
+ FT_UInt ref2,
FT_Vector* in_points,
FT_Vector* out_points )
{
- int p, i;
+ FT_UInt p, i;
FT_Pos out, in1, in2, out1, out2, d1, d2;
@@ -4135,24 +4373,18 @@
FT_Vector* in_points,
FT_Bool* has_delta )
{
- FT_Int first_point;
- FT_Int end_point;
-
- FT_Int first_delta;
- FT_Int cur_delta;
-
- FT_Int point;
- FT_Short contour;
+ FT_UInt first_point;
+ FT_UInt end_point;
+ FT_UInt first_delta;
+ FT_UInt cur_delta;
- /* ignore empty outlines */
- if ( !outline->n_contours )
- return;
+ FT_UInt point;
+ FT_UInt contour;
- contour = 0;
- point = 0;
- do
+ for ( point = 0, contour = 0;
+ contour < outline->n_contours; contour++ )
{
end_point = outline->contours[contour];
first_point = point;
@@ -4213,9 +4445,7 @@
out_points );
}
}
- contour++;
-
- } while ( contour < outline->n_contours );
+ }
}
@@ -4269,6 +4499,8 @@
FT_UInt peak_coords_size;
FT_UInt point_deltas_x_size;
+ FT_UInt deltas_buf_size;
+ FT_UInt points_buf_size;
FT_UInt points_org_size;
FT_UInt points_out_size;
FT_UInt has_delta_size;
@@ -4286,15 +4518,25 @@
FT_UInt point_count;
FT_UInt spoint_count = 0;
- FT_UShort* sharedpoints = NULL;
- FT_UShort* localpoints = NULL;
+ FT_UShort* sharedpoints = NULL;
+ FT_UShort* localpoints = NULL;
FT_UShort* points;
+ /* scratch buffer pool for `sharedpoints` */
+ FT_UShort* shared_points_buf = NULL;
+ /* scratch buffer pool for `localpoints` */
+ FT_UShort* local_points_buf;
FT_Fixed* deltas_x = NULL;
FT_Fixed* deltas_y = NULL;
+ /* scratch buffer pool for `deltas_x` */
+ FT_Fixed* deltas_x_buf;
+ /* scratch buffer pool for `deltas_y` */
+ FT_Fixed* deltas_y_buf;
FT_Fixed* point_deltas_x = NULL;
FT_Fixed* point_deltas_y = NULL;
+ FT_Fixed* tupleScalars;
+
for ( i = 0; i < n_points; i++ )
{
@@ -4328,8 +4570,15 @@
/* each set of glyph variation data is formatted similarly to `cvar' */
+ if ( dataSize < ( blend->gvar_is_extended ? 5U : 4U ) )
+ {
+ error = FT_THROW( Invalid_Table );
+ goto FExit;
+ }
+
tupleCount = FT_GET_USHORT();
- offsetToData = FT_GET_USHORT();
+ offsetToData = blend->gvar_is_extended ? FT_GET_UOFF3()
+ : FT_GET_USHORT();
/* rough sanity test */
if ( offsetToData > dataSize ||
@@ -4344,57 +4593,62 @@
offsetToData += glyph_start;
- if ( tupleCount & GX_TC_TUPLES_SHARE_POINT_NUMBERS )
- {
- here = FT_Stream_FTell( stream );
-
- FT_Stream_SeekSet( stream, offsetToData );
-
- sharedpoints = ft_var_readpackedpoints( stream, &spoint_count );
-
- offsetToData = FT_Stream_FTell( stream );
-
- FT_Stream_SeekSet( stream, here );
- }
-
- FT_TRACE5(( "gvar: there %s %u tuple%s:\n",
- ( tupleCount & GX_TC_TUPLE_COUNT_MASK ) == 1 ? "is" : "are",
- tupleCount & GX_TC_TUPLE_COUNT_MASK,
- ( tupleCount & GX_TC_TUPLE_COUNT_MASK ) == 1 ? "" : "s" ));
-
+ /* Re-use a per-face scratch pool, grown on demand, instead of */
+ /* allocating (and freeing) it on every glyph. This pool is freed */
+ /* in `tt_done_blend`. The shared/local point-number lists are */
+ /* carved out of it too, so it must be set up before they are read. */
peak_coords_size = ALIGN_SIZE( 3 * blend->num_axis *
sizeof ( *peak_coords ) );
point_deltas_x_size = ALIGN_SIZE( 2 * n_points *
sizeof ( *point_deltas_x ) );
+ deltas_buf_size = ALIGN_SIZE( 2 * n_points *
+ sizeof ( *deltas_x ) );
+ points_buf_size = ALIGN_SIZE( 2 * n_points *
+ sizeof ( *shared_points_buf ) );
points_org_size = ALIGN_SIZE( n_points * sizeof ( *points_org ) );
points_out_size = ALIGN_SIZE( n_points * sizeof ( *points_out ) );
has_delta_size = ALIGN_SIZE( n_points * sizeof ( *has_delta ) );
pool_size = peak_coords_size +
point_deltas_x_size +
+ deltas_buf_size +
+ points_buf_size +
points_org_size +
points_out_size +
has_delta_size;
- if ( FT_ALLOC( pool, pool_size ) )
- goto Exit;
-
- p = pool;
- peak_coords = (FT_Fixed*)p;
- p += peak_coords_size;
- point_deltas_x = (FT_Fixed*)p;
- p += point_deltas_x_size;
- points_org = (FT_Vector*)p;
- p += points_org_size;
- points_out = (FT_Vector*)p;
- p += points_out_size;
- has_delta = (FT_Bool*)p;
+ if ( pool_size > blend->glyph_delta_pool_size )
+ {
+ if ( FT_QREALLOC( blend->glyph_delta_pool,
+ blend->glyph_delta_pool_size,
+ pool_size ) )
+ goto Exit;
+ blend->glyph_delta_pool_size = pool_size;
+ }
+ pool = blend->glyph_delta_pool;
+
+ p = pool;
+ peak_coords = (FT_Fixed*)p;
+ p += peak_coords_size;
+ point_deltas_x = (FT_Fixed*)p;
+ p += point_deltas_x_size;
+ deltas_x_buf = (FT_Fixed*)p;
+ p += deltas_buf_size;
+ shared_points_buf = (FT_UShort*)p;
+ p += points_buf_size;
+ points_org = (FT_Vector*)p;
+ p += points_org_size;
+ points_out = (FT_Vector*)p;
+ p += points_out_size;
+ has_delta = (FT_Bool*)p;
FT_ARRAY_ZERO( point_deltas_x, 2 * n_points );
- im_start_coords = peak_coords + blend->num_axis;
- im_end_coords = im_start_coords + blend->num_axis;
- point_deltas_y = point_deltas_x + n_points;
+ im_start_coords = peak_coords + blend->num_axis;
+ im_end_coords = im_start_coords + blend->num_axis;
+ point_deltas_y = point_deltas_x + n_points;
+ deltas_y_buf = deltas_x_buf + n_points;
+ local_points_buf = shared_points_buf + n_points;
for ( j = 0; j < n_points; j++ )
{
@@ -4402,39 +4656,102 @@
points_org[j].y = FT_intToFixed( outline->points[j].y );
}
- p = stream->cursor;
+ if ( tupleCount & GX_TC_TUPLES_SHARE_POINT_NUMBERS )
+ {
+ here = FT_Stream_FTell( stream );
+
+ FT_Stream_SeekSet( stream, offsetToData );
+
+ sharedpoints = ft_var_readpackedpoints( stream, &spoint_count,
+ shared_points_buf, n_points );
+
+ offsetToData = FT_Stream_FTell( stream );
+
+ FT_Stream_SeekSet( stream, here );
+ }
+
+ FT_TRACE5(( "gvar: there %s %u tuple%s:\n",
+ ( tupleCount & GX_TC_TUPLE_COUNT_MASK ) == 1 ? "is" : "are",
+ tupleCount & GX_TC_TUPLE_COUNT_MASK,
+ ( tupleCount & GX_TC_TUPLE_COUNT_MASK ) == 1 ? "" : "s" ));
+
+ p = stream->cursor;
+ tupleScalars = blend->tuplescalars;
tupleCount &= GX_TC_TUPLE_COUNT_MASK;
+
+ /* The tuple-variation headers are walked sequentially through `p`. */
+ /* Each header is at least four bytes (`tupleDataSize` and */
+ /* `tupleIndex`); embedded and intermediate tuples store additional */
+ /* coordinate arrays after that. Bounds-check the four-byte minimum */
+ /* of *all* tuples here, once, so the common path inside the loop */
+ /* needs no per-tuple check. This is sound because `p` only ever */
+ /* advances over these headers -- the variation data of active tuples */
+ /* is read further below through a separate stream cursor -- and */
+ /* `stream->limit` stays constant within the frame entered above. */
+ /* Whenever a tuple consumes more than the four-byte minimum, the */
+ /* embedded/intermediate branches re-check the surplus (see below). */
+ if ( 4 * tupleCount > (FT_UInt)( stream->limit - p ) )
+ {
+ FT_TRACE2(( "TT_Vary_Apply_Glyph_Deltas:"
+ " invalid glyph variation array header\n" ));
+ error = FT_THROW( Invalid_Table );
+ goto Exit;
+ }
+
for ( i = 0; i < tupleCount; i++ )
{
FT_UInt tupleDataSize;
FT_UInt tupleIndex;
FT_Fixed apply;
- FT_Fixed* tupleScalars;
FT_TRACE6(( " tuple %u:\n", i ));
- tupleScalars = blend->tuplescalars;
+ /* The four-byte header is already covered by the up-front check. */
+ tupleDataSize = FT_NEXT_USHORT( p );
+ tupleIndex = FT_NEXT_USHORT( p );
- /* Enter frame for four bytes. */
- if ( 4 > stream->limit - p )
+ /* Fast path for the common shared-tuple case. */
+ /* */
+ /* `blend->tuplecount` is clamped to `GX_TI_TUPLE_INDEX_MASK + 1` at */
+ /* load time, so it is at most 0x1000. Of the tuple-index flags */
+ /* only `GX_TI_PRIVATE_POINT_NUMBERS` (0x2000) may legitimately */
+ /* accompany a shared-tuple index, so it is masked off here; the */
+ /* embedded (0x8000), intermediate (0x4000), and reserved (0x1000) */
+ /* flags each yield a value >= 0x1000 >= `blend->tuplecount', so if */
+ /* any is set the comparison is false. Entering the body therefore */
+ /* guarantees a plain shared tuple with an in-range index whose */
+ /* scalar lives in the cache. */
+ if ( ( tupleIndex & ~GX_TI_PRIVATE_POINT_NUMBERS )
+ < blend->tuplecount )
{
- FT_TRACE2(( "TT_Vary_Apply_Glyph_Deltas:"
- " invalid glyph variation array header\n" ));
- error = FT_THROW( Invalid_Table );
- goto Exit;
- }
+ FT_Fixed scalar = tupleScalars[tupleIndex & GX_TI_TUPLE_INDEX_MASK];
- tupleDataSize = FT_NEXT_USHORT( p );
- tupleIndex = FT_NEXT_USHORT( p );
- if ( tupleIndex & GX_TI_INTERMEDIATE_TUPLE )
- tupleScalars = NULL;
+ if ( scalar == 0 )
+ {
+ offsetToData += tupleDataSize;
+ continue;
+ }
+
+ if ( scalar != (FT_Fixed)-0x20000 )
+ {
+ apply = scalar;
+ goto apply_found;
+ }
+ }
if ( tupleIndex & GX_TI_EMBEDDED_TUPLE_COORD )
{
- if ( 2 * blend->num_axis > (FT_UInt)( stream->limit - p ) )
+ /* This tuple stores its peak coordinates inline. `p` has */
+ /* advanced past the four-byte header, so the up-front check now */
+ /* only accounts for the four-byte minimum of the tuples still to */
+ /* come, `4 * ( tupleCount - i - 1 )`. Ensure the embedded */
+ /* coordinates fit on top of that reserve; this bounds the read */
+ /* and keeps the invariant intact for the following tuples. */
+ if ( 2 * blend->num_axis + 4 * ( tupleCount - i - 1 ) >
+ (FT_UInt)( stream->limit - p ) )
{
FT_TRACE2(( "TT_Vary_Apply_Glyph_Deltas:"
" invalid glyph variation array header\n" ));
@@ -4446,22 +4763,9 @@
peak_coords[j] = FT_fdot14ToFixed( FT_NEXT_SHORT( p ) );
tuple_coords = peak_coords;
- tupleScalars = NULL;
}
else if ( ( tupleIndex & GX_TI_TUPLE_INDEX_MASK ) < blend->tuplecount )
{
- FT_Fixed scalar =
- tupleScalars
- ? tupleScalars[tupleIndex & GX_TI_TUPLE_INDEX_MASK]
- : (FT_Fixed)-0x20000;
-
-
- if ( scalar != (FT_Fixed)-0x20000 )
- {
- apply = scalar;
- goto apply_found;
- }
-
tuple_coords = blend->tuplecoords +
( tupleIndex & GX_TI_TUPLE_INDEX_MASK ) *
blend->num_axis;
@@ -4477,7 +4781,12 @@
if ( tupleIndex & GX_TI_INTERMEDIATE_TUPLE )
{
- if ( 4 * blend->num_axis > (FT_UInt)( stream->limit - p ) )
+ /* Intermediate tuples store inline start and end coordinates, */
+ /* `4 * num_axis` bytes in total. As above, check them against */
+ /* the four-byte reserve of the remaining tuples so the invariant */
+ /* survives into the next iteration. */
+ if ( 4 * blend->num_axis + 4 * ( tupleCount - i - 1 ) >
+ (FT_UInt)( stream->limit - p ) )
{
FT_TRACE2(( "TT_Vary_Apply_Glyph_Deltas:"
" invalid glyph variation array header\n" ));
@@ -4497,7 +4806,9 @@
im_start_coords,
im_end_coords );
- if ( tupleScalars )
+ /* Cache the scalar for shared tuples only (see fast path above). */
+ if ( ( tupleIndex & ~GX_TI_PRIVATE_POINT_NUMBERS )
+ < blend->tuplecount )
tupleScalars[tupleIndex & GX_TI_TUPLE_INDEX_MASK] = apply;
apply_found:
@@ -4514,7 +4825,8 @@
if ( tupleIndex & GX_TI_PRIVATE_POINT_NUMBERS )
{
- localpoints = ft_var_readpackedpoints( stream, &point_count );
+ localpoints = ft_var_readpackedpoints( stream, &point_count,
+ local_points_buf, n_points );
points = localpoints;
}
else
@@ -4525,10 +4837,14 @@
deltas_x = ft_var_readpackeddeltas( stream,
point_count == 0 ? n_points
- : point_count );
+ : point_count,
+ deltas_x_buf,
+ n_points );
deltas_y = ft_var_readpackeddeltas( stream,
point_count == 0 ? n_points
- : point_count );
+ : point_count,
+ deltas_y_buf,
+ n_points );
if ( !points || !deltas_y || !deltas_x )
; /* failure, ignore it */
@@ -4653,10 +4969,14 @@
#endif
}
- if ( localpoints != ALL_POINTS )
+ /* Only free deltas that fell back to a heap allocation; */
+ /* the common case reuses the pooled scratch buffers. */
+ if ( localpoints != ALL_POINTS && localpoints != local_points_buf )
FT_FREE( localpoints );
- FT_FREE( deltas_x );
- FT_FREE( deltas_y );
+ if ( deltas_x != deltas_x_buf )
+ FT_FREE( deltas_x );
+ if ( deltas_y != deltas_y_buf )
+ FT_FREE( deltas_y );
offsetToData += tupleDataSize;
@@ -4711,9 +5031,10 @@
}
Exit:
- if ( sharedpoints != ALL_POINTS )
+ if ( sharedpoints != ALL_POINTS && sharedpoints != shared_points_buf )
FT_FREE( sharedpoints );
- FT_FREE( pool );
+ /* The persistent per-face scratch buffer (`blend->glyph_delta_pool`) */
+ /* is freed in `tt_done_blend`, not here. */
FExit:
FT_FRAME_EXIT();
@@ -4793,6 +5114,9 @@
FT_FREE( itemStore->varRegionList );
}
+
+ FT_FREE( itemStore->regionScalars );
+ FT_FREE( itemStore->cachedCoords );
}
@@ -4829,13 +5153,13 @@
FT_UInt i, num_axes;
- /* blend->num_axis might not be set up yet */
- num_axes = blend->mmvar->num_axis;
+ num_axes = blend->num_axis;
FT_FREE( blend->coords );
FT_FREE( blend->normalizedcoords );
FT_FREE( blend->normalized_stylecoords );
FT_FREE( blend->mmvar );
+ FT_FREE( blend->glyph_delta_pool );
if ( blend->avar_table )
{
@@ -4888,6 +5212,9 @@
FT_FREE( blend->tuplecoords );
FT_FREE( blend->glyphoffsets );
FT_FREE( blend );
+
+ /* the face may outlive its blend (e.g., after a failed setup) */
+ ttface->blend = NULL;
}
}
diff --git a/thirdparty/freetype/src/truetype/ttgxvar.h b/thirdparty/freetype/src/truetype/ttgxvar.h
index 8a59c0cd6e6..c8446e134b2 100644
--- a/thirdparty/freetype/src/truetype/ttgxvar.h
+++ b/thirdparty/freetype/src/truetype/ttgxvar.h
@@ -262,6 +262,10 @@ FT_BEGIN_HEADER
* gv_glyphcnt ::
* The number of glyphs handled in the `gvar' table.
*
+ * gvar_is_extended ::
+ * A boolean which is set when glyph variation data comes from the
+ * 'GVAR' table (instead of 'gvar').
+ *
* glyphoffsets ::
* Offsets into the glyph variation data array.
*
@@ -300,10 +304,16 @@ FT_BEGIN_HEADER
FT_Fixed* tuplescalars; /* tuplescalars[tuplecount] */
FT_UInt gv_glyphcnt;
+ FT_Bool gvar_is_extended;
FT_ULong* glyphoffsets; /* glyphoffsets[gv_glyphcnt + 1] */
FT_ULong gvar_size;
+ /* Scratch pool reused across glyphs by `TT_Vary_Apply_Glyph_Deltas`, */
+ /* grown on demand, to avoid per-glyph/per-tuple heap allocations. */
+ FT_Byte* glyph_delta_pool;
+ FT_ULong glyph_delta_pool_size;
+
} GX_BlendRec;
@@ -443,6 +453,13 @@ FT_BEGIN_HEADER
FT_Fixed* *normalizedcoords,
FT_MM_Var* *mm_var );
+ FT_LOCAL( FT_Error )
+ tt_var_init_gvar( FT_Face face );
+
+ FT_LOCAL( FT_Error )
+ tt_var_init_blend( FT_Face face,
+ FT_UInt axis_count );
+
FT_LOCAL( void )
tt_done_blend( FT_Face face );
diff --git a/thirdparty/freetype/src/truetype/ttinterp.c b/thirdparty/freetype/src/truetype/ttinterp.c
index f850bc418e7..cc99b2ebd42 100644
--- a/thirdparty/freetype/src/truetype/ttinterp.c
+++ b/thirdparty/freetype/src/truetype/ttinterp.c
@@ -1104,7 +1104,7 @@
/* documentation is in freetype.h */
static __inline FT_Int32
- TT_MulFixi14_i386( FT_Int32 a,
+ TT_MulFix14_i386( FT_Int32 a,
FT_Int32 b )
{
FT_Int32 result;
@@ -1499,7 +1499,9 @@
*
* @Note:
* See `ttinterp.h' for details on backward compatibility mode.
- * `Touches' the point.
+ * `Touches' the point. When both freedom and projection vectors
+ * are aligned along the axes, the moves are handled by simplified
+ * versions of this function; more complex moves are handled here.
*/
static void
Direct_Move( TT_ExecContext exc,
@@ -1514,10 +1516,10 @@
if ( v != 0 )
{
#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
- /* Exception to the post-IUP curfew: Allow the x component of */
- /* diagonal moves, but only post-IUP. DejaVu tries to adjust */
- /* diagonal stems like on `Z' and `z' post-IUP. */
- if ( !exc->backward_compatibility )
+ /* See `ttinterp.h' for details on backward compatibility mode. */
+ /* Exception for horizontal projection is made for Comic Sans MS. */
+ if ( !exc->backward_compatibility ||
+ exc->GS.projVector.y == 0 )
#endif
zone->cur[point].x = ADD_LONG( zone->cur[point].x,
FT_MulFix( distance, v ) );
@@ -1529,8 +1531,10 @@
if ( v != 0 )
{
#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
- /* See `ttinterp.h' for details on backward compatibility mode. */
- if ( exc->backward_compatibility != 0x7 )
+ /* See `ttinterp.h' for details on backward compatibility mode. */
+ /* Exception for horizontal projection is made for Comic Sans MS. */
+ if ( exc->backward_compatibility != 0x7 ||
+ exc->GS.projVector.y == 0 )
#endif
zone->cur[point].y = ADD_LONG( zone->cur[point].y,
FT_MulFix( distance, v ) );
@@ -1660,6 +1664,27 @@
zone->org[point].y = ADD_LONG( zone->org[point].y, distance );
}
+
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+
+ static void
+ Update_Native_ClearType_X_State( TT_ExecContext exc )
+ {
+ /* `backward_compatibility' is also zero in v35, the CVT program, */
+ /* monochrome rendering, and tricky-font execution. */
+ exc->native_cleartype_x = FT_BOOL(
+ !exc->backward_compatibility &&
+ exc->GS.projVector.y == 0 &&
+ exc->GS.freeVector.y == 0 &&
+ SUBPIXEL_HINTING_MINIMAL &&
+ exc->iniRange == tt_coderange_glyph &&
+ exc->mode != FT_RENDER_MODE_MONO &&
+ !FT_IS_TRICKY( (FT_Face)exc->face ) );
+ }
+
+#endif /* TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL */
+
+
/**************************************************************************
*
* @Function:
@@ -2250,9 +2275,12 @@
exc->moveVector.x = exc->GS.freeVector.x * 4;
exc->moveVector.y = exc->GS.freeVector.y * 4;
}
- else if ( -0x400L < F_dot_P && F_dot_P < 0x400L )
+ else if ( -0x1555L < F_dot_P && F_dot_P < 0x1555L )
{
- /* prohibitively orthogonal */
+ /* prohibitively near-orthogonal; the empirical limit avoids */
+ /* bad rendering in Palatino without negative consequences */
+ FT_TRACE3(( "F_dot_P = %.2f, too small for valid movement.\n",
+ F_dot_P / 16384.));
exc->moveVector.x = 0;
exc->moveVector.y = 0;
}
@@ -2292,6 +2320,12 @@
else
exc->func_dualproj = (TT_Project_Func)Dual_Project;
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ /* Projection/freedom-vector changes can enter or leave the */
+ /* native ClearType direction. */
+ Update_Native_ClearType_X_State( exc );
+#endif
+
/* Disable cached aspect ratio */
exc->tt_metrics.ratio = 0;
}
@@ -2841,7 +2875,13 @@
ARRAY_BOUND_ERROR;
}
else
+ {
+ Modify_CVT_Check( exc );
+ if ( exc->error )
+ return;
+
exc->cvt[I] = FT_MulFix( args[1], exc->tt_metrics.scale );
+ }
}
@@ -2908,8 +2948,19 @@
Ins_ROUND( TT_ExecContext exc,
FT_Long* args )
{
- args[0] = exc->func_round( exc, args[0],
- exc->GS.compensation[exc->opcode & 3] );
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ /*
+ * Native ClearType applies ROUND to the 1/16-pixel virtual
+ * grid in the ClearType direction. Approximate this in v40
+ * by leaving the result unrounded.
+ */
+ if ( exc->native_cleartype_x )
+ args[0] = Round_None( exc, args[0],
+ exc->GS.compensation[exc->opcode & 3] );
+ else
+#endif
+ args[0] = exc->func_round( exc, args[0],
+ exc->GS.compensation[exc->opcode & 3] );
}
@@ -4722,7 +4773,13 @@
/* compatibility hacks and lets them program points to the grid like */
/* it's 1996. They might sign a waiver for just one glyph, though. */
if ( SUBPIXEL_HINTING_MINIMAL )
+ {
exc->backward_compatibility = ( L & 4 ) ^ 4;
+
+ /* A glyph can temporarily switch between backward-compatible */
+ /* and native ClearType, so update the native-X state too. */
+ Update_Native_ClearType_X_State( exc );
+ }
#endif
}
else if ( exc->pedantic_hinting )
@@ -4923,37 +4980,38 @@
Compute_Point_Displacement( TT_ExecContext exc,
FT_F26Dot6* x,
FT_F26Dot6* y,
- TT_GlyphZone zone,
- FT_UShort* refp )
+ FT_Vector* cur,
+ FT_UInt* refp )
{
- TT_GlyphZoneRec zp;
- FT_UShort p;
- FT_F26Dot6 d;
+ TT_GlyphZone zp;
+ FT_UShort p;
+ FT_F26Dot6 d;
if ( exc->opcode & 1 )
{
- zp = exc->zp0;
+ zp = &exc->zp0;
p = exc->GS.rp1;
}
else
{
- zp = exc->zp1;
+ zp = &exc->zp1;
p = exc->GS.rp2;
}
- if ( BOUNDS( p, zp.n_points ) )
+ if ( BOUNDS( p, zp->n_points ) )
{
if ( exc->pedantic_hinting )
exc->error = FT_THROW( Invalid_Reference );
- *refp = 0;
return FAILURE;
}
- *zone = zp;
- *refp = p;
+ /* return reference if zones match */
+ if ( refp )
+ *refp = cur == zp->cur ? p
+ : ~0U; /* nan */
- d = PROJECT( zp.cur + p, zp.org + p );
+ d = PROJECT( zp->cur + p, zp->org + p );
*x = FT_MulFix( d, exc->moveVector.x );
*y = FT_MulFix( d, exc->moveVector.y );
@@ -4965,10 +5023,9 @@
/* See `ttinterp.h' for details on backward compatibility mode. */
static void
Move_Zp2_Point( TT_ExecContext exc,
- FT_UShort point,
+ FT_UInt point,
FT_F26Dot6 dx,
- FT_F26Dot6 dy,
- FT_Bool touch )
+ FT_F26Dot6 dy )
{
if ( exc->GS.freeVector.x != 0 )
{
@@ -4978,8 +5035,7 @@
#endif
exc->zp2.cur[point].x = ADD_LONG( exc->zp2.cur[point].x, dx );
- if ( touch )
- exc->zp2.tags[point] |= FT_CURVE_TAG_TOUCH_X;
+ exc->zp2.tags[point] |= FT_CURVE_TAG_TOUCH_X;
}
if ( exc->GS.freeVector.y != 0 )
@@ -4990,8 +5046,7 @@
#endif
exc->zp2.cur[point].y = ADD_LONG( exc->zp2.cur[point].y, dy );
- if ( touch )
- exc->zp2.tags[point] |= FT_CURVE_TAG_TOUCH_Y;
+ exc->zp2.tags[point] |= FT_CURVE_TAG_TOUCH_Y;
}
}
@@ -5007,11 +5062,9 @@
FT_Long* args )
{
FT_Long loop = exc->GS.loop;
- TT_GlyphZoneRec zp;
- FT_UShort refp;
FT_F26Dot6 dx, dy;
- FT_UShort point;
+ FT_UInt point;
if ( exc->new_top < loop )
@@ -5023,12 +5076,12 @@
exc->new_top -= loop;
- if ( Compute_Point_Displacement( exc, &dx, &dy, &zp, &refp ) )
+ if ( Compute_Point_Displacement( exc, &dx, &dy, NULL, NULL ) )
return;
while ( loop-- )
{
- point = (FT_UShort)*(--args);
+ point = (FT_UInt)*(--args);
if ( BOUNDS( point, exc->zp2.n_points ) )
{
@@ -5039,7 +5092,7 @@
}
}
else
- Move_Zp2_Point( exc, point, dx, dy, TRUE );
+ Move_Zp2_Point( exc, point, dx, dy );
}
Fail:
@@ -5061,12 +5114,10 @@
Ins_SHC( TT_ExecContext exc,
FT_Long* args )
{
- TT_GlyphZoneRec zp;
- FT_UShort refp;
+ FT_UInt refp, start, limit, i;
FT_F26Dot6 dx, dy;
FT_UShort contour, bounds;
- FT_UShort start, limit, i;
contour = (FT_UShort)args[0];
@@ -5079,7 +5130,7 @@
return;
}
- if ( Compute_Point_Displacement( exc, &dx, &dy, &zp, &refp ) )
+ if ( Compute_Point_Displacement( exc, &dx, &dy, exc->zp2.cur, &refp ) )
return;
if ( contour == 0 )
@@ -5095,8 +5146,8 @@
for ( i = start; i < limit; i++ )
{
- if ( zp.cur != exc->zp2.cur || refp != i )
- Move_Zp2_Point( exc, i, dx, dy, TRUE );
+ if ( refp != i )
+ Move_Zp2_Point( exc, i, dx, dy );
}
}
@@ -5111,40 +5162,59 @@
Ins_SHZ( TT_ExecContext exc,
FT_Long* args )
{
- TT_GlyphZoneRec zp;
- FT_UShort refp;
- FT_F26Dot6 dx,
- dy;
-
- FT_UShort limit, i;
+ FT_Vector* cur;
+ FT_UInt refp, i, limit;
+ FT_F26Dot6 dx, dy;
- if ( BOUNDS( args[0], 2 ) )
+ /* XXX: UNDOCUMENTED! SHZ doesn't move the phantom points, */
+ /* which must be subtracted. */
+ switch ( (FT_Int)args[0] )
{
+ case 0:
+ cur = exc->twilight.cur;
+ limit = exc->twilight.n_points;
+ break;
+
+ case 1:
+ cur = exc->pts.cur;
+ limit = exc->pts.n_points > 4U ? exc->pts.n_points - 4U : 0;
+ break;
+
+ default:
if ( exc->pedantic_hinting )
exc->error = FT_THROW( Invalid_Reference );
return;
}
- if ( Compute_Point_Displacement( exc, &dx, &dy, &zp, &refp ) )
+ if ( Compute_Point_Displacement( exc, &dx, &dy, cur, &refp ) )
return;
- /* XXX: UNDOCUMENTED! SHZ doesn't move the phantom points. */
- /* Twilight zone has no real contours, so use `n_points'. */
- /* Normal zone's `n_points' includes phantoms, so must */
- /* use end of last contour. */
- if ( exc->GS.gep2 == 0 )
- limit = exc->zp2.n_points;
- else if ( exc->GS.gep2 == 1 && exc->zp2.n_contours > 0 )
- limit = exc->zp2.contours[exc->zp2.n_contours - 1] + 1;
- else
- limit = 0;
+ /* XXX: UNDOCUMENTED! SHZ doesn't touch the points. */
+ if ( dx )
+ {
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ /* See `ttinterp.h' for details on backward compatibility mode. */
+ if ( !exc->backward_compatibility )
+#endif
+ for ( i = 0; i < limit; i++ )
+ {
+ if ( refp != i )
+ cur[i].x = ADD_LONG( cur[i].x, dx );
+ }
+ }
- /* XXX: UNDOCUMENTED! SHZ doesn't touch the points */
- for ( i = 0; i < limit; i++ )
+ if ( dy )
{
- if ( zp.cur != exc->zp2.cur || refp != i )
- Move_Zp2_Point( exc, i, dx, dy, FALSE );
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ /* See `ttinterp.h' for details on backward compatibility mode. */
+ if ( exc->backward_compatibility != 0x7 )
+#endif
+ for ( i = 0; i < limit; i++ )
+ {
+ if ( refp != i )
+ cur[i].y = ADD_LONG( cur[i].y, dy );
+ }
}
}
@@ -5161,7 +5231,7 @@
{
FT_Long loop = exc->GS.loop;
FT_F26Dot6 dx, dy;
- FT_UShort point;
+ FT_UInt point;
#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
FT_Bool in_twilight = FT_BOOL( exc->GS.gep0 == 0 ||
exc->GS.gep1 == 0 ||
@@ -5183,7 +5253,7 @@
while ( loop-- )
{
- point = (FT_UShort)*(--args);
+ point = (FT_UInt)*(--args);
if ( BOUNDS( point, exc->zp2.n_points ) )
{
@@ -5206,11 +5276,11 @@
( exc->backward_compatibility != 0x7 &&
( ( exc->is_composite && exc->GS.freeVector.y != 0 ) ||
( exc->zp2.tags[point] & FT_CURVE_TAG_TOUCH_Y ) ) ) )
- Move_Zp2_Point( exc, point, 0, dy, TRUE );
+ Move_Zp2_Point( exc, point, 0, dy );
}
else
#endif
- Move_Zp2_Point( exc, point, dx, dy, TRUE );
+ Move_Zp2_Point( exc, point, dx, dy );
}
Fail:
@@ -5293,7 +5363,12 @@
if ( ( exc->opcode & 1 ) != 0 )
{
cur_dist = FAST_PROJECT( &exc->zp0.cur[point] );
- distance = SUB_LONG( exc->func_round( exc, cur_dist, 0 ), cur_dist );
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ if ( exc->native_cleartype_x )
+ distance = 0;
+ else
+#endif
+ distance = SUB_LONG( exc->func_round( exc, cur_dist, 0 ), cur_dist );
}
else
distance = 0;
@@ -5371,6 +5446,13 @@
FT_F26Dot6 delta;
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ /* Native ClearType reduces CVT cut-in to 1/16 in the */
+ /* ClearType direction. */
+ if ( exc->native_cleartype_x )
+ control_value_cutin >>= 4;
+#endif
+
delta = SUB_LONG( distance, org_dist );
if ( delta < 0 )
delta = NEG_LONG( delta );
@@ -5378,7 +5460,10 @@
if ( delta > control_value_cutin )
distance = org_dist;
- distance = exc->func_round( exc, distance, 0 );
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ if ( !exc->native_cleartype_x )
+#endif
+ distance = exc->func_round( exc, distance, 0 );
}
exc->func_move( exc, &exc->zp0, point, SUB_LONG( distance, org_dist ) );
@@ -5472,7 +5557,14 @@
compensation = exc->GS.compensation[exc->opcode & 3];
if ( ( exc->opcode & 4 ) != 0 )
- distance = exc->func_round( exc, org_dist, compensation );
+ {
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ if ( exc->native_cleartype_x )
+ distance = Round_None( exc, org_dist, compensation );
+ else
+#endif
+ distance = exc->func_round( exc, org_dist, compensation );
+ }
else
distance = Round_None( exc, org_dist, compensation );
@@ -5483,6 +5575,13 @@
FT_F26Dot6 minimum_distance = exc->GS.minimum_distance;
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ /* Native ClearType reduces minimum distance to 1/2 in the */
+ /* ClearType direction. */
+ if ( exc->native_cleartype_x )
+ minimum_distance >>= 1;
+#endif
+
if ( org_dist >= 0 )
{
if ( distance < minimum_distance )
@@ -5605,6 +5704,13 @@
FT_F26Dot6 control_value_cutin = exc->GS.control_value_cutin;
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ /* Native ClearType reduces CVT cut-in to 1/16 in the */
+ /* ClearType direction. */
+ if ( exc->native_cleartype_x )
+ control_value_cutin >>= 4;
+#endif
+
/* XXX: According to Greg Hitchcock, the following wording is */
/* the right one: */
/* */
@@ -5625,7 +5731,12 @@
cvt_dist = org_dist;
}
- distance = exc->func_round( exc, cvt_dist, compensation );
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ if ( exc->native_cleartype_x )
+ distance = Round_None( exc, cvt_dist, compensation );
+ else
+#endif
+ distance = exc->func_round( exc, cvt_dist, compensation );
}
else
distance = Round_None( exc, cvt_dist, compensation );
@@ -5637,6 +5748,13 @@
FT_F26Dot6 minimum_distance = exc->GS.minimum_distance;
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+ /* Native ClearType reduces minimum distance to 1/2 in the */
+ /* ClearType direction. */
+ if ( exc->native_cleartype_x )
+ minimum_distance >>= 1;
+#endif
+
if ( org_dist >= 0 )
{
if ( distance < minimum_distance )
@@ -6191,14 +6309,14 @@
IUP_WorkerRec V;
FT_Byte mask;
- FT_UInt first_point; /* first point of contour */
- FT_UInt end_point; /* end point (last+1) of contour */
+ FT_UInt first_point; /* first point of contour */
+ FT_UInt end_point; /* end point (last+1) of contour */
- FT_UInt first_touched; /* first touched point in contour */
- FT_UInt cur_touched; /* current touched point in contour */
+ FT_UInt first_touched; /* first touched point in contour */
+ FT_UInt cur_touched; /* current touched point in contour */
- FT_UInt point; /* current point */
- FT_Short contour; /* current contour */
+ FT_UInt point; /* current point */
+ FT_UInt contour; /* current contour */
#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
@@ -6566,11 +6684,23 @@
* Selector Bit: 11
* Return Bit(s): 18
*
- * The only smoothing method FreeType supports unless someone sets
- * FT_LOAD_TARGET_MONO.
+ * Report the smoothing mode selected by the DirectWrite-style
+ * ClearType policy for this face and PPEM.
*/
- if ( ( args[0] & 2048 ) != 0 && exc->mode != FT_RENDER_MODE_MONO )
- K |= 1 << 18;
+ if ( ( args[0] & 2048 ) != 0 &&
+ exc->mode != FT_RENDER_MODE_MONO )
+ {
+ FT_Bool symmetric_smoothing;
+
+
+ tt_face_get_cleartype_policy( exc->face,
+ exc->metrics.y_ppem,
+ &symmetric_smoothing,
+ NULL );
+
+ if ( symmetric_smoothing )
+ K |= 1 << 18;
+ }
/*********************************
* CLEARTYPE HINTING AND
@@ -7339,56 +7469,11 @@
}
if ( exc->error )
- {
- switch ( exc->error )
- {
- case FT_ERR( Invalid_Opcode ):
- {
- TT_DefRecord* def = exc->IDefs;
- TT_DefRecord* limit = FT_OFFSET( def, exc->numIDefs );
-
-
- /* looking for redefined instructions */
- for ( ; def < limit; def++ )
- {
- if ( def->active && exc->opcode == (FT_Byte)def->opc )
- {
- TT_CallRec* callrec;
-
-
- if ( exc->callTop >= exc->callSize )
- {
- exc->error = FT_THROW( Invalid_Reference );
- goto LErrorLabel_;
- }
-
- callrec = &exc->callStack[exc->callTop];
-
- callrec->Caller_Range = exc->curRange;
- callrec->Caller_IP = exc->IP + 1;
- callrec->Cur_Count = 1;
- callrec->Def = def;
-
- if ( Ins_Goto_CodeRange( exc,
- def->range,
- def->start ) == FAILURE )
- goto LErrorLabel_;
-
- goto LSuiteLabel_;
- }
- }
- }
- FALL_THROUGH;
-
- default:
- goto LErrorLabel_;
- }
- }
+ goto LErrorLabel_;
exc->top = exc->new_top;
exc->IP += exc->length;
- LSuiteLabel_:
if ( exc->IP >= exc->codeSize )
{
if ( exc->callTop > 0 )
diff --git a/thirdparty/freetype/src/truetype/ttinterp.h b/thirdparty/freetype/src/truetype/ttinterp.h
index 05ac831dfd1..5b71bb82d80 100644
--- a/thirdparty/freetype/src/truetype/ttinterp.h
+++ b/thirdparty/freetype/src/truetype/ttinterp.h
@@ -378,6 +378,14 @@ FT_BEGIN_HEADER
*
*/
+ /*
+ * Native ClearType horizontal execution state. Microsoft ClearType
+ * uses a 1/16-pixel grid in the ClearType direction; version 40
+ * approximates this by leaving rounded horizontal point moves
+ * unrounded.
+ */
+ FT_Bool native_cleartype_x;
+
/* Activate backward compatibility (bit 2) and track IUP (bits 0-1). */
/* If this is zero, it means that the interpreter is either in v35 */
/* or in native ClearType mode. */
diff --git a/thirdparty/freetype/src/truetype/ttobjs.c b/thirdparty/freetype/src/truetype/ttobjs.c
index dc897688584..8c76e23dbf8 100644
--- a/thirdparty/freetype/src/truetype/ttobjs.c
+++ b/thirdparty/freetype/src/truetype/ttobjs.c
@@ -21,6 +21,7 @@
#include <freetype/tttags.h>
#include <freetype/internal/sfnt.h>
#include <freetype/ftdriver.h>
+#include <freetype/ftgasp.h>
#include "ttgload.h"
#include "ttpload.h"
@@ -852,11 +853,74 @@
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
tt_done_blend( ttface );
- face->blend = NULL;
#endif
}
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+
+ FT_LOCAL_DEF( void )
+ tt_face_get_cleartype_policy( TT_Face face,
+ FT_UInt ppem,
+ FT_Bool* symmetric_smoothing,
+ FT_Bool* grid_fit )
+ {
+ FT_Bool symmetric;
+ FT_Bool fit;
+ FT_Bool found = FALSE;
+ FT_UShort flags = 0;
+ FT_UInt i;
+
+
+ /*
+ * DirectWrite fallback for hinted fonts without an applicable
+ * version-1 ClearType 'gasp' range:
+ *
+ * <= 20 ppem NATURAL
+ * > 20 ppem NATURAL_SYMMETRIC
+ *
+ * Fonts without glyph bytecode use symmetric rendering.
+ *
+ * `maxSizeOfInstructions` is also what Skia uses to determine
+ * whether a TrueType face is hinted for this DirectWrite policy.
+ */
+ fit = FT_BOOL( face->max_profile.maxSizeOfInstructions != 0 );
+
+ symmetric = FT_BOOL( !fit || ppem > 20 );
+
+ /*
+ * A valid version-1 `gasp' range explicitly controls the two
+ * independent ClearType properties.
+ */
+ if ( face->gasp.version >= 1 )
+ {
+ for ( i = 0; i < face->gasp.numRanges; i++ )
+ {
+ if ( ppem <= face->gasp.gaspRanges[i].maxPPEM )
+ {
+ flags = face->gasp.gaspRanges[i].gaspFlag;
+ found = TRUE;
+ break;
+ }
+ }
+
+ if ( found )
+ {
+ symmetric = FT_BOOL( flags & FT_GASP_SYMMETRIC_SMOOTHING );
+ fit = FT_BOOL( flags & FT_GASP_SYMMETRIC_GRIDFIT );
+ }
+ }
+
+ if ( symmetric_smoothing )
+ *symmetric_smoothing = symmetric;
+
+ if ( grid_fit )
+ *grid_fit = fit;
+ }
+
+#endif
+
+
/**************************************************************************
*
* SIZE FUNCTIONS
@@ -909,15 +973,15 @@
error = TT_Run_Context( exec, size );
FT_TRACE4(( error ? " failed (error code 0x%x)\n" : "",
error ));
+
+ if ( !error )
+ TT_Save_Context( exec, size );
}
else
error = FT_Err_Ok;
size->bytecode_ready = error;
- if ( !error )
- TT_Save_Context( exec, size );
-
return error;
}
@@ -990,15 +1054,15 @@
error = TT_Run_Context( exec, size );
FT_TRACE4(( error ? " failed (error code 0x%x)\n" : "",
error ));
+
+ if ( !error )
+ TT_Save_Context( exec, size );
}
else
error = FT_Err_Ok;
size->cvt_ready = error;
- if ( !error )
- TT_Save_Context( exec, size );
-
return error;
}
diff --git a/thirdparty/freetype/src/truetype/ttobjs.h b/thirdparty/freetype/src/truetype/ttobjs.h
index 9b2f412ac47..01e581e1c1a 100644
--- a/thirdparty/freetype/src/truetype/ttobjs.h
+++ b/thirdparty/freetype/src/truetype/ttobjs.h
@@ -325,6 +325,22 @@ FT_BEGIN_HEADER
tt_slot_init( FT_GlyphSlot slot );
+#ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL
+
+ /*
+ * Return the DirectWrite-style ClearType policy for a face and PPEM.
+ *
+ * Either output pointer may be NULL.
+ */
+ FT_LOCAL( void )
+ tt_face_get_cleartype_policy( TT_Face face,
+ FT_UInt ppem,
+ FT_Bool* symmetric_smoothing,
+ FT_Bool* grid_fit );
+
+#endif
+
+
/* auxiliary */
#define IS_HINTED( flags ) ( ( flags & FT_LOAD_NO_HINTING ) == 0 )
diff --git a/thirdparty/freetype/src/truetype/ttpload.c b/thirdparty/freetype/src/truetype/ttpload.c
index 5b0a3944d6a..ec85f48ce2c 100644
--- a/thirdparty/freetype/src/truetype/ttpload.c
+++ b/thirdparty/freetype/src/truetype/ttpload.c
@@ -69,7 +69,11 @@
/* we need the size of the `glyf' table for malformed `loca' tables */
- error = face->goto_table( face, TTAG_glyf, stream, &face->glyf_len );
+ error = face->goto_table( face,
+ face->is_extended_glyf ? TTAG_GLYF
+ : TTAG_glyf,
+ stream,
+ &face->glyf_len );
/* it is possible that a font doesn't have a glyf table at all */
/* or its size is zero */
@@ -91,7 +95,11 @@
}
FT_TRACE2(( "Locations " ));
- error = face->goto_table( face, TTAG_loca, stream, &table_len );
+ error = face->goto_table( face,
+ face->is_extended_glyf ? TTAG_LOCA
+ : TTAG_loca,
+ stream,
+ &table_len );
if ( error )
{
error = FT_THROW( Locations_Missing );
@@ -100,7 +108,8 @@
shift = face->header.Index_To_Loc_Format != 0 ? 2 : 1;
- if ( table_len > 0x10000UL << shift )
+ if ( !face->is_extended_glyf &&
+ table_len > 0x10000UL << shift )
{
FT_TRACE2(( "table too large\n" ));
table_len = 0x10000UL << shift;
diff --git a/thirdparty/freetype/src/truetype/ttvarc.c b/thirdparty/freetype/src/truetype/ttvarc.c
new file mode 100644
index 00000000000..de57443ced6
--- /dev/null
+++ b/thirdparty/freetype/src/truetype/ttvarc.c
@@ -0,0 +1,3774 @@
+/****************************************************************************
+ *
+ * ttvarc.c
+ *
+ * TrueType and OpenType VARC (Variable Composites) support (body).
+ *
+ * Copyright (C) 2025-2026 by
+ * Behdad Esfahbod, David Turner, Robert Wilhelm, and Werner Lemberg.
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+
+ /**************************************************************************
+ *
+ * VARC table specification:
+ *
+ * https://github.com/harfbuzz/boring-expansion-spec/blob/main/VARC.md
+ *
+ */
+
+
+#include <freetype/internal/ftdebug.h>
+#include <freetype/internal/ftstream.h>
+#include <freetype/internal/ftcalc.h>
+#include <freetype/internal/ftobjs.h>
+#include <freetype/tttags.h>
+#include <freetype/ftmm.h>
+#include <freetype/fttrigon.h>
+#include <freetype/ftoutln.h>
+#include <ft2build.h>
+
+#ifdef TT_CONFIG_OPTION_VARC
+
+#include "ttvarc.h"
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+#include "ttgxvar.h"
+#endif
+
+
+ /**************************************************************************
+ *
+ * The macro FT_COMPONENT is used in trace mode. It is an implicit
+ * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
+ * messages during execution.
+ */
+#undef FT_COMPONENT
+#define FT_COMPONENT ttvarc
+
+
+ FT_LOCAL_DEF( FT_Error )
+ tt_face_init_varc_axes( FT_Face face,
+ FT_UInt axis_count )
+ {
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+ return tt_var_init_blend( face, axis_count );
+#else
+ FT_UNUSED( face );
+ FT_UNUSED( axis_count );
+
+ return FT_Err_Ok;
+#endif
+ }
+
+
+ /* Macro for bounds checking when reading from table */
+#define CHECK_TABLE_BOUNDS( p, size ) \
+ ( (FT_Byte*)(p) >= \
+ (FT_Byte*)varc->table && \
+ (FT_Byte*)(p) + (size) <= \
+ (FT_Byte*)varc->table + varc->table_size )
+
+ /* Stack allocation limits for performance. */
+#define VARC_STACK_AXIS_COUNT 64
+#define VARC_STACK_DELTA_COUNT 128
+#define VARC_STACK_COORD_COUNT 64
+#define VARC_STACK_INDICES_COUNT 64
+
+
+ /**************************************************************************
+ *
+ * Utility Functions
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * read_uint32var
+ *
+ * @Description:
+ * Read a variable-length uint32 value (1-5 bytes).
+ * Similar to UTF-8 encoding but for integers.
+ *
+ * @Input:
+ * p ::
+ * Pointer to the byte stream (will be advanced).
+ *
+ * limit ::
+ * Maximum valid address in the stream.
+ *
+ * @Output:
+ * value ::
+ * The decoded uint32 value.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ read_uint32var( FT_Byte** p,
+ FT_Byte* limit,
+ FT_UInt32* value )
+ {
+ FT_Byte b0;
+ FT_UInt32 result;
+
+
+ if ( *p >= limit )
+ return FT_THROW( Invalid_Table );
+
+ b0 = *(*p)++;
+
+ /* single byte: 0xxxxxxx */
+ if ( b0 < 0x80 )
+ {
+ *value = b0;
+ return FT_Err_Ok;
+ }
+
+ /* two bytes: 10xxxxxx xxxxxxxx */
+ if ( b0 < 0xC0 )
+ {
+ if ( *p >= limit )
+ return FT_THROW( Invalid_Table );
+ result = ( ( b0 - 0x80 ) << 8 ) |
+ *(*p)++;
+ *value = result;
+ return FT_Err_Ok;
+ }
+
+ /* three bytes: 110xxxxx xxxxxxxx xxxxxxxx */
+ if ( b0 < 0xE0 )
+ {
+ if ( *p + 1 >= limit )
+ return FT_THROW( Invalid_Table );
+ result = ( ( b0 - 0xC0 ) << 16 ) |
+ ( (*p)[0] << 8 ) |
+ (*p)[1];
+ *p += 2;
+ *value = result;
+ return FT_Err_Ok;
+ }
+
+ /* four bytes: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx */
+ if ( b0 < 0xF0 )
+ {
+ if ( *p + 2 >= limit )
+ return FT_THROW( Invalid_Table );
+ result = ( ( b0 - 0xE0 ) << 24 ) |
+ ( (*p)[0] << 16 ) |
+ ( (*p)[1] << 8 ) |
+ (*p)[2];
+ *p += 3;
+ *value = result;
+ return FT_Err_Ok;
+ }
+
+ /* five bytes: 11110000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx */
+ if ( b0 == 0xF0 )
+ {
+ if ( *p + 3 >= limit )
+ return FT_THROW( Invalid_Table );
+ result = ( (*p)[0] << 24 ) |
+ ( (*p)[1] << 16 ) |
+ ( (*p)[2] << 8 ) |
+ (*p)[3];
+ *p += 4;
+ *value = result;
+ return FT_Err_Ok;
+ }
+
+ /* invalid encoding */
+ return FT_THROW( Invalid_Table );
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * read_fword
+ *
+ * @Description:
+ * Read a signed 16-bit FWORD value.
+ *
+ * @Input:
+ * p ::
+ * Pointer to the byte stream (will be advanced).
+ *
+ * limit ::
+ * Maximum valid address in the stream.
+ *
+ * @Output:
+ * value ::
+ * The decoded FWORD value as 26.6 fixed-point.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ read_fword( FT_Byte** p,
+ FT_Byte* limit,
+ FT_Pos* value )
+ {
+ FT_Short val;
+
+
+ if ( *p + 1 >= limit )
+ return FT_THROW( Invalid_Table );
+
+ val = (FT_Short)( ( (FT_UShort)(*p)[0] << 8 ) |
+ (FT_UShort)(*p)[1] );
+ *p += 2;
+
+ /* FWORD is a signed 16-bit integer, */
+ /* shift left by 6 for FT_Pos (26.6 format). */
+ *value = (FT_Pos)val * 64;
+
+ return FT_Err_Ok;
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * read_f4dot12
+ *
+ * @Description:
+ * Read a signed F4DOT12 value (4.12 fixed-point).
+ *
+ * @Input:
+ * p ::
+ * Pointer to the byte stream (will be advanced).
+ *
+ * limit ::
+ * Maximum valid address in the stream.
+ *
+ * @Output:
+ * value ::
+ * The decoded value as 16.16 fixed-point.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ read_f4dot12( FT_Byte** p,
+ FT_Byte* limit,
+ FT_Fixed* value )
+ {
+ FT_Short val;
+
+
+ if ( *p + 1 >= limit )
+ return FT_THROW( Invalid_Table );
+
+ val = (FT_Short)( ( (FT_UShort)(*p)[0] << 8 ) |
+ (FT_UShort)(*p)[1] );
+ *p += 2;
+
+ /* Convert F4DOT12 (4.12) to F16DOT16 (16.16) by shifting left 4. */
+ *value = (FT_Fixed)val * 16;
+
+ return FT_Err_Ok;
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * read_f6dot10
+ *
+ * @Description:
+ * Read a signed F6DOT10 value (6.10 fixed-point).
+ *
+ * @Input:
+ * p ::
+ * Pointer to the byte stream (will be advanced).
+ *
+ * limit ::
+ * Maximum valid address in the stream.
+ *
+ * @Output:
+ * value ::
+ * The decoded value as 16.16 fixed-point.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ read_f6dot10( FT_Byte** p,
+ FT_Byte* limit,
+ FT_Fixed* value )
+ {
+ FT_Short val;
+
+
+ if ( *p + 1 >= limit )
+ return FT_THROW( Invalid_Table );
+
+ val = (FT_Short)( ( (FT_UShort)(*p)[0] << 8 ) |
+ (FT_UShort)(*p)[1] );
+ *p += 2;
+
+ /* Simply shift left by 6. */
+ *value = (FT_Fixed)val * 64;
+
+ return FT_Err_Ok;
+ }
+
+
+ /**************************************************************************
+ *
+ * Coverage Table Functions
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_get_coverage
+ *
+ * @Description:
+ * Check whether a glyph is covered by the 'VARC' table using binary
+ * search.
+ *
+ * @Input:
+ * varc ::
+ * The 'VARC' table structure.
+ *
+ * glyph_index ::
+ * The glyph ID to check.
+ *
+ * @Return:
+ * Coverage index if found, -1 otherwise.
+ */
+ static FT_Int
+ tt_varc_get_coverage( TT_Varc varc,
+ FT_UInt glyph_index )
+ {
+ FT_Byte* p;
+ FT_UInt format;
+ FT_UInt count;
+
+
+ if ( !varc || !varc->coverage )
+ return -1;
+
+ p = varc->coverage;
+
+ /* Check bounds for format and count. */
+ if ( !CHECK_TABLE_BOUNDS( p, 4 ) )
+ return -1;
+
+ format = ( p[0] << 8 ) | p[1];
+ p += 2;
+
+ /* formats 1 and 3: list of 16-bit or 24-bit glyph IDs */
+ if ( format == 1 || format == 3 )
+ {
+ FT_UInt glyph_size = format == 1 ? 2 : 3;
+
+
+ if ( !CHECK_TABLE_BOUNDS( p, glyph_size ) )
+ return -1;
+
+ count = format == 1 ? FT_NEXT_USHORT( p )
+ : FT_NEXT_UOFF3( p );
+
+ if ( !CHECK_TABLE_BOUNDS( p, count * glyph_size ) )
+ return -1;
+
+ /* binary search */
+ {
+ FT_UInt min = 0;
+ FT_UInt max = count;
+
+
+ while ( min < max )
+ {
+ FT_UInt mid = ( min + max ) >> 1;
+ FT_Byte* gid_p = p + mid * glyph_size;
+ FT_UInt gid = format == 1 ? FT_PEEK_USHORT( gid_p )
+ : FT_PEEK_UOFF3( gid_p );
+
+
+ if ( gid == glyph_index )
+ return (FT_Int)mid;
+ else if ( gid < glyph_index )
+ min = mid + 1;
+ else
+ max = mid;
+ }
+ }
+ }
+
+ /* formats 2 and 4: range list with 16-bit or 24-bit values */
+ else if ( format == 2 || format == 4 )
+ {
+ FT_UInt value_size = format == 2 ? 2 : 3;
+ FT_UInt record_size = value_size * 3;
+ FT_UInt min, max;
+
+
+ if ( !CHECK_TABLE_BOUNDS( p, value_size ) )
+ return -1;
+
+ count = format == 2 ? FT_NEXT_USHORT( p )
+ : FT_NEXT_UOFF3( p );
+
+ if ( !CHECK_TABLE_BOUNDS( p, count * record_size ) )
+ return -1;
+
+ /* binary search the sorted ranges */
+ min = 0;
+ max = count;
+ while ( min < max )
+ {
+ FT_UInt mid = ( min + max ) >> 1;
+ FT_Byte* rec = p + mid * record_size;
+ FT_UInt start = format == 2 ? FT_PEEK_USHORT( rec )
+ : FT_PEEK_UOFF3( rec );
+ FT_UInt end = format == 2 ? FT_PEEK_USHORT( rec + value_size )
+ : FT_PEEK_UOFF3( rec + value_size );
+
+
+ if ( glyph_index < start )
+ max = mid;
+ else if ( glyph_index > end )
+ min = mid + 1;
+ else
+ {
+ FT_Byte* index_p = rec + value_size * 2;
+ FT_UInt index = format == 2 ? FT_PEEK_USHORT( index_p )
+ : FT_PEEK_UOFF3( index_p );
+
+
+ return (FT_Int)( index + glyph_index - start );
+ }
+ }
+ }
+
+ return -1;
+ }
+
+
+ /**************************************************************************
+ *
+ * CFF2IndexOf Functions
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_read_cff2_index_entry
+ *
+ * @Description:
+ * Read entry `index` from a CFF2-style INDEX:
+ *
+ * count (uint32) | offSize (uint8) | offset[count + 1] | data
+ *
+ * Each offset is `offSize` bytes and 1-based. On success `*data`
+ * points to the entry's bytes and `*size` is its length. Every read
+ * and the returned range are validated against `table_limit`; the
+ * caller must pass an `index_data` pointer that lies within the table.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ tt_varc_read_cff2_index_entry( FT_Byte* index_data,
+ FT_Byte* table_limit,
+ FT_UInt index,
+ FT_Byte** data,
+ FT_UInt* size )
+ {
+ FT_Byte* p = index_data;
+ FT_UInt32 count;
+ FT_UInt offSize;
+ FT_ULong offset1 = 0;
+ FT_ULong offset2 = 0;
+ FT_Byte* offset_base;
+
+ FT_UInt i;
+
+
+ if ( !p || p + 4 > table_limit )
+ return FT_THROW( Invalid_Table );
+
+ count = ( (FT_UInt32)p[0] << 24 ) |
+ ( (FT_UInt32)p[1] << 16 ) |
+ ( (FT_UInt32)p[2] << 8 ) |
+ (FT_UInt32)p[3];
+ p += 4;
+
+ if ( p >= table_limit )
+ return FT_THROW( Invalid_Table );
+
+ offSize = *p++;
+ if ( offSize < 1 || offSize > 4 || index >= count )
+ return FT_THROW( Invalid_Table );
+
+ /* division avoids overflow in the offset-array bounds check */
+ if ( count >= (FT_UInt32)( ( table_limit - p ) / offSize ) )
+ return FT_THROW( Invalid_Table );
+
+ offset_base = p + ( count + 1 ) * offSize;
+
+ p += index * offSize;
+ for ( i = 0; i < offSize; i++ )
+ offset1 = ( offset1 << 8 ) | p[i];
+
+ p += offSize;
+ for ( i = 0; i < offSize; i++ )
+ offset2 = ( offset2 << 8 ) | p[i];
+
+ /* offsets are 1-based and must stay within the table */
+ if ( offset2 < offset1 ||
+ offset1 < 1 ||
+ offset2 - 1 > (FT_ULong)( table_limit - offset_base ) )
+ return FT_THROW( Invalid_Table );
+
+ *data = offset_base + offset1 - 1;
+ *size = (FT_UInt)( offset2 - offset1 );
+
+ return FT_Err_Ok;
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_get_glyph_record
+ *
+ * @Description:
+ * Get the glyph record from the `CFF2IndexOf` structure.
+ *
+ * @Input:
+ * varc ::
+ * The 'VARC' table structure.
+ *
+ * glyph_index ::
+ * The glyph ID.
+ *
+ * @Output:
+ * record_data ::
+ * Pointer to the glyph record data.
+ *
+ * record_size ::
+ * Size of the glyph record in bytes.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ tt_varc_get_glyph_record( TT_Varc varc,
+ FT_UInt glyph_index,
+ FT_Byte** record_data,
+ FT_UInt* record_size )
+ {
+ FT_Error error;
+ FT_Int coverage_index;
+
+
+ if ( !varc || !varc->var_composite_glyphs )
+ return FT_THROW( Invalid_Table );
+
+ coverage_index = tt_varc_get_coverage( varc, glyph_index );
+ if ( coverage_index < 0 )
+ return FT_THROW( Invalid_Glyph_Index );
+
+ error = tt_varc_read_cff2_index_entry(
+ varc->var_composite_glyphs,
+ (FT_Byte*)varc->table + varc->table_size,
+ (FT_UInt)coverage_index,
+ record_data, record_size );
+ if ( error )
+ return error;
+
+ /* a component record is at most 64KB */
+ if ( *record_size > 0xFFFFU )
+ return FT_THROW( Invalid_Table );
+
+ return FT_Err_Ok;
+ }
+
+
+ /**************************************************************************
+ *
+ * Component Parsing Functions
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_parse_component
+ *
+ * @Description:
+ * Parse a single VARC component record.
+ *
+ * @Input:
+ * face ::
+ * The font face.
+ *
+ * varc ::
+ * The 'VARC' table structure.
+ *
+ * p ::
+ * Pointer to component data (will be advanced).
+ *
+ * limit ::
+ * End of component data.
+ *
+ * axis_values_buffer ::
+ * A stack buffer to be used for small arrays.
+ *
+ * axis_values_buffer_size ::
+ * The size of `axis_values_buffer`. If the number of parsed axis
+ * values exceeds this size, an array gets allocated on the heap.
+ *
+ * @Output:
+ * component ::
+ * The parsed component structure.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ tt_varc_parse_component( TT_Face face,
+ TT_Varc varc,
+ FT_Byte** p,
+ FT_Byte* limit,
+ TT_VarcComponent component,
+ FT_Fixed* axis_values_buffer,
+ FT_UInt axis_values_buffer_size )
+ {
+ FT_Error error;
+ FT_Memory memory = face->root.memory;
+
+ FT_UInt32 flags32;
+
+
+ FT_ZERO( component );
+
+ /* read flags (uint32var) */
+ error = read_uint32var( p, limit, &flags32 );
+ if ( error )
+ return error;
+
+ component->flags = flags32;
+ /* read GID */
+ if ( component->flags & VARC_GID_IS_24BIT )
+ {
+ if ( *p + 2 >= limit )
+ return FT_THROW( Invalid_Table );
+ component->gid = ( (*p)[0] << 16 ) |
+ ( (*p)[1] << 8 ) |
+ (*p)[2];
+ *p += 3;
+ }
+ else
+ {
+ if ( *p + 1 >= limit )
+ return FT_THROW( Invalid_Table );
+ component->gid = ( (*p)[0] << 8 ) |
+ (*p)[1];
+ *p += 2;
+ }
+
+ /* validate GID */
+ if ( component->gid >= (FT_UInt)face->root.num_glyphs )
+ {
+ /* For now, don't fail - the font might be using extended GID space. */
+ /* return FT_THROW( Invalid_Glyph_Index ); */
+ }
+
+ /* read condition index */
+ if ( component->flags & VARC_HAVE_CONDITION )
+ {
+ error = read_uint32var( p, limit, &component->condition_index );
+ if ( error )
+ return error;
+ }
+
+ /* read axis indices index */
+ if ( component->flags & VARC_HAVE_AXES )
+ {
+ error = read_uint32var( p, limit, &component->axis_indices_index );
+ if ( error )
+ return error;
+ }
+
+ /* read axis values (TupleValues) */
+ if ( component->flags & VARC_HAVE_AXES )
+ {
+ FT_UInt axis_count = 0;
+ FT_Byte* table_limit = (FT_Byte*)varc->table + varc->table_size;
+ FT_Byte* tuple_data;
+ FT_UInt tuple_size;
+
+
+ if ( !varc->axis_indices_list )
+ return FT_THROW( Invalid_Table );
+
+ error = tt_varc_read_cff2_index_entry( varc->axis_indices_list,
+ table_limit,
+ component->axis_indices_index,
+ &tuple_data, &tuple_size );
+ if ( error )
+ return error;
+
+ /* decode the TupleValues just to count the axis values */
+ {
+ FT_Byte* tuple_limit = tuple_data + tuple_size;
+ FT_Byte* tp = tuple_data;
+
+
+ while ( tp < tuple_limit )
+ {
+ FT_Byte control = *tp++;
+ FT_UInt cnt = ( control & 0x3F ) + 1;
+
+
+ if ( ( control & 0xC0 ) == 0xC0 )
+ {
+ /* 32-bit values */
+ if ( tp + 4 * cnt > tuple_limit )
+ break;
+ tp += 4 * cnt;
+ }
+ else if ( control & 0x80 )
+ ; /* zeros - no data bytes */
+ else if ( control & 0x40 )
+ {
+ /* 2-byte values */
+ if ( tp + 2 * cnt > tuple_limit )
+ break;
+ tp += 2 * cnt;
+ }
+ else
+ {
+ /* 1-byte values */
+ if ( tp + cnt > tuple_limit )
+ break;
+ tp += cnt;
+ }
+
+ axis_count += cnt;
+ }
+ }
+
+ component->num_axis_values = axis_count;
+
+ if ( axis_count > 0 )
+ {
+ FT_UInt i;
+ FT_UInt j;
+
+
+ /* use caller's stack buffer for small arrays, heap for large */
+ if ( axis_count <= axis_values_buffer_size )
+ {
+ component->axis_values = axis_values_buffer;
+ component->axis_values_on_heap = FALSE;
+ }
+ else
+ {
+ if ( FT_NEW_ARRAY( component->axis_values, axis_count ) )
+ return error;
+ component->axis_values_on_heap = TRUE;
+ }
+
+ /* read axis values from TupleValues (packed deltas format) */
+ i = 0;
+ while ( i < axis_count )
+ {
+ FT_Byte control;
+ FT_UInt cnt;
+
+
+ /* read control byte */
+ if ( *p >= limit )
+ goto Fail;
+ control = *(*p)++;
+
+ /* extract run count (lower 6 bits), add 1 */
+ cnt = ( control & 0x3F ) + 1;
+ if ( cnt > axis_count - i )
+ cnt = axis_count - i;
+
+ /* check top 2 bits for encoding type */
+ if ( ( control & 0xC0 ) == 0xC0 )
+ {
+ /* Both bits set = 32-bit signed integers */
+ if ( *p + 4 * cnt > limit )
+ goto Fail;
+ for ( j = 0; j < cnt; j++ )
+ {
+ /* read as signed 32-bit integer, */
+ /* reinterpret as F2DOT14 -> F16DOT16 */
+ FT_Int32 val = (FT_Int32)( ( (FT_UInt32)(*p)[0] << 24 ) |
+ ( (FT_UInt32)(*p)[1] << 16 ) |
+ ( (FT_UInt32)(*p)[2] << 8 ) |
+ (FT_UInt32)(*p)[3] );
+
+
+ component->axis_values[i++] = (FT_Fixed)MUL_INT32( val, 4 );
+ *p += 4;
+ }
+ }
+ else if ( control & 0x80 )
+ {
+ /* bit 7 set = zeros */
+ for ( j = 0; j < cnt; j++ )
+ component->axis_values[i++] = 0;
+ }
+ else if ( control & 0x40 )
+ {
+ /* bit 6 set = 2-byte signed integers */
+ if ( *p + 2 * cnt > limit )
+ goto Fail;
+ for ( j = 0; j < cnt; j++ )
+ {
+ /* read as signed 16-bit integer, */
+ /* reinterpret as F2DOT14 -> F16DOT16 */
+ FT_Short val = (FT_Short)( ( (FT_UShort)(*p)[0] << 8 ) |
+ (FT_UShort)(*p)[1] );
+
+
+ component->axis_values[i++] = (FT_Fixed)( val * 4 );
+ *p += 2;
+ }
+ }
+ else
+ {
+ /* neither bit set = 1-byte signed integers */
+ if ( *p + cnt > limit )
+ goto Fail;
+ for ( j = 0; j < cnt; j++ )
+ {
+ /* read signed byte integer, */
+ /* reinterpret as F2DOT14 -> F16DOT16 */
+ FT_Char val = (FT_Char)(*(*p)++);
+
+
+ component->axis_values[i++] = (FT_Fixed)( val * 4 );
+ }
+ }
+ }
+ }
+ }
+
+ /* read axis values variation index */
+ if ( component->flags & VARC_AXES_HAVE_VARIATION )
+ {
+ error = read_uint32var( p, limit, &component->axis_values_var_index );
+ if ( error )
+ goto Fail;
+ }
+
+ /* read transform variation index */
+ if ( component->flags & VARC_TRANSFORM_HAS_VARIATION )
+ {
+ error = read_uint32var( p, limit, &component->transform_var_index );
+ if ( error )
+ goto Fail;
+ }
+
+ /* initialize transform to identity */
+ component->translate_x = 0;
+ component->translate_y = 0;
+ component->rotation = 0;
+ component->scale_x = 0x10000; /* 1.0 in 16.16 */
+ component->scale_y = 0x10000; /* 1.0 in 16.16 */
+ component->skew_x = 0;
+ component->skew_y = 0;
+ component->tcenter_x = 0;
+ component->tcenter_y = 0;
+
+ /* read transform components - each field is optional based on flags */
+ if ( component->flags & VARC_HAVE_TRANSLATE_X )
+ {
+ error = read_fword( p, limit, &component->translate_x );
+ if ( error )
+ goto Fail;
+ }
+
+ if ( component->flags & VARC_HAVE_TRANSLATE_Y )
+ {
+ error = read_fword( p, limit, &component->translate_y );
+ if ( error )
+ goto Fail;
+ }
+
+ if ( component->flags & VARC_HAVE_ROTATION )
+ {
+ error = read_f4dot12( p, limit, &component->rotation );
+ if ( error )
+ goto Fail;
+ }
+
+ if ( component->flags & VARC_HAVE_SCALE_X )
+ {
+ error = read_f6dot10( p, limit, &component->scale_x );
+ if ( error )
+ goto Fail;
+ }
+
+ if ( component->flags & VARC_HAVE_SCALE_Y )
+ {
+ error = read_f6dot10( p, limit, &component->scale_y );
+ if ( error )
+ goto Fail;
+ }
+ else if ( component->flags & VARC_HAVE_SCALE_X )
+ {
+ /* ScaleY defaults to ScaleX if ScaleX is present */
+ component->scale_y = component->scale_x;
+ }
+
+ if ( component->flags & VARC_HAVE_SKEW_X )
+ {
+ error = read_f4dot12( p, limit, &component->skew_x );
+ if ( error )
+ goto Fail;
+ }
+
+ if ( component->flags & VARC_HAVE_SKEW_Y )
+ {
+ error = read_f4dot12( p, limit, &component->skew_y );
+ if ( error )
+ goto Fail;
+ }
+
+ if ( component->flags & VARC_HAVE_TCENTER_X )
+ {
+ error = read_fword( p, limit, &component->tcenter_x );
+ if ( error )
+ goto Fail;
+ }
+
+ if ( component->flags & VARC_HAVE_TCENTER_Y )
+ {
+ error = read_fword( p, limit, &component->tcenter_y );
+ if ( error )
+ goto Fail;
+ }
+
+ /* Skip one uint32var for each set reserved flag bit (bits 15-31), */
+ /* keeping the byte stream aligned for forward-compatible */
+ /* extensions, as HarfBuzz, skrifa, and fontTools do. */
+ {
+ FT_UInt32 reserved = component->flags & VARC_RESERVED_MASK;
+
+
+ while ( reserved )
+ {
+ FT_UInt32 ignore;
+
+
+ error = read_uint32var( p, limit, &ignore );
+ if ( error )
+ goto Fail;
+
+ reserved &= reserved - 1;
+ }
+ }
+
+ return FT_Err_Ok;
+
+ Fail:
+ /* the axis-value reading loop jumps here on truncated data */
+ /* without setting `error`; never report success on failure */
+ if ( !error )
+ error = FT_THROW( Invalid_Table );
+ if ( component->axis_values && component->axis_values_on_heap )
+ FT_FREE( component->axis_values );
+ return error;
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_free_component
+ *
+ * @Description:
+ * Free resources allocated for a component.
+ *
+ * @Input:
+ * face ::
+ * The font face.
+ *
+ * component ::
+ * The component to free.
+ */
+ static void
+ tt_varc_free_component( TT_Face face,
+ TT_VarcComponent component )
+ {
+ FT_Memory memory = face->root.memory;
+
+
+ if ( component->axis_values && component->axis_values_on_heap )
+ FT_FREE( component->axis_values );
+ }
+
+
+ /**************************************************************************
+ *
+ * MultiItemVariationStore Functions
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_evaluate_region
+ *
+ * @Description:
+ * Evaluate a variation region against current coordinates.
+ * Return a scalar value (0.0 to 1.0) indicating how much this
+ * region contributes at the current variation location.
+ *
+ * @Input:
+ * region_data ::
+ * Pointer to region data (axis ranges).
+ *
+ * limit ::
+ * End of valid data.
+ *
+ * coords ::
+ * Current normalized coordinates (F2DOT14).
+ *
+ * coord_count ::
+ * Number of coordinates.
+ *
+ * @Return:
+ * Scalar value as FT_Fixed (16.16 format), 0 to 0x10000 (1.0).
+ */
+ static FT_Fixed
+ tt_varc_evaluate_region( FT_Byte* region_data,
+ FT_Byte* limit,
+ FT_Fixed* coords,
+ FT_UInt coord_count )
+ {
+ FT_Byte* p = region_data;
+ FT_Fixed scalar = 0x10000; /* start with 1.0 in 16.16 */
+ FT_UInt axis_count;
+
+ FT_UInt i;
+
+
+ if ( !coords || coord_count == 0 )
+ return scalar; /* no coords available, return 1.0 */
+
+ /* `SparseVariationRegion` table:
+ *
+ * regionAxisCount (u16) - number of non-default axes
+ * axisOffsets (u32[regionAxisCount]) - relative to this region
+ * for each sparse axis:
+ * axisIndex (u16)
+ * startCoord (F2DOT14)
+ * peakCoord (F2DOT14)
+ * endCoord (F2DOT14)
+ */
+
+ if ( limit - p < 2 )
+ return 0;
+
+ /* read sparse axis count (u16) */
+ axis_count = ( (FT_UInt)p[0] << 8 ) | p[1];
+ p += 2;
+
+ if ( axis_count > (FT_ULong)( limit - p ) / 4 )
+ return 0;
+
+ /* process each axis range */
+ for ( i = 0; i < axis_count; i++ )
+ {
+ FT_UInt16 axis_index;
+ FT_Short min_val, peak_val, max_val;
+ FT_Fixed coord;
+ FT_Fixed axis_scalar;
+ FT_UInt32 axis_offset;
+ FT_Byte* axis;
+
+ FT_Fixed min_16;
+ FT_Fixed peak_16;
+ FT_Fixed max_16;
+
+
+ axis_offset = ( (FT_UInt32)p[0] << 24 ) |
+ ( (FT_UInt32)p[1] << 16 ) |
+ ( (FT_UInt32)p[2] << 8 ) |
+ (FT_UInt32)p[3];
+ p += 4;
+
+ if ( axis_offset > (FT_ULong)( limit - region_data ) ||
+ (FT_ULong)( limit - region_data ) - axis_offset < 8 )
+ return 0;
+
+ axis = region_data + axis_offset;
+ axis_index = ( (FT_UInt16)axis[0] << 8 ) | axis[1];
+ axis += 2;
+
+ /* read min, peak, max (F2DOT14) - each is 2 bytes, */
+ /* advance after each read */
+ min_val = (FT_Short)( ( (FT_UShort)axis[0] << 8 ) |
+ (FT_UShort)axis[1] );
+ axis += 2;
+ peak_val = (FT_Short)( ( (FT_UShort)axis[0] << 8 ) |
+ (FT_UShort)axis[1] );
+ axis += 2;
+ max_val = (FT_Short)( ( (FT_UShort)axis[0] << 8 ) |
+ (FT_UShort)axis[1] );
+
+ /* get coordinate for this axis */
+ if ( axis_index >= coord_count )
+ continue; /* skip unknown axes */
+
+ coord = coords[axis_index];
+
+ /* convert F2DOT14 to F16DOT16 for comparison */
+ min_16 = (FT_Fixed)min_val * 4;
+ peak_16 = (FT_Fixed)peak_val * 4;
+ max_16 = (FT_Fixed)max_val * 4;
+
+ /* Evaluate this axis with the sparse-region tent function, */
+ /* matching HarfBuzz (`VarRegionAxis::evaluate`) and skrifa */
+ /* (`SparseVariationRegion::compute_scalar`). */
+ if ( peak_16 == 0 )
+ continue; /* axis not part of region -> factor 1 */
+
+ if ( coord == peak_16 )
+ continue; /* at peak -> factor 1 */
+
+ if ( coord == 0 )
+ return 0; /* default location -> region gives 0 */
+
+ if ( min_16 > peak_16 ||
+ peak_16 > max_16 ||
+ ( min_16 < 0 && max_16 > 0 ) )
+ continue; /* ill-formed axis -> factor 1 */
+
+ if ( coord < min_16 || coord > max_16 )
+ return 0; /* outside range -> region gives 0 */
+
+ if ( coord < peak_16 )
+ axis_scalar = FT_DivFix( coord - min_16, peak_16 - min_16 );
+ else
+ axis_scalar = FT_DivFix( max_16 - coord, max_16 - peak_16 );
+
+ /* multiply into overall scalar */
+ scalar = FT_MulFix( scalar, axis_scalar );
+
+ /* early exit if scalar becomes 0 */
+ if ( scalar == 0 )
+ return 0;
+ }
+
+ return scalar;
+ }
+
+
+ /**************************************************************************
+ *
+ * MultiItemVariationStore Functions
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_read_tuple_deltas
+ *
+ * @Description:
+ * Read a tuple of delta values from TupleValues (packed deltas).
+ * Return plain integers that will be scaled later.
+ *
+ * @Input:
+ * data ::
+ * Pointer to TupleValues data.
+ *
+ * limit ::
+ * End of valid data.
+ *
+ * num_deltas ::
+ * Number of deltas to read.
+ *
+ * @Output:
+ * deltas ::
+ * Array to store deltas (as plain integers).
+ *
+ * bytes_consumed ::
+ * The number of bytes consumed for this tuple.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ tt_varc_read_tuple_deltas( FT_Byte* data,
+ FT_Byte* limit,
+ FT_UInt num_deltas,
+ FT_Int32* deltas,
+ FT_UInt* bytes_consumed )
+ {
+ FT_Byte* p = data;
+ FT_Byte* start = data;
+
+ FT_UInt i = 0;
+
+
+ /* read deltas using TupleValues (packed delta) format */
+ while ( i < num_deltas && p < limit )
+ {
+ FT_Byte control;
+ FT_UInt cnt, j;
+
+
+ /* read control byte */
+ if ( p >= limit )
+ return FT_THROW( Invalid_Table );
+
+ control = *p++;
+
+ /* extract run count (lower 6 bits), add 1 */
+ cnt = ( control & 0x3F ) + 1;
+ if ( cnt > num_deltas - i )
+ cnt = num_deltas - i;
+
+ /* check top 2 bits for encoding type */
+ if ( ( control & 0xC0 ) == 0xC0 )
+ {
+ /* both bits set = 32-bit plain integer values */
+ if ( p + 4 * cnt > limit )
+ return FT_THROW( Invalid_Table );
+
+ for ( j = 0; j < cnt; j++ )
+ {
+ /* read as signed 32-bit plain integer */
+ FT_Int32 val = (FT_Int32)( ( (FT_UInt32)p[0] << 24 ) |
+ ( (FT_UInt32)p[1] << 16 ) |
+ ( (FT_UInt32)p[2] << 8 ) |
+ (FT_UInt32)p[3] );
+
+
+ deltas[i++] = val; /* store as plain integer */
+ p += 4;
+ }
+ }
+ else if ( control & 0x80 )
+ {
+ /* bit 7 set = zeros */
+ for ( j = 0; j < cnt; j++ )
+ deltas[i++] = 0;
+ }
+ else if ( control & 0x40 )
+ {
+ /* bit 6 set = 2-byte signed integer values */
+ if ( p + 2 * cnt > limit )
+ return FT_THROW( Invalid_Table );
+
+ for ( j = 0; j < cnt; j++ )
+ {
+ /* read as signed 16-bit plain integer */
+ FT_Short val = (FT_Short)( ( (FT_UShort)p[0] << 8 ) |
+ (FT_UShort)p[1] );
+
+
+ deltas[i++] = val; /* store as plain integer */
+ p += 2;
+ }
+ }
+ else
+ {
+ /* neither bit set = 1-byte signed integer values */
+ if ( p + cnt > limit )
+ return FT_THROW( Invalid_Table );
+
+ for ( j = 0; j < cnt; j++ )
+ {
+ /* read signed byte as plain integer */
+ FT_Char val = (FT_Char)(*p++);
+
+
+ deltas[i++] = val; /* store as plain integer */
+ }
+ }
+ }
+
+ /* if we hit the end before reading all deltas, */
+ /* fill remaining with zeros */
+ while ( i < num_deltas )
+ deltas[i++] = 0;
+
+ /* return the number of bytes consumed */
+ *bytes_consumed = (FT_UInt)( p - start );
+
+ return FT_Err_Ok;
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_get_item_deltas
+ *
+ * @Description:
+ * Get a tuple of deltas from the `MultiItemVariationStore`.
+ * Unlike `ItemVariationStore`, which returns a single delta,
+ * `MultiItemVariationStore` returns multiple deltas (a tuple).
+ *
+ * @Input:
+ * face ::
+ * The font face.
+ *
+ * varc ::
+ * The VARC table.
+ *
+ * var_index ::
+ * Variation index (outer << 16 | inner).
+ *
+ * num_deltas ::
+ * Number of deltas expected in the tuple.
+ *
+ * shift ::
+ * Number of bits to preserve from fractional interpolation.
+ * The raw integer deltas are effectively shifted left by this
+ * amount. Use shift=2 for F2DOT14 data (returns F16DOT16),
+ * shift=4 for F4DOT12 data (returns F16DOT16), etc.
+ * Maximum value is 16.
+ *
+ * current_coords ::
+ * Current normalized coordinates (F2DOT14).
+ *
+ * num_coords ::
+ * Number of coords in `current_coords`.
+ *
+ * condition_defaults ::
+ * Optional defaults for condition values. If non-NULL, return each
+ * unrounded value's positive sign (0 or 1), instead of scaled deltas.
+ *
+ * @Output:
+ * deltas ::
+ * Array to store the deltas (must have space for `num_deltas`).
+ * Values are raw integer deltas scaled left by `shift` bits.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ tt_varc_get_item_deltas( TT_Face face,
+ TT_Varc varc,
+ FT_UInt32 var_index,
+ FT_UInt num_deltas,
+ FT_Long* deltas,
+ FT_UInt shift,
+ FT_Fixed* current_coords,
+ FT_UInt num_coords,
+ FT_Short* condition_defaults )
+ {
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+
+ FT_Error error;
+ FT_Memory memory = face->root.memory;
+
+ FT_Byte* mvs_data; /* MultiItemVariationStore */
+ FT_Byte* table_limit;
+ FT_UInt16 format;
+ FT_UInt32 regions_offset;
+ FT_UInt16 data_sets_count;
+ FT_UInt outer, inner;
+ FT_Byte* p;
+ FT_Byte* data_set_offsets;
+ FT_UInt32 data_set_offset;
+
+ FT_Byte* mvd_data; /* MultiVarData */
+ FT_Byte mvd_format;
+ FT_UInt16 region_count;
+ FT_Byte* delta_sets;
+ FT_UInt32 delta_sets_offset;
+ FT_Byte* tuple_data;
+ FT_UInt tuple_size;
+
+ FT_Int64 stack_accumulators[VARC_STACK_DELTA_COUNT];
+ /* deltas * regions */
+ FT_Int32 stack_all_deltas[VARC_STACK_DELTA_COUNT * 16];
+ FT_Int64* accumulators = NULL;
+ FT_Byte* regions_data = NULL;
+ FT_Byte* regions_limit = NULL;
+ FT_UInt32 region_list_count = 0;
+
+ FT_UInt total_deltas;
+ FT_Int32* all_deltas = NULL;
+ FT_UInt bytes_consumed = 0;
+ FT_Byte* region_indices_ptr;
+ FT_UInt region_idx;
+
+ FT_UInt i;
+
+
+ if ( !varc->multi_var_store_loaded || !varc->multi_var_store )
+ {
+ /* no variation store - return zeros */
+ for ( i = 0; i < num_deltas; i++ )
+ deltas[i] = condition_defaults ? FT_BOOL( condition_defaults[i] > 0 )
+ : 0;
+ return FT_Err_Ok;
+ }
+
+ mvs_data = varc->multi_var_store;
+ table_limit = (FT_Byte*)varc->table + varc->table_size;
+
+ /* split `var_index` into `outer` and `inner` */
+ outer = var_index >> 16;
+ inner = var_index & 0xFFFF;
+
+ /* parse `MultiItemVariationStore` header */
+ p = mvs_data;
+
+ if ( p + 8 > table_limit )
+ return FT_THROW( Invalid_Table );
+
+ /* read format (u16) - must be 1 */
+ format = ( (FT_UInt16)p[0] << 8 ) | p[1];
+ p += 2;
+
+ if ( format != 1 )
+ return FT_THROW( Invalid_Table );
+
+ /* read regions offset (u32) */
+ regions_offset = ( (FT_UInt32)p[0] << 24 ) |
+ ( (FT_UInt32)p[1] << 16 ) |
+ ( (FT_UInt32)p[2] << 8 ) |
+ (FT_UInt32)p[3];
+ p += 4;
+
+ /* read dataSets count (u16) */
+ data_sets_count = ( (FT_UInt16)p[0] << 8 ) | p[1];
+ p += 2;
+
+ /* validate outer index */
+ if ( outer >= data_sets_count )
+ return FT_THROW( Invalid_Table );
+
+ /* read data set offset for outer index */
+ data_set_offsets = p;
+ p = data_set_offsets + outer * 4;
+
+ if ( p + 4 > table_limit )
+ return FT_THROW( Invalid_Table );
+
+ data_set_offset = ( (FT_UInt32)p[0] << 24 ) |
+ ( (FT_UInt32)p[1] << 16 ) |
+ ( (FT_UInt32)p[2] << 8 ) |
+ (FT_UInt32)p[3];
+
+ /* get `MultiVarData` */
+ if ( data_set_offset > (FT_UInt32)( table_limit - mvs_data ) )
+ return FT_THROW( Invalid_Table );
+
+ mvd_data = mvs_data + data_set_offset;
+
+ if ( table_limit - mvd_data < 7 )
+ return FT_THROW( Invalid_Table );
+
+ /* parse `MultiVarData` header */
+ p = mvd_data;
+
+ /* read format (u8) - must be 1 */
+ mvd_format = *p++;
+
+ if ( mvd_format != 1 )
+ return FT_THROW( Invalid_Table );
+
+ /* read `regionIndices` count (u16) */
+ region_count = ( (FT_UInt16)p[0] << 8 ) | p[1];
+ p += 2;
+
+ if ( region_count > (FT_ULong)( table_limit - p ) / 2 )
+ return FT_THROW( Invalid_Table );
+ p += region_count * 2;
+
+ if ( table_limit - p < 4 )
+ return FT_THROW( Invalid_Table );
+
+ delta_sets_offset = ( (FT_UInt32)p[0] << 24 ) |
+ ( (FT_UInt32)p[1] << 16 ) |
+ ( (FT_UInt32)p[2] << 8 ) |
+ (FT_UInt32)p[3];
+ if ( !delta_sets_offset ||
+ delta_sets_offset > (FT_ULong)( table_limit - mvd_data ) ||
+ (FT_ULong)( table_limit - mvd_data ) - delta_sets_offset < 4 )
+ return FT_THROW( Invalid_Table );
+ delta_sets = mvd_data + delta_sets_offset;
+
+ /* read tuple from INDEX2 at inner index */
+ error = tt_varc_read_cff2_index_entry( delta_sets, table_limit, inner,
+ &tuple_data, &tuple_size );
+ if ( error )
+ return error;
+
+ /* allocate 64-bit accumulators for each delta */
+ if ( num_deltas <= VARC_STACK_DELTA_COUNT )
+ accumulators = stack_accumulators;
+ else
+ {
+ if ( FT_NEW_ARRAY( accumulators, num_deltas ) )
+ {
+ error = FT_THROW( Out_Of_Memory );
+ goto Cleanup;
+ }
+ }
+
+ /* initialize accumulators to zero */
+ FT_ARRAY_ZERO( accumulators, num_deltas );
+ if ( condition_defaults )
+ {
+ for ( i = 0; i < num_deltas; i++ )
+ {
+#ifdef FT_INT64
+ accumulators[i] = (FT_Int64)condition_defaults[i] * 0x10000L;
+#else
+ accumulators[i].lo = (FT_UInt32)(FT_Int32)condition_defaults[i] << 16;
+ accumulators[i].hi = condition_defaults[i] < 0 ? 0xFFFFFFFFU : 0;
+#endif
+ }
+ }
+
+ /* parse `SparseVarRegionList` to get region definitions */
+ if ( regions_offset > 0 &&
+ regions_offset <= (FT_ULong)( table_limit - mvs_data ) )
+ {
+ regions_data = mvs_data + regions_offset;
+ regions_limit = table_limit;
+
+ /* read region count (u32) */
+ if ( regions_limit - regions_data >= 4 )
+ {
+ region_list_count = ( (FT_UInt32)regions_data[0] << 24 ) |
+ ( (FT_UInt32)regions_data[1] << 16 ) |
+ ( (FT_UInt32)regions_data[2] << 8 ) |
+ (FT_UInt32)regions_data[3];
+ regions_data += 4;
+
+ if ( region_list_count >
+ (FT_ULong)( regions_limit - regions_data ) / 4 )
+ regions_data = NULL;
+ }
+ else
+ regions_data = NULL;
+ }
+
+ if ( !regions_data )
+ {
+ if ( accumulators != stack_accumulators )
+ FT_FREE( accumulators );
+ error = FT_THROW( Invalid_Table );
+ goto Cleanup;
+ }
+
+ /* read ALL deltas from the flat tuple */
+ /* (region_count * num_deltas values) */
+
+ /* a wrapped product would under-size `all_deltas` below */
+ if ( num_deltas != 0 && region_count > FT_UINT_MAX / num_deltas )
+ {
+ if ( accumulators != stack_accumulators )
+ FT_FREE( accumulators );
+ error = FT_THROW( Invalid_Table );
+ goto Cleanup;
+ }
+
+ total_deltas = region_count * num_deltas;
+
+ /* allocate space for all deltas */
+ if ( total_deltas <= VARC_STACK_DELTA_COUNT * 16 )
+ all_deltas = stack_all_deltas;
+ else
+ {
+ if ( FT_NEW_ARRAY( all_deltas, total_deltas ) )
+ {
+ if ( accumulators != stack_accumulators )
+ FT_FREE( accumulators );
+ error = FT_THROW( Out_Of_Memory );
+ goto Cleanup;
+ }
+ }
+
+ /* read all deltas from TupleValues */
+ error = tt_varc_read_tuple_deltas( tuple_data, tuple_data + tuple_size,
+ total_deltas, all_deltas,
+ &bytes_consumed );
+ if ( error )
+ {
+ if ( all_deltas != stack_all_deltas )
+ FT_FREE( all_deltas );
+ if ( accumulators != stack_accumulators )
+ FT_FREE( accumulators );
+ goto Cleanup;
+ }
+
+ /* parse regionIndices and accumulate scaled deltas */
+
+ /* after format(u8) + region_count(u16) */
+ region_indices_ptr = mvd_data + 3;
+
+ for ( region_idx = 0; region_idx < region_count; region_idx++ )
+ {
+ FT_UInt16 region_index;
+ FT_Fixed region_scalar;
+
+ FT_UInt k;
+
+
+ /* read region index from `regionIndices` array (u16) */
+ if ( region_indices_ptr + 2 > table_limit )
+ break;
+
+ region_index = ( (FT_UInt16)region_indices_ptr[0] << 8 ) |
+ region_indices_ptr[1];
+ region_indices_ptr += 2;
+
+ /* evaluate region to get scalar */
+ if ( regions_data && current_coords )
+ {
+ /* SparseVarRegionList format:
+ *
+ * u32: regionCount
+ * u32[regionCount]: array of offsets (from start of list)
+ * to each region
+ */
+
+ /* back to start (before count) */
+ FT_Byte* region_list_start = regions_data - 4;
+ FT_Byte* region_offset_ptr;
+
+
+ if ( region_index < region_list_count &&
+ region_index < (FT_ULong)( regions_limit - regions_data ) / 4 )
+ {
+ FT_UInt32 region_offset;
+
+
+ region_offset_ptr = regions_data + region_index * 4;
+ region_offset = ( (FT_UInt32)region_offset_ptr[0] << 24 ) |
+ ( (FT_UInt32)region_offset_ptr[1] << 16 ) |
+ ( (FT_UInt32)region_offset_ptr[2] << 8 ) |
+ (FT_UInt32)region_offset_ptr[3];
+
+
+ if ( region_offset <
+ (FT_UInt32)( regions_limit - region_list_start ) )
+ region_scalar =
+ tt_varc_evaluate_region( region_list_start + region_offset,
+ regions_limit,
+ current_coords,
+ num_coords );
+ else
+ region_scalar = 0;
+ }
+ else
+ region_scalar = 0;
+ }
+ else
+ {
+ /* no coords or regions - use 1.0 for first region, 0 for others */
+ region_scalar = ( region_idx == 0 ) ? 0x10000 : 0;
+ }
+
+ /* scale and accumulate deltas for this region; */
+ /* tuple is organized as regions-major: */
+ /* */
+ /* [d0_r0, d1_r0, ..., dN_r0, d0_r1, ...] */
+ if ( region_scalar != 0 )
+ {
+ FT_UInt base_offset = region_idx * num_deltas;
+
+
+ for ( k = 0; k < num_deltas; k++ )
+ {
+ /* plain integer */
+ FT_Int32 region_delta = all_deltas[base_offset + k];
+
+ /* accumulate using 64-bit math: accum += delta * scalar */
+#ifdef FT_INT64
+ FT_Int64 product = (FT_Int64)region_delta * region_scalar;
+
+
+ accumulators[k] += product;
+#else /* !FT_INT64 */
+ /* 32-bit fallback */
+ if ( (FT_UInt32)region_delta + 0x8000U <= 0xFFFFU )
+ {
+ /* fast path - multiplication fits in 32 bits */
+ FT_Int32 product = region_delta * region_scalar;
+
+
+ accumulators[k].lo += (FT_UInt32)product;
+ if ( accumulators[k].lo < (FT_UInt32)product )
+ accumulators[k].hi += 1;
+ if ( product < 0 )
+ accumulators[k].hi -= 1;
+ }
+ else
+ {
+ /* slow path - full 64-bit signed multiplication */
+ FT_UInt32 a = (FT_UInt32)ULABS( region_delta );
+ FT_UInt32 b = (FT_UInt32)ULABS( region_scalar );
+
+ FT_UInt32 a_lo = a & 0xFFFF;
+ FT_UInt32 a_hi = a >> 16;
+ FT_UInt32 b_lo = b & 0xFFFF;
+ FT_UInt32 b_hi = b >> 16;
+
+ FT_UInt32 mid = ( a_lo * b_hi ) + ( a_hi * b_lo );
+ FT_UInt32 lo = a_lo * b_lo;
+ FT_UInt32 hi = a_hi * b_hi;
+
+
+ hi += ( mid >> 16 ) +
+ ( ( ( lo >> 16 ) + ( mid & 0xFFFF ) ) >> 16 );
+ lo += ( mid << 16 );
+
+ if ( ( region_delta < 0 ) != ( region_scalar < 0 ) )
+ {
+ lo = ~lo + 1;
+ hi = ~hi + !lo;
+ }
+
+ accumulators[k].lo += lo;
+ if ( accumulators[k].lo < lo )
+ accumulators[k].hi += 1;
+ accumulators[k].hi += hi;
+ }
+#endif /* !FT_INT64 */
+ }
+ }
+ }
+
+ /* free the flat delta array */
+ if ( all_deltas != stack_all_deltas )
+ FT_FREE( all_deltas );
+
+ /* Convert 64-bit accumulators to deltas with rounding. */
+ /* Accumulators hold: raw_int_delta * region_scalar_16.16 */
+ /* We shift right by (16 - shift) to preserve `shift` fractional */
+ /* bits from the interpolation. */
+ {
+ FT_Int right_shift = 16 - shift;
+ FT_Long rounding = right_shift > 0 ? ( 1L << ( right_shift - 1 ) )
+ : 0;
+
+
+ for ( i = 0; i < num_deltas; i++ )
+ {
+#ifdef FT_INT64
+ if ( condition_defaults )
+ deltas[i] = FT_BOOL( accumulators[i] > 0 );
+ else
+ deltas[i] =
+ (FT_Long)( ( accumulators[i] + rounding ) >> right_shift );
+#else
+ /* 32-bit fallback */
+ FT_UInt32 hi = accumulators[i].hi;
+ FT_UInt32 lo = accumulators[i].lo;
+
+
+ if ( condition_defaults )
+ {
+ deltas[i] = FT_BOOL( !( hi & 0x80000000U ) && ( hi || lo ) );
+ continue;
+ }
+
+ /* add rounding */
+ lo += (FT_UInt32)rounding;
+ if ( lo < (FT_UInt32)rounding )
+ hi += 1;
+
+ /* shift right by (16 - shift) bits */
+ if ( right_shift == 0 )
+ deltas[i] = (FT_Long)(FT_Int32)lo;
+ else
+ deltas[i] = (FT_Long)(FT_Int32)( ( hi << ( 16 + shift ) ) |
+ ( lo >> right_shift ) );
+#endif
+ }
+ }
+
+ /* free accumulators */
+ if ( accumulators != stack_accumulators )
+ FT_FREE( accumulators );
+
+ error = FT_Err_Ok;
+
+ Cleanup:
+ return error;
+
+#else /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+
+ FT_UNUSED( face );
+ FT_UNUSED( varc );
+ FT_UNUSED( var_index );
+ FT_UNUSED( num_deltas );
+ FT_UNUSED( deltas );
+ FT_UNUSED( shift );
+ FT_UNUSED( current_coords );
+ FT_UNUSED( num_coords );
+ FT_UNUSED( condition_defaults );
+
+ return FT_THROW( Unimplemented_Feature );
+
+#endif /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+ }
+
+
+ /**************************************************************************
+ *
+ * Condition Evaluation
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_get_condition
+ *
+ * @Description:
+ * Look up a 'Condition' by index in the top-level `ConditionList`.
+ * ConditionList:
+ * uint32 count, then Offset32[count] from its start.
+ *
+ * @Input:
+ * varc ::
+ * The 'VARC' table structure.
+ *
+ * cond_index ::
+ * The index into the `ConditionList` structure.
+ *
+ * table_limit ::
+ * End of valid table data.
+ *
+ * @Return:
+ * The condition offset.
+ */
+ static FT_Byte*
+ tt_varc_get_condition( TT_Varc varc,
+ FT_UInt32 cond_index,
+ FT_Byte* table_limit )
+ {
+ FT_Byte* cl = varc->condition_list;
+ FT_UInt32 count;
+ FT_Byte* p;
+ FT_UInt32 off;
+
+
+ if ( !cl || cl + 4 > table_limit )
+ return NULL;
+
+ count = ( (FT_UInt32)cl[0] << 24 ) |
+ ( (FT_UInt32)cl[1] << 16 ) |
+ ( (FT_UInt32)cl[2] << 8 ) |
+ (FT_UInt32)cl[3];
+ if ( cond_index >= count )
+ return NULL;
+
+ /* bound the index with division so `cond_index * 4` cannot overflow */
+ if ( cond_index > (FT_UInt32)( ( table_limit - cl - 4 ) / 4 ) )
+ return NULL;
+
+ p = cl + 4 + cond_index * 4;
+ if ( p + 4 > table_limit )
+ return NULL;
+
+ off = ( (FT_UInt32)p[0] << 24 ) |
+ ( (FT_UInt32)p[1] << 16 ) |
+ ( (FT_UInt32)p[2] << 8 ) |
+ (FT_UInt32)p[3];
+
+ if ( off >= (FT_UInt32)( table_limit - cl ) )
+ return NULL;
+
+ return cl + off;
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_eval_condition
+ *
+ * @Description:
+ * Evaluate a 'Condition', matching HarfBuzz/skrifa/fontTools.
+ * Sub-condition offsets in the And/Or/Negate formats are 24-bit and
+ * relative to the start of the containing condition.
+ *
+ * @Input:
+ * face ::
+ * The font face.
+ *
+ * varc ::
+ * The VARC table.
+ *
+ * cond ::
+ * Pointer to the Condition.
+ *
+ * table_limit ::
+ * End of valid table data.
+ *
+ * coords ::
+ * Current normalized coordinates (F2DOT14).
+ *
+ * coord_count ::
+ * Number of coordinates.
+ *
+ * depth ::
+ * Recursion depth.
+ *
+ * @Return:
+ * TRUE when the condition is satisfied, FALSE otherwise.
+ */
+ static FT_Bool
+ tt_varc_eval_condition( TT_Face face,
+ TT_Varc varc,
+ FT_Byte* cond,
+ FT_Byte* table_limit,
+ FT_Fixed* coords,
+ FT_UInt num_coords,
+ FT_UInt depth )
+ {
+ FT_UInt format;
+
+
+ /* A zero condition offset represents True. Top-level zero offsets */
+ /* resolve to the start of the ConditionList. */
+ if ( cond == varc->condition_list )
+ return TRUE;
+
+ if ( !cond ||
+ depth > TT_VARC_MAX_NESTING_LEVEL ||
+ cond + 2 > table_limit )
+ return FALSE;
+
+ format = ( (FT_UInt)cond[0] << 8 ) | cond[1];
+
+ switch ( format )
+ {
+ case 1: /* ConditionAxisRange */
+ {
+ FT_UInt axis_index;
+ FT_Short min_val, max_val;
+ FT_Fixed coord;
+
+
+ if ( cond + 8 > table_limit )
+ return FALSE;
+
+ axis_index = ( (FT_UInt)cond[2] << 8 ) | cond[3];
+
+ min_val = (FT_Short)( ( (FT_UShort)cond[4] << 8 ) |
+ (FT_UShort)cond[5] );
+ max_val = (FT_Short)( ( (FT_UShort)cond[6] << 8 ) |
+ (FT_UShort)cond[7] );
+
+ coord = ( coords && axis_index < num_coords ) ? coords[axis_index]
+ : 0;
+
+ /* min_val/max_val are F2DOT14; coords are 16.16 (F2DOT14 << 2) */
+ return FT_BOOL( coord >= ( (FT_Fixed)min_val * 4 ) &&
+ coord <= ( (FT_Fixed)max_val * 4 ) );
+ }
+
+ case 2: /* ConditionValue */
+ {
+ FT_Short default_value;
+ FT_UInt32 var_idx;
+ FT_Long positive;
+
+
+ if ( cond + 8 > table_limit )
+ return FALSE;
+
+ default_value = (FT_Short)( ( (FT_UShort)cond[2] << 8 ) |
+ (FT_UShort)cond[3] );
+
+ var_idx = ( (FT_UInt32)cond[4] << 24 ) |
+ ( (FT_UInt32)cond[5] << 16 ) |
+ ( (FT_UInt32)cond[6] << 8 ) |
+ (FT_UInt32)cond[7];
+
+ /* Compare the unrounded default plus interpolated delta. */
+ /* Even a small positive fraction makes a condition true. */
+ positive = FT_BOOL( default_value > 0 );
+ if ( var_idx != 0xFFFFFFFFUL )
+ (void)tt_varc_get_item_deltas( face, varc, var_idx, 1,
+ &positive, 0,
+ coords, num_coords,
+ &default_value );
+
+ return FT_BOOL( positive );
+ }
+
+ case 3: /* ConditionAnd */
+ case 4: /* ConditionOr */
+ {
+ FT_UInt count, i;
+ FT_Byte* p;
+ FT_Bool is_and = FT_BOOL( format == 3 );
+
+
+ if ( cond + 3 > table_limit )
+ return FALSE;
+
+ count = cond[2];
+ p = cond + 3;
+
+ if ( p + (FT_Offset)count * 3 > table_limit )
+ return FALSE;
+
+ for ( i = 0; i < count; i++ )
+ {
+ FT_UInt32 sub_off = ( (FT_UInt32)p[0] << 16 ) |
+ ( (FT_UInt32)p[1] << 8 ) |
+ (FT_UInt32)p[2];
+ FT_Bool r;
+
+
+ p += 3;
+ r = sub_off == 0 ? TRUE
+ : tt_varc_eval_condition( face, varc,
+ cond + sub_off,
+ table_limit,
+ coords, num_coords,
+ depth + 1 );
+
+ if ( is_and && !r )
+ return FALSE;
+ if ( !is_and && r )
+ return TRUE;
+ }
+
+ return is_and;
+ }
+
+ case 5: /* ConditionNegate */
+ {
+ FT_UInt32 sub_off;
+
+
+ if ( cond + 5 > table_limit )
+ return FALSE;
+
+ sub_off = ( (FT_UInt32)cond[2] << 16 ) |
+ ( (FT_UInt32)cond[3] << 8 ) |
+ (FT_UInt32)cond[4];
+
+ if ( sub_off == 0 )
+ return FALSE;
+
+ return FT_BOOL( !tt_varc_eval_condition( face, varc, cond + sub_off,
+ table_limit, coords,
+ num_coords, depth + 1 ) );
+ }
+
+ default:
+ /* Unknown format: treat as not satisfied (matches fontTools). */
+ return FALSE;
+ }
+ }
+
+
+ /**************************************************************************
+ *
+ * Axis Index Reading
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_get_axis_indices
+ *
+ * @Description:
+ * Read axis indices from the axis indices list (TupleList of integers).
+ *
+ * @Input:
+ * varc ::
+ * The VARC table.
+ *
+ * axis_indices_index ::
+ * Index into the TupleList.
+ *
+ * num_indices ::
+ * Number of indices to read.
+ *
+ * @Output:
+ * indices ::
+ * Array to store axis indices (caller allocates).
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ static FT_Error
+ tt_varc_get_axis_indices( TT_Varc varc,
+ FT_UInt axis_indices_index,
+ FT_UInt num_indices,
+ FT_UInt* indices )
+ {
+ FT_Error error;
+
+ FT_Byte* table_limit = (FT_Byte*)varc->table + varc->table_size;
+ FT_Byte* tuple_data;
+ FT_UInt tuple_size;
+ FT_Byte* tuple_limit;
+
+ FT_Byte* p;
+ FT_UInt i, j;
+
+
+ if ( !varc || !varc->axis_indices_list || !indices )
+ return FT_THROW( Invalid_Argument );
+
+ error = tt_varc_read_cff2_index_entry( varc->axis_indices_list,
+ table_limit,
+ axis_indices_index,
+ &tuple_data, &tuple_size );
+ if ( error )
+ return error;
+
+ tuple_limit = tuple_data + tuple_size;
+ p = tuple_data;
+
+ /* read indices using TupleValues encoding */
+ i = 0;
+ while ( i < num_indices && p < tuple_limit )
+ {
+ FT_Byte control;
+ FT_UInt cnt;
+
+
+ control = *p++;
+ cnt = ( control & 0x3F ) + 1;
+
+ if ( cnt > num_indices - i )
+ cnt = num_indices - i;
+
+ if ( ( control & 0xC0 ) == 0xC0 )
+ {
+ /* 32-bit values */
+ if ( p + 4 * cnt > tuple_limit )
+ return FT_THROW( Invalid_Table );
+
+ for ( j = 0; j < cnt && i < num_indices; j++, i++ )
+ {
+ indices[i] = ( (FT_UInt)p[0] << 24 ) |
+ ( (FT_UInt)p[1] << 16 ) |
+ ( (FT_UInt)p[2] << 8 ) |
+ (FT_UInt)p[3];
+ p += 4;
+ }
+ }
+ else if ( control & 0x80 )
+ {
+ /* zeros */
+ for ( j = 0; j < cnt && i < num_indices; j++, i++ )
+ indices[i] = 0;
+ }
+ else if ( control & 0x40 )
+ {
+ /* 2-byte values */
+ if ( p + 2 * cnt > tuple_limit )
+ return FT_THROW( Invalid_Table );
+
+ for ( j = 0; j < cnt && i < num_indices; j++, i++ )
+ {
+ indices[i] = ( (FT_UInt)p[0] << 8 ) |
+ (FT_UInt)p[1];
+ p += 2;
+ }
+ }
+ else
+ {
+ /* 1-byte values */
+ if ( p + cnt > tuple_limit )
+ return FT_THROW( Invalid_Table );
+
+ for ( j = 0; j < cnt && i < num_indices; j++, i++ )
+ indices[i] = *p++;
+ }
+ }
+
+ if ( i < num_indices )
+ return FT_THROW( Invalid_Table );
+
+ return FT_Err_Ok;
+ }
+
+
+ /**************************************************************************
+ *
+ * Variation Delta Application Functions
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_apply_axis_deltas
+ *
+ * @Description:
+ * Apply variation deltas to axis values using the
+ * `MultiItemVariationStore`.
+ *
+ * @Input:
+ * face ::
+ * The font face.
+ *
+ * varc ::
+ * The VARC table.
+ *
+ * component ::
+ * The component whose axis values to vary.
+ *
+ * current_coords ::
+ * Current normalized coordinates (F2DOT14).
+ *
+ * num_coords ::
+ * Number of coords in `current_coords`.
+ */
+ static void
+ tt_varc_apply_axis_deltas( TT_Face face,
+ TT_Varc varc,
+ TT_VarcComponent component,
+ FT_Fixed* current_coords,
+ FT_UInt num_coords )
+ {
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+
+ FT_Error error;
+ FT_Memory memory = face->root.memory;
+
+ FT_Long stack_deltas[VARC_STACK_DELTA_COUNT];
+ FT_Long* deltas = NULL;
+
+ FT_UInt i;
+
+
+ if ( !varc->multi_var_store_loaded || component->num_axis_values == 0 )
+ return;
+
+ /* use stack allocation for small arrays, heap for large */
+ if ( component->num_axis_values <= VARC_STACK_DELTA_COUNT )
+ deltas = stack_deltas;
+ else
+ {
+ if ( FT_NEW_ARRAY( deltas, component->num_axis_values ) )
+ return;
+ }
+
+ /* Get deltas with shift=2: raw F2DOT14 integers are returned */
+ /* already shifted to F16DOT16, matching `axis_values` format. */
+ error = tt_varc_get_item_deltas( face, varc,
+ component->axis_values_var_index,
+ component->num_axis_values,
+ deltas, 2,
+ current_coords,
+ num_coords, NULL );
+ if ( !error )
+ {
+ for ( i = 0; i < component->num_axis_values; i++ )
+ component->axis_values[i] += deltas[i];
+ }
+#ifdef FT_DEBUG_LEVEL_TRACE
+ else
+ FT_TRACE2(( "tt_varc_apply_axis_deltas: variation index"
+ " 0x%08lx failed (error 0x%x)\n",
+ (FT_ULong)component->axis_values_var_index, error ));
+#endif
+
+ if ( deltas != stack_deltas )
+ FT_FREE( deltas );
+
+#else /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+
+ FT_UNUSED( face );
+ FT_UNUSED( varc );
+ FT_UNUSED( component );
+ FT_UNUSED( current_coords );
+ FT_UNUSED( num_coords );
+
+#endif /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_apply_transform_deltas
+ *
+ * @Description:
+ * Apply variation deltas to transform values using the
+ * `MultiItemVariationStore`.
+ *
+ * @Input:
+ * face ::
+ * The font face.
+ *
+ * varc ::
+ * The VARC table.
+ *
+ * component ::
+ * The component whose transform values to vary.
+ *
+ * current_coords ::
+ * Current normalized coordinates (F2DOT14).
+ *
+ * num_coords ::
+ * Number of coords in `current_coords`.
+ */
+ static void
+ tt_varc_apply_transform_deltas( TT_Face face,
+ TT_Varc varc,
+ TT_VarcComponent component,
+ FT_Fixed* current_coords,
+ FT_UInt num_coords )
+ {
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+
+ FT_Error error;
+ FT_Memory memory = face->root.memory;
+
+ FT_Long stack_deltas[VARC_STACK_DELTA_COUNT];
+ FT_Long* deltas = NULL;
+ FT_UInt num_deltas = 0;
+ FT_UInt delta_index = 0;
+
+
+ if ( !varc->multi_var_store_loaded )
+ return;
+
+ /* count how many transform components are present */
+ if ( component->flags & VARC_HAVE_TRANSLATE_X ) num_deltas++;
+ if ( component->flags & VARC_HAVE_TRANSLATE_Y ) num_deltas++;
+ if ( component->flags & VARC_HAVE_ROTATION ) num_deltas++;
+ if ( component->flags & VARC_HAVE_SCALE_X ) num_deltas++;
+ if ( component->flags & VARC_HAVE_SCALE_Y ) num_deltas++;
+ if ( component->flags & VARC_HAVE_SKEW_X ) num_deltas++;
+ if ( component->flags & VARC_HAVE_SKEW_Y ) num_deltas++;
+ if ( component->flags & VARC_HAVE_TCENTER_X ) num_deltas++;
+ if ( component->flags & VARC_HAVE_TCENTER_Y ) num_deltas++;
+
+ if ( num_deltas == 0 )
+ return;
+
+ /* use stack allocation for small arrays, heap for large */
+ if ( num_deltas <= VARC_STACK_DELTA_COUNT )
+ deltas = stack_deltas;
+ else
+ {
+ if ( FT_NEW_ARRAY( deltas, num_deltas ) )
+ return;
+ }
+
+ /* Get deltas with shift=4: raw integers shifted left by 4. */
+ /* F4DOT12 fields (rotation, skew) become F16DOT16 directly. */
+ /* FWORD and F6DOT10 fields need an additional << 2 at callsite. */
+ error = tt_varc_get_item_deltas( face, varc,
+ component->transform_var_index,
+ num_deltas,
+ deltas, 4,
+ current_coords,
+ num_coords, NULL );
+ if ( !error )
+ {
+ /* Apply deltas to transform components in order. */
+ /* Deltas are already shifted left by 4 from get_item_deltas. */
+ /* F4DOT12 fields (shift=4): add directly (already F16DOT16). */
+ /* FWORD fields (shift=6): need << 2 more for 26.6 format. */
+ /* F6DOT10 fields (shift=6): need << 2 more for F16DOT16. */
+ delta_index = 0;
+
+ if ( component->flags & VARC_HAVE_TRANSLATE_X )
+ {
+ /* FWORD: 4+2=6 */
+ component->translate_x += deltas[delta_index] * 4;
+ delta_index++;
+ }
+
+ if ( component->flags & VARC_HAVE_TRANSLATE_Y )
+ {
+ /* FWORD: 4+2=6 */
+ component->translate_y += deltas[delta_index] * 4;
+ delta_index++;
+ }
+
+ if ( component->flags & VARC_HAVE_ROTATION )
+ {
+ /* F4DOT12: already 16.16 */
+ component->rotation += deltas[delta_index];
+ delta_index++;
+ }
+
+ if ( component->flags & VARC_HAVE_SCALE_X )
+ {
+ /* F6DOT10: 4+2=6 */
+ component->scale_x += deltas[delta_index] * 4;
+ delta_index++;
+ }
+
+ if ( component->flags & VARC_HAVE_SCALE_Y )
+ {
+ /* F6DOT10: 4+2=6 */
+ component->scale_y += deltas[delta_index] * 4;
+ delta_index++;
+ }
+
+ if ( component->flags & VARC_HAVE_SKEW_X )
+ {
+ /* F4DOT12: already 16.16 */
+ component->skew_x += deltas[delta_index];
+ delta_index++;
+ }
+
+ if ( component->flags & VARC_HAVE_SKEW_Y )
+ {
+ /* F4DOT12: already 16.16 */
+ component->skew_y += deltas[delta_index];
+ delta_index++;
+ }
+
+ if ( component->flags & VARC_HAVE_TCENTER_X )
+ {
+ /* FWORD: 4+2=6 */
+ component->tcenter_x += deltas[delta_index] * 4;
+ delta_index++;
+ }
+
+ if ( component->flags & VARC_HAVE_TCENTER_Y )
+ {
+ /* FWORD: 4+2=6 */
+ component->tcenter_y += deltas[delta_index] * 4;
+ delta_index++;
+ }
+
+ /* When ScaleY is absent it mirrors ScaleX. Re-apply the mirror */
+ /* after variation so a varying uniform scale stays uniform, */
+ /* matching fontTools and skrifa (which redo this post-delta). */
+ if ( ( component->flags & VARC_HAVE_SCALE_X ) &&
+ !( component->flags & VARC_HAVE_SCALE_Y ) )
+ component->scale_y = component->scale_x;
+ }
+#ifdef FT_DEBUG_LEVEL_TRACE
+ else
+ FT_TRACE2(( "tt_varc_apply_transform_deltas: variation index"
+ " 0x%08lx failed (error 0x%x)\n",
+ (FT_ULong)component->transform_var_index, error ));
+#endif
+
+ if ( deltas != stack_deltas )
+ FT_FREE( deltas );
+
+#else /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+
+ FT_UNUSED( face );
+ FT_UNUSED( varc );
+ FT_UNUSED( component );
+ FT_UNUSED( current_coords );
+ FT_UNUSED( num_coords );
+
+#endif /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+ }
+
+
+ /**************************************************************************
+ *
+ * Transform Matrix Functions
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_build_transform
+ *
+ * @Description:
+ * Build a transformation matrix from component transform values.
+ *
+ * Transformation order (per VARC spec):
+ *
+ * 1. translate to transformation center (-tCenterX, -tCenterY)
+ * 2. apply scale (scaleX, scaleY)
+ * 3. apply skew (skewX, skewY)
+ * 4. apply rotation (rotation * pi)
+ * 5. translate back from center (tCenterX, tCenterY)
+ * 6. apply final translation (translateX, translateY)
+ *
+ * @Input:
+ * component ::
+ * The component with transform values.
+ *
+ * @Output:
+ * matrix ::
+ * The resulting 2x2 transformation matrix.
+ *
+ * offset ::
+ * The translation vector.
+ */
+ static void
+ tt_varc_build_transform( TT_VarcComponent component,
+ FT_Matrix* matrix,
+ FT_Vector* offset )
+ {
+ FT_Fixed scale_x = component->scale_x;
+ FT_Fixed scale_y = component->scale_y;
+ FT_Fixed skew_x = component->skew_x;
+ FT_Fixed skew_y = component->skew_y;
+ FT_Fixed rotation = component->rotation;
+
+
+ /* Build rotation matrix */
+ if ( rotation != 0 )
+ {
+ FT_Angle angle;
+ FT_Vector v;
+
+
+ /* Rotation is F4DOT12 in multiples of pi, converted to F16DOT16 by */
+ /* << 4. FT_ANGLE_PI = 180 * 65536 (degrees), so this converts pi */
+ /* multiples to degrees. */
+ angle = FT_MulFix( rotation, FT_ANGLE_PI );
+
+ FT_Vector_Unit( &v, angle );
+
+ /* Rotation matrix: [cos -sin]
+ [sin cos] */
+ matrix->xx = v.x;
+ matrix->xy = -v.y;
+ matrix->yx = v.y;
+ matrix->yy = v.x;
+ }
+ else
+ {
+ matrix->xx = 0x10000;
+ matrix->xy = 0;
+ matrix->yx = 0;
+ matrix->yy = 0x10000;
+ }
+
+ /* Apply scale - scale each column, not row. */
+ /* First column (xx, yx) represents X-axis, scaled by `scale_x`. */
+ /* Second column (xy, yy) represents Y-axis, scaled by `scale_y`. */
+ if ( scale_x != 0x10000 || scale_y != 0x10000 )
+ {
+ matrix->xx = FT_MulFix( matrix->xx, scale_x );
+ matrix->yx = FT_MulFix( matrix->yx, scale_x );
+ matrix->xy = FT_MulFix( matrix->xy, scale_y );
+ matrix->yy = FT_MulFix( matrix->yy, scale_y );
+ }
+
+ /* apply skew */
+ if ( skew_x != 0 || skew_y != 0 )
+ {
+ FT_Matrix skew_matrix;
+ FT_Fixed tan_x, tan_y;
+
+
+ /* Convert skew angles to tangent values. */
+ /* Skew is in radians * pi, so we need to compute tan(skew * pi). */
+ tan_x = skew_x ? FT_Tan( FT_MulFix( skew_x, FT_ANGLE_PI ) ) : 0;
+ tan_y = skew_y ? FT_Tan( FT_MulFix( skew_y, FT_ANGLE_PI ) ) : 0;
+
+ /* The transform uses skew(-skewX, skewY), so only the xy term */
+ /* (the skewX contribution) is negated; the yx (skewY) term is */
+ /* positive. Matches HarfBuzz/skrifa/fontTools. */
+ skew_matrix.xx = 0x10000;
+ skew_matrix.xy = -tan_x;
+ skew_matrix.yx = tan_y;
+ skew_matrix.yy = 0x10000;
+
+ /* Apply skew on the RIGHT so the linear part is Rot*Scale*Skew */
+ /* (skew applied to the point first). `FT_Matrix_Multiply(a, b)` */
+ /* computes b = a*b, so this leaves skew_matrix = matrix*skew; */
+ /* copy it back into matrix. */
+ FT_Matrix_Multiply( matrix, &skew_matrix );
+ *matrix = skew_matrix;
+ }
+
+ /* calculate translation with center point transformation */
+ offset->x = component->translate_x;
+ offset->y = component->translate_y;
+
+ if ( component->tcenter_x != 0 || component->tcenter_y != 0 )
+ {
+ FT_Vector center;
+
+
+ center.x = component->tcenter_x;
+ center.y = component->tcenter_y;
+
+ /* offset += center - matrix * center */
+ offset->x += center.x - FT_MulFix( matrix->xx, center.x ) -
+ FT_MulFix( matrix->xy, center.y );
+ offset->y += center.y - FT_MulFix( matrix->yx, center.x ) -
+ FT_MulFix( matrix->yy, center.y );
+ }
+ }
+
+
+ /**************************************************************************
+ *
+ * Recursion Context Functions
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_context_push
+ *
+ * @Description:
+ * Push a glyph ID onto the recursion stack.
+ *
+ * @Input:
+ * context ::
+ * The recursion context.
+ *
+ * glyph_index ::
+ * The glyph ID to push.
+ *
+ * @Return:
+ * FreeType error code. Returns `FT_Err_Invalid_Table` if cycle
+ * detected or max depth exceeded.
+ */
+ static FT_Error
+ tt_varc_context_push( TT_VarcContext context,
+ FT_UInt glyph_index )
+ {
+ FT_UInt i;
+
+
+ /* check max recursion depth */
+ if ( context->recursion_depth >= TT_VARC_MAX_NESTING_LEVEL )
+ {
+ FT_TRACE2(( "tt_varc_context_push: nesting limit reached"
+ " for glyph %u\n",
+ glyph_index ));
+ return FT_THROW( Invalid_Table );
+ }
+
+ /* check for cycles */
+ for ( i = 0; i < context->recursion_depth; i++ )
+ {
+ if ( context->visited_glyphs[i] == glyph_index )
+ {
+ FT_TRACE2(( "tt_varc_context_push: cycle detected at glyph %u\n",
+ glyph_index ));
+ return FT_THROW( Invalid_Table ); /* cycle detected */
+ }
+ }
+
+ /* push onto stack */
+ context->visited_glyphs[context->recursion_depth] = glyph_index;
+ context->recursion_depth++;
+
+ return FT_Err_Ok;
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_context_pop
+ *
+ * @Description:
+ * Pop a glyph ID from the recursion stack.
+ *
+ * @Input:
+ * context ::
+ * The recursion context.
+ */
+ static void
+ tt_varc_context_pop( TT_VarcContext context )
+ {
+ if ( context->recursion_depth > 0 )
+ context->recursion_depth--;
+ }
+
+
+ /**************************************************************************
+ *
+ * VARC Table Loading Functions
+ *
+ */
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_face_load_varc
+ *
+ * @Description:
+ * Load the 'VARC' table from the font file.
+ *
+ * @Input:
+ * face ::
+ * The font face.
+ *
+ * stream ::
+ * The font stream.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ FT_LOCAL_DEF( FT_Error )
+ tt_face_load_varc( FT_Face ftface,
+ FT_Stream stream )
+ {
+ TT_Face face = (TT_Face)ftface;
+ FT_Memory memory = face->root.memory;
+ FT_Error error;
+
+ FT_Byte* table = NULL;
+ FT_ULong table_size;
+ TT_Varc varc = NULL;
+ FT_Byte* p;
+
+
+ /* Initialize VARC loading flag */
+ face->varc_loading_components = FALSE;
+ face->varc_context = NULL;
+
+ FT_TRACE2(( "VARC " ));
+
+ error = face->goto_table( face, TTAG_VARC, stream, &table_size );
+ if ( error )
+ {
+ FT_TRACE2(( "is missing\n" ));
+ goto Exit;
+ }
+ if ( table_size < 24 ) /* minimum header size */
+ {
+ FT_TRACE2(( "is too short\n" ));
+ goto Exit;
+ }
+
+ if ( FT_FRAME_EXTRACT( table_size, table ) )
+ {
+ FT_TRACE2(( "could not be read\n" ));
+ goto Exit;
+ }
+
+ if ( FT_NEW( varc ) )
+ {
+ FT_TRACE2(( "could not be loaded\n" ));
+ goto Fail;
+ }
+
+ varc->table = table;
+ varc->table_size = table_size;
+
+ p = table;
+
+ /* read header */
+ varc->version_major = ( p[0] << 8 ) | p[1];
+ varc->version_minor = ( p[2] << 8 ) | p[3];
+ p += 4;
+
+ /* check version */
+ if ( varc->version_major != 1 || varc->version_minor != 0 )
+ {
+ FT_TRACE2(( "has unsupported version %u.%u\n",
+ varc->version_major, varc->version_minor ));
+ error = FT_THROW( Invalid_Table );
+ goto Fail;
+ }
+
+ /* read offsets */
+ varc->coverage_offset = ( (FT_ULong)p[0] << 24 ) |
+ ( (FT_ULong)p[1] << 16 ) |
+ ( (FT_ULong)p[2] << 8 ) |
+ (FT_ULong)p[3];
+ p += 4;
+
+ varc->multi_var_store_offset = ( (FT_ULong)p[0] << 24 ) |
+ ( (FT_ULong)p[1] << 16 ) |
+ ( (FT_ULong)p[2] << 8 ) |
+ (FT_ULong)p[3];
+ p += 4;
+
+ varc->condition_list_offset = ( (FT_ULong)p[0] << 24 ) |
+ ( (FT_ULong)p[1] << 16 ) |
+ ( (FT_ULong)p[2] << 8 ) |
+ (FT_ULong)p[3];
+ p += 4;
+
+ varc->axis_indices_list_offset = ( (FT_ULong)p[0] << 24 ) |
+ ( (FT_ULong)p[1] << 16 ) |
+ ( (FT_ULong)p[2] << 8 ) |
+ (FT_ULong)p[3];
+ p += 4;
+
+ varc->var_composite_glyphs_offset = ( (FT_ULong)p[0] << 24 ) |
+ ( (FT_ULong)p[1] << 16 ) |
+ ( (FT_ULong)p[2] << 8 ) |
+ (FT_ULong)p[3];
+
+ /* validate offsets */
+ if ( varc->coverage_offset >= table_size ||
+ varc->var_composite_glyphs_offset >= table_size )
+ {
+ FT_TRACE2(( "has invalid offsets\n" ));
+ error = FT_THROW( Invalid_Table );
+ goto Fail;
+ }
+
+ /* set up pointers */
+ varc->coverage = table + varc->coverage_offset;
+ varc->var_composite_glyphs = table + varc->var_composite_glyphs_offset;
+
+ if ( varc->condition_list_offset > 0 &&
+ varc->condition_list_offset < table_size )
+ varc->condition_list = table + varc->condition_list_offset;
+ else
+ varc->condition_list = NULL;
+
+ if ( varc->axis_indices_list_offset > 0 &&
+ varc->axis_indices_list_offset < table_size )
+ varc->axis_indices_list = table + varc->axis_indices_list_offset;
+ else
+ varc->axis_indices_list = NULL;
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+
+ /* `MultiItemVariationStore` is NOT the same as `ItemVariationStore`! */
+ /* `MultiItemVariationStore` stores TUPLES of deltas, */
+ /* not single deltas. */
+ varc->multi_var_store_loaded = FALSE;
+ varc->multi_var_store = NULL;
+
+ if ( varc->multi_var_store_offset > 0 &&
+ varc->multi_var_store_offset < table_size )
+ {
+ /* point to the `MultiItemVariationStore` data in the 'VARC' table */
+ varc->multi_var_store = table + varc->multi_var_store_offset;
+ varc->multi_var_store_loaded = TRUE;
+
+ /* TODO: Implement proper MultiItemVariationStore parser:
+ * - Format: u16 (must be 1)
+ * - Regions: Offset32 to SparseVarRegionList
+ * - DataSets: Array16 of Offset32 to MultiVarData
+ * MultiVarData format:
+ * - Format: u8 (must be 1)
+ * - RegionIndices: Array16 of u16
+ * - DeltaSets: INDEX2 of TupleValues (CFF2-style index)
+ */
+ }
+
+#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+
+ face->varc = varc;
+
+ FT_TRACE2(( "loaded\n" ));
+ FT_TRACE4(( " version %u.%u, %lu bytes\n",
+ varc->version_major,
+ varc->version_minor,
+ varc->table_size ));
+ FT_TRACE4(( " offsets: coverage %lu, variation store %lu,\n",
+ varc->coverage_offset,
+ varc->multi_var_store_offset ));
+ FT_TRACE4(( " conditions %lu, axis indices %lu, glyphs %lu\n",
+ varc->condition_list_offset,
+ varc->axis_indices_list_offset,
+ varc->var_composite_glyphs_offset ));
+ goto Exit;
+
+ Fail:
+ FT_FREE( varc );
+ FT_FRAME_RELEASE( table );
+
+ Exit:
+ return error;
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_face_free_varc
+ *
+ * @Description:
+ * Free the 'VARC' table resources.
+ *
+ * @Input:
+ * face ::
+ * The font face.
+ */
+ FT_LOCAL_DEF( void )
+ tt_face_free_varc( FT_Face ftface )
+ {
+ TT_Face face = (TT_Face)ftface;
+ FT_Memory memory = face->root.memory;
+ FT_Stream stream = face->root.stream;
+ TT_Varc varc = (TT_Varc)face->varc;
+
+
+ if ( varc )
+ {
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+ /* `MultiItemVariationStore` cleanup. */
+ /* No special cleanup needed - */
+ /* we just point to data in the 'VARC' table. */
+ varc->multi_var_store = NULL;
+ varc->multi_var_store_loaded = FALSE;
+#endif
+
+ FT_FRAME_RELEASE( varc->table );
+ FT_FREE( varc );
+ face->varc = NULL;
+ }
+ }
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_face_has_varc_glyph
+ *
+ * @Description:
+ * Check whether a glyph has VARC data.
+ *
+ * @Input:
+ * face ::
+ * The font face.
+ *
+ * glyph_index ::
+ * The glyph ID to check.
+ *
+ * @Return:
+ * TRUE if the glyph has VARC data, FALSE otherwise.
+ */
+ FT_LOCAL_DEF( FT_Bool )
+ tt_face_has_varc_glyph( FT_Face ftface,
+ FT_UInt glyph_index )
+ {
+ TT_Face face = (TT_Face)ftface;
+ TT_Varc varc = (TT_Varc)face->varc;
+ FT_Int coverage_idx;
+
+
+ if ( !varc )
+ return FALSE;
+
+ coverage_idx = tt_varc_get_coverage( varc, glyph_index );
+ return coverage_idx >= 0;
+ }
+
+
+ /**************************************************************************
+ *
+ * VARC Glyph Loading Functions
+ *
+ */
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_varc_set_normalized_coords
+ *
+ * @Description:
+ * Lightweight coordinate swap for VARC component loading.
+ * Unlike `FT_Set_Var_Blend_Coordinates`, this only sets the normalized
+ * coordinates and invalidates the gvar tuple scalar cache. It skips
+ * CVT reload, MVAR, auto-hinter invalidation, PS name construction,
+ * and design coordinate computation - none of which are needed when
+ * loading VARC components with `FT_LOAD_NO_HINTING`.
+ *
+ * @Input:
+ * face ::
+ * The font face.
+ *
+ * coords ::
+ * Current normalized coordinates (F2DOT14).
+ *
+ * coord_count ::
+ * Number of coordinates.
+ */
+ static void
+ tt_varc_set_normalized_coords( TT_Face face,
+ FT_Fixed* coords,
+ FT_UInt num_coords )
+ {
+ GX_Blend blend = face->blend;
+ FT_UInt i;
+
+
+ if ( !blend || !blend->normalizedcoords )
+ return;
+
+ if ( num_coords > blend->num_axis )
+ num_coords = blend->num_axis;
+
+ FT_MEM_COPY( blend->normalizedcoords,
+ coords,
+ num_coords * sizeof ( FT_Fixed ) );
+
+ /* zero out remaining axes */
+ for ( i = num_coords; i < blend->num_axis; i++ )
+ blend->normalizedcoords[i] = 0;
+
+ /* update `doblend` flag */
+ face->doblend = FALSE;
+ for ( i = 0; i < blend->num_axis; i++ )
+ {
+ if ( blend->normalizedcoords[i] )
+ {
+ face->doblend = TRUE;
+ break;
+ }
+ }
+
+ /* Update face variation flag so IS_DEFAULT_INSTANCE is correct. */
+ /* Without this, `load_truetype_glyph` skips gvar deltas entirely. */
+ if ( face->doblend )
+ face->root.face_flags |= FT_FACE_FLAG_VARIATION;
+ else
+ face->root.face_flags &= ~FT_FACE_FLAG_VARIATION;
+
+ /* invalidate gvar tuple scalar cache */
+ for ( i = 0; i < blend->tuplecount; i++ )
+ blend->tuplescalars[i] = (FT_Fixed)-0x20000L;
+ }
+
+#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+
+
+ /**************************************************************************
+ *
+ * @Function:
+ * tt_face_load_varc_glyph
+ *
+ * @Description:
+ * Load a VARC glyph into the glyph slot.
+ *
+ * @Input:
+ * ftface ::
+ * The font face.
+ *
+ * glyph_slot ::
+ * The glyph slot to load into.
+ *
+ * glyph_index ::
+ * The glyph ID to load.
+ *
+ * load_flags ::
+ * Loading flags.
+ *
+ * @Return:
+ * FreeType error code. 0 means success.
+ */
+ FT_LOCAL_DEF( FT_Error )
+ tt_face_load_varc_glyph( FT_Face ftface,
+ FT_GlyphSlot glyph_slot,
+ FT_UInt glyph_index,
+ FT_Int32 load_flags )
+ {
+ TT_Face face = (TT_Face)ftface;
+ FT_Memory memory = face->root.memory;
+ FT_Error error;
+
+ TT_Varc varc = (TT_Varc)face->varc;
+ FT_Byte* record_data;
+ FT_UInt record_size;
+
+ FT_Byte* p = NULL;
+ FT_Byte* limit;
+
+ TT_VarcContext context;
+ FT_Bool context_owner = FALSE;
+
+ TT_GlyphSlot slot = (TT_GlyphSlot)glyph_slot;
+ FT_GlyphLoader gloader;
+
+ FT_Fixed* parent_coords = NULL;
+ FT_UInt num_coords = 0;
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+ FT_Bool static_axes = FALSE;
+#endif
+
+ FT_Fixed stack_new_coords[VARC_STACK_COORD_COUNT];
+ FT_UInt stack_axis_indices[VARC_STACK_INDICES_COUNT];
+ FT_Fixed stack_axis_values[VARC_STACK_AXIS_COUNT];
+
+ FT_Matrix saved_transform_matrix;
+ FT_Vector saved_transform_delta;
+ FT_UInt saved_transform_flags;
+
+
+ if ( !varc )
+ return FT_THROW( Invalid_Table );
+
+ /* get or create recursion context */
+ context = (TT_VarcContext)face->varc_context;
+ if ( !context )
+ {
+ /* first call - create new context on heap */
+ if ( FT_NEW( context ) )
+ return error;
+
+ FT_ZERO( context );
+ face->varc_context = context;
+ context_owner = TRUE;
+
+ /* save font's current variation coordinates */
+ /* for inheritance at depth 1 */
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+
+ {
+ FT_MM_Var* master;
+
+
+ /* `FT_Get_MM_Var` ensures `face->blend` is initialized */
+ /* (it's lazy) */
+ error = FT_Get_MM_Var( (FT_Face)face, &master );
+ if ( !error && master )
+ {
+ if ( master->num_axis > 0 )
+ context->num_font_coords = master->num_axis;
+ FT_Done_MM_Var( face->root.driver->root.library,
+ master );
+ }
+ else if ( error &&
+ !FT_HAS_MULTIPLE_MASTERS( (FT_Face)face ) )
+ {
+ if ( !face->blend )
+ (void)tt_var_init_gvar( (FT_Face)face );
+
+ if ( face->blend && !face->blend->mmvar )
+ {
+ (void)tt_get_var_blend( (FT_Face)face,
+ &context->num_font_coords,
+ NULL,
+ NULL,
+ NULL );
+ static_axes = TRUE;
+ }
+ }
+ error = FT_Err_Ok; /* non-fatal */
+ }
+
+ if ( context->num_font_coords > 0 )
+ {
+ if ( !FT_NEW_ARRAY( context->font_coords,
+ context->num_font_coords ) )
+ {
+ if ( static_axes )
+ FT_MEM_COPY( context->font_coords,
+ face->blend->normalizedcoords,
+ context->num_font_coords * sizeof ( FT_Fixed ) );
+ else
+ error = FT_Get_Var_Blend_Coordinates(
+ (FT_Face)face,
+ context->num_font_coords,
+ context->font_coords );
+
+ if ( error )
+ {
+ FT_FREE( context->font_coords );
+ context->num_font_coords = 0;
+ }
+ else
+ {
+ FT_Generic saved_autohint = face->root.autohint;
+
+
+ /* set initial current_coords to font_coords */
+ context->current_coords = context->font_coords;
+ context->num_current_coords = context->num_font_coords;
+
+ if ( !static_axes )
+ {
+ /* Force blend initialization: allocate */
+ /* `normalizedcoords`, load 'gvar', etc. This ensures */
+ /* `tt_varc_set_normalized_coords` can work as a */
+ /* lightweight coordinate swapper later. */
+ face->root.autohint.data = NULL;
+ face->root.autohint.finalizer = NULL;
+
+ FT_Set_Var_Blend_Coordinates( (FT_Face)face,
+ context->num_font_coords,
+ context->font_coords );
+
+ face->root.autohint = saved_autohint;
+ }
+ }
+ }
+ }
+
+#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+ }
+
+ /* Save the face's current transform (user's transform at top level, */
+ /* parent's VARC transform at recursive levels). We restore or clear */
+ /* this before returning so `FT_Load_Glyph`'s post-processing does the */
+ /* right thing. */
+ saved_transform_matrix = face->root.internal->transform_matrix;
+ saved_transform_delta = face->root.internal->transform_delta;
+ saved_transform_flags = face->root.internal->transform_flags;
+
+ /* get parent coords from context for delta evaluation */
+ parent_coords = context->current_coords;
+ num_coords = context->num_current_coords;
+
+ /* push this glyph onto recursion stack */
+ error = tt_varc_context_push( context, glyph_index );
+ if ( error )
+ {
+ if ( context_owner )
+ face->varc_context = NULL;
+ return error;
+ }
+
+ /* get glyph record */
+ error = tt_varc_get_glyph_record( varc, glyph_index,
+ &record_data, &record_size );
+ if ( error )
+ goto Cleanup;
+
+ p = record_data;
+ limit = record_data + record_size;
+
+ FT_TRACE4(( "VARC glyph %u: nesting level %u, %u-byte record\n",
+ glyph_index, context->recursion_depth, record_size ));
+
+ /* Allocate a reusable temp glyph slot for component loading. */
+ /* `FT_New_GlyphSlot` prepends to `face->glyph` list, so */
+ /* `FT_Load_Glyph` will load into this slot. One slot per */
+ /* recursion level suffices. */
+ {
+ FT_GlyphSlot temp_glyph;
+
+
+ error = FT_New_GlyphSlot( (FT_Face)face, &temp_glyph );
+ if ( error )
+ goto Cleanup;
+ }
+
+ /* Accumulate into the slot's own loader, like the glyf composite */
+ /* path; the slot owns this storage and frees it on destruction. */
+ gloader = slot->internal->loader;
+ FT_GlyphLoader_Rewind( gloader );
+
+ /* parse components until we run out of data */
+ while ( p < limit )
+ {
+ TT_VarcComponentRec component;
+ FT_GlyphSlot component_slot;
+
+ FT_Matrix matrix;
+ FT_Vector offset;
+ FT_Matrix composed_matrix;
+ FT_Vector composed_delta;
+
+ FT_Matrix saved_parent_matrix;
+ FT_Vector saved_parent_delta;
+ FT_Bool saved_has_parent;
+
+ FT_Fixed* new_coords;
+ FT_Bool new_coords_on_heap;
+ FT_Bool has_axis_override;
+
+
+ /* parse component */
+ error = tt_varc_parse_component( face, varc,
+ &p, limit,
+ &component,
+ stack_axis_values,
+ VARC_STACK_AXIS_COUNT );
+ if ( error )
+ {
+ /* skip malformed component and continue with others */
+ FT_TRACE2(( "tt_face_load_varc_glyph: malformed component"
+ " in glyph %u (error 0x%x)\n",
+ glyph_index, error ));
+ break; /* exit loop - can't safely continue parsing */
+ /* if we don't know component size */
+ }
+
+ FT_TRACE5(( " component glyph %u, flags 0x%08lx\n",
+ component.gid, (FT_ULong)component.flags ));
+
+ /* Evaluate the component's condition, if any. When it is not */
+ /* satisfied, the component is not rendered, matching HarfBuzz, */
+ /* skrifa, and fontTools. */
+ if ( component.flags & VARC_HAVE_CONDITION )
+ {
+ FT_Byte* table_limit = (FT_Byte*)varc->table + varc->table_size;
+ FT_Byte* cond =
+ tt_varc_get_condition( varc,
+ component.condition_index,
+ table_limit );
+
+
+#ifdef FT_DEBUG_LEVEL_TRACE
+ if ( !cond )
+ FT_TRACE2(( " component glyph %u has invalid condition %lu;"
+ " ignoring condition\n",
+ component.gid,
+ (FT_ULong)component.condition_index ));
+#endif
+ if ( cond &&
+ !tt_varc_eval_condition( face, varc, cond, table_limit,
+ parent_coords, num_coords, 0 ) )
+ {
+ FT_TRACE5(( " condition %lu is false; skipping component\n",
+ (FT_ULong)component.condition_index ));
+ tt_varc_free_component( face, &component );
+ continue;
+ }
+#ifdef FT_DEBUG_LEVEL_TRACE
+ else
+ FT_TRACE6(( " condition %lu is true\n",
+ (FT_ULong)component.condition_index ));
+#endif
+ }
+
+ /* apply variation deltas to axis values if present */
+ if ( component.flags & VARC_AXES_HAVE_VARIATION )
+ tt_varc_apply_axis_deltas( face, varc, &component,
+ parent_coords, num_coords );
+
+ /* apply variation deltas to transform values if present */
+ if ( component.flags & VARC_TRANSFORM_HAS_VARIATION )
+ tt_varc_apply_transform_deltas( face, varc, &component,
+ parent_coords, num_coords );
+
+ /* build transformation matrix */
+ tt_varc_build_transform( &component, &matrix, &offset );
+
+ /* apply axis value overrides if present */
+ new_coords = NULL;
+ new_coords_on_heap = FALSE;
+ has_axis_override = FALSE;
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+
+ if ( ( ( component.num_axis_values > 0 && component.axis_values ) ||
+ ( component.flags & VARC_RESET_UNSPECIFIED_AXES ) ) &&
+ num_coords > 0 &&
+ parent_coords )
+ {
+ /* allocate new_coords only */
+ /* (`parent_coords` replaces `saved_coords`) */
+ if ( num_coords <= VARC_STACK_COORD_COUNT )
+ new_coords = stack_new_coords;
+ else
+ {
+ if ( FT_NEW_ARRAY( new_coords, num_coords ) )
+ goto Skip_Axis_Override;
+ new_coords_on_heap = TRUE;
+ }
+
+ /* start with current coordinates or reset to default */
+ if ( component.flags & VARC_RESET_UNSPECIFIED_AXES )
+ {
+ /* Reset unspecified axes to the font's current instance */
+ /* coordinates, matching HarfBuzz (font->coords), skrifa, and */
+ /* fontTools. Axes explicitly specified below are overridden */
+ /* afterwards. */
+ FT_UInt j;
+ FT_UInt ncopy = context->num_font_coords < num_coords
+ ? context->num_font_coords : num_coords;
+
+
+ if ( context->font_coords )
+ FT_MEM_COPY( new_coords, context->font_coords,
+ ncopy * sizeof ( FT_Fixed ) );
+ else
+ ncopy = 0;
+
+ for ( j = ncopy; j < num_coords; j++ )
+ new_coords[j] = 0; /* default for axes beyond `font_coords` */
+ }
+ else
+ {
+ /* inherit from parent coords (handles both depth 1 and > 1) */
+ FT_MEM_COPY( new_coords, parent_coords,
+ num_coords * sizeof ( FT_Fixed ) );
+ }
+
+ /* read axis indices and apply values */
+ if ( component.flags & VARC_HAVE_AXES &&
+ varc->axis_indices_list )
+ {
+ FT_UInt* axis_indices = NULL;
+ FT_UInt j;
+
+
+ /* allocate array for axis indices */
+ if ( component.num_axis_values <= VARC_STACK_INDICES_COUNT )
+ axis_indices = stack_axis_indices;
+ else
+ {
+ if ( FT_NEW_ARRAY( axis_indices, component.num_axis_values ) )
+ {
+ if ( new_coords_on_heap )
+ FT_FREE( new_coords );
+ new_coords = NULL;
+ goto Skip_Axis_Override;
+ }
+ }
+
+ /* read axis indices from TupleList */
+ error = tt_varc_get_axis_indices( varc,
+ component.axis_indices_index,
+ component.num_axis_values,
+ axis_indices );
+ if ( error )
+ {
+ FT_TRACE2(( " component glyph %u has invalid axis indices"
+ " (error 0x%x)\n",
+ component.gid, error ));
+ if ( axis_indices != stack_axis_indices )
+ FT_FREE( axis_indices );
+ if ( new_coords_on_heap )
+ FT_FREE( new_coords );
+ new_coords = NULL;
+ goto Skip_Axis_Override;
+ }
+
+ /* apply axis values using the indices */
+ for ( j = 0; j < component.num_axis_values; j++ )
+ {
+ FT_UInt axis_idx = axis_indices[j];
+ FT_Fixed v = component.axis_values[j];
+
+
+ if ( axis_idx < num_coords )
+ {
+ /* Round to the nearest F2DOT14 grid point (a multiple of 4 */
+ /* in 16.16) before the value feeds the leaf's gvar */
+ /* interpolation, matching HarfBuzz (roundf) and skrifa. */
+ if ( v >= 0 )
+ v = ( v + 2 ) & ~(FT_Fixed)3;
+ else
+ v = NEG_LONG( ( NEG_LONG( v ) + 2 ) & ~(FT_Fixed)3 );
+
+ new_coords[axis_idx] = v;
+ FT_TRACE7(( " axis %u: %.5f\n",
+ axis_idx, (double)v / 65536 ));
+ }
+#ifdef FT_DEBUG_LEVEL_TRACE
+ else
+ FT_TRACE2(( " component glyph %u has out-of-range axis %u\n",
+ component.gid, axis_idx ));
+#endif
+ }
+
+ if ( axis_indices != stack_axis_indices )
+ FT_FREE( axis_indices );
+ }
+
+ /* Apply the new normalized coordinates. Use lightweight path */
+ /* that only swaps coords and invalidates the gvar tuple cache. */
+ tt_varc_set_normalized_coords( face, new_coords, num_coords );
+ has_axis_override = TRUE;
+ }
+
+ Skip_Axis_Override:
+
+#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
+
+ /* Load component - allow recursive VARC loading. */
+ /* Compose transforms: we want (T1*T2)(leaf) not T1(T2(leaf)). */
+
+ /* save current parent transform */
+ saved_parent_matrix = context->parent_matrix;
+ saved_parent_delta = context->parent_delta;
+ saved_has_parent = context->has_parent_transform;
+
+ /* compose with parent transform if present */
+ if ( context->has_parent_transform )
+ {
+ /* Compose: final = parent * child. `FT_Matrix_Multiply(a, b)` */
+ /* computes b = a*b, so seed the result with the child matrix */
+ /* and left-multiply by the parent; this matches the */
+ /* composed_delta below (the translation of parent*child) and */
+ /* HarfBuzz/skrifa. */
+ composed_matrix = matrix;
+ FT_Matrix_Multiply( &context->parent_matrix, &composed_matrix );
+
+ /* transform offset by parent matrix and add parent delta */
+ composed_delta.x = FT_MulFix( offset.x, context->parent_matrix.xx ) +
+ FT_MulFix( offset.y, context->parent_matrix.xy ) +
+ context->parent_delta.x;
+ composed_delta.y = FT_MulFix( offset.x, context->parent_matrix.yx ) +
+ FT_MulFix( offset.y, context->parent_matrix.yy ) +
+ context->parent_delta.y;
+ }
+ else
+ {
+ /* no parent transform, use component transform directly */
+ composed_matrix = matrix;
+ composed_delta = offset;
+ }
+
+ /* store composed transform in context for child VARC components */
+ context->parent_matrix = composed_matrix;
+ context->parent_delta = composed_delta;
+ context->has_parent_transform = TRUE;
+
+ FT_TRACE7(( " matrix: [ %.5f %.5f; %.5f %.5f ]\n",
+ (double)composed_matrix.xx / 65536,
+ (double)composed_matrix.xy / 65536,
+ (double)composed_matrix.yx / 65536,
+ (double)composed_matrix.yy / 65536 ));
+ FT_TRACE7(( " offset: (%.5f, %.5f)\n",
+ (double)composed_delta.x / 64,
+ (double)composed_delta.y / 64 ));
+
+
+ /* set child's coords in context for recursive VARC processing */
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+ if ( has_axis_override )
+ {
+ context->current_coords = new_coords;
+ context->num_current_coords = num_coords;
+ }
+#endif
+
+ /* Set composed VARC transform on face so `FT_Load_Glyph` applies */
+ /* it. The matrix is dimensionless (16.16) and works in any */
+ /* coordinate system. The delta must match the outline's coordinate */
+ /* space: 26.6 device pixels (scaled) or integer font units */
+ /* (NO_SCALE). */
+ {
+ FT_Face_Internal internal = face->root.internal;
+
+
+ internal->transform_matrix = composed_matrix;
+ internal->transform_flags = 0;
+
+ if ( ( composed_matrix.xy | composed_matrix.yx ) ||
+ composed_matrix.xx != 0x10000L ||
+ composed_matrix.yy != 0x10000L )
+ internal->transform_flags |= 1;
+
+ if ( load_flags & FT_LOAD_NO_SCALE )
+ {
+ /* NO_SCALE: outline in integer font units */
+ internal->transform_delta.x = ( composed_delta.x + 32 ) >> 6;
+ internal->transform_delta.y = ( composed_delta.y + 32 ) >> 6;
+ }
+ else
+ {
+ FT_Size size = face->root.size;
+
+
+ if ( size )
+ {
+ FT_Fixed x_scale = size->metrics.x_scale;
+ FT_Fixed y_scale = size->metrics.y_scale;
+
+
+ /* Convert from 26.6 font units to 26.6 device pixels. */
+ /* 26.6 * 16.16 = 42.22; >> 22 gives 26.6. */
+#ifdef FT_INT64
+ internal->transform_delta.x =
+ (FT_Pos)( ( (FT_Int64)composed_delta.x * x_scale +
+ 0x200000L ) >> 22 );
+ internal->transform_delta.y =
+ (FT_Pos)( ( (FT_Int64)composed_delta.y * y_scale +
+ 0x200000L ) >> 22 );
+#else
+ internal->transform_delta.x =
+ FT_MulDiv( composed_delta.x, x_scale, 0x400000L );
+ internal->transform_delta.y =
+ FT_MulDiv( composed_delta.y, y_scale, 0x400000L );
+#endif
+ }
+ else
+ {
+ internal->transform_delta.x = ( composed_delta.x + 32 ) >> 6;
+ internal->transform_delta.y = ( composed_delta.y + 32 ) >> 6;
+ }
+ }
+
+ if ( internal->transform_delta.x | internal->transform_delta.y )
+ internal->transform_flags |= 2;
+ }
+
+ /* Load component. Strip IGNORE_TRANSFORM so our VARC transform */
+ /* is applied by `FT_Load_Glyph`'s post-processing. Add NO_HINTING */
+ /* to avoid auto-fitter reentrancy: the auto-fitter initializes */
+ /* per-face metrics lazily, and `FT_Set_Var_Blend_Coordinates` */
+ /* (called below to change axis values) frees that state, causing */
+ /* use-after-free if the auto-fitter was mid-initialization. */
+ {
+ FT_Int32 component_load_flags =
+ ( load_flags & ~(FT_Int32)FT_LOAD_IGNORE_TRANSFORM ) |
+ FT_LOAD_NO_HINTING;
+
+
+ error = FT_Load_Glyph( (FT_Face)face, component.gid,
+ component_load_flags );
+ }
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+ /* restore parent coordinates */
+ if ( has_axis_override )
+ {
+ tt_varc_set_normalized_coords( face, parent_coords, num_coords );
+
+ context->current_coords = parent_coords;
+ context->num_current_coords = num_coords;
+
+ if ( new_coords_on_heap )
+ FT_FREE( new_coords );
+ }
+#endif
+
+ /* restore parent transform in context */
+ context->parent_matrix = saved_parent_matrix;
+ context->parent_delta = saved_parent_delta;
+ context->has_parent_transform = saved_has_parent;
+
+ component_slot = face->root.glyph;
+
+ if ( error )
+ {
+ FT_TRACE2(( " component glyph %u failed (error 0x%x); skipped\n",
+ component.gid, error ));
+ tt_varc_free_component( face, &component );
+ continue; /* skip failed component, continue with others */
+ }
+
+ /* `FT_Load_Glyph` already applied our composed transform.
+ *
+ * For base glyphs: the driver loaded and scaled, then
+ * `FT_Load_Glyph` applied our matrix + delta.
+ *
+ * For VARC sub-components: the recursive handler processed
+ * them and cleared the face transform before returning, so
+ * `FT_Load_Glyph`'s post-processing was a no-op.
+ */
+ if ( component_slot->outline.n_points > 0 )
+ {
+ FT_Outline* src = &component_slot->outline;
+ FT_Outline* cur = &gloader->current.outline;
+
+
+ /* ensure capacity in gloader with geometric growth */
+ error = FT_GLYPHLOADER_CHECK_POINTS( gloader,
+ src->n_points,
+ src->n_contours );
+ if ( error )
+ {
+ tt_varc_free_component( face, &component );
+ goto Cleanup;
+ }
+
+ FT_ARRAY_COPY( cur->points, src->points, src->n_points );
+ FT_ARRAY_COPY( cur->tags, src->tags, src->n_points );
+ FT_ARRAY_COPY( cur->contours, src->contours, src->n_contours );
+
+ cur->n_points = src->n_points;
+ cur->n_contours = src->n_contours;
+
+ FT_GlyphLoader_Add( gloader );
+
+ FT_TRACE5(( " appended %d points in %d contours\n",
+ src->n_points, src->n_contours ));
+ }
+
+ tt_varc_free_component( face, &component );
+ }
+
+ /* Hand the loader's accumulated outline to the slot. */
+ {
+ FT_Outline* base = &gloader->base.outline;
+
+
+ if ( base->n_points > 0 )
+ {
+ slot->outline = *base;
+ slot->format = FT_GLYPH_FORMAT_OUTLINE;
+ }
+
+ FT_TRACE4(( "VARC glyph %u: %d points in %d contours\n",
+ glyph_index, base->n_points, base->n_contours ));
+ }
+
+ error = FT_Err_Ok;
+
+ Cleanup:
+ /* Free the temp glyph slot for this recursion level. */
+ /* `face->root.glyph` points to the temp slot we allocated above; */
+ /* `FT_Done_GlyphSlot` removes it and restores the previous head. */
+ if ( face->root.glyph != glyph_slot )
+ FT_Done_GlyphSlot( face->root.glyph );
+
+ /* pop from recursion stack */
+ tt_varc_context_pop( context );
+
+ /* clean up context if we own it */
+ if ( context_owner )
+ {
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+ /* Restore font's original coordinates in case we bailed out */
+ /* mid-component with overridden axis values still active. */
+ if ( context->font_coords )
+ tt_varc_set_normalized_coords( face,
+ context->font_coords,
+ context->num_font_coords );
+#endif
+ if ( context->font_coords )
+ FT_FREE( context->font_coords );
+ FT_FREE( context );
+ face->varc_context = NULL;
+ }
+
+ /* Manage face transform for `FT_Load_Glyph`'s post-processing. */
+ /* At top level: restore user's transform so it gets applied once. */
+ /* At recursive levels: clear to identity so the parent's */
+ /* `FT_Load_Glyph` doesn't apply twice. */
+ if ( context_owner )
+ {
+ face->root.internal->transform_matrix = saved_transform_matrix;
+ face->root.internal->transform_delta = saved_transform_delta;
+ face->root.internal->transform_flags = saved_transform_flags;
+ }
+ else
+ {
+ face->root.internal->transform_matrix.xx = 0x10000L;
+ face->root.internal->transform_matrix.xy = 0;
+ face->root.internal->transform_matrix.yx = 0;
+ face->root.internal->transform_matrix.yy = 0x10000L;
+ face->root.internal->transform_delta.x = 0;
+ face->root.internal->transform_delta.y = 0;
+ face->root.internal->transform_flags = 0;
+ }
+
+#ifdef FT_DEBUG_LEVEL_TRACE
+ if ( error )
+ FT_TRACE2(( "tt_face_load_varc_glyph: glyph %u failed"
+ " (error 0x%x)\n",
+ glyph_index, error ));
+#endif
+
+ return error;
+ }
+
+
+#endif /* TT_CONFIG_OPTION_VARC */
+
+
+/* END */
diff --git a/thirdparty/freetype/src/truetype/ttvarc.h b/thirdparty/freetype/src/truetype/ttvarc.h
new file mode 100644
index 00000000000..1ac742a3962
--- /dev/null
+++ b/thirdparty/freetype/src/truetype/ttvarc.h
@@ -0,0 +1,372 @@
+/****************************************************************************
+ *
+ * ttvarc.h
+ *
+ * TrueType and OpenType VARC (Variable Composites) support
+ * (specification).
+ *
+ * Copyright (C) 2025-2026 by
+ * Behdad Esfahbod, David Turner, Robert Wilhelm, and Werner Lemberg.
+ *
+ * This file is part of the FreeType project, and may only be used,
+ * modified, and distributed under the terms of the FreeType project
+ * license, LICENSE.TXT. By continuing to use, modify, or distribute
+ * this file you indicate that you have read the license and
+ * understand and accept it fully.
+ *
+ */
+
+
+#ifndef TTVARC_H_
+#define TTVARC_H_
+
+
+#include "ttobjs.h"
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+#include <freetype/internal/ftmmtypes.h>
+#endif
+
+
+FT_BEGIN_HEADER
+
+
+#ifdef TT_CONFIG_OPTION_VARC
+
+
+ FT_LOCAL( FT_Error )
+ tt_face_init_varc_axes( FT_Face face,
+ FT_UInt axis_count );
+
+
+ /**************************************************************************
+ *
+ * VARC component flags from the specification.
+ *
+ */
+
+ /* Variable Component Flags - from VARC spec. */
+#define VARC_RESET_UNSPECIFIED_AXES 0x00000001U /* Bit 0 */
+#define VARC_HAVE_AXES 0x00000002U /* Bit 1 */
+#define VARC_AXES_HAVE_VARIATION 0x00000004U /* Bit 2 */
+#define VARC_TRANSFORM_HAS_VARIATION 0x00000008U /* Bit 3 */
+#define VARC_HAVE_TRANSLATE_X 0x00000010U /* Bit 4 */
+#define VARC_HAVE_TRANSLATE_Y 0x00000020U /* Bit 5 */
+#define VARC_HAVE_ROTATION 0x00000040U /* Bit 6 */
+#define VARC_HAVE_CONDITION 0x00000080U /* Bit 7 */
+#define VARC_HAVE_SCALE_X 0x00000100U /* Bit 8 */
+#define VARC_HAVE_SCALE_Y 0x00000200U /* Bit 9 */
+#define VARC_HAVE_TCENTER_X 0x00000400U /* Bit 10 */
+#define VARC_HAVE_TCENTER_Y 0x00000800U /* Bit 11 */
+#define VARC_GID_IS_24BIT 0x00001000U /* Bit 12 */
+#define VARC_HAVE_SKEW_X 0x00002000U /* Bit 13 */
+#define VARC_HAVE_SKEW_Y 0x00004000U /* Bit 14 */
+
+ /* Reserved flags - bits 15-31 must be zero. */
+#define VARC_RESERVED_MASK 0xFFFF8000U
+
+
+ /**************************************************************************
+ *
+ * @Struct:
+ * TT_VarcComponentRec
+ *
+ * @Description:
+ * Represents a single component within a VARC glyph.
+ *
+ * @Fields:
+ * flags ::
+ * Component flags indicating which optional fields are present.
+ *
+ * gid ::
+ * The glyph ID of the component to be included.
+ *
+ * condition_index ::
+ * Index into the condition list (if `VARC_HAVE_CONDITION` is set).
+ * Component is only rendered if condition evaluates to true.
+ *
+ * axis_indices_index ::
+ * Index into the axis indices list (if `VARC_HAVE_AXES` is set).
+ *
+ * num_axis_values ::
+ * Number of axis values in the `axis_values` array.
+ *
+ * axis_values ::
+ * Array of normalized axis coordinates (-1.0 to +1.0 in F2DOT14).
+ * These override the current design space location for this component.
+ *
+ * axis_values_var_index ::
+ * Variation store index for axis values (if `VARC_AXES_HAVE_VARIATION`
+ * is set).
+ *
+ * transform_var_index ::
+ * Variation store index for transform values (if
+ * `VARC_TRANSFORM_HAS_VARIATION` is set).
+ *
+ * translate_x ::
+ * translate_y ::
+ * Translation amounts in font units (26.6 fixed-point).
+ *
+ * rotation ::
+ * Rotation angle in radians, multiplied by pi (16.16 fixed-point).
+ * So 0x10000 represents pi radians (180 degrees).
+ *
+ * scale_x ::
+ * scale_y ::
+ * Scale factors (16.16 fixed-point, 1.0 = 0x10000).
+ *
+ * skew_x ::
+ * skew_y ::
+ * Skew angles in radians, multiplied by pi (16.16 fixed-point).
+ *
+ * tcenter_x ::
+ * tcenter_y ::
+ * Transformation center point in font units (26.6 fixed-point).
+ * Transformations are applied relative to this point.
+ */
+ typedef struct TT_VarcComponentRec_
+ {
+ FT_UInt32 flags;
+ FT_UInt gid;
+
+ /* conditional rendering */
+ FT_UInt32 condition_index;
+
+ /* axis value overrides */
+ FT_UInt32 axis_indices_index;
+ FT_UInt num_axis_values;
+ FT_Fixed* axis_values; /* normalized coordinates (F2DOT14) */
+ FT_Bool axis_values_on_heap; /* TRUE if axis_values needs `FT_FREE` */
+ FT_UInt32 axis_values_var_index;
+
+ /* transform variation */
+ FT_UInt32 transform_var_index;
+
+ /* transform components (all 26.6 or 16.16 fixed-point) */
+ FT_Pos translate_x;
+ FT_Pos translate_y;
+ FT_Fixed rotation; /* radians * pi, 16.16 format */
+ FT_Fixed scale_x;
+ FT_Fixed scale_y;
+ FT_Fixed skew_x;
+ FT_Fixed skew_y;
+ FT_Pos tcenter_x;
+ FT_Pos tcenter_y;
+
+ } TT_VarcComponentRec, *TT_VarcComponent;
+
+
+ /**************************************************************************
+ *
+ * @Struct:
+ * TT_VarcRec
+ *
+ * @Description:
+ * The 'VARC' table structure, loaded from the font file.
+ *
+ * @Fields:
+ * version_major ::
+ * Major version of the 'VARC' table (currently 1).
+ *
+ * version_minor ::
+ * Minor version of the 'VARC' table (currently 0).
+ *
+ * coverage_offset ::
+ * Offset to the coverage table from start of 'VARC' table.
+ *
+ * multi_var_store_offset ::
+ * Offset to the `MultiItemVariationStore`.
+ *
+ * condition_list_offset ::
+ * Offset to the condition list.
+ *
+ * axis_indices_list_offset ::
+ * Offset to the axis indices list.
+ *
+ * var_composite_glyphs_offset ::
+ * Offset to the glyph records (CFF2-style index).
+ *
+ * coverage ::
+ * Pointer to the coverage table data.
+ *
+ * condition_list ::
+ * Pointer to the condition list data, or `NULL` if the table has no
+ * condition list.
+ *
+ * axis_indices_list ::
+ * Pointer to the CFF2-style index that contains the axis indices for
+ * component axis-value tuples, or `NULL` if the table has no axis
+ * indices list.
+ *
+ * var_composite_glyphs ::
+ * Pointer to the CFF2-style index that contains the component records
+ * for the glyphs referenced by `coverage`.
+ *
+ * multi_var_store ::
+ * The loaded `MultiItemVariationStore` for variation deltas.
+ *
+ * multi_var_store_loaded ::
+ * A boolean that indicates whether `multi_var_store` points to a
+ * `MultiItemVariationStore` in the table.
+ *
+ * table ::
+ * Pointer to the raw 'VARC' table data.
+ *
+ * table_size ::
+ * Size of the 'VARC' table in bytes.
+ */
+ typedef struct TT_VarcRec_
+ {
+ FT_UShort version_major;
+ FT_UShort version_minor;
+
+ /* table offsets */
+ FT_ULong coverage_offset;
+ FT_ULong multi_var_store_offset;
+ FT_ULong condition_list_offset;
+ FT_ULong axis_indices_list_offset;
+ FT_ULong var_composite_glyphs_offset;
+
+ /* loaded data pointers */
+ FT_Byte* coverage;
+ FT_Byte* condition_list;
+ FT_Byte* axis_indices_list;
+ FT_Byte* var_composite_glyphs;
+
+#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
+ /* `MultiItemVariationStore` - stores tuples of deltas, */
+ /* not single values */
+ FT_Byte* multi_var_store; /* pointer to MultiItemVariationStore data */
+ FT_Bool multi_var_store_loaded;
+#endif
+
+ /* raw table data */
+ void* table;
+ FT_ULong table_size;
+
+ } TT_VarcRec, *TT_Varc;
+
+
+ /**************************************************************************
+ *
+ * Maximum recursion depth for VARC glyph loading.
+ * This prevents stack overflow and infinite loops from malformed fonts.
+ */
+#define TT_VARC_MAX_NESTING_LEVEL 16
+
+
+ /**************************************************************************
+ *
+ * @Struct:
+ * TT_VarcContextRec
+ *
+ * @Description:
+ * Context structure for recursive VARC glyph loading.
+ * Tracks recursion depth and visited glyphs for cycle detection.
+ *
+ * @Fields:
+ * recursion_depth ::
+ * Current depth in the recursion stack.
+ *
+ * visited_glyphs ::
+ * Array of glyph IDs currently being processed.
+ * Used for cycle detection to prevent infinite loops.
+ *
+ * parent_matrix ::
+ * The transformation matrix accumulated from parent components.
+ *
+ * parent_delta ::
+ * The translation accumulated from parent components.
+ *
+ * has_parent_transform ::
+ * A boolean that indicates whether `parent_matrix` and `parent_delta`
+ * contain a transform to apply to child components.
+ *
+ * font_coords ::
+ * The face's normalized variation coordinates before loading the VARC
+ * glyph. The context owns this array.
+ *
+ * num_font_coords ::
+ * The number of coordinates in `font_coords`.
+ *
+ * current_coords ::
+ * The normalized variation coordinates used to evaluate deltas at the
+ * current recursion level. This points to `font_coords` at the top
+ * level or to a component's temporary coordinate array at deeper
+ * levels; the context does not own the latter.
+ *
+ * num_corrent_coords ::
+ * The number of coordinates in `current_coords`.
+ */
+ typedef struct TT_VarcContextRec_
+ {
+ FT_UInt recursion_depth;
+ FT_UInt visited_glyphs[TT_VARC_MAX_NESTING_LEVEL];
+
+ /* Parent transform to compose with child transforms. */
+ FT_Matrix parent_matrix;
+ FT_Vector parent_delta;
+ FT_Bool has_parent_transform;
+
+ /* Font's original variation coordinates (before VARC processing). */
+ /* Used when RESET_UNSPECIFIED_AXES is not set at depth 1. */
+ FT_Fixed* font_coords;
+ FT_UInt num_font_coords;
+
+ /* Current variation coords for delta evaluation at this recursion level.
+ * Points to `font_coords` at depth 1, or parent's coords at depth > 1.
+ * Not owned - do not free. */
+ FT_Fixed* current_coords;
+ FT_UInt num_current_coords;
+
+ /* Reusable buffers to avoid per-component allocations. */
+ FT_Fixed* axis_values_buffer;
+ FT_UInt axis_values_buffer_size;
+ FT_Fixed* deltas_buffer;
+ FT_UInt deltas_buffer_size;
+ FT_Fixed* coords_buffer;
+ FT_UInt coords_buffer_size;
+ FT_UInt* indices_buffer;
+ FT_UInt indices_buffer_size;
+
+ } TT_VarcContextRec, *TT_VarcContext;
+
+
+ /**************************************************************************
+ *
+ * VARC API functions
+ *
+ */
+
+ /* Load the VARC table from the font stream. */
+ FT_LOCAL( FT_Error )
+ tt_face_load_varc( FT_Face face,
+ FT_Stream stream );
+
+ /* Free the VARC table resources. */
+ FT_LOCAL( void )
+ tt_face_free_varc( FT_Face face );
+
+ /* Check whether a glyph has VARC data. */
+ FT_LOCAL( FT_Bool )
+ tt_face_has_varc_glyph( FT_Face face,
+ FT_UInt glyph_index );
+
+ /* Load a VARC glyph into the glyph slot. */
+ FT_LOCAL( FT_Error )
+ tt_face_load_varc_glyph( FT_Face face,
+ FT_GlyphSlot glyph_slot,
+ FT_UInt glyph_index,
+ FT_Int32 load_flags );
+
+
+#endif /* TT_CONFIG_OPTION_VARC */
+
+
+FT_END_HEADER
+
+
+#endif /* TTVARC_H_ */
+
+
+/* END */
diff --git a/thirdparty/freetype/src/type1/t1load.c b/thirdparty/freetype/src/type1/t1load.c
index 1d095f8ac1f..2ed327ba2e7 100644
--- a/thirdparty/freetype/src/type1/t1load.c
+++ b/thirdparty/freetype/src/type1/t1load.c
@@ -677,7 +677,7 @@
PS_Blend blend = t1face->blend;
FT_Fixed axiscoords[4];
- FT_UInt i, nc;
+ FT_UInt i;
if ( !blend )
@@ -687,19 +687,18 @@
axiscoords,
blend->num_axis );
- nc = num_coords;
if ( num_coords > blend->num_axis )
{
FT_TRACE2(( "T1_Get_Var_Design:"
" only using first %u of %u coordinates\n",
blend->num_axis, num_coords ));
- nc = blend->num_axis;
+
+ FT_ARRAY_ZERO( coords + blend->num_axis, num_coords - blend->num_axis );
+ num_coords = blend->num_axis;
}
- for ( i = 0; i < nc; i++ )
+ for ( i = 0; i < num_coords; i++ )
coords[i] = mm_axis_unmap( &blend->design_map[i], axiscoords[i] );
- for ( ; i < num_coords; i++ )
- coords[i] = 0;
return FT_Err_Ok;
}
diff --git a/thirdparty/freetype/src/type1/t1parse.c b/thirdparty/freetype/src/type1/t1parse.c
index a9b8d18a6ff..73bfb0cdecc 100644
--- a/thirdparty/freetype/src/type1/t1parse.c
+++ b/thirdparty/freetype/src/type1/t1parse.c
@@ -268,33 +268,34 @@
if ( parser->in_pfb )
{
- /* in the case of the PFB format, the private dictionary can be */
- /* made of several segments. We thus first read the number of */
- /* segments to compute the total size of the private dictionary */
- /* then re-read them into memory. */
- FT_ULong start_pos = FT_STREAM_POS();
+ /* in the case of the PFB format, the private dictionary can be */
+ /* made of several adjacent segments. */
FT_UShort tag;
+ FT_Byte* priv_dict = NULL;
+ FT_ULong priv_len = 0;
- parser->private_len = 0;
for (;;)
{
error = read_pfb_tag( stream, &tag, &size );
if ( error )
- goto Fail;
+ break;
- if ( tag != 0x8002U )
+ if ( tag != 0x8002U || size > stream->size - stream->pos )
break;
- parser->private_len += size;
+ if ( FT_QREALLOC( priv_dict, priv_len, priv_len + size ) ||
+ FT_STREAM_READ( priv_dict + priv_len, size ) )
+ break;
- if ( FT_STREAM_SKIP( size ) )
- goto Fail;
+ priv_len += size;
}
+ parser->private_dict = priv_dict;
+ parser->private_len = priv_len;
+
/* Check that we have a private dictionary there */
- /* and allocate private dictionary buffer */
- if ( parser->private_len == 0 )
+ if ( priv_len == 0 )
{
FT_ERROR(( "T1_Get_Private_Dict:"
" invalid private dictionary section\n" ));
@@ -302,26 +303,8 @@
goto Fail;
}
- if ( FT_STREAM_SEEK( start_pos ) ||
- FT_QALLOC( parser->private_dict, parser->private_len ) )
- goto Fail;
-
- parser->private_len = 0;
- for (;;)
- {
- error = read_pfb_tag( stream, &tag, &size );
- if ( error || tag != 0x8002U )
- {
- error = FT_Err_Ok;
- break;
- }
-
- if ( FT_STREAM_READ( parser->private_dict + parser->private_len,
- size ) )
- goto Fail;
-
- parser->private_len += size;
- }
+ /* discard any errors from truncated files */
+ error = FT_Err_Ok;
}
else
{
@@ -339,11 +322,8 @@
FT_Bool test_cr;
- parser->root.cursor = parser->base_dict;
- parser->root.limit = parser->base_dict + parser->base_len;
-
- cur = parser->root.cursor;
- limit = parser->root.limit;
+ parser->root.cursor = cur;
+ parser->root.limit = limit;
while ( cur < limit )
{
diff --git a/thirdparty/freetype/src/winfonts/winfnt.c b/thirdparty/freetype/src/winfonts/winfnt.c
index 264185058ab..99f31f61f7b 100644
--- a/thirdparty/freetype/src/winfonts/winfnt.c
+++ b/thirdparty/freetype/src/winfonts/winfnt.c
@@ -479,6 +479,13 @@
FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &root_dir ) )
goto Exit;
+ if ( root_dir.number_of_id_entries >
+ 0xFFFF - root_dir.number_of_named_entries )
+ {
+ error = FT_THROW( Invalid_File_Format );
+ goto Exit;
+ }
+
root_dir_offset = pe32_section.pointer_to_raw_data;
for ( i = 0; i < root_dir.number_of_named_entries +
@@ -505,6 +512,13 @@
FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &name_dir ) )
goto Exit;
+ if ( name_dir.number_of_id_entries >
+ 0xFFFF - name_dir.number_of_named_entries )
+ {
+ error = FT_THROW( Invalid_File_Format );
+ goto Exit;
+ }
+
for ( j = 0; j < name_dir.number_of_named_entries +
name_dir.number_of_id_entries; j++ )
{
@@ -529,6 +543,13 @@
FT_STREAM_READ_FIELDS( winpe_rsrc_dir_fields, &lang_dir ) )
goto Exit;
+ if ( lang_dir.number_of_id_entries >
+ 0xFFFF - lang_dir.number_of_named_entries )
+ {
+ error = FT_THROW( Invalid_File_Format );
+ goto Exit;
+ }
+
for ( k = 0; k < lang_dir.number_of_named_entries +
lang_dir.number_of_id_entries; k++ )
{