4 * Copyright (C) 2014-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 `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_Bool default_script
)
87 hb_set_t
* gsub_lookups
; /* GSUB lookups for a given script */
88 hb_set_t
* gsub_glyphs
; /* glyphs covered by GSUB lookups */
89 hb_set_t
* gpos_lookups
; /* GPOS lookups for a given script */
90 hb_set_t
* gpos_glyphs
; /* glyphs covered by GPOS lookups */
93 const hb_tag_t
* coverage_tags
;
94 hb_tag_t script_tags
[] = { HB_TAG_NONE
,
105 if (!globals
|| !style_class
|| !gstyles
)
106 return FT_Err_Invalid_Argument
;
108 face
= hb_font_get_face(globals
->hb_font
);
110 gsub_lookups
= hb_set_create();
111 gsub_glyphs
= hb_set_create();
112 gpos_lookups
= hb_set_create();
113 gpos_glyphs
= hb_set_create();
115 coverage_tags
= coverages
[style_class
->coverage
];
116 script
= scripts
[style_class
->script
];
118 /* convert a HarfBuzz script tag into the corresponding OpenType */
119 /* tag or tags -- some Indic scripts like Devanagari have an old */
120 /* and a new set of features */
121 hb_ot_tags_from_script(script
,
125 /* `hb_ot_tags_from_script' usually returns HB_OT_TAG_DEFAULT_SCRIPT */
126 /* as the second tag; we change that to HB_TAG_NONE except for the */
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 /* we use non-standard tags like `khms' for special purposes; */
143 /* HarfBuzz maps them to `DFLT', which we don't want to handle here */
144 if (script_tags
[0] == HB_OT_TAG_DEFAULT_SCRIPT
)
147 if (script_tags
[1] == HB_OT_TAG_DEFAULT_SCRIPT
)
148 script_tags
[1] = HB_TAG_NONE
;
151 hb_ot_layout_collect_lookups(face
,
158 if (hb_set_is_empty(gsub_lookups
))
159 goto Exit
; /* nothing to do */
161 hb_ot_layout_collect_lookups(face
,
168 TA_LOG_GLOBAL(("GSUB lookups (style `%s'):\n"
170 ta_style_names
[style_class
->style
]));
176 for (idx
= HB_SET_VALUE_INVALID
; hb_set_next(gsub_lookups
, &idx
);)
179 TA_LOG_GLOBAL((" %d", idx
));
183 /* get output coverage of GSUB feature */
184 hb_ot_layout_lookup_collect_glyphs(face
,
195 TA_LOG_GLOBAL((" (none)"));
196 TA_LOG_GLOBAL(("\n\n"));
199 TA_LOG_GLOBAL(("GPOS lookups (style `%s'):\n"
201 ta_style_names
[style_class
->style
]));
207 for (idx
= HB_SET_VALUE_INVALID
; hb_set_next(gpos_lookups
, &idx
);)
210 TA_LOG_GLOBAL((" %d", idx
));
214 /* get input coverage of GPOS feature */
215 hb_ot_layout_lookup_collect_glyphs(face
,
226 TA_LOG_GLOBAL((" (none)"));
227 TA_LOG_GLOBAL(("\n\n"));
231 * we now check whether we can construct blue zones, using glyphs
232 * covered by the feature only -- in case there is not a single zone
233 * (this is, not a single character is covered), we skip this coverage
235 if (style_class
->coverage
!= TA_COVERAGE_DEFAULT
)
237 TA_Blue_Stringset bss
= style_class
->blue_stringset
;
238 const TA_Blue_StringRec
* bs
= &ta_blue_stringsets
[bss
];
243 for (; bs
->string
!= TA_BLUE_STRING_MAX
; bs
++)
245 const char* p
= &ta_blue_strings
[bs
->string
];
253 GET_UTF8_CHAR(ch
, p
);
255 for (idx
= HB_SET_VALUE_INVALID
; hb_set_next(gsub_lookups
, &idx
);)
257 hb_codepoint_t gidx
= FT_Get_Char_Index(globals
->face
, ch
);
260 if (hb_ot_layout_lookup_would_substitute(face
, idx
,
272 TA_LOG_GLOBAL((" no blue characters found; style skipped\n"));
278 * Various OpenType features might use the same glyphs at different
279 * vertical positions; for example, superscript and subscript glyphs
280 * could be the same. However, the auto-hinter is completely
281 * agnostic of OpenType features after the feature analysis has been
282 * completed: The engine then simply receives a glyph index and returns a
283 * hinted and usually rendered glyph.
285 * Consider the superscript feature of font `pala.ttf': Some of the
286 * glyphs are `real', this is, they have a zero vertical offset, but
287 * most of them are small caps glyphs shifted up to the superscript
288 * position (this is, the `sups' feature is present in both the GSUB and
289 * GPOS tables). The code for blue zones computation actually uses a
290 * feature's y offset so that the `real' glyphs get correct hints. But
291 * later on it is impossible to decide whether a glyph index belongs to,
292 * say, the small caps or superscript feature.
294 * For this reason, we don't assign a style to a glyph if the current
295 * feature covers the glyph in both the GSUB and the GPOS tables. This
296 * is quite a broad condition, assuming that
298 * (a) glyphs that get used in multiple features are present in a
299 * feature without vertical shift,
303 * (b) a feature's GPOS data really moves the glyph vertically.
305 * Not fulfilling condition (a) makes a font larger; it would also
306 * reduce the number of glyphs that could be addressed directly without
307 * using OpenType features, so this assumption is rather strong.
309 * Condition (b) is much weaker, and there might be glyphs which get
310 * missed. However, the OpenType features we are going to handle are
311 * primarily located in GSUB, and HarfBuzz doesn't provide an API to
312 * directly get the necessary information from the GPOS table. A
313 * possible solution might be to directly parse the GPOS table to find
314 * out whether a glyph gets shifted vertically, but this is something I
315 * would like to avoid if not really necessary.
317 * Note that we don't follow this logic for the default coverage.
318 * Complex scripts like Devanagari have mandatory GPOS features to
319 * position many glyph elements, using mark-to-base or mark-to-ligature
320 * tables; the number of glyphs missed due to condition (b) would be far
323 if (style_class
->coverage
!= TA_COVERAGE_DEFAULT
)
324 hb_set_subtract(gsub_glyphs
, gpos_glyphs
);
327 TA_LOG_GLOBAL((" glyphs without GPOS data (`*' means already assigned)"));
331 for (idx
= HB_SET_VALUE_INVALID
; hb_set_next(gsub_glyphs
, &idx
);)
338 TA_LOG_GLOBAL((" %d", idx
));
342 /* glyph indices returned by `hb_ot_layout_lookup_collect_glyphs' */
343 /* can be arbitrary: some fonts use fake indices for processing */
344 /* internal to GSUB or GPOS, which is fully valid */
345 if (idx
>= (hb_codepoint_t
)globals
->glyph_count
)
348 if (gstyles
[idx
] == TA_STYLE_UNASSIGNED
)
349 gstyles
[idx
] = (FT_UShort
)style_class
->style
;
352 TA_LOG_GLOBAL(("*"));
360 TA_LOG_GLOBAL(("\n\n"));
364 hb_set_destroy(gsub_lookups
);
365 hb_set_destroy(gsub_glyphs
);
366 hb_set_destroy(gpos_lookups
);
367 hb_set_destroy(gpos_glyphs
);
373 /* construct HarfBuzz features */
375 #define COVERAGE(name, NAME, description, \
376 tag, tag1, tag2, tag3, tag4) \
377 static const hb_feature_t name ## _feature[] = \
380 HB_TAG(tag1, tag2, tag3, tag4), \
381 1, 0, (unsigned int)-1 \
385 #include "ttfautohint-coverages.h"
388 /* define mapping between HarfBuzz features and TA_Coverage */
390 #define COVERAGE(name, NAME, description, \
391 tag, tag1, tag2, tag3, tag4) \
394 static const hb_feature_t
* features
[] =
396 #include "ttfautohint-coverages.h"
398 NULL
/* TA_COVERAGE_DEFAULT */
403 ta_shaper_buf_create(FT_Face face
)
407 return (void*)hb_buffer_create();
412 ta_shaper_buf_destroy(FT_Face face
,
417 hb_buffer_destroy((hb_buffer_t
*)buf
);
422 ta_shaper_get_cluster(const char* p
,
423 TA_StyleMetrics metrics
,
427 TA_StyleClass style_class
;
428 const hb_feature_t
* feature
;
433 hb_buffer_t
* buf
= (hb_buffer_t
*)buf_
;
435 hb_codepoint_t dummy
;
438 upem
= (FT_Int
)metrics
->globals
->face
->units_per_EM
;
439 style_class
= metrics
->style_class
;
440 feature
= features
[style_class
->coverage
];
442 font
= metrics
->globals
->hb_font
;
444 /* we shape at a size of units per EM; this means font units */
445 hb_font_set_scale(font
, upem
, upem
);
450 /* count bytes up to next space (or end of buffer) */
452 while (!(*q
== ' ' || *q
== '\0'))
453 GET_UTF8_CHAR(dummy
, q
);
456 /* feed character(s) to the HarfBuzz buffer */
457 hb_buffer_clear_contents(buf
);
458 hb_buffer_add_utf8(buf
, p
, len
, 0, len
);
460 /* we let HarfBuzz guess the script and writing direction */
461 hb_buffer_guess_segment_properties(buf
);
463 /* shape buffer, which means conversion from character codes to */
464 /* glyph indices, possibly applying a feature */
465 hb_shape(font
, buf
, feature
, feature
? 1 : 0);
469 hb_buffer_t
* hb_buf
= metrics
->globals
->hb_buf
;
472 hb_glyph_info_t
* ginfo
;
474 unsigned int hb_gcount
;
475 hb_glyph_info_t
* hb_ginfo
;
478 /* we have to check whether applying a feature does actually change */
479 /* glyph indices; otherwise the affected glyph or glyphs aren't */
480 /* available at all in the feature */
482 hb_buffer_clear_contents(hb_buf
);
483 hb_buffer_add_utf8(hb_buf
, p
, len
, 0, len
);
484 hb_buffer_guess_segment_properties(hb_buf
);
485 hb_shape(font
, hb_buf
, NULL
, 0);
487 ginfo
= hb_buffer_get_glyph_infos(buf
, &gcount
);
488 hb_ginfo
= hb_buffer_get_glyph_infos(hb_buf
, &hb_gcount
);
490 if (gcount
== hb_gcount
)
495 for (i
= 0; i
< gcount
; i
++)
496 if (ginfo
[i
].codepoint
!= hb_ginfo
[i
].codepoint
)
501 /* both buffers have identical glyph indices */
502 hb_buffer_clear_contents(buf
);
507 *count
= hb_buffer_get_length(buf
);
510 if (feature
&& *count
> 1)
511 TA_LOG(("ta_shaper_get_cluster:"
512 " input character mapped to multiple glyphs\n"));
520 ta_shaper_get_elem(TA_StyleMetrics metrics
,
526 hb_buffer_t
* buf
= (hb_buffer_t
*)buf_
;
527 hb_glyph_info_t
* ginfo
;
528 hb_glyph_position_t
* gpos
;
534 ginfo
= hb_buffer_get_glyph_infos(buf
, &gcount
);
535 gpos
= hb_buffer_get_glyph_positions(buf
, &gcount
);
541 *advance
= gpos
[idx
].x_advance
;
543 *y_offset
= gpos
[idx
].y_offset
;
545 return ginfo
[idx
].codepoint
;
548 /* end of tashaper.c */