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;
93 hb_ot_layout_set_hinting (hb_ot_layout_t *layout,
96 layout->gpos_info.dvi = !hinted;
100 hb_ot_layout_set_scale (hb_ot_layout_t *layout,
101 hb_16dot16_t x_scale, hb_16dot16_t y_scale)
103 layout->gpos_info.x_scale = x_scale;
104 layout->gpos_info.y_scale = y_scale;
108 hb_ot_layout_set_ppem (hb_ot_layout_t *layout,
109 unsigned int x_ppem, unsigned int y_ppem)
111 layout->gpos_info.x_ppem = x_ppem;
112 layout->gpos_info.y_ppem = y_ppem;
120 /* TODO the public class_t is a mess */
123 hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout)
125 return layout->gdef->has_glyph_classes ();
128 HB_OT_LAYOUT_INTERNAL hb_bool_t
129 _hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout)
131 return layout->new_gdef.len > 0;
134 HB_OT_LAYOUT_INTERNAL unsigned int
135 _hb_ot_layout_get_glyph_property (hb_ot_layout_t *layout,
136 hb_codepoint_t glyph)
138 hb_ot_layout_class_t klass;
140 /* TODO old harfbuzz doesn't always parse mark attachments as it says it was
141 * introduced without a version bump, so it may not be safe */
142 klass = layout->gdef->get_mark_attachment_type (glyph);
146 klass = layout->gdef->get_glyph_class (glyph);
148 if (!klass && glyph < layout->new_gdef.len)
149 klass = layout->new_gdef.klasses[glyph];
153 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
154 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
155 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
156 case GDEF::MarkGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
157 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
161 HB_OT_LAYOUT_INTERNAL hb_bool_t
162 _hb_ot_layout_check_glyph_property (hb_ot_layout_t *layout,
164 unsigned int lookup_flags,
165 unsigned int *property)
167 hb_ot_layout_glyph_class_t basic_glyph_class;
168 unsigned int desired_attachment_class;
170 if (gitem->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
172 gitem->gproperty = *property = _hb_ot_layout_get_glyph_property (layout, gitem->gindex);
173 if (gitem->gproperty == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED)
177 *property = gitem->gproperty;
179 /* If the glyph was found in the MarkAttachmentClass table,
180 * then that class value is the high byte of the result,
181 * otherwise the low byte contains the basic type of the glyph
182 * as defined by the GlyphClassDef table.
184 if (*property & LookupFlag::MarkAttachmentType)
185 basic_glyph_class = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
187 basic_glyph_class = (hb_ot_layout_glyph_class_t) *property;
189 /* Not covered, if, for example, basic_glyph_class
190 * is HB_GDEF_LIGATURE and lookup_flags includes LookupFlags::IgnoreLigatures
192 if (lookup_flags & basic_glyph_class)
195 /* The high byte of lookup_flags has the meaning
196 * "ignore marks of attachment type different than
197 * the attachment type specified."
199 desired_attachment_class = lookup_flags & LookupFlag::MarkAttachmentType;
200 if (desired_attachment_class)
202 if (basic_glyph_class == HB_OT_LAYOUT_GLYPH_CLASS_MARK &&
203 *property != desired_attachment_class)
210 HB_OT_LAYOUT_INTERNAL void
211 _hb_ot_layout_set_glyph_property (hb_ot_layout_t *layout,
212 hb_codepoint_t glyph,
213 unsigned int property)
215 hb_ot_layout_glyph_class_t klass;
217 if (property & LookupFlag::MarkAttachmentType)
218 klass = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
220 klass = (hb_ot_layout_glyph_class_t) property;
222 hb_ot_layout_set_glyph_class (layout, glyph, klass);
226 hb_ot_layout_glyph_class_t
227 hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout,
228 hb_codepoint_t glyph)
230 unsigned int property;
231 hb_ot_layout_class_t klass;
233 property = _hb_ot_layout_get_glyph_property (layout, glyph);
235 if (property & LookupFlag::MarkAttachmentType)
236 return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
238 return (hb_ot_layout_glyph_class_t) property;
242 hb_ot_layout_set_glyph_class (hb_ot_layout_t *layout,
243 hb_codepoint_t glyph,
244 hb_ot_layout_glyph_class_t klass)
246 /* TODO optimize this, similar to old harfbuzz code for example */
248 hb_ot_layout_class_t gdef_klass;
249 int len = layout->new_gdef.len;
251 if (HB_UNLIKELY (glyph > 65535))
256 unsigned char *new_klasses;
258 new_len = len == 0 ? 120 : 2 * len;
261 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
263 if (HB_UNLIKELY (!new_klasses))
266 memset (new_klasses + len, 0, new_len - len);
268 layout->new_gdef.klasses = new_klasses;
269 layout->new_gdef.len = new_len;
274 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
275 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
276 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
277 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
278 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
281 layout->new_gdef.klasses[glyph] = gdef_klass;
286 hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout,
287 uint16_t num_total_glyphs,
288 hb_codepoint_t *glyphs,
289 unsigned char *klasses,
292 if (HB_UNLIKELY (!count || !glyphs || !klasses))
295 if (layout->new_gdef.len == 0) {
296 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
297 layout->new_gdef.len = count;
300 for (unsigned int i = 0; i < count; i++)
301 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
308 static const GSUBGPOS&
309 get_gsubgpos_table (hb_ot_layout_t *layout,
310 hb_ot_layout_table_type_t table_type)
312 switch (table_type) {
313 case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub);
314 case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos);
315 default: return Null(GSUBGPOS);
321 hb_ot_layout_table_get_script_count (hb_ot_layout_t *layout,
322 hb_ot_layout_table_type_t table_type)
324 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
326 return g.get_script_count ();
330 hb_ot_layout_table_get_script_tag (hb_ot_layout_t *layout,
331 hb_ot_layout_table_type_t table_type,
332 unsigned int script_index)
334 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
336 return g.get_script_tag (script_index);
340 hb_ot_layout_table_find_script (hb_ot_layout_t *layout,
341 hb_ot_layout_table_type_t table_type,
343 unsigned int *script_index)
345 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
346 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
348 if (g.find_script_index (script_tag, script_index))
351 /* try finding 'DFLT' */
352 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
355 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
356 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
359 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
364 hb_ot_layout_table_get_feature_count (hb_ot_layout_t *layout,
365 hb_ot_layout_table_type_t table_type)
367 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
369 return g.get_feature_count ();
373 hb_ot_layout_table_get_feature_tag (hb_ot_layout_t *layout,
374 hb_ot_layout_table_type_t table_type,
375 unsigned int feature_index)
377 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
379 return g.get_feature_tag (feature_index);
383 hb_ot_layout_table_find_feature (hb_ot_layout_t *layout,
384 hb_ot_layout_table_type_t table_type,
385 hb_tag_t feature_tag,
386 unsigned int *feature_index)
388 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
389 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
391 if (g.find_feature_index (feature_tag, feature_index))
394 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
399 hb_ot_layout_table_get_lookup_count (hb_ot_layout_t *layout,
400 hb_ot_layout_table_type_t table_type)
402 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
404 return g.get_lookup_count ();
409 hb_ot_layout_script_get_language_count (hb_ot_layout_t *layout,
410 hb_ot_layout_table_type_t table_type,
411 unsigned int script_index)
413 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
415 return s.get_lang_sys_count ();
419 hb_ot_layout_script_get_language_tag (hb_ot_layout_t *layout,
420 hb_ot_layout_table_type_t table_type,
421 unsigned int script_index,
422 unsigned int language_index)
424 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
426 return s.get_lang_sys_tag (language_index);
430 hb_ot_layout_script_find_language (hb_ot_layout_t *layout,
431 hb_ot_layout_table_type_t table_type,
432 unsigned int script_index,
433 hb_tag_t language_tag,
434 unsigned int *language_index)
436 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
437 const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
439 if (s.find_lang_sys_index (language_tag, language_index))
442 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
443 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
446 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
451 hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t *layout,
452 hb_ot_layout_table_type_t table_type,
453 unsigned int script_index,
454 unsigned int language_index,
455 unsigned int *feature_index)
457 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
459 if (feature_index) *feature_index = l.get_required_feature_index ();
461 return l.has_required_feature ();
465 hb_ot_layout_language_get_feature_count (hb_ot_layout_t *layout,
466 hb_ot_layout_table_type_t table_type,
467 unsigned int script_index,
468 unsigned int language_index)
470 const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
472 return l.get_feature_count ();
476 hb_ot_layout_language_get_feature_index (hb_ot_layout_t *layout,
477 hb_ot_layout_table_type_t table_type,
478 unsigned int script_index,
479 unsigned int language_index,
480 unsigned int num_feature)
482 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
483 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
485 return l.get_feature_index (num_feature);
489 hb_ot_layout_language_get_feature_tag (hb_ot_layout_t *layout,
490 hb_ot_layout_table_type_t table_type,
491 unsigned int script_index,
492 unsigned int language_index,
493 unsigned int num_feature)
495 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
496 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
497 unsigned int feature_index = l.get_feature_index (num_feature);
499 return g.get_feature_tag (feature_index);
504 hb_ot_layout_language_find_feature (hb_ot_layout_t *layout,
505 hb_ot_layout_table_type_t table_type,
506 unsigned int script_index,
507 unsigned int language_index,
508 hb_tag_t feature_tag,
509 unsigned int *feature_index)
511 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
512 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
513 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
515 unsigned int num_features = l.get_feature_count ();
516 for (unsigned int i = 0; i < num_features; i++) {
517 unsigned int f_index = l.get_feature_index (i);
519 if (feature_tag == g.get_feature_tag (f_index)) {
520 if (feature_index) *feature_index = f_index;
525 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
530 hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t *layout,
531 hb_ot_layout_table_type_t table_type,
532 unsigned int feature_index)
534 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
535 const Feature &f = g.get_feature (feature_index);
537 return f.get_lookup_count ();
541 hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t *layout,
542 hb_ot_layout_table_type_t table_type,
543 unsigned int feature_index,
544 unsigned int num_lookup)
546 const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
547 const Feature &f = g.get_feature (feature_index);
549 return f.get_lookup_index (num_lookup);
557 hb_ot_layout_substitute_lookup (hb_ot_layout_t *layout,
559 unsigned int lookup_index,
560 hb_ot_layout_feature_mask_t mask)
562 return layout->gsub->substitute_lookup (layout, buffer, lookup_index, mask);
570 hb_ot_layout_position_lookup (hb_ot_layout_t *layout,
572 unsigned int lookup_index,
573 hb_ot_layout_feature_mask_t mask)
575 return layout->gpos->position_lookup (layout, buffer, lookup_index, mask);
580 /* TODO dupped, until he old code can be removed */
583 hb_buffer_duplicate_out_buffer( HB_Buffer buffer )
585 if ( !buffer->alt_string )
589 if ( ALLOC_ARRAY( buffer->alt_string, buffer->allocated, HB_GlyphItemRec ) )
593 buffer->out_string = buffer->alt_string;
594 memcpy( buffer->out_string, buffer->in_string, buffer->out_length * sizeof (buffer->out_string[0]) );
595 buffer->separate_out = TRUE;
603 _hb_buffer_add_output_glyph_ids( HB_Buffer buffer,
606 const GlyphID *glyph_data,
615 error = hb_buffer_ensure( buffer, buffer->out_pos + num_out );
619 if ( !buffer->separate_out )
621 error = hb_buffer_duplicate_out_buffer( buffer );
626 properties = buffer->in_string[buffer->in_pos].properties;
627 cluster = buffer->in_string[buffer->in_pos].cluster;
628 if ( component == 0xFFFF )
629 component = buffer->in_string[buffer->in_pos].component;
630 if ( ligID == 0xFFFF )
631 ligID = buffer->in_string[buffer->in_pos].ligID;
633 for ( i = 0; i < num_out; i++ )
635 HB_GlyphItem item = &buffer->out_string[buffer->out_pos + i];
637 item->gindex = glyph_data[i];
638 item->properties = properties;
639 item->cluster = cluster;
640 item->component = component;
642 item->gproperty = HB_GLYPH_PROPERTY_UNKNOWN;
645 buffer->in_pos += num_in;
646 buffer->out_pos += num_out;
648 buffer->out_length = buffer->out_pos;