Synchronize with FreeType.
[ttfautohint.git] / lib / tashaper.c
blobd28c33b8467127562df88bf76556e1724b8b1714
1 /* tashaper.c */
3 /*
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> */
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_Bool default_script)
85 hb_face_t* face;
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 */
92 hb_script_t script;
93 const hb_tag_t* coverage_tags;
94 hb_tag_t script_tags[] = { HB_TAG_NONE,
95 HB_TAG_NONE,
96 HB_TAG_NONE,
97 HB_TAG_NONE };
99 hb_codepoint_t idx;
100 #ifdef TA_DEBUG
101 int count;
102 #endif
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,
122 &script_tags[0],
123 &script_tags[1]);
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 */
127 /* default script */
128 if (default_script)
130 if (script_tags[0] == HB_TAG_NONE)
131 script_tags[0] = HB_OT_TAG_DEFAULT_SCRIPT;
132 else
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;
140 else
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)
145 goto Exit;
147 if (script_tags[1] == HB_OT_TAG_DEFAULT_SCRIPT)
148 script_tags[1] = HB_TAG_NONE;
151 hb_ot_layout_collect_lookups(face,
152 HB_OT_TAG_GSUB,
153 script_tags,
154 NULL,
155 coverage_tags,
156 gsub_lookups);
158 if (hb_set_is_empty(gsub_lookups))
159 goto Exit; /* nothing to do */
161 hb_ot_layout_collect_lookups(face,
162 HB_OT_TAG_GPOS,
163 script_tags,
164 NULL,
165 coverage_tags,
166 gpos_lookups);
168 TA_LOG_GLOBAL(("GSUB lookups (style `%s'):\n"
169 " ",
170 ta_style_names[style_class->style]));
172 #ifdef TA_DEBUG
173 count = 0;
174 #endif
176 for (idx = HB_SET_VALUE_INVALID; hb_set_next(gsub_lookups, &idx);)
178 #ifdef TA_DEBUG
179 TA_LOG_GLOBAL((" %d", idx));
180 count++;
181 #endif
183 /* get output coverage of GSUB feature */
184 hb_ot_layout_lookup_collect_glyphs(face,
185 HB_OT_TAG_GSUB,
186 idx,
187 NULL,
188 NULL,
189 NULL,
190 gsub_glyphs);
193 #ifdef TA_DEBUG
194 if (!count)
195 TA_LOG_GLOBAL((" (none)"));
196 TA_LOG_GLOBAL(("\n\n"));
197 #endif
199 TA_LOG_GLOBAL(("GPOS lookups (style `%s'):\n"
200 " ",
201 ta_style_names[style_class->style]));
203 #ifdef TA_DEBUG
204 count = 0;
205 #endif
207 for (idx = HB_SET_VALUE_INVALID; hb_set_next(gpos_lookups, &idx);)
209 #ifdef TA_DEBUG
210 TA_LOG_GLOBAL((" %d", idx));
211 count++;
212 #endif
214 /* get input coverage of GPOS feature */
215 hb_ot_layout_lookup_collect_glyphs(face,
216 HB_OT_TAG_GPOS,
217 idx,
218 NULL,
219 gpos_glyphs,
220 NULL,
221 NULL);
224 #ifdef TA_DEBUG
225 if (!count)
226 TA_LOG_GLOBAL((" (none)"));
227 TA_LOG_GLOBAL(("\n\n"));
228 #endif
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];
240 FT_Bool found = 0;
243 for (; bs->string != TA_BLUE_STRING_MAX; bs++)
245 const char* p = &ta_blue_strings[bs->string];
248 while (*p)
250 hb_codepoint_t ch;
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,
261 &gidx, 1, 1))
263 found = 1;
264 break;
270 if (!found)
272 TA_LOG_GLOBAL((" no blue characters found; style skipped\n"));
273 goto Exit;
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,
301 * and
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
321 * too large.
323 if (style_class->coverage != TA_COVERAGE_DEFAULT)
324 hb_set_subtract(gsub_glyphs, gpos_glyphs);
326 #ifdef TA_DEBUG
327 TA_LOG_GLOBAL((" glyphs without GPOS data (`*' means already assigned)"));
328 count = 0;
329 #endif
331 for (idx = HB_SET_VALUE_INVALID; hb_set_next(gsub_glyphs, &idx);)
333 #ifdef TA_DEBUG
334 if (!(count % 10))
335 TA_LOG_GLOBAL(("\n"
336 " "));
338 TA_LOG_GLOBAL((" %d", idx));
339 count++;
340 #endif
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)
346 continue;
348 if (gstyles[idx] == TA_STYLE_UNASSIGNED)
349 gstyles[idx] = (FT_UShort)style_class->style;
350 #ifdef TA_DEBUG
351 else
352 TA_LOG_GLOBAL(("*"));
353 #endif
356 #ifdef TA_DEBUG
357 if (!count)
358 TA_LOG_GLOBAL(("\n"
359 " (none)"));
360 TA_LOG_GLOBAL(("\n\n"));
361 #endif
363 Exit:
364 hb_set_destroy(gsub_lookups);
365 hb_set_destroy(gsub_glyphs);
366 hb_set_destroy(gpos_lookups);
367 hb_set_destroy(gpos_glyphs);
369 return FT_Err_Ok;
373 /* construct HarfBuzz features */
374 #undef COVERAGE
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 */
389 #undef COVERAGE
390 #define COVERAGE(name, NAME, description, \
391 tag, tag1, tag2, tag3, tag4) \
392 name ## _feature,
394 static const hb_feature_t* features[] =
396 #include "ttfautohint-coverages.h"
398 NULL /* TA_COVERAGE_DEFAULT */
402 void*
403 ta_shaper_buf_create(FT_Face face)
405 FT_UNUSED(face);
407 return (void*)hb_buffer_create();
411 void
412 ta_shaper_buf_destroy(FT_Face face,
413 void* buf)
415 FT_UNUSED(face);
417 hb_buffer_destroy((hb_buffer_t*)buf);
421 const char*
422 ta_shaper_get_cluster(const char* p,
423 TA_StyleMetrics metrics,
424 void* buf_,
425 unsigned int* count)
427 TA_StyleClass style_class;
428 const hb_feature_t* feature;
429 FT_Int upem;
430 const char* q;
431 int len;
433 hb_buffer_t* buf = (hb_buffer_t*)buf_;
434 hb_font_t* font;
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);
447 while (*p == ' ')
448 p++;
450 /* count bytes up to next space (or end of buffer) */
451 q = p;
452 while (!(*q == ' ' || *q == '\0'))
453 GET_UTF8_CHAR(dummy, q);
454 len = (int)(q - p);
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);
467 if (feature)
469 hb_buffer_t* hb_buf = metrics->globals->hb_buf;
471 unsigned int gcount;
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)
492 unsigned int i;
495 for (i = 0; i < gcount; i++)
496 if (ginfo[i].codepoint != hb_ginfo[i].codepoint)
497 break;
499 if (i == gcount)
501 /* both buffers have identical glyph indices */
502 hb_buffer_clear_contents(buf);
507 *count = hb_buffer_get_length(buf);
509 #ifdef TA_DEBUG
510 if (feature && *count > 1)
511 TA_LOG(("ta_shaper_get_cluster:"
512 " input character mapped to multiple glyphs\n"));
513 #endif
515 return q;
519 FT_ULong
520 ta_shaper_get_elem(TA_StyleMetrics metrics,
521 void* buf_,
522 unsigned int idx,
523 FT_Long* advance,
524 FT_Long* y_offset)
526 hb_buffer_t* buf = (hb_buffer_t*)buf_;
527 hb_glyph_info_t* ginfo;
528 hb_glyph_position_t* gpos;
529 unsigned int gcount;
531 FT_UNUSED(metrics);
534 ginfo = hb_buffer_get_glyph_infos(buf, &gcount);
535 gpos = hb_buffer_get_glyph_positions(buf, &gcount);
537 if (idx >= gcount)
538 return 0;
540 if (advance)
541 *advance = gpos[idx].x_advance;
542 if (y_offset)
543 *y_offset = gpos[idx].y_offset;
545 return ginfo[idx].codepoint;
548 /* end of tashaper.c */