hb-subset-cff1.cc 19.9 KB
Newer Older
M
Michiharu Ariza 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * 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
 */

#include "hb-open-type-private.hh"
28
#include "hb-ot-cff1-table.hh"
M
Michiharu Ariza 已提交
29
#include "hb-set.h"
30
#include "hb-subset-cff1.hh"
M
Michiharu Ariza 已提交
31 32
#include "hb-subset-plan.hh"
#include "hb-subset-cff-common-private.hh"
33
#include "hb-cff1-interp-cs.hh"
M
Michiharu Ariza 已提交
34 35 36

using namespace CFF;

37 38
struct CFF1SubTableOffsets {
  inline CFF1SubTableOffsets (void)
M
Michiharu Ariza 已提交
39 40
  {
    memset (this, 0, sizeof(*this));
M
Michiharu Ariza 已提交
41 42 43
    localSubrsInfos.init ();
  }

44
  inline ~CFF1SubTableOffsets (void)
M
Michiharu Ariza 已提交
45 46
  {
    localSubrsInfos.fini ();
M
Michiharu Ariza 已提交
47 48 49
  }

  unsigned int  nameIndexOffset;
M
Michiharu Ariza 已提交
50
  TableInfo     topDictInfo;
M
Michiharu Ariza 已提交
51
  unsigned int  stringIndexOffset;
M
Michiharu Ariza 已提交
52
  TableInfo     globalSubrsInfo;
M
Michiharu Ariza 已提交
53 54 55
  unsigned int  encodingOffset;
  unsigned int  charsetOffset;
  TableInfo     FDSelectInfo;
M
Michiharu Ariza 已提交
56 57
  TableInfo     FDArrayInfo;
  TableInfo     charStringsInfo;
M
Michiharu Ariza 已提交
58
  TableInfo     privateDictInfo;
M
Michiharu Ariza 已提交
59
  hb_vector_t<TableInfo>  localSubrsInfos;
M
Michiharu Ariza 已提交
60 61
};

62
struct CFF1TopDict_OpSerializer : OpSerializer
M
Michiharu Ariza 已提交
63 64 65
{
  inline bool serialize (hb_serialize_context_t *c,
                         const OpStr &opstr,
66
                         const CFF1SubTableOffsets &offsets) const
M
Michiharu Ariza 已提交
67 68 69 70 71 72 73 74 75 76 77 78
  {
    TRACE_SERIALIZE (this);

    switch (opstr.op)
    {
      case OpCode_charset:
        return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.charsetOffset));

      case OpCode_Encoding:
        return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.encodingOffset));

      case OpCode_CharStrings:
M
Michiharu Ariza 已提交
79
        return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.charStringsInfo.offset));
M
Michiharu Ariza 已提交
80 81

      case OpCode_FDArray:
M
Michiharu Ariza 已提交
82
        return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.FDArrayInfo.offset));
M
Michiharu Ariza 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

      case OpCode_FDSelect:
        return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.FDSelectInfo.offset));

      case OpCode_Private:
        {
          if (unlikely (!UnsizedByteStr::serialize_int2 (c, offsets.privateDictInfo.size)))
            return_trace (false);
          if (unlikely (!UnsizedByteStr::serialize_int4 (c, offsets.privateDictInfo.offset)))
            return_trace (false);
          HBUINT8 *p = c->allocate_size<HBUINT8> (1);
          if (unlikely (p == nullptr)) return_trace (false);
          p->set (OpCode_Private);
        }
        break;

      default:
        return_trace (copy_opstr (c, opstr));
    }
    return_trace (true);
  }

  inline unsigned int calculate_serialized_size (const OpStr &opstr) const
  {
    switch (opstr.op)
    {
      case OpCode_charset:
      case OpCode_Encoding:
      case OpCode_CharStrings:
      case OpCode_FDArray:
      case OpCode_FDSelect:
M
Michiharu Ariza 已提交
114
        return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (opstr.op);
M
Michiharu Ariza 已提交
115 116
    
      case OpCode_Private:
M
Michiharu Ariza 已提交
117
        return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Private);
M
Michiharu Ariza 已提交
118 119 120 121 122 123 124
    
      default:
        return opstr.str.len;
    }
  }
};

125
struct CFF1FontDict_OpSerializer : OpSerializer
M
Michiharu Ariza 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
{
  inline bool serialize (hb_serialize_context_t *c,
                         const OpStr &opstr,
                         const TableInfo &privateDictInfo) const
  {
    TRACE_SERIALIZE (this);

    if (opstr.op == OpCode_Private)
    {
      /* serialize the private dict size as a 2-byte integer */
      if (unlikely (!UnsizedByteStr::serialize_int2 (c, privateDictInfo.size)))
        return_trace (false);

      /* serialize the private dict offset as a 4-byte integer */
      if (unlikely (!UnsizedByteStr::serialize_int4 (c, privateDictInfo.offset)))
        return_trace (false);

      /* serialize the opcode */
      HBUINT8 *p = c->allocate_size<HBUINT8> (1);
      if (unlikely (p == nullptr)) return_trace (false);
      p->set (OpCode_Private);

      return_trace (true);
    }
    else
    {
      HBUINT8 *d = c->allocate_size<HBUINT8> (opstr.str.len);
      if (unlikely (d == nullptr)) return_trace (false);
      memcpy (d, &opstr.str.str[0], opstr.str.len);
    }
    return_trace (true);
  }

  inline unsigned int calculate_serialized_size (const OpStr &opstr) const
  {
    if (opstr.op == OpCode_Private)
M
Michiharu Ariza 已提交
162
      return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Private);
M
Michiharu Ariza 已提交
163 164 165 166 167
    else
      return opstr.str.len;
  }
};

168
struct CFF1PrivateDict_OpSerializer : OpSerializer
M
Michiharu Ariza 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
{
  inline bool serialize (hb_serialize_context_t *c,
                         const OpStr &opstr,
                         const unsigned int subrsOffset) const
  {
    TRACE_SERIALIZE (this);

    if (opstr.op == OpCode_Subrs)
      return_trace (FontDict::serialize_offset2_op(c, OpCode_Subrs, subrsOffset));
    else
      return_trace (copy_opstr (c, opstr));
  }

  inline unsigned int calculate_serialized_size (const OpStr &opstr) const
  {
    if (opstr.op == OpCode_Subrs)
      return OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Subrs);
    else
      return opstr.str.len;
  }
};

191
struct CFF1CSOpSet_SubrSubset : CFF1CSOpSet<SubrRefMapPair>
M
Michiharu Ariza 已提交
192
{
193
  static inline bool process_op (OpCode op, CFF1CSInterpEnv &env, SubrRefMapPair& refMapPair)
M
Michiharu Ariza 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  {
    unsigned int  subr_num;
    switch (op) {
      case OpCode_callsubr:
        if (!unlikely (env.popSubrNum(env.localSubrs, subr_num)))
          return false;
        env.argStack.unpop ();
        refMapPair.local_map->add (subr_num);
        break;
      case OpCode_callgsubr:
        if (!unlikely (env.popSubrNum(env.globalSubrs, subr_num)))
          return false;
        env.argStack.unpop ();
        refMapPair.global_map->add (subr_num);
        break;
      default:
        break;
    }
212
    return CFF1CSOpSet<SubrRefMapPair>::process_op (op, env, refMapPair);
M
Michiharu Ariza 已提交
213 214 215
  }
};

M
Michiharu Ariza 已提交
216 217 218 219 220
struct cff_subset_plan {
  inline cff_subset_plan (void)
    : final_size (0),
      orig_fdcount (0),
      subst_fdcount(1),
M
Michiharu Ariza 已提交
221 222
      subst_fdselect_format (0),
      offsets()
M
Michiharu Ariza 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  {
    topdict_sizes.init ();
    topdict_sizes.resize (1);
    subst_fdselect_first_glyphs.init ();
    fdmap.init ();
    subset_charstrings.init ();
    privateDictInfos.init ();
  }

  inline ~cff_subset_plan (void)
  {
    topdict_sizes.fini ();
    subst_fdselect_first_glyphs.fini ();
    fdmap.fini ();
    subset_charstrings.fini ();
    privateDictInfos.fini ();
M
Michiharu Ariza 已提交
239
    subrRefMaps.fini ();
M
Michiharu Ariza 已提交
240 241
  }

242
  inline bool create (const OT::cff1::accelerator_subset_t &acc,
M
Michiharu Ariza 已提交
243 244 245 246 247 248
                      hb_subset_plan_t *plan)
  {
    final_size = 0;
    orig_fdcount = acc.fdCount;

    /* CFF header */
249
    final_size += OT::cff1::static_size;
M
Michiharu Ariza 已提交
250 251 252 253 254 255 256
    
    /* Name INDEX */
    offsets.nameIndexOffset = final_size;
    final_size += acc.nameIndex->get_size ();
    
    /* top dict INDEX */
    {
M
Michiharu Ariza 已提交
257
      offsets.topDictInfo.offset = final_size;
258
      CFF1TopDict_OpSerializer topSzr;
M
Michiharu Ariza 已提交
259
      unsigned int topDictSize = TopDict::calculate_serialized_size (acc.topDicts[0], topSzr);
M
Michiharu Ariza 已提交
260
      offsets.topDictInfo.offSize = calcOffSize(topDictSize);
261
      final_size += CFF1IndexOf<TopDict>::calculate_serialized_size<CFF1TopDictValues> (offsets.topDictInfo.offSize, acc.topDicts, topdict_sizes, topSzr);
M
Michiharu Ariza 已提交
262 263 264 265 266 267
    }

    /* String INDEX */
    offsets.stringIndexOffset = final_size;
    final_size += acc.stringIndex->get_size ();
    
M
Michiharu Ariza 已提交
268 269
    /* Subset global & local subrs */
    {
270
      SubrSubsetter<const OT::cff1::accelerator_subset_t, CFF1CSInterpEnv, CFF1CSOpSet_SubrSubset> subsetter(acc, plan->glyphs);
M
Michiharu Ariza 已提交
271 272 273 274 275 276 277 278 279 280
      if (!subsetter.collect_refs (subrRefMaps))
        return false;
      
      offsets.globalSubrsInfo.size = acc.globalSubrs->calculate_serialized_size (offsets.globalSubrsInfo.offSize, subrRefMaps.global_map, 1);
      if (!offsets.localSubrsInfos.resize (orig_fdcount))
        return false;
      for (unsigned int i = 0; i < orig_fdcount; i++)
        offsets.localSubrsInfos[i].size = acc.privateDicts[i].localSubrs->calculate_serialized_size (offsets.localSubrsInfos[i].offSize, subrRefMaps.local_maps[i], 1);
    }
    
M
Michiharu Ariza 已提交
281
    /* global subrs */
M
Michiharu Ariza 已提交
282 283
    offsets.globalSubrsInfo.offset = final_size;
    final_size += offsets.globalSubrsInfo.size;
M
Michiharu Ariza 已提交
284 285 286 287 288 289 290 291 292 293 294 295

    /* Encoding */
    offsets.encodingOffset = final_size;
    if (acc.encoding != &Null(Encoding))
      final_size += acc.encoding->get_size ();

    /* Charset */
    offsets.charsetOffset = final_size;
    if (acc.charset != &Null(Charset))
      final_size += acc.charset->get_size (acc.num_glyphs);

    /* FDSelect */
296
    if (acc.fdSelect != &Null(CFF1FDSelect))
M
Michiharu Ariza 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
    {
      offsets.FDSelectInfo.offset = final_size;
      if (unlikely (!hb_plan_subset_cff_fdselect (plan->glyphs,
                                  orig_fdcount,
                                  *acc.fdSelect,
                                  subst_fdcount,
                                  offsets.FDSelectInfo.size,
                                  subst_fdselect_format,
                                  subst_fdselect_first_glyphs,
                                  fdmap)))
        return false;
      
      if (!is_fds_subsetted ())
        offsets.FDSelectInfo.size = acc.fdSelect->calculate_serialized_size (acc.num_glyphs);
      final_size += offsets.FDSelectInfo.size;
    }

    /* FDArray (FDIndex) */
315
    if (acc.fdArray != &Null(CFF1FDArray)) {
M
Michiharu Ariza 已提交
316
      offsets.FDArrayInfo.offset = final_size;
317 318
      CFF1FontDict_OpSerializer fontSzr;
      final_size += CFF1FDArray::calculate_serialized_size(offsets.FDArrayInfo.offSize/*OUT*/, acc.fontDicts, subst_fdcount, fdmap, fontSzr);
M
Michiharu Ariza 已提交
319 320 321 322
    }

    /* CharStrings */
    {
M
Michiharu Ariza 已提交
323
      offsets.charStringsInfo.offset = final_size;
M
Michiharu Ariza 已提交
324 325 326 327 328 329 330
      unsigned int dataSize = 0;
      for (unsigned int i = 0; i < plan->glyphs.len; i++)
      {
        const ByteStr str = (*acc.charStrings)[plan->glyphs[i]];
        subset_charstrings.push (str);
        dataSize += str.len;
      }
M
Michiharu Ariza 已提交
331
      offsets.charStringsInfo.offSize = calcOffSize (dataSize + 1);
332
      final_size += CFF1CharStrings::calculate_serialized_size (offsets.charStringsInfo.offSize, plan->glyphs.len, dataSize);
M
Michiharu Ariza 已提交
333 334 335 336 337 338
    }

    /* private dicts & local subrs */
    offsets.privateDictInfo.offset = final_size;
    for (unsigned int i = 0; i < orig_fdcount; i++)
    {
339 340
      if (!fdmap.excludes (i))
      {
341
        CFF1PrivateDict_OpSerializer privSzr;
M
Michiharu Ariza 已提交
342
        TableInfo  privInfo = { final_size, PrivateDict::calculate_serialized_size (acc.privateDicts[i], privSzr), 0 };
343
        privateDictInfos.push (privInfo);
M
Michiharu Ariza 已提交
344
        final_size += privInfo.size + offsets.localSubrsInfos[i].size;
345
      }
M
Michiharu Ariza 已提交
346 347 348 349 350 351 352 353 354 355 356 357
    }

    if (!acc.is_CID ())
      offsets.privateDictInfo = privateDictInfos[0];

    return true;
  }

  inline unsigned int get_final_size (void) const  { return final_size; }

  unsigned int        final_size;
  hb_vector_t<unsigned int> topdict_sizes;
358
  CFF1SubTableOffsets  offsets;
M
Michiharu Ariza 已提交
359 360 361 362 363 364 365 366 367

  unsigned int    orig_fdcount;
  unsigned int    subst_fdcount;
  inline bool     is_fds_subsetted (void) const { return subst_fdcount < orig_fdcount; }
  unsigned int    subst_fdselect_format;
  hb_vector_t<hb_codepoint_t>   subst_fdselect_first_glyphs;

  /* font dict index remap table from fullset FDArray to subset FDArray.
   * set to HB_SET_VALUE_INVALID if excluded from subset */
368
  FDMap   fdmap;
M
Michiharu Ariza 已提交
369 370 371

  hb_vector_t<ByteStr> subset_charstrings;
  hb_vector_t<TableInfo> privateDictInfos;
M
Michiharu Ariza 已提交
372 373

  SubrRefMaps             subrRefMaps;
M
Michiharu Ariza 已提交
374 375
};

376
static inline bool _write_cff1 (const cff_subset_plan &plan,
377
                                const OT::cff1::accelerator_subset_t  &acc,
M
Michiharu Ariza 已提交
378 379 380 381 382 383
                                const hb_vector_t<hb_codepoint_t>& glyphs,
                                unsigned int dest_sz,
                                void *dest)
{
  hb_serialize_context_t c (dest, dest_sz);

M
Michiharu Ariza 已提交
384
  char RETURN_OP[1] = { OpCode_return };
M
Michiharu Ariza 已提交
385
  const ByteStr NULL_SUBR (RETURN_OP, 1);
M
Michiharu Ariza 已提交
386

387
  OT::cff1 *cff = c.start_serialize<OT::cff1> ();
M
Michiharu Ariza 已提交
388 389 390 391 392 393 394 395 396 397 398 399
  if (unlikely (!c.extend_min (*cff)))
    return false;

  /* header */
  cff->version.major.set (0x01);
  cff->version.minor.set (0x00);
  cff->nameIndex.set (cff->min_size);
  cff->offSize.set (4); /* unused? */

  /* name INDEX */
  {
    assert (cff->nameIndex == c.head - c.start);
400
    CFF1NameIndex *dest = c.start_embed<CFF1NameIndex> ();
M
Michiharu Ariza 已提交
401 402 403 404 405 406 407 408 409 410
    if (unlikely (dest == nullptr)) return false;
    if (unlikely (!dest->serialize (&c, *acc.nameIndex)))
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF name INDEX");
      return false;
    }
  }

  /* top dict INDEX */
  {
M
Michiharu Ariza 已提交
411
    assert (plan.offsets.topDictInfo.offset == c.head - c.start);
412
    CFF1IndexOf<TopDict> *dest = c.start_embed< CFF1IndexOf<TopDict> > ();
M
Michiharu Ariza 已提交
413
    if (dest == nullptr) return false;
414
    CFF1TopDict_OpSerializer topSzr;
M
Michiharu Ariza 已提交
415
    if (unlikely (!dest->serialize (&c, plan.offsets.topDictInfo.offSize, acc.topDicts, plan.topdict_sizes, topSzr, plan.offsets)))
M
Michiharu Ariza 已提交
416 417 418 419 420 421 422 423 424
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF top dict");
      return false;
    }
  }

  /* String INDEX */
  {
    assert (plan.offsets.stringIndexOffset == c.head - c.start);
425
    CFF1StringIndex *dest = c.start_embed<CFF1StringIndex> ();
M
Michiharu Ariza 已提交
426 427 428 429 430 431 432 433 434 435
    if (unlikely (dest == nullptr)) return false;
    if (unlikely (!dest->serialize (&c, *acc.stringIndex)))
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF string INDEX");
      return false;
    }
  }

  /* global subrs */
  {
M
Michiharu Ariza 已提交
436
    assert (plan.offsets.globalSubrsInfo.offset == c.head - c.start);
437
    CFF1Subrs *dest = c.start_embed<CFF1Subrs> ();
M
Michiharu Ariza 已提交
438
    if (unlikely (dest == nullptr)) return false;
M
Michiharu Ariza 已提交
439
    if (unlikely (!dest->serialize (&c, *acc.globalSubrs, plan.offsets.globalSubrsInfo.offSize, plan.subrRefMaps.global_map, NULL_SUBR)))
M
Michiharu Ariza 已提交
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF global subrs");
      return false;
    }
  }

  /* Encoding */
  if (acc.encoding != &Null(Encoding)){
    assert (plan.offsets.encodingOffset == c.head - c.start);
    Encoding *dest = c.start_embed<Encoding> ();
    if (unlikely (dest == nullptr)) return false;
    if (unlikely (!dest->serialize (&c, *acc.encoding, acc.num_glyphs)))  // XXX: TODO
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize Encoding");
      return false;
    }
  }

  /* Charset */
  if (acc.charset != &Null(Charset))
  {
    assert (plan.offsets.charsetOffset == c.head - c.start);
    Charset *dest = c.start_embed<Charset> ();
    if (unlikely (dest == nullptr)) return false;
    if (unlikely (!dest->serialize (&c, *acc.charset, acc.num_glyphs)))  // XXX: TODO
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize Charset");
      return false;
    }
  }

  /* FDSelect */
472
  if (acc.fdSelect != &Null(CFF1FDSelect))
M
Michiharu Ariza 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
  {
    assert (plan.offsets.FDSelectInfo.offset == c.head - c.start);
    
    if (plan.is_fds_subsetted ())
    {
      if (unlikely (!hb_serialize_cff_fdselect (&c, glyphs, *acc.fdSelect, acc.fdCount,
                                                plan.subst_fdselect_format, plan.offsets.FDSelectInfo.size,
                                                plan.subst_fdselect_first_glyphs,
                                                plan.fdmap)))
      {
        DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF subset FDSelect");
        return false;
      }
    }
    else
    {
489
      CFF1FDSelect *dest = c.start_embed<CFF1FDSelect> ();
M
Michiharu Ariza 已提交
490 491 492 493 494 495 496 497 498
      if (unlikely (!dest->serialize (&c, *acc.fdSelect, acc.num_glyphs)))
      {
        DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF FDSelect");
        return false;
      }
    }
  }

  /* FDArray (FD Index) */
499
  if (acc.fdArray != &Null(CFF1FDArray))
M
Michiharu Ariza 已提交
500
  {
M
Michiharu Ariza 已提交
501
    assert (plan.offsets.FDArrayInfo.offset == c.head - c.start);
502
    CFF1FDArray  *fda = c.start_embed<CFF1FDArray> ();
M
Michiharu Ariza 已提交
503
    if (unlikely (fda == nullptr)) return false;
504
    CFF1FontDict_OpSerializer  fontSzr;
M
Michiharu Ariza 已提交
505
    if (unlikely (!fda->serialize (&c, plan.offsets.FDArrayInfo.offSize,
M
Michiharu Ariza 已提交
506 507 508 509 510 511 512 513 514 515
                                   acc.fontDicts, plan.subst_fdcount, plan.fdmap,
                                   fontSzr, plan.privateDictInfos)))
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF FDArray");
      return false;
    }
  }

  /* CharStrings */
  {
M
Michiharu Ariza 已提交
516
    assert (plan.offsets.charStringsInfo.offset == c.head - c.start);
517
    CFF1CharStrings  *cs = c.start_embed<CFF1CharStrings> ();
M
Michiharu Ariza 已提交
518
    if (unlikely (cs == nullptr)) return false;
M
Michiharu Ariza 已提交
519
    if (unlikely (!cs->serialize (&c, plan.offsets.charStringsInfo.offSize, plan.subset_charstrings)))
M
Michiharu Ariza 已提交
520 521 522 523 524 525 526 527 528 529
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF CharStrings");
      return false;
    }
  }

  /* private dicts & local subrs */
  assert (plan.offsets.privateDictInfo.offset == c.head - c.start);
  for (unsigned int i = 0; i < acc.privateDicts.len; i++)
  {
530
    if (!plan.fdmap.excludes (i))
M
Michiharu Ariza 已提交
531
    {
532 533
      PrivateDict  *pd = c.start_embed<PrivateDict> ();
      if (unlikely (pd == nullptr)) return false;
534
      CFF1PrivateDict_OpSerializer privSzr;
535 536
      /* N.B. local subrs immediately follows its corresponding private dict. i.e., subr offset == private dict size */
      if (unlikely (!pd->serialize (&c, acc.privateDicts[i], privSzr, plan.privateDictInfos[plan.fdmap[i]].size)))
M
Michiharu Ariza 已提交
537
      {
538
        DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF Private Dict[%d]", i);
M
Michiharu Ariza 已提交
539 540
        return false;
      }
541
      if (acc.privateDicts[i].subrsOffset != 0)
M
Michiharu Ariza 已提交
542
      {
543 544
        CFF1Subrs *subrs = c.start_embed<CFF1Subrs> ();
        if (unlikely (subrs == nullptr) || acc.privateDicts[i].localSubrs == &Null(CFF1Subrs))
545 546 547 548
        {
          DEBUG_MSG (SUBSET, nullptr, "CFF subset: local subrs unexpectedly null [%d]", i);
          return false;
        }
M
Michiharu Ariza 已提交
549
        if (unlikely (!subrs->serialize (&c, *acc.privateDicts[i].localSubrs, plan.offsets.localSubrsInfos[i].offSize, plan.subrRefMaps.local_maps[i], NULL_SUBR)))
550 551 552 553
        {
          DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF local subrs [%d]", i);
          return false;
        }
M
Michiharu Ariza 已提交
554 555 556 557
      }
    }
  }

M
Michiharu Ariza 已提交
558
  assert (c.head == c.end);
M
Michiharu Ariza 已提交
559 560 561 562 563 564
  c.end_serialize ();

  return true;
}

static bool
565
_hb_subset_cff1 (const OT::cff1::accelerator_subset_t  &acc,
M
Michiharu Ariza 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
                const char                      *data,
                hb_subset_plan_t                *plan,
                hb_blob_t                       **prime /* OUT */)
{
  cff_subset_plan cff_plan;

  if (unlikely (!cff_plan.create (acc, plan)))
  {
    DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff subsetting plan.");
    return false;
  }

  unsigned int  cff_prime_size = cff_plan.get_final_size ();
  char *cff_prime_data = (char *) calloc (1, cff_prime_size);

581
  if (unlikely (!_write_cff1 (cff_plan, acc, plan->glyphs,
M
Michiharu Ariza 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
                              cff_prime_size, cff_prime_data))) {
    DEBUG_MSG(SUBSET, nullptr, "Failed to write a subset cff.");
    free (cff_prime_data);
    return false;
  }

  *prime = hb_blob_create (cff_prime_data,
                           cff_prime_size,
                           HB_MEMORY_MODE_READONLY,
                           cff_prime_data,
                           free);
  return true;
}

/**
597
 * hb_subset_cff1:
M
Michiharu Ariza 已提交
598 599 600 601 602
 * Subsets the CFF table according to a provided plan.
 *
 * Return value: subsetted cff table.
 **/
bool
603
hb_subset_cff1 (hb_subset_plan_t *plan,
M
Michiharu Ariza 已提交
604 605
                hb_blob_t       **prime /* OUT */)
{
606
  hb_blob_t *cff_blob = hb_sanitize_context_t().reference_table<CFF::cff1> (plan->source);
M
Michiharu Ariza 已提交
607 608
  const char *data = hb_blob_get_data(cff_blob, nullptr);

609
  OT::cff1::accelerator_subset_t acc;
M
Michiharu Ariza 已提交
610 611
  acc.init(plan->source);
  bool result = likely (acc.is_valid ()) &&
612
                        _hb_subset_cff1 (acc, data, plan, prime);
M
Michiharu Ariza 已提交
613 614 615 616 617
  hb_blob_destroy (cff_blob);
  acc.fini ();

  return result;
}