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"
39 #include "harfbuzz-buffer-private.h"
46 hb_ot_layout_create (void)
48 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
50 layout->gdef = &Null(GDEF);
51 layout->gsub = &Null(GSUB);
52 layout->gpos = &/*XXX*/Null(GSUBGPOS);
58 hb_ot_layout_create_for_data (const char *font_data,
61 hb_ot_layout_t *layout;
63 if (HB_UNLIKELY (font_data == NULL))
64 return hb_ot_layout_create ();
66 layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
68 const OpenTypeFontFile &font = OpenTypeFontFile::get_for_data (font_data);
69 const OpenTypeFontFace &face = font.get_face (face_index);
71 layout->gdef = &GDEF::get_for_data (font.get_table_data (face.get_table_by_tag (GDEF::Tag)));
72 layout->gsub = &GSUB::get_for_data (font.get_table_data (face.get_table_by_tag (GSUB::Tag)));
73 layout->gpos = &/*XXX*/GSUBGPOS::get_for_data (font.get_table_data (face.get_table_by_tag (/*XXX*/GSUBGPOS::GPOSTag)));
79 hb_ot_layout_destroy (hb_ot_layout_t *layout)
88 /* TODO the public class_t is a mess */
91 hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout)
93 return layout->gdef->has_glyph_classes ();
96 HB_OT_LAYOUT_INTERNAL hb_bool_t
97 _hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout)
99 return layout->new_gdef.len > 0;
102 HB_OT_LAYOUT_INTERNAL unsigned int
103 _hb_ot_layout_get_glyph_property (hb_ot_layout_t *layout,
104 hb_codepoint_t glyph)
106 hb_ot_layout_class_t klass;
108 /* TODO old harfbuzz doesn't always parse mark attachments as it says it was
109 * introduced without a version bump, so it may not be safe */
110 klass = layout->gdef->get_mark_attachment_type (glyph);
114 klass = layout->gdef->get_glyph_class (glyph);
116 if (!klass && glyph < layout->new_gdef.len)
117 klass = layout->new_gdef.klasses[glyph];
121 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
122 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
123 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
124 case GDEF::MarkGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
125 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
129 HB_OT_LAYOUT_INTERNAL hb_bool_t
130 _hb_ot_layout_check_glyph_property (hb_ot_layout_t *layout,
132 unsigned int lookup_flags,
133 unsigned int *property)
135 hb_ot_layout_glyph_class_t basic_glyph_class;
136 unsigned int desired_attachment_class;
138 if (gitem->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
140 gitem->gproperty = *property = _hb_ot_layout_get_glyph_property (layout, gitem->gindex);
141 if (gitem->gproperty == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED)
145 *property = gitem->gproperty;
147 /* If the glyph was found in the MarkAttachmentClass table,
148 * then that class value is the high byte of the result,
149 * otherwise the low byte contains the basic type of the glyph
150 * as defined by the GlyphClassDef table.
152 if (*property & LookupFlag::MarkAttachmentType)
153 basic_glyph_class = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
155 basic_glyph_class = (hb_ot_layout_glyph_class_t) *property;
157 /* Not covered, if, for example, basic_glyph_class
158 * is HB_GDEF_LIGATURE and lookup_flags includes LookupFlags::IgnoreLigatures
160 if (lookup_flags & basic_glyph_class)
163 /* The high byte of lookup_flags has the meaning
164 * "ignore marks of attachment type different than
165 * the attachment type specified."
167 desired_attachment_class = lookup_flags & LookupFlag::MarkAttachmentType;
168 if (desired_attachment_class)
170 if (basic_glyph_class == HB_OT_LAYOUT_GLYPH_CLASS_MARK &&
171 *property != desired_attachment_class)
178 HB_OT_LAYOUT_INTERNAL void
179 _hb_ot_layout_set_glyph_property (hb_ot_layout_t *layout,
180 hb_codepoint_t glyph,
181 unsigned int property)
183 hb_ot_layout_glyph_class_t klass;
185 if (property & LookupFlag::MarkAttachmentType)
186 klass = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
188 klass = (hb_ot_layout_glyph_class_t) property;
190 hb_ot_layout_set_glyph_class (layout, glyph, klass);
194 hb_ot_layout_glyph_class_t
195 hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout,
196 hb_codepoint_t glyph)
198 unsigned int property;
199 hb_ot_layout_class_t klass;
201 property = _hb_ot_layout_get_glyph_property (layout, glyph);
203 if (property & LookupFlag::MarkAttachmentType)
204 return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
206 return (hb_ot_layout_glyph_class_t) property;
210 hb_ot_layout_set_glyph_class (hb_ot_layout_t *layout,
211 hb_codepoint_t glyph,
212 hb_ot_layout_glyph_class_t klass)
214 /* TODO optimize this, similar to old harfbuzz code for example */
216 hb_ot_layout_class_t gdef_klass;
217 int len = layout->new_gdef.len;
219 if (G_UNLIKELY (glyph > 65535))
224 unsigned char *new_klasses;
226 new_len = len == 0 ? 120 : 2 * len;
229 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
231 if (G_UNLIKELY (!new_klasses))
234 memset (new_klasses + len, 0, new_len - len);
236 layout->new_gdef.klasses = new_klasses;
237 layout->new_gdef.len = new_len;
242 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
243 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
244 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
245 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
246 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
249 layout->new_gdef.klasses[glyph] = gdef_klass;
254 hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout,
255 uint16_t num_total_glyphs,
256 hb_codepoint_t *glyphs,
257 unsigned char *klasses,
260 if (G_UNLIKELY (!count || !glyphs || !klasses))
263 if (layout->new_gdef.len == 0) {
264 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
265 layout->new_gdef.len = count;
268 for (unsigned int i = 0; i < count; i++)
269 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
276 static const GSUBGPOS&
277 get_gsubgpos_table (hb_ot_layout_t *layout,
278 hb_ot_layout_table_type_t table_type)
280 switch (table_type) {
281 case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub);
282 case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos);
283 default: return Null(GSUBGPOS);
289 hb_ot_layout_table_get_script_count (hb_ot_layout_t *layout,
290 hb_ot_layout_table_type_t table_type)
292 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
294 return g.get_script_count ();
298 hb_ot_layout_table_get_script_tag (hb_ot_layout_t *layout,
299 hb_ot_layout_table_type_t table_type,
300 unsigned int script_index)
302 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
304 return g.get_script_tag (script_index);
308 hb_ot_layout_table_find_script (hb_ot_layout_t *layout,
309 hb_ot_layout_table_type_t table_type,
311 unsigned int *script_index)
313 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
314 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
316 if (g.find_script_index (script_tag, script_index))
319 /* try finding 'DFLT' */
320 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
323 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
324 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
327 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
332 hb_ot_layout_table_get_feature_count (hb_ot_layout_t *layout,
333 hb_ot_layout_table_type_t table_type)
335 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
337 return g.get_feature_count ();
341 hb_ot_layout_table_get_feature_tag (hb_ot_layout_t *layout,
342 hb_ot_layout_table_type_t table_type,
343 unsigned int feature_index)
345 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
347 return g.get_feature_tag (feature_index);
351 hb_ot_layout_table_find_feature (hb_ot_layout_t *layout,
352 hb_ot_layout_table_type_t table_type,
353 hb_tag_t feature_tag,
354 unsigned int *feature_index)
356 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
357 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
359 if (g.find_feature_index (feature_tag, feature_index))
362 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
367 hb_ot_layout_table_get_lookup_count (hb_ot_layout_t *layout,
368 hb_ot_layout_table_type_t table_type)
370 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
372 return g.get_lookup_count ();
377 hb_ot_layout_script_get_language_count (hb_ot_layout_t *layout,
378 hb_ot_layout_table_type_t table_type,
379 unsigned int script_index)
381 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
383 return s.get_lang_sys_count ();
387 hb_ot_layout_script_get_language_tag (hb_ot_layout_t *layout,
388 hb_ot_layout_table_type_t table_type,
389 unsigned int script_index,
390 unsigned int language_index)
392 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
394 return s.get_lang_sys_tag (language_index);
398 hb_ot_layout_script_find_language (hb_ot_layout_t *layout,
399 hb_ot_layout_table_type_t table_type,
400 unsigned int script_index,
401 hb_tag_t language_tag,
402 unsigned int *language_index)
404 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
405 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
407 if (s.find_lang_sys_index (language_tag, language_index))
410 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
411 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
414 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
419 hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t *layout,
420 hb_ot_layout_table_type_t table_type,
421 unsigned int script_index,
422 unsigned int language_index,
423 unsigned int *feature_index)
425 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
427 if (feature_index) *feature_index = l.get_required_feature_index ();
429 return l.has_required_feature ();
433 hb_ot_layout_language_get_feature_count (hb_ot_layout_t *layout,
434 hb_ot_layout_table_type_t table_type,
435 unsigned int script_index,
436 unsigned int language_index)
438 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
440 return l.get_feature_count ();
444 hb_ot_layout_language_get_feature_index (hb_ot_layout_t *layout,
445 hb_ot_layout_table_type_t table_type,
446 unsigned int script_index,
447 unsigned int language_index,
448 unsigned int num_feature)
450 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
451 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
453 return l.get_feature_index (num_feature);
457 hb_ot_layout_language_get_feature_tag (hb_ot_layout_t *layout,
458 hb_ot_layout_table_type_t table_type,
459 unsigned int script_index,
460 unsigned int language_index,
461 unsigned int num_feature)
463 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
464 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
465 unsigned int feature_index = l.get_feature_index (num_feature);
467 return g.get_feature_tag (feature_index);
472 hb_ot_layout_language_find_feature (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 hb_tag_t feature_tag,
477 unsigned int *feature_index)
479 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
480 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
481 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
483 unsigned int num_features = l.get_feature_count ();
484 for (unsigned int i = 0; i < num_features; i++) {
485 unsigned int f_index = l.get_feature_index (i);
487 if (feature_tag == g.get_feature_tag (f_index)) {
488 if (feature_index) *feature_index = f_index;
493 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
498 hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t *layout,
499 hb_ot_layout_table_type_t table_type,
500 unsigned int feature_index)
502 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
503 const Feature &f = g.get_feature (feature_index);
505 return f.get_lookup_count ();
509 hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t *layout,
510 hb_ot_layout_table_type_t table_type,
511 unsigned int feature_index,
512 unsigned int num_lookup)
514 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
515 const Feature &f = g.get_feature (feature_index);
517 return f.get_lookup_index (num_lookup);
525 hb_ot_layout_substitute_lookup (hb_ot_layout_t *layout,
527 unsigned int lookup_index,
528 hb_ot_layout_feature_mask_t mask)
530 return layout->gsub->substitute_lookup (layout, buffer, lookup_index, mask);
535 /* TODO dupped, until he old code can be removed */
538 hb_buffer_duplicate_out_buffer( HB_Buffer buffer )
540 if ( !buffer->alt_string )
544 if ( ALLOC_ARRAY( buffer->alt_string, buffer->allocated, HB_GlyphItemRec ) )
548 buffer->out_string = buffer->alt_string;
549 memcpy( buffer->out_string, buffer->in_string, buffer->out_length * sizeof (buffer->out_string[0]) );
550 buffer->separate_out = TRUE;
558 _hb_buffer_add_output_glyph_ids( HB_Buffer buffer,
561 const GlyphID *glyph_data,
570 error = hb_buffer_ensure( buffer, buffer->out_pos + num_out );
574 if ( !buffer->separate_out )
576 error = hb_buffer_duplicate_out_buffer( buffer );
581 properties = buffer->in_string[buffer->in_pos].properties;
582 cluster = buffer->in_string[buffer->in_pos].cluster;
583 if ( component == 0xFFFF )
584 component = buffer->in_string[buffer->in_pos].component;
585 if ( ligID == 0xFFFF )
586 ligID = buffer->in_string[buffer->in_pos].ligID;
588 for ( i = 0; i < num_out; i++ )
590 HB_GlyphItem item = &buffer->out_string[buffer->out_pos + i];
592 item->gindex = glyph_data[i];
593 item->properties = properties;
594 item->cluster = cluster;
595 item->component = component;
597 item->gproperty = HB_GLYPH_PROPERTY_UNKNOWN;
600 buffer->in_pos += num_in;
601 buffer->out_pos += num_out;
603 buffer->out_length = buffer->out_pos;