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"
39 #include "harfbuzz-buffer-private.h"
45 struct _hb_ot_layout_t {
48 const /*XXX*/GSUBGPOS *gpos;
51 unsigned char *klasses;
55 /* TODO add max-nesting-level here? */
59 hb_ot_layout_create (void)
61 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
63 layout->gdef = &NullGDEF;
64 layout->gsub = &NullGSUB;
65 layout->gpos = &/*XXX*/NullGSUBGPOS;
71 hb_ot_layout_create_for_data (const char *font_data,
74 hb_ot_layout_t *layout;
76 if (HB_UNLIKELY (font_data == NULL))
77 return hb_ot_layout_create ();
79 layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
81 const OpenTypeFontFile &font = OpenTypeFontFile::get_for_data (font_data);
82 const OpenTypeFontFace &face = font.get_face (face_index);
84 layout->gdef = &GDEF::get_for_data (font.get_table_data (face.get_table_by_tag (GDEF::Tag)));
85 layout->gsub = &GSUB::get_for_data (font.get_table_data (face.get_table_by_tag (GSUB::Tag)));
86 layout->gpos = &/*XXX*/GSUBGPOS::get_for_data (font.get_table_data (face.get_table_by_tag (/*XXX*/GSUBGPOS::GPOSTag)));
92 hb_ot_layout_destroy (hb_ot_layout_t *layout)
102 hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout)
104 return layout->gdef->has_glyph_classes ();
107 HB_OT_LAYOUT_INTERNAL hb_bool_t
108 _hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout)
110 return layout->new_gdef.len > 0;
113 HB_OT_LAYOUT_INTERNAL hb_ot_layout_glyph_properties_t
114 _hb_ot_layout_get_glyph_properties (hb_ot_layout_t *layout,
115 hb_codepoint_t glyph)
117 hb_ot_layout_class_t klass;
119 /* TODO old harfbuzz doesn't always parse mark attachments as it says it was
120 * introduced without a version bump, so it may not be safe */
121 klass = layout->gdef->get_mark_attachment_type (glyph);
125 klass = layout->gdef->get_glyph_class (glyph);
127 if (!klass && glyph < layout->new_gdef.len)
128 klass = layout->new_gdef.klasses[glyph];
132 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
133 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
134 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
135 case GDEF::MarkGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
136 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
140 HB_OT_LAYOUT_INTERNAL hb_bool_t
141 _hb_ot_layout_check_glyph_properties (hb_ot_layout_t *layout,
143 hb_ot_layout_lookup_flags_t lookup_flags,
144 hb_ot_layout_glyph_properties_t *property)
146 hb_ot_layout_glyph_class_t basic_glyph_class;
147 hb_ot_layout_glyph_properties_t desired_attachment_class;
149 if (gitem->gproperties == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
151 gitem->gproperties = *property = _hb_ot_layout_get_glyph_properties (layout, gitem->gindex);
152 if (gitem->gproperties == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED)
156 *property = gitem->gproperties;
158 /* If the glyph was found in the MarkAttachmentClass table,
159 * then that class value is the high byte of the result,
160 * otherwise the low byte contains the basic type of the glyph
161 * as defined by the GlyphClassDef table.
163 if (*property & LookupFlag::MarkAttachmentType)
164 basic_glyph_class = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
166 basic_glyph_class = (hb_ot_layout_glyph_class_t) *property;
168 /* Not covered, if, for example, basic_glyph_class
169 * is HB_GDEF_LIGATURE and lookup_flags includes LookupFlags::IgnoreLigatures
171 if (lookup_flags & basic_glyph_class)
174 /* The high byte of lookup_flags has the meaning
175 * "ignore marks of attachment type different than
176 * the attachment type specified."
178 desired_attachment_class = lookup_flags & LookupFlag::MarkAttachmentType;
179 if (desired_attachment_class)
181 if (basic_glyph_class == HB_OT_LAYOUT_GLYPH_CLASS_MARK &&
182 *property != desired_attachment_class )
190 hb_ot_layout_glyph_class_t
191 hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout,
192 hb_codepoint_t glyph)
194 hb_ot_layout_glyph_properties_t properties;
195 hb_ot_layout_class_t klass;
197 properties = _hb_ot_layout_get_glyph_properties (layout, glyph);
199 if (properties & 0xFF00)
200 return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
202 return (hb_ot_layout_glyph_class_t) properties;
206 hb_ot_layout_set_glyph_class (hb_ot_layout_t *layout,
207 hb_codepoint_t glyph,
208 hb_ot_layout_glyph_class_t klass)
210 /* TODO optimize this, similar to old harfbuzz code for example */
212 hb_ot_layout_class_t gdef_klass;
213 int len = layout->new_gdef.len;
217 unsigned char *new_klasses;
219 new_len = len == 0 ? 120 : 2 * len;
222 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
224 if (G_UNLIKELY (!new_klasses))
227 memset (new_klasses + len, 0, new_len - len);
229 layout->new_gdef.klasses = new_klasses;
230 layout->new_gdef.len = new_len;
235 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
236 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
237 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
238 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
239 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
242 layout->new_gdef.klasses[glyph] = gdef_klass;
247 hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout,
248 uint16_t num_total_glyphs,
249 hb_codepoint_t *glyphs,
250 unsigned char *klasses,
255 if (G_UNLIKELY (!count || !glyphs || !klasses))
258 if (layout->new_gdef.len == 0) {
259 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
260 layout->new_gdef.len = count;
263 for (i = 0; i < count; i++)
264 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
271 static const GSUBGPOS&
272 get_gsubgpos_table (hb_ot_layout_t *layout,
273 hb_ot_layout_table_type_t table_type)
275 switch (table_type) {
276 case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub);
277 case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos);
278 default: return NullGSUBGPOS;
284 hb_ot_layout_table_get_script_count (hb_ot_layout_t *layout,
285 hb_ot_layout_table_type_t table_type)
287 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
289 return g.get_script_count ();
293 hb_ot_layout_table_get_script_tag (hb_ot_layout_t *layout,
294 hb_ot_layout_table_type_t table_type,
295 unsigned int script_index)
297 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
299 return g.get_script_tag (script_index);
303 hb_ot_layout_table_find_script (hb_ot_layout_t *layout,
304 hb_ot_layout_table_type_t table_type,
306 unsigned int *script_index)
308 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
309 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
311 if (g.find_script_index (script_tag, script_index))
314 /* try finding 'DFLT' */
315 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
318 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
319 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
322 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
327 hb_ot_layout_table_get_feature_count (hb_ot_layout_t *layout,
328 hb_ot_layout_table_type_t table_type)
330 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
332 return g.get_feature_count ();
336 hb_ot_layout_table_get_feature_tag (hb_ot_layout_t *layout,
337 hb_ot_layout_table_type_t table_type,
338 unsigned int feature_index)
340 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
342 return g.get_feature_tag (feature_index);
346 hb_ot_layout_table_find_feature (hb_ot_layout_t *layout,
347 hb_ot_layout_table_type_t table_type,
348 hb_tag_t feature_tag,
349 unsigned int *feature_index)
351 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
352 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
354 if (g.find_feature_index (feature_tag, feature_index))
357 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
362 hb_ot_layout_table_get_lookup_count (hb_ot_layout_t *layout,
363 hb_ot_layout_table_type_t table_type)
365 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
367 return g.get_lookup_count ();
372 hb_ot_layout_script_get_language_count (hb_ot_layout_t *layout,
373 hb_ot_layout_table_type_t table_type,
374 unsigned int script_index)
376 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
378 return s.get_lang_sys_count ();
382 hb_ot_layout_script_get_language_tag (hb_ot_layout_t *layout,
383 hb_ot_layout_table_type_t table_type,
384 unsigned int script_index,
385 unsigned int language_index)
387 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
389 return s.get_lang_sys_tag (language_index);
393 hb_ot_layout_script_find_language (hb_ot_layout_t *layout,
394 hb_ot_layout_table_type_t table_type,
395 unsigned int script_index,
396 hb_tag_t language_tag,
397 unsigned int *language_index)
399 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
400 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
402 if (s.find_lang_sys_index (language_tag, language_index))
405 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
406 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
409 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
414 hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t *layout,
415 hb_ot_layout_table_type_t table_type,
416 unsigned int script_index,
417 unsigned int language_index,
418 unsigned int *feature_index)
420 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
422 if (feature_index) *feature_index = l.get_required_feature_index ();
424 return l.has_required_feature ();
428 hb_ot_layout_language_get_feature_count (hb_ot_layout_t *layout,
429 hb_ot_layout_table_type_t table_type,
430 unsigned int script_index,
431 unsigned int language_index)
433 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
435 return l.get_feature_count ();
439 hb_ot_layout_language_get_feature_index (hb_ot_layout_t *layout,
440 hb_ot_layout_table_type_t table_type,
441 unsigned int script_index,
442 unsigned int language_index,
443 unsigned int num_feature)
445 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
446 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
448 return l.get_feature_index (num_feature);
452 hb_ot_layout_language_get_feature_tag (hb_ot_layout_t *layout,
453 hb_ot_layout_table_type_t table_type,
454 unsigned int script_index,
455 unsigned int language_index,
456 unsigned int num_feature)
458 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
459 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
460 unsigned int feature_index = l.get_feature_index (num_feature);
462 return g.get_feature_tag (feature_index);
467 hb_ot_layout_language_find_feature (hb_ot_layout_t *layout,
468 hb_ot_layout_table_type_t table_type,
469 unsigned int script_index,
470 unsigned int language_index,
471 hb_tag_t feature_tag,
472 unsigned int *feature_index)
474 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
475 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
476 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
479 for (i = 0; i < l.get_feature_count (); i++) {
480 unsigned int f_index = l.get_feature_index (i);
482 if (feature_tag == g.get_feature_tag (f_index)) {
483 if (feature_index) *feature_index = f_index;
488 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
493 hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t *layout,
494 hb_ot_layout_table_type_t table_type,
495 unsigned int feature_index)
497 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
498 const Feature &f = g.get_feature (feature_index);
500 return f.get_lookup_count ();
504 hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t *layout,
505 hb_ot_layout_table_type_t table_type,
506 unsigned int feature_index,
507 unsigned int num_lookup)
509 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
510 const Feature &f = g.get_feature (feature_index);
512 return f.get_lookup_index (num_lookup);
520 hb_ot_layout_substitute_lookup (hb_ot_layout_t *layout,
522 unsigned int lookup_index,
523 hb_ot_layout_feature_mask_t mask)
525 const GSUB &gsub = *(layout->gsub);
526 const SubstLookup &l = gsub.get_lookup (lookup_index);
527 unsigned int lookup_type = l.get_type ();
528 unsigned int nesting_level_left = HB_OT_LAYOUT_MAX_NESTING_LEVEL;
529 unsigned int context_length = NO_CONTEXT;
530 bool handled, ret = false;
532 if (!l.is_reverse ()) {
534 /* in/out forward substitution */
535 _hb_buffer_clear_output (buffer);
537 while (buffer->in_pos < buffer->in_length) {
539 if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
540 l.substitute (layout, buffer, context_length, nesting_level_left))
543 _hb_buffer_copy_output_glyph (buffer);
546 _hb_buffer_swap (buffer);
550 /* in-place backward substitution */
551 buffer->in_pos = buffer->in_length - 1;
554 if ((~IN_PROPERTIES (buffer->in_pos) & mask) &&
555 l.substitute (layout, buffer, context_length, nesting_level_left))
560 } while (buffer->in_pos);