2018-07-18 23:17:52 +02:00
|
|
|
/*
|
2018-11-12 17:47:07 +01:00
|
|
|
* Copyright © 2018 Adobe Inc.
|
2018-07-18 23:17:52 +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-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;
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
struct cff2_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
|
|
|
cff2_sub_table_info_t ()
|
|
|
|
: cff_sub_table_info_t (),
|
|
|
|
var_store_link (0)
|
2018-08-30 03:18:18 +02:00
|
|
|
{}
|
2018-08-17 22:13:18 +02:00
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
objidx_t var_store_link;
|
2018-08-10 20:07:07 +02:00
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
struct cff2_top_dict_op_serializer_t : cff_top_dict_op_serializer_t<>
|
2018-07-18 23:17:52 +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 cff2_sub_table_info_t &info) 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:
|
2020-03-03 01:51:19 +01:00
|
|
|
return_trace (FontDict::serialize_link4_op(c, opstr.op, info.var_store_link));
|
2018-08-01 20:30:38 +02:00
|
|
|
|
|
|
|
default:
|
2020-03-03 01:51:19 +01:00
|
|
|
return_trace (cff_top_dict_op_serializer_t<>::serialize (c, opstr, info));
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-10 20:05:19 +02:00
|
|
|
struct cff2_cs_opset_flatten_t : cff2_cs_opset_t<cff2_cs_opset_flatten_t, flatten_param_t, blend_arg_t>
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2022-05-10 20:05:19 +02:00
|
|
|
static void flush_args_and_op (op_code_t op, cff2_cs_interp_env_t<blend_arg_t> &env, flatten_param_t& 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:
|
2018-12-01 05:04:59 +01:00
|
|
|
/* dummy opcodes in CFF2. ignore */
|
|
|
|
break;
|
2018-12-01 04:54:57 +01:00
|
|
|
|
2018-08-31 22:28:16 +02:00
|
|
|
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-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-30 03:18:18 +02:00
|
|
|
default:
|
2018-12-01 05:04:59 +01:00
|
|
|
SUPER::flush_args_and_op (op, env, param);
|
|
|
|
break;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 20:05:19 +02:00
|
|
|
static void flush_args (cff2_cs_interp_env_t<blend_arg_t> &env, flatten_param_t& 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-12-22 16:47:04 +01:00
|
|
|
const blend_arg_t &arg = env.argStack[i];
|
2018-10-09 00:05:36 +02:00
|
|
|
if (arg.blending ())
|
2018-09-04 19:25:21 +02:00
|
|
|
{
|
2019-12-31 13:23:02 +01:00
|
|
|
if (unlikely (!((arg.numValues > 0) && (env.argStack.get_count () >= arg.numValues))))
|
|
|
|
{
|
2018-12-11 21:20:20 +01:00
|
|
|
env.set_error ();
|
|
|
|
return;
|
|
|
|
}
|
2018-12-01 05:04:59 +01:00
|
|
|
flatten_blends (arg, i, env, param);
|
|
|
|
i += arg.numValues;
|
2018-09-04 19:25:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
str_encoder_t encoder (param.flatStr);
|
2018-12-01 05:04:59 +01:00
|
|
|
encoder.encode_num (arg);
|
|
|
|
i++;
|
2018-09-04 19:25:21 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-01 06:30:34 +01:00
|
|
|
SUPER::flush_args (env, param);
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
2022-05-10 20:05:19 +02:00
|
|
|
static void flatten_blends (const blend_arg_t &arg, unsigned int i, cff2_cs_interp_env_t<blend_arg_t> &env, flatten_param_t& param)
|
2018-09-04 19:25:21 +02:00
|
|
|
{
|
|
|
|
/* flatten the default values */
|
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 j = 0; j < arg.numValues; j++)
|
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
const blend_arg_t &arg1 = env.argStack[i + j];
|
2018-12-11 21:20:20 +01:00
|
|
|
if (unlikely (!((arg1.blending () && (arg.numValues == arg1.numValues) && (arg1.valueIndex == j) &&
|
2018-12-22 00:46:51 +01:00
|
|
|
(arg1.deltas.length == env.get_region_count ())))))
|
2018-12-11 21:20:20 +01:00
|
|
|
{
|
2019-12-31 13:23:02 +01:00
|
|
|
env.set_error ();
|
|
|
|
return;
|
2018-12-11 21:20:20 +01:00
|
|
|
}
|
2018-11-01 06:30:34 +01:00
|
|
|
encoder.encode_num (arg1);
|
2018-09-04 19:25:21 +02:00
|
|
|
}
|
|
|
|
/* flatten deltas for each value */
|
|
|
|
for (unsigned int j = 0; j < arg.numValues; j++)
|
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
const blend_arg_t &arg1 = env.argStack[i + j];
|
2018-12-22 00:46:51 +01:00
|
|
|
for (unsigned int k = 0; k < arg1.deltas.length; k++)
|
2018-12-01 05:04:59 +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-12-01 04:54:57 +01:00
|
|
|
|
2022-05-10 20:05:19 +02:00
|
|
|
static void flush_op (op_code_t op, cff2_cs_interp_env_t<blend_arg_t> &env, flatten_param_t& 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:
|
2018-12-01 05:04:59 +01:00
|
|
|
return;
|
2018-09-04 19:25:21 +02:00
|
|
|
default:
|
2018-12-22 16:47:04 +01:00
|
|
|
str_encoder_t encoder (param.flatStr);
|
2018-12-01 05:04:59 +01:00
|
|
|
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:
|
2022-05-10 20:05:19 +02:00
|
|
|
typedef cff2_cs_opset_t<cff2_cs_opset_flatten_t, flatten_param_t, blend_arg_t> SUPER;
|
|
|
|
typedef cs_opset_t<blend_arg_t, cff2_cs_opset_flatten_t, cff2_cs_opset_flatten_t, cff2_cs_interp_env_t<blend_arg_t>, flatten_param_t> CSOPSET;
|
2018-08-18 01:50:13 +02:00
|
|
|
};
|
|
|
|
|
2022-05-10 20:05:19 +02:00
|
|
|
struct cff2_cs_opset_subr_subset_t : cff2_cs_opset_t<cff2_cs_opset_subr_subset_t, subr_subset_param_t, blend_arg_t>
|
2018-11-07 23:48:37 +01:00
|
|
|
{
|
2022-05-10 20:05:19 +02:00
|
|
|
static void process_op (op_code_t op, cff2_cs_interp_env_t<blend_arg_t> &env, subr_subset_param_t& param)
|
2018-11-07 23:48:37 +01:00
|
|
|
{
|
|
|
|
switch (op) {
|
|
|
|
|
|
|
|
case OpCode_return:
|
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-07 23:48:37 +01:00
|
|
|
|
|
|
|
case OpCode_endchar:
|
2018-12-01 05:04:59 +01:00
|
|
|
param.current_parsed_str->set_parsed ();
|
|
|
|
SUPER::process_op (op, env, param);
|
|
|
|
break;
|
2018-11-07 23:48:37 +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-07 23:48:37 +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-07 23:48:37 +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-07 23:48:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-12-22 16:47:04 +01:00
|
|
|
static void process_call_subr (op_code_t op, cs_type_t type,
|
2022-05-10 20:05:19 +02:00
|
|
|
cff2_cs_interp_env_t<blend_arg_t> &env, subr_subset_param_t& param,
|
2018-12-22 16:47:04 +01:00
|
|
|
cff2_biased_subrs_t& subrs, hb_set_t *closure)
|
2018-11-07 23:48:37 +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-07 23:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-05-10 20:05:19 +02:00
|
|
|
typedef cff2_cs_opset_t<cff2_cs_opset_subr_subset_t, subr_subset_param_t, blend_arg_t> SUPER;
|
2018-11-07 23:48:37 +01:00
|
|
|
};
|
|
|
|
|
2022-05-10 20:05:19 +02:00
|
|
|
struct cff2_subr_subsetter_t : subr_subsetter_t<cff2_subr_subsetter_t, CFF2Subrs, const OT::cff2::accelerator_subset_t, cff2_cs_interp_env_t<blend_arg_t>, cff2_cs_opset_subr_subset_t>
|
2018-11-07 23:48:37 +01:00
|
|
|
{
|
2019-05-14 16:44:03 +02:00
|
|
|
cff2_subr_subsetter_t (const OT::cff2::accelerator_subset_t &acc_, const hb_subset_plan_t *plan_)
|
|
|
|
: subr_subsetter_t (acc_, plan_) {}
|
2019-01-31 23:16:37 +01:00
|
|
|
|
2022-05-10 20:05:19 +02:00
|
|
|
static void complete_parsed_str (cff2_cs_interp_env_t<blend_arg_t> &env, subr_subset_param_t& param, parsed_cs_str_t &charstring)
|
2018-11-07 23:48:37 +01:00
|
|
|
{
|
|
|
|
/* vsindex is inserted at the beginning of the charstring as necessary */
|
|
|
|
if (env.seen_vsindex ())
|
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
number_t ivs;
|
2018-11-07 23:48:37 +01:00
|
|
|
ivs.set_int ((int)env.get_ivs ());
|
|
|
|
charstring.set_prefix (ivs, OpCode_vsindexcs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-10 20:07:07 +02:00
|
|
|
struct cff2_subset_plan {
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
bool create (const OT::cff2::accelerator_subset_t &acc,
|
2018-12-01 05:04:59 +01:00
|
|
|
hb_subset_plan_t *plan)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2018-08-06 19:04:53 +02:00
|
|
|
orig_fdcount = acc.fdArray->count;
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2021-07-30 00:07:13 +02:00
|
|
|
drop_hints = plan->flags & HB_SUBSET_FLAGS_NO_HINTING;
|
2023-01-06 19:01:25 +01:00
|
|
|
desubroutinize = plan->flags & HB_SUBSET_FLAGS_DESUBROUTINIZE ||
|
2023-01-06 19:39:13 +01:00
|
|
|
plan->normalized_coords; // For instancing we need this path
|
2018-08-18 01:50:13 +02:00
|
|
|
|
2018-11-07 23:48:37 +01:00
|
|
|
if (desubroutinize)
|
2018-08-30 03:18:18 +02:00
|
|
|
{
|
|
|
|
/* Flatten global & local subrs */
|
2022-05-10 20:05:19 +02:00
|
|
|
subr_flattener_t<const OT::cff2::accelerator_subset_t, cff2_cs_interp_env_t<blend_arg_t>, cff2_cs_opset_flatten_t>
|
2019-01-31 23:16:37 +01:00
|
|
|
flattener(acc, plan);
|
2018-11-07 23:48:37 +01:00
|
|
|
if (!flattener.flatten (subset_charstrings))
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2018-08-30 03:18:18 +02:00
|
|
|
}
|
2018-11-07 23:48:37 +01:00
|
|
|
else
|
|
|
|
{
|
2019-01-31 23:16:37 +01:00
|
|
|
cff2_subr_subsetter_t subr_subsetter (acc, plan);
|
|
|
|
|
2018-11-07 23:48:37 +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-07 23:48:37 +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-07 23:48:37 +01:00
|
|
|
|
|
|
|
if (!subr_subsetter.encode_globalsubrs (subset_globalsubrs))
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2018-11-07 23:48:37 +01:00
|
|
|
|
|
|
|
/* local subrs */
|
|
|
|
if (!subset_localsubrs.resize (orig_fdcount))
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2018-11-07 23:48:37 +01:00
|
|
|
for (unsigned int fd = 0; fd < orig_fdcount; fd++)
|
|
|
|
{
|
2018-12-01 05:04:59 +01:00
|
|
|
subset_localsubrs[fd].init ();
|
2019-08-24 15:27:14 +02:00
|
|
|
if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd]))
|
|
|
|
return false;
|
2018-11-07 23:48:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
/* FDSelect */
|
2020-04-20 11:42:45 +02:00
|
|
|
if (acc.fdSelect != &Null (CFF2FDSelect))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2019-01-31 23:16:37 +01:00
|
|
|
if (unlikely (!hb_plan_subset_cff_fdselect (plan,
|
2020-07-18 19:44:52 +02:00
|
|
|
orig_fdcount,
|
|
|
|
*(const FDSelect *)acc.fdSelect,
|
|
|
|
subset_fdcount,
|
|
|
|
subset_fdselect_size,
|
|
|
|
subset_fdselect_format,
|
|
|
|
subset_fdselect_ranges,
|
|
|
|
fdmap)))
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
2018-11-16 22:46:58 +01:00
|
|
|
else
|
|
|
|
fdmap.identity (1);
|
2018-08-01 20:30:38 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
cff2_sub_table_info_t info;
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2022-01-18 17:27:32 +01:00
|
|
|
unsigned int orig_fdcount = 0;
|
|
|
|
unsigned int subset_fdcount = 1;
|
|
|
|
unsigned int subset_fdselect_size = 0;
|
|
|
|
unsigned int subset_fdselect_format = 0;
|
2018-12-22 16:47:04 +01:00
|
|
|
hb_vector_t<code_pair_t> subset_fdselect_ranges;
|
2018-08-06 19:04:53 +02:00
|
|
|
|
2019-06-18 07:12:40 +02:00
|
|
|
hb_inc_bimap_t fdmap;
|
2018-08-06 19:04:53 +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;
|
2018-08-17 22:13:18 +02:00
|
|
|
|
2022-01-18 17:27:32 +01:00
|
|
|
bool drop_hints = false;
|
|
|
|
bool desubroutinize = false;
|
2018-08-01 20:30:38 +02:00
|
|
|
};
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
static bool _serialize_cff2 (hb_serialize_context_t *c,
|
|
|
|
cff2_subset_plan &plan,
|
|
|
|
const OT::cff2::accelerator_subset_t &acc,
|
|
|
|
unsigned int num_glyphs)
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
/* private dicts & local subrs */
|
|
|
|
hb_vector_t<table_info_t> private_dict_infos;
|
2020-05-06 22:32:24 +02:00
|
|
|
if (unlikely (!private_dict_infos.resize (plan.subset_fdcount))) return false;
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
for (int i = (int)acc.privateDicts.length; --i >= 0 ;)
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
if (plan.fdmap.has (i))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
objidx_t subrs_link = 0;
|
2018-08-01 20:30:38 +02:00
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
if (plan.subset_localsubrs[i].length > 0)
|
|
|
|
{
|
|
|
|
CFF2Subrs *dest = c->start_embed <CFF2Subrs> ();
|
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_localsubrs[i])))
|
2022-11-22 01:46:32 +01:00
|
|
|
subrs_link = c->pop_pack (false);
|
2020-03-03 21:12:51 +01:00
|
|
|
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);
|
2020-03-03 21:12:51 +01:00
|
|
|
if (likely (pd->serialize (c, acc.privateDicts[i], privSzr, subrs_link)))
|
|
|
|
{
|
|
|
|
unsigned fd = plan.fdmap[i];
|
|
|
|
private_dict_infos[fd].size = c->length ();
|
|
|
|
private_dict_infos[fd].link = c->pop_pack ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-07 23:48:37 +01:00
|
|
|
}
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
2018-12-01 04:54:57 +01:00
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
/* CharStrings */
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2022-12-02 01:33:53 +01:00
|
|
|
c->push ();
|
|
|
|
|
|
|
|
unsigned total_size = CFF2CharStrings::total_size (plan.subset_charstrings);
|
|
|
|
if (unlikely (!c->start_zerocopy (total_size)))
|
|
|
|
return false;
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
CFF2CharStrings *cs = c->start_embed<CFF2CharStrings> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!cs)) return false;
|
2022-12-02 01:33:53 +01:00
|
|
|
|
2020-03-03 21:12:51 +01:00
|
|
|
if (likely (cs->serialize (c, plan.subset_charstrings)))
|
2022-11-22 01:46:32 +01:00
|
|
|
plan.info.char_strings_link = c->pop_pack (false);
|
2020-03-03 21:12:51 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FDSelect */
|
2020-04-20 11:42:45 +02:00
|
|
|
if (acc.fdSelect != &Null (CFF2FDSelect))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
c->push ();
|
2022-12-02 22:59:26 +01:00
|
|
|
if (likely (hb_serialize_cff_fdselect (c, num_glyphs, *(const FDSelect *)acc.fdSelect,
|
|
|
|
plan.orig_fdcount,
|
|
|
|
plan.subset_fdselect_format, plan.subset_fdselect_size,
|
|
|
|
plan.subset_fdselect_ranges)))
|
2020-03-03 21:12:51 +01:00
|
|
|
plan.info.fd_select.link = c->pop_pack ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->pop_discard ();
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
2020-03-10 08:10:05 +01:00
|
|
|
|
2018-08-01 20:30:38 +02:00
|
|
|
/* FDArray (FD Index) */
|
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
c->push ();
|
2020-04-20 21:18:23 +02:00
|
|
|
CFF2FDArray *fda = c->start_embed<CFF2FDArray> ();
|
|
|
|
if (unlikely (!fda)) return false;
|
2020-03-03 01:51:19 +01:00
|
|
|
cff_font_dict_op_serializer_t fontSzr;
|
|
|
|
auto it =
|
|
|
|
+ hb_zip (+ hb_iter (acc.fontDicts)
|
|
|
|
| hb_filter ([&] (const cff2_font_dict_values_t &_)
|
|
|
|
{ return plan.fdmap.has (&_ - &acc.fontDicts[0]); }),
|
|
|
|
hb_iter (private_dict_infos))
|
|
|
|
;
|
|
|
|
if (unlikely (!fda->serialize (c, it, fontSzr))) return false;
|
2022-11-29 00:18:21 +01:00
|
|
|
plan.info.fd_array_link = c->pop_pack (false);
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
/* variation store */
|
2020-04-20 11:42:45 +02:00
|
|
|
if (acc.varStore != &Null (CFF2VariationStore))
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
c->push ();
|
|
|
|
CFF2VariationStore *dest = c->start_embed<CFF2VariationStore> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!dest || !dest->serialize (c, acc.varStore))) return false;
|
2022-11-29 00:17:09 +01:00
|
|
|
plan.info.var_store_link = c->pop_pack (false);
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
OT::cff2 *cff2 = c->allocate_min<OT::cff2> ();
|
|
|
|
if (unlikely (!cff2)) return false;
|
|
|
|
|
|
|
|
/* header */
|
|
|
|
cff2->version.major = 0x02;
|
|
|
|
cff2->version.minor = 0x00;
|
|
|
|
cff2->topDict = OT::cff2::static_size;
|
|
|
|
|
|
|
|
/* top dict */
|
2018-08-01 20:30:38 +02:00
|
|
|
{
|
2020-03-03 01:51:19 +01:00
|
|
|
TopDict &dict = cff2 + cff2->topDict;
|
|
|
|
cff2_top_dict_op_serializer_t topSzr;
|
|
|
|
if (unlikely (!dict.serialize (c, acc.topDict, topSzr, plan.info))) return false;
|
|
|
|
cff2->topDictSize = c->head - (const char *)&dict;
|
2018-08-01 20:30:38 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
/* global subrs */
|
|
|
|
{
|
|
|
|
CFF2Subrs *dest = c->start_embed <CFF2Subrs> ();
|
2020-04-20 21:18:23 +02:00
|
|
|
if (unlikely (!dest)) return false;
|
2020-03-03 01:51:19 +01:00
|
|
|
return dest->serialize (c, plan.subset_globalsubrs);
|
|
|
|
}
|
2018-07-18 23:17:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
static bool
|
2018-08-03 01:28:10 +02:00
|
|
|
_hb_subset_cff2 (const OT::cff2::accelerator_subset_t &acc,
|
2020-02-03 07:00:53 +01:00
|
|
|
hb_subset_context_t *c)
|
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
|
|
|
|
2020-03-03 01:51:19 +01:00
|
|
|
if (unlikely (!cff2_plan.create (acc, c->plan))) return false;
|
|
|
|
return _serialize_cff2 (c->serializer, cff2_plan, acc, c->plan->num_output_glyphs ());
|
2018-07-18 23:17:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-02-03 07:00:53 +01:00
|
|
|
hb_subset_cff2 (hb_subset_context_t *c)
|
2018-07-18 23:17:52 +02:00
|
|
|
{
|
2022-01-20 19:47:17 +01:00
|
|
|
OT::cff2::accelerator_subset_t acc (c->plan->source);
|
|
|
|
return acc.is_valid () && _hb_subset_cff2 (acc, c);
|
2018-07-18 23:17:52 +02:00
|
|
|
}
|
2019-05-12 19:29:47 +02:00
|
|
|
|
|
|
|
#endif
|