blob: 12a519534181f59a54f3a26e6f150a7332b38fc8 [file] [log] [blame]
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001/*
2 * Copyright © 2014 Google, Inc.
3 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
27#ifndef HB_OT_CMAP_TABLE_HH
28#define HB_OT_CMAP_TABLE_HH
29
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070030#include "hb-open-type.hh"
31#include "hb-set.hh"
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -040032
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -040033/*
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +043034 * cmap -- Character to Glyph Index Mapping
35 * https://docs.microsoft.com/en-us/typography/opentype/spec/cmap
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -040036 */
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -040037#define HB_OT_TAG_cmap HB_TAG('c','m','a','p')
38
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +043039namespace OT {
40
41
Behdad Esfahbodb7878cd2014-05-13 21:47:51 -040042struct CmapSubtableFormat0
43{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033044 bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
Behdad Esfahbodb7878cd2014-05-13 21:47:51 -040045 {
46 hb_codepoint_t gid = codepoint < 256 ? glyphIdArray[codepoint] : 0;
47 if (!gid)
48 return false;
49 *glyph = gid;
Behdad Esfahbod2ccc3222018-08-29 16:38:04 -070050 return true;
Behdad Esfahbodb7878cd2014-05-13 21:47:51 -040051 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033052 void collect_unicodes (hb_set_t *out) const
Behdad Esfahbod7d382fa2018-08-25 09:35:45 -070053 {
54 for (unsigned int i = 0; i < 256; i++)
55 if (glyphIdArray[i])
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +033056 out->add (i);
Behdad Esfahbod7d382fa2018-08-25 09:35:45 -070057 }
Behdad Esfahbodb7878cd2014-05-13 21:47:51 -040058
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033059 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +030060 {
Behdad Esfahbodb7878cd2014-05-13 21:47:51 -040061 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +010062 return_trace (c->check_struct (this));
Behdad Esfahbodb7878cd2014-05-13 21:47:51 -040063 }
64
65 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +010066 HBUINT16 format; /* Format number is set to 0. */
Behdad Esfahbod5b93f692018-05-02 14:59:14 -040067 HBUINT16 length; /* Byte length of this subtable. */
68 HBUINT16 language; /* Ignore. */
Behdad Esfahbod9aa2eb62018-02-11 19:00:42 -060069 HBUINT8 glyphIdArray[256];/* An array that maps character
Behdad Esfahbodb7878cd2014-05-13 21:47:51 -040070 * code to glyph index values. */
71 public:
72 DEFINE_SIZE_STATIC (6 + 256);
73};
74
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -040075struct CmapSubtableFormat4
76{
Qunxin Liu37572882019-06-25 13:17:30 -070077
78 template<typename Iterator,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +043079 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu37572882019-06-25 13:17:30 -070080 HBUINT16* serialize_endcode_array (hb_serialize_context_t *c,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +043081 Iterator it)
Garret Rieger295d67e2018-05-02 16:12:04 -070082 {
Qunxin Liu37572882019-06-25 13:17:30 -070083 HBUINT16 *endCode = c->start_embed<HBUINT16> ();
84 hb_codepoint_t prev_endcp = 0xFFFF;
Garret Rieger295d67e2018-05-02 16:12:04 -070085
Qunxin Liu37572882019-06-25 13:17:30 -070086 + it
87 | hb_apply ([&] (const hb_item_type<Iterator> _)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +043088 {
89 if (prev_endcp != 0xFFFF && prev_endcp + 1u != _.first)
90 {
91 HBUINT16 end_code;
92 end_code = prev_endcp;
93 c->copy<HBUINT16> (end_code);
94 }
95 prev_endcp = _.first;
96 })
Qunxin Liu37572882019-06-25 13:17:30 -070097 ;
98
99 {
100 // last endCode
101 HBUINT16 endcode;
102 endcode = prev_endcp;
103 if (unlikely (!c->copy<HBUINT16> (endcode))) return nullptr;
104 // There must be a final entry with end_code == 0xFFFF.
105 if (prev_endcp != 0xFFFF)
106 {
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430107 HBUINT16 finalcode;
108 finalcode = 0xFFFF;
109 if (unlikely (!c->copy<HBUINT16> (finalcode))) return nullptr;
Qunxin Liu37572882019-06-25 13:17:30 -0700110 }
111 }
112
113 return endCode;
114 }
115
116 template<typename Iterator,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430117 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu37572882019-06-25 13:17:30 -0700118 HBUINT16* serialize_startcode_array (hb_serialize_context_t *c,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430119 Iterator it)
Garret Rieger295d67e2018-05-02 16:12:04 -0700120 {
Qunxin Liu37572882019-06-25 13:17:30 -0700121 HBUINT16 *startCode = c->start_embed<HBUINT16> ();
122 hb_codepoint_t prev_cp = 0xFFFF;
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430123
Qunxin Liu37572882019-06-25 13:17:30 -0700124 + it
125 | hb_apply ([&] (const hb_item_type<Iterator> _)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430126 {
127 if (prev_cp == 0xFFFF || prev_cp + 1u != _.first)
128 {
129 HBUINT16 start_code;
130 start_code = _.first;
131 c->copy<HBUINT16> (start_code);
132 }
Garret Rieger4195a522018-05-02 17:11:18 -0700133
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430134 prev_cp = _.first;
135 })
Qunxin Liu37572882019-06-25 13:17:30 -0700136 ;
Garret Rieger4195a522018-05-02 17:11:18 -0700137
Qunxin Liu37572882019-06-25 13:17:30 -0700138 // There must be a final entry with end_code == 0xFFFF.
139 if (it.len () == 0 || prev_cp != 0xFFFF)
140 {
141 HBUINT16 finalcode;
142 finalcode = 0xFFFF;
143 if (unlikely (!c->copy<HBUINT16> (finalcode))) return nullptr;
144 }
145
146 return startCode;
147 }
148
149 template<typename Iterator,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430150 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu37572882019-06-25 13:17:30 -0700151 HBINT16* serialize_idDelta_array (hb_serialize_context_t *c,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430152 Iterator it,
153 HBUINT16 *endCode,
154 HBUINT16 *startCode,
155 unsigned segcount)
Qunxin Liu37572882019-06-25 13:17:30 -0700156 {
157 unsigned i = 0;
158 hb_codepoint_t last_gid = 0, start_gid = 0, last_cp = 0xFFFF;
159 bool use_delta = true;
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430160
Qunxin Liu37572882019-06-25 13:17:30 -0700161 HBINT16 *idDelta = c->start_embed<HBINT16> ();
162 if ((char *)idDelta - (char *)startCode != (int) segcount * (int) HBINT16::static_size)
163 return nullptr;
164
165 + it
166 | hb_apply ([&] (const hb_item_type<Iterator> _)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430167 {
168 if (_.first == startCode[i])
169 {
170 use_delta = true;
171 start_gid = _.second;
172 }
173 else if (_.second != last_gid + 1) use_delta = false;
Qunxin Liu37572882019-06-25 13:17:30 -0700174
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430175 if (_.first == endCode[i])
176 {
177 HBINT16 delta;
178 if (use_delta) delta = (int)start_gid - (int)startCode[i];
179 else delta = 0;
180 c->copy<HBINT16> (delta);
Qunxin Liu37572882019-06-25 13:17:30 -0700181
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430182 i++;
183 }
Qunxin Liu37572882019-06-25 13:17:30 -0700184
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430185 last_gid = _.second;
186 last_cp = _.first;
187 })
Qunxin Liu37572882019-06-25 13:17:30 -0700188 ;
189
190 if (it.len () == 0 || last_cp != 0xFFFF)
191 {
192 HBINT16 delta;
193 delta = 1;
194 if (unlikely (!c->copy<HBINT16> (delta))) return nullptr;
195 }
196
197 return idDelta;
198 }
199
200 template<typename Iterator,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430201 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu37572882019-06-25 13:17:30 -0700202 HBUINT16* serialize_rangeoffset_glyid (hb_serialize_context_t *c,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430203 Iterator it,
204 HBUINT16 *endCode,
205 HBUINT16 *startCode,
206 HBINT16 *idDelta,
207 unsigned segcount)
Qunxin Liu37572882019-06-25 13:17:30 -0700208 {
209 HBUINT16 *idRangeOffset = c->allocate_size<HBUINT16> (HBUINT16::static_size * segcount);
210 if (unlikely (!c->check_success (idRangeOffset))) return nullptr;
211 if (unlikely ((char *)idRangeOffset - (char *)idDelta != (int) segcount * (int) HBINT16::static_size)) return nullptr;
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430212
Qunxin Liu37572882019-06-25 13:17:30 -0700213 + hb_range (segcount)
214 | hb_filter ([&] (const unsigned _) { return idDelta[_] == 0; })
215 | hb_apply ([&] (const unsigned i)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430216 {
217 idRangeOffset[i] = 2 * (c->start_embed<HBUINT16> () - idRangeOffset - i);
Qunxin Liu37572882019-06-25 13:17:30 -0700218
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430219 + it
220 | hb_filter ([&] (const hb_item_type<Iterator> _) { return _.first >= startCode[i] && _.first <= endCode[i]; })
221 | hb_apply ([&] (const hb_item_type<Iterator> _)
222 {
223 HBUINT16 glyID;
224 glyID = _.second;
225 c->copy<HBUINT16> (glyID);
226 })
227 ;
Qunxin Liu37572882019-06-25 13:17:30 -0700228
229
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430230 })
Qunxin Liu37572882019-06-25 13:17:30 -0700231 ;
232
233 return idRangeOffset;
234 }
235
236 template<typename Iterator,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430237 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu37572882019-06-25 13:17:30 -0700238 void serialize (hb_serialize_context_t *c,
239 Iterator it)
240 {
241 unsigned table_initpos = c->length ();
242 if (unlikely (!c->extend_min (*this))) return;
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700243 this->format = 4;
Garret Rieger4195a522018-05-02 17:11:18 -0700244
Qunxin Liu37572882019-06-25 13:17:30 -0700245 //serialize endCode[]
246 HBUINT16 *endCode = serialize_endcode_array (c, it);
247 if (unlikely (!endCode)) return;
248
249 unsigned segcount = (c->length () - min_size) / HBUINT16::static_size;
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430250
Qunxin Liu37572882019-06-25 13:17:30 -0700251 // 2 bytes of padding.
252 if (unlikely (!c->allocate_size<HBUINT16> (HBUINT16::static_size))) return; // 2 bytes of padding.
253
254 // serialize startCode[]
255 HBUINT16 *startCode = serialize_startcode_array (c, it);
256 if (unlikely (!startCode)) return;
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430257
Qunxin Liu37572882019-06-25 13:17:30 -0700258 //serialize idDelta[]
259 HBINT16 *idDelta = serialize_idDelta_array (c, it, endCode, startCode, segcount);
260 if (unlikely (!idDelta)) return;
261
262 HBUINT16 *idRangeOffset = serialize_rangeoffset_glyid (c, it, endCode, startCode, idDelta, segcount);
263 if (unlikely (!c->check_success (idRangeOffset))) return;
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430264
Qunxin Liu37572882019-06-25 13:17:30 -0700265 if (unlikely (!c->check_assign(this->length, c->length () - table_initpos))) return;
266 this->segCountX2 = segcount * 2;
267 this->entrySelector = hb_max (1u, hb_bit_storage (segcount)) - 1;
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700268 this->searchRange = 2 * (1u << this->entrySelector);
Qunxin Liu37572882019-06-25 13:17:30 -0700269 this->rangeShift = segcount * 2 > this->searchRange
270 ? 2 * segcount - this->searchRange
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700271 : 0;
Garret Rieger295d67e2018-05-02 16:12:04 -0700272 }
273
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900274 struct accelerator_t
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400275 {
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330276 accelerator_t () {}
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330277 accelerator_t (const CmapSubtableFormat4 *subtable) { init (subtable); }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330278 ~accelerator_t () { fini (); }
Behdad Esfahbod56e0fd32018-10-29 22:35:44 -0700279
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330280 void init (const CmapSubtableFormat4 *subtable)
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900281 {
282 segCount = subtable->segCountX2 / 2;
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200283 endCount = subtable->values.arrayZ;
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900284 startCount = endCount + segCount + 1;
285 idDelta = startCount + segCount;
286 idRangeOffset = idDelta + segCount;
287 glyphIdArray = idRangeOffset + segCount;
288 glyphIdArrayLength = (subtable->length - 16 - 8 * segCount) / 2;
289 }
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330290 void fini () {}
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900291
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330292 bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900293 {
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900294 /* Custom two-array bsearch. */
Behdad Esfahbodb41c43b2018-08-25 15:25:03 -0700295 int min = 0, max = (int) this->segCount - 1;
296 const HBUINT16 *startCount = this->startCount;
297 const HBUINT16 *endCount = this->endCount;
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900298 unsigned int i;
299 while (min <= max)
300 {
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330301 int mid = ((unsigned int) min + (unsigned int) max) / 2;
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900302 if (codepoint < startCount[mid])
303 max = mid - 1;
304 else if (codepoint > endCount[mid])
305 min = mid + 1;
306 else
307 {
308 i = mid;
309 goto found;
310 }
311 }
312 return false;
313
314 found:
315 hb_codepoint_t gid;
Behdad Esfahbodb41c43b2018-08-25 15:25:03 -0700316 unsigned int rangeOffset = this->idRangeOffset[i];
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900317 if (rangeOffset == 0)
Behdad Esfahbodb41c43b2018-08-25 15:25:03 -0700318 gid = codepoint + this->idDelta[i];
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900319 else
320 {
321 /* Somebody has been smoking... */
Behdad Esfahbodb41c43b2018-08-25 15:25:03 -0700322 unsigned int index = rangeOffset / 2 + (codepoint - this->startCount[i]) + i - this->segCount;
323 if (unlikely (index >= this->glyphIdArrayLength))
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900324 return false;
Behdad Esfahbodb41c43b2018-08-25 15:25:03 -0700325 gid = this->glyphIdArray[index];
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900326 if (unlikely (!gid))
327 return false;
Behdad Esfahbodb41c43b2018-08-25 15:25:03 -0700328 gid += this->idDelta[i];
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900329 }
Behdad Esfahbod2ccc3222018-08-29 16:38:04 -0700330 gid &= 0xFFFFu;
331 if (!gid)
332 return false;
333 *glyph = gid;
334 return true;
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900335 }
Behdad Esfahbod95df00a2019-04-12 17:50:03 -0400336 HB_INTERNAL static bool get_glyph_func (const void *obj, hb_codepoint_t codepoint, hb_codepoint_t *glyph)
Behdad Esfahbodb41c43b2018-08-25 15:25:03 -0700337 {
338 return ((const accelerator_t *) obj)->get_glyph (codepoint, glyph);
339 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330340 void collect_unicodes (hb_set_t *out) const
Behdad Esfahbodb41c43b2018-08-25 15:25:03 -0700341 {
Behdad Esfahbodc8cfb702018-08-25 16:14:32 -0700342 unsigned int count = this->segCount;
343 if (count && this->startCount[count - 1] == 0xFFFFu)
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330344 count--; /* Skip sentinel segment. */
Behdad Esfahbodc8cfb702018-08-25 16:14:32 -0700345 for (unsigned int i = 0; i < count; i++)
Behdad Esfahbodb41c43b2018-08-25 15:25:03 -0700346 {
Behdad Esfahbod28634db2018-08-25 21:23:43 -0700347 unsigned int rangeOffset = this->idRangeOffset[i];
348 if (rangeOffset == 0)
349 out->add_range (this->startCount[i], this->endCount[i]);
350 else
351 {
352 for (hb_codepoint_t codepoint = this->startCount[i];
353 codepoint <= this->endCount[i];
354 codepoint++)
355 {
356 unsigned int index = rangeOffset / 2 + (codepoint - this->startCount[i]) + i - this->segCount;
357 if (unlikely (index >= this->glyphIdArrayLength))
358 break;
359 hb_codepoint_t gid = this->glyphIdArray[index];
360 if (unlikely (!gid))
361 continue;
362 out->add (codepoint);
363 }
364 }
Behdad Esfahbodb41c43b2018-08-25 15:25:03 -0700365 }
366 }
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900367
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100368 const HBUINT16 *endCount;
369 const HBUINT16 *startCount;
370 const HBUINT16 *idDelta;
371 const HBUINT16 *idRangeOffset;
372 const HBUINT16 *glyphIdArray;
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900373 unsigned int segCount;
Behdad Esfahbodc8a47452014-05-09 19:55:51 -0400374 unsigned int glyphIdArrayLength;
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900375 };
Behdad Esfahbodc8a47452014-05-09 19:55:51 -0400376
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330377 bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900378 {
Behdad Esfahbod56e0fd32018-10-29 22:35:44 -0700379 accelerator_t accel (this);
Behdad Esfahbod23335de2016-02-24 20:27:13 +0900380 return accel.get_glyph_func (&accel, codepoint, glyph);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400381 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330382 void collect_unicodes (hb_set_t *out) const
Behdad Esfahbodbd0e5422018-08-25 09:33:30 -0700383 {
Behdad Esfahbod56e0fd32018-10-29 22:35:44 -0700384 accelerator_t accel (this);
Behdad Esfahbodb41c43b2018-08-25 15:25:03 -0700385 accel.collect_unicodes (out);
Behdad Esfahbodbd0e5422018-08-25 09:33:30 -0700386 }
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400387
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330388 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod257d1ad2014-06-04 18:47:55 -0400389 {
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400390 TRACE_SANITIZE (this);
Behdad Esfahbod257d1ad2014-06-04 18:47:55 -0400391 if (unlikely (!c->check_struct (this)))
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100392 return_trace (false);
Behdad Esfahbod257d1ad2014-06-04 18:47:55 -0400393
394 if (unlikely (!c->check_range (this, length)))
395 {
396 /* Some broken fonts have too long of a "length" value.
397 * If that is the case, just change the value to truncate
398 * the subtable at the end of the blob. */
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700399 uint16_t new_length = (uint16_t) hb_min ((uintptr_t) 65535,
Behdad Esfahbod257d1ad2014-06-04 18:47:55 -0400400 (uintptr_t) (c->end -
401 (char *) this));
402 if (!c->try_set (&length, new_length))
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100403 return_trace (false);
Behdad Esfahbod257d1ad2014-06-04 18:47:55 -0400404 }
405
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100406 return_trace (16 + 4 * (unsigned int) segCountX2 <= length);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400407 }
408
Garret Rieger295d67e2018-05-02 16:12:04 -0700409
410
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400411 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100412 HBUINT16 format; /* Format number is set to 4. */
413 HBUINT16 length; /* This is the length in bytes of the
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400414 * subtable. */
Behdad Esfahbod5b93f692018-05-02 14:59:14 -0400415 HBUINT16 language; /* Ignore. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100416 HBUINT16 segCountX2; /* 2 x segCount. */
Behdad Esfahbod5b93f692018-05-02 14:59:14 -0400417 HBUINT16 searchRange; /* 2 * (2**floor(log2(segCount))) */
418 HBUINT16 entrySelector; /* log2(searchRange/2) */
419 HBUINT16 rangeShift; /* 2 x segCount - searchRange */
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400420
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200421 UnsizedArrayOf<HBUINT16>
422 values;
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400423#if 0
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100424 HBUINT16 endCount[segCount]; /* End characterCode for each segment,
Behdad Esfahbod76271002014-07-11 14:54:42 -0400425 * last=0xFFFFu. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100426 HBUINT16 reservedPad; /* Set to 0. */
427 HBUINT16 startCount[segCount]; /* Start character code for each segment. */
428 HBINT16 idDelta[segCount]; /* Delta for all character codes in segment. */
429 HBUINT16 idRangeOffset[segCount];/* Offsets into glyphIdArray or 0 */
Behdad Esfahboddff2c452018-09-10 23:29:26 +0200430 UnsizedArrayOf<HBUINT16>
431 glyphIdArray; /* Glyph index array (arbitrary length) */
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400432#endif
433
434 public:
435 DEFINE_SIZE_ARRAY (14, values);
436};
437
Behdad Esfahbodd294a2c2014-05-12 17:58:31 -0400438struct CmapSubtableLongGroup
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400439{
440 friend struct CmapSubtableFormat12;
Behdad Esfahbodd294a2c2014-05-12 17:58:31 -0400441 friend struct CmapSubtableFormat13;
Garret Rieger5dadbb02018-04-17 07:00:23 -0600442 template<typename U>
443 friend struct CmapSubtableLongSegmented;
Rod Sheeter1cd98d02018-02-08 19:39:57 -0800444 friend struct cmap;
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400445
446 int cmp (hb_codepoint_t codepoint) const
447 {
448 if (codepoint < startCharCode) return -1;
449 if (codepoint > endCharCode) return +1;
450 return 0;
451 }
452
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330453 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300454 {
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400455 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100456 return_trace (c->check_struct (this));
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400457 }
458
459 private:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100460 HBUINT32 startCharCode; /* First character code in this group. */
461 HBUINT32 endCharCode; /* Last character code in this group. */
462 HBUINT32 glyphID; /* Glyph index; interpretation depends on
Behdad Esfahbod7d382fa2018-08-25 09:35:45 -0700463 * subtable format. */
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400464 public:
465 DEFINE_SIZE_STATIC (12);
466};
Behdad Esfahbodec83b222018-11-23 19:58:49 -0500467DECLARE_NULL_NAMESPACE_BYTES (OT, CmapSubtableLongGroup);
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400468
Behdad Esfahbod94759e82014-05-13 21:17:28 -0400469template <typename UINT>
470struct CmapSubtableTrimmed
Behdad Esfahbod91bbfca2014-05-12 18:19:29 -0400471{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330472 bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
Behdad Esfahbod91bbfca2014-05-12 18:19:29 -0400473 {
474 /* Rely on our implicit array bound-checking. */
475 hb_codepoint_t gid = glyphIdArray[codepoint - startCharCode];
476 if (!gid)
477 return false;
478 *glyph = gid;
Behdad Esfahbod2ccc3222018-08-29 16:38:04 -0700479 return true;
Behdad Esfahbod91bbfca2014-05-12 18:19:29 -0400480 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330481 void collect_unicodes (hb_set_t *out) const
Behdad Esfahbod7d382fa2018-08-25 09:35:45 -0700482 {
483 hb_codepoint_t start = startCharCode;
484 unsigned int count = glyphIdArray.len;
485 for (unsigned int i = 0; i < count; i++)
486 if (glyphIdArray[i])
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +0330487 out->add (start + i);
Behdad Esfahbod7d382fa2018-08-25 09:35:45 -0700488 }
Behdad Esfahbod91bbfca2014-05-12 18:19:29 -0400489
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330490 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300491 {
Behdad Esfahbod91bbfca2014-05-12 18:19:29 -0400492 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100493 return_trace (c->check_struct (this) && glyphIdArray.sanitize (c));
Behdad Esfahbod91bbfca2014-05-12 18:19:29 -0400494 }
495
496 protected:
Behdad Esfahbod94759e82014-05-13 21:17:28 -0400497 UINT formatReserved; /* Subtable format and (maybe) padding. */
Behdad Esfahbod5b93f692018-05-02 14:59:14 -0400498 UINT length; /* Byte length of this subtable. */
499 UINT language; /* Ignore. */
Behdad Esfahbod94759e82014-05-13 21:17:28 -0400500 UINT startCharCode; /* First character code covered. */
Ebrahim Byagowid5120872019-09-14 10:36:29 +0430501 ArrayOf<HBGlyphID, UINT>
Behdad Esfahbod91bbfca2014-05-12 18:19:29 -0400502 glyphIdArray; /* Array of glyph index values for character
503 * codes in the range. */
504 public:
Behdad Esfahbod94759e82014-05-13 21:17:28 -0400505 DEFINE_SIZE_ARRAY (5 * sizeof (UINT), glyphIdArray);
Behdad Esfahbod91bbfca2014-05-12 18:19:29 -0400506};
507
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100508struct CmapSubtableFormat6 : CmapSubtableTrimmed<HBUINT16> {};
509struct CmapSubtableFormat10 : CmapSubtableTrimmed<HBUINT32 > {};
Behdad Esfahbod91bbfca2014-05-12 18:19:29 -0400510
Behdad Esfahbodca7b7742014-05-13 21:26:34 -0400511template <typename T>
512struct CmapSubtableLongSegmented
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400513{
Rod Sheeter9275bd02018-02-09 17:33:34 -0800514 friend struct cmap;
515
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330516 bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400517 {
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500518 hb_codepoint_t gid = T::group_get_glyph (groups.bsearch (codepoint), codepoint);
Behdad Esfahbod2ccc3222018-08-29 16:38:04 -0700519 if (!gid)
520 return false;
521 *glyph = gid;
522 return true;
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400523 }
524
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330525 void collect_unicodes (hb_set_t *out) const
Garret Rieger5dadbb02018-04-17 07:00:23 -0600526 {
527 for (unsigned int i = 0; i < this->groups.len; i++) {
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700528 out->add_range (this->groups[i].startCharCode,
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700529 hb_min ((hb_codepoint_t) this->groups[i].endCharCode,
Behdad Esfahbod82b12bc2018-08-25 22:07:17 -0700530 (hb_codepoint_t) HB_UNICODE_MAX));
Garret Rieger5dadbb02018-04-17 07:00:23 -0600531 }
532 }
533
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330534 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300535 {
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400536 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100537 return_trace (c->check_struct (this) && groups.sanitize (c));
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400538 }
539
540 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100541 HBUINT16 format; /* Subtable format; set to 12. */
Behdad Esfahbod5b93f692018-05-02 14:59:14 -0400542 HBUINT16 reserved; /* Reserved; set to 0. */
543 HBUINT32 length; /* Byte length of this subtable. */
544 HBUINT32 language; /* Ignore. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100545 SortedArrayOf<CmapSubtableLongGroup, HBUINT32>
Behdad Esfahbodd294a2c2014-05-12 17:58:31 -0400546 groups; /* Groupings. */
547 public:
548 DEFINE_SIZE_ARRAY (16, groups);
549};
550
Behdad Esfahbodca7b7742014-05-13 21:26:34 -0400551struct CmapSubtableFormat12 : CmapSubtableLongSegmented<CmapSubtableFormat12>
Behdad Esfahbodd294a2c2014-05-12 17:58:31 -0400552{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330553 static hb_codepoint_t group_get_glyph (const CmapSubtableLongGroup &group,
554 hb_codepoint_t u)
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500555 { return likely (group.startCharCode <= group.endCharCode) ?
556 group.glyphID + (u - group.startCharCode) : 0; }
Garret Rieger0053d132018-05-02 15:42:43 -0700557
558
Qunxin Liu37572882019-06-25 13:17:30 -0700559 template<typename Iterator,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430560 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu37572882019-06-25 13:17:30 -0700561 void serialize (hb_serialize_context_t *c,
562 Iterator it)
Garret Rieger0053d132018-05-02 15:42:43 -0700563 {
Qunxin Liu37572882019-06-25 13:17:30 -0700564 if (it.len () == 0) return;
565 unsigned table_initpos = c->length ();
566 if (unlikely (!c->extend_min (*this))) return;
567
568 hb_codepoint_t startCharCode = 0xFFFF, endCharCode = 0xFFFF;
569 hb_codepoint_t glyphID = 0;
570
571 + it
572 | hb_apply ([&] (const hb_item_type<Iterator> _)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430573 {
574 if (startCharCode == 0xFFFF)
575 {
576 startCharCode = _.first;
577 endCharCode = _.first;
578 glyphID = _.second;
579 }
580 else if (!_is_gid_consecutive (endCharCode, startCharCode, glyphID, _.first, _.second))
581 {
582 CmapSubtableLongGroup grouprecord;
583 grouprecord.startCharCode = startCharCode;
584 grouprecord.endCharCode = endCharCode;
585 grouprecord.glyphID = glyphID;
586 c->copy<CmapSubtableLongGroup> (grouprecord);
Qunxin Liu37572882019-06-25 13:17:30 -0700587
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430588 startCharCode = _.first;
589 endCharCode = _.first;
590 glyphID = _.second;
591 }
592 else
593 {
594 endCharCode = _.first;
595 }
596 })
Qunxin Liu37572882019-06-25 13:17:30 -0700597 ;
598
599 CmapSubtableLongGroup record;
600 record.startCharCode = startCharCode;
601 record.endCharCode = endCharCode;
602 record.glyphID = glyphID;
603 c->copy<CmapSubtableLongGroup> (record);
Garret Rieger0053d132018-05-02 15:42:43 -0700604
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -0700605 this->format = 12;
606 this->reserved = 0;
Qunxin Liu37572882019-06-25 13:17:30 -0700607 this->length = c->length () - table_initpos;
608 this->groups.len = (this->length - min_size)/CmapSubtableLongGroup::static_size;
Garret Rieger0053d132018-05-02 15:42:43 -0700609 }
610
Garret Riegera5fb44a2019-05-13 14:57:40 -0700611 static size_t get_sub_table_size (const hb_sorted_vector_t<CmapSubtableLongGroup> &groups_data)
Garret Rieger0053d132018-05-02 15:42:43 -0700612 {
Garret Riegera5fb44a2019-05-13 14:57:40 -0700613 return 16 + 12 * groups_data.length;
Garret Rieger0053d132018-05-02 15:42:43 -0700614 }
615
Garret Rieger0053d132018-05-02 15:42:43 -0700616 private:
Qunxin Liu37572882019-06-25 13:17:30 -0700617 static bool _is_gid_consecutive (hb_codepoint_t endCharCode,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430618 hb_codepoint_t startCharCode,
619 hb_codepoint_t glyphID,
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330620 hb_codepoint_t cp,
621 hb_codepoint_t new_gid)
Garret Rieger0053d132018-05-02 15:42:43 -0700622 {
Qunxin Liu37572882019-06-25 13:17:30 -0700623 return (cp - 1 == endCharCode) &&
624 new_gid == glyphID + (cp - startCharCode);
Garret Rieger0053d132018-05-02 15:42:43 -0700625 }
626
Behdad Esfahbodca7b7742014-05-13 21:26:34 -0400627};
Behdad Esfahbodd294a2c2014-05-12 17:58:31 -0400628
Behdad Esfahbodca7b7742014-05-13 21:26:34 -0400629struct CmapSubtableFormat13 : CmapSubtableLongSegmented<CmapSubtableFormat13>
630{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330631 static hb_codepoint_t group_get_glyph (const CmapSubtableLongGroup &group,
632 hb_codepoint_t u HB_UNUSED)
Behdad Esfahbodca7b7742014-05-13 21:26:34 -0400633 { return group.glyphID; }
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400634};
635
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400636typedef enum
637{
638 GLYPH_VARIANT_NOT_FOUND = 0,
639 GLYPH_VARIANT_FOUND = 1,
640 GLYPH_VARIANT_USE_DEFAULT = 2
641} glyph_variant_t;
642
643struct UnicodeValueRange
644{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330645 int cmp (const hb_codepoint_t &codepoint) const
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400646 {
647 if (codepoint < startUnicodeValue) return -1;
648 if (codepoint > startUnicodeValue + additionalCount) return +1;
649 return 0;
650 }
651
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330652 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300653 {
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400654 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100655 return_trace (c->check_struct (this));
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400656 }
657
Ebrahim Byagowi435b1872018-04-15 21:18:48 +0430658 HBUINT24 startUnicodeValue; /* First value in this range. */
Behdad Esfahbod4806b382018-08-25 15:56:07 -0700659 HBUINT8 additionalCount; /* Number of additional values in this
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400660 * range. */
661 public:
662 DEFINE_SIZE_STATIC (4);
663};
664
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700665struct DefaultUVS : SortedArrayOf<UnicodeValueRange, HBUINT32>
666{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330667 void collect_unicodes (hb_set_t *out) const
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700668 {
669 unsigned int count = len;
670 for (unsigned int i = 0; i < count; i++)
671 {
672 hb_codepoint_t first = arrayZ[i].startUnicodeValue;
Behdad Esfahbod41248cc2019-05-07 20:54:31 -0700673 hb_codepoint_t last = hb_min ((hb_codepoint_t) (first + arrayZ[i].additionalCount),
Behdad Esfahbod82b12bc2018-08-25 22:07:17 -0700674 (hb_codepoint_t) HB_UNICODE_MAX);
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700675 out->add_range (first, last);
676 }
677 }
678
Qunxin Liu2583afa2019-08-16 13:54:24 -0700679 DefaultUVS* copy (hb_serialize_context_t *c,
680 const hb_set_t *unicodes) const
681 {
682 DefaultUVS *out = c->start_embed<DefaultUVS> ();
683 if (unlikely (!out)) return nullptr;
684 auto snap = c->snapshot ();
685
686 HBUINT32 len;
687 len = 0;
688 if (unlikely (!c->copy<HBUINT32> (len))) return nullptr;
689 unsigned init_len = c->length ();
690
691 hb_codepoint_t lastCode = HB_MAP_VALUE_INVALID;
692 int count = -1;
693
694 + as_array ()
695 | hb_apply ([&] (const UnicodeValueRange& _)
696 {
697 + hb_range ((unsigned)_.additionalCount + 1)
698 | hb_apply ([&] (const unsigned addcnt)
699 {
700 unsigned curEntry = (unsigned)_.startUnicodeValue + addcnt;
Qunxin Liu43156662019-08-29 11:17:20 -0700701 if (!hb_set_has (unicodes, curEntry)) return;
702 count += 1;
703 if (lastCode == HB_MAP_VALUE_INVALID)
Qunxin Liu2583afa2019-08-16 13:54:24 -0700704 {
Qunxin Liu43156662019-08-29 11:17:20 -0700705 lastCode = curEntry;
706 } else if (lastCode + count != curEntry)
707 {
708 UnicodeValueRange rec;
709 rec.startUnicodeValue = lastCode;
710 rec.additionalCount = count - 1;
711 c->copy<UnicodeValueRange> (rec);
Qunxin Liu2583afa2019-08-16 13:54:24 -0700712
Qunxin Liu43156662019-08-29 11:17:20 -0700713 lastCode = curEntry;
714 count = 0;
Qunxin Liu2583afa2019-08-16 13:54:24 -0700715 }
716 })
717 ;
718 })
719 ;
720
721 if (lastCode != HB_MAP_VALUE_INVALID)
722 {
723 UnicodeValueRange rec;
724 rec.startUnicodeValue = lastCode;
725 rec.additionalCount = count;
726 c->copy<UnicodeValueRange> (rec);
727 }
728
729 if (c->length () - init_len == 0)
730 {
731 c->revert (snap);
732 return nullptr;
733 }
734 else
735 {
736 if (unlikely (!c->check_assign (out->len, (c->length () - init_len) / UnicodeValueRange::static_size))) return nullptr;
737 return out;
738 }
739 }
740
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700741 public:
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -0500742 DEFINE_SIZE_ARRAY (4, *this);
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700743};
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400744
745struct UVSMapping
746{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330747 int cmp (const hb_codepoint_t &codepoint) const
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400748 {
749 return unicodeValue.cmp (codepoint);
750 }
751
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330752 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300753 {
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400754 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100755 return_trace (c->check_struct (this));
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400756 }
757
Ebrahim Byagowi435b1872018-04-15 21:18:48 +0430758 HBUINT24 unicodeValue; /* Base Unicode value of the UVS */
Ebrahim Byagowid5120872019-09-14 10:36:29 +0430759 HBGlyphID glyphID; /* Glyph ID of the UVS */
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400760 public:
761 DEFINE_SIZE_STATIC (5);
762};
763
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700764struct NonDefaultUVS : SortedArrayOf<UVSMapping, HBUINT32>
765{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330766 void collect_unicodes (hb_set_t *out) const
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700767 {
768 unsigned int count = len;
769 for (unsigned int i = 0; i < count; i++)
770 out->add (arrayZ[i].glyphID);
771 }
772
Qunxin Liu078ddbd2019-08-07 13:17:26 -0700773 void closure_glyphs (const hb_set_t *unicodes,
774 hb_set_t *glyphset) const
775 {
776 + as_array ()
Qunxin Liu43156662019-08-29 11:17:20 -0700777 | hb_filter (unicodes, &UVSMapping::unicodeValue)
Qunxin Liu078ddbd2019-08-07 13:17:26 -0700778 | hb_map (&UVSMapping::glyphID)
779 | hb_sink (glyphset)
780 ;
781 }
782
Qunxin Liu2583afa2019-08-16 13:54:24 -0700783 NonDefaultUVS* copy (hb_serialize_context_t *c,
784 const hb_set_t *unicodes,
785 const hb_set_t *glyphs,
786 const hb_map_t *glyph_map) const
787 {
788 NonDefaultUVS *out = c->start_embed<NonDefaultUVS> ();
789 if (unlikely (!out)) return nullptr;
790
791 auto it =
792 + as_array ()
793 | hb_filter ([&] (const UVSMapping& _)
794 {
795 return hb_set_has (unicodes, _.unicodeValue) || hb_set_has (glyphs, _.glyphID);
796 })
797 ;
798
799 if (!it) return nullptr;
800
801 HBUINT32 len;
802 len = it.len ();
803 if (unlikely (!c->copy<HBUINT32> (len))) return nullptr;
804
805 + it
806 | hb_apply ([&] (const UVSMapping& _)
807 {
808 UVSMapping mapping;
809 mapping.unicodeValue = _.unicodeValue;
810 mapping.glyphID = glyph_map->get (_.glyphID);
811 c->copy<UVSMapping> (mapping);
812 })
813 ;
814
815 return out;
816 }
817
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700818 public:
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -0500819 DEFINE_SIZE_ARRAY (4, *this);
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700820};
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400821
822struct VariationSelectorRecord
823{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330824 glyph_variant_t get_glyph (hb_codepoint_t codepoint,
825 hb_codepoint_t *glyph,
826 const void *base) const
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400827 {
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500828 if ((base+defaultUVS).bfind (codepoint))
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400829 return GLYPH_VARIANT_USE_DEFAULT;
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500830 const UVSMapping &nonDefault = (base+nonDefaultUVS).bsearch (codepoint);
831 if (nonDefault.glyphID)
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400832 {
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500833 *glyph = nonDefault.glyphID;
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400834 return GLYPH_VARIANT_FOUND;
835 }
836 return GLYPH_VARIANT_NOT_FOUND;
837 }
838
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330839 void collect_unicodes (hb_set_t *out, const void *base) const
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700840 {
841 (base+defaultUVS).collect_unicodes (out);
842 (base+nonDefaultUVS).collect_unicodes (out);
843 }
844
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330845 int cmp (const hb_codepoint_t &variation_selector) const
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400846 {
847 return varSelector.cmp (variation_selector);
848 }
849
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330850 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300851 {
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400852 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100853 return_trace (c->check_struct (this) &&
854 defaultUVS.sanitize (c, base) &&
855 nonDefaultUVS.sanitize (c, base));
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400856 }
857
Qunxin Liu2583afa2019-08-16 13:54:24 -0700858 VariationSelectorRecord* copy (hb_serialize_context_t *c,
859 const hb_set_t *unicodes,
860 const hb_set_t *glyphs,
861 const hb_map_t *glyph_map,
862 const void *src_base,
863 const void *dst_base) const
864 {
865 auto snap = c->snapshot ();
866 auto *out = c->embed<VariationSelectorRecord> (*this);
867 if (unlikely (!out)) return nullptr;
868
869 out->defaultUVS = 0;
870 out->nonDefaultUVS = 0;
871
872 bool drop = true;
873
874 if (defaultUVS != 0)
875 {
876 c->push ();
877 if (c->copy (src_base+defaultUVS, unicodes))
878 {
879 c->add_link (out->defaultUVS, c->pop_pack (), dst_base);
880 drop = false;
881 }
882 else c->pop_discard ();
883 }
884
885 if (nonDefaultUVS != 0)
886 {
887 c->push ();
888 if (c->copy (src_base+nonDefaultUVS, unicodes, glyphs, glyph_map))
889 {
890 c->add_link (out->nonDefaultUVS, c->pop_pack (), dst_base);
891 drop = false;
892 }
893 else c->pop_discard ();
894 }
895
896 if (drop)
897 {
898 c->revert (snap);
899 return nullptr;
900 }
901 else return out;
902 }
903
Ebrahim Byagowi435b1872018-04-15 21:18:48 +0430904 HBUINT24 varSelector; /* Variation selector. */
Behdad Esfahbod5e156fa2017-01-22 20:28:56 -0800905 LOffsetTo<DefaultUVS>
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330906 defaultUVS; /* Offset to Default UVS Table. May be 0. */
Behdad Esfahbod5e156fa2017-01-22 20:28:56 -0800907 LOffsetTo<NonDefaultUVS>
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330908 nonDefaultUVS; /* Offset to Non-Default UVS Table. May be 0. */
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400909 public:
910 DEFINE_SIZE_STATIC (11);
911};
912
913struct CmapSubtableFormat14
914{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330915 glyph_variant_t get_glyph_variant (hb_codepoint_t codepoint,
916 hb_codepoint_t variation_selector,
917 hb_codepoint_t *glyph) const
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400918 {
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500919 return record.bsearch (variation_selector).get_glyph (codepoint, glyph, this);
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400920 }
921
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330922 void collect_variation_selectors (hb_set_t *out) const
Behdad Esfahbod4806b382018-08-25 15:56:07 -0700923 {
924 unsigned int count = record.len;
925 for (unsigned int i = 0; i < count; i++)
926 out->add (record.arrayZ[i].varSelector);
927 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330928 void collect_variation_unicodes (hb_codepoint_t variation_selector,
929 hb_set_t *out) const
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700930 {
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500931 record.bsearch (variation_selector).collect_unicodes (out, this);
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700932 }
Behdad Esfahbod4806b382018-08-25 15:56:07 -0700933
Qunxin Liu2583afa2019-08-16 13:54:24 -0700934 void serialize (hb_serialize_context_t *c,
935 const hb_set_t *unicodes,
936 const hb_set_t *glyphs,
937 const hb_map_t *glyph_map,
938 const void *src_base)
939 {
940 auto snap = c->snapshot ();
941 unsigned table_initpos = c->length ();
942 const char* init_tail = c->tail;
943
944 if (unlikely (!c->extend_min (*this))) return;
945 this->format = 14;
946
947 const CmapSubtableFormat14 *src_tbl = reinterpret_cast<const CmapSubtableFormat14*> (src_base);
948 + hb_iter (src_tbl->record)
949 | hb_apply ([&] (const VariationSelectorRecord& _)
950 {
951 c->copy (_, unicodes, glyphs, glyph_map, src_base, this);
952 })
953 ;
954
955 if (c->length () - table_initpos == CmapSubtableFormat14::min_size)
956 {
957 c->revert (snap);
958 }
959 else
960 {
961 int tail_len = init_tail - c->tail;
962 c->check_assign (this->length, c->length () - table_initpos + tail_len);
963 c->check_assign (this->record.len, (c->length () - table_initpos - CmapSubtableFormat14::min_size) / VariationSelectorRecord::static_size);
964 }
965 }
966
Qunxin Liu078ddbd2019-08-07 13:17:26 -0700967 void closure_glyphs (const hb_set_t *unicodes,
968 hb_set_t *glyphset) const
969 {
970 + hb_iter (record)
Qunxin Liu43156662019-08-29 11:17:20 -0700971 | hb_filter (hb_bool, &VariationSelectorRecord::nonDefaultUVS)
Qunxin Liu078ddbd2019-08-07 13:17:26 -0700972 | hb_map (&VariationSelectorRecord::nonDefaultUVS)
973 | hb_map (hb_add (this))
974 | hb_apply ([=] (const NonDefaultUVS& _) { _.closure_glyphs (unicodes, glyphset); })
975 ;
976 }
977
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330978 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300979 {
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400980 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100981 return_trace (c->check_struct (this) &&
982 record.sanitize (c, this));
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400983 }
984
985 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100986 HBUINT16 format; /* Format number is set to 14. */
Behdad Esfahbod5b93f692018-05-02 14:59:14 -0400987 HBUINT32 length; /* Byte length of this subtable. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100988 SortedArrayOf<VariationSelectorRecord, HBUINT32>
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400989 record; /* Variation selector records; sorted
990 * in increasing order of `varSelector'. */
991 public:
992 DEFINE_SIZE_ARRAY (10, record);
993};
994
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400995struct CmapSubtable
996{
Behdad Esfahbodc9558762014-05-14 00:42:18 -0400997 /* Note: We intentionally do NOT implement subtable formats 2 and 8. */
998
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330999 bool get_glyph (hb_codepoint_t codepoint,
1000 hb_codepoint_t *glyph) const
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001001 {
1002 switch (u.format) {
Rod Sheeter1725c352018-02-14 19:36:33 -08001003 case 0: return u.format0 .get_glyph (codepoint, glyph);
Qunxin Liu37572882019-06-25 13:17:30 -07001004 case 4: return u.format4 .get_glyph (codepoint, glyph);
Rod Sheeter1725c352018-02-14 19:36:33 -08001005 case 6: return u.format6 .get_glyph (codepoint, glyph);
1006 case 10: return u.format10.get_glyph (codepoint, glyph);
1007 case 12: return u.format12.get_glyph (codepoint, glyph);
1008 case 13: return u.format13.get_glyph (codepoint, glyph);
Behdad Esfahboda5a47362014-06-27 17:03:22 -04001009 case 14:
1010 default: return false;
1011 }
1012 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301013 void collect_unicodes (hb_set_t *out) const
Behdad Esfahbodbd0e5422018-08-25 09:33:30 -07001014 {
1015 switch (u.format) {
Behdad Esfahbod7d382fa2018-08-25 09:35:45 -07001016 case 0: u.format0 .collect_unicodes (out); return;
Qunxin Liu37572882019-06-25 13:17:30 -07001017 case 4: u.format4 .collect_unicodes (out); return;
Behdad Esfahbod7d382fa2018-08-25 09:35:45 -07001018 case 6: u.format6 .collect_unicodes (out); return;
1019 case 10: u.format10.collect_unicodes (out); return;
Behdad Esfahbodbd0e5422018-08-25 09:33:30 -07001020 case 12: u.format12.collect_unicodes (out); return;
1021 case 13: u.format13.collect_unicodes (out); return;
1022 case 14:
1023 default: return;
1024 }
1025 }
Behdad Esfahboda5a47362014-06-27 17:03:22 -04001026
Qunxin Liu37572882019-06-25 13:17:30 -07001027 template<typename Iterator,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +04301028 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu37572882019-06-25 13:17:30 -07001029 void serialize (hb_serialize_context_t *c,
1030 Iterator it,
Qunxin Liu2583afa2019-08-16 13:54:24 -07001031 unsigned format,
Qunxin Liu43156662019-08-29 11:17:20 -07001032 const hb_subset_plan_t *plan,
Qunxin Liu2583afa2019-08-16 13:54:24 -07001033 const void *src_base)
Qunxin Liu37572882019-06-25 13:17:30 -07001034 {
1035 switch (format) {
1036 case 4: u.format4.serialize (c, it); return;
1037 case 12: u.format12.serialize (c, it); return;
Qunxin Liu43156662019-08-29 11:17:20 -07001038 case 14: u.format14.serialize (c, plan->unicodes, plan->_glyphset, plan->glyph_map, src_base); return;
Qunxin Liu37572882019-06-25 13:17:30 -07001039 default: return;
1040 }
1041 }
1042
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301043 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001044 {
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001045 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001046 if (!u.format.sanitize (c)) return_trace (false);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001047 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001048 case 0: return_trace (u.format0 .sanitize (c));
Qunxin Liu37572882019-06-25 13:17:30 -07001049 case 4: return_trace (u.format4 .sanitize (c));
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001050 case 6: return_trace (u.format6 .sanitize (c));
1051 case 10: return_trace (u.format10.sanitize (c));
1052 case 12: return_trace (u.format12.sanitize (c));
1053 case 13: return_trace (u.format13.sanitize (c));
1054 case 14: return_trace (u.format14.sanitize (c));
1055 default:return_trace (true);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001056 }
1057 }
1058
Behdad Esfahbod5473ebf2016-02-24 19:32:43 +09001059 public:
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001060 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001061 HBUINT16 format; /* Format identifier */
Behdad Esfahbodb7878cd2014-05-13 21:47:51 -04001062 CmapSubtableFormat0 format0;
Qunxin Liu37572882019-06-25 13:17:30 -07001063 CmapSubtableFormat4 format4;
Behdad Esfahbod91bbfca2014-05-12 18:19:29 -04001064 CmapSubtableFormat6 format6;
1065 CmapSubtableFormat10 format10;
Behdad Esfahbod0d757932014-05-12 17:51:15 -04001066 CmapSubtableFormat12 format12;
Behdad Esfahbodd294a2c2014-05-12 17:58:31 -04001067 CmapSubtableFormat13 format13;
Behdad Esfahboda5a47362014-06-27 17:03:22 -04001068 CmapSubtableFormat14 format14;
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001069 } u;
1070 public:
1071 DEFINE_SIZE_UNION (2, format);
1072};
1073
1074
1075struct EncodingRecord
1076{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301077 int cmp (const EncodingRecord &other) const
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001078 {
1079 int ret;
Behdad Esfahbodf1a72fe2014-06-04 19:00:29 -04001080 ret = platformID.cmp (other.platformID);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001081 if (ret) return ret;
Behdad Esfahbodf1a72fe2014-06-04 19:00:29 -04001082 ret = encodingID.cmp (other.encodingID);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001083 if (ret) return ret;
1084 return 0;
1085 }
1086
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301087 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +03001088 {
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001089 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +01001090 return_trace (c->check_struct (this) &&
1091 subtable.sanitize (c, base));
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001092 }
1093
Qunxin Liu37572882019-06-25 13:17:30 -07001094 template<typename Iterator,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +04301095 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu37572882019-06-25 13:17:30 -07001096 EncodingRecord* copy (hb_serialize_context_t *c,
Qunxin Liu2583afa2019-08-16 13:54:24 -07001097 Iterator it,
1098 unsigned format,
1099 const void *src_base,
1100 const void *dst_base,
Qunxin Liu43156662019-08-29 11:17:20 -07001101 const hb_subset_plan_t *plan,
1102 /* INOUT */ unsigned *objidx) const
Qunxin Liu37572882019-06-25 13:17:30 -07001103 {
1104 TRACE_SERIALIZE (this);
Qunxin Liu2583afa2019-08-16 13:54:24 -07001105 auto snap = c->snapshot ();
Qunxin Liu37572882019-06-25 13:17:30 -07001106 auto *out = c->embed (this);
1107 if (unlikely (!out)) return_trace (nullptr);
1108 out->subtable = 0;
1109
1110 if (*objidx == 0)
1111 {
1112 CmapSubtable *cmapsubtable = c->push<CmapSubtable> ();
1113 unsigned origin_length = c->length ();
Qunxin Liu43156662019-08-29 11:17:20 -07001114 cmapsubtable->serialize (c, it, format, plan, &(src_base+subtable));
Qunxin Liu37572882019-06-25 13:17:30 -07001115 if (c->length () - origin_length > 0) *objidx = c->pop_pack ();
1116 else c->pop_discard ();
1117 }
1118
Qunxin Liu2583afa2019-08-16 13:54:24 -07001119 if (*objidx == 0)
1120 {
1121 c->revert (snap);
1122 return_trace (nullptr);
1123 }
Qunxin Liu43156662019-08-29 11:17:20 -07001124
1125 c->add_link (out->subtable, *objidx, dst_base);
1126 return_trace (out);
Qunxin Liu37572882019-06-25 13:17:30 -07001127 }
1128
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001129 HBUINT16 platformID; /* Platform ID. */
1130 HBUINT16 encodingID; /* Platform-specific encoding ID. */
Behdad Esfahbod5e156fa2017-01-22 20:28:56 -08001131 LOffsetTo<CmapSubtable>
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001132 subtable; /* Byte offset from beginning of table to the subtable for this encoding. */
1133 public:
1134 DEFINE_SIZE_STATIC (8);
1135};
1136
1137struct cmap
1138{
Behdad Esfahbodef006542019-01-22 12:08:57 +01001139 static constexpr hb_tag_t tableTag = HB_OT_TAG_cmap;
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001140
Qunxin Liu2583afa2019-08-16 13:54:24 -07001141 template<typename Iterator, typename EncodingRecIter,
1142 hb_requires (hb_is_iterator (Iterator))>
Qunxin Liu37572882019-06-25 13:17:30 -07001143 void serialize (hb_serialize_context_t *c,
Qunxin Liu2583afa2019-08-16 13:54:24 -07001144 Iterator it,
1145 EncodingRecIter encodingrec_iter,
1146 const void *src_base,
Qunxin Liu43156662019-08-29 11:17:20 -07001147 const hb_subset_plan_t *plan)
Behdad Esfahbode6cb9382018-08-26 00:21:29 -07001148 {
Qunxin Liu37572882019-06-25 13:17:30 -07001149 if (unlikely (!c->extend_min ((*this)))) return;
1150 this->version = 0;
Garret Rieger0053d132018-05-02 15:42:43 -07001151
Qunxin Liu2583afa2019-08-16 13:54:24 -07001152 unsigned format4objidx = 0, format12objidx = 0, format14objidx = 0;
Garret Rieger0053d132018-05-02 15:42:43 -07001153
Qunxin Liu2583afa2019-08-16 13:54:24 -07001154 + encodingrec_iter
1155 | hb_apply ([&] (const EncodingRecord& _)
1156 {
1157 unsigned format = (src_base+_.subtable).u.format;
Ebrahim Byagowi7a9d6432019-07-11 01:35:06 +04301158
Qunxin Liu43156662019-08-29 11:17:20 -07001159 if (format == 4) c->copy (_, it, 4u, src_base, this, plan, &format4objidx);
1160 else if (format == 12) c->copy (_, it, 12u, src_base, this, plan, &format12objidx);
1161 else if (format == 14) c->copy (_, it, 14u, src_base, this, plan, &format14objidx);
Qunxin Liu2583afa2019-08-16 13:54:24 -07001162 })
1163 ;
1164
1165 c->check_assign(this->encodingRecord.len, (c->length () - cmap::min_size)/EncodingRecord::static_size);
Garret Rieger5e318e02018-04-18 17:13:37 -07001166 }
1167
Qunxin Liu078ddbd2019-08-07 13:17:26 -07001168 void closure_glyphs (const hb_set_t *unicodes,
1169 hb_set_t *glyphset) const
1170 {
1171 + hb_iter (encodingRecord)
1172 | hb_map (&EncodingRecord::subtable)
1173 | hb_map (hb_add (this))
1174 | hb_filter ([&] (const CmapSubtable& _) { return _.u.format == 14; })
1175 | hb_apply ([=] (const CmapSubtable& _) { _.u.format14.closure_glyphs (unicodes, glyphset); })
1176 ;
1177 }
1178
Qunxin Liu37572882019-06-25 13:17:30 -07001179 bool subset (hb_subset_context_t *c) const
Rod Sheeter9275bd02018-02-09 17:33:34 -08001180 {
Qunxin Liu37572882019-06-25 13:17:30 -07001181 TRACE_SUBSET (this);
Rod Sheeter9275bd02018-02-09 17:33:34 -08001182
Qunxin Liu37572882019-06-25 13:17:30 -07001183 cmap *cmap_prime = c->serializer->start_embed<cmap> ();
1184 if (unlikely (!c->serializer->check_success (cmap_prime))) return_trace (false);
Rod Sheeter9275bd02018-02-09 17:33:34 -08001185
Qunxin Liu2583afa2019-08-16 13:54:24 -07001186 auto encodingrec_iter =
1187 + hb_iter (encodingRecord)
1188 | hb_filter ([&] (const EncodingRecord& _)
1189 {
Qunxin Liu43156662019-08-29 11:17:20 -07001190 if ((_.platformID == 0 && _.encodingID == 3) ||
1191 (_.platformID == 0 && _.encodingID == 4) ||
1192 (_.platformID == 3 && _.encodingID == 1) ||
1193 (_.platformID == 3 && _.encodingID == 10) ||
1194 (this + _.subtable).u.format == 14)
1195 return true;
1196
1197 return false;
1198 })
1199 ;
1200
1201
1202 if (unlikely (!encodingrec_iter.len ())) return_trace (false);
1203
1204 const EncodingRecord *unicode_bmp= nullptr, *unicode_ucs4 = nullptr, *ms_bmp = nullptr, *ms_ucs4 = nullptr;
1205 bool has_format12 = false, has_format14 = false;
1206
1207 + encodingrec_iter
1208 | hb_apply ([&] (const EncodingRecord& _)
1209 {
Qunxin Liu2583afa2019-08-16 13:54:24 -07001210 unsigned format = (this + _.subtable).u.format;
1211 if (format == 12) has_format12 = true;
1212 if (format == 14) has_format14 = true;
Rod Sheeter9275bd02018-02-09 17:33:34 -08001213
Qunxin Liu2583afa2019-08-16 13:54:24 -07001214 const EncodingRecord *table = hb_addressof (_);
1215 if (_.platformID == 0 && _.encodingID == 3)
1216 {
1217 unicode_bmp = table;
Qunxin Liu2583afa2019-08-16 13:54:24 -07001218 }
1219 else if (_.platformID == 0 && _.encodingID == 4)
1220 {
1221 unicode_ucs4 = table;
Qunxin Liu2583afa2019-08-16 13:54:24 -07001222 }
1223 else if (_.platformID == 3 && _.encodingID == 1)
1224 {
1225 ms_bmp = table;
Qunxin Liu2583afa2019-08-16 13:54:24 -07001226 }
1227 else if (_.platformID == 3 && _.encodingID == 10)
1228 {
1229 ms_ucs4 = table;
Qunxin Liu2583afa2019-08-16 13:54:24 -07001230 }
Qunxin Liu2583afa2019-08-16 13:54:24 -07001231 })
1232 ;
1233
Qunxin Liu37572882019-06-25 13:17:30 -07001234 if (unlikely (!unicode_bmp && !ms_bmp)) return_trace (false);
1235 if (unlikely (has_format12 && (!unicode_ucs4 && !ms_ucs4))) return_trace (false);
Rod Sheeter9275bd02018-02-09 17:33:34 -08001236
Qunxin Liu37572882019-06-25 13:17:30 -07001237 auto it =
1238 + hb_iter (c->plan->unicodes)
1239 | hb_map ([&] (hb_codepoint_t _)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +04301240 {
1241 hb_codepoint_t new_gid = HB_MAP_VALUE_INVALID;
1242 c->plan->new_gid_for_codepoint (_, &new_gid);
1243 return hb_pair_t<hb_codepoint_t, hb_codepoint_t> (_, new_gid);
1244 })
Qunxin Liu37572882019-06-25 13:17:30 -07001245 | hb_filter ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> _)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +04301246 {
1247 return (_.second != HB_MAP_VALUE_INVALID);
1248 })
Qunxin Liu37572882019-06-25 13:17:30 -07001249 ;
Michiharu Ariza82d4bfb2019-06-14 10:49:42 -07001250
Qunxin Liu43156662019-08-29 11:17:20 -07001251 cmap_prime->serialize (c->serializer, it, encodingrec_iter, this, c->plan);
Qunxin Liu37572882019-06-25 13:17:30 -07001252 return_trace (true);
Rod Sheeter9275bd02018-02-09 17:33:34 -08001253 }
1254
Behdad Esfahbod02fe03e2018-08-25 15:33:05 -07001255 const CmapSubtable *find_best_subtable (bool *symbol = nullptr) const
1256 {
1257 if (symbol) *symbol = false;
1258
1259 const CmapSubtable *subtable;
1260
Behdad Esfahbodd304d602019-08-21 12:30:22 -07001261 /* Symbol subtable.
1262 * Prefer symbol if available.
1263 * https://github.com/harfbuzz/harfbuzz/issues/1918 */
1264 if ((subtable = this->find_subtable (3, 0)))
1265 {
1266 if (symbol) *symbol = true;
1267 return subtable;
1268 }
1269
Behdad Esfahbod02fe03e2018-08-25 15:33:05 -07001270 /* 32-bit subtables. */
1271 if ((subtable = this->find_subtable (3, 10))) return subtable;
1272 if ((subtable = this->find_subtable (0, 6))) return subtable;
1273 if ((subtable = this->find_subtable (0, 4))) return subtable;
1274
1275 /* 16-bit subtables. */
1276 if ((subtable = this->find_subtable (3, 1))) return subtable;
1277 if ((subtable = this->find_subtable (0, 3))) return subtable;
1278 if ((subtable = this->find_subtable (0, 2))) return subtable;
1279 if ((subtable = this->find_subtable (0, 1))) return subtable;
1280 if ((subtable = this->find_subtable (0, 0))) return subtable;
1281
Behdad Esfahbod02fe03e2018-08-25 15:33:05 -07001282 /* Meh. */
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +03301283 return &Null (CmapSubtable);
Behdad Esfahbod02fe03e2018-08-25 15:33:05 -07001284 }
1285
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001286 struct accelerator_t
1287 {
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301288 void init (hb_face_t *face)
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001289 {
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +03301290 this->table = hb_sanitize_context_t ().reference_table<cmap> (face);
Behdad Esfahbod02fe03e2018-08-25 15:33:05 -07001291 bool symbol;
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001292 this->subtable = table->find_best_subtable (&symbol);
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +03301293 this->subtable_uvs = &Null (CmapSubtableFormat14);
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001294 {
Behdad Esfahbode57a6382018-07-23 12:00:02 -07001295 const CmapSubtable *st = table->find_subtable (0, 5);
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001296 if (st && st->u.format == 14)
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001297 subtable_uvs = &st->u.format14;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001298 }
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001299
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001300 this->get_glyph_data = subtable;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001301 if (unlikely (symbol))
Garret Rieger21a181a2018-04-10 15:40:24 -07001302 {
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001303 this->get_glyph_funcZ = get_glyph_from_symbol<CmapSubtable>;
Garret Rieger21a181a2018-04-10 15:40:24 -07001304 } else {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001305 switch (subtable->u.format) {
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001306 /* Accelerate format 4 and format 12. */
Garret Rieger21a181a2018-04-10 15:40:24 -07001307 default:
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001308 this->get_glyph_funcZ = get_glyph_from<CmapSubtable>;
Garret Rieger21a181a2018-04-10 15:40:24 -07001309 break;
1310 case 12:
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001311 this->get_glyph_funcZ = get_glyph_from<CmapSubtableFormat12>;
Garret Rieger21a181a2018-04-10 15:40:24 -07001312 break;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001313 case 4:
1314 {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001315 this->format4_accel.init (&subtable->u.format4);
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001316 this->get_glyph_data = &this->format4_accel;
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001317 this->get_glyph_funcZ = this->format4_accel.get_glyph_func;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001318 }
1319 break;
1320 }
Garret Rieger21a181a2018-04-10 15:40:24 -07001321 }
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001322 }
1323
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301324 void fini () { this->table.destroy (); }
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001325
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301326 bool get_nominal_glyph (hb_codepoint_t unicode,
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001327 hb_codepoint_t *glyph) const
1328 {
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001329 if (unlikely (!this->get_glyph_funcZ)) return false;
1330 return this->get_glyph_funcZ (this->get_glyph_data, unicode, glyph);
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001331 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301332 unsigned int get_nominal_glyphs (unsigned int count,
1333 const hb_codepoint_t *first_unicode,
1334 unsigned int unicode_stride,
1335 hb_codepoint_t *first_glyph,
1336 unsigned int glyph_stride) const
Behdad Esfahbod56ba9982018-11-05 19:49:54 -05001337 {
1338 if (unlikely (!this->get_glyph_funcZ)) return 0;
1339
1340 hb_cmap_get_glyph_func_t get_glyph_funcZ = this->get_glyph_funcZ;
1341 const void *get_glyph_data = this->get_glyph_data;
1342
1343 unsigned int done;
1344 for (done = 0;
1345 done < count && get_glyph_funcZ (get_glyph_data, *first_unicode, first_glyph);
1346 done++)
1347 {
Behdad Esfahbod447323b2019-01-22 12:45:40 +01001348 first_unicode = &StructAtOffsetUnaligned<hb_codepoint_t> (first_unicode, unicode_stride);
1349 first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
Behdad Esfahbod56ba9982018-11-05 19:49:54 -05001350 }
1351 return done;
1352 }
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001353
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301354 bool get_variation_glyph (hb_codepoint_t unicode,
1355 hb_codepoint_t variation_selector,
1356 hb_codepoint_t *glyph) const
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001357 {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001358 switch (this->subtable_uvs->get_glyph_variant (unicode,
1359 variation_selector,
1360 glyph))
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001361 {
Behdad Esfahbod36ed1632018-07-23 11:57:45 -07001362 case GLYPH_VARIANT_NOT_FOUND: return false;
1363 case GLYPH_VARIANT_FOUND: return true;
1364 case GLYPH_VARIANT_USE_DEFAULT: break;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001365 }
1366
1367 return get_nominal_glyph (unicode, glyph);
1368 }
1369
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301370 void collect_unicodes (hb_set_t *out) const
Garret Rieger21a181a2018-04-10 15:40:24 -07001371 {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001372 subtable->collect_unicodes (out);
Garret Rieger21a181a2018-04-10 15:40:24 -07001373 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301374 void collect_variation_selectors (hb_set_t *out) const
Behdad Esfahbod4806b382018-08-25 15:56:07 -07001375 {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001376 subtable_uvs->collect_variation_selectors (out);
Behdad Esfahbod4806b382018-08-25 15:56:07 -07001377 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301378 void collect_variation_unicodes (hb_codepoint_t variation_selector,
1379 hb_set_t *out) const
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -07001380 {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001381 subtable_uvs->collect_variation_unicodes (variation_selector, out);
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -07001382 }
Garret Rieger21a181a2018-04-10 15:40:24 -07001383
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001384 protected:
1385 typedef bool (*hb_cmap_get_glyph_func_t) (const void *obj,
1386 hb_codepoint_t codepoint,
1387 hb_codepoint_t *glyph);
Garret Rieger21a181a2018-04-10 15:40:24 -07001388
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001389 template <typename Type>
Behdad Esfahbod95df00a2019-04-12 17:50:03 -04001390 HB_INTERNAL static bool get_glyph_from (const void *obj,
1391 hb_codepoint_t codepoint,
1392 hb_codepoint_t *glyph)
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001393 {
1394 const Type *typed_obj = (const Type *) obj;
1395 return typed_obj->get_glyph (codepoint, glyph);
1396 }
1397
1398 template <typename Type>
Behdad Esfahbod95df00a2019-04-12 17:50:03 -04001399 HB_INTERNAL static bool get_glyph_from_symbol (const void *obj,
1400 hb_codepoint_t codepoint,
1401 hb_codepoint_t *glyph)
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001402 {
1403 const Type *typed_obj = (const Type *) obj;
1404 if (likely (typed_obj->get_glyph (codepoint, glyph)))
1405 return true;
1406
1407 if (codepoint <= 0x00FFu)
1408 {
1409 /* For symbol-encoded OpenType fonts, we duplicate the
1410 * U+F000..F0FF range at U+0000..U+00FF. That's what
1411 * Windows seems to do, and that's hinted about at:
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +04301412 * https://docs.microsoft.com/en-us/typography/opentype/spec/recom
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001413 * under "Non-Standard (Symbol) Fonts". */
1414 return typed_obj->get_glyph (0xF000u + codepoint, glyph);
1415 }
1416
1417 return false;
1418 }
1419
1420 private:
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001421 hb_nonnull_ptr_t<const CmapSubtable> subtable;
1422 hb_nonnull_ptr_t<const CmapSubtableFormat14> subtable_uvs;
Behdad Esfahbod4806b382018-08-25 15:56:07 -07001423
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001424 hb_cmap_get_glyph_func_t get_glyph_funcZ;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001425 const void *get_glyph_data;
Garret Rieger21a181a2018-04-10 15:40:24 -07001426
Behdad Esfahbod36ed1632018-07-23 11:57:45 -07001427 CmapSubtableFormat4::accelerator_t format4_accel;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001428
Qunxin Liu078ddbd2019-08-07 13:17:26 -07001429 public:
Behdad Esfahbod0e2680a2018-11-11 00:28:47 -05001430 hb_blob_ptr_t<cmap> table;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001431 };
1432
1433 protected:
1434
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301435 const CmapSubtable *find_subtable (unsigned int platform_id,
1436 unsigned int encoding_id) const
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001437 {
1438 EncodingRecord key;
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -07001439 key.platformID = platform_id;
1440 key.encodingID = encoding_id;
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001441
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -05001442 const EncodingRecord &result = encodingRecord.bsearch (key);
1443 if (!result.subtable)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001444 return nullptr;
Behdad Esfahbod3608a682014-05-12 13:46:29 -04001445
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -05001446 return &(this+result.subtable);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001447 }
1448
Qunxin Liu37572882019-06-25 13:17:30 -07001449 const EncodingRecord *find_encodingrec (unsigned int platform_id,
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +04301450 unsigned int encoding_id) const
Qunxin Liu37572882019-06-25 13:17:30 -07001451 {
1452 EncodingRecord key;
1453 key.platformID = platform_id;
1454 key.encodingID = encoding_id;
1455
1456 return encodingRecord.as_array ().bsearch (key);
1457 }
1458
Qunxin Liu993d81b2019-05-14 13:55:11 -07001459 bool find_subtable (unsigned format) const
1460 {
1461 auto it =
1462 + hb_iter (encodingRecord)
1463 | hb_map (&EncodingRecord::subtable)
1464 | hb_map (hb_add (this))
1465 | hb_filter ([&] (const CmapSubtable& _) { return _.u.format == format; })
1466 ;
1467
1468 return it.len ();
1469 }
1470
Behdad Esfahbode4a45552018-11-22 22:17:49 -05001471 public:
1472
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301473 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbode4a45552018-11-22 22:17:49 -05001474 {
1475 TRACE_SANITIZE (this);
1476 return_trace (c->check_struct (this) &&
1477 likely (version == 0) &&
1478 encodingRecord.sanitize (c, this));
1479 }
1480
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001481 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001482 HBUINT16 version; /* Table version number (0). */
Behdad Esfahboddf554af2014-06-19 15:39:18 -04001483 SortedArrayOf<EncodingRecord>
Behdad Esfahbodf1a72fe2014-06-04 19:00:29 -04001484 encodingRecord; /* Encoding tables. */
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001485 public:
1486 DEFINE_SIZE_ARRAY (4, encodingRecord);
1487};
1488
Behdad Esfahbod3a0b3a22018-08-26 15:11:24 -07001489struct cmap_accelerator_t : cmap::accelerator_t {};
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001490
1491} /* namespace OT */
1492
1493
1494#endif /* HB_OT_CMAP_TABLE_HH */