hb-ot-cff-common.hh 20.2 KB
Newer Older
M
Michiharu Ariza 已提交
1
/*
M
Michiharu Ariza 已提交
2
 * Copyright © 2018 Adobe Inc.
M
Michiharu Ariza 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  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
 */
M
Michiharu Ariza 已提交
26 27
#ifndef HB_OT_CFF_COMMON_HH
#define HB_OT_CFF_COMMON_HH
M
Michiharu Ariza 已提交
28

29 30 31
#include "hb-open-type.hh"
#include "hb-ot-layout-common.hh"
#include "hb-cff-interp-dict-common.hh"
M
Michiharu Ariza 已提交
32
#include "hb-subset-plan.hh"
M
Michiharu Ariza 已提交
33 34 35 36 37

namespace CFF {

using namespace OT;

M
Michiharu Ariza 已提交
38 39
#define CFF_UNDEF_CODE  0xFFFFFFFF

M
Michiharu Ariza 已提交
40 41 42 43 44
/* 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); }

45
inline unsigned int calcOffSize(unsigned int dataSize)
M
Michiharu Ariza 已提交
46 47
{
  unsigned int size = 1;
48
  unsigned int offset = dataSize + 1;
M
Michiharu Ariza 已提交
49 50 51 52 53 54 55 56
  while ((offset & ~0xFF) != 0)
  {
    size++;
    offset >>= 8;
  }
  assert (size <= 4);
  return size;
}
M
Michiharu Ariza 已提交
57

M
Michiharu Ariza 已提交
58 59 60 61 62 63
struct code_pair
{
  hb_codepoint_t  code;
  hb_codepoint_t  glyph;
};

64
typedef hb_vector_t<char, 1> StrBuff;
M
Michiharu Ariza 已提交
65
struct StrBuffArray : hb_vector_t<StrBuff>
66
{
M
Michiharu Ariza 已提交
67 68 69 70 71
  inline void fini (void)
  {
    SUPER::fini_deep ();
  }

72 73 74 75 76 77 78
  inline unsigned int total_size (void) const
  {
    unsigned int size = 0;
    for (unsigned int i = 0; i < len; i++)
      size += (*this)[i].len;
    return size;
  }
M
Michiharu Ariza 已提交
79 80 81

  private:
  typedef hb_vector_t<StrBuff> SUPER;
82 83
};

M
Michiharu Ariza 已提交
84
/* CFF INDEX */
M
Michiharu Ariza 已提交
85
template <typename COUNT>
86
struct CFFIndex
M
Michiharu Ariza 已提交
87 88 89 90
{
  inline bool sanitize (hb_sanitize_context_t *c) const
  {
    TRACE_SANITIZE (this);
91
    return_trace (likely ((count.sanitize (c) && count == 0) || /* empty INDEX */
B
Behdad Esfahbod 已提交
92 93 94
			  (c->check_struct (this) && offSize >= 1 && offSize <= 4 &&
			   c->check_array (offsets, offSize, count + 1) &&
			   c->check_array ((const HBUINT8*)data_base (), 1, max_offset () - 1))));
M
Michiharu Ariza 已提交
95 96
  }

M
Michiharu Ariza 已提交
97
  inline static unsigned int calculate_offset_array_size (unsigned int offSize, unsigned int count)
M
Michiharu Ariza 已提交
98 99
  { return offSize * (count + 1); }

M
Michiharu Ariza 已提交
100 101 102 103
  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)
104 105 106 107 108 109
  {
    if (count == 0)
      return COUNT::static_size;
    else
      return min_size + calculate_offset_array_size (offSize, count) + dataSize;
  }
M
Michiharu Ariza 已提交
110

111
  inline bool serialize (hb_serialize_context_t *c, const CFFIndex &src)
M
Michiharu Ariza 已提交
112
  {
M
Michiharu Ariza 已提交
113
    TRACE_SERIALIZE (this);
M
Michiharu Ariza 已提交
114
    unsigned int size = src.get_size ();
115
    CFFIndex *dest = c->allocate_size<CFFIndex> (size);
M
Michiharu Ariza 已提交
116 117 118 119 120 121
    if (unlikely (dest == nullptr)) return_trace (false);
    memcpy (dest, &src, size);
    return_trace (true);
  }

  inline bool serialize (hb_serialize_context_t *c,
B
Behdad Esfahbod 已提交
122 123
			 unsigned int offSize_,
			 const ByteStrArray &byteArray)
M
Michiharu Ariza 已提交
124 125
  {
    TRACE_SERIALIZE (this);
126
    if (byteArray.len == 0)
M
Michiharu Ariza 已提交
127
    {
128 129 130
      COUNT *dest = c->allocate_min<COUNT> ();
      if (unlikely (dest == nullptr)) return_trace (false);
      dest->set (0);
M
Michiharu Ariza 已提交
131
    }
132
    else
M
Michiharu Ariza 已提交
133
    {
134 135 136 137 138
      /* serialize CFFIndex header */
      if (unlikely (!c->extend_min (*this))) return_trace (false);
      this->count.set (byteArray.len);
      this->offSize.set (offSize_);
      if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (byteArray.len + 1))))
B
Behdad Esfahbod 已提交
139
	return_trace (false);
B
Behdad Esfahbod 已提交
140

141 142 143 144 145
      /* serialize indices */
      unsigned int  offset = 1;
      unsigned int  i = 0;
      for (; i < byteArray.len; i++)
      {
B
Behdad Esfahbod 已提交
146 147
	set_offset_at (i, offset);
	offset += byteArray[i].get_size ();
148 149 150 151 152 153
      }
      set_offset_at (i, offset);

      /* serialize data */
      for (unsigned int i = 0; i < byteArray.len; i++)
      {
B
Behdad Esfahbod 已提交
154 155 156 157
	ByteStr  *dest = c->start_embed<ByteStr> ();
	if (unlikely (dest == nullptr ||
		      !dest->serialize (c, byteArray[i])))
	  return_trace (false);
158
      }
M
Michiharu Ariza 已提交
159 160 161 162
    }
    return_trace (true);
  }

163
  inline bool serialize (hb_serialize_context_t *c,
B
Behdad Esfahbod 已提交
164 165
			 unsigned int offSize_,
			 const StrBuffArray &buffArray)
166 167 168 169 170 171 172 173 174 175 176 177 178
  {
    ByteStrArray  byteArray;
    byteArray.init ();
    byteArray.resize (buffArray.len);
    for (unsigned int i = 0; i < byteArray.len; i++)
    {
      byteArray[i] = ByteStr (buffArray[i].arrayZ (), buffArray[i].len);
    }
    bool result = this->serialize (c, offSize_, byteArray);
    byteArray.fini ();
    return result;
  }

M
Michiharu Ariza 已提交
179 180 181 182 183 184 185 186 187 188 189 190
  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;
    }
  }

M
Michiharu Ariza 已提交
191
  inline unsigned int offset_at (unsigned int index) const
M
Michiharu Ariza 已提交
192
  {
193
    assert (index <= count);
M
Michiharu Ariza 已提交
194 195 196 197 198 199 200 201
    const HBUINT8 *p = offsets + offSize * index;
    unsigned int size = offSize;
    unsigned int offset = 0;
    for (; size; size--)
      offset = (offset << 8) + *p++;
    return offset;
  }

M
Michiharu Ariza 已提交
202
  inline unsigned int length_at (unsigned int index) const
M
Michiharu Ariza 已提交
203 204 205
  { return offset_at (index + 1) - offset_at (index); }

  inline const char *data_base (void) const
M
Michiharu Ariza 已提交
206
  { return (const char *)this + min_size + offset_array_size (); }
M
Michiharu Ariza 已提交
207 208

  inline unsigned int data_size (void) const
M
Michiharu Ariza 已提交
209
  { return HBINT8::static_size; }
M
Michiharu Ariza 已提交
210

211
  inline ByteStr operator [] (unsigned int index) const
M
Michiharu Ariza 已提交
212 213
  {
    if (likely (index < count))
M
Michiharu Ariza 已提交
214 215 216
      return ByteStr (data_base () + offset_at (index) - 1, offset_at (index + 1) - offset_at (index));
    else
      return Null(ByteStr);
M
Michiharu Ariza 已提交
217 218 219
  }

  inline unsigned int get_size (void) const
M
Michiharu Ariza 已提交
220
  {
221
    if (this != &Null(CFFIndex))
222 223
    {
      if (count > 0)
B
Behdad Esfahbod 已提交
224
	return min_size + offset_array_size () + (offset_at (count) - 1);
225
      else
B
Behdad Esfahbod 已提交
226
	return count.static_size;  /* empty CFFIndex contains count only */
227
    }
M
Michiharu Ariza 已提交
228 229 230
    else
      return 0;
  }
M
Michiharu Ariza 已提交
231

232 233 234 235
  protected:
  inline unsigned int max_offset (void) const
  {
    unsigned int max = 0;
B
Behdad Esfahbod 已提交
236
    for (unsigned int i = 0; i < count + 1u; i++)
237 238 239 240 241 242 243 244
    {
      unsigned int off = offset_at (i);
      if (off > max) max = off;
    }
    return max;
  }

  public:
B
Behdad Esfahbod 已提交
245
  COUNT     count;	/* Number of object data. Note there are (count+1) offsets */
M
Michiharu Ariza 已提交
246 247 248 249
  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:
M
Michiharu Ariza 已提交
250
  DEFINE_SIZE_ARRAY (COUNT::static_size + HBUINT8::static_size, offsets);
M
Michiharu Ariza 已提交
251 252
};

M
Michiharu Ariza 已提交
253
template <typename COUNT, typename TYPE>
M
Michiharu Ariza 已提交
254
struct CFFIndexOf : CFFIndex<COUNT>
M
Michiharu Ariza 已提交
255
{
M
Michiharu Ariza 已提交
256
  inline const ByteStr operator [] (unsigned int index) const
M
Michiharu Ariza 已提交
257
  {
258 259
    if (likely (index < CFFIndex<COUNT>::count))
      return ByteStr (CFFIndex<COUNT>::data_base () + CFFIndex<COUNT>::offset_at (index) - 1, CFFIndex<COUNT>::length_at (index));
M
Michiharu Ariza 已提交
260
    return Null(ByteStr);
M
Michiharu Ariza 已提交
261
  }
M
Michiharu Ariza 已提交
262 263 264

  template <typename DATA, typename PARAM1, typename PARAM2>
  inline bool serialize (hb_serialize_context_t *c,
B
Behdad Esfahbod 已提交
265 266 267 268 269 270
			 unsigned int offSize_,
			 const DATA *dataArray,
			 unsigned int dataArrayLen,
			 const hb_vector_t<unsigned int> &dataSizeArray,
			 const PARAM1 &param1,
			 const PARAM2 &param2)
M
Michiharu Ariza 已提交
271 272
  {
    TRACE_SERIALIZE (this);
273
    /* serialize CFFIndex header */
M
Michiharu Ariza 已提交
274
    if (unlikely (!c->extend_min (*this))) return_trace (false);
M
Michiharu Ariza 已提交
275
    this->count.set (dataArrayLen);
M
Michiharu Ariza 已提交
276
    this->offSize.set (offSize_);
M
Michiharu Ariza 已提交
277
    if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (dataArrayLen + 1))))
M
Michiharu Ariza 已提交
278
      return_trace (false);
B
Behdad Esfahbod 已提交
279

M
Michiharu Ariza 已提交
280 281 282
    /* serialize indices */
    unsigned int  offset = 1;
    unsigned int  i = 0;
M
Michiharu Ariza 已提交
283
    for (; i < dataArrayLen; i++)
M
Michiharu Ariza 已提交
284
    {
285
      CFFIndex<COUNT>::set_offset_at (i, offset);
M
Michiharu Ariza 已提交
286 287
      offset += dataSizeArray[i];
    }
288
    CFFIndex<COUNT>::set_offset_at (i, offset);
M
Michiharu Ariza 已提交
289 290

    /* serialize data */
M
Michiharu Ariza 已提交
291
    for (unsigned int i = 0; i < dataArrayLen; i++)
M
Michiharu Ariza 已提交
292 293 294
    {
      TYPE  *dest = c->start_embed<TYPE> ();
      if (unlikely (dest == nullptr ||
B
Behdad Esfahbod 已提交
295 296
		    !dest->serialize (c, dataArray[i], param1, param2)))
	return_trace (false);
M
Michiharu Ariza 已提交
297 298 299 300 301 302 303
    }
    return_trace (true);
  }

  /* in parallel to above */
  template <typename DATA, typename PARAM>
  inline static unsigned int calculate_serialized_size (unsigned int &offSize_ /* OUT */,
B
Behdad Esfahbod 已提交
304 305 306 307
							const DATA *dataArray,
							unsigned int dataArrayLen,
							hb_vector_t<unsigned int> &dataSizeArray, /* OUT */
							const PARAM &param)
M
Michiharu Ariza 已提交
308 309 310
  {
    /* determine offset size */
    unsigned int  totalDataSize = 0;
M
Michiharu Ariza 已提交
311
    for (unsigned int i = 0; i < dataArrayLen; i++)
M
Michiharu Ariza 已提交
312 313 314 315 316 317 318
    {
      unsigned int dataSize = TYPE::calculate_serialized_size (dataArray[i], param);
      dataSizeArray[i] = dataSize;
      totalDataSize += dataSize;
    }
    offSize_ = calcOffSize (totalDataSize);

M
Michiharu Ariza 已提交
319
    return CFFIndex<COUNT>::calculate_serialized_size (offSize_, dataArrayLen, totalDataSize);
M
Michiharu Ariza 已提交
320 321 322
  }
};

M
Michiharu Ariza 已提交
323
/* Top Dict, Font Dict, Private Dict */
M
Michiharu Ariza 已提交
324 325
struct Dict : UnsizedByteStr
{
326
  template <typename DICTVAL, typename OP_SERIALIZER, typename PARAM>
M
Michiharu Ariza 已提交
327
  inline bool serialize (hb_serialize_context_t *c,
B
Behdad Esfahbod 已提交
328 329 330
			const DICTVAL &dictval,
			OP_SERIALIZER& opszr,
			PARAM& param)
M
Michiharu Ariza 已提交
331 332
  {
    TRACE_SERIALIZE (this);
333
    for (unsigned int i = 0; i < dictval.get_count (); i++)
M
Michiharu Ariza 已提交
334
    {
M
Michiharu Ariza 已提交
335
      if (unlikely (!opszr.serialize (c, dictval[i], param)))
B
Behdad Esfahbod 已提交
336
	return_trace (false);
M
Michiharu Ariza 已提交
337 338 339
    }
    return_trace (true);
  }
M
Michiharu Ariza 已提交
340

M
Michiharu Ariza 已提交
341
  /* in parallel to above */
M
Michiharu Ariza 已提交
342 343
  template <typename DICTVAL, typename OP_SERIALIZER, typename PARAM>
  inline static unsigned int calculate_serialized_size (const DICTVAL &dictval,
B
Behdad Esfahbod 已提交
344 345
							OP_SERIALIZER& opszr,
							PARAM& param)
M
Michiharu Ariza 已提交
346 347 348 349 350 351 352
  {
    unsigned int size = 0;
    for (unsigned int i = 0; i < dictval.get_count (); i++)
      size += opszr.calculate_serialized_size (dictval[i], param);
    return size;
  }

353 354
  template <typename DICTVAL, typename OP_SERIALIZER>
  inline static unsigned int calculate_serialized_size (const DICTVAL &dictval,
B
Behdad Esfahbod 已提交
355
							OP_SERIALIZER& opszr)
M
Michiharu Ariza 已提交
356 357
  {
    unsigned int size = 0;
358
    for (unsigned int i = 0; i < dictval.get_count (); i++)
M
Michiharu Ariza 已提交
359
      size += opszr.calculate_serialized_size (dictval[i]);
M
Michiharu Ariza 已提交
360 361 362 363
    return size;
  }

  template <typename INTTYPE, int minVal, int maxVal>
M
Michiharu Ariza 已提交
364
  inline static bool serialize_int_op (hb_serialize_context_t *c, OpCode op, int value, OpCode intOp)
M
Michiharu Ariza 已提交
365 366 367 368 369 370 371
  {
    // 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 */
M
Michiharu Ariza 已提交
372
    HBUINT8 *p = c->allocate_size<HBUINT8> (OpCode_Size (op));
M
Michiharu Ariza 已提交
373
    if (unlikely (p == nullptr)) return_trace (false);
M
Michiharu Ariza 已提交
374
    if (Is_OpCode_ESC (op))
M
Michiharu Ariza 已提交
375 376
    {
      p->set (OpCode_escape);
M
Michiharu Ariza 已提交
377
      op = Unmake_OpCode_ESC (op);
M
Michiharu Ariza 已提交
378 379 380 381 382 383
      p++;
    }
    p->set (op);
    return_trace (true);
  }

M
Michiharu Ariza 已提交
384 385 386 387 388 389
  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); }

M
Michiharu Ariza 已提交
390
  inline static bool serialize_offset4_op (hb_serialize_context_t *c, OpCode op, int value)
M
Michiharu Ariza 已提交
391 392 393
  {
    return serialize_uint4_op (c, op, value);
  }
M
Michiharu Ariza 已提交
394 395

  inline static bool serialize_offset2_op (hb_serialize_context_t *c, OpCode op, int value)
M
Michiharu Ariza 已提交
396 397 398
  {
    return serialize_uint2_op (c, op, value);
  }
M
Michiharu Ariza 已提交
399 400 401 402 403 404
};

struct TopDict : Dict {};
struct FontDict : Dict {};
struct PrivateDict : Dict {};

M
Michiharu Ariza 已提交
405 406
struct TableInfo
{
M
Michiharu Ariza 已提交
407
  void init (void) { offSize = offset = size = 0; }
M
Michiharu Ariza 已提交
408 409 410

  unsigned int    offset;
  unsigned int    size;
M
Michiharu Ariza 已提交
411
  unsigned int    offSize;
M
Michiharu Ariza 已提交
412 413
};

M
Michiharu Ariza 已提交
414 415 416
/* 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>
417 418
{
  inline void init (void)
419
  { SUPER::init (); }
420 421

  inline void fini (void)
422
  { SUPER::fini (); }
423

424
  inline bool reset (unsigned int size)
M
Michiharu Ariza 已提交
425
  {
426
    if (unlikely (!SUPER::resize (size)))
M
Michiharu Ariza 已提交
427 428 429 430 431 432 433
      return false;
    for (unsigned int i = 0; i < len; i++)
      (*this)[i] = CFF_UNDEF_CODE;
    count = 0;
    return true;
  }

434 435 436 437 438 439 440 441 442 443 444
  inline bool identity (unsigned int size)
  {
    if (unlikely (!SUPER::resize (size)))
      return false;
    unsigned int i;
    for (i = 0; i < len; i++)
      (*this)[i] = i;
    count = i;
    return true;
  }

M
Michiharu Ariza 已提交
445
  inline bool excludes (hb_codepoint_t id) const
M
Michiharu Ariza 已提交
446
  { return (id < len) && ((*this)[id] == CFF_UNDEF_CODE); }
447

448 449 450
  inline bool includes (hb_codepoint_t id) const
  { return !excludes (id); }

M
Michiharu Ariza 已提交
451 452 453 454 455 456 457 458 459 460 461 462
  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;
463 464 465

  private:
  typedef hb_vector_t<hb_codepoint_t> SUPER;
466 467
};

M
Michiharu Ariza 已提交
468
template <typename COUNT>
M
Michiharu Ariza 已提交
469
struct FDArray : CFFIndexOf<COUNT, FontDict>
M
Michiharu Ariza 已提交
470
{
M
Michiharu Ariza 已提交
471 472 473
  /* used by CFF1 */
  template <typename DICTVAL, typename OP_SERIALIZER>
  inline bool serialize (hb_serialize_context_t *c,
B
Behdad Esfahbod 已提交
474 475 476
			unsigned int offSize_,
			const hb_vector_t<DICTVAL> &fontDicts,
			OP_SERIALIZER& opszr)
M
Michiharu Ariza 已提交
477 478 479 480 481 482 483
  {
    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);
B
Behdad Esfahbod 已提交
484

M
Michiharu Ariza 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    /* 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])))
B
Behdad Esfahbod 已提交
500
	return_trace (false);
M
Michiharu Ariza 已提交
501 502 503
    }
    return_trace (true);
  }
B
Behdad Esfahbod 已提交
504

M
Michiharu Ariza 已提交
505
  /* used by CFF2 */
M
Michiharu Ariza 已提交
506 507
  template <typename DICTVAL, typename OP_SERIALIZER>
  inline bool serialize (hb_serialize_context_t *c,
B
Behdad Esfahbod 已提交
508 509 510 511 512 513
			unsigned int offSize_,
			const hb_vector_t<DICTVAL> &fontDicts,
			unsigned int fdCount,
			const Remap &fdmap,
			OP_SERIALIZER& opszr,
			const hb_vector_t<TableInfo> &privateInfos)
M
Michiharu Ariza 已提交
514 515 516
  {
    TRACE_SERIALIZE (this);
    if (unlikely (!c->extend_min (*this))) return_trace (false);
M
Michiharu Ariza 已提交
517
    this->count.set (fdCount);
M
Michiharu Ariza 已提交
518 519
    this->offSize.set (offSize_);
    if (!unlikely (c->allocate_size<HBUINT8> (offSize_ * (fdCount + 1))))
M
Michiharu Ariza 已提交
520
      return_trace (false);
B
Behdad Esfahbod 已提交
521

M
Michiharu Ariza 已提交
522 523
    /* serialize font dict offsets */
    unsigned int  offset = 1;
M
Michiharu Ariza 已提交
524 525
    unsigned int  fid = 0;
    for (unsigned i = 0; i < fontDicts.len; i++)
526
      if (fdmap.includes (i))
M
Michiharu Ariza 已提交
527
      {
B
Behdad Esfahbod 已提交
528 529
	CFFIndexOf<COUNT, FontDict>::set_offset_at (fid++, offset);
	offset += FontDict::calculate_serialized_size (fontDicts[i], opszr);
M
Michiharu Ariza 已提交
530
      }
M
Michiharu Ariza 已提交
531
    CFFIndexOf<COUNT, FontDict>::set_offset_at (fid, offset);
M
Michiharu Ariza 已提交
532 533 534

    /* serialize font dicts */
    for (unsigned int i = 0; i < fontDicts.len; i++)
535
      if (fdmap.includes (i))
M
Michiharu Ariza 已提交
536
      {
B
Behdad Esfahbod 已提交
537 538 539
	FontDict *dict = c->start_embed<FontDict> ();
	if (unlikely (!dict->serialize (c, fontDicts[i], opszr, privateInfos[fdmap[i]])))
	  return_trace (false);
M
Michiharu Ariza 已提交
540
      }
M
Michiharu Ariza 已提交
541 542
    return_trace (true);
  }
B
Behdad Esfahbod 已提交
543

M
Michiharu Ariza 已提交
544 545
  /* in parallel to above */
  template <typename OP_SERIALIZER, typename DICTVAL>
M
Michiharu Ariza 已提交
546
  inline static unsigned int calculate_serialized_size (unsigned int &offSize_ /* OUT */,
B
Behdad Esfahbod 已提交
547 548 549 550
							const hb_vector_t<DICTVAL> &fontDicts,
							unsigned int fdCount,
							const Remap &fdmap,
							OP_SERIALIZER& opszr)
M
Michiharu Ariza 已提交
551 552 553
  {
    unsigned int dictsSize = 0;
    for (unsigned int i = 0; i < fontDicts.len; i++)
554
      if (fdmap.includes (i))
B
Behdad Esfahbod 已提交
555
	dictsSize += FontDict::calculate_serialized_size (fontDicts[i], opszr);
M
Michiharu Ariza 已提交
556

557
    offSize_ = calcOffSize (dictsSize);
558
    return CFFIndex<COUNT>::calculate_serialized_size (offSize_, fdCount, dictsSize);
M
Michiharu Ariza 已提交
559 560
  }
};
M
Michiharu Ariza 已提交
561 562 563

/* FDSelect */
struct FDSelect0 {
M
Michiharu Ariza 已提交
564
  inline bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
565 566
  {
    TRACE_SANITIZE (this);
M
Michiharu Ariza 已提交
567 568 569 570
    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)))
B
Behdad Esfahbod 已提交
571
	return_trace (false);
M
Michiharu Ariza 已提交
572 573 574 575 576 577 578

    return_trace (true);
  }

  inline hb_codepoint_t get_fd (hb_codepoint_t glyph) const
  {
    return (hb_codepoint_t)fds[glyph];
579 580
  }

M
Michiharu Ariza 已提交
581 582 583
  inline unsigned int get_size (unsigned int num_glyphs) const
  { return HBUINT8::static_size * num_glyphs; }

M
Michiharu Ariza 已提交
584
  HBUINT8     fds[VAR];
585 586

  DEFINE_SIZE_MIN (1);
M
Michiharu Ariza 已提交
587 588
};

M
Michiharu Ariza 已提交
589 590 591
template <typename GID_TYPE, typename FD_TYPE>
struct FDSelect3_4_Range {
  inline bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
592 593
  {
    TRACE_SANITIZE (this);
M
Michiharu Ariza 已提交
594
    return_trace (likely (c->check_struct (this) && (first < c->get_num_glyphs ()) && (fd < fdcount)));
595 596
  }

M
Michiharu Ariza 已提交
597 598
  GID_TYPE    first;
  FD_TYPE     fd;
599

M
Michiharu Ariza 已提交
600
  DEFINE_SIZE_STATIC (GID_TYPE::static_size + FD_TYPE::static_size);
M
Michiharu Ariza 已提交
601 602
};

M
Michiharu Ariza 已提交
603 604
template <typename GID_TYPE, typename FD_TYPE>
struct FDSelect3_4 {
M
Michiharu Ariza 已提交
605
  inline unsigned int get_size (void) const
M
Michiharu Ariza 已提交
606
  { return GID_TYPE::static_size * 2 + FDSelect3_4_Range<GID_TYPE, FD_TYPE>::static_size * nRanges; }
M
Michiharu Ariza 已提交
607

M
Michiharu Ariza 已提交
608
  inline bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
609 610
  {
    TRACE_SANITIZE (this);
M
Michiharu Ariza 已提交
611 612 613 614 615 616
    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)))
B
Behdad Esfahbod 已提交
617
	return_trace (false);
M
Michiharu Ariza 已提交
618
      if ((0 < i) && unlikely (ranges[i - 1].first >= ranges[i].first))
B
Behdad Esfahbod 已提交
619
	return_trace (false);
M
Michiharu Ariza 已提交
620 621 622 623 624
    }
    if (unlikely (!sentinel().sanitize (c) || (sentinel() != c->get_num_glyphs ())))
      return_trace (false);

    return_trace (true);
625 626
  }

M
Michiharu Ariza 已提交
627 628
  inline hb_codepoint_t get_fd (hb_codepoint_t glyph) const
  {
M
Michiharu Ariza 已提交
629 630
    unsigned int i;
    for (i = 1; i < nRanges; i++)
M
Michiharu Ariza 已提交
631
      if (glyph < ranges[i].first)
B
Behdad Esfahbod 已提交
632
	break;
M
Michiharu Ariza 已提交
633

M
Michiharu Ariza 已提交
634
    return (hb_codepoint_t)ranges[i - 1].fd;
M
Michiharu Ariza 已提交
635
  }
636

M
Michiharu Ariza 已提交
637 638 639
  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]); }

B
Behdad Esfahbod 已提交
640
  GID_TYPE	 nRanges;
M
Michiharu Ariza 已提交
641 642 643
  FDSelect3_4_Range<GID_TYPE, FD_TYPE>  ranges[VAR];
  /* GID_TYPE sentinel */

M
Michiharu Ariza 已提交
644
  DEFINE_SIZE_ARRAY (GID_TYPE::static_size, ranges);
M
Michiharu Ariza 已提交
645 646
};

M
Michiharu Ariza 已提交
647 648 649
typedef FDSelect3_4<HBUINT16, HBUINT8> FDSelect3;
typedef FDSelect3_4_Range<HBUINT16, HBUINT8> FDSelect3_Range;

M
Michiharu Ariza 已提交
650
struct FDSelect {
M
Michiharu Ariza 已提交
651
  inline bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
652 653
  {
    TRACE_SANITIZE (this);
M
Michiharu Ariza 已提交
654

655
    return_trace (likely (c->check_struct (this) && (format == 0 || format == 3) &&
B
Behdad Esfahbod 已提交
656 657 658
			  (format == 0)?
			  u.format0.sanitize (c, fdcount):
			  u.format3.sanitize (c, fdcount)));
659 660
  }

M
Michiharu Ariza 已提交
661 662
  inline bool serialize (hb_serialize_context_t *c, const FDSelect &src, unsigned int num_glyphs)
  {
M
Michiharu Ariza 已提交
663
    TRACE_SERIALIZE (this);
M
Michiharu Ariza 已提交
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
    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);
M
Michiharu Ariza 已提交
679
    else
M
Michiharu Ariza 已提交
680 681 682 683
      size += u.format3.get_size ();
    return size;
  }

M
Michiharu Ariza 已提交
684 685
  inline hb_codepoint_t get_fd (hb_codepoint_t glyph) const
  {
M
Michiharu Ariza 已提交
686 687
    if (this == &Null(FDSelect))
      return 0;
M
Michiharu Ariza 已提交
688 689 690 691 692 693
    if (format == 0)
      return u.format0.get_fd (glyph);
    else
      return u.format3.get_fd (glyph);
  }

M
Michiharu Ariza 已提交
694 695 696 697 698
  HBUINT8       format;
  union {
    FDSelect0   format0;
    FDSelect3   format3;
  } u;
699

M
Michiharu Ariza 已提交
700
  DEFINE_SIZE_MIN (1);
M
Michiharu Ariza 已提交
701 702
};

M
Michiharu Ariza 已提交
703
template <typename COUNT>
704
struct Subrs : CFFIndex<COUNT>
M
Michiharu Ariza 已提交
705
{
706 707
  typedef COUNT count_type;
  typedef CFFIndex<COUNT> SUPER;
M
Michiharu Ariza 已提交
708 709 710 711
};

} /* namespace CFF */

M
Michiharu Ariza 已提交
712
#endif /* HB_OT_CFF_COMMON_HH */