1 /*
2 * Copyright © 2018 Adobe 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 * Adobe Author(s): Michiharu Ariza
25 */
26 #ifndef HB_OT_CFF_COMMON_HH
27 #define HB_OT_CFF_COMMON_HH
28
29 #include "hb-open-type.hh"
30 #include "hb-bimap.hh"
31 #include "hb-ot-layout-common.hh"
32 #include "hb-cff-interp-dict-common.hh"
33 #include "hb-subset-plan.hh"
34
35 namespace CFF {
36
37 using namespace OT;
38
39 #define CFF_UNDEF_CODE 0xFFFFFFFF
40
41 using objidx_t = hb_serialize_context_t::objidx_t;
42 using whence_t = hb_serialize_context_t::whence_t;
43
44 /* utility macro */
45 template<typename Type>
StructAtOffsetOrNull(const void * P,unsigned int offset)46 static inline const Type& StructAtOffsetOrNull (const void *P, unsigned int offset)
47 { return offset ? StructAtOffset<Type> (P, offset) : Null (Type); }
48
49 struct code_pair_t
50 {
51 hb_codepoint_t code;
52 hb_codepoint_t glyph;
53 };
54
55 using str_buff_t = hb_vector_t<unsigned char>;
56 using str_buff_vec_t = hb_vector_t<str_buff_t>;
57
58 /* CFF INDEX */
59 template <typename COUNT>
60 struct CFFIndex
61 {
offset_array_sizeCFF::CFFIndex62 unsigned int offset_array_size () const
63 { return offSize * (count + 1); }
64
copyCFF::CFFIndex65 CFFIndex *copy (hb_serialize_context_t *c) const
66 {
67 TRACE_SERIALIZE (this);
68 unsigned int size = get_size ();
69 CFFIndex *out = c->allocate_size<CFFIndex> (size, false);
70 if (likely (out))
71 hb_memcpy (out, this, size);
72 return_trace (out);
73 }
74
75 template <typename Iterable,
76 hb_requires (hb_is_iterable (Iterable))>
serializeCFF::CFFIndex77 bool serialize (hb_serialize_context_t *c,
78 const Iterable &iterable)
79 {
80 TRACE_SERIALIZE (this);
81 auto it = hb_iter (iterable);
82 serialize_header(c, + it | hb_map (hb_iter) | hb_map (hb_len));
83 for (const auto &_ : +it)
84 hb_iter (_).copy (c);
85 return_trace (true);
86 }
87
88 template <typename Iterator,
89 hb_requires (hb_is_iterator (Iterator))>
serialize_headerCFF::CFFIndex90 bool serialize_header (hb_serialize_context_t *c,
91 Iterator it)
92 {
93 TRACE_SERIALIZE (this);
94
95 unsigned total = + it | hb_reduce (hb_add, 0);
96 unsigned off_size = (hb_bit_storage (total + 1) + 7) / 8;
97
98 /* serialize CFFIndex header */
99 if (unlikely (!c->extend_min (this))) return_trace (false);
100 this->count = it.len ();
101 if (!this->count) return_trace (true);
102 if (unlikely (!c->extend (this->offSize))) return_trace (false);
103 this->offSize = off_size;
104 if (unlikely (!c->allocate_size<HBUINT8> (off_size * (this->count + 1), false)))
105 return_trace (false);
106
107 /* serialize indices */
108 unsigned int offset = 1;
109 unsigned int i = 0;
110 for (unsigned _ : +it)
111 {
112 set_offset_at (i++, offset);
113 offset += _;
114 }
115 set_offset_at (i, offset);
116
117 return_trace (true);
118 }
119
120 template <typename Iterable,
121 hb_requires (hb_is_iterable (Iterable))>
total_sizeCFF::CFFIndex122 static unsigned total_size (const Iterable &iterable)
123 {
124 auto it = + hb_iter (iterable) | hb_map (hb_iter) | hb_map (hb_len);
125 if (!it) return 0;
126
127 unsigned total = + it | hb_reduce (hb_add, 0);
128 unsigned off_size = (hb_bit_storage (total + 1) + 7) / 8;
129
130 return min_size + HBUINT8::static_size + (hb_len (it) + 1) * off_size + total;
131 }
132
set_offset_atCFF::CFFIndex133 void set_offset_at (unsigned int index, unsigned int offset)
134 {
135 assert (index <= count);
136 HBUINT8 *p = offsets + offSize * index + offSize;
137 unsigned int size = offSize;
138 for (; size; size--)
139 {
140 --p;
141 *p = offset & 0xFF;
142 offset >>= 8;
143 }
144 }
145
146 private:
offset_atCFF::CFFIndex147 unsigned int offset_at (unsigned int index) const
148 {
149 assert (index <= count);
150
151 unsigned int size = offSize;
152 const HBUINT8 *p = offsets + size * index;
153 switch (size)
154 {
155 case 1: return * (HBUINT8 *) p;
156 case 2: return * (HBUINT16 *) p;
157 case 3: return * (HBUINT24 *) p;
158 case 4: return * (HBUINT32 *) p;
159 default: return 0;
160 }
161 }
162
length_atCFF::CFFIndex163 unsigned int length_at (unsigned int index) const
164 {
165 unsigned offset0 = offset_at (index);
166 unsigned offset1 = offset_at (index + 1);
167 if (unlikely (offset1 < offset0 || offset1 > offset_at (count)))
168 return 0;
169 return offset1 - offset0;
170 }
171
data_baseCFF::CFFIndex172 const unsigned char *data_base () const
173 { return (const unsigned char *) this + min_size + offSize.static_size + offset_array_size (); }
174 public:
175
operator []CFF::CFFIndex176 hb_ubytes_t operator [] (unsigned int index) const
177 {
178 if (unlikely (index >= count)) return hb_ubytes_t ();
179 _hb_compiler_memory_r_barrier ();
180 unsigned length = length_at (index);
181 if (unlikely (!length)) return hb_ubytes_t ();
182 return hb_ubytes_t (data_base () + offset_at (index) - 1, length);
183 }
184
get_sizeCFF::CFFIndex185 unsigned int get_size () const
186 {
187 if (count)
188 return min_size + offSize.static_size + offset_array_size () + (offset_at (count) - 1);
189 return min_size; /* empty CFFIndex contains count only */
190 }
191
sanitizeCFF::CFFIndex192 bool sanitize (hb_sanitize_context_t *c) const
193 {
194 TRACE_SANITIZE (this);
195 return_trace (likely (c->check_struct (this) &&
196 (count == 0 || /* empty INDEX */
197 (count < count + 1u &&
198 c->check_struct (&offSize) && offSize >= 1 && offSize <= 4 &&
199 c->check_array (offsets, offSize, count + 1u) &&
200 c->check_array ((const HBUINT8*) data_base (), 1, offset_at (count) - 1)))));
201 }
202
203 public:
204 COUNT count; /* Number of object data. Note there are (count+1) offsets */
205 private:
206 HBUINT8 offSize; /* The byte size of each offset in the offsets array. */
207 HBUINT8 offsets[HB_VAR_ARRAY];
208 /* The array of (count + 1) offsets into objects array (1-base). */
209 /* HBUINT8 data[HB_VAR_ARRAY]; Object data */
210 public:
211 DEFINE_SIZE_MIN (COUNT::static_size);
212 };
213
214 template <typename COUNT, typename TYPE>
215 struct CFFIndexOf : CFFIndex<COUNT>
216 {
217 template <typename DATA, typename PARAM1, typename PARAM2>
serializeCFF::CFFIndexOf218 bool serialize (hb_serialize_context_t *c,
219 unsigned int offSize_,
220 const DATA *dataArray,
221 unsigned int dataArrayLen,
222 const hb_vector_t<unsigned int> &dataSizeArray,
223 const PARAM1 ¶m1,
224 const PARAM2 ¶m2)
225 {
226 TRACE_SERIALIZE (this);
227 /* serialize CFFIndex header */
228 if (unlikely (!c->extend_min (this))) return_trace (false);
229 this->count = dataArrayLen;
230 this->offSize = offSize_;
231 if (unlikely (!c->allocate_size<HBUINT8> (offSize_ * (dataArrayLen + 1), false)))
232 return_trace (false);
233
234 /* serialize indices */
235 unsigned int offset = 1;
236 unsigned int i = 0;
237 for (; i < dataArrayLen; i++)
238 {
239 this->set_offset_at (i, offset);
240 offset += dataSizeArray[i];
241 }
242 this->set_offset_at (i, offset);
243
244 /* serialize data */
245 for (unsigned int i = 0; i < dataArrayLen; i++)
246 {
247 TYPE *dest = c->start_embed<TYPE> ();
248 if (unlikely (!dest || !dest->serialize (c, dataArray[i], param1, param2)))
249 return_trace (false);
250 }
251 return_trace (true);
252 }
253 };
254
255 /* Top Dict, Font Dict, Private Dict */
256 struct Dict : UnsizedByteStr
257 {
258 template <typename DICTVAL, typename OP_SERIALIZER, typename ...Ts>
serializeCFF::Dict259 bool serialize (hb_serialize_context_t *c,
260 const DICTVAL &dictval,
261 OP_SERIALIZER& opszr,
262 Ts&&... ds)
263 {
264 TRACE_SERIALIZE (this);
265 for (unsigned int i = 0; i < dictval.get_count (); i++)
266 if (unlikely (!opszr.serialize (c, dictval[i], std::forward<Ts> (ds)...)))
267 return_trace (false);
268
269 return_trace (true);
270 }
271
272 template <typename T, typename V>
serialize_int_opCFF::Dict273 static bool serialize_int_op (hb_serialize_context_t *c, op_code_t op, V value, op_code_t intOp)
274 {
275 if (unlikely ((!serialize_int<T, V> (c, intOp, value))))
276 return false;
277
278 TRACE_SERIALIZE (this);
279 /* serialize the opcode */
280 HBUINT8 *p = c->allocate_size<HBUINT8> (OpCode_Size (op), false);
281 if (unlikely (!p)) return_trace (false);
282 if (Is_OpCode_ESC (op))
283 {
284 *p = OpCode_escape;
285 op = Unmake_OpCode_ESC (op);
286 p++;
287 }
288 *p = op;
289 return_trace (true);
290 }
291
292 template <typename V>
serialize_int4_opCFF::Dict293 static bool serialize_int4_op (hb_serialize_context_t *c, op_code_t op, V value)
294 { return serialize_int_op<HBINT32> (c, op, value, OpCode_longintdict); }
295
296 template <typename V>
serialize_int2_opCFF::Dict297 static bool serialize_int2_op (hb_serialize_context_t *c, op_code_t op, V value)
298 { return serialize_int_op<HBINT16> (c, op, value, OpCode_shortint); }
299
300 template <typename T, int int_op>
serialize_link_opCFF::Dict301 static bool serialize_link_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence)
302 {
303 T &ofs = *(T *) (c->head + OpCode_Size (int_op));
304 if (unlikely (!serialize_int_op<T> (c, op, 0, int_op))) return false;
305 c->add_link (ofs, link, whence);
306 return true;
307 }
308
serialize_link4_opCFF::Dict309 static bool serialize_link4_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head)
310 { return serialize_link_op<HBINT32, OpCode_longintdict> (c, op, link, whence); }
311
serialize_link2_opCFF::Dict312 static bool serialize_link2_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head)
313 { return serialize_link_op<HBINT16, OpCode_shortint> (c, op, link, whence); }
314 };
315
316 struct TopDict : Dict {};
317 struct FontDict : Dict {};
318 struct PrivateDict : Dict {};
319
320 struct table_info_t
321 {
initCFF::table_info_t322 void init () { offset = size = 0; link = 0; }
323
324 unsigned int offset;
325 unsigned int size;
326 objidx_t link;
327 };
328
329 template <typename COUNT>
330 struct FDArray : CFFIndexOf<COUNT, FontDict>
331 {
332 template <typename DICTVAL, typename INFO, typename Iterator, typename OP_SERIALIZER>
serializeCFF::FDArray333 bool serialize (hb_serialize_context_t *c,
334 Iterator it,
335 OP_SERIALIZER& opszr)
336 {
337 TRACE_SERIALIZE (this);
338
339 /* serialize INDEX data */
340 hb_vector_t<unsigned> sizes;
341 c->push ();
342 + it
343 | hb_map ([&] (const hb_pair_t<const DICTVAL&, const INFO&> &_)
344 {
345 FontDict *dict = c->start_embed<FontDict> ();
346 dict->serialize (c, _.first, opszr, _.second);
347 return c->head - (const char*)dict;
348 })
349 | hb_sink (sizes)
350 ;
351 c->pop_pack (false);
352
353 /* serialize INDEX header */
354 return_trace (CFFIndex<COUNT>::serialize_header (c, hb_iter (sizes)));
355 }
356 };
357
358 /* FDSelect */
359 struct FDSelect0 {
sanitizeCFF::FDSelect0360 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
361 {
362 TRACE_SANITIZE (this);
363 if (unlikely (!(c->check_struct (this))))
364 return_trace (false);
365 if (unlikely (!c->check_array (fds, c->get_num_glyphs ())))
366 return_trace (false);
367
368 return_trace (true);
369 }
370
get_fdCFF::FDSelect0371 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
372 { return (hb_codepoint_t) fds[glyph]; }
373
get_sizeCFF::FDSelect0374 unsigned int get_size (unsigned int num_glyphs) const
375 { return HBUINT8::static_size * num_glyphs; }
376
377 HBUINT8 fds[HB_VAR_ARRAY];
378
379 DEFINE_SIZE_MIN (0);
380 };
381
382 template <typename GID_TYPE, typename FD_TYPE>
383 struct FDSelect3_4_Range
384 {
sanitizeCFF::FDSelect3_4_Range385 bool sanitize (hb_sanitize_context_t *c, const void * /*nullptr*/, unsigned int fdcount) const
386 {
387 TRACE_SANITIZE (this);
388 return_trace (first < c->get_num_glyphs () && (fd < fdcount));
389 }
390
391 GID_TYPE first;
392 FD_TYPE fd;
393 public:
394 DEFINE_SIZE_STATIC (GID_TYPE::static_size + FD_TYPE::static_size);
395 };
396
397 template <typename GID_TYPE, typename FD_TYPE>
398 struct FDSelect3_4
399 {
get_sizeCFF::FDSelect3_4400 unsigned int get_size () const
401 { return GID_TYPE::static_size * 2 + ranges.get_size (); }
402
sanitizeCFF::FDSelect3_4403 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
404 {
405 TRACE_SANITIZE (this);
406 if (unlikely (!c->check_struct (this) || !ranges.sanitize (c, nullptr, fdcount) ||
407 (nRanges () == 0) || ranges[0].first != 0))
408 return_trace (false);
409
410 for (unsigned int i = 1; i < nRanges (); i++)
411 if (unlikely (ranges[i - 1].first >= ranges[i].first))
412 return_trace (false);
413
414 if (unlikely (!sentinel().sanitize (c) || (sentinel() != c->get_num_glyphs ())))
415 return_trace (false);
416
417 return_trace (true);
418 }
419
_cmp_rangeCFF::FDSelect3_4420 static int _cmp_range (const void *_key, const void *_item)
421 {
422 hb_codepoint_t glyph = * (hb_codepoint_t *) _key;
423 FDSelect3_4_Range<GID_TYPE, FD_TYPE> *range = (FDSelect3_4_Range<GID_TYPE, FD_TYPE> *) _item;
424
425 if (glyph < range[0].first) return -1;
426 if (glyph < range[1].first) return 0;
427 return +1;
428 }
429
get_fdCFF::FDSelect3_4430 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
431 {
432 auto *range = hb_bsearch (glyph, &ranges[0], nRanges () - 1, sizeof (ranges[0]), _cmp_range);
433 return range ? range->fd : ranges[nRanges () - 1].fd;
434 }
435
nRangesCFF::FDSelect3_4436 GID_TYPE &nRanges () { return ranges.len; }
nRangesCFF::FDSelect3_4437 GID_TYPE nRanges () const { return ranges.len; }
sentinelCFF::FDSelect3_4438 GID_TYPE &sentinel () { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
sentinelCFF::FDSelect3_4439 const GID_TYPE &sentinel () const { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
440
441 ArrayOf<FDSelect3_4_Range<GID_TYPE, FD_TYPE>, GID_TYPE> ranges;
442 /* GID_TYPE sentinel */
443
444 DEFINE_SIZE_ARRAY (GID_TYPE::static_size, ranges);
445 };
446
447 typedef FDSelect3_4<HBUINT16, HBUINT8> FDSelect3;
448 typedef FDSelect3_4_Range<HBUINT16, HBUINT8> FDSelect3_Range;
449
450 struct FDSelect
451 {
serializeCFF::FDSelect452 bool serialize (hb_serialize_context_t *c, const FDSelect &src, unsigned int num_glyphs)
453 {
454 TRACE_SERIALIZE (this);
455 unsigned int size = src.get_size (num_glyphs);
456 FDSelect *dest = c->allocate_size<FDSelect> (size, false);
457 if (unlikely (!dest)) return_trace (false);
458 hb_memcpy (dest, &src, size);
459 return_trace (true);
460 }
461
get_sizeCFF::FDSelect462 unsigned int get_size (unsigned int num_glyphs) const
463 {
464 switch (format)
465 {
466 case 0: return format.static_size + u.format0.get_size (num_glyphs);
467 case 3: return format.static_size + u.format3.get_size ();
468 default:return 0;
469 }
470 }
471
get_fdCFF::FDSelect472 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
473 {
474 if (this == &Null (FDSelect)) return 0;
475
476 switch (format)
477 {
478 case 0: return u.format0.get_fd (glyph);
479 case 3: return u.format3.get_fd (glyph);
480 default:return 0;
481 }
482 }
483
sanitizeCFF::FDSelect484 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
485 {
486 TRACE_SANITIZE (this);
487 if (unlikely (!c->check_struct (this)))
488 return_trace (false);
489
490 switch (format)
491 {
492 case 0: return_trace (u.format0.sanitize (c, fdcount));
493 case 3: return_trace (u.format3.sanitize (c, fdcount));
494 default:return_trace (false);
495 }
496 }
497
498 HBUINT8 format;
499 union {
500 FDSelect0 format0;
501 FDSelect3 format3;
502 } u;
503 public:
504 DEFINE_SIZE_MIN (1);
505 };
506
507 template <typename COUNT>
508 struct Subrs : CFFIndex<COUNT>
509 {
510 typedef COUNT count_type;
511 typedef CFFIndex<COUNT> SUPER;
512 };
513
514 } /* namespace CFF */
515
516 #endif /* HB_OT_CFF_COMMON_HH */
517