2018-07-18 23:17:52 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Adobe Systems Incorporated.
|
|
|
|
*
|
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
* Adobe Author(s): Michiharu Ariza
|
|
|
|
*/
|
|
|
|
|
2018-08-29 22:26:17 +02:00
|
|
|
#include "hb-open-type.hh"
|
2018-07-18 23:17:52 +02:00
|
|
|
#include "hb-ot-cff2-table.hh"
|
|
|
|
#include "hb-set.h"
|
|
|
|
#include "hb-subset-cff2.hh"
|
|
|
|
#include "hb-subset-plan.hh"
|
2018-08-29 22:26:17 +02:00
|
|
|
#include "hb-subset-cff-common.hh"
|
2018-08-17 22:13:18 +02:00
|
|
|
#include "hb-cff2-interp-cs.hh"
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
using namespace CFF;
|
|
|
|
|
2018-08-30 03:18:18 +02:00
|
|
|
struct CFF2SubTableOffsets : CFFSubTableOffsets
|
|
|
|
{
|
2018-08-10 20:07:07 +02:00
|
|
|
inline CFF2SubTableOffsets (void)
|
2018-08-30 03:18:18 +02:00
|
|
|
: CFFSubTableOffsets (),
|
|
|
|
varStoreOffset (0)
|
|
|
|
{}
|
2018-08-17 22:13:18 +02:00
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
unsigned int varStoreOffset;
|
|
|
|
};
|
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
struct CFF2TopDict_OpSerializer : CFFTopDict_OpSerializer<>
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2018-08-01 20:30:38 +02:00
|
|
|
inline bool serialize (hb_serialize_context_t *c,
|
|
|
|
const OpStr &opstr,
|
2018-08-10 20:07:07 +02:00
|
|
|
const CFF2SubTableOffsets &offsets) const
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
switch (opstr.op)
|
|
|
|
{
|
|
|
|
case OpCode_vstore:
|
|
|
|
return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.varStoreOffset));
|
|
|
|
|
|
|
|
default:
|
2018-09-11 01:00:20 +02:00
|
|
|
return_trace (CFFTopDict_OpSerializer<>::serialize (c, opstr, offsets));
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned int calculate_serialized_size (const OpStr &opstr) const
|
|
|
|
{
|
|
|
|
switch (opstr.op)
|
|
|
|
{
|
|
|
|
case OpCode_vstore:
|
2018-08-15 21:00:19 +02:00
|
|
|
return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (opstr.op);
|
2018-08-01 20:30:38 +02:00
|
|
|
|
|
|
|
default:
|
2018-09-11 01:00:20 +02:00
|
|
|
return CFFTopDict_OpSerializer<>::calculate_serialized_size (opstr);
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-31 02:21:56 +02:00
|
|
|
struct CFF2CSOpSet_Flatten : CFF2CSOpSet<CFF2CSOpSet_Flatten, FlattenParam>
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-11-01 06:30:34 +01:00
|
|
|
static inline void flush_args_and_op (OpCode op, CFF2CSInterpEnv &env, FlattenParam& param)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-08-30 03:18:18 +02:00
|
|
|
switch (op)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-08-31 22:28:16 +02:00
|
|
|
case OpCode_return:
|
|
|
|
case OpCode_endchar:
|
|
|
|
/* dummy opcodes in CFF2. ignore */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OpCode_hstem:
|
|
|
|
case OpCode_hstemhm:
|
|
|
|
case OpCode_vstem:
|
|
|
|
case OpCode_vstemhm:
|
2018-08-30 03:18:18 +02:00
|
|
|
case OpCode_hintmask:
|
|
|
|
case OpCode_cntrmask:
|
2018-08-31 02:21:56 +02:00
|
|
|
if (param.drop_hints)
|
|
|
|
{
|
2018-08-31 22:28:16 +02:00
|
|
|
env.clear_args ();
|
|
|
|
return;
|
2018-08-31 02:21:56 +02:00
|
|
|
}
|
2018-10-02 23:13:36 +02:00
|
|
|
HB_FALLTHROUGH;
|
|
|
|
|
2018-08-30 03:18:18 +02:00
|
|
|
default:
|
2018-11-01 06:30:34 +01:00
|
|
|
SUPER::flush_args_and_op (op, env, param);
|
2018-08-31 22:28:16 +02:00
|
|
|
break;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 06:30:34 +01:00
|
|
|
static inline void flush_args (CFF2CSInterpEnv &env, FlattenParam& param)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-11-01 06:30:34 +01:00
|
|
|
for (unsigned int i = 0; i < env.argStack.get_count ();)
|
2018-09-04 19:25:21 +02:00
|
|
|
{
|
2018-09-17 20:14:56 +02:00
|
|
|
const BlendArg &arg = env.argStack[i];
|
2018-10-09 00:05:36 +02:00
|
|
|
if (arg.blending ())
|
2018-09-04 19:25:21 +02:00
|
|
|
{
|
2018-11-01 06:30:34 +01:00
|
|
|
assert ((arg.numValues > 0) && (env.argStack.get_count () >= arg.numValues));
|
2018-09-04 19:25:21 +02:00
|
|
|
flatten_blends (arg, i, env, param);
|
|
|
|
i += arg.numValues;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-01 06:30:34 +01:00
|
|
|
StrEncoder encoder (param.flatStr);
|
|
|
|
encoder.encode_num (arg);
|
2018-09-04 19:25:21 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 06:30:34 +01:00
|
|
|
SUPER::flush_args (env, param);
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
2018-09-04 19:25:21 +02:00
|
|
|
static inline void flatten_blends (const BlendArg &arg, unsigned int i, CFF2CSInterpEnv &env, FlattenParam& param)
|
|
|
|
{
|
|
|
|
/* flatten the default values */
|
|
|
|
for (unsigned int j = 0; j < arg.numValues; j++)
|
|
|
|
{
|
2018-09-17 20:14:56 +02:00
|
|
|
const BlendArg &arg1 = env.argStack[i + j];
|
2018-10-09 00:05:36 +02:00
|
|
|
assert (arg1.blending () && (arg.numValues == arg1.numValues) && (arg1.valueIndex == j) &&
|
2018-09-04 19:25:21 +02:00
|
|
|
(arg1.deltas.len == env.get_region_count ()));
|
2018-11-01 06:30:34 +01:00
|
|
|
StrEncoder encoder (param.flatStr);
|
|
|
|
encoder.encode_num (arg1);
|
2018-09-04 19:25:21 +02:00
|
|
|
}
|
|
|
|
/* flatten deltas for each value */
|
2018-11-01 06:30:34 +01:00
|
|
|
StrEncoder encoder (param.flatStr);
|
2018-09-04 19:25:21 +02:00
|
|
|
for (unsigned int j = 0; j < arg.numValues; j++)
|
|
|
|
{
|
2018-09-17 20:14:56 +02:00
|
|
|
const BlendArg &arg1 = env.argStack[i + j];
|
2018-09-04 19:25:21 +02:00
|
|
|
for (unsigned int k = 0; k < arg1.deltas.len; k++)
|
2018-11-01 06:30:34 +01:00
|
|
|
encoder.encode_num (arg1.deltas[k]);
|
2018-09-04 19:25:21 +02:00
|
|
|
}
|
|
|
|
/* flatten the number of values followed by blend operator */
|
2018-11-01 06:30:34 +01:00
|
|
|
encoder.encode_int (arg.numValues);
|
|
|
|
encoder.encode_op (OpCode_blendcs);
|
2018-09-04 19:25:21 +02:00
|
|
|
}
|
2018-11-01 06:30:34 +01:00
|
|
|
|
2018-08-31 22:28:16 +02:00
|
|
|
static inline void flush_op (OpCode op, CFF2CSInterpEnv &env, FlattenParam& param)
|
2018-08-18 01:50:13 +02:00
|
|
|
{
|
2018-09-04 19:25:21 +02:00
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case OpCode_return:
|
|
|
|
case OpCode_endchar:
|
|
|
|
return;
|
|
|
|
default:
|
2018-11-01 06:30:34 +01:00
|
|
|
StrEncoder encoder (param.flatStr);
|
|
|
|
encoder.encode_op (op);
|
2018-09-04 19:25:21 +02:00
|
|
|
}
|
2018-08-18 01:50:13 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 03:18:18 +02:00
|
|
|
private:
|
2018-08-31 02:21:56 +02:00
|
|
|
typedef CFF2CSOpSet<CFF2CSOpSet_Flatten, FlattenParam> SUPER;
|
2018-09-17 20:14:56 +02:00
|
|
|
typedef CSOpSet<BlendArg, CFF2CSOpSet_Flatten, CFF2CSOpSet_Flatten, CFF2CSInterpEnv, FlattenParam> CSOPSET;
|
2018-08-18 01:50:13 +02:00
|
|
|
};
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
struct cff2_subset_plan {
|
|
|
|
inline cff2_subset_plan (void)
|
2018-08-06 19:04:53 +02:00
|
|
|
: final_size (0),
|
2018-08-06 19:44:12 +02:00
|
|
|
orig_fdcount (0),
|
2018-09-07 02:28:15 +02:00
|
|
|
subset_fdcount(1),
|
|
|
|
subset_fdselect_format (0),
|
2018-11-01 06:30:34 +01:00
|
|
|
drop_hints (false),
|
|
|
|
desubroutinize (false)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-09-13 01:08:54 +02:00
|
|
|
subset_fdselect_ranges.init ();
|
2018-08-06 19:04:53 +02:00
|
|
|
fdmap.init ();
|
2018-08-01 20:30:38 +02:00
|
|
|
subset_charstrings.init ();
|
2018-08-30 03:18:18 +02:00
|
|
|
flat_charstrings.init ();
|
2018-08-10 20:07:07 +02:00
|
|
|
privateDictInfos.init ();
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
inline ~cff2_subset_plan (void)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-09-13 01:08:54 +02:00
|
|
|
subset_fdselect_ranges.fini ();
|
2018-08-06 19:04:53 +02:00
|
|
|
fdmap.fini ();
|
2018-08-01 20:30:38 +02:00
|
|
|
subset_charstrings.fini ();
|
2018-08-30 03:18:18 +02:00
|
|
|
flat_charstrings.fini ();
|
2018-08-10 20:07:07 +02:00
|
|
|
privateDictInfos.fini ();
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 01:28:10 +02:00
|
|
|
inline bool create (const OT::cff2::accelerator_subset_t &acc,
|
2018-08-01 20:30:38 +02:00
|
|
|
hb_subset_plan_t *plan)
|
|
|
|
{
|
|
|
|
final_size = 0;
|
2018-08-06 19:04:53 +02:00
|
|
|
orig_fdcount = acc.fdArray->count;
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2018-08-18 01:50:13 +02:00
|
|
|
drop_hints = plan->drop_hints;
|
2018-11-01 06:30:34 +01:00
|
|
|
desubroutinize = plan->desubroutinize;
|
2018-08-18 01:50:13 +02:00
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
/* CFF2 header */
|
|
|
|
final_size += OT::cff2::static_size;
|
|
|
|
|
|
|
|
/* top dict */
|
|
|
|
{
|
|
|
|
CFF2TopDict_OpSerializer topSzr;
|
2018-08-30 03:18:18 +02:00
|
|
|
offsets.topDictInfo.size = TopDict::calculate_serialized_size (acc.topDict, topSzr);
|
|
|
|
final_size += offsets.topDictInfo.size;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 03:18:18 +02:00
|
|
|
{
|
|
|
|
/* Flatten global & local subrs */
|
2018-08-31 02:21:56 +02:00
|
|
|
SubrFlattener<const OT::cff2::accelerator_subset_t, CFF2CSInterpEnv, CFF2CSOpSet_Flatten>
|
|
|
|
flattener(acc, plan->glyphs, plan->drop_hints);
|
2018-08-30 03:18:18 +02:00
|
|
|
if (!flattener.flatten (flat_charstrings))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* no global/local subroutines */
|
|
|
|
offsets.globalSubrsInfo.size = HBUINT32::static_size; /* count 0 only */
|
|
|
|
}
|
2018-08-17 22:13:18 +02:00
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
/* global subrs */
|
2018-08-17 22:13:18 +02:00
|
|
|
offsets.globalSubrsInfo.offset = final_size;
|
|
|
|
final_size += offsets.globalSubrsInfo.size;
|
2018-08-01 20:30:38 +02:00
|
|
|
|
|
|
|
/* variation store */
|
|
|
|
if (acc.varStore != &Null(CFF2VariationStore))
|
|
|
|
{
|
|
|
|
offsets.varStoreOffset = final_size;
|
|
|
|
final_size += acc.varStore->get_size ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FDSelect */
|
2018-08-06 19:04:53 +02:00
|
|
|
if (acc.fdSelect != &Null(CFF2FDSelect))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-08-10 20:07:07 +02:00
|
|
|
offsets.FDSelectInfo.offset = final_size;
|
2018-08-06 19:04:53 +02:00
|
|
|
if (unlikely (!hb_plan_subset_cff_fdselect (plan->glyphs,
|
|
|
|
orig_fdcount,
|
2018-08-10 20:07:07 +02:00
|
|
|
*(const FDSelect *)acc.fdSelect,
|
2018-09-07 02:28:15 +02:00
|
|
|
subset_fdcount,
|
2018-08-10 20:07:07 +02:00
|
|
|
offsets.FDSelectInfo.size,
|
2018-09-07 02:28:15 +02:00
|
|
|
subset_fdselect_format,
|
2018-09-13 01:08:54 +02:00
|
|
|
subset_fdselect_ranges,
|
2018-08-06 19:04:53 +02:00
|
|
|
fdmap)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!is_fds_subsetted ())
|
2018-08-10 20:07:07 +02:00
|
|
|
offsets.FDSelectInfo.size = acc.fdSelect->calculate_serialized_size (acc.num_glyphs);
|
|
|
|
final_size += offsets.FDSelectInfo.size;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FDArray (FDIndex) */
|
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
offsets.FDArrayInfo.offset = final_size;
|
2018-08-30 03:18:18 +02:00
|
|
|
CFFFontDict_OpSerializer fontSzr;
|
2018-09-07 02:28:15 +02:00
|
|
|
final_size += CFF2FDArray::calculate_serialized_size(offsets.FDArrayInfo.offSize/*OUT*/, acc.fontDicts, subset_fdcount, fdmap, fontSzr);
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CharStrings */
|
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
offsets.charStringsInfo.offset = final_size;
|
2018-08-01 20:30:38 +02:00
|
|
|
unsigned int dataSize = 0;
|
|
|
|
for (unsigned int i = 0; i < plan->glyphs.len; i++)
|
|
|
|
{
|
2018-11-01 06:30:34 +01:00
|
|
|
StrBuff &flatstr = flat_charstrings[i];
|
2018-09-11 01:27:49 +02:00
|
|
|
ByteStr str (&flatstr[0], flatstr.len);
|
|
|
|
subset_charstrings.push (str);
|
|
|
|
dataSize += flatstr.len;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
2018-09-12 01:20:39 +02:00
|
|
|
offsets.charStringsInfo.offSize = calcOffSize (dataSize);
|
2018-08-15 21:00:19 +02:00
|
|
|
final_size += CFF2CharStrings::calculate_serialized_size (offsets.charStringsInfo.offSize, plan->glyphs.len, dataSize);
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* private dicts & local subrs */
|
|
|
|
offsets.privateDictsOffset = final_size;
|
2018-08-06 19:04:53 +02:00
|
|
|
for (unsigned int i = 0; i < orig_fdcount; i++)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-08-10 21:55:22 +02:00
|
|
|
if (!fdmap.excludes (i))
|
|
|
|
{
|
2018-08-18 01:50:13 +02:00
|
|
|
unsigned int priv_size;
|
2018-11-01 06:30:34 +01:00
|
|
|
CFFPrivateDict_OpSerializer privSzr (desubroutinize, drop_hints);
|
2018-08-30 03:18:18 +02:00
|
|
|
priv_size = PrivateDict::calculate_serialized_size (acc.privateDicts[i], privSzr);
|
2018-08-18 01:50:13 +02:00
|
|
|
TableInfo privInfo = { final_size, priv_size, 0 };
|
2018-08-10 21:55:22 +02:00
|
|
|
privateDictInfos.push (privInfo);
|
2018-08-30 03:18:18 +02:00
|
|
|
final_size += privInfo.size;
|
2018-08-10 21:55:22 +02:00
|
|
|
}
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned int get_final_size (void) const { return final_size; }
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
unsigned int final_size;
|
|
|
|
CFF2SubTableOffsets offsets;
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2018-08-06 19:04:53 +02:00
|
|
|
unsigned int orig_fdcount;
|
2018-09-07 02:28:15 +02:00
|
|
|
unsigned int subset_fdcount;
|
|
|
|
inline bool is_fds_subsetted (void) const { return subset_fdcount < orig_fdcount; }
|
|
|
|
unsigned int subset_fdselect_format;
|
2018-09-13 01:08:54 +02:00
|
|
|
hb_vector_t<code_pair> subset_fdselect_ranges;
|
2018-08-06 19:04:53 +02:00
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
Remap fdmap;
|
2018-08-06 19:04:53 +02:00
|
|
|
|
2018-11-01 06:30:34 +01:00
|
|
|
ByteStrArray subset_charstrings;
|
|
|
|
StrBuffArray flat_charstrings;
|
2018-08-30 03:18:18 +02:00
|
|
|
hb_vector_t<TableInfo> privateDictInfos;
|
2018-08-17 22:13:18 +02:00
|
|
|
|
2018-08-18 01:50:13 +02:00
|
|
|
bool drop_hints;
|
2018-11-01 06:30:34 +01:00
|
|
|
bool desubroutinize;
|
2018-08-01 20:30:38 +02:00
|
|
|
};
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
static inline bool _write_cff2 (const cff2_subset_plan &plan,
|
2018-08-03 01:28:10 +02:00
|
|
|
const OT::cff2::accelerator_subset_t &acc,
|
2018-08-06 19:04:53 +02:00
|
|
|
const hb_vector_t<hb_codepoint_t>& glyphs,
|
2018-08-01 20:30:38 +02:00
|
|
|
unsigned int dest_sz,
|
|
|
|
void *dest)
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2018-08-01 20:30:38 +02:00
|
|
|
hb_serialize_context_t c (dest, dest_sz);
|
|
|
|
|
|
|
|
OT::cff2 *cff2 = c.start_serialize<OT::cff2> ();
|
|
|
|
if (unlikely (!c.extend_min (*cff2)))
|
2018-08-02 21:23:10 +02:00
|
|
|
return false;
|
2018-08-01 20:30:38 +02:00
|
|
|
|
|
|
|
/* 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);
|
2018-08-30 03:18:18 +02:00
|
|
|
cff2->topDictSize.set (plan.offsets.topDictInfo.size);
|
2018-08-01 20:30:38 +02:00
|
|
|
TopDict &dict = cff2 + cff2->topDict;
|
|
|
|
CFF2TopDict_OpSerializer topSzr;
|
2018-08-10 20:07:07 +02:00
|
|
|
if (unlikely (!dict.serialize (&c, acc.topDict, topSzr, plan.offsets)))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 top dict");
|
2018-08-02 21:23:10 +02:00
|
|
|
return false;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* global subrs */
|
|
|
|
{
|
2018-08-30 03:18:18 +02:00
|
|
|
assert (cff2->topDict + plan.offsets.topDictInfo.size == c.head - c.start);
|
2018-09-11 01:27:49 +02:00
|
|
|
CFF2Subrs *dest = c.allocate_size <CFF2Subrs> (HBUINT32::static_size);
|
2018-08-02 21:23:10 +02:00
|
|
|
if (unlikely (dest == nullptr)) return false;
|
2018-09-11 01:27:49 +02:00
|
|
|
dest->count.set (0);
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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");
|
2018-08-02 21:23:10 +02:00
|
|
|
return false;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FDSelect */
|
2018-08-06 19:04:53 +02:00
|
|
|
if (acc.fdSelect != &Null(CFF2FDSelect))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-08-10 20:07:07 +02:00
|
|
|
assert (plan.offsets.FDSelectInfo.offset == c.head - c.start);
|
2018-08-06 19:04:53 +02:00
|
|
|
|
|
|
|
if (plan.is_fds_subsetted ())
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-09-13 01:08:54 +02:00
|
|
|
if (unlikely (!hb_serialize_cff_fdselect (&c, glyphs.len, *(const FDSelect *)acc.fdSelect, acc.fdArray->count,
|
2018-09-07 02:28:15 +02:00
|
|
|
plan.subset_fdselect_format, plan.offsets.FDSelectInfo.size,
|
2018-09-13 01:08:54 +02:00
|
|
|
plan.subset_fdselect_ranges,
|
2018-08-06 19:04:53 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FDArray (FD Index) */
|
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
assert (plan.offsets.FDArrayInfo.offset == c.head - c.start);
|
2018-08-10 20:07:07 +02:00
|
|
|
CFF2FDArray *fda = c.start_embed<CFF2FDArray> ();
|
2018-08-02 21:23:10 +02:00
|
|
|
if (unlikely (fda == nullptr)) return false;
|
2018-08-30 03:18:18 +02:00
|
|
|
CFFFontDict_OpSerializer fontSzr;
|
2018-08-15 21:00:19 +02:00
|
|
|
if (unlikely (!fda->serialize (&c, plan.offsets.FDArrayInfo.offSize,
|
2018-09-07 02:28:15 +02:00
|
|
|
acc.fontDicts, plan.subset_fdcount, plan.fdmap,
|
2018-08-10 20:07:07 +02:00
|
|
|
fontSzr, plan.privateDictInfos)))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 FDArray");
|
2018-08-02 21:23:10 +02:00
|
|
|
return false;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CharStrings */
|
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
assert (plan.offsets.charStringsInfo.offset == c.head - c.start);
|
2018-08-10 20:07:07 +02:00
|
|
|
CFF2CharStrings *cs = c.start_embed<CFF2CharStrings> ();
|
2018-08-02 21:23:10 +02:00
|
|
|
if (unlikely (cs == nullptr)) return false;
|
2018-08-15 21:00:19 +02:00
|
|
|
if (unlikely (!cs->serialize (&c, plan.offsets.charStringsInfo.offSize, plan.subset_charstrings)))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 CharStrings");
|
2018-08-02 21:23:10 +02:00
|
|
|
return false;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* private dicts & local subrs */
|
|
|
|
assert (plan.offsets.privateDictsOffset == c.head - c.start);
|
|
|
|
for (unsigned int i = 0; i < acc.privateDicts.len; i++)
|
|
|
|
{
|
2018-08-10 21:55:22 +02:00
|
|
|
if (!plan.fdmap.excludes (i))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-08-10 21:55:22 +02:00
|
|
|
PrivateDict *pd = c.start_embed<PrivateDict> ();
|
|
|
|
if (unlikely (pd == nullptr)) return false;
|
2018-08-18 01:50:13 +02:00
|
|
|
unsigned int priv_size = plan.privateDictInfos[plan.fdmap[i]].size;
|
|
|
|
bool result;
|
2018-11-01 06:30:34 +01:00
|
|
|
CFFPrivateDict_OpSerializer privSzr (plan.desubroutinize, plan.drop_hints);
|
2018-08-30 03:18:18 +02:00
|
|
|
result = pd->serialize (&c, acc.privateDicts[i], privSzr, priv_size);
|
2018-08-18 01:50:13 +02:00
|
|
|
if (unlikely (!result))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-08-10 21:55:22 +02:00
|
|
|
DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF2 Private Dict[%d]", i);
|
2018-08-02 21:23:10 +02:00
|
|
|
return false;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 22:13:18 +02:00
|
|
|
assert (c.head == c.end);
|
2018-08-01 20:30:38 +02:00
|
|
|
c.end_serialize ();
|
|
|
|
|
2018-07-18 23:17:52 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2018-08-03 01:28:10 +02:00
|
|
|
_hb_subset_cff2 (const OT::cff2::accelerator_subset_t &acc,
|
2018-08-01 20:30:38 +02:00
|
|
|
const char *data,
|
|
|
|
hb_subset_plan_t *plan,
|
|
|
|
hb_blob_t **prime /* OUT */)
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2018-08-10 20:07:07 +02:00
|
|
|
cff2_subset_plan cff2_plan;
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
if (unlikely (!cff2_plan.create (acc, plan)))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
|
|
|
DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff2 subsetting plan.");
|
2018-07-18 23:17:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
unsigned int cff2_prime_size = cff2_plan.get_final_size ();
|
2018-07-18 23:17:52 +02:00
|
|
|
char *cff2_prime_data = (char *) calloc (1, cff2_prime_size);
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
if (unlikely (!_write_cff2 (cff2_plan, acc, plan->glyphs,
|
2018-08-01 20:30:38 +02:00
|
|
|
cff2_prime_size, cff2_prime_data))) {
|
|
|
|
DEBUG_MSG(SUBSET, nullptr, "Failed to write a subset cff2.");
|
2018-07-18 23:17:52 +02:00
|
|
|
free (cff2_prime_data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
*prime = hb_blob_create (cff2_prime_data,
|
2018-07-18 23:17:52 +02:00
|
|
|
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,
|
2018-08-01 20:30:38 +02:00
|
|
|
hb_blob_t **prime /* OUT */)
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2018-08-02 20:39:22 +02:00
|
|
|
hb_blob_t *cff2_blob = hb_sanitize_context_t().reference_table<CFF::cff2> (plan->source);
|
2018-08-01 20:30:38 +02:00
|
|
|
const char *data = hb_blob_get_data(cff2_blob, nullptr);
|
2018-07-18 23:17:52 +02:00
|
|
|
|
2018-08-03 01:28:10 +02:00
|
|
|
OT::cff2::accelerator_subset_t acc;
|
2018-08-01 20:30:38 +02:00
|
|
|
acc.init(plan->source);
|
|
|
|
bool result = likely (acc.is_valid ()) &&
|
2018-08-10 20:07:07 +02:00
|
|
|
_hb_subset_cff2 (acc, data, plan, prime);
|
2018-07-18 23:17:52 +02:00
|
|
|
|
|
|
|
hb_blob_destroy (cff2_blob);
|
2018-08-10 20:07:07 +02:00
|
|
|
acc.fini ();
|
2018-07-18 23:17:52 +02:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|