0.2.0-dev Download ZIP
1442 lines ·41.3 KB Raw
| 1 | /**************************************************************************** |
| 2 | * |
| 3 | * ttobjs.c |
| 4 | * |
| 5 | * Objects manager (body). |
| 6 | * |
| 7 | * Copyright (C) 1996-2026 by |
| 8 | * David Turner, Robert Wilhelm, and Werner Lemberg. |
| 9 | * |
| 10 | * This file is part of the FreeType project, and may only be used, |
| 11 | * modified, and distributed under the terms of the FreeType project |
| 12 | * license, LICENSE.TXT. By continuing to use, modify, or distribute |
| 13 | * this file you indicate that you have read the license and |
| 14 | * understand and accept it fully. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #include <freetype/internal/ftdebug.h> |
| 20 | #include <freetype/internal/ftstream.h> |
| 21 | #include <freetype/tttags.h> |
| 22 | #include <freetype/internal/sfnt.h> |
| 23 | #include <freetype/ftdriver.h> |
| 24 | #include <freetype/ftgasp.h> |
| 25 | |
| 26 | #include "ttgload.h" |
| 27 | #include "ttpload.h" |
| 28 | |
| 29 | #include "tterrors.h" |
| 30 | |
| 31 | #ifdef TT_USE_BYTECODE_INTERPRETER |
| 32 | #include "ttinterp.h" |
| 33 | #endif |
| 34 | |
| 35 | #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT |
| 36 | #include "ttgxvar.h" |
| 37 | #endif |
| 38 | |
| 39 | /************************************************************************** |
| 40 | * |
| 41 | * The macro FT_COMPONENT is used in trace mode. It is an implicit |
| 42 | * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log |
| 43 | * messages during execution. |
| 44 | */ |
| 45 | #undef FT_COMPONENT |
| 46 | #define FT_COMPONENT ttobjs |
| 47 | |
| 48 | |
| 49 | #ifdef TT_USE_BYTECODE_INTERPRETER |
| 50 | |
| 51 | /************************************************************************** |
| 52 | * |
| 53 | * GLYPH ZONE FUNCTIONS |
| 54 | * |
| 55 | */ |
| 56 | |
| 57 | |
| 58 | /************************************************************************** |
| 59 | * |
| 60 | * @Function: |
| 61 | * tt_glyphzone_done |
| 62 | * |
| 63 | * @Description: |
| 64 | * Deallocate a glyph zone. |
| 65 | * |
| 66 | * @Input: |
| 67 | * zone :: |
| 68 | * A pointer to the target glyph zone. |
| 69 | */ |
| 70 | FT_LOCAL_DEF( void ) |
| 71 | tt_glyphzone_done( FT_Memory memory, |
| 72 | TT_GlyphZone zone ) |
| 73 | { |
| 74 | FT_FREE( zone->org ); |
| 75 | |
| 76 | zone->n_points = 0; |
| 77 | zone->n_contours = 0; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /************************************************************************** |
| 82 | * |
| 83 | * @Function: |
| 84 | * tt_glyphzone_new |
| 85 | * |
| 86 | * @Description: |
| 87 | * Allocate a new glyph zone. |
| 88 | * |
| 89 | * @Input: |
| 90 | * memory :: |
| 91 | * A handle to the current memory object. |
| 92 | * |
| 93 | * maxPoints :: |
| 94 | * The capacity of glyph zone in points. |
| 95 | * |
| 96 | * maxContours :: |
| 97 | * The capacity of glyph zone in contours. |
| 98 | * |
| 99 | * @Output: |
| 100 | * zone :: |
| 101 | * A pointer to the target glyph zone record. |
| 102 | * |
| 103 | * @Return: |
| 104 | * FreeType error code. 0 means success. |
| 105 | */ |
| 106 | FT_LOCAL_DEF( FT_Error ) |
| 107 | tt_glyphzone_new( FT_Memory memory, |
| 108 | FT_UShort maxPoints, |
| 109 | FT_UShort maxContours, |
| 110 | TT_GlyphZone zone ) |
| 111 | { |
| 112 | FT_Error error; |
| 113 | FT_Long size = 3 * maxPoints * sizeof ( FT_Vector ) + |
| 114 | maxContours * sizeof ( FT_UShort ) + |
| 115 | maxPoints * sizeof ( FT_Byte ); |
| 116 | |
| 117 | |
| 118 | if ( !FT_ALLOC( zone->org, size ) ) |
| 119 | { |
| 120 | zone->n_points = maxPoints; |
| 121 | zone->n_contours = maxContours; |
| 122 | |
| 123 | zone->cur = zone->org + maxPoints; |
| 124 | zone->orus = zone->cur + maxPoints; |
| 125 | zone->contours = (FT_UShort*)( zone->orus + maxPoints ); |
| 126 | zone->tags = (FT_Byte*)( zone->contours + maxContours ); |
| 127 | |
| 128 | zone->first_point = 0; |
| 129 | } |
| 130 | |
| 131 | return error; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* |
| 136 | * Fonts embedded in PDFs are made unique by prepending randomization |
| 137 | * prefixes to their names: as defined in Section 5.5.3, 'Font Subsets', |
| 138 | * of the PDF Reference, they consist of 6 uppercase letters followed by |
| 139 | * the `+` sign. For safety, we do not skip prefixes violating this rule. |
| 140 | */ |
| 141 | |
| 142 | static const FT_String* |
| 143 | tt_skip_pdffont_random_tag( const FT_String* name ) |
| 144 | { |
| 145 | if ( ft_isupper( name[0] ) && |
| 146 | ft_isupper( name[1] ) && |
| 147 | ft_isupper( name[2] ) && |
| 148 | ft_isupper( name[3] ) && |
| 149 | ft_isupper( name[4] ) && |
| 150 | ft_isupper( name[5] ) && |
| 151 | '+' == name[6] && |
| 152 | name[7] ) |
| 153 | { |
| 154 | FT_TRACE7(( "name without randomization tag: %s\n", name + 7 )); |
| 155 | return name + 7; |
| 156 | } |
| 157 | |
| 158 | return name; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /* Compare the face with a list of well-known `tricky' fonts. */ |
| 163 | /* This list shall be expanded as we find more of them. */ |
| 164 | |
| 165 | static FT_Bool |
| 166 | tt_check_trickyness_family( const FT_String* name ) |
| 167 | { |
| 168 | |
| 169 | #define TRICK_NAMES_MAX_CHARACTERS 19 |
| 170 | #define TRICK_NAMES_COUNT 20 |
| 171 | |
| 172 | static const char trick_names[TRICK_NAMES_COUNT] |
| 173 | [TRICK_NAMES_MAX_CHARACTERS + 1] = |
| 174 | { |
| 175 | /* |
| 176 | PostScript names are given in brackets if they differ from the |
| 177 | family name. The version numbers, together with the copyright or |
| 178 | release year data, are taken from fonts available to the |
| 179 | developers. |
| 180 | |
| 181 | Note that later versions of the fonts might be no longer tricky; |
| 182 | for example, `MingLiU' version 7.00 (file `mingliu.ttc' from |
| 183 | Windows 7) is an ordinary TTC with non-tricky subfonts. |
| 184 | */ |
| 185 | |
| 186 | "cpop", /* dftt-p7.ttf; version 1.00, 1992 [DLJGyShoMedium] */ |
| 187 | "DFGirl-W6-WIN-BF", /* dftt-h6.ttf; version 1.00, 1993 */ |
| 188 | "DFGothic-EB", /* DynaLab Inc. 1992-1995 */ |
| 189 | "DFGyoSho-Lt", /* DynaLab Inc. 1992-1995 */ |
| 190 | "DFHei", /* DynaLab Inc. 1992-1995 [DFHei-Bd-WIN-HK-BF] */ |
| 191 | /* covers "DFHei-Md-HK-BF", maybe DynaLab Inc. */ |
| 192 | |
| 193 | "DFHSGothic-W5", /* DynaLab Inc. 1992-1995 */ |
| 194 | "DFHSMincho-W3", /* DynaLab Inc. 1992-1995 */ |
| 195 | "DFHSMincho-W7", /* DynaLab Inc. 1992-1995 */ |
| 196 | "DFKaiSho-SB", /* dfkaisb.ttf */ |
| 197 | "DFKaiShu", /* covers "DFKaiShu-Md-HK-BF", maybe DynaLab Inc. */ |
| 198 | "DFKai-SB", /* kaiu.ttf; version 3.00, 1998 [DFKaiShu-SB-Estd-BF] */ |
| 199 | |
| 200 | "DFMing", /* DynaLab Inc. 1992-1995 [DFMing-Md-WIN-HK-BF] */ |
| 201 | /* covers "DFMing-Bd-HK-BF", maybe DynaLab Inc. */ |
| 202 | |
| 203 | "DLC", /* dftt-m7.ttf; version 1.00, 1993 [DLCMingBold] */ |
| 204 | /* dftt-f5.ttf; version 1.00, 1993 [DLCFongSung] */ |
| 205 | /* covers following */ |
| 206 | /* "DLCHayMedium", dftt-b5.ttf; version 1.00, 1993 */ |
| 207 | /* "DLCHayBold", dftt-b7.ttf; version 1.00, 1993 */ |
| 208 | /* "DLCKaiMedium", dftt-k5.ttf; version 1.00, 1992 */ |
| 209 | /* "DLCLiShu", dftt-l5.ttf; version 1.00, 1992 */ |
| 210 | /* "DLCRoundBold", dftt-r7.ttf; version 1.00, 1993 */ |
| 211 | |
| 212 | "HuaTianKaiTi?", /* htkt2.ttf */ |
| 213 | "HuaTianSongTi?", /* htst3.ttf */ |
| 214 | "Ming(for ISO10646)", /* hkscsiic.ttf; version 0.12, 2007 [Ming] */ |
| 215 | /* iicore.ttf; version 0.07, 2007 [Ming] */ |
| 216 | "MingLiU", /* mingliu.ttf */ |
| 217 | /* mingliu.ttc; version 3.21, 2001 */ |
| 218 | "MingMedium", /* dftt-m5.ttf; version 1.00, 1993 [DLCMingMedium] */ |
| 219 | "PMingLiU", /* mingliu.ttc; version 3.21, 2001 */ |
| 220 | "MingLi43", /* mingli.ttf; version 1.00, 1992 */ |
| 221 | }; |
| 222 | |
| 223 | int nn; |
| 224 | const FT_String* name_without_tag; |
| 225 | |
| 226 | |
| 227 | name_without_tag = tt_skip_pdffont_random_tag( name ); |
| 228 | for ( nn = 0; nn < TRICK_NAMES_COUNT; nn++ ) |
| 229 | if ( ft_strstr( name_without_tag, trick_names[nn] ) ) |
| 230 | return TRUE; |
| 231 | |
| 232 | return FALSE; |
| 233 | } |
| 234 | |
| 235 | |
| 236 | /* XXX: This function should be in the `sfnt' module. */ |
| 237 | |
| 238 | /* Some PDF generators clear the checksums in the TrueType header table. */ |
| 239 | /* For example, Quartz ContextPDF clears all entries, or Bullzip PDF */ |
| 240 | /* Printer clears the entries for subsetted subtables. We thus have to */ |
| 241 | /* recalculate the checksums where necessary. */ |
| 242 | |
| 243 | static FT_UInt32 |
| 244 | tt_synth_sfnt_checksum( FT_Stream stream, |
| 245 | FT_ULong length ) |
| 246 | { |
| 247 | FT_Error error; |
| 248 | FT_UInt32 checksum = 0; |
| 249 | FT_Byte* p; |
| 250 | FT_Int shift; |
| 251 | |
| 252 | |
| 253 | if ( FT_FRAME_ENTER( length ) ) |
| 254 | return 0; |
| 255 | |
| 256 | p = (FT_Byte*)stream->cursor; |
| 257 | |
| 258 | for ( ; length > 3; length -= 4 ) |
| 259 | checksum += FT_NEXT_ULONG( p ); |
| 260 | |
| 261 | for ( shift = 24; length > 0; length--, shift -=8 ) |
| 262 | checksum += (FT_UInt32)FT_NEXT_BYTE( p ) << shift; |
| 263 | |
| 264 | FT_FRAME_EXIT(); |
| 265 | |
| 266 | return checksum; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | /* XXX: This function should be in the `sfnt' module. */ |
| 271 | |
| 272 | static FT_ULong |
| 273 | tt_get_sfnt_checksum( TT_Face face, |
| 274 | FT_UShort i ) |
| 275 | { |
| 276 | #if 0 /* if we believe the written value, use following part. */ |
| 277 | if ( face->dir_tables[i].CheckSum ) |
| 278 | return face->dir_tables[i].CheckSum; |
| 279 | #endif |
| 280 | |
| 281 | if ( !face->goto_table ) |
| 282 | return 0; |
| 283 | |
| 284 | if ( face->goto_table( face, |
| 285 | face->dir_tables[i].Tag, |
| 286 | face->root.stream, |
| 287 | NULL ) ) |
| 288 | return 0; |
| 289 | |
| 290 | return (FT_ULong)tt_synth_sfnt_checksum( face->root.stream, |
| 291 | face->dir_tables[i].Length ); |
| 292 | } |
| 293 | |
| 294 | |
| 295 | typedef struct tt_sfnt_id_rec_ |
| 296 | { |
| 297 | FT_ULong CheckSum; |
| 298 | FT_ULong Length; |
| 299 | |
| 300 | } tt_sfnt_id_rec; |
| 301 | |
| 302 | |
| 303 | static FT_Bool |
| 304 | tt_check_trickyness_sfnt_ids( TT_Face face ) |
| 305 | { |
| 306 | #define TRICK_SFNT_IDS_PER_FACE 3 |
| 307 | #define TRICK_SFNT_IDS_NUM_FACES 31 |
| 308 | |
| 309 | static const tt_sfnt_id_rec sfnt_id[TRICK_SFNT_IDS_NUM_FACES] |
| 310 | [TRICK_SFNT_IDS_PER_FACE] = |
| 311 | { |
| 312 | |
| 313 | #define TRICK_SFNT_ID_cvt 0 |
| 314 | #define TRICK_SFNT_ID_fpgm 1 |
| 315 | #define TRICK_SFNT_ID_prep 2 |
| 316 | |
| 317 | { /* MingLiU 1995 */ |
| 318 | { 0x05BCF058UL, 0x000002E4UL }, /* cvt */ |
| 319 | { 0x28233BF1UL, 0x000087C4UL }, /* fpgm */ |
| 320 | { 0xA344A1EAUL, 0x000001E1UL } /* prep */ |
| 321 | }, |
| 322 | { /* MingLiU 1996- */ |
| 323 | { 0x05BCF058UL, 0x000002E4UL }, /* cvt */ |
| 324 | { 0x28233BF1UL, 0x000087C4UL }, /* fpgm */ |
| 325 | { 0xA344A1EBUL, 0x000001E1UL } /* prep */ |
| 326 | }, |
| 327 | { /* DFGothic-EB */ |
| 328 | { 0x12C3EBB2UL, 0x00000350UL }, /* cvt */ |
| 329 | { 0xB680EE64UL, 0x000087A7UL }, /* fpgm */ |
| 330 | { 0xCE939563UL, 0x00000758UL } /* prep */ |
| 331 | }, |
| 332 | { /* DFGyoSho-Lt */ |
| 333 | { 0x11E5EAD4UL, 0x00000350UL }, /* cvt */ |
| 334 | { 0xCE5956E9UL, 0x0000BC85UL }, /* fpgm */ |
| 335 | { 0x8272F416UL, 0x00000045UL } /* prep */ |
| 336 | }, |
| 337 | { /* DFHei-Md-HK-BF */ |
| 338 | { 0x1257EB46UL, 0x00000350UL }, /* cvt */ |
| 339 | { 0xF699D160UL, 0x0000715FUL }, /* fpgm */ |
| 340 | { 0xD222F568UL, 0x000003BCUL } /* prep */ |
| 341 | }, |
| 342 | { /* DFHSGothic-W5 */ |
| 343 | { 0x1262EB4EUL, 0x00000350UL }, /* cvt */ |
| 344 | { 0xE86A5D64UL, 0x00007940UL }, /* fpgm */ |
| 345 | { 0x7850F729UL, 0x000005FFUL } /* prep */ |
| 346 | }, |
| 347 | { /* DFHSMincho-W3 */ |
| 348 | { 0x122DEB0AUL, 0x00000350UL }, /* cvt */ |
| 349 | { 0x3D16328AUL, 0x0000859BUL }, /* fpgm */ |
| 350 | { 0xA93FC33BUL, 0x000002CBUL } /* prep */ |
| 351 | }, |
| 352 | { /* DFHSMincho-W7 */ |
| 353 | { 0x125FEB26UL, 0x00000350UL }, /* cvt */ |
| 354 | { 0xA5ACC982UL, 0x00007EE1UL }, /* fpgm */ |
| 355 | { 0x90999196UL, 0x0000041FUL } /* prep */ |
| 356 | }, |
| 357 | { /* DFKaiShu */ |
| 358 | { 0x11E5EAD4UL, 0x00000350UL }, /* cvt */ |
| 359 | { 0x5A30CA3BUL, 0x00009063UL }, /* fpgm */ |
| 360 | { 0x13A42602UL, 0x0000007EUL } /* prep */ |
| 361 | }, |
| 362 | { /* DFKaiShu, variant */ |
| 363 | { 0x11E5EAD4UL, 0x00000350UL }, /* cvt */ |
| 364 | { 0xA6E78C01UL, 0x00008998UL }, /* fpgm */ |
| 365 | { 0x13A42602UL, 0x0000007EUL } /* prep */ |
| 366 | }, |
| 367 | { /* DFKaiShu-Md-HK-BF */ |
| 368 | { 0x11E5EAD4UL, 0x00000360UL }, /* cvt */ |
| 369 | { 0x9DB282B2UL, 0x0000C06EUL }, /* fpgm */ |
| 370 | { 0x53E6D7CAUL, 0x00000082UL } /* prep */ |
| 371 | }, |
| 372 | { /* DFMing-Bd-HK-BF */ |
| 373 | { 0x1243EB18UL, 0x00000350UL }, /* cvt */ |
| 374 | { 0xBA0A8C30UL, 0x000074ADUL }, /* fpgm */ |
| 375 | { 0xF3D83409UL, 0x0000037BUL } /* prep */ |
| 376 | }, |
| 377 | { /* DLCLiShu */ |
| 378 | { 0x07DCF546UL, 0x00000308UL }, /* cvt */ |
| 379 | { 0x40FE7C90UL, 0x00008E2AUL }, /* fpgm */ |
| 380 | { 0x608174B5UL, 0x0000007AUL } /* prep */ |
| 381 | }, |
| 382 | { /* DLCHayBold */ |
| 383 | { 0xEB891238UL, 0x00000308UL }, /* cvt */ |
| 384 | { 0xD2E4DCD4UL, 0x0000676FUL }, /* fpgm */ |
| 385 | { 0x8EA5F293UL, 0x000003B8UL } /* prep */ |
| 386 | }, |
| 387 | { /* HuaTianKaiTi */ |
| 388 | { 0xFFFBFFFCUL, 0x00000008UL }, /* cvt */ |
| 389 | { 0x9C9E48B8UL, 0x0000BEA2UL }, /* fpgm */ |
| 390 | { 0x70020112UL, 0x00000008UL } /* prep */ |
| 391 | }, |
| 392 | { /* HuaTianSongTi */ |
| 393 | { 0xFFFBFFFCUL, 0x00000008UL }, /* cvt */ |
| 394 | { 0x0A5A0483UL, 0x00017C39UL }, /* fpgm */ |
| 395 | { 0x70020112UL, 0x00000008UL } /* prep */ |
| 396 | }, |
| 397 | { /* NEC fadpop7.ttf */ |
| 398 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 399 | { 0x40C92555UL, 0x000000E5UL }, /* fpgm */ |
| 400 | { 0xA39B58E3UL, 0x0000117CUL } /* prep */ |
| 401 | }, |
| 402 | { /* NEC fadrei5.ttf */ |
| 403 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 404 | { 0x33C41652UL, 0x000000E5UL }, /* fpgm */ |
| 405 | { 0x26D6C52AUL, 0x00000F6AUL } /* prep */ |
| 406 | }, |
| 407 | { /* NEC fangot7.ttf */ |
| 408 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 409 | { 0x6DB1651DUL, 0x0000019DUL }, /* fpgm */ |
| 410 | { 0x6C6E4B03UL, 0x00002492UL } /* prep */ |
| 411 | }, |
| 412 | { /* NEC fangyo5.ttf */ |
| 413 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 414 | { 0x40C92555UL, 0x000000E5UL }, /* fpgm */ |
| 415 | { 0xDE51FAD0UL, 0x0000117CUL } /* prep */ |
| 416 | }, |
| 417 | { /* NEC fankyo5.ttf */ |
| 418 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 419 | { 0x85E47664UL, 0x000000E5UL }, /* fpgm */ |
| 420 | { 0xA6C62831UL, 0x00001CAAUL } /* prep */ |
| 421 | }, |
| 422 | { /* NEC fanrgo5.ttf */ |
| 423 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 424 | { 0x2D891CFDUL, 0x0000019DUL }, /* fpgm */ |
| 425 | { 0xA0604633UL, 0x00001DE8UL } /* prep */ |
| 426 | }, |
| 427 | { /* NEC fangot5.ttc */ |
| 428 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 429 | { 0x40AA774CUL, 0x000001CBUL }, /* fpgm */ |
| 430 | { 0x9B5CAA96UL, 0x00001F9AUL } /* prep */ |
| 431 | }, |
| 432 | { /* NEC fanmin3.ttc */ |
| 433 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 434 | { 0x0D3DE9CBUL, 0x00000141UL }, /* fpgm */ |
| 435 | { 0xD4127766UL, 0x00002280UL } /* prep */ |
| 436 | }, |
| 437 | { /* NEC FA-Gothic, 1996 */ |
| 438 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 439 | { 0x4A692698UL, 0x000001F0UL }, /* fpgm */ |
| 440 | { 0x340D4346UL, 0x00001FCAUL } /* prep */ |
| 441 | }, |
| 442 | { /* NEC FA-Minchou, 1996 */ |
| 443 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 444 | { 0xCD34C604UL, 0x00000166UL }, /* fpgm */ |
| 445 | { 0x6CF31046UL, 0x000022B0UL } /* prep */ |
| 446 | }, |
| 447 | { /* NEC FA-RoundGothicB, 1996 */ |
| 448 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 449 | { 0x5DA75315UL, 0x0000019DUL }, /* fpgm */ |
| 450 | { 0x40745A5FUL, 0x000022E0UL } /* prep */ |
| 451 | }, |
| 452 | { /* NEC FA-RoundGothicM, 1996 */ |
| 453 | { 0x00000000UL, 0x00000000UL }, /* cvt */ |
| 454 | { 0xF055FC48UL, 0x000001C2UL }, /* fpgm */ |
| 455 | { 0x3900DED3UL, 0x00001E18UL } /* prep */ |
| 456 | }, |
| 457 | { /* MINGLI.TTF, 1992 */ |
| 458 | { 0x00170003UL, 0x00000060UL }, /* cvt */ |
| 459 | { 0xDBB4306EUL, 0x000058AAUL }, /* fpgm */ |
| 460 | { 0xD643482AUL, 0x00000035UL } /* prep */ |
| 461 | }, |
| 462 | { /* DFHei-Bd-WIN-HK-BF, issue #1087 */ |
| 463 | { 0x1269EB58UL, 0x00000350UL }, /* cvt */ |
| 464 | { 0x5CD5957AUL, 0x00006A4EUL }, /* fpgm */ |
| 465 | { 0xF758323AUL, 0x00000380UL } /* prep */ |
| 466 | }, |
| 467 | { /* DFMing-Md-WIN-HK-BF, issue #1087 */ |
| 468 | { 0x122FEB0BUL, 0x00000350UL }, /* cvt */ |
| 469 | { 0x7F10919AUL, 0x000070A9UL }, /* fpgm */ |
| 470 | { 0x7CD7E7B7UL, 0x0000025CUL } /* prep */ |
| 471 | } |
| 472 | }; |
| 473 | |
| 474 | FT_ULong checksum; |
| 475 | int num_matched_ids[TRICK_SFNT_IDS_NUM_FACES]; |
| 476 | FT_Bool has_cvt, has_fpgm, has_prep; |
| 477 | FT_UShort i; |
| 478 | int j, k; |
| 479 | |
| 480 | |
| 481 | FT_ARRAY_ZERO( num_matched_ids, TRICK_SFNT_IDS_NUM_FACES ); |
| 482 | has_cvt = FALSE; |
| 483 | has_fpgm = FALSE; |
| 484 | has_prep = FALSE; |
| 485 | |
| 486 | for ( i = 0; i < face->num_tables; i++ ) |
| 487 | { |
| 488 | checksum = 0; |
| 489 | |
| 490 | switch( face->dir_tables[i].Tag ) |
| 491 | { |
| 492 | case TTAG_cvt: |
| 493 | k = TRICK_SFNT_ID_cvt; |
| 494 | has_cvt = TRUE; |
| 495 | break; |
| 496 | |
| 497 | case TTAG_fpgm: |
| 498 | k = TRICK_SFNT_ID_fpgm; |
| 499 | has_fpgm = TRUE; |
| 500 | break; |
| 501 | |
| 502 | case TTAG_prep: |
| 503 | k = TRICK_SFNT_ID_prep; |
| 504 | has_prep = TRUE; |
| 505 | break; |
| 506 | |
| 507 | default: |
| 508 | continue; |
| 509 | } |
| 510 | |
| 511 | for ( j = 0; j < TRICK_SFNT_IDS_NUM_FACES; j++ ) |
| 512 | if ( face->dir_tables[i].Length == sfnt_id[j][k].Length ) |
| 513 | { |
| 514 | if ( !checksum ) |
| 515 | checksum = tt_get_sfnt_checksum( face, i ); |
| 516 | |
| 517 | if ( sfnt_id[j][k].CheckSum == checksum ) |
| 518 | num_matched_ids[j]++; |
| 519 | |
| 520 | if ( num_matched_ids[j] == TRICK_SFNT_IDS_PER_FACE ) |
| 521 | return TRUE; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | for ( j = 0; j < TRICK_SFNT_IDS_NUM_FACES; j++ ) |
| 526 | { |
| 527 | if ( !has_cvt && !sfnt_id[j][TRICK_SFNT_ID_cvt].Length ) |
| 528 | num_matched_ids[j]++; |
| 529 | if ( !has_fpgm && !sfnt_id[j][TRICK_SFNT_ID_fpgm].Length ) |
| 530 | num_matched_ids[j]++; |
| 531 | if ( !has_prep && !sfnt_id[j][TRICK_SFNT_ID_prep].Length ) |
| 532 | num_matched_ids[j]++; |
| 533 | if ( num_matched_ids[j] == TRICK_SFNT_IDS_PER_FACE ) |
| 534 | return TRUE; |
| 535 | } |
| 536 | |
| 537 | return FALSE; |
| 538 | } |
| 539 | |
| 540 | |
| 541 | static FT_Bool |
| 542 | tt_check_trickyness( FT_Face face ) |
| 543 | { |
| 544 | if ( !face ) |
| 545 | return FALSE; |
| 546 | |
| 547 | /* For first, check the face name for quick check. */ |
| 548 | if ( face->family_name && |
| 549 | tt_check_trickyness_family( face->family_name ) ) |
| 550 | { |
| 551 | FT_TRACE3(( "found as a tricky font" |
| 552 | " by its family name: %s\n", face->family_name )); |
| 553 | return TRUE; |
| 554 | } |
| 555 | |
| 556 | /* Type42 fonts may lack `name' tables, we thus try to identify */ |
| 557 | /* tricky fonts by checking the checksums of Type42-persistent */ |
| 558 | /* sfnt tables (`cvt', `fpgm', and `prep'). */ |
| 559 | if ( tt_check_trickyness_sfnt_ids( (TT_Face)face ) ) |
| 560 | { |
| 561 | FT_TRACE3(( "found as a tricky font" |
| 562 | " by its cvt/fpgm/prep table checksum\n" )); |
| 563 | return TRUE; |
| 564 | } |
| 565 | |
| 566 | return FALSE; |
| 567 | } |
| 568 | |
| 569 | #endif /* TT_USE_BYTECODE_INTERPRETER */ |
| 570 | |
| 571 | |
| 572 | /* Check whether `.notdef' is the only glyph in the `loca' table. */ |
| 573 | static FT_Bool |
| 574 | tt_check_single_notdef( FT_Face ttface ) |
| 575 | { |
| 576 | FT_Bool result = FALSE; |
| 577 | |
| 578 | TT_Face face = (TT_Face)ttface; |
| 579 | FT_ULong asize; |
| 580 | FT_ULong i; |
| 581 | FT_ULong glyph_index = 0; |
| 582 | FT_UInt count = 0; |
| 583 | |
| 584 | |
| 585 | for( i = 0; i < face->num_locations; i++ ) |
| 586 | { |
| 587 | tt_face_get_location( ttface, i, &asize ); |
| 588 | if ( asize > 0 ) |
| 589 | { |
| 590 | count += 1; |
| 591 | if ( count > 1 ) |
| 592 | break; |
| 593 | glyph_index = i; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | /* Only have a single outline. */ |
| 598 | if ( count == 1 ) |
| 599 | { |
| 600 | if ( glyph_index == 0 ) |
| 601 | result = TRUE; |
| 602 | else |
| 603 | { |
| 604 | /* FIXME: Need to test glyphname == .notdef ? */ |
| 605 | FT_Error error; |
| 606 | char buf[8]; |
| 607 | |
| 608 | |
| 609 | error = FT_Get_Glyph_Name( ttface, glyph_index, buf, 8 ); |
| 610 | if ( !error && |
| 611 | buf[0] == '.' && !ft_strncmp( buf, ".notdef", 8 ) ) |
| 612 | result = TRUE; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | return result; |
| 617 | } |
| 618 | |
| 619 | |
| 620 | /************************************************************************** |
| 621 | * |
| 622 | * @Function: |
| 623 | * tt_face_init |
| 624 | * |
| 625 | * @Description: |
| 626 | * Initialize a given TrueType face object. |
| 627 | * |
| 628 | * @Input: |
| 629 | * stream :: |
| 630 | * The source font stream. |
| 631 | * |
| 632 | * face_index :: |
| 633 | * The index of the TrueType font, if we are opening a |
| 634 | * collection, in bits 0-15. The numbered instance |
| 635 | * index~+~1 of a GX (sub)font, if applicable, in bits |
| 636 | * 16-30. |
| 637 | * |
| 638 | * num_params :: |
| 639 | * Number of additional generic parameters. Ignored. |
| 640 | * |
| 641 | * params :: |
| 642 | * Additional generic parameters. Ignored. |
| 643 | * |
| 644 | * @InOut: |
| 645 | * face :: |
| 646 | * The newly built face object. |
| 647 | * |
| 648 | * @Return: |
| 649 | * FreeType error code. 0 means success. |
| 650 | */ |
| 651 | FT_LOCAL_DEF( FT_Error ) |
| 652 | tt_face_init( FT_Stream stream, |
| 653 | FT_Face ttface, /* TT_Face */ |
| 654 | FT_Int face_index, |
| 655 | FT_Int num_params, |
| 656 | FT_Parameter* params ) |
| 657 | { |
| 658 | FT_Error error; |
| 659 | FT_Library library; |
| 660 | SFNT_Service sfnt; |
| 661 | TT_Face face = (TT_Face)ttface; |
| 662 | |
| 663 | |
| 664 | FT_TRACE2(( "TTF driver\n" )); |
| 665 | |
| 666 | library = ttface->driver->root.library; |
| 667 | |
| 668 | sfnt = (SFNT_Service)FT_Get_Module_Interface( library, "sfnt" ); |
| 669 | if ( !sfnt ) |
| 670 | { |
| 671 | FT_ERROR(( "tt_face_init: cannot access `sfnt' module\n" )); |
| 672 | error = FT_THROW( Missing_Module ); |
| 673 | goto Exit; |
| 674 | } |
| 675 | |
| 676 | /* create input stream from resource */ |
| 677 | if ( FT_STREAM_SEEK( 0 ) ) |
| 678 | goto Exit; |
| 679 | |
| 680 | /* check that we have a valid TrueType file */ |
| 681 | FT_TRACE2(( " " )); |
| 682 | error = sfnt->init_face( stream, face, face_index, num_params, params ); |
| 683 | |
| 684 | /* Stream may have changed. */ |
| 685 | stream = face->root.stream; |
| 686 | |
| 687 | if ( error ) |
| 688 | goto Exit; |
| 689 | |
| 690 | /* We must also be able to accept Mac/GX fonts, as well as OT ones. */ |
| 691 | /* The 0x00020000 tag is completely undocumented; some fonts from */ |
| 692 | /* Arphic made for Chinese Windows 3.1 have this. */ |
| 693 | if ( face->format_tag != 0x00010000L && /* MS fonts */ |
| 694 | face->format_tag != 0x00020000L && /* CJK fonts for Win 3.1 */ |
| 695 | face->format_tag != TTAG_true && /* Mac fonts */ |
| 696 | face->format_tag != TTAG_0xA5kbd && /* `Keyboard.dfont' (legacy Mac OS X) */ |
| 697 | face->format_tag != TTAG_0xA5lst ) /* `LastResort.dfont' (legacy Mac OS X) */ |
| 698 | { |
| 699 | FT_TRACE2(( " not a TTF font\n" )); |
| 700 | goto Bad_Format; |
| 701 | } |
| 702 | |
| 703 | #ifdef TT_USE_BYTECODE_INTERPRETER |
| 704 | ttface->face_flags |= FT_FACE_FLAG_HINTER; |
| 705 | #endif |
| 706 | |
| 707 | /* If we are performing a simple font format check, exit immediately. */ |
| 708 | if ( face_index < 0 ) |
| 709 | return FT_Err_Ok; |
| 710 | |
| 711 | /* Load font directory */ |
| 712 | error = sfnt->load_face( stream, face, face_index, num_params, params ); |
| 713 | if ( error ) |
| 714 | goto Exit; |
| 715 | |
| 716 | #ifdef TT_USE_BYTECODE_INTERPRETER |
| 717 | if ( tt_check_trickyness( ttface ) ) |
| 718 | ttface->face_flags |= FT_FACE_FLAG_TRICKY; |
| 719 | #endif |
| 720 | |
| 721 | error = tt_face_load_hdmx( face, stream ); |
| 722 | if ( error ) |
| 723 | goto Exit; |
| 724 | |
| 725 | if ( FT_IS_SCALABLE( ttface ) || |
| 726 | FT_HAS_SBIX( ttface ) ) |
| 727 | { |
| 728 | #ifdef FT_CONFIG_OPTION_INCREMENTAL |
| 729 | if ( !ttface->internal->incremental_interface ) |
| 730 | #endif |
| 731 | { |
| 732 | error = tt_face_load_loca( face, stream ); |
| 733 | |
| 734 | /* having a (non-zero) `glyf' table without */ |
| 735 | /* a `loca' table is not valid */ |
| 736 | if ( face->glyf_len && FT_ERR_EQ( error, Table_Missing ) ) |
| 737 | goto Exit; |
| 738 | /* if both `glyf' and `loca' tables are missing, */ |
| 739 | /* we don't have a valid font file either */ |
| 740 | if ( face->glyf_len == 0 && FT_ERR_EQ( error, Locations_Missing ) ) |
| 741 | goto Bad_Format; |
| 742 | if ( error ) |
| 743 | goto Exit; |
| 744 | } |
| 745 | |
| 746 | /* `fpgm', `cvt', and `prep' are optional */ |
| 747 | error = tt_face_load_cvt( face, stream ); |
| 748 | if ( error && FT_ERR_NEQ( error, Table_Missing ) ) |
| 749 | goto Exit; |
| 750 | |
| 751 | error = tt_face_load_fpgm( face, stream ); |
| 752 | if ( error && FT_ERR_NEQ( error, Table_Missing ) ) |
| 753 | goto Exit; |
| 754 | |
| 755 | error = tt_face_load_prep( face, stream ); |
| 756 | if ( error && FT_ERR_NEQ( error, Table_Missing ) ) |
| 757 | goto Exit; |
| 758 | |
| 759 | /* Check the scalable flag based on `loca'. */ |
| 760 | #ifdef FT_CONFIG_OPTION_INCREMENTAL |
| 761 | if ( !ttface->internal->incremental_interface ) |
| 762 | #endif |
| 763 | { |
| 764 | if ( ttface->num_fixed_sizes && |
| 765 | face->glyph_locations && |
| 766 | tt_check_single_notdef( ttface ) ) |
| 767 | { |
| 768 | FT_TRACE5(( "tt_face_init:" |
| 769 | " Only the `.notdef' glyph has an outline.\n" )); |
| 770 | FT_TRACE5(( " " |
| 771 | " Resetting scalable flag to FALSE.\n" )); |
| 772 | |
| 773 | ttface->face_flags &= ~FT_FACE_FLAG_SCALABLE; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT |
| 779 | { |
| 780 | FT_UInt instance_index = (FT_UInt)face_index >> 16; |
| 781 | |
| 782 | |
| 783 | if ( instance_index && FT_HAS_MULTIPLE_MASTERS( ttface ) ) |
| 784 | { |
| 785 | error = FT_Set_Named_Instance( ttface, instance_index ); |
| 786 | if ( error ) |
| 787 | goto Exit; |
| 788 | } |
| 789 | } |
| 790 | #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */ |
| 791 | |
| 792 | /* initialize standard glyph loading routines */ |
| 793 | TT_Init_Glyph_Loading( face ); |
| 794 | |
| 795 | Exit: |
| 796 | return error; |
| 797 | |
| 798 | Bad_Format: |
| 799 | error = FT_THROW( Unknown_File_Format ); |
| 800 | goto Exit; |
| 801 | } |
| 802 | |
| 803 | |
| 804 | /************************************************************************** |
| 805 | * |
| 806 | * @Function: |
| 807 | * tt_face_done |
| 808 | * |
| 809 | * @Description: |
| 810 | * Finalize a given face object. |
| 811 | * |
| 812 | * @Input: |
| 813 | * face :: |
| 814 | * A pointer to the face object to destroy. |
| 815 | */ |
| 816 | FT_LOCAL_DEF( void ) |
| 817 | tt_face_done( FT_Face ttface ) /* TT_Face */ |
| 818 | { |
| 819 | TT_Face face = (TT_Face)ttface; |
| 820 | FT_Memory memory; |
| 821 | FT_Stream stream; |
| 822 | SFNT_Service sfnt; |
| 823 | |
| 824 | |
| 825 | if ( !face ) |
| 826 | return; |
| 827 | |
| 828 | memory = ttface->memory; |
| 829 | stream = ttface->stream; |
| 830 | sfnt = (SFNT_Service)face->sfnt; |
| 831 | |
| 832 | /* for `extended TrueType formats' (i.e. compressed versions) */ |
| 833 | if ( face->extra.finalizer ) |
| 834 | face->extra.finalizer( face->extra.data ); |
| 835 | |
| 836 | if ( sfnt ) |
| 837 | sfnt->done_face( face ); |
| 838 | |
| 839 | /* freeing the locations table */ |
| 840 | tt_face_done_loca( face ); |
| 841 | |
| 842 | tt_face_free_hdmx( face ); |
| 843 | |
| 844 | /* freeing the CVT */ |
| 845 | FT_FREE( face->cvt ); |
| 846 | face->cvt_size = 0; |
| 847 | |
| 848 | /* freeing the programs */ |
| 849 | FT_FRAME_RELEASE( face->font_program ); |
| 850 | FT_FRAME_RELEASE( face->cvt_program ); |
| 851 | face->font_program_size = 0; |
| 852 | face->cvt_program_size = 0; |
| 853 | |
| 854 | #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT |
| 855 | tt_done_blend( ttface ); |
| 856 | #endif |
| 857 | } |
| 858 | |
| 859 | |
| 860 | #ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL |
| 861 | |
| 862 | FT_LOCAL_DEF( void ) |
| 863 | tt_face_get_cleartype_policy( TT_Face face, |
| 864 | FT_UInt ppem, |
| 865 | FT_Bool* symmetric_smoothing, |
| 866 | FT_Bool* grid_fit ) |
| 867 | { |
| 868 | FT_Bool symmetric; |
| 869 | FT_Bool fit; |
| 870 | FT_Bool found = FALSE; |
| 871 | FT_UShort flags = 0; |
| 872 | FT_UInt i; |
| 873 | |
| 874 | |
| 875 | /* |
| 876 | * DirectWrite fallback for hinted fonts without an applicable |
| 877 | * version-1 ClearType 'gasp' range: |
| 878 | * |
| 879 | * <= 20 ppem NATURAL |
| 880 | * > 20 ppem NATURAL_SYMMETRIC |
| 881 | * |
| 882 | * Fonts without glyph bytecode use symmetric rendering. |
| 883 | * |
| 884 | * `maxSizeOfInstructions` is also what Skia uses to determine |
| 885 | * whether a TrueType face is hinted for this DirectWrite policy. |
| 886 | */ |
| 887 | fit = FT_BOOL( face->max_profile.maxSizeOfInstructions != 0 ); |
| 888 | |
| 889 | symmetric = FT_BOOL( !fit || ppem > 20 ); |
| 890 | |
| 891 | /* |
| 892 | * A valid version-1 `gasp' range explicitly controls the two |
| 893 | * independent ClearType properties. |
| 894 | */ |
| 895 | if ( face->gasp.version >= 1 ) |
| 896 | { |
| 897 | for ( i = 0; i < face->gasp.numRanges; i++ ) |
| 898 | { |
| 899 | if ( ppem <= face->gasp.gaspRanges[i].maxPPEM ) |
| 900 | { |
| 901 | flags = face->gasp.gaspRanges[i].gaspFlag; |
| 902 | found = TRUE; |
| 903 | break; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | if ( found ) |
| 908 | { |
| 909 | symmetric = FT_BOOL( flags & FT_GASP_SYMMETRIC_SMOOTHING ); |
| 910 | fit = FT_BOOL( flags & FT_GASP_SYMMETRIC_GRIDFIT ); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | if ( symmetric_smoothing ) |
| 915 | *symmetric_smoothing = symmetric; |
| 916 | |
| 917 | if ( grid_fit ) |
| 918 | *grid_fit = fit; |
| 919 | } |
| 920 | |
| 921 | #endif |
| 922 | |
| 923 | |
| 924 | /************************************************************************** |
| 925 | * |
| 926 | * SIZE FUNCTIONS |
| 927 | * |
| 928 | */ |
| 929 | |
| 930 | #ifdef TT_USE_BYTECODE_INTERPRETER |
| 931 | |
| 932 | /************************************************************************** |
| 933 | * |
| 934 | * @Function: |
| 935 | * tt_size_run_fpgm |
| 936 | * |
| 937 | * @Description: |
| 938 | * Run the font program. |
| 939 | * |
| 940 | * @Input: |
| 941 | * size :: |
| 942 | * A handle to the size object. |
| 943 | * |
| 944 | * @Return: |
| 945 | * FreeType error code. 0 means success. |
| 946 | */ |
| 947 | FT_LOCAL_DEF( FT_Error ) |
| 948 | tt_size_run_fpgm( TT_Size size ) |
| 949 | { |
| 950 | TT_Face face = (TT_Face)size->root.face; |
| 951 | TT_ExecContext exec = size->context; |
| 952 | FT_Error error; |
| 953 | |
| 954 | |
| 955 | TT_Load_Context( exec, face, size ); |
| 956 | |
| 957 | /* disable CVT and glyph programs coderange */ |
| 958 | TT_Clear_CodeRange( exec, tt_coderange_cvt ); |
| 959 | TT_Clear_CodeRange( exec, tt_coderange_glyph ); |
| 960 | |
| 961 | if ( face->font_program_size > 0 ) |
| 962 | { |
| 963 | /* allow font program execution */ |
| 964 | TT_Set_CodeRange( exec, |
| 965 | tt_coderange_font, |
| 966 | face->font_program, |
| 967 | (FT_Long)face->font_program_size ); |
| 968 | |
| 969 | exec->pts.n_points = 0; |
| 970 | exec->pts.n_contours = 0; |
| 971 | |
| 972 | FT_TRACE4(( "Executing `fpgm' table.\n" )); |
| 973 | error = TT_Run_Context( exec, size ); |
| 974 | FT_TRACE4(( error ? " failed (error code 0x%x)\n" : "", |
| 975 | error )); |
| 976 | |
| 977 | if ( !error ) |
| 978 | TT_Save_Context( exec, size ); |
| 979 | } |
| 980 | else |
| 981 | error = FT_Err_Ok; |
| 982 | |
| 983 | size->bytecode_ready = error; |
| 984 | |
| 985 | return error; |
| 986 | } |
| 987 | |
| 988 | |
| 989 | /************************************************************************** |
| 990 | * |
| 991 | * @Function: |
| 992 | * tt_size_run_prep |
| 993 | * |
| 994 | * @Description: |
| 995 | * Run the control value program. |
| 996 | * |
| 997 | * @Input: |
| 998 | * size :: |
| 999 | * A handle to the size object. |
| 1000 | * |
| 1001 | * @Return: |
| 1002 | * FreeType error code. 0 means success. |
| 1003 | */ |
| 1004 | FT_LOCAL_DEF( FT_Error ) |
| 1005 | tt_size_run_prep( TT_Size size ) |
| 1006 | { |
| 1007 | TT_Face face = (TT_Face)size->root.face; |
| 1008 | TT_ExecContext exec = size->context; |
| 1009 | FT_Error error; |
| 1010 | FT_UInt i; |
| 1011 | |
| 1012 | |
| 1013 | /* set default GS, twilight points, and storage */ |
| 1014 | /* before CV program can modify them. */ |
| 1015 | size->GS = tt_default_graphics_state; |
| 1016 | |
| 1017 | /* all twilight points are originally zero */ |
| 1018 | FT_ARRAY_ZERO( size->twilight.org, size->twilight.n_points ); |
| 1019 | FT_ARRAY_ZERO( size->twilight.cur, size->twilight.n_points ); |
| 1020 | |
| 1021 | TT_Load_Context( exec, face, size ); |
| 1022 | |
| 1023 | /* clear storage area */ |
| 1024 | FT_ARRAY_ZERO( exec->storage, exec->storeSize ); |
| 1025 | |
| 1026 | /* Scale the cvt values to the new ppem. */ |
| 1027 | /* By default, we use the y ppem value for scaling. */ |
| 1028 | FT_TRACE6(( "CVT values:\n" )); |
| 1029 | for ( i = 0; i < exec->cvtSize; i++ ) |
| 1030 | { |
| 1031 | /* Unscaled CVT values are already stored in 26.6 format. */ |
| 1032 | /* Note that this scaling operation is very sensitive to rounding; */ |
| 1033 | /* the integer division by 64 must be applied to the first argument. */ |
| 1034 | exec->cvt[i] = FT_MulFix( face->cvt[i] / 64, size->ttmetrics.scale ); |
| 1035 | FT_TRACE6(( " %3u: %f (%f)\n", |
| 1036 | i, (double)face->cvt[i] / 64, (double)exec->cvt[i] / 64 )); |
| 1037 | } |
| 1038 | FT_TRACE6(( "\n" )); |
| 1039 | |
| 1040 | TT_Clear_CodeRange( exec, tt_coderange_glyph ); |
| 1041 | |
| 1042 | if ( face->cvt_program_size > 0 ) |
| 1043 | { |
| 1044 | /* allow CV program execution */ |
| 1045 | TT_Set_CodeRange( exec, |
| 1046 | tt_coderange_cvt, |
| 1047 | face->cvt_program, |
| 1048 | (FT_Long)face->cvt_program_size ); |
| 1049 | |
| 1050 | exec->pts.n_points = 0; |
| 1051 | exec->pts.n_contours = 0; |
| 1052 | |
| 1053 | FT_TRACE4(( "Executing `prep' table.\n" )); |
| 1054 | error = TT_Run_Context( exec, size ); |
| 1055 | FT_TRACE4(( error ? " failed (error code 0x%x)\n" : "", |
| 1056 | error )); |
| 1057 | |
| 1058 | if ( !error ) |
| 1059 | TT_Save_Context( exec, size ); |
| 1060 | } |
| 1061 | else |
| 1062 | error = FT_Err_Ok; |
| 1063 | |
| 1064 | size->cvt_ready = error; |
| 1065 | |
| 1066 | return error; |
| 1067 | } |
| 1068 | |
| 1069 | |
| 1070 | static void |
| 1071 | tt_size_done_bytecode( TT_Size size ) |
| 1072 | { |
| 1073 | FT_Memory memory = size->root.face->memory; |
| 1074 | TT_ExecContext exec = size->context; |
| 1075 | |
| 1076 | |
| 1077 | if ( exec ) |
| 1078 | { |
| 1079 | FT_FREE( exec->stack ); |
| 1080 | FT_FREE( exec->FDefs ); |
| 1081 | |
| 1082 | TT_Done_Context( exec ); |
| 1083 | size->context = NULL; |
| 1084 | } |
| 1085 | |
| 1086 | /* twilight zone */ |
| 1087 | tt_glyphzone_done( memory, &size->twilight ); |
| 1088 | } |
| 1089 | |
| 1090 | |
| 1091 | /* Initialize bytecode-related fields in the size object. */ |
| 1092 | /* We do this only if bytecode interpretation is really needed. */ |
| 1093 | FT_LOCAL_DEF( FT_Error ) |
| 1094 | tt_size_init_bytecode( TT_Size size, |
| 1095 | FT_Bool pedantic ) |
| 1096 | { |
| 1097 | FT_Error error; |
| 1098 | TT_Face face = (TT_Face)size->root.face; |
| 1099 | FT_Memory memory = size->root.face->memory; |
| 1100 | |
| 1101 | FT_UShort n_twilight; |
| 1102 | TT_MaxProfile* maxp = &face->max_profile; |
| 1103 | TT_ExecContext exec; |
| 1104 | |
| 1105 | |
| 1106 | exec = TT_New_Context( (TT_Driver)face->root.driver ); |
| 1107 | if ( !exec ) |
| 1108 | return FT_THROW( Could_Not_Find_Context ); |
| 1109 | |
| 1110 | size->context = exec; |
| 1111 | |
| 1112 | exec->pedantic_hinting = pedantic; |
| 1113 | |
| 1114 | exec->maxFDefs = maxp->maxFunctionDefs; |
| 1115 | exec->maxIDefs = maxp->maxInstructionDefs; |
| 1116 | |
| 1117 | if ( FT_NEW_ARRAY( exec->FDefs, exec->maxFDefs + exec->maxIDefs ) ) |
| 1118 | goto Fail; |
| 1119 | |
| 1120 | exec->IDefs = exec->FDefs + exec->maxFDefs; |
| 1121 | |
| 1122 | exec->numFDefs = 0; |
| 1123 | exec->numIDefs = 0; |
| 1124 | |
| 1125 | exec->maxFunc = 0; |
| 1126 | exec->maxIns = 0; |
| 1127 | |
| 1128 | /* We reserve extra elements on the stack to deal with broken fonts. */ |
| 1129 | /* */ |
| 1130 | /* Some fonts (e.g., `Rubik-Italic.ttf`) have buggy hinting bytecode */ |
| 1131 | /* that pushes more values than `maxStackElements` declared in the */ |
| 1132 | /* 'maxp' table. For example, `Rubik-Italic.ttf`'s 'prep' program */ |
| 1133 | /* pushes 255 values but `maxStackElements` is only set to 153. */ |
| 1134 | /* */ |
| 1135 | /* To alleviate this situation we increase the value of */ |
| 1136 | /* `maxStackElements` based on a percentage of `maxStackElements`, */ |
| 1137 | /* with a minimum of 128 extra slots. This allows most broken fonts */ |
| 1138 | /* to work without completely disabling hinting, while adding only a */ |
| 1139 | /* small overhead for correctly authored fonts. */ |
| 1140 | |
| 1141 | /* Use 50% more than declared, with minimum safety margin of 128. */ |
| 1142 | exec->stackSize = maxp->maxStackElements + |
| 1143 | FT_MAX( maxp->maxStackElements / 2, 128 ); |
| 1144 | |
| 1145 | exec->storeSize = maxp->maxStorage; |
| 1146 | exec->cvtSize = face->cvt_size; |
| 1147 | |
| 1148 | if ( FT_NEW_ARRAY( exec->stack, |
| 1149 | exec->stackSize + |
| 1150 | (FT_Long)( exec->storeSize + exec->cvtSize ) ) ) |
| 1151 | goto Fail; |
| 1152 | |
| 1153 | /* reserve twilight zone and set GS before fpgm is executed, */ |
| 1154 | /* just in case, even though fpgm should not touch them */ |
| 1155 | n_twilight = maxp->maxTwilightPoints; |
| 1156 | |
| 1157 | /* there are 4 phantom points (do we need this?) */ |
| 1158 | n_twilight += 4; |
| 1159 | |
| 1160 | error = tt_glyphzone_new( memory, n_twilight, 0, &size->twilight ); |
| 1161 | if ( error ) |
| 1162 | goto Fail; |
| 1163 | |
| 1164 | size->GS = tt_default_graphics_state; |
| 1165 | size->cvt_ready = -1; |
| 1166 | |
| 1167 | size->ttmetrics.rotated = FALSE; |
| 1168 | size->ttmetrics.stretched = FALSE; |
| 1169 | |
| 1170 | /* Fine, now run the font program! */ |
| 1171 | |
| 1172 | /* In case of an error while executing `fpgm', we intentionally don't */ |
| 1173 | /* clean up immediately – bugs in the `fpgm' are so fundamental that */ |
| 1174 | /* all following hinting calls should fail. Additionally, `fpgm' is */ |
| 1175 | /* to be executed just once; calling it again is completely useless */ |
| 1176 | /* and might even lead to extremely slow behaviour if it is malformed */ |
| 1177 | /* (containing an infinite loop, for example). */ |
| 1178 | error = tt_size_run_fpgm( size ); |
| 1179 | return error; |
| 1180 | |
| 1181 | Fail: |
| 1182 | tt_size_done_bytecode( size ); |
| 1183 | return error; |
| 1184 | } |
| 1185 | |
| 1186 | #endif /* TT_USE_BYTECODE_INTERPRETER */ |
| 1187 | |
| 1188 | |
| 1189 | /************************************************************************** |
| 1190 | * |
| 1191 | * @Function: |
| 1192 | * tt_size_init |
| 1193 | * |
| 1194 | * @Description: |
| 1195 | * Initialize a new TrueType size object. |
| 1196 | * |
| 1197 | * @InOut: |
| 1198 | * size :: |
| 1199 | * A handle to the size object. |
| 1200 | * |
| 1201 | * @Return: |
| 1202 | * FreeType error code. 0 means success. |
| 1203 | */ |
| 1204 | FT_LOCAL_DEF( FT_Error ) |
| 1205 | tt_size_init( FT_Size ttsize ) /* TT_Size */ |
| 1206 | { |
| 1207 | TT_Size size = (TT_Size)ttsize; |
| 1208 | FT_Error error = FT_Err_Ok; |
| 1209 | |
| 1210 | |
| 1211 | #ifdef TT_USE_BYTECODE_INTERPRETER |
| 1212 | size->bytecode_ready = -1; |
| 1213 | #endif |
| 1214 | |
| 1215 | size->strike_index = 0xFFFFFFFFUL; |
| 1216 | |
| 1217 | return error; |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | /************************************************************************** |
| 1222 | * |
| 1223 | * @Function: |
| 1224 | * tt_size_done |
| 1225 | * |
| 1226 | * @Description: |
| 1227 | * The TrueType size object finalizer. |
| 1228 | * |
| 1229 | * @Input: |
| 1230 | * size :: |
| 1231 | * A handle to the target size object. |
| 1232 | */ |
| 1233 | FT_LOCAL_DEF( void ) |
| 1234 | tt_size_done( FT_Size ttsize ) /* TT_Size */ |
| 1235 | { |
| 1236 | #ifdef TT_USE_BYTECODE_INTERPRETER |
| 1237 | tt_size_done_bytecode( (TT_Size)ttsize ); |
| 1238 | #else |
| 1239 | FT_UNUSED( ttsize ); |
| 1240 | #endif |
| 1241 | } |
| 1242 | |
| 1243 | |
| 1244 | /************************************************************************** |
| 1245 | * |
| 1246 | * @Function: |
| 1247 | * tt_size_reset_height |
| 1248 | * |
| 1249 | * @Description: |
| 1250 | * Recompute a TrueType size's ascender, descender, and height |
| 1251 | * when resolutions and character dimensions have been changed. |
| 1252 | * Used for variation fonts as an iterator function. |
| 1253 | * |
| 1254 | * @Input: |
| 1255 | * ft_size :: |
| 1256 | * A handle to the target TT_Size object. This function will be called |
| 1257 | * through a `FT_Size_Reset_Func` pointer which takes `FT_Size`. This |
| 1258 | * function must take `FT_Size` as a result. The passed `FT_Size` is |
| 1259 | * expected to point to a `TT_Size`. |
| 1260 | */ |
| 1261 | FT_LOCAL_DEF( void ) |
| 1262 | tt_size_reset_height( FT_Size ft_size ) |
| 1263 | { |
| 1264 | TT_Size size = (TT_Size)ft_size; |
| 1265 | TT_Face face = (TT_Face)ft_size->face; |
| 1266 | FT_Size_Metrics* size_metrics = &size->hinted_metrics; |
| 1267 | |
| 1268 | /* This bit flag, if set, indicates that the ppems must be */ |
| 1269 | /* rounded to integers. Nearly all TrueType fonts have this bit */ |
| 1270 | /* set, as hinting won't work really well otherwise. */ |
| 1271 | /* */ |
| 1272 | if ( face->header.Flags & 8 ) |
| 1273 | { |
| 1274 | /* the TT spec always asks for ROUND, not FLOOR or CEIL */ |
| 1275 | size_metrics->ascender = FT_PIX_ROUND( |
| 1276 | FT_MulFix( face->root.ascender, |
| 1277 | size_metrics->y_scale ) ); |
| 1278 | size_metrics->descender = FT_PIX_ROUND( |
| 1279 | FT_MulFix( face->root.descender, |
| 1280 | size_metrics->y_scale ) ); |
| 1281 | size_metrics->height = FT_PIX_ROUND( |
| 1282 | FT_MulFix( face->root.height, |
| 1283 | size_metrics->y_scale ) ); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | |
| 1288 | /************************************************************************** |
| 1289 | * |
| 1290 | * @Function: |
| 1291 | * tt_size_reset |
| 1292 | * |
| 1293 | * @Description: |
| 1294 | * Reset a TrueType size when resolutions and character dimensions |
| 1295 | * have been changed. |
| 1296 | * |
| 1297 | * @Input: |
| 1298 | * size :: |
| 1299 | * A handle to the target size object. |
| 1300 | */ |
| 1301 | FT_LOCAL_DEF( FT_Error ) |
| 1302 | tt_size_reset( TT_Size size ) |
| 1303 | { |
| 1304 | TT_Face face = (TT_Face)size->root.face; |
| 1305 | FT_Size_Metrics* size_metrics = &size->hinted_metrics; |
| 1306 | |
| 1307 | |
| 1308 | /* invalidate the size object first */ |
| 1309 | size->ttmetrics.ppem = 0; |
| 1310 | |
| 1311 | if ( size->root.metrics.x_ppem == 0 || size->root.metrics.y_ppem == 0 ) |
| 1312 | return FT_THROW( Invalid_PPem ); |
| 1313 | |
| 1314 | /* copy the result from base layer */ |
| 1315 | *size_metrics = size->root.metrics; |
| 1316 | |
| 1317 | tt_size_reset_height( (FT_Size)size ); |
| 1318 | |
| 1319 | if ( face->header.Flags & 8 ) |
| 1320 | { |
| 1321 | /* base scaling values on integer ppem values, */ |
| 1322 | /* as mandated by the TrueType specification */ |
| 1323 | size_metrics->x_scale = FT_DivFix( size_metrics->x_ppem << 6, |
| 1324 | face->root.units_per_EM ); |
| 1325 | size_metrics->y_scale = FT_DivFix( size_metrics->y_ppem << 6, |
| 1326 | face->root.units_per_EM ); |
| 1327 | |
| 1328 | size_metrics->max_advance = FT_PIX_ROUND( |
| 1329 | FT_MulFix( face->root.max_advance_width, |
| 1330 | size_metrics->x_scale ) ); |
| 1331 | } |
| 1332 | |
| 1333 | /* compute new transformation */ |
| 1334 | if ( size_metrics->x_ppem >= size_metrics->y_ppem ) |
| 1335 | { |
| 1336 | size->ttmetrics.scale = size_metrics->x_scale; |
| 1337 | size->ttmetrics.ppem = size_metrics->x_ppem; |
| 1338 | size->ttmetrics.x_ratio = 0x10000L; |
| 1339 | size->ttmetrics.y_ratio = FT_DivFix( size_metrics->y_ppem, |
| 1340 | size_metrics->x_ppem ); |
| 1341 | } |
| 1342 | else |
| 1343 | { |
| 1344 | size->ttmetrics.scale = size_metrics->y_scale; |
| 1345 | size->ttmetrics.ppem = size_metrics->y_ppem; |
| 1346 | size->ttmetrics.x_ratio = FT_DivFix( size_metrics->x_ppem, |
| 1347 | size_metrics->y_ppem ); |
| 1348 | size->ttmetrics.y_ratio = 0x10000L; |
| 1349 | } |
| 1350 | |
| 1351 | size->widthp = tt_face_get_device_metrics( face, size_metrics->x_ppem, 0 ); |
| 1352 | |
| 1353 | size->metrics = size_metrics; |
| 1354 | |
| 1355 | #ifdef TT_USE_BYTECODE_INTERPRETER |
| 1356 | size->cvt_ready = -1; |
| 1357 | #endif /* TT_USE_BYTECODE_INTERPRETER */ |
| 1358 | |
| 1359 | return FT_Err_Ok; |
| 1360 | } |
| 1361 | |
| 1362 | |
| 1363 | /************************************************************************** |
| 1364 | * |
| 1365 | * @Function: |
| 1366 | * tt_driver_init |
| 1367 | * |
| 1368 | * @Description: |
| 1369 | * Initialize a given TrueType driver object. |
| 1370 | * |
| 1371 | * @Input: |
| 1372 | * driver :: |
| 1373 | * A handle to the target driver object. |
| 1374 | * |
| 1375 | * @Return: |
| 1376 | * FreeType error code. 0 means success. |
| 1377 | */ |
| 1378 | FT_LOCAL_DEF( FT_Error ) |
| 1379 | tt_driver_init( FT_Module ttdriver ) /* TT_Driver */ |
| 1380 | { |
| 1381 | |
| 1382 | #ifdef TT_USE_BYTECODE_INTERPRETER |
| 1383 | |
| 1384 | TT_Driver driver = (TT_Driver)ttdriver; |
| 1385 | |
| 1386 | driver->interpreter_version = TT_INTERPRETER_VERSION_35; |
| 1387 | #ifdef TT_SUPPORT_SUBPIXEL_HINTING_MINIMAL |
| 1388 | driver->interpreter_version = TT_INTERPRETER_VERSION_40; |
| 1389 | #endif |
| 1390 | |
| 1391 | #else /* !TT_USE_BYTECODE_INTERPRETER */ |
| 1392 | |
| 1393 | FT_UNUSED( ttdriver ); |
| 1394 | |
| 1395 | #endif /* !TT_USE_BYTECODE_INTERPRETER */ |
| 1396 | |
| 1397 | return FT_Err_Ok; |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | /************************************************************************** |
| 1402 | * |
| 1403 | * @Function: |
| 1404 | * tt_driver_done |
| 1405 | * |
| 1406 | * @Description: |
| 1407 | * Finalize a given TrueType driver. |
| 1408 | * |
| 1409 | * @Input: |
| 1410 | * driver :: |
| 1411 | * A handle to the target TrueType driver. |
| 1412 | */ |
| 1413 | FT_LOCAL_DEF( void ) |
| 1414 | tt_driver_done( FT_Module ttdriver ) /* TT_Driver */ |
| 1415 | { |
| 1416 | FT_UNUSED( ttdriver ); |
| 1417 | } |
| 1418 | |
| 1419 | |
| 1420 | /************************************************************************** |
| 1421 | * |
| 1422 | * @Function: |
| 1423 | * tt_slot_init |
| 1424 | * |
| 1425 | * @Description: |
| 1426 | * Initialize a new slot object. |
| 1427 | * |
| 1428 | * @InOut: |
| 1429 | * slot :: |
| 1430 | * A handle to the slot object. |
| 1431 | * |
| 1432 | * @Return: |
| 1433 | * FreeType error code. 0 means success. |
| 1434 | */ |
| 1435 | FT_LOCAL_DEF( FT_Error ) |
| 1436 | tt_slot_init( FT_GlyphSlot slot ) |
| 1437 | { |
| 1438 | return FT_GlyphLoader_CreateExtra( slot->internal->loader ); |
| 1439 | } |
| 1440 | |
| 1441 | |
| 1442 | /* END */ |