1 /****************************************************************************
5 * Auto-fitter routines to compute global hinting values (body).
7 * Copyright (C) 2003-2020 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.
22 #include <freetype/internal/ftdebug.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 afglobal
35 /* get writing system specific header files */
37 #define WRITING_SYSTEM( ws, WS ) /* empty */
44 #define SCRIPT( s, S, d, h, H, ss ) \
45 AF_DEFINE_SCRIPT_CLASS( \
46 af_ ## s ## _script_class, \
48 af_ ## s ## _uniranges, \
49 af_ ## s ## _nonbase_uniranges, \
57 #define STYLE( s, S, d, ws, sc, ss, c ) \
58 AF_DEFINE_STYLE_CLASS( \
59 af_ ## s ## _style_class, \
70 #define WRITING_SYSTEM( ws, WS ) \
71 &af_ ## ws ## _writing_system_class,
73 FT_LOCAL_ARRAY_DEF( AF_WritingSystemClass )
74 af_writing_system_classes[] =
79 NULL /* do not remove */
84 #define SCRIPT( s, S, d, h, H, ss ) \
85 &af_ ## s ## _script_class,
87 FT_LOCAL_ARRAY_DEF( AF_ScriptClass )
93 NULL /* do not remove */
98 #define STYLE( s, S, d, ws, sc, ss, c ) \
99 &af_ ## s ## _style_class,
101 FT_LOCAL_ARRAY_DEF( AF_StyleClass )
105 #include "afstyles.h"
107 NULL /* do not remove */
111 #ifdef FT_DEBUG_LEVEL_TRACE
114 #define STYLE( s, S, d, ws, sc, ss, c ) #s,
116 FT_LOCAL_ARRAY_DEF( char* )
120 #include "afstyles.h"
124 #endif /* FT_DEBUG_LEVEL_TRACE */
127 /* Compute the style index of each glyph within a given face. */
130 af_face_globals_compute_style_coverage( AF_FaceGlobals globals )
133 FT_Face face = globals->face;
134 FT_CharMap old_charmap = face->charmap;
135 FT_UShort* gstyles = globals->glyph_styles;
138 FT_UInt dflt = ~0U; /* a non-valid value */
141 /* the value AF_STYLE_UNASSIGNED means `uncovered glyph' */
142 for ( i = 0; i < (FT_UInt)globals->glyph_count; i++ )
143 gstyles[i] = AF_STYLE_UNASSIGNED;
145 error = FT_Select_Charmap( face, FT_ENCODING_UNICODE );
149 * Ignore this error; we simply use the fallback style.
150 * XXX: Shouldn't we rather disable hinting?
156 /* scan each style in a Unicode charmap */
157 for ( ss = 0; af_style_classes[ss]; ss++ )
159 AF_StyleClass style_class =
160 af_style_classes[ss];
161 AF_ScriptClass script_class =
162 af_script_classes[style_class->script];
163 AF_Script_UniRange range;
166 if ( !script_class->script_uni_ranges )
170 * Scan all Unicode points in the range and set the corresponding
173 if ( style_class->coverage == AF_COVERAGE_DEFAULT )
175 if ( (FT_UInt)style_class->script ==
176 globals->module->default_script )
179 for ( range = script_class->script_uni_ranges;
183 FT_ULong charcode = range->first;
187 gindex = FT_Get_Char_Index( face, charcode );
190 gindex < (FT_ULong)globals->glyph_count &&
191 ( gstyles[gindex] & AF_STYLE_MASK ) == AF_STYLE_UNASSIGNED )
192 gstyles[gindex] = (FT_UShort)ss;
196 charcode = FT_Get_Next_Char( face, charcode, &gindex );
198 if ( gindex == 0 || charcode > range->last )
201 if ( gindex < (FT_ULong)globals->glyph_count &&
202 ( gstyles[gindex] & AF_STYLE_MASK ) == AF_STYLE_UNASSIGNED )
203 gstyles[gindex] = (FT_UShort)ss;
207 /* do the same for the script's non-base characters */
208 for ( range = script_class->script_uni_nonbase_ranges;
212 FT_ULong charcode = range->first;
216 gindex = FT_Get_Char_Index( face, charcode );
219 gindex < (FT_ULong)globals->glyph_count &&
220 ( gstyles[gindex] & AF_STYLE_MASK ) == (FT_UShort)ss )
221 gstyles[gindex] |= AF_NONBASE;
225 charcode = FT_Get_Next_Char( face, charcode, &gindex );
227 if ( gindex == 0 || charcode > range->last )
230 if ( gindex < (FT_ULong)globals->glyph_count &&
231 ( gstyles[gindex] & AF_STYLE_MASK ) == (FT_UShort)ss )
232 gstyles[gindex] |= AF_NONBASE;
238 /* get glyphs not directly addressable by cmap */
239 af_shaper_get_coverage( globals, style_class, gstyles, 0 );
243 /* handle the remaining default OpenType features ... */
244 for ( ss = 0; af_style_classes[ss]; ss++ )
246 AF_StyleClass style_class = af_style_classes[ss];
249 if ( style_class->coverage == AF_COVERAGE_DEFAULT )
250 af_shaper_get_coverage( globals, style_class, gstyles, 0 );
253 /* ... and finally the default OpenType features of the default script */
254 af_shaper_get_coverage( globals, af_style_classes[dflt], gstyles, 1 );
256 /* mark ASCII digits */
257 for ( i = 0x30; i <= 0x39; i++ )
259 FT_UInt gindex = FT_Get_Char_Index( face, i );
262 if ( gindex != 0 && gindex < (FT_ULong)globals->glyph_count )
263 gstyles[gindex] |= AF_DIGIT;
268 * By default, all uncovered glyphs are set to the fallback style.
269 * XXX: Shouldn't we disable hinting or do something similar?
271 if ( globals->module->fallback_style != AF_STYLE_UNASSIGNED )
276 for ( nn = 0; nn < globals->glyph_count; nn++ )
278 if ( ( gstyles[nn] & AF_STYLE_MASK ) == AF_STYLE_UNASSIGNED )
280 gstyles[nn] &= ~AF_STYLE_MASK;
281 gstyles[nn] |= globals->module->fallback_style;
286 #ifdef FT_DEBUG_LEVEL_TRACE
293 for ( ss = 0; af_style_classes[ss]; ss++ )
295 AF_StyleClass style_class = af_style_classes[ss];
300 FT_TRACE4(( "%s:\n", af_style_names[style_class->style] ));
302 for ( idx = 0; idx < globals->glyph_count; idx++ )
304 if ( ( gstyles[idx] & AF_STYLE_MASK ) == style_class->style )
306 if ( !( count % 10 ) )
309 FT_TRACE4(( " %ld", idx ));
312 if ( !( count % 10 ) )
318 FT_TRACE4(( " (none)\n" ));
323 #endif /* FT_DEBUG_LEVEL_TRACE */
325 FT_Set_Charmap( face, old_charmap );
330 FT_LOCAL_DEF( FT_Error )
331 af_face_globals_new( FT_Face face,
332 AF_FaceGlobals *aglobals,
337 AF_FaceGlobals globals = NULL;
340 memory = face->memory;
342 /* we allocate an AF_FaceGlobals structure together */
343 /* with the glyph_styles array */
344 if ( FT_ALLOC( globals,
345 sizeof ( *globals ) +
346 (FT_ULong)face->num_glyphs * sizeof ( FT_UShort ) ) )
349 globals->face = face;
350 globals->glyph_count = face->num_glyphs;
351 /* right after the globals structure come the glyph styles */
352 globals->glyph_styles = (FT_UShort*)( globals + 1 );
353 globals->module = module;
354 globals->stem_darkening_for_ppem = 0;
355 globals->darken_x = 0;
356 globals->darken_y = 0;
357 globals->standard_vertical_width = 0;
358 globals->standard_horizontal_width = 0;
359 globals->scale_down_factor = 0;
361 #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ
362 globals->hb_font = hb_ft_font_create( face, NULL );
363 globals->hb_buf = hb_buffer_create();
366 error = af_face_globals_compute_style_coverage( globals );
369 af_face_globals_free( globals );
373 globals->increase_x_height = AF_PROP_INCREASE_X_HEIGHT_MAX;
382 af_face_globals_free( AF_FaceGlobals globals )
386 FT_Memory memory = globals->face->memory;
390 for ( nn = 0; nn < AF_STYLE_MAX; nn++ )
392 if ( globals->metrics[nn] )
394 AF_StyleClass style_class =
395 af_style_classes[nn];
396 AF_WritingSystemClass writing_system_class =
397 af_writing_system_classes[style_class->writing_system];
400 if ( writing_system_class->style_metrics_done )
401 writing_system_class->style_metrics_done( globals->metrics[nn] );
403 FT_FREE( globals->metrics[nn] );
407 #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ
408 hb_font_destroy( globals->hb_font );
409 hb_buffer_destroy( globals->hb_buf );
412 /* no need to free `globals->glyph_styles'; */
413 /* it is part of the `globals' array */
419 FT_LOCAL_DEF( FT_Error )
420 af_face_globals_get_metrics( AF_FaceGlobals globals,
423 AF_StyleMetrics *ametrics )
425 AF_StyleMetrics metrics = NULL;
427 AF_Style style = (AF_Style)options;
428 AF_WritingSystemClass writing_system_class;
429 AF_StyleClass style_class;
431 FT_Error error = FT_Err_Ok;
434 if ( gindex >= (FT_ULong)globals->glyph_count )
436 error = FT_THROW( Invalid_Argument );
440 /* if we have a forced style (via `options'), use it, */
441 /* otherwise look into `glyph_styles' array */
442 if ( style == AF_STYLE_NONE_DFLT || style + 1 >= AF_STYLE_MAX )
443 style = (AF_Style)( globals->glyph_styles[gindex] &
444 AF_STYLE_UNASSIGNED );
447 style_class = af_style_classes[style];
448 writing_system_class = af_writing_system_classes
449 [style_class->writing_system];
451 metrics = globals->metrics[style];
454 /* create the global metrics object if necessary */
455 FT_Memory memory = globals->face->memory;
458 if ( FT_ALLOC( metrics, writing_system_class->style_metrics_size ) )
461 metrics->style_class = style_class;
462 metrics->globals = globals;
464 if ( writing_system_class->style_metrics_init )
466 error = writing_system_class->style_metrics_init( metrics,
470 if ( writing_system_class->style_metrics_done )
471 writing_system_class->style_metrics_done( metrics );
475 /* internal error code -1 indicates */
476 /* that no blue zones have been found */
479 style = (AF_Style)( globals->glyph_styles[gindex] &
480 AF_STYLE_UNASSIGNED );
488 globals->metrics[style] = metrics;
498 FT_LOCAL_DEF( FT_Bool )
499 af_face_globals_is_digit( AF_FaceGlobals globals,
502 if ( gindex < (FT_ULong)globals->glyph_count )
503 return FT_BOOL( globals->glyph_styles[gindex] & AF_DIGIT );