0.2.0-dev Download ZIP
372 lines ·11.1 KB Raw
| 1 | /**************************************************************************** |
| 2 | * |
| 3 | * ttvarc.h |
| 4 | * |
| 5 | * TrueType and OpenType VARC (Variable Composites) support |
| 6 | * (specification). |
| 7 | * |
| 8 | * Copyright (C) 2025-2026 by |
| 9 | * Behdad Esfahbod, David Turner, Robert Wilhelm, and Werner Lemberg. |
| 10 | * |
| 11 | * This file is part of the FreeType project, and may only be used, |
| 12 | * modified, and distributed under the terms of the FreeType project |
| 13 | * license, LICENSE.TXT. By continuing to use, modify, or distribute |
| 14 | * this file you indicate that you have read the license and |
| 15 | * understand and accept it fully. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | #ifndef TTVARC_H_ |
| 21 | #define TTVARC_H_ |
| 22 | |
| 23 | |
| 24 | #include "ttobjs.h" |
| 25 | |
| 26 | #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT |
| 27 | #include <freetype/internal/ftmmtypes.h> |
| 28 | #endif |
| 29 | |
| 30 | |
| 31 | FT_BEGIN_HEADER |
| 32 | |
| 33 | |
| 34 | #ifdef TT_CONFIG_OPTION_VARC |
| 35 | |
| 36 | |
| 37 | FT_LOCAL( FT_Error ) |
| 38 | tt_face_init_varc_axes( FT_Face face, |
| 39 | FT_UInt axis_count ); |
| 40 | |
| 41 | |
| 42 | /************************************************************************** |
| 43 | * |
| 44 | * VARC component flags from the specification. |
| 45 | * |
| 46 | */ |
| 47 | |
| 48 | /* Variable Component Flags - from VARC spec. */ |
| 49 | #define VARC_RESET_UNSPECIFIED_AXES 0x00000001U /* Bit 0 */ |
| 50 | #define VARC_HAVE_AXES 0x00000002U /* Bit 1 */ |
| 51 | #define VARC_AXES_HAVE_VARIATION 0x00000004U /* Bit 2 */ |
| 52 | #define VARC_TRANSFORM_HAS_VARIATION 0x00000008U /* Bit 3 */ |
| 53 | #define VARC_HAVE_TRANSLATE_X 0x00000010U /* Bit 4 */ |
| 54 | #define VARC_HAVE_TRANSLATE_Y 0x00000020U /* Bit 5 */ |
| 55 | #define VARC_HAVE_ROTATION 0x00000040U /* Bit 6 */ |
| 56 | #define VARC_HAVE_CONDITION 0x00000080U /* Bit 7 */ |
| 57 | #define VARC_HAVE_SCALE_X 0x00000100U /* Bit 8 */ |
| 58 | #define VARC_HAVE_SCALE_Y 0x00000200U /* Bit 9 */ |
| 59 | #define VARC_HAVE_TCENTER_X 0x00000400U /* Bit 10 */ |
| 60 | #define VARC_HAVE_TCENTER_Y 0x00000800U /* Bit 11 */ |
| 61 | #define VARC_GID_IS_24BIT 0x00001000U /* Bit 12 */ |
| 62 | #define VARC_HAVE_SKEW_X 0x00002000U /* Bit 13 */ |
| 63 | #define VARC_HAVE_SKEW_Y 0x00004000U /* Bit 14 */ |
| 64 | |
| 65 | /* Reserved flags - bits 15-31 must be zero. */ |
| 66 | #define VARC_RESERVED_MASK 0xFFFF8000U |
| 67 | |
| 68 | |
| 69 | /************************************************************************** |
| 70 | * |
| 71 | * @Struct: |
| 72 | * TT_VarcComponentRec |
| 73 | * |
| 74 | * @Description: |
| 75 | * Represents a single component within a VARC glyph. |
| 76 | * |
| 77 | * @Fields: |
| 78 | * flags :: |
| 79 | * Component flags indicating which optional fields are present. |
| 80 | * |
| 81 | * gid :: |
| 82 | * The glyph ID of the component to be included. |
| 83 | * |
| 84 | * condition_index :: |
| 85 | * Index into the condition list (if `VARC_HAVE_CONDITION` is set). |
| 86 | * Component is only rendered if condition evaluates to true. |
| 87 | * |
| 88 | * axis_indices_index :: |
| 89 | * Index into the axis indices list (if `VARC_HAVE_AXES` is set). |
| 90 | * |
| 91 | * num_axis_values :: |
| 92 | * Number of axis values in the `axis_values` array. |
| 93 | * |
| 94 | * axis_values :: |
| 95 | * Array of normalized axis coordinates (-1.0 to +1.0 in F2DOT14). |
| 96 | * These override the current design space location for this component. |
| 97 | * |
| 98 | * axis_values_var_index :: |
| 99 | * Variation store index for axis values (if `VARC_AXES_HAVE_VARIATION` |
| 100 | * is set). |
| 101 | * |
| 102 | * transform_var_index :: |
| 103 | * Variation store index for transform values (if |
| 104 | * `VARC_TRANSFORM_HAS_VARIATION` is set). |
| 105 | * |
| 106 | * translate_x :: |
| 107 | * translate_y :: |
| 108 | * Translation amounts in font units (26.6 fixed-point). |
| 109 | * |
| 110 | * rotation :: |
| 111 | * Rotation angle in radians, multiplied by pi (16.16 fixed-point). |
| 112 | * So 0x10000 represents pi radians (180 degrees). |
| 113 | * |
| 114 | * scale_x :: |
| 115 | * scale_y :: |
| 116 | * Scale factors (16.16 fixed-point, 1.0 = 0x10000). |
| 117 | * |
| 118 | * skew_x :: |
| 119 | * skew_y :: |
| 120 | * Skew angles in radians, multiplied by pi (16.16 fixed-point). |
| 121 | * |
| 122 | * tcenter_x :: |
| 123 | * tcenter_y :: |
| 124 | * Transformation center point in font units (26.6 fixed-point). |
| 125 | * Transformations are applied relative to this point. |
| 126 | */ |
| 127 | typedef struct TT_VarcComponentRec_ |
| 128 | { |
| 129 | FT_UInt32 flags; |
| 130 | FT_UInt gid; |
| 131 | |
| 132 | /* conditional rendering */ |
| 133 | FT_UInt32 condition_index; |
| 134 | |
| 135 | /* axis value overrides */ |
| 136 | FT_UInt32 axis_indices_index; |
| 137 | FT_UInt num_axis_values; |
| 138 | FT_Fixed* axis_values; /* normalized coordinates (F2DOT14) */ |
| 139 | FT_Bool axis_values_on_heap; /* TRUE if axis_values needs `FT_FREE` */ |
| 140 | FT_UInt32 axis_values_var_index; |
| 141 | |
| 142 | /* transform variation */ |
| 143 | FT_UInt32 transform_var_index; |
| 144 | |
| 145 | /* transform components (all 26.6 or 16.16 fixed-point) */ |
| 146 | FT_Pos translate_x; |
| 147 | FT_Pos translate_y; |
| 148 | FT_Fixed rotation; /* radians * pi, 16.16 format */ |
| 149 | FT_Fixed scale_x; |
| 150 | FT_Fixed scale_y; |
| 151 | FT_Fixed skew_x; |
| 152 | FT_Fixed skew_y; |
| 153 | FT_Pos tcenter_x; |
| 154 | FT_Pos tcenter_y; |
| 155 | |
| 156 | } TT_VarcComponentRec, *TT_VarcComponent; |
| 157 | |
| 158 | |
| 159 | /************************************************************************** |
| 160 | * |
| 161 | * @Struct: |
| 162 | * TT_VarcRec |
| 163 | * |
| 164 | * @Description: |
| 165 | * The 'VARC' table structure, loaded from the font file. |
| 166 | * |
| 167 | * @Fields: |
| 168 | * version_major :: |
| 169 | * Major version of the 'VARC' table (currently 1). |
| 170 | * |
| 171 | * version_minor :: |
| 172 | * Minor version of the 'VARC' table (currently 0). |
| 173 | * |
| 174 | * coverage_offset :: |
| 175 | * Offset to the coverage table from start of 'VARC' table. |
| 176 | * |
| 177 | * multi_var_store_offset :: |
| 178 | * Offset to the `MultiItemVariationStore`. |
| 179 | * |
| 180 | * condition_list_offset :: |
| 181 | * Offset to the condition list. |
| 182 | * |
| 183 | * axis_indices_list_offset :: |
| 184 | * Offset to the axis indices list. |
| 185 | * |
| 186 | * var_composite_glyphs_offset :: |
| 187 | * Offset to the glyph records (CFF2-style index). |
| 188 | * |
| 189 | * coverage :: |
| 190 | * Pointer to the coverage table data. |
| 191 | * |
| 192 | * condition_list :: |
| 193 | * Pointer to the condition list data, or `NULL` if the table has no |
| 194 | * condition list. |
| 195 | * |
| 196 | * axis_indices_list :: |
| 197 | * Pointer to the CFF2-style index that contains the axis indices for |
| 198 | * component axis-value tuples, or `NULL` if the table has no axis |
| 199 | * indices list. |
| 200 | * |
| 201 | * var_composite_glyphs :: |
| 202 | * Pointer to the CFF2-style index that contains the component records |
| 203 | * for the glyphs referenced by `coverage`. |
| 204 | * |
| 205 | * multi_var_store :: |
| 206 | * The loaded `MultiItemVariationStore` for variation deltas. |
| 207 | * |
| 208 | * multi_var_store_loaded :: |
| 209 | * A boolean that indicates whether `multi_var_store` points to a |
| 210 | * `MultiItemVariationStore` in the table. |
| 211 | * |
| 212 | * table :: |
| 213 | * Pointer to the raw 'VARC' table data. |
| 214 | * |
| 215 | * table_size :: |
| 216 | * Size of the 'VARC' table in bytes. |
| 217 | */ |
| 218 | typedef struct TT_VarcRec_ |
| 219 | { |
| 220 | FT_UShort version_major; |
| 221 | FT_UShort version_minor; |
| 222 | |
| 223 | /* table offsets */ |
| 224 | FT_ULong coverage_offset; |
| 225 | FT_ULong multi_var_store_offset; |
| 226 | FT_ULong condition_list_offset; |
| 227 | FT_ULong axis_indices_list_offset; |
| 228 | FT_ULong var_composite_glyphs_offset; |
| 229 | |
| 230 | /* loaded data pointers */ |
| 231 | FT_Byte* coverage; |
| 232 | FT_Byte* condition_list; |
| 233 | FT_Byte* axis_indices_list; |
| 234 | FT_Byte* var_composite_glyphs; |
| 235 | |
| 236 | #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT |
| 237 | /* `MultiItemVariationStore` - stores tuples of deltas, */ |
| 238 | /* not single values */ |
| 239 | FT_Byte* multi_var_store; /* pointer to MultiItemVariationStore data */ |
| 240 | FT_Bool multi_var_store_loaded; |
| 241 | #endif |
| 242 | |
| 243 | /* raw table data */ |
| 244 | void* table; |
| 245 | FT_ULong table_size; |
| 246 | |
| 247 | } TT_VarcRec, *TT_Varc; |
| 248 | |
| 249 | |
| 250 | /************************************************************************** |
| 251 | * |
| 252 | * Maximum recursion depth for VARC glyph loading. |
| 253 | * This prevents stack overflow and infinite loops from malformed fonts. |
| 254 | */ |
| 255 | #define TT_VARC_MAX_NESTING_LEVEL 16 |
| 256 | |
| 257 | |
| 258 | /************************************************************************** |
| 259 | * |
| 260 | * @Struct: |
| 261 | * TT_VarcContextRec |
| 262 | * |
| 263 | * @Description: |
| 264 | * Context structure for recursive VARC glyph loading. |
| 265 | * Tracks recursion depth and visited glyphs for cycle detection. |
| 266 | * |
| 267 | * @Fields: |
| 268 | * recursion_depth :: |
| 269 | * Current depth in the recursion stack. |
| 270 | * |
| 271 | * visited_glyphs :: |
| 272 | * Array of glyph IDs currently being processed. |
| 273 | * Used for cycle detection to prevent infinite loops. |
| 274 | * |
| 275 | * parent_matrix :: |
| 276 | * The transformation matrix accumulated from parent components. |
| 277 | * |
| 278 | * parent_delta :: |
| 279 | * The translation accumulated from parent components. |
| 280 | * |
| 281 | * has_parent_transform :: |
| 282 | * A boolean that indicates whether `parent_matrix` and `parent_delta` |
| 283 | * contain a transform to apply to child components. |
| 284 | * |
| 285 | * font_coords :: |
| 286 | * The face's normalized variation coordinates before loading the VARC |
| 287 | * glyph. The context owns this array. |
| 288 | * |
| 289 | * num_font_coords :: |
| 290 | * The number of coordinates in `font_coords`. |
| 291 | * |
| 292 | * current_coords :: |
| 293 | * The normalized variation coordinates used to evaluate deltas at the |
| 294 | * current recursion level. This points to `font_coords` at the top |
| 295 | * level or to a component's temporary coordinate array at deeper |
| 296 | * levels; the context does not own the latter. |
| 297 | * |
| 298 | * num_corrent_coords :: |
| 299 | * The number of coordinates in `current_coords`. |
| 300 | */ |
| 301 | typedef struct TT_VarcContextRec_ |
| 302 | { |
| 303 | FT_UInt recursion_depth; |
| 304 | FT_UInt visited_glyphs[TT_VARC_MAX_NESTING_LEVEL]; |
| 305 | |
| 306 | /* Parent transform to compose with child transforms. */ |
| 307 | FT_Matrix parent_matrix; |
| 308 | FT_Vector parent_delta; |
| 309 | FT_Bool has_parent_transform; |
| 310 | |
| 311 | /* Font's original variation coordinates (before VARC processing). */ |
| 312 | /* Used when RESET_UNSPECIFIED_AXES is not set at depth 1. */ |
| 313 | FT_Fixed* font_coords; |
| 314 | FT_UInt num_font_coords; |
| 315 | |
| 316 | /* Current variation coords for delta evaluation at this recursion level. |
| 317 | * Points to `font_coords` at depth 1, or parent's coords at depth > 1. |
| 318 | * Not owned - do not free. */ |
| 319 | FT_Fixed* current_coords; |
| 320 | FT_UInt num_current_coords; |
| 321 | |
| 322 | /* Reusable buffers to avoid per-component allocations. */ |
| 323 | FT_Fixed* axis_values_buffer; |
| 324 | FT_UInt axis_values_buffer_size; |
| 325 | FT_Fixed* deltas_buffer; |
| 326 | FT_UInt deltas_buffer_size; |
| 327 | FT_Fixed* coords_buffer; |
| 328 | FT_UInt coords_buffer_size; |
| 329 | FT_UInt* indices_buffer; |
| 330 | FT_UInt indices_buffer_size; |
| 331 | |
| 332 | } TT_VarcContextRec, *TT_VarcContext; |
| 333 | |
| 334 | |
| 335 | /************************************************************************** |
| 336 | * |
| 337 | * VARC API functions |
| 338 | * |
| 339 | */ |
| 340 | |
| 341 | /* Load the VARC table from the font stream. */ |
| 342 | FT_LOCAL( FT_Error ) |
| 343 | tt_face_load_varc( FT_Face face, |
| 344 | FT_Stream stream ); |
| 345 | |
| 346 | /* Free the VARC table resources. */ |
| 347 | FT_LOCAL( void ) |
| 348 | tt_face_free_varc( FT_Face face ); |
| 349 | |
| 350 | /* Check whether a glyph has VARC data. */ |
| 351 | FT_LOCAL( FT_Bool ) |
| 352 | tt_face_has_varc_glyph( FT_Face face, |
| 353 | FT_UInt glyph_index ); |
| 354 | |
| 355 | /* Load a VARC glyph into the glyph slot. */ |
| 356 | FT_LOCAL( FT_Error ) |
| 357 | tt_face_load_varc_glyph( FT_Face face, |
| 358 | FT_GlyphSlot glyph_slot, |
| 359 | FT_UInt glyph_index, |
| 360 | FT_Int32 load_flags ); |
| 361 | |
| 362 | |
| 363 | #endif /* TT_CONFIG_OPTION_VARC */ |
| 364 | |
| 365 | |
| 366 | FT_END_HEADER |
| 367 | |
| 368 | |
| 369 | #endif /* TTVARC_H_ */ |
| 370 | |
| 371 | |
| 372 | /* END */ |