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)
101 /* XXX the public class_t is a mess */
104 hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout)
106 return layout->gdef->has_glyph_classes ();
109 HB_OT_LAYOUT_INTERNAL hb_bool_t
110 _hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout)
112 return layout->new_gdef.len > 0;
115 HB_OT_LAYOUT_INTERNAL unsigned int
116 _hb_ot_layout_get_glyph_property (hb_ot_layout_t *layout,
117 hb_codepoint_t glyph)
119 hb_ot_layout_class_t klass;
121 /* TODO old harfbuzz doesn't always parse mark attachments as it says it was
122 * introduced without a version bump, so it may not be safe */
123 klass = layout->gdef->get_mark_attachment_type (glyph);
127 klass = layout->gdef->get_glyph_class (glyph);
129 if (!klass && glyph < layout->new_gdef.len)
130 klass = layout->new_gdef.klasses[glyph];
134 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
135 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
136 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
137 case GDEF::MarkGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
138 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
142 HB_OT_LAYOUT_INTERNAL hb_bool_t
143 _hb_ot_layout_check_glyph_property (hb_ot_layout_t *layout,
145 unsigned int lookup_flags,
146 unsigned int *property)
148 hb_ot_layout_glyph_class_t basic_glyph_class;
149 unsigned int desired_attachment_class;
151 if (gitem->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
153 gitem->gproperty = *property = _hb_ot_layout_get_glyph_property (layout, gitem->gindex);
154 if (gitem->gproperty == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED)
158 *property = gitem->gproperty;
160 /* If the glyph was found in the MarkAttachmentClass table,
161 * then that class value is the high byte of the result,
162 * otherwise the low byte contains the basic type of the glyph
163 * as defined by the GlyphClassDef table.
165 if (*property & LookupFlag::MarkAttachmentType)
166 basic_glyph_class = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
168 basic_glyph_class = (hb_ot_layout_glyph_class_t) *property;
170 /* Not covered, if, for example, basic_glyph_class
171 * is HB_GDEF_LIGATURE and lookup_flags includes LookupFlags::IgnoreLigatures
173 if (lookup_flags & basic_glyph_class)
176 /* The high byte of lookup_flags has the meaning
177 * "ignore marks of attachment type different than
178 * the attachment type specified."
180 desired_attachment_class = lookup_flags & LookupFlag::MarkAttachmentType;
181 if (desired_attachment_class)
183 if (basic_glyph_class == HB_OT_LAYOUT_GLYPH_CLASS_MARK &&
184 *property != desired_attachment_class)
191 HB_OT_LAYOUT_INTERNAL void
192 _hb_ot_layout_set_glyph_property (hb_ot_layout_t *layout,
193 hb_codepoint_t glyph,
194 unsigned int property)
196 hb_ot_layout_glyph_class_t klass;
198 if (property & LookupFlag::MarkAttachmentType)
199 klass = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
201 klass = (hb_ot_layout_glyph_class_t) property;
203 hb_ot_layout_set_glyph_class (layout, glyph, klass);
207 hb_ot_layout_glyph_class_t
208 hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout,
209 hb_codepoint_t glyph)
211 unsigned int property;
212 hb_ot_layout_class_t klass;
214 property = _hb_ot_layout_get_glyph_property (layout, glyph);
216 if (property & LookupFlag::MarkAttachmentType)
217 return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
219 return (hb_ot_layout_glyph_class_t) property;
223 hb_ot_layout_set_glyph_class (hb_ot_layout_t *layout,
224 hb_codepoint_t glyph,
225 hb_ot_layout_glyph_class_t klass)
227 /* TODO optimize this, similar to old harfbuzz code for example */
229 hb_ot_layout_class_t gdef_klass;
230 int len = layout->new_gdef.len;
232 if (G_UNLIKELY (glyph > 65535))
237 unsigned char *new_klasses;
239 new_len = len == 0 ? 120 : 2 * len;
242 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
244 if (G_UNLIKELY (!new_klasses))
247 memset (new_klasses + len, 0, new_len - len);
249 layout->new_gdef.klasses = new_klasses;
250 layout->new_gdef.len = new_len;
255 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
256 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
257 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
258 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
259 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
262 layout->new_gdef.klasses[glyph] = gdef_klass;
267 hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout,
268 uint16_t num_total_glyphs,
269 hb_codepoint_t *glyphs,
270 unsigned char *klasses,
275 if (G_UNLIKELY (!count || !glyphs || !klasses))
278 if (layout->new_gdef.len == 0) {
279 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
280 layout->new_gdef.len = count;
283 for (i = 0; i < count; i++)
284 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
291 static const GSUBGPOS&
292 get_gsubgpos_table (hb_ot_layout_t *layout,
293 hb_ot_layout_table_type_t table_type)
295 switch (table_type) {
296 case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub);
297 case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos);
298 default: return NullGSUBGPOS;
304 hb_ot_layout_table_get_script_count (hb_ot_layout_t *layout,
305 hb_ot_layout_table_type_t table_type)
307 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
309 return g.get_script_count ();
313 hb_ot_layout_table_get_script_tag (hb_ot_layout_t *layout,
314 hb_ot_layout_table_type_t table_type,
315 unsigned int script_index)
317 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
319 return g.get_script_tag (script_index);
323 hb_ot_layout_table_find_script (hb_ot_layout_t *layout,
324 hb_ot_layout_table_type_t table_type,
326 unsigned int *script_index)
328 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
329 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
331 if (g.find_script_index (script_tag, script_index))
334 /* try finding 'DFLT' */
335 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
338 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
339 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
342 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
347 hb_ot_layout_table_get_feature_count (hb_ot_layout_t *layout,
348 hb_ot_layout_table_type_t table_type)
350 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
352 return g.get_feature_count ();
356 hb_ot_layout_table_get_feature_tag (hb_ot_layout_t *layout,
357 hb_ot_layout_table_type_t table_type,
358 unsigned int feature_index)
360 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
362 return g.get_feature_tag (feature_index);
366 hb_ot_layout_table_find_feature (hb_ot_layout_t *layout,
367 hb_ot_layout_table_type_t table_type,
368 hb_tag_t feature_tag,
369 unsigned int *feature_index)
371 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
372 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
374 if (g.find_feature_index (feature_tag, feature_index))
377 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
382 hb_ot_layout_table_get_lookup_count (hb_ot_layout_t *layout,
383 hb_ot_layout_table_type_t table_type)
385 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
387 return g.get_lookup_count ();
392 hb_ot_layout_script_get_language_count (hb_ot_layout_t *layout,
393 hb_ot_layout_table_type_t table_type,
394 unsigned int script_index)
396 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
398 return s.get_lang_sys_count ();
402 hb_ot_layout_script_get_language_tag (hb_ot_layout_t *layout,
403 hb_ot_layout_table_type_t table_type,
404 unsigned int script_index,
405 unsigned int language_index)
407 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
409 return s.get_lang_sys_tag (language_index);
413 hb_ot_layout_script_find_language (hb_ot_layout_t *layout,
414 hb_ot_layout_table_type_t table_type,
415 unsigned int script_index,
416 hb_tag_t language_tag,
417 unsigned int *language_index)
419 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
420 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
422 if (s.find_lang_sys_index (language_tag, language_index))
425 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
426 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
429 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
434 hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t *layout,
435 hb_ot_layout_table_type_t table_type,
436 unsigned int script_index,
437 unsigned int language_index,
438 unsigned int *feature_index)
440 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
442 if (feature_index) *feature_index = l.get_required_feature_index ();
444 return l.has_required_feature ();
448 hb_ot_layout_language_get_feature_count (hb_ot_layout_t *layout,
449 hb_ot_layout_table_type_t table_type,
450 unsigned int script_index,
451 unsigned int language_index)
453 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
455 return l.get_feature_count ();
459 hb_ot_layout_language_get_feature_index (hb_ot_layout_t *layout,
460 hb_ot_layout_table_type_t table_type,
461 unsigned int script_index,
462 unsigned int language_index,
463 unsigned int num_feature)
465 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
466 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
468 return l.get_feature_index (num_feature);
472 hb_ot_layout_language_get_feature_tag (hb_ot_layout_t *layout,
473 hb_ot_layout_table_type_t table_type,
474 unsigned int script_index,
475 unsigned int language_index,
476 unsigned int num_feature)
478 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
479 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
480 unsigned int feature_index = l.get_feature_index (num_feature);
482 return g.get_feature_tag (feature_index);
487 hb_ot_layout_language_find_feature (hb_ot_layout_t *layout,
488 hb_ot_layout_table_type_t table_type,
489 unsigned int script_index,
490 unsigned int language_index,
491 hb_tag_t feature_tag,
492 unsigned int *feature_index)
494 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
495 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
496 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
499 for (i = 0; i < l.get_feature_count (); i++) {
500 unsigned int f_index = l.get_feature_index (i);
502 if (feature_tag == g.get_feature_tag (f_index)) {
503 if (feature_index) *feature_index = f_index;
508 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
513 hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t *layout,
514 hb_ot_layout_table_type_t table_type,
515 unsigned int feature_index)
517 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
518 const Feature &f = g.get_feature (feature_index);
520 return f.get_lookup_count ();
524 hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t *layout,
525 hb_ot_layout_table_type_t table_type,
526 unsigned int feature_index,
527 unsigned int num_lookup)
529 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
530 const Feature &f = g.get_feature (feature_index);
532 return f.get_lookup_index (num_lookup);
540 hb_ot_layout_substitute_lookup (hb_ot_layout_t *layout,
542 unsigned int lookup_index,
543 hb_ot_layout_feature_mask_t mask)
545 const GSUB &gsub = *(layout->gsub);
546 const SubstLookup &l = gsub.get_lookup (lookup_index);
548 return l.substitute_string (layout, buffer, mask);
553 /* XXX dupped, until he old code can be removed */
556 hb_buffer_duplicate_out_buffer( HB_Buffer buffer )
558 if ( !buffer->alt_string )
562 if ( ALLOC_ARRAY( buffer->alt_string, buffer->allocated, HB_GlyphItemRec ) )
566 buffer->out_string = buffer->alt_string;
567 memcpy( buffer->out_string, buffer->in_string, buffer->out_length * sizeof (buffer->out_string[0]) );
568 buffer->separate_out = TRUE;
576 _hb_buffer_add_output_glyph_ids( HB_Buffer buffer,
579 const GlyphID *glyph_data,
588 error = hb_buffer_ensure( buffer, buffer->out_pos + num_out );
592 if ( !buffer->separate_out )
594 error = hb_buffer_duplicate_out_buffer( buffer );
599 properties = buffer->in_string[buffer->in_pos].properties;
600 cluster = buffer->in_string[buffer->in_pos].cluster;
601 if ( component == 0xFFFF )
602 component = buffer->in_string[buffer->in_pos].component;
603 if ( ligID == 0xFFFF )
604 ligID = buffer->in_string[buffer->in_pos].ligID;
606 for ( i = 0; i < num_out; i++ )
608 HB_GlyphItem item = &buffer->out_string[buffer->out_pos + i];
610 item->gindex = glyph_data[i];
611 item->properties = properties;
612 item->cluster = cluster;
613 item->component = component;
615 item->gproperty = HB_GLYPH_PROPERTY_UNKNOWN;
618 buffer->in_pos += num_in;
619 buffer->out_pos += num_out;
621 buffer->out_length = buffer->out_pos;