blob: ef0a6b1274c85dd670af9a1b1c9fb5ef0bc46738 [file] [log] [blame]
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -05001/*
Behdad Esfahbod6f425b12008-01-24 19:38:56 -05002 * Copyright (C) 1998-2004 David Turner and Werner Lemberg
3 * Copyright (C) 2006 Behdad Esfahbod
Behdad Esfahbodee58aae2009-05-17 05:14:33 -04004 * Copyright (C) 2007,2008,2009 Red Hat, Inc.
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -05005 *
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
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050031#include "hb-ot-layout-private.h"
32
Behdad Esfahbod5f5b24f2009-08-02 20:03:12 -040033#include "hb-ot-layout-gdef-private.hh"
34#include "hb-ot-layout-gsub-private.hh"
35#include "hb-ot-layout-gpos-private.hh"
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050036
Behdad Esfahbod6f425b12008-01-24 19:38:56 -050037
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050038#include <stdlib.h>
Behdad Esfahbodaff831e2008-01-24 06:03:45 -050039#include <string.h>
40
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050041
Behdad Esfahbod0ead4812009-08-02 17:41:36 -040042void
Behdad Esfahbod70e0f2a2009-08-03 22:01:47 -040043_hb_ot_layout_init (hb_face_t *face)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -050044{
Behdad Esfahbod70e0f2a2009-08-03 22:01:47 -040045 hb_ot_layout_t *layout = &face->ot_layout;
46
47 /* XXX sanitize */
48
49 layout->gdef_blob = hb_face_get_table (face, HB_OT_TAG_GDEF);
50 layout->gdef = &GDEF::get_for_data (hb_blob_lock (layout->gdef_blob));
51
52 layout->gsub_blob = hb_face_get_table (face, HB_OT_TAG_GSUB);
53 layout->gsub = &GSUB::get_for_data (hb_blob_lock (layout->gsub_blob));
54
55 layout->gpos_blob = hb_face_get_table (face, HB_OT_TAG_GPOS);
56 layout->gpos = &GPOS::get_for_data (hb_blob_lock (layout->gpos_blob));
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -050057}
58
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050059void
Behdad Esfahbod70e0f2a2009-08-03 22:01:47 -040060_hb_ot_layout_fini (hb_face_t *face)
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050061{
Behdad Esfahbod70e0f2a2009-08-03 22:01:47 -040062 hb_ot_layout_t *layout = &face->ot_layout;
Behdad Esfahbodfd92a3d2008-01-24 03:11:09 -050063
Behdad Esfahbod70e0f2a2009-08-03 22:01:47 -040064 hb_blob_unlock (layout->gdef_blob);
65 hb_blob_unlock (layout->gsub_blob);
66 hb_blob_unlock (layout->gpos_blob);
67
68 hb_blob_destroy (layout->gdef_blob);
69 hb_blob_destroy (layout->gsub_blob);
70 hb_blob_destroy (layout->gpos_blob);
Behdad Esfahbod679f41f2009-08-04 21:32:06 -040071
72 free (layout->new_gdef.klasses);
Behdad Esfahbod2ebb89d2009-07-25 19:09:01 -040073}
74
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040075static const GDEF&
76_get_gdef (hb_face_t *face)
77{
Behdad Esfahbod70e0f2a2009-08-03 22:01:47 -040078 return HB_LIKELY (face->ot_layout.gdef) ? *face->ot_layout.gdef : Null(GDEF);
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040079}
80
81static const GSUB&
82_get_gsub (hb_face_t *face)
83{
Behdad Esfahbod70e0f2a2009-08-03 22:01:47 -040084 return HB_LIKELY (face->ot_layout.gsub) ? *face->ot_layout.gsub : Null(GSUB);
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040085}
86
87static const GPOS&
88_get_gpos (hb_face_t *face)
89{
Behdad Esfahbod70e0f2a2009-08-03 22:01:47 -040090 return HB_LIKELY (face->ot_layout.gpos) ? *face->ot_layout.gpos : Null(GPOS);
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -040091}
92
93
Behdad Esfahbod590d55c2008-01-24 19:13:50 -050094/*
95 * GDEF
96 */
97
Behdad Esfahbodd6aae5f2009-05-18 04:25:22 -040098/* TODO the public class_t is a mess */
Behdad Esfahbod30bd7632009-04-15 22:56:15 -040099
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500100hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400101hb_ot_layout_has_font_glyph_classes (hb_face_t *face)
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500102{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400103 return _get_gdef (face).has_glyph_classes ();
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500104}
105
Behdad Esfahbod347f0b82009-05-25 03:30:31 -0400106HB_INTERNAL hb_bool_t
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400107_hb_ot_layout_has_new_glyph_classes (hb_face_t *face)
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500108{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400109 return face->ot_layout.new_gdef.len > 0;
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500110}
111
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400112static unsigned int
113_hb_ot_layout_get_glyph_property (hb_face_t *face,
Behdad Esfahbod30bd7632009-04-15 22:56:15 -0400114 hb_codepoint_t glyph)
Behdad Esfahbodead428d2008-01-24 03:54:09 -0500115{
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500116 hb_ot_layout_class_t klass;
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400117 const GDEF &gdef = _get_gdef (face);
Behdad Esfahbodead428d2008-01-24 03:54:09 -0500118
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400119 klass = gdef.get_glyph_class (glyph);
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500120
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400121 if (!klass && glyph < face->ot_layout.new_gdef.len)
122 klass = face->ot_layout.new_gdef.klasses[glyph];
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500123
124 switch (klass) {
125 default:
126 case GDEF::UnclassifiedGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED;
127 case GDEF::BaseGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH;
128 case GDEF::LigatureGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE;
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500129 case GDEF::ComponentGlyph: return HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT;
Behdad Esfahbod5130c352009-05-26 15:45:41 -0400130 case GDEF::MarkGlyph:
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400131 klass = gdef.get_mark_attachment_type (glyph);
Behdad Esfahbod09c292e2009-05-26 19:48:16 -0400132 return HB_OT_LAYOUT_GLYPH_CLASS_MARK + (klass << 8);
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500133 }
134}
135
Behdad Esfahbod347f0b82009-05-25 03:30:31 -0400136HB_INTERNAL hb_bool_t
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400137_hb_ot_layout_check_glyph_property (hb_face_t *face,
Behdad Esfahbodf1322e52009-08-01 22:53:04 -0400138 hb_internal_glyph_info_t *ginfo,
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400139 unsigned int lookup_flags,
140 unsigned int *property_out)
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500141{
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400142 unsigned int property;
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500143
Behdad Esfahbodf1322e52009-08-01 22:53:04 -0400144 if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400145 ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint);
Behdad Esfahbodf1322e52009-08-01 22:53:04 -0400146 property = ginfo->gproperty;
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400147 if (property_out)
148 *property_out = property;
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500149
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400150 /* Not covered, if, for example, glyph class is ligature and
Behdad Esfahbod1246e412009-05-26 15:58:34 -0400151 * lookup_flags includes LookupFlags::IgnoreLigatures
Behdad Esfahbod6f425b12008-01-24 19:38:56 -0500152 */
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400153 if (property & lookup_flags)
Behdad Esfahbod6f425b12008-01-24 19:38:56 -0500154 return false;
155
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400156 if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
Behdad Esfahbod6f425b12008-01-24 19:38:56 -0500157 {
Behdad Esfahbod1246e412009-05-26 15:58:34 -0400158 /* If using mark filtering sets, the high short of
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400159 * lookup_flags has the set index.
Behdad Esfahbod1246e412009-05-26 15:58:34 -0400160 */
161 if (lookup_flags & LookupFlag::UseMarkFilteringSet)
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400162 return _get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
Behdad Esfahbod1246e412009-05-26 15:58:34 -0400163
164 /* The second byte of lookup_flags has the meaning
165 * "ignore marks of attachment type different than
166 * the attachment type specified."
167 */
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400168 if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
169 return (lookup_flags & LookupFlag::MarkAttachmentType) == (property & LookupFlag::MarkAttachmentType);
Behdad Esfahbod6f425b12008-01-24 19:38:56 -0500170 }
171
172 return true;
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500173}
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500174
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400175HB_INTERNAL hb_bool_t
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400176_hb_ot_layout_skip_mark (hb_face_t *face,
Behdad Esfahbodf1322e52009-08-01 22:53:04 -0400177 hb_internal_glyph_info_t *ginfo,
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400178 unsigned int lookup_flags,
179 unsigned int *property_out)
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400180{
181 unsigned int property;
182
Behdad Esfahbodf1322e52009-08-01 22:53:04 -0400183 if (ginfo->gproperty == HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN)
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400184 ginfo->gproperty = _hb_ot_layout_get_glyph_property (face, ginfo->codepoint);
Behdad Esfahbodf1322e52009-08-01 22:53:04 -0400185 property = ginfo->gproperty;
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400186 if (property_out)
187 *property_out = property;
188
189 if (property & HB_OT_LAYOUT_GLYPH_CLASS_MARK)
190 {
191 /* Skip mark if lookup_flags includes LookupFlags::IgnoreMarks */
192 if (lookup_flags & LookupFlag::IgnoreMarks)
193 return true;
194
195 /* If using mark filtering sets, the high short of lookup_flags has the set index. */
196 if (lookup_flags & LookupFlag::UseMarkFilteringSet)
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400197 return !_get_gdef (face).mark_set_covers (lookup_flags >> 16, ginfo->codepoint);
Behdad Esfahbod4189b922009-05-26 17:31:56 -0400198
199 /* The second byte of lookup_flags has the meaning "ignore marks of attachment type
200 * different than the attachment type specified." */
201 if (lookup_flags & LookupFlag::MarkAttachmentType && property & LookupFlag::MarkAttachmentType)
202 return (lookup_flags & LookupFlag::MarkAttachmentType) != (property & LookupFlag::MarkAttachmentType);
203 }
204
205 return false;
206}
207
Behdad Esfahbod347f0b82009-05-25 03:30:31 -0400208HB_INTERNAL void
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400209_hb_ot_layout_set_glyph_class (hb_face_t *face,
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400210 hb_codepoint_t glyph,
211 hb_ot_layout_glyph_class_t klass)
Behdad Esfahbodead428d2008-01-24 03:54:09 -0500212{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400213 if (HB_OBJECT_IS_INERT (face))
214 return;
215
Behdad Esfahbod2c802962009-08-02 15:20:22 -0400216 /* TODO optimize this? similar to old harfbuzz code for example */
Behdad Esfahbodead428d2008-01-24 03:54:09 -0500217
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400218 hb_ot_layout_t *layout = &face->ot_layout;
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500219 hb_ot_layout_class_t gdef_klass;
Behdad Esfahbod15164d92009-08-04 13:57:41 -0400220 unsigned int len = layout->new_gdef.len;
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500221
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400222 if (HB_UNLIKELY (glyph > 65535))
Behdad Esfahbod30bd7632009-04-15 22:56:15 -0400223 return;
224
Behdad Esfahbod2c802962009-08-02 15:20:22 -0400225 /* XXX this is not threadsafe */
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500226 if (glyph >= len) {
227 int new_len;
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500228 unsigned char *new_klasses;
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500229
230 new_len = len == 0 ? 120 : 2 * len;
231 if (new_len > 65535)
232 new_len = 65535;
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500233 new_klasses = (unsigned char *) realloc (layout->new_gdef.klasses, new_len * sizeof (unsigned char));
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500234
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400235 if (HB_UNLIKELY (!new_klasses))
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500236 return;
Behdad Esfahbodce48f032009-11-02 14:35:51 -0500237
Behdad Esfahbodaff831e2008-01-24 06:03:45 -0500238 memset (new_klasses + len, 0, new_len - len);
239
240 layout->new_gdef.klasses = new_klasses;
241 layout->new_gdef.len = new_len;
242 }
243
244 switch (klass) {
245 default:
246 case HB_OT_LAYOUT_GLYPH_CLASS_UNCLASSIFIED: gdef_klass = GDEF::UnclassifiedGlyph; break;
247 case HB_OT_LAYOUT_GLYPH_CLASS_BASE_GLYPH: gdef_klass = GDEF::BaseGlyph; break;
248 case HB_OT_LAYOUT_GLYPH_CLASS_LIGATURE: gdef_klass = GDEF::LigatureGlyph; break;
249 case HB_OT_LAYOUT_GLYPH_CLASS_MARK: gdef_klass = GDEF::MarkGlyph; break;
250 case HB_OT_LAYOUT_GLYPH_CLASS_COMPONENT: gdef_klass = GDEF::ComponentGlyph; break;
251 }
252
253 layout->new_gdef.klasses[glyph] = gdef_klass;
254 return;
Behdad Esfahbodead428d2008-01-24 03:54:09 -0500255}
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500256
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400257HB_INTERNAL void
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400258_hb_ot_layout_set_glyph_property (hb_face_t *face,
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400259 hb_codepoint_t glyph,
260 unsigned int property)
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400261{ _hb_ot_layout_set_glyph_class (face, glyph, (hb_ot_layout_glyph_class_t) (property & 0xff)); }
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400262
263
264hb_ot_layout_glyph_class_t
265hb_ot_layout_get_glyph_class (hb_face_t *face,
266 hb_codepoint_t glyph)
267{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400268 return (hb_ot_layout_glyph_class_t) (_hb_ot_layout_get_glyph_property (face, glyph) & 0xff);
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400269}
270
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500271void
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400272hb_ot_layout_set_glyph_class (hb_face_t *face,
273 hb_codepoint_t glyph,
274 hb_ot_layout_glyph_class_t klass)
275{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400276 _hb_ot_layout_set_glyph_class (face, glyph, klass);
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400277}
278
279void
280hb_ot_layout_build_glyph_classes (hb_face_t *face,
Behdad Esfahbode50c3972008-01-28 00:16:49 -0500281 uint16_t num_total_glyphs,
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400282 hb_codepoint_t *glyphs,
Behdad Esfahbode50c3972008-01-28 00:16:49 -0500283 unsigned char *klasses,
284 uint16_t count)
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500285{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400286 if (HB_OBJECT_IS_INERT (face))
287 return;
288
289 hb_ot_layout_t *layout = &face->ot_layout;
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400290
Behdad Esfahbod4acaffd2009-05-18 05:29:29 -0400291 if (HB_UNLIKELY (!count || !glyphs || !klasses))
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500292 return;
293
294 if (layout->new_gdef.len == 0) {
295 layout->new_gdef.klasses = (unsigned char *) calloc (num_total_glyphs, sizeof (unsigned char));
296 layout->new_gdef.len = count;
297 }
298
Behdad Esfahbodf4c95142009-05-17 04:59:56 -0400299 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400300 _hb_ot_layout_set_glyph_class (face, glyphs[i], (hb_ot_layout_glyph_class_t) klasses[i]);
Behdad Esfahbod590d55c2008-01-24 19:13:50 -0500301}
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500302
Behdad Esfahbod62964af2009-05-26 12:40:10 -0400303hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400304hb_ot_layout_get_attach_points (hb_face_t *face,
Behdad Esfahbod79420ad2009-05-26 12:24:16 -0400305 hb_codepoint_t glyph,
306 unsigned int *point_count /* IN/OUT */,
307 unsigned int *point_array /* OUT */)
308{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400309 return _get_gdef (face).get_attach_points (glyph, point_count, point_array);
Behdad Esfahbod62964af2009-05-26 12:40:10 -0400310}
311
312hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400313hb_ot_layout_get_lig_carets (hb_face_t *face,
314 hb_font_t *font,
Behdad Esfahbod62964af2009-05-26 12:40:10 -0400315 hb_codepoint_t glyph,
316 unsigned int *caret_count /* IN/OUT */,
317 int *caret_array /* OUT */)
318{
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400319 hb_ot_layout_context_t context;
320 context.font = font;
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400321 context.face = face;
322 return _get_gdef (face).get_lig_carets (&context, glyph, caret_count, caret_array);
Behdad Esfahbod79420ad2009-05-26 12:24:16 -0400323}
324
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500325/*
326 * GSUB/GPOS
327 */
328
329static const GSUBGPOS&
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400330get_gsubgpos_table (hb_face_t *face,
331 hb_tag_t table_tag)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500332{
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400333 switch (table_tag) {
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400334 case HB_OT_TAG_GSUB: return _get_gsub (face);
335 case HB_OT_TAG_GPOS: return _get_gpos (face);
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400336 default: return Null(GSUBGPOS);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500337 }
338}
339
340
341unsigned int
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400342hb_ot_layout_table_get_script_count (hb_face_t *face,
343 hb_tag_t table_tag)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500344{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400345 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500346
347 return g.get_script_count ();
348}
349
350hb_tag_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400351hb_ot_layout_table_get_script_tag (hb_face_t *face,
352 hb_tag_t table_tag,
353 unsigned int script_index)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500354{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400355 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500356
357 return g.get_script_tag (script_index);
358}
359
360hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400361hb_ot_layout_table_find_script (hb_face_t *face,
362 hb_tag_t table_tag,
363 hb_tag_t script_tag,
364 unsigned int *script_index)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500365{
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500366 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_SCRIPT_INDEX);
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400367 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500368
369 if (g.find_script_index (script_tag, script_index))
370 return TRUE;
371
372 /* try finding 'DFLT' */
373 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_SCRIPT, script_index))
374 return FALSE;
375
376 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
377 if (g.find_script_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, script_index))
378 return FALSE;
379
380 if (script_index) *script_index = HB_OT_LAYOUT_NO_SCRIPT_INDEX;
381 return FALSE;
382}
383
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500384unsigned int
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400385hb_ot_layout_table_get_feature_count (hb_face_t *face,
386 hb_tag_t table_tag)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500387{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400388 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500389
390 return g.get_feature_count ();
391}
392
393hb_tag_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400394hb_ot_layout_table_get_feature_tag (hb_face_t *face,
395 hb_tag_t table_tag,
396 unsigned int feature_index)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500397{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400398 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500399
400 return g.get_feature_tag (feature_index);
401}
402
403hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400404hb_ot_layout_table_find_feature (hb_face_t *face,
405 hb_tag_t table_tag,
406 hb_tag_t feature_tag,
407 unsigned int *feature_index)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500408{
409 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400410 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500411
412 if (g.find_feature_index (feature_tag, feature_index))
413 return TRUE;
414
415 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
416 return FALSE;
417}
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500418
419unsigned int
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400420hb_ot_layout_table_get_lookup_count (hb_face_t *face,
421 hb_tag_t table_tag)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500422{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400423 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500424
425 return g.get_lookup_count ();
426}
427
428
429unsigned int
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400430hb_ot_layout_script_get_language_count (hb_face_t *face,
431 hb_tag_t table_tag,
432 unsigned int script_index)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500433{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400434 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500435
436 return s.get_lang_sys_count ();
437}
438
439hb_tag_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400440hb_ot_layout_script_get_language_tag (hb_face_t *face,
441 hb_tag_t table_tag,
442 unsigned int script_index,
443 unsigned int language_index)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500444{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400445 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500446
447 return s.get_lang_sys_tag (language_index);
448}
449
450hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400451hb_ot_layout_script_find_language (hb_face_t *face,
452 hb_tag_t table_tag,
453 unsigned int script_index,
454 hb_tag_t language_tag,
455 unsigned int *language_index)
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500456{
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500457 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX);
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400458 const Script &s = get_gsubgpos_table (face, table_tag).get_script (script_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500459
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500460 if (s.find_lang_sys_index (language_tag, language_index))
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500461 return TRUE;
462
463 /* try with 'dflt'; MS site has had typos and many fonts use it now :( */
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500464 if (s.find_lang_sys_index (HB_OT_LAYOUT_TAG_DEFAULT_LANGUAGE, language_index))
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500465 return FALSE;
466
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500467 if (language_index) *language_index = HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500468 return FALSE;
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500469}
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500470
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500471hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400472hb_ot_layout_language_get_required_feature_index (hb_face_t *face,
473 hb_tag_t table_tag,
474 unsigned int script_index,
475 unsigned int language_index,
476 unsigned int *feature_index)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500477{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400478 const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500479
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500480 if (feature_index) *feature_index = l.get_required_feature_index ();
481
482 return l.has_required_feature ();
483}
484
485unsigned int
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400486hb_ot_layout_language_get_feature_count (hb_face_t *face,
487 hb_tag_t table_tag,
488 unsigned int script_index,
489 unsigned int language_index)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500490{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400491 const LangSys &l = get_gsubgpos_table (face, table_tag).get_script (script_index).get_lang_sys (language_index);
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500492
493 return l.get_feature_count ();
494}
495
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500496unsigned int
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400497hb_ot_layout_language_get_feature_index (hb_face_t *face,
498 hb_tag_t table_tag,
499 unsigned int script_index,
500 unsigned int language_index,
501 unsigned int num_feature)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500502{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400503 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500504 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
505
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500506 return l.get_feature_index (num_feature);
507}
508
509hb_tag_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400510hb_ot_layout_language_get_feature_tag (hb_face_t *face,
511 hb_tag_t table_tag,
512 unsigned int script_index,
513 unsigned int language_index,
514 unsigned int num_feature)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500515{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400516 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500517 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
518 unsigned int feature_index = l.get_feature_index (num_feature);
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500519
520 return g.get_feature_tag (feature_index);
521}
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500522
523
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500524hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400525hb_ot_layout_language_find_feature (hb_face_t *face,
526 hb_tag_t table_tag,
527 unsigned int script_index,
528 unsigned int language_index,
529 hb_tag_t feature_tag,
530 unsigned int *feature_index)
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500531{
532 ASSERT_STATIC (NO_INDEX == HB_OT_LAYOUT_NO_FEATURE_INDEX);
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400533 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500534 const LangSys &l = g.get_script (script_index).get_lang_sys (language_index);
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500535
Behdad Esfahbodf4c95142009-05-17 04:59:56 -0400536 unsigned int num_features = l.get_feature_count ();
537 for (unsigned int i = 0; i < num_features; i++) {
Behdad Esfahbod4a26ea42008-01-28 07:40:10 -0500538 unsigned int f_index = l.get_feature_index (i);
539
540 if (feature_tag == g.get_feature_tag (f_index)) {
541 if (feature_index) *feature_index = f_index;
542 return TRUE;
543 }
544 }
545
546 if (feature_index) *feature_index = HB_OT_LAYOUT_NO_FEATURE_INDEX;
Behdad Esfahbod706ab252008-01-28 05:58:50 -0500547 return FALSE;
548}
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500549
550unsigned int
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400551hb_ot_layout_feature_get_lookup_count (hb_face_t *face,
552 hb_tag_t table_tag,
553 unsigned int feature_index)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500554{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400555 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500556 const Feature &f = g.get_feature (feature_index);
557
558 return f.get_lookup_count ();
559}
560
561unsigned int
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400562hb_ot_layout_feature_get_lookup_index (hb_face_t *face,
563 hb_tag_t table_tag,
564 unsigned int feature_index,
565 unsigned int num_lookup)
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500566{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400567 const GSUBGPOS &g = get_gsubgpos_table (face, table_tag);
Behdad Esfahbodc4473352008-02-18 21:14:23 -0500568 const Feature &f = g.get_feature (feature_index);
569
570 return f.get_lookup_index (num_lookup);
571}
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400572
573/*
574 * GSUB
575 */
576
577hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400578hb_ot_layout_has_substitution (hb_face_t *face)
579{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400580 return &_get_gsub (face) != &Null(GSUB);
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400581}
582
583hb_bool_t
584hb_ot_layout_substitute_lookup (hb_face_t *face,
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400585 hb_buffer_t *buffer,
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400586 unsigned int lookup_index,
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400587 hb_ot_layout_feature_mask_t mask)
588{
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400589 hb_ot_layout_context_t context;
590 context.font = NULL;
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400591 context.face = face;
592 return _get_gsub (face).substitute_lookup (&context, buffer, lookup_index, mask);
Behdad Esfahbod5a0b7912009-04-16 04:45:30 -0400593}
Behdad Esfahbod2d15e722009-04-15 19:50:16 -0400594
Behdad Esfahbod9c42f052009-05-18 17:43:49 -0400595/*
596 * GPOS
597 */
598
599hb_bool_t
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400600hb_ot_layout_has_positioning (hb_face_t *face)
601{
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400602 return &_get_gpos (face) != &Null(GPOS);
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400603}
604
605hb_bool_t
606hb_ot_layout_position_lookup (hb_face_t *face,
607 hb_font_t *font,
Behdad Esfahbod9c42f052009-05-18 17:43:49 -0400608 hb_buffer_t *buffer,
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400609 unsigned int lookup_index,
Behdad Esfahbod9c42f052009-05-18 17:43:49 -0400610 hb_ot_layout_feature_mask_t mask)
611{
Behdad Esfahbod0ead4812009-08-02 17:41:36 -0400612 hb_ot_layout_context_t context;
613 context.font = font;
Behdad Esfahbod23c86aa2009-08-03 21:40:20 -0400614 context.face = face;
615 return _get_gpos (face).position_lookup (&context, buffer, lookup_index, mask);
Behdad Esfahbod9c42f052009-05-18 17:43:49 -0400616}