hb-ot-layout.cc revision c44733596c6648e209c12349e18e35424edf3d59
1/*
2 * Copyright (C) 1998-2004  David Turner and Werner Lemberg
3 * Copyright (C) 2006  Behdad Esfahbod
4 * Copyright (C) 2007,2008  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.h"
32#include "hb-ot-layout-private.h"
33
34#include "hb-ot-layout-open-private.h"
35#include "hb-ot-layout-gdef-private.h"
36#include "hb-ot-layout-gsub-private.h"
37
38
39#include <stdlib.h>
40#include <string.h>
41
42
43struct _hb_ot_layout_t {
44  const GDEF *gdef;
45  const GSUB *gsub;
46  const /*XXX*/GSUBGPOS *gpos;
47
48  struct {
49    unsigned char *klasses;
50    unsigned int len;
51  } new_gdef;
52
53};
54
55hb_ot_layout_t *
56hb_ot_layout_create (void)
57{
58  hb_ot_layout_t *layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
59
60  layout->gdef = &NullGDEF;
61  layout->gsub = &NullGSUB;
62  layout->gpos = &/*XXX*/NullGSUBGPOS;
63
64  return layout;
65}
66
67hb_ot_layout_t *
68hb_ot_layout_create_for_data (const char *font_data,
69			      int         face_index)
70{
71  hb_ot_layout_t *layout;
72
73  if (HB_UNLIKELY (font_data == NULL))
74    return hb_ot_layout_create ();
75
76  layout = (hb_ot_layout_t *) calloc (1, sizeof (hb_ot_layout_t));
77
78  const OpenTypeFontFile &font = OpenTypeFontFile::get_for_data (font_data);
79  const OpenTypeFontFace &face = font.get_face (face_index);
80
81  layout->gdef = &GDEF::get_for_data (font.get_table_data (face.get_table_by_tag (GDEF::Tag)));
82  layout->gsub = &GSUB::get_for_data (font.get_table_data (face.get_table_by_tag (GSUB::Tag)));
83  layout->gpos = &/*XXX*/GSUBGPOS::get_for_data (font.get_table_data (face.get_table_by_tag (/*XXX*/GSUBGPOS::GPOSTag)));
84
85  return layout;
86}
87
88void
89hb_ot_layout_destroy (hb_ot_layout_t *layout)
90{
91  free (layout);
92}
93
94/*
95 * GDEF
96 */
97
98hb_bool_t
99hb_ot_layout_has_font_glyph_classes (hb_ot_layout_t *layout)
100{
101  return layout->gdef->has_glyph_classes ();
102}
103
104HB_OT_LAYOUT_INTERNAL hb_bool_t
105_hb_ot_layout_has_new_glyph_classes (hb_ot_layout_t *layout)
106{
107  return layout->new_gdef.len > 0;
108}
109
110HB_OT_LAYOUT_INTERNAL hb_ot_layout_glyph_properties_t
111_hb_ot_layout_get_glyph_properties (hb_ot_layout_t *layout,
112				    hb_glyph_t      glyph)
113{
114  hb_ot_layout_class_t klass;
115
116  /* TODO old harfbuzz doesn't always parse mark attachments as it says it was
117   * introduced without a version bump, so it may not be safe */
118  klass = layout->gdef->get_mark_attachment_type (glyph);
119  if (klass)
120    return klass << 8;
121
122  klass = layout->gdef->get_glyph_class (glyph);
123
124  if (!klass && glyph < layout->new_gdef.len)
125    klass = layout->new_gdef.klasses[glyph];
126
127  switch (klass) {
128  default:
129  case GDEF::UnclassifiedGlyph:	return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
130  case GDEF::BaseGlyph:		return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
131  case GDEF::LigatureGlyph:	return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
132  case GDEF::MarkGlyph:		return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
133  case GDEF::ComponentGlyph:	return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
134  }
135}
136
137HB_OT_LAYOUT_INTERNAL hb_bool_t
138_hb_ot_layout_check_glyph_properties (hb_ot_layout_t                  *layout,
139				      HB_GlyphItem                     gitem,
140				      hb_ot_layout_lookup_flags_t      lookup_flags,
141				      hb_ot_layout_glyph_properties_t *property)
142{
143  hb_ot_layout_glyph_class_t basic_glyph_class;
144  hb_ot_layout_glyph_properties_t desired_attachment_class;
145
146  if (gitem->gproperties == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
147  {
148    gitem->gproperties = *property = _hb_ot_layout_get_glyph_properties (layout, gitem->gindex);
149    if (gitem->gproperties == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED)
150      return false;
151  }
152
153  *property = gitem->gproperties;
154
155  /* If the glyph was found in the MarkAttachmentClass table,
156   * then that class value is the high byte of the result,
157   * otherwise the low byte contains the basic type of the glyph
158   * as defined by the GlyphClassDef table.
159   */
160  if (*property & LookupFlag::MarkAttachmentType)
161    basic_glyph_class = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
162  else
163    basic_glyph_class = (hb_ot_layout_glyph_class_t) *property;
164
165  /* Not covered, if, for example, basic_glyph_class
166   * is HB_GDEF_LIGATURE and lookup_flags includes LookupFlags::IgnoreLigatures
167   */
168  if (lookup_flags & basic_glyph_class)
169    return false;
170
171  /* The high byte of lookup_flags has the meaning
172   * "ignore marks of attachment type different than
173   * the attachment type specified."
174   */
175  desired_attachment_class = lookup_flags & LookupFlag::MarkAttachmentType;
176  if (desired_attachment_class)
177  {
178    if (basic_glyph_class == HB_OT_LAYOUT_GLYPH_CLASS_MARK &&
179        *property != desired_attachment_class )
180      return false;
181  }
182
183  return true;
184}
185
186
187hb_ot_layout_glyph_class_t
188hb_ot_layout_get_glyph_class (hb_ot_layout_t *layout,
189			      hb_glyph_t      glyph)
190{
191  hb_ot_layout_glyph_properties_t properties;
192  hb_ot_layout_class_t klass;
193
194  properties = _hb_ot_layout_get_glyph_properties (layout, glyph);
195
196  if (properties & 0xFF00)
197    return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
198
199  return (hb_ot_layout_glyph_class_t) properties;
200}
201
202void
203hb_ot_layout_set_glyph_class (hb_ot_layout_t             *layout,
204			      hb_glyph_t                  glyph,
205			      hb_ot_layout_glyph_class_t  klass)
206{
207  /* TODO optimize this, similar to old harfbuzz code for example */
208
209  hb_ot_layout_class_t gdef_klass;
210  int len = layout->new_gdef.len;
211
212  if (glyph >= len) {
213    int new_len;
214    unsigned char *new_klasses;
215
216    new_len = len == 0 ? 120 : 2 * len;
217    if (new_len > 65535)
218      new_len = 65535;
219    new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
220
221    if (G_UNLIKELY (!new_klasses))
222      return;
223
224    memset (new_klasses + len, 0, new_len - len);
225
226    layout->new_gdef.klasses = new_klasses;
227    layout->new_gdef.len = new_len;
228  }
229
230  switch (klass) {
231  default:
232  case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED:	gdef_klass = GDEF::UnclassifiedGlyph;	break;
233  case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH:	gdef_klass = GDEF::BaseGlyph;		break;
234  case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE:	gdef_klass = GDEF::LigatureGlyph;	break;
235  case HB_OT_LAYOUT_GLYPH_CLASS_MARK:		gdef_klass = GDEF::MarkGlyph;		break;
236  case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT:	gdef_klass = GDEF::ComponentGlyph;	break;
237  }
238
239  layout->new_gdef.klasses[glyph] = gdef_klass;
240  return;
241}
242
243void
244hb_ot_layout_build_glyph_classes (hb_ot_layout_t *layout,
245				  uint16_t        num_total_glyphs,
246				  hb_glyph_t     *glyphs,
247				  unsigned char  *klasses,
248				  uint16_t        count)
249{
250  int i;
251
252  if (G_UNLIKELY (!count || !glyphs || !klasses))
253    return;
254
255  if (layout->new_gdef.len == 0) {
256    layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
257    layout->new_gdef.len = count;
258  }
259
260  for (i = 0; i < count; i++)
261    hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
262}
263
264/*
265 * GSUB/GPOS
266 */
267
268static const GSUBGPOS&
269get_gsubgpos_table (hb_ot_layout_t            *layout,
270		    hb_ot_layout_table_type_t  table_type)
271{
272  switch (table_type) {
273    case HB_OT_LAYOUT_TABLE_TYPE_GSUB: return *(layout->gsub);
274    case HB_OT_LAYOUT_TABLE_TYPE_GPOS: return *(layout->gpos);
275    default:                           return NullGSUBGPOS;
276  }
277}
278
279
280unsigned int
281hb_ot_layout_table_get_script_count (hb_ot_layout_t            *layout,
282				     hb_ot_layout_table_type_t  table_type)
283{
284  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
285
286  return g.get_script_count ();
287}
288
289hb_tag_t
290hb_ot_layout_table_get_script_tag (hb_ot_layout_t            *layout,
291				   hb_ot_layout_table_type_t  table_type,
292				   unsigned int               script_index)
293{
294  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
295
296  return g.get_script_tag (script_index);
297}
298
299hb_bool_t
300hb_ot_layout_table_find_script (hb_ot_layout_t            *layout,
301				hb_ot_layout_table_type_t  table_type,
302				hb_tag_t                   script_tag,
303				unsigned int              *script_index)
304{
305  ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
306  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
307
308  if (g.find_script_index (script_tag, script_index))
309    return TRUE;
310
311  /* try finding 'DFLT' */
312  if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
313    return FALSE;
314
315  /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
316  if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
317    return FALSE;
318
319  if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
320  return FALSE;
321}
322
323unsigned int
324hb_ot_layout_table_get_feature_count (hb_ot_layout_t            *layout,
325				      hb_ot_layout_table_type_t  table_type)
326{
327  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
328
329  return g.get_feature_count ();
330}
331
332hb_tag_t
333hb_ot_layout_table_get_feature_tag (hb_ot_layout_t            *layout,
334				    hb_ot_layout_table_type_t  table_type,
335				    unsigned int               feature_index)
336{
337  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
338
339  return g.get_feature_tag (feature_index);
340}
341
342hb_bool_t
343hb_ot_layout_table_find_feature (hb_ot_layout_t            *layout,
344				 hb_ot_layout_table_type_t  table_type,
345				 hb_tag_t                   feature_tag,
346				 unsigned int              *feature_index)
347{
348  ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
349  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
350
351  if (g.find_feature_index (feature_tag, feature_index))
352    return TRUE;
353
354  if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
355  return FALSE;
356}
357
358unsigned int
359hb_ot_layout_table_get_lookup_count (hb_ot_layout_t            *layout,
360				     hb_ot_layout_table_type_t  table_type)
361{
362  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
363
364  return g.get_lookup_count ();
365}
366
367
368unsigned int
369hb_ot_layout_script_get_language_count (hb_ot_layout_t            *layout,
370					hb_ot_layout_table_type_t  table_type,
371					unsigned int               script_index)
372{
373  const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
374
375  return s.get_lang_sys_count ();
376}
377
378hb_tag_t
379hb_ot_layout_script_get_language_tag (hb_ot_layout_t            *layout,
380				      hb_ot_layout_table_type_t  table_type,
381				      unsigned int               script_index,
382				      unsigned int               language_index)
383{
384  const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
385
386  return s.get_lang_sys_tag (language_index);
387}
388
389hb_bool_t
390hb_ot_layout_script_find_language (hb_ot_layout_t            *layout,
391				   hb_ot_layout_table_type_t  table_type,
392				   unsigned int               script_index,
393				   hb_tag_t                   language_tag,
394				   unsigned int              *language_index)
395{
396  ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
397  const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
398
399  if (s.find_lang_sys_index (language_tag, language_index))
400    return TRUE;
401
402  /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
403  if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
404    return FALSE;
405
406  if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
407  return FALSE;
408}
409
410hb_bool_t
411hb_ot_layout_language_get_required_feature_index (hb_ot_layout_t            *layout,
412						  hb_ot_layout_table_type_t  table_type,
413						  unsigned int               script_index,
414						  unsigned int               language_index,
415						  unsigned int              *feature_index)
416{
417  const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
418
419  if (feature_index) *feature_index = l.get_required_feature_index ();
420
421  return l.has_required_feature ();
422}
423
424unsigned int
425hb_ot_layout_language_get_feature_count (hb_ot_layout_t            *layout,
426					 hb_ot_layout_table_type_t  table_type,
427					 unsigned int               script_index,
428					 unsigned int               language_index)
429{
430  const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
431
432  return l.get_feature_count ();
433}
434
435unsigned int
436hb_ot_layout_language_get_feature_index (hb_ot_layout_t            *layout,
437					 hb_ot_layout_table_type_t  table_type,
438					 unsigned int               script_index,
439					 unsigned int               language_index,
440					 unsigned int               num_feature)
441{
442  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
443  const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
444
445  return l.get_feature_index (num_feature);
446}
447
448hb_tag_t
449hb_ot_layout_language_get_feature_tag (hb_ot_layout_t            *layout,
450				       hb_ot_layout_table_type_t  table_type,
451				       unsigned int               script_index,
452				       unsigned int               language_index,
453				       unsigned int               num_feature)
454{
455  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
456  const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
457  unsigned int feature_index = l.get_feature_index (num_feature);
458
459  return g.get_feature_tag (feature_index);
460}
461
462
463hb_bool_t
464hb_ot_layout_language_find_feature (hb_ot_layout_t            *layout,
465				    hb_ot_layout_table_type_t  table_type,
466				    unsigned int               script_index,
467				    unsigned int               language_index,
468				    hb_tag_t                   feature_tag,
469				    unsigned int              *feature_index)
470{
471  ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
472  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
473  const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
474  unsigned int i;
475
476  for (i = 0; i < l.get_feature_count (); i++) {
477    unsigned int f_index = l.get_feature_index (i);
478
479    if (feature_tag == g.get_feature_tag (f_index)) {
480      if (feature_index) *feature_index = f_index;
481      return TRUE;
482    }
483  }
484
485  if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
486  return FALSE;
487}
488
489unsigned int
490hb_ot_layout_feature_get_lookup_count (hb_ot_layout_t            *layout,
491				       hb_ot_layout_table_type_t  table_type,
492				       unsigned int               feature_index)
493{
494  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
495  const Feature &f = g.get_feature (feature_index);
496
497  return f.get_lookup_count ();
498}
499
500unsigned int
501hb_ot_layout_feature_get_lookup_index (hb_ot_layout_t            *layout,
502				       hb_ot_layout_table_type_t  table_type,
503				       unsigned int               feature_index,
504				       unsigned int               num_lookup)
505{
506  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
507  const Feature &f = g.get_feature (feature_index);
508
509  return f.get_lookup_index (num_lookup);
510}
511