2018-08-10 20:07:07 +02:00
|
|
|
/*
|
2018-11-12 17:47:07 +01:00
|
|
|
* Copyright © 2018 Adobe Inc.
|
2018-08-10 20:07:07 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2019-06-18 22:07:44 +02:00
|
|
|
#include "hb.hh"
|
|
|
|
|
|
|
|
#ifndef HB_NO_SUBSET_CFF
|
|
|
|
|
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"
|
2019-06-18 07:12:40 +02:00
|
|
|
#include "hb-bimap.hh"
|
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;
|
|
|
|
|
2019-06-18 07:12:40 +02:00
|
|
|
struct remap_sid_t : hb_inc_bimap_t
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
2018-12-16 20:08:10 +01:00
|
|
|
unsigned int add (unsigned int sid)
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
|
|
|
if ((sid != CFF_UNDEF_SID) && !is_std_std (sid))
|
2019-06-18 07:12:40 +02:00
|
|
|
return offset_sid (hb_inc_bimap_t::add (unoffset_sid (sid)));
|
2018-09-11 01:00:20 +02:00
|
|
|
else
|
|
|
|
return sid;
|
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
unsigned int operator[] (unsigned int sid) const
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
2018-11-16 00:39:43 +01:00
|
|
|
if (is_std_std (sid) || (sid == CFF_UNDEF_SID))
|
2018-09-11 01:00:20 +02:00
|
|
|
return sid;
|
|
|
|
else
|
2019-06-20 23:33:09 +02:00
|
|
|
return offset_sid (get (unoffset_sid (sid)));
|
2018-09-11 01:00:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const unsigned int num_std_strings = 391;
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
static bool is_std_std (unsigned int sid) { return sid < num_std_strings; }
|
|
|
|
static unsigned int offset_sid (unsigned int sid) { return sid + num_std_strings; }
|
|
|
|
static unsigned int unoffset_sid (unsigned int sid) { return sid - num_std_strings; }
|
2018-09-11 01:00:20 +02:00
|
|
|
};
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
struct cff1_sub_table_info_t : cff_sub_table_info_t
|
2018-08-30 03:18:18 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
cff1_sub_table_info_t ()
|
|
|
|
: cff_sub_table_info_t (),
|
|
|
|
encoding_link (0),
|
|
|
|
charset_link (0)
|
|
|
|
{
|
2020-03-05 07:31:21 +01:00
|
|
|
privateDictInfo.init ();
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
objidx_t encoding_link;
|
|
|
|
objidx_t charset_link;
|
2020-03-05 07:31:21 +01:00
|
|
|
table_info_t privateDictInfo;
|
2018-08-10 20:07:07 +02:00
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
/* a copy of a parsed out cff1_top_dict_values_t augmented with additional operators */
|
|
|
|
struct cff1_top_dict_values_mod_t : cff1_top_dict_values_t
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
2020-04-20 11:42:45 +02:00
|
|
|
void init (const cff1_top_dict_values_t *base_= &Null (cff1_top_dict_values_t))
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
|
|
|
SUPER::init ();
|
|
|
|
base = base_;
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
void fini () { SUPER::fini (); }
|
2018-09-11 01:00:20 +02:00
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
unsigned get_count () const { return base->get_count () + SUPER::get_count (); }
|
2018-12-22 16:47:04 +01:00
|
|
|
const cff1_top_dict_val_t &get_value (unsigned int i) const
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
2018-11-01 06:30:34 +01:00
|
|
|
if (i < base->get_count ())
|
2018-09-11 01:00:20 +02:00
|
|
|
return (*base)[i];
|
|
|
|
else
|
2018-11-01 06:30:34 +01:00
|
|
|
return SUPER::values[i - base->get_count ()];
|
2018-09-11 01:00:20 +02:00
|
|
|
}
|
2018-12-22 16:47:04 +01:00
|
|
|
const cff1_top_dict_val_t &operator [] (unsigned int i) const { return get_value (i); }
|
2018-09-11 01:00:20 +02:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
void reassignSIDs (const remap_sid_t& sidmap)
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
|
2018-09-11 01:00:20 +02:00
|
|
|
nameSIDs[i] = sidmap[base->nameSIDs[i]];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-12-22 16:47:04 +01:00
|
|
|
typedef cff1_top_dict_values_t SUPER;
|
|
|
|
const cff1_top_dict_values_t *base;
|
2018-09-11 01:00:20 +02:00
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
struct top_dict_modifiers_t
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
top_dict_modifiers_t (const cff1_sub_table_info_t &info_,
|
2018-12-22 16:47:04 +01:00
|
|
|
const unsigned int (&nameSIDs_)[name_dict_values_t::ValCount])
|
2020-03-03 01:51:19 +01:00
|
|
|
: info (info_),
|
2018-09-11 01:00:20 +02:00
|
|
|
nameSIDs (nameSIDs_)
|
|
|
|
{}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
const cff1_sub_table_info_t &info;
|
2018-12-22 16:47:04 +01:00
|
|
|
const unsigned int (&nameSIDs)[name_dict_values_t::ValCount];
|
2018-09-11 01:00:20 +02:00
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
struct cff1_top_dict_op_serializer_t : cff_top_dict_op_serializer_t<cff1_top_dict_val_t>
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2018-12-16 20:08:10 +01:00
|
|
|
bool serialize (hb_serialize_context_t *c,
|
2018-12-22 16:47:04 +01:00
|
|
|
const cff1_top_dict_val_t &opstr,
|
|
|
|
const top_dict_modifiers_t &mod) const
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
op_code_t op = opstr.op;
|
2018-09-11 01:00:20 +02:00
|
|
|
switch (op)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
case OpCode_charset:
|
2020-03-03 01:51:19 +01:00
|
|
|
if (mod.info.charset_link)
|
|
|
|
return_trace (FontDict::serialize_link4_op(c, op, mod.info.charset_link, whence_t::Absolute));
|
|
|
|
else
|
|
|
|
goto fall_back;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
case OpCode_Encoding:
|
2020-03-03 01:51:19 +01:00
|
|
|
if (mod.info.encoding_link)
|
|
|
|
return_trace (FontDict::serialize_link4_op(c, op, mod.info.encoding_link, whence_t::Absolute));
|
|
|
|
else
|
|
|
|
goto fall_back;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
case OpCode_Private:
|
2020-03-05 07:31:21 +01:00
|
|
|
return_trace (UnsizedByteStr::serialize_int2 (c, mod.info.privateDictInfo.size) &&
|
|
|
|
Dict::serialize_link4_op (c, op, mod.info.privateDictInfo.link, whence_t::Absolute));
|
2018-08-10 20:07:07 +02:00
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
case OpCode_version:
|
|
|
|
case OpCode_Notice:
|
|
|
|
case OpCode_Copyright:
|
|
|
|
case OpCode_FullName:
|
|
|
|
case OpCode_FamilyName:
|
|
|
|
case OpCode_Weight:
|
|
|
|
case OpCode_PostScript:
|
|
|
|
case OpCode_BaseFontName:
|
|
|
|
case OpCode_FontName:
|
2020-03-05 01:54:27 +01:00
|
|
|
return_trace (FontDict::serialize_int2_op (c, op, mod.nameSIDs[name_dict_values_t::name_op_to_index (op)]));
|
2018-09-11 01:00:20 +02:00
|
|
|
|
|
|
|
case OpCode_ROS:
|
2018-12-01 05:04:59 +01:00
|
|
|
{
|
|
|
|
/* for registry & ordering, reassigned SIDs are serialized
|
|
|
|
* for supplement, the original byte string is copied along with the op code */
|
2018-12-22 16:47:04 +01:00
|
|
|
op_str_t supp_op;
|
2018-12-01 05:04:59 +01:00
|
|
|
supp_op.op = op;
|
2018-12-22 16:47:04 +01:00
|
|
|
if ( unlikely (!(opstr.str.length >= opstr.last_arg_offset + 3)))
|
2018-12-11 21:20:20 +01:00
|
|
|
return_trace (false);
|
2018-12-22 16:47:04 +01:00
|
|
|
supp_op.str = byte_str_t (&opstr.str + opstr.last_arg_offset, opstr.str.length - opstr.last_arg_offset);
|
|
|
|
return_trace (UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::registry]) &&
|
|
|
|
UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::ordering]) &&
|
2018-12-11 21:20:20 +01:00
|
|
|
copy_opstr (c, supp_op));
|
2018-12-01 05:04:59 +01:00
|
|
|
}
|
2020-03-03 01:51:19 +01:00
|
|
|
fall_back:
|
2018-08-10 20:07:07 +02:00
|
|
|
default:
|
2020-03-03 01:51:19 +01:00
|
|
|
return_trace (cff_top_dict_op_serializer_t<cff1_top_dict_val_t>::serialize (c, opstr, mod.info));
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
return_trace (true);
|
|
|
|
}
|
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
struct cff1_font_dict_op_serializer_t : cff_font_dict_op_serializer_t
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
2018-12-16 20:08:10 +01:00
|
|
|
bool serialize (hb_serialize_context_t *c,
|
2018-12-22 16:47:04 +01:00
|
|
|
const op_str_t &opstr,
|
2020-03-03 01:51:19 +01:00
|
|
|
const cff1_font_dict_values_mod_t &mod) const
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
|
|
|
TRACE_SERIALIZE (this);
|
|
|
|
|
|
|
|
if (opstr.op == OpCode_FontName)
|
2020-02-28 02:34:26 +01:00
|
|
|
return_trace (FontDict::serialize_int2_op (c, opstr.op, mod.fontName));
|
2018-09-11 01:00:20 +02:00
|
|
|
else
|
2020-03-05 07:31:21 +01:00
|
|
|
return_trace (SUPER::serialize (c, opstr, mod.privateDictInfo));
|
2018-09-11 01:00:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-12-22 16:47:04 +01:00
|
|
|
typedef cff_font_dict_op_serializer_t SUPER;
|
2018-09-11 01:00:20 +02:00
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
struct cff1_cs_opset_flatten_t : cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t>
|
2018-08-18 01:50:13 +02:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
static void flush_args_and_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
|
2018-08-18 01:50:13 +02:00
|
|
|
{
|
2018-11-01 06:30:34 +01:00
|
|
|
if (env.arg_start > 0)
|
2018-09-19 02:24:30 +02:00
|
|
|
flush_width (env, param);
|
|
|
|
|
2018-08-29 21:14:30 +02:00
|
|
|
switch (op)
|
|
|
|
{
|
2018-08-31 22:28:16 +02:00
|
|
|
case OpCode_hstem:
|
|
|
|
case OpCode_hstemhm:
|
|
|
|
case OpCode_vstem:
|
|
|
|
case OpCode_vstemhm:
|
2018-08-29 21:14:30 +02:00
|
|
|
case OpCode_hintmask:
|
|
|
|
case OpCode_cntrmask:
|
2018-11-16 21:28:24 +01:00
|
|
|
case OpCode_dotsection:
|
2018-12-01 05:04:59 +01:00
|
|
|
if (param.drop_hints)
|
|
|
|
{
|
|
|
|
env.clear_args ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
HB_FALLTHROUGH;
|
2018-12-01 04:54:57 +01:00
|
|
|
|
2018-08-29 21:14:30 +02:00
|
|
|
default:
|
2018-12-01 05:04:59 +01:00
|
|
|
SUPER::flush_args_and_op (op, env, param);
|
|
|
|
break;
|
2018-08-29 21:14:30 +02:00
|
|
|
}
|
2018-08-18 01:50:13 +02:00
|
|
|
}
|
2018-12-22 16:47:04 +01:00
|
|
|
static void flush_args (cff1_cs_interp_env_t &env, flatten_param_t& param)
|
2018-08-18 01:50:13 +02:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
str_encoder_t encoder (param.flatStr);
|
2018-11-01 06:30:34 +01:00
|
|
|
for (unsigned int i = env.arg_start; i < env.argStack.get_count (); i++)
|
|
|
|
encoder.encode_num (env.eval_arg (i));
|
|
|
|
SUPER::flush_args (env, param);
|
2018-08-31 22:28:16 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
static void flush_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
|
2018-08-31 22:28:16 +02:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
str_encoder_t encoder (param.flatStr);
|
2018-11-01 06:30:34 +01:00
|
|
|
encoder.encode_op (op);
|
2018-08-31 22:28:16 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
static void flush_width (cff1_cs_interp_env_t &env, flatten_param_t& param)
|
2018-09-19 02:24:30 +02:00
|
|
|
{
|
|
|
|
assert (env.has_width);
|
2018-12-22 16:47:04 +01:00
|
|
|
str_encoder_t encoder (param.flatStr);
|
2018-11-01 06:30:34 +01:00
|
|
|
encoder.encode_num (env.width);
|
2018-09-19 02:24:30 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
static void flush_hintmask (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
|
2018-08-31 22:28:16 +02:00
|
|
|
{
|
|
|
|
SUPER::flush_hintmask (op, env, param);
|
2018-09-04 19:25:21 +02:00
|
|
|
if (!param.drop_hints)
|
2018-11-01 06:30:34 +01:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
str_encoder_t encoder (param.flatStr);
|
2018-09-04 19:25:21 +02:00
|
|
|
for (unsigned int i = 0; i < env.hintmask_size; i++)
|
2018-12-22 16:47:04 +01:00
|
|
|
encoder.encode_byte (env.str_ref[i]);
|
2018-11-01 06:30:34 +01:00
|
|
|
}
|
2018-08-18 01:50:13 +02:00
|
|
|
}
|
2018-08-29 21:14:30 +02:00
|
|
|
|
|
|
|
private:
|
2018-12-22 16:47:04 +01:00
|
|
|
typedef cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t> SUPER;
|
2018-08-18 01:50:13 +02:00
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
struct range_list_t : hb_vector_t<code_pair_t>
|
2018-09-12 22:22:19 +02:00
|
|
|
{
|
|
|
|
/* replace the first glyph ID in the "glyph" field each range with a nLeft value */
|
2019-02-21 00:48:29 +01:00
|
|
|
bool complete (unsigned int last_glyph)
|
2018-09-12 22:22:19 +02:00
|
|
|
{
|
2018-11-17 02:29:03 +01:00
|
|
|
bool two_byte = false;
|
2018-12-22 00:46:51 +01:00
|
|
|
for (unsigned int i = (*this).length; i > 0; i--)
|
2018-09-12 22:22:19 +02:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
code_pair_t &pair = (*this)[i - 1];
|
2018-09-12 22:22:19 +02:00
|
|
|
unsigned int nLeft = last_glyph - pair.glyph - 1;
|
2018-11-17 02:29:03 +01:00
|
|
|
if (nLeft >= 0x100)
|
2018-12-01 05:04:59 +01:00
|
|
|
two_byte = true;
|
2018-09-12 22:22:19 +02:00
|
|
|
last_glyph = pair.glyph;
|
|
|
|
pair.glyph = nLeft;
|
|
|
|
}
|
2018-11-17 02:29:03 +01:00
|
|
|
return two_byte;
|
2018-09-12 22:22:19 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
struct cff1_cs_opset_subr_subset_t : cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t>
|
2018-11-01 06:30:34 +01:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
static void process_op (op_code_t op, cff1_cs_interp_env_t &env, subr_subset_param_t& param)
|
2018-11-01 06:30:34 +01:00
|
|
|
{
|
|
|
|
switch (op) {
|
|
|
|
|
|
|
|
case OpCode_return:
|
2018-12-22 16:47:04 +01:00
|
|
|
param.current_parsed_str->add_op (op, env.str_ref);
|
2018-12-01 05:04:59 +01:00
|
|
|
param.current_parsed_str->set_parsed ();
|
2019-05-08 22:44:03 +02:00
|
|
|
env.return_from_subr ();
|
2018-12-11 21:21:24 +01:00
|
|
|
param.set_current_str (env, false);
|
2018-12-01 05:04:59 +01:00
|
|
|
break;
|
2018-11-01 06:30:34 +01:00
|
|
|
|
|
|
|
case OpCode_endchar:
|
2018-12-22 16:47:04 +01:00
|
|
|
param.current_parsed_str->add_op (op, env.str_ref);
|
2018-12-01 05:04:59 +01:00
|
|
|
param.current_parsed_str->set_parsed ();
|
|
|
|
SUPER::process_op (op, env, param);
|
|
|
|
break;
|
2018-11-01 06:30:34 +01:00
|
|
|
|
|
|
|
case OpCode_callsubr:
|
2018-12-01 05:04:59 +01:00
|
|
|
process_call_subr (op, CSType_LocalSubr, env, param, env.localSubrs, param.local_closure);
|
|
|
|
break;
|
2018-11-01 06:30:34 +01:00
|
|
|
|
|
|
|
case OpCode_callgsubr:
|
2018-12-01 05:04:59 +01:00
|
|
|
process_call_subr (op, CSType_GlobalSubr, env, param, env.globalSubrs, param.global_closure);
|
|
|
|
break;
|
2018-11-01 06:30:34 +01:00
|
|
|
|
|
|
|
default:
|
2018-12-01 05:04:59 +01:00
|
|
|
SUPER::process_op (op, env, param);
|
2018-12-22 16:47:04 +01:00
|
|
|
param.current_parsed_str->add_op (op, env.str_ref);
|
2018-12-01 05:04:59 +01:00
|
|
|
break;
|
2018-11-01 06:30:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-12-22 16:47:04 +01:00
|
|
|
static void process_call_subr (op_code_t op, cs_type_t type,
|
|
|
|
cff1_cs_interp_env_t &env, subr_subset_param_t& param,
|
|
|
|
cff1_biased_subrs_t& subrs, hb_set_t *closure)
|
2018-11-01 06:30:34 +01:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
byte_str_ref_t str_ref = env.str_ref;
|
2019-05-08 22:44:03 +02:00
|
|
|
env.call_subr (subrs, type);
|
2018-12-22 16:47:04 +01:00
|
|
|
param.current_parsed_str->add_call_op (op, str_ref, env.context.subr_num);
|
2019-10-08 11:54:26 +02:00
|
|
|
closure->add (env.context.subr_num);
|
2018-12-11 21:21:24 +01:00
|
|
|
param.set_current_str (env, true);
|
2018-11-01 06:30:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-12-22 16:47:04 +01:00
|
|
|
typedef cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t> SUPER;
|
2018-11-01 06:30:34 +01:00
|
|
|
};
|
|
|
|
|
2019-01-31 23:16:37 +01:00
|
|
|
struct cff1_subr_subsetter_t : subr_subsetter_t<cff1_subr_subsetter_t, CFF1Subrs, const OT::cff1::accelerator_subset_t, cff1_cs_interp_env_t, cff1_cs_opset_subr_subset_t, OpCode_endchar>
|
2018-11-01 06:30:34 +01:00
|
|
|
{
|
2019-05-14 08:47:28 +02:00
|
|
|
cff1_subr_subsetter_t (const OT::cff1::accelerator_subset_t &acc_, const hb_subset_plan_t *plan_)
|
|
|
|
: subr_subsetter_t (acc_, plan_) {}
|
2019-01-31 23:16:37 +01:00
|
|
|
|
2019-02-21 00:48:29 +01:00
|
|
|
static void complete_parsed_str (cff1_cs_interp_env_t &env, subr_subset_param_t& param, parsed_cs_str_t &charstring)
|
2018-11-01 06:30:34 +01:00
|
|
|
{
|
2018-11-04 06:42:22 +01:00
|
|
|
/* insert width at the beginning of the charstring as necessary */
|
2018-11-02 23:28:01 +01:00
|
|
|
if (env.has_width)
|
2018-11-01 06:30:34 +01:00
|
|
|
charstring.set_prefix (env.width);
|
2018-11-04 06:42:22 +01:00
|
|
|
|
|
|
|
/* subroutines/charstring left on the call stack are legally left unmarked
|
|
|
|
* unmarked when a subroutine terminates with endchar. mark them.
|
|
|
|
*/
|
|
|
|
param.current_parsed_str->set_parsed ();
|
|
|
|
for (unsigned int i = 0; i < env.callStack.get_count (); i++)
|
|
|
|
{
|
2020-04-20 21:18:23 +02:00
|
|
|
parsed_cs_str_t *parsed_str = param.get_parsed_str_for_context (env.callStack[i]);
|
|
|
|
if (likely (parsed_str))
|
2018-12-01 05:04:59 +01:00
|
|
|
parsed_str->set_parsed ();
|
2018-11-04 06:42:22 +01:00
|
|
|
else
|
2018-12-01 05:04:59 +01:00
|
|
|
env.set_error ();
|
2018-11-04 06:42:22 +01:00
|
|
|
}
|
2018-11-01 06:30:34 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
struct cff_subset_plan {
|
2018-12-17 19:01:01 +01:00
|
|
|
cff_subset_plan ()
|
2020-03-03 01:51:19 +01:00
|
|
|
: info (),
|
2018-10-03 06:08:36 +02:00
|
|
|
orig_fdcount (0),
|
2018-10-03 05:44:30 +02:00
|
|
|
subset_fdcount (1),
|
2018-10-02 23:38:06 +02:00
|
|
|
subset_fdselect_format (0),
|
2018-11-01 06:30:34 +01:00
|
|
|
drop_hints (false),
|
|
|
|
desubroutinize(false)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2018-09-07 02:28:15 +02:00
|
|
|
topdict_mod.init ();
|
2018-09-13 01:08:54 +02:00
|
|
|
subset_fdselect_ranges.init ();
|
2018-08-10 20:07:07 +02:00
|
|
|
fdmap.init ();
|
|
|
|
subset_charstrings.init ();
|
2018-11-01 06:30:34 +01:00
|
|
|
subset_globalsubrs.init ();
|
|
|
|
subset_localsubrs.init ();
|
2018-09-11 01:00:20 +02:00
|
|
|
fontdicts_mod.init ();
|
2018-09-07 02:28:15 +02:00
|
|
|
subset_enc_code_ranges.init ();
|
|
|
|
subset_enc_supp_codes.init ();
|
|
|
|
subset_charset_ranges.init ();
|
2018-09-11 01:00:20 +02:00
|
|
|
sidmap.init ();
|
2018-12-22 16:47:04 +01:00
|
|
|
for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
|
2018-09-11 01:00:20 +02:00
|
|
|
topDictModSIDs[i] = CFF_UNDEF_SID;
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-17 19:01:01 +01:00
|
|
|
~cff_subset_plan ()
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2018-09-07 02:28:15 +02:00
|
|
|
topdict_mod.fini ();
|
2018-09-13 01:08:54 +02:00
|
|
|
subset_fdselect_ranges.fini ();
|
2018-08-10 20:07:07 +02:00
|
|
|
fdmap.fini ();
|
2018-11-01 18:31:21 +01:00
|
|
|
subset_charstrings.fini_deep ();
|
|
|
|
subset_globalsubrs.fini_deep ();
|
2018-11-01 06:30:34 +01:00
|
|
|
subset_localsubrs.fini_deep ();
|
2018-09-11 01:00:20 +02:00
|
|
|
fontdicts_mod.fini ();
|
2018-09-07 02:28:15 +02:00
|
|
|
subset_enc_code_ranges.fini ();
|
2019-01-08 21:37:48 +01:00
|
|
|
subset_enc_supp_codes.fini ();
|
2018-09-07 02:28:15 +02:00
|
|
|
subset_charset_ranges.fini ();
|
2018-09-11 01:00:20 +02:00
|
|
|
sidmap.fini ();
|
2018-09-07 02:28:15 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
void plan_subset_encoding (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
|
2018-09-07 02:28:15 +02:00
|
|
|
{
|
|
|
|
const Encoding *encoding = acc.encoding;
|
|
|
|
unsigned int size0, size1, supp_size;
|
|
|
|
hb_codepoint_t code, last_code = CFF_UNDEF_CODE;
|
|
|
|
hb_vector_t<hb_codepoint_t> supp_codes;
|
|
|
|
|
|
|
|
subset_enc_code_ranges.resize (0);
|
|
|
|
supp_size = 0;
|
|
|
|
supp_codes.init ();
|
|
|
|
|
2019-01-31 23:16:37 +01:00
|
|
|
subset_enc_num_codes = plan->num_output_glyphs () - 1;
|
2018-09-07 02:28:15 +02:00
|
|
|
unsigned int glyph;
|
2019-01-31 23:16:37 +01:00
|
|
|
for (glyph = 1; glyph < plan->num_output_glyphs (); glyph++)
|
2018-09-07 02:28:15 +02:00
|
|
|
{
|
2019-02-01 23:50:01 +01:00
|
|
|
hb_codepoint_t old_glyph;
|
|
|
|
if (!plan->old_gid_for_new_gid (glyph, &old_glyph))
|
2018-09-07 02:28:15 +02:00
|
|
|
{
|
2019-12-31 13:23:02 +01:00
|
|
|
/* Retain the code for the old missing glyph ID */
|
2019-02-01 23:50:01 +01:00
|
|
|
old_glyph = glyph;
|
2019-01-31 23:16:37 +01:00
|
|
|
}
|
2019-02-01 23:50:01 +01:00
|
|
|
code = acc.glyph_to_code (old_glyph);
|
|
|
|
if (code == CFF_UNDEF_CODE)
|
2019-01-31 23:16:37 +01:00
|
|
|
{
|
2019-02-01 23:50:01 +01:00
|
|
|
subset_enc_num_codes = glyph - 1;
|
|
|
|
break;
|
2018-09-07 02:28:15 +02:00
|
|
|
}
|
|
|
|
|
2019-02-01 23:50:01 +01:00
|
|
|
if ((last_code == CFF_UNDEF_CODE) || (code != last_code + 1))
|
2018-09-07 02:28:15 +02:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
code_pair_t pair = { code, glyph };
|
2018-12-01 05:04:59 +01:00
|
|
|
subset_enc_code_ranges.push (pair);
|
2018-09-07 02:28:15 +02:00
|
|
|
}
|
|
|
|
last_code = code;
|
|
|
|
|
2020-04-20 11:42:45 +02:00
|
|
|
if (encoding != &Null (Encoding))
|
2018-09-07 02:28:15 +02:00
|
|
|
{
|
2019-02-01 23:50:01 +01:00
|
|
|
hb_codepoint_t sid = acc.glyph_to_sid (old_glyph);
|
2018-12-01 05:04:59 +01:00
|
|
|
encoding->get_supplement_codes (sid, supp_codes);
|
2018-12-22 00:46:51 +01:00
|
|
|
for (unsigned int i = 0; i < supp_codes.length; i++)
|
2018-12-01 05:04:59 +01:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
code_pair_t pair = { supp_codes[i], sid };
|
2018-12-01 05:04:59 +01:00
|
|
|
subset_enc_supp_codes.push (pair);
|
|
|
|
}
|
2018-12-22 00:46:51 +01:00
|
|
|
supp_size += SuppEncoding::static_size * supp_codes.length;
|
2018-09-07 02:28:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
supp_codes.fini ();
|
2018-09-12 22:22:19 +02:00
|
|
|
|
2019-02-21 00:48:29 +01:00
|
|
|
subset_enc_code_ranges.complete (glyph);
|
2018-09-07 02:28:15 +02:00
|
|
|
|
|
|
|
assert (subset_enc_num_codes <= 0xFF);
|
|
|
|
size0 = Encoding0::min_size + HBUINT8::static_size * subset_enc_num_codes;
|
2018-12-22 00:46:51 +01:00
|
|
|
size1 = Encoding1::min_size + Encoding1_Range::static_size * subset_enc_code_ranges.length;
|
2018-09-07 02:28:15 +02:00
|
|
|
|
|
|
|
if (size0 < size1)
|
|
|
|
subset_enc_format = 0;
|
|
|
|
else
|
|
|
|
subset_enc_format = 1;
|
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
void plan_subset_charset (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
|
2018-09-07 02:28:15 +02:00
|
|
|
{
|
|
|
|
unsigned int size0, size_ranges;
|
|
|
|
hb_codepoint_t sid, last_sid = CFF_UNDEF_CODE;
|
|
|
|
|
|
|
|
subset_charset_ranges.resize (0);
|
|
|
|
unsigned int glyph;
|
2019-01-31 23:16:37 +01:00
|
|
|
for (glyph = 1; glyph < plan->num_output_glyphs (); glyph++)
|
2018-09-07 02:28:15 +02:00
|
|
|
{
|
2019-02-01 23:50:01 +01:00
|
|
|
hb_codepoint_t old_glyph;
|
|
|
|
if (!plan->old_gid_for_new_gid (glyph, &old_glyph))
|
2019-01-31 23:16:37 +01:00
|
|
|
{
|
2019-12-31 13:23:02 +01:00
|
|
|
/* Retain the SID for the old missing glyph ID */
|
2019-02-01 23:50:01 +01:00
|
|
|
old_glyph = glyph;
|
2019-01-31 23:16:37 +01:00
|
|
|
}
|
2019-02-01 23:50:01 +01:00
|
|
|
sid = acc.glyph_to_sid (old_glyph);
|
2018-09-07 02:28:15 +02:00
|
|
|
|
2019-02-01 23:50:01 +01:00
|
|
|
if (!acc.is_CID ())
|
|
|
|
sid = sidmap.add (sid);
|
2018-09-11 01:00:20 +02:00
|
|
|
|
2019-02-01 23:50:01 +01:00
|
|
|
if ((last_sid == CFF_UNDEF_CODE) || (sid != last_sid + 1))
|
2018-09-07 02:28:15 +02:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
code_pair_t pair = { sid, glyph };
|
2018-12-01 05:04:59 +01:00
|
|
|
subset_charset_ranges.push (pair);
|
2018-09-07 02:28:15 +02:00
|
|
|
}
|
|
|
|
last_sid = sid;
|
|
|
|
}
|
|
|
|
|
2019-02-21 00:48:29 +01:00
|
|
|
bool two_byte = subset_charset_ranges.complete (glyph);
|
2018-09-07 02:28:15 +02:00
|
|
|
|
2019-01-31 23:16:37 +01:00
|
|
|
size0 = Charset0::min_size + HBUINT16::static_size * (plan->num_output_glyphs () - 1);
|
2018-09-07 02:28:15 +02:00
|
|
|
if (!two_byte)
|
2018-12-22 00:46:51 +01:00
|
|
|
size_ranges = Charset1::min_size + Charset1_Range::static_size * subset_charset_ranges.length;
|
2018-09-07 02:28:15 +02:00
|
|
|
else
|
2018-12-22 00:46:51 +01:00
|
|
|
size_ranges = Charset2::min_size + Charset2_Range::static_size * subset_charset_ranges.length;
|
2018-09-07 02:28:15 +02:00
|
|
|
|
|
|
|
if (size0 < size_ranges)
|
|
|
|
subset_charset_format = 0;
|
|
|
|
else if (!two_byte)
|
|
|
|
subset_charset_format = 1;
|
|
|
|
else
|
|
|
|
subset_charset_format = 2;
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
bool collect_sids_in_dicts (const OT::cff1::accelerator_subset_t &acc)
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
2019-06-18 07:12:40 +02:00
|
|
|
sidmap.reset ();
|
2018-12-01 04:54:57 +01:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
|
2018-09-11 01:00:20 +02:00
|
|
|
{
|
|
|
|
unsigned int sid = acc.topDict.nameSIDs[i];
|
|
|
|
if (sid != CFF_UNDEF_SID)
|
|
|
|
{
|
2018-12-01 05:04:59 +01:00
|
|
|
(void)sidmap.add (sid);
|
|
|
|
topDictModSIDs[i] = sidmap[sid];
|
2018-09-11 01:00:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 11:42:45 +02:00
|
|
|
if (acc.fdArray != &Null (CFF1FDArray))
|
2018-11-01 06:30:34 +01:00
|
|
|
for (unsigned int i = 0; i < orig_fdcount; i++)
|
2019-02-21 00:48:29 +01:00
|
|
|
if (fdmap.has (i))
|
2018-12-01 05:04:59 +01:00
|
|
|
(void)sidmap.add (acc.fontDicts[i].fontName);
|
2018-12-01 04:54:57 +01:00
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
bool create (const OT::cff1::accelerator_subset_t &acc,
|
2019-01-31 23:16:37 +01:00
|
|
|
hb_subset_plan_t *plan)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2019-01-31 23:16:37 +01:00
|
|
|
/* make sure notdef is first */
|
|
|
|
hb_codepoint_t old_glyph;
|
|
|
|
if (!plan->old_gid_for_new_gid (0, &old_glyph) || (old_glyph != 0)) return false;
|
2018-09-07 02:28:15 +02:00
|
|
|
|
2019-01-31 23:16:37 +01:00
|
|
|
num_glyphs = plan->num_output_glyphs ();
|
2018-08-10 20:07:07 +02:00
|
|
|
orig_fdcount = acc.fdCount;
|
2018-08-18 01:50:13 +02:00
|
|
|
drop_hints = plan->drop_hints;
|
2018-11-02 01:25:23 +01:00
|
|
|
desubroutinize = plan->desubroutinize;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
2018-09-07 02:28:15 +02:00
|
|
|
/* check whether the subset renumbers any glyph IDs */
|
|
|
|
gid_renum = false;
|
2019-01-31 23:16:37 +01:00
|
|
|
for (hb_codepoint_t new_glyph = 0; new_glyph < plan->num_output_glyphs (); new_glyph++)
|
2018-09-07 02:28:15 +02:00
|
|
|
{
|
2019-01-31 23:16:37 +01:00
|
|
|
if (!plan->old_gid_for_new_gid(new_glyph, &old_glyph))
|
2019-12-31 13:23:02 +01:00
|
|
|
continue;
|
2019-01-31 23:16:37 +01:00
|
|
|
if (new_glyph != old_glyph) {
|
2018-12-01 05:04:59 +01:00
|
|
|
gid_renum = true;
|
|
|
|
break;
|
2018-09-07 02:28:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subset_charset = gid_renum || !acc.is_predef_charset ();
|
2018-09-19 02:24:30 +02:00
|
|
|
subset_encoding = !acc.is_CID() && !acc.is_predef_encoding ();
|
2018-09-07 02:28:15 +02:00
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
/* top dict INDEX */
|
|
|
|
{
|
2018-09-07 02:28:15 +02:00
|
|
|
/* Add encoding/charset to a (copy of) top dict as necessary */
|
2018-09-11 01:00:20 +02:00
|
|
|
topdict_mod.init (&acc.topDict);
|
2018-11-01 06:30:34 +01:00
|
|
|
bool need_to_add_enc = (subset_encoding && !acc.topDict.has_op (OpCode_Encoding));
|
|
|
|
bool need_to_add_set = (subset_charset && !acc.topDict.has_op (OpCode_charset));
|
2018-09-07 02:28:15 +02:00
|
|
|
if (need_to_add_enc || need_to_add_set)
|
|
|
|
{
|
2018-12-01 05:04:59 +01:00
|
|
|
if (need_to_add_enc)
|
|
|
|
topdict_mod.add_op (OpCode_Encoding);
|
|
|
|
if (need_to_add_set)
|
|
|
|
topdict_mod.add_op (OpCode_charset);
|
2018-09-07 02:28:15 +02:00
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
/* Determine re-mapping of font index as fdmap among other info */
|
2020-04-20 11:42:45 +02:00
|
|
|
if (acc.fdSelect != &Null (CFF1FDSelect))
|
2018-11-16 22:46:58 +01:00
|
|
|
{
|
2019-01-31 23:16:37 +01:00
|
|
|
if (unlikely (!hb_plan_subset_cff_fdselect (plan,
|
2018-12-01 05:04:59 +01:00
|
|
|
orig_fdcount,
|
|
|
|
*acc.fdSelect,
|
|
|
|
subset_fdcount,
|
2020-03-03 01:51:19 +01:00
|
|
|
info.fd_select.size,
|
2018-12-01 05:04:59 +01:00
|
|
|
subset_fdselect_format,
|
|
|
|
subset_fdselect_ranges,
|
|
|
|
fdmap)))
|
|
|
|
return false;
|
2018-11-16 22:46:58 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
fdmap.identity (1);
|
2018-09-11 01:00:20 +02:00
|
|
|
|
|
|
|
/* remove unused SIDs & reassign SIDs */
|
|
|
|
{
|
|
|
|
/* SIDs for name strings in dicts are added before glyph names so they fit in 16-bit int range */
|
|
|
|
if (unlikely (!collect_sids_in_dicts (acc)))
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2019-06-18 07:12:40 +02:00
|
|
|
if (unlikely (sidmap.get_population () > 0x8000)) /* assumption: a dict won't reference that many strings */
|
2019-12-31 13:23:02 +01:00
|
|
|
return false;
|
2020-03-10 08:10:05 +01:00
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
if (subset_charset) plan_subset_charset (acc, plan);
|
2018-09-11 01:00:20 +02:00
|
|
|
|
|
|
|
topdict_mod.reassignSIDs (sidmap);
|
|
|
|
}
|
|
|
|
|
2018-11-01 06:30:34 +01:00
|
|
|
if (desubroutinize)
|
2018-08-29 21:14:30 +02:00
|
|
|
{
|
|
|
|
/* Flatten global & local subrs */
|
2019-01-31 23:16:37 +01:00
|
|
|
subr_flattener_t<const OT::cff1::accelerator_subset_t, cff1_cs_interp_env_t, cff1_cs_opset_flatten_t, OpCode_endchar>
|
|
|
|
flattener(acc, plan);
|
2018-11-01 06:30:34 +01:00
|
|
|
if (!flattener.flatten (subset_charstrings))
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2018-08-29 21:14:30 +02:00
|
|
|
}
|
2018-11-01 06:30:34 +01:00
|
|
|
else
|
|
|
|
{
|
2019-01-31 23:16:37 +01:00
|
|
|
cff1_subr_subsetter_t subr_subsetter (acc, plan);
|
|
|
|
|
2018-11-01 06:30:34 +01:00
|
|
|
/* Subset subrs: collect used subroutines, leaving all unused ones behind */
|
2019-01-31 23:16:37 +01:00
|
|
|
if (!subr_subsetter.subset ())
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2018-11-01 06:30:34 +01:00
|
|
|
|
|
|
|
/* encode charstrings, global subrs, local subrs with new subroutine numbers */
|
2019-01-31 23:16:37 +01:00
|
|
|
if (!subr_subsetter.encode_charstrings (subset_charstrings))
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2018-11-01 06:30:34 +01:00
|
|
|
|
|
|
|
if (!subr_subsetter.encode_globalsubrs (subset_globalsubrs))
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2018-11-01 06:30:34 +01:00
|
|
|
|
|
|
|
/* local subrs */
|
|
|
|
if (!subset_localsubrs.resize (orig_fdcount))
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2018-11-01 06:30:34 +01:00
|
|
|
for (unsigned int fd = 0; fd < orig_fdcount; fd++)
|
|
|
|
{
|
2018-12-01 05:04:59 +01:00
|
|
|
subset_localsubrs[fd].init ();
|
2019-02-21 00:48:29 +01:00
|
|
|
if (fdmap.has (fd))
|
2018-12-01 05:04:59 +01:00
|
|
|
{
|
|
|
|
if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd]))
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-01 06:30:34 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-11 01:27:49 +02:00
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
/* Encoding */
|
2020-03-03 01:51:19 +01:00
|
|
|
if (subset_encoding)
|
|
|
|
plan_subset_encoding (acc, plan);
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
/* private dicts & local subrs */
|
2020-03-03 01:51:19 +01:00
|
|
|
if (!acc.is_CID ())
|
|
|
|
fontdicts_mod.push (cff1_font_dict_values_mod_t ());
|
|
|
|
else
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
+ hb_iter (acc.fontDicts)
|
|
|
|
| hb_filter ([&] (const cff1_font_dict_values_t &_)
|
|
|
|
{ return fdmap.has (&_ - &acc.fontDicts[0]); } )
|
|
|
|
| hb_map ([&] (const cff1_font_dict_values_t &_)
|
2018-12-01 05:04:59 +01:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
cff1_font_dict_values_mod_t mod;
|
|
|
|
mod.init (&_, sidmap[_.fontName]);
|
|
|
|
return mod;
|
|
|
|
})
|
|
|
|
| hb_sink (fontdicts_mod)
|
|
|
|
;
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2019-01-31 23:16:37 +01:00
|
|
|
return ((subset_charstrings.length == plan->num_output_glyphs ())
|
2018-12-22 00:46:51 +01:00
|
|
|
&& (fontdicts_mod.length == subset_fdcount));
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
cff1_top_dict_values_mod_t topdict_mod;
|
2020-03-03 01:51:19 +01:00
|
|
|
cff1_sub_table_info_t info;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
2018-09-07 02:28:15 +02:00
|
|
|
unsigned int num_glyphs;
|
2018-08-10 20:07:07 +02:00
|
|
|
unsigned int orig_fdcount;
|
2018-09-07 02:28:15 +02:00
|
|
|
unsigned int subset_fdcount;
|
|
|
|
unsigned int subset_fdselect_format;
|
2018-12-22 16:47:04 +01:00
|
|
|
hb_vector_t<code_pair_t> subset_fdselect_ranges;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
|
|
|
/* font dict index remap table from fullset FDArray to subset FDArray.
|
2018-09-11 01:00:20 +02:00
|
|
|
* set to CFF_UNDEF_CODE if excluded from subset */
|
2019-06-18 07:12:40 +02:00
|
|
|
hb_inc_bimap_t fdmap;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
str_buff_vec_t subset_charstrings;
|
|
|
|
str_buff_vec_t subset_globalsubrs;
|
|
|
|
hb_vector_t<str_buff_vec_t> subset_localsubrs;
|
2020-03-03 01:51:19 +01:00
|
|
|
hb_vector_t<cff1_font_dict_values_mod_t> fontdicts_mod;
|
2018-08-15 21:00:19 +02:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
bool drop_hints;
|
2018-09-07 02:28:15 +02:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
bool gid_renum;
|
|
|
|
bool subset_encoding;
|
|
|
|
uint8_t subset_enc_format;
|
|
|
|
unsigned int subset_enc_num_codes;
|
|
|
|
range_list_t subset_enc_code_ranges;
|
|
|
|
hb_vector_t<code_pair_t> subset_enc_supp_codes;
|
2018-09-07 02:28:15 +02:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
uint8_t subset_charset_format;
|
|
|
|
range_list_t subset_charset_ranges;
|
|
|
|
bool subset_charset;
|
2018-09-11 01:00:20 +02:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
remap_sid_t sidmap;
|
|
|
|
unsigned int topDictModSIDs[name_dict_values_t::ValCount];
|
2018-11-01 06:30:34 +01:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
bool desubroutinize;
|
2018-08-10 20:07:07 +02:00
|
|
|
};
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
static bool _serialize_cff1 (hb_serialize_context_t *c,
|
|
|
|
cff_subset_plan &plan,
|
|
|
|
const OT::cff1::accelerator_subset_t &acc,
|
|
|
|
unsigned int num_glyphs)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
/* private dicts & local subrs */
|
|
|
|
for (int i = (int)acc.privateDicts.length; --i >= 0 ;)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
if (plan.fdmap.has (i))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
objidx_t subrs_link = 0;
|
|
|
|
if (plan.subset_localsubrs[i].length > 0)
|
|
|
|
{
|
|
|
|
CFF1Subrs *dest = c->start_embed <CFF1Subrs> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!dest)) return false;
|
2020-03-03 21:12:51 +01:00
|
|
|
c->push ();
|
|
|
|
if (likely (dest && dest->serialize (c, plan.subset_localsubrs[i])))
|
|
|
|
subrs_link = c->pop_pack ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-03 01:51:19 +01:00
|
|
|
}
|
|
|
|
|
2020-04-20 21:18:23 +02:00
|
|
|
PrivateDict *pd = c->start_embed<PrivateDict> ();
|
|
|
|
if (unlikely (!pd)) return false;
|
2020-03-03 21:12:51 +01:00
|
|
|
c->push ();
|
2020-03-03 01:51:19 +01:00
|
|
|
cff_private_dict_op_serializer_t privSzr (plan.desubroutinize, plan.drop_hints);
|
|
|
|
/* N.B. local subrs immediately follows its corresponding private dict. i.e., subr offset == private dict size */
|
2020-03-03 21:12:51 +01:00
|
|
|
if (likely (pd->serialize (c, acc.privateDicts[i], privSzr, subrs_link)))
|
|
|
|
{
|
|
|
|
unsigned fd = plan.fdmap[i];
|
2020-03-05 07:31:21 +01:00
|
|
|
plan.fontdicts_mod[fd].privateDictInfo.size = c->length ();
|
|
|
|
plan.fontdicts_mod[fd].privateDictInfo.link = c->pop_pack ();
|
2020-03-03 21:12:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
if (!acc.is_CID ())
|
2020-03-05 07:31:21 +01:00
|
|
|
plan.info.privateDictInfo = plan.fontdicts_mod[0].privateDictInfo;
|
2020-03-03 01:51:19 +01:00
|
|
|
|
|
|
|
/* CharStrings */
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
CFF1CharStrings *cs = c->start_embed<CFF1CharStrings> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!cs)) return false;
|
2020-03-03 21:12:51 +01:00
|
|
|
c->push ();
|
|
|
|
if (likely (cs->serialize (c, plan.subset_charstrings)))
|
|
|
|
plan.info.char_strings_link = c->pop_pack ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
/* FDArray (FD Index) */
|
2020-04-20 11:42:45 +02:00
|
|
|
if (acc.fdArray != &Null (CFF1FDArray))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2020-04-20 21:18:23 +02:00
|
|
|
CFF1FDArray *fda = c->start_embed<CFF1FDArray> ();
|
|
|
|
if (unlikely (!fda)) return false;
|
2020-03-03 21:12:51 +01:00
|
|
|
c->push ();
|
2020-03-03 01:51:19 +01:00
|
|
|
cff1_font_dict_op_serializer_t fontSzr;
|
|
|
|
auto it = + hb_zip (+ hb_iter (plan.fontdicts_mod), + hb_iter (plan.fontdicts_mod));
|
2020-03-03 21:12:51 +01:00
|
|
|
if (likely (fda->serialize (c, it, fontSzr)))
|
|
|
|
plan.info.fd_array_link = c->pop_pack (false);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
/* FDSelect */
|
2020-04-20 11:42:45 +02:00
|
|
|
if (acc.fdSelect != &Null (CFF1FDSelect))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
c->push ();
|
2020-03-03 21:12:51 +01:00
|
|
|
if (likely (hb_serialize_cff_fdselect (c, num_glyphs, *acc.fdSelect, acc.fdCount,
|
|
|
|
plan.subset_fdselect_format, plan.info.fd_select.size,
|
|
|
|
plan.subset_fdselect_ranges)))
|
|
|
|
plan.info.fd_select.link = c->pop_pack ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2020-03-03 01:51:19 +01:00
|
|
|
}
|
2018-12-01 04:54:57 +01:00
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
/* Charset */
|
|
|
|
if (plan.subset_charset)
|
|
|
|
{
|
|
|
|
Charset *dest = c->start_embed<Charset> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!dest)) return false;
|
2020-03-03 21:12:51 +01:00
|
|
|
c->push ();
|
|
|
|
if (likely (dest->serialize (c,
|
|
|
|
plan.subset_charset_format,
|
|
|
|
plan.num_glyphs,
|
|
|
|
plan.subset_charset_ranges)))
|
|
|
|
plan.info.charset_link = c->pop_pack ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Encoding */
|
2018-09-07 02:28:15 +02:00
|
|
|
if (plan.subset_encoding)
|
|
|
|
{
|
2020-02-03 07:00:53 +01:00
|
|
|
Encoding *dest = c->start_embed<Encoding> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!dest)) return false;
|
2020-03-03 01:51:19 +01:00
|
|
|
c->push ();
|
2020-03-03 21:12:51 +01:00
|
|
|
if (likely (dest->serialize (c,
|
|
|
|
plan.subset_enc_format,
|
|
|
|
plan.subset_enc_num_codes,
|
|
|
|
plan.subset_enc_code_ranges,
|
|
|
|
plan.subset_enc_supp_codes)))
|
|
|
|
plan.info.encoding_link = c->pop_pack ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
/* global subrs */
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
c->push ();
|
|
|
|
CFF1Subrs *dest = c->start_embed <CFF1Subrs> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!dest)) return false;
|
2020-03-03 21:12:51 +01:00
|
|
|
if (likely (dest->serialize (c, plan.subset_globalsubrs)))
|
|
|
|
c->pop_pack ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
/* String INDEX */
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
CFF1StringIndex *dest = c->start_embed<CFF1StringIndex> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!dest)) return false;
|
2020-03-03 21:12:51 +01:00
|
|
|
c->push ();
|
|
|
|
if (likely (dest->serialize (c, *acc.stringIndex, plan.sidmap)))
|
|
|
|
c->pop_pack ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
OT::cff1 *cff = c->allocate_min<OT::cff1> ();
|
|
|
|
if (unlikely (!cff))
|
|
|
|
return false;
|
2018-08-10 20:07:07 +02:00
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
/* header */
|
|
|
|
cff->version.major = 0x01;
|
|
|
|
cff->version.minor = 0x00;
|
|
|
|
cff->nameIndex = cff->min_size;
|
|
|
|
cff->offSize = 4; /* unused? */
|
|
|
|
|
|
|
|
/* name INDEX */
|
|
|
|
if (unlikely (!(*acc.nameIndex).copy (c))) return false;
|
|
|
|
|
|
|
|
/* top dict INDEX */
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
/* serialize singleton TopDict */
|
|
|
|
TopDict *top = c->start_embed<TopDict> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (!top) return false;
|
2020-03-03 21:12:51 +01:00
|
|
|
c->push ();
|
2020-03-03 01:51:19 +01:00
|
|
|
cff1_top_dict_op_serializer_t topSzr;
|
2020-03-03 21:12:51 +01:00
|
|
|
unsigned top_size = 0;
|
2020-03-03 01:51:19 +01:00
|
|
|
top_dict_modifiers_t modifier (plan.info, plan.topDictModSIDs);
|
2020-03-03 21:12:51 +01:00
|
|
|
if (likely (top->serialize (c, plan.topdict_mod, topSzr, modifier)))
|
|
|
|
{
|
|
|
|
top_size = c->length ();
|
|
|
|
c->pop_pack (false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
2018-08-10 20:07:07 +02:00
|
|
|
return false;
|
2020-03-03 21:12:51 +01:00
|
|
|
}
|
2020-03-03 01:51:19 +01:00
|
|
|
/* serialize INDEX header for above */
|
|
|
|
CFF1Index *dest = c->start_embed<CFF1Index> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (!dest) return false;
|
2020-03-03 01:51:19 +01:00
|
|
|
return dest->serialize_header (c, hb_iter (hb_array_t<unsigned> (&top_size, 1)));
|
2018-11-01 06:30:34 +01:00
|
|
|
}
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
static bool
|
2018-08-16 09:25:57 +02:00
|
|
|
_hb_subset_cff1 (const OT::cff1::accelerator_subset_t &acc,
|
2020-02-03 07:00:53 +01:00
|
|
|
hb_subset_context_t *c)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
cff_subset_plan cff_plan;
|
|
|
|
|
2020-02-03 07:00:53 +01:00
|
|
|
if (unlikely (!cff_plan.create (acc, c->plan)))
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
|
|
|
DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff subsetting plan.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
return _serialize_cff1 (c->serializer, cff_plan, acc, c->plan->num_output_glyphs ());
|
2018-08-10 20:07:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2020-02-03 07:00:53 +01:00
|
|
|
hb_subset_cff1 (hb_subset_context_t *c)
|
2018-08-10 20:07:07 +02:00
|
|
|
{
|
2018-08-16 09:25:57 +02:00
|
|
|
OT::cff1::accelerator_subset_t acc;
|
2020-03-10 08:10:05 +01:00
|
|
|
acc.init (c->plan->source);
|
|
|
|
bool result = likely (acc.is_valid ()) && _hb_subset_cff1 (acc, c);
|
2018-08-10 20:07:07 +02:00
|
|
|
acc.fini ();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-05-12 19:29:47 +02:00
|
|
|
|
2019-06-18 22:07:44 +02:00
|
|
|
|
2019-05-12 19:29:47 +02:00
|
|
|
#endif
|