hb-ot-layout.cc revision 52ea47767c7c35650ebddfba6ddc8203a3e33d3a
1/*
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.
5 *
6 *  This is part of HarfBuzz, an OpenType Layout engine 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#define HB_OT_LAYOUT_CC
30
31#include "hb-ot-layout-private.h"
32
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
37
38#include <stdlib.h>
39#include <string.h>
40
41
42void
43_hb_ot_layout_init (hb_face_t *face)
44{
45  hb_ot_layout_t *layout = &face->ot_layout;
46
47  layout->gdef_blob = Sanitizer<GDEF>::sanitize (hb_face_get_table (face, HB_OT_TAG_GDEF));
48  layout->gdef = &Sanitizer<GDEF>::lock_instance (layout->gdef_blob);
49
50  layout->gsub_blob = Sanitizer<GSUB>::sanitize (hb_face_get_table (face, HB_OT_TAG_GSUB));
51  layout->gsub = &Sanitizer<GSUB>::lock_instance (layout->gsub_blob);
52
53  layout->gpos_blob = Sanitizer<GPOS>::sanitize (hb_face_get_table (face, HB_OT_TAG_GPOS));
54  layout->gpos = &Sanitizer<GPOS>::lock_instance (layout->gpos_blob);
55}
56
57void
58_hb_ot_layout_fini (hb_face_t *face)
59{
60  hb_ot_layout_t *layout = &face->ot_layout;
61
62  hb_blob_unlock (layout->gdef_blob);
63  hb_blob_unlock (layout->gsub_blob);
64  hb_blob_unlock (layout->gpos_blob);
65
66  hb_blob_destroy (layout->gdef_blob);
67  hb_blob_destroy (layout->gsub_blob);
68  hb_blob_destroy (layout->gpos_blob);
69
70  free (layout->new_gdef.klasses);
71}
72
73static const GDEF&
74_get_gdef (hb_face_t *face)
75{
76  return HB_LIKELY (face->ot_layout.gdef) ? *face->ot_layout.gdef : Null(GDEF);
77}
78
79static const GSUB&
80_get_gsub (hb_face_t *face)
81{
82  return HB_LIKELY (face->ot_layout.gsub) ? *face->ot_layout.gsub : Null(GSUB);
83}
84
85static const GPOS&
86_get_gpos (hb_face_t *face)
87{
88  return HB_LIKELY (face->ot_layout.gpos) ? *face->ot_layout.gpos : Null(GPOS);
89}
90
91
92/*
93 * GDEF
94 */
95
96/* TODO the public class_t is a mess */
97
98hb_bool_t
99hb_ot_layout_has_glyph_classes (hb_face_t *face)
100{
101  return _get_gdef (face).has_glyph_classes ();
102}
103
104hb_bool_t
105_hb_ot_layout_has_new_glyph_classes (hb_face_t *face)
106{
107  return face->ot_layout.new_gdef.len > 0;
108}
109
110static unsigned int
111_hb_ot_layout_get_glyph_property (hb_face_t      *face,
112				  hb_codepoint_t  glyph)
113{
114  hb_ot_layout_class_t klass;
115  const GDEF &gdef = _get_gdef (face);
116
117  klass = gdef.get_glyph_class (glyph);
118
119  if (!klass && glyph < face->ot_layout.new_gdef.len)
120    klass = face->ot_layout.new_gdef.klasses[glyph];
121
122  switch (klass) {
123  default:
124  case GDEF::UnclassifiedGlyph:	return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
125  case GDEF::BaseGlyph:		return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
126  case GDEF::LigatureGlyph:	return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
127  case GDEF::ComponentGlyph:	return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
128  case GDEF::MarkGlyph:
129	klass = gdef.get_mark_attachment_type (glyph);
130	return HB_OT_LAYOUT_GLYPH_CLASS_MARK + (klass << 8);
131  }
132}
133
134hb_bool_t
135_hb_ot_layout_check_glyph_property (hb_face_t    *face,
136				    hb_internal_glyph_info_t *ginfo,
137				    unsigned int  lookup_flags,
138				    unsigned int *property_out)
139{
140  unsigned int property;
141
142  if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
143    ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint);
144  property = ginfo->gproperty;
145  if (property_out)
146    *property_out = property;
147
148  /* Not covered, if, for example, glyph class is ligature and
149   * lookup_flags includes LookupFlags::IgnoreLigatures
150   */
151  if (property & lookup_flags & LookupFlag::IgnoreFlags)
152    return false;
153
154  if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
155  {
156    /* If using mark filtering sets, the high short of
157     * lookup_flags has the set index.
158     */
159    if (lookup_flags & LookupFlag::UseMarkFilteringSet)
160      return _get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
161
162    /* The second byte of lookup_flags has the meaning
163     * "ignore marks of attachment type different than
164     * the attachment type specified."
165     */
166    if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
167      return (lookup_flags & LookupFlag::MarkAttachmentType) == (property & LookupFlag::MarkAttachmentType);
168  }
169
170  return true;
171}
172
173hb_bool_t
174_hb_ot_layout_skip_mark (hb_face_t    *face,
175			 hb_internal_glyph_info_t *ginfo,
176			 unsigned int  lookup_flags,
177			 unsigned int *property_out)
178{
179  unsigned int property;
180
181  if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
182    ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint);
183  property = ginfo->gproperty;
184  if (property_out)
185    *property_out = property;
186
187  if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
188  {
189    /* Skip mark if lookup_flags includes LookupFlags::IgnoreMarks */
190    if (lookup_flags & LookupFlag::IgnoreMarks)
191      return true;
192
193    /* If using mark filtering sets, the high short of lookup_flags has the set index. */
194    if (lookup_flags & LookupFlag::UseMarkFilteringSet)
195      return !_get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
196
197    /* The second byte of lookup_flags has the meaning "ignore marks of attachment type
198     * different than the attachment type specified." */
199    if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
200      return (lookup_flags & LookupFlag::MarkAttachmentType) != (property & LookupFlag::MarkAttachmentType);
201  }
202
203  return false;
204}
205
206void
207_hb_ot_layout_set_glyph_class (hb_face_t                  *face,
208			       hb_codepoint_t              glyph,
209			       hb_ot_layout_glyph_class_t  klass)
210{
211  if (HB_OBJECT_IS_INERT (face))
212    return;
213
214  /* TODO optimize this? similar to old harfbuzz code for example */
215
216  hb_ot_layout_t *layout = &face->ot_layout;
217  hb_ot_layout_class_t gdef_klass;
218  unsigned int len = layout->new_gdef.len;
219
220  if (HB_UNLIKELY (glyph > 65535))
221    return;
222
223  /* XXX this is not threadsafe */
224  if (glyph >= len) {
225    unsigned int new_len;
226    unsigned char *new_klasses;
227
228    new_len = len == 0 ? 120 : 2 * len;
229    while (new_len <= glyph)
230      new_len *= 2;
231
232    if (new_len > 65536)
233      new_len = 65536;
234    new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
235
236    if (HB_UNLIKELY (!new_klasses))
237      return;
238
239    memset (new_klasses + len, 0, new_len - len);
240
241    layout->new_gdef.klasses = new_klasses;
242    layout->new_gdef.len = new_len;
243  }
244
245  switch (klass) {
246  default:
247  case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED:	gdef_klass = GDEF::UnclassifiedGlyph;	break;
248  case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH:	gdef_klass = GDEF::BaseGlyph;		break;
249  case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE:	gdef_klass = GDEF::LigatureGlyph;	break;
250  case HB_OT_LAYOUT_GLYPH_CLASS_MARK:		gdef_klass = GDEF::MarkGlyph;		break;
251  case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT:	gdef_klass = GDEF::ComponentGlyph;	break;
252  }
253
254  layout->new_gdef.klasses[glyph] = gdef_klass;
255  return;
256}
257
258void
259_hb_ot_layout_set_glyph_property (hb_face_t      *face,
260				  hb_codepoint_t  glyph,
261				  unsigned int    property)
262{ _hb_ot_layout_set_glyph_class (face, glyph, (hb_ot_layout_glyph_class_t) (property & 0xff)); }
263
264
265hb_ot_layout_glyph_class_t
266hb_ot_layout_get_glyph_class (hb_face_t      *face,
267			      hb_codepoint_t  glyph)
268{
269  return (hb_ot_layout_glyph_class_t) (_hb_ot_layout_get_glyph_property (face, glyph) & 0xff);
270}
271
272void
273hb_ot_layout_set_glyph_class (hb_face_t                 *face,
274			      hb_codepoint_t             glyph,
275			      hb_ot_layout_glyph_class_t klass)
276{
277  _hb_ot_layout_set_glyph_class (face, glyph, klass);
278}
279
280void
281hb_ot_layout_build_glyph_classes (hb_face_t      *face,
282				  uint16_t        num_total_glyphs,
283				  hb_codepoint_t *glyphs,
284				  unsigned char  *klasses,
285				  uint16_t        count)
286{
287  if (HB_OBJECT_IS_INERT (face))
288    return;
289
290  hb_ot_layout_t *layout = &face->ot_layout;
291
292  if (HB_UNLIKELY (!count || !glyphs || !klasses))
293    return;
294
295  if (layout->new_gdef.len == 0) {
296    layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
297    layout->new_gdef.len = count;
298  }
299
300  for (unsigned int i = 0; i < count; i++)
301    _hb_ot_layout_set_glyph_class (face, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
302}
303
304unsigned int
305hb_ot_layout_get_attach_points (hb_face_t      *face,
306				hb_codepoint_t  glyph,
307				unsigned int    start_offset,
308				unsigned int   *point_count /* IN/OUT */,
309				unsigned int   *point_array /* OUT */)
310{
311  return _get_gdef (face).get_attach_points (glyph, start_offset, point_count, point_array);
312}
313
314unsigned int
315hb_ot_layout_get_lig_carets (hb_font_t      *font,
316			     hb_face_t      *face,
317			     hb_codepoint_t  glyph,
318			     unsigned int    start_offset,
319			     unsigned int   *caret_count /* IN/OUT */,
320			     int            *caret_array /* OUT */)
321{
322  hb_ot_layout_context_t context;
323  context.font = font;
324  context.face = face;
325  return _get_gdef (face).get_lig_carets (&context, glyph, start_offset, caret_count, caret_array);
326}
327
328/*
329 * GSUB/GPOS
330 */
331
332static const GSUBGPOS&
333get_gsubgpos_table (hb_face_t *face,
334		    hb_tag_t   table_tag)
335{
336  switch (table_tag) {
337    case HB_OT_TAG_GSUB: return _get_gsub (face);
338    case HB_OT_TAG_GPOS: return _get_gpos (face);
339    default:             return Null(GSUBGPOS);
340  }
341}
342
343
344unsigned int
345hb_ot_layout_table_get_script_tags (hb_face_t    *face,
346				    hb_tag_t      table_tag,
347				    unsigned int  start_offset,
348				    unsigned int *script_count /* IN/OUT */,
349				    hb_tag_t     *script_tags /* OUT */)
350{
351  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
352
353  return g.get_script_tags (start_offset, script_count, script_tags);
354}
355
356hb_bool_t
357hb_ot_layout_table_find_script (hb_face_t    *face,
358				hb_tag_t      table_tag,
359				hb_tag_t      script_tag,
360				unsigned int *script_index)
361{
362  ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
363  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
364
365  if (g.find_script_index (script_tag, script_index))
366    return TRUE;
367
368  /* try finding 'DFLT' */
369  if (g.find_script_index (HB_OT_TAG_DEFAULT_SCRIPT, script_index))
370    return FALSE;
371
372  /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
373  if (g.find_script_index (HB_OT_TAG_DEFAULT_LANGUAGE, script_index))
374    return FALSE;
375
376  if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
377  return FALSE;
378}
379
380unsigned int
381hb_ot_layout_table_get_feature_tags (hb_face_t    *face,
382				     hb_tag_t      table_tag,
383				     unsigned int  start_offset,
384				     unsigned int *feature_count /* IN/OUT */,
385				     hb_tag_t     *feature_tags /* OUT */)
386{
387  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
388
389  return g.get_feature_tags (start_offset, feature_count, feature_tags);
390}
391
392
393unsigned int
394hb_ot_layout_script_get_language_tags (hb_face_t    *face,
395				       hb_tag_t      table_tag,
396				       unsigned int  script_index,
397				       unsigned int  start_offset,
398				       unsigned int *language_count /* IN/OUT */,
399				       hb_tag_t     *language_tags /* OUT */)
400{
401  const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
402
403  return s.get_lang_sys_tags (start_offset, language_count, language_tags);
404}
405
406hb_bool_t
407hb_ot_layout_script_find_language (hb_face_t    *face,
408				   hb_tag_t      table_tag,
409				   unsigned int  script_index,
410				   hb_tag_t      language_tag,
411				   unsigned int *language_index)
412{
413  ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
414  const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
415
416  if (s.find_lang_sys_index (language_tag, language_index))
417    return TRUE;
418
419  /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
420  if (s.find_lang_sys_index (HB_OT_TAG_DEFAULT_LANGUAGE, language_index))
421    return FALSE;
422
423  if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
424  return FALSE;
425}
426
427hb_bool_t
428hb_ot_layout_language_get_required_feature_index (hb_face_t    *face,
429						  hb_tag_t      table_tag,
430						  unsigned int  script_index,
431						  unsigned int  language_index,
432						  unsigned int *feature_index)
433{
434  const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
435
436  if (feature_index) *feature_index = l.get_required_feature_index ();
437
438  return l.has_required_feature ();
439}
440
441unsigned int
442hb_ot_layout_language_get_feature_indexes (hb_face_t    *face,
443					   hb_tag_t      table_tag,
444					   unsigned int  script_index,
445					   unsigned int  language_index,
446					   unsigned int  start_offset,
447					   unsigned int *feature_count /* IN/OUT */,
448					   unsigned int *feature_indexes /* OUT */)
449{
450  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
451  const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
452
453  return l.get_feature_indexes (start_offset, feature_count, feature_indexes);
454}
455
456unsigned int
457hb_ot_layout_language_get_feature_tags (hb_face_t    *face,
458					hb_tag_t      table_tag,
459					unsigned int  script_index,
460					unsigned int  language_index,
461					unsigned int  start_offset,
462					unsigned int *feature_count /* IN/OUT */,
463					hb_tag_t     *feature_tags /* OUT */)
464{
465  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
466  const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
467
468  ASSERT_STATIC (sizeof (unsigned int) == sizeof (hb_tag_t));
469  unsigned int ret = l.get_feature_indexes (start_offset, feature_count, (unsigned int *) feature_tags);
470
471  if (feature_tags) {
472    unsigned int count = *feature_count;
473    for (unsigned int i = 0; i < count; i++)
474      feature_tags[i] = g.get_feature_tag ((unsigned int) feature_tags[i]);
475  }
476
477  return ret;
478}
479
480
481hb_bool_t
482hb_ot_layout_language_find_feature (hb_face_t    *face,
483				    hb_tag_t      table_tag,
484				    unsigned int  script_index,
485				    unsigned int  language_index,
486				    hb_tag_t      feature_tag,
487				    unsigned int *feature_index)
488{
489  ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
490  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
491  const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
492
493  unsigned int num_features = l.get_feature_count ();
494  for (unsigned int i = 0; i < num_features; i++) {
495    unsigned int f_index = l.get_feature_index (i);
496
497    if (feature_tag == g.get_feature_tag (f_index)) {
498      if (feature_index) *feature_index = f_index;
499      return TRUE;
500    }
501  }
502
503  if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
504  return FALSE;
505}
506
507unsigned int
508hb_ot_layout_feature_get_lookup_indexes (hb_face_t    *face,
509					 hb_tag_t      table_tag,
510					 unsigned int  feature_index,
511					 unsigned int  start_offset,
512					 unsigned int *lookup_count /* IN/OUT */,
513					 unsigned int *lookup_indexes /* OUT */)
514{
515  const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
516  const Feature &f = g.get_feature (feature_index);
517
518  return f.get_lookup_indexes (start_offset, lookup_count, lookup_indexes);
519}
520
521
522/*
523 * GSUB
524 */
525
526hb_bool_t
527hb_ot_layout_has_substitution (hb_face_t *face)
528{
529  return &_get_gsub (face) != &Null(GSUB);
530}
531
532hb_bool_t
533hb_ot_layout_substitute_lookup (hb_face_t    *face,
534				hb_buffer_t  *buffer,
535				unsigned int  lookup_index,
536				hb_mask_t     mask)
537{
538  hb_ot_layout_context_t context;
539  context.font = NULL;
540  context.face = face;
541  return _get_gsub (face).substitute_lookup (&context, buffer, lookup_index, mask);
542}
543
544
545/*
546 * GPOS
547 */
548
549hb_bool_t
550hb_ot_layout_has_positioning (hb_face_t *face)
551{
552  return &_get_gpos (face) != &Null(GPOS);
553}
554
555hb_bool_t
556hb_ot_layout_position_lookup   (hb_font_t    *font,
557				hb_face_t    *face,
558				hb_buffer_t  *buffer,
559				unsigned int  lookup_index,
560				hb_mask_t     mask)
561{
562  hb_ot_layout_context_t context;
563  context.font = font;
564  context.face = face;
565  return _get_gpos (face).position_lookup (&context, buffer, lookup_index, mask);
566}
567
568void
569hb_ot_layout_position_finish (hb_font_t    *font,
570			      hb_face_t    *face,
571			      hb_buffer_t  *buffer)
572{
573  unsigned int i, j;
574  unsigned int len = hb_buffer_get_length (buffer);
575  hb_internal_glyph_position_t *positions = (hb_internal_glyph_position_t *) hb_buffer_get_glyph_positions (buffer);
576
577  /* TODO: Vertical */
578
579  /* Handle cursive connections */
580  /* First handle all left-to-right connections */
581  for (j = 0; j < len; j++) {
582    if (positions[j].cursive_chain > 0) {
583      positions[j].y_offset += positions[j - positions[j].cursive_chain].y_offset;
584      positions[j].cursive_chain = 0;
585    }
586  }
587  /* Then handle all right-to-left connections */
588  for (i = len; i > 0; i--) {
589    j = i - 1;
590    if (positions[j].cursive_chain < 0) {
591      positions[j].y_offset += positions[j - positions[j].cursive_chain].y_offset;
592      positions[j].cursive_chain = 0;
593    }
594  }
595
596  /* Handle attachments */
597  for (i = 0; i < len; i++)
598    if (positions[i].back)
599      {
600	unsigned int back = i - positions[i].back;
601	positions[i].back = 0;
602	positions[i].x_offset += positions[back].x_offset;
603	positions[i].y_offset += positions[back].y_offset;
604
605	if (buffer->direction == HB_DIRECTION_RTL)
606	  for (j = back + 1; j < i + 1; j++) {
607	    positions[i].x_offset += positions[j].x_advance;
608	    positions[i].y_offset += positions[j].y_advance;
609	  }
610	else
611	  for (j = back; j < i; j++) {
612	    positions[i].x_offset -= positions[j].x_advance;
613	    positions[i].y_offset -= positions[j].y_advance;
614	  }
615      }
616}
617