4 * Copyright (C) 2011-2014 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> */
24 #include "taharfbuzz.h"
28 #define SCRIPT(s, S, d, h, sc1, sc2, sc3) \
29 const TA_ScriptClassRec ta_ ## s ## _script_class = \
32 ta_ ## s ## _uniranges, \
36 #include <ttfautohint-scripts.h>
40 #define STYLE(s, S, d, ws, sc, ss, c) \
41 const TA_StyleClassRec ta_ ## s ## _style_class = \
53 /* get writing system specific header files */
55 #define WRITING_SYSTEM(ws, WS) /* empty */
60 #define WRITING_SYSTEM(ws, WS) \
61 &ta_ ## ws ## _writing_system_class,
63 TA_WritingSystemClass
const ta_writing_system_classes
[] =
68 NULL
/* do not remove */
73 #define SCRIPT(s, S, d, h, sc1, sc2, sc3) \
74 &ta_ ## s ## _script_class,
76 TA_ScriptClass
const ta_script_classes
[] =
79 #include <ttfautohint-scripts.h>
81 NULL
/* do not remove */
86 #define STYLE(s, S, d, ws, sc, ss, c) \
87 &ta_ ## s ## _style_class,
89 TA_StyleClass
const ta_style_classes
[] =
94 NULL
/* do not remove */
101 #define STYLE(s, S, d, ws, sc, ss, c) #s,
103 const char* ta_style_names
[] =
106 #include <tastyles.h>
110 #endif /* TA_DEBUG */
113 /* Recursively assign a style to all components of a composite glyph. */
116 ta_face_globals_scan_composite(FT_Face face
,
120 FT_Int nesting_level
)
127 FT_Int
* subglyph_indices
= NULL
;
128 FT_UInt used_subglyphs
;
134 FT_Matrix p_transform
;
137 /* limit recursion arbitrarily */
138 if (nesting_level
> 100)
139 return FT_Err_Invalid_Table
;
141 error
= FT_Load_Glyph(face
, gindex
, FT_LOAD_NO_RECURSE
);
147 /* in FreeType < 2.5.4, FT_Get_SubGlyph_Info always returns an error */
148 /* due to a bug even if the call was successful; */
149 /* for this reason we do the argument checking by ourselves */
150 /* and ignore the returned error code of FT_Get_SubGlyph_Info */
151 if (!glyph
->subglyphs
152 || glyph
->format
!= FT_GLYPH_FORMAT_COMPOSITE
)
155 /* since a call to FT_Load_Glyph changes `glyph', */
156 /* we have to first store the subglyph indices, then do the recursion */
157 subglyph_indices
= (FT_Int
*)malloc(sizeof (FT_Int
) * glyph
->num_subglyphs
);
158 if (!subglyph_indices
)
159 return FT_Err_Out_Of_Memory
;
162 for (i
= 0; i
< glyph
->num_subglyphs
; i
++)
164 (void)FT_Get_SubGlyph_Info(glyph
,
172 if (p_index
>= face
->num_glyphs
173 || (gstyles
[p_index
] & ~TA_DIGIT
) != TA_STYLE_UNASSIGNED
)
176 /* only take subglyphs that are not shifted vertically; */
177 /* otherwise blue zones don't fit */
178 if (p_flags
& ARGS_ARE_XY_VALUES
181 gstyles
[p_index
] = gstyle
;
182 subglyph_indices
[used_subglyphs
++] = p_index
;
187 for (i
= 0; i
< used_subglyphs
; i
++)
189 error
= ta_face_globals_scan_composite(face
,
198 free(subglyph_indices
);
204 /* Compute the style index of each glyph within a given face. */
207 ta_face_globals_compute_style_coverage(TA_FaceGlobals globals
)
210 FT_Face face
= globals
->face
;
211 FT_CharMap old_charmap
= face
->charmap
;
212 FT_Byte
* gstyles
= globals
->glyph_styles
;
215 FT_UInt dflt
= ~0U; /* a non-valid value */
218 /* the value TA_STYLE_UNASSIGNED means `uncovered glyph' */
219 memset(globals
->glyph_styles
, TA_STYLE_UNASSIGNED
, globals
->glyph_count
);
221 error
= FT_Select_Charmap(face
, FT_ENCODING_UNICODE
);
224 /* ignore this error; we simply use the fallback style */
225 /* XXX: Shouldn't we rather disable hinting? */
230 /* scan each style in a Unicode charmap */
231 for (ss
= 0; ta_style_classes
[ss
]; ss
++)
233 TA_StyleClass style_class
= ta_style_classes
[ss
];
234 TA_ScriptClass script_class
= ta_script_classes
[style_class
->script
];
235 TA_Script_UniRange range
;
238 if (script_class
->script_uni_ranges
== NULL
)
241 /* scan all Unicode points in the range and */
242 /* set the corresponding glyph style index */
243 if (style_class
->coverage
== TA_COVERAGE_DEFAULT
)
245 if ((FT_UInt
)style_class
->script
== globals
->font
->default_script
)
248 for (range
= script_class
->script_uni_ranges
; range
->first
!= 0; range
++)
250 FT_ULong charcode
= range
->first
;
254 gindex
= FT_Get_Char_Index(face
, charcode
);
257 && gindex
< (FT_ULong
)globals
->glyph_count
258 && gstyles
[gindex
] == TA_STYLE_UNASSIGNED
)
259 gstyles
[gindex
] = (FT_Byte
)ss
;
263 charcode
= FT_Get_Next_Char(face
, charcode
, &gindex
);
265 if (gindex
== 0 || charcode
> range
->last
)
268 if (gindex
< (FT_ULong
)globals
->glyph_count
269 && gstyles
[gindex
] == TA_STYLE_UNASSIGNED
)
270 gstyles
[gindex
] = (FT_Byte
)ss
;
276 /* get glyphs not directly addressable by cmap */
277 ta_get_coverage(globals
, style_class
, gstyles
);
281 /* handle the default OpenType features of the default script ... */
282 ta_get_coverage(globals
, ta_style_classes
[dflt
], gstyles
);
284 /* ... and the remaining default OpenType features */
285 for (ss
= 0; ta_style_classes
[ss
]; ss
++)
287 TA_StyleClass style_class
= ta_style_classes
[ss
];
290 if (ss
!= dflt
&& style_class
->coverage
== TA_COVERAGE_DEFAULT
)
291 ta_get_coverage(globals
, style_class
, gstyles
);
294 /* mark ASCII digits */
295 for (i
= 0x30; i
<= 0x39; i
++)
297 FT_UInt gindex
= FT_Get_Char_Index(face
, i
);
301 && gindex
< (FT_ULong
)globals
->glyph_count
)
302 gstyles
[gindex
] |= TA_DIGIT
;
305 /* now walk over all glyphs and check for composites: */
306 /* since subglyphs are hinted separately if option `hint-composites' */
307 /* isn't set, we have to tag them with style indices, too */
312 for (nn
= 0; nn
< globals
->glyph_count
; nn
++)
314 if ((gstyles
[nn
] & ~TA_DIGIT
) == TA_STYLE_UNASSIGNED
)
317 error
= ta_face_globals_scan_composite(globals
->face
,
328 /* by default, all uncovered glyphs are set to the fallback style */
329 /* XXX: Shouldn't we disable hinting or do something similar? */
330 if (globals
->font
->fallback_style
!= TA_STYLE_UNASSIGNED
)
335 for (nn
= 0; nn
< globals
->glyph_count
; nn
++)
337 if ((gstyles
[nn
] & ~TA_DIGIT
) == TA_STYLE_UNASSIGNED
)
339 gstyles
[nn
] &= ~TA_STYLE_UNASSIGNED
;
340 gstyles
[nn
] |= globals
->font
->fallback_style
;
347 if (face
->num_faces
> 1)
349 "style coverage (subfont %d, glyf table index %d)\n"
350 "================================================\n"
353 globals
->font
->sfnts
[face
->face_index
].glyf_idx
));
360 for (ss
= 0; ta_style_classes
[ss
]; ss
++)
362 TA_StyleClass style_class
= ta_style_classes
[ss
];
367 TA_LOG_GLOBAL(("%s:\n", ta_style_names
[style_class
->style
]));
369 for (idx
= 0; idx
< globals
->glyph_count
; idx
++)
371 if ((gstyles
[idx
] & ~TA_DIGIT
) == style_class
->style
)
374 TA_LOG_GLOBAL((" "));
376 TA_LOG_GLOBAL((" %d", idx
));
380 TA_LOG_GLOBAL(("\n"));
385 TA_LOG_GLOBAL((" (none)\n"));
387 TA_LOG_GLOBAL(("\n"));
390 #endif /* TA_DEBUG */
392 FT_Set_Charmap(face
, old_charmap
);
398 ta_face_globals_new(FT_Face face
,
399 TA_FaceGlobals
*aglobals
,
403 TA_FaceGlobals globals
;
406 globals
= (TA_FaceGlobals
)calloc(1, sizeof (TA_FaceGlobalsRec
) +
407 face
->num_glyphs
* sizeof (FT_Byte
));
410 error
= FT_Err_Out_Of_Memory
;
414 globals
->face
= face
;
415 globals
->glyph_count
= face
->num_glyphs
;
416 globals
->glyph_styles
= (FT_Byte
*)(globals
+ 1);
417 globals
->font
= font
;
418 globals
->hb_font
= hb_ft_font_create(face
, NULL
);
420 error
= ta_face_globals_compute_style_coverage(globals
);
423 ta_face_globals_free(globals
);
427 globals
->increase_x_height
= TA_PROP_INCREASE_X_HEIGHT_MAX
;
436 ta_face_globals_free(TA_FaceGlobals globals
)
443 for (nn
= 0; nn
< TA_STYLE_MAX
; nn
++)
445 if (globals
->metrics
[nn
])
447 TA_StyleClass style_class
=
448 ta_style_classes
[nn
];
449 TA_WritingSystemClass writing_system_class
=
450 ta_writing_system_classes
[style_class
->writing_system
];
453 if (writing_system_class
->style_metrics_done
)
454 writing_system_class
->style_metrics_done(globals
->metrics
[nn
]);
456 free(globals
->metrics
[nn
]);
457 globals
->metrics
[nn
] = NULL
;
461 hb_font_destroy(globals
->hb_font
);
463 globals
->hb_font
= NULL
;
464 globals
->glyph_count
= 0;
465 globals
->glyph_styles
= NULL
; /* no need to free this one! */
466 globals
->face
= NULL
;
474 ta_face_globals_get_metrics(TA_FaceGlobals globals
,
477 TA_StyleMetrics
*ametrics
)
479 TA_StyleMetrics metrics
= NULL
;
480 TA_Style style
= (TA_Style
)options
;
481 TA_WritingSystemClass writing_system_class
;
482 TA_StyleClass style_class
;
483 FT_Error error
= FT_Err_Ok
;
486 if (gindex
>= (FT_ULong
)globals
->glyph_count
)
488 error
= FT_Err_Invalid_Argument
;
492 /* if we have a forced style (via `options'), use it, */
493 /* otherwise look into `glyph_styles' array */
494 if (style
== TA_STYLE_NONE_DFLT
|| style
+ 1 >= TA_STYLE_MAX
)
495 style
= (TA_Style
)(globals
->glyph_styles
[gindex
]
496 & TA_STYLE_UNASSIGNED
);
499 ta_style_classes
[style
];
500 writing_system_class
=
501 ta_writing_system_classes
[style_class
->writing_system
];
503 metrics
= globals
->metrics
[style
];
506 /* create the global metrics object if necessary */
507 metrics
= (TA_StyleMetrics
)
508 calloc(1, writing_system_class
->style_metrics_size
);
511 error
= FT_Err_Out_Of_Memory
;
515 metrics
->style_class
= style_class
;
516 metrics
->globals
= globals
;
518 if (writing_system_class
->style_metrics_init
)
520 error
= writing_system_class
->style_metrics_init(metrics
,
524 if (writing_system_class
->style_metrics_done
)
525 writing_system_class
->style_metrics_done(metrics
);
533 globals
->metrics
[style
] = metrics
;
544 ta_face_globals_is_digit(TA_FaceGlobals globals
,
547 if (gindex
< (FT_ULong
)globals
->glyph_count
)
548 return (FT_Bool
)(globals
->glyph_styles
[gindex
] & TA_DIGIT
);
553 /* end of taglobal.c */