hb-ot-layout.cc revision ad6a6f22401d6256e34521d0f52e91348c5ed4c9
1/*
2 * Copyright © 1998-2004  David Turner and Werner Lemberg
3 * Copyright © 2006  Behdad Esfahbod
4 * Copyright © 2007,2008,2009  Red Hat, Inc.
5 *
6 *  This is part of HarfBuzz, a text shaping library.
7 *
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.
13 *
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
18 * DAMAGE.
19 *
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.
25 *
26 * Red Hat Author(s): Behdad Esfahbod
27 */
28
29#include "hb-ot-layout-private.hh"
30
31#include "hb-ot-layout-gdef-table.hh"
32#include "hb-ot-layout-gsub-table.hh"
33#include "hb-ot-layout-gpos-table.hh"
34#include "hb-ot-maxp-table.hh"
35
36
37#include <stdlib.h>
38#include <string.h>
39
40
41
42hb_ot_layout_t *
43_hb_ot_layout_create (hb_face_t *face)
44{
45  /* TODO Remove this object altogether */
46  hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
47
48  layout->gdef_blob = Sanitizer<GDEF>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GDEF));
49  layout->gdef = Sanitizer<GDEF>::lock_instance (layout->gdef_blob);
50
51  layout->gsub_blob = Sanitizer<GSUB>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GSUB));
52  layout->gsub = Sanitizer<GSUB>::lock_instance (layout->gsub_blob);
53
54  layout->gpos_blob = Sanitizer<GPOS>::sanitize (hb_face_reference_table (face, HB_OT_TAG_GPOS));
55  layout->gpos = Sanitizer<GPOS>::lock_instance (layout->gpos_blob);
56
57  return layout;
58}
59
60void
61_hb_ot_layout_destroy (hb_ot_layout_t *layout)
62{
63  hb_blob_destroy (layout->gdef_blob);
64  hb_blob_destroy (layout->gsub_blob);
65  hb_blob_destroy (layout->gpos_blob);
66
67  free (layout);
68}
69
70static inline const GDEF&
71_get_gdef (hb_face_t *face)
72{
73  return likely (face->ot_layout && face->ot_layout->gdef) ? *face->ot_layout->gdef : Null(GDEF);
74}
75static inline const GSUB&
76_get_gsub (hb_face_t *face)
77{
78  return likely (face->ot_layout && face->ot_layout->gsub) ? *face->ot_layout->gsub : Null(GSUB);
79}
80static inline const GPOS&
81_get_gpos (hb_face_t *face)
82{
83  return likely (face->ot_layout && face->ot_layout->gpos) ? *face->ot_layout->gpos : Null(GPOS);
84}
85
86
87/*
88 * GDEF
89 */
90
91hb_bool_t
92hb_ot_layout_has_glyph_classes (hb_face_t *face)
93{
94  return _get_gdef (face).has_glyph_classes ();
95}
96
97static inline unsigned int
98_hb_ot_layout_get_glyph_property (hb_face_t       *face,
99				  hb_glyph_info_t *info)
100{
101  if (!info->props_cache())
102  {
103    const GDEF &gdef = _get_gdef (face);
104    info->props_cache() = gdef.get_glyph_props (info->codepoint);
105  }
106
107  return info->props_cache();
108}
109
110static inline hb_bool_t
111_hb_ot_layout_match_properties_mark (hb_face_t      *face,
112				     hb_codepoint_t  glyph,
113				     unsigned int    glyph_props,
114				     unsigned int    lookup_props)
115{
116  /* If using mark filtering sets, the high short of
117   * lookup_props has the set index.
118   */
119  if (lookup_props & LookupFlag::UseMarkFilteringSet)
120    return _get_gdef (face).mark_set_covers (lookup_props >> 16, glyph);
121
122  /* The second byte of lookup_props has the meaning
123   * "ignore marks of attachment type different than
124   * the attachment type specified."
125   */
126  if (lookup_props & LookupFlag::MarkAttachmentType && glyph_props & LookupFlag::MarkAttachmentType)
127    return (lookup_props & LookupFlag::MarkAttachmentType) == (glyph_props & LookupFlag::MarkAttachmentType);
128
129  return true;
130}
131
132static inline hb_bool_t
133_hb_ot_layout_match_properties (hb_face_t      *face,
134				hb_codepoint_t  glyph,
135				unsigned int    glyph_props,
136				unsigned int    lookup_props)
137{
138  /* Not covered, if, for example, glyph class is ligature and
139   * lookup_props includes LookupFlags::IgnoreLigatures
140   */
141  if (glyph_props & lookup_props & LookupFlag::IgnoreFlags)
142    return false;
143
144  if (unlikely (glyph_props & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
145    return _hb_ot_layout_match_properties_mark (face, glyph, glyph_props, lookup_props);
146
147  return true;
148}
149
150hb_bool_t
151_hb_ot_layout_check_glyph_property (hb_face_t    *face,
152				    hb_glyph_info_t *ginfo,
153				    unsigned int  lookup_props,
154				    unsigned int *property_out)
155{
156  unsigned int property;
157
158  property = _hb_ot_layout_get_glyph_property (face, ginfo);
159  *property_out = property;
160
161  return _hb_ot_layout_match_properties (face, ginfo->codepoint, property, lookup_props);
162}
163
164hb_bool_t
165_hb_ot_layout_skip_mark (hb_face_t    *face,
166			 hb_glyph_info_t *ginfo,
167			 unsigned int  lookup_props,
168			 unsigned int *property_out)
169{
170  unsigned int property;
171
172  property = _hb_ot_layout_get_glyph_property (face, ginfo);
173  if (property_out)
174    *property_out = property;
175
176  /* If it's a mark, skip it we don't accept it. */
177  if (unlikely (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK))
178    return !_hb_ot_layout_match_properties (face, ginfo->codepoint, property, lookup_props);
179
180  /* If not a mark, don't skip. */
181  return false;
182}
183
184
185
186unsigned int
187hb_ot_layout_get_attach_points (hb_face_t      *face,
188				hb_codepoint_t  glyph,
189				unsigned int    start_offset,
190				unsigned int   *point_count /* IN/OUT */,
191				unsigned int   *point_array /* OUT */)
192{
193  return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
194}
195
196unsigned int
197hb_ot_layout_get_ligature_carets (hb_font_t      *font,
198				  hb_direction_t  direction,
199				  hb_codepoint_t  glyph,
200				  unsigned int    start_offset,
201				  unsigned int   *caret_count /* IN/OUT */,
202				  int            *caret_array /* OUT */)
203{
204  return _get_gdef (font->face).get_lig_carets (font, direction, glyph, start_offset, caret_count, caret_array);
205}
206
207/*
208 * GSUB/GPOS
209 */
210
211static const GSUBGPOS&
212get_gsubgpos_table (hb_face_t *face,
213		    hb_tag_t   table_tag)
214{
215  switch (table_tag) {
216    case HB_OT_TAG_GSUB: return _get_gsub (face);
217    case HB_OT_TAG_GPOS: return _get_gpos (face);
218    default:             return Null(GSUBGPOS);
219  }
220}
221
222
223unsigned int
224hb_ot_layout_table_get_script_tags (hb_face_t    *face,
225				    hb_tag_t      table_tag,
226				    unsigned int  start_offset,
227				    unsigned int *script_count /* IN/OUT */,
228				    hb_tag_t     *script_tags /* OUT */)
229{
230  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
231
232  return g.get_script_tags (start_offset, script_count, script_tags);
233}
234
235hb_bool_t
236hb_ot_layout_table_find_script (hb_face_t    *face,
237				hb_tag_t      table_tag,
238				hb_tag_t      script_tag,
239				unsigned int *script_index)
240{
241  ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
242  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
243
244  if (g.find_script_index (script_tag, script_index))
245    return true;
246
247  /* try finding 'DFLT' */
248  if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
249    return false;
250
251  /* try with 'dflt'; MS site has had typos and many fonts use it now :(.
252   * including many versions of DejaVu Sans Mono! */
253  if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
254    return false;
255
256  if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
257  return false;
258}
259
260hb_bool_t
261hb_ot_layout_table_choose_script (hb_face_t      *face,
262				  hb_tag_t        table_tag,
263				  const hb_tag_t *script_tags,
264				  unsigned int   *script_index,
265				  hb_tag_t       *chosen_script)
266{
267  ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
268  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
269
270  while (*script_tags)
271  {
272    if (g.find_script_index (*script_tags, script_index)) {
273      if (chosen_script)
274        *chosen_script = *script_tags;
275      return true;
276    }
277    script_tags++;
278  }
279
280  /* try finding 'DFLT' */
281  if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index)) {
282    if (chosen_script)
283      *chosen_script = HB_OT_TAG_DEFAULT_SCRIPT;
284    return false;
285  }
286
287  /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
288  if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index)) {
289    if (chosen_script)
290      *chosen_script = HB_OT_TAG_DEFAULT_LANGUAGE;
291    return false;
292  }
293
294  /* try with 'latn'; some old fonts put their features there even though
295     they're really trying to support Thai, for example :( */
296#define HB_OT_TAG_LATIN_SCRIPT		HB_TAG ('l', 'a', 't', 'n')
297  if (g.find_script_index (HB_OT_TAG_LATIN_SCRIPT, script_index)) {
298    if (chosen_script)
299      *chosen_script = HB_OT_TAG_LATIN_SCRIPT;
300    return false;
301  }
302
303  if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
304  if (chosen_script)
305    *chosen_script = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
306  return false;
307}
308
309unsigned int
310hb_ot_layout_table_get_feature_tags (hb_face_t    *face,
311				     hb_tag_t      table_tag,
312				     unsigned int  start_offset,
313				     unsigned int *feature_count /* IN/OUT */,
314				     hb_tag_t     *feature_tags /* OUT */)
315{
316  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
317
318  return g.get_feature_tags (start_offset, feature_count, feature_tags);
319}
320
321
322unsigned int
323hb_ot_layout_script_get_language_tags (hb_face_t    *face,
324				       hb_tag_t      table_tag,
325				       unsigned int  script_index,
326				       unsigned int  start_offset,
327				       unsigned int *language_count /* IN/OUT */,
328				       hb_tag_t     *language_tags /* OUT */)
329{
330  const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
331
332  return s.get_lang_sys_tags (start_offset, language_count, language_tags);
333}
334
335hb_bool_t
336hb_ot_layout_script_find_language (hb_face_t    *face,
337				   hb_tag_t      table_tag,
338				   unsigned int  script_index,
339				   hb_tag_t      language_tag,
340				   unsigned int *language_index)
341{
342  ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
343  const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
344
345  if (s.find_lang_sys_index (language_tag, language_index))
346    return true;
347
348  /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
349  if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
350    return false;
351
352  if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
353  return false;
354}
355
356hb_bool_t
357hb_ot_layout_language_get_required_feature_index (hb_face_t    *face,
358						  hb_tag_t      table_tag,
359						  unsigned int  script_index,
360						  unsigned int  language_index,
361						  unsigned int *feature_index)
362{
363  const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
364
365  if (feature_index) *feature_index = l.get_required_feature_index ();
366
367  return l.has_required_feature ();
368}
369
370unsigned int
371hb_ot_layout_language_get_feature_indexes (hb_face_t    *face,
372					   hb_tag_t      table_tag,
373					   unsigned int  script_index,
374					   unsigned int  language_index,
375					   unsigned int  start_offset,
376					   unsigned int *feature_count /* IN/OUT */,
377					   unsigned int *feature_indexes /* OUT */)
378{
379  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
380  const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
381
382  return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
383}
384
385unsigned int
386hb_ot_layout_language_get_feature_tags (hb_face_t    *face,
387					hb_tag_t      table_tag,
388					unsigned int  script_index,
389					unsigned int  language_index,
390					unsigned int  start_offset,
391					unsigned int *feature_count /* IN/OUT */,
392					hb_tag_t     *feature_tags /* OUT */)
393{
394  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
395  const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
396
397  ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
398  unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
399
400  if (feature_tags) {
401    unsigned int count = *feature_count;
402    for (unsigned int i = 0; i < count; i++)
403      feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
404  }
405
406  return ret;
407}
408
409
410hb_bool_t
411hb_ot_layout_language_find_feature (hb_face_t    *face,
412				    hb_tag_t      table_tag,
413				    unsigned int  script_index,
414				    unsigned int  language_index,
415				    hb_tag_t      feature_tag,
416				    unsigned int *feature_index)
417{
418  ASSERT_STATIC (Index::NOT_FOUND_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
419  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
420  const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
421
422  unsigned int num_features = l.get_feature_count ();
423  for (unsigned int i = 0; i < num_features; i++) {
424    unsigned int f_index = l.get_feature_index (i);
425
426    if (feature_tag == g.get_feature_tag (f_index)) {
427      if (feature_index) *feature_index = f_index;
428      return true;
429    }
430  }
431
432  if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
433  return false;
434}
435
436unsigned int
437hb_ot_layout_feature_get_lookup_indexes (hb_face_t    *face,
438					 hb_tag_t      table_tag,
439					 unsigned int  feature_index,
440					 unsigned int  start_offset,
441					 unsigned int *lookup_count /* IN/OUT */,
442					 unsigned int *lookup_indexes /* OUT */)
443{
444  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
445  const Feature &f = g.get_feature (feature_index);
446
447  return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
448}
449
450
451/*
452 * GSUB
453 */
454
455hb_bool_t
456hb_ot_layout_has_substitution (hb_face_t *face)
457{
458  return &_get_gsub (face) != &Null(GSUB);
459}
460
461void
462hb_ot_layout_substitute_start (hb_buffer_t  *buffer)
463{
464  GSUB::substitute_start (buffer);
465}
466
467hb_bool_t
468hb_ot_layout_substitute_lookup (hb_face_t    *face,
469				hb_buffer_t  *buffer,
470				unsigned int  lookup_index,
471				hb_mask_t     mask)
472{
473  hb_apply_context_t c (NULL, face, buffer, mask);
474  return _get_gsub (face).substitute_lookup (&c, lookup_index);
475}
476
477void
478hb_ot_layout_substitute_finish (hb_buffer_t  *buffer HB_UNUSED)
479{
480  GSUB::substitute_finish (buffer);
481}
482
483void
484hb_ot_layout_substitute_closure_lookup (hb_face_t    *face,
485				        hb_set_t     *glyphs,
486				        unsigned int  lookup_index)
487{
488  hb_closure_context_t c (face, glyphs);
489  _get_gsub (face).closure_lookup (&c, lookup_index);
490}
491
492/*
493 * GPOS
494 */
495
496hb_bool_t
497hb_ot_layout_has_positioning (hb_face_t *face)
498{
499  return &_get_gpos (face) != &Null(GPOS);
500}
501
502void
503hb_ot_layout_position_start (hb_buffer_t  *buffer)
504{
505  GPOS::position_start (buffer);
506}
507
508hb_bool_t
509hb_ot_layout_position_lookup   (hb_font_t    *font,
510				hb_buffer_t  *buffer,
511				unsigned int  lookup_index,
512				hb_mask_t     mask)
513{
514  hb_apply_context_t c (font, font->face, buffer, mask);
515  return _get_gpos (font->face).position_lookup (&c, lookup_index);
516}
517
518void
519hb_ot_layout_position_finish (hb_buffer_t  *buffer)
520{
521  GPOS::position_finish (buffer);
522}
523
524
525