2 * Copyright (C) 1998-2004 David Turner and Werner Lemberg
3 * Copyright (C) 2006 Behdad Esfahbod
4 * Copyright (C) 2007,2008 Red Hat, Inc.
6 * This is part of HarfBuzz, an OpenType Layout engine library.
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.
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
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.
26 * Red Hat Author(s): Behdad Esfahbod
29 #define HB_OT_LAYOUT_CC
31 #include "hb-ot-layout.h"
32 #include "hb-ot-layout-private.h"
34 #include "hb-ot-layout-open-private.h"
35 #include "hb-ot-layout-gdef-private.h"
36 #include "hb-ot-layout-gsub-private.h"
43 struct _HB_OT_Layout {
49 unsigned char *klasses;
56 hb_ot_layout_create (const char *font_data,
59 HB_OT_Layout *layout = (HB_OT_Layout *) calloc (1, sizeof (HB_OT_Layout));
61 const OpenTypeFontFile &font = OpenTypeFontFile::get_for_data (font_data);
62 const OpenTypeFontFace &face = font.get_face (face_index);
64 layout->gdef = &GDEF::get_for_data (font.get_table_data (face.get_table_by_tag (GDEF::Tag)));
65 layout->gsub = &GSUB::get_for_data (font.get_table_data (face.get_table_by_tag (GSUB::Tag)));
66 //layout->gpos = &GPOS::get_for_data (font.get_table_data (face.get_table_by_tag (GPOS::Tag)));
72 hb_ot_layout_destroy (HB_OT_Layout *layout)
82 hb_ot_layout_has_font_glyph_classes (HB_OT_Layout *layout)
84 return layout->gdef->has_glyph_classes ();
87 HB_OT_LAYOUT_INTERNAL hb_bool_t
88 _hb_ot_layout_has_new_glyph_classes (HB_OT_Layout *layout)
90 return layout->new_gdef.len > 0;
93 HB_OT_LAYOUT_INTERNAL hb_ot_layout_glyph_properties_t
94 _hb_ot_layout_get_glyph_properties (HB_OT_Layout *layout,
97 hb_ot_layout_class_t klass;
99 /* TODO old harfbuzz doesn't always parse mark attachments as it says it was
100 * introduced without a version bump, so it may not be safe */
101 klass = layout->gdef->get_mark_attachment_type (glyph);
105 klass = layout->gdef->get_glyph_class (glyph);
107 if (!klass && glyph < layout->new_gdef.len)
108 klass = layout->new_gdef.klasses[glyph];
112 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
113 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
114 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
115 case GDEF::MarkGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
116 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
120 HB_OT_LAYOUT_INTERNAL hb_bool_t
121 _hb_ot_layout_check_glyph_properties (HB_OT_Layout *layout,
123 hb_ot_layout_lookup_flags_t lookup_flags,
124 hb_ot_layout_glyph_properties_t *property)
126 hb_ot_layout_glyph_class_t basic_glyph_class;
127 hb_ot_layout_glyph_properties_t desired_attachment_class;
129 if (gitem->gproperties == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
131 gitem->gproperties = *property = _hb_ot_layout_get_glyph_properties (layout, gitem->gindex);
132 if (gitem->gproperties == HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED)
136 *property = gitem->gproperties;
138 /* If the glyph was found in the MarkAttachmentClass table,
139 * then that class value is the high byte of the result,
140 * otherwise the low byte contains the basic type of the glyph
141 * as defined by the GlyphClassDef table.
143 if (*property & LookupFlag::MarkAttachmentType)
144 basic_glyph_class = HB_OT_LAYOUT_GLYPH_CLASS_MARK;
146 basic_glyph_class = (hb_ot_layout_glyph_class_t) *property;
148 /* Not covered, if, for example, basic_glyph_class
149 * is HB_GDEF_LIGATURE and lookup_flags includes LookupFlags::IgnoreLigatures
151 if (lookup_flags & basic_glyph_class)
154 /* The high byte of lookup_flags has the meaning
155 * "ignore marks of attachment type different than
156 * the attachment type specified."
158 desired_attachment_class = lookup_flags & LookupFlag::MarkAttachmentType;
159 if (desired_attachment_class)
161 if (basic_glyph_class == HB_OT_LAYOUT_GLYPH_CLASS_MARK &&
162 *property != desired_attachment_class )
170 hb_ot_layout_glyph_class_t
171 hb_ot_layout_get_glyph_class (HB_OT_Layout *layout,
174 hb_ot_layout_glyph_properties_t properties;
175 hb_ot_layout_class_t klass;
177 properties = _hb_ot_layout_get_glyph_properties (layout, glyph);
179 if (properties & 0xFF00)
180 return HB_OT_LAYOUT_GLYPH_CLASS_MARK;
182 return (hb_ot_layout_glyph_class_t) properties;
186 hb_ot_layout_set_glyph_class (HB_OT_Layout *layout,
188 hb_ot_layout_glyph_class_t klass)
190 /* TODO optimize this, similar to old harfbuzz code for example */
192 hb_ot_layout_class_t gdef_klass;
193 int len = layout->new_gdef.len;
197 unsigned char *new_klasses;
199 new_len = len == 0 ? 120 : 2 * len;
202 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
204 if (G_UNLIKELY (!new_klasses))
207 memset (new_klasses + len, 0, new_len - len);
209 layout->new_gdef.klasses = new_klasses;
210 layout->new_gdef.len = new_len;
215 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
216 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
217 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
218 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
219 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
222 layout->new_gdef.klasses[glyph] = gdef_klass;
227 hb_ot_layout_build_glyph_classes (HB_OT_Layout *layout,
228 uint16_t num_total_glyphs,
230 unsigned char *klasses,
235 if (G_UNLIKELY (!count || !glyphs || !klasses))
238 if (layout->new_gdef.len == 0) {
239 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
240 layout->new_gdef.len = count;
243 for (i = 0; i < count; i++)
244 hb_ot_layout_set_glyph_class (layout, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);