2 * Copyright (C) 1998-2004 David Turner and Werner Lemberg
3 * Copyright (C) 2006 Behdad Esfahbod
4 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
6 * This is part of HarfBuzz, an OpenType Layout engine library.
8 * Permission is hereby granted, without written agreement and without
9 * license or royalty fees, to use, copy, modify, and distribute this
10 * software and its documentation for any purpose, provided that the
11 * above copyright notice and the following two paragraphs appear in
12 * all copies of this software.
14 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
20 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
26 * Red Hat Author(s): Behdad Esfahbod
29 #define HB_OT_LAYOUT_CC
31 #include "hb-ot-layout-private.h"
33 #include "hb-ot-layout-gdef-private.hh"
34 #include "hb-ot-layout-gsub-private.hh"
35 #include "hb-ot-layout-gpos-private.hh"
43 _hb_ot_layout_init (hb_face_t *face)
45 hb_ot_layout_t *layout = &face->ot_layout;
47 layout->gdef_blob = Sanitizer<GDEF>::sanitize (hb_face_get_table (face, HB_OT_TAG_GDEF));
48 layout->gdef = &Sanitizer<GDEF>::lock_instance (layout->gdef_blob);
50 layout->gsub_blob = Sanitizer<GSUB>::sanitize (hb_face_get_table (face, HB_OT_TAG_GSUB));
51 layout->gsub = &Sanitizer<GSUB>::lock_instance (layout->gsub_blob);
53 layout->gpos_blob = Sanitizer<GPOS>::sanitize (hb_face_get_table (face, HB_OT_TAG_GPOS));
54 layout->gpos = &Sanitizer<GPOS>::lock_instance (layout->gpos_blob);
58 _hb_ot_layout_fini (hb_face_t *face)
60 hb_ot_layout_t *layout = &face->ot_layout;
62 hb_blob_unlock (layout->gdef_blob);
63 hb_blob_unlock (layout->gsub_blob);
64 hb_blob_unlock (layout->gpos_blob);
66 hb_blob_destroy (layout->gdef_blob);
67 hb_blob_destroy (layout->gsub_blob);
68 hb_blob_destroy (layout->gpos_blob);
70 free (layout->new_gdef.klasses);
74 _get_gdef (hb_face_t *face)
76 return HB_LIKELY (face->ot_layout.gdef) ? *face->ot_layout.gdef : Null(GDEF);
80 _get_gsub (hb_face_t *face)
82 return HB_LIKELY (face->ot_layout.gsub) ? *face->ot_layout.gsub : Null(GSUB);
86 _get_gpos (hb_face_t *face)
88 return HB_LIKELY (face->ot_layout.gpos) ? *face->ot_layout.gpos : Null(GPOS);
96 /* TODO the public class_t is a mess */
99 hb_ot_layout_has_font_glyph_classes (hb_face_t *face)
101 return _get_gdef (face).has_glyph_classes ();
105 _hb_ot_layout_has_new_glyph_classes (hb_face_t *face)
107 return face->ot_layout.new_gdef.len > 0;
111 _hb_ot_layout_get_glyph_property (hb_face_t *face,
112 hb_codepoint_t glyph)
114 hb_ot_layout_class_t klass;
115 const GDEF &gdef = _get_gdef (face);
117 klass = gdef.get_glyph_class (glyph);
119 if (!klass && glyph < face->ot_layout.new_gdef.len)
120 klass = face->ot_layout.new_gdef.klasses[glyph];
124 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
125 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
126 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
127 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
128 case GDEF::MarkGlyph:
129 klass = gdef.get_mark_attachment_type (glyph);
130 return HB_OT_LAYOUT_GLYPH_CLASS_MARK + (klass << 8);
135 _hb_ot_layout_check_glyph_property (hb_face_t *face,
136 hb_internal_glyph_info_t *ginfo,
137 unsigned int lookup_flags,
138 unsigned int *property_out)
140 unsigned int property;
142 if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
143 ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint);
144 property = ginfo->gproperty;
146 *property_out = property;
148 /* Not covered, if, for example, glyph class is ligature and
149 * lookup_flags includes LookupFlags::IgnoreLigatures
151 if (property & lookup_flags)
154 if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
156 /* If using mark filtering sets, the high short of
157 * lookup_flags has the set index.
159 if (lookup_flags & LookupFlag::UseMarkFilteringSet)
160 return _get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
162 /* The second byte of lookup_flags has the meaning
163 * "ignore marks of attachment type different than
164 * the attachment type specified."
166 if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
167 return (lookup_flags & LookupFlag::MarkAttachmentType) == (property & LookupFlag::MarkAttachmentType);
174 _hb_ot_layout_skip_mark (hb_face_t *face,
175 hb_internal_glyph_info_t *ginfo,
176 unsigned int lookup_flags,
177 unsigned int *property_out)
179 unsigned int property;
181 if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
182 ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint);
183 property = ginfo->gproperty;
185 *property_out = property;
187 if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
189 /* Skip mark if lookup_flags includes LookupFlags::IgnoreMarks */
190 if (lookup_flags & LookupFlag::IgnoreMarks)
193 /* If using mark filtering sets, the high short of lookup_flags has the set index. */
194 if (lookup_flags & LookupFlag::UseMarkFilteringSet)
195 return !_get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
197 /* The second byte of lookup_flags has the meaning "ignore marks of attachment type
198 * different than the attachment type specified." */
199 if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
200 return (lookup_flags & LookupFlag::MarkAttachmentType) != (property & LookupFlag::MarkAttachmentType);
207 _hb_ot_layout_set_glyph_class (hb_face_t *face,
208 hb_codepoint_t glyph,
209 hb_ot_layout_glyph_class_t klass)
211 if (HB_OBJECT_IS_INERT (face))
214 /* TODO optimize this? similar to old harfbuzz code for example */
216 hb_ot_layout_t *layout = &face->ot_layout;
217 hb_ot_layout_class_t gdef_klass;
218 unsigned int len = layout->new_gdef.len;
220 if (HB_UNLIKELY (glyph > 65535))
223 /* XXX this is not threadsafe */
225 unsigned int new_len;
226 unsigned char *new_klasses;
228 new_len = len == 0 ? 120 : 2 * len;
229 while (new_len <= glyph)
234 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
236 if (HB_UNLIKELY (!new_klasses))
239 memset (new_klasses + len, 0, new_len - len);
241 layout->new_gdef.klasses = new_klasses;
242 layout->new_gdef.len = new_len;
247 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
248 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
249 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
250 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
251 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
254 layout->new_gdef.klasses[glyph] = gdef_klass;
259 _hb_ot_layout_set_glyph_property (hb_face_t *face,
260 hb_codepoint_t glyph,
261 unsigned int property)
262 { _hb_ot_layout_set_glyph_class (face, glyph, (hb_ot_layout_glyph_class_t) (property & 0xff)); }
265 hb_ot_layout_glyph_class_t
266 hb_ot_layout_get_glyph_class (hb_face_t *face,
267 hb_codepoint_t glyph)
269 return (hb_ot_layout_glyph_class_t) (_hb_ot_layout_get_glyph_property (face, glyph) & 0xff);
273 hb_ot_layout_set_glyph_class (hb_face_t *face,
274 hb_codepoint_t glyph,
275 hb_ot_layout_glyph_class_t klass)
277 _hb_ot_layout_set_glyph_class (face, glyph, klass);
281 hb_ot_layout_build_glyph_classes (hb_face_t *face,
282 uint16_t num_total_glyphs,
283 hb_codepoint_t *glyphs,
284 unsigned char *klasses,
287 if (HB_OBJECT_IS_INERT (face))
290 hb_ot_layout_t *layout = &face->ot_layout;
292 if (HB_UNLIKELY (!count || !glyphs || !klasses))
295 if (layout->new_gdef.len == 0) {
296 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
297 layout->new_gdef.len = count;
300 for (unsigned int i = 0; i < count; i++)
301 _hb_ot_layout_set_glyph_class (face, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
305 hb_ot_layout_get_attach_points (hb_face_t *face,
306 hb_codepoint_t glyph,
307 unsigned int *point_count /* IN/OUT */,
308 unsigned int *point_array /* OUT */)
310 return _get_gdef (face).get_attach_points (glyph, point_count, point_array);
314 hb_ot_layout_get_lig_carets (hb_face_t *face,
316 hb_codepoint_t glyph,
317 unsigned int *caret_count /* IN/OUT */,
318 int *caret_array /* OUT */)
320 hb_ot_layout_context_t context;
323 return _get_gdef (face).get_lig_carets (&context, glyph, caret_count, caret_array);
330 static const GSUBGPOS&
331 get_gsubgpos_table (hb_face_t *face,
335 case HB_OT_TAG_GSUB: return _get_gsub (face);
336 case HB_OT_TAG_GPOS: return _get_gpos (face);
337 default: return Null(GSUBGPOS);
343 hb_ot_layout_table_get_script_tags (hb_face_t *face,
345 unsigned int *script_count /* IN/OUT */,
346 hb_tag_t *script_tags /* OUT */)
348 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
350 return g.get_script_tags (script_count, script_tags);
354 hb_ot_layout_table_find_script (hb_face_t *face,
357 unsigned int *script_index)
359 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
360 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
362 if (g.find_script_index (script_tag, script_index))
365 /* try finding 'DFLT' */
366 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
369 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
370 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
373 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
378 hb_ot_layout_table_get_feature_tags (hb_face_t *face,
380 unsigned int *feature_count /* IN/OUT */,
381 hb_tag_t *feature_tags /* OUT */)
383 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
385 return g.get_feature_tags (feature_count, feature_tags);
390 hb_ot_layout_script_get_language_tags (hb_face_t *face,
392 unsigned int script_index,
393 unsigned int *language_count /* IN/OUT */,
394 hb_tag_t *language_tags /* OUT */)
396 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
398 return s.get_lang_sys_tags (language_count, language_tags);
402 hb_ot_layout_script_find_language (hb_face_t *face,
404 unsigned int script_index,
405 hb_tag_t language_tag,
406 unsigned int *language_index)
408 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
409 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
411 if (s.find_lang_sys_index (language_tag, language_index))
414 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
415 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
418 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
423 hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
425 unsigned int script_index,
426 unsigned int language_index,
427 unsigned int *feature_index)
429 const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
431 if (feature_index) *feature_index = l.get_required_feature_index ();
433 return l.has_required_feature ();
437 hb_ot_layout_language_get_feature_indexes (hb_face_t *face,
439 unsigned int script_index,
440 unsigned int language_index,
441 unsigned int *feature_count /* IN/OUT */,
442 unsigned int *feature_indexes /* OUT */)
444 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
445 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
447 return l.get_feature_indexes (feature_count, feature_indexes);
451 hb_ot_layout_language_get_feature_tags (hb_face_t *face,
453 unsigned int script_index,
454 unsigned int language_index,
455 unsigned int *feature_count /* IN/OUT */,
456 hb_tag_t *feature_tags /* OUT */)
458 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
459 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
461 ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
462 hb_bool_t ret = l.get_feature_indexes (feature_count, (unsigned int *) feature_tags);
465 unsigned int count = *feature_count;
466 for (unsigned int i = 0; i < count; i++)
467 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
475 hb_ot_layout_language_find_feature (hb_face_t *face,
477 unsigned int script_index,
478 unsigned int language_index,
479 hb_tag_t feature_tag,
480 unsigned int *feature_index)
482 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
483 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
484 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
486 unsigned int num_features = l.get_feature_count ();
487 for (unsigned int i = 0; i < num_features; i++) {
488 unsigned int f_index = l.get_feature_index (i);
490 if (feature_tag == g.get_feature_tag (f_index)) {
491 if (feature_index) *feature_index = f_index;
496 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
501 hb_ot_layout_feature_get_lookup_indexes (hb_face_t *face,
503 unsigned int feature_index,
504 unsigned int *lookup_count /* IN/OUT */,
505 unsigned int *lookup_indexes /* OUT */)
507 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
508 const Feature &f = g.get_feature (feature_index);
510 return f.get_lookup_indexes (lookup_count, lookup_indexes);
519 hb_ot_layout_has_substitution (hb_face_t *face)
521 return &_get_gsub (face) != &Null(GSUB);
525 hb_ot_layout_substitute_lookup (hb_face_t *face,
527 unsigned int lookup_index,
530 hb_ot_layout_context_t context;
533 return _get_gsub (face).substitute_lookup (&context, buffer, lookup_index, mask);
542 hb_ot_layout_has_positioning (hb_face_t *face)
544 return &_get_gpos (face) != &Null(GPOS);
548 hb_ot_layout_position_lookup (hb_face_t *face,
551 unsigned int lookup_index,
554 hb_ot_layout_context_t context;
557 return _get_gpos (face).position_lookup (&context, buffer, lookup_index, mask);