4 * Copyright (C) 2014-2015 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 `hbshim.c' (2014-Jan-11) from FreeType */
18 /* heavily modified 2014 by Werner Lemberg <wl@gnu.org> */
20 #include "taharfbuzz.h"
24 * We use `sets' (in the HarfBuzz sense, which comes quite near to the
25 * usual mathematical meaning) to manage both lookups and glyph indices.
27 * 1. For each coverage, collect lookup IDs in a set. Note that an
28 * auto-hinter `coverage' is represented by one `feature', and a
29 * feature consists of an arbitrary number of (font specific) `lookup's
30 * that actually do the mapping job. Please check the OpenType
31 * specification for more details on features and lookups.
33 * 2. Create glyph ID sets from the corresponding lookup sets.
35 * 3. The glyph set corresponding to TA_COVERAGE_DEFAULT is computed
36 * with all lookups specific to the OpenType script activated. It
37 * relies on the order of ta_style_classes entries so that
38 * special coverages (like `oldstyle figures') don't get overwritten.
42 /* load coverage tags */
44 #define COVERAGE(name, NAME, description, \
45 tag1, tag2, tag3, tag4) \
46 static const hb_tag_t name ## _coverage[] = \
48 HB_TAG(tag1, tag2, tag3, tag4), \
55 /* define mapping between coverage tags and TA_Coverage */
57 #define COVERAGE(name, NAME, description, \
58 tag1, tag2, tag3, tag4) \
61 static const hb_tag_t
* coverages
[] =
65 NULL
/* TA_COVERAGE_DEFAULT */
69 /* load HarfBuzz script tags */
71 #define SCRIPT(s, S, d, h, sc1, sc2, sc3) h,
73 static const hb_script_t scripts
[] =
75 #include "ttfautohint-scripts.h"
80 ta_get_coverage(TA_FaceGlobals globals
,
81 TA_StyleClass style_class
,
86 hb_set_t
* gsub_lookups
; /* GSUB lookups for a given script */
87 hb_set_t
* gsub_glyphs
; /* glyphs covered by GSUB lookups */
88 hb_set_t
* gpos_lookups
; /* GPOS lookups for a given script */
89 hb_set_t
* gpos_glyphs
; /* glyphs covered by GPOS lookups */
92 const hb_tag_t
* coverage_tags
;
93 hb_tag_t script_tags
[] = { HB_TAG_NONE
,
104 if (!globals
|| !style_class
|| !gstyles
)
105 return FT_Err_Invalid_Argument
;
107 face
= hb_font_get_face(globals
->hb_font
);
109 gsub_lookups
= hb_set_create();
110 gsub_glyphs
= hb_set_create();
111 gpos_lookups
= hb_set_create();
112 gpos_glyphs
= hb_set_create();
114 coverage_tags
= coverages
[style_class
->coverage
];
115 script
= scripts
[style_class
->script
];
117 /* convert a HarfBuzz script tag into the corresponding OpenType */
118 /* tag or tags -- some Indic scripts like Devanagari have an old */
119 /* and a new set of features */
120 hb_ot_tags_from_script(script
,
124 /* `hb_ot_tags_from_script' usually returns HB_OT_TAG_DEFAULT_SCRIPT */
125 /* as the second tag; we change that to HB_TAG_NONE except for the */
127 if (style_class
->script
== globals
->font
->default_script
128 && style_class
->coverage
== TA_COVERAGE_DEFAULT
)
130 if (script_tags
[0] == HB_TAG_NONE
)
131 script_tags
[0] = HB_OT_TAG_DEFAULT_SCRIPT
;
134 if (script_tags
[1] == HB_TAG_NONE
)
135 script_tags
[1] = HB_OT_TAG_DEFAULT_SCRIPT
;
136 else if (script_tags
[1] != HB_OT_TAG_DEFAULT_SCRIPT
)
137 script_tags
[2] = HB_OT_TAG_DEFAULT_SCRIPT
;
142 if (script_tags
[1] == HB_OT_TAG_DEFAULT_SCRIPT
)
143 script_tags
[1] = HB_TAG_NONE
;
146 hb_ot_layout_collect_lookups(face
,
153 if (hb_set_is_empty(gsub_lookups
))
154 goto Exit
; /* nothing to do */
156 hb_ot_layout_collect_lookups(face
,
163 TA_LOG_GLOBAL(("GSUB lookups (style `%s'):\n"
165 ta_style_names
[style_class
->style
]));
171 for (idx
= -1; hb_set_next(gsub_lookups
, &idx
);)
174 TA_LOG_GLOBAL((" %d", idx
));
178 /* get output coverage of GSUB feature */
179 hb_ot_layout_lookup_collect_glyphs(face
,
190 TA_LOG_GLOBAL((" (none)"));
191 TA_LOG_GLOBAL(("\n\n"));
194 TA_LOG_GLOBAL(("GPOS lookups (style `%s'):\n"
196 ta_style_names
[style_class
->style
]));
202 for (idx
= -1; hb_set_next(gpos_lookups
, &idx
);)
205 TA_LOG_GLOBAL((" %d", idx
));
209 /* get input coverage of GPOS feature */
210 hb_ot_layout_lookup_collect_glyphs(face
,
221 TA_LOG_GLOBAL((" (none)"));
222 TA_LOG_GLOBAL(("\n\n"));
226 * we now check whether we can construct blue zones, using glyphs
227 * covered by the feature only -- in case there is not a single zone
228 * (this is, not a single character is covered), we skip this coverage
230 if (style_class
->coverage
!= TA_COVERAGE_DEFAULT
)
232 TA_Blue_Stringset bss
= style_class
->blue_stringset
;
233 const TA_Blue_StringRec
* bs
= &ta_blue_stringsets
[bss
];
238 for (; bs
->string
!= TA_BLUE_STRING_MAX
; bs
++)
240 const char* p
= &ta_blue_strings
[bs
->string
];
248 GET_UTF8_CHAR(ch
, p
);
250 for (idx
= -1; hb_set_next(gsub_lookups
, &idx
);)
252 hb_codepoint_t gidx
= FT_Get_Char_Index(globals
->face
, ch
);
255 if (hb_ot_layout_lookup_would_substitute(face
, idx
,
267 TA_LOG_GLOBAL((" no blue characters found; style skipped\n"));
273 * Various OpenType features might use the same glyphs at different
274 * vertical positions; for example, superscript and subscript glyphs
275 * could be the same. However, the auto-hinter is completely
276 * agnostic of OpenType features after the feature analysis has been
277 * completed: The engine then simply receives a glyph index and returns a
278 * hinted and usually rendered glyph.
280 * Consider the superscript feature of font `pala.ttf': Some of the
281 * glyphs are `real', this is, they have a zero vertical offset, but
282 * most of them are small caps glyphs shifted up to the superscript
283 * position (this is, the `sups' feature is present in both the GSUB and
284 * GPOS tables). The code for blue zones computation actually uses a
285 * feature's y offset so that the `real' glyphs get correct hints. But
286 * later on it is impossible to decide whether a glyph index belongs to,
287 * say, the small caps or superscript feature.
289 * For this reason, we don't assign a style to a glyph if the current
290 * feature covers the glyph in both the GSUB and the GPOS tables. This
291 * is quite a broad condition, assuming that
293 * (a) glyphs that get used in multiple features are present in a
294 * feature without vertical shift,
298 * (b) a feature's GPOS data really moves the glyph vertically.
300 * Not fulfilling condition (a) makes a font larger; it would also
301 * reduce the number of glyphs that could be addressed directly without
302 * using OpenType features, so this assumption is rather strong.
304 * Condition (b) is much weaker, and there might be glyphs which get
305 * missed. However, the OpenType features we are going to handle are
306 * primarily located in GSUB, and HarfBuzz doesn't provide an API to
307 * directly get the necessary information from the GPOS table. A
308 * possible solution might be to directly parse the GPOS table to find
309 * out whether a glyph gets shifted vertically, but this is something I
310 * would like to avoid if not really necessary.
312 * Note that we don't follow this logic for the default coverage.
313 * Complex scripts like Devanagari have mandatory GPOS features to
314 * position many glyph elements, using mark-to-base or mark-to-ligature
315 * tables; the number of glyphs missed due to condition (b) would be far
318 if (style_class
->coverage
!= TA_COVERAGE_DEFAULT
)
319 hb_set_subtract(gsub_glyphs
, gpos_glyphs
);
322 TA_LOG_GLOBAL((" glyphs without GPOS data (`*' means already assigned)"));
326 for (idx
= -1; hb_set_next(gsub_glyphs
, &idx
);)
333 TA_LOG_GLOBAL((" %d", idx
));
337 /* glyph indices returned by `hb_ot_layout_lookup_collect_glyphs' */
338 /* can be arbitrary: some fonts use fake indices for processing */
339 /* internal to GSUB or GPOS, which is fully valid */
340 if (idx
>= (hb_codepoint_t
)globals
->glyph_count
)
343 if (gstyles
[idx
] == TA_STYLE_UNASSIGNED
)
344 gstyles
[idx
] = (FT_Byte
)style_class
->style
;
347 TA_LOG_GLOBAL(("*"));
355 TA_LOG_GLOBAL(("\n\n"));
359 hb_set_destroy(gsub_lookups
);
360 hb_set_destroy(gsub_glyphs
);
361 hb_set_destroy(gpos_lookups
);
362 hb_set_destroy(gpos_glyphs
);
368 /* construct HarfBuzz features */
370 #define COVERAGE(name, NAME, description, \
371 tag1, tag2, tag3, tag4) \
372 static const hb_feature_t name ## _feature[] = \
375 HB_TAG(tag1, tag2, tag3, tag4), \
376 1, 0, (unsigned int)-1 \
383 /* define mapping between HarfBuzz features and TA_Coverage */
385 #define COVERAGE(name, NAME, description, \
386 tag1, tag2, tag3, tag4) \
389 static const hb_feature_t
* features
[] =
393 NULL
/* TA_COVERAGE_DEFAULT */
398 ta_get_char_index(TA_StyleMetrics metrics
,
403 TA_StyleClass style_class
;
405 const hb_feature_t
* feature
;
407 FT_ULong in_idx
, out_idx
;
411 return FT_Err_Invalid_Argument
;
413 in_idx
= FT_Get_Char_Index(metrics
->globals
->face
, charcode
);
415 style_class
= metrics
->style_class
;
417 feature
= features
[style_class
->coverage
];
421 FT_UInt upem
= metrics
->globals
->face
->units_per_EM
;
423 hb_font_t
* font
= metrics
->globals
->hb_font
;
424 hb_buffer_t
* buf
= hb_buffer_create();
426 uint32_t c
= (uint32_t)charcode
;
428 hb_glyph_info_t
* ginfo
;
429 hb_glyph_position_t
* gpos
;
433 /* we shape at a size of units per EM; this means font units */
434 hb_font_set_scale(font
, upem
, upem
);
436 /* XXX: is this sufficient for a single character of any script? */
437 hb_buffer_set_direction(buf
, HB_DIRECTION_LTR
);
438 hb_buffer_set_script(buf
, scripts
[style_class
->script
]);
440 /* we add one character to `buf' ... */
441 hb_buffer_add_utf32(buf
, &c
, 1, 0, 1);
443 /* ... and apply one feature */
444 hb_shape(font
, buf
, feature
, 1);
446 ginfo
= hb_buffer_get_glyph_infos(buf
, &gcount
);
447 gpos
= hb_buffer_get_glyph_positions(buf
, &gcount
);
449 out_idx
= ginfo
[0].codepoint
;
451 /* getting the same index indicates no substitution, */
452 /* which means that the glyph isn't available in the feature */
453 if (in_idx
== out_idx
)
460 *codepoint
= out_idx
;
461 *y_offset
= gpos
[0].y_offset
;
464 hb_buffer_destroy(buf
);
468 TA_LOG(("ta_get_char_index:"
469 " input character mapped to multiple glyphs\n"));
481 /* end of taharfbuzz.c */