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;
49 layout->gdef_blob = hb_face_get_table (face, HB_OT_TAG_GDEF);
50 layout->gdef = &GDEF::get_for_data (hb_blob_lock (layout->gdef_blob));
52 layout->gsub_blob = hb_face_get_table (face, HB_OT_TAG_GSUB);
53 layout->gsub = &GSUB::get_for_data (hb_blob_lock (layout->gsub_blob));
55 layout->gpos_blob = hb_face_get_table (face, HB_OT_TAG_GPOS);
56 layout->gpos = &GPOS::get_for_data (hb_blob_lock (layout->gpos_blob));
60 _hb_ot_layout_fini (hb_face_t *face)
62 hb_ot_layout_t *layout = &face->ot_layout;
64 hb_blob_unlock (layout->gdef_blob);
65 hb_blob_unlock (layout->gsub_blob);
66 hb_blob_unlock (layout->gpos_blob);
68 hb_blob_destroy (layout->gdef_blob);
69 hb_blob_destroy (layout->gsub_blob);
70 hb_blob_destroy (layout->gpos_blob);
72 free (layout->new_gdef.klasses);
76 _get_gdef (hb_face_t *face)
78 return HB_LIKELY (face->ot_layout.gdef) ? *face->ot_layout.gdef : Null(GDEF);
82 _get_gsub (hb_face_t *face)
84 return HB_LIKELY (face->ot_layout.gsub) ? *face->ot_layout.gsub : Null(GSUB);
88 _get_gpos (hb_face_t *face)
90 return HB_LIKELY (face->ot_layout.gpos) ? *face->ot_layout.gpos : Null(GPOS);
98 /* TODO the public class_t is a mess */
101 hb_ot_layout_has_font_glyph_classes (hb_face_t *face)
103 return _get_gdef (face).has_glyph_classes ();
106 HB_INTERNAL hb_bool_t
107 _hb_ot_layout_has_new_glyph_classes (hb_face_t *face)
109 return face->ot_layout.new_gdef.len > 0;
113 _hb_ot_layout_get_glyph_property (hb_face_t *face,
114 hb_codepoint_t glyph)
116 hb_ot_layout_class_t klass;
117 const GDEF &gdef = _get_gdef (face);
119 klass = gdef.get_glyph_class (glyph);
121 if (!klass && glyph < face->ot_layout.new_gdef.len)
122 klass = face->ot_layout.new_gdef.klasses[glyph];
126 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
127 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
128 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
129 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
130 case GDEF::MarkGlyph:
131 klass = gdef.get_mark_attachment_type (glyph);
132 return HB_OT_LAYOUT_GLYPH_CLASS_MARK + (klass << 8);
136 HB_INTERNAL hb_bool_t
137 _hb_ot_layout_check_glyph_property (hb_face_t *face,
138 hb_internal_glyph_info_t *ginfo,
139 unsigned int lookup_flags,
140 unsigned int *property_out)
142 unsigned int property;
144 if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
145 ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint);
146 property = ginfo->gproperty;
148 *property_out = property;
150 /* Not covered, if, for example, glyph class is ligature and
151 * lookup_flags includes LookupFlags::IgnoreLigatures
153 if (property & lookup_flags)
156 if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
158 /* If using mark filtering sets, the high short of
159 * lookup_flags has the set index.
161 if (lookup_flags & LookupFlag::UseMarkFilteringSet)
162 return _get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
164 /* The second byte of lookup_flags has the meaning
165 * "ignore marks of attachment type different than
166 * the attachment type specified."
168 if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
169 return (lookup_flags & LookupFlag::MarkAttachmentType) == (property & LookupFlag::MarkAttachmentType);
175 HB_INTERNAL hb_bool_t
176 _hb_ot_layout_skip_mark (hb_face_t *face,
177 hb_internal_glyph_info_t *ginfo,
178 unsigned int lookup_flags,
179 unsigned int *property_out)
181 unsigned int property;
183 if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
184 ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint);
185 property = ginfo->gproperty;
187 *property_out = property;
189 if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
191 /* Skip mark if lookup_flags includes LookupFlags::IgnoreMarks */
192 if (lookup_flags & LookupFlag::IgnoreMarks)
195 /* If using mark filtering sets, the high short of lookup_flags has the set index. */
196 if (lookup_flags & LookupFlag::UseMarkFilteringSet)
197 return !_get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
199 /* The second byte of lookup_flags has the meaning "ignore marks of attachment type
200 * different than the attachment type specified." */
201 if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
202 return (lookup_flags & LookupFlag::MarkAttachmentType) != (property & LookupFlag::MarkAttachmentType);
209 _hb_ot_layout_set_glyph_class (hb_face_t *face,
210 hb_codepoint_t glyph,
211 hb_ot_layout_glyph_class_t klass)
213 if (HB_OBJECT_IS_INERT (face))
216 /* TODO optimize this? similar to old harfbuzz code for example */
218 hb_ot_layout_t *layout = &face->ot_layout;
219 hb_ot_layout_class_t gdef_klass;
220 unsigned int len = layout->new_gdef.len;
222 if (HB_UNLIKELY (glyph > 65535))
225 /* XXX this is not threadsafe */
228 unsigned char *new_klasses;
230 new_len = len == 0 ? 120 : 2 * len;
233 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
235 if (HB_UNLIKELY (!new_klasses))
238 memset (new_klasses + len, 0, new_len - len);
240 layout->new_gdef.klasses = new_klasses;
241 layout->new_gdef.len = new_len;
246 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
247 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
248 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
249 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
250 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
253 layout->new_gdef.klasses[glyph] = gdef_klass;
258 _hb_ot_layout_set_glyph_property (hb_face_t *face,
259 hb_codepoint_t glyph,
260 unsigned int property)
261 { _hb_ot_layout_set_glyph_class (face, glyph, (hb_ot_layout_glyph_class_t) (property & 0xff)); }
264 hb_ot_layout_glyph_class_t
265 hb_ot_layout_get_glyph_class (hb_face_t *face,
266 hb_codepoint_t glyph)
268 return (hb_ot_layout_glyph_class_t) (_hb_ot_layout_get_glyph_property (face, glyph) & 0xff);
272 hb_ot_layout_set_glyph_class (hb_face_t *face,
273 hb_codepoint_t glyph,
274 hb_ot_layout_glyph_class_t klass)
276 _hb_ot_layout_set_glyph_class (face, glyph, klass);
280 hb_ot_layout_build_glyph_classes (hb_face_t *face,
281 uint16_t num_total_glyphs,
282 hb_codepoint_t *glyphs,
283 unsigned char *klasses,
286 if (HB_OBJECT_IS_INERT (face))
289 hb_ot_layout_t *layout = &face->ot_layout;
291 if (HB_UNLIKELY (!count || !glyphs || !klasses))
294 if (layout->new_gdef.len == 0) {
295 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
296 layout->new_gdef.len = count;
299 for (unsigned int i = 0; i < count; i++)
300 _hb_ot_layout_set_glyph_class (face, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
304 hb_ot_layout_get_attach_points (hb_face_t *face,
305 hb_codepoint_t glyph,
306 unsigned int *point_count /* IN/OUT */,
307 unsigned int *point_array /* OUT */)
309 return _get_gdef (face).get_attach_points (glyph, point_count, point_array);
313 hb_ot_layout_get_lig_carets (hb_face_t *face,
315 hb_codepoint_t glyph,
316 unsigned int *caret_count /* IN/OUT */,
317 int *caret_array /* OUT */)
319 hb_ot_layout_context_t context;
322 return _get_gdef (face).get_lig_carets (&context, glyph, caret_count, caret_array);
329 static const GSUBGPOS&
330 get_gsubgpos_table (hb_face_t *face,
334 case HB_OT_TAG_GSUB: return _get_gsub (face);
335 case HB_OT_TAG_GPOS: return _get_gpos (face);
336 default: return Null(GSUBGPOS);
342 hb_ot_layout_table_get_script_count (hb_face_t *face,
345 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
347 return g.get_script_count ();
351 hb_ot_layout_table_get_script_tag (hb_face_t *face,
353 unsigned int script_index)
355 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
357 return g.get_script_tag (script_index);
361 hb_ot_layout_table_find_script (hb_face_t *face,
364 unsigned int *script_index)
366 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
367 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
369 if (g.find_script_index (script_tag, script_index))
372 /* try finding 'DFLT' */
373 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
376 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
377 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
380 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
385 hb_ot_layout_table_get_feature_count (hb_face_t *face,
388 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
390 return g.get_feature_count ();
394 hb_ot_layout_table_get_feature_tag (hb_face_t *face,
396 unsigned int feature_index)
398 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
400 return g.get_feature_tag (feature_index);
404 hb_ot_layout_table_find_feature (hb_face_t *face,
406 hb_tag_t feature_tag,
407 unsigned int *feature_index)
409 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
410 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
412 if (g.find_feature_index (feature_tag, feature_index))
415 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
420 hb_ot_layout_table_get_lookup_count (hb_face_t *face,
423 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
425 return g.get_lookup_count ();
430 hb_ot_layout_script_get_language_count (hb_face_t *face,
432 unsigned int script_index)
434 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
436 return s.get_lang_sys_count ();
440 hb_ot_layout_script_get_language_tag (hb_face_t *face,
442 unsigned int script_index,
443 unsigned int language_index)
445 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
447 return s.get_lang_sys_tag (language_index);
451 hb_ot_layout_script_find_language (hb_face_t *face,
453 unsigned int script_index,
454 hb_tag_t language_tag,
455 unsigned int *language_index)
457 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
458 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
460 if (s.find_lang_sys_index (language_tag, language_index))
463 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
464 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
467 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
472 hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
474 unsigned int script_index,
475 unsigned int language_index,
476 unsigned int *feature_index)
478 const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
480 if (feature_index) *feature_index = l.get_required_feature_index ();
482 return l.has_required_feature ();
486 hb_ot_layout_language_get_feature_count (hb_face_t *face,
488 unsigned int script_index,
489 unsigned int language_index)
491 const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
493 return l.get_feature_count ();
497 hb_ot_layout_language_get_feature_index (hb_face_t *face,
499 unsigned int script_index,
500 unsigned int language_index,
501 unsigned int num_feature)
503 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
504 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
506 return l.get_feature_index (num_feature);
510 hb_ot_layout_language_get_feature_tag (hb_face_t *face,
512 unsigned int script_index,
513 unsigned int language_index,
514 unsigned int num_feature)
516 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
517 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
518 unsigned int feature_index = l.get_feature_index (num_feature);
520 return g.get_feature_tag (feature_index);
525 hb_ot_layout_language_find_feature (hb_face_t *face,
527 unsigned int script_index,
528 unsigned int language_index,
529 hb_tag_t feature_tag,
530 unsigned int *feature_index)
532 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
533 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
534 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
536 unsigned int num_features = l.get_feature_count ();
537 for (unsigned int i = 0; i < num_features; i++) {
538 unsigned int f_index = l.get_feature_index (i);
540 if (feature_tag == g.get_feature_tag (f_index)) {
541 if (feature_index) *feature_index = f_index;
546 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
551 hb_ot_layout_feature_get_lookup_count (hb_face_t *face,
553 unsigned int feature_index)
555 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
556 const Feature &f = g.get_feature (feature_index);
558 return f.get_lookup_count ();
562 hb_ot_layout_feature_get_lookup_index (hb_face_t *face,
564 unsigned int feature_index,
565 unsigned int num_lookup)
567 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
568 const Feature &f = g.get_feature (feature_index);
570 return f.get_lookup_index (num_lookup);
578 hb_ot_layout_has_substitution (hb_face_t *face)
580 return &_get_gsub (face) != &Null(GSUB);
584 hb_ot_layout_substitute_lookup (hb_face_t *face,
586 unsigned int lookup_index,
587 hb_ot_layout_feature_mask_t mask)
589 hb_ot_layout_context_t context;
592 return _get_gsub (face).substitute_lookup (&context, buffer, lookup_index, mask);
600 hb_ot_layout_has_positioning (hb_face_t *face)
602 return &_get_gpos (face) != &Null(GPOS);
606 hb_ot_layout_position_lookup (hb_face_t *face,
609 unsigned int lookup_index,
610 hb_ot_layout_feature_mask_t mask)
612 hb_ot_layout_context_t context;
615 return _get_gpos (face).position_lookup (&context, buffer, lookup_index, mask);