1 /***************************************************************************/
2 /*                                                                         */
3 /*  aftypes.h                                                              */
4 /*                                                                         */
5 /*    Auto-fitter types (specification only).                              */
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   /*************************************************************************
20    *
21    *  The auto-fitter is a complete rewrite of the old auto-hinter.
22    *  Its main feature is the ability to differentiate between different
23    *  writing systems and scripts in order to apply specific rules.
24    *
25    *  The code has also been compartmentized into several entities that
26    *  should make algorithmic experimentation easier than with the old
27    *  code.
28    *
29    *************************************************************************/
30 
31 
32 #ifndef __AFTYPES_H__
33 #define __AFTYPES_H__
34 
35 #include <ft2build.h>
36 
37 #include FT_FREETYPE_H
38 #include FT_OUTLINE_H
39 #include FT_INTERNAL_OBJECTS_H
40 #include FT_INTERNAL_DEBUG_H
41 
42 #include "afblue.h"
43 
44 
45 FT_BEGIN_HEADER
46 
47   /*************************************************************************/
48   /*************************************************************************/
49   /*****                                                               *****/
50   /*****                    D E B U G G I N G                          *****/
51   /*****                                                               *****/
52   /*************************************************************************/
53   /*************************************************************************/
54 
55 #ifdef FT_DEBUG_AUTOFIT
56 
57 #include FT_CONFIG_STANDARD_LIBRARY_H
58 
59 extern int    _af_debug_disable_horz_hints;
60 extern int    _af_debug_disable_vert_hints;
61 extern int    _af_debug_disable_blue_hints;
62 extern void*  _af_debug_hints;
63 
64 #endif /* FT_DEBUG_AUTOFIT */
65 
66 
67   /*************************************************************************/
68   /*************************************************************************/
69   /*****                                                               *****/
70   /*****                 U T I L I T Y   S T U F F                     *****/
71   /*****                                                               *****/
72   /*************************************************************************/
73   /*************************************************************************/
74 
75   typedef struct  AF_WidthRec_
76   {
77     FT_Pos  org;  /* original position/width in font units              */
78     FT_Pos  cur;  /* current/scaled position/width in device sub-pixels */
79     FT_Pos  fit;  /* current/fitted position/width in device sub-pixels */
80 
81   } AF_WidthRec, *AF_Width;
82 
83 
84   FT_LOCAL( void )
85   af_sort_pos( FT_UInt  count,
86                FT_Pos*  table );
87 
88   FT_LOCAL( void )
89   af_sort_and_quantize_widths( FT_UInt*  count,
90                                AF_Width  widths,
91                                FT_Pos    threshold );
92 
93 
94   /*************************************************************************/
95   /*************************************************************************/
96   /*****                                                               *****/
97   /*****                   A N G L E   T Y P E S                       *****/
98   /*****                                                               *****/
99   /*************************************************************************/
100   /*************************************************************************/
101 
102   /*
103    *  The auto-fitter doesn't need a very high angular accuracy;
104    *  this allows us to speed up some computations considerably with a
105    *  light Cordic algorithm (see afangles.c).
106    */
107 
108   typedef FT_Int  AF_Angle;
109 
110 
111 #define AF_ANGLE_PI   256
112 #define AF_ANGLE_2PI  ( AF_ANGLE_PI * 2 )
113 #define AF_ANGLE_PI2  ( AF_ANGLE_PI / 2 )
114 #define AF_ANGLE_PI4  ( AF_ANGLE_PI / 4 )
115 
116 
117 #if 0
118   /*
119    *  compute the angle of a given 2-D vector
120    */
121   FT_LOCAL( AF_Angle )
122   af_angle_atan( FT_Pos  dx,
123                  FT_Pos  dy );
124 
125 
126   /*
127    *  compute `angle2 - angle1'; the result is always within
128    *  the range [-AF_ANGLE_PI .. AF_ANGLE_PI - 1]
129    */
130   FT_LOCAL( AF_Angle )
131   af_angle_diff( AF_Angle  angle1,
132                  AF_Angle  angle2 );
133 #endif /* 0 */
134 
135 
136 #define AF_ANGLE_DIFF( result, angle1, angle2 ) \
137   FT_BEGIN_STMNT                                \
138     AF_Angle  _delta = (angle2) - (angle1);     \
139                                                 \
140                                                 \
141     while ( _delta <= -AF_ANGLE_PI )            \
142       _delta += AF_ANGLE_2PI;                   \
143                                                 \
144     while ( _delta > AF_ANGLE_PI )              \
145       _delta -= AF_ANGLE_2PI;                   \
146                                                 \
147     result = _delta;                            \
148   FT_END_STMNT
149 
150 
151   /*  opaque handle to glyph-specific hints -- see `afhints.h' for more
152    *  details
153    */
154   typedef struct AF_GlyphHintsRec_*  AF_GlyphHints;
155 
156 
157   /*************************************************************************/
158   /*************************************************************************/
159   /*****                                                               *****/
160   /*****                       S C A L E R S                           *****/
161   /*****                                                               *****/
162   /*************************************************************************/
163   /*************************************************************************/
164 
165   /*
166    *  A scaler models the target pixel device that will receive the
167    *  auto-hinted glyph image.
168    */
169 
170 #define AF_SCALER_FLAG_NO_HORIZONTAL  1U /* disable horizontal hinting */
171 #define AF_SCALER_FLAG_NO_VERTICAL    2U /* disable vertical hinting   */
172 #define AF_SCALER_FLAG_NO_ADVANCE     4U /* disable advance hinting    */
173 #define AF_SCALER_FLAG_NO_WARPER      8U /* disable warper             */
174 
175 
176   typedef struct  AF_ScalerRec_
177   {
178     FT_Face         face;        /* source font face                        */
179     FT_Fixed        x_scale;     /* from font units to 1/64th device pixels */
180     FT_Fixed        y_scale;     /* from font units to 1/64th device pixels */
181     FT_Pos          x_delta;     /* in 1/64th device pixels                 */
182     FT_Pos          y_delta;     /* in 1/64th device pixels                 */
183     FT_Render_Mode  render_mode; /* monochrome, anti-aliased, LCD, etc.     */
184     FT_UInt32       flags;       /* additional control flags, see above     */
185 
186   } AF_ScalerRec, *AF_Scaler;
187 
188 
189 #define AF_SCALER_EQUAL_SCALES( a, b )      \
190           ( (a)->x_scale == (b)->x_scale && \
191             (a)->y_scale == (b)->y_scale && \
192             (a)->x_delta == (b)->x_delta && \
193             (a)->y_delta == (b)->y_delta )
194 
195 
196   typedef struct AF_StyleMetricsRec_*  AF_StyleMetrics;
197 
198   /*  This function parses an FT_Face to compute global metrics for
199    *  a specific style.
200    */
201   typedef FT_Error
202   (*AF_WritingSystem_InitMetricsFunc)( AF_StyleMetrics  metrics,
203                                        FT_Face          face );
204 
205   typedef void
206   (*AF_WritingSystem_ScaleMetricsFunc)( AF_StyleMetrics  metrics,
207                                         AF_Scaler        scaler );
208 
209   typedef void
210   (*AF_WritingSystem_DoneMetricsFunc)( AF_StyleMetrics  metrics );
211 
212 
213   typedef FT_Error
214   (*AF_WritingSystem_InitHintsFunc)( AF_GlyphHints    hints,
215                                      AF_StyleMetrics  metrics );
216 
217   typedef void
218   (*AF_WritingSystem_ApplyHintsFunc)( AF_GlyphHints    hints,
219                                       FT_Outline*      outline,
220                                       AF_StyleMetrics  metrics );
221 
222 
223   /*************************************************************************/
224   /*************************************************************************/
225   /*****                                                               *****/
226   /*****                W R I T I N G   S Y S T E M S                  *****/
227   /*****                                                               *****/
228   /*************************************************************************/
229   /*************************************************************************/
230 
231   /*
232    *  For the auto-hinter, a writing system consists of multiple scripts that
233    *  can be handled similarly *in a typographical way*; the relationship is
234    *  not based on history.  For example, both the Greek and the unrelated
235    *  Armenian scripts share the same features like ascender, descender,
236    *  x-height, etc.  Essentially, a writing system is covered by a
237    *  submodule of the auto-fitter; it contains
238    *
239    *  - a specific global analyzer that computes global metrics specific to
240    *    the script (based on script-specific characters to identify ascender
241    *    height, x-height, etc.),
242    *
243    *  - a specific glyph analyzer that computes segments and edges for each
244    *    glyph covered by the script,
245    *
246    *  - a specific grid-fitting algorithm that distorts the scaled glyph
247    *    outline according to the results of the glyph analyzer.
248    */
249 
250 #define __AFWRTSYS_H__  /* don't load header files */
251 #undef  WRITING_SYSTEM
252 #define WRITING_SYSTEM( ws, WS )    \
253           AF_WRITING_SYSTEM_ ## WS,
254 
255   /* The list of known writing systems. */
256   typedef enum  AF_WritingSystem_
257   {
258 
259 #include "afwrtsys.h"
260 
261     AF_WRITING_SYSTEM_MAX   /* do not remove */
262 
263   } AF_WritingSystem;
264 
265 #undef  __AFWRTSYS_H__
266 
267 
268   typedef struct  AF_WritingSystemClassRec_
269   {
270     AF_WritingSystem  writing_system;
271 
272     FT_Offset                          style_metrics_size;
273     AF_WritingSystem_InitMetricsFunc   style_metrics_init;
274     AF_WritingSystem_ScaleMetricsFunc  style_metrics_scale;
275     AF_WritingSystem_DoneMetricsFunc   style_metrics_done;
276 
277     AF_WritingSystem_InitHintsFunc     style_hints_init;
278     AF_WritingSystem_ApplyHintsFunc    style_hints_apply;
279 
280   } AF_WritingSystemClassRec;
281 
282   typedef const AF_WritingSystemClassRec*  AF_WritingSystemClass;
283 
284 
285   /*************************************************************************/
286   /*************************************************************************/
287   /*****                                                               *****/
288   /*****                        S C R I P T S                          *****/
289   /*****                                                               *****/
290   /*************************************************************************/
291   /*************************************************************************/
292 
293   /*
294    *  Each script is associated with a set of Unicode ranges that gets used
295    *  to test whether the font face supports the script.
296    *
297    *  We use four-letter script tags from the OpenType specification,
298    *  extended by `NONE', which indicates `no script'.
299    */
300 
301 #undef  SCRIPT
302 #define SCRIPT( s, S, d, h, sc1, sc2, sc3 ) \
303           AF_SCRIPT_ ## S,
304 
305   /* The list of known scripts. */
306   typedef enum  AF_Script_
307   {
308 
309 #include "afscript.h"
310 
311     AF_SCRIPT_MAX   /* do not remove */
312 
313   } AF_Script;
314 
315 
316   typedef struct  AF_Script_UniRangeRec_
317   {
318     FT_UInt32  first;
319     FT_UInt32  last;
320 
321   } AF_Script_UniRangeRec;
322 
323 #define AF_UNIRANGE_REC( a, b ) { (FT_UInt32)(a), (FT_UInt32)(b) }
324 
325   typedef const AF_Script_UniRangeRec*  AF_Script_UniRange;
326 
327 
328   typedef struct  AF_ScriptClassRec_
329   {
330     AF_Script  script;
331 
332     AF_Script_UniRange  script_uni_ranges; /* last must be { 0, 0 }        */
333 
334     FT_UInt32  standard_char1;             /* for default width and height */
335     FT_UInt32  standard_char2;             /* ditto                        */
336     FT_UInt32  standard_char3;             /* ditto                        */
337 
338   } AF_ScriptClassRec;
339 
340   typedef const AF_ScriptClassRec*  AF_ScriptClass;
341 
342 
343   /*************************************************************************/
344   /*************************************************************************/
345   /*****                                                               *****/
346   /*****                      C O V E R A G E S                        *****/
347   /*****                                                               *****/
348   /*************************************************************************/
349   /*************************************************************************/
350 
351   /*
352    *  Usually, a font contains more glyphs than can be addressed by its
353    *  character map.
354    *
355    *  In the PostScript font world, encoding vectors specific to a given
356    *  task are used to select such glyphs, and these glyphs can be often
357    *  recognized by having a suffix in its glyph names.  For example, a
358    *  superscript glyph `A' might be called `A.sup'.  Unfortunately, this
359    *  naming scheme is not standardized and thus unusable for us.
360    *
361    *  In the OpenType world, a better solution was invented, namely
362    *  `features', which cleanly separate a character's input encoding from
363    *  the corresponding glyph's appearance, and which don't use glyph names
364    *  at all.  For our purposes, and slightly generalized, an OpenType
365    *  feature is a name of a mapping that maps character codes to
366    *  non-standard glyph indices (features get used for other things also).
367    *  For example, the `sups' feature provides superscript glyphs, thus
368    *  mapping character codes like `A' or `B' to superscript glyph
369    *  representation forms.  How this mapping happens is completely
370    *  uninteresting to us.
371    *
372    *  For the auto-hinter, a `coverage' represents all glyphs of an OpenType
373    *  feature collected in a set (as listed below) that can be hinted
374    *  together.  To continue the above example, superscript glyphs must not
375    *  be hinted together with normal glyphs because the blue zones
376    *  completely differ.
377    *
378    *  Note that FreeType itself doesn't compute coverages; it only provides
379    *  the glyphs addressable by the default Unicode character map.  Instead,
380    *  we use the HarfBuzz library (if available), which has many functions
381    *  exactly for this purpose.
382    *
383    *  AF_COVERAGE_DEFAULT is special: It should cover everything that isn't
384    *  listed separately (including the glyphs addressable by the character
385    *  map).  In case HarfBuzz isn't available, it exactly covers the glyphs
386    *  addressable by the character map.
387    *
388    */
389 
390 #undef  COVERAGE
391 #define COVERAGE( name, NAME, description, \
392                   tag1, tag2, tag3, tag4 ) \
393           AF_COVERAGE_ ## NAME,
394 
395 
396   typedef enum  AF_Coverage_
397   {
398 #include "afcover.h"
399 
400     AF_COVERAGE_DEFAULT
401 
402   } AF_Coverage;
403 
404 
405   /*************************************************************************/
406   /*************************************************************************/
407   /*****                                                               *****/
408   /*****                         S T Y L E S                           *****/
409   /*****                                                               *****/
410   /*************************************************************************/
411   /*************************************************************************/
412 
413   /*
414    *  The topmost structure for modelling the auto-hinter glyph input data
415    *  is a `style class', grouping everything together.
416    */
417 
418 #undef  STYLE
419 #define STYLE( s, S, d, ws, sc, ss, c ) \
420           AF_STYLE_ ## S,
421 
422   /* The list of known styles. */
423   typedef enum  AF_Style_
424   {
425 
426 #include "afstyles.h"
427 
428     AF_STYLE_MAX   /* do not remove */
429 
430   } AF_Style;
431 
432 
433   typedef struct  AF_StyleClassRec_
434   {
435     AF_Style  style;
436 
437     AF_WritingSystem   writing_system;
438     AF_Script          script;
439     AF_Blue_Stringset  blue_stringset;
440     AF_Coverage        coverage;
441 
442   } AF_StyleClassRec;
443 
444   typedef const AF_StyleClassRec*  AF_StyleClass;
445 
446 
447   /*************************************************************************/
448   /*************************************************************************/
449   /*****                                                               *****/
450   /*****                   S T Y L E   M E T R I C S                   *****/
451   /*****                                                               *****/
452   /*************************************************************************/
453   /*************************************************************************/
454 
455   typedef struct AF_FaceGlobalsRec_*  AF_FaceGlobals;
456 
457   /* This is the main structure that combines everything.  Autofit modules */
458   /* specific to writing systems derive their structures from it, for      */
459   /* example `AF_LatinMetrics'.                                            */
460 
461   typedef struct  AF_StyleMetricsRec_
462   {
463     AF_StyleClass   style_class;
464     AF_ScalerRec    scaler;
465     FT_Bool         digits_have_same_width;
466 
467     AF_FaceGlobals  globals;    /* to access properties */
468 
469   } AF_StyleMetricsRec;
470 
471 
472   /* Declare and define vtables for classes */
473 #ifndef FT_CONFIG_OPTION_PIC
474 
475 #define AF_DECLARE_WRITING_SYSTEM_CLASS( writing_system_class ) \
476   FT_CALLBACK_TABLE const AF_WritingSystemClassRec              \
477   writing_system_class;
478 
479 #define AF_DEFINE_WRITING_SYSTEM_CLASS(                  \
480           writing_system_class,                          \
481           system,                                        \
482           m_size,                                        \
483           m_init,                                        \
484           m_scale,                                       \
485           m_done,                                        \
486           h_init,                                        \
487           h_apply )                                      \
488   FT_CALLBACK_TABLE_DEF                                  \
489   const AF_WritingSystemClassRec  writing_system_class = \
490   {                                                      \
491     system,                                              \
492                                                          \
493     m_size,                                              \
494                                                          \
495     m_init,                                              \
496     m_scale,                                             \
497     m_done,                                              \
498                                                          \
499     h_init,                                              \
500     h_apply                                              \
501   };
502 
503 
504 #define AF_DECLARE_SCRIPT_CLASS( script_class ) \
505   FT_CALLBACK_TABLE const AF_ScriptClassRec     \
506   script_class;
507 
508 #define AF_DEFINE_SCRIPT_CLASS(           \
509           script_class,                   \
510           script,                         \
511           ranges,                         \
512           std_char1,                      \
513           std_char2,                      \
514           std_char3 )                     \
515   FT_CALLBACK_TABLE_DEF                   \
516   const AF_ScriptClassRec  script_class = \
517   {                                       \
518     script,                               \
519     ranges,                               \
520     std_char1,                            \
521     std_char2,                            \
522     std_char3                             \
523   };
524 
525 
526 #define AF_DECLARE_STYLE_CLASS( style_class ) \
527   FT_CALLBACK_TABLE const AF_StyleClassRec    \
528   style_class;
529 
530 #define AF_DEFINE_STYLE_CLASS(          \
531           style_class,                  \
532           style,                        \
533           writing_system,               \
534           script,                       \
535           blue_stringset,               \
536           coverage )                    \
537   FT_CALLBACK_TABLE_DEF                 \
538   const AF_StyleClassRec  style_class = \
539   {                                     \
540     style,                              \
541     writing_system,                     \
542     script,                             \
543     blue_stringset,                     \
544     coverage                            \
545   };
546 
547 #else /* FT_CONFIG_OPTION_PIC */
548 
549 #define AF_DECLARE_WRITING_SYSTEM_CLASS( writing_system_class )            \
550   FT_LOCAL( void )                                                         \
551   FT_Init_Class_ ## writing_system_class( AF_WritingSystemClassRec*  ac );
552 
553 #define AF_DEFINE_WRITING_SYSTEM_CLASS(                                   \
554           writing_system_class,                                           \
555           system,                                                         \
556           m_size,                                                         \
557           m_init,                                                         \
558           m_scale,                                                        \
559           m_done,                                                         \
560           h_init,                                                         \
561           h_apply )                                                       \
562   FT_LOCAL_DEF( void )                                                    \
563   FT_Init_Class_ ## writing_system_class( AF_WritingSystemClassRec*  ac ) \
564   {                                                                       \
565     ac->writing_system      = system;                                     \
566                                                                           \
567     ac->style_metrics_size  = m_size;                                     \
568                                                                           \
569     ac->style_metrics_init  = m_init;                                     \
570     ac->style_metrics_scale = m_scale;                                    \
571     ac->style_metrics_done  = m_done;                                     \
572                                                                           \
573     ac->style_hints_init    = h_init;                                     \
574     ac->style_hints_apply   = h_apply;                                    \
575   }
576 
577 
578 #define AF_DECLARE_SCRIPT_CLASS( script_class )             \
579   FT_LOCAL( void )                                          \
580   FT_Init_Class_ ## script_class( AF_ScriptClassRec*  ac );
581 
582 #define AF_DEFINE_SCRIPT_CLASS(                            \
583           script_class,                                    \
584           script_,                                         \
585           ranges,                                          \
586           std_char1,                                       \
587           std_char2,                                       \
588           std_char3 )                                      \
589   FT_LOCAL_DEF( void )                                     \
590   FT_Init_Class_ ## script_class( AF_ScriptClassRec*  ac ) \
591   {                                                        \
592     ac->script            = script_;                       \
593     ac->script_uni_ranges = ranges;                        \
594     ac->standard_char1    = std_char1;                     \
595     ac->standard_char2    = std_char2;                     \
596     ac->standard_char3    = std_char3;                     \
597   }
598 
599 
600 #define AF_DECLARE_STYLE_CLASS( style_class )             \
601   FT_LOCAL( void )                                        \
602   FT_Init_Class_ ## style_class( AF_StyleClassRec*  ac );
603 
604 #define AF_DEFINE_STYLE_CLASS(                           \
605           style_class,                                   \
606           style_,                                        \
607           writing_system_,                               \
608           script_,                                       \
609           blue_stringset_,                               \
610           coverage_ )                                    \
611   FT_LOCAL_DEF( void )                                   \
612   FT_Init_Class_ ## style_class( AF_StyleClassRec*  ac ) \
613   {                                                      \
614     ac->style          = style_;                         \
615     ac->writing_system = writing_system_;                \
616     ac->script         = script_;                        \
617     ac->blue_stringset = blue_stringset_;                \
618     ac->coverage       = coverage_;                      \
619   }
620 
621 #endif /* FT_CONFIG_OPTION_PIC */
622 
623 
624 /* */
625 
626 FT_END_HEADER
627 
628 #endif /* __AFTYPES_H__ */
629 
630 
631 /* END */
632