2 * Copyright © 2018 Adobe Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Adobe Author(s): Michiharu Ariza
27 #ifndef HB_OT_CFF1_TABLE_HH
28 #define HB_OT_CFF1_TABLE_HH
30 #include "hb-ot-head-table.hh"
31 #include "hb-ot-cff-common.hh"
32 #include "hb-subset-cff1.hh"
37 * CFF -- Compact Font Format (CFF)
38 * http://www.adobe.com/content/dam/acom/en/devnet/font/pdfs/5176.CFF.pdf
40 #define HB_OT_TAG_cff1 HB_TAG('C','F','F',' ')
42 #define CFF_UNDEF_SID CFF_UNDEF_CODE
44 enum EncodingID { StandardEncoding = 0, ExpertEncoding = 1 };
45 enum CharsetID { ISOAdobeCharset = 0, ExpertCharset = 1, ExpertSubsetCharset = 2 };
47 typedef CFFIndex<HBUINT16> CFF1Index;
48 template <typename Type> struct CFF1IndexOf : CFFIndexOf<HBUINT16, Type> {};
50 typedef CFFIndex<HBUINT16> CFF1Index;
51 typedef CFF1Index CFF1CharStrings;
52 typedef FDArray<HBUINT16> CFF1FDArray;
53 typedef Subrs<HBUINT16> CFF1Subrs;
55 struct CFF1FDSelect : FDSelect {};
59 bool sanitize (hb_sanitize_context_t *c) const
61 TRACE_SANITIZE (this);
62 return_trace (c->check_struct (this) && codes[nCodes - 1].sanitize (c));
65 hb_codepoint_t get_code (hb_codepoint_t glyph) const
71 return (hb_codepoint_t)codes[glyph];
74 return CFF_UNDEF_CODE;
77 unsigned int get_size () const
78 { return HBUINT8::static_size * (nCodes + 1); }
83 DEFINE_SIZE_ARRAY(1, codes);
86 struct Encoding1_Range {
87 bool sanitize (hb_sanitize_context_t *c) const
89 TRACE_SANITIZE (this);
90 return_trace (c->check_struct (this));
96 DEFINE_SIZE_STATIC (2);
100 unsigned int get_size () const
101 { return HBUINT8::static_size + Encoding1_Range::static_size * nRanges; }
103 bool sanitize (hb_sanitize_context_t *c) const
105 TRACE_SANITIZE (this);
106 return_trace (c->check_struct (this) && ((nRanges == 0) || (ranges[nRanges - 1]).sanitize (c)));
109 hb_codepoint_t get_code (hb_codepoint_t glyph) const
113 for (unsigned int i = 0; i < nRanges; i++)
115 if (glyph <= ranges[i].nLeft)
117 return (hb_codepoint_t)ranges[i].first + glyph;
119 glyph -= (ranges[i].nLeft + 1);
121 return CFF_UNDEF_CODE;
125 Encoding1_Range ranges[VAR];
127 DEFINE_SIZE_ARRAY (1, ranges);
130 struct SuppEncoding {
131 bool sanitize (hb_sanitize_context_t *c) const
133 TRACE_SANITIZE (this);
134 return_trace (c->check_struct (this));
140 DEFINE_SIZE_STATIC (3);
143 struct CFF1SuppEncData {
144 bool sanitize (hb_sanitize_context_t *c) const
146 TRACE_SANITIZE (this);
147 return_trace (c->check_struct (this) && ((nSups == 0) || (supps[nSups - 1]).sanitize (c)));
150 void get_codes (hb_codepoint_t sid, hb_vector_t<hb_codepoint_t> &codes) const
152 for (unsigned int i = 0; i < nSups; i++)
153 if (sid == supps[i].glyph)
154 codes.push (supps[i].code);
157 unsigned int get_size () const
158 { return HBUINT8::static_size + SuppEncoding::static_size * nSups; }
161 SuppEncoding supps[VAR];
163 DEFINE_SIZE_ARRAY (1, supps);
167 bool sanitize (hb_sanitize_context_t *c) const
169 TRACE_SANITIZE (this);
171 if (unlikely (!c->check_struct (this)))
172 return_trace (false);
173 unsigned int fmt = format & 0x7F;
174 if (unlikely (fmt > 1))
175 return_trace (false);
176 if (unlikely (!((fmt == 0)? u.format0.sanitize (c): u.format1.sanitize (c))))
177 return_trace (false);
178 return_trace (((format & 0x80) == 0) || suppEncData ().sanitize (c));
181 /* serialize a fullset Encoding */
182 bool serialize (hb_serialize_context_t *c, const Encoding &src)
184 TRACE_SERIALIZE (this);
185 unsigned int size = src.get_size ();
186 Encoding *dest = c->allocate_size<Encoding> (size);
187 if (unlikely (dest == nullptr)) return_trace (false);
188 memcpy (dest, &src, size);
192 /* serialize a subset Encoding */
193 bool serialize (hb_serialize_context_t *c,
195 unsigned int enc_count,
196 const hb_vector_t<code_pair_t>& code_ranges,
197 const hb_vector_t<code_pair_t>& supp_codes)
199 TRACE_SERIALIZE (this);
200 Encoding *dest = c->extend_min (*this);
201 if (unlikely (dest == nullptr)) return_trace (false);
202 dest->format.set (format | ((supp_codes.length > 0)? 0x80: 0));
205 Encoding0 *fmt0 = c->allocate_size<Encoding0> (Encoding0::min_size + HBUINT8::static_size * enc_count);
206 if (unlikely (fmt0 == nullptr)) return_trace (false);
207 fmt0->nCodes.set (enc_count);
208 unsigned int glyph = 0;
209 for (unsigned int i = 0; i < code_ranges.length; i++)
211 hb_codepoint_t code = code_ranges[i].code;
212 for (int left = (int)code_ranges[i].glyph; left >= 0; left--)
213 fmt0->codes[glyph++].set (code++);
214 if (unlikely (!((glyph <= 0x100) && (code <= 0x100))))
215 return_trace (false);
220 Encoding1 *fmt1 = c->allocate_size<Encoding1> (Encoding1::min_size + Encoding1_Range::static_size * code_ranges.length);
221 if (unlikely (fmt1 == nullptr)) return_trace (false);
222 fmt1->nRanges.set (code_ranges.length);
223 for (unsigned int i = 0; i < code_ranges.length; i++)
225 if (unlikely (!((code_ranges[i].code <= 0xFF) && (code_ranges[i].glyph <= 0xFF))))
226 return_trace (false);
227 fmt1->ranges[i].first.set (code_ranges[i].code);
228 fmt1->ranges[i].nLeft.set (code_ranges[i].glyph);
231 if (supp_codes.length > 0)
233 CFF1SuppEncData *suppData = c->allocate_size<CFF1SuppEncData> (CFF1SuppEncData::min_size + SuppEncoding::static_size * supp_codes.length);
234 if (unlikely (suppData == nullptr)) return_trace (false);
235 suppData->nSups.set (supp_codes.length);
236 for (unsigned int i = 0; i < supp_codes.length; i++)
238 suppData->supps[i].code.set (supp_codes[i].code);
239 suppData->supps[i].glyph.set (supp_codes[i].glyph); /* actually SID */
245 /* parallel to above: calculate the size of a subset Encoding */
246 static unsigned int calculate_serialized_size (uint8_t format,
247 unsigned int enc_count,
248 unsigned int supp_count)
250 unsigned int size = min_size;
252 size += Encoding0::min_size + HBUINT8::static_size * enc_count;
254 size += Encoding1::min_size + Encoding1_Range::static_size * enc_count;
256 size += CFF1SuppEncData::min_size + SuppEncoding::static_size * supp_count;
260 unsigned int get_size () const
262 unsigned int size = min_size;
263 if (table_format () == 0)
264 size += u.format0.get_size ();
266 size += u.format1.get_size ();
267 if (has_supplement ())
268 size += suppEncData ().get_size ();
272 hb_codepoint_t get_code (hb_codepoint_t glyph) const
274 if (table_format () == 0)
275 return u.format0.get_code (glyph);
277 return u.format1.get_code (glyph);
280 uint8_t table_format () const { return (format & 0x7F); }
281 bool has_supplement () const { return (format & 0x80) != 0; }
283 void get_supplement_codes (hb_codepoint_t sid, hb_vector_t<hb_codepoint_t> &codes) const
286 if (has_supplement ())
287 suppEncData().get_codes (sid, codes);
291 const CFF1SuppEncData &suppEncData () const
293 if ((format & 0x7F) == 0)
294 return StructAfter<CFF1SuppEncData> (u.format0.codes[u.format0.nCodes-1]);
296 return StructAfter<CFF1SuppEncData> (u.format1.ranges[u.format1.nRanges-1]);
306 /* CFF1SuppEncData suppEncData; */
313 bool sanitize (hb_sanitize_context_t *c, unsigned int num_glyphs) const
315 TRACE_SANITIZE (this);
316 return_trace (c->check_struct (this) && sids[num_glyphs - 1].sanitize (c));
319 hb_codepoint_t get_sid (hb_codepoint_t glyph) const
324 return sids[glyph - 1];
327 hb_codepoint_t get_glyph (hb_codepoint_t sid, unsigned int num_glyphs) const
332 for (unsigned int glyph = 1; glyph < num_glyphs; glyph++)
334 if (sids[glyph-1] == sid)
340 unsigned int get_size (unsigned int num_glyphs) const
342 assert (num_glyphs > 0);
343 return HBUINT16::static_size * (num_glyphs - 1);
348 DEFINE_SIZE_ARRAY(0, sids);
351 template <typename TYPE>
352 struct Charset_Range {
353 bool sanitize (hb_sanitize_context_t *c) const
355 TRACE_SANITIZE (this);
356 return_trace (c->check_struct (this));
362 DEFINE_SIZE_STATIC (HBUINT16::static_size + TYPE::static_size);
365 template <typename TYPE>
367 bool sanitize (hb_sanitize_context_t *c, unsigned int num_glyphs) const
369 TRACE_SANITIZE (this);
370 if (unlikely (!c->check_struct (this)))
371 return_trace (false);
373 for (unsigned int i = 0; num_glyphs > 0; i++)
375 if (unlikely (!ranges[i].sanitize (c) || (num_glyphs < ranges[i].nLeft + 1)))
376 return_trace (false);
377 num_glyphs -= (ranges[i].nLeft + 1);
382 hb_codepoint_t get_sid (hb_codepoint_t glyph) const
384 if (glyph == 0) return 0;
386 for (unsigned int i = 0;; i++)
388 if (glyph <= ranges[i].nLeft)
389 return (hb_codepoint_t)ranges[i].first + glyph;
390 glyph -= (ranges[i].nLeft + 1);
396 hb_codepoint_t get_glyph (hb_codepoint_t sid, unsigned int num_glyphs) const
398 if (sid == 0) return 0;
399 hb_codepoint_t glyph = 1;
400 for (unsigned int i = 0;; i++)
402 if (glyph >= num_glyphs)
404 if ((ranges[i].first <= sid) && (sid <= ranges[i].first + ranges[i].nLeft))
405 return glyph + (sid - ranges[i].first);
406 glyph += (ranges[i].nLeft + 1);
412 unsigned int get_size (unsigned int num_glyphs) const
414 unsigned int size = HBUINT8::static_size;
415 int glyph = (int)num_glyphs;
419 for (unsigned int i = 0; glyph > 0; i++)
421 glyph -= (ranges[i].nLeft + 1);
422 size += Charset_Range<TYPE>::static_size;
428 Charset_Range<TYPE> ranges[VAR];
430 DEFINE_SIZE_ARRAY (0, ranges);
433 typedef Charset1_2<HBUINT8> Charset1;
434 typedef Charset1_2<HBUINT16> Charset2;
435 typedef Charset_Range<HBUINT8> Charset1_Range;
436 typedef Charset_Range<HBUINT16> Charset2_Range;
439 bool sanitize (hb_sanitize_context_t *c) const
441 TRACE_SANITIZE (this);
443 if (unlikely (!c->check_struct (this)))
444 return_trace (false);
446 return_trace (u.format0.sanitize (c, c->get_num_glyphs ()));
447 else if (format == 1)
448 return_trace (u.format1.sanitize (c, c->get_num_glyphs ()));
449 else if (likely (format == 2))
450 return_trace (u.format2.sanitize (c, c->get_num_glyphs ()));
452 return_trace (false);
455 /* serialize a fullset Charset */
456 bool serialize (hb_serialize_context_t *c, const Charset &src, unsigned int num_glyphs)
458 TRACE_SERIALIZE (this);
459 unsigned int size = src.get_size (num_glyphs);
460 Charset *dest = c->allocate_size<Charset> (size);
461 if (unlikely (dest == nullptr)) return_trace (false);
462 memcpy (dest, &src, size);
466 /* serialize a subset Charset */
467 bool serialize (hb_serialize_context_t *c,
469 unsigned int num_glyphs,
470 const hb_vector_t<code_pair_t>& sid_ranges)
472 TRACE_SERIALIZE (this);
473 Charset *dest = c->extend_min (*this);
474 if (unlikely (dest == nullptr)) return_trace (false);
475 dest->format.set (format);
478 Charset0 *fmt0 = c->allocate_size<Charset0> (Charset0::min_size + HBUINT16::static_size * (num_glyphs - 1));
479 if (unlikely (fmt0 == nullptr)) return_trace (false);
480 unsigned int glyph = 0;
481 for (unsigned int i = 0; i < sid_ranges.length; i++)
483 hb_codepoint_t sid = sid_ranges[i].code;
484 for (int left = (int)sid_ranges[i].glyph; left >= 0; left--)
485 fmt0->sids[glyph++].set (sid++);
488 else if (format == 1)
490 Charset1 *fmt1 = c->allocate_size<Charset1> (Charset1::min_size + Charset1_Range::static_size * sid_ranges.length);
491 if (unlikely (fmt1 == nullptr)) return_trace (false);
492 for (unsigned int i = 0; i < sid_ranges.length; i++)
494 if (unlikely (!(sid_ranges[i].glyph <= 0xFF)))
495 return_trace (false);
496 fmt1->ranges[i].first.set (sid_ranges[i].code);
497 fmt1->ranges[i].nLeft.set (sid_ranges[i].glyph);
502 Charset2 *fmt2 = c->allocate_size<Charset2> (Charset2::min_size + Charset2_Range::static_size * sid_ranges.length);
503 if (unlikely (fmt2 == nullptr)) return_trace (false);
504 for (unsigned int i = 0; i < sid_ranges.length; i++)
506 if (unlikely (!(sid_ranges[i].glyph <= 0xFFFF)))
507 return_trace (false);
508 fmt2->ranges[i].first.set (sid_ranges[i].code);
509 fmt2->ranges[i].nLeft.set (sid_ranges[i].glyph);
515 /* parallel to above: calculate the size of a subset Charset */
516 static unsigned int calculate_serialized_size (
520 unsigned int size = min_size;
522 size += Charset0::min_size + HBUINT16::static_size * (count - 1);
523 else if (format == 1)
524 size += Charset1::min_size + Charset1_Range::static_size * count;
526 size += Charset2::min_size + Charset2_Range::static_size * count;
531 unsigned int get_size (unsigned int num_glyphs) const
533 unsigned int size = min_size;
535 size += u.format0.get_size (num_glyphs);
536 else if (format == 1)
537 size += u.format1.get_size (num_glyphs);
539 size += u.format2.get_size (num_glyphs);
543 hb_codepoint_t get_sid (hb_codepoint_t glyph) const
546 return u.format0.get_sid (glyph);
547 else if (format == 1)
548 return u.format1.get_sid (glyph);
550 return u.format2.get_sid (glyph);
553 hb_codepoint_t get_glyph (hb_codepoint_t sid, unsigned int num_glyphs) const
556 return u.format0.get_glyph (sid, num_glyphs);
557 else if (format == 1)
558 return u.format1.get_glyph (sid, num_glyphs);
560 return u.format2.get_glyph (sid, num_glyphs);
573 struct CFF1StringIndex : CFF1Index
575 bool serialize (hb_serialize_context_t *c, const CFF1StringIndex &strings,
576 unsigned int offSize_, const remap_t &sidmap)
578 TRACE_SERIALIZE (this);
579 if (unlikely ((strings.count == 0) || (sidmap.get_count () == 0)))
581 if (!unlikely (c->extend_min (this->count)))
582 return_trace (false);
587 byte_str_array_t bytesArray;
589 if (!bytesArray.resize (sidmap.get_count ()))
590 return_trace (false);
591 for (unsigned int i = 0; i < strings.count; i++)
593 hb_codepoint_t j = sidmap[i];
594 if (j != CFF_UNDEF_CODE)
595 bytesArray[j] = strings[i];
598 bool result = CFF1Index::serialize (c, offSize_, bytesArray);
600 return_trace (result);
603 /* in parallel to above */
604 unsigned int calculate_serialized_size (unsigned int &offSize /*OUT*/, const remap_t &sidmap) const
607 if ((count == 0) || (sidmap.get_count () == 0))
608 return count.static_size;
610 unsigned int dataSize = 0;
611 for (unsigned int i = 0; i < count; i++)
612 if (sidmap[i] != CFF_UNDEF_CODE)
613 dataSize += length_at (i);
615 offSize = calcOffSize(dataSize);
616 return CFF1Index::calculate_serialized_size (offSize, sidmap.get_count (), dataSize);
620 struct cff1_top_dict_interp_env_t : num_interp_env_t
622 cff1_top_dict_interp_env_t ()
623 : num_interp_env_t(), prev_offset(0), last_offset(0) {}
625 unsigned int prev_offset;
626 unsigned int last_offset;
629 struct name_dict_values_t
631 enum name_dict_val_index_t
650 for (unsigned int i = 0; i < ValCount; i++)
651 values[i] = CFF_UNDEF_SID;
654 unsigned int& operator[] (unsigned int i)
655 { assert (i < ValCount); return values[i]; }
657 unsigned int operator[] (unsigned int i) const
658 { assert (i < ValCount); return values[i]; }
660 static enum name_dict_val_index_t name_op_to_index (op_code_t op)
663 default: // can't happen - just make some compiler happy
668 case OpCode_Copyright:
670 case OpCode_FullName:
672 case OpCode_FamilyName:
676 case OpCode_PostScript:
678 case OpCode_FontName:
680 case OpCode_BaseFontName:
685 unsigned int values[ValCount];
688 struct cff1_top_dict_val_t : op_str_t
690 unsigned int last_arg_offset;
693 struct cff1_top_dict_values_t : top_dict_values_t<cff1_top_dict_val_t>
697 top_dict_values_t<cff1_top_dict_val_t>::init ();
705 privateDictInfo.init ();
707 void fini () { top_dict_values_t<cff1_top_dict_val_t>::fini (); }
710 { return nameSIDs[name_dict_values_t::registry] != CFF_UNDEF_SID; }
712 name_dict_values_t nameSIDs;
713 unsigned int ros_supplement_offset;
714 unsigned int ros_supplement;
715 unsigned int cidCount;
717 unsigned int EncodingOffset;
718 unsigned int CharsetOffset;
719 unsigned int FDSelectOffset;
720 table_info_t privateDictInfo;
723 struct cff1_top_dict_opset_t : top_dict_opset_t<cff1_top_dict_val_t>
725 static void process_op (op_code_t op, cff1_top_dict_interp_env_t& env, cff1_top_dict_values_t& dictval)
727 cff1_top_dict_val_t val;
728 val.last_arg_offset = (env.last_offset-1) - dictval.opStart; /* offset to the last argument */
733 case OpCode_Copyright:
734 case OpCode_FullName:
735 case OpCode_FamilyName:
737 case OpCode_PostScript:
738 case OpCode_BaseFontName:
739 dictval.nameSIDs[name_dict_values_t::name_op_to_index (op)] = env.argStack.pop_uint ();
742 case OpCode_isFixedPitch:
743 case OpCode_ItalicAngle:
744 case OpCode_UnderlinePosition:
745 case OpCode_UnderlineThickness:
746 case OpCode_PaintType:
747 case OpCode_CharstringType:
748 case OpCode_UniqueID:
749 case OpCode_StrokeWidth:
750 case OpCode_SyntheticBase:
751 case OpCode_CIDFontVersion:
752 case OpCode_CIDFontRevision:
753 case OpCode_CIDFontType:
755 case OpCode_FontBBox:
757 case OpCode_BaseFontBlend:
761 case OpCode_CIDCount:
762 dictval.cidCount = env.argStack.pop_uint ();
767 dictval.ros_supplement = env.argStack.pop_uint ();
768 dictval.nameSIDs[name_dict_values_t::ordering] = env.argStack.pop_uint ();
769 dictval.nameSIDs[name_dict_values_t::registry] = env.argStack.pop_uint ();
773 case OpCode_Encoding:
774 dictval.EncodingOffset = env.argStack.pop_uint ();
776 if (unlikely (dictval.EncodingOffset == 0)) return;
780 dictval.CharsetOffset = env.argStack.pop_uint ();
782 if (unlikely (dictval.CharsetOffset == 0)) return;
785 case OpCode_FDSelect:
786 dictval.FDSelectOffset = env.argStack.pop_uint ();
791 dictval.privateDictInfo.offset = env.argStack.pop_uint ();
792 dictval.privateDictInfo.size = env.argStack.pop_uint ();
797 env.last_offset = env.str_ref.offset;
798 top_dict_opset_t<cff1_top_dict_val_t>::process_op (op, env, dictval);
799 /* Record this operand below if stack is empty, otherwise done */
800 if (!env.argStack.is_empty ()) return;
804 if (unlikely (env.in_error ())) return;
806 dictval.add_op (op, env.str_ref, val);
810 struct cff1_font_dict_values_t : dict_values_t<op_str_t>
814 dict_values_t<op_str_t>::init ();
815 privateDictInfo.init ();
816 fontName = CFF_UNDEF_SID;
818 void fini () { dict_values_t<op_str_t>::fini (); }
820 table_info_t privateDictInfo;
821 unsigned int fontName;
824 struct cff1_font_dict_opset_t : dict_opset_t
826 static void process_op (op_code_t op, num_interp_env_t& env, cff1_font_dict_values_t& dictval)
829 case OpCode_FontName:
830 dictval.fontName = env.argStack.pop_uint ();
833 case OpCode_FontMatrix:
834 case OpCode_PaintType:
838 dictval.privateDictInfo.offset = env.argStack.pop_uint ();
839 dictval.privateDictInfo.size = env.argStack.pop_uint ();
844 dict_opset_t::process_op (op, env);
845 if (!env.argStack.is_empty ()) return;
849 if (unlikely (env.in_error ())) return;
851 dictval.add_op (op, env.str_ref);
855 template <typename VAL>
856 struct cff1_private_dict_values_base_t : dict_values_t<VAL>
860 dict_values_t<VAL>::init ();
862 localSubrs = &Null(CFF1Subrs);
864 void fini () { dict_values_t<VAL>::fini (); }
866 unsigned int calculate_serialized_size () const
868 unsigned int size = 0;
869 for (unsigned int i = 0; i < dict_values_t<VAL>::get_count; i++)
870 if (dict_values_t<VAL>::get_value (i).op == OpCode_Subrs)
871 size += OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Subrs);
873 size += dict_values_t<VAL>::get_value (i).str.length;
877 unsigned int subrsOffset;
878 const CFF1Subrs *localSubrs;
881 typedef cff1_private_dict_values_base_t<op_str_t> cff1_private_dict_values_subset_t;
882 typedef cff1_private_dict_values_base_t<num_dict_val_t> cff1_private_dict_values_t;
884 struct cff1_private_dict_opset_t : dict_opset_t
886 static void process_op (op_code_t op, num_interp_env_t& env, cff1_private_dict_values_t& dictval)
892 case OpCode_BlueValues:
893 case OpCode_OtherBlues:
894 case OpCode_FamilyBlues:
895 case OpCode_FamilyOtherBlues:
896 case OpCode_StemSnapH:
897 case OpCode_StemSnapV:
902 case OpCode_BlueScale:
903 case OpCode_BlueShift:
904 case OpCode_BlueFuzz:
905 case OpCode_ForceBold:
906 case OpCode_LanguageGroup:
907 case OpCode_ExpansionFactor:
908 case OpCode_initialRandomSeed:
909 case OpCode_defaultWidthX:
910 case OpCode_nominalWidthX:
911 val.single_val = env.argStack.pop_num ();
915 dictval.subrsOffset = env.argStack.pop_uint ();
920 dict_opset_t::process_op (op, env);
921 if (!env.argStack.is_empty ()) return;
925 if (unlikely (env.in_error ())) return;
927 dictval.add_op (op, env.str_ref, val);
931 struct cff1_private_dict_opset_subset : dict_opset_t
933 static void process_op (op_code_t op, num_interp_env_t& env, cff1_private_dict_values_subset_t& dictval)
936 case OpCode_BlueValues:
937 case OpCode_OtherBlues:
938 case OpCode_FamilyBlues:
939 case OpCode_FamilyOtherBlues:
940 case OpCode_StemSnapH:
941 case OpCode_StemSnapV:
944 case OpCode_BlueScale:
945 case OpCode_BlueShift:
946 case OpCode_BlueFuzz:
947 case OpCode_ForceBold:
948 case OpCode_LanguageGroup:
949 case OpCode_ExpansionFactor:
950 case OpCode_initialRandomSeed:
951 case OpCode_defaultWidthX:
952 case OpCode_nominalWidthX:
957 dictval.subrsOffset = env.argStack.pop_uint ();
962 dict_opset_t::process_op (op, env);
963 if (!env.argStack.is_empty ()) return;
967 if (unlikely (env.in_error ())) return;
969 dictval.add_op (op, env.str_ref);
973 typedef dict_interpreter_t<cff1_top_dict_opset_t, cff1_top_dict_values_t, cff1_top_dict_interp_env_t> cff1_top_dict_interpreter_t;
974 typedef dict_interpreter_t<cff1_font_dict_opset_t, cff1_font_dict_values_t> cff1_font_dict_interpreter_t;
976 typedef CFF1Index CFF1NameIndex;
977 typedef CFF1IndexOf<TopDict> CFF1TopDictIndex;
979 } /* namespace CFF */
987 static constexpr hb_tag_t tableTag = HB_OT_TAG_cff1;
989 bool sanitize (hb_sanitize_context_t *c) const
991 TRACE_SANITIZE (this);
992 return_trace (c->check_struct (this) &&
993 likely (version.major == 1));
996 template <typename PRIVOPSET, typename PRIVDICTVAL>
997 struct accelerator_templ_t
999 void init (hb_face_t *face)
1003 privateDicts.init ();
1005 this->blob = sc.reference_table<cff1> (face);
1007 /* setup for run-time santization */
1008 sc.init (this->blob);
1009 sc.start_processing ();
1011 const OT::cff1 *cff = this->blob->template as<OT::cff1> ();
1013 if (cff == &Null(OT::cff1))
1014 { fini (); return; }
1016 nameIndex = &cff->nameIndex (cff);
1017 if ((nameIndex == &Null (CFF1NameIndex)) || !nameIndex->sanitize (&sc))
1018 { fini (); return; }
1020 topDictIndex = &StructAtOffset<CFF1TopDictIndex> (nameIndex, nameIndex->get_size ());
1021 if ((topDictIndex == &Null (CFF1TopDictIndex)) || !topDictIndex->sanitize (&sc) || (topDictIndex->count == 0))
1022 { fini (); return; }
1024 { /* parse top dict */
1025 const byte_str_t topDictStr = (*topDictIndex)[0];
1026 if (unlikely (!topDictStr.sanitize (&sc))) { fini (); return; }
1027 cff1_top_dict_interpreter_t top_interp;
1028 top_interp.env.init (topDictStr);
1030 if (unlikely (!top_interp.interpret (topDict))) { fini (); return; }
1033 if (is_predef_charset ())
1034 charset = &Null(Charset);
1037 charset = &StructAtOffsetOrNull<Charset> (cff, topDict.CharsetOffset);
1038 if (unlikely ((charset == &Null (Charset)) || !charset->sanitize (&sc))) { fini (); return; }
1044 fdArray = &StructAtOffsetOrNull<CFF1FDArray> (cff, topDict.FDArrayOffset);
1045 fdSelect = &StructAtOffsetOrNull<CFF1FDSelect> (cff, topDict.FDSelectOffset);
1046 if (unlikely ((fdArray == &Null(CFF1FDArray)) || !fdArray->sanitize (&sc) ||
1047 (fdSelect == &Null(CFF1FDSelect)) || !fdSelect->sanitize (&sc, fdArray->count)))
1048 { fini (); return; }
1050 fdCount = fdArray->count;
1054 fdArray = &Null(CFF1FDArray);
1055 fdSelect = &Null(CFF1FDSelect);
1058 stringIndex = &StructAtOffset<CFF1StringIndex> (topDictIndex, topDictIndex->get_size ());
1059 if ((stringIndex == &Null (CFF1StringIndex)) || !stringIndex->sanitize (&sc))
1060 { fini (); return; }
1062 globalSubrs = &StructAtOffset<CFF1Subrs> (stringIndex, stringIndex->get_size ());
1063 if ((globalSubrs != &Null (CFF1Subrs)) && !globalSubrs->sanitize (&sc))
1064 { fini (); return; }
1066 charStrings = &StructAtOffsetOrNull<CFF1CharStrings> (cff, topDict.charStringsOffset);
1068 if ((charStrings == &Null(CFF1CharStrings)) || unlikely (!charStrings->sanitize (&sc)))
1069 { fini (); return; }
1071 num_glyphs = charStrings->count;
1072 if (num_glyphs != sc.get_num_glyphs ())
1073 { fini (); return; }
1075 privateDicts.resize (fdCount);
1076 for (unsigned int i = 0; i < fdCount; i++)
1077 privateDicts[i].init ();
1079 // parse CID font dicts and gather private dicts
1082 for (unsigned int i = 0; i < fdCount; i++)
1084 byte_str_t fontDictStr = (*fdArray)[i];
1085 if (unlikely (!fontDictStr.sanitize (&sc))) { fini (); return; }
1086 cff1_font_dict_values_t *font;
1087 cff1_font_dict_interpreter_t font_interp;
1088 font_interp.env.init (fontDictStr);
1089 font = fontDicts.push ();
1090 if (unlikely (font == &Crap(cff1_font_dict_values_t))) { fini (); return; }
1092 if (unlikely (!font_interp.interpret (*font))) { fini (); return; }
1093 PRIVDICTVAL *priv = &privateDicts[i];
1094 const byte_str_t privDictStr (StructAtOffset<UnsizedByteStr> (cff, font->privateDictInfo.offset), font->privateDictInfo.size);
1095 if (unlikely (!privDictStr.sanitize (&sc))) { fini (); return; }
1096 dict_interpreter_t<PRIVOPSET, PRIVDICTVAL> priv_interp;
1097 priv_interp.env.init (privDictStr);
1099 if (unlikely (!priv_interp.interpret (*priv))) { fini (); return; }
1101 priv->localSubrs = &StructAtOffsetOrNull<CFF1Subrs> (&privDictStr, priv->subrsOffset);
1102 if (priv->localSubrs != &Null(CFF1Subrs) &&
1103 unlikely (!priv->localSubrs->sanitize (&sc)))
1104 { fini (); return; }
1109 cff1_top_dict_values_t *font = &topDict;
1110 PRIVDICTVAL *priv = &privateDicts[0];
1112 const byte_str_t privDictStr (StructAtOffset<UnsizedByteStr> (cff, font->privateDictInfo.offset), font->privateDictInfo.size);
1113 if (unlikely (!privDictStr.sanitize (&sc))) { fini (); return; }
1114 dict_interpreter_t<PRIVOPSET, PRIVDICTVAL> priv_interp;
1115 priv_interp.env.init (privDictStr);
1117 if (unlikely (!priv_interp.interpret (*priv))) { fini (); return; }
1119 priv->localSubrs = &StructAtOffsetOrNull<CFF1Subrs> (&privDictStr, priv->subrsOffset);
1120 if (priv->localSubrs != &Null(CFF1Subrs) &&
1121 unlikely (!priv->localSubrs->sanitize (&sc)))
1122 { fini (); return; }
1128 sc.end_processing ();
1130 fontDicts.fini_deep ();
1131 privateDicts.fini_deep ();
1132 hb_blob_destroy (blob);
1136 bool is_valid () const { return blob != nullptr; }
1137 bool is_CID () const { return topDict.is_CID (); }
1139 bool is_predef_charset () const { return topDict.CharsetOffset <= ExpertSubsetCharset; }
1141 unsigned int std_code_to_glyph (hb_codepoint_t code) const
1143 hb_codepoint_t sid = lookup_standard_encoding_for_sid (code);
1144 if (unlikely (sid == CFF_UNDEF_SID))
1147 if (charset != &Null(Charset))
1148 return charset->get_glyph (sid, num_glyphs);
1149 else if ((topDict.CharsetOffset == ISOAdobeCharset)
1150 && (code <= 228 /*zcaron*/)) return sid;
1156 hb_sanitize_context_t sc;
1159 const Charset *charset;
1160 const CFF1NameIndex *nameIndex;
1161 const CFF1TopDictIndex *topDictIndex;
1162 const CFF1StringIndex *stringIndex;
1163 const CFF1Subrs *globalSubrs;
1164 const CFF1CharStrings *charStrings;
1165 const CFF1FDArray *fdArray;
1166 const CFF1FDSelect *fdSelect;
1167 unsigned int fdCount;
1169 cff1_top_dict_values_t topDict;
1170 hb_vector_t<cff1_font_dict_values_t> fontDicts;
1171 hb_vector_t<PRIVDICTVAL> privateDicts;
1173 unsigned int num_glyphs;
1176 struct accelerator_t : accelerator_templ_t<cff1_private_dict_opset_t, cff1_private_dict_values_t>
1178 HB_INTERNAL bool get_extents (hb_codepoint_t glyph, hb_glyph_extents_t *extents) const;
1179 HB_INTERNAL bool get_seac_components (hb_codepoint_t glyph, hb_codepoint_t *base, hb_codepoint_t *accent) const;
1182 struct accelerator_subset_t : accelerator_templ_t<cff1_private_dict_opset_subset, cff1_private_dict_values_subset_t>
1184 void init (hb_face_t *face)
1187 if (blob == nullptr) return;
1189 const OT::cff1 *cff = this->blob->as<OT::cff1> ();
1190 encoding = &Null(Encoding);
1193 if (unlikely (charset == &Null(Charset))) { fini (); return; }
1197 if (!is_predef_encoding ())
1199 encoding = &StructAtOffsetOrNull<Encoding> (cff, topDict.EncodingOffset);
1200 if (unlikely ((encoding == &Null (Encoding)) || !encoding->sanitize (&sc))) { fini (); return; }
1205 bool is_predef_encoding () const { return topDict.EncodingOffset <= ExpertEncoding; }
1207 hb_codepoint_t glyph_to_code (hb_codepoint_t glyph) const
1209 if (encoding != &Null(Encoding))
1210 return encoding->get_code (glyph);
1213 hb_codepoint_t sid = glyph_to_sid (glyph);
1214 if (sid == 0) return 0;
1215 hb_codepoint_t code = 0;
1216 switch (topDict.EncodingOffset)
1218 case StandardEncoding:
1219 code = lookup_standard_encoding_for_code (sid);
1221 case ExpertEncoding:
1222 code = lookup_expert_encoding_for_code (sid);
1231 hb_codepoint_t glyph_to_sid (hb_codepoint_t glyph) const
1233 if (charset != &Null(Charset))
1234 return charset->get_sid (glyph);
1237 hb_codepoint_t sid = 0;
1238 switch (topDict.CharsetOffset)
1240 case ISOAdobeCharset:
1241 if (glyph <= 228 /*zcaron*/) sid = glyph;
1244 sid = lookup_expert_charset_for_sid (glyph);
1246 case ExpertSubsetCharset:
1247 sid = lookup_expert_subset_charset_for_sid (glyph);
1256 const Encoding *encoding;
1259 typedef accelerator_templ_t<cff1_private_dict_opset_subset, cff1_private_dict_values_subset_t> SUPER;
1262 bool subset (hb_subset_plan_t *plan) const
1264 hb_blob_t *cff_prime = nullptr;
1266 bool success = true;
1267 if (hb_subset_cff1 (plan, &cff_prime)) {
1268 success = success && plan->add_table (HB_OT_TAG_cff1, cff_prime);
1269 hb_blob_t *head_blob = hb_sanitize_context_t().reference_table<head> (plan->source);
1270 success = success && head_blob && plan->add_table (HB_OT_TAG_head, head_blob);
1271 hb_blob_destroy (head_blob);
1275 hb_blob_destroy (cff_prime);
1281 HB_INTERNAL static hb_codepoint_t lookup_standard_encoding_for_code (hb_codepoint_t sid);
1282 HB_INTERNAL static hb_codepoint_t lookup_expert_encoding_for_code (hb_codepoint_t sid);
1283 HB_INTERNAL static hb_codepoint_t lookup_expert_charset_for_sid (hb_codepoint_t glyph);
1284 HB_INTERNAL static hb_codepoint_t lookup_expert_subset_charset_for_sid (hb_codepoint_t glyph);
1285 HB_INTERNAL static hb_codepoint_t lookup_standard_encoding_for_sid (hb_codepoint_t code);
1288 FixedVersion<HBUINT8> version; /* Version of CFF table. set to 0x0100u */
1289 OffsetTo<CFF1NameIndex, HBUINT8> nameIndex; /* headerSize = Offset to Name INDEX. */
1290 HBUINT8 offSize; /* offset size (unused?) */
1293 DEFINE_SIZE_STATIC (4);
1296 struct cff1_accelerator_t : cff1::accelerator_t {};
1297 } /* namespace OT */
1299 #endif /* HB_OT_CFF1_TABLE_HH */