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
32 #include "hb-buffer-private.h"
34 #include "hb-ot-layout.h"
35 #include "hb-ot-layout-private.h"
37 #include "hb-ot-layout-open-private.h"
38 #include "hb-ot-layout-gdef-private.h"
39 #include "hb-ot-layout-gsub-private.h"
40 #include "hb-ot-layout-gpos-private.h"
48 hb_ot_layout_create (void)
50 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
52 layout->gdef = &Null(GDEF);
53 layout->gsub = &Null(GSUB);
54 layout->gpos = &Null(GPOS);
60 hb_ot_layout_has_substitution (hb_ot_layout_t *layout)
62 return layout->gsub != &Null(GSUB);
66 hb_ot_layout_has_positioning (hb_ot_layout_t *layout)
68 return layout->gpos != &Null(GPOS);
72 hb_ot_layout_create_for_data (const char *font_data,
75 hb_ot_layout_t *layout;
77 if (HB_UNLIKELY (font_data == NULL))
78 return hb_ot_layout_create ();
80 layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
82 const OpenTypeFontFile &font = OpenTypeFontFile::get_for_data (font_data);
83 const OpenTypeFontFace &face = font.get_face (face_index);
85 layout->gdef = &GDEF::get_for_data (font.get_table_data (face.get_table_by_tag (GDEF::Tag)));
86 layout->gsub = &GSUB::get_for_data (font.get_table_data (face.get_table_by_tag (GSUB::Tag)));
87 layout->gpos = &GPOS::get_for_data (font.get_table_data (face.get_table_by_tag (GPOS::Tag)));
93 hb_ot_layout_create_for_tables (const char *gdef_data,
94 const char *gsub_data,
95 const char *gpos_data)
97 hb_ot_layout_t *layout;
99 if (HB_UNLIKELY (gdef_data == NULL && gsub_data == NULL && gpos_data == NULL))
100 return hb_ot_layout_create ();
102 layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
104 layout->gdef = &GDEF::get_for_data (gdef_data);
105 layout->gsub = &GSUB::get_for_data (gsub_data);
106 layout->gpos = &GPOS::get_for_data (gpos_data);
112 hb_ot_layout_destroy (hb_ot_layout_t *layout)
118 hb_ot_layout_set_hinting (hb_ot_layout_t *layout,
121 layout->gpos_info.dvi = !hinted;
125 hb_ot_layout_set_scale (hb_ot_layout_t *layout,
126 hb_16dot16_t x_scale, hb_16dot16_t y_scale)
128 layout->gpos_info.x_scale = x_scale;
129 layout->gpos_info.y_scale = y_scale;
133 hb_ot_layout_set_ppem (hb_ot_layout_t *layout,
134 unsigned int x_ppem, unsigned int y_ppem)
136 layout->gpos_info.x_ppem = x_ppem;
137 layout->gpos_info.y_ppem = y_ppem;
145 /* TODO the public class_t is a mess */
148 hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout)
150 return layout->gdef->has_glyph_classes ();
153 HB_INTERNAL hb_bool_t
154 _hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout)
156 return layout->new_gdef.len > 0;
159 HB_INTERNAL unsigned int
160 _hb_ot_layout_get_glyph_property (hb_ot_layout_t *layout,
161 hb_codepoint_t glyph)
163 hb_ot_layout_class_t klass;
165 klass = layout->gdef->get_glyph_class (glyph);
167 if (!klass && glyph < layout->new_gdef.len)
168 klass = layout->new_gdef.klasses[glyph];
172 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
173 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
174 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
175 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
176 case GDEF::MarkGlyph:
177 /* TODO old harfbuzz doesn't always parse mark attachments as it says it was
178 * introduced without a version bump, so it may not be safe */
179 klass = layout->gdef->get_mark_attachment_type (glyph);
180 return HB_OT_LAYOUT_GLYPH_CLASS_MARK + (klass << 8);
184 HB_INTERNAL hb_bool_t
185 _hb_ot_layout_check_glyph_property (hb_ot_layout_t *layout,
186 hb_glyph_info_t *ginfo,
187 unsigned int lookup_flags,
188 unsigned int *property_out)
190 unsigned int property;
192 if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
193 ginfo->gproperty = _hb_ot_layout_get_glyph_property (layout, ginfo->gindex);
194 property = ginfo->gproperty;
196 *property_out = property;
198 /* Not covered, if, for example, glyph class is ligature and
199 * lookup_flags includes LookupFlags::IgnoreLigatures
201 if (property & lookup_flags)
204 if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
206 /* If using mark filtering sets, the high short of
207 * lookup_flags has the set index.
209 if (lookup_flags & LookupFlag::UseMarkFilteringSet)
210 return layout->gdef->mark_set_covers (lookup_flags >> 16, ginfo->gindex);
212 /* The second byte of lookup_flags has the meaning
213 * "ignore marks of attachment type different than
214 * the attachment type specified."
216 if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
217 return (lookup_flags & LookupFlag::MarkAttachmentType) == (property & LookupFlag::MarkAttachmentType);
223 HB_INTERNAL hb_bool_t
224 _hb_ot_layout_skip_mark (hb_ot_layout_t *layout,
225 hb_glyph_info_t *ginfo,
226 unsigned int lookup_flags,
227 unsigned int *property_out)
229 unsigned int property;
231 if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
232 ginfo->gproperty = _hb_ot_layout_get_glyph_property (layout, ginfo->gindex);
233 property = ginfo->gproperty;
235 *property_out = property;
237 if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
239 /* Skip mark if lookup_flags includes LookupFlags::IgnoreMarks */
240 if (lookup_flags & LookupFlag::IgnoreMarks)
243 /* If using mark filtering sets, the high short of lookup_flags has the set index. */
244 if (lookup_flags & LookupFlag::UseMarkFilteringSet)
245 return !layout->gdef->mark_set_covers (lookup_flags >> 16, ginfo->gindex);
247 /* The second byte of lookup_flags has the meaning "ignore marks of attachment type
248 * different than the attachment type specified." */
249 if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
250 return (lookup_flags & LookupFlag::MarkAttachmentType) != (property & LookupFlag::MarkAttachmentType);
257 _hb_ot_layout_set_glyph_property (hb_ot_layout_t *layout,
258 hb_codepoint_t glyph,
259 unsigned int property)
260 { hb_ot_layout_set_glyph_class (layout, glyph, (hb_ot_layout_glyph_class_t) (property & 0xff)); }
263 hb_ot_layout_glyph_class_t
264 hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout,
265 hb_codepoint_t glyph)
266 { return (hb_ot_layout_glyph_class_t) (_hb_ot_layout_get_glyph_property (layout, glyph) & 0xff); }
269 hb_ot_layout_set_glyph_class (hb_ot_layout_t *layout,
270 hb_codepoint_t glyph,
271 hb_ot_layout_glyph_class_t klass)
273 /* TODO optimize this, similar to old harfbuzz code for example */
275 hb_ot_layout_class_t gdef_klass;
276 int len = layout->new_gdef.len;
278 if (HB_UNLIKELY (glyph > 65535))
283 unsigned char *new_klasses;
285 new_len = len == 0 ? 120 : 2 * len;
288 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
290 if (HB_UNLIKELY (!new_klasses))
293 memset (new_klasses + len, 0, new_len - len);
295 layout->new_gdef.klasses = new_klasses;
296 layout->new_gdef.len = new_len;
301 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
302 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
303 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
304 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
305 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
308 layout->new_gdef.klasses[glyph] = gdef_klass;
313 hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout,
314 uint16_t num_total_glyphs,
315 hb_codepoint_t *glyphs,
316 unsigned char *klasses,
319 if (HB_UNLIKELY (!count || !glyphs || !klasses))
322 if (layout->new_gdef.len == 0) {
323 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
324 layout->new_gdef.len = count;
327 for (unsigned int i = 0; i < count; i++)
328 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
332 hb_ot_layout_get_attach_points (hb_ot_layout_t *layout,
333 hb_codepoint_t glyph,
334 unsigned int *point_count /* IN/OUT */,
335 unsigned int *point_array /* OUT */)
337 return layout->gdef->get_attach_points (glyph, point_count, point_array);
341 hb_ot_layout_get_lig_carets (hb_ot_layout_t *layout,
342 hb_codepoint_t glyph,
343 unsigned int *caret_count /* IN/OUT */,
344 int *caret_array /* OUT */)
346 return layout->gdef->get_lig_carets (layout, glyph, caret_count, caret_array);
353 static const GSUBGPOS&
354 get_gsubgpos_table (hb_ot_layout_t *layout,
355 hb_ot_layout_table_type_t table_type)
357 switch (table_type) {
358 case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub);
359 case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos);
360 default: return Null(GSUBGPOS);
366 hb_ot_layout_table_get_script_count (hb_ot_layout_t *layout,
367 hb_ot_layout_table_type_t table_type)
369 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
371 return g.get_script_count ();
375 hb_ot_layout_table_get_script_tag (hb_ot_layout_t *layout,
376 hb_ot_layout_table_type_t table_type,
377 unsigned int script_index)
379 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
381 return g.get_script_tag (script_index);
385 hb_ot_layout_table_find_script (hb_ot_layout_t *layout,
386 hb_ot_layout_table_type_t table_type,
388 unsigned int *script_index)
390 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
391 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
393 if (g.find_script_index (script_tag, script_index))
396 /* try finding 'DFLT' */
397 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
400 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
401 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
404 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
409 hb_ot_layout_table_get_feature_count (hb_ot_layout_t *layout,
410 hb_ot_layout_table_type_t table_type)
412 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
414 return g.get_feature_count ();
418 hb_ot_layout_table_get_feature_tag (hb_ot_layout_t *layout,
419 hb_ot_layout_table_type_t table_type,
420 unsigned int feature_index)
422 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
424 return g.get_feature_tag (feature_index);
428 hb_ot_layout_table_find_feature (hb_ot_layout_t *layout,
429 hb_ot_layout_table_type_t table_type,
430 hb_tag_t feature_tag,
431 unsigned int *feature_index)
433 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
434 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
436 if (g.find_feature_index (feature_tag, feature_index))
439 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
444 hb_ot_layout_table_get_lookup_count (hb_ot_layout_t *layout,
445 hb_ot_layout_table_type_t table_type)
447 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
449 return g.get_lookup_count ();
454 hb_ot_layout_script_get_language_count (hb_ot_layout_t *layout,
455 hb_ot_layout_table_type_t table_type,
456 unsigned int script_index)
458 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
460 return s.get_lang_sys_count ();
464 hb_ot_layout_script_get_language_tag (hb_ot_layout_t *layout,
465 hb_ot_layout_table_type_t table_type,
466 unsigned int script_index,
467 unsigned int language_index)
469 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
471 return s.get_lang_sys_tag (language_index);
475 hb_ot_layout_script_find_language (hb_ot_layout_t *layout,
476 hb_ot_layout_table_type_t table_type,
477 unsigned int script_index,
478 hb_tag_t language_tag,
479 unsigned int *language_index)
481 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
482 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
484 if (s.find_lang_sys_index (language_tag, language_index))
487 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
488 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
491 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
496 hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t *layout,
497 hb_ot_layout_table_type_t table_type,
498 unsigned int script_index,
499 unsigned int language_index,
500 unsigned int *feature_index)
502 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
504 if (feature_index) *feature_index = l.get_required_feature_index ();
506 return l.has_required_feature ();
510 hb_ot_layout_language_get_feature_count (hb_ot_layout_t *layout,
511 hb_ot_layout_table_type_t table_type,
512 unsigned int script_index,
513 unsigned int language_index)
515 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
517 return l.get_feature_count ();
521 hb_ot_layout_language_get_feature_index (hb_ot_layout_t *layout,
522 hb_ot_layout_table_type_t table_type,
523 unsigned int script_index,
524 unsigned int language_index,
525 unsigned int num_feature)
527 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
528 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
530 return l.get_feature_index (num_feature);
534 hb_ot_layout_language_get_feature_tag (hb_ot_layout_t *layout,
535 hb_ot_layout_table_type_t table_type,
536 unsigned int script_index,
537 unsigned int language_index,
538 unsigned int num_feature)
540 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
541 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
542 unsigned int feature_index = l.get_feature_index (num_feature);
544 return g.get_feature_tag (feature_index);
549 hb_ot_layout_language_find_feature (hb_ot_layout_t *layout,
550 hb_ot_layout_table_type_t table_type,
551 unsigned int script_index,
552 unsigned int language_index,
553 hb_tag_t feature_tag,
554 unsigned int *feature_index)
556 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
557 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
558 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
560 unsigned int num_features = l.get_feature_count ();
561 for (unsigned int i = 0; i < num_features; i++) {
562 unsigned int f_index = l.get_feature_index (i);
564 if (feature_tag == g.get_feature_tag (f_index)) {
565 if (feature_index) *feature_index = f_index;
570 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
575 hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t *layout,
576 hb_ot_layout_table_type_t table_type,
577 unsigned int feature_index)
579 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
580 const Feature &f = g.get_feature (feature_index);
582 return f.get_lookup_count ();
586 hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t *layout,
587 hb_ot_layout_table_type_t table_type,
588 unsigned int feature_index,
589 unsigned int num_lookup)
591 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
592 const Feature &f = g.get_feature (feature_index);
594 return f.get_lookup_index (num_lookup);
602 hb_ot_layout_substitute_lookup (hb_ot_layout_t *layout,
604 unsigned int lookup_index,
605 hb_ot_layout_feature_mask_t mask)
607 return layout->gsub->substitute_lookup (layout, buffer, lookup_index, mask);
615 hb_ot_layout_position_lookup (hb_ot_layout_t *layout,
617 unsigned int lookup_index,
618 hb_ot_layout_feature_mask_t mask)
620 return layout->gpos->position_lookup (layout, buffer, lookup_index, mask);