blob: cc36fa2770169dd965881564a560c8e505a0e768 [file] [log] [blame]
Behdad Esfahbod4924aff2010-10-08 19:18:16 -04001/*
Behdad Esfahbod2409d5f2011-04-21 17:14:28 -04002 * Copyright © 2009,2010 Red Hat, Inc.
Behdad Esfahbod06a44e82013-04-21 15:13:08 -04003 * Copyright © 2010,2011,2012,2013 Google, Inc.
Behdad Esfahbod4924aff2010-10-08 19:18:16 -04004 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
12 *
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 *
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 *
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
27 */
28
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070029#ifndef HB_OT_MAP_HH
30#define HB_OT_MAP_HH
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040031
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070032#include "hb-buffer.hh"
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040033
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040034
Behdad Esfahbod71c9f842018-09-10 22:37:19 +020035#define HB_OT_MAP_MAX_BITS 8u
36#define HB_OT_MAP_MAX_VALUE ((1u << HB_OT_MAP_MAX_BITS) - 1u)
37
Behdad Esfahbod2265be02013-05-02 14:25:09 -040038struct hb_ot_shape_plan_t;
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040039
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040040static const hb_tag_t table_tags[2] = {HB_OT_TAG_GSUB, HB_OT_TAG_GPOS};
41
Behdad Esfahbod90645fb2011-05-27 18:13:31 -040042struct hb_ot_map_t
43{
44 friend struct hb_ot_map_builder_t;
45
46 public:
47
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040048 struct feature_map_t {
49 hb_tag_t tag; /* should be first for our bsearch to work */
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -040050 unsigned int index[2]; /* GSUB/GPOS */
51 unsigned int stage[2]; /* GSUB/GPOS */
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040052 unsigned int shift;
53 hb_mask_t mask;
Behdad Esfahbod39dede92010-10-13 15:54:06 -040054 hb_mask_t _1_mask; /* mask for value=1, for quick access */
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -050055 unsigned int needs_fallback : 1;
Behdad Esfahbodcdf1fd02017-07-14 12:43:34 +010056 unsigned int auto_zwnj : 1;
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -040057 unsigned int auto_zwj : 1;
David Corbettf05df642018-01-26 21:36:15 -050058 unsigned int random : 1;
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040059
Behdad Esfahboddb5f7ef2017-10-15 16:00:22 +020060 inline int cmp (const hb_tag_t *tag_) const
61 { return *tag_ < tag ? -1 : *tag_ > tag ? 1 : 0; }
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040062 };
63
64 struct lookup_map_t {
Behdad Esfahbodcfc507c2013-02-14 10:40:12 -050065 unsigned short index;
Behdad Esfahbodcdf1fd02017-07-14 12:43:34 +010066 unsigned short auto_zwnj : 1;
Behdad Esfahboda8cf7b42013-03-19 05:53:26 -040067 unsigned short auto_zwj : 1;
David Corbettc2a75e02018-01-25 14:22:03 -050068 unsigned short random : 1;
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040069 hb_mask_t mask;
70
Behdad Esfahbod0712e912017-10-29 17:01:47 -060071 static int cmp (const void *pa, const void *pb)
72 {
73 const lookup_map_t *a = (const lookup_map_t *) pa;
74 const lookup_map_t *b = (const lookup_map_t *) pb;
75 return a->index < b->index ? -1 : a->index > b->index ? 1 : 0;
76 }
Behdad Esfahbod4924aff2010-10-08 19:18:16 -040077 };
78
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -040079 typedef void (*pause_func_t) (const struct hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer);
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -040080
Behdad Esfahbod8ac3c9c2013-04-21 15:19:38 -040081 struct stage_map_t {
Behdad Esfahbod893f57b2013-04-21 15:21:49 -040082 unsigned int last_lookup; /* Cumulative */
83 pause_func_t pause_func;
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -040084 };
85
Behdad Esfahbodf9abbf82018-06-02 15:30:59 -070086 inline void init (void)
87 {
88 memset (this, 0, sizeof (*this));
89
90 features.init ();
91 for (unsigned int table_index = 0; table_index < 2; table_index++)
92 {
93 lookups[table_index].init ();
94 stages[table_index].init ();
95 }
96 }
97 inline void fini (void)
98 {
99 features.fini ();
100 for (unsigned int table_index = 0; table_index < 2; table_index++)
101 {
102 lookups[table_index].fini ();
103 stages[table_index].fini ();
104 }
105 }
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400106
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400107 inline hb_mask_t get_global_mask (void) const { return global_mask; }
108
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200109 inline hb_mask_t get_mask (hb_tag_t feature_tag, unsigned int *shift = nullptr) const {
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600110 const feature_map_t *map = features.bsearch (feature_tag);
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400111 if (shift) *shift = map ? map->shift : 0;
112 return map ? map->mask : 0;
113 }
114
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400115 inline bool needs_fallback (hb_tag_t feature_tag) const {
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600116 const feature_map_t *map = features.bsearch (feature_tag);
Behdad Esfahbodfabd3112012-09-05 22:19:28 -0400117 return map ? map->needs_fallback : false;
118 }
119
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400120 inline hb_mask_t get_1_mask (hb_tag_t feature_tag) const {
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600121 const feature_map_t *map = features.bsearch (feature_tag);
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400122 return map ? map->_1_mask : 0;
123 }
124
125 inline unsigned int get_feature_index (unsigned int table_index, hb_tag_t feature_tag) const {
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600126 const feature_map_t *map = features.bsearch (feature_tag);
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400127 return map ? map->index[table_index] : HB_OT_LAYOUT_NO_FEATURE_INDEX;
128 }
129
130 inline unsigned int get_feature_stage (unsigned int table_index, hb_tag_t feature_tag) const {
Behdad Esfahbodc479a592018-02-07 21:13:10 -0600131 const feature_map_t *map = features.bsearch (feature_tag);
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400132 return map ? map->stage[table_index] : (unsigned int) -1;
133 }
134
135 inline void get_stage_lookups (unsigned int table_index, unsigned int stage,
136 const struct lookup_map_t **plookups, unsigned int *lookup_count) const {
137 if (unlikely (stage == (unsigned int) -1)) {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200138 *plookups = nullptr;
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400139 *lookup_count = 0;
140 return;
141 }
Behdad Esfahbod8ac3c9c2013-04-21 15:19:38 -0400142 assert (stage <= stages[table_index].len);
Behdad Esfahbod893f57b2013-04-21 15:21:49 -0400143 unsigned int start = stage ? stages[table_index][stage - 1].last_lookup : 0;
144 unsigned int end = stage < stages[table_index].len ? stages[table_index][stage].last_lookup : lookups[table_index].len;
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200145 *plookups = end == start ? nullptr : &lookups[table_index][start];
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400146 *lookup_count = end - start;
147 }
148
Behdad Esfahbodf3064102012-11-15 18:39:46 -0800149 HB_INTERNAL void collect_lookups (unsigned int table_index, hb_set_t *lookups) const;
Behdad Esfahbodbac1dd62013-05-02 18:52:24 -0400150 template <typename Proxy>
151 HB_INTERNAL inline void apply (const Proxy &proxy,
152 const struct hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const;
Behdad Esfahbod8bb5deb2012-08-02 10:07:58 -0400153 HB_INTERNAL void substitute (const struct hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const;
154 HB_INTERNAL void position (const struct hb_ot_shape_plan_t *plan, hb_font_t *font, hb_buffer_t *buffer) const;
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400155
Behdad Esfahbod851784f2012-11-14 16:24:05 -0800156 public:
157 hb_tag_t chosen_script[2];
158 bool found_script[2];
Behdad Esfahbod610e5e82012-08-02 05:27:46 -0400159
160 private:
161
Behdad Esfahbod4924aff2010-10-08 19:18:16 -0400162 hb_mask_t global_mask;
163
Behdad Esfahbod5c3112a2018-05-01 19:07:04 -0400164 hb_vector_t<feature_map_t, 8> features;
165 hb_vector_t<lookup_map_t, 32> lookups[2]; /* GSUB/GPOS */
166 hb_vector_t<stage_map_t, 4> stages[2]; /* GSUB/GPOS */
Behdad Esfahbod4924aff2010-10-08 19:18:16 -0400167};
168
Behdad Esfahbodec544862013-02-14 11:25:10 -0500169enum hb_ot_map_feature_flags_t {
Behdad Esfahbod76271002014-07-11 14:54:42 -0400170 F_NONE = 0x0000u,
Behdad Esfahbod0f98fe82015-07-23 11:52:11 +0100171 F_GLOBAL = 0x0001u, /* Feature applies to all characters; results in no mask allocated for it. */
172 F_HAS_FALLBACK = 0x0002u, /* Has fallback implementation, so include mask bit even if feature not found. */
Behdad Esfahbodcdf1fd02017-07-14 12:43:34 +0100173 F_MANUAL_ZWNJ = 0x0004u, /* Don't skip over ZWNJ when matching **context**. */
174 F_MANUAL_ZWJ = 0x0008u, /* Don't skip over ZWJ when matching **input**. */
David Corbettf05df642018-01-26 21:36:15 -0500175 F_GLOBAL_SEARCH = 0x0010u, /* If feature not found in LangSys, look for it in global feature list and pick one. */
176 F_RANDOM = 0x0020u /* Randomly select a glyph from an AlternateSubstFormat1 subtable. */
Behdad Esfahbodec544862013-02-14 11:25:10 -0500177};
Chun-wei Fan167c3272015-11-09 17:17:56 +0800178HB_MARK_AS_FLAG_T (hb_ot_map_feature_flags_t);
Behdad Esfahbod41732f12013-02-27 20:40:54 -0500179/* Macro version for where const is desired. */
180#define F_COMBINE(l,r) (hb_ot_map_feature_flags_t ((unsigned int) (l) | (unsigned int) (r)))
Behdad Esfahbodec544862013-02-14 11:25:10 -0500181
Behdad Esfahbod4924aff2010-10-08 19:18:16 -0400182
Behdad Esfahbodf6d7a9b2011-07-07 16:20:35 -0400183struct hb_ot_map_builder_t
184{
185 public:
186
Behdad Esfahbod6fddf2d2012-11-12 17:57:24 -0800187 HB_INTERNAL hb_ot_map_builder_t (hb_face_t *face_,
188 const hb_segment_properties_t *props_);
Behdad Esfahbod83361862011-08-04 19:49:05 -0400189
Behdad Esfahbodf9abbf82018-06-02 15:30:59 -0700190 HB_INTERNAL ~hb_ot_map_builder_t (void);
191
Behdad Esfahbodec544862013-02-14 11:25:10 -0500192 HB_INTERNAL void add_feature (hb_tag_t tag, unsigned int value,
193 hb_ot_map_feature_flags_t flags);
Behdad Esfahbodf6d7a9b2011-07-07 16:20:35 -0400194
Behdad Esfahbode7ffcfa2013-02-14 11:05:56 -0500195 inline void add_global_bool_feature (hb_tag_t tag)
Behdad Esfahbodec544862013-02-14 11:25:10 -0500196 { add_feature (tag, 1, F_GLOBAL); }
Behdad Esfahbodf6d7a9b2011-07-07 16:20:35 -0400197
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400198 inline void add_gsub_pause (hb_ot_map_t::pause_func_t pause_func)
199 { add_pause (0, pause_func); }
200 inline void add_gpos_pause (hb_ot_map_t::pause_func_t pause_func)
201 { add_pause (1, pause_func); }
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400202
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700203 HB_INTERNAL void compile (hb_ot_map_t &m,
204 const int *coords,
205 unsigned int num_coords);
Behdad Esfahbodf6d7a9b2011-07-07 16:20:35 -0400206
Behdad Esfahbodf6d7a9b2011-07-07 16:20:35 -0400207 private:
208
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700209 HB_INTERNAL void add_lookups (hb_ot_map_t &m,
Behdad Esfahbod72ada4f2016-09-10 03:57:24 -0700210 unsigned int table_index,
211 unsigned int feature_index,
212 unsigned int variations_index,
213 hb_mask_t mask,
Behdad Esfahbodcdf1fd02017-07-14 12:43:34 +0100214 bool auto_zwnj = true,
David Corbettc2a75e02018-01-25 14:22:03 -0500215 bool auto_zwj = true,
216 bool random = false);
Behdad Esfahbodbde5e392016-09-10 02:43:20 -0700217
Behdad Esfahbodf6d7a9b2011-07-07 16:20:35 -0400218 struct feature_info_t {
219 hb_tag_t tag;
220 unsigned int seq; /* sequence#, used for stable sorting only */
221 unsigned int max_value;
Behdad Esfahbodec544862013-02-14 11:25:10 -0500222 hb_ot_map_feature_flags_t flags;
Behdad Esfahbodf6d7a9b2011-07-07 16:20:35 -0400223 unsigned int default_value; /* for non-global features, what should the unset glyphs take */
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400224 unsigned int stage[2]; /* GSUB/GPOS */
Behdad Esfahbodf6d7a9b2011-07-07 16:20:35 -0400225
Behdad Esfahbod0712e912017-10-29 17:01:47 -0600226 static int cmp (const void *pa, const void *pb)
227 {
228 const feature_info_t *a = (const feature_info_t *) pa;
229 const feature_info_t *b = (const feature_info_t *) pb;
230 return (a->tag != b->tag) ? (a->tag < b->tag ? -1 : 1) :
231 (a->seq < b->seq ? -1 : a->seq > b->seq ? 1 : 0);
232 }
Behdad Esfahbodf6d7a9b2011-07-07 16:20:35 -0400233 };
234
Behdad Esfahbod8ac3c9c2013-04-21 15:19:38 -0400235 struct stage_info_t {
236 unsigned int index;
Behdad Esfahbod893f57b2013-04-21 15:21:49 -0400237 hb_ot_map_t::pause_func_t pause_func;
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400238 };
239
Behdad Esfahbod3e38c0f2012-08-02 09:44:18 -0400240 HB_INTERNAL void add_pause (unsigned int table_index, hb_ot_map_t::pause_func_t pause_func);
Behdad Esfahbodd8d0c482011-07-07 21:22:08 -0400241
Behdad Esfahbod5ab38552012-11-12 18:27:42 -0800242 public:
243
Behdad Esfahbod6fddf2d2012-11-12 17:57:24 -0800244 hb_face_t *face;
245 hb_segment_properties_t props;
246
247 hb_tag_t chosen_script[2];
Behdad Esfahbod851784f2012-11-14 16:24:05 -0800248 bool found_script[2];
Behdad Esfahbod6fddf2d2012-11-12 17:57:24 -0800249 unsigned int script_index[2], language_index[2];
250
Behdad Esfahbod5ab38552012-11-12 18:27:42 -0800251 private:
252
Behdad Esfahbodb70c96d2011-07-07 21:07:41 -0400253 unsigned int current_stage[2]; /* GSUB/GPOS */
Behdad Esfahbod5c3112a2018-05-01 19:07:04 -0400254 hb_vector_t<feature_info_t, 32> feature_infos;
255 hb_vector_t<stage_info_t, 8> stages[2]; /* GSUB/GPOS */
Behdad Esfahbodf6d7a9b2011-07-07 16:20:35 -0400256};
257
258
Behdad Esfahbod4924aff2010-10-08 19:18:16 -0400259
Behdad Esfahbodc77ae402018-08-25 22:36:36 -0700260#endif /* HB_OT_MAP_HH */