1 /***************************************************************************/
5 /* Auto-fitter glyph loading routines (body). */
7 /* Copyright 2003-2009, 2011-2013 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 /* Initialize glyph loader. */
28 FT_LOCAL_DEF( FT_Error
)
29 af_loader_init( AF_Module module
)
31 AF_Loader loader
= module
->loader
;
32 FT_Memory memory
= module
->root
.library
->memory
;
37 af_glyph_hints_init( &loader
->hints
, memory
);
38 #ifdef FT_DEBUG_AUTOFIT
39 _af_debug_hints
= &loader
->hints
;
41 return FT_GlyphLoader_New( memory
, &loader
->gloader
);
45 /* Reset glyph loader and compute globals if necessary. */
47 FT_LOCAL_DEF( FT_Error
)
48 af_loader_reset( AF_Module module
,
51 FT_Error error
= FT_Err_Ok
;
52 AF_Loader loader
= module
->loader
;
56 loader
->globals
= (AF_FaceGlobals
)face
->autohint
.data
;
58 FT_GlyphLoader_Rewind( loader
->gloader
);
60 if ( loader
->globals
== NULL
)
62 error
= af_face_globals_new( face
, &loader
->globals
, module
);
66 (FT_Pointer
)loader
->globals
;
67 face
->autohint
.finalizer
=
68 (FT_Generic_Finalizer
)af_face_globals_free
;
76 /* Finalize glyph loader. */
79 af_loader_done( AF_Module module
)
81 AF_Loader loader
= module
->loader
;
84 af_glyph_hints_done( &loader
->hints
);
87 loader
->globals
= NULL
;
89 #ifdef FT_DEBUG_AUTOFIT
90 _af_debug_hints
= NULL
;
92 FT_GlyphLoader_Done( loader
->gloader
);
93 loader
->gloader
= NULL
;
97 /* Load a single glyph component. This routine calls itself */
98 /* recursively, if necessary, and does the main work of */
99 /* `af_loader_load_glyph.' */
102 af_loader_load_g( AF_Loader loader
,
109 FT_Face face
= loader
->face
;
110 FT_GlyphLoader gloader
= loader
->gloader
;
111 AF_ScriptMetrics metrics
= loader
->metrics
;
112 AF_GlyphHints hints
= &loader
->hints
;
113 FT_GlyphSlot slot
= face
->glyph
;
114 FT_Slot_Internal internal
= slot
->internal
;
118 flags
= load_flags
| FT_LOAD_LINEAR_DESIGN
;
119 error
= FT_Load_Glyph( face
, glyph_index
, flags
);
123 loader
->transformed
= internal
->glyph_transformed
;
124 if ( loader
->transformed
)
129 loader
->trans_matrix
= internal
->glyph_matrix
;
130 loader
->trans_delta
= internal
->glyph_delta
;
132 inverse
= loader
->trans_matrix
;
133 FT_Matrix_Invert( &inverse
);
134 FT_Vector_Transform( &loader
->trans_delta
, &inverse
);
137 switch ( slot
->format
)
139 case FT_GLYPH_FORMAT_OUTLINE
:
140 /* translate the loaded glyph when an internal transform is needed */
141 if ( loader
->transformed
)
142 FT_Outline_Translate( &slot
->outline
,
143 loader
->trans_delta
.x
,
144 loader
->trans_delta
.y
);
146 /* copy the outline points in the loader's current */
147 /* extra points which are used to keep original glyph coordinates */
148 error
= FT_GLYPHLOADER_CHECK_POINTS( gloader
,
149 slot
->outline
.n_points
+ 4,
150 slot
->outline
.n_contours
);
154 FT_ARRAY_COPY( gloader
->current
.outline
.points
,
155 slot
->outline
.points
,
156 slot
->outline
.n_points
);
158 FT_ARRAY_COPY( gloader
->current
.outline
.contours
,
159 slot
->outline
.contours
,
160 slot
->outline
.n_contours
);
162 FT_ARRAY_COPY( gloader
->current
.outline
.tags
,
164 slot
->outline
.n_points
);
166 gloader
->current
.outline
.n_points
= slot
->outline
.n_points
;
167 gloader
->current
.outline
.n_contours
= slot
->outline
.n_contours
;
169 /* compute original horizontal phantom points (and ignore */
171 loader
->pp1
.x
= hints
->x_delta
;
172 loader
->pp1
.y
= hints
->y_delta
;
173 loader
->pp2
.x
= FT_MulFix( slot
->metrics
.horiAdvance
,
174 hints
->x_scale
) + hints
->x_delta
;
175 loader
->pp2
.y
= hints
->y_delta
;
177 /* be sure to check for spacing glyphs */
178 if ( slot
->outline
.n_points
== 0 )
181 /* now load the slot image into the auto-outline and run the */
182 /* automatic hinting process */
183 if ( metrics
->clazz
->script_hints_apply
)
184 metrics
->clazz
->script_hints_apply( hints
,
185 &gloader
->current
.outline
,
188 /* we now need to adjust the metrics according to the change in */
189 /* width/positioning that occurred during the hinting process */
190 if ( scaler
->render_mode
!= FT_RENDER_MODE_LIGHT
)
192 FT_Pos old_rsb
, old_lsb
, new_lsb
;
193 FT_Pos pp1x_uh
, pp2x_uh
;
194 AF_AxisHints axis
= &hints
->axis
[AF_DIMENSION_HORZ
];
195 AF_Edge edge1
= axis
->edges
; /* leftmost edge */
196 AF_Edge edge2
= edge1
+
197 axis
->num_edges
- 1; /* rightmost edge */
200 if ( axis
->num_edges
> 1 && AF_HINTS_DO_ADVANCE( hints
) )
202 old_rsb
= loader
->pp2
.x
- edge2
->opos
;
203 old_lsb
= edge1
->opos
;
204 new_lsb
= edge1
->pos
;
206 /* remember unhinted values to later account */
207 /* for rounding errors */
209 pp1x_uh
= new_lsb
- old_lsb
;
210 pp2x_uh
= edge2
->pos
+ old_rsb
;
212 /* prefer too much space over too little space */
213 /* for very small sizes */
221 loader
->pp1
.x
= FT_PIX_ROUND( pp1x_uh
);
222 loader
->pp2
.x
= FT_PIX_ROUND( pp2x_uh
);
224 if ( loader
->pp1
.x
>= new_lsb
&& old_lsb
> 0 )
227 if ( loader
->pp2
.x
<= edge2
->pos
&& old_rsb
> 0 )
230 slot
->lsb_delta
= loader
->pp1
.x
- pp1x_uh
;
231 slot
->rsb_delta
= loader
->pp2
.x
- pp2x_uh
;
235 FT_Pos pp1x
= loader
->pp1
.x
;
236 FT_Pos pp2x
= loader
->pp2
.x
;
239 loader
->pp1
.x
= FT_PIX_ROUND( pp1x
);
240 loader
->pp2
.x
= FT_PIX_ROUND( pp2x
);
242 slot
->lsb_delta
= loader
->pp1
.x
- pp1x
;
243 slot
->rsb_delta
= loader
->pp2
.x
- pp2x
;
248 FT_Pos pp1x
= loader
->pp1
.x
;
249 FT_Pos pp2x
= loader
->pp2
.x
;
252 loader
->pp1
.x
= FT_PIX_ROUND( pp1x
+ hints
->xmin_delta
);
253 loader
->pp2
.x
= FT_PIX_ROUND( pp2x
+ hints
->xmax_delta
);
255 slot
->lsb_delta
= loader
->pp1
.x
- pp1x
;
256 slot
->rsb_delta
= loader
->pp2
.x
- pp2x
;
259 /* good, we simply add the glyph to our loader's base */
260 FT_GlyphLoader_Add( gloader
);
263 case FT_GLYPH_FORMAT_COMPOSITE
:
265 FT_UInt nn
, num_subglyphs
= slot
->num_subglyphs
;
266 FT_UInt num_base_subgs
, start_point
;
267 FT_SubGlyph subglyph
;
270 start_point
= gloader
->base
.outline
.n_points
;
272 /* first of all, copy the subglyph descriptors in the glyph loader */
273 error
= FT_GlyphLoader_CheckSubGlyphs( gloader
, num_subglyphs
);
277 FT_ARRAY_COPY( gloader
->current
.subglyphs
,
281 gloader
->current
.num_subglyphs
= num_subglyphs
;
282 num_base_subgs
= gloader
->base
.num_subglyphs
;
284 /* now read each subglyph independently */
285 for ( nn
= 0; nn
< num_subglyphs
; nn
++ )
289 FT_UInt num_points
, num_new_points
, num_base_points
;
292 /* gloader.current.subglyphs can change during glyph loading due */
293 /* to re-allocation -- we must recompute the current subglyph on */
295 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ nn
;
300 num_base_points
= gloader
->base
.outline
.n_points
;
302 error
= af_loader_load_g( loader
, scaler
, subglyph
->index
,
303 load_flags
, depth
+ 1 );
307 /* recompute subglyph pointer */
308 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ nn
;
310 if ( subglyph
->flags
& FT_SUBGLYPH_FLAG_USE_MY_METRICS
)
321 num_points
= gloader
->base
.outline
.n_points
;
322 num_new_points
= num_points
- num_base_points
;
324 /* now perform the transformation required for this subglyph */
326 if ( subglyph
->flags
& ( FT_SUBGLYPH_FLAG_SCALE
|
327 FT_SUBGLYPH_FLAG_XY_SCALE
|
328 FT_SUBGLYPH_FLAG_2X2
) )
330 FT_Vector
* cur
= gloader
->base
.outline
.points
+
332 FT_Vector
* limit
= cur
+ num_new_points
;
335 for ( ; cur
< limit
; cur
++ )
336 FT_Vector_Transform( cur
, &subglyph
->transform
);
341 if ( !( subglyph
->flags
& FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES
) )
343 FT_Int k
= subglyph
->arg1
;
344 FT_UInt l
= subglyph
->arg2
;
349 if ( start_point
+ k
>= num_base_points
||
350 l
>= (FT_UInt
)num_new_points
)
352 error
= FT_THROW( Invalid_Composite
);
356 l
+= num_base_points
;
358 /* for now, only use the current point coordinates; */
359 /* we eventually may consider another approach */
360 p1
= gloader
->base
.outline
.points
+ start_point
+ k
;
361 p2
= gloader
->base
.outline
.points
+ start_point
+ l
;
368 x
= FT_MulFix( subglyph
->arg1
, hints
->x_scale
) + hints
->x_delta
;
369 y
= FT_MulFix( subglyph
->arg2
, hints
->y_scale
) + hints
->y_delta
;
371 x
= FT_PIX_ROUND( x
);
372 y
= FT_PIX_ROUND( y
);
376 FT_Outline dummy
= gloader
->base
.outline
;
379 dummy
.points
+= num_base_points
;
380 dummy
.n_points
= (short)num_new_points
;
382 FT_Outline_Translate( &dummy
, x
, y
);
389 /* we don't support other formats (yet?) */
390 error
= FT_THROW( Unimplemented_Feature
);
400 vvector
.x
= slot
->metrics
.vertBearingX
- slot
->metrics
.horiBearingX
;
401 vvector
.y
= slot
->metrics
.vertBearingY
- slot
->metrics
.horiBearingY
;
402 vvector
.x
= FT_MulFix( vvector
.x
, metrics
->scaler
.x_scale
);
403 vvector
.y
= FT_MulFix( vvector
.y
, metrics
->scaler
.y_scale
);
405 /* transform the hinted outline if needed */
406 if ( loader
->transformed
)
408 FT_Outline_Transform( &gloader
->base
.outline
, &loader
->trans_matrix
);
409 FT_Vector_Transform( &vvector
, &loader
->trans_matrix
);
412 /* we must translate our final outline by -pp1.x and compute */
413 /* the new metrics */
415 FT_Outline_Translate( &gloader
->base
.outline
, -loader
->pp1
.x
, 0 );
417 FT_Outline_Get_CBox( &gloader
->base
.outline
, &bbox
);
419 bbox
.xMin
= FT_PIX_FLOOR( bbox
.xMin
);
420 bbox
.yMin
= FT_PIX_FLOOR( bbox
.yMin
);
421 bbox
.xMax
= FT_PIX_CEIL( bbox
.xMax
);
422 bbox
.yMax
= FT_PIX_CEIL( bbox
.yMax
);
424 slot
->metrics
.width
= bbox
.xMax
- bbox
.xMin
;
425 slot
->metrics
.height
= bbox
.yMax
- bbox
.yMin
;
426 slot
->metrics
.horiBearingX
= bbox
.xMin
;
427 slot
->metrics
.horiBearingY
= bbox
.yMax
;
429 slot
->metrics
.vertBearingX
= FT_PIX_FLOOR( bbox
.xMin
+ vvector
.x
);
430 slot
->metrics
.vertBearingY
= FT_PIX_FLOOR( bbox
.yMax
+ vvector
.y
);
432 /* for mono-width fonts (like Andale, Courier, etc.) we need */
433 /* to keep the original rounded advance width; ditto for */
434 /* digits if all have the same advance width */
436 if ( !FT_IS_FIXED_WIDTH( slot
->face
) )
437 slot
->metrics
.horiAdvance
= loader
->pp2
.x
- loader
->pp1
.x
;
439 slot
->metrics
.horiAdvance
= FT_MulFix( slot
->metrics
.horiAdvance
,
442 if ( scaler
->render_mode
!= FT_RENDER_MODE_LIGHT
&&
443 ( FT_IS_FIXED_WIDTH( slot
->face
) ||
444 ( af_face_globals_is_digit( loader
->globals
, glyph_index
) &&
445 metrics
->digits_have_same_width
) ) )
447 slot
->metrics
.horiAdvance
= FT_MulFix( slot
->metrics
.horiAdvance
,
448 metrics
->scaler
.x_scale
);
450 /* Set delta values to 0. Otherwise code that uses them is */
451 /* going to ruin the fixed advance width. */
457 /* non-spacing glyphs must stay as-is */
458 if ( slot
->metrics
.horiAdvance
)
459 slot
->metrics
.horiAdvance
= loader
->pp2
.x
- loader
->pp1
.x
;
463 slot
->metrics
.vertAdvance
= FT_MulFix( slot
->metrics
.vertAdvance
,
464 metrics
->scaler
.y_scale
);
466 slot
->metrics
.horiAdvance
= FT_PIX_ROUND( slot
->metrics
.horiAdvance
);
467 slot
->metrics
.vertAdvance
= FT_PIX_ROUND( slot
->metrics
.vertAdvance
);
469 /* now copy outline into glyph slot */
470 FT_GlyphLoader_Rewind( internal
->loader
);
471 error
= FT_GlyphLoader_CopyPoints( internal
->loader
, gloader
);
475 /* reassign all outline fields except flags to protect them */
476 slot
->outline
.n_contours
= internal
->loader
->base
.outline
.n_contours
;
477 slot
->outline
.n_points
= internal
->loader
->base
.outline
.n_points
;
478 slot
->outline
.points
= internal
->loader
->base
.outline
.points
;
479 slot
->outline
.tags
= internal
->loader
->base
.outline
.tags
;
480 slot
->outline
.contours
= internal
->loader
->base
.outline
.contours
;
482 slot
->format
= FT_GLYPH_FORMAT_OUTLINE
;
492 FT_LOCAL_DEF( FT_Error
)
493 af_loader_load_glyph( AF_Module module
,
496 FT_Int32 load_flags
)
499 FT_Size size
= face
->size
;
500 AF_Loader loader
= module
->loader
;
505 return FT_THROW( Invalid_Argument
);
510 scaler
.x_scale
= size
->metrics
.x_scale
;
511 scaler
.x_delta
= 0; /* XXX: TODO: add support for sub-pixel hinting */
512 scaler
.y_scale
= size
->metrics
.y_scale
;
513 scaler
.y_delta
= 0; /* XXX: TODO: add support for sub-pixel hinting */
515 scaler
.render_mode
= FT_LOAD_TARGET_MODE( load_flags
);
516 scaler
.flags
= 0; /* XXX: fix this */
518 error
= af_loader_reset( module
, face
);
521 AF_ScriptMetrics metrics
;
525 #ifdef FT_OPTION_AUTOFIT2
526 /* XXX: undocumented hook to activate the latin2 hinter */
527 if ( load_flags
& ( 1UL << 20 ) )
531 error
= af_face_globals_get_metrics( loader
->globals
, gindex
,
535 loader
->metrics
= metrics
;
537 if ( metrics
->clazz
->script_metrics_scale
)
538 metrics
->clazz
->script_metrics_scale( metrics
, &scaler
);
540 metrics
->scaler
= scaler
;
542 load_flags
|= FT_LOAD_NO_SCALE
| FT_LOAD_IGNORE_TRANSFORM
;
543 load_flags
&= ~FT_LOAD_RENDER
;
545 if ( metrics
->clazz
->script_hints_init
)
547 error
= metrics
->clazz
->script_hints_init( &loader
->hints
,
553 error
= af_loader_load_g( loader
, &scaler
, gindex
, load_flags
, 0 );