diff options
Diffstat (limited to 'Build/source/libs/harfbuzz/harfbuzz-src/src/hb-ot-cff-common.hh')
-rw-r--r-- | Build/source/libs/harfbuzz/harfbuzz-src/src/hb-ot-cff-common.hh | 293 |
1 files changed, 131 insertions, 162 deletions
diff --git a/Build/source/libs/harfbuzz/harfbuzz-src/src/hb-ot-cff-common.hh b/Build/source/libs/harfbuzz/harfbuzz-src/src/hb-ot-cff-common.hh index 6735c74be49..e5286cd792d 100644 --- a/Build/source/libs/harfbuzz/harfbuzz-src/src/hb-ot-cff-common.hh +++ b/Build/source/libs/harfbuzz/harfbuzz-src/src/hb-ot-cff-common.hh @@ -38,6 +38,9 @@ using namespace OT; #define CFF_UNDEF_CODE 0xFFFFFFFF +using objidx_t = hb_serialize_context_t::objidx_t; +using whence_t = hb_serialize_context_t::whence_t; + /* utility macro */ template<typename Type> static inline const Type& StructAtOffsetOrNull (const void *P, unsigned int offset) @@ -89,11 +92,14 @@ struct CFFIndex unsigned int offset_array_size () const { return calculate_offset_array_size (offSize, count); } - static unsigned int calculate_serialized_size (unsigned int offSize_, unsigned int count, - unsigned int dataSize) + CFFIndex *copy (hb_serialize_context_t *c) const { - if (count == 0) return COUNT::static_size; - return min_size + calculate_offset_array_size (offSize_, count) + dataSize; + TRACE_SERIALIZE (this); + unsigned int size = get_size (); + CFFIndex *out = c->allocate_size<CFFIndex> (size); + if (likely (out)) + memcpy (out, this, size); + return_trace (out); } bool serialize (hb_serialize_context_t *c, const CFFIndex &src) @@ -101,7 +107,7 @@ struct CFFIndex TRACE_SERIALIZE (this); unsigned int size = src.get_size (); CFFIndex *dest = c->allocate_size<CFFIndex> (size); - if (unlikely (dest == nullptr)) return_trace (false); + if (unlikely (!dest)) return_trace (false); memcpy (dest, &src, size); return_trace (true); } @@ -114,7 +120,7 @@ struct CFFIndex if (byteArray.length == 0) { COUNT *dest = c->allocate_min<COUNT> (); - if (unlikely (dest == nullptr)) return_trace (false); + if (unlikely (!dest)) return_trace (false); *dest = 0; } else @@ -139,10 +145,9 @@ struct CFFIndex /* serialize data */ for (unsigned int i = 0; i < byteArray.length; i++) { - const byte_str_t &bs = byteArray[i]; - unsigned char *dest = c->allocate_size<unsigned char> (bs.length); - if (unlikely (dest == nullptr)) - return_trace (false); + const byte_str_t &bs = byteArray[i]; + unsigned char *dest = c->allocate_size<unsigned char> (bs.length); + if (unlikely (!dest)) return_trace (false); memcpy (dest, &bs[0], bs.length); } } @@ -163,6 +168,71 @@ struct CFFIndex return result; } + template <typename Iterator, + hb_requires (hb_is_iterator (Iterator))> + bool serialize (hb_serialize_context_t *c, + Iterator it) + { + TRACE_SERIALIZE (this); + if (it.len () == 0) + { + COUNT *dest = c->allocate_min<COUNT> (); + if (unlikely (!dest)) return_trace (false); + *dest = 0; + } + else + { + serialize_header(c, + it | hb_map ([] (const byte_str_t &_) { return _.length; })); + for (const byte_str_t &_ : +it) + _.copy (c); + } + return_trace (true); + } + + bool serialize (hb_serialize_context_t *c, + const byte_str_array_t &byteArray) + { return serialize (c, + hb_iter (byteArray)); } + + bool serialize (hb_serialize_context_t *c, + const str_buff_vec_t &buffArray) + { + auto it = + + hb_iter (buffArray) + | hb_map ([] (const str_buff_t &_) { return byte_str_t (_.arrayZ, _.length); }) + ; + return serialize (c, it); + } + + template <typename Iterator, + hb_requires (hb_is_iterator (Iterator))> + bool serialize_header (hb_serialize_context_t *c, + Iterator it) + { + TRACE_SERIALIZE (this); + + unsigned total = + it | hb_reduce (hb_add, 0); + unsigned off_size = calcOffSize (total); + + /* serialize CFFIndex header */ + if (unlikely (!c->extend_min (*this))) return_trace (false); + this->count = it.len (); + this->offSize = off_size; + if (unlikely (!c->allocate_size<HBUINT8> (off_size * (it.len () + 1)))) + return_trace (false); + + /* serialize indices */ + unsigned int offset = 1; + unsigned int i = 0; + for (unsigned _ : +it) + { + CFFIndex<COUNT>::set_offset_at (i++, offset); + offset += _; + } + CFFIndex<COUNT>::set_offset_at (i, offset); + + return_trace (true); + } + void set_offset_at (unsigned int index, unsigned int offset) { HBUINT8 *p = offsets + offSize * index + offSize; @@ -189,7 +259,7 @@ struct CFFIndex unsigned int length_at (unsigned int index) const { if (unlikely ((offset_at (index + 1) < offset_at (index)) || - (offset_at (index + 1) > offset_at (count)))) + (offset_at (index + 1) > offset_at (count)))) return 0; return offset_at (index + 1) - offset_at (index); } @@ -237,7 +307,8 @@ struct CFFIndex public: COUNT count; /* Number of object data. Note there are (count+1) offsets */ HBUINT8 offSize; /* The byte size of each offset in the offsets array. */ - HBUINT8 offsets[HB_VAR_ARRAY]; /* The array of (count + 1) offsets into objects array (1-base). */ + HBUINT8 offsets[HB_VAR_ARRAY]; + /* The array of (count + 1) offsets into objects array (1-base). */ /* HBUINT8 data[HB_VAR_ARRAY]; Object data */ public: DEFINE_SIZE_ARRAY (COUNT::static_size + HBUINT8::static_size, offsets); @@ -250,7 +321,7 @@ struct CFFIndexOf : CFFIndex<COUNT> { if (likely (index < CFFIndex<COUNT>::count)) return byte_str_t (CFFIndex<COUNT>::data_base () + CFFIndex<COUNT>::offset_at (index) - 1, CFFIndex<COUNT>::length_at (index)); - return Null(byte_str_t); + return Null (byte_str_t); } template <typename DATA, typename PARAM1, typename PARAM2> @@ -284,85 +355,41 @@ struct CFFIndexOf : CFFIndex<COUNT> for (unsigned int i = 0; i < dataArrayLen; i++) { TYPE *dest = c->start_embed<TYPE> (); - if (unlikely (dest == nullptr || - !dest->serialize (c, dataArray[i], param1, param2))) + if (unlikely (!dest || !dest->serialize (c, dataArray[i], param1, param2))) return_trace (false); } return_trace (true); } - - /* in parallel to above */ - template <typename DATA, typename PARAM> - static unsigned int calculate_serialized_size (unsigned int &offSize_ /* OUT */, - const DATA *dataArray, - unsigned int dataArrayLen, - hb_vector_t<unsigned int> &dataSizeArray, /* OUT */ - const PARAM ¶m) - { - /* determine offset size */ - unsigned int totalDataSize = 0; - for (unsigned int i = 0; i < dataArrayLen; i++) - { - unsigned int dataSize = TYPE::calculate_serialized_size (dataArray[i], param); - dataSizeArray[i] = dataSize; - totalDataSize += dataSize; - } - offSize_ = calcOffSize (totalDataSize); - - return CFFIndex<COUNT>::calculate_serialized_size (offSize_, dataArrayLen, totalDataSize); - } }; /* Top Dict, Font Dict, Private Dict */ struct Dict : UnsizedByteStr { - template <typename DICTVAL, typename OP_SERIALIZER, typename PARAM> + template <typename DICTVAL, typename OP_SERIALIZER, typename ...Ts> bool serialize (hb_serialize_context_t *c, const DICTVAL &dictval, OP_SERIALIZER& opszr, - PARAM& param) + Ts&&... ds) { TRACE_SERIALIZE (this); for (unsigned int i = 0; i < dictval.get_count (); i++) - if (unlikely (!opszr.serialize (c, dictval[i], param))) + if (unlikely (!opszr.serialize (c, dictval[i], hb_forward<Ts> (ds)...))) return_trace (false); return_trace (true); } - /* in parallel to above */ - template <typename DICTVAL, typename OP_SERIALIZER, typename PARAM> - static unsigned int calculate_serialized_size (const DICTVAL &dictval, - OP_SERIALIZER& opszr, - PARAM& param) - { - unsigned int size = 0; - for (unsigned int i = 0; i < dictval.get_count (); i++) - size += opszr.calculate_serialized_size (dictval[i], param); - return size; - } - - template <typename DICTVAL, typename OP_SERIALIZER> - static unsigned int calculate_serialized_size (const DICTVAL &dictval, - OP_SERIALIZER& opszr) - { - unsigned int size = 0; - for (unsigned int i = 0; i < dictval.get_count (); i++) - size += opszr.calculate_serialized_size (dictval[i]); - return size; - } - - template <typename INTTYPE, int minVal, int maxVal> - static bool serialize_int_op (hb_serialize_context_t *c, op_code_t op, int value, op_code_t intOp) + template <typename T, typename V> + static bool serialize_int_op (hb_serialize_context_t *c, op_code_t op, V value, op_code_t intOp) { // XXX: not sure why but LLVM fails to compile the following 'unlikely' macro invocation - if (/*unlikely*/ (!serialize_int<INTTYPE, minVal, maxVal> (c, intOp, value))) + if (/*unlikely*/ (!serialize_int<T, V> (c, intOp, value))) return false; TRACE_SERIALIZE (this); /* serialize the opcode */ HBUINT8 *p = c->allocate_size<HBUINT8> (OpCode_Size (op)); - if (unlikely (p == nullptr)) return_trace (false); + if (unlikely (!p)) return_trace (false); if (Is_OpCode_ESC (op)) { *p = OpCode_escape; @@ -373,17 +400,28 @@ struct Dict : UnsizedByteStr return_trace (true); } - static bool serialize_uint4_op (hb_serialize_context_t *c, op_code_t op, int value) - { return serialize_int_op<HBUINT32, 0, 0x7FFFFFFF> (c, op, value, OpCode_longintdict); } + template <typename V> + static bool serialize_int4_op (hb_serialize_context_t *c, op_code_t op, V value) + { return serialize_int_op<HBINT32> (c, op, value, OpCode_longintdict); } + + template <typename V> + static bool serialize_int2_op (hb_serialize_context_t *c, op_code_t op, V value) + { return serialize_int_op<HBINT16> (c, op, value, OpCode_shortint); } - static bool serialize_uint2_op (hb_serialize_context_t *c, op_code_t op, int value) - { return serialize_int_op<HBUINT16, 0, 0x7FFF> (c, op, value, OpCode_shortint); } + template <typename T, int int_op> + static bool serialize_link_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence) + { + T &ofs = *(T *) (c->head + OpCode_Size (int_op)); + if (unlikely (!serialize_int_op<T> (c, op, 0, int_op))) return false; + c->add_link (ofs, link, whence); + return true; + } - static bool serialize_offset4_op (hb_serialize_context_t *c, op_code_t op, int value) - { return serialize_uint4_op (c, op, value); } + static bool serialize_link4_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head) + { return serialize_link_op<HBINT32, OpCode_longintdict> (c, op, link, whence); } - static bool serialize_offset2_op (hb_serialize_context_t *c, op_code_t op, int value) - { return serialize_uint2_op (c, op, value); } + static bool serialize_link2_op (hb_serialize_context_t *c, op_code_t op, objidx_t link, whence_t whence = whence_t::Head) + { return serialize_link_op<HBINT16, OpCode_shortint> (c, op, link, whence); } }; struct TopDict : Dict {}; @@ -392,105 +430,39 @@ struct PrivateDict : Dict {}; struct table_info_t { - void init () { offSize = offset = size = 0; } + void init () { offset = size = 0; link = 0; } unsigned int offset; unsigned int size; - unsigned int offSize; + objidx_t link; }; template <typename COUNT> struct FDArray : CFFIndexOf<COUNT, FontDict> { - /* used by CFF1 */ - template <typename DICTVAL, typename OP_SERIALIZER> + template <typename DICTVAL, typename INFO, typename Iterator, typename OP_SERIALIZER> bool serialize (hb_serialize_context_t *c, - unsigned int offSize_, - const hb_vector_t<DICTVAL> &fontDicts, + Iterator it, OP_SERIALIZER& opszr) { TRACE_SERIALIZE (this); - if (unlikely (!c->extend_min (*this))) return_trace (false); - this->count = fontDicts.length; - this->offSize = offSize_; - if (unlikely (!c->allocate_size<HBUINT8> (offSize_ * (fontDicts.length + 1)))) - return_trace (false); - /* serialize font dict offsets */ - unsigned int offset = 1; - unsigned int fid = 0; - for (; fid < fontDicts.length; fid++) - { - CFFIndexOf<COUNT, FontDict>::set_offset_at (fid, offset); - offset += FontDict::calculate_serialized_size (fontDicts[fid], opszr); - } - CFFIndexOf<COUNT, FontDict>::set_offset_at (fid, offset); - - /* serialize font dicts */ - for (unsigned int i = 0; i < fontDicts.length; i++) + /* serialize INDEX data */ + hb_vector_t<unsigned> sizes; + c->push (); + + it + | hb_map ([&] (const hb_pair_t<const DICTVAL&, const INFO&> &_) { FontDict *dict = c->start_embed<FontDict> (); - if (unlikely (!dict->serialize (c, fontDicts[i], opszr, fontDicts[i]))) - return_trace (false); - } - return_trace (true); - } - - /* used by CFF2 */ - template <typename DICTVAL, typename OP_SERIALIZER> - bool serialize (hb_serialize_context_t *c, - unsigned int offSize_, - const hb_vector_t<DICTVAL> &fontDicts, - unsigned int fdCount, - const hb_inc_bimap_t &fdmap, - OP_SERIALIZER& opszr, - const hb_vector_t<table_info_t> &privateInfos) - { - TRACE_SERIALIZE (this); - if (unlikely (!c->extend_min (*this))) return_trace (false); - this->count = fdCount; - this->offSize = offSize_; - if (unlikely (!c->allocate_size<HBUINT8> (offSize_ * (fdCount + 1)))) - return_trace (false); - - /* serialize font dict offsets */ - unsigned int offset = 1; - unsigned int fid = 0; - for (unsigned i = 0; i < fontDicts.length; i++) - if (fdmap.has (i)) - { - if (unlikely (fid >= fdCount)) return_trace (false); - CFFIndexOf<COUNT, FontDict>::set_offset_at (fid++, offset); - offset += FontDict::calculate_serialized_size (fontDicts[i], opszr); - } - CFFIndexOf<COUNT, FontDict>::set_offset_at (fid, offset); - - /* serialize font dicts */ - for (unsigned int i = 0; i < fontDicts.length; i++) - if (fdmap.has (i)) - { - FontDict *dict = c->start_embed<FontDict> (); - if (unlikely (!dict->serialize (c, fontDicts[i], opszr, privateInfos[fdmap[i]]))) - return_trace (false); - } - return_trace (true); - } - - /* in parallel to above */ - template <typename OP_SERIALIZER, typename DICTVAL> - static unsigned int calculate_serialized_size (unsigned int &offSize_ /* OUT */, - const hb_vector_t<DICTVAL> &fontDicts, - unsigned int fdCount, - const hb_inc_bimap_t &fdmap, - OP_SERIALIZER& opszr) - { - unsigned int dictsSize = 0; - for (unsigned int i = 0; i < fontDicts.len; i++) - if (fdmap.has (i)) - dictsSize += FontDict::calculate_serialized_size (fontDicts[i], opszr); - - offSize_ = calcOffSize (dictsSize); - return CFFIndex<COUNT>::calculate_serialized_size (offSize_, fdCount, dictsSize); + dict->serialize (c, _.first, opszr, _.second); + return c->head - (const char*)dict; + }) + | hb_sink (sizes) + ; + c->pop_pack (false); + + /* serialize INDEX header */ + return_trace (CFFIndex<COUNT>::serialize_header (c, hb_iter (sizes))); } }; @@ -544,7 +516,7 @@ struct FDSelect3_4 { TRACE_SANITIZE (this); if (unlikely (!c->check_struct (this) || !ranges.sanitize (c, nullptr, fdcount) || - (nRanges () == 0) || ranges[0].first != 0)) + (nRanges () == 0) || ranges[0].first != 0)) return_trace (false); for (unsigned int i = 1; i < nRanges (); i++) @@ -588,14 +560,11 @@ struct FDSelect TRACE_SERIALIZE (this); unsigned int size = src.get_size (num_glyphs); FDSelect *dest = c->allocate_size<FDSelect> (size); - if (unlikely (dest == nullptr)) return_trace (false); + if (unlikely (!dest)) return_trace (false); memcpy (dest, &src, size); return_trace (true); } - unsigned int calculate_serialized_size (unsigned int num_glyphs) const - { return get_size (num_glyphs); } - unsigned int get_size (unsigned int num_glyphs) const { switch (format) |