4 * Copyright (C) 2011-2016 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 `afglobal.c' (2011-Mar-28) from FreeType */
18 /* heavily modified 2011 by Werner Lemberg <wl@gnu.org> */
27 #define SCRIPT(s, S, d, h, H, ss) \
28 const TA_ScriptClassRec ta_ ## s ## _script_class = \
31 ta_ ## s ## _uniranges, \
32 ta_ ## s ## _nonbase_uniranges, \
37 #include <ttfautohint-scripts.h>
41 #define STYLE(s, S, d, ws, sc, ss, c) \
42 const TA_StyleClassRec ta_ ## s ## _style_class = \
54 /* get writing system specific header files */
56 #define WRITING_SYSTEM(ws, WS) /* empty */
61 #define WRITING_SYSTEM(ws, WS) \
62 &ta_ ## ws ## _writing_system_class,
64 TA_WritingSystemClass
const ta_writing_system_classes
[] =
69 NULL
/* do not remove */
74 #define SCRIPT(s, S, d, h, H, ss) \
75 &ta_ ## s ## _script_class,
77 TA_ScriptClass
const ta_script_classes
[] =
80 #include <ttfautohint-scripts.h>
82 NULL
/* do not remove */
87 #define STYLE(s, S, d, ws, sc, ss, c) \
88 &ta_ ## s ## _style_class,
90 TA_StyleClass
const ta_style_classes
[] =
95 NULL
/* do not remove */
102 #define STYLE(s, S, d, ws, sc, ss, c) #s,
104 const char* ta_style_names
[] =
107 #include <tastyles.h>
111 #endif /* TA_DEBUG */
114 /* Recursively assign a style to all components of a composite glyph. */
117 ta_face_globals_scan_composite(FT_Face face
,
121 FT_Int nesting_level
)
128 FT_Int
* subglyph_indices
= NULL
;
129 FT_UInt used_subglyphs
;
135 FT_Matrix p_transform
;
138 /* limit recursion arbitrarily */
139 if (nesting_level
> 100)
140 return FT_Err_Invalid_Table
;
142 error
= FT_Load_Glyph(face
, (FT_UInt
)gindex
, FT_LOAD_NO_RECURSE
);
148 /* in FreeType < 2.5.4, FT_Get_SubGlyph_Info always returns an error */
149 /* due to a bug even if the call was successful; */
150 /* for this reason we do the argument checking by ourselves */
151 /* and ignore the returned error code of FT_Get_SubGlyph_Info */
152 if (!glyph
->subglyphs
153 || glyph
->format
!= FT_GLYPH_FORMAT_COMPOSITE
)
156 /* since a call to FT_Load_Glyph changes `glyph', */
157 /* we have to first store the subglyph indices, then do the recursion */
158 subglyph_indices
= (FT_Int
*)malloc(sizeof (FT_Int
) * glyph
->num_subglyphs
);
159 if (!subglyph_indices
)
160 return FT_Err_Out_Of_Memory
;
163 for (i
= 0; i
< glyph
->num_subglyphs
; i
++)
165 (void)FT_Get_SubGlyph_Info(glyph
,
173 if (p_index
>= face
->num_glyphs
174 || (gstyles
[p_index
] & TA_STYLE_MASK
) != TA_STYLE_UNASSIGNED
)
177 /* only take subglyphs that are not shifted vertically; */
178 /* otherwise blue zones don't fit */
179 if (p_flags
& ARGS_ARE_XY_VALUES
182 gstyles
[p_index
] = gstyle
;
183 subglyph_indices
[used_subglyphs
++] = p_index
;
188 for (i
= 0; i
< used_subglyphs
; i
++)
190 error
= ta_face_globals_scan_composite(face
,
199 free(subglyph_indices
);
205 /* Compute the style index of each glyph within a given face. */
208 ta_face_globals_compute_style_coverage(TA_FaceGlobals globals
)
211 FT_Face face
= globals
->face
;
212 FT_CharMap old_charmap
= face
->charmap
;
213 FT_UShort
* gstyles
= globals
->glyph_styles
;
216 FT_UInt dflt
= ~0U; /* a non-valid value */
219 /* the value TA_STYLE_UNASSIGNED means `uncovered glyph' */
220 for (i
= 0; i
< (unsigned int)globals
->glyph_count
; i
++)
221 gstyles
[i
] = TA_STYLE_UNASSIGNED
;
223 error
= FT_Select_Charmap(face
, FT_ENCODING_UNICODE
);
226 /* ignore this error; we simply use the fallback style */
227 /* XXX: Shouldn't we rather disable hinting? */
232 /* scan each style in a Unicode charmap */
233 for (ss
= 0; ta_style_classes
[ss
]; ss
++)
235 TA_StyleClass style_class
= ta_style_classes
[ss
];
236 TA_ScriptClass script_class
= ta_script_classes
[style_class
->script
];
237 TA_Script_UniRange range
;
240 if (script_class
->script_uni_ranges
->first
== 0)
243 /* scan all Unicode points in the range and */
244 /* set the corresponding glyph style index */
245 if (style_class
->coverage
== TA_COVERAGE_DEFAULT
)
247 if ((FT_UInt
)style_class
->script
== globals
->font
->default_script
)
250 for (range
= script_class
->script_uni_ranges
;
254 FT_ULong charcode
= range
->first
;
258 gindex
= FT_Get_Char_Index(face
, charcode
);
261 && gindex
< (FT_ULong
)globals
->glyph_count
262 && (gstyles
[gindex
] & TA_STYLE_MASK
) == TA_STYLE_UNASSIGNED
)
263 gstyles
[gindex
] = (FT_UShort
)ss
;
267 charcode
= FT_Get_Next_Char(face
, charcode
, &gindex
);
269 if (gindex
== 0 || charcode
> range
->last
)
272 if (gindex
< (FT_ULong
)globals
->glyph_count
273 && (gstyles
[gindex
] & TA_STYLE_MASK
) == TA_STYLE_UNASSIGNED
)
274 gstyles
[gindex
] = (FT_UShort
)ss
;
278 /* do the same for the script's non-base characters */
279 for (range
= script_class
->script_uni_nonbase_ranges
;
283 FT_ULong charcode
= range
->first
;
287 gindex
= FT_Get_Char_Index(face
, charcode
);
290 && gindex
< (FT_ULong
)globals
->glyph_count
291 && (gstyles
[gindex
] & TA_STYLE_MASK
) == (FT_UShort
)ss
)
292 gstyles
[gindex
] |= TA_NONBASE
;
296 charcode
= FT_Get_Next_Char(face
, charcode
, &gindex
);
298 if (gindex
== 0 || charcode
> range
->last
)
301 if (gindex
< (FT_ULong
)globals
->glyph_count
302 && (gstyles
[gindex
] & TA_STYLE_MASK
) == (FT_UShort
)ss
)
303 gstyles
[gindex
] |= TA_NONBASE
;
309 /* get glyphs not directly addressable by cmap */
310 ta_shaper_get_coverage(globals
, style_class
, gstyles
, 0);
314 /* handle the remaining default OpenType features ... */
315 for (ss
= 0; ta_style_classes
[ss
]; ss
++)
317 TA_StyleClass style_class
= ta_style_classes
[ss
];
320 if (style_class
->coverage
== TA_COVERAGE_DEFAULT
)
321 ta_shaper_get_coverage(globals
, style_class
, gstyles
, 0);
324 /* ... and finally the default OpenType features of the default script */
325 ta_shaper_get_coverage(globals
, ta_style_classes
[dflt
], gstyles
, 1);
327 /* mark ASCII digits */
328 for (i
= 0x30; i
<= 0x39; i
++)
330 FT_UInt gindex
= FT_Get_Char_Index(face
, i
);
334 && gindex
< (FT_ULong
)globals
->glyph_count
)
335 gstyles
[gindex
] |= TA_DIGIT
;
338 /* now walk over all glyphs and check for composites: */
339 /* since subglyphs are hinted separately if option `hint-composites' */
340 /* isn't set, we have to tag them with style indices, too */
345 for (nn
= 0; nn
< globals
->glyph_count
; nn
++)
347 if ((gstyles
[nn
] & TA_STYLE_MASK
) == TA_STYLE_UNASSIGNED
)
350 error
= ta_face_globals_scan_composite(globals
->face
,
361 /* by default, all uncovered glyphs are set to the fallback style */
362 /* XXX: Shouldn't we disable hinting or do something similar? */
363 if (globals
->font
->fallback_style
!= (TA_Style
)TA_STYLE_UNASSIGNED
)
368 for (nn
= 0; nn
< globals
->glyph_count
; nn
++)
370 if ((gstyles
[nn
] & TA_STYLE_MASK
) == TA_STYLE_UNASSIGNED
)
372 gstyles
[nn
] &= ~TA_STYLE_MASK
;
373 gstyles
[nn
] |= globals
->font
->fallback_style
;
380 if (face
->num_faces
> 1)
382 "style coverage (subfont %d, glyf table index %d)\n"
383 "================================================\n"
386 globals
->font
->sfnts
[face
->face_index
].glyf_idx
));
393 for (ss
= 0; ta_style_classes
[ss
]; ss
++)
395 TA_StyleClass style_class
= ta_style_classes
[ss
];
400 TA_LOG_GLOBAL(("%s:\n", ta_style_names
[style_class
->style
]));
402 for (idx
= 0; idx
< globals
->glyph_count
; idx
++)
404 if ((gstyles
[idx
] & TA_STYLE_MASK
) == style_class
->style
)
407 TA_LOG_GLOBAL((" "));
409 TA_LOG_GLOBAL((" %d", idx
));
413 TA_LOG_GLOBAL(("\n"));
418 TA_LOG_GLOBAL((" (none)\n"));
420 TA_LOG_GLOBAL(("\n"));
423 #endif /* TA_DEBUG */
425 FT_Set_Charmap(face
, old_charmap
);
431 ta_face_globals_new(FT_Face face
,
432 TA_FaceGlobals
*aglobals
,
436 TA_FaceGlobals globals
;
439 /* we allocate an TA_FaceGlobals structure together */
440 /* with the glyph_styles array */
441 globals
= (TA_FaceGlobals
)calloc(
442 1, sizeof (TA_FaceGlobalsRec
) +
443 (FT_ULong
)face
->num_glyphs
* sizeof (FT_UShort
));
446 error
= FT_Err_Out_Of_Memory
;
450 globals
->face
= face
;
451 globals
->glyph_count
= face
->num_glyphs
;
452 /* right after the globals structure come the glyph styles */
453 globals
->glyph_styles
= (FT_UShort
*)(globals
+ 1);
454 globals
->font
= font
;
455 globals
->hb_font
= hb_ft_font_create(face
, NULL
);
456 globals
->hb_buf
= hb_buffer_create();
458 error
= ta_face_globals_compute_style_coverage(globals
);
461 ta_face_globals_free(globals
);
465 globals
->increase_x_height
= TA_PROP_INCREASE_X_HEIGHT_MAX
;
474 ta_face_globals_free(TA_FaceGlobals globals
)
481 for (nn
= 0; nn
< TA_STYLE_MAX
; nn
++)
483 if (globals
->metrics
[nn
])
485 TA_StyleClass style_class
=
486 ta_style_classes
[nn
];
487 TA_WritingSystemClass writing_system_class
=
488 ta_writing_system_classes
[style_class
->writing_system
];
491 if (writing_system_class
->style_metrics_done
)
492 writing_system_class
->style_metrics_done(globals
->metrics
[nn
]);
494 free(globals
->metrics
[nn
]);
495 globals
->metrics
[nn
] = NULL
;
499 hb_font_destroy(globals
->hb_font
);
500 globals
->hb_font
= NULL
;
502 hb_buffer_destroy(globals
->hb_buf
);
503 globals
->hb_buf
= NULL
;
505 globals
->glyph_count
= 0;
506 globals
->glyph_styles
= NULL
; /* no need to free this one! */
507 globals
->face
= NULL
;
515 ta_face_globals_get_metrics(TA_FaceGlobals globals
,
518 TA_StyleMetrics
*ametrics
)
520 TA_StyleMetrics metrics
= NULL
;
521 TA_Style style
= (TA_Style
)options
;
522 TA_WritingSystemClass writing_system_class
;
523 TA_StyleClass style_class
;
524 FT_Error error
= FT_Err_Ok
;
527 if (gindex
>= (FT_ULong
)globals
->glyph_count
)
529 error
= FT_Err_Invalid_Argument
;
533 /* if we have a forced style (via `options'), use it, */
534 /* otherwise look into `glyph_styles' array */
535 if (style
== TA_STYLE_NONE_DFLT
|| style
+ 1 >= TA_STYLE_MAX
)
536 style
= (TA_Style
)(globals
->glyph_styles
[gindex
]
537 & TA_STYLE_UNASSIGNED
);
540 ta_style_classes
[style
];
541 writing_system_class
=
542 ta_writing_system_classes
[style_class
->writing_system
];
544 metrics
= globals
->metrics
[style
];
547 /* create the global metrics object if necessary */
548 metrics
= (TA_StyleMetrics
)
549 calloc(1, writing_system_class
->style_metrics_size
);
552 error
= FT_Err_Out_Of_Memory
;
556 metrics
->style_class
= style_class
;
557 metrics
->globals
= globals
;
559 if (writing_system_class
->style_metrics_init
)
561 error
= writing_system_class
->style_metrics_init(metrics
,
565 if (writing_system_class
->style_metrics_done
)
566 writing_system_class
->style_metrics_done(metrics
);
574 globals
->metrics
[style
] = metrics
;
585 ta_face_globals_is_digit(TA_FaceGlobals globals
,
588 if (gindex
< (FT_ULong
)globals
->glyph_count
)
589 return (FT_Bool
)(globals
->glyph_styles
[gindex
] & TA_DIGIT
);
594 /* end of taglobal.c */