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
32 #include "hb-buffer-private.h"
34 #include "hb-ot-layout.h"
35 #include "hb-ot-layout-private.h"
37 #include "hb-ot-layout-open-private.h"
38 #include "hb-ot-layout-gdef-private.h"
39 #include "hb-ot-layout-gsub-private.h"
40 #include "hb-ot-layout-gpos-private.h"
48 hb_ot_layout_create (void)
50 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
52 layout->gdef = &Null(GDEF);
53 layout->gsub = &Null(GSUB);
54 layout->gpos = &Null(GPOS);
60 hb_ot_layout_create_for_data (const char *font_data,
63 hb_ot_layout_t *layout;
65 if (HB_UNLIKELY (font_data == NULL))
66 return hb_ot_layout_create ();
68 layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
70 const OpenTypeFontFile &font = OpenTypeFontFile::get_for_data (font_data);
71 const OpenTypeFontFace &face = font.get_face (face_index);
73 layout->gdef = &GDEF::get_for_data (font.get_table_data (face.get_table_by_tag (GDEF::Tag)));
74 layout->gsub = &GSUB::get_for_data (font.get_table_data (face.get_table_by_tag (GSUB::Tag)));
75 layout->gpos = &GPOS::get_for_data (font.get_table_data (face.get_table_by_tag (GPOS::Tag)));
81 hb_ot_layout_destroy (hb_ot_layout_t *layout)
87 hb_ot_layout_set_direction (hb_ot_layout_t *layout,
90 layout->gpos_info.r2l = !!r2l;
94 hb_ot_layout_set_hinting (hb_ot_layout_t *layout,
97 layout->gpos_info.dvi = !hinted;
101 hb_ot_layout_set_scale (hb_ot_layout_t *layout,
102 hb_16dot16_t x_scale, hb_16dot16_t y_scale)
104 layout->gpos_info.x_scale = x_scale;
105 layout->gpos_info.y_scale = y_scale;
109 hb_ot_layout_set_ppem (hb_ot_layout_t *layout,
110 unsigned int x_ppem, unsigned int y_ppem)
112 layout->gpos_info.x_ppem = x_ppem;
113 layout->gpos_info.y_ppem = y_ppem;
121 /* TODO the public class_t is a mess */
124 hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout)
126 return layout->gdef->has_glyph_classes ();
129 HB_INTERNAL hb_bool_t
130 _hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout)
132 return layout->new_gdef.len > 0;
135 HB_INTERNAL unsigned int
136 _hb_ot_layout_get_glyph_property (hb_ot_layout_t *layout,
137 hb_codepoint_t glyph)
139 hb_ot_layout_class_t klass;
141 klass = layout->gdef->get_glyph_class (glyph);
143 if (!klass && glyph < layout->new_gdef.len)
144 klass = layout->new_gdef.klasses[glyph];
148 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
149 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
150 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
151 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
152 case GDEF::MarkGlyph:
153 /* TODO old harfbuzz doesn't always parse mark attachments as it says it was
154 * introduced without a version bump, so it may not be safe */
155 klass = layout->gdef->get_mark_attachment_type (glyph);
156 return HB_OT_LAYOUT_GLYPH_CLASS_MARK + klass << 8;
160 HB_INTERNAL hb_bool_t
161 _hb_ot_layout_check_glyph_property (hb_ot_layout_t *layout,
162 hb_glyph_info_t *ginfo,
163 unsigned int lookup_flags,
164 unsigned int *property)
166 hb_ot_layout_glyph_class_t glyph_class;
167 unsigned int desired_attachment_class;
169 if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
171 ginfo->gproperty = *property = _hb_ot_layout_get_glyph_property (layout, ginfo->gindex);
172 if (ginfo->gproperty == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED)
176 *property = ginfo->gproperty;
178 /* If the glyph was found in the MarkAttachmentClass table,
179 * then that class value is stored in the high byte of the result.
180 * The low byte contains the basic type of the glyph as defined by
181 * the GlyphClassDef table.
183 glyph_class = (hb_ot_layout_glyph_class_t) *property;
185 /* Not covered, if, for example, glyph_class is HB_GDEF_LIGATURE and
186 * lookup_flags includes LookupFlags::IgnoreLigatures
188 if (lookup_flags & glyph_class)
191 if (glyph_class & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
193 /* If using mark filtering sets, the high short of
194 * lookup_flags hsa the set index.
196 if (lookup_flags & LookupFlag::UseMarkFilteringSet)
197 return layout->gdef->mark_set_covers (lookup_flags >> 16, ginfo->gindex);
199 /* The second byte of lookup_flags has the meaning
200 * "ignore marks of attachment type different than
201 * the attachment type specified."
203 if (lookup_flags & LookupFlag::MarkAttachmentType && *property & LookupFlag::MarkAttachmentType)
204 return (lookup_flags & LookupFlag::MarkAttachmentType) == (*property & LookupFlag::MarkAttachmentType);
212 _hb_ot_layout_set_glyph_property (hb_ot_layout_t *layout,
213 hb_codepoint_t glyph,
214 unsigned int property)
215 { hb_ot_layout_set_glyph_class (layout, glyph, (hb_ot_layout_glyph_class_t) (property & 0xff)); }
218 hb_ot_layout_glyph_class_t
219 hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout,
220 hb_codepoint_t glyph)
221 { return (hb_ot_layout_glyph_class_t) (_hb_ot_layout_get_glyph_property (layout, glyph) & 0xff); }
224 hb_ot_layout_set_glyph_class (hb_ot_layout_t *layout,
225 hb_codepoint_t glyph,
226 hb_ot_layout_glyph_class_t klass)
228 /* TODO optimize this, similar to old harfbuzz code for example */
230 hb_ot_layout_class_t gdef_klass;
231 int len = layout->new_gdef.len;
233 if (HB_UNLIKELY (glyph > 65535))
238 unsigned char *new_klasses;
240 new_len = len == 0 ? 120 : 2 * len;
243 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
245 if (HB_UNLIKELY (!new_klasses))
248 memset (new_klasses + len, 0, new_len - len);
250 layout->new_gdef.klasses = new_klasses;
251 layout->new_gdef.len = new_len;
256 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
257 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
258 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
259 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
260 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
263 layout->new_gdef.klasses[glyph] = gdef_klass;
268 hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout,
269 uint16_t num_total_glyphs,
270 hb_codepoint_t *glyphs,
271 unsigned char *klasses,
274 if (HB_UNLIKELY (!count || !glyphs || !klasses))
277 if (layout->new_gdef.len == 0) {
278 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
279 layout->new_gdef.len = count;
282 for (unsigned int i = 0; i < count; i++)
283 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
287 hb_ot_layout_get_attach_points (hb_ot_layout_t *layout,
288 hb_codepoint_t glyph,
289 unsigned int *point_count /* IN/OUT */,
290 unsigned int *point_array /* OUT */)
292 return layout->gdef->get_attach_points (glyph, point_count, point_array);
296 hb_ot_layout_get_lig_carets (hb_ot_layout_t *layout,
297 hb_codepoint_t glyph,
298 unsigned int *caret_count /* IN/OUT */,
299 int *caret_array /* OUT */)
301 return layout->gdef->get_lig_carets (layout, glyph, caret_count, caret_array);
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);