2018-07-18 23:17:52 +02:00
|
|
|
/*
|
2018-11-12 17:47:07 +01:00
|
|
|
* Copyright © 2018 Adobe Inc.
|
2018-07-18 23:17:52 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2019-06-18 07:12:40 +02:00
|
|
|
#include "hb-bimap.hh"
|
2018-08-29 22:26:17 +02:00
|
|
|
#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
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
using objidx_t = hb_serialize_context_t::objidx_t;
|
|
|
|
using whence_t = hb_serialize_context_t::whence_t;
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
/* utility macro */
|
|
|
|
template<typename Type>
|
2019-06-28 21:14:14 +02:00
|
|
|
static inline const Type& StructAtOffsetOrNull (const void *P, unsigned int offset)
|
|
|
|
{ return offset ? StructAtOffset<Type> (P, offset) : Null (Type); }
|
2018-08-10 20:07:07 +02:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
struct code_pair_t
|
2018-09-13 01:08:54 +02:00
|
|
|
{
|
2019-06-28 21:14:14 +02:00
|
|
|
hb_codepoint_t code;
|
|
|
|
hb_codepoint_t glyph;
|
2018-09-13 01:08:54 +02:00
|
|
|
};
|
|
|
|
|
2022-05-10 00:16:07 +02:00
|
|
|
using str_buff_t = hb_vector_t<unsigned char>;
|
|
|
|
using str_buff_vec_t = hb_vector_t<str_buff_t>;
|
2018-11-01 06:30:34 +01:00
|
|
|
|
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
|
|
|
{
|
2018-12-17 19:01:01 +01:00
|
|
|
unsigned int offset_array_size () const
|
2022-05-09 23:27:27 +02:00
|
|
|
{ return offSize * (count + 1); }
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
CFFIndex *copy (hb_serialize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
|
|
|
unsigned int size = get_size ();
|
2022-12-01 22:56:28 +01:00
|
|
|
CFFIndex *out = c->allocate_size<CFFIndex> (size, false);
|
2020-03-03 01:51:19 +01:00
|
|
|
if (likely (out))
|
2022-11-22 20:54:50 +01:00
|
|
|
hb_memcpy (out, this, size);
|
2020-03-03 01:51:19 +01:00
|
|
|
return_trace (out);
|
|
|
|
}
|
|
|
|
|
2022-12-02 00:09:03 +01:00
|
|
|
template <typename Iterable,
|
|
|
|
hb_requires (hb_is_iterable (Iterable))>
|
2020-03-03 01:51:19 +01:00
|
|
|
bool serialize (hb_serialize_context_t *c,
|
2022-12-02 00:09:03 +01:00
|
|
|
const Iterable &iterable)
|
2020-03-03 01:51:19 +01:00
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
2022-12-02 00:09:03 +01:00
|
|
|
auto it = hb_iter (iterable);
|
|
|
|
serialize_header(c, + it | hb_map (hb_iter) | hb_map (hb_len));
|
2022-05-06 20:54:02 +02:00
|
|
|
for (const auto &_ : +it)
|
2022-12-02 00:09:03 +01:00
|
|
|
hb_iter (_).copy (c);
|
2020-03-03 01:51:19 +01:00
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-05-09 23:38:40 +02:00
|
|
|
unsigned off_size = (hb_bit_storage (total + 1) + 7) / 8;
|
2020-03-03 01:51:19 +01:00
|
|
|
|
|
|
|
/* serialize CFFIndex header */
|
2021-07-29 01:36:22 +02:00
|
|
|
if (unlikely (!c->extend_min (this))) return_trace (false);
|
2020-03-03 01:51:19 +01:00
|
|
|
this->count = it.len ();
|
2022-05-06 20:50:37 +02:00
|
|
|
if (!this->count) return_trace (true);
|
|
|
|
if (unlikely (!c->extend (this->offSize))) return_trace (false);
|
2020-03-03 01:51:19 +01:00
|
|
|
this->offSize = off_size;
|
2022-12-01 23:46:00 +01:00
|
|
|
if (unlikely (!c->allocate_size<HBUINT8> (off_size * (this->count + 1), false)))
|
2020-03-03 01:51:19 +01:00
|
|
|
return_trace (false);
|
|
|
|
|
|
|
|
/* serialize indices */
|
2020-03-13 06:03:34 +01:00
|
|
|
unsigned int offset = 1;
|
|
|
|
unsigned int i = 0;
|
|
|
|
for (unsigned _ : +it)
|
|
|
|
{
|
2022-11-22 02:40:01 +01:00
|
|
|
set_offset_at (i++, offset);
|
2020-03-13 06:03:34 +01:00
|
|
|
offset += _;
|
|
|
|
}
|
2022-11-22 02:40:01 +01:00
|
|
|
set_offset_at (i, offset);
|
2020-03-03 01:51:19 +01:00
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2022-12-02 00:52:29 +01:00
|
|
|
template <typename Iterable,
|
|
|
|
hb_requires (hb_is_iterable (Iterable))>
|
|
|
|
static unsigned total_size (const Iterable &iterable)
|
|
|
|
{
|
|
|
|
auto it = + hb_iter (iterable) | hb_map (hb_iter) | hb_map (hb_len);
|
|
|
|
if (!it) return 0;
|
|
|
|
|
|
|
|
unsigned total = + it | hb_reduce (hb_add, 0);
|
|
|
|
unsigned off_size = (hb_bit_storage (total + 1) + 7) / 8;
|
|
|
|
|
|
|
|
return min_size + HBUINT8::static_size + (hb_len (it) + 1) * off_size + total;
|
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
void set_offset_at (unsigned int index, unsigned int offset)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2022-05-09 23:35:04 +02:00
|
|
|
assert (index <= count);
|
2018-08-01 20:30:38 +02:00
|
|
|
HBUINT8 *p = offsets + offSize * index + offSize;
|
|
|
|
unsigned int size = offSize;
|
|
|
|
for (; size; size--)
|
|
|
|
{
|
|
|
|
--p;
|
2019-03-30 04:17:46 +01:00
|
|
|
*p = offset & 0xFF;
|
2018-08-01 20:30:38 +02:00
|
|
|
offset >>= 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 23:23:59 +02:00
|
|
|
private:
|
2018-12-16 20:08:10 +01:00
|
|
|
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);
|
2022-05-13 20:05:05 +02:00
|
|
|
|
2018-07-18 23:17:52 +02:00
|
|
|
unsigned int size = offSize;
|
2022-05-13 20:05:05 +02:00
|
|
|
const HBUINT8 *p = offsets + size * index;
|
2022-11-21 22:36:57 +01:00
|
|
|
switch (size)
|
|
|
|
{
|
|
|
|
case 1: return * (HBUINT8 *) p;
|
|
|
|
case 2: return * (HBUINT16 *) p;
|
|
|
|
case 3: return * (HBUINT24 *) p;
|
|
|
|
case 4: return * (HBUINT32 *) p;
|
|
|
|
default: return 0;
|
|
|
|
}
|
2018-07-18 23:17:52 +02:00
|
|
|
}
|
|
|
|
|
2022-05-10 22:54:04 +02:00
|
|
|
unsigned int length_at (unsigned int index) const
|
|
|
|
{
|
2022-05-10 22:58:53 +02:00
|
|
|
unsigned offset0 = offset_at (index);
|
|
|
|
unsigned offset1 = offset_at (index + 1);
|
|
|
|
if (unlikely (offset1 < offset0 || offset1 > offset_at (count)))
|
2022-05-10 22:54:04 +02:00
|
|
|
return 0;
|
2022-05-10 22:58:53 +02:00
|
|
|
return offset1 - offset0;
|
2022-05-10 22:54:04 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
const unsigned char *data_base () const
|
2022-05-06 20:50:37 +02:00
|
|
|
{ return (const unsigned char *) this + min_size + offSize.static_size + offset_array_size (); }
|
2022-05-09 23:23:59 +02:00
|
|
|
public:
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2022-05-10 00:09:56 +02:00
|
|
|
hb_ubytes_t operator [] (unsigned int index) const
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2022-05-10 00:09:56 +02:00
|
|
|
if (unlikely (index >= count)) return hb_ubytes_t ();
|
2022-11-21 19:00:29 +01:00
|
|
|
_hb_compiler_memory_r_barrier ();
|
2022-05-13 21:53:17 +02:00
|
|
|
unsigned length = length_at (index);
|
|
|
|
if (unlikely (!length)) return hb_ubytes_t ();
|
|
|
|
return hb_ubytes_t (data_base () + offset_at (index) - 1, length);
|
2018-07-18 23:17:52 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
unsigned int get_size () const
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2022-05-06 20:50:37 +02:00
|
|
|
if (count)
|
|
|
|
return min_size + offSize.static_size + offset_array_size () + (offset_at (count) - 1);
|
|
|
|
return min_size; /* empty CFFIndex contains count only */
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2019-06-09 09:19:25 +02:00
|
|
|
bool sanitize (hb_sanitize_context_t *c) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2022-05-06 20:50:37 +02:00
|
|
|
return_trace (likely (c->check_struct (this) &&
|
|
|
|
(count == 0 || /* empty INDEX */
|
2022-05-06 21:37:11 +02:00
|
|
|
(count < count + 1u &&
|
|
|
|
c->check_struct (&offSize) && offSize >= 1 && offSize <= 4 &&
|
|
|
|
c->check_array (offsets, offSize, count + 1u) &&
|
|
|
|
c->check_array ((const HBUINT8*) data_base (), 1, offset_at (count) - 1)))));
|
2018-07-30 23:28:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2019-06-09 09:19:25 +02:00
|
|
|
COUNT count; /* Number of object data. Note there are (count+1) offsets */
|
2022-05-09 23:20:16 +02:00
|
|
|
private:
|
2019-06-09 09:19:25 +02:00
|
|
|
HBUINT8 offSize; /* The byte size of each offset in the offsets array. */
|
2020-04-24 21:15:17 +02:00
|
|
|
HBUINT8 offsets[HB_VAR_ARRAY];
|
|
|
|
/* The array of (count + 1) offsets into objects array (1-base). */
|
2019-09-06 22:54:27 +02:00
|
|
|
/* HBUINT8 data[HB_VAR_ARRAY]; Object data */
|
2018-07-18 23:17:52 +02:00
|
|
|
public:
|
2022-05-06 20:50:37 +02:00
|
|
|
DEFINE_SIZE_MIN (COUNT::static_size);
|
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
|
|
|
template <typename DATA, typename PARAM1, typename PARAM2>
|
2018-12-16 20:08:10 +01:00
|
|
|
bool serialize (hb_serialize_context_t *c,
|
|
|
|
unsigned int offSize_,
|
|
|
|
const DATA *dataArray,
|
|
|
|
unsigned int dataArrayLen,
|
|
|
|
const hb_vector_t<unsigned int> &dataSizeArray,
|
|
|
|
const PARAM1 ¶m1,
|
|
|
|
const PARAM2 ¶m2)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
2018-08-16 17:03:46 +02:00
|
|
|
/* serialize CFFIndex header */
|
2021-07-29 01:36:22 +02:00
|
|
|
if (unlikely (!c->extend_min (this))) return_trace (false);
|
2019-03-30 04:17:46 +01:00
|
|
|
this->count = dataArrayLen;
|
|
|
|
this->offSize = offSize_;
|
2022-12-01 22:56:28 +01:00
|
|
|
if (unlikely (!c->allocate_size<HBUINT8> (offSize_ * (dataArrayLen + 1), false)))
|
2018-08-10 20:07:07 +02:00
|
|
|
return_trace (false);
|
2018-12-01 04:54:57 +01:00
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
/* 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
|
|
|
{
|
2022-11-22 02:40:01 +01:00
|
|
|
this->set_offset_at (i, offset);
|
2018-08-10 20:07:07 +02:00
|
|
|
offset += dataSizeArray[i];
|
|
|
|
}
|
2022-11-22 02:40:01 +01:00
|
|
|
this->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
|
|
|
{
|
2019-06-09 09:19:25 +02:00
|
|
|
TYPE *dest = c->start_embed<TYPE> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!dest || !dest->serialize (c, dataArray[i], param1, param2)))
|
2018-12-01 05:04:59 +01:00
|
|
|
return_trace (false);
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
template <typename DICTVAL, typename OP_SERIALIZER, typename ...Ts>
|
2018-12-16 20:08:10 +01:00
|
|
|
bool serialize (hb_serialize_context_t *c,
|
|
|
|
const DICTVAL &dictval,
|
|
|
|
OP_SERIALIZER& opszr,
|
2020-03-03 01:51:19 +01:00
|
|
|
Ts&&... ds)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
2018-11-01 06:30:34 +01:00
|
|
|
for (unsigned int i = 0; i < dictval.get_count (); i++)
|
2021-11-02 07:18:22 +01:00
|
|
|
if (unlikely (!opszr.serialize (c, dictval[i], std::forward<Ts> (ds)...)))
|
2018-12-01 05:04:59 +01:00
|
|
|
return_trace (false);
|
2019-06-09 09:19:25 +02:00
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
return_trace (true);
|
|
|
|
}
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2020-02-28 02:34:26 +01:00
|
|
|
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)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2022-11-30 19:25:50 +01:00
|
|
|
if (unlikely ((!serialize_int<T, V> (c, intOp, value))))
|
2018-08-01 20:30:38 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
TRACE_SERIALIZE (this);
|
|
|
|
/* serialize the opcode */
|
2022-12-01 22:56:28 +01:00
|
|
|
HBUINT8 *p = c->allocate_size<HBUINT8> (OpCode_Size (op), false);
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!p)) return_trace (false);
|
2018-08-29 21:14:30 +02:00
|
|
|
if (Is_OpCode_ESC (op))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2019-03-30 04:17:46 +01:00
|
|
|
*p = OpCode_escape;
|
2018-08-29 21:14:30 +02:00
|
|
|
op = Unmake_OpCode_ESC (op);
|
2018-08-01 20:30:38 +02:00
|
|
|
p++;
|
|
|
|
}
|
2019-03-30 04:17:46 +01:00
|
|
|
*p = op;
|
2018-08-01 20:30:38 +02:00
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2020-02-28 02:34:26 +01:00
|
|
|
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); }
|
2018-09-11 01:00:20 +02:00
|
|
|
|
2020-02-28 02:34:26 +01:00
|
|
|
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); }
|
2018-09-11 01:00:20 +02:00
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
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;
|
2020-03-06 00:40:44 +01:00
|
|
|
c->add_link (ofs, link, whence);
|
2020-03-03 01:51:19 +01:00
|
|
|
return true;
|
|
|
|
}
|
2020-03-13 06:03:34 +01:00
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
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_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); }
|
2018-08-01 20:30:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TopDict : Dict {};
|
|
|
|
struct FontDict : Dict {};
|
|
|
|
struct PrivateDict : Dict {};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
struct table_info_t
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2020-03-05 01:54:27 +01:00
|
|
|
void init () { offset = size = 0; link = 0; }
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
unsigned int offset;
|
|
|
|
unsigned int size;
|
2020-03-03 01:51:19 +01:00
|
|
|
objidx_t link;
|
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
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
template <typename DICTVAL, typename INFO, typename Iterator, typename OP_SERIALIZER>
|
2018-12-16 20:08:10 +01:00
|
|
|
bool serialize (hb_serialize_context_t *c,
|
2020-03-03 01:51:19 +01:00
|
|
|
Iterator it,
|
2018-12-16 20:08:10 +01:00
|
|
|
OP_SERIALIZER& opszr)
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
/* serialize INDEX data */
|
|
|
|
hb_vector_t<unsigned> sizes;
|
|
|
|
c->push ();
|
|
|
|
+ it
|
|
|
|
| hb_map ([&] (const hb_pair_t<const DICTVAL&, const INFO&> &_)
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
|
|
|
FontDict *dict = c->start_embed<FontDict> ();
|
2020-03-03 01:51:19 +01:00
|
|
|
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)));
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
};
|
2018-07-18 23:17:52 +02:00
|
|
|
|
|
|
|
/* FDSelect */
|
|
|
|
struct FDSelect0 {
|
2018-12-16 20:08:10 +01:00
|
|
|
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);
|
2022-11-30 19:20:19 +01:00
|
|
|
if (unlikely (!c->check_array (fds, c->get_num_glyphs ())))
|
|
|
|
return_trace (false);
|
2018-08-06 19:04:53 +02:00
|
|
|
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_codepoint_t get_fd (hb_codepoint_t glyph) const
|
2019-06-09 09:19:25 +02:00
|
|
|
{ return (hb_codepoint_t) fds[glyph]; }
|
2018-07-30 23:28:40 +02:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
unsigned int get_size (unsigned int num_glyphs) const
|
2018-08-01 20:30:38 +02:00
|
|
|
{ return HBUINT8::static_size * num_glyphs; }
|
|
|
|
|
2019-09-06 22:54:27 +02:00
|
|
|
HBUINT8 fds[HB_VAR_ARRAY];
|
2018-07-30 23:28:40 +02:00
|
|
|
|
2019-09-06 22:41:12 +02:00
|
|
|
DEFINE_SIZE_MIN (0);
|
2018-07-18 23:17:52 +02:00
|
|
|
};
|
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
template <typename GID_TYPE, typename FD_TYPE>
|
2019-06-09 09:19:25 +02:00
|
|
|
struct FDSelect3_4_Range
|
|
|
|
{
|
2019-04-05 11:29:56 +02:00
|
|
|
bool sanitize (hb_sanitize_context_t *c, const void * /*nullptr*/, unsigned int fdcount) const
|
2018-07-30 23:28:40 +02:00
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2019-01-15 03:23:17 +01:00
|
|
|
return_trace (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;
|
2019-06-09 09:19:25 +02:00
|
|
|
public:
|
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>
|
2019-06-09 09:19:25 +02:00
|
|
|
struct FDSelect3_4
|
|
|
|
{
|
2018-12-17 19:01:01 +01:00
|
|
|
unsigned int get_size () const
|
2019-01-15 03:23:17 +01:00
|
|
|
{ return GID_TYPE::static_size * 2 + ranges.get_size (); }
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
|
2018-07-30 23:28:40 +02:00
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
2019-01-15 03:23:17 +01:00
|
|
|
if (unlikely (!c->check_struct (this) || !ranges.sanitize (c, nullptr, fdcount) ||
|
2019-12-31 13:23:02 +01:00
|
|
|
(nRanges () == 0) || ranges[0].first != 0))
|
2018-08-06 19:04:53 +02:00
|
|
|
return_trace (false);
|
|
|
|
|
2019-01-15 03:23:17 +01:00
|
|
|
for (unsigned int i = 1; i < nRanges (); i++)
|
|
|
|
if (unlikely (ranges[i - 1].first >= ranges[i].first))
|
2019-06-09 09:19:25 +02:00
|
|
|
return_trace (false);
|
2019-01-15 03:23:17 +01:00
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
if (unlikely (!sentinel().sanitize (c) || (sentinel() != c->get_num_glyphs ())))
|
|
|
|
return_trace (false);
|
|
|
|
|
|
|
|
return_trace (true);
|
2018-07-30 23:28:40 +02:00
|
|
|
}
|
|
|
|
|
2022-11-28 21:31:40 +01:00
|
|
|
static int _cmp_range (const void *_key, const void *_item)
|
2018-08-06 19:04:53 +02:00
|
|
|
{
|
2022-11-28 21:31:40 +01:00
|
|
|
hb_codepoint_t glyph = * (hb_codepoint_t *) _key;
|
|
|
|
FDSelect3_4_Range<GID_TYPE, FD_TYPE> *range = (FDSelect3_4_Range<GID_TYPE, FD_TYPE> *) _item;
|
|
|
|
|
|
|
|
if (glyph < range[0].first) return -1;
|
|
|
|
if (glyph < range[1].first) return 0;
|
|
|
|
return +1;
|
|
|
|
}
|
2018-08-06 19:04:53 +02:00
|
|
|
|
2022-11-28 21:31:40 +01:00
|
|
|
hb_codepoint_t get_fd (hb_codepoint_t glyph) const
|
|
|
|
{
|
|
|
|
auto *range = hb_bsearch (glyph, &ranges[0], nRanges () - 1, sizeof (ranges[0]), _cmp_range);
|
|
|
|
return range ? range->fd : ranges[nRanges () - 1].fd;
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
2018-07-30 23:28:40 +02:00
|
|
|
|
2019-10-01 13:35:02 +02:00
|
|
|
GID_TYPE &nRanges () { return ranges.len; }
|
|
|
|
GID_TYPE nRanges () const { return ranges.len; }
|
2019-10-01 12:19:55 +02:00
|
|
|
GID_TYPE &sentinel () { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
|
|
|
|
const GID_TYPE &sentinel () const { return StructAfter<GID_TYPE> (ranges[nRanges () - 1]); }
|
2018-08-06 19:04:53 +02:00
|
|
|
|
2019-01-15 03:23:17 +01:00
|
|
|
ArrayOf<FDSelect3_4_Range<GID_TYPE, FD_TYPE>, GID_TYPE> ranges;
|
2018-08-06 19:04:53 +02:00
|
|
|
/* GID_TYPE sentinel */
|
|
|
|
|
2018-11-03 00:40:20 +01:00
|
|
|
DEFINE_SIZE_ARRAY (GID_TYPE::static_size, 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;
|
|
|
|
|
2019-06-09 09:19:25 +02:00
|
|
|
struct FDSelect
|
|
|
|
{
|
2018-12-16 20:08:10 +01:00
|
|
|
bool serialize (hb_serialize_context_t *c, const FDSelect &src, unsigned int num_glyphs)
|
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 (num_glyphs);
|
2022-12-01 22:56:28 +01:00
|
|
|
FDSelect *dest = c->allocate_size<FDSelect> (size, false);
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!dest)) return_trace (false);
|
2022-11-22 20:54:50 +01:00
|
|
|
hb_memcpy (dest, &src, size);
|
2018-08-01 20:30:38 +02:00
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
unsigned int get_size (unsigned int num_glyphs) const
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2019-06-09 09:19:25 +02:00
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case 0: return format.static_size + u.format0.get_size (num_glyphs);
|
|
|
|
case 3: return format.static_size + u.format3.get_size ();
|
|
|
|
default:return 0;
|
|
|
|
}
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
hb_codepoint_t get_fd (hb_codepoint_t glyph) const
|
2018-08-06 19:04:53 +02:00
|
|
|
{
|
2019-10-01 13:35:02 +02:00
|
|
|
if (this == &Null (FDSelect)) return 0;
|
|
|
|
|
2019-06-09 09:19:25 +02:00
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case 0: return u.format0.get_fd (glyph);
|
|
|
|
case 3: return u.format3.get_fd (glyph);
|
|
|
|
default:return 0;
|
|
|
|
}
|
2018-08-06 19:04:53 +02:00
|
|
|
}
|
|
|
|
|
2019-06-09 09:19:25 +02:00
|
|
|
bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
|
|
|
|
{
|
|
|
|
TRACE_SANITIZE (this);
|
|
|
|
if (unlikely (!c->check_struct (this)))
|
|
|
|
return_trace (false);
|
|
|
|
|
|
|
|
switch (format)
|
|
|
|
{
|
|
|
|
case 0: return_trace (u.format0.sanitize (c, fdcount));
|
|
|
|
case 3: return_trace (u.format3.sanitize (c, fdcount));
|
|
|
|
default:return_trace (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HBUINT8 format;
|
2018-07-18 23:17:52 +02:00
|
|
|
union {
|
2019-06-09 09:19:25 +02:00
|
|
|
FDSelect0 format0;
|
|
|
|
FDSelect3 format3;
|
2018-07-18 23:17:52 +02:00
|
|
|
} u;
|
2019-06-09 09:19:25 +02:00
|
|
|
public:
|
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-11-01 06:30:34 +01:00
|
|
|
typedef COUNT count_type;
|
|
|
|
typedef CFFIndex<COUNT> SUPER;
|
2018-07-18 23:17:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace CFF */
|
|
|
|
|
2018-08-29 22:36:39 +02:00
|
|
|
#endif /* HB_OT_CFF_COMMON_HH */
|