4 * Copyright (C) 2014-2018 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> */
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 tag, tag1, tag2, tag3, tag4) \
46 static const hb_tag_t name ## _coverage[] = \
48 HB_TAG(tag1, tag2, tag3, tag4), \
52 #include "ttfautohint-coverages.h"
55 /* define mapping between coverage tags and TA_Coverage */
57 #define COVERAGE(name, NAME, description, \
58 tag, tag1, tag2, tag3, tag4) \
61 static const hb_tag_t
* coverages
[] =
63 #include "ttfautohint-coverages.h"
65 NULL
/* TA_COVERAGE_DEFAULT */
69 /* load HarfBuzz script tags */
71 #define SCRIPT(s, S, d, h, H, ss) h,
73 static const hb_script_t scripts
[] =
75 #include "ttfautohint-scripts.h"
80 ta_shaper_get_coverage(TA_FaceGlobals globals
,
81 TA_StyleClass style_class
,
83 FT_UInt
* sample_glyph
,
84 FT_Bool default_script
)
88 hb_set_t
* gsub_lookups
; /* GSUB lookups for a given script */
89 hb_set_t
* gsub_glyphs
; /* glyphs covered by GSUB lookups */
90 hb_set_t
* gpos_lookups
; /* GPOS lookups for a given script */
91 hb_set_t
* gpos_glyphs
; /* glyphs covered by GPOS lookups */
94 const hb_tag_t
* coverage_tags
;
95 hb_tag_t script_tags
[] = { HB_TAG_NONE
,
106 if (!globals
|| !style_class
|| !gstyles
)
107 return FT_Err_Invalid_Argument
;
109 face
= hb_font_get_face(globals
->hb_font
);
111 gsub_lookups
= hb_set_create();
112 gsub_glyphs
= hb_set_create();
113 gpos_lookups
= hb_set_create();
114 gpos_glyphs
= hb_set_create();
116 coverage_tags
= coverages
[style_class
->coverage
];
117 script
= scripts
[style_class
->script
];
119 /* convert a HarfBuzz script tag into the corresponding OpenType */
120 /* tag or tags -- some Indic scripts like Devanagari have an old */
121 /* and a new set of features */
122 hb_ot_tags_from_script(script
,
126 /* `hb_ot_tags_from_script' usually returns HB_OT_TAG_DEFAULT_SCRIPT */
127 /* as the second tag; we change that to HB_TAG_NONE except for the */
131 if (script_tags
[0] == HB_TAG_NONE
)
132 script_tags
[0] = HB_OT_TAG_DEFAULT_SCRIPT
;
135 if (script_tags
[1] == HB_TAG_NONE
)
136 script_tags
[1] = HB_OT_TAG_DEFAULT_SCRIPT
;
137 else if (script_tags
[1] != HB_OT_TAG_DEFAULT_SCRIPT
)
138 script_tags
[2] = HB_OT_TAG_DEFAULT_SCRIPT
;
143 /* we use non-standard tags like `khms' for special purposes; */
144 /* HarfBuzz maps them to `DFLT', which we don't want to handle here */
145 if (script_tags
[0] == HB_OT_TAG_DEFAULT_SCRIPT
)
148 if (script_tags
[1] == HB_OT_TAG_DEFAULT_SCRIPT
)
149 script_tags
[1] = HB_TAG_NONE
;
152 hb_ot_layout_collect_lookups(face
,
159 if (hb_set_is_empty(gsub_lookups
))
160 goto Exit
; /* nothing to do */
162 hb_ot_layout_collect_lookups(face
,
169 TA_LOG_GLOBAL(("GSUB lookups (style `%s'):\n"
171 ta_style_names
[style_class
->style
]));
177 for (idx
= HB_SET_VALUE_INVALID
; hb_set_next(gsub_lookups
, &idx
);)
180 TA_LOG_GLOBAL((" %d", idx
));
184 /* get output coverage of GSUB feature */
185 hb_ot_layout_lookup_collect_glyphs(face
,
196 TA_LOG_GLOBAL((" (none)"));
197 TA_LOG_GLOBAL(("\n\n"));
200 TA_LOG_GLOBAL(("GPOS lookups (style `%s'):\n"
202 ta_style_names
[style_class
->style
]));
208 for (idx
= HB_SET_VALUE_INVALID
; hb_set_next(gpos_lookups
, &idx
);)
211 TA_LOG_GLOBAL((" %d", idx
));
215 /* get input coverage of GPOS feature */
216 hb_ot_layout_lookup_collect_glyphs(face
,
227 TA_LOG_GLOBAL((" (none)"));
228 TA_LOG_GLOBAL(("\n\n"));
232 * we now check whether we can construct blue zones, using glyphs
233 * covered by the feature only -- in case there is not a single zone
234 * (this is, not a single character is covered), we skip this coverage
236 if (style_class
->coverage
!= TA_COVERAGE_DEFAULT
)
238 TA_Blue_Stringset bss
= style_class
->blue_stringset
;
239 const TA_Blue_StringRec
* bs
= &ta_blue_stringsets
[bss
];
244 for (; bs
->string
!= TA_BLUE_STRING_MAX
; bs
++)
246 const char* p
= &ta_blue_strings
[bs
->string
];
254 GET_UTF8_CHAR(ch
, p
);
256 for (idx
= HB_SET_VALUE_INVALID
; hb_set_next(gsub_lookups
, &idx
);)
258 hb_codepoint_t gidx
= FT_Get_Char_Index(globals
->face
, ch
);
261 if (hb_ot_layout_lookup_would_substitute(face
, idx
,
273 TA_LOG_GLOBAL((" no blue characters found; style skipped\n"));
279 * Various OpenType features might use the same glyphs at different
280 * vertical positions; for example, superscript and subscript glyphs
281 * could be the same. However, the auto-hinter is completely
282 * agnostic of OpenType features after the feature analysis has been
283 * completed: The engine then simply receives a glyph index and returns a
284 * hinted and usually rendered glyph.
286 * Consider the superscript feature of font `pala.ttf': Some of the
287 * glyphs are `real', this is, they have a zero vertical offset, but
288 * most of them are small caps glyphs shifted up to the superscript
289 * position (this is, the `sups' feature is present in both the GSUB and
290 * GPOS tables). The code for blue zones computation actually uses a
291 * feature's y offset so that the `real' glyphs get correct hints. But
292 * later on it is impossible to decide whether a glyph index belongs to,
293 * say, the small caps or superscript feature.
295 * For this reason, we don't assign a style to a glyph if the current
296 * feature covers the glyph in both the GSUB and the GPOS tables. This
297 * is quite a broad condition, assuming that
299 * (a) glyphs that get used in multiple features are present in a
300 * feature without vertical shift,
304 * (b) a feature's GPOS data really moves the glyph vertically.
306 * Not fulfilling condition (a) makes a font larger; it would also
307 * reduce the number of glyphs that could be addressed directly without
308 * using OpenType features, so this assumption is rather strong.
310 * Condition (b) is much weaker, and there might be glyphs which get
311 * missed. However, the OpenType features we are going to handle are
312 * primarily located in GSUB, and HarfBuzz doesn't provide an API to
313 * directly get the necessary information from the GPOS table. A
314 * possible solution might be to directly parse the GPOS table to find
315 * out whether a glyph gets shifted vertically, but this is something I
316 * would like to avoid if not really necessary.
318 * Note that we don't follow this logic for the default coverage.
319 * Complex scripts like Devanagari have mandatory GPOS features to
320 * position many glyph elements, using mark-to-base or mark-to-ligature
321 * tables; the number of glyphs missed due to condition (b) would be far
324 if (style_class
->coverage
!= TA_COVERAGE_DEFAULT
)
325 hb_set_subtract(gsub_glyphs
, gpos_glyphs
);
328 TA_LOG_GLOBAL((" glyphs without GPOS data (`*' means already assigned)"));
332 for (idx
= HB_SET_VALUE_INVALID
; hb_set_next(gsub_glyphs
, &idx
);)
339 TA_LOG_GLOBAL((" %d", idx
));
343 /* glyph indices returned by `hb_ot_layout_lookup_collect_glyphs' */
344 /* can be arbitrary: some fonts use fake indices for processing */
345 /* internal to GSUB or GPOS, which is fully valid */
346 if (idx
>= (hb_codepoint_t
)globals
->glyph_count
)
349 if (gstyles
[idx
] == TA_STYLE_UNASSIGNED
)
351 gstyles
[idx
] = (FT_UShort
)style_class
->style
;
357 TA_LOG_GLOBAL(("*"));
365 TA_LOG_GLOBAL(("\n\n"));
369 hb_set_destroy(gsub_lookups
);
370 hb_set_destroy(gsub_glyphs
);
371 hb_set_destroy(gpos_lookups
);
372 hb_set_destroy(gpos_glyphs
);
378 /* construct HarfBuzz features */
380 #define COVERAGE(name, NAME, description, \
381 tag, tag1, tag2, tag3, tag4) \
382 static const hb_feature_t name ## _feature[] = \
385 HB_TAG(tag1, tag2, tag3, tag4), \
386 1, 0, (unsigned int)-1 \
390 #include "ttfautohint-coverages.h"
393 /* define mapping between HarfBuzz features and TA_Coverage */
395 #define COVERAGE(name, NAME, description, \
396 tag, tag1, tag2, tag3, tag4) \
399 static const hb_feature_t
* features
[] =
401 #include "ttfautohint-coverages.h"
403 NULL
/* TA_COVERAGE_DEFAULT */
408 ta_shaper_buf_create(FT_Face face
)
412 return (void*)hb_buffer_create();
417 ta_shaper_buf_destroy(FT_Face face
,
422 hb_buffer_destroy((hb_buffer_t
*)buf
);
427 ta_shaper_get_cluster(const char* p
,
428 TA_StyleMetrics metrics
,
432 TA_StyleClass style_class
;
433 const hb_feature_t
* feature
;
438 hb_buffer_t
* buf
= (hb_buffer_t
*)buf_
;
440 hb_codepoint_t dummy
;
443 upem
= (FT_Int
)metrics
->globals
->face
->units_per_EM
;
444 style_class
= metrics
->style_class
;
445 feature
= features
[style_class
->coverage
];
447 font
= metrics
->globals
->hb_font
;
449 /* we shape at a size of units per EM; this means font units */
450 hb_font_set_scale(font
, upem
, upem
);
455 /* count bytes up to next space (or end of buffer) */
457 while (!(*q
== ' ' || *q
== '\0'))
458 GET_UTF8_CHAR(dummy
, q
);
461 /* feed character(s) to the HarfBuzz buffer */
462 hb_buffer_clear_contents(buf
);
463 hb_buffer_add_utf8(buf
, p
, len
, 0, len
);
465 /* we let HarfBuzz guess the script and writing direction */
466 hb_buffer_guess_segment_properties(buf
);
468 /* shape buffer, which means conversion from character codes to */
469 /* glyph indices, possibly applying a feature */
470 hb_shape(font
, buf
, feature
, feature
? 1 : 0);
474 hb_buffer_t
* hb_buf
= metrics
->globals
->hb_buf
;
477 hb_glyph_info_t
* ginfo
;
479 unsigned int hb_gcount
;
480 hb_glyph_info_t
* hb_ginfo
;
483 /* we have to check whether applying a feature does actually change */
484 /* glyph indices; otherwise the affected glyph or glyphs aren't */
485 /* available at all in the feature */
487 hb_buffer_clear_contents(hb_buf
);
488 hb_buffer_add_utf8(hb_buf
, p
, len
, 0, len
);
489 hb_buffer_guess_segment_properties(hb_buf
);
490 hb_shape(font
, hb_buf
, NULL
, 0);
492 ginfo
= hb_buffer_get_glyph_infos(buf
, &gcount
);
493 hb_ginfo
= hb_buffer_get_glyph_infos(hb_buf
, &hb_gcount
);
495 if (gcount
== hb_gcount
)
500 for (i
= 0; i
< gcount
; i
++)
501 if (ginfo
[i
].codepoint
!= hb_ginfo
[i
].codepoint
)
506 /* both buffers have identical glyph indices */
507 hb_buffer_clear_contents(buf
);
512 *count
= hb_buffer_get_length(buf
);
515 if (feature
&& *count
> 1)
516 TA_LOG(("ta_shaper_get_cluster:"
517 " input character mapped to multiple glyphs\n"));
525 ta_shaper_get_elem(TA_StyleMetrics metrics
,
531 hb_buffer_t
* buf
= (hb_buffer_t
*)buf_
;
532 hb_glyph_info_t
* ginfo
;
533 hb_glyph_position_t
* gpos
;
539 ginfo
= hb_buffer_get_glyph_infos(buf
, &gcount
);
540 gpos
= hb_buffer_get_glyph_positions(buf
, &gcount
);
546 *advance
= gpos
[idx
].x_advance
;
548 *y_offset
= gpos
[idx
].y_offset
;
550 return ginfo
[idx
].codepoint
;
553 /* end of tashaper.c */