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)
89 /* TODO the public class_t is a mess */
92 hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout)
94 return layout->gdef->has_glyph_classes ();
97 HB_OT_LAYOUT_INTERNAL hb_bool_t
98 _hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout)
100 return layout->new_gdef.len > 0;
103 HB_OT_LAYOUT_INTERNAL unsigned int
104 _hb_ot_layout_get_glyph_property (hb_ot_layout_t *layout,
105 hb_codepoint_t glyph)
107 hb_ot_layout_class_t klass;
109 /* TODO old harfbuzz doesn't always parse mark attachments as it says it was
110 * introduced without a version bump, so it may not be safe */
111 klass = layout->gdef->get_mark_attachment_type (glyph);
115 klass = layout->gdef->get_glyph_class (glyph);
117 if (!klass && glyph < layout->new_gdef.len)
118 klass = layout->new_gdef.klasses[glyph];
122 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
123 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
124 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
125 case GDEF::MarkGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
126 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
130 HB_OT_LAYOUT_INTERNAL hb_bool_t
131 _hb_ot_layout_check_glyph_property (hb_ot_layout_t *layout,
133 unsigned int lookup_flags,
134 unsigned int *property)
136 hb_ot_layout_glyph_class_t basic_glyph_class;
137 unsigned int desired_attachment_class;
139 if (gitem->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
141 gitem->gproperty = *property = _hb_ot_layout_get_glyph_property (layout, gitem->gindex);
142 if (gitem->gproperty == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED)
146 *property = gitem->gproperty;
148 /* If the glyph was found in the MarkAttachmentClass table,
149 * then that class value is the high byte of the result,
150 * otherwise the low byte contains the basic type of the glyph
151 * as defined by the GlyphClassDef table.
153 if (*property & LookupFlag::MarkAttachmentType)
154 basic_glyph_class = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
156 basic_glyph_class = (hb_ot_layout_glyph_class_t) *property;
158 /* Not covered, if, for example, basic_glyph_class
159 * is HB_GDEF_LIGATURE and lookup_flags includes LookupFlags::IgnoreLigatures
161 if (lookup_flags & basic_glyph_class)
164 /* The high byte of lookup_flags has the meaning
165 * "ignore marks of attachment type different than
166 * the attachment type specified."
168 desired_attachment_class = lookup_flags & LookupFlag::MarkAttachmentType;
169 if (desired_attachment_class)
171 if (basic_glyph_class == HB_OT_LAYOUT_GLYPH_CLASS_MARK &&
172 *property != desired_attachment_class)
179 HB_OT_LAYOUT_INTERNAL void
180 _hb_ot_layout_set_glyph_property (hb_ot_layout_t *layout,
181 hb_codepoint_t glyph,
182 unsigned int property)
184 hb_ot_layout_glyph_class_t klass;
186 if (property & LookupFlag::MarkAttachmentType)
187 klass = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
189 klass = (hb_ot_layout_glyph_class_t) property;
191 hb_ot_layout_set_glyph_class (layout, glyph, klass);
195 hb_ot_layout_glyph_class_t
196 hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout,
197 hb_codepoint_t glyph)
199 unsigned int property;
200 hb_ot_layout_class_t klass;
202 property = _hb_ot_layout_get_glyph_property (layout, glyph);
204 if (property & LookupFlag::MarkAttachmentType)
205 return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
207 return (hb_ot_layout_glyph_class_t) property;
211 hb_ot_layout_set_glyph_class (hb_ot_layout_t *layout,
212 hb_codepoint_t glyph,
213 hb_ot_layout_glyph_class_t klass)
215 /* TODO optimize this, similar to old harfbuzz code for example */
217 hb_ot_layout_class_t gdef_klass;
218 int len = layout->new_gdef.len;
220 if (HB_UNLIKELY (glyph > 65535))
225 unsigned char *new_klasses;
227 new_len = len == 0 ? 120 : 2 * len;
230 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
232 if (HB_UNLIKELY (!new_klasses))
235 memset (new_klasses + len, 0, new_len - len);
237 layout->new_gdef.klasses = new_klasses;
238 layout->new_gdef.len = new_len;
243 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
244 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
245 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
246 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
247 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
250 layout->new_gdef.klasses[glyph] = gdef_klass;
255 hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout,
256 uint16_t num_total_glyphs,
257 hb_codepoint_t *glyphs,
258 unsigned char *klasses,
261 if (HB_UNLIKELY (!count || !glyphs || !klasses))
264 if (layout->new_gdef.len == 0) {
265 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
266 layout->new_gdef.len = count;
269 for (unsigned int i = 0; i < count; i++)
270 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
277 static const GSUBGPOS&
278 get_gsubgpos_table (hb_ot_layout_t *layout,
279 hb_ot_layout_table_type_t table_type)
281 switch (table_type) {
282 case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub);
283 case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos);
284 default: return Null(GSUBGPOS);
290 hb_ot_layout_table_get_script_count (hb_ot_layout_t *layout,
291 hb_ot_layout_table_type_t table_type)
293 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
295 return g.get_script_count ();
299 hb_ot_layout_table_get_script_tag (hb_ot_layout_t *layout,
300 hb_ot_layout_table_type_t table_type,
301 unsigned int script_index)
303 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
305 return g.get_script_tag (script_index);
309 hb_ot_layout_table_find_script (hb_ot_layout_t *layout,
310 hb_ot_layout_table_type_t table_type,
312 unsigned int *script_index)
314 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
315 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
317 if (g.find_script_index (script_tag, script_index))
320 /* try finding 'DFLT' */
321 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
324 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
325 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
328 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
333 hb_ot_layout_table_get_feature_count (hb_ot_layout_t *layout,
334 hb_ot_layout_table_type_t table_type)
336 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
338 return g.get_feature_count ();
342 hb_ot_layout_table_get_feature_tag (hb_ot_layout_t *layout,
343 hb_ot_layout_table_type_t table_type,
344 unsigned int feature_index)
346 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
348 return g.get_feature_tag (feature_index);
352 hb_ot_layout_table_find_feature (hb_ot_layout_t *layout,
353 hb_ot_layout_table_type_t table_type,
354 hb_tag_t feature_tag,
355 unsigned int *feature_index)
357 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
358 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
360 if (g.find_feature_index (feature_tag, feature_index))
363 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
368 hb_ot_layout_table_get_lookup_count (hb_ot_layout_t *layout,
369 hb_ot_layout_table_type_t table_type)
371 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
373 return g.get_lookup_count ();
378 hb_ot_layout_script_get_language_count (hb_ot_layout_t *layout,
379 hb_ot_layout_table_type_t table_type,
380 unsigned int script_index)
382 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
384 return s.get_lang_sys_count ();
388 hb_ot_layout_script_get_language_tag (hb_ot_layout_t *layout,
389 hb_ot_layout_table_type_t table_type,
390 unsigned int script_index,
391 unsigned int language_index)
393 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
395 return s.get_lang_sys_tag (language_index);
399 hb_ot_layout_script_find_language (hb_ot_layout_t *layout,
400 hb_ot_layout_table_type_t table_type,
401 unsigned int script_index,
402 hb_tag_t language_tag,
403 unsigned int *language_index)
405 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
406 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
408 if (s.find_lang_sys_index (language_tag, language_index))
411 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
412 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
415 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
420 hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t *layout,
421 hb_ot_layout_table_type_t table_type,
422 unsigned int script_index,
423 unsigned int language_index,
424 unsigned int *feature_index)
426 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
428 if (feature_index) *feature_index = l.get_required_feature_index ();
430 return l.has_required_feature ();
434 hb_ot_layout_language_get_feature_count (hb_ot_layout_t *layout,
435 hb_ot_layout_table_type_t table_type,
436 unsigned int script_index,
437 unsigned int language_index)
439 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
441 return l.get_feature_count ();
445 hb_ot_layout_language_get_feature_index (hb_ot_layout_t *layout,
446 hb_ot_layout_table_type_t table_type,
447 unsigned int script_index,
448 unsigned int language_index,
449 unsigned int num_feature)
451 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
452 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
454 return l.get_feature_index (num_feature);
458 hb_ot_layout_language_get_feature_tag (hb_ot_layout_t *layout,
459 hb_ot_layout_table_type_t table_type,
460 unsigned int script_index,
461 unsigned int language_index,
462 unsigned int num_feature)
464 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
465 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
466 unsigned int feature_index = l.get_feature_index (num_feature);
468 return g.get_feature_tag (feature_index);
473 hb_ot_layout_language_find_feature (hb_ot_layout_t *layout,
474 hb_ot_layout_table_type_t table_type,
475 unsigned int script_index,
476 unsigned int language_index,
477 hb_tag_t feature_tag,
478 unsigned int *feature_index)
480 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
481 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
482 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
484 unsigned int num_features = l.get_feature_count ();
485 for (unsigned int i = 0; i < num_features; i++) {
486 unsigned int f_index = l.get_feature_index (i);
488 if (feature_tag == g.get_feature_tag (f_index)) {
489 if (feature_index) *feature_index = f_index;
494 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
499 hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t *layout,
500 hb_ot_layout_table_type_t table_type,
501 unsigned int feature_index)
503 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
504 const Feature &f = g.get_feature (feature_index);
506 return f.get_lookup_count ();
510 hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t *layout,
511 hb_ot_layout_table_type_t table_type,
512 unsigned int feature_index,
513 unsigned int num_lookup)
515 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
516 const Feature &f = g.get_feature (feature_index);
518 return f.get_lookup_index (num_lookup);
526 hb_ot_layout_substitute_lookup (hb_ot_layout_t *layout,
528 unsigned int lookup_index,
529 hb_ot_layout_feature_mask_t mask)
531 return layout->gsub->substitute_lookup (layout, buffer, lookup_index, mask);
539 hb_ot_layout_position_lookup (hb_ot_layout_t *layout,
541 unsigned int lookup_index,
542 hb_ot_layout_feature_mask_t mask)
544 return layout->gpos->position_lookup (layout, buffer, lookup_index, mask);
549 /* TODO dupped, until he old code can be removed */
552 hb_buffer_duplicate_out_buffer( HB_Buffer buffer )
554 if ( !buffer->alt_string )
558 if ( ALLOC_ARRAY( buffer->alt_string, buffer->allocated, HB_GlyphItemRec ) )
562 buffer->out_string = buffer->alt_string;
563 memcpy( buffer->out_string, buffer->in_string, buffer->out_length * sizeof (buffer->out_string[0]) );
564 buffer->separate_out = TRUE;
572 _hb_buffer_add_output_glyph_ids( HB_Buffer buffer,
575 const GlyphID *glyph_data,
584 error = hb_buffer_ensure( buffer, buffer->out_pos + num_out );
588 if ( !buffer->separate_out )
590 error = hb_buffer_duplicate_out_buffer( buffer );
595 properties = buffer->in_string[buffer->in_pos].properties;
596 cluster = buffer->in_string[buffer->in_pos].cluster;
597 if ( component == 0xFFFF )
598 component = buffer->in_string[buffer->in_pos].component;
599 if ( ligID == 0xFFFF )
600 ligID = buffer->in_string[buffer->in_pos].ligID;
602 for ( i = 0; i < num_out; i++ )
604 HB_GlyphItem item = &buffer->out_string[buffer->out_pos + i];
606 item->gindex = glyph_data[i];
607 item->properties = properties;
608 item->cluster = cluster;
609 item->component = component;
611 item->gproperty = HB_GLYPH_PROPERTY_UNKNOWN;
614 buffer->in_pos += num_in;
615 buffer->out_pos += num_out;
617 buffer->out_length = buffer->out_pos;