1 /*
2 * Copyright © 2018 Adobe Inc.
3 *
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
27 #include "hb.hh"
28
29 #ifndef HB_NO_SUBSET_CFF
30
31 #include "hb-open-type.hh"
32 #include "hb-ot-cff1-table.hh"
33 #include "hb-set.h"
34 #include "hb-bimap.hh"
35 #include "hb-subset-cff1.hh"
36 #include "hb-subset-plan.hh"
37 #include "hb-subset-cff-common.hh"
38 #include "hb-cff1-interp-cs.hh"
39
40 using namespace CFF;
41
42 struct remap_sid_t : hb_inc_bimap_t
43 {
addremap_sid_t44 unsigned int add (unsigned int sid)
45 {
46 if ((sid != CFF_UNDEF_SID) && !is_std_std (sid))
47 return offset_sid (hb_inc_bimap_t::add (unoffset_sid (sid)));
48 else
49 return sid;
50 }
51
operator []remap_sid_t52 unsigned int operator[] (unsigned int sid) const
53 {
54 if (is_std_std (sid) || (sid == CFF_UNDEF_SID))
55 return sid;
56 else
57 return offset_sid (get (unoffset_sid (sid)));
58 }
59
60 static const unsigned int num_std_strings = 391;
61
is_std_stdremap_sid_t62 static bool is_std_std (unsigned int sid) { return sid < num_std_strings; }
offset_sidremap_sid_t63 static unsigned int offset_sid (unsigned int sid) { return sid + num_std_strings; }
unoffset_sidremap_sid_t64 static unsigned int unoffset_sid (unsigned int sid) { return sid - num_std_strings; }
65 };
66
67 struct cff1_sub_table_info_t : cff_sub_table_info_t
68 {
cff1_sub_table_info_tcff1_sub_table_info_t69 cff1_sub_table_info_t ()
70 : cff_sub_table_info_t (),
71 encoding_link (0),
72 charset_link (0)
73 {
74 privateDictInfo.init ();
75 }
76
77 objidx_t encoding_link;
78 objidx_t charset_link;
79 table_info_t privateDictInfo;
80 };
81
82 /* a copy of a parsed out cff1_top_dict_values_t augmented with additional operators */
83 struct cff1_top_dict_values_mod_t : cff1_top_dict_values_t
84 {
initcff1_top_dict_values_mod_t85 void init (const cff1_top_dict_values_t *base_= &Null (cff1_top_dict_values_t))
86 {
87 SUPER::init ();
88 base = base_;
89 }
90
finicff1_top_dict_values_mod_t91 void fini () { SUPER::fini (); }
92
get_countcff1_top_dict_values_mod_t93 unsigned get_count () const { return base->get_count () + SUPER::get_count (); }
get_valuecff1_top_dict_values_mod_t94 const cff1_top_dict_val_t &get_value (unsigned int i) const
95 {
96 if (i < base->get_count ())
97 return (*base)[i];
98 else
99 return SUPER::values[i - base->get_count ()];
100 }
operator []cff1_top_dict_values_mod_t101 const cff1_top_dict_val_t &operator [] (unsigned int i) const { return get_value (i); }
102
reassignSIDscff1_top_dict_values_mod_t103 void reassignSIDs (const remap_sid_t& sidmap)
104 {
105 for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
106 nameSIDs[i] = sidmap[base->nameSIDs[i]];
107 }
108
109 protected:
110 typedef cff1_top_dict_values_t SUPER;
111 const cff1_top_dict_values_t *base;
112 };
113
114 struct top_dict_modifiers_t
115 {
top_dict_modifiers_ttop_dict_modifiers_t116 top_dict_modifiers_t (const cff1_sub_table_info_t &info_,
117 const unsigned int (&nameSIDs_)[name_dict_values_t::ValCount])
118 : info (info_),
119 nameSIDs (nameSIDs_)
120 {}
121
122 const cff1_sub_table_info_t &info;
123 const unsigned int (&nameSIDs)[name_dict_values_t::ValCount];
124 };
125
126 struct cff1_top_dict_op_serializer_t : cff_top_dict_op_serializer_t<cff1_top_dict_val_t>
127 {
serializecff1_top_dict_op_serializer_t128 bool serialize (hb_serialize_context_t *c,
129 const cff1_top_dict_val_t &opstr,
130 const top_dict_modifiers_t &mod) const
131 {
132 TRACE_SERIALIZE (this);
133
134 op_code_t op = opstr.op;
135 switch (op)
136 {
137 case OpCode_charset:
138 if (mod.info.charset_link)
139 return_trace (FontDict::serialize_link4_op(c, op, mod.info.charset_link, whence_t::Absolute));
140 else
141 goto fall_back;
142
143 case OpCode_Encoding:
144 if (mod.info.encoding_link)
145 return_trace (FontDict::serialize_link4_op(c, op, mod.info.encoding_link, whence_t::Absolute));
146 else
147 goto fall_back;
148
149 case OpCode_Private:
150 return_trace (UnsizedByteStr::serialize_int2 (c, mod.info.privateDictInfo.size) &&
151 Dict::serialize_link4_op (c, op, mod.info.privateDictInfo.link, whence_t::Absolute));
152
153 case OpCode_version:
154 case OpCode_Notice:
155 case OpCode_Copyright:
156 case OpCode_FullName:
157 case OpCode_FamilyName:
158 case OpCode_Weight:
159 case OpCode_PostScript:
160 case OpCode_BaseFontName:
161 case OpCode_FontName:
162 return_trace (FontDict::serialize_int2_op (c, op, mod.nameSIDs[name_dict_values_t::name_op_to_index (op)]));
163
164 case OpCode_ROS:
165 {
166 /* for registry & ordering, reassigned SIDs are serialized
167 * for supplement, the original byte string is copied along with the op code */
168 op_str_t supp_op;
169 supp_op.op = op;
170 if ( unlikely (!(opstr.length >= opstr.last_arg_offset + 3)))
171 return_trace (false);
172 supp_op.ptr = opstr.ptr + opstr.last_arg_offset;
173 supp_op.length = opstr.length - opstr.last_arg_offset;
174 return_trace (UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::registry]) &&
175 UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[name_dict_values_t::ordering]) &&
176 copy_opstr (c, supp_op));
177 }
178 fall_back:
179 default:
180 return_trace (cff_top_dict_op_serializer_t<cff1_top_dict_val_t>::serialize (c, opstr, mod.info));
181 }
182 return_trace (true);
183 }
184
185 };
186
187 struct cff1_font_dict_op_serializer_t : cff_font_dict_op_serializer_t
188 {
serializecff1_font_dict_op_serializer_t189 bool serialize (hb_serialize_context_t *c,
190 const op_str_t &opstr,
191 const cff1_font_dict_values_mod_t &mod) const
192 {
193 TRACE_SERIALIZE (this);
194
195 if (opstr.op == OpCode_FontName)
196 return_trace (FontDict::serialize_int2_op (c, opstr.op, mod.fontName));
197 else
198 return_trace (SUPER::serialize (c, opstr, mod.privateDictInfo));
199 }
200
201 private:
202 typedef cff_font_dict_op_serializer_t SUPER;
203 };
204
205 struct cff1_cs_opset_flatten_t : cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t>
206 {
flush_args_and_opcff1_cs_opset_flatten_t207 static void flush_args_and_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
208 {
209 if (env.arg_start > 0)
210 flush_width (env, param);
211
212 switch (op)
213 {
214 case OpCode_hstem:
215 case OpCode_hstemhm:
216 case OpCode_vstem:
217 case OpCode_vstemhm:
218 case OpCode_hintmask:
219 case OpCode_cntrmask:
220 case OpCode_dotsection:
221 if (param.drop_hints)
222 {
223 env.clear_args ();
224 return;
225 }
226 HB_FALLTHROUGH;
227
228 default:
229 SUPER::flush_args_and_op (op, env, param);
230 break;
231 }
232 }
flush_argscff1_cs_opset_flatten_t233 static void flush_args (cff1_cs_interp_env_t &env, flatten_param_t& param)
234 {
235 str_encoder_t encoder (param.flatStr);
236 for (unsigned int i = env.arg_start; i < env.argStack.get_count (); i++)
237 encoder.encode_num (env.eval_arg (i));
238 SUPER::flush_args (env, param);
239 }
240
flush_opcff1_cs_opset_flatten_t241 static void flush_op (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
242 {
243 str_encoder_t encoder (param.flatStr);
244 encoder.encode_op (op);
245 }
246
flush_widthcff1_cs_opset_flatten_t247 static void flush_width (cff1_cs_interp_env_t &env, flatten_param_t& param)
248 {
249 assert (env.has_width);
250 str_encoder_t encoder (param.flatStr);
251 encoder.encode_num (env.width);
252 }
253
flush_hintmaskcff1_cs_opset_flatten_t254 static void flush_hintmask (op_code_t op, cff1_cs_interp_env_t &env, flatten_param_t& param)
255 {
256 SUPER::flush_hintmask (op, env, param);
257 if (!param.drop_hints)
258 {
259 str_encoder_t encoder (param.flatStr);
260 for (unsigned int i = 0; i < env.hintmask_size; i++)
261 encoder.encode_byte (env.str_ref[i]);
262 }
263 }
264
265 private:
266 typedef cff1_cs_opset_t<cff1_cs_opset_flatten_t, flatten_param_t> SUPER;
267 };
268
269 struct range_list_t : hb_vector_t<code_pair_t>
270 {
271 /* replace the first glyph ID in the "glyph" field each range with a nLeft value */
completerange_list_t272 bool complete (unsigned int last_glyph)
273 {
274 bool two_byte = false;
275 unsigned count = this->length;
276 for (unsigned int i = count; i; i--)
277 {
278 code_pair_t &pair = arrayZ[i - 1];
279 unsigned int nLeft = last_glyph - pair.glyph - 1;
280 two_byte |= nLeft >= 0x100;
281 last_glyph = pair.glyph;
282 pair.glyph = nLeft;
283 }
284 return two_byte;
285 }
286 };
287
288 struct cff1_cs_opset_subr_subset_t : cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t>
289 {
process_opcff1_cs_opset_subr_subset_t290 static void process_op (op_code_t op, cff1_cs_interp_env_t &env, subr_subset_param_t& param)
291 {
292 switch (op) {
293
294 case OpCode_return:
295 param.current_parsed_str->add_op (op, env.str_ref);
296 param.current_parsed_str->set_parsed ();
297 env.return_from_subr ();
298 param.set_current_str (env, false);
299 break;
300
301 case OpCode_endchar:
302 param.current_parsed_str->add_op (op, env.str_ref);
303 param.current_parsed_str->set_parsed ();
304 SUPER::process_op (op, env, param);
305 break;
306
307 case OpCode_callsubr:
308 process_call_subr (op, CSType_LocalSubr, env, param, env.localSubrs, param.local_closure);
309 break;
310
311 case OpCode_callgsubr:
312 process_call_subr (op, CSType_GlobalSubr, env, param, env.globalSubrs, param.global_closure);
313 break;
314
315 default:
316 SUPER::process_op (op, env, param);
317 param.current_parsed_str->add_op (op, env.str_ref);
318 break;
319 }
320 }
321
322 protected:
process_call_subrcff1_cs_opset_subr_subset_t323 static void process_call_subr (op_code_t op, cs_type_t type,
324 cff1_cs_interp_env_t &env, subr_subset_param_t& param,
325 cff1_biased_subrs_t& subrs, hb_set_t *closure)
326 {
327 byte_str_ref_t str_ref = env.str_ref;
328 env.call_subr (subrs, type);
329 param.current_parsed_str->add_call_op (op, str_ref, env.context.subr_num);
330 closure->add (env.context.subr_num);
331 param.set_current_str (env, true);
332 }
333
334 private:
335 typedef cff1_cs_opset_t<cff1_cs_opset_subr_subset_t, subr_subset_param_t> SUPER;
336 };
337
338 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>
339 {
cff1_subr_subsetter_tcff1_subr_subsetter_t340 cff1_subr_subsetter_t (const OT::cff1::accelerator_subset_t &acc_, const hb_subset_plan_t *plan_)
341 : subr_subsetter_t (acc_, plan_) {}
342
complete_parsed_strcff1_subr_subsetter_t343 static void complete_parsed_str (cff1_cs_interp_env_t &env, subr_subset_param_t& param, parsed_cs_str_t &charstring)
344 {
345 /* insert width at the beginning of the charstring as necessary */
346 if (env.has_width)
347 charstring.set_prefix (env.width);
348
349 /* subroutines/charstring left on the call stack are legally left unmarked
350 * unmarked when a subroutine terminates with endchar. mark them.
351 */
352 param.current_parsed_str->set_parsed ();
353 for (unsigned int i = 0; i < env.callStack.get_count (); i++)
354 {
355 parsed_cs_str_t *parsed_str = param.get_parsed_str_for_context (env.callStack[i]);
356 if (likely (parsed_str))
357 parsed_str->set_parsed ();
358 else
359 env.set_error ();
360 }
361 }
362 };
363
364 struct cff_subset_plan {
cff_subset_plancff_subset_plan365 cff_subset_plan ()
366 {
367 for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
368 topDictModSIDs[i] = CFF_UNDEF_SID;
369 }
370
plan_subset_encodingcff_subset_plan371 void plan_subset_encoding (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
372 {
373 const Encoding *encoding = acc.encoding;
374 unsigned int size0, size1;
375 hb_codepoint_t code, last_code = CFF_UNDEF_CODE;
376 hb_vector_t<hb_codepoint_t> supp_codes;
377
378 if (unlikely (!subset_enc_code_ranges.resize (0)))
379 {
380 plan->check_success (false);
381 return;
382 }
383
384 supp_codes.init ();
385
386 subset_enc_num_codes = plan->num_output_glyphs () - 1;
387 unsigned int glyph;
388 for (glyph = 1; glyph < plan->num_output_glyphs (); glyph++)
389 {
390 hb_codepoint_t old_glyph;
391 if (!plan->old_gid_for_new_gid (glyph, &old_glyph))
392 {
393 /* Retain the code for the old missing glyph ID */
394 old_glyph = glyph;
395 }
396 code = acc.glyph_to_code (old_glyph);
397 if (code == CFF_UNDEF_CODE)
398 {
399 subset_enc_num_codes = glyph - 1;
400 break;
401 }
402
403 if ((last_code == CFF_UNDEF_CODE) || (code != last_code + 1))
404 {
405 code_pair_t pair = { code, glyph };
406 subset_enc_code_ranges.push (pair);
407 }
408 last_code = code;
409
410 if (encoding != &Null (Encoding))
411 {
412 hb_codepoint_t sid = acc.glyph_to_sid (old_glyph);
413 encoding->get_supplement_codes (sid, supp_codes);
414 for (unsigned int i = 0; i < supp_codes.length; i++)
415 {
416 code_pair_t pair = { supp_codes[i], sid };
417 subset_enc_supp_codes.push (pair);
418 }
419 }
420 }
421 supp_codes.fini ();
422
423 subset_enc_code_ranges.complete (glyph);
424
425 assert (subset_enc_num_codes <= 0xFF);
426 size0 = Encoding0::min_size + HBUINT8::static_size * subset_enc_num_codes;
427 size1 = Encoding1::min_size + Encoding1_Range::static_size * subset_enc_code_ranges.length;
428
429 if (size0 < size1)
430 subset_enc_format = 0;
431 else
432 subset_enc_format = 1;
433 }
434
plan_subset_charsetcff_subset_plan435 void plan_subset_charset (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan)
436 {
437 unsigned int size0, size_ranges;
438 hb_codepoint_t sid, last_sid = CFF_UNDEF_CODE;
439
440 if (unlikely (!subset_charset_ranges.resize (0)))
441 {
442 plan->check_success (false);
443 return;
444 }
445
446 hb_map_t *glyph_to_sid_map = (plan->accelerator && plan->accelerator->cff_accelerator) ?
447 plan->accelerator->cff_accelerator->glyph_to_sid_map :
448 nullptr;
449 bool created_map = false;
450 if (!glyph_to_sid_map &&
451 ((plan->accelerator && plan->accelerator->cff_accelerator) ||
452 plan->num_output_glyphs () > plan->source->get_num_glyphs () / 8.))
453 {
454 created_map = true;
455 glyph_to_sid_map = acc.create_glyph_to_sid_map ();
456 }
457
458 unsigned int glyph;
459 for (glyph = 1; glyph < plan->num_output_glyphs (); glyph++)
460 {
461 hb_codepoint_t old_glyph;
462 if (!plan->old_gid_for_new_gid (glyph, &old_glyph))
463 {
464 /* Retain the SID for the old missing glyph ID */
465 old_glyph = glyph;
466 }
467 sid = glyph_to_sid_map ? glyph_to_sid_map->get (old_glyph) : acc.glyph_to_sid (old_glyph);
468
469 if (!acc.is_CID ())
470 sid = sidmap.add (sid);
471
472 if ((last_sid == CFF_UNDEF_CODE) || (sid != last_sid + 1))
473 {
474 code_pair_t pair = { sid, glyph };
475 subset_charset_ranges.push (pair);
476 }
477 last_sid = sid;
478 }
479
480 if (created_map)
481 {
482 if (!(plan->accelerator && plan->accelerator->cff_accelerator) ||
483 !plan->accelerator->cff_accelerator->glyph_to_sid_map.cmpexch (nullptr, glyph_to_sid_map))
484 hb_map_destroy (glyph_to_sid_map);
485 }
486
487 bool two_byte = subset_charset_ranges.complete (glyph);
488
489 size0 = Charset0::min_size + HBUINT16::static_size * (plan->num_output_glyphs () - 1);
490 if (!two_byte)
491 size_ranges = Charset1::min_size + Charset1_Range::static_size * subset_charset_ranges.length;
492 else
493 size_ranges = Charset2::min_size + Charset2_Range::static_size * subset_charset_ranges.length;
494
495 if (size0 < size_ranges)
496 subset_charset_format = 0;
497 else if (!two_byte)
498 subset_charset_format = 1;
499 else
500 subset_charset_format = 2;
501 }
502
collect_sids_in_dictscff_subset_plan503 bool collect_sids_in_dicts (const OT::cff1::accelerator_subset_t &acc)
504 {
505 sidmap.reset ();
506
507 for (unsigned int i = 0; i < name_dict_values_t::ValCount; i++)
508 {
509 unsigned int sid = acc.topDict.nameSIDs[i];
510 if (sid != CFF_UNDEF_SID)
511 {
512 (void)sidmap.add (sid);
513 topDictModSIDs[i] = sidmap[sid];
514 }
515 }
516
517 if (acc.fdArray != &Null (CFF1FDArray))
518 for (unsigned int i = 0; i < orig_fdcount; i++)
519 if (fdmap.has (i))
520 (void)sidmap.add (acc.fontDicts[i].fontName);
521
522 return true;
523 }
524
createcff_subset_plan525 bool create (const OT::cff1::accelerator_subset_t &acc,
526 hb_subset_plan_t *plan)
527 {
528 /* make sure notdef is first */
529 hb_codepoint_t old_glyph;
530 if (!plan->old_gid_for_new_gid (0, &old_glyph) || (old_glyph != 0)) return false;
531
532 num_glyphs = plan->num_output_glyphs ();
533 orig_fdcount = acc.fdCount;
534 drop_hints = plan->flags & HB_SUBSET_FLAGS_NO_HINTING;
535 desubroutinize = plan->flags & HB_SUBSET_FLAGS_DESUBROUTINIZE;
536
537 /* check whether the subset renumbers any glyph IDs */
538 gid_renum = false;
539 for (hb_codepoint_t new_glyph = 0; new_glyph < plan->num_output_glyphs (); new_glyph++)
540 {
541 if (!plan->old_gid_for_new_gid(new_glyph, &old_glyph))
542 continue;
543 if (new_glyph != old_glyph) {
544 gid_renum = true;
545 break;
546 }
547 }
548
549 subset_charset = gid_renum || !acc.is_predef_charset ();
550 subset_encoding = !acc.is_CID() && !acc.is_predef_encoding ();
551
552 /* top dict INDEX */
553 {
554 /* Add encoding/charset to a (copy of) top dict as necessary */
555 topdict_mod.init (&acc.topDict);
556 bool need_to_add_enc = (subset_encoding && !acc.topDict.has_op (OpCode_Encoding));
557 bool need_to_add_set = (subset_charset && !acc.topDict.has_op (OpCode_charset));
558 if (need_to_add_enc || need_to_add_set)
559 {
560 if (need_to_add_enc)
561 topdict_mod.add_op (OpCode_Encoding);
562 if (need_to_add_set)
563 topdict_mod.add_op (OpCode_charset);
564 }
565 }
566
567 /* Determine re-mapping of font index as fdmap among other info */
568 if (acc.fdSelect != &Null (CFF1FDSelect))
569 {
570 if (unlikely (!hb_plan_subset_cff_fdselect (plan,
571 orig_fdcount,
572 *acc.fdSelect,
573 subset_fdcount,
574 info.fd_select.size,
575 subset_fdselect_format,
576 subset_fdselect_ranges,
577 fdmap)))
578 return false;
579 }
580 else
581 fdmap.identity (1);
582
583 /* remove unused SIDs & reassign SIDs */
584 {
585 /* SIDs for name strings in dicts are added before glyph names so they fit in 16-bit int range */
586 if (unlikely (!collect_sids_in_dicts (acc)))
587 return false;
588 if (unlikely (sidmap.get_population () > 0x8000)) /* assumption: a dict won't reference that many strings */
589 return false;
590
591 if (subset_charset) plan_subset_charset (acc, plan);
592
593 topdict_mod.reassignSIDs (sidmap);
594 }
595
596 if (desubroutinize)
597 {
598 /* Flatten global & local subrs */
599 subr_flattener_t<const OT::cff1::accelerator_subset_t, cff1_cs_interp_env_t, cff1_cs_opset_flatten_t, OpCode_endchar>
600 flattener(acc, plan);
601 if (!flattener.flatten (subset_charstrings))
602 return false;
603 }
604 else
605 {
606 cff1_subr_subsetter_t subr_subsetter (acc, plan);
607
608 /* Subset subrs: collect used subroutines, leaving all unused ones behind */
609 if (!subr_subsetter.subset ())
610 return false;
611
612 /* encode charstrings, global subrs, local subrs with new subroutine numbers */
613 if (!subr_subsetter.encode_charstrings (subset_charstrings))
614 return false;
615
616 if (!subr_subsetter.encode_globalsubrs (subset_globalsubrs))
617 return false;
618
619 /* local subrs */
620 if (!subset_localsubrs.resize (orig_fdcount))
621 return false;
622 for (unsigned int fd = 0; fd < orig_fdcount; fd++)
623 {
624 subset_localsubrs[fd].init ();
625 if (fdmap.has (fd))
626 {
627 if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd]))
628 return false;
629 }
630 }
631 }
632
633 /* Encoding */
634 if (subset_encoding)
635 plan_subset_encoding (acc, plan);
636
637 /* private dicts & local subrs */
638 if (!acc.is_CID ())
639 fontdicts_mod.push (cff1_font_dict_values_mod_t ());
640 else
641 {
642 + hb_iter (acc.fontDicts)
643 | hb_filter ([&] (const cff1_font_dict_values_t &_)
644 { return fdmap.has (&_ - &acc.fontDicts[0]); } )
645 | hb_map ([&] (const cff1_font_dict_values_t &_)
646 {
647 cff1_font_dict_values_mod_t mod;
648 mod.init (&_, sidmap[_.fontName]);
649 return mod;
650 })
651 | hb_sink (fontdicts_mod)
652 ;
653 }
654
655 return ((subset_charstrings.length == plan->num_output_glyphs ())
656 && (fontdicts_mod.length == subset_fdcount));
657 }
658
659 cff1_top_dict_values_mod_t topdict_mod;
660 cff1_sub_table_info_t info;
661
662 unsigned int num_glyphs;
663 unsigned int orig_fdcount = 0;
664 unsigned int subset_fdcount = 1;
665 unsigned int subset_fdselect_format = 0;
666 hb_vector_t<code_pair_t> subset_fdselect_ranges;
667
668 /* font dict index remap table from fullset FDArray to subset FDArray.
669 * set to CFF_UNDEF_CODE if excluded from subset */
670 hb_inc_bimap_t fdmap;
671
672 str_buff_vec_t subset_charstrings;
673 str_buff_vec_t subset_globalsubrs;
674 hb_vector_t<str_buff_vec_t> subset_localsubrs;
675 hb_vector_t<cff1_font_dict_values_mod_t> fontdicts_mod;
676
677 bool drop_hints = false;
678
679 bool gid_renum;
680 bool subset_encoding;
681 uint8_t subset_enc_format;
682 unsigned int subset_enc_num_codes;
683 range_list_t subset_enc_code_ranges;
684 hb_vector_t<code_pair_t> subset_enc_supp_codes;
685
686 uint8_t subset_charset_format;
687 range_list_t subset_charset_ranges;
688 bool subset_charset;
689
690 remap_sid_t sidmap;
691 unsigned int topDictModSIDs[name_dict_values_t::ValCount];
692
693 bool desubroutinize = false;
694 };
695
_serialize_cff1(hb_serialize_context_t * c,cff_subset_plan & plan,const OT::cff1::accelerator_subset_t & acc,unsigned int num_glyphs)696 static bool _serialize_cff1 (hb_serialize_context_t *c,
697 cff_subset_plan &plan,
698 const OT::cff1::accelerator_subset_t &acc,
699 unsigned int num_glyphs)
700 {
701 /* private dicts & local subrs */
702 for (int i = (int)acc.privateDicts.length; --i >= 0 ;)
703 {
704 if (plan.fdmap.has (i))
705 {
706 objidx_t subrs_link = 0;
707 if (plan.subset_localsubrs[i].length > 0)
708 {
709 CFF1Subrs *dest = c->start_embed <CFF1Subrs> ();
710 if (unlikely (!dest)) return false;
711 c->push ();
712 if (likely (dest && dest->serialize (c, plan.subset_localsubrs[i])))
713 subrs_link = c->pop_pack ();
714 else
715 {
716 c->pop_discard ();
717 return false;
718 }
719 }
720
721 PrivateDict *pd = c->start_embed<PrivateDict> ();
722 if (unlikely (!pd)) return false;
723 c->push ();
724 cff_private_dict_op_serializer_t privSzr (plan.desubroutinize, plan.drop_hints);
725 /* N.B. local subrs immediately follows its corresponding private dict. i.e., subr offset == private dict size */
726 if (likely (pd->serialize (c, acc.privateDicts[i], privSzr, subrs_link)))
727 {
728 unsigned fd = plan.fdmap[i];
729 plan.fontdicts_mod[fd].privateDictInfo.size = c->length ();
730 plan.fontdicts_mod[fd].privateDictInfo.link = c->pop_pack ();
731 }
732 else
733 {
734 c->pop_discard ();
735 return false;
736 }
737 }
738 }
739
740 if (!acc.is_CID ())
741 plan.info.privateDictInfo = plan.fontdicts_mod[0].privateDictInfo;
742
743 /* CharStrings */
744 {
745 c->push<CFF1CharStrings> ();
746
747 unsigned total_size = CFF1CharStrings::total_size (plan.subset_charstrings);
748 if (unlikely (!c->start_zerocopy (total_size)))
749 return false;
750
751 CFF1CharStrings *cs = c->start_embed<CFF1CharStrings> ();
752 if (unlikely (!cs)) return false;
753
754 if (likely (cs->serialize (c, plan.subset_charstrings)))
755 plan.info.char_strings_link = c->pop_pack (false);
756 else
757 {
758 c->pop_discard ();
759 return false;
760 }
761 }
762
763 /* FDArray (FD Index) */
764 if (acc.fdArray != &Null (CFF1FDArray))
765 {
766 CFF1FDArray *fda = c->start_embed<CFF1FDArray> ();
767 if (unlikely (!fda)) return false;
768 c->push ();
769 cff1_font_dict_op_serializer_t fontSzr;
770 auto it = + hb_zip (+ hb_iter (plan.fontdicts_mod), + hb_iter (plan.fontdicts_mod));
771 if (likely (fda->serialize (c, it, fontSzr)))
772 plan.info.fd_array_link = c->pop_pack (false);
773 else
774 {
775 c->pop_discard ();
776 return false;
777 }
778 }
779
780 /* FDSelect */
781 if (acc.fdSelect != &Null (CFF1FDSelect))
782 {
783 c->push ();
784 if (likely (hb_serialize_cff_fdselect (c, num_glyphs, *acc.fdSelect, acc.fdCount,
785 plan.subset_fdselect_format, plan.info.fd_select.size,
786 plan.subset_fdselect_ranges)))
787 plan.info.fd_select.link = c->pop_pack ();
788 else
789 {
790 c->pop_discard ();
791 return false;
792 }
793 }
794
795 /* Charset */
796 if (plan.subset_charset)
797 {
798 Charset *dest = c->start_embed<Charset> ();
799 if (unlikely (!dest)) return false;
800 c->push ();
801 if (likely (dest->serialize (c,
802 plan.subset_charset_format,
803 plan.num_glyphs,
804 plan.subset_charset_ranges)))
805 plan.info.charset_link = c->pop_pack ();
806 else
807 {
808 c->pop_discard ();
809 return false;
810 }
811 }
812
813 /* Encoding */
814 if (plan.subset_encoding)
815 {
816 Encoding *dest = c->start_embed<Encoding> ();
817 if (unlikely (!dest)) return false;
818 c->push ();
819 if (likely (dest->serialize (c,
820 plan.subset_enc_format,
821 plan.subset_enc_num_codes,
822 plan.subset_enc_code_ranges,
823 plan.subset_enc_supp_codes)))
824 plan.info.encoding_link = c->pop_pack ();
825 else
826 {
827 c->pop_discard ();
828 return false;
829 }
830 }
831
832 /* global subrs */
833 {
834 c->push ();
835 CFF1Subrs *dest = c->start_embed <CFF1Subrs> ();
836 if (unlikely (!dest)) return false;
837 if (likely (dest->serialize (c, plan.subset_globalsubrs)))
838 c->pop_pack (false);
839 else
840 {
841 c->pop_discard ();
842 return false;
843 }
844 }
845
846 /* String INDEX */
847 {
848 CFF1StringIndex *dest = c->start_embed<CFF1StringIndex> ();
849 if (unlikely (!dest)) return false;
850 c->push ();
851 if (likely (dest->serialize (c, *acc.stringIndex, plan.sidmap)))
852 c->pop_pack ();
853 else
854 {
855 c->pop_discard ();
856 return false;
857 }
858 }
859
860 OT::cff1 *cff = c->allocate_min<OT::cff1> ();
861 if (unlikely (!cff))
862 return false;
863
864 /* header */
865 cff->version.major = 0x01;
866 cff->version.minor = 0x00;
867 cff->nameIndex = cff->min_size;
868 cff->offSize = 4; /* unused? */
869
870 /* name INDEX */
871 if (unlikely (!(*acc.nameIndex).copy (c))) return false;
872
873 /* top dict INDEX */
874 {
875 /* serialize singleton TopDict */
876 TopDict *top = c->start_embed<TopDict> ();
877 if (!top) return false;
878 c->push ();
879 cff1_top_dict_op_serializer_t topSzr;
880 unsigned top_size = 0;
881 top_dict_modifiers_t modifier (plan.info, plan.topDictModSIDs);
882 if (likely (top->serialize (c, plan.topdict_mod, topSzr, modifier)))
883 {
884 top_size = c->length ();
885 c->pop_pack (false);
886 }
887 else
888 {
889 c->pop_discard ();
890 return false;
891 }
892 /* serialize INDEX header for above */
893 CFF1Index *dest = c->start_embed<CFF1Index> ();
894 if (!dest) return false;
895 return dest->serialize_header (c, hb_iter (hb_array_t<unsigned> (&top_size, 1)));
896 }
897 }
898
899 static bool
_hb_subset_cff1(const OT::cff1::accelerator_subset_t & acc,hb_subset_context_t * c)900 _hb_subset_cff1 (const OT::cff1::accelerator_subset_t &acc,
901 hb_subset_context_t *c)
902 {
903 cff_subset_plan cff_plan;
904
905 if (unlikely (!cff_plan.create (acc, c->plan)))
906 {
907 DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff subsetting plan.");
908 return false;
909 }
910
911 return _serialize_cff1 (c->serializer, cff_plan, acc, c->plan->num_output_glyphs ());
912 }
913
914 bool
hb_subset_cff1(hb_subset_context_t * c)915 hb_subset_cff1 (hb_subset_context_t *c)
916 {
917 OT::cff1::accelerator_subset_t acc;
918 acc.init (c->plan->source);
919 bool result = likely (acc.is_valid ()) && _hb_subset_cff1 (acc, c);
920 acc.fini ();
921
922 return result;
923 }
924
925
926 #endif
927