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.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"
37 #include "hb-ot-layout-gpos-private.h"
40 #include "harfbuzz-buffer-private.h"
47 hb_ot_layout_create (void)
49 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
51 layout->gdef = &Null(GDEF);
52 layout->gsub = &Null(GSUB);
53 layout->gpos = &Null(GPOS);
59 hb_ot_layout_create_for_data (const char *font_data,
62 hb_ot_layout_t *layout;
64 if (HB_UNLIKELY (font_data == NULL))
65 return hb_ot_layout_create ();
67 layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
69 const OpenTypeFontFile &font = OpenTypeFontFile::get_for_data (font_data);
70 const OpenTypeFontFace &face = font.get_face (face_index);
72 layout->gdef = &GDEF::get_for_data (font.get_table_data (face.get_table_by_tag (GDEF::Tag)));
73 layout->gsub = &GSUB::get_for_data (font.get_table_data (face.get_table_by_tag (GSUB::Tag)));
74 layout->gpos = &GPOS::get_for_data (font.get_table_data (face.get_table_by_tag (GPOS::Tag)));
80 hb_ot_layout_destroy (hb_ot_layout_t *layout)
86 hb_ot_layout_set_direction (hb_ot_layout_t *layout,
89 layout->gpos_info.r2l = !!r2l;
96 /* TODO the public class_t is a mess */
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 unsigned int
111 _hb_ot_layout_get_glyph_property (hb_ot_layout_t *layout,
112 hb_codepoint_t glyph)
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_property (hb_ot_layout_t *layout,
140 unsigned int lookup_flags,
141 unsigned int *property)
143 hb_ot_layout_glyph_class_t basic_glyph_class;
144 unsigned int desired_attachment_class;
146 if (gitem->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
148 gitem->gproperty = *property = _hb_ot_layout_get_glyph_property (layout, gitem->gindex);
149 if (gitem->gproperty == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED)
153 *property = gitem->gproperty;
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)
186 HB_OT_LAYOUT_INTERNAL void
187 _hb_ot_layout_set_glyph_property (hb_ot_layout_t *layout,
188 hb_codepoint_t glyph,
189 unsigned int property)
191 hb_ot_layout_glyph_class_t klass;
193 if (property & LookupFlag::MarkAttachmentType)
194 klass = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
196 klass = (hb_ot_layout_glyph_class_t) property;
198 hb_ot_layout_set_glyph_class (layout, glyph, klass);
202 hb_ot_layout_glyph_class_t
203 hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout,
204 hb_codepoint_t glyph)
206 unsigned int property;
207 hb_ot_layout_class_t klass;
209 property = _hb_ot_layout_get_glyph_property (layout, glyph);
211 if (property & LookupFlag::MarkAttachmentType)
212 return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
214 return (hb_ot_layout_glyph_class_t) property;
218 hb_ot_layout_set_glyph_class (hb_ot_layout_t *layout,
219 hb_codepoint_t glyph,
220 hb_ot_layout_glyph_class_t klass)
222 /* TODO optimize this, similar to old harfbuzz code for example */
224 hb_ot_layout_class_t gdef_klass;
225 int len = layout->new_gdef.len;
227 if (HB_UNLIKELY (glyph > 65535))
232 unsigned char *new_klasses;
234 new_len = len == 0 ? 120 : 2 * len;
237 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
239 if (HB_UNLIKELY (!new_klasses))
242 memset (new_klasses + len, 0, new_len - len);
244 layout->new_gdef.klasses = new_klasses;
245 layout->new_gdef.len = new_len;
250 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
251 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
252 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
253 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
254 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
257 layout->new_gdef.klasses[glyph] = gdef_klass;
262 hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout,
263 uint16_t num_total_glyphs,
264 hb_codepoint_t *glyphs,
265 unsigned char *klasses,
268 if (HB_UNLIKELY (!count || !glyphs || !klasses))
271 if (layout->new_gdef.len == 0) {
272 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
273 layout->new_gdef.len = count;
276 for (unsigned int i = 0; i < count; i++)
277 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
284 static const GSUBGPOS&
285 get_gsubgpos_table (hb_ot_layout_t *layout,
286 hb_ot_layout_table_type_t table_type)
288 switch (table_type) {
289 case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub);
290 case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos);
291 default: return Null(GSUBGPOS);
297 hb_ot_layout_table_get_script_count (hb_ot_layout_t *layout,
298 hb_ot_layout_table_type_t table_type)
300 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
302 return g.get_script_count ();
306 hb_ot_layout_table_get_script_tag (hb_ot_layout_t *layout,
307 hb_ot_layout_table_type_t table_type,
308 unsigned int script_index)
310 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
312 return g.get_script_tag (script_index);
316 hb_ot_layout_table_find_script (hb_ot_layout_t *layout,
317 hb_ot_layout_table_type_t table_type,
319 unsigned int *script_index)
321 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
322 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
324 if (g.find_script_index (script_tag, script_index))
327 /* try finding 'DFLT' */
328 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
331 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
332 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
335 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
340 hb_ot_layout_table_get_feature_count (hb_ot_layout_t *layout,
341 hb_ot_layout_table_type_t table_type)
343 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
345 return g.get_feature_count ();
349 hb_ot_layout_table_get_feature_tag (hb_ot_layout_t *layout,
350 hb_ot_layout_table_type_t table_type,
351 unsigned int feature_index)
353 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
355 return g.get_feature_tag (feature_index);
359 hb_ot_layout_table_find_feature (hb_ot_layout_t *layout,
360 hb_ot_layout_table_type_t table_type,
361 hb_tag_t feature_tag,
362 unsigned int *feature_index)
364 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
365 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
367 if (g.find_feature_index (feature_tag, feature_index))
370 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
375 hb_ot_layout_table_get_lookup_count (hb_ot_layout_t *layout,
376 hb_ot_layout_table_type_t table_type)
378 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
380 return g.get_lookup_count ();
385 hb_ot_layout_script_get_language_count (hb_ot_layout_t *layout,
386 hb_ot_layout_table_type_t table_type,
387 unsigned int script_index)
389 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
391 return s.get_lang_sys_count ();
395 hb_ot_layout_script_get_language_tag (hb_ot_layout_t *layout,
396 hb_ot_layout_table_type_t table_type,
397 unsigned int script_index,
398 unsigned int language_index)
400 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
402 return s.get_lang_sys_tag (language_index);
406 hb_ot_layout_script_find_language (hb_ot_layout_t *layout,
407 hb_ot_layout_table_type_t table_type,
408 unsigned int script_index,
409 hb_tag_t language_tag,
410 unsigned int *language_index)
412 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
413 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
415 if (s.find_lang_sys_index (language_tag, language_index))
418 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
419 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
422 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
427 hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t *layout,
428 hb_ot_layout_table_type_t table_type,
429 unsigned int script_index,
430 unsigned int language_index,
431 unsigned int *feature_index)
433 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
435 if (feature_index) *feature_index = l.get_required_feature_index ();
437 return l.has_required_feature ();
441 hb_ot_layout_language_get_feature_count (hb_ot_layout_t *layout,
442 hb_ot_layout_table_type_t table_type,
443 unsigned int script_index,
444 unsigned int language_index)
446 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
448 return l.get_feature_count ();
452 hb_ot_layout_language_get_feature_index (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);
461 return l.get_feature_index (num_feature);
465 hb_ot_layout_language_get_feature_tag (hb_ot_layout_t *layout,
466 hb_ot_layout_table_type_t table_type,
467 unsigned int script_index,
468 unsigned int language_index,
469 unsigned int num_feature)
471 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
472 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
473 unsigned int feature_index = l.get_feature_index (num_feature);
475 return g.get_feature_tag (feature_index);
480 hb_ot_layout_language_find_feature (hb_ot_layout_t *layout,
481 hb_ot_layout_table_type_t table_type,
482 unsigned int script_index,
483 unsigned int language_index,
484 hb_tag_t feature_tag,
485 unsigned int *feature_index)
487 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
488 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
489 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
491 unsigned int num_features = l.get_feature_count ();
492 for (unsigned int i = 0; i < num_features; i++) {
493 unsigned int f_index = l.get_feature_index (i);
495 if (feature_tag == g.get_feature_tag (f_index)) {
496 if (feature_index) *feature_index = f_index;
501 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
506 hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t *layout,
507 hb_ot_layout_table_type_t table_type,
508 unsigned int feature_index)
510 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
511 const Feature &f = g.get_feature (feature_index);
513 return f.get_lookup_count ();
517 hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t *layout,
518 hb_ot_layout_table_type_t table_type,
519 unsigned int feature_index,
520 unsigned int num_lookup)
522 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
523 const Feature &f = g.get_feature (feature_index);
525 return f.get_lookup_index (num_lookup);
533 hb_ot_layout_substitute_lookup (hb_ot_layout_t *layout,
535 unsigned int lookup_index,
536 hb_ot_layout_feature_mask_t mask)
538 return layout->gsub->substitute_lookup (layout, buffer, lookup_index, mask);
546 hb_ot_layout_position_lookup (hb_ot_layout_t *layout,
548 unsigned int lookup_index,
549 hb_ot_layout_feature_mask_t mask)
551 return layout->gpos->position_lookup (layout, buffer, lookup_index, mask);
556 /* TODO dupped, until he old code can be removed */
559 hb_buffer_duplicate_out_buffer( HB_Buffer buffer )
561 if ( !buffer->alt_string )
565 if ( ALLOC_ARRAY( buffer->alt_string, buffer->allocated, HB_GlyphItemRec ) )
569 buffer->out_string = buffer->alt_string;
570 memcpy( buffer->out_string, buffer->in_string, buffer->out_length * sizeof (buffer->out_string[0]) );
571 buffer->separate_out = TRUE;
579 _hb_buffer_add_output_glyph_ids( HB_Buffer buffer,
582 const GlyphID *glyph_data,
591 error = hb_buffer_ensure( buffer, buffer->out_pos + num_out );
595 if ( !buffer->separate_out )
597 error = hb_buffer_duplicate_out_buffer( buffer );
602 properties = buffer->in_string[buffer->in_pos].properties;
603 cluster = buffer->in_string[buffer->in_pos].cluster;
604 if ( component == 0xFFFF )
605 component = buffer->in_string[buffer->in_pos].component;
606 if ( ligID == 0xFFFF )
607 ligID = buffer->in_string[buffer->in_pos].ligID;
609 for ( i = 0; i < num_out; i++ )
611 HB_GlyphItem item = &buffer->out_string[buffer->out_pos + i];
613 item->gindex = glyph_data[i];
614 item->properties = properties;
615 item->cluster = cluster;
616 item->component = component;
618 item->gproperty = HB_GLYPH_PROPERTY_UNKNOWN;
621 buffer->in_pos += num_in;
622 buffer->out_pos += num_out;
624 buffer->out_length = buffer->out_pos;