2018-07-18 23:17:52 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Adobe Systems Incorporated.
|
|
|
|
*
|
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
* Adobe Author(s): Michiharu Ariza
|
|
|
|
*/
|
2018-08-29 22:36:39 +02:00
|
|
|
#ifndef HB_OT_CFF_COMMON_HH
|
|
|
|
#define HB_OT_CFF_COMMON_HH
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2018-08-29 22:26:17 +02:00
|
|
|
#include "hb-open-type.hh"
|
|
|
|
#include "hb-ot-layout-common.hh"
|
|
|
|
#include "hb-cff-interp-dict-common.hh"
|
2018-08-01 20:30:38 +02:00
|
|
|
#include "hb-subset-plan.hh"
|
2018-07-18 23:17:52 +02:00
|
|
|
|
|
|
|
namespace CFF {
|
|
|
|
|
|
|
|
using namespace OT;
|
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
#define CFF_UNDEF_CODE 0xFFFFFFFF
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
/* utility macro */
|
|
|
|
template<typename Type>
|
|
|
|
static inline const Type& StructAtOffsetOrNull(const void *P, unsigned int offset)
|
|
|
|
{ return offset? (* reinterpret_cast<const Type*> ((const char *) P + offset)): Null(Type); }
|
|
|
|
|
2018-09-12 01:20:39 +02:00
|
|
|
inline unsigned int calcOffSize(unsigned int dataSize)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
unsigned int size = 1;
|
2018-09-12 01:20:39 +02:00
|
|
|
unsigned int offset = dataSize + 1;
|
2018-08-01 20:30:38 +02:00
|
|
|
while ((offset & ~0xFF) != 0)
|
|
|
|
{
|
|
|
|
size++;
|
|
|
|
offset >>= 8;
|
|
|
|
}
|
|
|
|
assert (size <= 4);
|
|
|
|
return size;
|
|
|
|
}
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2018-09-13 01:08:54 +02:00
|
|
|
struct code_pair
|
|
|
|
{
|
|
|
|
hb_codepoint_t code;
|
|
|
|
hb_codepoint_t glyph;
|
|
|
|
};
|
|
|
|
|
2018-07-18 23:17:52 +02:00
|
|
|
/* CFF INDEX */
|
2018-08-10 20:07:07 +02:00
|
|
|
template <typename COUNT>
|
2018-08-16 17:03:46 +02:00
|
|
|
struct CFFIndex
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
|
|
|
inline bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-08-20 23:04:46 +02:00
|
|
|
return_trace (likely ((count.sanitize (c) && count == 0) || /* empty INDEX */
|
|
|
|
(c->check_struct (this) && offSize >= 1 && offSize <= 4 &&
|
|
|
|
c->check_array (offsets, offSize, count + 1) &&
|
|
|
|
c->check_array (data_base (), 1, max_offset () - 1))));
|
2018-07-18 23:17:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
inline static unsigned int calculate_offset_array_size (unsigned int offSize, unsigned int count)
|
2018-07-18 23:17:52 +02:00
|
|
|
{ return offSize * (count + 1); }
|
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
inline unsigned int offset_array_size (void) const
|
|
|
|
{ return calculate_offset_array_size (offSize, count); }
|
|
|
|
|
|
|
|
inline static unsigned int calculate_serialized_size (unsigned int offSize, unsigned int count, unsigned int dataSize)
|
|
|
|
{ return min_size + calculate_offset_array_size (offSize, count) + dataSize; }
|
|
|
|
|
2018-08-16 17:03:46 +02:00
|
|
|
inline bool serialize (hb_serialize_context_t *c, const CFFIndex &src)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-08-02 19:52:08 +02:00
|
|
|
TRACE_SERIALIZE (this);
|
2018-08-01 20:30:38 +02:00
|
|
|
unsigned int size = src.get_size ();
|
2018-08-16 17:03:46 +02:00
|
|
|
CFFIndex *dest = c->allocate_size<CFFIndex> (size);
|
2018-08-01 20:30:38 +02:00
|
|
|
if (unlikely (dest == nullptr)) return_trace (false);
|
|
|
|
memcpy (dest, &src, size);
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool serialize (hb_serialize_context_t *c,
|
2018-08-10 20:07:07 +02:00
|
|
|
unsigned int offSize_,
|
|
|
|
const hb_vector_t<ByteStr> &byteArray)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
2018-08-16 17:03:46 +02:00
|
|
|
/* serialize CFFIndex header */
|
2018-08-01 20:30:38 +02:00
|
|
|
if (unlikely (!c->extend_min (*this))) return_trace (false);
|
2018-08-10 20:07:07 +02:00
|
|
|
this->count.set (byteArray.len);
|
|
|
|
this->offSize.set (offSize_);
|
|
|
|
if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (byteArray.len + 1))))
|
2018-08-01 20:30:38 +02:00
|
|
|
return_trace (false);
|
|
|
|
|
|
|
|
/* serialize indices */
|
|
|
|
unsigned int offset = 1;
|
2018-08-02 01:32:27 +02:00
|
|
|
unsigned int i = 0;
|
2018-08-10 20:07:07 +02:00
|
|
|
for (; i < byteArray.len; i++)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
set_offset_at (i, offset);
|
2018-08-10 20:07:07 +02:00
|
|
|
offset += byteArray[i].get_size ();
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
2018-08-02 01:32:27 +02:00
|
|
|
set_offset_at (i, offset);
|
2018-08-01 20:30:38 +02:00
|
|
|
|
|
|
|
/* serialize data */
|
2018-08-10 20:07:07 +02:00
|
|
|
for (unsigned int i = 0; i < byteArray.len; i++)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-08-10 20:07:07 +02:00
|
|
|
ByteStr *dest = c->start_embed<ByteStr> ();
|
|
|
|
if (unlikely (dest == nullptr ||
|
|
|
|
!dest->serialize (c, byteArray[i])))
|
2018-08-01 20:30:38 +02:00
|
|
|
return_trace (false);
|
|
|
|
}
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void set_offset_at (unsigned int index, unsigned int offset)
|
|
|
|
{
|
|
|
|
HBUINT8 *p = offsets + offSize * index + offSize;
|
|
|
|
unsigned int size = offSize;
|
|
|
|
for (; size; size--)
|
|
|
|
{
|
|
|
|
--p;
|
|
|
|
p->set (offset & 0xFF);
|
|
|
|
offset >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-02 23:13:36 +02:00
|
|
|
inline unsigned int offset_at (unsigned int index) const
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2018-08-03 01:28:10 +02:00
|
|
|
assert (index <= count);
|
2018-07-18 23:17:52 +02:00
|
|
|
const HBUINT8 *p = offsets + offSize * index;
|
|
|
|
unsigned int size = offSize;
|
|
|
|
unsigned int offset = 0;
|
|
|
|
for (; size; size--)
|
|
|
|
offset = (offset << 8) + *p++;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2018-10-02 23:13:36 +02:00
|
|
|
inline unsigned int length_at (unsigned int index) const
|
2018-07-18 23:17:52 +02:00
|
|
|
{ return offset_at (index + 1) - offset_at (index); }
|
|
|
|
|
|
|
|
inline const char *data_base (void) const
|
2018-08-10 20:07:07 +02:00
|
|
|
{ return (const char *)this + min_size + offset_array_size (); }
|
2018-07-18 23:17:52 +02:00
|
|
|
|
|
|
|
inline unsigned int data_size (void) const
|
|
|
|
{ return HBINT8::static_size; };
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
ByteStr operator [] (unsigned int index) const
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
|
|
|
if (likely (index < count))
|
2018-08-01 20:30:38 +02:00
|
|
|
return ByteStr (data_base () + offset_at (index) - 1, offset_at (index + 1) - offset_at (index));
|
|
|
|
else
|
|
|
|
return Null(ByteStr);
|
2018-07-18 23:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned int get_size (void) const
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-08-16 17:03:46 +02:00
|
|
|
if (this != &Null(CFFIndex))
|
2018-08-03 01:28:10 +02:00
|
|
|
{
|
|
|
|
if (count > 0)
|
|
|
|
return min_size + offset_array_size () + (offset_at (count) - 1);
|
|
|
|
else
|
2018-08-16 17:03:46 +02:00
|
|
|
return count.static_size; /* empty CFFIndex contains count only */
|
2018-08-03 01:28:10 +02:00
|
|
|
}
|
2018-08-01 20:30:38 +02:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2018-07-30 23:28:40 +02:00
|
|
|
protected:
|
|
|
|
inline unsigned int max_offset (void) const
|
|
|
|
{
|
|
|
|
unsigned int max = 0;
|
|
|
|
for (unsigned int i = 0; i <= count; i++)
|
|
|
|
{
|
|
|
|
unsigned int off = offset_at (i);
|
|
|
|
if (off > max) max = off;
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2018-08-10 20:07:07 +02:00
|
|
|
COUNT count; /* Number of object data. Note there are (count+1) offsets */
|
2018-07-18 23:17:52 +02:00
|
|
|
HBUINT8 offSize; /* The byte size of each offset in the offsets array. */
|
|
|
|
HBUINT8 offsets[VAR]; /* The array of (count + 1) offsets into objects array (1-base). */
|
|
|
|
/* HBUINT8 data[VAR]; Object data */
|
|
|
|
public:
|
2018-08-10 20:07:07 +02:00
|
|
|
DEFINE_SIZE_ARRAY (COUNT::static_size + HBUINT8::static_size, offsets);
|
2018-07-18 23:17:52 +02:00
|
|
|
};
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
template <typename COUNT, typename TYPE>
|
2018-09-07 02:28:15 +02:00
|
|
|
struct CFFIndexOf : CFFIndex<COUNT>
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2018-08-10 20:07:07 +02:00
|
|
|
inline const ByteStr operator [] (unsigned int index) const
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2018-08-16 17:03:46 +02:00
|
|
|
if (likely (index < CFFIndex<COUNT>::count))
|
|
|
|
return ByteStr (CFFIndex<COUNT>::data_base () + CFFIndex<COUNT>::offset_at (index) - 1, CFFIndex<COUNT>::length_at (index));
|
2018-08-01 20:30:38 +02:00
|
|
|
return Null(ByteStr);
|
2018-07-18 23:17:52 +02:00
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
template <typename DATA, typename PARAM1, typename PARAM2>
|
|
|
|
inline bool serialize (hb_serialize_context_t *c,
|
|
|
|
unsigned int offSize_,
|
2018-09-07 02:28:15 +02:00
|
|
|
const DATA *dataArray,
|
|
|
|
unsigned int dataArrayLen,
|
2018-08-10 20:07:07 +02:00
|
|
|
const hb_vector_t<unsigned int> &dataSizeArray,
|
|
|
|
const PARAM1 ¶m1,
|
|
|
|
const PARAM2 ¶m2)
|
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
2018-08-16 17:03:46 +02:00
|
|
|
/* serialize CFFIndex header */
|
2018-08-10 20:07:07 +02:00
|
|
|
if (unlikely (!c->extend_min (*this))) return_trace (false);
|
2018-09-07 02:28:15 +02:00
|
|
|
this->count.set (dataArrayLen);
|
2018-08-10 20:07:07 +02:00
|
|
|
this->offSize.set (offSize_);
|
2018-09-07 02:28:15 +02:00
|
|
|
if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (dataArrayLen + 1))))
|
2018-08-10 20:07:07 +02:00
|
|
|
return_trace (false);
|
|
|
|
|
|
|
|
/* serialize indices */
|
|
|
|
unsigned int offset = 1;
|
|
|
|
unsigned int i = 0;
|
2018-09-07 02:28:15 +02:00
|
|
|
for (; i < dataArrayLen; i++)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2018-08-16 17:03:46 +02:00
|
|
|
CFFIndex<COUNT>::set_offset_at (i, offset);
|
2018-08-10 20:07:07 +02:00
|
|
|
offset += dataSizeArray[i];
|
|
|
|
}
|
2018-08-16 17:03:46 +02:00
|
|
|
CFFIndex<COUNT>::set_offset_at (i, offset);
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
/* serialize data */
|
2018-09-07 02:28:15 +02:00
|
|
|
for (unsigned int i = 0; i < dataArrayLen; i++)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
TYPE *dest = c->start_embed<TYPE> ();
|
|
|
|
if (unlikely (dest == nullptr ||
|
|
|
|
!dest->serialize (c, dataArray[i], param1, param2)))
|
|
|
|
return_trace (false);
|
|
|
|
}
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* in parallel to above */
|
|
|
|
template <typename DATA, typename PARAM>
|
|
|
|
inline static unsigned int calculate_serialized_size (unsigned int &offSize_ /* OUT */,
|
2018-09-07 02:28:15 +02:00
|
|
|
const DATA *dataArray,
|
|
|
|
unsigned int dataArrayLen,
|
2018-08-10 20:07:07 +02:00
|
|
|
hb_vector_t<unsigned int> &dataSizeArray, /* OUT */
|
|
|
|
const PARAM ¶m)
|
|
|
|
{
|
|
|
|
/* determine offset size */
|
|
|
|
unsigned int totalDataSize = 0;
|
2018-09-07 02:28:15 +02:00
|
|
|
for (unsigned int i = 0; i < dataArrayLen; i++)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
unsigned int dataSize = TYPE::calculate_serialized_size (dataArray[i], param);
|
|
|
|
dataSizeArray[i] = dataSize;
|
|
|
|
totalDataSize += dataSize;
|
|
|
|
}
|
|
|
|
offSize_ = calcOffSize (totalDataSize);
|
|
|
|
|
2018-09-07 02:28:15 +02:00
|
|
|
return CFFIndex<COUNT>::calculate_serialized_size (offSize_, dataArrayLen, totalDataSize);
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-18 23:17:52 +02:00
|
|
|
/* Top Dict, Font Dict, Private Dict */
|
2018-08-01 20:30:38 +02:00
|
|
|
struct Dict : UnsizedByteStr
|
|
|
|
{
|
2018-08-03 23:35:09 +02:00
|
|
|
template <typename DICTVAL, typename OP_SERIALIZER, typename PARAM>
|
2018-08-01 20:30:38 +02:00
|
|
|
inline bool serialize (hb_serialize_context_t *c,
|
2018-08-03 23:35:09 +02:00
|
|
|
const DICTVAL &dictval,
|
2018-08-01 20:30:38 +02:00
|
|
|
OP_SERIALIZER& opszr,
|
|
|
|
PARAM& param)
|
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
2018-09-07 02:28:15 +02:00
|
|
|
for (unsigned int i = 0; i < dictval.getNumValues (); i++)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-09-07 02:28:15 +02:00
|
|
|
if (unlikely (!opszr.serialize (c, dictval[i], param)))
|
2018-08-01 20:30:38 +02:00
|
|
|
return_trace (false);
|
|
|
|
}
|
|
|
|
return_trace (true);
|
|
|
|
}
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
/* in parallel to above */
|
2018-08-03 23:35:09 +02:00
|
|
|
template <typename DICTVAL, typename OP_SERIALIZER>
|
|
|
|
inline static unsigned int calculate_serialized_size (const DICTVAL &dictval,
|
2018-08-01 20:30:38 +02:00
|
|
|
OP_SERIALIZER& opszr)
|
|
|
|
{
|
|
|
|
unsigned int size = 0;
|
2018-09-07 02:28:15 +02:00
|
|
|
for (unsigned int i = 0; i < dictval.getNumValues (); i++)
|
|
|
|
size += opszr.calculate_serialized_size (dictval[i]);
|
2018-08-01 20:30:38 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename INTTYPE, int minVal, int maxVal>
|
2018-09-11 01:00:20 +02:00
|
|
|
inline static bool serialize_int_op (hb_serialize_context_t *c, OpCode op, int value, OpCode intOp)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
// XXX: not sure why but LLVM fails to compile the following 'unlikely' macro invocation
|
|
|
|
if (/*unlikely*/ (!serialize_int<INTTYPE, minVal, maxVal> (c, intOp, value)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
TRACE_SERIALIZE (this);
|
|
|
|
/* serialize the opcode */
|
2018-08-29 21:14:30 +02:00
|
|
|
HBUINT8 *p = c->allocate_size<HBUINT8> (OpCode_Size (op));
|
2018-08-01 20:30:38 +02:00
|
|
|
if (unlikely (p == nullptr)) return_trace (false);
|
2018-08-29 21:14:30 +02:00
|
|
|
if (Is_OpCode_ESC (op))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
p->set (OpCode_escape);
|
2018-08-29 21:14:30 +02:00
|
|
|
op = Unmake_OpCode_ESC (op);
|
2018-08-01 20:30:38 +02:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
p->set (op);
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
inline static bool serialize_uint4_op (hb_serialize_context_t *c, OpCode op, int value)
|
|
|
|
{ return serialize_int_op<HBUINT32, 0, 0x7FFFFFFF> (c, op, value, OpCode_longintdict); }
|
|
|
|
|
|
|
|
inline static bool serialize_uint2_op (hb_serialize_context_t *c, OpCode op, int value)
|
|
|
|
{ return serialize_int_op<HBUINT16, 0, 0x7FFF> (c, op, value, OpCode_shortint); }
|
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
inline static bool serialize_offset4_op (hb_serialize_context_t *c, OpCode op, int value)
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
|
|
|
if (value == 0) return true;
|
|
|
|
return serialize_uint4_op (c, op, value);
|
|
|
|
}
|
2018-08-01 20:30:38 +02:00
|
|
|
|
|
|
|
inline static bool serialize_offset2_op (hb_serialize_context_t *c, OpCode op, int value)
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
|
|
|
if (value == 0) return true;
|
|
|
|
return serialize_uint2_op (c, op, value);
|
|
|
|
}
|
2018-08-01 20:30:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TopDict : Dict {};
|
|
|
|
struct FontDict : Dict {};
|
|
|
|
struct PrivateDict : Dict {};
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
struct TableInfo
|
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
void init (void) { offSize = offset = size = 0; }
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
unsigned int offset;
|
|
|
|
unsigned int size;
|
2018-08-15 21:00:19 +02:00
|
|
|
unsigned int offSize;
|
2018-08-10 20:07:07 +02:00
|
|
|
};
|
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
/* used to remap font index or SID from fullset to subset.
|
|
|
|
* set to CFF_UNDEF_CODE if excluded from subset */
|
|
|
|
struct Remap : hb_vector_t<hb_codepoint_t>
|
2018-08-10 21:55:22 +02:00
|
|
|
{
|
|
|
|
inline void init (void)
|
|
|
|
{ hb_vector_t<hb_codepoint_t>::init (); }
|
|
|
|
|
|
|
|
inline void fini (void)
|
|
|
|
{ hb_vector_t<hb_codepoint_t>::fini (); }
|
|
|
|
|
2018-09-12 07:41:50 +02:00
|
|
|
inline bool reset (unsigned int size)
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
2018-09-12 07:41:50 +02:00
|
|
|
if (unlikely (!hb_vector_t<hb_codepoint_t>::resize (size)))
|
2018-09-11 01:00:20 +02:00
|
|
|
return false;
|
|
|
|
for (unsigned int i = 0; i < len; i++)
|
|
|
|
(*this)[i] = CFF_UNDEF_CODE;
|
|
|
|
count = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-10 21:55:22 +02:00
|
|
|
inline bool fullset (void) const
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < len; i++)
|
2018-09-11 01:00:20 +02:00
|
|
|
if (hb_vector_t<hb_codepoint_t>::operator[] (i) == CFF_UNDEF_CODE)
|
2018-08-10 21:55:22 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
inline bool excludes (hb_codepoint_t id) const
|
|
|
|
{ return (id < len) && ((*this)[id] == CFF_UNDEF_CODE); }
|
2018-08-20 23:04:46 +02:00
|
|
|
|
|
|
|
inline hb_codepoint_t operator[] (hb_codepoint_t i) const
|
|
|
|
{
|
|
|
|
if (fullset ())
|
|
|
|
return i;
|
|
|
|
else
|
|
|
|
return hb_vector_t<hb_codepoint_t>::operator[] (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline hb_codepoint_t &operator[] (hb_codepoint_t i)
|
|
|
|
{
|
|
|
|
assert (i < len);
|
|
|
|
return hb_vector_t<hb_codepoint_t>::operator[] (i);
|
|
|
|
}
|
2018-09-11 01:00:20 +02:00
|
|
|
|
|
|
|
inline unsigned int add (unsigned int i)
|
|
|
|
{
|
|
|
|
if ((*this)[i] == CFF_UNDEF_CODE)
|
|
|
|
(*this)[i] = count++;
|
|
|
|
return (*this)[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline hb_codepoint_t get_count (void) const
|
|
|
|
{ return count; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
hb_codepoint_t count;
|
2018-08-10 21:55:22 +02:00
|
|
|
};
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
template <typename COUNT>
|
2018-09-07 02:28:15 +02:00
|
|
|
struct FDArray : CFFIndexOf<COUNT, FontDict>
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-09-11 01:00:20 +02:00
|
|
|
/* used by CFF1 */
|
|
|
|
template <typename DICTVAL, typename OP_SERIALIZER>
|
|
|
|
inline bool serialize (hb_serialize_context_t *c,
|
|
|
|
unsigned int offSize_,
|
|
|
|
const hb_vector_t<DICTVAL> &fontDicts,
|
|
|
|
OP_SERIALIZER& opszr)
|
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
|
|
|
if (unlikely (!c->extend_min (*this))) return_trace (false);
|
|
|
|
this->count.set (fontDicts.len);
|
|
|
|
this->offSize.set (offSize_);
|
|
|
|
if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (fontDicts.len + 1))))
|
|
|
|
return_trace (false);
|
|
|
|
|
|
|
|
/* serialize font dict offsets */
|
|
|
|
unsigned int offset = 1;
|
|
|
|
unsigned int fid = 0;
|
|
|
|
for (; fid < fontDicts.len; 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.len; i++)
|
|
|
|
{
|
|
|
|
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 */
|
2018-08-01 20:30:38 +02:00
|
|
|
template <typename DICTVAL, typename OP_SERIALIZER>
|
|
|
|
inline bool serialize (hb_serialize_context_t *c,
|
2018-08-10 20:07:07 +02:00
|
|
|
unsigned int offSize_,
|
2018-08-01 20:30:38 +02:00
|
|
|
const hb_vector_t<DICTVAL> &fontDicts,
|
2018-08-06 19:04:53 +02:00
|
|
|
unsigned int fdCount,
|
2018-09-11 01:00:20 +02:00
|
|
|
const Remap &fdmap,
|
2018-08-01 20:30:38 +02:00
|
|
|
OP_SERIALIZER& opszr,
|
2018-08-10 20:07:07 +02:00
|
|
|
const hb_vector_t<TableInfo> &privateInfos)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
|
|
|
if (unlikely (!c->extend_min (*this))) return_trace (false);
|
2018-08-06 19:04:53 +02:00
|
|
|
this->count.set (fdCount);
|
2018-08-10 20:07:07 +02:00
|
|
|
this->offSize.set (offSize_);
|
|
|
|
if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (fdCount + 1))))
|
2018-08-01 20:30:38 +02:00
|
|
|
return_trace (false);
|
|
|
|
|
|
|
|
/* serialize font dict offsets */
|
|
|
|
unsigned int offset = 1;
|
2018-08-06 19:04:53 +02:00
|
|
|
unsigned int fid = 0;
|
|
|
|
for (unsigned i = 0; i < fontDicts.len; i++)
|
2018-08-10 21:55:22 +02:00
|
|
|
if (!fdmap.excludes (i))
|
2018-08-06 19:04:53 +02:00
|
|
|
{
|
2018-09-07 02:28:15 +02:00
|
|
|
CFFIndexOf<COUNT, FontDict>::set_offset_at (fid++, offset);
|
2018-08-06 19:04:53 +02:00
|
|
|
offset += FontDict::calculate_serialized_size (fontDicts[i], opszr);
|
|
|
|
}
|
2018-09-07 02:28:15 +02:00
|
|
|
CFFIndexOf<COUNT, FontDict>::set_offset_at (fid, offset);
|
2018-08-01 20:30:38 +02:00
|
|
|
|
|
|
|
/* serialize font dicts */
|
|
|
|
for (unsigned int i = 0; i < fontDicts.len; i++)
|
2018-08-10 21:55:22 +02:00
|
|
|
if (!fdmap.excludes (i))
|
2018-08-06 19:04:53 +02:00
|
|
|
{
|
|
|
|
FontDict *dict = c->start_embed<FontDict> ();
|
2018-08-10 21:55:22 +02:00
|
|
|
if (unlikely (!dict->serialize (c, fontDicts[i], opszr, privateInfos[fdmap[i]])))
|
2018-08-06 19:04:53 +02:00
|
|
|
return_trace (false);
|
|
|
|
}
|
2018-08-01 20:30:38 +02:00
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* in parallel to above */
|
|
|
|
template <typename OP_SERIALIZER, typename DICTVAL>
|
2018-08-10 20:07:07 +02:00
|
|
|
inline static unsigned int calculate_serialized_size (unsigned int &offSize_ /* OUT */,
|
2018-08-01 20:30:38 +02:00
|
|
|
const hb_vector_t<DICTVAL> &fontDicts,
|
2018-08-06 19:04:53 +02:00
|
|
|
unsigned int fdCount,
|
2018-09-11 01:00:20 +02:00
|
|
|
const Remap &fdmap,
|
2018-08-01 20:30:38 +02:00
|
|
|
OP_SERIALIZER& opszr)
|
|
|
|
{
|
|
|
|
unsigned int dictsSize = 0;
|
|
|
|
for (unsigned int i = 0; i < fontDicts.len; i++)
|
2018-08-10 21:55:22 +02:00
|
|
|
if (!fdmap.excludes (i))
|
2018-08-06 19:04:53 +02:00
|
|
|
dictsSize += FontDict::calculate_serialized_size (fontDicts[i], opszr);
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2018-09-12 01:20:39 +02:00
|
|
|
offSize_ = calcOffSize (dictsSize);
|
2018-08-16 17:03:46 +02:00
|
|
|
return CFFIndex<COUNT>::calculate_serialized_size (offSize_, fdCount, dictsSize);
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
};
|
2018-07-18 23:17:52 +02:00
|
|
|
|
|
|
|
/* FDSelect */
|
|
|
|
struct FDSelect0 {
|
2018-08-06 19:04:53 +02:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
|
2018-07-30 23:28:40 +02:00
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-08-06 19:04:53 +02:00
|
|
|
if (unlikely (!(c->check_struct (this))))
|
|
|
|
return_trace (false);
|
|
|
|
for (unsigned int i = 0; i < c->get_num_glyphs (); i++)
|
|
|
|
if (unlikely (!fds[i].sanitize (c)))
|
|
|
|
return_trace (false);
|
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline hb_codepoint_t get_fd (hb_codepoint_t glyph) const
|
|
|
|
{
|
|
|
|
return (hb_codepoint_t)fds[glyph];
|
2018-07-30 23:28:40 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
inline unsigned int get_size (unsigned int num_glyphs) const
|
|
|
|
{ return HBUINT8::static_size * num_glyphs; }
|
|
|
|
|
2018-07-18 23:17:52 +02:00
|
|
|
HBUINT8 fds[VAR];
|
2018-07-30 23:28:40 +02:00
|
|
|
|
|
|
|
DEFINE_SIZE_MIN (1);
|
2018-07-18 23:17:52 +02:00
|
|
|
};
|
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
template <typename GID_TYPE, typename FD_TYPE>
|
|
|
|
struct FDSelect3_4_Range {
|
|
|
|
inline bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
|
2018-07-30 23:28:40 +02:00
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-08-06 19:04:53 +02:00
|
|
|
return_trace (likely (c->check_struct (this) && (first < c->get_num_glyphs ()) && (fd < fdcount)));
|
2018-07-30 23:28:40 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
GID_TYPE first;
|
|
|
|
FD_TYPE fd;
|
2018-07-30 23:28:40 +02:00
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
DEFINE_SIZE_STATIC (GID_TYPE::static_size + FD_TYPE::static_size);
|
2018-07-18 23:17:52 +02:00
|
|
|
};
|
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
template <typename GID_TYPE, typename FD_TYPE>
|
|
|
|
struct FDSelect3_4 {
|
2018-08-01 20:30:38 +02:00
|
|
|
inline unsigned int get_size (void) const
|
2018-08-06 19:04:53 +02:00
|
|
|
{ return GID_TYPE::static_size * 2 + FDSelect3_4_Range<GID_TYPE, FD_TYPE>::static_size * nRanges; }
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
|
2018-07-30 23:28:40 +02:00
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-08-06 19:04:53 +02:00
|
|
|
if (unlikely (!(c->check_struct (this) && (nRanges > 0) && (ranges[0].first == 0))))
|
|
|
|
return_trace (false);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < nRanges; i++)
|
|
|
|
{
|
|
|
|
if (unlikely (!ranges[i].sanitize (c, fdcount)))
|
|
|
|
return_trace (false);
|
|
|
|
if ((0 < i) && unlikely (ranges[i - 1].first >= ranges[i].first))
|
|
|
|
return_trace (false);
|
|
|
|
}
|
|
|
|
if (unlikely (!sentinel().sanitize (c) || (sentinel() != c->get_num_glyphs ())))
|
|
|
|
return_trace (false);
|
|
|
|
|
|
|
|
return_trace (true);
|
2018-07-30 23:28:40 +02:00
|
|
|
}
|
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
inline hb_codepoint_t get_fd (hb_codepoint_t glyph) const
|
|
|
|
{
|
2018-09-27 03:51:36 +02:00
|
|
|
unsigned int i;
|
|
|
|
for (i = 1; i < nRanges; i++)
|
2018-08-10 20:07:07 +02:00
|
|
|
if (glyph < ranges[i].first)
|
2018-09-27 03:51:36 +02:00
|
|
|
break;
|
2018-08-06 19:04:53 +02:00
|
|
|
|
2018-09-27 03:51:36 +02:00
|
|
|
return (hb_codepoint_t)ranges[i - 1].fd;
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
2018-07-30 23:28:40 +02:00
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
inline GID_TYPE &sentinel (void) { return StructAfter<GID_TYPE> (ranges[nRanges - 1]); }
|
|
|
|
inline const GID_TYPE &sentinel (void) const { return StructAfter<GID_TYPE> (ranges[nRanges - 1]); }
|
|
|
|
|
|
|
|
GID_TYPE nRanges;
|
|
|
|
FDSelect3_4_Range<GID_TYPE, FD_TYPE> ranges[VAR];
|
|
|
|
/* GID_TYPE sentinel */
|
|
|
|
|
|
|
|
DEFINE_SIZE_ARRAY (GID_TYPE::static_size * 2, ranges);
|
2018-07-18 23:17:52 +02:00
|
|
|
};
|
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
typedef FDSelect3_4<HBUINT16, HBUINT8> FDSelect3;
|
|
|
|
typedef FDSelect3_4_Range<HBUINT16, HBUINT8> FDSelect3_Range;
|
|
|
|
|
2018-07-18 23:17:52 +02:00
|
|
|
struct FDSelect {
|
2018-08-06 19:04:53 +02:00
|
|
|
inline bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
|
2018-07-30 23:28:40 +02:00
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2018-07-30 23:28:40 +02:00
|
|
|
return_trace (likely (c->check_struct (this) && (format == 0 || format == 3) &&
|
2018-08-06 19:04:53 +02:00
|
|
|
(format == 0)?
|
|
|
|
u.format0.sanitize (c, fdcount):
|
|
|
|
u.format3.sanitize (c, fdcount)));
|
2018-07-30 23:28:40 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
inline bool serialize (hb_serialize_context_t *c, const FDSelect &src, unsigned int num_glyphs)
|
|
|
|
{
|
2018-08-02 19:52:08 +02:00
|
|
|
TRACE_SERIALIZE (this);
|
2018-08-01 20:30:38 +02:00
|
|
|
unsigned int size = src.get_size (num_glyphs);
|
|
|
|
FDSelect *dest = c->allocate_size<FDSelect> (size);
|
|
|
|
if (unlikely (dest == nullptr)) return_trace (false);
|
|
|
|
memcpy (dest, &src, size);
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned int calculate_serialized_size (unsigned int num_glyphs) const
|
|
|
|
{ return get_size (num_glyphs); }
|
|
|
|
|
|
|
|
inline unsigned int get_size (unsigned int num_glyphs) const
|
|
|
|
{
|
|
|
|
unsigned int size = format.static_size;
|
|
|
|
if (format == 0)
|
|
|
|
size += u.format0.get_size (num_glyphs);
|
2018-08-06 19:04:53 +02:00
|
|
|
else
|
2018-08-01 20:30:38 +02:00
|
|
|
size += u.format3.get_size ();
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
inline hb_codepoint_t get_fd (hb_codepoint_t glyph) const
|
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
if (this == &Null(FDSelect))
|
|
|
|
return 0;
|
2018-08-06 19:04:53 +02:00
|
|
|
if (format == 0)
|
|
|
|
return u.format0.get_fd (glyph);
|
|
|
|
else
|
|
|
|
return u.format3.get_fd (glyph);
|
|
|
|
}
|
|
|
|
|
2018-07-18 23:17:52 +02:00
|
|
|
HBUINT8 format;
|
|
|
|
union {
|
|
|
|
FDSelect0 format0;
|
|
|
|
FDSelect3 format3;
|
|
|
|
} u;
|
2018-07-30 23:28:40 +02:00
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
DEFINE_SIZE_MIN (1);
|
2018-07-18 23:17:52 +02:00
|
|
|
};
|
|
|
|
|
2018-08-15 21:00:19 +02:00
|
|
|
template <typename COUNT>
|
2018-08-16 17:03:46 +02:00
|
|
|
struct Subrs : CFFIndex<COUNT>
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
inline bool serialize (hb_serialize_context_t *c, const Subrs<COUNT> &subrs, unsigned int offSize, const hb_set_t *set, const ByteStr& nullStr = ByteStr())
|
2018-08-02 19:18:01 +02:00
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
TRACE_SERIALIZE (this);
|
2018-08-17 22:13:18 +02:00
|
|
|
if (&subrs == &Null(Subrs<COUNT>))
|
|
|
|
return_trace (true);
|
2018-08-29 21:14:30 +02:00
|
|
|
if ((subrs.count == 0) || (set == nullptr) || (hb_set_is_empty (set)))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
if (!unlikely (c->allocate_size<COUNT> (COUNT::static_size)))
|
|
|
|
return_trace (false);
|
2018-08-16 17:03:46 +02:00
|
|
|
CFFIndex<COUNT>::count.set (0);
|
2018-08-15 21:00:19 +02:00
|
|
|
return_trace (true);
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2018-08-15 21:00:19 +02:00
|
|
|
hb_vector_t<ByteStr> bytesArray;
|
|
|
|
bytesArray.init ();
|
|
|
|
if (!bytesArray.resize (subrs.count))
|
|
|
|
return_trace (false);
|
|
|
|
for (hb_codepoint_t i = 0; i < subrs.count; i++)
|
|
|
|
bytesArray[i] = (hb_set_has (set, i))? subrs[i]: nullStr;
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2018-08-16 17:03:46 +02:00
|
|
|
bool result = CFFIndex<COUNT>::serialize (c, offSize, bytesArray);
|
2018-08-15 21:00:19 +02:00
|
|
|
bytesArray.fini ();
|
|
|
|
return_trace (result);
|
2018-07-18 23:17:52 +02:00
|
|
|
}
|
|
|
|
|
2018-08-15 21:00:19 +02:00
|
|
|
/* in parallel to above */
|
|
|
|
inline unsigned int calculate_serialized_size (unsigned int &offSize /*OUT*/, const hb_set_t *set, unsigned int nullStrSize = 0) const
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2018-08-17 22:13:18 +02:00
|
|
|
if (this == &Null(Subrs<COUNT>))
|
|
|
|
return 0;
|
2018-08-16 17:03:46 +02:00
|
|
|
unsigned int count_ = CFFIndex<COUNT>::count;
|
2018-08-15 21:00:19 +02:00
|
|
|
offSize = 0;
|
|
|
|
if ((count_ == 0) || (hb_set_get_population (set) == 0))
|
|
|
|
return COUNT::static_size;
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2018-08-15 21:00:19 +02:00
|
|
|
unsigned int dataSize = 0;
|
|
|
|
for (hb_codepoint_t i = 0; i < count_; i++)
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
if (hb_set_has (set, i))
|
|
|
|
dataSize += (*this)[i].len;
|
|
|
|
else
|
|
|
|
dataSize += nullStrSize;
|
2018-07-18 23:17:52 +02:00
|
|
|
}
|
2018-08-15 21:00:19 +02:00
|
|
|
offSize = calcOffSize(dataSize);
|
2018-08-16 17:03:46 +02:00
|
|
|
return CFFIndex<COUNT>::calculate_serialized_size (offSize, count_, dataSize);
|
2018-07-18 23:17:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace CFF */
|
|
|
|
|
2018-08-29 22:36:39 +02:00
|
|
|
#endif /* HB_OT_CFF_COMMON_HH */
|
2018-07-18 23:17:52 +02:00
|
|
|
|