2 * Copyright © 2018 Adobe Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Adobe Author(s): Michiharu Ariza
26 #ifndef HB_OT_CFF_COMMON_HH
27 #define HB_OT_CFF_COMMON_HH
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"
39 #define CFF_UNDEF_CODE 0xFFFFFFFF
41 using objidx_t = hb_serialize_context_t::objidx_t;
42 using whence_t = hb_serialize_context_t::whence_t;
45 template<typename Type>
46 static inline const Type& StructAtOffsetOrNull (const void *P, unsigned int offset)
47 { return offset ? StructAtOffset<Type> (P, offset) : Null (Type); }
49 inline unsigned int calcOffSize (unsigned int dataSize)
51 unsigned int size = 1;
52 unsigned int offset = dataSize + 1;
53 while (offset & ~0xFF)
58 /* format does not support size > 4; caller should handle it as an error */
68 typedef hb_vector_t<unsigned char> str_buff_t;
69 struct str_buff_vec_t : hb_vector_t<str_buff_t>
71 void fini () { SUPER::fini_deep (); }
73 unsigned int total_size () const
75 unsigned int size = 0;
76 for (unsigned int i = 0; i < length; i++)
77 size += (*this)[i].length;
82 typedef hb_vector_t<str_buff_t> SUPER;
86 template <typename COUNT>
89 static unsigned int calculate_offset_array_size (unsigned int offSize, unsigned int count)
90 { return offSize * (count + 1); }
92 unsigned int offset_array_size () const
93 { return calculate_offset_array_size (offSize, count); }
95 CFFIndex *copy (hb_serialize_context_t *c) const
97 TRACE_SERIALIZE (this);
98 unsigned int size = get_size ();
99 CFFIndex *out = c->allocate_size<CFFIndex> (size);
101 memcpy (out, this, size);
105 bool serialize (hb_serialize_context_t *c, const CFFIndex &src)
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);
115 bool serialize (hb_serialize_context_t *c,
116 unsigned int offSize_,
117 const byte_str_array_t &byteArray)
119 TRACE_SERIALIZE (this);
120 if (byteArray.length == 0)
122 COUNT *dest = c->allocate_min<COUNT> ();
123 if (unlikely (!dest)) return_trace (false);
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);
135 /* serialize indices */
136 unsigned int offset = 1;
138 for (; i < byteArray.length; i++)
140 set_offset_at (i, offset);
141 offset += byteArray[i].get_size ();
143 set_offset_at (i, offset);
146 for (unsigned int i = 0; i < byteArray.length; i++)
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);
157 bool serialize (hb_serialize_context_t *c,
158 unsigned int offSize_,
159 const str_buff_vec_t &buffArray)
161 byte_str_array_t byteArray;
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);
171 template <typename Iterator,
172 hb_requires (hb_is_iterator (Iterator))>
173 bool serialize (hb_serialize_context_t *c,
176 TRACE_SERIALIZE (this);
179 COUNT *dest = c->allocate_min<COUNT> ();
180 if (unlikely (!dest)) return_trace (false);
185 serialize_header(c, + it | hb_map ([] (const byte_str_t &_) { return _.length; }));
186 for (const byte_str_t &_ : +it)
192 bool serialize (hb_serialize_context_t *c,
193 const byte_str_array_t &byteArray)
194 { return serialize (c, + hb_iter (byteArray)); }
196 bool serialize (hb_serialize_context_t *c,
197 const str_buff_vec_t &buffArray)
200 + hb_iter (buffArray)
201 | hb_map ([] (const str_buff_t &_) { return byte_str_t (_.arrayZ, _.length); })
203 return serialize (c, it);
206 template <typename Iterator,
207 hb_requires (hb_is_iterator (Iterator))>
208 bool serialize_header (hb_serialize_context_t *c,
211 TRACE_SERIALIZE (this);
213 unsigned total = + it | hb_reduce (hb_add, 0);
214 unsigned off_size = calcOffSize (total);
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);
223 /* serialize indices */
224 unsigned int offset = 1;
226 for (unsigned _ : +it)
228 CFFIndex<COUNT>::set_offset_at (i++, offset);
231 CFFIndex<COUNT>::set_offset_at (i, offset);
236 void set_offset_at (unsigned int index, unsigned int offset)
238 HBUINT8 *p = offsets + offSize * index + offSize;
239 unsigned int size = offSize;
248 unsigned int offset_at (unsigned int index) const
250 assert (index <= count);
251 const HBUINT8 *p = offsets + offSize * index;
252 unsigned int size = offSize;
253 unsigned int offset = 0;
255 offset = (offset << 8) + *p++;
259 unsigned int length_at (unsigned int index) const
261 if (unlikely ((offset_at (index + 1) < offset_at (index)) ||
262 (offset_at (index + 1) > offset_at (count))))
264 return offset_at (index + 1) - offset_at (index);
267 const unsigned char *data_base () const
268 { return (const unsigned char *) this + min_size + offset_array_size (); }
270 unsigned int data_size () const { return HBINT8::static_size; }
272 byte_str_t operator [] (unsigned int index) const
274 if (unlikely (index >= count)) return Null (byte_str_t);
275 return byte_str_t (data_base () + offset_at (index) - 1, length_at (index));
278 unsigned int get_size () const
280 if (this == &Null (CFFIndex)) return 0;
282 return min_size + offset_array_size () + (offset_at (count) - 1);
283 return count.static_size; /* empty CFFIndex contains count only */
286 bool sanitize (hb_sanitize_context_t *c) const
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))));
296 unsigned int max_offset () const
298 unsigned int max = 0;
299 for (unsigned int i = 0; i < count + 1u; i++)
301 unsigned int off = offset_at (i);
302 if (off > max) max = off;
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 */
314 DEFINE_SIZE_ARRAY (COUNT::static_size + HBUINT8::static_size, offsets);
317 template <typename COUNT, typename TYPE>
318 struct CFFIndexOf : CFFIndex<COUNT>
320 const byte_str_t operator [] (unsigned int index) const
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);
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 ¶m1,
334 const PARAM2 ¶m2)
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);
344 /* serialize indices */
345 unsigned int offset = 1;
347 for (; i < dataArrayLen; i++)
349 CFFIndex<COUNT>::set_offset_at (i, offset);
350 offset += dataSizeArray[i];
352 CFFIndex<COUNT>::set_offset_at (i, offset);
355 for (unsigned int i = 0; i < dataArrayLen; i++)
357 TYPE *dest = c->start_embed<TYPE> ();
358 if (unlikely (!dest || !dest->serialize (c, dataArray[i], param1, param2)))
359 return_trace (false);
365 /* Top Dict, Font Dict, Private Dict */
366 struct Dict : UnsizedByteStr
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,
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);
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)
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)))
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))
396 op = Unmake_OpCode_ESC (op);
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); }
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); }
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)
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);
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); }
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); }
427 struct TopDict : Dict {};
428 struct FontDict : Dict {};
429 struct PrivateDict : Dict {};
433 void init () { offset = size = 0; link = 0; }
440 template <typename COUNT>
441 struct FDArray : CFFIndexOf<COUNT, FontDict>
443 template <typename DICTVAL, typename INFO, typename Iterator, typename OP_SERIALIZER>
444 bool serialize (hb_serialize_context_t *c,
446 OP_SERIALIZER& opszr)
448 TRACE_SERIALIZE (this);
450 /* serialize INDEX data */
451 hb_vector_t<unsigned> sizes;
454 | hb_map ([&] (const hb_pair_t<const DICTVAL&, const INFO&> &_)
456 FontDict *dict = c->start_embed<FontDict> ();
457 dict->serialize (c, _.first, opszr, _.second);
458 return c->head - (const char*)dict;
464 /* serialize INDEX header */
465 return_trace (CFFIndex<COUNT>::serialize_header (c, hb_iter (sizes)));
471 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
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);
483 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
484 { return (hb_codepoint_t) fds[glyph]; }
486 unsigned int get_size (unsigned int num_glyphs) const
487 { return HBUINT8::static_size * num_glyphs; }
489 HBUINT8 fds[HB_VAR_ARRAY];
494 template <typename GID_TYPE, typename FD_TYPE>
495 struct FDSelect3_4_Range
497 bool sanitize (hb_sanitize_context_t *c, const void * /*nullptr*/, unsigned int fdcount) const
499 TRACE_SANITIZE (this);
500 return_trace (first < c->get_num_glyphs () && (fd < fdcount));
506 DEFINE_SIZE_STATIC (GID_TYPE::static_size + FD_TYPE::static_size);
509 template <typename GID_TYPE, typename FD_TYPE>
512 unsigned int get_size () const
513 { return GID_TYPE::static_size * 2 + ranges.get_size (); }
515 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
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);
522 for (unsigned int i = 1; i < nRanges (); i++)
523 if (unlikely (ranges[i - 1].first >= ranges[i].first))
524 return_trace (false);
526 if (unlikely (!sentinel().sanitize (c) || (sentinel() != c->get_num_glyphs ())))
527 return_trace (false);
532 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
535 for (i = 1; i < nRanges (); i++)
536 if (glyph < ranges[i].first)
539 return (hb_codepoint_t) ranges[i - 1].fd;
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]); }
547 ArrayOf<FDSelect3_4_Range<GID_TYPE, FD_TYPE>, GID_TYPE> ranges;
548 /* GID_TYPE sentinel */
550 DEFINE_SIZE_ARRAY (GID_TYPE::static_size, ranges);
553 typedef FDSelect3_4<HBUINT16, HBUINT8> FDSelect3;
554 typedef FDSelect3_4_Range<HBUINT16, HBUINT8> FDSelect3_Range;
558 bool serialize (hb_serialize_context_t *c, const FDSelect &src, unsigned int num_glyphs)
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);
568 unsigned int get_size (unsigned int num_glyphs) const
572 case 0: return format.static_size + u.format0.get_size (num_glyphs);
573 case 3: return format.static_size + u.format3.get_size ();
578 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
580 if (this == &Null (FDSelect)) return 0;
584 case 0: return u.format0.get_fd (glyph);
585 case 3: return u.format3.get_fd (glyph);
590 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
592 TRACE_SANITIZE (this);
593 if (unlikely (!c->check_struct (this)))
594 return_trace (false);
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);
613 template <typename COUNT>
614 struct Subrs : CFFIndex<COUNT>
616 typedef COUNT count_type;
617 typedef CFFIndex<COUNT> SUPER;
620 } /* namespace CFF */
622 #endif /* HB_OT_CFF_COMMON_HH */