1 /***************************************************************************/
5 /* Auto-fitter glyph loading routines (body). */
7 /* Copyright 2003-2009, 2011-2012 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 /***************************************************************************/
25 /* Initialize glyph loader. */
27 FT_LOCAL_DEF( FT_Error )
28 af_loader_init( AF_Loader loader,
33 af_glyph_hints_init( &loader->hints, memory );
34 #ifdef FT_DEBUG_AUTOFIT
35 _af_debug_hints = &loader->hints;
37 return FT_GlyphLoader_New( memory, &loader->gloader );
41 /* Reset glyph loader and compute globals if necessary. */
43 FT_LOCAL_DEF( FT_Error )
44 af_loader_reset( AF_Loader loader,
47 FT_Error error = AF_Err_Ok;
51 loader->globals = (AF_FaceGlobals)face->autohint.data;
53 FT_GlyphLoader_Rewind( loader->gloader );
55 if ( loader->globals == NULL )
57 error = af_face_globals_new( face, &loader->globals );
61 (FT_Pointer)loader->globals;
62 face->autohint.finalizer =
63 (FT_Generic_Finalizer)af_face_globals_free;
71 /* Finalize glyph loader. */
74 af_loader_done( AF_Loader loader )
76 af_glyph_hints_done( &loader->hints );
79 loader->globals = NULL;
81 #ifdef FT_DEBUG_AUTOFIT
82 _af_debug_hints = NULL;
84 FT_GlyphLoader_Done( loader->gloader );
85 loader->gloader = NULL;
89 /* Load a single glyph component. This routine calls itself */
90 /* recursively, if necessary, and does the main work of */
91 /* `af_loader_load_glyph.' */
94 af_loader_load_g( AF_Loader loader,
101 FT_Face face = loader->face;
102 FT_GlyphLoader gloader = loader->gloader;
103 AF_ScriptMetrics metrics = loader->metrics;
104 AF_GlyphHints hints = &loader->hints;
105 FT_GlyphSlot slot = face->glyph;
106 FT_Slot_Internal internal = slot->internal;
109 error = FT_Load_Glyph( face, glyph_index, load_flags );
113 loader->transformed = internal->glyph_transformed;
114 if ( loader->transformed )
119 loader->trans_matrix = internal->glyph_matrix;
120 loader->trans_delta = internal->glyph_delta;
122 inverse = loader->trans_matrix;
123 FT_Matrix_Invert( &inverse );
124 FT_Vector_Transform( &loader->trans_delta, &inverse );
127 /* set linear metrics */
128 slot->linearHoriAdvance = slot->metrics.horiAdvance;
129 slot->linearVertAdvance = slot->metrics.vertAdvance;
131 switch ( slot->format )
133 case FT_GLYPH_FORMAT_OUTLINE:
134 /* translate the loaded glyph when an internal transform is needed */
135 if ( loader->transformed )
136 FT_Outline_Translate( &slot->outline,
137 loader->trans_delta.x,
138 loader->trans_delta.y );
140 /* copy the outline points in the loader's current */
141 /* extra points which is used to keep original glyph coordinates */
142 error = FT_GLYPHLOADER_CHECK_POINTS( gloader,
143 slot->outline.n_points + 4,
144 slot->outline.n_contours );
148 FT_ARRAY_COPY( gloader->current.outline.points,
149 slot->outline.points,
150 slot->outline.n_points );
152 FT_ARRAY_COPY( gloader->current.outline.contours,
153 slot->outline.contours,
154 slot->outline.n_contours );
156 FT_ARRAY_COPY( gloader->current.outline.tags,
158 slot->outline.n_points );
160 gloader->current.outline.n_points = slot->outline.n_points;
161 gloader->current.outline.n_contours = slot->outline.n_contours;
163 /* compute original horizontal phantom points (and ignore */
165 loader->pp1.x = hints->x_delta;
166 loader->pp1.y = hints->y_delta;
167 loader->pp2.x = FT_MulFix( slot->metrics.horiAdvance,
168 hints->x_scale ) + hints->x_delta;
169 loader->pp2.y = hints->y_delta;
171 /* be sure to check for spacing glyphs */
172 if ( slot->outline.n_points == 0 )
175 /* now load the slot image into the auto-outline and run the */
176 /* automatic hinting process */
177 if ( metrics->clazz->script_hints_apply )
178 metrics->clazz->script_hints_apply( hints,
179 &gloader->current.outline,
182 /* we now need to adjust the metrics according to the change in */
183 /* width/positioning that occurred during the hinting process */
184 if ( scaler->render_mode != FT_RENDER_MODE_LIGHT )
186 FT_Pos old_rsb, old_lsb, new_lsb;
187 FT_Pos pp1x_uh, pp2x_uh;
188 AF_AxisHints axis = &hints->axis[AF_DIMENSION_HORZ];
189 AF_Edge edge1 = axis->edges; /* leftmost edge */
190 AF_Edge edge2 = edge1 +
191 axis->num_edges - 1; /* rightmost edge */
194 if ( axis->num_edges > 1 && AF_HINTS_DO_ADVANCE( hints ) )
196 old_rsb = loader->pp2.x - edge2->opos;
197 old_lsb = edge1->opos;
198 new_lsb = edge1->pos;
200 /* remember unhinted values to later account */
201 /* for rounding errors */
203 pp1x_uh = new_lsb - old_lsb;
204 pp2x_uh = edge2->pos + old_rsb;
206 /* prefer too much space over too little space */
207 /* for very small sizes */
215 loader->pp1.x = FT_PIX_ROUND( pp1x_uh );
216 loader->pp2.x = FT_PIX_ROUND( pp2x_uh );
218 if ( loader->pp1.x >= new_lsb && old_lsb > 0 )
221 if ( loader->pp2.x <= edge2->pos && old_rsb > 0 )
224 slot->lsb_delta = loader->pp1.x - pp1x_uh;
225 slot->rsb_delta = loader->pp2.x - pp2x_uh;
229 FT_Pos pp1x = loader->pp1.x;
230 FT_Pos pp2x = loader->pp2.x;
233 loader->pp1.x = FT_PIX_ROUND( pp1x );
234 loader->pp2.x = FT_PIX_ROUND( pp2x );
236 slot->lsb_delta = loader->pp1.x - pp1x;
237 slot->rsb_delta = loader->pp2.x - pp2x;
242 FT_Pos pp1x = loader->pp1.x;
243 FT_Pos pp2x = loader->pp2.x;
246 loader->pp1.x = FT_PIX_ROUND( pp1x + hints->xmin_delta );
247 loader->pp2.x = FT_PIX_ROUND( pp2x + hints->xmax_delta );
249 slot->lsb_delta = loader->pp1.x - pp1x;
250 slot->rsb_delta = loader->pp2.x - pp2x;
253 /* good, we simply add the glyph to our loader's base */
254 FT_GlyphLoader_Add( gloader );
257 case FT_GLYPH_FORMAT_COMPOSITE:
259 FT_UInt nn, num_subglyphs = slot->num_subglyphs;
260 FT_UInt num_base_subgs, start_point;
261 FT_SubGlyph subglyph;
264 start_point = gloader->base.outline.n_points;
266 /* first of all, copy the subglyph descriptors in the glyph loader */
267 error = FT_GlyphLoader_CheckSubGlyphs( gloader, num_subglyphs );
271 FT_ARRAY_COPY( gloader->current.subglyphs,
275 gloader->current.num_subglyphs = num_subglyphs;
276 num_base_subgs = gloader->base.num_subglyphs;
278 /* now read each subglyph independently */
279 for ( nn = 0; nn < num_subglyphs; nn++ )
283 FT_UInt num_points, num_new_points, num_base_points;
286 /* gloader.current.subglyphs can change during glyph loading due */
287 /* to re-allocation -- we must recompute the current subglyph on */
289 subglyph = gloader->base.subglyphs + num_base_subgs + nn;
294 num_base_points = gloader->base.outline.n_points;
296 error = af_loader_load_g( loader, scaler, subglyph->index,
297 load_flags, depth + 1 );
301 /* recompute subglyph pointer */
302 subglyph = gloader->base.subglyphs + num_base_subgs + nn;
304 if ( subglyph->flags & FT_SUBGLYPH_FLAG_USE_MY_METRICS )
315 num_points = gloader->base.outline.n_points;
316 num_new_points = num_points - num_base_points;
318 /* now perform the transformation required for this subglyph */
320 if ( subglyph->flags & ( FT_SUBGLYPH_FLAG_SCALE |
321 FT_SUBGLYPH_FLAG_XY_SCALE |
322 FT_SUBGLYPH_FLAG_2X2 ) )
324 FT_Vector* cur = gloader->base.outline.points +
326 FT_Vector* limit = cur + num_new_points;
329 for ( ; cur < limit; cur++ )
330 FT_Vector_Transform( cur, &subglyph->transform );
335 if ( !( subglyph->flags & FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES ) )
337 FT_Int k = subglyph->arg1;
338 FT_UInt l = subglyph->arg2;
343 if ( start_point + k >= num_base_points ||
344 l >= (FT_UInt)num_new_points )
346 error = AF_Err_Invalid_Composite;
350 l += num_base_points;
352 /* for now, only use the current point coordinates; */
353 /* we may consider another approach in the near future */
354 p1 = gloader->base.outline.points + start_point + k;
355 p2 = gloader->base.outline.points + start_point + l;
362 x = FT_MulFix( subglyph->arg1, hints->x_scale ) + hints->x_delta;
363 y = FT_MulFix( subglyph->arg2, hints->y_scale ) + hints->y_delta;
365 x = FT_PIX_ROUND( x );
366 y = FT_PIX_ROUND( y );
370 FT_Outline dummy = gloader->base.outline;
373 dummy.points += num_base_points;
374 dummy.n_points = (short)num_new_points;
376 FT_Outline_Translate( &dummy, x, y );
383 /* we don't support other formats (yet?) */
384 error = AF_Err_Unimplemented_Feature;
394 vvector.x = slot->metrics.vertBearingX - slot->metrics.horiBearingX;
395 vvector.y = slot->metrics.vertBearingY - slot->metrics.horiBearingY;
396 vvector.x = FT_MulFix( vvector.x, metrics->scaler.x_scale );
397 vvector.y = FT_MulFix( vvector.y, metrics->scaler.y_scale );
399 /* transform the hinted outline if needed */
400 if ( loader->transformed )
402 FT_Outline_Transform( &gloader->base.outline, &loader->trans_matrix );
403 FT_Vector_Transform( &vvector, &loader->trans_matrix );
406 /* we must translate our final outline by -pp1.x and compute */
407 /* the new metrics */
409 FT_Outline_Translate( &gloader->base.outline, -loader->pp1.x, 0 );
411 FT_Outline_Get_CBox( &gloader->base.outline, &bbox );
413 bbox.xMin = FT_PIX_FLOOR( bbox.xMin );
414 bbox.yMin = FT_PIX_FLOOR( bbox.yMin );
415 bbox.xMax = FT_PIX_CEIL( bbox.xMax );
416 bbox.yMax = FT_PIX_CEIL( bbox.yMax );
418 slot->metrics.width = bbox.xMax - bbox.xMin;
419 slot->metrics.height = bbox.yMax - bbox.yMin;
420 slot->metrics.horiBearingX = bbox.xMin;
421 slot->metrics.horiBearingY = bbox.yMax;
423 slot->metrics.vertBearingX = FT_PIX_FLOOR( bbox.xMin + vvector.x );
424 slot->metrics.vertBearingY = FT_PIX_FLOOR( bbox.yMax + vvector.y );
426 /* for mono-width fonts (like Andale, Courier, etc.) we need */
427 /* to keep the original rounded advance width; ditto for */
428 /* digits if all have the same advance width */
430 if ( !FT_IS_FIXED_WIDTH( slot->face ) )
431 slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
433 slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance,
436 if ( scaler->render_mode != FT_RENDER_MODE_LIGHT &&
437 ( FT_IS_FIXED_WIDTH( slot->face ) ||
438 ( af_face_globals_is_digit( loader->globals, glyph_index ) &&
439 metrics->digits_have_same_width ) ) )
441 slot->metrics.horiAdvance = FT_MulFix( slot->metrics.horiAdvance,
442 metrics->scaler.x_scale );
444 /* Set delta values to 0. Otherwise code that uses them is */
445 /* going to ruin the fixed advance width. */
451 /* non-spacing glyphs must stay as-is */
452 if ( slot->metrics.horiAdvance )
453 slot->metrics.horiAdvance = loader->pp2.x - loader->pp1.x;
457 slot->metrics.vertAdvance = FT_MulFix( slot->metrics.vertAdvance,
458 metrics->scaler.y_scale );
460 slot->metrics.horiAdvance = FT_PIX_ROUND( slot->metrics.horiAdvance );
461 slot->metrics.vertAdvance = FT_PIX_ROUND( slot->metrics.vertAdvance );
463 /* now copy outline into glyph slot */
464 FT_GlyphLoader_Rewind( internal->loader );
465 error = FT_GlyphLoader_CopyPoints( internal->loader, gloader );
469 /* reassign all outline fields except flags to protect them */
470 slot->outline.n_contours = internal->loader->base.outline.n_contours;
471 slot->outline.n_points = internal->loader->base.outline.n_points;
472 slot->outline.points = internal->loader->base.outline.points;
473 slot->outline.tags = internal->loader->base.outline.tags;
474 slot->outline.contours = internal->loader->base.outline.contours;
476 slot->format = FT_GLYPH_FORMAT_OUTLINE;
486 FT_LOCAL_DEF( FT_Error )
487 af_loader_load_glyph( AF_Loader loader,
490 FT_Int32 load_flags )
493 FT_Size size = face->size;
498 return AF_Err_Invalid_Argument;
503 scaler.x_scale = size->metrics.x_scale;
504 scaler.x_delta = 0; /* XXX: TODO: add support for sub-pixel hinting */
505 scaler.y_scale = size->metrics.y_scale;
506 scaler.y_delta = 0; /* XXX: TODO: add support for sub-pixel hinting */
508 scaler.render_mode = FT_LOAD_TARGET_MODE( load_flags );
509 scaler.flags = 0; /* XXX: fix this */
511 error = af_loader_reset( loader, face );
514 AF_ScriptMetrics metrics;
518 #ifdef FT_OPTION_AUTOFIT2
519 /* XXX: undocumented hook to activate the latin2 hinter */
520 if ( load_flags & ( 1UL << 20 ) )
524 error = af_face_globals_get_metrics( loader->globals, gindex,
528 loader->metrics = metrics;
530 if ( metrics->clazz->script_metrics_scale )
531 metrics->clazz->script_metrics_scale( metrics, &scaler );
533 metrics->scaler = scaler;
535 load_flags |= FT_LOAD_NO_SCALE | FT_LOAD_IGNORE_TRANSFORM;
536 load_flags &= ~FT_LOAD_RENDER;
538 if ( metrics->clazz->script_hints_init )
540 error = metrics->clazz->script_hints_init( &loader->hints,
546 error = af_loader_load_g( loader, &scaler, gindex, load_flags, 0 );