4 * Copyright (C) 2011-2013 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
, font
);
80 face
->autohint
.data
= (FT_Pointer
)loader
->globals
;
81 face
->autohint
.finalizer
= (FT_Generic_Finalizer
)ta_face_globals_free
;
89 /* finalize glyph loader */
92 ta_loader_done(FONT
* font
)
94 TA_Loader loader
= font
->loader
;
97 ta_glyph_hints_done(&loader
->hints
);
100 loader
->globals
= NULL
;
103 _ta_debug_hints
= NULL
;
105 TA_GlyphLoader_Done(loader
->gloader
);
106 loader
->gloader
= NULL
;
110 /* load a single glyph component; this routine calls itself recursively, */
111 /* if necessary, and does the main work of `ta_loader_load_glyph' */
114 ta_loader_load_g(TA_Loader loader
,
121 FT_Face face
= loader
->face
;
122 TA_GlyphLoader gloader
= loader
->gloader
;
123 TA_ScriptMetrics metrics
= loader
->metrics
;
124 TA_GlyphHints hints
= &loader
->hints
;
125 FT_GlyphSlot slot
= face
->glyph
;
127 FT_Slot_Internal internal
= slot
->internal
;
132 flags
= load_flags
| FT_LOAD_LINEAR_DESIGN
;
133 error
= FT_Load_Glyph(face
, glyph_index
, flags
);
138 loader
->transformed
= internal
->glyph_transformed
;
139 if (loader
->transformed
)
144 loader
->trans_matrix
= internal
->glyph_matrix
;
145 loader
->trans_delta
= internal
->glyph_delta
;
147 inverse
= loader
->trans_matrix
;
148 FT_Matrix_Invert(&inverse
);
149 FT_Vector_Transform(&loader
->trans_delta
, &inverse
);
153 switch (slot
->format
)
155 case FT_GLYPH_FORMAT_OUTLINE
:
156 /* translate the loaded glyph when an internal transform is needed */
157 if (loader
->transformed
)
158 FT_Outline_Translate(&slot
->outline
,
159 loader
->trans_delta
.x
,
160 loader
->trans_delta
.y
);
162 /* copy the outline points in the loader's current extra points */
163 /* which are used to keep original glyph coordinates */
164 error
= TA_GLYPHLOADER_CHECK_POINTS(gloader
,
165 slot
->outline
.n_points
+ 4,
166 slot
->outline
.n_contours
);
170 memcpy(gloader
->current
.outline
.points
,
171 slot
->outline
.points
,
172 slot
->outline
.n_points
* sizeof (FT_Vector
));
173 memcpy(gloader
->current
.outline
.contours
,
174 slot
->outline
.contours
,
175 slot
->outline
.n_contours
* sizeof (short));
176 memcpy(gloader
->current
.outline
.tags
,
178 slot
->outline
.n_points
* sizeof (char));
180 gloader
->current
.outline
.n_points
= slot
->outline
.n_points
;
181 gloader
->current
.outline
.n_contours
= slot
->outline
.n_contours
;
183 /* compute original horizontal phantom points */
184 /* (and ignore vertical ones) */
185 loader
->pp1
.x
= hints
->x_delta
;
186 loader
->pp1
.y
= hints
->y_delta
;
187 loader
->pp2
.x
= FT_MulFix(slot
->metrics
.horiAdvance
,
188 hints
->x_scale
) + hints
->x_delta
;
189 loader
->pp2
.y
= hints
->y_delta
;
191 /* be sure to check for spacing glyphs */
192 if (slot
->outline
.n_points
== 0)
195 /* now load the slot image into the auto-outline */
196 /* and run the automatic hinting process */
197 if (metrics
->clazz
->script_hints_apply
)
198 metrics
->clazz
->script_hints_apply(hints
,
199 &gloader
->current
.outline
,
202 /* we now need to adjust the metrics according to the change in */
203 /* width/positioning that occurred during the hinting process */
204 if (scaler
->render_mode
!= FT_RENDER_MODE_LIGHT
)
206 FT_Pos old_rsb
, old_lsb
, new_lsb
;
207 FT_Pos pp1x_uh
, pp2x_uh
;
208 TA_AxisHints axis
= &hints
->axis
[TA_DIMENSION_HORZ
];
210 TA_Edge edge1
= axis
->edges
; /* leftmost edge */
211 TA_Edge edge2
= edge1
+ axis
->num_edges
- 1; /* rightmost edge */
214 if (axis
->num_edges
> 1 && TA_HINTS_DO_ADVANCE(hints
))
216 old_rsb
= loader
->pp2
.x
- edge2
->opos
;
217 old_lsb
= edge1
->opos
;
218 new_lsb
= edge1
->pos
;
220 /* remember unhinted values to later account */
221 /* for rounding errors */
222 pp1x_uh
= new_lsb
- old_lsb
;
223 pp2x_uh
= edge2
->pos
+ old_rsb
;
225 /* prefer too much space over too little space */
226 /* for very small sizes */
232 loader
->pp1
.x
= TA_PIX_ROUND(pp1x_uh
);
233 loader
->pp2
.x
= TA_PIX_ROUND(pp2x_uh
);
235 if (loader
->pp1
.x
>= new_lsb
238 if (loader
->pp2
.x
<= edge2
->pos
242 slot
->lsb_delta
= loader
->pp1
.x
- pp1x_uh
;
243 slot
->rsb_delta
= loader
->pp2
.x
- pp2x_uh
;
247 FT_Pos pp1x
= loader
->pp1
.x
;
248 FT_Pos pp2x
= loader
->pp2
.x
;
251 loader
->pp1
.x
= TA_PIX_ROUND(pp1x
);
252 loader
->pp2
.x
= TA_PIX_ROUND(pp2x
);
254 slot
->lsb_delta
= loader
->pp1
.x
- pp1x
;
255 slot
->rsb_delta
= loader
->pp2
.x
- pp2x
;
260 FT_Pos pp1x
= loader
->pp1
.x
;
261 FT_Pos pp2x
= loader
->pp2
.x
;
264 loader
->pp1
.x
= TA_PIX_ROUND(pp1x
+ hints
->xmin_delta
);
265 loader
->pp2
.x
= TA_PIX_ROUND(pp2x
+ hints
->xmax_delta
);
267 slot
->lsb_delta
= loader
->pp1
.x
- pp1x
;
268 slot
->rsb_delta
= loader
->pp2
.x
- pp2x
;
271 /* good, we simply add the glyph to our loader's base */
272 TA_GlyphLoader_Add(gloader
);
275 case FT_GLYPH_FORMAT_COMPOSITE
:
277 FT_UInt nn
, num_subglyphs
= slot
->num_subglyphs
;
278 FT_UInt num_base_subgs
, start_point
;
279 TA_SubGlyph subglyph
;
282 start_point
= gloader
->base
.outline
.n_points
;
284 /* first of all, copy the subglyph descriptors in the glyph loader */
285 error
= TA_GlyphLoader_CheckSubGlyphs(gloader
, num_subglyphs
);
289 memcpy(gloader
->current
.subglyphs
,
291 num_subglyphs
* sizeof (TA_SubGlyphRec
));
293 gloader
->current
.num_subglyphs
= num_subglyphs
;
294 num_base_subgs
= gloader
->base
.num_subglyphs
;
296 /* now read each subglyph independently */
297 for (nn
= 0; nn
< num_subglyphs
; nn
++)
301 FT_UInt num_points
, num_new_points
, num_base_points
;
304 /* gloader.current.subglyphs can change during glyph loading due */
305 /* to re-allocation -- we must recompute the current subglyph on */
307 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ nn
;
312 num_base_points
= gloader
->base
.outline
.n_points
;
314 error
= ta_loader_load_g(loader
, scaler
, subglyph
->index
,
315 load_flags
, depth
+ 1);
319 /* recompute subglyph pointer */
320 subglyph
= gloader
->base
.subglyphs
+ num_base_subgs
+ nn
;
322 if (subglyph
->flags
& FT_SUBGLYPH_FLAG_USE_MY_METRICS
)
333 num_points
= gloader
->base
.outline
.n_points
;
334 num_new_points
= num_points
- num_base_points
;
336 /* now perform the transformation required for this subglyph */
337 if (subglyph
->flags
& (FT_SUBGLYPH_FLAG_SCALE
338 | FT_SUBGLYPH_FLAG_XY_SCALE
339 | FT_SUBGLYPH_FLAG_2X2
))
341 FT_Vector
* cur
= gloader
->base
.outline
.points
+ num_base_points
;
342 FT_Vector
* limit
= cur
+ num_new_points
;
345 for (; cur
< limit
; cur
++)
346 FT_Vector_Transform(cur
, &subglyph
->transform
);
350 if (!(subglyph
->flags
& FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES
))
352 FT_Int k
= subglyph
->arg1
;
353 FT_UInt l
= subglyph
->arg2
;
358 if (start_point
+ k
>= num_base_points
359 || l
>= (FT_UInt
)num_new_points
)
361 error
= FT_Err_Invalid_Composite
;
365 l
+= num_base_points
;
367 /* for now, only use the current point coordinates; */
368 /* we eventually may consider another approach */
369 p1
= gloader
->base
.outline
.points
+ start_point
+ k
;
370 p2
= gloader
->base
.outline
.points
+ start_point
+ l
;
377 x
= FT_MulFix(subglyph
->arg1
, hints
->x_scale
) + hints
->x_delta
;
378 y
= FT_MulFix(subglyph
->arg2
, hints
->y_scale
) + hints
->y_delta
;
385 FT_Outline dummy
= gloader
->base
.outline
;
388 dummy
.points
+= num_base_points
;
389 dummy
.n_points
= (short)num_new_points
;
391 FT_Outline_Translate(&dummy
, x
, y
);
398 /* we don't support other formats (yet?) */
399 error
= FT_Err_Unimplemented_Feature
;
409 vvector
.x
= slot
->metrics
.vertBearingX
- slot
->metrics
.horiBearingX
;
410 vvector
.y
= slot
->metrics
.vertBearingY
- slot
->metrics
.horiBearingY
;
411 vvector
.x
= FT_MulFix(vvector
.x
, metrics
->scaler
.x_scale
);
412 vvector
.y
= FT_MulFix(vvector
.y
, metrics
->scaler
.y_scale
);
414 /* transform the hinted outline if needed */
415 if (loader
->transformed
)
417 FT_Outline_Transform(&gloader
->base
.outline
, &loader
->trans_matrix
);
418 FT_Vector_Transform(&vvector
, &loader
->trans_matrix
);
421 /* we must translate our final outline by -pp1.x */
422 /* and compute the new metrics */
424 FT_Outline_Translate(&gloader
->base
.outline
, -loader
->pp1
.x
, 0);
426 FT_Outline_Get_CBox(&gloader
->base
.outline
, &bbox
);
428 bbox
.xMin
= TA_PIX_FLOOR(bbox
.xMin
);
429 bbox
.yMin
= TA_PIX_FLOOR(bbox
.yMin
);
430 bbox
.xMax
= TA_PIX_CEIL(bbox
.xMax
);
431 bbox
.yMax
= TA_PIX_CEIL(bbox
.yMax
);
433 slot
->metrics
.width
= bbox
.xMax
- bbox
.xMin
;
434 slot
->metrics
.height
= bbox
.yMax
- bbox
.yMin
;
435 slot
->metrics
.horiBearingX
= bbox
.xMin
;
436 slot
->metrics
.horiBearingY
= bbox
.yMax
;
438 slot
->metrics
.vertBearingX
= TA_PIX_FLOOR(bbox
.xMin
+ vvector
.x
);
439 slot
->metrics
.vertBearingY
= TA_PIX_FLOOR(bbox
.yMax
+ vvector
.y
);
441 /* for mono-width fonts (like Andale, Courier, etc.) we need */
442 /* to keep the original rounded advance width; ditto for */
443 /* digits if all have the same advance width */
444 if (scaler
->render_mode
!= FT_RENDER_MODE_LIGHT
445 && (FT_IS_FIXED_WIDTH(slot
->face
)
446 || (ta_face_globals_is_digit(loader
->globals
, glyph_index
)
447 && metrics
->digits_have_same_width
)))
449 slot
->metrics
.horiAdvance
= FT_MulFix(slot
->metrics
.horiAdvance
,
450 metrics
->scaler
.x_scale
);
452 /* set delta values to 0, otherwise code that uses them */
453 /* is going to ruin the fixed advance width */
459 /* non-spacing glyphs must stay as-is */
460 if (slot
->metrics
.horiAdvance
)
461 slot
->metrics
.horiAdvance
= loader
->pp2
.x
- loader
->pp1
.x
;
464 slot
->metrics
.vertAdvance
= FT_MulFix(slot
->metrics
.vertAdvance
,
465 metrics
->scaler
.y_scale
);
467 slot
->metrics
.horiAdvance
= TA_PIX_ROUND(slot
->metrics
.horiAdvance
);
468 slot
->metrics
.vertAdvance
= TA_PIX_ROUND(slot
->metrics
.vertAdvance
);
471 /* now copy outline into glyph slot */
472 TA_GlyphLoader_Rewind(internal
->loader
);
473 error
= TA_GlyphLoader_CopyPoints(internal
->loader
, gloader
);
477 /* reassign all outline fields except flags to protect them */
478 slot
->outline
.n_contours
= internal
->loader
->base
.outline
.n_contours
;
479 slot
->outline
.n_points
= internal
->loader
->base
.outline
.n_points
;
480 slot
->outline
.points
= internal
->loader
->base
.outline
.points
;
481 slot
->outline
.tags
= internal
->loader
->base
.outline
.tags
;
482 slot
->outline
.contours
= internal
->loader
->base
.outline
.contours
;
484 slot
->format
= FT_GLYPH_FORMAT_OUTLINE
;
496 ta_loader_load_glyph(FONT
* font
,
502 FT_Size size
= face
->size
;
503 TA_Loader loader
= font
->loader
;
508 return FT_Err_Invalid_Argument
;
510 memset(&scaler
, 0, sizeof (TA_ScalerRec
));
513 scaler
.x_scale
= size
->metrics
.x_scale
;
514 scaler
.x_delta
= 0; /* XXX: TODO: add support for sub-pixel hinting */
515 scaler
.y_scale
= size
->metrics
.y_scale
;
516 scaler
.y_delta
= 0; /* XXX: TODO: add support for sub-pixel hinting */
518 scaler
.render_mode
= FT_LOAD_TARGET_MODE(load_flags
);
519 scaler
.flags
= 0; /* XXX: fix this */
521 /* XXX this is an ugly hack of ttfautohint: */
522 /* bit 29 triggers vertical hinting only */
523 if (load_flags
& (1 << 29))
524 scaler
.flags
|= TA_SCALER_FLAG_NO_HORIZONTAL
;
526 /* note that the fallback script can't be changed anymore */
527 /* after the first call of `ta_loader_load_glyph' */
528 error
= ta_loader_reset(font
, face
);
531 TA_ScriptMetrics metrics
;
535 #ifdef FT_OPTION_AUTOFIT2
536 /* XXX: undocumented hook to activate the latin2 hinter */
537 if (load_flags
& (1UL << 20))
541 error
= ta_face_globals_get_metrics(loader
->globals
, gindex
,
545 loader
->metrics
= metrics
;
547 if (metrics
->clazz
->script_metrics_scale
)
548 metrics
->clazz
->script_metrics_scale(metrics
, &scaler
);
550 metrics
->scaler
= scaler
;
552 load_flags
|= FT_LOAD_NO_SCALE
553 | FT_LOAD_IGNORE_TRANSFORM
;
554 load_flags
&= ~FT_LOAD_RENDER
;
556 if (metrics
->clazz
->script_hints_init
)
558 error
= metrics
->clazz
->script_hints_init(&loader
->hints
,
564 error
= ta_loader_load_g(loader
, &scaler
, gindex
, load_flags
, 0);
573 ta_loader_register_hints_recorder(TA_Loader loader
,
574 TA_Hints_Recorder hints_recorder
,
577 loader
->hints
.recorder
= hints_recorder
;
578 loader
->hints
.user
= user
;
581 /* end of taloader.c */