blob: 09364a22cea1399e9f737238b29e024706c30d74 [file] [log] [blame]
Michiharu Ariza64c54122018-08-10 11:07:07 -07001/*
Michiharu Ariza0dfa5842018-11-12 08:47:07 -08002 * Copyright © 2018 Adobe Inc.
Michiharu Ariza64c54122018-08-10 11:07:07 -07003 *
4 * This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Adobe Author(s): Michiharu Ariza
25 */
26
Michiharu Ariza8af96902018-08-29 13:26:17 -070027#include "hb-open-type.hh"
Michiharu Arizafdbfa182018-08-16 00:13:09 -070028#include "hb-ot-cff1-table.hh"
Michiharu Ariza64c54122018-08-10 11:07:07 -070029#include "hb-set.h"
Michiharu Arizafdbfa182018-08-16 00:13:09 -070030#include "hb-subset-cff1.hh"
Michiharu Ariza64c54122018-08-10 11:07:07 -070031#include "hb-subset-plan.hh"
Michiharu Ariza8af96902018-08-29 13:26:17 -070032#include "hb-subset-cff-common.hh"
Michiharu Arizafdbfa182018-08-16 00:13:09 -070033#include "hb-cff1-interp-cs.hh"
Michiharu Ariza64c54122018-08-10 11:07:07 -070034
35using namespace CFF;
36
Michiharu Ariza1666b892018-09-10 16:00:20 -070037struct RemapSID : Remap
38{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033039 unsigned int add (unsigned int sid)
Michiharu Ariza1666b892018-09-10 16:00:20 -070040 {
41 if ((sid != CFF_UNDEF_SID) && !is_std_std (sid))
42 return offset_sid (Remap::add (unoffset_sid (sid)));
43 else
44 return sid;
45 }
46
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033047 unsigned int operator[] (unsigned int sid) const
Michiharu Ariza1666b892018-09-10 16:00:20 -070048 {
Michiharu Ariza9d0231c2018-11-15 15:39:43 -080049 if (is_std_std (sid) || (sid == CFF_UNDEF_SID))
Michiharu Ariza1666b892018-09-10 16:00:20 -070050 return sid;
51 else
52 return offset_sid (Remap::operator [] (unoffset_sid (sid)));
53 }
54
55 static const unsigned int num_std_strings = 391;
56
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033057 static bool is_std_std (unsigned int sid) { return sid < num_std_strings; }
58 static unsigned int offset_sid (unsigned int sid) { return sid + num_std_strings; }
59 static unsigned int unoffset_sid (unsigned int sid) { return sid - num_std_strings; }
Michiharu Ariza1666b892018-09-10 16:00:20 -070060};
61
Michiharu Ariza9fd08cc2018-08-29 18:18:18 -070062struct CFF1SubTableOffsets : CFFSubTableOffsets
63{
Ebrahim Byagowie4120082018-12-17 21:31:01 +033064 CFF1SubTableOffsets ()
Michiharu Ariza9fd08cc2018-08-29 18:18:18 -070065 : CFFSubTableOffsets (),
66 nameIndexOffset (0),
Michiharu Ariza1666b892018-09-10 16:00:20 -070067 encodingOffset (0)
Michiharu Ariza64c54122018-08-10 11:07:07 -070068 {
Michiharu Ariza1666b892018-09-10 16:00:20 -070069 stringIndexInfo.init ();
70 charsetInfo.init ();
Michiharu Ariza9fd08cc2018-08-29 18:18:18 -070071 privateDictInfo.init ();
Michiharu Ariza64c54122018-08-10 11:07:07 -070072 }
73
74 unsigned int nameIndexOffset;
Michiharu Ariza1666b892018-09-10 16:00:20 -070075 TableInfo stringIndexInfo;
Michiharu Ariza64c54122018-08-10 11:07:07 -070076 unsigned int encodingOffset;
Michiharu Ariza1666b892018-09-10 16:00:20 -070077 TableInfo charsetInfo;
Michiharu Ariza64c54122018-08-10 11:07:07 -070078 TableInfo privateDictInfo;
79};
80
Michiharu Ariza1666b892018-09-10 16:00:20 -070081/* a copy of a parsed out CFF1TopDictValues augmented with additional operators */
82struct CFF1TopDictValuesMod : CFF1TopDictValues
83{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033084 void init (const CFF1TopDictValues *base_= &Null(CFF1TopDictValues))
Michiharu Ariza1666b892018-09-10 16:00:20 -070085 {
86 SUPER::init ();
87 base = base_;
88 }
89
Ebrahim Byagowie4120082018-12-17 21:31:01 +033090 void fini () { SUPER::fini (); }
Michiharu Ariza1666b892018-09-10 16:00:20 -070091
Ebrahim Byagowie4120082018-12-17 21:31:01 +033092 unsigned get_count () const { return base->get_count () + SUPER::get_count (); }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033093 const CFF1TopDictVal &get_value (unsigned int i) const
Michiharu Ariza1666b892018-09-10 16:00:20 -070094 {
Michiharu Arizad56e3382018-10-31 22:30:34 -070095 if (i < base->get_count ())
Michiharu Ariza1666b892018-09-10 16:00:20 -070096 return (*base)[i];
97 else
Michiharu Arizad56e3382018-10-31 22:30:34 -070098 return SUPER::values[i - base->get_count ()];
Michiharu Ariza1666b892018-09-10 16:00:20 -070099 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330100 const CFF1TopDictVal &operator [] (unsigned int i) const { return get_value (i); }
Michiharu Ariza1666b892018-09-10 16:00:20 -0700101
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330102 void reassignSIDs (const RemapSID& sidmap)
Michiharu Ariza1666b892018-09-10 16:00:20 -0700103 {
Michiharu Arizac6f75c32018-09-10 17:02:31 -0700104 for (unsigned int i = 0; i < NameDictValues::ValCount; i++)
Michiharu Ariza1666b892018-09-10 16:00:20 -0700105 nameSIDs[i] = sidmap[base->nameSIDs[i]];
106 }
107
108 protected:
109 typedef CFF1TopDictValues SUPER;
110 const CFF1TopDictValues *base;
111};
112
113struct TopDictModifiers
114{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330115 TopDictModifiers (const CFF1SubTableOffsets &offsets_,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500116 const unsigned int (&nameSIDs_)[NameDictValues::ValCount])
Michiharu Ariza1666b892018-09-10 16:00:20 -0700117 : offsets (offsets_),
118 nameSIDs (nameSIDs_)
119 {}
120
121 const CFF1SubTableOffsets &offsets;
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500122 const unsigned int (&nameSIDs)[NameDictValues::ValCount];
Michiharu Ariza1666b892018-09-10 16:00:20 -0700123};
124
125struct CFF1TopDict_OpSerializer : CFFTopDict_OpSerializer<CFF1TopDictVal>
Michiharu Ariza64c54122018-08-10 11:07:07 -0700126{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330127 bool serialize (hb_serialize_context_t *c,
128 const CFF1TopDictVal &opstr,
129 const TopDictModifiers &mod) const
Michiharu Ariza64c54122018-08-10 11:07:07 -0700130 {
131 TRACE_SERIALIZE (this);
132
Michiharu Ariza1666b892018-09-10 16:00:20 -0700133 OpCode op = opstr.op;
134 switch (op)
Michiharu Ariza64c54122018-08-10 11:07:07 -0700135 {
136 case OpCode_charset:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500137 return_trace (FontDict::serialize_offset4_op(c, op, mod.offsets.charsetInfo.offset));
Michiharu Ariza64c54122018-08-10 11:07:07 -0700138
139 case OpCode_Encoding:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500140 return_trace (FontDict::serialize_offset4_op(c, op, mod.offsets.encodingOffset));
Michiharu Ariza64c54122018-08-10 11:07:07 -0700141
Michiharu Ariza64c54122018-08-10 11:07:07 -0700142 case OpCode_Private:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500143 {
144 if (unlikely (!UnsizedByteStr::serialize_int2 (c, mod.offsets.privateDictInfo.size)))
145 return_trace (false);
146 if (unlikely (!UnsizedByteStr::serialize_int4 (c, mod.offsets.privateDictInfo.offset)))
147 return_trace (false);
148 HBUINT8 *p = c->allocate_size<HBUINT8> (1);
149 if (unlikely (p == nullptr)) return_trace (false);
150 p->set (OpCode_Private);
151 }
152 break;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700153
Michiharu Ariza1666b892018-09-10 16:00:20 -0700154 case OpCode_version:
155 case OpCode_Notice:
156 case OpCode_Copyright:
157 case OpCode_FullName:
158 case OpCode_FamilyName:
159 case OpCode_Weight:
160 case OpCode_PostScript:
161 case OpCode_BaseFontName:
162 case OpCode_FontName:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500163 return_trace (FontDict::serialize_offset2_op(c, op, mod.nameSIDs[NameDictValues::name_op_to_index (op)]));
Michiharu Ariza1666b892018-09-10 16:00:20 -0700164
165 case OpCode_ROS:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500166 {
167 /* for registry & ordering, reassigned SIDs are serialized
168 * for supplement, the original byte string is copied along with the op code */
169 OpStr supp_op;
170 supp_op.op = op;
171 supp_op.str.str = opstr.str.str + opstr.last_arg_offset;
Michiharu Ariza33358622018-12-11 12:20:20 -0800172 if ( unlikely (!(opstr.str.len >= opstr.last_arg_offset + 3)))
173 return_trace (false);
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500174 supp_op.str.len = opstr.str.len - opstr.last_arg_offset;
Michiharu Ariza33358622018-12-11 12:20:20 -0800175 return_trace (UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[NameDictValues::registry]) &&
176 UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[NameDictValues::ordering]) &&
177 copy_opstr (c, supp_op));
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500178 }
Michiharu Ariza64c54122018-08-10 11:07:07 -0700179 default:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500180 return_trace (CFFTopDict_OpSerializer<CFF1TopDictVal>::serialize (c, opstr, mod.offsets));
Michiharu Ariza64c54122018-08-10 11:07:07 -0700181 }
182 return_trace (true);
183 }
184
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330185 unsigned int calculate_serialized_size (const CFF1TopDictVal &opstr) const
Michiharu Ariza64c54122018-08-10 11:07:07 -0700186 {
Michiharu Ariza1666b892018-09-10 16:00:20 -0700187 OpCode op = opstr.op;
188 switch (op)
Michiharu Ariza64c54122018-08-10 11:07:07 -0700189 {
190 case OpCode_charset:
191 case OpCode_Encoding:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500192 return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (op);
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500193
Michiharu Ariza64c54122018-08-10 11:07:07 -0700194 case OpCode_Private:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500195 return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Private);
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500196
Michiharu Ariza1666b892018-09-10 16:00:20 -0700197 case OpCode_version:
198 case OpCode_Notice:
199 case OpCode_Copyright:
200 case OpCode_FullName:
201 case OpCode_FamilyName:
202 case OpCode_Weight:
203 case OpCode_PostScript:
204 case OpCode_BaseFontName:
205 case OpCode_FontName:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500206 return OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (op);
Michiharu Ariza1666b892018-09-10 16:00:20 -0700207
208 case OpCode_ROS:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500209 return ((OpCode_Size (OpCode_shortint) + 2) * 2) + (opstr.str.len - opstr.last_arg_offset)/* supplement + op */;
Michiharu Ariza1666b892018-09-10 16:00:20 -0700210
Michiharu Ariza64c54122018-08-10 11:07:07 -0700211 default:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500212 return CFFTopDict_OpSerializer<CFF1TopDictVal>::calculate_serialized_size (opstr);
Michiharu Ariza64c54122018-08-10 11:07:07 -0700213 }
214 }
215};
216
Michiharu Ariza1666b892018-09-10 16:00:20 -0700217struct FontDictValuesMod
218{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330219 void init (const CFF1FontDictValues *base_,
220 unsigned int fontName_,
221 const TableInfo &privateDictInfo_)
Michiharu Ariza1666b892018-09-10 16:00:20 -0700222 {
223 base = base_;
224 fontName = fontName_;
225 privateDictInfo = privateDictInfo_;
226 }
227
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330228 unsigned get_count () const { return base->get_count (); }
Michiharu Ariza1666b892018-09-10 16:00:20 -0700229
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330230 const OpStr &operator [] (unsigned int i) const { return (*base)[i]; }
Michiharu Ariza1666b892018-09-10 16:00:20 -0700231
232 const CFF1FontDictValues *base;
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500233 TableInfo privateDictInfo;
234 unsigned int fontName;
Michiharu Ariza1666b892018-09-10 16:00:20 -0700235};
236
237struct CFF1FontDict_OpSerializer : CFFFontDict_OpSerializer
238{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330239 bool serialize (hb_serialize_context_t *c,
240 const OpStr &opstr,
241 const FontDictValuesMod &mod) const
Michiharu Ariza1666b892018-09-10 16:00:20 -0700242 {
243 TRACE_SERIALIZE (this);
244
245 if (opstr.op == OpCode_FontName)
246 return_trace (FontDict::serialize_uint2_op (c, opstr.op, mod.fontName));
247 else
248 return_trace (SUPER::serialize (c, opstr, mod.privateDictInfo));
249 }
250
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330251 unsigned int calculate_serialized_size (const OpStr &opstr) const
Michiharu Ariza1666b892018-09-10 16:00:20 -0700252 {
253 if (opstr.op == OpCode_FontName)
254 return OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_FontName);
255 else
256 return SUPER::calculate_serialized_size (opstr);
257 }
258
259 private:
260 typedef CFFFontDict_OpSerializer SUPER;
261};
262
Michiharu Ariza8c5e03b2018-08-30 17:21:56 -0700263struct CFF1CSOpSet_Flatten : CFF1CSOpSet<CFF1CSOpSet_Flatten, FlattenParam>
Michiharu Ariza5cde2f52018-08-17 16:50:13 -0700264{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330265 static void flush_args_and_op (OpCode op, CFF1CSInterpEnv &env, FlattenParam& param)
Michiharu Ariza5cde2f52018-08-17 16:50:13 -0700266 {
Michiharu Arizad56e3382018-10-31 22:30:34 -0700267 if (env.arg_start > 0)
Michiharu Arizaebeccf32018-09-18 17:24:30 -0700268 flush_width (env, param);
269
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700270 switch (op)
271 {
Michiharu Ariza968168b2018-08-31 13:28:16 -0700272 case OpCode_hstem:
273 case OpCode_hstemhm:
274 case OpCode_vstem:
275 case OpCode_vstemhm:
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700276 case OpCode_hintmask:
277 case OpCode_cntrmask:
Michiharu Arizab67a7c72018-11-16 12:28:24 -0800278 case OpCode_dotsection:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500279 if (param.drop_hints)
280 {
281 env.clear_args ();
282 return;
283 }
284 HB_FALLTHROUGH;
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500285
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700286 default:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500287 SUPER::flush_args_and_op (op, env, param);
288 break;
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700289 }
Michiharu Ariza5cde2f52018-08-17 16:50:13 -0700290 }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330291 static void flush_args (CFF1CSInterpEnv &env, FlattenParam& param)
Michiharu Ariza5cde2f52018-08-17 16:50:13 -0700292 {
Michiharu Arizad56e3382018-10-31 22:30:34 -0700293 StrEncoder encoder (param.flatStr);
294 for (unsigned int i = env.arg_start; i < env.argStack.get_count (); i++)
295 encoder.encode_num (env.eval_arg (i));
296 SUPER::flush_args (env, param);
Michiharu Ariza968168b2018-08-31 13:28:16 -0700297 }
298
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330299 static void flush_op (OpCode op, CFF1CSInterpEnv &env, FlattenParam& param)
Michiharu Ariza968168b2018-08-31 13:28:16 -0700300 {
Michiharu Arizad56e3382018-10-31 22:30:34 -0700301 StrEncoder encoder (param.flatStr);
302 encoder.encode_op (op);
Michiharu Ariza968168b2018-08-31 13:28:16 -0700303 }
304
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330305 static void flush_width (CFF1CSInterpEnv &env, FlattenParam& param)
Michiharu Arizaebeccf32018-09-18 17:24:30 -0700306 {
307 assert (env.has_width);
Michiharu Arizad56e3382018-10-31 22:30:34 -0700308 StrEncoder encoder (param.flatStr);
309 encoder.encode_num (env.width);
Michiharu Arizaebeccf32018-09-18 17:24:30 -0700310 }
311
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330312 static void flush_hintmask (OpCode op, CFF1CSInterpEnv &env, FlattenParam& param)
Michiharu Ariza968168b2018-08-31 13:28:16 -0700313 {
314 SUPER::flush_hintmask (op, env, param);
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700315 if (!param.drop_hints)
Michiharu Arizad56e3382018-10-31 22:30:34 -0700316 {
317 StrEncoder encoder (param.flatStr);
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700318 for (unsigned int i = 0; i < env.hintmask_size; i++)
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500319 encoder.encode_byte (env.substr[i]);
Michiharu Arizad56e3382018-10-31 22:30:34 -0700320 }
Michiharu Ariza5cde2f52018-08-17 16:50:13 -0700321 }
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700322
323 private:
Michiharu Ariza8c5e03b2018-08-30 17:21:56 -0700324 typedef CFF1CSOpSet<CFF1CSOpSet_Flatten, FlattenParam> SUPER;
Michiharu Ariza5cde2f52018-08-17 16:50:13 -0700325};
326
Michiharu Ariza16084812018-09-12 13:22:19 -0700327struct RangeList : hb_vector_t<code_pair>
328{
329 /* replace the first glyph ID in the "glyph" field each range with a nLeft value */
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330330 bool finalize (unsigned int last_glyph)
Michiharu Ariza16084812018-09-12 13:22:19 -0700331 {
Michiharu Arizaa6da9b92018-11-16 17:29:03 -0800332 bool two_byte = false;
Michiharu Ariza16084812018-09-12 13:22:19 -0700333 for (unsigned int i = (*this).len; i > 0; i--)
334 {
335 code_pair &pair = (*this)[i - 1];
336 unsigned int nLeft = last_glyph - pair.glyph - 1;
Michiharu Arizaa6da9b92018-11-16 17:29:03 -0800337 if (nLeft >= 0x100)
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500338 two_byte = true;
Michiharu Ariza16084812018-09-12 13:22:19 -0700339 last_glyph = pair.glyph;
340 pair.glyph = nLeft;
341 }
Michiharu Arizaa6da9b92018-11-16 17:29:03 -0800342 return two_byte;
Michiharu Ariza16084812018-09-12 13:22:19 -0700343 }
344};
345
Michiharu Arizad56e3382018-10-31 22:30:34 -0700346struct CFF1CSOpSet_SubrSubset : CFF1CSOpSet<CFF1CSOpSet_SubrSubset, SubrSubsetParam>
347{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330348 static void process_op (OpCode op, CFF1CSInterpEnv &env, SubrSubsetParam& param)
Michiharu Arizad56e3382018-10-31 22:30:34 -0700349 {
350 switch (op) {
351
352 case OpCode_return:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500353 param.current_parsed_str->add_op (op, env.substr);
354 param.current_parsed_str->set_parsed ();
355 env.returnFromSubr ();
Michiharu Ariza29412082018-12-11 12:21:24 -0800356 param.set_current_str (env, false);
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500357 break;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700358
359 case OpCode_endchar:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500360 param.current_parsed_str->add_op (op, env.substr);
361 param.current_parsed_str->set_parsed ();
362 SUPER::process_op (op, env, param);
363 break;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700364
365 case OpCode_callsubr:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500366 process_call_subr (op, CSType_LocalSubr, env, param, env.localSubrs, param.local_closure);
367 break;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700368
369 case OpCode_callgsubr:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500370 process_call_subr (op, CSType_GlobalSubr, env, param, env.globalSubrs, param.global_closure);
371 break;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700372
373 default:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500374 SUPER::process_op (op, env, param);
375 param.current_parsed_str->add_op (op, env.substr);
376 break;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700377 }
378 }
379
Michiharu Arizad56e3382018-10-31 22:30:34 -0700380 protected:
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330381 static void process_call_subr (OpCode op, CSType type,
382 CFF1CSInterpEnv &env, SubrSubsetParam& param,
383 CFF1BiasedSubrs& subrs, hb_set_t *closure)
Michiharu Arizad56e3382018-10-31 22:30:34 -0700384 {
385 SubByteStr substr = env.substr;
386 env.callSubr (subrs, type);
Michiharu Ariza6186dbf2018-11-01 17:25:23 -0700387 param.current_parsed_str->add_call_op (op, substr, env.context.subr_num);
Michiharu Arizad56e3382018-10-31 22:30:34 -0700388 hb_set_add (closure, env.context.subr_num);
Michiharu Ariza29412082018-12-11 12:21:24 -0800389 param.set_current_str (env, true);
Michiharu Arizad56e3382018-10-31 22:30:34 -0700390 }
391
392 private:
393 typedef CFF1CSOpSet<CFF1CSOpSet_SubrSubset, SubrSubsetParam> SUPER;
394};
395
396struct CFF1SubrSubsetter : SubrSubsetter<CFF1SubrSubsetter, CFF1Subrs, const OT::cff1::accelerator_subset_t, CFF1CSInterpEnv, CFF1CSOpSet_SubrSubset>
397{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330398 static void finalize_parsed_str (CFF1CSInterpEnv &env, SubrSubsetParam& param, ParsedCStr &charstring)
Michiharu Arizad56e3382018-10-31 22:30:34 -0700399 {
Michiharu Ariza191ca0f2018-11-03 22:42:22 -0700400 /* insert width at the beginning of the charstring as necessary */
Michiharu Ariza1bc710a2018-11-02 15:28:01 -0700401 if (env.has_width)
Michiharu Arizad56e3382018-10-31 22:30:34 -0700402 charstring.set_prefix (env.width);
Michiharu Ariza191ca0f2018-11-03 22:42:22 -0700403
404 /* subroutines/charstring left on the call stack are legally left unmarked
405 * unmarked when a subroutine terminates with endchar. mark them.
406 */
407 param.current_parsed_str->set_parsed ();
408 for (unsigned int i = 0; i < env.callStack.get_count (); i++)
409 {
410 ParsedCStr *parsed_str = param.get_parsed_str_for_context (env.callStack[i]);
411 if (likely (parsed_str != nullptr))
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500412 parsed_str->set_parsed ();
Michiharu Ariza191ca0f2018-11-03 22:42:22 -0700413 else
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500414 env.set_error ();
Michiharu Ariza191ca0f2018-11-03 22:42:22 -0700415 }
Michiharu Arizad56e3382018-10-31 22:30:34 -0700416 }
417};
418
Michiharu Ariza64c54122018-08-10 11:07:07 -0700419struct cff_subset_plan {
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330420 cff_subset_plan ()
Michiharu Ariza64c54122018-08-10 11:07:07 -0700421 : final_size (0),
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700422 offsets (),
Michiharu Arizad7b384a2018-10-02 21:08:36 -0700423 orig_fdcount (0),
Michiharu Arizae9cc71a2018-10-02 20:44:30 -0700424 subset_fdcount (1),
Michiharu Ariza51d5bf42018-10-02 14:38:06 -0700425 subset_fdselect_format (0),
Michiharu Arizad56e3382018-10-31 22:30:34 -0700426 drop_hints (false),
427 desubroutinize(false)
Michiharu Ariza64c54122018-08-10 11:07:07 -0700428 {
429 topdict_sizes.init ();
430 topdict_sizes.resize (1);
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700431 topdict_mod.init ();
Michiharu Ariza0f159a32018-09-12 16:08:54 -0700432 subset_fdselect_ranges.init ();
Michiharu Ariza64c54122018-08-10 11:07:07 -0700433 fdmap.init ();
434 subset_charstrings.init ();
Michiharu Arizad56e3382018-10-31 22:30:34 -0700435 subset_globalsubrs.init ();
436 subset_localsubrs.init ();
Michiharu Ariza1666b892018-09-10 16:00:20 -0700437 fontdicts_mod.init ();
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700438 subset_enc_code_ranges.init ();
439 subset_enc_supp_codes.init ();
440 subset_charset_ranges.init ();
Michiharu Ariza1666b892018-09-10 16:00:20 -0700441 sidmap.init ();
Michiharu Arizac6f75c32018-09-10 17:02:31 -0700442 for (unsigned int i = 0; i < NameDictValues::ValCount; i++)
Michiharu Ariza1666b892018-09-10 16:00:20 -0700443 topDictModSIDs[i] = CFF_UNDEF_SID;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700444 }
445
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330446 ~cff_subset_plan ()
Michiharu Ariza64c54122018-08-10 11:07:07 -0700447 {
448 topdict_sizes.fini ();
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700449 topdict_mod.fini ();
Michiharu Ariza0f159a32018-09-12 16:08:54 -0700450 subset_fdselect_ranges.fini ();
Michiharu Ariza64c54122018-08-10 11:07:07 -0700451 fdmap.fini ();
Michiharu Arizaa9c305c2018-11-01 10:31:21 -0700452 subset_charstrings.fini_deep ();
453 subset_globalsubrs.fini_deep ();
Michiharu Arizad56e3382018-10-31 22:30:34 -0700454 subset_localsubrs.fini_deep ();
Michiharu Ariza1666b892018-09-10 16:00:20 -0700455 fontdicts_mod.fini ();
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700456 subset_enc_code_ranges.fini ();
457 subset_enc_supp_codes.init ();
458 subset_charset_ranges.fini ();
Michiharu Ariza1666b892018-09-10 16:00:20 -0700459 sidmap.fini ();
460 fontdicts_mod.fini ();
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700461 }
462
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330463 unsigned int plan_subset_encoding (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700464 {
465 const Encoding *encoding = acc.encoding;
466 unsigned int size0, size1, supp_size;
467 hb_codepoint_t code, last_code = CFF_UNDEF_CODE;
468 hb_vector_t<hb_codepoint_t> supp_codes;
469
470 subset_enc_code_ranges.resize (0);
471 supp_size = 0;
472 supp_codes.init ();
473
474 subset_enc_num_codes = plan->glyphs.len - 1;
475 unsigned int glyph;
476 for (glyph = 1; glyph < plan->glyphs.len; glyph++)
477 {
478 hb_codepoint_t orig_glyph = plan->glyphs[glyph];
479 code = acc.glyph_to_code (orig_glyph);
480 if (code == CFF_UNDEF_CODE)
481 {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500482 subset_enc_num_codes = glyph - 1;
483 break;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700484 }
485
486 if (code != last_code + 1)
487 {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500488 code_pair pair = { code, glyph };
489 subset_enc_code_ranges.push (pair);
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700490 }
491 last_code = code;
492
493 if (encoding != &Null(Encoding))
494 {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500495 hb_codepoint_t sid = acc.glyph_to_sid (orig_glyph);
496 encoding->get_supplement_codes (sid, supp_codes);
497 for (unsigned int i = 0; i < supp_codes.len; i++)
498 {
499 code_pair pair = { supp_codes[i], sid };
500 subset_enc_supp_codes.push (pair);
501 }
502 supp_size += SuppEncoding::static_size * supp_codes.len;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700503 }
504 }
505 supp_codes.fini ();
Michiharu Ariza16084812018-09-12 13:22:19 -0700506
507 subset_enc_code_ranges.finalize (glyph);
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700508
509 assert (subset_enc_num_codes <= 0xFF);
510 size0 = Encoding0::min_size + HBUINT8::static_size * subset_enc_num_codes;
511 size1 = Encoding1::min_size + Encoding1_Range::static_size * subset_enc_code_ranges.len;
512
513 if (size0 < size1)
514 subset_enc_format = 0;
515 else
516 subset_enc_format = 1;
517
518 return Encoding::calculate_serialized_size (
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500519 subset_enc_format,
520 subset_enc_format? subset_enc_code_ranges.len: subset_enc_num_codes,
521 subset_enc_supp_codes.len);
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700522 }
523
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330524 unsigned int plan_subset_charset (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700525 {
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700526 unsigned int size0, size_ranges;
527 hb_codepoint_t sid, last_sid = CFF_UNDEF_CODE;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700528
529 subset_charset_ranges.resize (0);
530 unsigned int glyph;
531 for (glyph = 1; glyph < plan->glyphs.len; glyph++)
532 {
533 hb_codepoint_t orig_glyph = plan->glyphs[glyph];
534 sid = acc.glyph_to_sid (orig_glyph);
535
Michiharu Ariza1666b892018-09-10 16:00:20 -0700536 if (!acc.is_CID ())
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500537 sid = sidmap.add (sid);
Michiharu Ariza1666b892018-09-10 16:00:20 -0700538
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700539 if (sid != last_sid + 1)
540 {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500541 code_pair pair = { sid, glyph };
542 subset_charset_ranges.push (pair);
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700543 }
544 last_sid = sid;
545 }
546
Michiharu Arizaa6da9b92018-11-16 17:29:03 -0800547 bool two_byte = subset_charset_ranges.finalize (glyph);
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700548
549 size0 = Charset0::min_size + HBUINT16::static_size * (plan->glyphs.len - 1);
550 if (!two_byte)
551 size_ranges = Charset1::min_size + Charset1_Range::static_size * subset_charset_ranges.len;
552 else
553 size_ranges = Charset2::min_size + Charset2_Range::static_size * subset_charset_ranges.len;
554
555 if (size0 < size_ranges)
556 subset_charset_format = 0;
557 else if (!two_byte)
558 subset_charset_format = 1;
559 else
560 subset_charset_format = 2;
561
562 return Charset::calculate_serialized_size (
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500563 subset_charset_format,
564 subset_charset_format? subset_charset_ranges.len: plan->glyphs.len);
Michiharu Ariza64c54122018-08-10 11:07:07 -0700565 }
566
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330567 bool collect_sids_in_dicts (const OT::cff1::accelerator_subset_t &acc)
Michiharu Ariza1666b892018-09-10 16:00:20 -0700568 {
569 if (unlikely (!sidmap.reset (acc.stringIndex->count)))
570 return false;
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500571
Michiharu Arizac6f75c32018-09-10 17:02:31 -0700572 for (unsigned int i = 0; i < NameDictValues::ValCount; i++)
Michiharu Ariza1666b892018-09-10 16:00:20 -0700573 {
574 unsigned int sid = acc.topDict.nameSIDs[i];
575 if (sid != CFF_UNDEF_SID)
576 {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500577 (void)sidmap.add (sid);
578 topDictModSIDs[i] = sidmap[sid];
Michiharu Ariza1666b892018-09-10 16:00:20 -0700579 }
580 }
581
582 if (acc.fdArray != &Null(CFF1FDArray))
Michiharu Arizad56e3382018-10-31 22:30:34 -0700583 for (unsigned int i = 0; i < orig_fdcount; i++)
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500584 if (fdmap.includes (i))
585 (void)sidmap.add (acc.fontDicts[i].fontName);
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500586
Michiharu Ariza1666b892018-09-10 16:00:20 -0700587 return true;
588 }
589
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330590 bool create (const OT::cff1::accelerator_subset_t &acc,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500591 hb_subset_plan_t *plan)
Michiharu Ariza64c54122018-08-10 11:07:07 -0700592 {
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700593 /* make sure notdef is first */
Michiharu Arizad8c69132018-11-30 18:58:14 -0800594 if ((plan->glyphs.len == 0) || (plan->glyphs[0] != 0)) return false;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700595
Michiharu Ariza64c54122018-08-10 11:07:07 -0700596 final_size = 0;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700597 num_glyphs = plan->glyphs.len;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700598 orig_fdcount = acc.fdCount;
Michiharu Ariza5cde2f52018-08-17 16:50:13 -0700599 drop_hints = plan->drop_hints;
Michiharu Ariza6186dbf2018-11-01 17:25:23 -0700600 desubroutinize = plan->desubroutinize;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700601
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700602 /* check whether the subset renumbers any glyph IDs */
603 gid_renum = false;
604 for (unsigned int glyph = 0; glyph < plan->glyphs.len; glyph++)
605 {
606 if (plan->glyphs[glyph] != glyph) {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500607 gid_renum = true;
608 break;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700609 }
610 }
611
612 subset_charset = gid_renum || !acc.is_predef_charset ();
Michiharu Arizaebeccf32018-09-18 17:24:30 -0700613 subset_encoding = !acc.is_CID() && !acc.is_predef_encoding ();
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700614
Michiharu Ariza64c54122018-08-10 11:07:07 -0700615 /* CFF header */
Michiharu Arizae67bb3f2018-08-16 00:25:57 -0700616 final_size += OT::cff1::static_size;
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500617
Michiharu Ariza64c54122018-08-10 11:07:07 -0700618 /* Name INDEX */
619 offsets.nameIndexOffset = final_size;
620 final_size += acc.nameIndex->get_size ();
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700621
Michiharu Ariza64c54122018-08-10 11:07:07 -0700622 /* top dict INDEX */
623 {
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700624 /* Add encoding/charset to a (copy of) top dict as necessary */
Michiharu Ariza1666b892018-09-10 16:00:20 -0700625 topdict_mod.init (&acc.topDict);
Michiharu Arizad56e3382018-10-31 22:30:34 -0700626 bool need_to_add_enc = (subset_encoding && !acc.topDict.has_op (OpCode_Encoding));
627 bool need_to_add_set = (subset_charset && !acc.topDict.has_op (OpCode_charset));
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700628 if (need_to_add_enc || need_to_add_set)
629 {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500630 if (need_to_add_enc)
631 topdict_mod.add_op (OpCode_Encoding);
632 if (need_to_add_set)
633 topdict_mod.add_op (OpCode_charset);
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700634 }
Michiharu Ariza633ce882018-08-15 12:00:19 -0700635 offsets.topDictInfo.offset = final_size;
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700636 CFF1TopDict_OpSerializer topSzr;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700637 unsigned int topDictSize = TopDict::calculate_serialized_size (topdict_mod, topSzr);
Michiharu Ariza633ce882018-08-15 12:00:19 -0700638 offsets.topDictInfo.offSize = calcOffSize(topDictSize);
Michiharu Ariza33358622018-12-11 12:20:20 -0800639 if (unlikely (offsets.topDictInfo.offSize > 4))
640 return false;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700641 final_size += CFF1IndexOf<TopDict>::calculate_serialized_size<CFF1TopDictValuesMod>
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500642 (offsets.topDictInfo.offSize,
643 &topdict_mod, 1, topdict_sizes, topSzr);
Michiharu Ariza64c54122018-08-10 11:07:07 -0700644 }
645
Michiharu Ariza1666b892018-09-10 16:00:20 -0700646 /* Determine re-mapping of font index as fdmap among other info */
Michiharu Arizab6903bd2018-11-16 13:46:58 -0800647 if (acc.fdSelect != &Null(CFF1FDSelect))
648 {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500649 if (unlikely (!hb_plan_subset_cff_fdselect (plan->glyphs,
650 orig_fdcount,
651 *acc.fdSelect,
652 subset_fdcount,
653 offsets.FDSelectInfo.size,
654 subset_fdselect_format,
655 subset_fdselect_ranges,
656 fdmap)))
657 return false;
Michiharu Arizab6903bd2018-11-16 13:46:58 -0800658 }
659 else
660 fdmap.identity (1);
Michiharu Ariza1666b892018-09-10 16:00:20 -0700661
662 /* remove unused SIDs & reassign SIDs */
663 {
664 /* SIDs for name strings in dicts are added before glyph names so they fit in 16-bit int range */
665 if (unlikely (!collect_sids_in_dicts (acc)))
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500666 return false;
Michiharu Ariza33358622018-12-11 12:20:20 -0800667 if (unlikely (sidmap.get_count () > 0x8000)) /* assumption: a dict won't reference that many strings */
668 return false;
Michiharu Ariza1666b892018-09-10 16:00:20 -0700669 if (subset_charset)
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500670 offsets.charsetInfo.size = plan_subset_charset (acc, plan);
Michiharu Ariza1666b892018-09-10 16:00:20 -0700671
672 topdict_mod.reassignSIDs (sidmap);
673 }
674
Michiharu Ariza64c54122018-08-10 11:07:07 -0700675 /* String INDEX */
Michiharu Ariza1666b892018-09-10 16:00:20 -0700676 {
677 offsets.stringIndexInfo.offset = final_size;
678 offsets.stringIndexInfo.size = acc.stringIndex->calculate_serialized_size (offsets.stringIndexInfo.offSize, sidmap);
679 final_size += offsets.stringIndexInfo.size;
680 }
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500681
Michiharu Arizad56e3382018-10-31 22:30:34 -0700682 if (desubroutinize)
Michiharu Ariza633ce882018-08-15 12:00:19 -0700683 {
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700684 /* Flatten global & local subrs */
Michiharu Ariza8c5e03b2018-08-30 17:21:56 -0700685 SubrFlattener<const OT::cff1::accelerator_subset_t, CFF1CSInterpEnv, CFF1CSOpSet_Flatten>
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500686 flattener(acc, plan->glyphs, plan->drop_hints);
Michiharu Arizad56e3382018-10-31 22:30:34 -0700687 if (!flattener.flatten (subset_charstrings))
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500688 return false;
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500689
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700690 /* no global/local subroutines */
Michiharu Ariza1bc710a2018-11-02 15:28:01 -0700691 offsets.globalSubrsInfo.size = CFF1Subrs::calculate_serialized_size (1, 0, 0);
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700692 }
Michiharu Arizad56e3382018-10-31 22:30:34 -0700693 else
694 {
695 /* Subset subrs: collect used subroutines, leaving all unused ones behind */
696 if (!subr_subsetter.subset (acc, plan->glyphs, plan->drop_hints))
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500697 return false;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700698
699 /* encode charstrings, global subrs, local subrs with new subroutine numbers */
700 if (!subr_subsetter.encode_charstrings (acc, plan->glyphs, subset_charstrings))
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500701 return false;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700702
703 if (!subr_subsetter.encode_globalsubrs (subset_globalsubrs))
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500704 return false;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700705
706 /* global subrs */
707 unsigned int dataSize = subset_globalsubrs.total_size ();
708 offsets.globalSubrsInfo.offSize = calcOffSize (dataSize);
Michiharu Ariza33358622018-12-11 12:20:20 -0800709 if (unlikely (offsets.globalSubrsInfo.offSize > 4))
710 return false;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700711 offsets.globalSubrsInfo.size = CFF1Subrs::calculate_serialized_size (offsets.globalSubrsInfo.offSize, subset_globalsubrs.len, dataSize);
712
713 /* local subrs */
714 if (!offsets.localSubrsInfos.resize (orig_fdcount))
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500715 return false;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700716 if (!subset_localsubrs.resize (orig_fdcount))
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500717 return false;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700718 for (unsigned int fd = 0; fd < orig_fdcount; fd++)
719 {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500720 subset_localsubrs[fd].init ();
721 offsets.localSubrsInfos[fd].init ();
722 if (fdmap.includes (fd))
723 {
724 if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd]))
725 return false;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700726
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500727 unsigned int dataSize = subset_localsubrs[fd].total_size ();
728 if (dataSize > 0)
729 {
730 offsets.localSubrsInfos[fd].offset = final_size;
731 offsets.localSubrsInfos[fd].offSize = calcOffSize (dataSize);
Michiharu Ariza33358622018-12-11 12:20:20 -0800732 if (unlikely (offsets.localSubrsInfos[fd].offSize > 4))
733 return false;
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500734 offsets.localSubrsInfos[fd].size = CFF1Subrs::calculate_serialized_size (offsets.localSubrsInfos[fd].offSize, subset_localsubrs[fd].len, dataSize);
735 }
736 }
Michiharu Arizad56e3382018-10-31 22:30:34 -0700737 }
738 }
Michiharu Arizacc52e532018-09-10 16:27:49 -0700739
Michiharu Ariza64c54122018-08-10 11:07:07 -0700740 /* global subrs */
Michiharu Ariza633ce882018-08-15 12:00:19 -0700741 offsets.globalSubrsInfo.offset = final_size;
742 final_size += offsets.globalSubrsInfo.size;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700743
744 /* Encoding */
Michiharu Arizaecdb77f2018-11-15 10:54:15 -0800745 if (!subset_encoding)
Michiharu Ariza9228db02018-10-13 17:25:09 -0700746 offsets.encodingOffset = acc.topDict.EncodingOffset;
747 else
Michiharu Arizaecdb77f2018-11-15 10:54:15 -0800748 {
Michiharu Ariza9228db02018-10-13 17:25:09 -0700749 offsets.encodingOffset = final_size;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700750 final_size += plan_subset_encoding (acc, plan);
Michiharu Arizaecdb77f2018-11-15 10:54:15 -0800751 }
Michiharu Ariza64c54122018-08-10 11:07:07 -0700752
753 /* Charset */
Michiharu Ariza9228db02018-10-13 17:25:09 -0700754 if (!subset_charset && acc.is_predef_charset ())
755 offsets.charsetInfo.offset = acc.topDict.CharsetOffset;
756 else
757 offsets.charsetInfo.offset = final_size;
Michiharu Ariza1666b892018-09-10 16:00:20 -0700758 final_size += offsets.charsetInfo.size;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700759
760 /* FDSelect */
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700761 if (acc.fdSelect != &Null(CFF1FDSelect))
Michiharu Ariza64c54122018-08-10 11:07:07 -0700762 {
763 offsets.FDSelectInfo.offset = final_size;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700764 final_size += offsets.FDSelectInfo.size;
765 }
766
767 /* FDArray (FDIndex) */
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700768 if (acc.fdArray != &Null(CFF1FDArray)) {
Michiharu Ariza633ce882018-08-15 12:00:19 -0700769 offsets.FDArrayInfo.offset = final_size;
Michiharu Ariza1666b892018-09-10 16:00:20 -0700770 CFF1FontDict_OpSerializer fontSzr;
771 unsigned int dictsSize = 0;
772 for (unsigned int i = 0; i < acc.fontDicts.len; i++)
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500773 if (fdmap.includes (i))
774 dictsSize += FontDict::calculate_serialized_size (acc.fontDicts[i], fontSzr);
Michiharu Ariza1666b892018-09-10 16:00:20 -0700775
Michiharu Ariza5b453f72018-09-11 16:20:39 -0700776 offsets.FDArrayInfo.offSize = calcOffSize (dictsSize);
Michiharu Ariza33358622018-12-11 12:20:20 -0800777 if (unlikely (offsets.FDArrayInfo.offSize > 4))
778 return false;
Michiharu Ariza1666b892018-09-10 16:00:20 -0700779 final_size += CFF1Index::calculate_serialized_size (offsets.FDArrayInfo.offSize, subset_fdcount, dictsSize);
Michiharu Ariza64c54122018-08-10 11:07:07 -0700780 }
781
782 /* CharStrings */
783 {
Michiharu Ariza633ce882018-08-15 12:00:19 -0700784 offsets.charStringsInfo.offset = final_size;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700785 unsigned int dataSize = subset_charstrings.total_size ();
Michiharu Ariza5b453f72018-09-11 16:20:39 -0700786 offsets.charStringsInfo.offSize = calcOffSize (dataSize);
Michiharu Ariza33358622018-12-11 12:20:20 -0800787 if (unlikely (offsets.charStringsInfo.offSize > 4))
788 return false;
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700789 final_size += CFF1CharStrings::calculate_serialized_size (offsets.charStringsInfo.offSize, plan->glyphs.len, dataSize);
Michiharu Ariza64c54122018-08-10 11:07:07 -0700790 }
791
792 /* private dicts & local subrs */
793 offsets.privateDictInfo.offset = final_size;
794 for (unsigned int i = 0; i < orig_fdcount; i++)
795 {
Michiharu Arizad56e3382018-10-31 22:30:34 -0700796 if (fdmap.includes (i))
Michiharu Arizaa97ed342018-08-10 12:55:22 -0700797 {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500798 bool has_localsubrs = offsets.localSubrsInfos[i].size > 0;
799 CFFPrivateDict_OpSerializer privSzr (desubroutinize, plan->drop_hints);
800 unsigned int priv_size = PrivateDict::calculate_serialized_size (acc.privateDicts[i], privSzr, has_localsubrs);
801 TableInfo privInfo = { final_size, priv_size, 0 };
802 FontDictValuesMod fontdict_mod;
803 if (!acc.is_CID ())
804 fontdict_mod.init ( &Null(CFF1FontDictValues), CFF_UNDEF_SID, privInfo );
805 else
806 fontdict_mod.init ( &acc.fontDicts[i], sidmap[acc.fontDicts[i].fontName], privInfo );
807 fontdicts_mod.push (fontdict_mod);
808 final_size += privInfo.size;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700809
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500810 if (!plan->desubroutinize && has_localsubrs)
811 {
812 offsets.localSubrsInfos[i].offset = final_size;
813 final_size += offsets.localSubrsInfos[i].size;
814 }
Michiharu Arizaa97ed342018-08-10 12:55:22 -0700815 }
Michiharu Ariza64c54122018-08-10 11:07:07 -0700816 }
817
818 if (!acc.is_CID ())
Michiharu Arizad8c69132018-11-30 18:58:14 -0800819 offsets.privateDictInfo = fontdicts_mod[0].privateDictInfo;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700820
Michiharu Arizad56e3382018-10-31 22:30:34 -0700821 return ((subset_charstrings.len == plan->glyphs.len)
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500822 && (fontdicts_mod.len == subset_fdcount));
Michiharu Ariza64c54122018-08-10 11:07:07 -0700823 }
824
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330825 unsigned int get_final_size () const { return final_size; }
Michiharu Ariza64c54122018-08-10 11:07:07 -0700826
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500827 unsigned int final_size;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700828 hb_vector_t<unsigned int> topdict_sizes;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700829 CFF1TopDictValuesMod topdict_mod;
830 CFF1SubTableOffsets offsets;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700831
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700832 unsigned int num_glyphs;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700833 unsigned int orig_fdcount;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700834 unsigned int subset_fdcount;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700835 unsigned int subset_fdselect_format;
Michiharu Ariza0f159a32018-09-12 16:08:54 -0700836 hb_vector_t<code_pair> subset_fdselect_ranges;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700837
838 /* font dict index remap table from fullset FDArray to subset FDArray.
Michiharu Ariza1666b892018-09-10 16:00:20 -0700839 * set to CFF_UNDEF_CODE if excluded from subset */
840 Remap fdmap;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700841
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500842 StrBuffArray subset_charstrings;
843 StrBuffArray subset_globalsubrs;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700844 hb_vector_t<StrBuffArray> subset_localsubrs;
Michiharu Ariza1666b892018-09-10 16:00:20 -0700845 hb_vector_t<FontDictValuesMod> fontdicts_mod;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700846
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500847 bool drop_hints;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700848
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500849 bool gid_renum;
850 bool subset_encoding;
851 uint8_t subset_enc_format;
852 unsigned int subset_enc_num_codes;
853 RangeList subset_enc_code_ranges;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700854 hb_vector_t<code_pair> subset_enc_supp_codes;
855
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500856 uint8_t subset_charset_format;
857 RangeList subset_charset_ranges;
858 bool subset_charset;
Michiharu Ariza1666b892018-09-10 16:00:20 -0700859
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500860 RemapSID sidmap;
861 unsigned int topDictModSIDs[NameDictValues::ValCount];
Michiharu Arizad56e3382018-10-31 22:30:34 -0700862
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500863 bool desubroutinize;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700864 CFF1SubrSubsetter subr_subsetter;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700865};
866
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700867static inline bool _write_cff1 (const cff_subset_plan &plan,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500868 const OT::cff1::accelerator_subset_t &acc,
869 const hb_vector_t<hb_codepoint_t>& glyphs,
870 unsigned int dest_sz,
871 void *dest)
Michiharu Ariza64c54122018-08-10 11:07:07 -0700872{
873 hb_serialize_context_t c (dest, dest_sz);
874
Michiharu Arizac2348392018-08-15 13:04:43 -0700875 char RETURN_OP[1] = { OpCode_return };
Michiharu Ariza270452a2018-08-15 13:15:08 -0700876 const ByteStr NULL_SUBR (RETURN_OP, 1);
Michiharu Arizac2348392018-08-15 13:04:43 -0700877
Michiharu Arizae67bb3f2018-08-16 00:25:57 -0700878 OT::cff1 *cff = c.start_serialize<OT::cff1> ();
Michiharu Ariza64c54122018-08-10 11:07:07 -0700879 if (unlikely (!c.extend_min (*cff)))
880 return false;
881
882 /* header */
883 cff->version.major.set (0x01);
884 cff->version.minor.set (0x00);
885 cff->nameIndex.set (cff->min_size);
886 cff->offSize.set (4); /* unused? */
887
888 /* name INDEX */
889 {
890 assert (cff->nameIndex == c.head - c.start);
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700891 CFF1NameIndex *dest = c.start_embed<CFF1NameIndex> ();
Michiharu Ariza64c54122018-08-10 11:07:07 -0700892 if (unlikely (dest == nullptr)) return false;
893 if (unlikely (!dest->serialize (&c, *acc.nameIndex)))
894 {
895 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF name INDEX");
896 return false;
897 }
898 }
899
900 /* top dict INDEX */
901 {
Michiharu Ariza633ce882018-08-15 12:00:19 -0700902 assert (plan.offsets.topDictInfo.offset == c.head - c.start);
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700903 CFF1IndexOf<TopDict> *dest = c.start_embed< CFF1IndexOf<TopDict> > ();
Michiharu Ariza64c54122018-08-10 11:07:07 -0700904 if (dest == nullptr) return false;
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700905 CFF1TopDict_OpSerializer topSzr;
Michiharu Ariza1666b892018-09-10 16:00:20 -0700906 TopDictModifiers modifier (plan.offsets, plan.topDictModSIDs);
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700907 if (unlikely (!dest->serialize (&c, plan.offsets.topDictInfo.offSize,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500908 &plan.topdict_mod, 1,
909 plan.topdict_sizes, topSzr, modifier)))
Michiharu Ariza64c54122018-08-10 11:07:07 -0700910 {
911 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF top dict");
912 return false;
913 }
914 }
915
916 /* String INDEX */
917 {
Michiharu Ariza1666b892018-09-10 16:00:20 -0700918 assert (plan.offsets.stringIndexInfo.offset == c.head - c.start);
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700919 CFF1StringIndex *dest = c.start_embed<CFF1StringIndex> ();
Michiharu Ariza64c54122018-08-10 11:07:07 -0700920 if (unlikely (dest == nullptr)) return false;
Michiharu Ariza1666b892018-09-10 16:00:20 -0700921 if (unlikely (!dest->serialize (&c, *acc.stringIndex, plan.offsets.stringIndexInfo.offSize, plan.sidmap)))
Michiharu Ariza64c54122018-08-10 11:07:07 -0700922 {
923 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF string INDEX");
924 return false;
925 }
926 }
927
928 /* global subrs */
929 {
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700930 assert (plan.offsets.globalSubrsInfo.offset != 0);
Michiharu Ariza633ce882018-08-15 12:00:19 -0700931 assert (plan.offsets.globalSubrsInfo.offset == c.head - c.start);
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500932
Michiharu Ariza1bc710a2018-11-02 15:28:01 -0700933 CFF1Subrs *dest = c.start_embed <CFF1Subrs> ();
934 if (unlikely (dest == nullptr)) return false;
935 if (unlikely (!dest->serialize (&c, plan.offsets.globalSubrsInfo.offSize, plan.subset_globalsubrs)))
Michiharu Arizad56e3382018-10-31 22:30:34 -0700936 {
Michiharu Ariza1bc710a2018-11-02 15:28:01 -0700937 DEBUG_MSG (SUBSET, nullptr, "failed to serialize global subroutines");
938 return false;
Michiharu Arizad56e3382018-10-31 22:30:34 -0700939 }
Michiharu Ariza64c54122018-08-10 11:07:07 -0700940 }
941
942 /* Encoding */
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700943 if (plan.subset_encoding)
944 {
Michiharu Ariza64c54122018-08-10 11:07:07 -0700945 assert (plan.offsets.encodingOffset == c.head - c.start);
946 Encoding *dest = c.start_embed<Encoding> ();
947 if (unlikely (dest == nullptr)) return false;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700948 if (unlikely (!dest->serialize (&c,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500949 plan.subset_enc_format,
950 plan.subset_enc_num_codes,
951 plan.subset_enc_code_ranges,
952 plan.subset_enc_supp_codes)))
Michiharu Ariza64c54122018-08-10 11:07:07 -0700953 {
954 DEBUG_MSG (SUBSET, nullptr, "failed to serialize Encoding");
955 return false;
956 }
957 }
958
959 /* Charset */
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700960 if (plan.subset_charset)
Michiharu Ariza64c54122018-08-10 11:07:07 -0700961 {
Michiharu Ariza1666b892018-09-10 16:00:20 -0700962 assert (plan.offsets.charsetInfo.offset == c.head - c.start);
Michiharu Ariza64c54122018-08-10 11:07:07 -0700963 Charset *dest = c.start_embed<Charset> ();
964 if (unlikely (dest == nullptr)) return false;
Michiharu Arizaaca73c92018-09-06 17:28:15 -0700965 if (unlikely (!dest->serialize (&c,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500966 plan.subset_charset_format,
967 plan.num_glyphs,
968 plan.subset_charset_ranges)))
Michiharu Ariza64c54122018-08-10 11:07:07 -0700969 {
970 DEBUG_MSG (SUBSET, nullptr, "failed to serialize Charset");
971 return false;
972 }
973 }
974
975 /* FDSelect */
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700976 if (acc.fdSelect != &Null(CFF1FDSelect))
Michiharu Ariza64c54122018-08-10 11:07:07 -0700977 {
978 assert (plan.offsets.FDSelectInfo.offset == c.head - c.start);
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500979
Michiharu Ariza9d0231c2018-11-15 15:39:43 -0800980 if (unlikely (!hb_serialize_cff_fdselect (&c, glyphs.len, *acc.fdSelect, acc.fdCount,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500981 plan.subset_fdselect_format, plan.offsets.FDSelectInfo.size,
982 plan.subset_fdselect_ranges)))
Michiharu Ariza64c54122018-08-10 11:07:07 -0700983 {
Michiharu Ariza9d0231c2018-11-15 15:39:43 -0800984 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF subset FDSelect");
985 return false;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700986 }
987 }
988
989 /* FDArray (FD Index) */
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700990 if (acc.fdArray != &Null(CFF1FDArray))
Michiharu Ariza64c54122018-08-10 11:07:07 -0700991 {
Michiharu Ariza633ce882018-08-15 12:00:19 -0700992 assert (plan.offsets.FDArrayInfo.offset == c.head - c.start);
Michiharu Arizafdbfa182018-08-16 00:13:09 -0700993 CFF1FDArray *fda = c.start_embed<CFF1FDArray> ();
Michiharu Ariza64c54122018-08-10 11:07:07 -0700994 if (unlikely (fda == nullptr)) return false;
Michiharu Ariza1666b892018-09-10 16:00:20 -0700995 CFF1FontDict_OpSerializer fontSzr;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700996 if (unlikely (!fda->serialize (&c, plan.offsets.FDArrayInfo.offSize,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500997 plan.fontdicts_mod,
998 fontSzr)))
Michiharu Ariza64c54122018-08-10 11:07:07 -0700999 {
1000 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF FDArray");
1001 return false;
1002 }
1003 }
1004
1005 /* CharStrings */
1006 {
Michiharu Ariza633ce882018-08-15 12:00:19 -07001007 assert (plan.offsets.charStringsInfo.offset == c.head - c.start);
Michiharu Arizafdbfa182018-08-16 00:13:09 -07001008 CFF1CharStrings *cs = c.start_embed<CFF1CharStrings> ();
Michiharu Ariza64c54122018-08-10 11:07:07 -07001009 if (unlikely (cs == nullptr)) return false;
Michiharu Ariza633ce882018-08-15 12:00:19 -07001010 if (unlikely (!cs->serialize (&c, plan.offsets.charStringsInfo.offSize, plan.subset_charstrings)))
Michiharu Ariza64c54122018-08-10 11:07:07 -07001011 {
1012 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF CharStrings");
1013 return false;
1014 }
1015 }
1016
1017 /* private dicts & local subrs */
1018 assert (plan.offsets.privateDictInfo.offset == c.head - c.start);
1019 for (unsigned int i = 0; i < acc.privateDicts.len; i++)
1020 {
Michiharu Ariza1bc710a2018-11-02 15:28:01 -07001021 if (plan.fdmap.includes (i))
Michiharu Ariza64c54122018-08-10 11:07:07 -07001022 {
Michiharu Arizaa97ed342018-08-10 12:55:22 -07001023 PrivateDict *pd = c.start_embed<PrivateDict> ();
1024 if (unlikely (pd == nullptr)) return false;
Michiharu Arizacc52e532018-09-10 16:27:49 -07001025 unsigned int priv_size = plan.fontdicts_mod[plan.fdmap[i]].privateDictInfo.size;
Michiharu Ariza5cde2f52018-08-17 16:50:13 -07001026 bool result;
Michiharu Arizad56e3382018-10-31 22:30:34 -07001027 CFFPrivateDict_OpSerializer privSzr (plan.desubroutinize, plan.drop_hints);
Michiharu Arizaa97ed342018-08-10 12:55:22 -07001028 /* N.B. local subrs immediately follows its corresponding private dict. i.e., subr offset == private dict size */
Michiharu Ariza191ca0f2018-11-03 22:42:22 -07001029 unsigned int subroffset = (plan.offsets.localSubrsInfos[i].size > 0)? priv_size: 0;
1030 result = pd->serialize (&c, acc.privateDicts[i], privSzr, subroffset);
Michiharu Ariza5cde2f52018-08-17 16:50:13 -07001031 if (unlikely (!result))
Michiharu Ariza64c54122018-08-10 11:07:07 -07001032 {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -05001033 DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF Private Dict[%d]", i);
1034 return false;
Michiharu Ariza64c54122018-08-10 11:07:07 -07001035 }
Michiharu Ariza1bc710a2018-11-02 15:28:01 -07001036 if (plan.offsets.localSubrsInfos[i].size > 0)
Michiharu Arizad56e3382018-10-31 22:30:34 -07001037 {
Behdad Esfahbod825df6d2018-11-30 23:04:59 -05001038 CFF1Subrs *dest = c.start_embed <CFF1Subrs> ();
1039 if (unlikely (dest == nullptr)) return false;
1040 if (unlikely (!dest->serialize (&c, plan.offsets.localSubrsInfos[i].offSize, plan.subset_localsubrs[i])))
1041 {
1042 DEBUG_MSG (SUBSET, nullptr, "failed to serialize local subroutines");
1043 return false;
1044 }
Michiharu Arizad56e3382018-10-31 22:30:34 -07001045 }
1046 }
1047 }
1048
Michiharu Arizacef75ea2018-08-17 13:13:18 -07001049 assert (c.head == c.end);
Michiharu Ariza64c54122018-08-10 11:07:07 -07001050 c.end_serialize ();
1051
1052 return true;
1053}
1054
1055static bool
Michiharu Arizae67bb3f2018-08-16 00:25:57 -07001056_hb_subset_cff1 (const OT::cff1::accelerator_subset_t &acc,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -05001057 const char *data,
1058 hb_subset_plan_t *plan,
1059 hb_blob_t **prime /* OUT */)
Michiharu Ariza64c54122018-08-10 11:07:07 -07001060{
1061 cff_subset_plan cff_plan;
1062
1063 if (unlikely (!cff_plan.create (acc, plan)))
1064 {
1065 DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff subsetting plan.");
1066 return false;
1067 }
1068
1069 unsigned int cff_prime_size = cff_plan.get_final_size ();
1070 char *cff_prime_data = (char *) calloc (1, cff_prime_size);
1071
Michiharu Arizafdbfa182018-08-16 00:13:09 -07001072 if (unlikely (!_write_cff1 (cff_plan, acc, plan->glyphs,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -05001073 cff_prime_size, cff_prime_data))) {
Michiharu Ariza64c54122018-08-10 11:07:07 -07001074 DEBUG_MSG(SUBSET, nullptr, "Failed to write a subset cff.");
1075 free (cff_prime_data);
1076 return false;
1077 }
1078
1079 *prime = hb_blob_create (cff_prime_data,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -05001080 cff_prime_size,
1081 HB_MEMORY_MODE_READONLY,
1082 cff_prime_data,
1083 free);
Michiharu Ariza64c54122018-08-10 11:07:07 -07001084 return true;
1085}
1086
1087/**
Michiharu Arizafdbfa182018-08-16 00:13:09 -07001088 * hb_subset_cff1:
Michiharu Ariza64c54122018-08-10 11:07:07 -07001089 * Subsets the CFF table according to a provided plan.
1090 *
1091 * Return value: subsetted cff table.
1092 **/
1093bool
Michiharu Arizafdbfa182018-08-16 00:13:09 -07001094hb_subset_cff1 (hb_subset_plan_t *plan,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -05001095 hb_blob_t **prime /* OUT */)
Michiharu Ariza64c54122018-08-10 11:07:07 -07001096{
Michiharu Arizae67bb3f2018-08-16 00:25:57 -07001097 hb_blob_t *cff_blob = hb_sanitize_context_t().reference_table<CFF::cff1> (plan->source);
Michiharu Ariza64c54122018-08-10 11:07:07 -07001098 const char *data = hb_blob_get_data(cff_blob, nullptr);
1099
Michiharu Arizae67bb3f2018-08-16 00:25:57 -07001100 OT::cff1::accelerator_subset_t acc;
Michiharu Ariza64c54122018-08-10 11:07:07 -07001101 acc.init(plan->source);
1102 bool result = likely (acc.is_valid ()) &&
Behdad Esfahbod825df6d2018-11-30 23:04:59 -05001103 _hb_subset_cff1 (acc, data, plan, prime);
Michiharu Ariza64c54122018-08-10 11:07:07 -07001104 hb_blob_destroy (cff_blob);
1105 acc.fini ();
1106
1107 return result;
1108}