blob: 746e87f48fd89c39f342e4adef89db4fe063aea4 [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,
79 hb_requires (hb_is_iterator (Iterator))>
80 HBUINT16* serialize_endcode_array (hb_serialize_context_t *c,
81 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> _)
88 {
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 })
97 ;
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 {
107 HBUINT16 finalcode;
108 finalcode = 0xFFFF;
109 if (unlikely (!c->copy<HBUINT16> (finalcode))) return nullptr;
110 }
111 }
112
113 return endCode;
114 }
115
116 template<typename Iterator,
117 hb_requires (hb_is_iterator (Iterator))>
118 HBUINT16* serialize_startcode_array (hb_serialize_context_t *c,
119 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;
123
124 + it
125 | hb_apply ([&] (const hb_item_type<Iterator> _)
126 {
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
Qunxin Liu37572882019-06-25 13:17:30 -0700134 prev_cp = _.first;
135 })
136 ;
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,
150 hb_requires (hb_is_iterator (Iterator))>
151 HBINT16* serialize_idDelta_array (hb_serialize_context_t *c,
152 Iterator it,
153 HBUINT16 *endCode,
154 HBUINT16 *startCode,
155 unsigned segcount)
156 {
157 unsigned i = 0;
158 hb_codepoint_t last_gid = 0, start_gid = 0, last_cp = 0xFFFF;
159 bool use_delta = true;
160
161 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> _)
167 {
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;
174
175 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);
181
182 i++;
183 }
184
185 last_gid = _.second;
186 last_cp = _.first;
187 })
188 ;
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,
201 hb_requires (hb_is_iterator (Iterator))>
202 HBUINT16* serialize_rangeoffset_glyid (hb_serialize_context_t *c,
203 Iterator it,
204 HBUINT16 *endCode,
205 HBUINT16 *startCode,
206 HBINT16 *idDelta,
207 unsigned segcount)
208 {
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;
212
213 + hb_range (segcount)
214 | hb_filter ([&] (const unsigned _) { return idDelta[_] == 0; })
215 | hb_apply ([&] (const unsigned i)
216 {
217 idRangeOffset[i] = 2 * (c->start_embed<HBUINT16> () - idRangeOffset - i);
218
219 + 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 ;
228
229
230 })
231 ;
232
233 return idRangeOffset;
234 }
235
236 template<typename Iterator,
237 hb_requires (hb_is_iterator (Iterator))>
238 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;
250
251 // 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;
257
258 //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;
264
265 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. */
Behdad Esfahbod9da552d2014-06-27 15:09:42 -0400501 ArrayOf<GlyphID, 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,
560 hb_requires (hb_is_iterator (Iterator))>
561 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> _)
573 {
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);
587
588 startCharCode = _.first;
589 endCharCode = _.first;
590 glyphID = _.second;
591 }
592 else
593 {
594 endCharCode = _.first;
595 }
596 })
597 ;
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,
618 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
679 public:
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -0500680 DEFINE_SIZE_ARRAY (4, *this);
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700681};
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400682
683struct UVSMapping
684{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330685 int cmp (const hb_codepoint_t &codepoint) const
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400686 {
687 return unicodeValue.cmp (codepoint);
688 }
689
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330690 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300691 {
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400692 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100693 return_trace (c->check_struct (this));
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400694 }
695
Ebrahim Byagowi435b1872018-04-15 21:18:48 +0430696 HBUINT24 unicodeValue; /* Base Unicode value of the UVS */
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400697 GlyphID glyphID; /* Glyph ID of the UVS */
698 public:
699 DEFINE_SIZE_STATIC (5);
700};
701
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700702struct NonDefaultUVS : SortedArrayOf<UVSMapping, HBUINT32>
703{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330704 void collect_unicodes (hb_set_t *out) const
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700705 {
706 unsigned int count = len;
707 for (unsigned int i = 0; i < count; i++)
708 out->add (arrayZ[i].glyphID);
709 }
710
711 public:
Behdad Esfahbod1d66cdc2018-11-10 19:54:08 -0500712 DEFINE_SIZE_ARRAY (4, *this);
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700713};
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400714
715struct VariationSelectorRecord
716{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330717 glyph_variant_t get_glyph (hb_codepoint_t codepoint,
718 hb_codepoint_t *glyph,
719 const void *base) const
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400720 {
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500721 if ((base+defaultUVS).bfind (codepoint))
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400722 return GLYPH_VARIANT_USE_DEFAULT;
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500723 const UVSMapping &nonDefault = (base+nonDefaultUVS).bsearch (codepoint);
724 if (nonDefault.glyphID)
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400725 {
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500726 *glyph = nonDefault.glyphID;
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400727 return GLYPH_VARIANT_FOUND;
728 }
729 return GLYPH_VARIANT_NOT_FOUND;
730 }
731
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330732 void collect_unicodes (hb_set_t *out, const void *base) const
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700733 {
734 (base+defaultUVS).collect_unicodes (out);
735 (base+nonDefaultUVS).collect_unicodes (out);
736 }
737
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330738 int cmp (const hb_codepoint_t &variation_selector) const
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400739 {
740 return varSelector.cmp (variation_selector);
741 }
742
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330743 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300744 {
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400745 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100746 return_trace (c->check_struct (this) &&
747 defaultUVS.sanitize (c, base) &&
748 nonDefaultUVS.sanitize (c, base));
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400749 }
750
Ebrahim Byagowi435b1872018-04-15 21:18:48 +0430751 HBUINT24 varSelector; /* Variation selector. */
Behdad Esfahbod5e156fa2017-01-22 20:28:56 -0800752 LOffsetTo<DefaultUVS>
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330753 defaultUVS; /* Offset to Default UVS Table. May be 0. */
Behdad Esfahbod5e156fa2017-01-22 20:28:56 -0800754 LOffsetTo<NonDefaultUVS>
Ebrahim Byagowi63109432018-10-13 14:00:05 +0330755 nonDefaultUVS; /* Offset to Non-Default UVS Table. May be 0. */
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400756 public:
757 DEFINE_SIZE_STATIC (11);
758};
759
760struct CmapSubtableFormat14
761{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330762 glyph_variant_t get_glyph_variant (hb_codepoint_t codepoint,
763 hb_codepoint_t variation_selector,
764 hb_codepoint_t *glyph) const
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400765 {
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500766 return record.bsearch (variation_selector).get_glyph (codepoint, glyph, this);
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400767 }
768
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330769 void collect_variation_selectors (hb_set_t *out) const
Behdad Esfahbod4806b382018-08-25 15:56:07 -0700770 {
771 unsigned int count = record.len;
772 for (unsigned int i = 0; i < count; i++)
773 out->add (record.arrayZ[i].varSelector);
774 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330775 void collect_variation_unicodes (hb_codepoint_t variation_selector,
776 hb_set_t *out) const
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700777 {
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -0500778 record.bsearch (variation_selector).collect_unicodes (out, this);
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -0700779 }
Behdad Esfahbod4806b382018-08-25 15:56:07 -0700780
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330781 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300782 {
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400783 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100784 return_trace (c->check_struct (this) &&
785 record.sanitize (c, this));
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400786 }
787
788 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100789 HBUINT16 format; /* Format number is set to 14. */
Behdad Esfahbod5b93f692018-05-02 14:59:14 -0400790 HBUINT32 length; /* Byte length of this subtable. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100791 SortedArrayOf<VariationSelectorRecord, HBUINT32>
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400792 record; /* Variation selector records; sorted
793 * in increasing order of `varSelector'. */
794 public:
795 DEFINE_SIZE_ARRAY (10, record);
796};
797
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400798struct CmapSubtable
799{
Behdad Esfahbodc9558762014-05-14 00:42:18 -0400800 /* Note: We intentionally do NOT implement subtable formats 2 and 8. */
801
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330802 bool get_glyph (hb_codepoint_t codepoint,
803 hb_codepoint_t *glyph) const
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400804 {
805 switch (u.format) {
Rod Sheeter1725c352018-02-14 19:36:33 -0800806 case 0: return u.format0 .get_glyph (codepoint, glyph);
Qunxin Liu37572882019-06-25 13:17:30 -0700807 case 4: return u.format4 .get_glyph (codepoint, glyph);
Rod Sheeter1725c352018-02-14 19:36:33 -0800808 case 6: return u.format6 .get_glyph (codepoint, glyph);
809 case 10: return u.format10.get_glyph (codepoint, glyph);
810 case 12: return u.format12.get_glyph (codepoint, glyph);
811 case 13: return u.format13.get_glyph (codepoint, glyph);
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400812 case 14:
813 default: return false;
814 }
815 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330816 void collect_unicodes (hb_set_t *out) const
Behdad Esfahbodbd0e5422018-08-25 09:33:30 -0700817 {
818 switch (u.format) {
Behdad Esfahbod7d382fa2018-08-25 09:35:45 -0700819 case 0: u.format0 .collect_unicodes (out); return;
Qunxin Liu37572882019-06-25 13:17:30 -0700820 case 4: u.format4 .collect_unicodes (out); return;
Behdad Esfahbod7d382fa2018-08-25 09:35:45 -0700821 case 6: u.format6 .collect_unicodes (out); return;
822 case 10: u.format10.collect_unicodes (out); return;
Behdad Esfahbodbd0e5422018-08-25 09:33:30 -0700823 case 12: u.format12.collect_unicodes (out); return;
824 case 13: u.format13.collect_unicodes (out); return;
825 case 14:
826 default: return;
827 }
828 }
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400829
Qunxin Liu37572882019-06-25 13:17:30 -0700830 template<typename Iterator,
831 hb_requires (hb_is_iterator (Iterator))>
832 void serialize (hb_serialize_context_t *c,
833 Iterator it,
834 unsigned format)
835 {
836 switch (format) {
837 case 4: u.format4.serialize (c, it); return;
838 case 12: u.format12.serialize (c, it); return;
839 default: return;
840 }
841 }
842
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330843 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300844 {
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400845 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100846 if (!u.format.sanitize (c)) return_trace (false);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400847 switch (u.format) {
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100848 case 0: return_trace (u.format0 .sanitize (c));
Qunxin Liu37572882019-06-25 13:17:30 -0700849 case 4: return_trace (u.format4 .sanitize (c));
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100850 case 6: return_trace (u.format6 .sanitize (c));
851 case 10: return_trace (u.format10.sanitize (c));
852 case 12: return_trace (u.format12.sanitize (c));
853 case 13: return_trace (u.format13.sanitize (c));
854 case 14: return_trace (u.format14.sanitize (c));
855 default:return_trace (true);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400856 }
857 }
858
Behdad Esfahbod5473ebf2016-02-24 19:32:43 +0900859 public:
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400860 union {
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100861 HBUINT16 format; /* Format identifier */
Behdad Esfahbodb7878cd2014-05-13 21:47:51 -0400862 CmapSubtableFormat0 format0;
Qunxin Liu37572882019-06-25 13:17:30 -0700863 CmapSubtableFormat4 format4;
Behdad Esfahbod91bbfca2014-05-12 18:19:29 -0400864 CmapSubtableFormat6 format6;
865 CmapSubtableFormat10 format10;
Behdad Esfahbod0d757932014-05-12 17:51:15 -0400866 CmapSubtableFormat12 format12;
Behdad Esfahbodd294a2c2014-05-12 17:58:31 -0400867 CmapSubtableFormat13 format13;
Behdad Esfahboda5a47362014-06-27 17:03:22 -0400868 CmapSubtableFormat14 format14;
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400869 } u;
870 public:
871 DEFINE_SIZE_UNION (2, format);
872};
873
874
875struct EncodingRecord
876{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330877 int cmp (const EncodingRecord &other) const
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400878 {
879 int ret;
Behdad Esfahbodf1a72fe2014-06-04 19:00:29 -0400880 ret = platformID.cmp (other.platformID);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400881 if (ret) return ret;
Behdad Esfahbodf1a72fe2014-06-04 19:00:29 -0400882 ret = encodingID.cmp (other.encodingID);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400883 if (ret) return ret;
884 return 0;
885 }
886
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330887 bool sanitize (hb_sanitize_context_t *c, const void *base) const
Behdad Esfahbodde2118e2015-02-17 17:27:44 +0300888 {
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400889 TRACE_SANITIZE (this);
Behdad Esfahbodb4715902015-09-29 14:57:02 +0100890 return_trace (c->check_struct (this) &&
891 subtable.sanitize (c, base));
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400892 }
893
Qunxin Liu37572882019-06-25 13:17:30 -0700894 template<typename Iterator,
895 hb_requires (hb_is_iterator (Iterator))>
896 EncodingRecord* copy (hb_serialize_context_t *c,
897 Iterator it,
898 unsigned format,
899 void *base,
900 /* INOUT */ unsigned *objidx) const
901 {
902 TRACE_SERIALIZE (this);
903 auto *out = c->embed (this);
904 if (unlikely (!out)) return_trace (nullptr);
905 out->subtable = 0;
906
907 if (*objidx == 0)
908 {
909 CmapSubtable *cmapsubtable = c->push<CmapSubtable> ();
910 unsigned origin_length = c->length ();
911 cmapsubtable->serialize (c, it, format);
912 if (c->length () - origin_length > 0) *objidx = c->pop_pack ();
913 else c->pop_discard ();
914 }
915
916 c->add_link (out->subtable, *objidx, base);
917 return_trace (out);
918 }
919
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100920 HBUINT16 platformID; /* Platform ID. */
921 HBUINT16 encodingID; /* Platform-specific encoding ID. */
Behdad Esfahbod5e156fa2017-01-22 20:28:56 -0800922 LOffsetTo<CmapSubtable>
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400923 subtable; /* Byte offset from beginning of table to the subtable for this encoding. */
924 public:
925 DEFINE_SIZE_STATIC (8);
926};
927
928struct cmap
929{
Behdad Esfahbodef006542019-01-22 12:08:57 +0100930 static constexpr hb_tag_t tableTag = HB_OT_TAG_cmap;
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -0400931
Qunxin Liu37572882019-06-25 13:17:30 -0700932 template<typename Iterator,
933 hb_requires (hb_is_iterator (Iterator))>
934 void serialize (hb_serialize_context_t *c,
935 Iterator it,
936 const EncodingRecord *unicode_bmp,
937 const EncodingRecord *unicode_ucs4,
938 const EncodingRecord *ms_bmp,
939 const EncodingRecord *ms_ucs4)
Behdad Esfahbode6cb9382018-08-26 00:21:29 -0700940 {
Qunxin Liu37572882019-06-25 13:17:30 -0700941 if (unlikely (!c->extend_min ((*this)))) return;
942 this->version = 0;
Garret Rieger0053d132018-05-02 15:42:43 -0700943
Qunxin Liu37572882019-06-25 13:17:30 -0700944 unsigned numTables = (unicode_bmp ? 1 : 0) + (unicode_ucs4 ? 1 : 0) + (ms_bmp ? 1 : 0) + (ms_ucs4 ? 1 : 0);
945 if (unlikely (!c->check_assign(this->encodingRecord.len, numTables))) return;
Garret Rieger0053d132018-05-02 15:42:43 -0700946
Qunxin Liu37572882019-06-25 13:17:30 -0700947 unsigned format4objidx = 0, format12objidx = 0;
948 if (unicode_bmp) c->copy (unicode_bmp, it, 4u, this, &format4objidx);
949 if (unicode_ucs4) c->copy (unicode_ucs4, it, 12u, this, &format12objidx);
950 if (ms_bmp) c->copy (ms_bmp, it, 4u, this, &format4objidx);
951 if (ms_ucs4) c->copy (ms_ucs4, it, 12u, this, &format12objidx);
Ebrahim Byagowi7a9d6432019-07-11 01:35:06 +0430952
Garret Rieger5e318e02018-04-18 17:13:37 -0700953 }
954
Qunxin Liu37572882019-06-25 13:17:30 -0700955 bool subset (hb_subset_context_t *c) const
Rod Sheeter9275bd02018-02-09 17:33:34 -0800956 {
Qunxin Liu37572882019-06-25 13:17:30 -0700957 TRACE_SUBSET (this);
Rod Sheeter9275bd02018-02-09 17:33:34 -0800958
Qunxin Liu37572882019-06-25 13:17:30 -0700959 cmap *cmap_prime = c->serializer->start_embed<cmap> ();
960 if (unlikely (!c->serializer->check_success (cmap_prime))) return_trace (false);
Rod Sheeter9275bd02018-02-09 17:33:34 -0800961
Qunxin Liu37572882019-06-25 13:17:30 -0700962 const EncodingRecord *unicode_bmp = find_encodingrec (0, 3);
963 const EncodingRecord *unicode_ucs4 = find_encodingrec (0, 4);
964 const EncodingRecord *ms_bmp = find_encodingrec (3, 1);
965 const EncodingRecord *ms_ucs4 = find_encodingrec (3, 10);
966 bool has_format12 = find_subtable (12);
Rod Sheeter9275bd02018-02-09 17:33:34 -0800967
Qunxin Liu37572882019-06-25 13:17:30 -0700968 if (unlikely (!unicode_bmp && !ms_bmp)) return_trace (false);
969 if (unlikely (has_format12 && (!unicode_ucs4 && !ms_ucs4))) return_trace (false);
Rod Sheeter9275bd02018-02-09 17:33:34 -0800970
Rod Sheeter9275bd02018-02-09 17:33:34 -0800971
Qunxin Liu37572882019-06-25 13:17:30 -0700972 auto it =
973 + hb_iter (c->plan->unicodes)
974 | hb_map ([&] (hb_codepoint_t _)
975 {
976 hb_codepoint_t new_gid = HB_MAP_VALUE_INVALID;
977 c->plan->new_gid_for_codepoint (_, &new_gid);
978 return hb_pair_t<hb_codepoint_t, hb_codepoint_t> (_, new_gid);
979 })
980 | hb_filter ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> _)
981 {
982 return (_.second != HB_MAP_VALUE_INVALID);
983 })
984 ;
Michiharu Ariza82d4bfb2019-06-14 10:49:42 -0700985
Qunxin Liu37572882019-06-25 13:17:30 -0700986 cmap_prime->serialize (c->serializer, it, unicode_bmp, unicode_ucs4, ms_bmp, ms_ucs4);
987 return_trace (true);
Rod Sheeter9275bd02018-02-09 17:33:34 -0800988 }
989
Behdad Esfahbod02fe03e2018-08-25 15:33:05 -0700990 const CmapSubtable *find_best_subtable (bool *symbol = nullptr) const
991 {
992 if (symbol) *symbol = false;
993
994 const CmapSubtable *subtable;
995
996 /* 32-bit subtables. */
997 if ((subtable = this->find_subtable (3, 10))) return subtable;
998 if ((subtable = this->find_subtable (0, 6))) return subtable;
999 if ((subtable = this->find_subtable (0, 4))) return subtable;
1000
1001 /* 16-bit subtables. */
1002 if ((subtable = this->find_subtable (3, 1))) return subtable;
1003 if ((subtable = this->find_subtable (0, 3))) return subtable;
1004 if ((subtable = this->find_subtable (0, 2))) return subtable;
1005 if ((subtable = this->find_subtable (0, 1))) return subtable;
1006 if ((subtable = this->find_subtable (0, 0))) return subtable;
1007
1008 /* Symbol subtable. */
1009 if ((subtable = this->find_subtable (3, 0)))
1010 {
1011 if (symbol) *symbol = true;
1012 return subtable;
1013 }
1014
1015 /* Meh. */
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +03301016 return &Null (CmapSubtable);
Behdad Esfahbod02fe03e2018-08-25 15:33:05 -07001017 }
1018
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001019 struct accelerator_t
1020 {
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301021 void init (hb_face_t *face)
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001022 {
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +03301023 this->table = hb_sanitize_context_t ().reference_table<cmap> (face);
Behdad Esfahbod02fe03e2018-08-25 15:33:05 -07001024 bool symbol;
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001025 this->subtable = table->find_best_subtable (&symbol);
Ebrahim Byagowi11aa0462018-11-15 23:10:56 +03301026 this->subtable_uvs = &Null (CmapSubtableFormat14);
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001027 {
Behdad Esfahbode57a6382018-07-23 12:00:02 -07001028 const CmapSubtable *st = table->find_subtable (0, 5);
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001029 if (st && st->u.format == 14)
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001030 subtable_uvs = &st->u.format14;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001031 }
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001032
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001033 this->get_glyph_data = subtable;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001034 if (unlikely (symbol))
Garret Rieger21a181a2018-04-10 15:40:24 -07001035 {
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001036 this->get_glyph_funcZ = get_glyph_from_symbol<CmapSubtable>;
Garret Rieger21a181a2018-04-10 15:40:24 -07001037 } else {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001038 switch (subtable->u.format) {
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001039 /* Accelerate format 4 and format 12. */
Garret Rieger21a181a2018-04-10 15:40:24 -07001040 default:
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001041 this->get_glyph_funcZ = get_glyph_from<CmapSubtable>;
Garret Rieger21a181a2018-04-10 15:40:24 -07001042 break;
1043 case 12:
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001044 this->get_glyph_funcZ = get_glyph_from<CmapSubtableFormat12>;
Garret Rieger21a181a2018-04-10 15:40:24 -07001045 break;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001046 case 4:
1047 {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001048 this->format4_accel.init (&subtable->u.format4);
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001049 this->get_glyph_data = &this->format4_accel;
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001050 this->get_glyph_funcZ = this->format4_accel.get_glyph_func;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001051 }
1052 break;
1053 }
Garret Rieger21a181a2018-04-10 15:40:24 -07001054 }
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001055 }
1056
Ebrahim Byagowie4120082018-12-17 21:31:01 +03301057 void fini () { this->table.destroy (); }
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001058
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301059 bool get_nominal_glyph (hb_codepoint_t unicode,
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001060 hb_codepoint_t *glyph) const
1061 {
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001062 if (unlikely (!this->get_glyph_funcZ)) return false;
1063 return this->get_glyph_funcZ (this->get_glyph_data, unicode, glyph);
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001064 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301065 unsigned int get_nominal_glyphs (unsigned int count,
1066 const hb_codepoint_t *first_unicode,
1067 unsigned int unicode_stride,
1068 hb_codepoint_t *first_glyph,
1069 unsigned int glyph_stride) const
Behdad Esfahbod56ba9982018-11-05 19:49:54 -05001070 {
1071 if (unlikely (!this->get_glyph_funcZ)) return 0;
1072
1073 hb_cmap_get_glyph_func_t get_glyph_funcZ = this->get_glyph_funcZ;
1074 const void *get_glyph_data = this->get_glyph_data;
1075
1076 unsigned int done;
1077 for (done = 0;
1078 done < count && get_glyph_funcZ (get_glyph_data, *first_unicode, first_glyph);
1079 done++)
1080 {
Behdad Esfahbod447323b2019-01-22 12:45:40 +01001081 first_unicode = &StructAtOffsetUnaligned<hb_codepoint_t> (first_unicode, unicode_stride);
1082 first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
Behdad Esfahbod56ba9982018-11-05 19:49:54 -05001083 }
1084 return done;
1085 }
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001086
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301087 bool get_variation_glyph (hb_codepoint_t unicode,
1088 hb_codepoint_t variation_selector,
1089 hb_codepoint_t *glyph) const
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001090 {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001091 switch (this->subtable_uvs->get_glyph_variant (unicode,
1092 variation_selector,
1093 glyph))
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001094 {
Behdad Esfahbod36ed1632018-07-23 11:57:45 -07001095 case GLYPH_VARIANT_NOT_FOUND: return false;
1096 case GLYPH_VARIANT_FOUND: return true;
1097 case GLYPH_VARIANT_USE_DEFAULT: break;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001098 }
1099
1100 return get_nominal_glyph (unicode, glyph);
1101 }
1102
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301103 void collect_unicodes (hb_set_t *out) const
Garret Rieger21a181a2018-04-10 15:40:24 -07001104 {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001105 subtable->collect_unicodes (out);
Garret Rieger21a181a2018-04-10 15:40:24 -07001106 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301107 void collect_variation_selectors (hb_set_t *out) const
Behdad Esfahbod4806b382018-08-25 15:56:07 -07001108 {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001109 subtable_uvs->collect_variation_selectors (out);
Behdad Esfahbod4806b382018-08-25 15:56:07 -07001110 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301111 void collect_variation_unicodes (hb_codepoint_t variation_selector,
1112 hb_set_t *out) const
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -07001113 {
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001114 subtable_uvs->collect_variation_unicodes (variation_selector, out);
Behdad Esfahbod1dcf5fb2018-08-25 16:11:26 -07001115 }
Garret Rieger21a181a2018-04-10 15:40:24 -07001116
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001117 protected:
1118 typedef bool (*hb_cmap_get_glyph_func_t) (const void *obj,
1119 hb_codepoint_t codepoint,
1120 hb_codepoint_t *glyph);
Garret Rieger21a181a2018-04-10 15:40:24 -07001121
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001122 template <typename Type>
Behdad Esfahbod95df00a2019-04-12 17:50:03 -04001123 HB_INTERNAL static bool get_glyph_from (const void *obj,
1124 hb_codepoint_t codepoint,
1125 hb_codepoint_t *glyph)
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001126 {
1127 const Type *typed_obj = (const Type *) obj;
1128 return typed_obj->get_glyph (codepoint, glyph);
1129 }
1130
1131 template <typename Type>
Behdad Esfahbod95df00a2019-04-12 17:50:03 -04001132 HB_INTERNAL static bool get_glyph_from_symbol (const void *obj,
1133 hb_codepoint_t codepoint,
1134 hb_codepoint_t *glyph)
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001135 {
1136 const Type *typed_obj = (const Type *) obj;
1137 if (likely (typed_obj->get_glyph (codepoint, glyph)))
1138 return true;
1139
1140 if (codepoint <= 0x00FFu)
1141 {
1142 /* For symbol-encoded OpenType fonts, we duplicate the
1143 * U+F000..F0FF range at U+0000..U+00FF. That's what
1144 * Windows seems to do, and that's hinted about at:
Ebrahim Byagowif24b0b92018-04-12 13:40:45 +04301145 * https://docs.microsoft.com/en-us/typography/opentype/spec/recom
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001146 * under "Non-Standard (Symbol) Fonts". */
1147 return typed_obj->get_glyph (0xF000u + codepoint, glyph);
1148 }
1149
1150 return false;
1151 }
1152
1153 private:
Behdad Esfahbod36d85dc2018-11-05 19:46:29 -05001154 hb_nonnull_ptr_t<const CmapSubtable> subtable;
1155 hb_nonnull_ptr_t<const CmapSubtableFormat14> subtable_uvs;
Behdad Esfahbod4806b382018-08-25 15:56:07 -07001156
Behdad Esfahbodbb380ec2018-11-05 13:45:12 -05001157 hb_cmap_get_glyph_func_t get_glyph_funcZ;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001158 const void *get_glyph_data;
Garret Rieger21a181a2018-04-10 15:40:24 -07001159
Behdad Esfahbod36ed1632018-07-23 11:57:45 -07001160 CmapSubtableFormat4::accelerator_t format4_accel;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001161
Behdad Esfahbod0e2680a2018-11-11 00:28:47 -05001162 hb_blob_ptr_t<cmap> table;
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001163 };
1164
1165 protected:
1166
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301167 const CmapSubtable *find_subtable (unsigned int platform_id,
1168 unsigned int encoding_id) const
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001169 {
1170 EncodingRecord key;
Behdad Esfahbodb986c6a2019-03-29 20:17:46 -07001171 key.platformID = platform_id;
1172 key.encodingID = encoding_id;
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001173
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -05001174 const EncodingRecord &result = encodingRecord.bsearch (key);
1175 if (!result.subtable)
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +02001176 return nullptr;
Behdad Esfahbod3608a682014-05-12 13:46:29 -04001177
Behdad Esfahbod30cb45b2018-11-24 00:35:31 -05001178 return &(this+result.subtable);
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001179 }
1180
Qunxin Liu37572882019-06-25 13:17:30 -07001181 const EncodingRecord *find_encodingrec (unsigned int platform_id,
1182 unsigned int encoding_id) const
1183 {
1184 EncodingRecord key;
1185 key.platformID = platform_id;
1186 key.encodingID = encoding_id;
1187
1188 return encodingRecord.as_array ().bsearch (key);
1189 }
1190
Qunxin Liu993d81b2019-05-14 13:55:11 -07001191 bool find_subtable (unsigned format) const
1192 {
1193 auto it =
1194 + hb_iter (encodingRecord)
1195 | hb_map (&EncodingRecord::subtable)
1196 | hb_map (hb_add (this))
1197 | hb_filter ([&] (const CmapSubtable& _) { return _.u.format == format; })
1198 ;
1199
1200 return it.len ();
1201 }
1202
Behdad Esfahbode4a45552018-11-22 22:17:49 -05001203 public:
1204
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +03301205 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbode4a45552018-11-22 22:17:49 -05001206 {
1207 TRACE_SANITIZE (this);
1208 return_trace (c->check_struct (this) &&
1209 likely (version == 0) &&
1210 encodingRecord.sanitize (c, this));
1211 }
1212
Behdad Esfahbod977ddff2017-11-14 20:06:19 -08001213 protected:
Behdad Esfahbod6b191782018-01-10 03:07:30 +01001214 HBUINT16 version; /* Table version number (0). */
Behdad Esfahboddf554af2014-06-19 15:39:18 -04001215 SortedArrayOf<EncodingRecord>
Behdad Esfahbodf1a72fe2014-06-04 19:00:29 -04001216 encodingRecord; /* Encoding tables. */
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001217 public:
1218 DEFINE_SIZE_ARRAY (4, encodingRecord);
1219};
1220
Behdad Esfahbod3a0b3a22018-08-26 15:11:24 -07001221struct cmap_accelerator_t : cmap::accelerator_t {};
Behdad Esfahbod41ca1fb2014-05-09 15:35:56 -04001222
1223} /* namespace OT */
1224
1225
1226#endif /* HB_OT_CMAP_TABLE_HH */