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 | #ifndef HB_OT_CFF2_TABLE_HH |
28 | #define HB_OT_CFF2_TABLE_HH |
29 | |
30 | #include "hb-ot-head-table.hh" |
31 | #include "hb-ot-cff-common.hh" |
32 | #include "hb-subset-cff2.hh" |
33 | |
34 | namespace CFF { |
35 | |
36 | /* |
37 | * CFF2 -- Compact Font Format (CFF) Version 2 |
38 | * https://docs.microsoft.com/en-us/typography/opentype/spec/cff2 |
39 | */ |
40 | #define HB_OT_TAG_cff2 HB_TAG('C','F','F','2') |
41 | |
42 | typedef CFFIndex<HBUINT32> CFF2Index; |
43 | template <typename Type> struct CFF2IndexOf : CFFIndexOf<HBUINT32, Type> {}; |
44 | |
45 | typedef CFF2Index CFF2CharStrings; |
46 | typedef FDArray<HBUINT32> CFF2FDArray; |
47 | typedef Subrs<HBUINT32> CFF2Subrs; |
48 | |
49 | typedef FDSelect3_4<HBUINT32, HBUINT16> FDSelect4; |
50 | typedef FDSelect3_4_Range<HBUINT32, HBUINT16> FDSelect4_Range; |
51 | |
52 | struct CFF2FDSelect |
53 | { |
54 | bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const |
55 | { |
56 | TRACE_SANITIZE (this); |
57 | |
58 | return_trace (likely (c->check_struct (this) && (format == 0 || format == 3 || format == 4) && |
59 | (format == 0)? |
60 | u.format0.sanitize (c, fdcount): |
61 | ((format == 3)? |
62 | u.format3.sanitize (c, fdcount): |
63 | u.format4.sanitize (c, fdcount)))); |
64 | } |
65 | |
66 | bool serialize (hb_serialize_context_t *c, const CFF2FDSelect &src, unsigned int num_glyphs) |
67 | { |
68 | TRACE_SERIALIZE (this); |
69 | unsigned int size = src.get_size (num_glyphs); |
70 | CFF2FDSelect *dest = c->allocate_size<CFF2FDSelect> (size); |
71 | if (unlikely (dest == nullptr)) return_trace (false); |
72 | memcpy (dest, &src, size); |
73 | return_trace (true); |
74 | } |
75 | |
76 | unsigned int calculate_serialized_size (unsigned int num_glyphs) const |
77 | { return get_size (num_glyphs); } |
78 | |
79 | unsigned int get_size (unsigned int num_glyphs) const |
80 | { |
81 | unsigned int size = format.static_size; |
82 | if (format == 0) |
83 | size += u.format0.get_size (num_glyphs); |
84 | else if (format == 3) |
85 | size += u.format3.get_size (); |
86 | else |
87 | size += u.format4.get_size (); |
88 | return size; |
89 | } |
90 | |
91 | hb_codepoint_t get_fd (hb_codepoint_t glyph) const |
92 | { |
93 | if (this == &Null(CFF2FDSelect)) |
94 | return 0; |
95 | if (format == 0) |
96 | return u.format0.get_fd (glyph); |
97 | else if (format == 3) |
98 | return u.format3.get_fd (glyph); |
99 | else |
100 | return u.format4.get_fd (glyph); |
101 | } |
102 | |
103 | HBUINT8 format; |
104 | union { |
105 | FDSelect0 format0; |
106 | FDSelect3 format3; |
107 | FDSelect4 format4; |
108 | } u; |
109 | |
110 | DEFINE_SIZE_MIN (2); |
111 | }; |
112 | |
113 | struct CFF2VariationStore |
114 | { |
115 | bool sanitize (hb_sanitize_context_t *c) const |
116 | { |
117 | TRACE_SANITIZE (this); |
118 | return_trace (likely (c->check_struct (this)) && c->check_range (&varStore, size) && varStore.sanitize (c)); |
119 | } |
120 | |
121 | bool serialize (hb_serialize_context_t *c, const CFF2VariationStore *varStore) |
122 | { |
123 | TRACE_SERIALIZE (this); |
124 | unsigned int size_ = varStore->get_size (); |
125 | CFF2VariationStore *dest = c->allocate_size<CFF2VariationStore> (size_); |
126 | if (unlikely (dest == nullptr)) return_trace (false); |
127 | memcpy (dest, varStore, size_); |
128 | return_trace (true); |
129 | } |
130 | |
131 | unsigned int get_size () const { return HBUINT16::static_size + size; } |
132 | |
133 | HBUINT16 size; |
134 | VariationStore varStore; |
135 | |
136 | DEFINE_SIZE_MIN (2 + VariationStore::min_size); |
137 | }; |
138 | |
139 | struct cff2_top_dict_values_t : top_dict_values_t<> |
140 | { |
141 | void init () |
142 | { |
143 | top_dict_values_t<>::init (); |
144 | vstoreOffset = 0; |
145 | FDSelectOffset = 0; |
146 | } |
147 | void fini () { top_dict_values_t<>::fini (); } |
148 | |
149 | unsigned int calculate_serialized_size () const |
150 | { |
151 | unsigned int size = 0; |
152 | for (unsigned int i = 0; i < get_count (); i++) |
153 | { |
154 | op_code_t op = get_value (i).op; |
155 | switch (op) |
156 | { |
157 | case OpCode_vstore: |
158 | case OpCode_FDSelect: |
159 | size += OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (op); |
160 | break; |
161 | default: |
162 | size += top_dict_values_t<>::calculate_serialized_op_size (get_value (i)); |
163 | break; |
164 | } |
165 | } |
166 | return size; |
167 | } |
168 | |
169 | unsigned int vstoreOffset; |
170 | unsigned int FDSelectOffset; |
171 | }; |
172 | |
173 | struct cff2_top_dict_opset_t : top_dict_opset_t<> |
174 | { |
175 | static void process_op (op_code_t op, num_interp_env_t& env, cff2_top_dict_values_t& dictval) |
176 | { |
177 | switch (op) { |
178 | case OpCode_FontMatrix: |
179 | { |
180 | dict_val_t val; |
181 | val.init (); |
182 | dictval.add_op (op, env.str_ref); |
183 | env.clear_args (); |
184 | } |
185 | break; |
186 | |
187 | case OpCode_vstore: |
188 | dictval.vstoreOffset = env.argStack.pop_uint (); |
189 | env.clear_args (); |
190 | break; |
191 | case OpCode_FDSelect: |
192 | dictval.FDSelectOffset = env.argStack.pop_uint (); |
193 | env.clear_args (); |
194 | break; |
195 | |
196 | default: |
197 | SUPER::process_op (op, env, dictval); |
198 | /* Record this operand below if stack is empty, otherwise done */ |
199 | if (!env.argStack.is_empty ()) return; |
200 | } |
201 | |
202 | if (unlikely (env.in_error ())) return; |
203 | |
204 | dictval.add_op (op, env.str_ref); |
205 | } |
206 | |
207 | typedef top_dict_opset_t<> SUPER; |
208 | }; |
209 | |
210 | struct cff2_font_dict_values_t : dict_values_t<op_str_t> |
211 | { |
212 | void init () |
213 | { |
214 | dict_values_t<op_str_t>::init (); |
215 | privateDictInfo.init (); |
216 | } |
217 | void fini () { dict_values_t<op_str_t>::fini (); } |
218 | |
219 | table_info_t privateDictInfo; |
220 | }; |
221 | |
222 | struct cff2_font_dict_opset_t : dict_opset_t |
223 | { |
224 | static void process_op (op_code_t op, num_interp_env_t& env, cff2_font_dict_values_t& dictval) |
225 | { |
226 | switch (op) { |
227 | case OpCode_Private: |
228 | dictval.privateDictInfo.offset = env.argStack.pop_uint (); |
229 | dictval.privateDictInfo.size = env.argStack.pop_uint (); |
230 | env.clear_args (); |
231 | break; |
232 | |
233 | default: |
234 | SUPER::process_op (op, env); |
235 | if (!env.argStack.is_empty ()) |
236 | return; |
237 | } |
238 | |
239 | if (unlikely (env.in_error ())) return; |
240 | |
241 | dictval.add_op (op, env.str_ref); |
242 | } |
243 | |
244 | private: |
245 | typedef dict_opset_t SUPER; |
246 | }; |
247 | |
248 | template <typename VAL> |
249 | struct cff2_private_dict_values_base_t : dict_values_t<VAL> |
250 | { |
251 | void init () |
252 | { |
253 | dict_values_t<VAL>::init (); |
254 | subrsOffset = 0; |
255 | localSubrs = &Null(CFF2Subrs); |
256 | ivs = 0; |
257 | } |
258 | void fini () { dict_values_t<VAL>::fini (); } |
259 | |
260 | unsigned int calculate_serialized_size () const |
261 | { |
262 | unsigned int size = 0; |
263 | for (unsigned int i = 0; i < dict_values_t<VAL>::get_count; i++) |
264 | if (dict_values_t<VAL>::get_value (i).op == OpCode_Subrs) |
265 | size += OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Subrs); |
266 | else |
267 | size += dict_values_t<VAL>::get_value (i).str.length; |
268 | return size; |
269 | } |
270 | |
271 | unsigned int subrsOffset; |
272 | const CFF2Subrs *localSubrs; |
273 | unsigned int ivs; |
274 | }; |
275 | |
276 | typedef cff2_private_dict_values_base_t<op_str_t> cff2_private_dict_values_subset_t; |
277 | typedef cff2_private_dict_values_base_t<num_dict_val_t> cff2_private_dict_values_t; |
278 | |
279 | struct cff2_priv_dict_interp_env_t : num_interp_env_t |
280 | { |
281 | void init (const byte_str_t &str) |
282 | { |
283 | num_interp_env_t::init (str); |
284 | ivs = 0; |
285 | seen_vsindex = false; |
286 | } |
287 | |
288 | void process_vsindex () |
289 | { |
290 | if (likely (!seen_vsindex)) |
291 | { |
292 | set_ivs (argStack.pop_uint ()); |
293 | } |
294 | seen_vsindex = true; |
295 | } |
296 | |
297 | unsigned int get_ivs () const { return ivs; } |
298 | void set_ivs (unsigned int ivs_) { ivs = ivs_; } |
299 | |
300 | protected: |
301 | unsigned int ivs; |
302 | bool seen_vsindex; |
303 | }; |
304 | |
305 | struct cff2_private_dict_opset_t : dict_opset_t |
306 | { |
307 | static void process_op (op_code_t op, cff2_priv_dict_interp_env_t& env, cff2_private_dict_values_t& dictval) |
308 | { |
309 | num_dict_val_t val; |
310 | val.init (); |
311 | |
312 | switch (op) { |
313 | case OpCode_StdHW: |
314 | case OpCode_StdVW: |
315 | case OpCode_BlueScale: |
316 | case OpCode_BlueShift: |
317 | case OpCode_BlueFuzz: |
318 | case OpCode_ExpansionFactor: |
319 | case OpCode_LanguageGroup: |
320 | val.single_val = env.argStack.pop_num (); |
321 | env.clear_args (); |
322 | break; |
323 | case OpCode_BlueValues: |
324 | case OpCode_OtherBlues: |
325 | case OpCode_FamilyBlues: |
326 | case OpCode_FamilyOtherBlues: |
327 | case OpCode_StemSnapH: |
328 | case OpCode_StemSnapV: |
329 | env.clear_args (); |
330 | break; |
331 | case OpCode_Subrs: |
332 | dictval.subrsOffset = env.argStack.pop_uint (); |
333 | env.clear_args (); |
334 | break; |
335 | case OpCode_vsindexdict: |
336 | env.process_vsindex (); |
337 | dictval.ivs = env.get_ivs (); |
338 | env.clear_args (); |
339 | break; |
340 | case OpCode_blenddict: |
341 | break; |
342 | |
343 | default: |
344 | dict_opset_t::process_op (op, env); |
345 | if (!env.argStack.is_empty ()) return; |
346 | break; |
347 | } |
348 | |
349 | if (unlikely (env.in_error ())) return; |
350 | |
351 | dictval.add_op (op, env.str_ref, val); |
352 | } |
353 | }; |
354 | |
355 | struct cff2_private_dict_opset_subset_t : dict_opset_t |
356 | { |
357 | static void process_op (op_code_t op, cff2_priv_dict_interp_env_t& env, cff2_private_dict_values_subset_t& dictval) |
358 | { |
359 | switch (op) { |
360 | case OpCode_BlueValues: |
361 | case OpCode_OtherBlues: |
362 | case OpCode_FamilyBlues: |
363 | case OpCode_FamilyOtherBlues: |
364 | case OpCode_StdHW: |
365 | case OpCode_StdVW: |
366 | case OpCode_BlueScale: |
367 | case OpCode_BlueShift: |
368 | case OpCode_BlueFuzz: |
369 | case OpCode_StemSnapH: |
370 | case OpCode_StemSnapV: |
371 | case OpCode_LanguageGroup: |
372 | case OpCode_ExpansionFactor: |
373 | env.clear_args (); |
374 | break; |
375 | |
376 | case OpCode_blenddict: |
377 | env.clear_args (); |
378 | return; |
379 | |
380 | case OpCode_Subrs: |
381 | dictval.subrsOffset = env.argStack.pop_uint (); |
382 | env.clear_args (); |
383 | break; |
384 | |
385 | default: |
386 | SUPER::process_op (op, env); |
387 | if (!env.argStack.is_empty ()) return; |
388 | break; |
389 | } |
390 | |
391 | if (unlikely (env.in_error ())) return; |
392 | |
393 | dictval.add_op (op, env.str_ref); |
394 | } |
395 | |
396 | private: |
397 | typedef dict_opset_t SUPER; |
398 | }; |
399 | |
400 | typedef dict_interpreter_t<cff2_top_dict_opset_t, cff2_top_dict_values_t> cff2_top_dict_interpreter_t; |
401 | typedef dict_interpreter_t<cff2_font_dict_opset_t, cff2_font_dict_values_t> cff2_font_dict_interpreter_t; |
402 | |
403 | } /* namespace CFF */ |
404 | |
405 | namespace OT { |
406 | |
407 | using namespace CFF; |
408 | |
409 | struct cff2 |
410 | { |
411 | static constexpr hb_tag_t tableTag = HB_OT_TAG_cff2; |
412 | |
413 | bool sanitize (hb_sanitize_context_t *c) const |
414 | { |
415 | TRACE_SANITIZE (this); |
416 | return_trace (c->check_struct (this) && |
417 | likely (version.major == 2)); |
418 | } |
419 | |
420 | template <typename PRIVOPSET, typename PRIVDICTVAL> |
421 | struct accelerator_templ_t |
422 | { |
423 | void init (hb_face_t *face) |
424 | { |
425 | topDict.init (); |
426 | fontDicts.init (); |
427 | privateDicts.init (); |
428 | |
429 | this->blob = sc.reference_table<cff2> (face); |
430 | |
431 | /* setup for run-time santization */ |
432 | sc.init (this->blob); |
433 | sc.start_processing (); |
434 | |
435 | const OT::cff2 *cff2 = this->blob->template as<OT::cff2> (); |
436 | |
437 | if (cff2 == &Null(OT::cff2)) |
438 | { fini (); return; } |
439 | |
440 | { /* parse top dict */ |
441 | byte_str_t topDictStr (cff2 + cff2->topDict, cff2->topDictSize); |
442 | if (unlikely (!topDictStr.sanitize (&sc))) { fini (); return; } |
443 | cff2_top_dict_interpreter_t top_interp; |
444 | top_interp.env.init (topDictStr); |
445 | topDict.init (); |
446 | if (unlikely (!top_interp.interpret (topDict))) { fini (); return; } |
447 | } |
448 | |
449 | globalSubrs = &StructAtOffset<CFF2Subrs> (cff2, cff2->topDict + cff2->topDictSize); |
450 | varStore = &StructAtOffsetOrNull<CFF2VariationStore> (cff2, topDict.vstoreOffset); |
451 | charStrings = &StructAtOffsetOrNull<CFF2CharStrings> (cff2, topDict.charStringsOffset); |
452 | fdArray = &StructAtOffsetOrNull<CFF2FDArray> (cff2, topDict.FDArrayOffset); |
453 | fdSelect = &StructAtOffsetOrNull<CFF2FDSelect> (cff2, topDict.FDSelectOffset); |
454 | |
455 | if (((varStore != &Null(CFF2VariationStore)) && unlikely (!varStore->sanitize (&sc))) || |
456 | (charStrings == &Null(CFF2CharStrings)) || unlikely (!charStrings->sanitize (&sc)) || |
457 | (globalSubrs == &Null(CFF2Subrs)) || unlikely (!globalSubrs->sanitize (&sc)) || |
458 | (fdArray == &Null(CFF2FDArray)) || unlikely (!fdArray->sanitize (&sc)) || |
459 | (((fdSelect != &Null(CFF2FDSelect)) && unlikely (!fdSelect->sanitize (&sc, fdArray->count))))) |
460 | { fini (); return; } |
461 | |
462 | num_glyphs = charStrings->count; |
463 | if (num_glyphs != sc.get_num_glyphs ()) |
464 | { fini (); return; } |
465 | |
466 | fdCount = fdArray->count; |
467 | privateDicts.resize (fdCount); |
468 | |
469 | /* parse font dicts and gather private dicts */ |
470 | for (unsigned int i = 0; i < fdCount; i++) |
471 | { |
472 | const byte_str_t fontDictStr = (*fdArray)[i]; |
473 | if (unlikely (!fontDictStr.sanitize (&sc))) { fini (); return; } |
474 | cff2_font_dict_values_t *font; |
475 | cff2_font_dict_interpreter_t font_interp; |
476 | font_interp.env.init (fontDictStr); |
477 | font = fontDicts.push (); |
478 | if (unlikely (font == &Crap(cff2_font_dict_values_t))) { fini (); return; } |
479 | font->init (); |
480 | if (unlikely (!font_interp.interpret (*font))) { fini (); return; } |
481 | |
482 | const byte_str_t privDictStr (StructAtOffsetOrNull<UnsizedByteStr> (cff2, font->privateDictInfo.offset), font->privateDictInfo.size); |
483 | if (unlikely (!privDictStr.sanitize (&sc))) { fini (); return; } |
484 | dict_interpreter_t<PRIVOPSET, PRIVDICTVAL, cff2_priv_dict_interp_env_t> priv_interp; |
485 | priv_interp.env.init(privDictStr); |
486 | privateDicts[i].init (); |
487 | if (unlikely (!priv_interp.interpret (privateDicts[i]))) { fini (); return; } |
488 | |
489 | privateDicts[i].localSubrs = &StructAtOffsetOrNull<CFF2Subrs> (&privDictStr[0], privateDicts[i].subrsOffset); |
490 | if (privateDicts[i].localSubrs != &Null(CFF2Subrs) && |
491 | unlikely (!privateDicts[i].localSubrs->sanitize (&sc))) |
492 | { fini (); return; } |
493 | } |
494 | } |
495 | |
496 | void fini () |
497 | { |
498 | sc.end_processing (); |
499 | topDict.fini (); |
500 | fontDicts.fini_deep (); |
501 | privateDicts.fini_deep (); |
502 | hb_blob_destroy (blob); |
503 | blob = nullptr; |
504 | } |
505 | |
506 | bool is_valid () const { return blob != nullptr; } |
507 | |
508 | protected: |
509 | hb_blob_t *blob; |
510 | hb_sanitize_context_t sc; |
511 | |
512 | public: |
513 | cff2_top_dict_values_t topDict; |
514 | const CFF2Subrs *globalSubrs; |
515 | const CFF2VariationStore *varStore; |
516 | const CFF2CharStrings *charStrings; |
517 | const CFF2FDArray *fdArray; |
518 | const CFF2FDSelect *fdSelect; |
519 | unsigned int fdCount; |
520 | |
521 | hb_vector_t<cff2_font_dict_values_t> fontDicts; |
522 | hb_vector_t<PRIVDICTVAL> privateDicts; |
523 | |
524 | unsigned int num_glyphs; |
525 | }; |
526 | |
527 | struct accelerator_t : accelerator_templ_t<cff2_private_dict_opset_t, cff2_private_dict_values_t> |
528 | { |
529 | HB_INTERNAL bool get_extents (hb_font_t *font, |
530 | hb_codepoint_t glyph, |
531 | hb_glyph_extents_t *extents) const; |
532 | }; |
533 | |
534 | typedef accelerator_templ_t<cff2_private_dict_opset_subset_t, cff2_private_dict_values_subset_t> accelerator_subset_t; |
535 | |
536 | bool subset (hb_subset_plan_t *plan) const |
537 | { |
538 | hb_blob_t *cff2_prime = nullptr; |
539 | |
540 | bool success = true; |
541 | if (hb_subset_cff2 (plan, &cff2_prime)) { |
542 | success = success && plan->add_table (HB_OT_TAG_cff2, cff2_prime); |
543 | hb_blob_t *head_blob = hb_sanitize_context_t().reference_table<head> (plan->source); |
544 | success = success && head_blob && plan->add_table (HB_OT_TAG_head, head_blob); |
545 | hb_blob_destroy (head_blob); |
546 | } else { |
547 | success = false; |
548 | } |
549 | hb_blob_destroy (cff2_prime); |
550 | |
551 | return success; |
552 | } |
553 | |
554 | public: |
555 | FixedVersion<HBUINT8> version; /* Version of CFF2 table. set to 0x0200u */ |
556 | NNOffsetTo<TopDict, HBUINT8> topDict; /* headerSize = Offset to Top DICT. */ |
557 | HBUINT16 topDictSize; /* Top DICT size */ |
558 | |
559 | public: |
560 | DEFINE_SIZE_STATIC (5); |
561 | }; |
562 | |
563 | struct cff2_accelerator_t : cff2::accelerator_t {}; |
564 | } /* namespace OT */ |
565 | |
566 | #endif /* HB_OT_CFF2_TABLE_HH */ |
567 | |