2 * Copyright © 1998-2004 David Turner and Werner Lemberg
3 * Copyright © 2006 Behdad Esfahbod
4 * Copyright © 2007,2008,2009 Red Hat, Inc.
5 * Copyright © 2012 Google, Inc.
7 * This is part of HarfBuzz, a text shaping library.
9 * Permission is hereby granted, without written agreement and without
10 * license or royalty fees, to use, copy, modify, and distribute this
11 * software and its documentation for any purpose, provided that the
12 * above copyright notice and the following two paragraphs appear in
13 * all copies of this software.
15 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
16 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
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
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.
27 * Red Hat Author(s): Behdad Esfahbod
28 * Google Author(s): Behdad Esfahbod
31 #include "hb-ot-layout-private.hh"
33 #include "hb-ot-layout-gdef-table.hh"
34 #include "hb-ot-layout-gsub-table.hh"
35 #include "hb-ot-layout-gpos-table.hh"
41 HB_SHAPER_DATA_ENSURE_DECLARE(ot, face)
44 _hb_ot_layout_create (hb_face_t *face)
46 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
47 if (unlikely (!layout))
50 layout->gdef_blob = OT::Sanitizer<OT::GDEF>::sanitize (face->reference_table (HB_OT_TAG_GDEF));
51 layout->gdef = OT::Sanitizer<OT::GDEF>::lock_instance (layout->gdef_blob);
53 layout->gsub_blob = OT::Sanitizer<OT::GSUB>::sanitize (face->reference_table (HB_OT_TAG_GSUB));
54 layout->gsub = OT::Sanitizer<OT::GSUB>::lock_instance (layout->gsub_blob);
56 layout->gpos_blob = OT::Sanitizer<OT::GPOS>::sanitize (face->reference_table (HB_OT_TAG_GPOS));
57 layout->gpos = OT::Sanitizer<OT::GPOS>::lock_instance (layout->gpos_blob);
59 layout->gsub_lookup_count = layout->gsub->get_lookup_count ();
60 layout->gpos_lookup_count = layout->gpos->get_lookup_count ();
62 layout->gsub_digests = (hb_set_digest_t *) calloc (layout->gsub->get_lookup_count (), sizeof (hb_set_digest_t));
63 layout->gpos_digests = (hb_set_digest_t *) calloc (layout->gpos->get_lookup_count (), sizeof (hb_set_digest_t));
65 if (unlikely ((layout->gsub_lookup_count && !layout->gsub_digests) ||
66 (layout->gpos_lookup_count && !layout->gpos_digests)))
68 _hb_ot_layout_destroy (layout);
72 for (unsigned int i = 0; i < layout->gsub_lookup_count; i++)
73 layout->gsub->get_lookup (i).add_coverage (&layout->gsub_digests[i]);
74 for (unsigned int i = 0; i < layout->gpos_lookup_count; i++)
75 layout->gpos->get_lookup (i).add_coverage (&layout->gpos_digests[i]);
81 _hb_ot_layout_destroy (hb_ot_layout_t *layout)
83 hb_blob_destroy (layout->gdef_blob);
84 hb_blob_destroy (layout->gsub_blob);
85 hb_blob_destroy (layout->gpos_blob);
87 free (layout->gsub_digests);
88 free (layout->gpos_digests);
93 static inline const OT::GDEF&
94 _get_gdef (hb_face_t *face)
96 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GDEF);
97 return *hb_ot_layout_from_face (face)->gdef;
99 static inline const OT::GSUB&
100 _get_gsub (hb_face_t *face)
102 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GSUB);
103 return *hb_ot_layout_from_face (face)->gsub;
105 static inline const OT::GPOS&
106 _get_gpos (hb_face_t *face)
108 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return OT::Null(OT::GPOS);
109 return *hb_ot_layout_from_face (face)->gpos;
118 hb_ot_layout_has_glyph_classes (hb_face_t *face)
120 return _get_gdef (face).has_glyph_classes ();
123 hb_ot_layout_glyph_class_t
124 hb_ot_layout_get_glyph_class (hb_face_t *face,
125 hb_codepoint_t glyph)
127 return (hb_ot_layout_glyph_class_t) _get_gdef (face).get_glyph_class (glyph);
131 hb_ot_layout_get_glyphs_in_class (hb_face_t *face,
132 hb_ot_layout_glyph_class_t klass,
133 hb_set_t *glyphs /* OUT */)
135 return _get_gdef (face).get_glyphs_in_class (klass, glyphs);
139 hb_ot_layout_get_attach_points (hb_face_t *face,
140 hb_codepoint_t glyph,
141 unsigned int start_offset,
142 unsigned int *point_count /* IN/OUT */,
143 unsigned int *point_array /* OUT */)
145 return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
149 hb_ot_layout_get_ligature_carets (hb_font_t *font,
150 hb_direction_t direction,
151 hb_codepoint_t glyph,
152 unsigned int start_offset,
153 unsigned int *caret_count /* IN/OUT */,
154 int *caret_array /* OUT */)
156 return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
164 static const OT::GSUBGPOS&
165 get_gsubgpos_table (hb_face_t *face,
169 case HB_OT_TAG_GSUB: return _get_gsub (face);
170 case HB_OT_TAG_GPOS: return _get_gpos (face);
171 default: return OT::Null(OT::GSUBGPOS);
177 hb_ot_layout_table_get_script_tags (hb_face_t *face,
179 unsigned int start_offset,
180 unsigned int *script_count /* IN/OUT */,
181 hb_tag_t *script_tags /* OUT */)
183 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
185 return g.get_script_tags (start_offset, script_count, script_tags);
188 #define HB_OT_TAG_LATIN_SCRIPT HB_TAG ('l', 'a', 't', 'n')
191 hb_ot_layout_table_find_script (hb_face_t *face,
194 unsigned int *script_index)
196 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
197 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
199 if (g.find_script_index (script_tag, script_index))
202 /* try finding 'DFLT' */
203 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
206 /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
207 * including many versions of DejaVu Sans Mono! */
208 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
211 /* try with 'latn'; some old fonts put their features there even though
212 they're really trying to support Thai, for example :( */
213 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index))
216 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
221 hb_ot_layout_table_choose_script (hb_face_t *face,
223 const hb_tag_t *script_tags,
224 unsigned int *script_index,
225 hb_tag_t *chosen_script)
227 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
228 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
232 if (g.find_script_index (*script_tags, script_index)) {
234 *chosen_script = *script_tags;
240 /* try finding 'DFLT' */
241 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
243 *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
247 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
248 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) {
250 *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE;
254 /* try with 'latn'; some old fonts put their features there even though
255 they're really trying to support Thai, for example :( */
256 if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) {
258 *chosen_script = HB_OT_TAG_LATIN_SCRIPT;
262 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
264 *chosen_script = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
269 hb_ot_layout_table_get_feature_tags (hb_face_t *face,
271 unsigned int start_offset,
272 unsigned int *feature_count /* IN/OUT */,
273 hb_tag_t *feature_tags /* OUT */)
275 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
277 return g.get_feature_tags (start_offset, feature_count, feature_tags);
282 hb_ot_layout_script_get_language_tags (hb_face_t *face,
284 unsigned int script_index,
285 unsigned int start_offset,
286 unsigned int *language_count /* IN/OUT */,
287 hb_tag_t *language_tags /* OUT */)
289 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
291 return s.get_lang_sys_tags (start_offset, language_count, language_tags);
295 hb_ot_layout_script_find_language (hb_face_t *face,
297 unsigned int script_index,
298 hb_tag_t language_tag,
299 unsigned int *language_index)
301 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
302 const OT::Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
304 if (s.find_lang_sys_index (language_tag, language_index))
307 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
308 if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
311 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
316 hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
318 unsigned int script_index,
319 unsigned int language_index,
320 unsigned int *feature_index)
322 const OT::LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
324 if (feature_index) *feature_index = l.get_required_feature_index ();
326 return l.has_required_feature ();
330 hb_ot_layout_language_get_feature_indexes (hb_face_t *face,
332 unsigned int script_index,
333 unsigned int language_index,
334 unsigned int start_offset,
335 unsigned int *feature_count /* IN/OUT */,
336 unsigned int *feature_indexes /* OUT */)
338 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
339 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
341 return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
345 hb_ot_layout_language_get_feature_tags (hb_face_t *face,
347 unsigned int script_index,
348 unsigned int language_index,
349 unsigned int start_offset,
350 unsigned int *feature_count /* IN/OUT */,
351 hb_tag_t *feature_tags /* OUT */)
353 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
354 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
356 ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
357 unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
360 unsigned int count = *feature_count;
361 for (unsigned int i = 0; i < count; i++)
362 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
370 hb_ot_layout_language_find_feature (hb_face_t *face,
372 unsigned int script_index,
373 unsigned int language_index,
374 hb_tag_t feature_tag,
375 unsigned int *feature_index)
377 ASSERT_STATIC (OT::Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
378 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
379 const OT::LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
381 unsigned int num_features = l.get_feature_count ();
382 for (unsigned int i = 0; i < num_features; i++) {
383 unsigned int f_index = l.get_feature_index (i);
385 if (feature_tag == g.get_feature_tag (f_index)) {
386 if (feature_index) *feature_index = f_index;
391 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
396 hb_ot_layout_feature_get_lookups (hb_face_t *face,
398 unsigned int feature_index,
399 unsigned int start_offset,
400 unsigned int *lookup_count /* IN/OUT */,
401 unsigned int *lookup_indexes /* OUT */)
403 const OT::GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
404 const OT::Feature &f = g.get_feature (feature_index);
406 return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
410 _hb_ot_layout_collect_lookups_lookups (hb_face_t *face,
412 unsigned int feature_index,
413 hb_set_t *lookup_indexes /* OUT */)
415 unsigned int lookup_indices[32];
416 unsigned int offset, len;
420 len = ARRAY_LENGTH (lookup_indices);
421 hb_ot_layout_feature_get_lookups (face,
427 for (unsigned int i = 0; i < len; i++)
428 lookup_indexes->add (lookup_indices[i]);
431 } while (len == ARRAY_LENGTH (lookup_indices));
435 _hb_ot_layout_collect_lookups_features (hb_face_t *face,
437 unsigned int script_index,
438 unsigned int language_index,
439 const hb_tag_t *features,
440 hb_set_t *lookup_indexes /* OUT */)
442 unsigned int required_feature_index;
443 if (hb_ot_layout_language_get_required_feature_index (face,
447 &required_feature_index))
448 _hb_ot_layout_collect_lookups_lookups (face,
450 required_feature_index,
456 unsigned int feature_indices[32];
457 unsigned int offset, len;
461 len = ARRAY_LENGTH (feature_indices);
462 hb_ot_layout_language_get_feature_indexes (face,
469 for (unsigned int i = 0; i < len; i++)
470 _hb_ot_layout_collect_lookups_lookups (face,
476 } while (len == ARRAY_LENGTH (feature_indices));
480 for (; *features; features++)
482 unsigned int feature_index;
483 if (hb_ot_layout_language_find_feature (face,
489 _hb_ot_layout_collect_lookups_lookups (face,
498 _hb_ot_layout_collect_lookups_languages (hb_face_t *face,
500 unsigned int script_index,
501 const hb_tag_t *languages,
502 const hb_tag_t *features,
503 hb_set_t *lookup_indexes /* OUT */)
505 _hb_ot_layout_collect_lookups_features (face,
508 HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX,
515 unsigned int count = hb_ot_layout_script_get_language_tags (face,
519 for (unsigned int language_index = 0; language_index < count; language_index++)
520 _hb_ot_layout_collect_lookups_features (face,
529 for (; *languages; languages++)
531 unsigned int language_index;
532 if (hb_ot_layout_script_find_language (face,
537 _hb_ot_layout_collect_lookups_features (face,
548 hb_ot_layout_collect_lookups (hb_face_t *face,
550 const hb_tag_t *scripts,
551 const hb_tag_t *languages,
552 const hb_tag_t *features,
553 hb_set_t *lookup_indexes /* OUT */)
558 unsigned int count = hb_ot_layout_table_get_script_tags (face,
561 for (unsigned int script_index = 0; script_index < count; script_index++)
562 _hb_ot_layout_collect_lookups_languages (face,
571 for (; *scripts; scripts++)
573 unsigned int script_index;
574 if (hb_ot_layout_table_find_script (face,
578 _hb_ot_layout_collect_lookups_languages (face,
589 hb_ot_layout_lookup_collect_glyphs (hb_face_t *face,
591 unsigned int lookup_index,
592 hb_set_t *glyphs_before, /* OUT. May be NULL */
593 hb_set_t *glyphs_input, /* OUT. May be NULL */
594 hb_set_t *glyphs_after, /* OUT. May be NULL */
595 hb_set_t *glyphs_output /* OUT. May be NULL */)
597 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return;
599 OT::hb_collect_glyphs_context_t c (face,
609 const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
610 l.collect_glyphs_lookup (&c);
615 const OT::PosLookup& l = hb_ot_layout_from_face (face)->gpos->get_lookup (lookup_index);
616 l.collect_glyphs_lookup (&c);
628 hb_ot_layout_has_substitution (hb_face_t *face)
630 return &_get_gsub (face) != &OT::Null(OT::GSUB);
634 hb_ot_layout_lookup_would_substitute (hb_face_t *face,
635 unsigned int lookup_index,
636 const hb_codepoint_t *glyphs,
637 unsigned int glyphs_length,
638 hb_bool_t zero_context)
640 if (unlikely (!hb_ot_shaper_face_data_ensure (face))) return false;
641 return hb_ot_layout_lookup_would_substitute_fast (face, lookup_index, glyphs, glyphs_length, zero_context);
645 hb_ot_layout_lookup_would_substitute_fast (hb_face_t *face,
646 unsigned int lookup_index,
647 const hb_codepoint_t *glyphs,
648 unsigned int glyphs_length,
649 hb_bool_t zero_context)
651 if (unlikely (lookup_index >= hb_ot_layout_from_face (face)->gsub_lookup_count)) return false;
652 OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, zero_context);
654 const OT::SubstLookup& l = hb_ot_layout_from_face (face)->gsub->get_lookup (lookup_index);
656 return l.would_apply (&c, &hb_ot_layout_from_face (face)->gsub_digests[lookup_index]);
660 hb_ot_layout_substitute_start (hb_font_t *font, hb_buffer_t *buffer)
662 OT::GSUB::substitute_start (font, buffer);
666 hb_ot_layout_substitute_lookup (hb_font_t *font,
668 unsigned int lookup_index,
671 if (unlikely (lookup_index >= hb_ot_layout_from_face (font->face)->gsub_lookup_count)) return false;
673 OT::hb_apply_context_t c (font, buffer, mask);
675 const OT::SubstLookup& l = hb_ot_layout_from_face (font->face)->gsub->get_lookup (lookup_index);
677 return l.apply_string (&c, &hb_ot_layout_from_face (font->face)->gsub_digests[lookup_index]);
681 hb_ot_layout_substitute_finish (hb_font_t *font, hb_buffer_t *buffer)
683 OT::GSUB::substitute_finish (font, buffer);
687 hb_ot_layout_lookup_substitute_closure (hb_face_t *face,
688 unsigned int lookup_index,
691 OT::hb_closure_context_t c (face, glyphs);
693 const OT::SubstLookup& l = _get_gsub (face).get_lookup (lookup_index);
703 hb_ot_layout_has_positioning (hb_face_t *face)
705 return &_get_gpos (face) != &OT::Null(OT::GPOS);
709 hb_ot_layout_position_start (hb_font_t *font, hb_buffer_t *buffer)
711 OT::GPOS::position_start (font, buffer);
715 hb_ot_layout_position_lookup (hb_font_t *font,
717 unsigned int lookup_index,
720 if (unlikely (lookup_index >= hb_ot_layout_from_face (font->face)->gpos_lookup_count)) return false;
722 OT::hb_apply_context_t c (font, buffer, mask);
724 const OT::PosLookup& l = hb_ot_layout_from_face (font->face)->gpos->get_lookup (lookup_index);
726 return l.apply_string (&c, &hb_ot_layout_from_face (font->face)->gpos_digests[lookup_index]);
730 hb_ot_layout_position_finish (hb_font_t *font, hb_buffer_t *buffer, hb_bool_t zero_width_attached_marks)
732 OT::GPOS::position_finish (font, buffer, zero_width_attached_marks);
736 hb_ot_layout_get_size_params (hb_face_t *face,
737 unsigned int *design_size, /* OUT. May be NULL */
738 unsigned int *subfamily_id, /* OUT. May be NULL */
739 unsigned int *subfamily_name_id, /* OUT. May be NULL */
740 unsigned int *range_start, /* OUT. May be NULL */
741 unsigned int *range_end /* OUT. May be NULL */)
743 const OT::GPOS &gpos = _get_gpos (face);
744 const hb_tag_t tag = HB_TAG ('s','i','z','e');
746 unsigned int num_features = gpos.get_feature_count ();
747 for (unsigned int i = 0; i < num_features; i++)
749 if (tag == gpos.get_feature_tag (i))
751 const OT::Feature &f = gpos.get_feature (i);
752 const OT::FeatureParamsSize ¶ms = f.get_feature_params ().get_size_params (tag);
754 if (params.designSize)
756 #define PARAM(a, A) if (a) *a = params.A
757 PARAM (design_size, designSize);
758 PARAM (subfamily_id, subfamilyID);
759 PARAM (subfamily_name_id, subfamilyNameID);
760 PARAM (range_start, rangeStart);
761 PARAM (range_end, rangeEnd);
769 #define PARAM(a, A) if (a) *a = 0
770 PARAM (design_size, designSize);
771 PARAM (subfamily_id, subfamilyID);
772 PARAM (subfamily_name_id, subfamilyNameID);
773 PARAM (range_start, rangeStart);
774 PARAM (range_end, rangeEnd);