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"
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 /* XXX 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,
262 if (G_UNLIKELY (!count || !glyphs || !klasses))
265 if (layout->new_gdef.len == 0) {
266 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
267 layout->new_gdef.len = count;
270 for (i = 0; i < count; i++)
271 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
278 static const GSUBGPOS&
279 get_gsubgpos_table (hb_ot_layout_t *layout,
280 hb_ot_layout_table_type_t table_type)
282 switch (table_type) {
283 case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub);
284 case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos);
285 default: return Null(GSUBGPOS);
291 hb_ot_layout_table_get_script_count (hb_ot_layout_t *layout,
292 hb_ot_layout_table_type_t table_type)
294 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
296 return g.get_script_count ();
300 hb_ot_layout_table_get_script_tag (hb_ot_layout_t *layout,
301 hb_ot_layout_table_type_t table_type,
302 unsigned int script_index)
304 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
306 return g.get_script_tag (script_index);
310 hb_ot_layout_table_find_script (hb_ot_layout_t *layout,
311 hb_ot_layout_table_type_t table_type,
313 unsigned int *script_index)
315 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
316 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
318 if (g.find_script_index (script_tag, script_index))
321 /* try finding 'DFLT' */
322 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
325 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
326 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
329 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
334 hb_ot_layout_table_get_feature_count (hb_ot_layout_t *layout,
335 hb_ot_layout_table_type_t table_type)
337 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
339 return g.get_feature_count ();
343 hb_ot_layout_table_get_feature_tag (hb_ot_layout_t *layout,
344 hb_ot_layout_table_type_t table_type,
345 unsigned int feature_index)
347 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
349 return g.get_feature_tag (feature_index);
353 hb_ot_layout_table_find_feature (hb_ot_layout_t *layout,
354 hb_ot_layout_table_type_t table_type,
355 hb_tag_t feature_tag,
356 unsigned int *feature_index)
358 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
359 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
361 if (g.find_feature_index (feature_tag, feature_index))
364 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
369 hb_ot_layout_table_get_lookup_count (hb_ot_layout_t *layout,
370 hb_ot_layout_table_type_t table_type)
372 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
374 return g.get_lookup_count ();
379 hb_ot_layout_script_get_language_count (hb_ot_layout_t *layout,
380 hb_ot_layout_table_type_t table_type,
381 unsigned int script_index)
383 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
385 return s.get_lang_sys_count ();
389 hb_ot_layout_script_get_language_tag (hb_ot_layout_t *layout,
390 hb_ot_layout_table_type_t table_type,
391 unsigned int script_index,
392 unsigned int language_index)
394 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
396 return s.get_lang_sys_tag (language_index);
400 hb_ot_layout_script_find_language (hb_ot_layout_t *layout,
401 hb_ot_layout_table_type_t table_type,
402 unsigned int script_index,
403 hb_tag_t language_tag,
404 unsigned int *language_index)
406 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
407 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
409 if (s.find_lang_sys_index (language_tag, language_index))
412 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
413 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
416 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
421 hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t *layout,
422 hb_ot_layout_table_type_t table_type,
423 unsigned int script_index,
424 unsigned int language_index,
425 unsigned int *feature_index)
427 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
429 if (feature_index) *feature_index = l.get_required_feature_index ();
431 return l.has_required_feature ();
435 hb_ot_layout_language_get_feature_count (hb_ot_layout_t *layout,
436 hb_ot_layout_table_type_t table_type,
437 unsigned int script_index,
438 unsigned int language_index)
440 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
442 return l.get_feature_count ();
446 hb_ot_layout_language_get_feature_index (hb_ot_layout_t *layout,
447 hb_ot_layout_table_type_t table_type,
448 unsigned int script_index,
449 unsigned int language_index,
450 unsigned int num_feature)
452 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
453 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
455 return l.get_feature_index (num_feature);
459 hb_ot_layout_language_get_feature_tag (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);
467 unsigned int feature_index = l.get_feature_index (num_feature);
469 return g.get_feature_tag (feature_index);
474 hb_ot_layout_language_find_feature (hb_ot_layout_t *layout,
475 hb_ot_layout_table_type_t table_type,
476 unsigned int script_index,
477 unsigned int language_index,
478 hb_tag_t feature_tag,
479 unsigned int *feature_index)
481 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
482 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
483 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
486 for (i = 0; i < l.get_feature_count (); i++) {
487 unsigned int f_index = l.get_feature_index (i);
489 if (feature_tag == g.get_feature_tag (f_index)) {
490 if (feature_index) *feature_index = f_index;
495 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
500 hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t *layout,
501 hb_ot_layout_table_type_t table_type,
502 unsigned int feature_index)
504 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
505 const Feature &f = g.get_feature (feature_index);
507 return f.get_lookup_count ();
511 hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t *layout,
512 hb_ot_layout_table_type_t table_type,
513 unsigned int feature_index,
514 unsigned int num_lookup)
516 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
517 const Feature &f = g.get_feature (feature_index);
519 return f.get_lookup_index (num_lookup);
527 hb_ot_layout_substitute_lookup (hb_ot_layout_t *layout,
529 unsigned int lookup_index,
530 hb_ot_layout_feature_mask_t mask)
532 return layout->gsub->substitute_lookup (layout, buffer, lookup_index, mask);
537 /* XXX dupped, until he old code can be removed */
540 hb_buffer_duplicate_out_buffer( HB_Buffer buffer )
542 if ( !buffer->alt_string )
546 if ( ALLOC_ARRAY( buffer->alt_string, buffer->allocated, HB_GlyphItemRec ) )
550 buffer->out_string = buffer->alt_string;
551 memcpy( buffer->out_string, buffer->in_string, buffer->out_length * sizeof (buffer->out_string[0]) );
552 buffer->separate_out = TRUE;
560 _hb_buffer_add_output_glyph_ids( HB_Buffer buffer,
563 const GlyphID *glyph_data,
572 error = hb_buffer_ensure( buffer, buffer->out_pos + num_out );
576 if ( !buffer->separate_out )
578 error = hb_buffer_duplicate_out_buffer( buffer );
583 properties = buffer->in_string[buffer->in_pos].properties;
584 cluster = buffer->in_string[buffer->in_pos].cluster;
585 if ( component == 0xFFFF )
586 component = buffer->in_string[buffer->in_pos].component;
587 if ( ligID == 0xFFFF )
588 ligID = buffer->in_string[buffer->in_pos].ligID;
590 for ( i = 0; i < num_out; i++ )
592 HB_GlyphItem item = &buffer->out_string[buffer->out_pos + i];
594 item->gindex = glyph_data[i];
595 item->properties = properties;
596 item->cluster = cluster;
597 item->component = component;
599 item->gproperty = HB_GLYPH_PROPERTY_UNKNOWN;
602 buffer->in_pos += num_in;
603 buffer->out_pos += num_out;
605 buffer->out_length = buffer->out_pos;