1 /***************************************************************************/
5 /* Auto-fitter glyph loading routines (body). */
7 /* Copyright 2003, 2004, 2005, 2006, 2007, 2008 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
26 FT_LOCAL_DEF( FT_Error
)
27 af_loader_init( AF_Loader loader
,
32 af_glyph_hints_init( &loader
->hints
, memory
);
34 _af_debug_hints
= &loader
->hints
;
36 return FT_GlyphLoader_New( memory
, &loader
->gloader
);
40 FT_LOCAL_DEF( FT_Error
)
41 af_loader_reset( AF_Loader loader
,
44 FT_Error error
= AF_Err_Ok
;
48 loader
->globals
= (AF_FaceGlobals
)face
->autohint
.data
;
50 FT_GlyphLoader_Rewind( loader
->gloader
);
52 if ( loader
->globals
== NULL
)
54 error
= af_face_globals_new( face
, &loader
->globals
);
58 (FT_Pointer
)loader
->globals
;
59 face
->autohint
.finalizer
=
60 (FT_Generic_Finalizer
)af_face_globals_free
;
69 af_loader_done( AF_Loader loader
)
71 af_glyph_hints_done( &loader
->hints
);
74 loader
->globals
= NULL
;
77 _af_debug_hints
= NULL
;
79 FT_GlyphLoader_Done( loader
->gloader
);
80 loader
->gloader
= NULL
;
85 af_loader_load_g( AF_Loader loader
,
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
;
100 error
= FT_Load_Glyph( face
, glyph_index
, load_flags
);
104 loader
->transformed
= internal
->glyph_transformed
;
105 if ( loader
->transformed
)
110 loader
->trans_matrix
= internal
->glyph_matrix
;
111 loader
->trans_delta
= internal
->glyph_delta
;
113 inverse
= loader
->trans_matrix
;
114 FT_Matrix_Invert( &inverse
);
115 FT_Vector_Transform( &loader
->trans_delta
, &inverse
);
118 /* set linear metrics */
119 slot
->linearHoriAdvance
= slot
->metrics
.horiAdvance
;
120 slot
->linearVertAdvance
= slot
->metrics
.vertAdvance
;
122 switch ( slot
->format
)
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
);
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
);
139 FT_ARRAY_COPY( gloader
->current
.outline
.points
,
140 slot
->outline
.points
,
141 slot
->outline
.n_points
);
143 FT_ARRAY_COPY( gloader
->current
.outline
.contours
,
144 slot
->outline
.contours
,
145 slot
->outline
.n_contours
);
147 FT_ARRAY_COPY( gloader
->current
.outline
.tags
,
149 slot
->outline
.n_points
);
151 gloader
->current
.outline
.n_points
= slot
->outline
.n_points
;
152 gloader
->current
.outline
.n_contours
= slot
->outline
.n_contours
;
154 /* compute original horizontal phantom points (and ignore */
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
;
162 /* be sure to check for spacing glyphs */
163 if ( slot
->outline
.n_points
== 0 )
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
,
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
)
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 */
185 if ( axis
->num_edges
> 1 && AF_HINTS_DO_ADVANCE( hints
) )
187 old_rsb
= loader
->pp2
.x
- edge2
->opos
;
188 old_lsb
= edge1
->opos
;
189 new_lsb
= edge1
->pos
;
191 /* remember unhinted values to later account */
192 /* for rounding errors */
194 pp1x_uh
= new_lsb
- old_lsb
;
195 pp2x_uh
= edge2
->pos
+ old_rsb
;
197 /* prefer too much space over too little space */
198 /* for very small sizes */
206 loader
->pp1
.x
= FT_PIX_ROUND( pp1x_uh
);
207 loader
->pp2
.x
= FT_PIX_ROUND( pp2x_uh
);
209 if ( loader
->pp1
.x
>= new_lsb
&& old_lsb
> 0 )
212 if ( loader
->pp2
.x
<= edge2
->pos
&& old_rsb
> 0 )
215 slot
->lsb_delta
= loader
->pp1
.x
- pp1x_uh
;
216 slot
->rsb_delta
= loader
->pp2
.x
- pp2x_uh
;
220 FT_Pos pp1x
= loader
->pp1
.x
;
221 FT_Pos pp2x
= loader
->pp2
.x
;
223 loader
->pp1
.x
= FT_PIX_ROUND( pp1x
);
224 loader
->pp2
.x
= FT_PIX_ROUND( pp2x
);
226 slot
->lsb_delta
= loader
->pp1
.x
- pp1x
;
227 slot
->rsb_delta
= loader
->pp2
.x
- pp2x
;
232 FT_Pos pp1x
= loader
->pp1
.x
;
233 FT_Pos pp2x
= loader
->pp2
.x
;
235 loader
->pp1
.x
= FT_PIX_ROUND( pp1x
+ hints
->xmin_delta
);
236 loader
->pp2
.x
= FT_PIX_ROUND( pp2x
+ hints
->xmax_delta
);
238 slot
->lsb_delta
= loader
->pp1
.x
- pp1x
;
239 slot
->rsb_delta
= loader
->pp2
.x
- pp2x
;
242 /* good, we simply add the glyph to our loader's base */
243 FT_GlyphLoader_Add( gloader
);
246 case FT_GLYPH_FORMAT_COMPOSITE
:
248 FT_UInt nn
, num_subglyphs
= slot
->num_subglyphs
;
249 FT_UInt num_base_subgs
, start_point
;
250 FT_SubGlyph subglyph
;
253 start_point
= gloader
->base
.outline
.n_points
;
255 /* first of all, copy the subglyph descriptors in the glyph loader */
256 error
= FT_GlyphLoader_CheckSubGlyphs( gloader
, num_subglyphs
);
260 FT_ARRAY_COPY( gloader
->current
.subglyphs
,
264 gloader
->current
.num_subglyphs
= num_subglyphs
;
265 num_base_subgs
= gloader
->base
.num_subglyphs
;
267 /* now, read each subglyph independently */
268 for ( nn
= 0; nn
< num_subglyphs
; nn
++ )
272 FT_UInt num_points
, num_new_points
, num_base_points
;
275 /* gloader.current.subglyphs can change during glyph loading due */
276 /* to re-allocation -- we must recompute the current subglyph on */
278 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ nn
;
283 num_base_points
= gloader
->base
.outline
.n_points
;
285 error
= af_loader_load_g( loader
, scaler
, subglyph
->index
,
286 load_flags
, depth
+ 1 );
290 /* recompute subglyph pointer */
291 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ nn
;
293 if ( subglyph
->flags
& FT_SUBGLYPH_FLAG_USE_MY_METRICS
)
304 num_points
= gloader
->base
.outline
.n_points
;
305 num_new_points
= num_points
- num_base_points
;
307 /* now perform the transform required for this subglyph */
309 if ( subglyph
->flags
& ( FT_SUBGLYPH_FLAG_SCALE
|
310 FT_SUBGLYPH_FLAG_XY_SCALE
|
311 FT_SUBGLYPH_FLAG_2X2
) )
313 FT_Vector
* cur
= gloader
->base
.outline
.points
+
315 FT_Vector
* limit
= cur
+ num_new_points
;
318 for ( ; cur
< limit
; cur
++ )
319 FT_Vector_Transform( cur
, &subglyph
->transform
);
324 if ( !( subglyph
->flags
& FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES
) )
326 FT_Int k
= subglyph
->arg1
;
327 FT_UInt l
= subglyph
->arg2
;
332 if ( start_point
+ k
>= num_base_points
||
333 l
>= (FT_UInt
)num_new_points
)
335 error
= AF_Err_Invalid_Composite
;
339 l
+= num_base_points
;
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
;
351 x
= FT_MulFix( subglyph
->arg1
, hints
->x_scale
) + hints
->x_delta
;
352 y
= FT_MulFix( subglyph
->arg2
, hints
->y_scale
) + hints
->y_delta
;
354 x
= FT_PIX_ROUND( x
);
355 y
= FT_PIX_ROUND( y
);
359 FT_Outline dummy
= gloader
->base
.outline
;
362 dummy
.points
+= num_base_points
;
363 dummy
.n_points
= (short)num_new_points
;
365 FT_Outline_Translate( &dummy
, x
, y
);
372 /* we don't support other formats (yet?) */
373 error
= AF_Err_Unimplemented_Feature
;
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
);
388 /* transform the hinted outline if needed */
389 if ( loader
->transformed
)
391 FT_Outline_Transform( &gloader
->base
.outline
, &loader
->trans_matrix
);
392 FT_Vector_Transform( &vvector
, &loader
->trans_matrix
);
395 /* we must translate our final outline by -pp1.x and compute */
396 /* the new metrics */
398 FT_Outline_Translate( &gloader
->base
.outline
, -loader
->pp1
.x
, 0 );
400 FT_Outline_Get_CBox( &gloader
->base
.outline
, &bbox
);
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
);
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
;
412 slot
->metrics
.vertBearingX
= FT_PIX_FLOOR( bbox
.xMin
+ vvector
.x
);
413 slot
->metrics
.vertBearingY
= FT_PIX_FLOOR( bbox
.yMax
+ vvector
.y
);
415 /* for mono-width fonts (like Andale, Courier, etc.) we need */
416 /* to keep the original rounded advance width */
418 if ( !FT_IS_FIXED_WIDTH( slot
->face
) )
419 slot
->metrics
.horiAdvance
= loader
->pp2
.x
- loader
->pp1
.x
;
421 slot
->metrics
.horiAdvance
= FT_MulFix( slot
->metrics
.horiAdvance
,
424 if ( !FT_IS_FIXED_WIDTH( slot
->face
) )
426 /* non-spacing glyphs must stay as-is */
427 if ( slot
->metrics
.horiAdvance
)
428 slot
->metrics
.horiAdvance
= loader
->pp2
.x
- loader
->pp1
.x
;
432 slot
->metrics
.horiAdvance
= FT_MulFix( slot
->metrics
.horiAdvance
,
433 metrics
->scaler
.x_scale
);
435 /* Set delta values to 0. Otherwise code that uses them is */
436 /* going to ruin the fixed advance width. */
442 slot
->metrics
.vertAdvance
= FT_MulFix( slot
->metrics
.vertAdvance
,
443 metrics
->scaler
.y_scale
);
445 slot
->metrics
.horiAdvance
= FT_PIX_ROUND( slot
->metrics
.horiAdvance
);
446 slot
->metrics
.vertAdvance
= FT_PIX_ROUND( slot
->metrics
.vertAdvance
);
448 /* now copy outline into glyph slot */
449 FT_GlyphLoader_Rewind( internal
->loader
);
450 error
= FT_GlyphLoader_CopyPoints( internal
->loader
, gloader
);
454 slot
->outline
= internal
->loader
->base
.outline
;
455 slot
->format
= FT_GLYPH_FORMAT_OUTLINE
;
459 af_debug_hinter
= hinter
;
467 FT_LOCAL_DEF( FT_Error
)
468 af_loader_load_glyph( AF_Loader loader
,
471 FT_UInt32 load_flags
)
474 FT_Size size
= face
->size
;
479 return AF_Err_Invalid_Argument
;
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 */
489 scaler
.render_mode
= FT_LOAD_TARGET_MODE( load_flags
);
490 scaler
.flags
= 0; /* XXX: fix this */
492 error
= af_loader_reset( loader
, face
);
495 AF_ScriptMetrics metrics
;
499 #ifdef FT_OPTION_AUTOFIT2
500 /* XXX: undocumented hook to activate the latin2 hinter */
501 if ( load_flags
& ( 1UL << 20 ) )
505 error
= af_face_globals_get_metrics( loader
->globals
, gindex
,
509 loader
->metrics
= metrics
;
511 if ( metrics
->clazz
->script_metrics_scale
)
512 metrics
->clazz
->script_metrics_scale( metrics
, &scaler
);
514 metrics
->scaler
= scaler
;
516 load_flags
|= FT_LOAD_NO_SCALE
| FT_LOAD_IGNORE_TRANSFORM
;
517 load_flags
&= ~FT_LOAD_RENDER
;
519 if ( metrics
->clazz
->script_hints_init
)
521 error
= metrics
->clazz
->script_hints_init( &loader
->hints
,
527 error
= af_loader_load_g( loader
, &scaler
, gindex
, load_flags
, 0 );