1 /***************************************************************************/
2 /*                                                                         */
3 /*  afmodule.c                                                             */
4 /*                                                                         */
5 /*    Auto-fitter module implementation (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 "afmodule.h"
21 #include "afloader.h"
22 #include "aferrors.h"
23 #include "afpic.h"
24 
25 #ifdef FT_DEBUG_AUTOFIT
26 
27 #ifndef FT_MAKE_OPTION_SINGLE_OBJECT
28 
29 #ifdef __cplusplus
30   extern "C" {
31 #endif
32   extern void
33   af_glyph_hints_dump_segments( AF_GlyphHints  hints,
34                                 FT_Bool        to_stdout );
35   extern void
36   af_glyph_hints_dump_points( AF_GlyphHints  hints,
37                               FT_Bool        to_stdout );
38   extern void
39   af_glyph_hints_dump_edges( AF_GlyphHints  hints,
40                              FT_Bool        to_stdout );
41 #ifdef __cplusplus
42   }
43 #endif
44 
45 #endif
46 
47   int  _af_debug_disable_horz_hints;
48   int  _af_debug_disable_vert_hints;
49   int  _af_debug_disable_blue_hints;
50 
51   /* we use a global object instead of a local one for debugging */
52   AF_GlyphHintsRec  _af_debug_hints_rec[1];
53 
54   void*  _af_debug_hints = _af_debug_hints_rec;
55 #endif
56 
57 #include FT_INTERNAL_OBJECTS_H
58 #include FT_INTERNAL_DEBUG_H
59 #include FT_AUTOHINTER_H
60 #include FT_SERVICE_PROPERTIES_H
61 
62 
63   /*************************************************************************/
64   /*                                                                       */
65   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
66   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
67   /* messages during execution.                                            */
68   /*                                                                       */
69 #undef  FT_COMPONENT
70 #define FT_COMPONENT  trace_afmodule
71 
72 
73   static FT_Error
af_property_get_face_globals(FT_Face face,AF_FaceGlobals * aglobals,AF_Module module)74   af_property_get_face_globals( FT_Face          face,
75                                 AF_FaceGlobals*  aglobals,
76                                 AF_Module        module )
77   {
78     FT_Error        error = FT_Err_Ok;
79     AF_FaceGlobals  globals;
80 
81 
82     if ( !face )
83       return FT_THROW( Invalid_Face_Handle );
84 
85     globals = (AF_FaceGlobals)face->autohint.data;
86     if ( !globals )
87     {
88       /* trigger computation of the global style data */
89       /* in case it hasn't been done yet              */
90       error = af_face_globals_new( face, &globals, module );
91       if ( !error )
92       {
93         face->autohint.data =
94           (FT_Pointer)globals;
95         face->autohint.finalizer =
96           (FT_Generic_Finalizer)af_face_globals_free;
97       }
98     }
99 
100     if ( !error )
101       *aglobals = globals;
102 
103     return error;
104   }
105 
106 
107   static FT_Error
af_property_set(FT_Module ft_module,const char * property_name,const void * value)108   af_property_set( FT_Module    ft_module,
109                    const char*  property_name,
110                    const void*  value )
111   {
112     FT_Error   error  = FT_Err_Ok;
113     AF_Module  module = (AF_Module)ft_module;
114 
115 
116     if ( !ft_strcmp( property_name, "fallback-script" ) )
117     {
118       FT_UInt*  fallback_script = (FT_UInt*)value;
119 
120       FT_UInt  ss;
121 
122 
123       /* We translate the fallback script to a fallback style that uses */
124       /* `fallback-script' as its script and `AF_COVERAGE_NONE' as its  */
125       /* coverage value.                                                */
126       for ( ss = 0; AF_STYLE_CLASSES_GET[ss]; ss++ )
127       {
128         AF_StyleClass  style_class = AF_STYLE_CLASSES_GET[ss];
129 
130 
131         if ( (FT_UInt)style_class->script == *fallback_script &&
132              style_class->coverage == AF_COVERAGE_DEFAULT     )
133         {
134           module->fallback_style = ss;
135           break;
136         }
137       }
138 
139       if ( !AF_STYLE_CLASSES_GET[ss] )
140       {
141         FT_TRACE0(( "af_property_set: Invalid value %d for property `%s'\n",
142                     fallback_script, property_name ));
143         return FT_THROW( Invalid_Argument );
144       }
145 
146       return error;
147     }
148     else if ( !ft_strcmp( property_name, "default-script" ) )
149     {
150       FT_UInt*  default_script = (FT_UInt*)value;
151 
152 
153       module->default_script = *default_script;
154 
155       return error;
156     }
157     else if ( !ft_strcmp( property_name, "increase-x-height" ) )
158     {
159       FT_Prop_IncreaseXHeight*  prop = (FT_Prop_IncreaseXHeight*)value;
160       AF_FaceGlobals            globals;
161 
162 
163       error = af_property_get_face_globals( prop->face, &globals, module );
164       if ( !error )
165         globals->increase_x_height = prop->limit;
166 
167       return error;
168     }
169 #ifdef AF_CONFIG_OPTION_USE_WARPER
170     else if ( !ft_strcmp( property_name, "warping" ) )
171     {
172       FT_Bool*  warping = (FT_Bool*)value;
173 
174 
175       module->warping = *warping;
176 
177       return error;
178     }
179 #endif /* AF_CONFIG_OPTION_USE_WARPER */
180 
181     FT_TRACE0(( "af_property_set: missing property `%s'\n",
182                 property_name ));
183     return FT_THROW( Missing_Property );
184   }
185 
186 
187   static FT_Error
af_property_get(FT_Module ft_module,const char * property_name,void * value)188   af_property_get( FT_Module    ft_module,
189                    const char*  property_name,
190                    void*        value )
191   {
192     FT_Error   error          = FT_Err_Ok;
193     AF_Module  module         = (AF_Module)ft_module;
194     FT_UInt    fallback_style = module->fallback_style;
195     FT_UInt    default_script = module->default_script;
196 #ifdef AF_CONFIG_OPTION_USE_WARPER
197     FT_Bool    warping        = module->warping;
198 #endif
199 
200 
201     if ( !ft_strcmp( property_name, "glyph-to-script-map" ) )
202     {
203       FT_Prop_GlyphToScriptMap*  prop = (FT_Prop_GlyphToScriptMap*)value;
204       AF_FaceGlobals             globals;
205 
206 
207       error = af_property_get_face_globals( prop->face, &globals, module );
208       if ( !error )
209         prop->map = globals->glyph_styles;
210 
211       return error;
212     }
213     else if ( !ft_strcmp( property_name, "fallback-script" ) )
214     {
215       FT_UInt*  val = (FT_UInt*)value;
216 
217       AF_StyleClass  style_class = AF_STYLE_CLASSES_GET[fallback_style];
218 
219 
220       *val = style_class->script;
221 
222       return error;
223     }
224     else if ( !ft_strcmp( property_name, "default-script" ) )
225     {
226       FT_UInt*  val = (FT_UInt*)value;
227 
228 
229       *val = default_script;
230 
231       return error;
232     }
233     else if ( !ft_strcmp( property_name, "increase-x-height" ) )
234     {
235       FT_Prop_IncreaseXHeight*  prop = (FT_Prop_IncreaseXHeight*)value;
236       AF_FaceGlobals            globals;
237 
238 
239       error = af_property_get_face_globals( prop->face, &globals, module );
240       if ( !error )
241         prop->limit = globals->increase_x_height;
242 
243       return error;
244     }
245 #ifdef AF_CONFIG_OPTION_USE_WARPER
246     else if ( !ft_strcmp( property_name, "warping" ) )
247     {
248       FT_Bool*  val = (FT_Bool*)value;
249 
250 
251       *val = warping;
252 
253       return error;
254     }
255 #endif /* AF_CONFIG_OPTION_USE_WARPER */
256 
257     FT_TRACE0(( "af_property_get: missing property `%s'\n",
258                 property_name ));
259     return FT_THROW( Missing_Property );
260   }
261 
262 
263   FT_DEFINE_SERVICE_PROPERTIESREC(
264     af_service_properties,
265     (FT_Properties_SetFunc)af_property_set,
266     (FT_Properties_GetFunc)af_property_get )
267 
268 
269   FT_DEFINE_SERVICEDESCREC1(
270     af_services,
271     FT_SERVICE_ID_PROPERTIES, &AF_SERVICE_PROPERTIES_GET )
272 
273 
FT_CALLBACK_DEF(FT_Module_Interface)274   FT_CALLBACK_DEF( FT_Module_Interface )
275   af_get_interface( FT_Module    module,
276                     const char*  module_interface )
277   {
278     /* AF_SERVICES_GET dereferences `library' in PIC mode */
279 #ifdef FT_CONFIG_OPTION_PIC
280     FT_Library  library;
281 
282 
283     if ( !module )
284       return NULL;
285     library = module->library;
286     if ( !library )
287       return NULL;
288 #else
289     FT_UNUSED( module );
290 #endif
291 
292     return ft_service_list_lookup( AF_SERVICES_GET, module_interface );
293   }
294 
295 
296   FT_CALLBACK_DEF( FT_Error )
af_autofitter_init(FT_Module ft_module)297   af_autofitter_init( FT_Module  ft_module )      /* AF_Module */
298   {
299     AF_Module  module = (AF_Module)ft_module;
300 
301 
302     module->fallback_style = AF_STYLE_FALLBACK;
303     module->default_script = AF_SCRIPT_DEFAULT;
304 #ifdef AF_CONFIG_OPTION_USE_WARPER
305     module->warping        = 0;
306 #endif
307 
308     return FT_Err_Ok;
309   }
310 
311 
312   FT_CALLBACK_DEF( void )
af_autofitter_done(FT_Module ft_module)313   af_autofitter_done( FT_Module  ft_module )      /* AF_Module */
314   {
315     FT_UNUSED( ft_module );
316 
317 #ifdef FT_DEBUG_AUTOFIT
318     if ( _af_debug_hints_rec->memory )
319       af_glyph_hints_done( _af_debug_hints_rec );
320 #endif
321   }
322 
323 
324   FT_CALLBACK_DEF( FT_Error )
af_autofitter_load_glyph(AF_Module module,FT_GlyphSlot slot,FT_Size size,FT_UInt glyph_index,FT_Int32 load_flags)325   af_autofitter_load_glyph( AF_Module     module,
326                             FT_GlyphSlot  slot,
327                             FT_Size       size,
328                             FT_UInt       glyph_index,
329                             FT_Int32      load_flags )
330   {
331     FT_Error   error  = FT_Err_Ok;
332     FT_Memory  memory = module->root.library->memory;
333 
334 #ifdef FT_DEBUG_AUTOFIT
335 
336     /* in debug mode, we use a global object that survives this routine */
337 
338     AF_GlyphHints  hints = _af_debug_hints_rec;
339     AF_LoaderRec   loader[1];
340 
341     FT_UNUSED( size );
342 
343 
344     if ( hints->memory )
345       af_glyph_hints_done( hints );
346 
347     af_glyph_hints_init( hints, memory );
348     af_loader_init( loader, hints );
349 
350     error = af_loader_load_glyph( loader, module, slot->face,
351                                   glyph_index, load_flags );
352 
353     af_glyph_hints_dump_points( hints, 0 );
354     af_glyph_hints_dump_segments( hints, 0 );
355     af_glyph_hints_dump_edges( hints, 0 );
356 
357     af_loader_done( loader );
358 
359     return error;
360 
361 #else /* !FT_DEBUG_AUTOFIT */
362 
363     AF_GlyphHintsRec  hints[1];
364     AF_LoaderRec      loader[1];
365 
366     FT_UNUSED( size );
367 
368 
369     af_glyph_hints_init( hints, memory );
370     af_loader_init( loader, hints );
371 
372     error = af_loader_load_glyph( loader, module, slot->face,
373                                   glyph_index, load_flags );
374 
375     af_loader_done( loader );
376     af_glyph_hints_done( hints );
377 
378     return error;
379 
380 #endif /* !FT_DEBUG_AUTOFIT */
381   }
382 
383 
384   FT_DEFINE_AUTOHINTER_INTERFACE(
385     af_autofitter_interface,
386     NULL,                                                    /* reset_face */
387     NULL,                                              /* get_global_hints */
388     NULL,                                             /* done_global_hints */
389     (FT_AutoHinter_GlyphLoadFunc)af_autofitter_load_glyph )  /* load_glyph */
390 
391 
392   FT_DEFINE_MODULE(
393     autofit_module_class,
394 
395     FT_MODULE_HINTER,
396     sizeof ( AF_ModuleRec ),
397 
398     "autofitter",
399     0x10000L,   /* version 1.0 of the autofitter  */
400     0x20000L,   /* requires FreeType 2.0 or above */
401 
402     (const void*)&AF_INTERFACE_GET,
403 
404     (FT_Module_Constructor)af_autofitter_init,
405     (FT_Module_Destructor) af_autofitter_done,
406     (FT_Module_Requester)  af_get_interface )
407 
408 
409 /* END */
410