1 /***************************************************************************/
5 /* Auto-fitter glyph loading routines (body). */
7 /* Copyright 2003, 2004, 2005, 2006, 2007 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 metrics
->clazz
->script_hints_apply( hints
,
169 &gloader
->current
.outline
,
172 /* we now need to hint the metrics according to the change in */
173 /* width/positioning that occurred during the hinting process */
174 if ( scaler
->render_mode
!= FT_RENDER_MODE_LIGHT
)
176 FT_Pos old_rsb
, old_lsb
, new_lsb
;
177 FT_Pos pp1x_uh
, pp2x_uh
;
178 AF_AxisHints axis
= &hints
->axis
[AF_DIMENSION_HORZ
];
179 AF_Edge edge1
= axis
->edges
; /* leftmost edge */
180 AF_Edge edge2
= edge1
+
181 axis
->num_edges
- 1; /* rightmost edge */
184 if ( axis
->num_edges
> 1 && AF_HINTS_DO_ADVANCE( hints
) )
186 old_rsb
= loader
->pp2
.x
- edge2
->opos
;
187 old_lsb
= edge1
->opos
;
188 new_lsb
= edge1
->pos
;
190 /* remember unhinted values to later account */
191 /* for rounding errors */
193 pp1x_uh
= new_lsb
- old_lsb
;
194 pp2x_uh
= edge2
->pos
+ old_rsb
;
196 /* prefer too much space over too little space */
197 /* for very small sizes */
205 loader
->pp1
.x
= FT_PIX_ROUND( pp1x_uh
);
206 loader
->pp2
.x
= FT_PIX_ROUND( pp2x_uh
);
208 if ( loader
->pp1
.x
>= new_lsb
&& old_lsb
> 0 )
211 if ( loader
->pp2
.x
<= edge2
->pos
&& old_rsb
> 0 )
214 slot
->lsb_delta
= loader
->pp1
.x
- pp1x_uh
;
215 slot
->rsb_delta
= loader
->pp2
.x
- pp2x_uh
;
219 FT_Pos pp1x
= loader
->pp1
.x
;
220 FT_Pos pp2x
= loader
->pp2
.x
;
222 loader
->pp1
.x
= FT_PIX_ROUND( pp1x
);
223 loader
->pp2
.x
= FT_PIX_ROUND( pp2x
);
225 slot
->lsb_delta
= loader
->pp1
.x
- pp1x
;
226 slot
->rsb_delta
= loader
->pp2
.x
- pp2x
;
231 FT_Pos pp1x
= loader
->pp1
.x
;
232 FT_Pos pp2x
= loader
->pp2
.x
;
234 loader
->pp1
.x
= FT_PIX_ROUND( pp1x
+ hints
->xmin_delta
);
235 loader
->pp2
.x
= FT_PIX_ROUND( pp2x
+ hints
->xmax_delta
);
237 slot
->lsb_delta
= loader
->pp1
.x
- pp1x
;
238 slot
->rsb_delta
= loader
->pp2
.x
- pp2x
;
241 /* good, we simply add the glyph to our loader's base */
242 FT_GlyphLoader_Add( gloader
);
245 case FT_GLYPH_FORMAT_COMPOSITE
:
247 FT_UInt nn
, num_subglyphs
= slot
->num_subglyphs
;
248 FT_UInt num_base_subgs
, start_point
;
249 FT_SubGlyph subglyph
;
252 start_point
= gloader
->base
.outline
.n_points
;
254 /* first of all, copy the subglyph descriptors in the glyph loader */
255 error
= FT_GlyphLoader_CheckSubGlyphs( gloader
, num_subglyphs
);
259 FT_ARRAY_COPY( gloader
->current
.subglyphs
,
263 gloader
->current
.num_subglyphs
= num_subglyphs
;
264 num_base_subgs
= gloader
->base
.num_subglyphs
;
266 /* now, read each subglyph independently */
267 for ( nn
= 0; nn
< num_subglyphs
; nn
++ )
271 FT_UInt num_points
, num_new_points
, num_base_points
;
274 /* gloader.current.subglyphs can change during glyph loading due */
275 /* to re-allocation -- we must recompute the current subglyph on */
277 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ nn
;
282 num_base_points
= gloader
->base
.outline
.n_points
;
284 error
= af_loader_load_g( loader
, scaler
, subglyph
->index
,
285 load_flags
, depth
+ 1 );
289 /* recompute subglyph pointer */
290 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ nn
;
292 if ( subglyph
->flags
& FT_SUBGLYPH_FLAG_USE_MY_METRICS
)
303 num_points
= gloader
->base
.outline
.n_points
;
304 num_new_points
= num_points
- num_base_points
;
306 /* now perform the transform required for this subglyph */
308 if ( subglyph
->flags
& ( FT_SUBGLYPH_FLAG_SCALE
|
309 FT_SUBGLYPH_FLAG_XY_SCALE
|
310 FT_SUBGLYPH_FLAG_2X2
) )
312 FT_Vector
* cur
= gloader
->base
.outline
.points
+
314 FT_Vector
* limit
= cur
+ num_new_points
;
317 for ( ; cur
< limit
; cur
++ )
318 FT_Vector_Transform( cur
, &subglyph
->transform
);
323 if ( !( subglyph
->flags
& FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES
) )
325 FT_Int k
= subglyph
->arg1
;
326 FT_UInt l
= subglyph
->arg2
;
331 if ( start_point
+ k
>= num_base_points
||
332 l
>= (FT_UInt
)num_new_points
)
334 error
= AF_Err_Invalid_Composite
;
338 l
+= num_base_points
;
340 /* for now, only use the current point coordinates; */
341 /* we may consider another approach in the near future */
342 p1
= gloader
->base
.outline
.points
+ start_point
+ k
;
343 p2
= gloader
->base
.outline
.points
+ start_point
+ l
;
350 x
= FT_MulFix( subglyph
->arg1
, hints
->x_scale
) + hints
->x_delta
;
351 y
= FT_MulFix( subglyph
->arg2
, hints
->y_scale
) + hints
->y_delta
;
353 x
= FT_PIX_ROUND( x
);
354 y
= FT_PIX_ROUND( y
);
358 FT_Outline dummy
= gloader
->base
.outline
;
361 dummy
.points
+= num_base_points
;
362 dummy
.n_points
= (short)num_new_points
;
364 FT_Outline_Translate( &dummy
, x
, y
);
371 /* we don't support other formats (yet?) */
372 error
= AF_Err_Unimplemented_Feature
;
382 vvector
.x
= slot
->metrics
.vertBearingX
- slot
->metrics
.horiBearingX
;
383 vvector
.y
= slot
->metrics
.vertBearingY
- slot
->metrics
.horiBearingY
;
384 vvector
.x
= FT_MulFix( vvector
.x
, metrics
->scaler
.x_scale
);
385 vvector
.y
= FT_MulFix( vvector
.y
, metrics
->scaler
.y_scale
);
387 /* transform the hinted outline if needed */
388 if ( loader
->transformed
)
390 FT_Outline_Transform( &gloader
->base
.outline
, &loader
->trans_matrix
);
391 FT_Vector_Transform( &vvector
, &loader
->trans_matrix
);
394 /* we must translate our final outline by -pp1.x and compute */
395 /* the new metrics */
397 FT_Outline_Translate( &gloader
->base
.outline
, -loader
->pp1
.x
, 0 );
399 FT_Outline_Get_CBox( &gloader
->base
.outline
, &bbox
);
401 bbox
.xMin
= FT_PIX_FLOOR( bbox
.xMin
);
402 bbox
.yMin
= FT_PIX_FLOOR( bbox
.yMin
);
403 bbox
.xMax
= FT_PIX_CEIL( bbox
.xMax
);
404 bbox
.yMax
= FT_PIX_CEIL( bbox
.yMax
);
406 slot
->metrics
.width
= bbox
.xMax
- bbox
.xMin
;
407 slot
->metrics
.height
= bbox
.yMax
- bbox
.yMin
;
408 slot
->metrics
.horiBearingX
= bbox
.xMin
;
409 slot
->metrics
.horiBearingY
= bbox
.yMax
;
411 slot
->metrics
.vertBearingX
= FT_PIX_FLOOR( bbox
.xMin
+ vvector
.x
);
412 slot
->metrics
.vertBearingY
= FT_PIX_FLOOR( bbox
.yMax
+ vvector
.y
);
414 /* for mono-width fonts (like Andale, Courier, etc.) we need */
415 /* to keep the original rounded advance width */
417 if ( !FT_IS_FIXED_WIDTH( slot
->face
) )
418 slot
->metrics
.horiAdvance
= loader
->pp2
.x
- loader
->pp1
.x
;
420 slot
->metrics
.horiAdvance
= FT_MulFix( slot
->metrics
.horiAdvance
,
423 if ( !FT_IS_FIXED_WIDTH( slot
->face
) )
425 /* non-spacing glyphs must stay as-is */
426 if ( slot
->metrics
.horiAdvance
)
427 slot
->metrics
.horiAdvance
= loader
->pp2
.x
- loader
->pp1
.x
;
431 slot
->metrics
.horiAdvance
= FT_MulFix( slot
->metrics
.horiAdvance
,
432 metrics
->scaler
.x_scale
);
434 /* Set delta values to 0. Otherwise code that uses them is */
435 /* going to ruin the fixed advance width. */
441 slot
->metrics
.vertAdvance
= FT_MulFix( slot
->metrics
.vertAdvance
,
442 metrics
->scaler
.y_scale
);
444 slot
->metrics
.horiAdvance
= FT_PIX_ROUND( slot
->metrics
.horiAdvance
);
445 slot
->metrics
.vertAdvance
= FT_PIX_ROUND( slot
->metrics
.vertAdvance
);
447 /* now copy outline into glyph slot */
448 FT_GlyphLoader_Rewind( internal
->loader
);
449 error
= FT_GlyphLoader_CopyPoints( internal
->loader
, gloader
);
453 slot
->outline
= internal
->loader
->base
.outline
;
454 slot
->format
= FT_GLYPH_FORMAT_OUTLINE
;
458 af_debug_hinter
= hinter
;
466 FT_LOCAL_DEF( FT_Error
)
467 af_loader_load_glyph( AF_Loader loader
,
470 FT_UInt32 load_flags
)
473 FT_Size size
= face
->size
;
478 return AF_Err_Invalid_Argument
;
483 scaler
.x_scale
= size
->metrics
.x_scale
;
484 scaler
.x_delta
= 0; /* XXX: TODO: add support for sub-pixel hinting */
485 scaler
.y_scale
= size
->metrics
.y_scale
;
486 scaler
.y_delta
= 0; /* XXX: TODO: add support for sub-pixel hinting */
488 scaler
.render_mode
= FT_LOAD_TARGET_MODE( load_flags
);
489 scaler
.flags
= 0; /* XXX: fix this */
491 error
= af_loader_reset( loader
, face
);
494 AF_ScriptMetrics metrics
;
498 #ifdef FT_OPTION_AUTOFIT2
499 /* XXX: undocumented hook to activate the latin2 hinter */
500 if ( load_flags
& ( 1UL << 20 ) )
504 error
= af_face_globals_get_metrics( loader
->globals
, gindex
,
508 loader
->metrics
= metrics
;
510 if ( metrics
->clazz
->script_metrics_scale
)
511 metrics
->clazz
->script_metrics_scale( metrics
, &scaler
);
513 metrics
->scaler
= scaler
;
515 load_flags
|= FT_LOAD_NO_SCALE
| FT_LOAD_IGNORE_TRANSFORM
;
516 load_flags
&= ~FT_LOAD_RENDER
;
518 error
= metrics
->clazz
->script_hints_init( &loader
->hints
, metrics
);
522 error
= af_loader_load_g( loader
, &scaler
, gindex
, load_flags
, 0 );