2018-08-10 20:07:07 +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-08-16 09:13:09 +02:00
|
|
|
#include "hb-ot-cff1-table.hh"
|
2018-08-10 20:07:07 +02:00
|
|
|
#include "hb-set.h"
|
2018-08-16 09:13:09 +02:00
|
|
|
#include "hb-subset-cff1.hh"
|
2018-08-10 20:07:07 +02:00
|
|
|
#include "hb-subset-plan.hh"
|
2018-08-29 22:26:17 +02:00
|
|
|
#include "hb-subset-cff-common.hh"
|
2018-08-16 09:13:09 +02:00
|
|
|
#include "hb-cff1-interp-cs.hh"
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
using namespace CFF;
|
|
|
|
|
2018-08-16 09:13:09 +02:00
|
|
|
struct CFF1SubTableOffsets {
|
|
|
|
inline CFF1SubTableOffsets (void)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
memset (this, 0, sizeof(*this));
|
2018-08-15 21:00:19 +02:00
|
|
|
localSubrsInfos.init ();
|
|
|
|
}
|
|
|
|
|
2018-08-16 09:13:09 +02:00
|
|
|
inline ~CFF1SubTableOffsets (void)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
localSubrsInfos.fini ();
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int nameIndexOffset;
|
2018-08-15 21:00:19 +02:00
|
|
|
TableInfo topDictInfo;
|
2018-08-10 20:07:07 +02:00
|
|
|
unsigned int stringIndexOffset;
|
2018-08-15 21:00:19 +02:00
|
|
|
TableInfo globalSubrsInfo;
|
2018-08-10 20:07:07 +02:00
|
|
|
unsigned int encodingOffset;
|
|
|
|
unsigned int charsetOffset;
|
|
|
|
TableInfo FDSelectInfo;
|
2018-08-15 21:00:19 +02:00
|
|
|
TableInfo FDArrayInfo;
|
|
|
|
TableInfo charStringsInfo;
|
2018-08-10 20:07:07 +02:00
|
|
|
TableInfo privateDictInfo;
|
2018-08-15 21:00:19 +02:00
|
|
|
hb_vector_t<TableInfo> localSubrsInfos;
|
2018-08-10 20:07:07 +02:00
|
|
|
};
|
|
|
|
|
2018-08-16 09:13:09 +02:00
|
|
|
struct CFF1TopDict_OpSerializer : OpSerializer
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
inline bool serialize (hb_serialize_context_t *c,
|
|
|
|
const OpStr &opstr,
|
2018-08-16 09:13:09 +02:00
|
|
|
const CFF1SubTableOffsets &offsets) const
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
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:
|
2018-08-15 21:00:19 +02:00
|
|
|
return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.charStringsInfo.offset));
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
case OpCode_FDArray:
|
2018-08-15 21:00:19 +02:00
|
|
|
return_trace (FontDict::serialize_offset4_op(c, opstr.op, offsets.FDArrayInfo.offset));
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
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:
|
2018-08-15 21:00:19 +02:00
|
|
|
return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (opstr.op);
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
case OpCode_Private:
|
2018-08-15 21:00:19 +02:00
|
|
|
return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Private);
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
return opstr.str.len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-16 09:13:09 +02:00
|
|
|
struct CFF1FontDict_OpSerializer : OpSerializer
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
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)
|
2018-08-15 21:00:19 +02:00
|
|
|
return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Private);
|
2018-08-10 20:07:07 +02:00
|
|
|
else
|
|
|
|
return opstr.str.len;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-16 09:13:09 +02:00
|
|
|
struct CFF1PrivateDict_OpSerializer : OpSerializer
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2018-08-29 21:14:30 +02:00
|
|
|
inline CFF1PrivateDict_OpSerializer (bool drop_hints_=false, bool flatten_subrs_=false)
|
|
|
|
: drop_hints (drop_hints_), flatten_subrs (flatten_subrs_) {}
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
inline bool serialize (hb_serialize_context_t *c,
|
|
|
|
const OpStr &opstr,
|
|
|
|
const unsigned int subrsOffset) const
|
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
|
|
|
|
2018-08-29 21:14:30 +02:00
|
|
|
if (drop_hints && DictOpSet::is_hint_op (opstr.op))
|
|
|
|
return true;
|
2018-08-10 20:07:07 +02:00
|
|
|
if (opstr.op == OpCode_Subrs)
|
2018-08-29 21:14:30 +02:00
|
|
|
{
|
|
|
|
if (flatten_subrs)
|
|
|
|
return_trace (true);
|
|
|
|
else
|
|
|
|
return_trace (FontDict::serialize_offset2_op(c, OpCode_Subrs, subrsOffset));
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
else
|
|
|
|
return_trace (copy_opstr (c, opstr));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned int calculate_serialized_size (const OpStr &opstr) const
|
|
|
|
{
|
2018-08-29 21:14:30 +02:00
|
|
|
if (drop_hints && DictOpSet::is_hint_op (opstr.op))
|
|
|
|
return 0;
|
2018-08-10 20:07:07 +02:00
|
|
|
if (opstr.op == OpCode_Subrs)
|
2018-08-29 21:14:30 +02:00
|
|
|
{
|
|
|
|
if (flatten_subrs)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Subrs);
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
else
|
|
|
|
return opstr.str.len;
|
|
|
|
}
|
2018-08-29 21:14:30 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
const bool drop_hints;
|
|
|
|
const bool flatten_subrs;
|
2018-08-10 20:07:07 +02:00
|
|
|
};
|
|
|
|
|
2018-08-29 21:14:30 +02:00
|
|
|
struct CFF1CSOpSet_Flatten : CFF1CSOpSet<CFF1CSOpSet_Flatten, ByteStrBuff>
|
2018-08-18 01:50:13 +02:00
|
|
|
{
|
2018-08-29 21:14:30 +02:00
|
|
|
static inline bool process_op (OpCode op, CFF1CSInterpEnv &env, ByteStrBuff& flatStr)
|
2018-08-18 01:50:13 +02:00
|
|
|
{
|
2018-08-29 21:14:30 +02:00
|
|
|
if (unlikely (!SUPER::process_op (op, env, flatStr)))
|
|
|
|
return false;
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case OpCode_hintmask:
|
|
|
|
case OpCode_cntrmask:
|
|
|
|
if (unlikely (!flatStr.encode_op (op)))
|
|
|
|
return false;
|
|
|
|
for (int i = -env.hintmask_size; i < 0; i++)
|
|
|
|
if (unlikely (!flatStr.encode_byte (env.substr[i])))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
default:
|
2018-08-29 21:56:48 +02:00
|
|
|
if (!SUPER::is_subr_op (op) &&
|
|
|
|
!SUPER::is_arg_op (op))
|
2018-08-29 21:14:30 +02:00
|
|
|
return flatStr.encode_op (op);
|
|
|
|
}
|
|
|
|
return true;
|
2018-08-18 01:50:13 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 21:14:30 +02:00
|
|
|
static inline void flush_stack (CFF1CSInterpEnv &env, ByteStrBuff& flatStr)
|
2018-08-18 01:50:13 +02:00
|
|
|
{
|
2018-08-29 21:14:30 +02:00
|
|
|
for (unsigned int i = 0; i < env.argStack.size; i++)
|
|
|
|
flatStr.encode_num (env.argStack.elements[i]);
|
|
|
|
SUPER::flush_stack (env, flatStr);
|
2018-08-18 01:50:13 +02:00
|
|
|
}
|
2018-08-29 21:14:30 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef CFF1CSOpSet<CFF1CSOpSet_Flatten, ByteStrBuff> SUPER;
|
2018-08-18 01:50:13 +02:00
|
|
|
};
|
|
|
|
|
2018-08-29 21:14:30 +02:00
|
|
|
struct CFF1CSOpSet_SubsetSubrs : CFF1CSOpSet<CFF1CSOpSet_SubsetSubrs, SubrRefMapPair>
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-08-16 09:13:09 +02:00
|
|
|
static inline bool process_op (OpCode op, CFF1CSInterpEnv &env, SubrRefMapPair& refMapPair)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2018-08-29 21:14:30 +02:00
|
|
|
return CFF1CSOpSet<CFF1CSOpSet_SubsetSubrs, SubrRefMapPair>::process_op (op, env, refMapPair);
|
2018-08-15 21:00:19 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
struct cff_subset_plan {
|
|
|
|
inline cff_subset_plan (void)
|
|
|
|
: final_size (0),
|
|
|
|
orig_fdcount (0),
|
2018-08-29 21:14:30 +02:00
|
|
|
subst_fdcount (1),
|
2018-08-15 21:00:19 +02:00
|
|
|
subst_fdselect_format (0),
|
2018-08-29 21:14:30 +02:00
|
|
|
offsets (),
|
|
|
|
flatten_subrs (true),
|
|
|
|
drop_hints (false)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
topdict_sizes.init ();
|
|
|
|
topdict_sizes.resize (1);
|
|
|
|
subst_fdselect_first_glyphs.init ();
|
|
|
|
fdmap.init ();
|
|
|
|
subset_charstrings.init ();
|
2018-08-29 21:14:30 +02:00
|
|
|
flat_charstrings.init ();
|
2018-08-10 20:07:07 +02:00
|
|
|
privateDictInfos.init ();
|
2018-08-29 21:14:30 +02:00
|
|
|
subrRefMaps.init ();
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ~cff_subset_plan (void)
|
|
|
|
{
|
|
|
|
topdict_sizes.fini ();
|
|
|
|
subst_fdselect_first_glyphs.fini ();
|
|
|
|
fdmap.fini ();
|
|
|
|
subset_charstrings.fini ();
|
2018-08-29 21:14:30 +02:00
|
|
|
flat_charstrings.fini ();
|
2018-08-10 20:07:07 +02:00
|
|
|
privateDictInfos.fini ();
|
2018-08-15 21:00:19 +02:00
|
|
|
subrRefMaps.fini ();
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2018-08-16 09:25:57 +02:00
|
|
|
inline bool create (const OT::cff1::accelerator_subset_t &acc,
|
2018-08-10 20:07:07 +02:00
|
|
|
hb_subset_plan_t *plan)
|
|
|
|
{
|
|
|
|
final_size = 0;
|
|
|
|
orig_fdcount = acc.fdCount;
|
2018-08-18 01:50:13 +02:00
|
|
|
drop_hints = plan->drop_hints;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
/* CFF header */
|
2018-08-16 09:25:57 +02:00
|
|
|
final_size += OT::cff1::static_size;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
/* Name INDEX */
|
|
|
|
offsets.nameIndexOffset = final_size;
|
|
|
|
final_size += acc.nameIndex->get_size ();
|
|
|
|
|
|
|
|
/* top dict INDEX */
|
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
offsets.topDictInfo.offset = final_size;
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1TopDict_OpSerializer topSzr;
|
2018-08-10 20:07:07 +02:00
|
|
|
unsigned int topDictSize = TopDict::calculate_serialized_size (acc.topDicts[0], topSzr);
|
2018-08-15 21:00:19 +02:00
|
|
|
offsets.topDictInfo.offSize = calcOffSize(topDictSize);
|
2018-08-16 09:13:09 +02:00
|
|
|
final_size += CFF1IndexOf<TopDict>::calculate_serialized_size<CFF1TopDictValues> (offsets.topDictInfo.offSize, acc.topDicts, topdict_sizes, topSzr);
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* String INDEX */
|
|
|
|
offsets.stringIndexOffset = final_size;
|
|
|
|
final_size += acc.stringIndex->get_size ();
|
|
|
|
|
2018-08-29 21:14:30 +02:00
|
|
|
if (flatten_subrs)
|
|
|
|
{
|
|
|
|
/* Flatten global & local subrs */
|
|
|
|
SubrFlattener<const OT::cff1::accelerator_subset_t, CFF1CSInterpEnv, CFF1CSOpSet_Flatten> flattener(acc, plan->glyphs);
|
|
|
|
if (!flattener.flatten (flat_charstrings))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* no global/local subroutines */
|
|
|
|
offsets.globalSubrsInfo.size = HBUINT16::static_size; /* count 0 only */
|
|
|
|
}
|
|
|
|
else
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-08-29 21:14:30 +02:00
|
|
|
/* Subset global & local subrs */
|
|
|
|
SubrSubsetter<const OT::cff1::accelerator_subset_t, CFF1CSInterpEnv, CFF1CSOpSet_SubsetSubrs> subsetter(acc, plan->glyphs);
|
2018-08-15 21:00:19 +02:00
|
|
|
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);
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
/* global subrs */
|
2018-08-15 21:00:19 +02:00
|
|
|
offsets.globalSubrsInfo.offset = final_size;
|
|
|
|
final_size += offsets.globalSubrsInfo.size;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
/* 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 */
|
2018-08-16 09:13:09 +02:00
|
|
|
if (acc.fdSelect != &Null(CFF1FDSelect))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
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) */
|
2018-08-16 09:13:09 +02:00
|
|
|
if (acc.fdArray != &Null(CFF1FDArray)) {
|
2018-08-15 21:00:19 +02:00
|
|
|
offsets.FDArrayInfo.offset = final_size;
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1FontDict_OpSerializer fontSzr;
|
|
|
|
final_size += CFF1FDArray::calculate_serialized_size(offsets.FDArrayInfo.offSize/*OUT*/, acc.fontDicts, subst_fdcount, fdmap, fontSzr);
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* CharStrings */
|
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
offsets.charStringsInfo.offset = final_size;
|
2018-08-10 20:07:07 +02:00
|
|
|
unsigned int dataSize = 0;
|
|
|
|
for (unsigned int i = 0; i < plan->glyphs.len; i++)
|
|
|
|
{
|
2018-08-29 21:14:30 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
2018-08-15 21:00:19 +02:00
|
|
|
offsets.charStringsInfo.offSize = calcOffSize (dataSize + 1);
|
2018-08-16 09:13:09 +02:00
|
|
|
final_size += CFF1CharStrings::calculate_serialized_size (offsets.charStringsInfo.offSize, plan->glyphs.len, dataSize);
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* private dicts & local subrs */
|
|
|
|
offsets.privateDictInfo.offset = final_size;
|
|
|
|
for (unsigned int i = 0; i < orig_fdcount; i++)
|
|
|
|
{
|
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-08-29 21:14:30 +02:00
|
|
|
CFF1PrivateDict_OpSerializer privSzr (plan->drop_hints, flatten_subrs);
|
|
|
|
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-29 21:14:30 +02:00
|
|
|
final_size += privInfo.size;
|
|
|
|
if (!flatten_subrs)
|
|
|
|
final_size += offsets.localSubrsInfos[i].size;
|
2018-08-10 21:55:22 +02:00
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!acc.is_CID ())
|
|
|
|
offsets.privateDictInfo = privateDictInfos[0];
|
|
|
|
|
2018-08-29 21:14:30 +02:00
|
|
|
return ((subset_charstrings.len == plan->glyphs.len) &&
|
|
|
|
(privateDictInfos.len == subst_fdcount));
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned int get_final_size (void) const { return final_size; }
|
|
|
|
|
|
|
|
unsigned int final_size;
|
|
|
|
hb_vector_t<unsigned int> topdict_sizes;
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1SubTableOffsets offsets;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
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 */
|
2018-08-10 21:55:22 +02:00
|
|
|
FDMap fdmap;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
2018-08-29 21:14:30 +02:00
|
|
|
hb_vector_t<ByteStr> subset_charstrings;
|
|
|
|
ByteStrBuffArray flat_charstrings;
|
|
|
|
hb_vector_t<TableInfo> privateDictInfos;
|
2018-08-15 21:00:19 +02:00
|
|
|
|
|
|
|
SubrRefMaps subrRefMaps;
|
2018-08-18 01:50:13 +02:00
|
|
|
|
2018-08-29 21:14:30 +02:00
|
|
|
bool flatten_subrs;
|
2018-08-18 01:50:13 +02:00
|
|
|
bool drop_hints;
|
2018-08-10 20:07:07 +02:00
|
|
|
};
|
|
|
|
|
2018-08-16 09:13:09 +02:00
|
|
|
static inline bool _write_cff1 (const cff_subset_plan &plan,
|
2018-08-16 09:25:57 +02:00
|
|
|
const OT::cff1::accelerator_subset_t &acc,
|
2018-08-10 20:07:07 +02:00
|
|
|
const hb_vector_t<hb_codepoint_t>& glyphs,
|
|
|
|
unsigned int dest_sz,
|
|
|
|
void *dest)
|
|
|
|
{
|
|
|
|
hb_serialize_context_t c (dest, dest_sz);
|
|
|
|
|
2018-08-15 22:04:43 +02:00
|
|
|
char RETURN_OP[1] = { OpCode_return };
|
2018-08-15 22:15:08 +02:00
|
|
|
const ByteStr NULL_SUBR (RETURN_OP, 1);
|
2018-08-15 22:04:43 +02:00
|
|
|
|
2018-08-16 09:25:57 +02:00
|
|
|
OT::cff1 *cff = c.start_serialize<OT::cff1> ();
|
2018-08-10 20:07:07 +02:00
|
|
|
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);
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1NameIndex *dest = c.start_embed<CFF1NameIndex> ();
|
2018-08-10 20:07:07 +02:00
|
|
|
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 */
|
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
assert (plan.offsets.topDictInfo.offset == c.head - c.start);
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1IndexOf<TopDict> *dest = c.start_embed< CFF1IndexOf<TopDict> > ();
|
2018-08-10 20:07:07 +02:00
|
|
|
if (dest == nullptr) return false;
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1TopDict_OpSerializer topSzr;
|
2018-08-15 21:00:19 +02:00
|
|
|
if (unlikely (!dest->serialize (&c, plan.offsets.topDictInfo.offSize, acc.topDicts, plan.topdict_sizes, topSzr, plan.offsets)))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF top dict");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* String INDEX */
|
|
|
|
{
|
|
|
|
assert (plan.offsets.stringIndexOffset == c.head - c.start);
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1StringIndex *dest = c.start_embed<CFF1StringIndex> ();
|
2018-08-10 20:07:07 +02:00
|
|
|
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 */
|
|
|
|
{
|
2018-08-29 21:14:30 +02:00
|
|
|
assert (plan.offsets.globalSubrsInfo.offset != 0);
|
2018-08-15 21:00:19 +02:00
|
|
|
assert (plan.offsets.globalSubrsInfo.offset == c.head - c.start);
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1Subrs *dest = c.start_embed<CFF1Subrs> ();
|
2018-08-10 20:07:07 +02:00
|
|
|
if (unlikely (dest == nullptr)) return false;
|
2018-08-15 21:00:19 +02:00
|
|
|
if (unlikely (!dest->serialize (&c, *acc.globalSubrs, plan.offsets.globalSubrsInfo.offSize, plan.subrRefMaps.global_map, NULL_SUBR)))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
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 */
|
2018-08-16 09:13:09 +02:00
|
|
|
if (acc.fdSelect != &Null(CFF1FDSelect))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1FDSelect *dest = c.start_embed<CFF1FDSelect> ();
|
2018-08-10 20:07:07 +02:00
|
|
|
if (unlikely (!dest->serialize (&c, *acc.fdSelect, acc.num_glyphs)))
|
|
|
|
{
|
|
|
|
DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF FDSelect");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FDArray (FD Index) */
|
2018-08-16 09:13:09 +02:00
|
|
|
if (acc.fdArray != &Null(CFF1FDArray))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
assert (plan.offsets.FDArrayInfo.offset == c.head - c.start);
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1FDArray *fda = c.start_embed<CFF1FDArray> ();
|
2018-08-10 20:07:07 +02:00
|
|
|
if (unlikely (fda == nullptr)) return false;
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1FontDict_OpSerializer fontSzr;
|
2018-08-15 21:00:19 +02:00
|
|
|
if (unlikely (!fda->serialize (&c, plan.offsets.FDArrayInfo.offSize,
|
2018-08-10 20:07:07 +02:00
|
|
|
acc.fontDicts, plan.subst_fdcount, plan.fdmap,
|
|
|
|
fontSzr, plan.privateDictInfos)))
|
|
|
|
{
|
|
|
|
DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF FDArray");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* CharStrings */
|
|
|
|
{
|
2018-08-15 21:00:19 +02:00
|
|
|
assert (plan.offsets.charStringsInfo.offset == c.head - c.start);
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1CharStrings *cs = c.start_embed<CFF1CharStrings> ();
|
2018-08-10 20:07:07 +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-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
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++)
|
|
|
|
{
|
2018-08-10 21:55:22 +02:00
|
|
|
if (!plan.fdmap.excludes (i))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2018-08-10 21:55:22 +02:00
|
|
|
PrivateDict *pd = c.start_embed<PrivateDict> ();
|
|
|
|
if (unlikely (pd == nullptr)) return false;
|
2018-08-29 21:14:30 +02:00
|
|
|
unsigned int priv_size = plan.flatten_subrs? 0: plan.privateDictInfos[plan.fdmap[i]].size;
|
2018-08-18 01:50:13 +02:00
|
|
|
bool result;
|
2018-08-29 21:14:30 +02:00
|
|
|
CFF1PrivateDict_OpSerializer privSzr (plan.drop_hints, plan.flatten_subrs);
|
2018-08-10 21:55:22 +02:00
|
|
|
/* N.B. local subrs immediately follows its corresponding private dict. i.e., subr offset == private dict size */
|
2018-08-29 21:14:30 +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-10 20:07:07 +02:00
|
|
|
{
|
2018-08-10 21:55:22 +02:00
|
|
|
DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF Private Dict[%d]", i);
|
2018-08-10 20:07:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-08-29 21:14:30 +02:00
|
|
|
if (!plan.flatten_subrs && (acc.privateDicts[i].subrsOffset != 0))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2018-08-16 09:13:09 +02:00
|
|
|
CFF1Subrs *subrs = c.start_embed<CFF1Subrs> ();
|
|
|
|
if (unlikely (subrs == nullptr) || acc.privateDicts[i].localSubrs == &Null(CFF1Subrs))
|
2018-08-10 21:55:22 +02:00
|
|
|
{
|
|
|
|
DEBUG_MSG (SUBSET, nullptr, "CFF subset: local subrs unexpectedly null [%d]", i);
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-15 21:00:19 +02:00
|
|
|
if (unlikely (!subrs->serialize (&c, *acc.privateDicts[i].localSubrs, plan.offsets.localSubrsInfos[i].offSize, plan.subrRefMaps.local_maps[i], NULL_SUBR)))
|
2018-08-10 21:55:22 +02:00
|
|
|
{
|
|
|
|
DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF local subrs [%d]", i);
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-17 22:13:18 +02:00
|
|
|
assert (c.head == c.end);
|
2018-08-10 20:07:07 +02:00
|
|
|
c.end_serialize ();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2018-08-16 09:25:57 +02:00
|
|
|
_hb_subset_cff1 (const OT::cff1::accelerator_subset_t &acc,
|
2018-08-10 20:07:07 +02:00
|
|
|
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);
|
|
|
|
|
2018-08-16 09:13:09 +02:00
|
|
|
if (unlikely (!_write_cff1 (cff_plan, acc, plan->glyphs,
|
2018-08-10 20:07:07 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-16 09:13:09 +02:00
|
|
|
* hb_subset_cff1:
|
2018-08-10 20:07:07 +02:00
|
|
|
* Subsets the CFF table according to a provided plan.
|
|
|
|
*
|
|
|
|
* Return value: subsetted cff table.
|
|
|
|
**/
|
|
|
|
bool
|
2018-08-16 09:13:09 +02:00
|
|
|
hb_subset_cff1 (hb_subset_plan_t *plan,
|
2018-08-10 20:07:07 +02:00
|
|
|
hb_blob_t **prime /* OUT */)
|
|
|
|
{
|
2018-08-16 09:25:57 +02:00
|
|
|
hb_blob_t *cff_blob = hb_sanitize_context_t().reference_table<CFF::cff1> (plan->source);
|
2018-08-10 20:07:07 +02:00
|
|
|
const char *data = hb_blob_get_data(cff_blob, nullptr);
|
|
|
|
|
2018-08-16 09:25:57 +02:00
|
|
|
OT::cff1::accelerator_subset_t acc;
|
2018-08-10 20:07:07 +02:00
|
|
|
acc.init(plan->source);
|
|
|
|
bool result = likely (acc.is_valid ()) &&
|
2018-08-16 09:13:09 +02:00
|
|
|
_hb_subset_cff1 (acc, data, plan, prime);
|
2018-08-10 20:07:07 +02:00
|
|
|
hb_blob_destroy (cff_blob);
|
|
|
|
acc.fini ();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|