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, 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"
45 _hb_ot_layout_new (hb_face_t *face)
47 /* Remove this object altogether */
48 hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
50 layout->gdef_blob = Sanitizer<GDEF>::sanitize (hb_face_get_table (face, HB_OT_TAG_GDEF));
51 layout->gdef = Sanitizer<GDEF>::lock_instance (layout->gdef_blob);
53 layout->gsub_blob = Sanitizer<GSUB>::sanitize (hb_face_get_table (face, HB_OT_TAG_GSUB));
54 layout->gsub = Sanitizer<GSUB>::lock_instance (layout->gsub_blob);
56 layout->gpos_blob = Sanitizer<GPOS>::sanitize (hb_face_get_table (face, HB_OT_TAG_GPOS));
57 layout->gpos = Sanitizer<GPOS>::lock_instance (layout->gpos_blob);
63 _hb_ot_layout_free (hb_ot_layout_t *layout)
65 hb_blob_unlock (layout->gdef_blob);
66 hb_blob_unlock (layout->gsub_blob);
67 hb_blob_unlock (layout->gpos_blob);
69 hb_blob_destroy (layout->gdef_blob);
70 hb_blob_destroy (layout->gsub_blob);
71 hb_blob_destroy (layout->gpos_blob);
76 static inline const GDEF&
77 _get_gdef (hb_face_t *face)
79 return likely (face->ot_layout && face->ot_layout->gdef) ? *face->ot_layout->gdef : Null(GDEF);
82 static inline const GSUB&
83 _get_gsub (hb_face_t *face)
85 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);
100 hb_ot_layout_has_glyph_classes (hb_face_t *face)
102 return _get_gdef (face).has_glyph_classes ();
106 _hb_ot_layout_get_glyph_property (hb_face_t *face,
107 hb_glyph_info_t *info)
109 if (!info->gproperty())
111 const GDEF &gdef = _get_gdef (face);
112 info->gproperty() = gdef.get_glyph_props (info->codepoint);
115 return info->gproperty();
119 _hb_ot_layout_check_glyph_property (hb_face_t *face,
120 hb_glyph_info_t *ginfo,
121 unsigned int lookup_props,
122 unsigned int *property_out)
124 unsigned int property;
126 property = _hb_ot_layout_get_glyph_property (face, ginfo);
128 *property_out = property;
130 /* Not covered, if, for example, glyph class is ligature and
131 * lookup_props includes LookupFlags::IgnoreLigatures
133 if (property & lookup_props & LookupFlag::IgnoreFlags)
136 if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
138 /* If using mark filtering sets, the high short of
139 * lookup_props has the set index.
141 if (lookup_props & LookupFlag::UseMarkFilteringSet)
142 return _get_gdef (face).mark_set_covers (lookup_props >> 16, ginfo->codepoint);
144 /* The second byte of lookup_props has the meaning
145 * "ignore marks of attachment type different than
146 * the attachment type specified."
148 if (lookup_props & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
149 return (lookup_props & LookupFlag::MarkAttachmentType) == (property & LookupFlag::MarkAttachmentType);
156 hb_ot_layout_get_attach_points (hb_face_t *face,
157 hb_codepoint_t glyph,
158 unsigned int start_offset,
159 unsigned int *point_count /* IN/OUT */,
160 unsigned int *point_array /* OUT */)
162 return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
166 hb_ot_layout_get_ligature_carets (hb_font_t *font,
168 hb_direction_t direction,
169 hb_codepoint_t glyph,
170 unsigned int start_offset,
171 unsigned int *caret_count /* IN/OUT */,
172 int *caret_array /* OUT */)
174 hb_ot_layout_context_t c;
177 return _get_gdef (face).get_lig_carets (&c, direction, glyph, start_offset, caret_count, caret_array);
184 static const GSUBGPOS&
185 get_gsubgpos_table (hb_face_t *face,
189 case HB_OT_TAG_GSUB: return _get_gsub (face);
190 case HB_OT_TAG_GPOS: return _get_gpos (face);
191 default: return Null(GSUBGPOS);
197 hb_ot_layout_table_get_script_tags (hb_face_t *face,
199 unsigned int start_offset,
200 unsigned int *script_count /* IN/OUT */,
201 hb_tag_t *script_tags /* OUT */)
203 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
205 return g.get_script_tags (start_offset, script_count, script_tags);
209 hb_ot_layout_table_find_script (hb_face_t *face,
212 unsigned int *script_index)
214 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
215 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
217 if (g.find_script_index (script_tag, script_index))
220 /* try finding 'DFLT' */
221 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
224 /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
225 * including many versions of DejaVu Sans Mono! */
226 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
229 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
234 hb_ot_layout_table_choose_script (hb_face_t *face,
236 const hb_tag_t *script_tags,
237 unsigned int *script_index)
239 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
240 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
244 if (g.find_script_index (*script_tags, script_index))
249 /* try finding 'DFLT' */
250 if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
253 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
254 if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
257 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
262 hb_ot_layout_table_get_feature_tags (hb_face_t *face,
264 unsigned int start_offset,
265 unsigned int *feature_count /* IN/OUT */,
266 hb_tag_t *feature_tags /* OUT */)
268 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
270 return g.get_feature_tags (start_offset, feature_count, feature_tags);
275 hb_ot_layout_script_get_language_tags (hb_face_t *face,
277 unsigned int script_index,
278 unsigned int start_offset,
279 unsigned int *language_count /* IN/OUT */,
280 hb_tag_t *language_tags /* OUT */)
282 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
284 return s.get_lang_sys_tags (start_offset, language_count, language_tags);
288 hb_ot_layout_script_find_language (hb_face_t *face,
290 unsigned int script_index,
291 hb_tag_t language_tag,
292 unsigned int *language_index)
294 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
295 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
297 if (s.find_lang_sys_index (language_tag, language_index))
300 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
301 if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
304 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
309 hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
311 unsigned int script_index,
312 unsigned int language_index,
313 unsigned int *feature_index)
315 const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
317 if (feature_index) *feature_index = l.get_required_feature_index ();
319 return l.has_required_feature ();
323 hb_ot_layout_language_get_feature_indexes (hb_face_t *face,
325 unsigned int script_index,
326 unsigned int language_index,
327 unsigned int start_offset,
328 unsigned int *feature_count /* IN/OUT */,
329 unsigned int *feature_indexes /* OUT */)
331 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
332 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
334 return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
338 hb_ot_layout_language_get_feature_tags (hb_face_t *face,
340 unsigned int script_index,
341 unsigned int language_index,
342 unsigned int start_offset,
343 unsigned int *feature_count /* IN/OUT */,
344 hb_tag_t *feature_tags /* OUT */)
346 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
347 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
349 ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
350 unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
353 unsigned int count = *feature_count;
354 for (unsigned int i = 0; i < count; i++)
355 feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
363 hb_ot_layout_language_find_feature (hb_face_t *face,
365 unsigned int script_index,
366 unsigned int language_index,
367 hb_tag_t feature_tag,
368 unsigned int *feature_index)
370 ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
371 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
372 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
374 unsigned int num_features = l.get_feature_count ();
375 for (unsigned int i = 0; i < num_features; i++) {
376 unsigned int f_index = l.get_feature_index (i);
378 if (feature_tag == g.get_feature_tag (f_index)) {
379 if (feature_index) *feature_index = f_index;
384 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
389 hb_ot_layout_feature_get_lookup_indexes (hb_face_t *face,
391 unsigned int feature_index,
392 unsigned int start_offset,
393 unsigned int *lookup_count /* IN/OUT */,
394 unsigned int *lookup_indexes /* OUT */)
396 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
397 const Feature &f = g.get_feature (feature_index);
399 return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
408 hb_ot_layout_has_substitution (hb_face_t *face)
410 return &_get_gsub (face) != &Null(GSUB);
414 hb_ot_layout_substitute_lookup (hb_face_t *face,
416 unsigned int lookup_index,
419 hb_ot_layout_context_t c;
422 return _get_gsub (face).substitute_lookup (&c, buffer, lookup_index, mask);
431 hb_ot_layout_has_positioning (hb_face_t *face)
433 return &_get_gpos (face) != &Null(GPOS);
437 hb_ot_layout_position_lookup (hb_font_t *font,
440 unsigned int lookup_index,
443 hb_ot_layout_context_t c;
446 return _get_gpos (face).position_lookup (&c, buffer, lookup_index, mask);
450 hb_ot_layout_position_finish (hb_font_t *font HB_UNUSED,
451 hb_face_t *face HB_UNUSED,
455 unsigned int len = hb_buffer_get_length (buffer);
456 hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer);
457 hb_direction_t direction = buffer->props.direction;
461 /* Handle cursive connections:
462 * First handle all chain-back connections, then handle all chain-forward connections. */
463 if (likely (HB_DIRECTION_IS_HORIZONTAL (direction)))
465 for (j = 0; j < len; j++) {
466 if (pos[j].cursive_chain() < 0)
467 pos[j].y_offset += pos[j + pos[j].cursive_chain()].y_offset;
469 for (i = len; i > 0; i--) {
471 if (pos[j].cursive_chain() > 0)
472 pos[j].y_offset += pos[j + pos[j].cursive_chain()].y_offset;
477 for (j = 0; j < len; j++) {
478 if (pos[j].cursive_chain() < 0)
479 pos[j].x_offset += pos[j + pos[j].cursive_chain()].x_offset;
481 for (i = len; i > 0; i--) {
483 if (pos[j].cursive_chain() > 0)
484 pos[j].x_offset += pos[j + pos[j].cursive_chain()].x_offset;
489 /* Handle attachments */
490 for (i = 0; i < len; i++)
493 unsigned int back = i - pos[i].back();
494 pos[i].x_offset += pos[back].x_offset;
495 pos[i].y_offset += pos[back].y_offset;
497 if (HB_DIRECTION_IS_BACKWARD (buffer->props.direction))
498 for (j = back + 1; j < i + 1; j++) {
499 pos[i].x_offset += pos[j].x_advance;
500 pos[i].y_offset += pos[j].y_advance;
503 for (j = back; j < i; j++) {
504 pos[i].x_offset -= pos[j].x_advance;
505 pos[i].y_offset -= pos[j].y_advance;