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-ot-layout-common.hh"
31 #include "hb-cff-interp-dict-common.hh"
32 #include "hb-subset-plan.hh"
38 #define CFF_UNDEF_CODE 0xFFFFFFFF
41 template<typename Type>
42 static inline const Type& StructAtOffsetOrNull(const void *P, unsigned int offset)
43 { return offset? (* reinterpret_cast<const Type*> ((const char *) P + offset)): Null(Type); }
45 inline unsigned int calcOffSize(unsigned int dataSize)
47 unsigned int size = 1;
48 unsigned int offset = dataSize + 1;
49 while ((offset & ~0xFF) != 0)
54 /* format does not support size > 4; caller should handle it as an error */
64 typedef hb_vector_t<unsigned char> str_buff_t;
65 struct str_buff_vec_t : hb_vector_t<str_buff_t>
67 void fini () { SUPER::fini_deep (); }
69 unsigned int total_size () const
71 unsigned int size = 0;
72 for (unsigned int i = 0; i < length; i++)
73 size += (*this)[i].length;
78 typedef hb_vector_t<str_buff_t> SUPER;
82 template <typename COUNT>
85 bool sanitize (hb_sanitize_context_t *c) const
87 TRACE_SANITIZE (this);
88 return_trace (likely ((count.sanitize (c) && count == 0) || /* empty INDEX */
89 (c->check_struct (this) && offSize >= 1 && offSize <= 4 &&
90 c->check_array (offsets, offSize, count + 1) &&
91 c->check_array ((const HBUINT8*)data_base (), 1, max_offset () - 1))));
94 static unsigned int calculate_offset_array_size (unsigned int offSize, unsigned int count)
95 { return offSize * (count + 1); }
97 unsigned int offset_array_size () const
98 { return calculate_offset_array_size (offSize, count); }
100 static unsigned int calculate_serialized_size (unsigned int offSize, unsigned int count, unsigned int dataSize)
103 return COUNT::static_size;
105 return min_size + calculate_offset_array_size (offSize, count) + dataSize;
108 bool serialize (hb_serialize_context_t *c, const CFFIndex &src)
110 TRACE_SERIALIZE (this);
111 unsigned int size = src.get_size ();
112 CFFIndex *dest = c->allocate_size<CFFIndex> (size);
113 if (unlikely (dest == nullptr)) return_trace (false);
114 memcpy (dest, &src, size);
118 bool serialize (hb_serialize_context_t *c,
119 unsigned int offSize_,
120 const byte_str_array_t &byteArray)
122 TRACE_SERIALIZE (this);
123 if (byteArray.length == 0)
125 COUNT *dest = c->allocate_min<COUNT> ();
126 if (unlikely (dest == nullptr)) return_trace (false);
131 /* serialize CFFIndex header */
132 if (unlikely (!c->extend_min (*this))) return_trace (false);
133 this->count.set (byteArray.length);
134 this->offSize.set (offSize_);
135 if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (byteArray.length + 1))))
136 return_trace (false);
138 /* serialize indices */
139 unsigned int offset = 1;
141 for (; i < byteArray.length; i++)
143 set_offset_at (i, offset);
144 offset += byteArray[i].get_size ();
146 set_offset_at (i, offset);
149 for (unsigned int i = 0; i < byteArray.length; i++)
151 const byte_str_t &bs = byteArray[i];
152 unsigned char *dest = c->allocate_size<unsigned char> (bs.length);
153 if (unlikely (dest == nullptr))
154 return_trace (false);
155 memcpy (dest, &bs[0], bs.length);
161 bool serialize (hb_serialize_context_t *c,
162 unsigned int offSize_,
163 const str_buff_vec_t &buffArray)
165 byte_str_array_t byteArray;
167 byteArray.resize (buffArray.length);
168 for (unsigned int i = 0; i < byteArray.length; i++)
170 byteArray[i] = byte_str_t (buffArray[i].arrayZ (), buffArray[i].length);
172 bool result = this->serialize (c, offSize_, byteArray);
177 void set_offset_at (unsigned int index, unsigned int offset)
179 HBUINT8 *p = offsets + offSize * index + offSize;
180 unsigned int size = offSize;
184 p->set (offset & 0xFF);
189 unsigned int offset_at (unsigned int index) const
191 assert (index <= count);
192 const HBUINT8 *p = offsets + offSize * index;
193 unsigned int size = offSize;
194 unsigned int offset = 0;
196 offset = (offset << 8) + *p++;
200 unsigned int length_at (unsigned int index) const
202 if (likely ((offset_at (index + 1) >= offset_at (index)) &&
203 (offset_at (index + 1) <= offset_at (count))))
204 return offset_at (index + 1) - offset_at (index);
209 const unsigned char *data_base () const
210 { return (const unsigned char *)this + min_size + offset_array_size (); }
212 unsigned int data_size () const { return HBINT8::static_size; }
214 byte_str_t operator [] (unsigned int index) const
216 if (likely (index < count))
217 return byte_str_t (data_base () + offset_at (index) - 1, length_at (index));
219 return Null(byte_str_t);
222 unsigned int get_size () const
224 if (this != &Null(CFFIndex))
227 return min_size + offset_array_size () + (offset_at (count) - 1);
229 return count.static_size; /* empty CFFIndex contains count only */
236 unsigned int max_offset () const
238 unsigned int max = 0;
239 for (unsigned int i = 0; i < count + 1u; i++)
241 unsigned int off = offset_at (i);
242 if (off > max) max = off;
248 COUNT count; /* Number of object data. Note there are (count+1) offsets */
249 HBUINT8 offSize; /* The byte size of each offset in the offsets array. */
250 HBUINT8 offsets[VAR]; /* The array of (count + 1) offsets into objects array (1-base). */
251 /* HBUINT8 data[VAR]; Object data */
253 DEFINE_SIZE_ARRAY (COUNT::static_size + HBUINT8::static_size, offsets);
256 template <typename COUNT, typename TYPE>
257 struct CFFIndexOf : CFFIndex<COUNT>
259 const byte_str_t operator [] (unsigned int index) const
261 if (likely (index < CFFIndex<COUNT>::count))
262 return byte_str_t (CFFIndex<COUNT>::data_base () + CFFIndex<COUNT>::offset_at (index) - 1, CFFIndex<COUNT>::length_at (index));
263 return Null(byte_str_t);
266 template <typename DATA, typename PARAM1, typename PARAM2>
267 bool serialize (hb_serialize_context_t *c,
268 unsigned int offSize_,
269 const DATA *dataArray,
270 unsigned int dataArrayLen,
271 const hb_vector_t<unsigned int> &dataSizeArray,
272 const PARAM1 ¶m1,
273 const PARAM2 ¶m2)
275 TRACE_SERIALIZE (this);
276 /* serialize CFFIndex header */
277 if (unlikely (!c->extend_min (*this))) return_trace (false);
278 this->count.set (dataArrayLen);
279 this->offSize.set (offSize_);
280 if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (dataArrayLen + 1))))
281 return_trace (false);
283 /* serialize indices */
284 unsigned int offset = 1;
286 for (; i < dataArrayLen; i++)
288 CFFIndex<COUNT>::set_offset_at (i, offset);
289 offset += dataSizeArray[i];
291 CFFIndex<COUNT>::set_offset_at (i, offset);
294 for (unsigned int i = 0; i < dataArrayLen; i++)
296 TYPE *dest = c->start_embed<TYPE> ();
297 if (unlikely (dest == nullptr ||
298 !dest->serialize (c, dataArray[i], param1, param2)))
299 return_trace (false);
304 /* in parallel to above */
305 template <typename DATA, typename PARAM>
306 static unsigned int calculate_serialized_size (unsigned int &offSize_ /* OUT */,
307 const DATA *dataArray,
308 unsigned int dataArrayLen,
309 hb_vector_t<unsigned int> &dataSizeArray, /* OUT */
312 /* determine offset size */
313 unsigned int totalDataSize = 0;
314 for (unsigned int i = 0; i < dataArrayLen; i++)
316 unsigned int dataSize = TYPE::calculate_serialized_size (dataArray[i], param);
317 dataSizeArray[i] = dataSize;
318 totalDataSize += dataSize;
320 offSize_ = calcOffSize (totalDataSize);
322 return CFFIndex<COUNT>::calculate_serialized_size (offSize_, dataArrayLen, totalDataSize);
326 /* Top Dict, Font Dict, Private Dict */
327 struct Dict : UnsizedByteStr
329 template <typename DICTVAL, typename OP_SERIALIZER, typename PARAM>
330 bool serialize (hb_serialize_context_t *c,
331 const DICTVAL &dictval,
332 OP_SERIALIZER& opszr,
335 TRACE_SERIALIZE (this);
336 for (unsigned int i = 0; i < dictval.get_count (); i++)
338 if (unlikely (!opszr.serialize (c, dictval[i], param)))
339 return_trace (false);
344 /* in parallel to above */
345 template <typename DICTVAL, typename OP_SERIALIZER, typename PARAM>
346 static unsigned int calculate_serialized_size (const DICTVAL &dictval,
347 OP_SERIALIZER& opszr,
350 unsigned int size = 0;
351 for (unsigned int i = 0; i < dictval.get_count (); i++)
352 size += opszr.calculate_serialized_size (dictval[i], param);
356 template <typename DICTVAL, typename OP_SERIALIZER>
357 static unsigned int calculate_serialized_size (const DICTVAL &dictval,
358 OP_SERIALIZER& opszr)
360 unsigned int size = 0;
361 for (unsigned int i = 0; i < dictval.get_count (); i++)
362 size += opszr.calculate_serialized_size (dictval[i]);
366 template <typename INTTYPE, int minVal, int maxVal>
367 static bool serialize_int_op (hb_serialize_context_t *c, op_code_t op, int value, op_code_t intOp)
369 // XXX: not sure why but LLVM fails to compile the following 'unlikely' macro invocation
370 if (/*unlikely*/ (!serialize_int<INTTYPE, minVal, maxVal> (c, intOp, value)))
373 TRACE_SERIALIZE (this);
374 /* serialize the opcode */
375 HBUINT8 *p = c->allocate_size<HBUINT8> (OpCode_Size (op));
376 if (unlikely (p == nullptr)) return_trace (false);
377 if (Is_OpCode_ESC (op))
379 p->set (OpCode_escape);
380 op = Unmake_OpCode_ESC (op);
387 static bool serialize_uint4_op (hb_serialize_context_t *c, op_code_t op, int value)
388 { return serialize_int_op<HBUINT32, 0, 0x7FFFFFFF> (c, op, value, OpCode_longintdict); }
390 static bool serialize_uint2_op (hb_serialize_context_t *c, op_code_t op, int value)
391 { return serialize_int_op<HBUINT16, 0, 0x7FFF> (c, op, value, OpCode_shortint); }
393 static bool serialize_offset4_op (hb_serialize_context_t *c, op_code_t op, int value)
395 return serialize_uint4_op (c, op, value);
398 static bool serialize_offset2_op (hb_serialize_context_t *c, op_code_t op, int value)
400 return serialize_uint2_op (c, op, value);
404 struct TopDict : Dict {};
405 struct FontDict : Dict {};
406 struct PrivateDict : Dict {};
410 void init () { offSize = offset = size = 0; }
414 unsigned int offSize;
417 /* used to remap font index or SID from fullset to subset.
418 * set to CFF_UNDEF_CODE if excluded from subset */
419 struct remap_t : hb_vector_t<hb_codepoint_t>
421 void init () { SUPER::init (); }
423 void fini () { SUPER::fini (); }
425 bool reset (unsigned int size)
427 if (unlikely (!SUPER::resize (size)))
429 for (unsigned int i = 0; i < length; i++)
430 (*this)[i] = CFF_UNDEF_CODE;
435 bool identity (unsigned int size)
437 if (unlikely (!SUPER::resize (size)))
440 for (i = 0; i < length; i++)
446 bool excludes (hb_codepoint_t id) const
447 { return (id < length) && ((*this)[id] == CFF_UNDEF_CODE); }
449 bool includes (hb_codepoint_t id) const
450 { return !excludes (id); }
452 unsigned int add (unsigned int i)
454 if ((*this)[i] == CFF_UNDEF_CODE)
455 (*this)[i] = count++;
459 hb_codepoint_t get_count () const { return count; }
462 hb_codepoint_t count;
465 typedef hb_vector_t<hb_codepoint_t> SUPER;
468 template <typename COUNT>
469 struct FDArray : CFFIndexOf<COUNT, FontDict>
472 template <typename DICTVAL, typename OP_SERIALIZER>
473 bool serialize (hb_serialize_context_t *c,
474 unsigned int offSize_,
475 const hb_vector_t<DICTVAL> &fontDicts,
476 OP_SERIALIZER& opszr)
478 TRACE_SERIALIZE (this);
479 if (unlikely (!c->extend_min (*this))) return_trace (false);
480 this->count.set (fontDicts.length);
481 this->offSize.set (offSize_);
482 if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (fontDicts.length + 1))))
483 return_trace (false);
485 /* serialize font dict offsets */
486 unsigned int offset = 1;
487 unsigned int fid = 0;
488 for (; fid < fontDicts.length; fid++)
490 CFFIndexOf<COUNT, FontDict>::set_offset_at (fid, offset);
491 offset += FontDict::calculate_serialized_size (fontDicts[fid], opszr);
493 CFFIndexOf<COUNT, FontDict>::set_offset_at (fid, offset);
495 /* serialize font dicts */
496 for (unsigned int i = 0; i < fontDicts.length; i++)
498 FontDict *dict = c->start_embed<FontDict> ();
499 if (unlikely (!dict->serialize (c, fontDicts[i], opszr, fontDicts[i])))
500 return_trace (false);
506 template <typename DICTVAL, typename OP_SERIALIZER>
507 bool serialize (hb_serialize_context_t *c,
508 unsigned int offSize_,
509 const hb_vector_t<DICTVAL> &fontDicts,
510 unsigned int fdCount,
511 const remap_t &fdmap,
512 OP_SERIALIZER& opszr,
513 const hb_vector_t<table_info_t> &privateInfos)
515 TRACE_SERIALIZE (this);
516 if (unlikely (!c->extend_min (*this))) return_trace (false);
517 this->count.set (fdCount);
518 this->offSize.set (offSize_);
519 if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (fdCount + 1))))
520 return_trace (false);
522 /* serialize font dict offsets */
523 unsigned int offset = 1;
524 unsigned int fid = 0;
525 for (unsigned i = 0; i < fontDicts.length; i++)
526 if (fdmap.includes (i))
528 if (unlikely (fid >= fdCount)) return_trace (false);
529 CFFIndexOf<COUNT, FontDict>::set_offset_at (fid++, offset);
530 offset += FontDict::calculate_serialized_size (fontDicts[i], opszr);
532 CFFIndexOf<COUNT, FontDict>::set_offset_at (fid, offset);
534 /* serialize font dicts */
535 for (unsigned int i = 0; i < fontDicts.length; i++)
536 if (fdmap.includes (i))
538 FontDict *dict = c->start_embed<FontDict> ();
539 if (unlikely (!dict->serialize (c, fontDicts[i], opszr, privateInfos[fdmap[i]])))
540 return_trace (false);
545 /* in parallel to above */
546 template <typename OP_SERIALIZER, typename DICTVAL>
547 static unsigned int calculate_serialized_size (unsigned int &offSize_ /* OUT */,
548 const hb_vector_t<DICTVAL> &fontDicts,
549 unsigned int fdCount,
550 const remap_t &fdmap,
551 OP_SERIALIZER& opszr)
553 unsigned int dictsSize = 0;
554 for (unsigned int i = 0; i < fontDicts.len; i++)
555 if (fdmap.includes (i))
556 dictsSize += FontDict::calculate_serialized_size (fontDicts[i], opszr);
558 offSize_ = calcOffSize (dictsSize);
559 return CFFIndex<COUNT>::calculate_serialized_size (offSize_, fdCount, dictsSize);
565 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
567 TRACE_SANITIZE (this);
568 if (unlikely (!(c->check_struct (this))))
569 return_trace (false);
570 for (unsigned int i = 0; i < c->get_num_glyphs (); i++)
571 if (unlikely (!fds[i].sanitize (c)))
572 return_trace (false);
577 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
579 return (hb_codepoint_t)fds[glyph];
582 unsigned int get_size (unsigned int num_glyphs) const
583 { return HBUINT8::static_size * num_glyphs; }
590 template <typename GID_TYPE, typename FD_TYPE>
591 struct FDSelect3_4_Range {
592 bool sanitize (hb_sanitize_context_t *c, const void */*nullptr*/, unsigned int fdcount) const
594 TRACE_SANITIZE (this);
595 return_trace (first < c->get_num_glyphs () && (fd < fdcount));
601 DEFINE_SIZE_STATIC (GID_TYPE::static_size + FD_TYPE::static_size);
604 template <typename GID_TYPE, typename FD_TYPE>
606 unsigned int get_size () const
607 { return GID_TYPE::static_size * 2 + ranges.get_size (); }
609 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
611 TRACE_SANITIZE (this);
612 if (unlikely (!c->check_struct (this) || !ranges.sanitize (c, nullptr, fdcount) ||
613 (nRanges () == 0) || ranges[0].first != 0))
614 return_trace (false);
616 for (unsigned int i = 1; i < nRanges (); i++)
618 if (unlikely (ranges[i - 1].first >= ranges[i].first))
619 return_trace (false);
622 if (unlikely (!sentinel().sanitize (c) || (sentinel() != c->get_num_glyphs ())))
623 return_trace (false);
628 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
631 for (i = 1; i < nRanges (); i++)
632 if (glyph < ranges[i].first)
635 return (hb_codepoint_t)ranges[i - 1].fd;
638 GID_TYPE &nRanges () { return ranges.len; }
639 GID_TYPE nRanges () const { return ranges.len; }
640 GID_TYPE &sentinel () { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
641 const GID_TYPE &sentinel () const { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
643 ArrayOf<FDSelect3_4_Range<GID_TYPE, FD_TYPE>, GID_TYPE> ranges;
644 /* GID_TYPE sentinel */
646 DEFINE_SIZE_ARRAY (GID_TYPE::static_size, ranges);
649 typedef FDSelect3_4<HBUINT16, HBUINT8> FDSelect3;
650 typedef FDSelect3_4_Range<HBUINT16, HBUINT8> FDSelect3_Range;
653 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
655 TRACE_SANITIZE (this);
657 return_trace (likely (c->check_struct (this) && (format == 0 || format == 3) &&
659 u.format0.sanitize (c, fdcount):
660 u.format3.sanitize (c, fdcount)));
663 bool serialize (hb_serialize_context_t *c, const FDSelect &src, unsigned int num_glyphs)
665 TRACE_SERIALIZE (this);
666 unsigned int size = src.get_size (num_glyphs);
667 FDSelect *dest = c->allocate_size<FDSelect> (size);
668 if (unlikely (dest == nullptr)) return_trace (false);
669 memcpy (dest, &src, size);
673 unsigned int calculate_serialized_size (unsigned int num_glyphs) const
674 { return get_size (num_glyphs); }
676 unsigned int get_size (unsigned int num_glyphs) const
678 unsigned int size = format.static_size;
680 size += u.format0.get_size (num_glyphs);
682 size += u.format3.get_size ();
686 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
688 if (this == &Null(FDSelect))
691 return u.format0.get_fd (glyph);
693 return u.format3.get_fd (glyph);
705 template <typename COUNT>
706 struct Subrs : CFFIndex<COUNT>
708 typedef COUNT count_type;
709 typedef CFFIndex<COUNT> SUPER;
712 } /* namespace CFF */
714 #endif /* HB_OT_CFF_COMMON_HH */