2 * Copyright (C) 1998-2004 David Turner and Werner Lemberg
3 * Copyright (C) 2006 Behdad Esfahbod
4 * Copyright (C) 2007,2008 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.h"
32 #include "hb-ot-layout-private.h"
34 #include "hb-ot-layout-open-private.h"
35 #include "hb-ot-layout-gdef-private.h"
36 #include "hb-ot-layout-gsub-private.h"
43 struct _hb_ot_layout_t {
46 const /*XXX*/GSUBGPOS *gpos;
49 unsigned char *klasses;
56 hb_ot_layout_create (void)
58 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
60 layout->gdef = &NullGDEF;
61 layout->gsub = &NullGSUB;
62 layout->gpos = &/*XXX*/NullGSUBGPOS;
68 hb_ot_layout_create_for_data (const char *font_data,
71 hb_ot_layout_t *layout;
73 if (HB_UNLIKELY (font_data == NULL))
74 return hb_ot_layout_create ();
76 layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
78 const OpenTypeFontFile &font = OpenTypeFontFile::get_for_data (font_data);
79 const OpenTypeFontFace &face = font.get_face (face_index);
81 layout->gdef = &GDEF::get_for_data (font.get_table_data (face.get_table_by_tag (GDEF::Tag)));
82 layout->gsub = &GSUB::get_for_data (font.get_table_data (face.get_table_by_tag (GSUB::Tag)));
83 layout->gpos = &/*XXX*/GSUBGPOS::get_for_data (font.get_table_data (face.get_table_by_tag (/*XXX*/GSUBGPOS::GPOSTag)));
89 hb_ot_layout_destroy (hb_ot_layout_t *layout)
99 hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout)
101 return layout->gdef->has_glyph_classes ();
104 HB_OT_LAYOUT_INTERNAL hb_bool_t
105 _hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout)
107 return layout->new_gdef.len > 0;
110 HB_OT_LAYOUT_INTERNAL hb_ot_layout_glyph_properties_t
111 _hb_ot_layout_get_glyph_properties (hb_ot_layout_t *layout,
114 hb_ot_layout_class_t klass;
116 /* TODO old harfbuzz doesn't always parse mark attachments as it says it was
117 * introduced without a version bump, so it may not be safe */
118 klass = layout->gdef->get_mark_attachment_type (glyph);
122 klass = layout->gdef->get_glyph_class (glyph);
124 if (!klass && glyph < layout->new_gdef.len)
125 klass = layout->new_gdef.klasses[glyph];
129 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
130 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
131 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
132 case GDEF::MarkGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
133 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
137 HB_OT_LAYOUT_INTERNAL hb_bool_t
138 _hb_ot_layout_check_glyph_properties (hb_ot_layout_t *layout,
140 hb_ot_layout_lookup_flags_t lookup_flags,
141 hb_ot_layout_glyph_properties_t *property)
143 hb_ot_layout_glyph_class_t basic_glyph_class;
144 hb_ot_layout_glyph_properties_t desired_attachment_class;
146 if (gitem->gproperties == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
148 gitem->gproperties = *property = _hb_ot_layout_get_glyph_properties (layout, gitem->gindex);
149 if (gitem->gproperties == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED)
153 *property = gitem->gproperties;
155 /* If the glyph was found in the MarkAttachmentClass table,
156 * then that class value is the high byte of the result,
157 * otherwise the low byte contains the basic type of the glyph
158 * as defined by the GlyphClassDef table.
160 if (*property & LookupFlag::MarkAttachmentType)
161 basic_glyph_class = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
163 basic_glyph_class = (hb_ot_layout_glyph_class_t) *property;
165 /* Not covered, if, for example, basic_glyph_class
166 * is HB_GDEF_LIGATURE and lookup_flags includes LookupFlags::IgnoreLigatures
168 if (lookup_flags & basic_glyph_class)
171 /* The high byte of lookup_flags has the meaning
172 * "ignore marks of attachment type different than
173 * the attachment type specified."
175 desired_attachment_class = lookup_flags & LookupFlag::MarkAttachmentType;
176 if (desired_attachment_class)
178 if (basic_glyph_class == HB_OT_LAYOUT_GLYPH_CLASS_MARK &&
179 *property != desired_attachment_class )
187 hb_ot_layout_glyph_class_t
188 hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout,
191 hb_ot_layout_glyph_properties_t properties;
192 hb_ot_layout_class_t klass;
194 properties = _hb_ot_layout_get_glyph_properties (layout, glyph);
196 if (properties & 0xFF00)
197 return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
199 return (hb_ot_layout_glyph_class_t) properties;
203 hb_ot_layout_set_glyph_class (hb_ot_layout_t *layout,
205 hb_ot_layout_glyph_class_t klass)
207 /* TODO optimize this, similar to old harfbuzz code for example */
209 hb_ot_layout_class_t gdef_klass;
210 int len = layout->new_gdef.len;
214 unsigned char *new_klasses;
216 new_len = len == 0 ? 120 : 2 * len;
219 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
221 if (G_UNLIKELY (!new_klasses))
224 memset (new_klasses + len, 0, new_len - len);
226 layout->new_gdef.klasses = new_klasses;
227 layout->new_gdef.len = new_len;
232 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
233 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
234 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
235 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
236 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
239 layout->new_gdef.klasses[glyph] = gdef_klass;
244 hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout,
245 uint16_t num_total_glyphs,
247 unsigned char *klasses,
252 if (G_UNLIKELY (!count || !glyphs || !klasses))
255 if (layout->new_gdef.len == 0) {
256 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
257 layout->new_gdef.len = count;
260 for (i = 0; i < count; i++)
261 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
268 static const GSUBGPOS&
269 get_gsubgpos_table (hb_ot_layout_t *layout,
270 hb_ot_layout_table_type_t table_type)
272 switch (table_type) {
273 case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub);
274 case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos);
275 default: return NullGSUBGPOS;
281 hb_ot_layout_table_get_script_count (hb_ot_layout_t *layout,
282 hb_ot_layout_table_type_t table_type)
284 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
286 return g.get_script_count ();
290 hb_ot_layout_table_get_script_tag (hb_ot_layout_t *layout,
291 hb_ot_layout_table_type_t table_type,
292 unsigned int script_index)
294 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
296 return g.get_script_tag (script_index);
300 hb_ot_layout_table_find_script (hb_ot_layout_t *layout,
301 hb_ot_layout_table_type_t table_type,
303 unsigned int *script_index)
305 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
306 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
308 if (g.find_script_index (script_tag, script_index))
311 /* try finding 'DFLT' */
312 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
315 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
316 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
319 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
324 hb_ot_layout_table_get_feature_count (hb_ot_layout_t *layout,
325 hb_ot_layout_table_type_t table_type)
327 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
329 return g.get_feature_count ();
333 hb_ot_layout_table_get_feature_tag (hb_ot_layout_t *layout,
334 hb_ot_layout_table_type_t table_type,
335 unsigned int feature_index)
337 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
339 return g.get_feature_tag (feature_index);
343 hb_ot_layout_table_find_feature (hb_ot_layout_t *layout,
344 hb_ot_layout_table_type_t table_type,
345 hb_tag_t feature_tag,
346 unsigned int *feature_index)
348 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
349 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
351 if (g.find_feature_index (feature_tag, feature_index))
354 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
359 hb_ot_layout_table_get_lookup_count (hb_ot_layout_t *layout,
360 hb_ot_layout_table_type_t table_type)
362 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
364 return g.get_lookup_count ();
369 hb_ot_layout_script_get_language_count (hb_ot_layout_t *layout,
370 hb_ot_layout_table_type_t table_type,
371 unsigned int script_index)
373 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
375 return s.get_lang_sys_count ();
379 hb_ot_layout_script_get_language_tag (hb_ot_layout_t *layout,
380 hb_ot_layout_table_type_t table_type,
381 unsigned int script_index,
382 unsigned int language_index)
384 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
386 return s.get_lang_sys_tag (language_index);
390 hb_ot_layout_script_find_language (hb_ot_layout_t *layout,
391 hb_ot_layout_table_type_t table_type,
392 unsigned int script_index,
393 hb_tag_t language_tag,
394 unsigned int *language_index)
396 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
397 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
399 if (s.find_lang_sys_index (language_tag, language_index))
402 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
403 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
406 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
411 hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t *layout,
412 hb_ot_layout_table_type_t table_type,
413 unsigned int script_index,
414 unsigned int language_index,
415 unsigned int *feature_index)
417 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
419 if (feature_index) *feature_index = l.get_required_feature_index ();
421 return l.has_required_feature ();
425 hb_ot_layout_language_get_feature_count (hb_ot_layout_t *layout,
426 hb_ot_layout_table_type_t table_type,
427 unsigned int script_index,
428 unsigned int language_index)
430 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
432 return l.get_feature_count ();
436 hb_ot_layout_language_get_feature_index (hb_ot_layout_t *layout,
437 hb_ot_layout_table_type_t table_type,
438 unsigned int script_index,
439 unsigned int language_index,
440 unsigned int num_feature)
442 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
443 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
445 return l.get_feature_index (num_feature);
449 hb_ot_layout_language_get_feature_tag (hb_ot_layout_t *layout,
450 hb_ot_layout_table_type_t table_type,
451 unsigned int script_index,
452 unsigned int language_index,
453 unsigned int num_feature)
455 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
456 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
457 unsigned int feature_index = l.get_feature_index (num_feature);
459 return g.get_feature_tag (feature_index);
464 hb_ot_layout_language_find_feature (hb_ot_layout_t *layout,
465 hb_ot_layout_table_type_t table_type,
466 unsigned int script_index,
467 unsigned int language_index,
468 hb_tag_t feature_tag,
469 unsigned int *feature_index)
471 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
472 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
473 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
476 for (i = 0; i < l.get_feature_count (); i++) {
477 unsigned int f_index = l.get_feature_index (i);
479 if (feature_tag == g.get_feature_tag (f_index)) {
480 if (feature_index) *feature_index = f_index;
485 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
490 hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t *layout,
491 hb_ot_layout_table_type_t table_type,
492 unsigned int feature_index)
494 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
495 const Feature &f = g.get_feature (feature_index);
497 return f.get_lookup_count ();
501 hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t *layout,
502 hb_ot_layout_table_type_t table_type,
503 unsigned int feature_index,
504 unsigned int num_lookup)
506 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
507 const Feature &f = g.get_feature (feature_index);
509 return f.get_lookup_index (num_lookup);