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