blob: af24bb9986f8517b409d0a11050c9e85c51ecf29 [file] [log] [blame]
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -07001/*
Michiharu Ariza0dfa5842018-11-12 08:47:07 -08002 * Copyright © 2018 Adobe Inc.
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -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
27#ifndef HB_OT_CFF2_TABLE_HH
28#define HB_OT_CFF2_TABLE_HH
29
Michiharu Ariza8af96902018-08-29 13:26:17 -070030#include "hb-ot-cff-common.hh"
Behdad Esfahbod5f399132023-06-23 11:22:01 -060031#include "hb-subset-cff-common.hh"
Ebrahim Byagowi9fe0dc32020-02-26 17:40:44 +033032#include "hb-draw.hh"
Matthias Clasen3e39dd42022-12-16 23:46:15 -050033#include "hb-paint.hh"
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -070034
35namespace CFF {
36
37/*
38 * CFF2 -- Compact Font Format (CFF) Version 2
39 * https://docs.microsoft.com/en-us/typography/opentype/spec/cff2
40 */
Khaled Hosnyb6516f32023-05-08 15:31:24 +030041#define HB_OT_TAG_CFF2 HB_TAG('C','F','F','2')
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -070042
Michiharu Arizaf57d6bc2018-08-16 08:03:46 -070043typedef CFFIndex<HBUINT32> CFF2Index;
Michiharu Ariza64c54122018-08-10 11:07:07 -070044
45typedef CFF2Index CFF2CharStrings;
Michiharu Ariza633ce882018-08-15 12:00:19 -070046typedef Subrs<HBUINT32> CFF2Subrs;
Michiharu Ariza64c54122018-08-10 11:07:07 -070047
Michiharu Ariza5561b812018-08-06 10:04:53 -070048typedef FDSelect3_4<HBUINT32, HBUINT16> FDSelect4;
49typedef FDSelect3_4_Range<HBUINT32, HBUINT16> FDSelect4_Range;
50
51struct CFF2FDSelect
52{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033053 bool serialize (hb_serialize_context_t *c, const CFF2FDSelect &src, unsigned int num_glyphs)
Michiharu Ariza5561b812018-08-06 10:04:53 -070054 {
55 TRACE_SERIALIZE (this);
56 unsigned int size = src.get_size (num_glyphs);
57 CFF2FDSelect *dest = c->allocate_size<CFF2FDSelect> (size);
Ebrahim Byagowi2be859d2020-04-20 23:48:23 +043058 if (unlikely (!dest)) return_trace (false);
Behdad Esfahbod59c45f62022-11-22 12:54:50 -070059 hb_memcpy (dest, &src, size);
Michiharu Ariza5561b812018-08-06 10:04:53 -070060 return_trace (true);
61 }
62
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033063 unsigned int get_size (unsigned int num_glyphs) const
Michiharu Ariza5561b812018-08-06 10:04:53 -070064 {
Ebrahim Byagowi9b853f72019-06-09 11:49:25 +043065 switch (format)
66 {
67 case 0: return format.static_size + u.format0.get_size (num_glyphs);
68 case 3: return format.static_size + u.format3.get_size ();
69 case 4: return format.static_size + u.format4.get_size ();
70 default:return 0;
71 }
Michiharu Ariza5561b812018-08-06 10:04:53 -070072 }
73
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +033074 hb_codepoint_t get_fd (hb_codepoint_t glyph) const
Michiharu Ariza5561b812018-08-06 10:04:53 -070075 {
Ebrahim Byagowi9b853f72019-06-09 11:49:25 +043076 if (this == &Null (CFF2FDSelect))
Michiharu Ariza633ce882018-08-15 12:00:19 -070077 return 0;
Ebrahim Byagowi9b853f72019-06-09 11:49:25 +043078
79 switch (format)
80 {
81 case 0: return u.format0.get_fd (glyph);
82 case 3: return u.format3.get_fd (glyph);
83 case 4: return u.format4.get_fd (glyph);
84 default:return 0;
85 }
Michiharu Ariza5561b812018-08-06 10:04:53 -070086 }
87
Ebrahim Byagowi9b853f72019-06-09 11:49:25 +043088 bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount) const
89 {
90 TRACE_SANITIZE (this);
91 if (unlikely (!c->check_struct (this)))
92 return_trace (false);
Michiharu Ariza5561b812018-08-06 10:04:53 -070093
Ebrahim Byagowi9b853f72019-06-09 11:49:25 +043094 switch (format)
95 {
96 case 0: return_trace (u.format0.sanitize (c, fdcount));
97 case 3: return_trace (u.format3.sanitize (c, fdcount));
98 case 4: return_trace (u.format4.sanitize (c, fdcount));
99 default:return_trace (false);
100 }
101 }
102
103 HBUINT8 format;
104 union {
105 FDSelect0 format0;
106 FDSelect3 format3;
107 FDSelect4 format4;
108 } u;
109 public:
Michiharu Ariza5561b812018-08-06 10:04:53 -0700110 DEFINE_SIZE_MIN (2);
111};
112
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700113struct CFF2VariationStore
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700114{
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330115 bool sanitize (hb_sanitize_context_t *c) const
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700116 {
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700117 TRACE_SANITIZE (this);
Michiharu Arizac31092a2018-12-05 17:04:55 -0800118 return_trace (likely (c->check_struct (this)) && c->check_range (&varStore, size) && varStore.sanitize (c));
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700119 }
120
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330121 bool serialize (hb_serialize_context_t *c, const CFF2VariationStore *varStore)
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700122 {
Michiharu Ariza693ae802018-08-02 11:22:42 -0700123 TRACE_SERIALIZE (this);
Michiharu Ariza16f4dc92018-08-01 16:06:52 -0700124 unsigned int size_ = varStore->get_size ();
125 CFF2VariationStore *dest = c->allocate_size<CFF2VariationStore> (size_);
Ebrahim Byagowi2be859d2020-04-20 23:48:23 +0430126 if (unlikely (!dest)) return_trace (false);
Behdad Esfahbod59c45f62022-11-22 12:54:50 -0700127 hb_memcpy (dest, varStore, size_);
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700128 return_trace (true);
129 }
130
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330131 unsigned int get_size () const { return HBUINT16::static_size + size; }
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700132
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500133 HBUINT16 size;
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700134 VariationStore varStore;
135
136 DEFINE_SIZE_MIN (2 + VariationStore::min_size);
137};
138
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800139struct cff2_top_dict_values_t : top_dict_values_t<>
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700140{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330141 void init ()
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700142 {
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800143 top_dict_values_t<>::init ();
Michiharu Ariza64c54122018-08-10 11:07:07 -0700144 vstoreOffset = 0;
145 FDSelectOffset = 0;
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700146 }
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800147 void fini () { top_dict_values_t<>::fini (); }
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700148
Michiharu Ariza64c54122018-08-10 11:07:07 -0700149 unsigned int vstoreOffset;
150 unsigned int FDSelectOffset;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700151};
152
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800153struct cff2_top_dict_opset_t : top_dict_opset_t<>
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700154{
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800155 static void process_op (op_code_t op, num_interp_env_t& env, cff2_top_dict_values_t& dictval)
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700156 {
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700157 switch (op) {
Michiharu Ariza64c54122018-08-10 11:07:07 -0700158 case OpCode_FontMatrix:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500159 {
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800160 dict_val_t val;
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500161 val.init ();
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800162 dictval.add_op (op, env.str_ref);
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500163 env.clear_args ();
164 }
165 break;
Michiharu Ariza64c54122018-08-10 11:07:07 -0700166
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700167 case OpCode_vstore:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500168 dictval.vstoreOffset = env.argStack.pop_uint ();
169 env.clear_args ();
170 break;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700171 case OpCode_FDSelect:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500172 dictval.FDSelectOffset = env.argStack.pop_uint ();
173 env.clear_args ();
174 break;
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500175
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700176 default:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500177 SUPER::process_op (op, env, dictval);
178 /* Record this operand below if stack is empty, otherwise done */
179 if (!env.argStack.is_empty ()) return;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700180 }
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700181
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700182 if (unlikely (env.in_error ())) return;
183
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800184 dictval.add_op (op, env.str_ref);
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700185 }
Michiharu Arizafcf17782018-08-31 16:28:47 -0700186
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800187 typedef top_dict_opset_t<> SUPER;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700188};
189
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800190struct cff2_font_dict_values_t : dict_values_t<op_str_t>
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700191{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330192 void init ()
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700193 {
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800194 dict_values_t<op_str_t>::init ();
ariza0b290532020-03-04 22:31:21 -0800195 privateDictInfo.init ();
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700196 }
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800197 void fini () { dict_values_t<op_str_t>::fini (); }
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700198
ariza0b290532020-03-04 22:31:21 -0800199 table_info_t privateDictInfo;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700200};
201
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800202struct cff2_font_dict_opset_t : dict_opset_t
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700203{
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800204 static void process_op (op_code_t op, num_interp_env_t& env, cff2_font_dict_values_t& dictval)
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700205 {
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700206 switch (op) {
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700207 case OpCode_Private:
ariza0b290532020-03-04 22:31:21 -0800208 dictval.privateDictInfo.offset = env.argStack.pop_uint ();
209 dictval.privateDictInfo.size = env.argStack.pop_uint ();
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500210 env.clear_args ();
211 break;
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500212
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700213 default:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500214 SUPER::process_op (op, env);
215 if (!env.argStack.is_empty ())
216 return;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700217 }
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700218
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700219 if (unlikely (env.in_error ())) return;
220
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800221 dictval.add_op (op, env.str_ref);
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700222 }
Michiharu Arizafcf17782018-08-31 16:28:47 -0700223
224 private:
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800225 typedef dict_opset_t SUPER;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700226};
227
Michiharu Ariza00970dd2018-08-03 14:35:09 -0700228template <typename VAL>
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800229struct cff2_private_dict_values_base_t : dict_values_t<VAL>
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700230{
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330231 void init ()
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700232 {
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800233 dict_values_t<VAL>::init ();
Michiharu Ariza64c54122018-08-10 11:07:07 -0700234 subrsOffset = 0;
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430235 localSubrs = &Null (CFF2Subrs);
Michiharu Arizab51418f2018-10-08 15:05:36 -0700236 ivs = 0;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700237 }
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800238 void fini () { dict_values_t<VAL>::fini (); }
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700239
Michiharu Ariza64c54122018-08-10 11:07:07 -0700240 unsigned int subrsOffset;
241 const CFF2Subrs *localSubrs;
Michiharu Arizab51418f2018-10-08 15:05:36 -0700242 unsigned int ivs;
Michiharu Ariza76f73972018-08-02 16:28:10 -0700243};
244
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800245typedef cff2_private_dict_values_base_t<op_str_t> cff2_private_dict_values_subset_t;
246typedef cff2_private_dict_values_base_t<num_dict_val_t> cff2_private_dict_values_t;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700247
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800248struct cff2_priv_dict_interp_env_t : num_interp_env_t
Michiharu Arizab51418f2018-10-08 15:05:36 -0700249{
Behdad Esfahbodbff78e62022-05-10 16:33:37 -0600250 cff2_priv_dict_interp_env_t (const hb_ubytes_t &str) :
251 num_interp_env_t (str) {}
Michiharu Arizab51418f2018-10-08 15:05:36 -0700252
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330253 void process_vsindex ()
Michiharu Arizab51418f2018-10-08 15:05:36 -0700254 {
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700255 if (likely (!seen_vsindex))
Michiharu Arizab51418f2018-10-08 15:05:36 -0700256 {
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700257 set_ivs (argStack.pop_uint ());
Michiharu Arizab51418f2018-10-08 15:05:36 -0700258 }
259 seen_vsindex = true;
260 }
261
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330262 unsigned int get_ivs () const { return ivs; }
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330263 void set_ivs (unsigned int ivs_) { ivs = ivs_; }
Michiharu Arizab51418f2018-10-08 15:05:36 -0700264
265 protected:
Behdad Esfahbodbff78e62022-05-10 16:33:37 -0600266 unsigned int ivs = 0;
267 bool seen_vsindex = false;
Michiharu Arizab51418f2018-10-08 15:05:36 -0700268};
269
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800270struct cff2_private_dict_opset_t : dict_opset_t
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700271{
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800272 static void process_op (op_code_t op, cff2_priv_dict_interp_env_t& env, cff2_private_dict_values_t& dictval)
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700273 {
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800274 num_dict_val_t val;
Michiharu Ariza00970dd2018-08-03 14:35:09 -0700275 val.init ();
276
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700277 switch (op) {
Michiharu Ariza64c54122018-08-10 11:07:07 -0700278 case OpCode_StdHW:
279 case OpCode_StdVW:
280 case OpCode_BlueScale:
281 case OpCode_BlueShift:
282 case OpCode_BlueFuzz:
283 case OpCode_ExpansionFactor:
284 case OpCode_LanguageGroup:
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700285 case OpCode_BlueValues:
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700286 case OpCode_OtherBlues:
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700287 case OpCode_FamilyBlues:
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700288 case OpCode_FamilyOtherBlues:
Michiharu Ariza00970dd2018-08-03 14:35:09 -0700289 case OpCode_StemSnapH:
290 case OpCode_StemSnapV:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500291 env.clear_args ();
292 break;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700293 case OpCode_Subrs:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500294 dictval.subrsOffset = env.argStack.pop_uint ();
295 env.clear_args ();
296 break;
Michiharu Arizacef75ea2018-08-17 13:13:18 -0700297 case OpCode_vsindexdict:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500298 env.process_vsindex ();
299 dictval.ivs = env.get_ivs ();
300 env.clear_args ();
301 break;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700302 case OpCode_blenddict:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500303 break;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700304
305 default:
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800306 dict_opset_t::process_op (op, env);
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500307 if (!env.argStack.is_empty ()) return;
308 break;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700309 }
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700310
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700311 if (unlikely (env.in_error ())) return;
312
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800313 dictval.add_op (op, env.str_ref, val);
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700314 }
315};
316
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800317struct cff2_private_dict_opset_subset_t : dict_opset_t
Michiharu Ariza76f73972018-08-02 16:28:10 -0700318{
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800319 static void process_op (op_code_t op, cff2_priv_dict_interp_env_t& env, cff2_private_dict_values_subset_t& dictval)
Michiharu Ariza76f73972018-08-02 16:28:10 -0700320 {
321 switch (op) {
322 case OpCode_BlueValues:
323 case OpCode_OtherBlues:
324 case OpCode_FamilyBlues:
325 case OpCode_FamilyOtherBlues:
326 case OpCode_StdHW:
327 case OpCode_StdVW:
328 case OpCode_BlueScale:
329 case OpCode_BlueShift:
330 case OpCode_BlueFuzz:
331 case OpCode_StemSnapH:
332 case OpCode_StemSnapV:
333 case OpCode_LanguageGroup:
334 case OpCode_ExpansionFactor:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500335 env.clear_args ();
336 break;
Michiharu Ariza76f73972018-08-02 16:28:10 -0700337
Michiharu Ariza633ce882018-08-15 12:00:19 -0700338 case OpCode_blenddict:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500339 env.clear_args ();
340 return;
Michiharu Ariza00970dd2018-08-03 14:35:09 -0700341
Michiharu Ariza76f73972018-08-02 16:28:10 -0700342 case OpCode_Subrs:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500343 dictval.subrsOffset = env.argStack.pop_uint ();
344 env.clear_args ();
345 break;
Michiharu Ariza76f73972018-08-02 16:28:10 -0700346
347 default:
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500348 SUPER::process_op (op, env);
349 if (!env.argStack.is_empty ()) return;
350 break;
Michiharu Ariza76f73972018-08-02 16:28:10 -0700351 }
352
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700353 if (unlikely (env.in_error ())) return;
354
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800355 dictval.add_op (op, env.str_ref);
Michiharu Ariza76f73972018-08-02 16:28:10 -0700356 }
Michiharu Arizafcf17782018-08-31 16:28:47 -0700357
358 private:
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800359 typedef dict_opset_t SUPER;
Michiharu Ariza76f73972018-08-02 16:28:10 -0700360};
361
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800362typedef dict_interpreter_t<cff2_top_dict_opset_t, cff2_top_dict_values_t> cff2_top_dict_interpreter_t;
363typedef dict_interpreter_t<cff2_font_dict_opset_t, cff2_font_dict_values_t> cff2_font_dict_interpreter_t;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700364
arizac05458e2020-03-02 16:51:19 -0800365struct CFF2FDArray : FDArray<HBUINT32>
366{
367 /* FDArray::serialize does not compile without this partial specialization */
368 template <typename ITER, typename OP_SERIALIZER>
369 bool serialize (hb_serialize_context_t *c, ITER it, OP_SERIALIZER& opszr)
370 { return FDArray<HBUINT32>::serialize<cff2_font_dict_values_t, table_info_t> (c, it, opszr); }
371};
372
Behdad Esfahbodd25a2f12018-12-23 20:19:52 -0500373} /* namespace CFF */
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700374
375namespace OT {
376
377using namespace CFF;
378
379struct cff2
380{
Khaled Hosnyb6516f32023-05-08 15:31:24 +0300381 static constexpr hb_tag_t tableTag = HB_OT_TAG_CFF2;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700382
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330383 bool sanitize (hb_sanitize_context_t *c) const
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700384 {
385 TRACE_SANITIZE (this);
386 return_trace (c->check_struct (this) &&
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500387 likely (version.major == 2));
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700388 }
389
Michiharu Arizafcf17782018-08-31 16:28:47 -0700390 template <typename PRIVOPSET, typename PRIVDICTVAL>
Michiharu Ariza76f73972018-08-02 16:28:10 -0700391 struct accelerator_templ_t
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700392 {
Behdad Esfahbod557653a2023-07-09 10:18:04 -0600393 static constexpr hb_tag_t tableTag = cff2::tableTag;
394
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700395 accelerator_templ_t (hb_face_t *face)
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700396 {
Behdad Esfahbodc4b29502023-06-23 11:55:39 -0600397 if (!face) return;
398
Michiharu Ariza64c54122018-08-10 11:07:07 -0700399 topDict.init ();
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700400 fontDicts.init ();
401 privateDicts.init ();
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500402
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700403 this->blob = sc.reference_table<cff2> (face);
404
405 /* setup for run-time santization */
406 sc.init (this->blob);
407 sc.start_processing ();
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500408
Michiharu Ariza76f73972018-08-02 16:28:10 -0700409 const OT::cff2 *cff2 = this->blob->template as<OT::cff2> ();
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700410
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430411 if (cff2 == &Null (OT::cff2))
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700412 goto fail;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700413
414 { /* parse top dict */
Behdad Esfahbod8aa54aa2022-05-09 16:09:56 -0600415 hb_ubytes_t topDictStr = (cff2 + cff2->topDict).as_ubytes (cff2->topDictSize);
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700416 if (unlikely (!topDictStr.sanitize (&sc))) goto fail;
Behdad Esfahbodbff78e62022-05-10 16:33:37 -0600417 num_interp_env_t env (topDictStr);
418 cff2_top_dict_interpreter_t top_interp (env);
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500419 topDict.init ();
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700420 if (unlikely (!top_interp.interpret (topDict))) goto fail;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700421 }
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500422
Michiharu Ariza64c54122018-08-10 11:07:07 -0700423 globalSubrs = &StructAtOffset<CFF2Subrs> (cff2, cff2->topDict + cff2->topDictSize);
424 varStore = &StructAtOffsetOrNull<CFF2VariationStore> (cff2, topDict.vstoreOffset);
425 charStrings = &StructAtOffsetOrNull<CFF2CharStrings> (cff2, topDict.charStringsOffset);
426 fdArray = &StructAtOffsetOrNull<CFF2FDArray> (cff2, topDict.FDArrayOffset);
427 fdSelect = &StructAtOffsetOrNull<CFF2FDSelect> (cff2, topDict.FDSelectOffset);
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500428
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430429 if (((varStore != &Null (CFF2VariationStore)) && unlikely (!varStore->sanitize (&sc))) ||
430 (charStrings == &Null (CFF2CharStrings)) || unlikely (!charStrings->sanitize (&sc)) ||
431 (globalSubrs == &Null (CFF2Subrs)) || unlikely (!globalSubrs->sanitize (&sc)) ||
432 (fdArray == &Null (CFF2FDArray)) || unlikely (!fdArray->sanitize (&sc)) ||
433 (((fdSelect != &Null (CFF2FDSelect)) && unlikely (!fdSelect->sanitize (&sc, fdArray->count)))))
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700434 goto fail;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700435
436 num_glyphs = charStrings->count;
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700437 if (num_glyphs != sc.get_num_glyphs ())
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700438 goto fail;
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700439
Michiharu Arizacef75ea2018-08-17 13:13:18 -0700440 fdCount = fdArray->count;
Garret Rieger7f358a52020-07-30 13:57:30 -0700441 if (!privateDicts.resize (fdCount))
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700442 goto fail;
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700443
Michiharu Ariza633ce882018-08-15 12:00:19 -0700444 /* parse font dicts and gather private dicts */
Michiharu Arizacef75ea2018-08-17 13:13:18 -0700445 for (unsigned int i = 0; i < fdCount; i++)
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700446 {
Behdad Esfahbod8aa54aa2022-05-09 16:09:56 -0600447 const hb_ubytes_t fontDictStr = (*fdArray)[i];
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700448 if (unlikely (!fontDictStr.sanitize (&sc))) goto fail;
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800449 cff2_font_dict_values_t *font;
Behdad Esfahbodbff78e62022-05-10 16:33:37 -0600450 num_interp_env_t env (fontDictStr);
451 cff2_font_dict_interpreter_t font_interp (env);
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500452 font = fontDicts.push ();
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700453 if (unlikely (font == &Crap (cff2_font_dict_values_t))) goto fail;
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500454 font->init ();
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700455 if (unlikely (!font_interp.interpret (*font))) goto fail;
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700456
Behdad Esfahbod8aa54aa2022-05-09 16:09:56 -0600457 const hb_ubytes_t privDictStr = StructAtOffsetOrNull<UnsizedByteStr> (cff2, font->privateDictInfo.offset).as_ubytes (font->privateDictInfo.size);
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700458 if (unlikely (!privDictStr.sanitize (&sc))) goto fail;
Behdad Esfahbodbff78e62022-05-10 16:33:37 -0600459 cff2_priv_dict_interp_env_t env2 (privDictStr);
460 dict_interpreter_t<PRIVOPSET, PRIVDICTVAL, cff2_priv_dict_interp_env_t> priv_interp (env2);
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500461 privateDicts[i].init ();
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700462 if (unlikely (!priv_interp.interpret (privateDicts[i]))) goto fail;
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700463
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800464 privateDicts[i].localSubrs = &StructAtOffsetOrNull<CFF2Subrs> (&privDictStr[0], privateDicts[i].subrsOffset);
Ebrahim Byagowi2dda6dd2020-04-20 14:12:45 +0430465 if (privateDicts[i].localSubrs != &Null (CFF2Subrs) &&
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500466 unlikely (!privateDicts[i].localSubrs->sanitize (&sc)))
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700467 goto fail;
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700468 }
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700469
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700470 return;
471
472 fail:
473 _fini ();
474 }
475 ~accelerator_templ_t () { _fini (); }
476 void _fini ()
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700477 {
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700478 sc.end_processing ();
Michiharu Ariza09fa5362018-12-27 08:33:09 -0800479 topDict.fini ();
Behdad Esfahbod4eb6e6e2022-01-18 12:07:05 -0700480 fontDicts.fini ();
481 privateDicts.fini ();
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700482 hb_blob_destroy (blob);
Michiharu Ariza9c4cada2018-07-30 14:28:40 -0700483 blob = nullptr;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700484 }
485
Behdad Esfahbodffc68992023-06-05 14:16:47 -0600486 hb_vector_t<uint16_t> *create_glyph_to_sid_map () const
Behdad Esfahbod32dd9812022-11-30 13:15:58 -0700487 {
488 return nullptr;
489 }
490
Behdad Esfahbod43ec78f2023-06-23 10:22:30 -0600491 hb_blob_t *get_blob () const { return blob; }
492
Ebrahim Byagowi2be859d2020-04-20 23:48:23 +0430493 bool is_valid () const { return blob; }
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700494
Michiharu Ariza58279c32018-08-02 10:18:01 -0700495 protected:
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800496 hb_sanitize_context_t sc;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700497
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700498 public:
Garret Rieger8d5c8992022-11-25 20:33:39 +0000499 hb_blob_t *blob = nullptr;
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800500 cff2_top_dict_values_t topDict;
Behdad Esfahboda8b7f182022-01-21 07:38:33 -0700501 const CFF2Subrs *globalSubrs = nullptr;
502 const CFF2VariationStore *varStore = nullptr;
503 const CFF2CharStrings *charStrings = nullptr;
504 const CFF2FDArray *fdArray = nullptr;
505 const CFF2FDSelect *fdSelect = nullptr;
506 unsigned int fdCount = 0;
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700507
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800508 hb_vector_t<cff2_font_dict_values_t> fontDicts;
Michiharu Arizafcf17782018-08-31 16:28:47 -0700509 hb_vector_t<PRIVDICTVAL> privateDicts;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700510
Behdad Esfahboda8b7f182022-01-21 07:38:33 -0700511 unsigned int num_glyphs = 0;
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700512 };
513
Michiharu Ariza29f0b6b2018-12-22 07:47:04 -0800514 struct accelerator_t : accelerator_templ_t<cff2_private_dict_opset_t, cff2_private_dict_values_t>
Michiharu Arizab51418f2018-10-08 15:05:36 -0700515 {
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700516 accelerator_t (hb_face_t *face) : accelerator_templ_t (face) {}
517
Michiharu Ariza2840a102018-11-01 16:18:13 -0700518 HB_INTERNAL bool get_extents (hb_font_t *font,
Behdad Esfahbod825df6d2018-11-30 23:04:59 -0500519 hb_codepoint_t glyph,
520 hb_glyph_extents_t *extents) const;
Matthias Clasen9be01b62022-12-21 14:04:32 -0500521 HB_INTERNAL bool paint_glyph (hb_font_t *font, hb_codepoint_t glyph, hb_paint_funcs_t *funcs, void *data, hb_color_t foreground) const;
Behdad Esfahbod370bec92022-02-04 18:40:44 -0600522 HB_INTERNAL bool get_path (hb_font_t *font, hb_codepoint_t glyph, hb_draw_session_t &draw_session) const;
Michiharu Arizab51418f2018-10-08 15:05:36 -0700523 };
524
Behdad Esfahbod43ec78f2023-06-23 10:22:30 -0600525 struct accelerator_subset_t : accelerator_templ_t<cff2_private_dict_opset_subset_t, cff2_private_dict_values_subset_t>
526 {
527 accelerator_subset_t (hb_face_t *face) : SUPER (face) {}
Behdad Esfahbod5f399132023-06-23 11:22:01 -0600528 ~accelerator_subset_t ()
529 {
530 if (cff_accelerator)
531 cff_subset_accelerator_t::destroy (cff_accelerator);
532 }
Behdad Esfahbod43ec78f2023-06-23 10:22:30 -0600533
534 HB_INTERNAL bool subset (hb_subset_context_t *c) const;
Behdad Esfahbod97d63e82023-06-23 10:43:34 -0600535 HB_INTERNAL bool serialize (hb_serialize_context_t *c,
536 struct cff2_subset_plan &plan,
537 hb_array_t<int> normalized_coords) const;
Behdad Esfahbod43ec78f2023-06-23 10:22:30 -0600538
Behdad Esfahbod5f399132023-06-23 11:22:01 -0600539 mutable CFF::cff_subset_accelerator_t* cff_accelerator = nullptr;
540
Behdad Esfahbod43ec78f2023-06-23 10:22:30 -0600541 typedef accelerator_templ_t<cff2_private_dict_opset_subset_t, cff2_private_dict_values_subset_t> SUPER;
542 };
Michiharu Ariza76f73972018-08-02 16:28:10 -0700543
Michiharu Arizab2ff5822018-08-01 11:30:38 -0700544 public:
Behdad Esfahbodb1152d52019-01-17 18:17:04 -0500545 FixedVersion<HBUINT8> version; /* Version of CFF2 table. set to 0x0200u */
546 NNOffsetTo<TopDict, HBUINT8> topDict; /* headerSize = Offset to Top DICT. */
547 HBUINT16 topDictSize; /* Top DICT size */
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700548
549 public:
550 DEFINE_SIZE_STATIC (5);
551};
552
Behdad Esfahbodac1bb3e2022-01-20 11:47:17 -0700553struct cff2_accelerator_t : cff2::accelerator_t {
554 cff2_accelerator_t (hb_face_t *face) : cff2::accelerator_t (face) {}
555};
556
Behdad Esfahbod43ec78f2023-06-23 10:22:30 -0600557struct cff2_subset_accelerator_t : cff2::accelerator_subset_t {
558 cff2_subset_accelerator_t (hb_face_t *face) : cff2::accelerator_subset_t (face) {}
559};
560
Michiharu Ariza3a61c3e2018-07-18 14:17:52 -0700561} /* namespace OT */
562
563#endif /* HB_OT_CFF2_TABLE_HH */