hb-subset-cff2.cc 17.0 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
/*
 * 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
 */

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

M
Michiharu Ariza 已提交
35 36
using namespace CFF;

M
Michiharu Ariza 已提交
37 38
struct CFF2SubTableOffsets : CFFSubTableOffsets
{
M
Michiharu Ariza 已提交
39
  inline CFF2SubTableOffsets (void)
M
Michiharu Ariza 已提交
40 41 42
    : CFFSubTableOffsets (),
      varStoreOffset (0)
  {}
M
Michiharu Ariza 已提交
43

M
Michiharu Ariza 已提交
44 45 46
  unsigned int  varStoreOffset;
};

M
Michiharu Ariza 已提交
47
struct CFF2TopDict_OpSerializer : CFFTopDict_OpSerializer
M
Michiharu Ariza 已提交
48
{
M
Michiharu Ariza 已提交
49 50
  inline bool serialize (hb_serialize_context_t *c,
                         const OpStr &opstr,
M
Michiharu Ariza 已提交
51
                         const CFF2SubTableOffsets &offsets) const
M
Michiharu Ariza 已提交
52 53
  {
    TRACE_SERIALIZE (this);
M
Michiharu Ariza 已提交
54

M
Michiharu Ariza 已提交
55 56 57 58 59 60
    switch (opstr.op)
    {
      case OpCode_vstore:
        return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.varStoreOffset));

      default:
M
Michiharu Ariza 已提交
61
        return_trace (CFFTopDict_OpSerializer::serialize (c, opstr, offsets));
M
Michiharu Ariza 已提交
62 63 64 65 66 67 68 69
    }
  }

  inline unsigned int calculate_serialized_size (const OpStr &opstr) const
  {
    switch (opstr.op)
    {
      case OpCode_vstore:
M
Michiharu Ariza 已提交
70
        return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (opstr.op);
M
Michiharu Ariza 已提交
71 72
    
      default:
M
Michiharu Ariza 已提交
73
        return CFFTopDict_OpSerializer::calculate_serialized_size (opstr);
M
Michiharu Ariza 已提交
74 75 76 77
    }
  }
};

78
struct CFF2CSOpSet_Flatten : CFF2CSOpSet<CFF2CSOpSet_Flatten, FlattenParam>
M
Michiharu Ariza 已提交
79
{
80
  static inline bool process_op (OpCode op, CFF2CSInterpEnv &env, FlattenParam& param)
M
Michiharu Ariza 已提交
81
  {
82 83 84 85 86 87
    if (param.drop_hints && CSOPSET::is_hint_op (op))
    {
      env.clear_stack ();
      return true;
    }
    if (unlikely (!SUPER::process_op (op, env, param)))
M
Michiharu Ariza 已提交
88 89
      return false;
    switch (op)
M
Michiharu Ariza 已提交
90
    {
M
Michiharu Ariza 已提交
91 92
      case OpCode_hintmask:
      case OpCode_cntrmask:
93 94 95 96 97 98
        if (param.drop_hints)
        {
          env.clear_stack ();
          return true;
        }
        if (unlikely (!param.flatStr.encode_op (op)))
M
Michiharu Ariza 已提交
99 100
          return false;
        for (int i = -env.hintmask_size; i < 0; i++)
101
          if (unlikely (!param.flatStr.encode_byte (env.substr[i])))
M
Michiharu Ariza 已提交
102 103 104 105 106 107 108 109 110
            return false;
        break;
      case OpCode_return:
      case OpCode_endchar:
        /* dummy opcodes in CFF2. ignore */
        break;
      default:
        if (!CSOPSET::is_subr_op (op) &&
            !CSOPSET::is_arg_op (op))
111
          return param.flatStr.encode_op (op);
M
Michiharu Ariza 已提交
112
    }
M
Michiharu Ariza 已提交
113
    return true;
M
Michiharu Ariza 已提交
114 115
  }

116
  static inline bool process_blend (CFF2CSInterpEnv &env, FlattenParam& param)
M
Michiharu Ariza 已提交
117
  {
118
    flush_stack (env, param);
M
Michiharu Ariza 已提交
119
    return true;
M
Michiharu Ariza 已提交
120 121
  }

122
  static inline bool process_vsindex (CFF2CSInterpEnv &env, FlattenParam& param)
M
Michiharu Ariza 已提交
123
  {
124
    flush_stack (env, param);
M
Michiharu Ariza 已提交
125
    return true;
M
Michiharu Ariza 已提交
126 127
  }

128
  static inline void flush_stack (CFF2CSInterpEnv &env, FlattenParam& param)
129
  {
M
Michiharu Ariza 已提交
130
    for (unsigned int i = 0; i < env.argStack.size; i++)
131 132
      param.flatStr.encode_num (env.argStack.elements[i]);
    SUPER::flush_stack (env, param);
133 134
  }

M
Michiharu Ariza 已提交
135
  private:
136 137
  typedef CFF2CSOpSet<CFF2CSOpSet_Flatten, FlattenParam> SUPER;
  typedef CSOpSet<CFF2CSOpSet_Flatten, CFF2CSInterpEnv, FlattenParam> CSOPSET;
138 139
};

M
Michiharu Ariza 已提交
140
struct CFF2CSOpSet_SubsetSubrs : CFF2CSOpSet<CFF2CSOpSet_SubsetSubrs, SubrRefMapPair>
M
Michiharu Ariza 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
{
  static inline bool process_op (OpCode op, CFF2CSInterpEnv &env, SubrRefMapPair& refMapPair)
  {
    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;
    }
M
Michiharu Ariza 已提交
161
    return CFF2CSOpSet<CFF2CSOpSet_SubsetSubrs, SubrRefMapPair>::process_op (op, env, refMapPair);
M
Michiharu Ariza 已提交
162 163 164
  }
};

M
Michiharu Ariza 已提交
165 166
struct cff2_subset_plan {
  inline cff2_subset_plan (void)
M
Michiharu Ariza 已提交
167
    : final_size (0),
M
Michiharu Ariza 已提交
168 169
      orig_fdcount (0),
      subst_fdcount(1),
M
Michiharu Ariza 已提交
170 171 172
      subst_fdselect_format (0),
      flatten_subrs (true),
      drop_hints (false)
M
Michiharu Ariza 已提交
173
  {
M
Michiharu Ariza 已提交
174 175
    subst_fdselect_first_glyphs.init ();
    fdmap.init ();
M
Michiharu Ariza 已提交
176
    subset_charstrings.init ();
M
Michiharu Ariza 已提交
177
    flat_charstrings.init ();
M
Michiharu Ariza 已提交
178
    privateDictInfos.init ();
M
Michiharu Ariza 已提交
179
    subrRefMaps.init ();
M
Michiharu Ariza 已提交
180 181
  }

M
Michiharu Ariza 已提交
182
  inline ~cff2_subset_plan (void)
M
Michiharu Ariza 已提交
183
  {
M
Michiharu Ariza 已提交
184 185
    subst_fdselect_first_glyphs.fini ();
    fdmap.fini ();
M
Michiharu Ariza 已提交
186
    subset_charstrings.fini ();
M
Michiharu Ariza 已提交
187
    flat_charstrings.fini ();
M
Michiharu Ariza 已提交
188
    privateDictInfos.fini ();
M
Michiharu Ariza 已提交
189
    subrRefMaps.fini ();
M
Michiharu Ariza 已提交
190 191
  }

192
  inline bool create (const OT::cff2::accelerator_subset_t &acc,
M
Michiharu Ariza 已提交
193 194 195
              hb_subset_plan_t *plan)
  {
    final_size = 0;
M
Michiharu Ariza 已提交
196
    orig_fdcount = acc.fdArray->count;
M
Michiharu Ariza 已提交
197

198 199
    drop_hints = plan->drop_hints;

M
Michiharu Ariza 已提交
200 201 202 203 204 205
    /* CFF2 header */
    final_size += OT::cff2::static_size;
    
    /* top dict */
    {
      CFF2TopDict_OpSerializer topSzr;
M
Michiharu Ariza 已提交
206 207
      offsets.topDictInfo.size = TopDict::calculate_serialized_size (acc.topDict, topSzr);
      final_size += offsets.topDictInfo.size;
M
Michiharu Ariza 已提交
208 209
    }

M
Michiharu Ariza 已提交
210 211 212
    if (flatten_subrs)
    {
      /* Flatten global & local subrs */
213 214
      SubrFlattener<const OT::cff2::accelerator_subset_t, CFF2CSInterpEnv, CFF2CSOpSet_Flatten>
                    flattener(acc, plan->glyphs, plan->drop_hints);
M
Michiharu Ariza 已提交
215 216 217 218 219 220 221
      if (!flattener.flatten (flat_charstrings))
        return false;
      
      /* no global/local subroutines */
      offsets.globalSubrsInfo.size = HBUINT32::static_size; /* count 0 only */
    }
    else
M
Michiharu Ariza 已提交
222
    {
M
Michiharu Ariza 已提交
223
      /* Subset global & local subrs */
M
Michiharu Ariza 已提交
224
      SubrSubsetter<const OT::cff2::accelerator_subset_t, CFF2CSInterpEnv, CFF2CSOpSet_SubsetSubrs> subsetter(acc, plan->glyphs);
M
Michiharu Ariza 已提交
225 226 227 228 229 230 231 232 233 234
      if (!subsetter.collect_refs (subrRefMaps))
        return false;
      
      offsets.globalSubrsInfo.size = acc.globalSubrs->calculate_serialized_size (offsets.globalSubrsInfo.offSize, subrRefMaps.global_map);
      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]);
    }
    
M
Michiharu Ariza 已提交
235
    /* global subrs */
M
Michiharu Ariza 已提交
236 237
    offsets.globalSubrsInfo.offset = final_size;
    final_size += offsets.globalSubrsInfo.size;
M
Michiharu Ariza 已提交
238 239 240 241 242 243 244 245 246

    /* variation store */
    if (acc.varStore != &Null(CFF2VariationStore))
    {
      offsets.varStoreOffset = final_size;
      final_size += acc.varStore->get_size ();
    }

    /* FDSelect */
M
Michiharu Ariza 已提交
247
    if (acc.fdSelect != &Null(CFF2FDSelect))
M
Michiharu Ariza 已提交
248
    {
M
Michiharu Ariza 已提交
249
      offsets.FDSelectInfo.offset = final_size;
M
Michiharu Ariza 已提交
250 251
      if (unlikely (!hb_plan_subset_cff_fdselect (plan->glyphs,
                                  orig_fdcount,
M
Michiharu Ariza 已提交
252
                                  *(const FDSelect *)acc.fdSelect,
M
Michiharu Ariza 已提交
253
                                  subst_fdcount,
M
Michiharu Ariza 已提交
254
                                  offsets.FDSelectInfo.size,
M
Michiharu Ariza 已提交
255 256 257 258 259 260
                                  subst_fdselect_format,
                                  subst_fdselect_first_glyphs,
                                  fdmap)))
        return false;
      
      if (!is_fds_subsetted ())
M
Michiharu Ariza 已提交
261 262
        offsets.FDSelectInfo.size = acc.fdSelect->calculate_serialized_size (acc.num_glyphs);
      final_size += offsets.FDSelectInfo.size;
M
Michiharu Ariza 已提交
263 264 265 266
    }

    /* FDArray (FDIndex) */
    {
M
Michiharu Ariza 已提交
267
      offsets.FDArrayInfo.offset = final_size;
M
Michiharu Ariza 已提交
268
      CFFFontDict_OpSerializer fontSzr;
M
Michiharu Ariza 已提交
269
      final_size += CFF2FDArray::calculate_serialized_size(offsets.FDArrayInfo.offSize/*OUT*/, acc.fontDicts, subst_fdcount, fdmap, fontSzr);
M
Michiharu Ariza 已提交
270 271 272 273
    }

    /* CharStrings */
    {
M
Michiharu Ariza 已提交
274
      offsets.charStringsInfo.offset = final_size;
M
Michiharu Ariza 已提交
275 276 277
      unsigned int dataSize = 0;
      for (unsigned int i = 0; i < plan->glyphs.len; i++)
      {
M
Michiharu Ariza 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290
        if (flatten_subrs)
        {
          ByteStrBuff &flatstr = flat_charstrings[i];
          ByteStr str (&flatstr[0], flatstr.len);
          subset_charstrings.push (str);
          dataSize += flatstr.len;
        }
        else
        {
          const ByteStr str = (*acc.charStrings)[plan->glyphs[i]];
          subset_charstrings.push (str);
          dataSize += str.len;
        }
M
Michiharu Ariza 已提交
291
      }
M
Michiharu Ariza 已提交
292 293
      offsets.charStringsInfo.offSize = calcOffSize (dataSize + 1);
      final_size += CFF2CharStrings::calculate_serialized_size (offsets.charStringsInfo.offSize, plan->glyphs.len, dataSize);
M
Michiharu Ariza 已提交
294 295 296 297
    }

    /* private dicts & local subrs */
    offsets.privateDictsOffset = final_size;
M
Michiharu Ariza 已提交
298
    for (unsigned int i = 0; i < orig_fdcount; i++)
M
Michiharu Ariza 已提交
299
    {
300 301
      if (!fdmap.excludes (i))
      {
302
        unsigned int priv_size;
M
Michiharu Ariza 已提交
303 304
        CFFPrivateDict_OpSerializer privSzr (drop_hints, flatten_subrs);
        priv_size = PrivateDict::calculate_serialized_size (acc.privateDicts[i], privSzr);
305
        TableInfo  privInfo = { final_size, priv_size, 0 };
306
        privateDictInfos.push (privInfo);
M
Michiharu Ariza 已提交
307 308 309
        final_size += privInfo.size;
        if (!flatten_subrs)
          final_size += offsets.localSubrsInfos[i].size;
310
      }
M
Michiharu Ariza 已提交
311 312 313 314 315 316 317
    }

    return true;
  }

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

M
Michiharu Ariza 已提交
318 319
  unsigned int        final_size;
  CFF2SubTableOffsets offsets;
M
Michiharu Ariza 已提交
320

M
Michiharu Ariza 已提交
321 322 323 324 325 326
  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;

327
  FDMap   fdmap;
M
Michiharu Ariza 已提交
328

M
Michiharu Ariza 已提交
329 330 331
  hb_vector_t<ByteStr>    subset_charstrings;
  ByteStrBuffArray        flat_charstrings;
  hb_vector_t<TableInfo>  privateDictInfos;
M
Michiharu Ariza 已提交
332 333

  SubrRefMaps             subrRefMaps;
334

M
Michiharu Ariza 已提交
335
  bool            flatten_subrs;
336
  bool            drop_hints;
M
Michiharu Ariza 已提交
337 338
};

M
Michiharu Ariza 已提交
339
static inline bool _write_cff2 (const cff2_subset_plan &plan,
340
                                const OT::cff2::accelerator_subset_t  &acc,
M
Michiharu Ariza 已提交
341
                                const hb_vector_t<hb_codepoint_t>& glyphs,
M
Michiharu Ariza 已提交
342 343
                                unsigned int dest_sz,
                                void *dest)
M
Michiharu Ariza 已提交
344
{
M
Michiharu Ariza 已提交
345 346 347 348
  hb_serialize_context_t c (dest, dest_sz);

  OT::cff2 *cff2 = c.start_serialize<OT::cff2> ();
  if (unlikely (!c.extend_min (*cff2)))
349
    return false;
M
Michiharu Ariza 已提交
350 351 352 353 354 355 356 357 358

  /* header */
  cff2->version.major.set (0x02);
  cff2->version.minor.set (0x00);
  cff2->topDict.set (OT::cff2::static_size);

  /* top dict */
  {
    assert (cff2->topDict == c.head - c.start);
M
Michiharu Ariza 已提交
359
    cff2->topDictSize.set (plan.offsets.topDictInfo.size);
M
Michiharu Ariza 已提交
360 361
    TopDict &dict = cff2 + cff2->topDict;
    CFF2TopDict_OpSerializer topSzr;
M
Michiharu Ariza 已提交
362
    if (unlikely (!dict.serialize (&c, acc.topDict, topSzr, plan.offsets)))
M
Michiharu Ariza 已提交
363 364
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 top dict");
365
      return false;
M
Michiharu Ariza 已提交
366 367 368 369 370
    }
  }

  /* global subrs */
  {
M
Michiharu Ariza 已提交
371
    assert (cff2->topDict + plan.offsets.topDictInfo.size == c.head - c.start);
M
Michiharu Ariza 已提交
372
    CFF2Subrs *dest = c.start_embed<CFF2Subrs> ();
373
    if (unlikely (dest == nullptr)) return false;
M
Michiharu Ariza 已提交
374
    if (unlikely (!dest->serialize (&c, *acc.globalSubrs, plan.offsets.globalSubrsInfo.offSize, plan.subrRefMaps.global_map)))
M
Michiharu Ariza 已提交
375 376
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 global subrs");
377
      return false;
M
Michiharu Ariza 已提交
378 379 380 381 382 383 384 385 386 387 388
    }
  }
  
  /* variation store */
  if (acc.varStore != &Null(CFF2VariationStore))
  {
    assert (plan.offsets.varStoreOffset == c.head - c.start);
    CFF2VariationStore *dest = c.start_embed<CFF2VariationStore> ();
    if (unlikely (!dest->serialize (&c, acc.varStore)))
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 Variation Store");
389
      return false;
M
Michiharu Ariza 已提交
390 391 392 393
    }
  }

  /* FDSelect */
M
Michiharu Ariza 已提交
394
  if (acc.fdSelect != &Null(CFF2FDSelect))
M
Michiharu Ariza 已提交
395
  {
M
Michiharu Ariza 已提交
396
    assert (plan.offsets.FDSelectInfo.offset == c.head - c.start);
M
Michiharu Ariza 已提交
397 398
    
    if (plan.is_fds_subsetted ())
M
Michiharu Ariza 已提交
399
    {
M
Michiharu Ariza 已提交
400 401
      if (unlikely (!hb_serialize_cff_fdselect (&c, glyphs, *(const FDSelect *)acc.fdSelect, acc.fdArray->count,
                                                plan.subst_fdselect_format, plan.offsets.FDSelectInfo.size,
M
Michiharu Ariza 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
                                                plan.subst_fdselect_first_glyphs,
                                                plan.fdmap)))
      {
        DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 subset FDSelect");
        return false;
      }
    }
    else
    {
      CFF2FDSelect *dest = c.start_embed<CFF2FDSelect> ();
      if (unlikely (!dest->serialize (&c, *acc.fdSelect, acc.num_glyphs)))
      {
        DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 FDSelect");
        return false;
      }
M
Michiharu Ariza 已提交
417 418 419 420 421
    }
  }

  /* FDArray (FD Index) */
  {
M
Michiharu Ariza 已提交
422
    assert (plan.offsets.FDArrayInfo.offset == c.head - c.start);
M
Michiharu Ariza 已提交
423
    CFF2FDArray  *fda = c.start_embed<CFF2FDArray> ();
424
    if (unlikely (fda == nullptr)) return false;
M
Michiharu Ariza 已提交
425
    CFFFontDict_OpSerializer  fontSzr;
M
Michiharu Ariza 已提交
426
    if (unlikely (!fda->serialize (&c, plan.offsets.FDArrayInfo.offSize,
M
Michiharu Ariza 已提交
427
                                   acc.fontDicts, plan.subst_fdcount, plan.fdmap,
M
Michiharu Ariza 已提交
428
                                   fontSzr, plan.privateDictInfos)))
M
Michiharu Ariza 已提交
429 430
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 FDArray");
431
      return false;
M
Michiharu Ariza 已提交
432 433 434 435 436
    }
  }

  /* CharStrings */
  {
M
Michiharu Ariza 已提交
437
    assert (plan.offsets.charStringsInfo.offset == c.head - c.start);
M
Michiharu Ariza 已提交
438
    CFF2CharStrings  *cs = c.start_embed<CFF2CharStrings> ();
439
    if (unlikely (cs == nullptr)) return false;
M
Michiharu Ariza 已提交
440
    if (unlikely (!cs->serialize (&c, plan.offsets.charStringsInfo.offSize, plan.subset_charstrings)))
M
Michiharu Ariza 已提交
441 442
    {
      DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 CharStrings");
443
      return false;
M
Michiharu Ariza 已提交
444 445 446 447 448 449 450
    }
  }

  /* private dicts & local subrs */
  assert (plan.offsets.privateDictsOffset == c.head - c.start);
  for (unsigned int i = 0; i < acc.privateDicts.len; i++)
  {
451
    if (!plan.fdmap.excludes (i))
M
Michiharu Ariza 已提交
452
    {
453 454
      PrivateDict  *pd = c.start_embed<PrivateDict> ();
      if (unlikely (pd == nullptr)) return false;
455 456
      unsigned int priv_size = plan.privateDictInfos[plan.fdmap[i]].size;
      bool result;
M
Michiharu Ariza 已提交
457 458
      CFFPrivateDict_OpSerializer privSzr (plan.drop_hints, plan.flatten_subrs);
      result = pd->serialize (&c, acc.privateDicts[i], privSzr, priv_size);
459
      if (unlikely (!result))
M
Michiharu Ariza 已提交
460
      {
461
        DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 Private Dict[%d]", i);
462
        return false;
M
Michiharu Ariza 已提交
463
      }
M
Michiharu Ariza 已提交
464
      if (!plan.flatten_subrs && (acc.privateDicts[i].subrsOffset != 0))
M
Michiharu Ariza 已提交
465
      {
466 467 468 469 470 471
        CFF2Subrs *subrs = c.start_embed<CFF2Subrs> ();
        if (unlikely (subrs == nullptr) || acc.privateDicts[i].localSubrs == &Null(CFF2Subrs))
        {
          DEBUG_MSG (SUBSET, nullptr, "CFF2 subset: local subrs unexpectedly null [%d]", i);
          return false;
        }
M
Michiharu Ariza 已提交
472
        if (unlikely (!subrs->serialize (&c, *acc.privateDicts[i].localSubrs, plan.offsets.localSubrsInfos[i].offSize, plan.subrRefMaps.local_maps[i])))
473 474 475 476
        {
          DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 local subrs [%d]", i);
          return false;
        }
M
Michiharu Ariza 已提交
477 478 479 480
      }
    }
  }

M
Michiharu Ariza 已提交
481
  assert (c.head == c.end);
M
Michiharu Ariza 已提交
482 483
  c.end_serialize ();

M
Michiharu Ariza 已提交
484 485 486 487
  return true;
}

static bool
488
_hb_subset_cff2 (const OT::cff2::accelerator_subset_t  &acc,
M
Michiharu Ariza 已提交
489 490 491
                const char                      *data,
                hb_subset_plan_t                *plan,
                hb_blob_t                       **prime /* OUT */)
M
Michiharu Ariza 已提交
492
{
M
Michiharu Ariza 已提交
493
  cff2_subset_plan cff2_plan;
M
Michiharu Ariza 已提交
494

M
Michiharu Ariza 已提交
495
  if (unlikely (!cff2_plan.create (acc, plan)))
M
Michiharu Ariza 已提交
496 497
  {
    DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff2 subsetting plan.");
M
Michiharu Ariza 已提交
498 499 500
    return false;
  }

M
Michiharu Ariza 已提交
501
  unsigned int  cff2_prime_size = cff2_plan.get_final_size ();
M
Michiharu Ariza 已提交
502
  char *cff2_prime_data = (char *) calloc (1, cff2_prime_size);
M
Michiharu Ariza 已提交
503

M
Michiharu Ariza 已提交
504
  if (unlikely (!_write_cff2 (cff2_plan, acc, plan->glyphs,
M
Michiharu Ariza 已提交
505 506
                              cff2_prime_size, cff2_prime_data))) {
    DEBUG_MSG(SUBSET, nullptr, "Failed to write a subset cff2.");
M
Michiharu Ariza 已提交
507 508 509 510
    free (cff2_prime_data);
    return false;
  }

M
Michiharu Ariza 已提交
511
  *prime = hb_blob_create (cff2_prime_data,
M
Michiharu Ariza 已提交
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
                                cff2_prime_size,
                                HB_MEMORY_MODE_READONLY,
                                cff2_prime_data,
                                free);
  return true;
}

/**
 * hb_subset_cff2:
 * Subsets the CFF2 table according to a provided plan.
 *
 * Return value: subsetted cff2 table.
 **/
bool
hb_subset_cff2 (hb_subset_plan_t *plan,
M
Michiharu Ariza 已提交
527
                hb_blob_t       **prime /* OUT */)
M
Michiharu Ariza 已提交
528
{
529
  hb_blob_t *cff2_blob = hb_sanitize_context_t().reference_table<CFF::cff2> (plan->source);
M
Michiharu Ariza 已提交
530
  const char *data = hb_blob_get_data(cff2_blob, nullptr);
M
Michiharu Ariza 已提交
531

532
  OT::cff2::accelerator_subset_t acc;
M
Michiharu Ariza 已提交
533 534
  acc.init(plan->source);
  bool result = likely (acc.is_valid ()) &&
M
Michiharu Ariza 已提交
535
                _hb_subset_cff2 (acc, data, plan, prime);
M
Michiharu Ariza 已提交
536 537

  hb_blob_destroy (cff2_blob);
M
Michiharu Ariza 已提交
538
  acc.fini ();
M
Michiharu Ariza 已提交
539 540 541

  return result;
}