1 /***************************************************************************/
5 /* Auto-fitter routines to compute global hinting values (body). */
7 /* Copyright 2003-2016 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
22 #include FT_INTERNAL_DEBUG_H
25 /*************************************************************************/
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. */
32 #define FT_COMPONENT trace_afglobal
35 /* get writing system specific header files */
37 #define WRITING_SYSTEM( ws, WS ) /* empty */
45 #define SCRIPT( s, S, d, h, H, ss ) \
46 AF_DEFINE_SCRIPT_CLASS( \
47 af_ ## s ## _script_class, \
49 af_ ## s ## _uniranges, \
50 af_ ## s ## _nonbase_uniranges, \
58 #define STYLE( s, S, d, ws, sc, ss, c ) \
59 AF_DEFINE_STYLE_CLASS( \
60 af_ ## s ## _style_class, \
70 #ifndef FT_CONFIG_OPTION_PIC
73 #define WRITING_SYSTEM( ws, WS ) \
74 &af_ ## ws ## _writing_system_class,
76 FT_LOCAL_ARRAY_DEF( AF_WritingSystemClass )
77 af_writing_system_classes[] =
82 NULL /* do not remove */
87 #define SCRIPT( s, S, d, h, H, ss ) \
88 &af_ ## s ## _script_class,
90 FT_LOCAL_ARRAY_DEF( AF_ScriptClass )
96 NULL /* do not remove */
101 #define STYLE( s, S, d, ws, sc, ss, c ) \
102 &af_ ## s ## _style_class,
104 FT_LOCAL_ARRAY_DEF( AF_StyleClass )
108 #include "afstyles.h"
110 NULL /* do not remove */
113 #endif /* !FT_CONFIG_OPTION_PIC */
116 #ifdef FT_DEBUG_LEVEL_TRACE
119 #define STYLE( s, S, d, ws, sc, ss, c ) #s,
121 FT_LOCAL_ARRAY_DEF( char* )
125 #include "afstyles.h"
129 #endif /* FT_DEBUG_LEVEL_TRACE */
132 /* Compute the style index of each glyph within a given face. */
135 af_face_globals_compute_style_coverage( AF_FaceGlobals globals )
138 FT_Face face = globals->face;
139 FT_CharMap old_charmap = face->charmap;
140 FT_UShort* gstyles = globals->glyph_styles;
143 FT_UInt dflt = ~0U; /* a non-valid value */
146 /* the value AF_STYLE_UNASSIGNED means `uncovered glyph' */
147 for ( i = 0; i < (FT_UInt)globals->glyph_count; i++ )
148 gstyles[i] = AF_STYLE_UNASSIGNED;
150 error = FT_Select_Charmap( face, FT_ENCODING_UNICODE );
154 * Ignore this error; we simply use the fallback style.
155 * XXX: Shouldn't we rather disable hinting?
161 /* scan each style in a Unicode charmap */
162 for ( ss = 0; AF_STYLE_CLASSES_GET[ss]; ss++ )
164 AF_StyleClass style_class =
165 AF_STYLE_CLASSES_GET[ss];
166 AF_ScriptClass script_class =
167 AF_SCRIPT_CLASSES_GET[style_class->script];
168 AF_Script_UniRange range;
171 if ( script_class->script_uni_ranges == NULL )
175 * Scan all Unicode points in the range and set the corresponding
178 if ( style_class->coverage == AF_COVERAGE_DEFAULT )
180 if ( (FT_UInt)style_class->script ==
181 globals->module->default_script )
184 for ( range = script_class->script_uni_ranges;
188 FT_ULong charcode = range->first;
192 gindex = FT_Get_Char_Index( face, charcode );
195 gindex < (FT_ULong)globals->glyph_count &&
196 ( gstyles[gindex] & AF_STYLE_MASK ) == AF_STYLE_UNASSIGNED )
197 gstyles[gindex] = (FT_UShort)ss;
201 charcode = FT_Get_Next_Char( face, charcode, &gindex );
203 if ( gindex == 0 || charcode > range->last )
206 if ( gindex < (FT_ULong)globals->glyph_count &&
207 ( gstyles[gindex] & AF_STYLE_MASK ) == AF_STYLE_UNASSIGNED )
208 gstyles[gindex] = (FT_UShort)ss;
212 /* do the same for the script's non-base characters */
213 for ( range = script_class->script_uni_nonbase_ranges;
217 FT_ULong charcode = range->first;
221 gindex = FT_Get_Char_Index( face, charcode );
224 gindex < (FT_ULong)globals->glyph_count &&
225 ( gstyles[gindex] & AF_STYLE_MASK ) == (FT_UShort)ss )
226 gstyles[gindex] |= AF_NONBASE;
230 charcode = FT_Get_Next_Char( face, charcode, &gindex );
232 if ( gindex == 0 || charcode > range->last )
235 if ( gindex < (FT_ULong)globals->glyph_count &&
236 ( gstyles[gindex] & AF_STYLE_MASK ) == (FT_UShort)ss )
237 gstyles[gindex] |= AF_NONBASE;
243 /* get glyphs not directly addressable by cmap */
244 af_shaper_get_coverage( globals, style_class, gstyles, 0 );
248 /* handle the remaining default OpenType features ... */
249 for ( ss = 0; AF_STYLE_CLASSES_GET[ss]; ss++ )
251 AF_StyleClass style_class = AF_STYLE_CLASSES_GET[ss];
254 if ( style_class->coverage == AF_COVERAGE_DEFAULT )
255 af_shaper_get_coverage( globals, style_class, gstyles, 0 );
258 /* ... and finally the default OpenType features of the default script */
259 af_shaper_get_coverage( globals, AF_STYLE_CLASSES_GET[dflt], gstyles, 1 );
261 /* mark ASCII digits */
262 for ( i = 0x30; i <= 0x39; i++ )
264 FT_UInt gindex = FT_Get_Char_Index( face, i );
267 if ( gindex != 0 && gindex < (FT_ULong)globals->glyph_count )
268 gstyles[gindex] |= AF_DIGIT;
273 * By default, all uncovered glyphs are set to the fallback style.
274 * XXX: Shouldn't we disable hinting or do something similar?
276 if ( globals->module->fallback_style != AF_STYLE_UNASSIGNED )
281 for ( nn = 0; nn < globals->glyph_count; nn++ )
283 if ( ( gstyles[nn] & AF_STYLE_MASK ) == AF_STYLE_UNASSIGNED )
285 gstyles[nn] &= ~AF_STYLE_MASK;
286 gstyles[nn] |= globals->module->fallback_style;
291 #ifdef FT_DEBUG_LEVEL_TRACE
298 for ( ss = 0; AF_STYLE_CLASSES_GET[ss]; ss++ )
300 AF_StyleClass style_class = AF_STYLE_CLASSES_GET[ss];
305 FT_TRACE4(( "%s:\n", af_style_names[style_class->style] ));
307 for ( idx = 0; idx < globals->glyph_count; idx++ )
309 if ( ( gstyles[idx] & AF_STYLE_MASK ) == style_class->style )
311 if ( !( count % 10 ) )
314 FT_TRACE4(( " %d", idx ));
317 if ( !( count % 10 ) )
323 FT_TRACE4(( " (none)\n" ));
328 #endif /* FT_DEBUG_LEVEL_TRACE */
330 FT_Set_Charmap( face, old_charmap );
335 FT_LOCAL_DEF( FT_Error )
336 af_face_globals_new( FT_Face face,
337 AF_FaceGlobals *aglobals,
342 AF_FaceGlobals globals = NULL;
345 memory = face->memory;
347 /* we allocate an AF_FaceGlobals structure together */
348 /* with the glyph_styles array */
349 if ( FT_ALLOC( globals,
350 sizeof ( *globals ) +
351 (FT_ULong)face->num_glyphs * sizeof ( FT_UShort ) ) )
354 globals->face = face;
355 globals->glyph_count = face->num_glyphs;
356 /* right after the globals structure come the glyph styles */
357 globals->glyph_styles = (FT_UShort*)( globals + 1 );
358 globals->module = module;
359 globals->stem_darkening_for_ppem = 0;
360 globals->darken_x = 0;
361 globals->darken_y = 0;
362 globals->standard_vertical_width = 0;
363 globals->standard_horizontal_width = 0;
364 globals->scale_down_factor = 0;
366 #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ
367 globals->hb_font = hb_ft_font_create( face, NULL );
368 globals->hb_buf = hb_buffer_create();
371 error = af_face_globals_compute_style_coverage( globals );
374 af_face_globals_free( globals );
378 globals->increase_x_height = AF_PROP_INCREASE_X_HEIGHT_MAX;
387 af_face_globals_free( AF_FaceGlobals globals )
391 FT_Memory memory = globals->face->memory;
395 for ( nn = 0; nn < AF_STYLE_MAX; nn++ )
397 if ( globals->metrics[nn] )
399 AF_StyleClass style_class =
400 AF_STYLE_CLASSES_GET[nn];
401 AF_WritingSystemClass writing_system_class =
402 AF_WRITING_SYSTEM_CLASSES_GET[style_class->writing_system];
405 if ( writing_system_class->style_metrics_done )
406 writing_system_class->style_metrics_done( globals->metrics[nn] );
408 FT_FREE( globals->metrics[nn] );
412 #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ
413 hb_font_destroy( globals->hb_font );
414 globals->hb_font = NULL;
416 hb_buffer_destroy( globals->hb_buf );
417 globals->hb_buf = NULL;
420 globals->glyph_count = 0;
421 globals->stem_darkening_for_ppem = 0;
422 globals->darken_x = 0;
423 globals->darken_y = 0;
424 globals->standard_vertical_width = 0;
425 globals->standard_horizontal_width = 0;
426 globals->scale_down_factor = 0;
427 /* no need to free this one! */
428 globals->glyph_styles = NULL;
429 globals->face = NULL;
436 FT_LOCAL_DEF( FT_Error )
437 af_face_globals_get_metrics( AF_FaceGlobals globals,
440 AF_StyleMetrics *ametrics )
442 AF_StyleMetrics metrics = NULL;
444 AF_Style style = (AF_Style)options;
445 AF_WritingSystemClass writing_system_class;
446 AF_StyleClass style_class;
448 FT_Error error = FT_Err_Ok;
451 if ( gindex >= (FT_ULong)globals->glyph_count )
453 error = FT_THROW( Invalid_Argument );
457 /* if we have a forced style (via `options'), use it, */
458 /* otherwise look into `glyph_styles' array */
459 if ( style == AF_STYLE_NONE_DFLT || style + 1 >= AF_STYLE_MAX )
460 style = (AF_Style)( globals->glyph_styles[gindex] &
461 AF_STYLE_UNASSIGNED );
463 style_class = AF_STYLE_CLASSES_GET[style];
464 writing_system_class = AF_WRITING_SYSTEM_CLASSES_GET
465 [style_class->writing_system];
467 metrics = globals->metrics[style];
468 if ( metrics == NULL )
470 /* create the global metrics object if necessary */
471 FT_Memory memory = globals->face->memory;
474 if ( FT_ALLOC( metrics, writing_system_class->style_metrics_size ) )
477 metrics->style_class = style_class;
478 metrics->globals = globals;
480 if ( writing_system_class->style_metrics_init )
482 error = writing_system_class->style_metrics_init( metrics,
486 if ( writing_system_class->style_metrics_done )
487 writing_system_class->style_metrics_done( metrics );
494 globals->metrics[style] = metrics;
504 FT_LOCAL_DEF( FT_Bool )
505 af_face_globals_is_digit( AF_FaceGlobals globals,
508 if ( gindex < (FT_ULong)globals->glyph_count )
509 return (FT_Bool)( globals->glyph_styles[gindex] & AF_DIGIT );