17 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
18 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * DAMAGE.
20 *
21 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
22 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
23 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
24 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
25 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26 *
27 * Red Hat Author(s): Behdad Esfahbod
28 * Google Author(s): Behdad Esfahbod
29 */
30
31 #include "hb-open-type-private.hh"
32 #include "hb-ot-layout-private.hh"
33
34 #include "hb-ot-layout-gdef-table.hh"
35 #include "hb-ot-layout-gsub-table.hh"
36 #include "hb-ot-layout-gpos-table.hh"
37 #include "hb-ot-layout-jstf-table.hh"
38
39 #include "hb-ot-map-private.hh"
40
41 #include <stdlib.h>
42 #include <string.h>
43
44
45 HB_SHAPER_DATA_ENSURE_DECLARE(ot, face)
46
47 hb_ot_layout_t *
48 _hb_ot_layout_create (hb_face_t *face)
49 {
50 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
51 if (unlikely (!layout))
52 return NULL;
53
54 layout->gdef_blob = OT::Sanitizer<OT::GDEF>::sanitize (face->reference_table (HB_OT_TAG_GDEF));
55 layout->gdef = OT::Sanitizer<OT::GDEF>::lock_instance (layout->gdef_blob);
56
57 layout->gsub_blob = OT::Sanitizer<OT::GSUB>::sanitize (face->reference_table (HB_OT_TAG_GSUB));
58 layout->gsub = OT::Sanitizer<OT::GSUB>::lock_instance (layout->gsub_blob);
59
60 layout->gpos_blob = OT::Sanitizer<OT::GPOS>::sanitize (face->reference_table (HB_OT_TAG_GPOS));
61 layout->gpos = OT::Sanitizer<OT::GPOS>::lock_instance (layout->gpos_blob);
62
63 /* The MATH table is rarely used, so only try and load it in _get_math. */
64 layout->math_blob = NULL;
65 layout->math = NULL;
66
67 {
68 /*
69 * The ugly business of blacklisting individual fonts' tables happen here!
70 * See this thread for why we finally had to bend in and do this:
71 * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
72 */
73 unsigned int gdef_len = hb_blob_get_length (layout->gdef_blob);
74 unsigned int gsub_len = hb_blob_get_length (layout->gsub_blob);
75 unsigned int gpos_len = hb_blob_get_length (layout->gpos_blob);
76 if (0
77 /* sha1sum:c5ee92f0bca4bfb7d06c4d03e8cf9f9cf75d2e8a Windows 7? timesi.ttf */
78 || (442 == gdef_len && 42038 == gpos_len && 2874 == gsub_len)
79 /* sha1sum:37fc8c16a0894ab7b749e35579856c73c840867b Windows 7? timesbi.ttf */
80 || (430 == gdef_len && 40662 == gpos_len && 2874 == gsub_len)
81 /* sha1sum:19fc45110ea6cd3cdd0a5faca256a3797a069a80 Windows 7 timesi.ttf */
82 || (442 == gdef_len && 39116 == gpos_len && 2874 == gsub_len)
83 /* sha1sum:6d2d3c9ed5b7de87bc84eae0df95ee5232ecde26 Windows 7 timesbi.ttf */
84 || (430 == gdef_len && 39374 == gpos_len && 2874 == gsub_len)
85 /* sha1sum:8583225a8b49667c077b3525333f84af08c6bcd8 OS X 10.11.3 Times New Roman Italic.ttf */
89 )
90 {
91 /* In certain versions of Times New Roman Italic and Bold Italic,
92 * ASCII double quotation mark U+0022, mapped to glyph 5, has wrong
93 * glyph class 3 (mark) in GDEF. Nuke the GDEF to avoid zero-width
94 * double-quote. See:
95 * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
96 */
97 if (3 == layout->gdef->get_glyph_class (5))
98 layout->gdef = &OT::Null(OT::GDEF);
99 }
100 else if (0
101 /* sha1sum:96eda93f7d33e79962451c6c39a6b51ee893ce8c tahoma.ttf from Windows 8 */
102 || (898 == gdef_len && 46470 == gpos_len && 12554 == gsub_len)
103 /* sha1sum:20928dc06014e0cd120b6fc942d0c3b1a46ac2bc tahomabd.ttf from Windows 8 */
104 || (910 == gdef_len && 47732 == gpos_len && 12566 == gsub_len)
105 /* sha1sum:4f95b7e4878f60fa3a39ca269618dfde9721a79e tahoma.ttf from Windows 8.1 */
106 || (928 == gdef_len && 59332 == gpos_len && 23298 == gsub_len)
107 /* sha1sum:6d400781948517c3c0441ba42acb309584b73033 tahomabd.ttf from Windows 8.1 */
108 || (940 == gdef_len && 60732 == gpos_len && 23310 == gsub_len)
109 /* sha1sum:e55fa2dfe957a9f7ec26be516a0e30b0c925f846 tahoma.ttf from Windows 10 */
110 || (994 == gdef_len && 60336 == gpos_len && 24474 == gsub_len)
111 /* sha1sum:7199385abb4c2cc81c83a151a7599b6368e92343 tahomabd.ttf from Windows 10 */
112 || (1006 == gdef_len && 61740 == gpos_len && 24470 == gsub_len)
113 /* sha1sum:b0d36cf5a2fbe746a3dd277bffc6756a820807a7 Tahoma.ttf from Mac OS X 10.9 */
114 || (832 == gdef_len && 47162 == gpos_len && 7324 == gsub_len)
115 /* sha1sum:12fc4538e84d461771b30c18b5eb6bd434e30fba Tahoma Bold.ttf from Mac OS X 10.9 */
116 || (844 == gdef_len && 45474 == gpos_len && 7302 == gsub_len)
117 /* sha1sum:73da7f025b238a3f737aa1fde22577a6370f77b0 himalaya.ttf from Windows 8 */
118 || (192 == gdef_len && 7254 == gpos_len && 12638 == gsub_len)
119 /* sha1sum:6e80fd1c0b059bbee49272401583160dc1e6a427 himalaya.ttf from Windows 8.1 */
120 || (192 == gdef_len && 7254 == gpos_len && 12690 == gsub_len)
121 /* 8d9267aea9cd2c852ecfb9f12a6e834bfaeafe44 cantarell-fonts-0.0.21/otf/Cantarell-Regular.otf */
122 /* 983988ff7b47439ab79aeaf9a45bd4a2c5b9d371 cantarell-fonts-0.0.21/otf/Cantarell-Oblique.otf */
123 || (188 == gdef_len && 3852 == gpos_len && 248 == gsub_len)
124 /* 2c0c90c6f6087ffbfea76589c93113a9cbb0e75f cantarell-fonts-0.0.21/otf/Cantarell-Bold.otf */
125 /* 55461f5b853c6da88069ffcdf7f4dd3f8d7e3e6b cantarell-fonts-0.0.21/otf/Cantarell-Bold-Oblique.otf */
126 || (188 == gdef_len && 3426 == gpos_len && 264 == gsub_len)
127 /* 6c93b63b64e8b2c93f5e824e78caca555dc887c7 padauk-2.80/Padauk-book.ttf */
128 || (1046 == gdef_len && 17112 == gpos_len && 71788 == gsub_len)
129 /* d89b1664058359b8ec82e35d3531931125991fb9 padauk-2.80/Padauk-bookbold.ttf */
130 || (1058 == gdef_len && 17514 == gpos_len && 71794 == gsub_len)
131 /* 824cfd193aaf6234b2b4dc0cf3c6ef576c0d00ef padauk-3.0/Padauk-book.ttf */
132 || (1330 == gdef_len && 57938 == gpos_len && 109904 == gsub_len)
133 /* 91fcc10cf15e012d27571e075b3b4dfe31754a8a padauk-3.0/Padauk-bookbold.ttf */
134 || (1330 == gdef_len && 58972 == gpos_len && 109904 == gsub_len)
135 )
136 {
137 /* Many versions of Tahoma have bad GDEF tables that incorrectly classify some spacing marks
138 * such as certain IPA symbols as glyph class 3. So do older versions of Microsoft Himalaya,
139 * and the version of Cantarell shipped by Ubuntu 16.04.
140 * Nuke the GDEF tables of these fonts to avoid unwanted width-zeroing.
141 * See https://bugzilla.mozilla.org/show_bug.cgi?id=1279925
142 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279693
143 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279875
144 */
145 layout->gdef = &OT::Null(OT::GDEF);
146 }
147 }
148
149 layout->gsub_lookup_count = layout->gsub->get_lookup_count ();
150 layout->gpos_lookup_count = layout->gpos->get_lookup_count ();
151
152 layout->gsub_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gsub->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
153 layout->gpos_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gpos->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
154
155 if (unlikely ((layout->gsub_lookup_count && !layout->gsub_accels) ||
156 (layout->gpos_lookup_count && !layout->gpos_accels)))
157 {
158 _hb_ot_layout_destroy (layout);
159 return NULL;
160 }
161
162 for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
163 layout->gsub_accels[i].init (layout->gsub->get_lookup (i));
164 for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
165 layout->gpos_accels[i].init (layout->gpos->get_lookup (i));
166
167 return layout;
168 }
169
170 void
171 _hb_ot_layout_destroy (hb_ot_layout_t *layout)
172 {
173 for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
174 layout->gsub_accels[i].fini ();
175 for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
176 layout->gpos_accels[i].fini ();
177
178 free (layout->gsub_accels);
179 free (layout->gpos_accels);
180
181 hb_blob_destroy (layout->gdef_blob);
182 hb_blob_destroy (layout->gsub_blob);
183 hb_blob_destroy (layout->gpos_blob);
184 hb_blob_destroy (layout->math_blob);
185
186 free (layout);
187 }
188
189 static inline const OT::GDEF&
190 _get_gdef (hb_face_t *face)
191 {
192 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GDEF);
193 return *hb_ot_layout_from_face (face)->gdef;
194 }
195 static inline const OT::GSUB&
196 _get_gsub (hb_face_t *face)
197 {
198 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GSUB);
199 return *hb_ot_layout_from_face (face)->gsub;
200 }
201 static inline const OT::GPOS&
202 _get_gpos (hb_face_t *face)
203 {
204 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GPOS);
239 {
240 return _get_gdef (face).get_glyphs_in_class (klass, glyphs);
241 }
242
243 unsigned int
244 hb_ot_layout_get_attach_points (hb_face_t *face,
245 hb_codepoint_t glyph,
246 unsigned int start_offset,
247 unsigned int *point_count /* IN/OUT */,
248 unsigned int *point_array /* OUT */)
249 {
250 return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
251 }
252
253 unsigned int
254 hb_ot_layout_get_ligature_carets (hb_font_t *font,
255 hb_direction_t direction,
256 hb_codepoint_t glyph,
257 unsigned int start_offset,
258 unsigned int *caret_count /* IN/OUT */,
259 int *caret_array /* OUT */)
260 {
261 return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
262 }
263
264
265 /*
266 * GSUB/GPOS
267 */
268
269 static const OT::GSUBGPOS&
270 get_gsubgpos_table (hb_face_t *face,
271 hb_tag_t table_tag)
272 {
273 switch (table_tag) {
274 case HB_OT_TAG_GSUB: return _get_gsub (face);
275 case HB_OT_TAG_GPOS: return _get_gpos (face);
276 default: return OT::Null(OT::GSUBGPOS);
277 }
278 }
279
281 unsigned int
282 hb_ot_layout_table_get_script_tags (hb_face_t *face,
283 hb_tag_t table_tag,
284 unsigned int start_offset,
285 unsigned int *script_count /* IN/OUT */,
286 hb_tag_t *script_tags /* OUT */)
287 {
288 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
289
290 return g.get_script_tags (start_offset, script_count, script_tags);
291 }
292
293 #define HB_OT_TAG_LATIN_SCRIPT HB_TAG ('l', 'a', 't', 'n')
294
295 hb_bool_t
296 hb_ot_layout_table_find_script (hb_face_t *face,
297 hb_tag_t table_tag,
298 hb_tag_t script_tag,
299 unsigned int *script_index)
300 {
301 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
302 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
303
304 if (g.find_script_index (script_tag, script_index))
305 return true;
306
307 /* try finding 'DFLT' */
308 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
309 return false;
310
311 /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
312 * including many versions of DejaVu Sans Mono! */
313 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
314 return false;
315
316 /* try with 'latn'; some old fonts put their features there even though
317 they're really trying to support Thai, for example :( */
318 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index))
319 return false;
320
321 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
322 return false;
323 }
324
325 hb_bool_t
326 hb_ot_layout_table_choose_script (hb_face_t *face,
327 hb_tag_t table_tag,
328 const hb_tag_t *script_tags,
329 unsigned int *script_index,
330 hb_tag_t *chosen_script)
331 {
332 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
333 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
334
335 while (*script_tags)
336 {
337 if (g.find_script_index (*script_tags, script_index)) {
338 if (chosen_script)
339 *chosen_script = *script_tags;
340 return true;
341 }
342 script_tags++;
343 }
344
345 /* try finding 'DFLT' */
346 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
347 if (chosen_script)
348 *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
349 return false;
350 }
351
352 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
371 }
372
373 unsigned int
374 hb_ot_layout_table_get_feature_tags (hb_face_t *face,
375 hb_tag_t table_tag,
376 unsigned int start_offset,
377 unsigned int *feature_count /* IN/OUT */,
378 hb_tag_t *feature_tags /* OUT */)
379 {
380 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
381
382 return g.get_feature_tags (start_offset, feature_count, feature_tags);
383 }
384
385 hb_bool_t
386 hb_ot_layout_table_find_feature (hb_face_t *face,
387 hb_tag_t table_tag,
388 hb_tag_t feature_tag,
389 unsigned int *feature_index)
390 {
391 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
392 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
393
394 unsigned int num_features = g.get_feature_count ();
395 for (unsigned int i = 0; i < num_features; i++)
396 {
397 if (feature_tag == g.get_feature_tag (i)) {
398 if (feature_index) *feature_index = i;
399 return true;
400 }
401 }
402
403 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
404 return false;
405 }
406
407
408 unsigned int
409 hb_ot_layout_script_get_language_tags (hb_face_t *face,
410 hb_tag_t table_tag,
411 unsigned int script_index,
412 unsigned int start_offset,
413 unsigned int *language_count /* IN/OUT */,
414 hb_tag_t *language_tags /* OUT */)
415 {
416 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
417
418 return s.get_lang_sys_tags (start_offset, language_count, language_tags);
419 }
420
421 hb_bool_t
422 hb_ot_layout_script_find_language (hb_face_t *face,
423 hb_tag_t table_tag,
424 unsigned int script_index,
425 hb_tag_t language_tag,
426 unsigned int *language_index)
427 {
428 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
429 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
430
431 if (s.find_lang_sys_index (language_tag, language_index))
432 return true;
433
434 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
435 if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
436 return false;
437
438 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
439 return false;
440 }
441
442 hb_bool_t
443 hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
444 hb_tag_t table_tag,
445 unsigned int script_index,
446 unsigned int language_index,
447 unsigned int *feature_index)
448 {
449 return hb_ot_layout_language_get_required_feature (face,
450 table_tag,
451 script_index,
452 language_index,
453 feature_index,
454 NULL);
455 }
456
457 /**
458 * hb_ot_layout_language_get_required_feature:
459 *
460 * Since: 0.9.30
461 **/
462 hb_bool_t
463 hb_ot_layout_language_get_required_feature (hb_face_t *face,
464 hb_tag_t table_tag,
465 unsigned int script_index,
466 unsigned int language_index,
467 unsigned int *feature_index,
468 hb_tag_t *feature_tag)
469 {
470 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
471 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
472
473 unsigned int index = l.get_required_feature_index ();
474 if (feature_index) *feature_index = index;
487 unsigned int *feature_indexes /* OUT */)
488 {
489 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
490 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
491
492 return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
493 }
494
495 unsigned int
496 hb_ot_layout_language_get_feature_tags (hb_face_t *face,
497 hb_tag_t table_tag,
498 unsigned int script_index,
499 unsigned int language_index,
500 unsigned int start_offset,
501 unsigned int *feature_count /* IN/OUT */,
502 hb_tag_t *feature_tags /* OUT */)
503 {
504 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
505 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
506
507 ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
508 unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
509
510 if (feature_tags) {
511 unsigned int count = *feature_count;
512 for (unsigned int i = 0; i < count; i++)
513 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
514 }
515
516 return ret;
517 }
518
519
520 hb_bool_t
521 hb_ot_layout_language_find_feature (hb_face_t *face,
522 hb_tag_t table_tag,
523 unsigned int script_index,
524 unsigned int language_index,
525 hb_tag_t feature_tag,
526 unsigned int *feature_index)
527 {
528 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
529 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
530 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
531
532 unsigned int num_features = l.get_feature_count ();
533 for (unsigned int i = 0; i < num_features; i++) {
534 unsigned int f_index = l.get_feature_index (i);
535
536 if (feature_tag == g.get_feature_tag (f_index)) {
537 if (feature_index) *feature_index = f_index;
538 return true;
539 }
540 }
541
542 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
543 return false;
544 }
545
546 /**
547 * hb_ot_layout_feature_get_lookups:
548 *
612 offset += len;
613 } while (len == ARRAY_LENGTH (lookup_indices));
614 }
615
616 static void
617 _hb_ot_layout_collect_lookups_features (hb_face_t *face,
618 hb_tag_t table_tag,
619 unsigned int script_index,
620 unsigned int language_index,
621 const hb_tag_t *features,
622 hb_set_t *lookup_indexes /* OUT */)
623 {
624 if (!features)
625 {
626 unsigned int required_feature_index;
627 if (hb_ot_layout_language_get_required_feature (face,
628 table_tag,
629 script_index,
630 language_index,
631 &required_feature_index,
632 NULL))
633 _hb_ot_layout_collect_lookups_lookups (face,
634 table_tag,
635 required_feature_index,
636 lookup_indexes);
637
638 /* All features */
639 unsigned int feature_indices[32];
640 unsigned int offset, len;
641
642 offset = 0;
643 do {
644 len = ARRAY_LENGTH (feature_indices);
645 hb_ot_layout_language_get_feature_indexes (face,
646 table_tag,
647 script_index,
648 language_index,
649 offset, &len,
650 feature_indices);
651
652 for (unsigned int i = 0; i < len; i++)
681 _hb_ot_layout_collect_lookups_languages (hb_face_t *face,
682 hb_tag_t table_tag,
683 unsigned int script_index,
684 const hb_tag_t *languages,
685 const hb_tag_t *features,
686 hb_set_t *lookup_indexes /* OUT */)
687 {
688 _hb_ot_layout_collect_lookups_features (face,
689 table_tag,
690 script_index,
691 HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX,
692 features,
693 lookup_indexes);
694
695 if (!languages)
696 {
697 /* All languages */
698 unsigned int count = hb_ot_layout_script_get_language_tags (face,
699 table_tag,
700 script_index,
701 0, NULL, NULL);
702 for (unsigned int language_index = 0; language_index < count; language_index++)
703 _hb_ot_layout_collect_lookups_features (face,
704 table_tag,
705 script_index,
706 language_index,
707 features,
708 lookup_indexes);
709 }
710 else
711 {
712 for (; *languages; languages++)
713 {
714 unsigned int language_index;
715 if (hb_ot_layout_script_find_language (face,
716 table_tag,
717 script_index,
718 *languages,
719 &language_index))
720 _hb_ot_layout_collect_lookups_features (face,
721 table_tag,
728 }
729
730 /**
731 * hb_ot_layout_collect_lookups:
732 *
733 * Since: 0.9.8
734 **/
735 void
736 hb_ot_layout_collect_lookups (hb_face_t *face,
737 hb_tag_t table_tag,
738 const hb_tag_t *scripts,
739 const hb_tag_t *languages,
740 const hb_tag_t *features,
741 hb_set_t *lookup_indexes /* OUT */)
742 {
743 if (!scripts)
744 {
745 /* All scripts */
746 unsigned int count = hb_ot_layout_table_get_script_tags (face,
747 table_tag,
748 0, NULL, NULL);
749 for (unsigned int script_index = 0; script_index < count; script_index++)
750 _hb_ot_layout_collect_lookups_languages (face,
751 table_tag,
752 script_index,
753 languages,
754 features,
755 lookup_indexes);
756 }
757 else
758 {
759 for (; *scripts; scripts++)
760 {
761 unsigned int script_index;
762 if (hb_ot_layout_table_find_script (face,
763 table_tag,
764 *scripts,
765 &script_index))
766 _hb_ot_layout_collect_lookups_languages (face,
767 table_tag,
768 script_index,
769 languages,
770 features,
771 lookup_indexes);
772 }
773 }
774 }
775
776 /**
777 * hb_ot_layout_lookup_collect_glyphs:
778 *
779 * Since: 0.9.7
780 **/
781 void
782 hb_ot_layout_lookup_collect_glyphs (hb_face_t *face,
783 hb_tag_t table_tag,
784 unsigned int lookup_index,
785 hb_set_t *glyphs_before, /* OUT. May be NULL */
786 hb_set_t *glyphs_input, /* OUT. May be NULL */
787 hb_set_t *glyphs_after, /* OUT. May be NULL */
788 hb_set_t *glyphs_output /* OUT. May be NULL */)
789 {
790 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return;
791
792 OT::hb_collect_glyphs_context_t c (face,
793 glyphs_before,
794 glyphs_input,
795 glyphs_after,
796 glyphs_output);
797
798 switch (table_tag)
799 {
800 case HB_OT_TAG_GSUB:
801 {
802 const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
803 l.collect_glyphs (&c);
804 return;
805 }
806 case HB_OT_TAG_GPOS:
807 {
808 const OT::PosLookup& l = hb_ot_layout_from_face (face)->gpos->get_lookup (lookup_index);
819 hb_ot_layout_table_find_feature_variations (hb_face_t *face,
820 hb_tag_t table_tag,
821 const int *coords,
822 unsigned int num_coords,
823 unsigned int *variations_index /* out */)
824 {
825 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
826
827 return g.find_variations_index (coords, num_coords, variations_index);
828 }
829
830 unsigned int
831 hb_ot_layout_feature_with_variations_get_lookups (hb_face_t *face,
832 hb_tag_t table_tag,
833 unsigned int feature_index,
834 unsigned int variations_index,
835 unsigned int start_offset,
836 unsigned int *lookup_count /* IN/OUT */,
837 unsigned int *lookup_indexes /* OUT */)
838 {
839 ASSERT_STATIC (OT::FeatureVariations::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_VARIATIONS_INDEX);
840 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
841
842 const OT::Feature &f = g.get_feature_variation (feature_index, variations_index);
843
844 return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
845 }
846
847
848 /*
849 * OT::GSUB
850 */
851
852 hb_bool_t
853 hb_ot_layout_has_substitution (hb_face_t *face)
854 {
855 return &_get_gsub (face) != &OT::Null(OT::GSUB);
856 }
857
858 /**
859 * hb_ot_layout_lookup_would_substitute:
927
928 void
929 hb_ot_layout_position_finish_advances (hb_font_t *font, hb_buffer_t *buffer)
930 {
931 OT::GPOS::position_finish_advances (font, buffer);
932 }
933
934 void
935 hb_ot_layout_position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
936 {
937 OT::GPOS::position_finish_offsets (font, buffer);
938 }
939
940 /**
941 * hb_ot_layout_get_size_params:
942 *
943 * Since: 0.9.10
944 **/
945 hb_bool_t
946 hb_ot_layout_get_size_params (hb_face_t *face,
947 unsigned int *design_size, /* OUT. May be NULL */
948 unsigned int *subfamily_id, /* OUT. May be NULL */
949 unsigned int *subfamily_name_id, /* OUT. May be NULL */
950 unsigned int *range_start, /* OUT. May be NULL */
951 unsigned int *range_end /* OUT. May be NULL */)
952 {
953 const OT::GPOS &gpos = _get_gpos (face);
954 const hb_tag_t tag = HB_TAG ('s','i','z','e');
955
956 unsigned int num_features = gpos.get_feature_count ();
957 for (unsigned int i = 0; i < num_features; i++)
958 {
959 if (tag == gpos.get_feature_tag (i))
960 {
961 const OT::Feature &f = gpos.get_feature (i);
962 const OT::FeatureParamsSize ¶ms = f.get_feature_params ().get_size_params (tag);
963
964 if (params.designSize)
965 {
966 #define PARAM(a, A) if (a) *a = params.A
967 PARAM (design_size, designSize);
968 PARAM (subfamily_id, subfamilyID);
969 PARAM (subfamily_name_id, subfamilyNameID);
970 PARAM (range_start, rangeStart);
971 PARAM (range_end, rangeEnd);
1179 template <typename Proxy>
1180 inline void hb_ot_map_t::apply (const Proxy &proxy,
1181 const hb_ot_shape_plan_t *plan,
1182 hb_font_t *font,
1183 hb_buffer_t *buffer) const
1184 {
1185 const unsigned int table_index = proxy.table_index;
1186 unsigned int i = 0;
1187 OT::hb_apply_context_t c (table_index, font, buffer);
1188 c.set_recurse_func (Proxy::Lookup::apply_recurse_func);
1189
1190 for (unsigned int stage_index = 0; stage_index < stages[table_index].len; stage_index++) {
1191 const stage_map_t *stage = &stages[table_index][stage_index];
1192 for (; i < stage->last_lookup; i++)
1193 {
1194 unsigned int lookup_index = lookups[table_index][i].index;
1195 if (!buffer->message (font, "start lookup %d", lookup_index)) continue;
1196 c.set_lookup_index (lookup_index);
1197 c.set_lookup_mask (lookups[table_index][i].mask);
1198 c.set_auto_zwj (lookups[table_index][i].auto_zwj);
1199 apply_string<Proxy> (&c,
1200 proxy.table.get_lookup (lookup_index),
1201 proxy.accels[lookup_index]);
1202 (void) buffer->message (font, "end lookup %d", lookup_index);
1203 }
1204
1205 if (stage->pause_func)
1206 {
1207 buffer->clear_output ();
1208 stage->pause_func (plan, font, buffer);
1209 }
1210 }
1211 }
1212
1213 void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1214 {
1215 GSUBProxy proxy (font->face);
1216 apply (proxy, plan, font, buffer);
1217 }
1218
|
17 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
18 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * DAMAGE.
20 *
21 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
22 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
23 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
24 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
25 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26 *
27 * Red Hat Author(s): Behdad Esfahbod
28 * Google Author(s): Behdad Esfahbod
29 */
30
31 #include "hb-open-type-private.hh"
32 #include "hb-ot-layout-private.hh"
33
34 #include "hb-ot-layout-gdef-table.hh"
35 #include "hb-ot-layout-gsub-table.hh"
36 #include "hb-ot-layout-gpos-table.hh"
37 #include "hb-ot-layout-jstf-table.hh" // Just so we compile it; unused otherwise.
38 #include "hb-ot-name-table.hh" // Just so we compile it; unused otherwise.
39
40 #include "hb-ot-map-private.hh"
41
42
43 const void * const OT::_hb_NullPool[HB_NULL_POOL_SIZE / sizeof (void *)] = {};
44
45
46 hb_ot_layout_t *
47 _hb_ot_layout_create (hb_face_t *face)
48 {
49 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
50 if (unlikely (!layout))
51 return nullptr;
52
53 layout->gdef_blob = OT::Sanitizer<OT::GDEF>::sanitize (face->reference_table (HB_OT_TAG_GDEF));
54 layout->gdef = OT::Sanitizer<OT::GDEF>::lock_instance (layout->gdef_blob);
55
56 layout->gsub_blob = OT::Sanitizer<OT::GSUB>::sanitize (face->reference_table (HB_OT_TAG_GSUB));
57 layout->gsub = OT::Sanitizer<OT::GSUB>::lock_instance (layout->gsub_blob);
58
59 layout->gpos_blob = OT::Sanitizer<OT::GPOS>::sanitize (face->reference_table (HB_OT_TAG_GPOS));
60 layout->gpos = OT::Sanitizer<OT::GPOS>::lock_instance (layout->gpos_blob);
61
62 layout->math.init (face);
63 layout->fvar.init (face);
64 layout->avar.init (face);
65
66 {
67 /*
68 * The ugly business of blacklisting individual fonts' tables happen here!
69 * See this thread for why we finally had to bend in and do this:
70 * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
71 */
72 unsigned int gdef_len = hb_blob_get_length (layout->gdef_blob);
73 unsigned int gsub_len = hb_blob_get_length (layout->gsub_blob);
74 unsigned int gpos_len = hb_blob_get_length (layout->gpos_blob);
75 if (0
76 /* sha1sum:c5ee92f0bca4bfb7d06c4d03e8cf9f9cf75d2e8a Windows 7? timesi.ttf */
77 || (442 == gdef_len && 42038 == gpos_len && 2874 == gsub_len)
78 /* sha1sum:37fc8c16a0894ab7b749e35579856c73c840867b Windows 7? timesbi.ttf */
79 || (430 == gdef_len && 40662 == gpos_len && 2874 == gsub_len)
80 /* sha1sum:19fc45110ea6cd3cdd0a5faca256a3797a069a80 Windows 7 timesi.ttf */
81 || (442 == gdef_len && 39116 == gpos_len && 2874 == gsub_len)
82 /* sha1sum:6d2d3c9ed5b7de87bc84eae0df95ee5232ecde26 Windows 7 timesbi.ttf */
83 || (430 == gdef_len && 39374 == gpos_len && 2874 == gsub_len)
84 /* sha1sum:8583225a8b49667c077b3525333f84af08c6bcd8 OS X 10.11.3 Times New Roman Italic.ttf */
88 )
89 {
90 /* In certain versions of Times New Roman Italic and Bold Italic,
91 * ASCII double quotation mark U+0022, mapped to glyph 5, has wrong
92 * glyph class 3 (mark) in GDEF. Nuke the GDEF to avoid zero-width
93 * double-quote. See:
94 * https://lists.freedesktop.org/archives/harfbuzz/2016-February/005489.html
95 */
96 if (3 == layout->gdef->get_glyph_class (5))
97 layout->gdef = &OT::Null(OT::GDEF);
98 }
99 else if (0
100 /* sha1sum:96eda93f7d33e79962451c6c39a6b51ee893ce8c tahoma.ttf from Windows 8 */
101 || (898 == gdef_len && 46470 == gpos_len && 12554 == gsub_len)
102 /* sha1sum:20928dc06014e0cd120b6fc942d0c3b1a46ac2bc tahomabd.ttf from Windows 8 */
103 || (910 == gdef_len && 47732 == gpos_len && 12566 == gsub_len)
104 /* sha1sum:4f95b7e4878f60fa3a39ca269618dfde9721a79e tahoma.ttf from Windows 8.1 */
105 || (928 == gdef_len && 59332 == gpos_len && 23298 == gsub_len)
106 /* sha1sum:6d400781948517c3c0441ba42acb309584b73033 tahomabd.ttf from Windows 8.1 */
107 || (940 == gdef_len && 60732 == gpos_len && 23310 == gsub_len)
108 /* tahoma.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
109 || (964 == gdef_len && 60072 == gpos_len && 23836 == gsub_len)
110 /* tahomabd.ttf v6.04 from Windows 8.1 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
111 || (976 == gdef_len && 61456 == gpos_len && 23832 == gsub_len)
112 /* sha1sum:e55fa2dfe957a9f7ec26be516a0e30b0c925f846 tahoma.ttf from Windows 10 */
113 || (994 == gdef_len && 60336 == gpos_len && 24474 == gsub_len)
114 /* sha1sum:7199385abb4c2cc81c83a151a7599b6368e92343 tahomabd.ttf from Windows 10 */
115 || (1006 == gdef_len && 61740 == gpos_len && 24470 == gsub_len)
116 /* tahoma.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
117 || (1006 == gdef_len && 61346 == gpos_len && 24576 == gsub_len)
118 /* tahomabd.ttf v6.91 from Windows 10 x64, see https://bugzilla.mozilla.org/show_bug.cgi?id=1279925 */
119 || (1018 == gdef_len && 62828 == gpos_len && 24572 == gsub_len)
120 /* sha1sum:b9c84d820c49850d3d27ec498be93955b82772b5 tahoma.ttf from Windows 10 AU */
121 || (1006 == gdef_len && 61352 == gpos_len && 24576 == gsub_len)
122 /* sha1sum:2bdfaab28174bdadd2f3d4200a30a7ae31db79d2 tahomabd.ttf from Windows 10 AU */
123 || (1018 == gdef_len && 62834 == gpos_len && 24572 == gsub_len)
124 /* sha1sum:b0d36cf5a2fbe746a3dd277bffc6756a820807a7 Tahoma.ttf from Mac OS X 10.9 */
125 || (832 == gdef_len && 47162 == gpos_len && 7324 == gsub_len)
126 /* sha1sum:12fc4538e84d461771b30c18b5eb6bd434e30fba Tahoma Bold.ttf from Mac OS X 10.9 */
127 || (844 == gdef_len && 45474 == gpos_len && 7302 == gsub_len)
128 /* sha1sum:eb8afadd28e9cf963e886b23a30b44ab4fd83acc himalaya.ttf from Windows 7 */
129 || (180 == gdef_len && 7254 == gpos_len && 13054 == gsub_len)
130 /* sha1sum:73da7f025b238a3f737aa1fde22577a6370f77b0 himalaya.ttf from Windows 8 */
131 || (192 == gdef_len && 7254 == gpos_len && 12638 == gsub_len)
132 /* sha1sum:6e80fd1c0b059bbee49272401583160dc1e6a427 himalaya.ttf from Windows 8.1 */
133 || (192 == gdef_len && 7254 == gpos_len && 12690 == gsub_len)
134 /* 8d9267aea9cd2c852ecfb9f12a6e834bfaeafe44 cantarell-fonts-0.0.21/otf/Cantarell-Regular.otf */
135 /* 983988ff7b47439ab79aeaf9a45bd4a2c5b9d371 cantarell-fonts-0.0.21/otf/Cantarell-Oblique.otf */
136 || (188 == gdef_len && 3852 == gpos_len && 248 == gsub_len)
137 /* 2c0c90c6f6087ffbfea76589c93113a9cbb0e75f cantarell-fonts-0.0.21/otf/Cantarell-Bold.otf */
138 /* 55461f5b853c6da88069ffcdf7f4dd3f8d7e3e6b cantarell-fonts-0.0.21/otf/Cantarell-Bold-Oblique.otf */
139 || (188 == gdef_len && 3426 == gpos_len && 264 == gsub_len)
140 /* d125afa82a77a6475ac0e74e7c207914af84b37a padauk-2.80/Padauk.ttf RHEL 7.2 */
141 || (1058 == gdef_len && 11818 == gpos_len && 47032 == gsub_len)
142 /* 0f7b80437227b90a577cc078c0216160ae61b031 padauk-2.80/Padauk-Bold.ttf RHEL 7.2*/
143 || (1046 == gdef_len && 12600 == gpos_len && 47030 == gsub_len)
144 /* d3dde9aa0a6b7f8f6a89ef1002e9aaa11b882290 padauk-2.80/Padauk.ttf Ubuntu 16.04 */
145 || (1058 == gdef_len && 16770 == gpos_len && 71796 == gsub_len)
146 /* 5f3c98ccccae8a953be2d122c1b3a77fd805093f padauk-2.80/Padauk-Bold.ttf Ubuntu 16.04 */
147 || (1046 == gdef_len && 17862 == gpos_len && 71790 == gsub_len)
148 /* 6c93b63b64e8b2c93f5e824e78caca555dc887c7 padauk-2.80/Padauk-book.ttf */
149 || (1046 == gdef_len && 17112 == gpos_len && 71788 == gsub_len)
150 /* d89b1664058359b8ec82e35d3531931125991fb9 padauk-2.80/Padauk-bookbold.ttf */
151 || (1058 == gdef_len && 17514 == gpos_len && 71794 == gsub_len)
152 /* 824cfd193aaf6234b2b4dc0cf3c6ef576c0d00ef padauk-3.0/Padauk-book.ttf */
153 || (1330 == gdef_len && 57938 == gpos_len && 109904 == gsub_len)
154 /* 91fcc10cf15e012d27571e075b3b4dfe31754a8a padauk-3.0/Padauk-bookbold.ttf */
155 || (1330 == gdef_len && 58972 == gpos_len && 109904 == gsub_len)
156 /* sha1sum: c26e41d567ed821bed997e937bc0c41435689e85 Padauk.ttf
157 * "Padauk Regular" "Version 2.5", see https://crbug.com/681813 */
158 || (1004 == gdef_len && 14836 == gpos_len && 59092 == gsub_len)
159 )
160 {
161 /* Many versions of Tahoma have bad GDEF tables that incorrectly classify some spacing marks
162 * such as certain IPA symbols as glyph class 3. So do older versions of Microsoft Himalaya,
163 * and the version of Cantarell shipped by Ubuntu 16.04.
164 * Nuke the GDEF tables of these fonts to avoid unwanted width-zeroing.
165 * See https://bugzilla.mozilla.org/show_bug.cgi?id=1279925
166 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279693
167 * https://bugzilla.mozilla.org/show_bug.cgi?id=1279875
168 */
169 layout->gdef = &OT::Null(OT::GDEF);
170 }
171 }
172
173 layout->gsub_lookup_count = layout->gsub->get_lookup_count ();
174 layout->gpos_lookup_count = layout->gpos->get_lookup_count ();
175
176 layout->gsub_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gsub->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
177 layout->gpos_accels = (hb_ot_layout_lookup_accelerator_t *) calloc (layout->gpos->get_lookup_count (), sizeof (hb_ot_layout_lookup_accelerator_t));
178
179 if (unlikely ((layout->gsub_lookup_count && !layout->gsub_accels) ||
180 (layout->gpos_lookup_count && !layout->gpos_accels)))
181 {
182 _hb_ot_layout_destroy (layout);
183 return nullptr;
184 }
185
186 for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
187 layout->gsub_accels[i].init (layout->gsub->get_lookup (i));
188 for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
189 layout->gpos_accels[i].init (layout->gpos->get_lookup (i));
190
191 return layout;
192 }
193
194 void
195 _hb_ot_layout_destroy (hb_ot_layout_t *layout)
196 {
197 for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
198 layout->gsub_accels[i].fini ();
199 for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
200 layout->gpos_accels[i].fini ();
201
202 free (layout->gsub_accels);
203 free (layout->gpos_accels);
204
205 hb_blob_destroy (layout->gdef_blob);
206 hb_blob_destroy (layout->gsub_blob);
207 hb_blob_destroy (layout->gpos_blob);
208
209 layout->math.fini ();
210 layout->fvar.fini ();
211 layout->avar.fini ();
212
213 free (layout);
214 }
215
216 static inline const OT::GDEF&
217 _get_gdef (hb_face_t *face)
218 {
219 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GDEF);
220 return *hb_ot_layout_from_face (face)->gdef;
221 }
222 static inline const OT::GSUB&
223 _get_gsub (hb_face_t *face)
224 {
225 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GSUB);
226 return *hb_ot_layout_from_face (face)->gsub;
227 }
228 static inline const OT::GPOS&
229 _get_gpos (hb_face_t *face)
230 {
231 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GPOS);
266 {
267 return _get_gdef (face).get_glyphs_in_class (klass, glyphs);
268 }
269
270 unsigned int
271 hb_ot_layout_get_attach_points (hb_face_t *face,
272 hb_codepoint_t glyph,
273 unsigned int start_offset,
274 unsigned int *point_count /* IN/OUT */,
275 unsigned int *point_array /* OUT */)
276 {
277 return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
278 }
279
280 unsigned int
281 hb_ot_layout_get_ligature_carets (hb_font_t *font,
282 hb_direction_t direction,
283 hb_codepoint_t glyph,
284 unsigned int start_offset,
285 unsigned int *caret_count /* IN/OUT */,
286 hb_position_t *caret_array /* OUT */)
287 {
288 return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
289 }
290
291
292 /*
293 * GSUB/GPOS
294 */
295
296 static const OT::GSUBGPOS&
297 get_gsubgpos_table (hb_face_t *face,
298 hb_tag_t table_tag)
299 {
300 switch (table_tag) {
301 case HB_OT_TAG_GSUB: return _get_gsub (face);
302 case HB_OT_TAG_GPOS: return _get_gpos (face);
303 default: return OT::Null(OT::GSUBGPOS);
304 }
305 }
306
308 unsigned int
309 hb_ot_layout_table_get_script_tags (hb_face_t *face,
310 hb_tag_t table_tag,
311 unsigned int start_offset,
312 unsigned int *script_count /* IN/OUT */,
313 hb_tag_t *script_tags /* OUT */)
314 {
315 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
316
317 return g.get_script_tags (start_offset, script_count, script_tags);
318 }
319
320 #define HB_OT_TAG_LATIN_SCRIPT HB_TAG ('l', 'a', 't', 'n')
321
322 hb_bool_t
323 hb_ot_layout_table_find_script (hb_face_t *face,
324 hb_tag_t table_tag,
325 hb_tag_t script_tag,
326 unsigned int *script_index)
327 {
328 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
329 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
330
331 if (g.find_script_index (script_tag, script_index))
332 return true;
333
334 /* try finding 'DFLT' */
335 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
336 return false;
337
338 /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
339 * including many versions of DejaVu Sans Mono! */
340 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
341 return false;
342
343 /* try with 'latn'; some old fonts put their features there even though
344 they're really trying to support Thai, for example :( */
345 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index))
346 return false;
347
348 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
349 return false;
350 }
351
352 hb_bool_t
353 hb_ot_layout_table_choose_script (hb_face_t *face,
354 hb_tag_t table_tag,
355 const hb_tag_t *script_tags,
356 unsigned int *script_index,
357 hb_tag_t *chosen_script)
358 {
359 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX), "");
360 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
361
362 while (*script_tags)
363 {
364 if (g.find_script_index (*script_tags, script_index)) {
365 if (chosen_script)
366 *chosen_script = *script_tags;
367 return true;
368 }
369 script_tags++;
370 }
371
372 /* try finding 'DFLT' */
373 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
374 if (chosen_script)
375 *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
376 return false;
377 }
378
379 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
398 }
399
400 unsigned int
401 hb_ot_layout_table_get_feature_tags (hb_face_t *face,
402 hb_tag_t table_tag,
403 unsigned int start_offset,
404 unsigned int *feature_count /* IN/OUT */,
405 hb_tag_t *feature_tags /* OUT */)
406 {
407 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
408
409 return g.get_feature_tags (start_offset, feature_count, feature_tags);
410 }
411
412 hb_bool_t
413 hb_ot_layout_table_find_feature (hb_face_t *face,
414 hb_tag_t table_tag,
415 hb_tag_t feature_tag,
416 unsigned int *feature_index)
417 {
418 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
419 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
420
421 unsigned int num_features = g.get_feature_count ();
422 for (unsigned int i = 0; i < num_features; i++)
423 {
424 if (feature_tag == g.get_feature_tag (i)) {
425 if (feature_index) *feature_index = i;
426 return true;
427 }
428 }
429
430 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
431 return false;
432 }
433
434
435 unsigned int
436 hb_ot_layout_script_get_language_tags (hb_face_t *face,
437 hb_tag_t table_tag,
438 unsigned int script_index,
439 unsigned int start_offset,
440 unsigned int *language_count /* IN/OUT */,
441 hb_tag_t *language_tags /* OUT */)
442 {
443 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
444
445 return s.get_lang_sys_tags (start_offset, language_count, language_tags);
446 }
447
448 hb_bool_t
449 hb_ot_layout_script_find_language (hb_face_t *face,
450 hb_tag_t table_tag,
451 unsigned int script_index,
452 hb_tag_t language_tag,
453 unsigned int *language_index)
454 {
455 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX), "");
456 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
457
458 if (s.find_lang_sys_index (language_tag, language_index))
459 return true;
460
461 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
462 if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
463 return false;
464
465 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
466 return false;
467 }
468
469 hb_bool_t
470 hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
471 hb_tag_t table_tag,
472 unsigned int script_index,
473 unsigned int language_index,
474 unsigned int *feature_index)
475 {
476 return hb_ot_layout_language_get_required_feature (face,
477 table_tag,
478 script_index,
479 language_index,
480 feature_index,
481 nullptr);
482 }
483
484 /**
485 * hb_ot_layout_language_get_required_feature:
486 *
487 * Since: 0.9.30
488 **/
489 hb_bool_t
490 hb_ot_layout_language_get_required_feature (hb_face_t *face,
491 hb_tag_t table_tag,
492 unsigned int script_index,
493 unsigned int language_index,
494 unsigned int *feature_index,
495 hb_tag_t *feature_tag)
496 {
497 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
498 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
499
500 unsigned int index = l.get_required_feature_index ();
501 if (feature_index) *feature_index = index;
514 unsigned int *feature_indexes /* OUT */)
515 {
516 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
517 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
518
519 return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
520 }
521
522 unsigned int
523 hb_ot_layout_language_get_feature_tags (hb_face_t *face,
524 hb_tag_t table_tag,
525 unsigned int script_index,
526 unsigned int language_index,
527 unsigned int start_offset,
528 unsigned int *feature_count /* IN/OUT */,
529 hb_tag_t *feature_tags /* OUT */)
530 {
531 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
532 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
533
534 static_assert ((sizeof (unsigned int) == sizeof (hb_tag_t)), "");
535 unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
536
537 if (feature_tags) {
538 unsigned int count = *feature_count;
539 for (unsigned int i = 0; i < count; i++)
540 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
541 }
542
543 return ret;
544 }
545
546
547 hb_bool_t
548 hb_ot_layout_language_find_feature (hb_face_t *face,
549 hb_tag_t table_tag,
550 unsigned int script_index,
551 unsigned int language_index,
552 hb_tag_t feature_tag,
553 unsigned int *feature_index)
554 {
555 static_assert ((OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX), "");
556 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
557 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
558
559 unsigned int num_features = l.get_feature_count ();
560 for (unsigned int i = 0; i < num_features; i++) {
561 unsigned int f_index = l.get_feature_index (i);
562
563 if (feature_tag == g.get_feature_tag (f_index)) {
564 if (feature_index) *feature_index = f_index;
565 return true;
566 }
567 }
568
569 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
570 return false;
571 }
572
573 /**
574 * hb_ot_layout_feature_get_lookups:
575 *
639 offset += len;
640 } while (len == ARRAY_LENGTH (lookup_indices));
641 }
642
643 static void
644 _hb_ot_layout_collect_lookups_features (hb_face_t *face,
645 hb_tag_t table_tag,
646 unsigned int script_index,
647 unsigned int language_index,
648 const hb_tag_t *features,
649 hb_set_t *lookup_indexes /* OUT */)
650 {
651 if (!features)
652 {
653 unsigned int required_feature_index;
654 if (hb_ot_layout_language_get_required_feature (face,
655 table_tag,
656 script_index,
657 language_index,
658 &required_feature_index,
659 nullptr))
660 _hb_ot_layout_collect_lookups_lookups (face,
661 table_tag,
662 required_feature_index,
663 lookup_indexes);
664
665 /* All features */
666 unsigned int feature_indices[32];
667 unsigned int offset, len;
668
669 offset = 0;
670 do {
671 len = ARRAY_LENGTH (feature_indices);
672 hb_ot_layout_language_get_feature_indexes (face,
673 table_tag,
674 script_index,
675 language_index,
676 offset, &len,
677 feature_indices);
678
679 for (unsigned int i = 0; i < len; i++)
708 _hb_ot_layout_collect_lookups_languages (hb_face_t *face,
709 hb_tag_t table_tag,
710 unsigned int script_index,
711 const hb_tag_t *languages,
712 const hb_tag_t *features,
713 hb_set_t *lookup_indexes /* OUT */)
714 {
715 _hb_ot_layout_collect_lookups_features (face,
716 table_tag,
717 script_index,
718 HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX,
719 features,
720 lookup_indexes);
721
722 if (!languages)
723 {
724 /* All languages */
725 unsigned int count = hb_ot_layout_script_get_language_tags (face,
726 table_tag,
727 script_index,
728 0, nullptr, nullptr);
729 for (unsigned int language_index = 0; language_index < count; language_index++)
730 _hb_ot_layout_collect_lookups_features (face,
731 table_tag,
732 script_index,
733 language_index,
734 features,
735 lookup_indexes);
736 }
737 else
738 {
739 for (; *languages; languages++)
740 {
741 unsigned int language_index;
742 if (hb_ot_layout_script_find_language (face,
743 table_tag,
744 script_index,
745 *languages,
746 &language_index))
747 _hb_ot_layout_collect_lookups_features (face,
748 table_tag,
755 }
756
757 /**
758 * hb_ot_layout_collect_lookups:
759 *
760 * Since: 0.9.8
761 **/
762 void
763 hb_ot_layout_collect_lookups (hb_face_t *face,
764 hb_tag_t table_tag,
765 const hb_tag_t *scripts,
766 const hb_tag_t *languages,
767 const hb_tag_t *features,
768 hb_set_t *lookup_indexes /* OUT */)
769 {
770 if (!scripts)
771 {
772 /* All scripts */
773 unsigned int count = hb_ot_layout_table_get_script_tags (face,
774 table_tag,
775 0, nullptr, nullptr);
776 for (unsigned int script_index = 0; script_index < count; script_index++)
777 _hb_ot_layout_collect_lookups_languages (face,
778 table_tag,
779 script_index,
780 languages,
781 features,
782 lookup_indexes);
783 }
784 else
785 {
786 for (; *scripts; scripts++)
787 {
788 unsigned int script_index;
789 if (hb_ot_layout_table_find_script (face,
790 table_tag,
791 *scripts,
792 &script_index))
793 _hb_ot_layout_collect_lookups_languages (face,
794 table_tag,
795 script_index,
796 languages,
797 features,
798 lookup_indexes);
799 }
800 }
801 }
802
803 /**
804 * hb_ot_layout_lookup_collect_glyphs:
805 *
806 * Since: 0.9.7
807 **/
808 void
809 hb_ot_layout_lookup_collect_glyphs (hb_face_t *face,
810 hb_tag_t table_tag,
811 unsigned int lookup_index,
812 hb_set_t *glyphs_before, /* OUT. May be nullptr */
813 hb_set_t *glyphs_input, /* OUT. May be nullptr */
814 hb_set_t *glyphs_after, /* OUT. May be nullptr */
815 hb_set_t *glyphs_output /* OUT. May be nullptr */)
816 {
817 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return;
818
819 OT::hb_collect_glyphs_context_t c (face,
820 glyphs_before,
821 glyphs_input,
822 glyphs_after,
823 glyphs_output);
824
825 switch (table_tag)
826 {
827 case HB_OT_TAG_GSUB:
828 {
829 const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
830 l.collect_glyphs (&c);
831 return;
832 }
833 case HB_OT_TAG_GPOS:
834 {
835 const OT::PosLookup& l = hb_ot_layout_from_face (face)->gpos->get_lookup (lookup_index);
846 hb_ot_layout_table_find_feature_variations (hb_face_t *face,
847 hb_tag_t table_tag,
848 const int *coords,
849 unsigned int num_coords,
850 unsigned int *variations_index /* out */)
851 {
852 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
853
854 return g.find_variations_index (coords, num_coords, variations_index);
855 }
856
857 unsigned int
858 hb_ot_layout_feature_with_variations_get_lookups (hb_face_t *face,
859 hb_tag_t table_tag,
860 unsigned int feature_index,
861 unsigned int variations_index,
862 unsigned int start_offset,
863 unsigned int *lookup_count /* IN/OUT */,
864 unsigned int *lookup_indexes /* OUT */)
865 {
866 static_assert ((OT::FeatureVariations::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_VARIATIONS_INDEX), "");
867 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
868
869 const OT::Feature &f = g.get_feature_variation (feature_index, variations_index);
870
871 return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
872 }
873
874
875 /*
876 * OT::GSUB
877 */
878
879 hb_bool_t
880 hb_ot_layout_has_substitution (hb_face_t *face)
881 {
882 return &_get_gsub (face) != &OT::Null(OT::GSUB);
883 }
884
885 /**
886 * hb_ot_layout_lookup_would_substitute:
954
955 void
956 hb_ot_layout_position_finish_advances (hb_font_t *font, hb_buffer_t *buffer)
957 {
958 OT::GPOS::position_finish_advances (font, buffer);
959 }
960
961 void
962 hb_ot_layout_position_finish_offsets (hb_font_t *font, hb_buffer_t *buffer)
963 {
964 OT::GPOS::position_finish_offsets (font, buffer);
965 }
966
967 /**
968 * hb_ot_layout_get_size_params:
969 *
970 * Since: 0.9.10
971 **/
972 hb_bool_t
973 hb_ot_layout_get_size_params (hb_face_t *face,
974 unsigned int *design_size, /* OUT. May be nullptr */
975 unsigned int *subfamily_id, /* OUT. May be nullptr */
976 unsigned int *subfamily_name_id, /* OUT. May be nullptr */
977 unsigned int *range_start, /* OUT. May be nullptr */
978 unsigned int *range_end /* OUT. May be nullptr */)
979 {
980 const OT::GPOS &gpos = _get_gpos (face);
981 const hb_tag_t tag = HB_TAG ('s','i','z','e');
982
983 unsigned int num_features = gpos.get_feature_count ();
984 for (unsigned int i = 0; i < num_features; i++)
985 {
986 if (tag == gpos.get_feature_tag (i))
987 {
988 const OT::Feature &f = gpos.get_feature (i);
989 const OT::FeatureParamsSize ¶ms = f.get_feature_params ().get_size_params (tag);
990
991 if (params.designSize)
992 {
993 #define PARAM(a, A) if (a) *a = params.A
994 PARAM (design_size, designSize);
995 PARAM (subfamily_id, subfamilyID);
996 PARAM (subfamily_name_id, subfamilyNameID);
997 PARAM (range_start, rangeStart);
998 PARAM (range_end, rangeEnd);
1206 template <typename Proxy>
1207 inline void hb_ot_map_t::apply (const Proxy &proxy,
1208 const hb_ot_shape_plan_t *plan,
1209 hb_font_t *font,
1210 hb_buffer_t *buffer) const
1211 {
1212 const unsigned int table_index = proxy.table_index;
1213 unsigned int i = 0;
1214 OT::hb_apply_context_t c (table_index, font, buffer);
1215 c.set_recurse_func (Proxy::Lookup::apply_recurse_func);
1216
1217 for (unsigned int stage_index = 0; stage_index < stages[table_index].len; stage_index++) {
1218 const stage_map_t *stage = &stages[table_index][stage_index];
1219 for (; i < stage->last_lookup; i++)
1220 {
1221 unsigned int lookup_index = lookups[table_index][i].index;
1222 if (!buffer->message (font, "start lookup %d", lookup_index)) continue;
1223 c.set_lookup_index (lookup_index);
1224 c.set_lookup_mask (lookups[table_index][i].mask);
1225 c.set_auto_zwj (lookups[table_index][i].auto_zwj);
1226 c.set_auto_zwnj (lookups[table_index][i].auto_zwnj);
1227 apply_string<Proxy> (&c,
1228 proxy.table.get_lookup (lookup_index),
1229 proxy.accels[lookup_index]);
1230 (void) buffer->message (font, "end lookup %d", lookup_index);
1231 }
1232
1233 if (stage->pause_func)
1234 {
1235 buffer->clear_output ();
1236 stage->pause_func (plan, font, buffer);
1237 }
1238 }
1239 }
1240
1241 void hb_ot_map_t::substitute (const hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const
1242 {
1243 GSUBProxy proxy (font->face);
1244 apply (proxy, plan, font, buffer);
1245 }
1246
|