1 /***************************************************************************/ 2 /* */ 3 /* afglobal.c */ 4 /* */ 5 /* Auto-fitter routines to compute global hinting values (body). */ 6 /* */ 7 /* Copyright 2003-2015 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 "afglobal.h" 20 #include "afranges.h" 21 #include "afshaper.h" 22 #include FT_INTERNAL_DEBUG_H 23 24 25 /*************************************************************************/ 26 /* */ 27 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */ 28 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */ 29 /* messages during execution. */ 30 /* */ 31 #undef FT_COMPONENT 32 #define FT_COMPONENT trace_afglobal 33 34 35 /* get writing system specific header files */ 36 #undef WRITING_SYSTEM 37 #define WRITING_SYSTEM( ws, WS ) /* empty */ 38 #include "afwrtsys.h" 39 40 #include "aferrors.h" 41 #include "afpic.h" 42 43 44 #undef SCRIPT 45 #define SCRIPT( s, S, d, h, ss ) \ 46 AF_DEFINE_SCRIPT_CLASS( \ 47 af_ ## s ## _script_class, \ 48 AF_SCRIPT_ ## S, \ 49 af_ ## s ## _uniranges, \ 50 af_ ## s ## _nonbase_uniranges, \ 51 ss ) 52 53 #include "afscript.h" 54 55 56 #undef STYLE 57 #define STYLE( s, S, d, ws, sc, ss, c ) \ 58 AF_DEFINE_STYLE_CLASS( \ 59 af_ ## s ## _style_class, \ 60 AF_STYLE_ ## S, \ 61 ws, \ 62 sc, \ 63 ss, \ 64 c ) 65 66 #include "afstyles.h" 67 68 69 #ifndef FT_CONFIG_OPTION_PIC 70 71 #undef WRITING_SYSTEM 72 #define WRITING_SYSTEM( ws, WS ) \ 73 &af_ ## ws ## _writing_system_class, 74 75 FT_LOCAL_ARRAY_DEF( AF_WritingSystemClass ) 76 af_writing_system_classes[] = 77 { 78 79 #include "afwrtsys.h" 80 81 NULL /* do not remove */ 82 }; 83 84 85 #undef SCRIPT 86 #define SCRIPT( s, S, d, h, ss ) \ 87 &af_ ## s ## _script_class, 88 89 FT_LOCAL_ARRAY_DEF( AF_ScriptClass ) 90 af_script_classes[] = 91 { 92 93 #include "afscript.h" 94 95 NULL /* do not remove */ 96 }; 97 98 99 #undef STYLE 100 #define STYLE( s, S, d, ws, sc, ss, c ) \ 101 &af_ ## s ## _style_class, 102 103 FT_LOCAL_ARRAY_DEF( AF_StyleClass ) 104 af_style_classes[] = 105 { 106 107 #include "afstyles.h" 108 109 NULL /* do not remove */ 110 }; 111 112 #endif /* !FT_CONFIG_OPTION_PIC */ 113 114 115 #ifdef FT_DEBUG_LEVEL_TRACE 116 117 #undef STYLE 118 #define STYLE( s, S, d, ws, sc, ss, c ) #s, 119 120 FT_LOCAL_ARRAY_DEF( char* ) 121 af_style_names[] = 122 { 123 124 #include "afstyles.h" 125 126 }; 127 128 #endif /* FT_DEBUG_LEVEL_TRACE */ 129 130 131 /* Compute the style index of each glyph within a given face. */ 132 133 static FT_Error af_face_globals_compute_style_coverage(AF_FaceGlobals globals)134 af_face_globals_compute_style_coverage( AF_FaceGlobals globals ) 135 { 136 FT_Error error; 137 FT_Face face = globals->face; 138 FT_CharMap old_charmap = face->charmap; 139 FT_UShort* gstyles = globals->glyph_styles; 140 FT_UInt ss; 141 FT_UInt i; 142 FT_UInt dflt = ~0U; /* a non-valid value */ 143 144 145 /* the value AF_STYLE_UNASSIGNED means `uncovered glyph' */ 146 for ( i = 0; i < (FT_UInt)globals->glyph_count; i++ ) 147 gstyles[i] = AF_STYLE_UNASSIGNED; 148 149 error = FT_Select_Charmap( face, FT_ENCODING_UNICODE ); 150 if ( error ) 151 { 152 /* 153 * Ignore this error; we simply use the fallback style. 154 * XXX: Shouldn't we rather disable hinting? 155 */ 156 error = FT_Err_Ok; 157 goto Exit; 158 } 159 160 /* scan each style in a Unicode charmap */ 161 for ( ss = 0; AF_STYLE_CLASSES_GET[ss]; ss++ ) 162 { 163 AF_StyleClass style_class = 164 AF_STYLE_CLASSES_GET[ss]; 165 AF_ScriptClass script_class = 166 AF_SCRIPT_CLASSES_GET[style_class->script]; 167 AF_Script_UniRange range; 168 169 170 if ( script_class->script_uni_ranges == NULL ) 171 continue; 172 173 /* 174 * Scan all Unicode points in the range and set the corresponding 175 * glyph style index. 176 */ 177 if ( style_class->coverage == AF_COVERAGE_DEFAULT ) 178 { 179 if ( (FT_UInt)style_class->script == 180 globals->module->default_script ) 181 dflt = ss; 182 183 for ( range = script_class->script_uni_ranges; 184 range->first != 0; 185 range++ ) 186 { 187 FT_ULong charcode = range->first; 188 FT_UInt gindex; 189 190 191 gindex = FT_Get_Char_Index( face, charcode ); 192 193 if ( gindex != 0 && 194 gindex < (FT_ULong)globals->glyph_count && 195 ( gstyles[gindex] & AF_STYLE_MASK ) == AF_STYLE_UNASSIGNED ) 196 gstyles[gindex] = (FT_UShort)ss; 197 198 for (;;) 199 { 200 charcode = FT_Get_Next_Char( face, charcode, &gindex ); 201 202 if ( gindex == 0 || charcode > range->last ) 203 break; 204 205 if ( gindex < (FT_ULong)globals->glyph_count && 206 ( gstyles[gindex] & AF_STYLE_MASK ) == AF_STYLE_UNASSIGNED ) 207 gstyles[gindex] = (FT_UShort)ss; 208 } 209 } 210 211 /* do the same for the script's non-base characters */ 212 for ( range = script_class->script_uni_nonbase_ranges; 213 range->first != 0; 214 range++ ) 215 { 216 FT_ULong charcode = range->first; 217 FT_UInt gindex; 218 219 220 gindex = FT_Get_Char_Index( face, charcode ); 221 222 if ( gindex != 0 && 223 gindex < (FT_ULong)globals->glyph_count && 224 ( gstyles[gindex] & AF_STYLE_MASK ) == (FT_UShort)ss ) 225 gstyles[gindex] |= AF_NONBASE; 226 227 for (;;) 228 { 229 charcode = FT_Get_Next_Char( face, charcode, &gindex ); 230 231 if ( gindex == 0 || charcode > range->last ) 232 break; 233 234 if ( gindex < (FT_ULong)globals->glyph_count && 235 ( gstyles[gindex] & AF_STYLE_MASK ) == (FT_UShort)ss ) 236 gstyles[gindex] |= AF_NONBASE; 237 } 238 } 239 } 240 else 241 { 242 /* get glyphs not directly addressable by cmap */ 243 af_shaper_get_coverage( globals, style_class, gstyles ); 244 } 245 } 246 247 /* handle the default OpenType features of the default script ... */ 248 af_shaper_get_coverage( globals, AF_STYLE_CLASSES_GET[dflt], gstyles ); 249 250 /* ... and the remaining default OpenType features */ 251 for ( ss = 0; AF_STYLE_CLASSES_GET[ss]; ss++ ) 252 { 253 AF_StyleClass style_class = AF_STYLE_CLASSES_GET[ss]; 254 255 256 if ( ss != dflt && style_class->coverage == AF_COVERAGE_DEFAULT ) 257 af_shaper_get_coverage( globals, style_class, gstyles ); 258 } 259 260 /* mark ASCII digits */ 261 for ( i = 0x30; i <= 0x39; i++ ) 262 { 263 FT_UInt gindex = FT_Get_Char_Index( face, i ); 264 265 266 if ( gindex != 0 && gindex < (FT_ULong)globals->glyph_count ) 267 gstyles[gindex] |= AF_DIGIT; 268 } 269 270 Exit: 271 /* 272 * By default, all uncovered glyphs are set to the fallback style. 273 * XXX: Shouldn't we disable hinting or do something similar? 274 */ 275 if ( globals->module->fallback_style != AF_STYLE_UNASSIGNED ) 276 { 277 FT_Long nn; 278 279 280 for ( nn = 0; nn < globals->glyph_count; nn++ ) 281 { 282 if ( ( gstyles[nn] & AF_STYLE_MASK ) == AF_STYLE_UNASSIGNED ) 283 { 284 gstyles[nn] &= ~AF_STYLE_MASK; 285 gstyles[nn] |= globals->module->fallback_style; 286 } 287 } 288 } 289 290 #ifdef FT_DEBUG_LEVEL_TRACE 291 292 FT_TRACE4(( "\n" 293 "style coverage\n" 294 "==============\n" 295 "\n" )); 296 297 for ( ss = 0; AF_STYLE_CLASSES_GET[ss]; ss++ ) 298 { 299 AF_StyleClass style_class = AF_STYLE_CLASSES_GET[ss]; 300 FT_UInt count = 0; 301 FT_Long idx; 302 303 304 FT_TRACE4(( "%s:\n", af_style_names[style_class->style] )); 305 306 for ( idx = 0; idx < globals->glyph_count; idx++ ) 307 { 308 if ( ( gstyles[idx] & AF_STYLE_MASK ) == style_class->style ) 309 { 310 if ( !( count % 10 ) ) 311 FT_TRACE4(( " " )); 312 313 FT_TRACE4(( " %d", idx )); 314 count++; 315 316 if ( !( count % 10 ) ) 317 FT_TRACE4(( "\n" )); 318 } 319 } 320 321 if ( !count ) 322 FT_TRACE4(( " (none)\n" )); 323 if ( count % 10 ) 324 FT_TRACE4(( "\n" )); 325 } 326 327 #endif /* FT_DEBUG_LEVEL_TRACE */ 328 329 FT_Set_Charmap( face, old_charmap ); 330 return error; 331 } 332 333 334 FT_LOCAL_DEF( FT_Error ) af_face_globals_new(FT_Face face,AF_FaceGlobals * aglobals,AF_Module module)335 af_face_globals_new( FT_Face face, 336 AF_FaceGlobals *aglobals, 337 AF_Module module ) 338 { 339 FT_Error error; 340 FT_Memory memory; 341 AF_FaceGlobals globals = NULL; 342 343 344 memory = face->memory; 345 346 /* we allocate an AF_FaceGlobals structure together */ 347 /* with the glyph_styles array */ 348 if ( FT_ALLOC( globals, 349 sizeof ( *globals ) + 350 (FT_ULong)face->num_glyphs * sizeof ( FT_UShort ) ) ) 351 goto Exit; 352 353 globals->face = face; 354 globals->glyph_count = face->num_glyphs; 355 /* right after the globals structure come the glyph styles */ 356 globals->glyph_styles = (FT_UShort*)( globals + 1 ); 357 globals->module = module; 358 globals->stem_darkening_for_ppem = 0; 359 globals->darken_x = 0; 360 globals->darken_y = 0; 361 globals->standard_vertical_width = 0; 362 globals->standard_horizontal_width = 0; 363 globals->scale_down_factor = 0; 364 365 #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ 366 globals->hb_font = hb_ft_font_create( face, NULL ); 367 globals->hb_buf = hb_buffer_create(); 368 #endif 369 370 error = af_face_globals_compute_style_coverage( globals ); 371 if ( error ) 372 { 373 af_face_globals_free( globals ); 374 globals = NULL; 375 } 376 else 377 globals->increase_x_height = AF_PROP_INCREASE_X_HEIGHT_MAX; 378 379 Exit: 380 *aglobals = globals; 381 return error; 382 } 383 384 385 FT_LOCAL_DEF( void ) af_face_globals_free(AF_FaceGlobals globals)386 af_face_globals_free( AF_FaceGlobals globals ) 387 { 388 if ( globals ) 389 { 390 FT_Memory memory = globals->face->memory; 391 FT_UInt nn; 392 393 394 for ( nn = 0; nn < AF_STYLE_MAX; nn++ ) 395 { 396 if ( globals->metrics[nn] ) 397 { 398 AF_StyleClass style_class = 399 AF_STYLE_CLASSES_GET[nn]; 400 AF_WritingSystemClass writing_system_class = 401 AF_WRITING_SYSTEM_CLASSES_GET[style_class->writing_system]; 402 403 404 if ( writing_system_class->style_metrics_done ) 405 writing_system_class->style_metrics_done( globals->metrics[nn] ); 406 407 FT_FREE( globals->metrics[nn] ); 408 } 409 } 410 411 #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ 412 hb_font_destroy( globals->hb_font ); 413 globals->hb_font = NULL; 414 415 hb_buffer_destroy( globals->hb_buf ); 416 globals->hb_buf = NULL; 417 #endif 418 419 globals->glyph_count = 0; 420 globals->stem_darkening_for_ppem = 0; 421 globals->darken_x = 0; 422 globals->darken_y = 0; 423 globals->standard_vertical_width = 0; 424 globals->standard_horizontal_width = 0; 425 globals->scale_down_factor = 0; 426 /* no need to free this one! */ 427 globals->glyph_styles = NULL; 428 globals->face = NULL; 429 430 FT_FREE( globals ); 431 } 432 } 433 434 435 FT_LOCAL_DEF( FT_Error ) af_face_globals_get_metrics(AF_FaceGlobals globals,FT_UInt gindex,FT_UInt options,AF_StyleMetrics * ametrics)436 af_face_globals_get_metrics( AF_FaceGlobals globals, 437 FT_UInt gindex, 438 FT_UInt options, 439 AF_StyleMetrics *ametrics ) 440 { 441 AF_StyleMetrics metrics = NULL; 442 443 AF_Style style = (AF_Style)options; 444 AF_WritingSystemClass writing_system_class; 445 AF_StyleClass style_class; 446 447 FT_Error error = FT_Err_Ok; 448 449 450 if ( gindex >= (FT_ULong)globals->glyph_count ) 451 { 452 error = FT_THROW( Invalid_Argument ); 453 goto Exit; 454 } 455 456 /* if we have a forced style (via `options'), use it, */ 457 /* otherwise look into `glyph_styles' array */ 458 if ( style == AF_STYLE_NONE_DFLT || style + 1 >= AF_STYLE_MAX ) 459 style = (AF_Style)( globals->glyph_styles[gindex] & 460 AF_STYLE_UNASSIGNED ); 461 462 style_class = AF_STYLE_CLASSES_GET[style]; 463 writing_system_class = AF_WRITING_SYSTEM_CLASSES_GET 464 [style_class->writing_system]; 465 466 metrics = globals->metrics[style]; 467 if ( metrics == NULL ) 468 { 469 /* create the global metrics object if necessary */ 470 FT_Memory memory = globals->face->memory; 471 472 473 if ( FT_ALLOC( metrics, writing_system_class->style_metrics_size ) ) 474 goto Exit; 475 476 metrics->style_class = style_class; 477 metrics->globals = globals; 478 479 if ( writing_system_class->style_metrics_init ) 480 { 481 error = writing_system_class->style_metrics_init( metrics, 482 globals->face ); 483 if ( error ) 484 { 485 if ( writing_system_class->style_metrics_done ) 486 writing_system_class->style_metrics_done( metrics ); 487 488 FT_FREE( metrics ); 489 goto Exit; 490 } 491 } 492 493 globals->metrics[style] = metrics; 494 } 495 496 Exit: 497 *ametrics = metrics; 498 499 return error; 500 } 501 502 503 FT_LOCAL_DEF( FT_Bool ) af_face_globals_is_digit(AF_FaceGlobals globals,FT_UInt gindex)504 af_face_globals_is_digit( AF_FaceGlobals globals, 505 FT_UInt gindex ) 506 { 507 if ( gindex < (FT_ULong)globals->glyph_count ) 508 return (FT_Bool)( globals->glyph_styles[gindex] & AF_DIGIT ); 509 510 return (FT_Bool)0; 511 } 512 513 514 /* END */ 515