• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************/
2 /*                                                                         */
3 /*  afloader.c                                                             */
4 /*                                                                         */
5 /*    Auto-fitter glyph loading routines (body).                           */
6 /*                                                                         */
7 /*  Copyright 2003, 2004, 2005, 2006, 2007, 2008 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 "afloader.h"
20 #include "afhints.h"
21 #include "afglobal.h"
22 #include "aflatin.h"
23 #include "aferrors.h"
24 
25 
26   FT_LOCAL_DEF( FT_Error )
af_loader_init(AF_Loader loader,FT_Memory memory)27   af_loader_init( AF_Loader  loader,
28                   FT_Memory  memory )
29   {
30     FT_ZERO( loader );
31 
32     af_glyph_hints_init( &loader->hints, memory );
33 #ifdef AF_DEBUG
34     _af_debug_hints = &loader->hints;
35 #endif
36     return FT_GlyphLoader_New( memory, &loader->gloader );
37   }
38 
39 
40   FT_LOCAL_DEF( FT_Error )
af_loader_reset(AF_Loader loader,FT_Face face)41   af_loader_reset( AF_Loader  loader,
42                    FT_Face    face )
43   {
44     FT_Error  error = AF_Err_Ok;
45 
46 
47     loader->face    = face;
48     loader->globals = (AF_FaceGlobals)face->autohint.data;
49 
50     FT_GlyphLoader_Rewind( loader->gloader );
51 
52     if ( loader->globals == NULL )
53     {
54       error = af_face_globals_new( face, &loader->globals );
55       if ( !error )
56       {
57         face->autohint.data =
58           (FT_Pointer)loader->globals;
59         face->autohint.finalizer =
60           (FT_Generic_Finalizer)af_face_globals_free;
61       }
62     }
63 
64     return error;
65   }
66 
67 
68   FT_LOCAL_DEF( void )
af_loader_done(AF_Loader loader)69   af_loader_done( AF_Loader  loader )
70   {
71     af_glyph_hints_done( &loader->hints );
72 
73     loader->face    = NULL;
74     loader->globals = NULL;
75 
76 #ifdef AF_DEBUG
77     _af_debug_hints = NULL;
78 #endif
79     FT_GlyphLoader_Done( loader->gloader );
80     loader->gloader = NULL;
81   }
82 
83 
84   static FT_Error
af_loader_load_g(AF_Loader loader,AF_Scaler scaler,FT_UInt glyph_index,FT_Int32 load_flags,FT_UInt depth)85   af_loader_load_g( AF_Loader  loader,
86                     AF_Scaler  scaler,
87                     FT_UInt    glyph_index,
88                     FT_Int32   load_flags,
89                     FT_UInt    depth )
90   {
91     FT_Error          error;
92     FT_Face           face     = loader->face;
93     FT_GlyphLoader    gloader  = loader->gloader;
94     AF_ScriptMetrics  metrics  = loader->metrics;
95     AF_GlyphHints     hints    = &loader->hints;
96     FT_GlyphSlot      slot     = face->glyph;
97     FT_Slot_Internal  internal = slot->internal;
98 
99 
100     error = FT_Load_Glyph( face, glyph_index, load_flags );
101     if ( error )
102       goto Exit;
103 
104     loader->transformed = internal->glyph_transformed;
105     if ( loader->transformed )
106     {
107       FT_Matrix  inverse;
108 
109 
110       loader->trans_matrix = internal->glyph_matrix;
111       loader->trans_delta  = internal->glyph_delta;
112 
113       inverse = loader->trans_matrix;
114       FT_Matrix_Invert( &inverse );
115       FT_Vector_Transform( &loader->trans_delta, &inverse );
116     }
117 
118     /* set linear metrics */
119     slot->linearHoriAdvance = slot->metrics.horiAdvance;
120     slot->linearVertAdvance = slot->metrics.vertAdvance;
121 
122     switch ( slot->format )
123     {
124     case FT_GLYPH_FORMAT_OUTLINE:
125       /* translate the loaded glyph when an internal transform is needed */
126       if ( loader->transformed )
127         FT_Outline_Translate( &slot->outline,
128                               loader->trans_delta.x,
129                               loader->trans_delta.y );
130 
131       /* copy the outline points in the loader's current               */
132       /* extra points which is used to keep original glyph coordinates */
133       error = FT_GLYPHLOADER_CHECK_POINTS( gloader,
134                                            slot->outline.n_points + 4,
135                                            slot->outline.n_contours );
136       if ( error )
137         goto Exit;
138 
139       FT_ARRAY_COPY( gloader->current.outline.points,
140                      slot->outline.points,
141                      slot->outline.n_points );
142 
143       FT_ARRAY_COPY( gloader->current.outline.contours,
144                      slot->outline.contours,
145                      slot->outline.n_contours );
146 
147       FT_ARRAY_COPY( gloader->current.outline.tags,
148                      slot->outline.tags,
149                      slot->outline.n_points );
150 
151       gloader->current.outline.n_points   = slot->outline.n_points;
152       gloader->current.outline.n_contours = slot->outline.n_contours;
153 
154       /* compute original horizontal phantom points (and ignore */
155       /* vertical ones)                                         */
156       loader->pp1.x = hints->x_delta;
157       loader->pp1.y = hints->y_delta;
158       loader->pp2.x = FT_MulFix( slot->metrics.horiAdvance,
159                                  hints->x_scale ) + hints->x_delta;
160       loader->pp2.y = hints->y_delta;
161 
162       /* be sure to check for spacing glyphs */
163       if ( slot->outline.n_points == 0 )
164         goto Hint_Metrics;
165 
166       /* now load the slot image into the auto-outline and run the */
167       /* automatic hinting process                                 */
168       if ( metrics->clazz->script_hints_apply )
169         metrics->clazz->script_hints_apply( hints,
170                                             &gloader->current.outline,
171                                             metrics );
172 
173       /* we now need to hint the metrics according to the change in */
174       /* width/positioning that occurred during the hinting process */
175       if ( scaler->render_mode != FT_RENDER_MODE_LIGHT )
176       {
177         FT_Pos        old_rsb, old_lsb, new_lsb;
178         FT_Pos        pp1x_uh, pp2x_uh;
179         AF_AxisHints  axis  = &hints->axis[AF_DIMENSION_HORZ];
180         AF_Edge       edge1 = axis->edges;         /* leftmost edge  */
181         AF_Edge       edge2 = edge1 +
182                               axis->num_edges - 1; /* rightmost edge */
183 
184 
185         if ( axis->num_edges > 1 && AF_HINTS_DO_ADVANCE( hints ) )
186         {
187           old_rsb     = loader->pp2.x - edge2->opos;
188           old_lsb     = edge1->opos;
189           new_lsb     = edge1->pos;
190 
191           /* remember unhinted values to later account */
192           /* for rounding errors                       */
193 
194           pp1x_uh = new_lsb    - old_lsb;
195           pp2x_uh = edge2->pos + old_rsb;
196 
197           /* prefer too much space over too little space */
198           /* for very small sizes                        */
199 
200           if ( old_lsb < 24 )
201             pp1x_uh -= 8;
202 
203           if ( old_rsb < 24 )
204             pp2x_uh += 8;
205 
206           loader->pp1.x = FT_PIX_ROUND( pp1x_uh );
207           loader->pp2.x = FT_PIX_ROUND( pp2x_uh );
208 
209           if ( loader->pp1.x >= new_lsb && old_lsb > 0 )
210             loader->pp1.x -= 64;
211 
212           if ( loader->pp2.x <= edge2->pos && old_rsb > 0 )
213             loader->pp2.x += 64;
214 
215           slot->lsb_delta = loader->pp1.x - pp1x_uh;
216           slot->rsb_delta = loader->pp2.x - pp2x_uh;
217         }
218         else
219         {
220           FT_Pos   pp1x = loader->pp1.x;
221           FT_Pos   pp2x = loader->pp2.x;
222 
223           loader->pp1.x = FT_PIX_ROUND( pp1x );
224           loader->pp2.x = FT_PIX_ROUND( pp2x );
225 
226           slot->lsb_delta = loader->pp1.x - pp1x;
227           slot->rsb_delta = loader->pp2.x - pp2x;
228         }
229       }
230       else
231       {
232         FT_Pos   pp1x = loader->pp1.x;
233         FT_Pos   pp2x = loader->pp2.x;
234 
235         loader->pp1.x = FT_PIX_ROUND( pp1x + hints->xmin_delta );
236         loader->pp2.x = FT_PIX_ROUND( pp2x + hints->xmax_delta );
237 
238         slot->lsb_delta = loader->pp1.x - pp1x;
239         slot->rsb_delta = loader->pp2.x - pp2x;
240       }
241 
242       /* good, we simply add the glyph to our loader's base */
243       FT_GlyphLoader_Add( gloader );
244       break;
245 
246     case FT_GLYPH_FORMAT_COMPOSITE:
247       {
248         FT_UInt      nn, num_subglyphs = slot->num_subglyphs;
249         FT_UInt      num_base_subgs, start_point;
250         FT_SubGlyph  subglyph;
251 
252 
253         start_point = gloader->base.outline.n_points;
254 
255         /* first of all, copy the subglyph descriptors in the glyph loader */
256         error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs );
257         if ( error )
258           goto Exit;
259 
260         FT_ARRAY_COPY( gloader->current.subglyphs,
261                        slot->subglyphs,
262                        num_subglyphs );
263 
264         gloader->current.num_subglyphs = num_subglyphs;
265         num_base_subgs                 = gloader->base.num_subglyphs;
266 
267         /* now, read each subglyph independently */
268         for ( nn = 0; nn < num_subglyphs; nn++ )
269         {
270           FT_Vector  pp1, pp2;
271           FT_Pos     x, y;
272           FT_UInt    num_points, num_new_points, num_base_points;
273 
274 
275           /* gloader.current.subglyphs can change during glyph loading due */
276           /* to re-allocation -- we must recompute the current subglyph on */
277           /* each iteration                                                */
278           subglyph = gloader->base.subglyphs + num_base_subgs + nn;
279 
280           pp1 = loader->pp1;
281           pp2 = loader->pp2;
282 
283           num_base_points = gloader->base.outline.n_points;
284 
285           error = af_loader_load_g( loader, scaler, subglyph->index,
286                                     load_flags, depth + 1 );
287           if ( error )
288             goto Exit;
289 
290           /* recompute subglyph pointer */
291           subglyph = gloader->base.subglyphs + num_base_subgs + nn;
292 
293           if ( subglyph->flags & FT_SUBGLYPH_FLAG_USE_MY_METRICS )
294           {
295             pp1 = loader->pp1;
296             pp2 = loader->pp2;
297           }
298           else
299           {
300             loader->pp1 = pp1;
301             loader->pp2 = pp2;
302           }
303 
304           num_points     = gloader->base.outline.n_points;
305           num_new_points = num_points - num_base_points;
306 
307           /* now perform the transform required for this subglyph */
308 
309           if ( subglyph->flags & ( FT_SUBGLYPH_FLAG_SCALE    |
310                                    FT_SUBGLYPH_FLAG_XY_SCALE |
311                                    FT_SUBGLYPH_FLAG_2X2      ) )
312           {
313             FT_Vector*  cur   = gloader->base.outline.points +
314                                 num_base_points;
315             FT_Vector*  limit = cur + num_new_points;
316 
317 
318             for ( ; cur < limit; cur++ )
319               FT_Vector_Transform( cur, &subglyph->transform );
320           }
321 
322           /* apply offset */
323 
324           if ( !( subglyph->flags & FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES ) )
325           {
326             FT_Int      k = subglyph->arg1;
327             FT_UInt     l = subglyph->arg2;
328             FT_Vector*  p1;
329             FT_Vector*  p2;
330 
331 
332             if ( start_point + k >= num_base_points         ||
333                                l >= (FT_UInt)num_new_points )
334             {
335               error = AF_Err_Invalid_Composite;
336               goto Exit;
337             }
338 
339             l += num_base_points;
340 
341             /* for now, only use the current point coordinates;    */
342             /* we may consider another approach in the near future */
343             p1 = gloader->base.outline.points + start_point + k;
344             p2 = gloader->base.outline.points + start_point + l;
345 
346             x = p1->x - p2->x;
347             y = p1->y - p2->y;
348           }
349           else
350           {
351             x = FT_MulFix( subglyph->arg1, hints->x_scale ) + hints->x_delta;
352             y = FT_MulFix( subglyph->arg2, hints->y_scale ) + hints->y_delta;
353 
354             x = FT_PIX_ROUND( x );
355             y = FT_PIX_ROUND( y );
356           }
357 
358           {
359             FT_Outline  dummy = gloader->base.outline;
360 
361 
362             dummy.points  += num_base_points;
363             dummy.n_points = (short)num_new_points;
364 
365             FT_Outline_Translate( &dummy, x, y );
366           }
367         }
368       }
369       break;
370 
371     default:
372       /* we don't support other formats (yet?) */
373       error = AF_Err_Unimplemented_Feature;
374     }
375 
376   Hint_Metrics:
377     if ( depth == 0 )
378     {
379       FT_BBox    bbox;
380       FT_Vector  vvector;
381 
382 
383       vvector.x = slot->metrics.vertBearingX - slot->metrics.horiBearingX;
384       vvector.y = slot->metrics.vertBearingY - slot->metrics.horiBearingY;
385       vvector.x = FT_MulFix( vvector.x, metrics->scaler.x_scale );
386       vvector.y = FT_MulFix( vvector.y, metrics->scaler.y_scale );
387 
388       /* transform the hinted outline if needed */
389       if ( loader->transformed )
390       {
391         FT_Outline_Transform( &gloader->base.outline, &loader->trans_matrix );
392         FT_Vector_Transform( &vvector, &loader->trans_matrix );
393       }
394 #if 1
395       /* we must translate our final outline by -pp1.x and compute */
396       /* the new metrics                                           */
397       if ( loader->pp1.x )
398         FT_Outline_Translate( &gloader->base.outline, -loader->pp1.x, 0 );
399 #endif
400       FT_Outline_Get_CBox( &gloader->base.outline, &bbox );
401 
402       bbox.xMin = FT_PIX_FLOOR( bbox.xMin );
403       bbox.yMin = FT_PIX_FLOOR( bbox.yMin );
404       bbox.xMax = FT_PIX_CEIL(  bbox.xMax );
405       bbox.yMax = FT_PIX_CEIL(  bbox.yMax );
406 
407       slot->metrics.width        = bbox.xMax - bbox.xMin;
408       slot->metrics.height       = bbox.yMax - bbox.yMin;
409       slot->metrics.horiBearingX = bbox.xMin;
410       slot->metrics.horiBearingY = bbox.yMax;
411 
412       slot->metrics.vertBearingX = FT_PIX_FLOOR( bbox.xMin + vvector.x );
413       slot->metrics.vertBearingY = FT_PIX_FLOOR( bbox.yMax + vvector.y );
414 
415       /* for mono-width fonts (like Andale, Courier, etc.) we need */
416       /* to keep the original rounded advance width                */
417 #if 0
418       if ( !FT_IS_FIXED_WIDTH( slot->face ) )
419         slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
420       else
421         slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance,
422                                                x_scale );
423 #else
424       if ( !FT_IS_FIXED_WIDTH( slot->face ) )
425       {
426         /* non-spacing glyphs must stay as-is */
427         if ( slot->metrics.horiAdvance )
428           slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
429       }
430       else
431       {
432         slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance,
433                                                metrics->scaler.x_scale );
434 
435         /* Set delta values to 0.  Otherwise code that uses them is */
436         /* going to ruin the fixed advance width.                   */
437         slot->lsb_delta = 0;
438         slot->rsb_delta = 0;
439       }
440 #endif
441 
442       slot->metrics.vertAdvance = FT_MulFix( slot->metrics.vertAdvance,
443                                               metrics->scaler.y_scale );
444 
445       slot->metrics.horiAdvance = FT_PIX_ROUND( slot->metrics.horiAdvance );
446       slot->metrics.vertAdvance = FT_PIX_ROUND( slot->metrics.vertAdvance );
447 
448       /* now copy outline into glyph slot */
449       FT_GlyphLoader_Rewind( internal->loader );
450       error = FT_GlyphLoader_CopyPoints( internal->loader, gloader );
451       if ( error )
452         goto Exit;
453 
454       slot->outline = internal->loader->base.outline;
455       slot->format  = FT_GLYPH_FORMAT_OUTLINE;
456     }
457 
458 #ifdef DEBUG_HINTER
459     af_debug_hinter = hinter;
460 #endif
461 
462   Exit:
463     return error;
464   }
465 
466 
467   FT_LOCAL_DEF( FT_Error )
af_loader_load_glyph(AF_Loader loader,FT_Face face,FT_UInt gindex,FT_UInt32 load_flags)468   af_loader_load_glyph( AF_Loader  loader,
469                         FT_Face    face,
470                         FT_UInt    gindex,
471                         FT_UInt32  load_flags )
472   {
473     FT_Error      error;
474     FT_Size       size = face->size;
475     AF_ScalerRec  scaler;
476 
477 
478     if ( !size )
479       return AF_Err_Invalid_Argument;
480 
481     FT_ZERO( &scaler );
482 
483     scaler.face    = face;
484     scaler.x_scale = size->metrics.x_scale;
485     scaler.x_delta = 0;  /* XXX: TODO: add support for sub-pixel hinting */
486     scaler.y_scale = size->metrics.y_scale;
487     scaler.y_delta = 0;  /* XXX: TODO: add support for sub-pixel hinting */
488 
489     scaler.render_mode = FT_LOAD_TARGET_MODE( load_flags );
490     scaler.flags       = 0;  /* XXX: fix this */
491 
492     error = af_loader_reset( loader, face );
493     if ( !error )
494     {
495       AF_ScriptMetrics  metrics;
496       FT_UInt           options = 0;
497 
498 
499 #ifdef FT_OPTION_AUTOFIT2
500       /* XXX: undocumented hook to activate the latin2 hinter */
501       if ( load_flags & ( 1UL << 20 ) )
502         options = 2;
503 #endif
504 
505       error = af_face_globals_get_metrics( loader->globals, gindex,
506                                            options, &metrics );
507       if ( !error )
508       {
509         loader->metrics = metrics;
510 
511         if ( metrics->clazz->script_metrics_scale )
512           metrics->clazz->script_metrics_scale( metrics, &scaler );
513         else
514           metrics->scaler = scaler;
515 
516         load_flags |=  FT_LOAD_NO_SCALE | FT_LOAD_IGNORE_TRANSFORM;
517         load_flags &= ~FT_LOAD_RENDER;
518 
519         if ( metrics->clazz->script_hints_init )
520         {
521           error = metrics->clazz->script_hints_init( &loader->hints,
522                                                      metrics );
523           if ( error )
524             goto Exit;
525         }
526 
527         error = af_loader_load_g( loader, &scaler, gindex, load_flags, 0 );
528       }
529     }
530   Exit:
531     return error;
532   }
533 
534 
535 /* END */
536