2 * Copyright © 1998-2004 David Turner and Werner Lemberg
3 * Copyright © 2006 Behdad Esfahbod
4 * Copyright © 2007,2008,2009 Red Hat, Inc.
6 * This is part of HarfBuzz, a text shaping 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-private.hh"
33 #include "hb-ot-layout-gdef-private.hh"
34 #include "hb-ot-layout-gsub-private.hh"
35 #include "hb-ot-layout-gpos-private.hh"
36 #include "hb-ot-head-private.hh"
37 #include "hb-ot-maxp-private.hh"
47 _hb_ot_layout_create (hb_face_t *face)
49 /* TODO Remove this object altogether */
50 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
52 layout->gdef_blob = Sanitizer<GDEF>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GDEF));
53 layout->gdef = Sanitizer<GDEF>::lock_instance (layout->gdef_blob);
55 layout->gsub_blob = Sanitizer<GSUB>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GSUB));
56 layout->gsub = Sanitizer<GSUB>::lock_instance (layout->gsub_blob);
58 layout->gpos_blob = Sanitizer<GPOS>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GPOS));
59 layout->gpos = Sanitizer<GPOS>::lock_instance (layout->gpos_blob);
61 layout->head_blob = Sanitizer<head>::sanitize (hb_face_reference_table (face, HB_OT_TAG_head));
62 layout->head = Sanitizer<head>::lock_instance (layout->head_blob);
68 _hb_ot_layout_destroy (hb_ot_layout_t *layout)
70 hb_blob_destroy (layout->gdef_blob);
71 hb_blob_destroy (layout->gsub_blob);
72 hb_blob_destroy (layout->gpos_blob);
73 hb_blob_destroy (layout->head_blob);
78 static inline const GDEF&
79 _get_gdef (hb_face_t *face)
81 return likely (face->ot_layout && face->ot_layout->gdef) ? *face->ot_layout->gdef : Null(GDEF);
83 static inline const GSUB&
84 _get_gsub (hb_face_t *face)
86 return likely (face->ot_layout && face->ot_layout->gsub) ? *face->ot_layout->gsub : Null(GSUB);
88 static inline const GPOS&
89 _get_gpos (hb_face_t *face)
91 return likely (face->ot_layout && face->ot_layout->gpos) ? *face->ot_layout->gpos : Null(GPOS);
93 static inline const head&
94 _get_head (hb_face_t *face)
96 return likely (face->ot_layout && face->ot_layout->head) ? *face->ot_layout->head : Null(head);
105 hb_ot_layout_has_glyph_classes (hb_face_t *face)
107 return _get_gdef (face).has_glyph_classes ();
111 _hb_ot_layout_get_glyph_property (hb_face_t *face,
112 hb_glyph_info_t *info)
114 if (!info->props_cache())
116 const GDEF &gdef = _get_gdef (face);
117 info->props_cache() = gdef.get_glyph_props (info->codepoint);
120 return info->props_cache();
124 _hb_ot_layout_match_properties (hb_face_t *face,
125 hb_codepoint_t codepoint,
126 unsigned int glyph_props,
127 unsigned int lookup_props)
129 /* Not covered, if, for example, glyph class is ligature and
130 * lookup_props includes LookupFlags::IgnoreLigatures
132 if (glyph_props & lookup_props & LookupFlag::IgnoreFlags)
135 if (glyph_props & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
137 /* If using mark filtering sets, the high short of
138 * lookup_props has the set index.
140 if (lookup_props & LookupFlag::UseMarkFilteringSet)
141 return _get_gdef (face).mark_set_covers (lookup_props >> 16, codepoint);
143 /* The second byte of lookup_props has the meaning
144 * "ignore marks of attachment type different than
145 * the attachment type specified."
147 if (lookup_props & LookupFlag::MarkAttachmentType && glyph_props & LookupFlag::MarkAttachmentType)
148 return (lookup_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
155 _hb_ot_layout_check_glyph_property (hb_face_t *face,
156 hb_glyph_info_t *ginfo,
157 unsigned int lookup_props,
158 unsigned int *property_out)
160 unsigned int property;
162 property = _hb_ot_layout_get_glyph_property (face, ginfo);
163 (void) (property_out && (*property_out = property));
165 return _hb_ot_layout_match_properties (face, ginfo->codepoint, property, lookup_props);
169 _hb_ot_layout_skip_mark (hb_face_t *face,
170 hb_glyph_info_t *ginfo,
171 unsigned int lookup_props,
172 unsigned int *property_out)
174 unsigned int property;
176 property = _hb_ot_layout_get_glyph_property (face, ginfo);
177 (void) (property_out && (*property_out = property));
179 /* If it's a mark, skip it we don't accept it. */
180 if (unlikely (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
181 return !_hb_ot_layout_match_properties (face, ginfo->codepoint, property, lookup_props);
183 /* If not a mark, don't skip. */
190 hb_ot_layout_get_attach_points (hb_face_t *face,
191 hb_codepoint_t glyph,
192 unsigned int start_offset,
193 unsigned int *point_count /* IN/OUT */,
194 unsigned int *point_array /* OUT */)
196 return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
200 hb_ot_layout_get_ligature_carets (hb_font_t *font,
201 hb_direction_t direction,
202 hb_codepoint_t glyph,
203 unsigned int start_offset,
204 unsigned int *caret_count /* IN/OUT */,
205 int *caret_array /* OUT */)
207 return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
214 static const GSUBGPOS&
215 get_gsubgpos_table (hb_face_t *face,
219 case HB_OT_TAG_GSUB: return _get_gsub (face);
220 case HB_OT_TAG_GPOS: return _get_gpos (face);
221 default: return Null(GSUBGPOS);
227 hb_ot_layout_table_get_script_tags (hb_face_t *face,
229 unsigned int start_offset,
230 unsigned int *script_count /* IN/OUT */,
231 hb_tag_t *script_tags /* OUT */)
233 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
235 return g.get_script_tags (start_offset, script_count, script_tags);
239 hb_ot_layout_table_find_script (hb_face_t *face,
242 unsigned int *script_index)
244 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
245 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
247 if (g.find_script_index (script_tag, script_index))
250 /* try finding 'DFLT' */
251 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
254 /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
255 * including many versions of DejaVu Sans Mono! */
256 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
259 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
264 hb_ot_layout_table_choose_script (hb_face_t *face,
266 const hb_tag_t *script_tags,
267 unsigned int *script_index)
269 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
270 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
274 if (g.find_script_index (*script_tags, script_index))
279 /* try finding 'DFLT' */
280 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
283 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
284 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
287 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
292 hb_ot_layout_table_get_feature_tags (hb_face_t *face,
294 unsigned int start_offset,
295 unsigned int *feature_count /* IN/OUT */,
296 hb_tag_t *feature_tags /* OUT */)
298 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
300 return g.get_feature_tags (start_offset, feature_count, feature_tags);
305 hb_ot_layout_script_get_language_tags (hb_face_t *face,
307 unsigned int script_index,
308 unsigned int start_offset,
309 unsigned int *language_count /* IN/OUT */,
310 hb_tag_t *language_tags /* OUT */)
312 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
314 return s.get_lang_sys_tags (start_offset, language_count, language_tags);
318 hb_ot_layout_script_find_language (hb_face_t *face,
320 unsigned int script_index,
321 hb_tag_t language_tag,
322 unsigned int *language_index)
324 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
325 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
327 if (s.find_lang_sys_index (language_tag, language_index))
330 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
331 if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
334 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
339 hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
341 unsigned int script_index,
342 unsigned int language_index,
343 unsigned int *feature_index)
345 const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
347 if (feature_index) *feature_index = l.get_required_feature_index ();
349 return l.has_required_feature ();
353 hb_ot_layout_language_get_feature_indexes (hb_face_t *face,
355 unsigned int script_index,
356 unsigned int language_index,
357 unsigned int start_offset,
358 unsigned int *feature_count /* IN/OUT */,
359 unsigned int *feature_indexes /* OUT */)
361 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
362 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
364 return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
368 hb_ot_layout_language_get_feature_tags (hb_face_t *face,
370 unsigned int script_index,
371 unsigned int language_index,
372 unsigned int start_offset,
373 unsigned int *feature_count /* IN/OUT */,
374 hb_tag_t *feature_tags /* OUT */)
376 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
377 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
379 ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
380 unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
383 unsigned int count = *feature_count;
384 for (unsigned int i = 0; i < count; i++)
385 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
393 hb_ot_layout_language_find_feature (hb_face_t *face,
395 unsigned int script_index,
396 unsigned int language_index,
397 hb_tag_t feature_tag,
398 unsigned int *feature_index)
400 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
401 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
402 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
404 unsigned int num_features = l.get_feature_count ();
405 for (unsigned int i = 0; i < num_features; i++) {
406 unsigned int f_index = l.get_feature_index (i);
408 if (feature_tag == g.get_feature_tag (f_index)) {
409 if (feature_index) *feature_index = f_index;
414 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
419 hb_ot_layout_feature_get_lookup_indexes (hb_face_t *face,
421 unsigned int feature_index,
422 unsigned int start_offset,
423 unsigned int *lookup_count /* IN/OUT */,
424 unsigned int *lookup_indexes /* OUT */)
426 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
427 const Feature &f = g.get_feature (feature_index);
429 return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
438 hb_ot_layout_has_substitution (hb_face_t *face)
440 return &_get_gsub (face) != &Null(GSUB);
444 hb_ot_layout_substitute_start (hb_buffer_t *buffer)
446 unsigned int count = buffer->len;
447 for (unsigned int i = 0; i < count; i++)
448 buffer->info[i].var1.u32 = buffer->info[i].var2.u32 = 0;
452 hb_ot_layout_substitute_lookup (hb_face_t *face,
454 unsigned int lookup_index,
457 return _get_gsub (face).substitute_lookup (face, buffer, lookup_index, mask);
461 hb_ot_layout_substitute_finish (hb_buffer_t *buffer HB_UNUSED)
471 hb_ot_layout_has_positioning (hb_face_t *face)
473 return &_get_gpos (face) != &Null(GPOS);
477 hb_ot_layout_position_start (hb_buffer_t *buffer)
479 buffer->clear_positions ();
483 hb_ot_layout_position_lookup (hb_font_t *font,
485 unsigned int lookup_index,
488 return _get_gpos (font->face).position_lookup (font, buffer, lookup_index, mask);
492 hb_ot_layout_position_finish (hb_buffer_t *buffer)
494 GPOS::position_finish (buffer);
503 _hb_ot_layout_get_upem (hb_face_t *face)
505 return _get_head (face).get_upem ();