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
35namespace CFF {
36
37using namespace OT;
38
39#define CFF_UNDEF_CODE 0xFFFFFFFF
40
41using objidx_t = hb_serialize_context_t::objidx_t;
42using whence_t = hb_serialize_context_t::whence_t;
43
44/* utility macro */
45template<typename Type>
46static inline const Type& StructAtOffsetOrNull (const void *P, unsigned int offset)
47{ return offset ? StructAtOffset<Type> (P, offset) : Null (Type); }
48
49inline unsigned int calcOffSize (unsigned int dataSize)
50{
51 unsigned int size = 1;
52 unsigned int offset = dataSize + 1;
53 while (offset & ~0xFF)
54 {
55 size++;
56 offset >>= 8;
57 }
58 /* format does not support size > 4; caller should handle it as an error */
59 return size;
60}
61
62struct code_pair_t
63{
64 hb_codepoint_t code;
65 hb_codepoint_t glyph;
66};
67
68typedef hb_vector_t<unsigned char> str_buff_t;
69struct str_buff_vec_t : hb_vector_t<str_buff_t>
70{
71 void fini () { SUPER::fini_deep (); }
72
73 unsigned int total_size () const
74 {
75 unsigned int size = 0;
76 for (unsigned int i = 0; i < length; i++)
77 size += (*this)[i].length;
78 return size;
79 }
80
81 private:
82 typedef hb_vector_t<str_buff_t> SUPER;
83};
84
85/* CFF INDEX */
86template <typename COUNT>
87struct CFFIndex
88{
89 static unsigned int calculate_offset_array_size (unsigned int offSize, unsigned int count)
90 { return offSize * (count + 1); }
91
92 unsigned int offset_array_size () const
93 { return calculate_offset_array_size (offSize, count); }
94
95 CFFIndex *copy (hb_serialize_context_t *c) const
96 {
97 TRACE_SERIALIZE (this);
98 unsigned int size = get_size ();
99 CFFIndex *out = c->allocate_size<CFFIndex> (size);
100 if (likely (out))
101 memcpy (out, this, size);
102 return_trace (out);
103 }
104
105 bool serialize (hb_serialize_context_t *c, const CFFIndex &src)
106 {
107 TRACE_SERIALIZE (this);
108 unsigned int size = src.get_size ();
109 CFFIndex *dest = c->allocate_size<CFFIndex> (size);
110 if (unlikely (!dest)) return_trace (false);
111 memcpy (dest, &src, size);
112 return_trace (true);
113 }
114
115 bool serialize (hb_serialize_context_t *c,
116 unsigned int offSize_,
117 const byte_str_array_t &byteArray)
118 {
119 TRACE_SERIALIZE (this);
120 if (byteArray.length == 0)
121 {
122 COUNT *dest = c->allocate_min<COUNT> ();
123 if (unlikely (!dest)) return_trace (false);
124 *dest = 0;
125 }
126 else
127 {
128 /* serialize CFFIndex header */
129 if (unlikely (!c->extend_min (*this))) return_trace (false);
130 this->count = byteArray.length;
131 this->offSize = offSize_;
132 if (unlikely (!c->allocate_size<HBUINT8> (offSize_ * (byteArray.length + 1))))
133 return_trace (false);
134
135 /* serialize indices */
136 unsigned int offset = 1;
137 unsigned int i = 0;
138 for (; i < byteArray.length; i++)
139 {
140 set_offset_at (i, offset);
141 offset += byteArray[i].get_size ();
142 }
143 set_offset_at (i, offset);
144
145 /* serialize data */
146 for (unsigned int i = 0; i < byteArray.length; i++)
147 {
148 const byte_str_t &bs = byteArray[i];
149 unsigned char *dest = c->allocate_size<unsigned char> (bs.length);
150 if (unlikely (!dest)) return_trace (false);
151 memcpy (dest, &bs[0], bs.length);
152 }
153 }
154 return_trace (true);
155 }
156
157 bool serialize (hb_serialize_context_t *c,
158 unsigned int offSize_,
159 const str_buff_vec_t &buffArray)
160 {
161 byte_str_array_t byteArray;
162 byteArray.init ();
163 byteArray.resize (buffArray.length);
164 for (unsigned int i = 0; i < byteArray.length; i++)
165 byteArray[i] = byte_str_t (buffArray[i].arrayZ, buffArray[i].length);
166 bool result = this->serialize (c, offSize_, byteArray);
167 byteArray.fini ();
168 return result;
169 }
170
171 template <typename Iterator,
172 hb_requires (hb_is_iterator (Iterator))>
173 bool serialize (hb_serialize_context_t *c,
174 Iterator it)
175 {
176 TRACE_SERIALIZE (this);
177 if (it.len () == 0)
178 {
179 COUNT *dest = c->allocate_min<COUNT> ();
180 if (unlikely (!dest)) return_trace (false);
181 *dest = 0;
182 }
183 else
184 {
185 serialize_header(c, + it | hb_map ([] (const byte_str_t &_) { return _.length; }));
186 for (const byte_str_t &_ : +it)
187 _.copy (c);
188 }
189 return_trace (true);
190 }
191
192 bool serialize (hb_serialize_context_t *c,
193 const byte_str_array_t &byteArray)
194 { return serialize (c, + hb_iter (byteArray)); }
195
196 bool serialize (hb_serialize_context_t *c,
197 const str_buff_vec_t &buffArray)
198 {
199 auto it =
200 + hb_iter (buffArray)
201 | hb_map ([] (const str_buff_t &_) { return byte_str_t (_.arrayZ, _.length); })
202 ;
203 return serialize (c, it);
204 }
205
206 template <typename Iterator,
207 hb_requires (hb_is_iterator (Iterator))>
208 bool serialize_header (hb_serialize_context_t *c,
209 Iterator it)
210 {
211 TRACE_SERIALIZE (this);
212
213 unsigned total = + it | hb_reduce (hb_add, 0);
214 unsigned off_size = calcOffSize (total);
215
216 /* serialize CFFIndex header */
217 if (unlikely (!c->extend_min (*this))) return_trace (false);
218 this->count = it.len ();
219 this->offSize = off_size;
220 if (unlikely (!c->allocate_size<HBUINT8> (off_size * (it.len () + 1))))
221 return_trace (false);
222
223 /* serialize indices */
224 unsigned int offset = 1;
225 unsigned int i = 0;
226 for (unsigned _ : +it)
227 {
228 CFFIndex<COUNT>::set_offset_at (i++, offset);
229 offset += _;
230 }
231 CFFIndex<COUNT>::set_offset_at (i, offset);
232
233 return_trace (true);
234 }
235
236 void set_offset_at (unsigned int index, unsigned int offset)
237 {
238 HBUINT8 *p = offsets + offSize * index + offSize;
239 unsigned int size = offSize;
240 for (; size; size--)
241 {
242 --p;
243 *p = offset & 0xFF;
244 offset >>= 8;
245 }
246 }
247
248 unsigned int offset_at (unsigned int index) const
249 {
250 assert (index <= count);
251 const HBUINT8 *p = offsets + offSize * index;
252 unsigned int size = offSize;
253 unsigned int offset = 0;
254 for (; size; size--)
255 offset = (offset << 8) + *p++;
256 return offset;
257 }
258
259 unsigned int length_at (unsigned int index) const
260 {
261 if (unlikely ((offset_at (index + 1) < offset_at (index)) ||
262 (offset_at (index + 1) > offset_at (count))))
263 return 0;
264 return offset_at (index + 1) - offset_at (index);
265 }
266
267 const unsigned char *data_base () const
268 { return (const unsigned char *) this + min_size + offset_array_size (); }
269
270 unsigned int data_size () const { return HBINT8::static_size; }
271
272 byte_str_t operator [] (unsigned int index) const
273 {
274 if (unlikely (index >= count)) return Null (byte_str_t);
275 return byte_str_t (data_base () + offset_at (index) - 1, length_at (index));
276 }
277
278 unsigned int get_size () const
279 {
280 if (this == &Null (CFFIndex)) return 0;
281 if (count > 0)
282 return min_size + offset_array_size () + (offset_at (count) - 1);
283 return count.static_size; /* empty CFFIndex contains count only */
284 }
285
286 bool sanitize (hb_sanitize_context_t *c) const
287 {
288 TRACE_SANITIZE (this);
289 return_trace (likely ((c->check_struct (this) && count == 0) || /* empty INDEX */
290 (c->check_struct (this) && offSize >= 1 && offSize <= 4 &&
291 c->check_array (offsets, offSize, count + 1) &&
292 c->check_array ((const HBUINT8*) data_base (), 1, max_offset () - 1))));
293 }
294
295 protected:
296 unsigned int max_offset () const
297 {
298 unsigned int max = 0;
299 for (unsigned int i = 0; i < count + 1u; i++)
300 {
301 unsigned int off = offset_at (i);
302 if (off > max) max = off;
303 }
304 return max;
305 }
306
307 public:
308 COUNT count; /* Number of object data. Note there are (count+1) offsets */
309 HBUINT8 offSize; /* The byte size of each offset in the offsets array. */
310 HBUINT8 offsets[HB_VAR_ARRAY];
311 /* The array of (count + 1) offsets into objects array (1-base). */
312 /* HBUINT8 data[HB_VAR_ARRAY]; Object data */
313 public:
314 DEFINE_SIZE_ARRAY (COUNT::static_size + HBUINT8::static_size, offsets);
315};
316
317template <typename COUNT, typename TYPE>
318struct CFFIndexOf : CFFIndex<COUNT>
319{
320 const byte_str_t operator [] (unsigned int index) const
321 {
322 if (likely (index < CFFIndex<COUNT>::count))
323 return byte_str_t (CFFIndex<COUNT>::data_base () + CFFIndex<COUNT>::offset_at (index) - 1, CFFIndex<COUNT>::length_at (index));
324 return Null (byte_str_t);
325 }
326
327 template <typename DATA, typename PARAM1, typename PARAM2>
328 bool serialize (hb_serialize_context_t *c,
329 unsigned int offSize_,
330 const DATA *dataArray,
331 unsigned int dataArrayLen,
332 const hb_vector_t<unsigned int> &dataSizeArray,
333 const PARAM1 &param1,
334 const PARAM2 &param2)
335 {
336 TRACE_SERIALIZE (this);
337 /* serialize CFFIndex header */
338 if (unlikely (!c->extend_min (*this))) return_trace (false);
339 this->count = dataArrayLen;
340 this->offSize = offSize_;
341 if (unlikely (!c->allocate_size<HBUINT8> (offSize_ * (dataArrayLen + 1))))
342 return_trace (false);
343
344 /* serialize indices */
345 unsigned int offset = 1;
346 unsigned int i = 0;
347 for (; i < dataArrayLen; i++)
348 {
349 CFFIndex<COUNT>::set_offset_at (i, offset);
350 offset += dataSizeArray[i];
351 }
352 CFFIndex<COUNT>::set_offset_at (i, offset);
353
354 /* serialize data */
355 for (unsigned int i = 0; i < dataArrayLen; i++)
356 {
357 TYPE *dest = c->start_embed<TYPE> ();
358 if (unlikely (!dest || !dest->serialize (c, dataArray[i], param1, param2)))
359 return_trace (false);
360 }
361 return_trace (true);
362 }
363};
364
365/* Top Dict, Font Dict, Private Dict */
366struct Dict : UnsizedByteStr
367{
368 template <typename DICTVAL, typename OP_SERIALIZER, typename ...Ts>
369 bool serialize (hb_serialize_context_t *c,
370 const DICTVAL &dictval,
371 OP_SERIALIZER& opszr,
372 Ts&&... ds)
373 {
374 TRACE_SERIALIZE (this);
375 for (unsigned int i = 0; i < dictval.get_count (); i++)
376 if (unlikely (!opszr.serialize (c, dictval[i], hb_forward<Ts> (ds)...)))
377 return_trace (false);
378
379 return_trace (true);
380 }
381
382 template <typename T, typename V>
383 static bool serialize_int_op (hb_serialize_context_t *c, op_code_t op, V value, op_code_t intOp)
384 {
385 // XXX: not sure why but LLVM fails to compile the following 'unlikely' macro invocation
386 if (/*unlikely*/ (!serialize_int<T, V> (c, intOp, value)))
387 return false;
388
389 TRACE_SERIALIZE (this);
390 /* serialize the opcode */
391 HBUINT8 *p = c->allocate_size<HBUINT8> (OpCode_Size (op));
392 if (unlikely (!p)) return_trace (false);
393 if (Is_OpCode_ESC (op))
394 {
395 *p = OpCode_escape;
396 op = Unmake_OpCode_ESC (op);
397 p++;
398 }
399 *p = op;
400 return_trace (true);
401 }
402
403 template <typename V>
404 static bool serialize_int4_op (hb_serialize_context_t *c, op_code_t op, V value)
405 { return serialize_int_op<HBINT32> (c, op, value, OpCode_longintdict); }
406
407 template <typename V>
408 static bool serialize_int2_op (hb_serialize_context_t *c, op_code_t op, V value)
409 { return serialize_int_op<HBINT16> (c, op, value, OpCode_shortint); }
410
411 template <typename T, int int_op>
412 static bool serialize_link_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence)
413 {
414 T &ofs = *(T *) (c->head + OpCode_Size (int_op));
415 if (unlikely (!serialize_int_op<T> (c, op, 0, int_op))) return false;
416 c->add_link (ofs, link, whence);
417 return true;
418 }
419
420 static bool serialize_link4_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head)
421 { return serialize_link_op<HBINT32, OpCode_longintdict> (c, op, link, whence); }
422
423 static bool serialize_link2_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head)
424 { return serialize_link_op<HBINT16, OpCode_shortint> (c, op, link, whence); }
425};
426
427struct TopDict : Dict {};
428struct FontDict : Dict {};
429struct PrivateDict : Dict {};
430
431struct table_info_t
432{
433 void init () { offset = size = 0; link = 0; }
434
435 unsigned int offset;
436 unsigned int size;
437 objidx_t link;
438};
439
440template <typename COUNT>
441struct FDArray : CFFIndexOf<COUNT, FontDict>
442{
443 template <typename DICTVAL, typename INFO, typename Iterator, typename OP_SERIALIZER>
444 bool serialize (hb_serialize_context_t *c,
445 Iterator it,
446 OP_SERIALIZER& opszr)
447 {
448 TRACE_SERIALIZE (this);
449
450 /* serialize INDEX data */
451 hb_vector_t<unsigned> sizes;
452 c->push ();
453 + it
454 | hb_map ([&] (const hb_pair_t<const DICTVAL&, const INFO&> &_)
455 {
456 FontDict *dict = c->start_embed<FontDict> ();
457 dict->serialize (c, _.first, opszr, _.second);
458 return c->head - (const char*)dict;
459 })
460 | hb_sink (sizes)
461 ;
462 c->pop_pack (false);
463
464 /* serialize INDEX header */
465 return_trace (CFFIndex<COUNT>::serialize_header (c, hb_iter (sizes)));
466 }
467};
468
469/* FDSelect */
470struct FDSelect0 {
471 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
472 {
473 TRACE_SANITIZE (this);
474 if (unlikely (!(c->check_struct (this))))
475 return_trace (false);
476 for (unsigned int i = 0; i < c->get_num_glyphs (); i++)
477 if (unlikely (!fds[i].sanitize (c)))
478 return_trace (false);
479
480 return_trace (true);
481 }
482
483 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
484 { return (hb_codepoint_t) fds[glyph]; }
485
486 unsigned int get_size (unsigned int num_glyphs) const
487 { return HBUINT8::static_size * num_glyphs; }
488
489 HBUINT8 fds[HB_VAR_ARRAY];
490
491 DEFINE_SIZE_MIN (0);
492};
493
494template <typename GID_TYPE, typename FD_TYPE>
495struct FDSelect3_4_Range
496{
497 bool sanitize (hb_sanitize_context_t *c, const void * /*nullptr*/, unsigned int fdcount) const
498 {
499 TRACE_SANITIZE (this);
500 return_trace (first < c->get_num_glyphs () && (fd < fdcount));
501 }
502
503 GID_TYPE first;
504 FD_TYPE fd;
505 public:
506 DEFINE_SIZE_STATIC (GID_TYPE::static_size + FD_TYPE::static_size);
507};
508
509template <typename GID_TYPE, typename FD_TYPE>
510struct FDSelect3_4
511{
512 unsigned int get_size () const
513 { return GID_TYPE::static_size * 2 + ranges.get_size (); }
514
515 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
516 {
517 TRACE_SANITIZE (this);
518 if (unlikely (!c->check_struct (this) || !ranges.sanitize (c, nullptr, fdcount) ||
519 (nRanges () == 0) || ranges[0].first != 0))
520 return_trace (false);
521
522 for (unsigned int i = 1; i < nRanges (); i++)
523 if (unlikely (ranges[i - 1].first >= ranges[i].first))
524 return_trace (false);
525
526 if (unlikely (!sentinel().sanitize (c) || (sentinel() != c->get_num_glyphs ())))
527 return_trace (false);
528
529 return_trace (true);
530 }
531
532 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
533 {
534 unsigned int i;
535 for (i = 1; i < nRanges (); i++)
536 if (glyph < ranges[i].first)
537 break;
538
539 return (hb_codepoint_t) ranges[i - 1].fd;
540 }
541
542 GID_TYPE &nRanges () { return ranges.len; }
543 GID_TYPE nRanges () const { return ranges.len; }
544 GID_TYPE &sentinel () { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
545 const GID_TYPE &sentinel () const { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
546
547 ArrayOf<FDSelect3_4_Range<GID_TYPE, FD_TYPE>, GID_TYPE> ranges;
548 /* GID_TYPE sentinel */
549
550 DEFINE_SIZE_ARRAY (GID_TYPE::static_size, ranges);
551};
552
553typedef FDSelect3_4<HBUINT16, HBUINT8> FDSelect3;
554typedef FDSelect3_4_Range<HBUINT16, HBUINT8> FDSelect3_Range;
555
556struct FDSelect
557{
558 bool serialize (hb_serialize_context_t *c, const FDSelect &src, unsigned int num_glyphs)
559 {
560 TRACE_SERIALIZE (this);
561 unsigned int size = src.get_size (num_glyphs);
562 FDSelect *dest = c->allocate_size<FDSelect> (size);
563 if (unlikely (!dest)) return_trace (false);
564 memcpy (dest, &src, size);
565 return_trace (true);
566 }
567
568 unsigned int get_size (unsigned int num_glyphs) const
569 {
570 switch (format)
571 {
572 case 0: return format.static_size + u.format0.get_size (num_glyphs);
573 case 3: return format.static_size + u.format3.get_size ();
574 default:return 0;
575 }
576 }
577
578 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
579 {
580 if (this == &Null (FDSelect)) return 0;
581
582 switch (format)
583 {
584 case 0: return u.format0.get_fd (glyph);
585 case 3: return u.format3.get_fd (glyph);
586 default:return 0;
587 }
588 }
589
590 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
591 {
592 TRACE_SANITIZE (this);
593 if (unlikely (!c->check_struct (this)))
594 return_trace (false);
595
596 switch (format)
597 {
598 case 0: return_trace (u.format0.sanitize (c, fdcount));
599 case 3: return_trace (u.format3.sanitize (c, fdcount));
600 default:return_trace (false);
601 }
602 }
603
604 HBUINT8 format;
605 union {
606 FDSelect0 format0;
607 FDSelect3 format3;
608 } u;
609 public:
610 DEFINE_SIZE_MIN (1);
611};
612
613template <typename COUNT>
614struct Subrs : CFFIndex<COUNT>
615{
616 typedef COUNT count_type;
617 typedef CFFIndex<COUNT> SUPER;
618};
619
620} /* namespace CFF */
621
622#endif /* HB_OT_CFF_COMMON_HH */
623