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 "hbshim.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, sc1, sc2, sc3 ) \
46           AF_DEFINE_SCRIPT_CLASS(           \
47             af_ ## s ## _script_class,      \
48             AF_SCRIPT_ ## S,                \
49             af_ ## s ## _uniranges,         \
50             af_ ## s ## _nonbase_uniranges, \
51             sc1, sc2, sc3 )
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, sc1, sc2, sc3 ) \
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_get_coverage( globals, style_class, gstyles );
244       }
245     }
246 
247     /* handle the default OpenType features of the default script ... */
248     af_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_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 
359 #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ
360     globals->hb_font = hb_ft_font_create( face, NULL );
361 #endif
362 
363     error = af_face_globals_compute_style_coverage( globals );
364     if ( error )
365     {
366       af_face_globals_free( globals );
367       globals = NULL;
368     }
369     else
370       globals->increase_x_height = AF_PROP_INCREASE_X_HEIGHT_MAX;
371 
372   Exit:
373     *aglobals = globals;
374     return error;
375   }
376 
377 
378   FT_LOCAL_DEF( void )
af_face_globals_free(AF_FaceGlobals globals)379   af_face_globals_free( AF_FaceGlobals  globals )
380   {
381     if ( globals )
382     {
383       FT_Memory  memory = globals->face->memory;
384       FT_UInt    nn;
385 
386 
387       for ( nn = 0; nn < AF_STYLE_MAX; nn++ )
388       {
389         if ( globals->metrics[nn] )
390         {
391           AF_StyleClass          style_class =
392             AF_STYLE_CLASSES_GET[nn];
393           AF_WritingSystemClass  writing_system_class =
394             AF_WRITING_SYSTEM_CLASSES_GET[style_class->writing_system];
395 
396 
397           if ( writing_system_class->style_metrics_done )
398             writing_system_class->style_metrics_done( globals->metrics[nn] );
399 
400           FT_FREE( globals->metrics[nn] );
401         }
402       }
403 
404 #ifdef FT_CONFIG_OPTION_USE_HARFBUZZ
405       hb_font_destroy( globals->hb_font );
406       globals->hb_font = NULL;
407 #endif
408 
409       globals->glyph_count  = 0;
410       globals->glyph_styles = NULL;  /* no need to free this one! */
411       globals->face         = NULL;
412 
413       FT_FREE( globals );
414     }
415   }
416 
417 
418   FT_LOCAL_DEF( FT_Error )
af_face_globals_get_metrics(AF_FaceGlobals globals,FT_UInt gindex,FT_UInt options,AF_StyleMetrics * ametrics)419   af_face_globals_get_metrics( AF_FaceGlobals    globals,
420                                FT_UInt           gindex,
421                                FT_UInt           options,
422                                AF_StyleMetrics  *ametrics )
423   {
424     AF_StyleMetrics  metrics = NULL;
425 
426     AF_Style               style = (AF_Style)options;
427     AF_WritingSystemClass  writing_system_class;
428     AF_StyleClass          style_class;
429 
430     FT_Error  error = FT_Err_Ok;
431 
432 
433     if ( gindex >= (FT_ULong)globals->glyph_count )
434     {
435       error = FT_THROW( Invalid_Argument );
436       goto Exit;
437     }
438 
439     /* if we have a forced style (via `options'), use it, */
440     /* otherwise look into `glyph_styles' array           */
441     if ( style == AF_STYLE_NONE_DFLT || style + 1 >= AF_STYLE_MAX )
442       style = (AF_Style)( globals->glyph_styles[gindex] &
443                           AF_STYLE_UNASSIGNED           );
444 
445     style_class          = AF_STYLE_CLASSES_GET[style];
446     writing_system_class = AF_WRITING_SYSTEM_CLASSES_GET
447                              [style_class->writing_system];
448 
449     metrics = globals->metrics[style];
450     if ( metrics == NULL )
451     {
452       /* create the global metrics object if necessary */
453       FT_Memory  memory = globals->face->memory;
454 
455 
456       if ( FT_ALLOC( metrics, writing_system_class->style_metrics_size ) )
457         goto Exit;
458 
459       metrics->style_class = style_class;
460       metrics->globals     = globals;
461 
462       if ( writing_system_class->style_metrics_init )
463       {
464         error = writing_system_class->style_metrics_init( metrics,
465                                                           globals->face );
466         if ( error )
467         {
468           if ( writing_system_class->style_metrics_done )
469             writing_system_class->style_metrics_done( metrics );
470 
471           FT_FREE( metrics );
472           goto Exit;
473         }
474       }
475 
476       globals->metrics[style] = metrics;
477     }
478 
479   Exit:
480     *ametrics = metrics;
481 
482     return error;
483   }
484 
485 
486   FT_LOCAL_DEF( FT_Bool )
af_face_globals_is_digit(AF_FaceGlobals globals,FT_UInt gindex)487   af_face_globals_is_digit( AF_FaceGlobals  globals,
488                             FT_UInt         gindex )
489   {
490     if ( gindex < (FT_ULong)globals->glyph_count )
491       return (FT_Bool)( globals->glyph_styles[gindex] & AF_DIGIT );
492 
493     return (FT_Bool)0;
494   }
495 
496 
497 /* END */
498