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