hb-ot-layout.cc revision 4a26ea408c87f0bb59deca9ff44008d138471aa3
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_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_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_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
323
324unsigned int
325hb_ot_layout_get_language_count (hb_ot_layout_t            *layout,
326				 hb_ot_layout_table_type_t  table_type,
327				 unsigned int               script_index)
328{
329  const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
330
331  return s.get_lang_sys_count ();
332}
333
334hb_tag_t
335hb_ot_layout_get_language_tag (hb_ot_layout_t            *layout,
336			       hb_ot_layout_table_type_t  table_type,
337			       unsigned int               script_index,
338			       unsigned int               language_index)
339{
340  const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
341
342  return s.get_lang_sys_tag (language_index);
343}
344
345hb_bool_t
346hb_ot_layout_find_language (hb_ot_layout_t            *layout,
347			    hb_ot_layout_table_type_t  table_type,
348			    unsigned int               script_index,
349			    hb_tag_t                   language_tag,
350			    unsigned int              *language_index)
351{
352  ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
353  const Script &s = get_gsubgpos_table (layout, table_type).get_script (script_index);
354
355  if (s.find_lang_sys_index (language_tag, language_index))
356    return TRUE;
357
358  /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
359  if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
360    return FALSE;
361
362  if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
363  return FALSE;
364}
365
366hb_bool_t
367hb_ot_layout_get_required_feature_index (hb_ot_layout_t            *layout,
368					 hb_ot_layout_table_type_t  table_type,
369					 unsigned int               script_index,
370					 unsigned int               language_index,
371					 unsigned int              *feature_index)
372{
373  const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
374
375  if (feature_index) *feature_index = l.get_required_feature_index ();
376
377  return l.has_required_feature ();
378}
379
380unsigned int
381hb_ot_layout_get_feature_count (hb_ot_layout_t            *layout,
382				hb_ot_layout_table_type_t  table_type,
383				unsigned int               script_index,
384				unsigned int               language_index)
385{
386  const LangSys &l = get_gsubgpos_table (layout, table_type).get_script (script_index).get_lang_sys (language_index);
387
388  return l.get_feature_count ();
389}
390
391hb_tag_t
392hb_ot_layout_get_feature_tag (hb_ot_layout_t            *layout,
393			      hb_ot_layout_table_type_t  table_type,
394			      unsigned int               script_index,
395			      unsigned int               language_index,
396			      unsigned int               feature_index)
397{
398  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
399  const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
400
401  feature_index = l.get_feature_index (feature_index);
402
403  return g.get_feature_tag (feature_index);
404}
405
406
407hb_bool_t
408hb_ot_layout_find_feature (hb_ot_layout_t            *layout,
409			   hb_ot_layout_table_type_t  table_type,
410			   unsigned int               script_index,
411			   unsigned int               language_index,
412			   hb_tag_t                   feature_tag,
413			   unsigned int              *feature_index)
414{
415  ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
416  const GSUBGPOS &g = get_gsubgpos_table (layout, table_type);
417  const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
418  unsigned int i;
419
420  for (i = 0; i < l.get_feature_count (); i++) {
421    unsigned int f_index = l.get_feature_index (i);
422
423    if (feature_tag == g.get_feature_tag (f_index)) {
424      if (feature_index) *feature_index = f_index;
425      return TRUE;
426    }
427  }
428
429  if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
430  return FALSE;
431}
432