Run `update-copyright' script.
[ttfautohint.git] / lib / tashaper.c
blobf1fc842899de39ed534647b50a6578a8643170d4
1 /* tashaper.c */
3 /*
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> */
20 #include "tashaper.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 */
43 #undef COVERAGE
44 #define COVERAGE(name, NAME, description, \
45 tag, tag1, tag2, tag3, tag4) \
46 static const hb_tag_t name ## _coverage[] = \
47 { \
48 HB_TAG(tag1, tag2, tag3, tag4), \
49 HB_TAG_NONE \
52 #include "ttfautohint-coverages.h"
55 /* define mapping between coverage tags and TA_Coverage */
56 #undef COVERAGE
57 #define COVERAGE(name, NAME, description, \
58 tag, tag1, tag2, tag3, tag4) \
59 name ## _coverage,
61 static const hb_tag_t* coverages[] =
63 #include "ttfautohint-coverages.h"
65 NULL /* TA_COVERAGE_DEFAULT */
69 /* load HarfBuzz script tags */
70 #undef SCRIPT
71 #define SCRIPT(s, S, d, h, H, ss) h,
73 static const hb_script_t scripts[] =
75 #include "ttfautohint-scripts.h"
79 FT_Error
80 ta_shaper_get_coverage(TA_FaceGlobals globals,
81 TA_StyleClass style_class,
82 FT_UShort* gstyles,
83 FT_UInt* sample_glyph,
84 FT_Bool default_script)
86 hb_face_t* face;
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 */
93 hb_script_t script;
94 const hb_tag_t* coverage_tags;
95 hb_tag_t script_tags[] = { HB_TAG_NONE,
96 HB_TAG_NONE,
97 HB_TAG_NONE,
98 HB_TAG_NONE };
100 hb_codepoint_t idx;
101 #ifdef TA_DEBUG
102 int count;
103 #endif
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,
123 &script_tags[0],
124 &script_tags[1]);
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 */
128 /* default script */
129 if (default_script)
131 if (script_tags[0] == HB_TAG_NONE)
132 script_tags[0] = HB_OT_TAG_DEFAULT_SCRIPT;
133 else
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;
141 else
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)
146 goto Exit;
148 if (script_tags[1] == HB_OT_TAG_DEFAULT_SCRIPT)
149 script_tags[1] = HB_TAG_NONE;
152 hb_ot_layout_collect_lookups(face,
153 HB_OT_TAG_GSUB,
154 script_tags,
155 NULL,
156 coverage_tags,
157 gsub_lookups);
159 if (hb_set_is_empty(gsub_lookups))
160 goto Exit; /* nothing to do */
162 hb_ot_layout_collect_lookups(face,
163 HB_OT_TAG_GPOS,
164 script_tags,
165 NULL,
166 coverage_tags,
167 gpos_lookups);
169 TA_LOG_GLOBAL(("GSUB lookups (style `%s'):\n"
170 " ",
171 ta_style_names[style_class->style]));
173 #ifdef TA_DEBUG
174 count = 0;
175 #endif
177 for (idx = HB_SET_VALUE_INVALID; hb_set_next(gsub_lookups, &idx);)
179 #ifdef TA_DEBUG
180 TA_LOG_GLOBAL((" %d", idx));
181 count++;
182 #endif
184 /* get output coverage of GSUB feature */
185 hb_ot_layout_lookup_collect_glyphs(face,
186 HB_OT_TAG_GSUB,
187 idx,
188 NULL,
189 NULL,
190 NULL,
191 gsub_glyphs);
194 #ifdef TA_DEBUG
195 if (!count)
196 TA_LOG_GLOBAL((" (none)"));
197 TA_LOG_GLOBAL(("\n\n"));
198 #endif
200 TA_LOG_GLOBAL(("GPOS lookups (style `%s'):\n"
201 " ",
202 ta_style_names[style_class->style]));
204 #ifdef TA_DEBUG
205 count = 0;
206 #endif
208 for (idx = HB_SET_VALUE_INVALID; hb_set_next(gpos_lookups, &idx);)
210 #ifdef TA_DEBUG
211 TA_LOG_GLOBAL((" %d", idx));
212 count++;
213 #endif
215 /* get input coverage of GPOS feature */
216 hb_ot_layout_lookup_collect_glyphs(face,
217 HB_OT_TAG_GPOS,
218 idx,
219 NULL,
220 gpos_glyphs,
221 NULL,
222 NULL);
225 #ifdef TA_DEBUG
226 if (!count)
227 TA_LOG_GLOBAL((" (none)"));
228 TA_LOG_GLOBAL(("\n\n"));
229 #endif
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];
241 FT_Bool found = 0;
244 for (; bs->string != TA_BLUE_STRING_MAX; bs++)
246 const char* p = &ta_blue_strings[bs->string];
249 while (*p)
251 hb_codepoint_t ch;
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,
262 &gidx, 1, 1))
264 found = 1;
265 break;
271 if (!found)
273 TA_LOG_GLOBAL((" no blue characters found; style skipped\n"));
274 goto Exit;
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,
302 * and
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
322 * too large.
324 if (style_class->coverage != TA_COVERAGE_DEFAULT)
325 hb_set_subtract(gsub_glyphs, gpos_glyphs);
327 #ifdef TA_DEBUG
328 TA_LOG_GLOBAL((" glyphs without GPOS data (`*' means already assigned)"));
329 count = 0;
330 #endif
332 for (idx = HB_SET_VALUE_INVALID; hb_set_next(gsub_glyphs, &idx);)
334 #ifdef TA_DEBUG
335 if (!(count % 10))
336 TA_LOG_GLOBAL(("\n"
337 " "));
339 TA_LOG_GLOBAL((" %d", idx));
340 count++;
341 #endif
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)
347 continue;
349 if (gstyles[idx] == TA_STYLE_UNASSIGNED)
351 gstyles[idx] = (FT_UShort)style_class->style;
352 if (!*sample_glyph)
353 *sample_glyph = idx;
355 #ifdef TA_DEBUG
356 else
357 TA_LOG_GLOBAL(("*"));
358 #endif
361 #ifdef TA_DEBUG
362 if (!count)
363 TA_LOG_GLOBAL(("\n"
364 " (none)"));
365 TA_LOG_GLOBAL(("\n\n"));
366 #endif
368 Exit:
369 hb_set_destroy(gsub_lookups);
370 hb_set_destroy(gsub_glyphs);
371 hb_set_destroy(gpos_lookups);
372 hb_set_destroy(gpos_glyphs);
374 return FT_Err_Ok;
378 /* construct HarfBuzz features */
379 #undef COVERAGE
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 */
394 #undef COVERAGE
395 #define COVERAGE(name, NAME, description, \
396 tag, tag1, tag2, tag3, tag4) \
397 name ## _feature,
399 static const hb_feature_t* features[] =
401 #include "ttfautohint-coverages.h"
403 NULL /* TA_COVERAGE_DEFAULT */
407 void*
408 ta_shaper_buf_create(FT_Face face)
410 FT_UNUSED(face);
412 return (void*)hb_buffer_create();
416 void
417 ta_shaper_buf_destroy(FT_Face face,
418 void* buf)
420 FT_UNUSED(face);
422 hb_buffer_destroy((hb_buffer_t*)buf);
426 const char*
427 ta_shaper_get_cluster(const char* p,
428 TA_StyleMetrics metrics,
429 void* buf_,
430 unsigned int* count)
432 TA_StyleClass style_class;
433 const hb_feature_t* feature;
434 FT_Int upem;
435 const char* q;
436 int len;
438 hb_buffer_t* buf = (hb_buffer_t*)buf_;
439 hb_font_t* font;
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);
452 while (*p == ' ')
453 p++;
455 /* count bytes up to next space (or end of buffer) */
456 q = p;
457 while (!(*q == ' ' || *q == '\0'))
458 GET_UTF8_CHAR(dummy, q);
459 len = (int)(q - p);
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);
472 if (feature)
474 hb_buffer_t* hb_buf = metrics->globals->hb_buf;
476 unsigned int gcount;
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)
497 unsigned int i;
500 for (i = 0; i < gcount; i++)
501 if (ginfo[i].codepoint != hb_ginfo[i].codepoint)
502 break;
504 if (i == gcount)
506 /* both buffers have identical glyph indices */
507 hb_buffer_clear_contents(buf);
512 *count = hb_buffer_get_length(buf);
514 #ifdef TA_DEBUG
515 if (feature && *count > 1)
516 TA_LOG(("ta_shaper_get_cluster:"
517 " input character mapped to multiple glyphs\n"));
518 #endif
520 return q;
524 FT_ULong
525 ta_shaper_get_elem(TA_StyleMetrics metrics,
526 void* buf_,
527 unsigned int idx,
528 FT_Long* advance,
529 FT_Long* y_offset)
531 hb_buffer_t* buf = (hb_buffer_t*)buf_;
532 hb_glyph_info_t* ginfo;
533 hb_glyph_position_t* gpos;
534 unsigned int gcount;
536 FT_UNUSED(metrics);
539 ginfo = hb_buffer_get_glyph_infos(buf, &gcount);
540 gpos = hb_buffer_get_glyph_positions(buf, &gcount);
542 if (idx >= gcount)
543 return 0;
545 if (advance)
546 *advance = gpos[idx].x_advance;
547 if (y_offset)
548 *y_offset = gpos[idx].y_offset;
550 return ginfo[idx].codepoint;
553 /* end of tashaper.c */