4 * Copyright (C) 2011-2012 by Werner Lemberg.
6 * This file is part of the ttfautohint library, and may only be used,
7 * modified, and distributed under the terms given in `COPYING'. By
8 * continuing to use, modify, or distribute this file you indicate that you
9 * have read `COPYING' and understand and accept it fully.
11 * The file `COPYING' mentioned in the previous paragraph is distributed
12 * with the ttfautohint library.
16 /* originally file `afloader.c' (2011-Mar-28) from FreeType */
18 /* heavily modified 2011 by Werner Lemberg <wl@gnu.org> */
30 /* from file `ftobjs.h' (2011-Mar-28) from FreeType */
31 typedef struct FT_Slot_InternalRec_
33 TA_GlyphLoader loader
;
35 FT_Bool glyph_transformed
;
36 FT_Matrix glyph_matrix
;
37 FT_Vector glyph_delta
;
39 } FT_GlyphSlot_InternalRec
;
42 /* initialize glyph loader */
45 ta_loader_init(FONT
* font
)
47 TA_Loader loader
= font
->loader
;
50 memset(loader
, 0, sizeof (TA_LoaderRec
));
52 ta_glyph_hints_init(&loader
->hints
);
54 _ta_debug_hints
= &loader
->hints
;
56 return TA_GlyphLoader_New(&loader
->gloader
);
60 /* reset glyph loader and compute globals if necessary */
63 ta_loader_reset(FONT
* font
,
66 FT_Error error
= FT_Err_Ok
;
67 TA_Loader loader
= font
->loader
;
71 loader
->globals
= (TA_FaceGlobals
)face
->autohint
.data
;
73 TA_GlyphLoader_Rewind(loader
->gloader
);
75 if (loader
->globals
== NULL
)
77 error
= ta_face_globals_new(face
, &loader
->globals
,
78 font
->fallback_script
);
81 face
->autohint
.data
= (FT_Pointer
)loader
->globals
;
82 face
->autohint
.finalizer
= (FT_Generic_Finalizer
)ta_face_globals_free
;
90 /* finalize glyph loader */
93 ta_loader_done(FONT
* font
)
95 TA_Loader loader
= font
->loader
;
98 ta_glyph_hints_done(&loader
->hints
);
101 loader
->globals
= NULL
;
104 _ta_debug_hints
= NULL
;
106 TA_GlyphLoader_Done(loader
->gloader
);
107 loader
->gloader
= NULL
;
111 /* load a single glyph component; this routine calls itself recursively, */
112 /* if necessary, and does the main work of `ta_loader_load_glyph' */
115 ta_loader_load_g(TA_Loader loader
,
122 FT_Face face
= loader
->face
;
123 TA_GlyphLoader gloader
= loader
->gloader
;
124 TA_ScriptMetrics metrics
= loader
->metrics
;
125 TA_GlyphHints hints
= &loader
->hints
;
126 FT_GlyphSlot slot
= face
->glyph
;
128 FT_Slot_Internal internal
= slot
->internal
;
133 flags
= load_flags
| FT_LOAD_LINEAR_DESIGN
;
134 error
= FT_Load_Glyph(face
, glyph_index
, flags
);
139 loader
->transformed
= internal
->glyph_transformed
;
140 if (loader
->transformed
)
145 loader
->trans_matrix
= internal
->glyph_matrix
;
146 loader
->trans_delta
= internal
->glyph_delta
;
148 inverse
= loader
->trans_matrix
;
149 FT_Matrix_Invert(&inverse
);
150 FT_Vector_Transform(&loader
->trans_delta
, &inverse
);
154 switch (slot
->format
)
156 case FT_GLYPH_FORMAT_OUTLINE
:
157 /* translate the loaded glyph when an internal transform is needed */
158 if (loader
->transformed
)
159 FT_Outline_Translate(&slot
->outline
,
160 loader
->trans_delta
.x
,
161 loader
->trans_delta
.y
);
163 /* copy the outline points in the loader's current extra points */
164 /* which are used to keep original glyph coordinates */
165 error
= TA_GLYPHLOADER_CHECK_POINTS(gloader
,
166 slot
->outline
.n_points
+ 4,
167 slot
->outline
.n_contours
);
171 memcpy(gloader
->current
.outline
.points
,
172 slot
->outline
.points
,
173 slot
->outline
.n_points
* sizeof (FT_Vector
));
174 memcpy(gloader
->current
.outline
.contours
,
175 slot
->outline
.contours
,
176 slot
->outline
.n_contours
* sizeof (short));
177 memcpy(gloader
->current
.outline
.tags
,
179 slot
->outline
.n_points
* sizeof (char));
181 gloader
->current
.outline
.n_points
= slot
->outline
.n_points
;
182 gloader
->current
.outline
.n_contours
= slot
->outline
.n_contours
;
184 /* compute original horizontal phantom points */
185 /* (and ignore vertical ones) */
186 loader
->pp1
.x
= hints
->x_delta
;
187 loader
->pp1
.y
= hints
->y_delta
;
188 loader
->pp2
.x
= FT_MulFix(slot
->metrics
.horiAdvance
,
189 hints
->x_scale
) + hints
->x_delta
;
190 loader
->pp2
.y
= hints
->y_delta
;
192 /* be sure to check for spacing glyphs */
193 if (slot
->outline
.n_points
== 0)
196 /* now load the slot image into the auto-outline */
197 /* and run the automatic hinting process */
198 if (metrics
->clazz
->script_hints_apply
)
199 metrics
->clazz
->script_hints_apply(hints
,
200 &gloader
->current
.outline
,
203 /* we now need to adjust the metrics according to the change in */
204 /* width/positioning that occurred during the hinting process */
205 if (scaler
->render_mode
!= FT_RENDER_MODE_LIGHT
)
207 FT_Pos old_rsb
, old_lsb
, new_lsb
;
208 FT_Pos pp1x_uh
, pp2x_uh
;
209 TA_AxisHints axis
= &hints
->axis
[TA_DIMENSION_HORZ
];
211 TA_Edge edge1
= axis
->edges
; /* leftmost edge */
212 TA_Edge edge2
= edge1
+ axis
->num_edges
- 1; /* rightmost edge */
215 if (axis
->num_edges
> 1 && TA_HINTS_DO_ADVANCE(hints
))
217 old_rsb
= loader
->pp2
.x
- edge2
->opos
;
218 old_lsb
= edge1
->opos
;
219 new_lsb
= edge1
->pos
;
221 /* remember unhinted values to later account */
222 /* for rounding errors */
223 pp1x_uh
= new_lsb
- old_lsb
;
224 pp2x_uh
= edge2
->pos
+ old_rsb
;
226 /* prefer too much space over too little space */
227 /* for very small sizes */
233 loader
->pp1
.x
= TA_PIX_ROUND(pp1x_uh
);
234 loader
->pp2
.x
= TA_PIX_ROUND(pp2x_uh
);
236 if (loader
->pp1
.x
>= new_lsb
239 if (loader
->pp2
.x
<= edge2
->pos
243 slot
->lsb_delta
= loader
->pp1
.x
- pp1x_uh
;
244 slot
->rsb_delta
= loader
->pp2
.x
- pp2x_uh
;
248 FT_Pos pp1x
= loader
->pp1
.x
;
249 FT_Pos pp2x
= loader
->pp2
.x
;
252 loader
->pp1
.x
= TA_PIX_ROUND(pp1x
);
253 loader
->pp2
.x
= TA_PIX_ROUND(pp2x
);
255 slot
->lsb_delta
= loader
->pp1
.x
- pp1x
;
256 slot
->rsb_delta
= loader
->pp2
.x
- pp2x
;
261 FT_Pos pp1x
= loader
->pp1
.x
;
262 FT_Pos pp2x
= loader
->pp2
.x
;
265 loader
->pp1
.x
= TA_PIX_ROUND(pp1x
+ hints
->xmin_delta
);
266 loader
->pp2
.x
= TA_PIX_ROUND(pp2x
+ hints
->xmax_delta
);
268 slot
->lsb_delta
= loader
->pp1
.x
- pp1x
;
269 slot
->rsb_delta
= loader
->pp2
.x
- pp2x
;
272 /* good, we simply add the glyph to our loader's base */
273 TA_GlyphLoader_Add(gloader
);
276 case FT_GLYPH_FORMAT_COMPOSITE
:
278 FT_UInt nn
, num_subglyphs
= slot
->num_subglyphs
;
279 FT_UInt num_base_subgs
, start_point
;
280 TA_SubGlyph subglyph
;
283 start_point
= gloader
->base
.outline
.n_points
;
285 /* first of all, copy the subglyph descriptors in the glyph loader */
286 error
= TA_GlyphLoader_CheckSubGlyphs(gloader
, num_subglyphs
);
290 memcpy(gloader
->current
.subglyphs
,
292 num_subglyphs
* sizeof (TA_SubGlyphRec
));
294 gloader
->current
.num_subglyphs
= num_subglyphs
;
295 num_base_subgs
= gloader
->base
.num_subglyphs
;
297 /* now read each subglyph independently */
298 for (nn
= 0; nn
< num_subglyphs
; nn
++)
302 FT_UInt num_points
, num_new_points
, num_base_points
;
305 /* gloader.current.subglyphs can change during glyph loading due */
306 /* to re-allocation -- we must recompute the current subglyph on */
308 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ nn
;
313 num_base_points
= gloader
->base
.outline
.n_points
;
315 error
= ta_loader_load_g(loader
, scaler
, subglyph
->index
,
316 load_flags
, depth
+ 1);
320 /* recompute subglyph pointer */
321 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ nn
;
323 if (subglyph
->flags
& FT_SUBGLYPH_FLAG_USE_MY_METRICS
)
334 num_points
= gloader
->base
.outline
.n_points
;
335 num_new_points
= num_points
- num_base_points
;
337 /* now perform the transformation required for this subglyph */
338 if (subglyph
->flags
& (FT_SUBGLYPH_FLAG_SCALE
339 | FT_SUBGLYPH_FLAG_XY_SCALE
340 | FT_SUBGLYPH_FLAG_2X2
))
342 FT_Vector
* cur
= gloader
->base
.outline
.points
+ num_base_points
;
343 FT_Vector
* limit
= cur
+ num_new_points
;
346 for (; cur
< limit
; cur
++)
347 FT_Vector_Transform(cur
, &subglyph
->transform
);
351 if (!(subglyph
->flags
& FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES
))
353 FT_Int k
= subglyph
->arg1
;
354 FT_UInt l
= subglyph
->arg2
;
359 if (start_point
+ k
>= num_base_points
360 || l
>= (FT_UInt
)num_new_points
)
362 error
= FT_Err_Invalid_Composite
;
366 l
+= num_base_points
;
368 /* for now, only use the current point coordinates; */
369 /* we eventually may consider another approach */
370 p1
= gloader
->base
.outline
.points
+ start_point
+ k
;
371 p2
= gloader
->base
.outline
.points
+ start_point
+ l
;
378 x
= FT_MulFix(subglyph
->arg1
, hints
->x_scale
) + hints
->x_delta
;
379 y
= FT_MulFix(subglyph
->arg2
, hints
->y_scale
) + hints
->y_delta
;
386 FT_Outline dummy
= gloader
->base
.outline
;
389 dummy
.points
+= num_base_points
;
390 dummy
.n_points
= (short)num_new_points
;
392 FT_Outline_Translate(&dummy
, x
, y
);
399 /* we don't support other formats (yet?) */
400 error
= FT_Err_Unimplemented_Feature
;
410 vvector
.x
= slot
->metrics
.vertBearingX
- slot
->metrics
.horiBearingX
;
411 vvector
.y
= slot
->metrics
.vertBearingY
- slot
->metrics
.horiBearingY
;
412 vvector
.x
= FT_MulFix(vvector
.x
, metrics
->scaler
.x_scale
);
413 vvector
.y
= FT_MulFix(vvector
.y
, metrics
->scaler
.y_scale
);
415 /* transform the hinted outline if needed */
416 if (loader
->transformed
)
418 FT_Outline_Transform(&gloader
->base
.outline
, &loader
->trans_matrix
);
419 FT_Vector_Transform(&vvector
, &loader
->trans_matrix
);
422 /* we must translate our final outline by -pp1.x */
423 /* and compute the new metrics */
425 FT_Outline_Translate(&gloader
->base
.outline
, -loader
->pp1
.x
, 0);
427 FT_Outline_Get_CBox(&gloader
->base
.outline
, &bbox
);
429 bbox
.xMin
= TA_PIX_FLOOR(bbox
.xMin
);
430 bbox
.yMin
= TA_PIX_FLOOR(bbox
.yMin
);
431 bbox
.xMax
= TA_PIX_CEIL(bbox
.xMax
);
432 bbox
.yMax
= TA_PIX_CEIL(bbox
.yMax
);
434 slot
->metrics
.width
= bbox
.xMax
- bbox
.xMin
;
435 slot
->metrics
.height
= bbox
.yMax
- bbox
.yMin
;
436 slot
->metrics
.horiBearingX
= bbox
.xMin
;
437 slot
->metrics
.horiBearingY
= bbox
.yMax
;
439 slot
->metrics
.vertBearingX
= TA_PIX_FLOOR(bbox
.xMin
+ vvector
.x
);
440 slot
->metrics
.vertBearingY
= TA_PIX_FLOOR(bbox
.yMax
+ vvector
.y
);
442 /* for mono-width fonts (like Andale, Courier, etc.) we need */
443 /* to keep the original rounded advance width; ditto for */
444 /* digits if all have the same advance width */
445 if (scaler
->render_mode
!= FT_RENDER_MODE_LIGHT
446 && (FT_IS_FIXED_WIDTH(slot
->face
)
447 || (ta_face_globals_is_digit(loader
->globals
, glyph_index
)
448 && metrics
->digits_have_same_width
)))
450 slot
->metrics
.horiAdvance
= FT_MulFix(slot
->metrics
.horiAdvance
,
451 metrics
->scaler
.x_scale
);
453 /* set delta values to 0, otherwise code that uses them */
454 /* is going to ruin the fixed advance width */
460 /* non-spacing glyphs must stay as-is */
461 if (slot
->metrics
.horiAdvance
)
462 slot
->metrics
.horiAdvance
= loader
->pp2
.x
- loader
->pp1
.x
;
465 slot
->metrics
.vertAdvance
= FT_MulFix(slot
->metrics
.vertAdvance
,
466 metrics
->scaler
.y_scale
);
468 slot
->metrics
.horiAdvance
= TA_PIX_ROUND(slot
->metrics
.horiAdvance
);
469 slot
->metrics
.vertAdvance
= TA_PIX_ROUND(slot
->metrics
.vertAdvance
);
472 /* now copy outline into glyph slot */
473 TA_GlyphLoader_Rewind(internal
->loader
);
474 error
= TA_GlyphLoader_CopyPoints(internal
->loader
, gloader
);
478 /* reassign all outline fields except flags to protect them */
479 slot
->outline
.n_contours
= internal
->loader
->base
.outline
.n_contours
;
480 slot
->outline
.n_points
= internal
->loader
->base
.outline
.n_points
;
481 slot
->outline
.points
= internal
->loader
->base
.outline
.points
;
482 slot
->outline
.tags
= internal
->loader
->base
.outline
.tags
;
483 slot
->outline
.contours
= internal
->loader
->base
.outline
.contours
;
485 slot
->format
= FT_GLYPH_FORMAT_OUTLINE
;
497 ta_loader_load_glyph(FONT
* font
,
503 FT_Size size
= face
->size
;
504 TA_Loader loader
= font
->loader
;
509 return FT_Err_Invalid_Argument
;
511 memset(&scaler
, 0, sizeof (TA_ScalerRec
));
514 scaler
.x_scale
= size
->metrics
.x_scale
;
515 scaler
.x_delta
= 0; /* XXX: TODO: add support for sub-pixel hinting */
516 scaler
.y_scale
= size
->metrics
.y_scale
;
517 scaler
.y_delta
= 0; /* XXX: TODO: add support for sub-pixel hinting */
519 scaler
.render_mode
= FT_LOAD_TARGET_MODE(load_flags
);
520 scaler
.flags
= 0; /* XXX: fix this */
522 /* XXX this is an ugly hack of ttfautohint: */
523 /* bit 29 triggers vertical hinting only, */
524 /* and bits 25-28 give the x height increase limit */
525 if (load_flags
& (1 << 29))
526 scaler
.flags
|= TA_SCALER_FLAG_NO_HORIZONTAL
;
527 scaler
.flags
|= ((load_flags
>> 25) & 15) << 3; /* bits 3-6 */
529 /* note that the fallback script can't be changed anymore */
530 /* after the first call of `ta_loader_load_glyph' */
531 error
= ta_loader_reset(font
, face
);
534 TA_ScriptMetrics metrics
;
538 #ifdef FT_OPTION_AUTOFIT2
539 /* XXX: undocumented hook to activate the latin2 hinter */
540 if (load_flags
& (1UL << 20))
544 error
= ta_face_globals_get_metrics(loader
->globals
, gindex
,
548 loader
->metrics
= metrics
;
550 if (metrics
->clazz
->script_metrics_scale
)
551 metrics
->clazz
->script_metrics_scale(metrics
, &scaler
);
553 metrics
->scaler
= scaler
;
555 load_flags
|= FT_LOAD_NO_SCALE
556 | FT_LOAD_IGNORE_TRANSFORM
;
557 load_flags
&= ~FT_LOAD_RENDER
;
559 if (metrics
->clazz
->script_hints_init
)
561 error
= metrics
->clazz
->script_hints_init(&loader
->hints
,
567 error
= ta_loader_load_g(loader
, &scaler
, gindex
, load_flags
, 0);
576 ta_loader_register_hints_recorder(TA_Loader loader
,
577 TA_Hints_Recorder hints_recorder
,
580 loader
->hints
.recorder
= hints_recorder
;
581 loader
->hints
.user
= user
;
584 /* end of taloader.c */