Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1 | /* |
Michiharu Ariza | 0dfa584 | 2018-11-12 08:47:07 -0800 | [diff] [blame] | 2 | * Copyright © 2018 Adobe Inc. |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 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 | |
Michiharu Ariza | 8af9690 | 2018-08-29 13:26:17 -0700 | [diff] [blame] | 27 | #include "hb-open-type.hh" |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 28 | #include "hb-ot-cff1-table.hh" |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 29 | #include "hb-set.h" |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 30 | #include "hb-subset-cff1.hh" |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 31 | #include "hb-subset-plan.hh" |
Michiharu Ariza | 8af9690 | 2018-08-29 13:26:17 -0700 | [diff] [blame] | 32 | #include "hb-subset-cff-common.hh" |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 33 | #include "hb-cff1-interp-cs.hh" |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 34 | |
| 35 | using namespace CFF; |
| 36 | |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 37 | struct RemapSID : Remap |
| 38 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 39 | unsigned int add (unsigned int sid) |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 40 | { |
| 41 | if ((sid != CFF_UNDEF_SID) && !is_std_std (sid)) |
| 42 | return offset_sid (Remap::add (unoffset_sid (sid))); |
| 43 | else |
| 44 | return sid; |
| 45 | } |
| 46 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 47 | unsigned int operator[] (unsigned int sid) const |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 48 | { |
Michiharu Ariza | 9d0231c | 2018-11-15 15:39:43 -0800 | [diff] [blame] | 49 | if (is_std_std (sid) || (sid == CFF_UNDEF_SID)) |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 50 | return sid; |
| 51 | else |
| 52 | return offset_sid (Remap::operator [] (unoffset_sid (sid))); |
| 53 | } |
| 54 | |
| 55 | static const unsigned int num_std_strings = 391; |
| 56 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 57 | static bool is_std_std (unsigned int sid) { return sid < num_std_strings; } |
| 58 | static unsigned int offset_sid (unsigned int sid) { return sid + num_std_strings; } |
| 59 | static unsigned int unoffset_sid (unsigned int sid) { return sid - num_std_strings; } |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Michiharu Ariza | 9fd08cc | 2018-08-29 18:18:18 -0700 | [diff] [blame] | 62 | struct CFF1SubTableOffsets : CFFSubTableOffsets |
| 63 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 64 | CFF1SubTableOffsets () |
Michiharu Ariza | 9fd08cc | 2018-08-29 18:18:18 -0700 | [diff] [blame] | 65 | : CFFSubTableOffsets (), |
| 66 | nameIndexOffset (0), |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 67 | encodingOffset (0) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 68 | { |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 69 | stringIndexInfo.init (); |
| 70 | charsetInfo.init (); |
Michiharu Ariza | 9fd08cc | 2018-08-29 18:18:18 -0700 | [diff] [blame] | 71 | privateDictInfo.init (); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | unsigned int nameIndexOffset; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 75 | TableInfo stringIndexInfo; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 76 | unsigned int encodingOffset; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 77 | TableInfo charsetInfo; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 78 | TableInfo privateDictInfo; |
| 79 | }; |
| 80 | |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 81 | /* a copy of a parsed out CFF1TopDictValues augmented with additional operators */ |
| 82 | struct CFF1TopDictValuesMod : CFF1TopDictValues |
| 83 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 84 | void init (const CFF1TopDictValues *base_= &Null(CFF1TopDictValues)) |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 85 | { |
| 86 | SUPER::init (); |
| 87 | base = base_; |
| 88 | } |
| 89 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 90 | void fini () { SUPER::fini (); } |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 91 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 92 | unsigned get_count () const { return base->get_count () + SUPER::get_count (); } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 93 | const CFF1TopDictVal &get_value (unsigned int i) const |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 94 | { |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 95 | if (i < base->get_count ()) |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 96 | return (*base)[i]; |
| 97 | else |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 98 | return SUPER::values[i - base->get_count ()]; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 99 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 100 | const CFF1TopDictVal &operator [] (unsigned int i) const { return get_value (i); } |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 101 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 102 | void reassignSIDs (const RemapSID& sidmap) |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 103 | { |
Michiharu Ariza | c6f75c3 | 2018-09-10 17:02:31 -0700 | [diff] [blame] | 104 | for (unsigned int i = 0; i < NameDictValues::ValCount; i++) |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 105 | nameSIDs[i] = sidmap[base->nameSIDs[i]]; |
| 106 | } |
| 107 | |
| 108 | protected: |
| 109 | typedef CFF1TopDictValues SUPER; |
| 110 | const CFF1TopDictValues *base; |
| 111 | }; |
| 112 | |
| 113 | struct TopDictModifiers |
| 114 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 115 | TopDictModifiers (const CFF1SubTableOffsets &offsets_, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 116 | const unsigned int (&nameSIDs_)[NameDictValues::ValCount]) |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 117 | : offsets (offsets_), |
| 118 | nameSIDs (nameSIDs_) |
| 119 | {} |
| 120 | |
| 121 | const CFF1SubTableOffsets &offsets; |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 122 | const unsigned int (&nameSIDs)[NameDictValues::ValCount]; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | struct CFF1TopDict_OpSerializer : CFFTopDict_OpSerializer<CFF1TopDictVal> |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 126 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 127 | bool serialize (hb_serialize_context_t *c, |
| 128 | const CFF1TopDictVal &opstr, |
| 129 | const TopDictModifiers &mod) const |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 130 | { |
| 131 | TRACE_SERIALIZE (this); |
| 132 | |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 133 | OpCode op = opstr.op; |
| 134 | switch (op) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 135 | { |
| 136 | case OpCode_charset: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 137 | return_trace (FontDict::serialize_offset4_op(c, op, mod.offsets.charsetInfo.offset)); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 138 | |
| 139 | case OpCode_Encoding: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 140 | return_trace (FontDict::serialize_offset4_op(c, op, mod.offsets.encodingOffset)); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 141 | |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 142 | case OpCode_Private: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 143 | { |
| 144 | if (unlikely (!UnsizedByteStr::serialize_int2 (c, mod.offsets.privateDictInfo.size))) |
| 145 | return_trace (false); |
| 146 | if (unlikely (!UnsizedByteStr::serialize_int4 (c, mod.offsets.privateDictInfo.offset))) |
| 147 | return_trace (false); |
| 148 | HBUINT8 *p = c->allocate_size<HBUINT8> (1); |
| 149 | if (unlikely (p == nullptr)) return_trace (false); |
| 150 | p->set (OpCode_Private); |
| 151 | } |
| 152 | break; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 153 | |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 154 | case OpCode_version: |
| 155 | case OpCode_Notice: |
| 156 | case OpCode_Copyright: |
| 157 | case OpCode_FullName: |
| 158 | case OpCode_FamilyName: |
| 159 | case OpCode_Weight: |
| 160 | case OpCode_PostScript: |
| 161 | case OpCode_BaseFontName: |
| 162 | case OpCode_FontName: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 163 | return_trace (FontDict::serialize_offset2_op(c, op, mod.nameSIDs[NameDictValues::name_op_to_index (op)])); |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 164 | |
| 165 | case OpCode_ROS: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 166 | { |
| 167 | /* for registry & ordering, reassigned SIDs are serialized |
| 168 | * for supplement, the original byte string is copied along with the op code */ |
| 169 | OpStr supp_op; |
| 170 | supp_op.op = op; |
| 171 | supp_op.str.str = opstr.str.str + opstr.last_arg_offset; |
Michiharu Ariza | 3335862 | 2018-12-11 12:20:20 -0800 | [diff] [blame] | 172 | if ( unlikely (!(opstr.str.len >= opstr.last_arg_offset + 3))) |
| 173 | return_trace (false); |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 174 | supp_op.str.len = opstr.str.len - opstr.last_arg_offset; |
Michiharu Ariza | 3335862 | 2018-12-11 12:20:20 -0800 | [diff] [blame] | 175 | return_trace (UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[NameDictValues::registry]) && |
| 176 | UnsizedByteStr::serialize_int2 (c, mod.nameSIDs[NameDictValues::ordering]) && |
| 177 | copy_opstr (c, supp_op)); |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 178 | } |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 179 | default: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 180 | return_trace (CFFTopDict_OpSerializer<CFF1TopDictVal>::serialize (c, opstr, mod.offsets)); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 181 | } |
| 182 | return_trace (true); |
| 183 | } |
| 184 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 185 | unsigned int calculate_serialized_size (const CFF1TopDictVal &opstr) const |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 186 | { |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 187 | OpCode op = opstr.op; |
| 188 | switch (op) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 189 | { |
| 190 | case OpCode_charset: |
| 191 | case OpCode_Encoding: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 192 | return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (op); |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 193 | |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 194 | case OpCode_Private: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 195 | return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_Private); |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 196 | |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 197 | case OpCode_version: |
| 198 | case OpCode_Notice: |
| 199 | case OpCode_Copyright: |
| 200 | case OpCode_FullName: |
| 201 | case OpCode_FamilyName: |
| 202 | case OpCode_Weight: |
| 203 | case OpCode_PostScript: |
| 204 | case OpCode_BaseFontName: |
| 205 | case OpCode_FontName: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 206 | return OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (op); |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 207 | |
| 208 | case OpCode_ROS: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 209 | return ((OpCode_Size (OpCode_shortint) + 2) * 2) + (opstr.str.len - opstr.last_arg_offset)/* supplement + op */; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 210 | |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 211 | default: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 212 | return CFFTopDict_OpSerializer<CFF1TopDictVal>::calculate_serialized_size (opstr); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | }; |
| 216 | |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 217 | struct FontDictValuesMod |
| 218 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 219 | void init (const CFF1FontDictValues *base_, |
| 220 | unsigned int fontName_, |
| 221 | const TableInfo &privateDictInfo_) |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 222 | { |
| 223 | base = base_; |
| 224 | fontName = fontName_; |
| 225 | privateDictInfo = privateDictInfo_; |
| 226 | } |
| 227 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 228 | unsigned get_count () const { return base->get_count (); } |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 229 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 230 | const OpStr &operator [] (unsigned int i) const { return (*base)[i]; } |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 231 | |
| 232 | const CFF1FontDictValues *base; |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 233 | TableInfo privateDictInfo; |
| 234 | unsigned int fontName; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | struct CFF1FontDict_OpSerializer : CFFFontDict_OpSerializer |
| 238 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 239 | bool serialize (hb_serialize_context_t *c, |
| 240 | const OpStr &opstr, |
| 241 | const FontDictValuesMod &mod) const |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 242 | { |
| 243 | TRACE_SERIALIZE (this); |
| 244 | |
| 245 | if (opstr.op == OpCode_FontName) |
| 246 | return_trace (FontDict::serialize_uint2_op (c, opstr.op, mod.fontName)); |
| 247 | else |
| 248 | return_trace (SUPER::serialize (c, opstr, mod.privateDictInfo)); |
| 249 | } |
| 250 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 251 | unsigned int calculate_serialized_size (const OpStr &opstr) const |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 252 | { |
| 253 | if (opstr.op == OpCode_FontName) |
| 254 | return OpCode_Size (OpCode_shortint) + 2 + OpCode_Size (OpCode_FontName); |
| 255 | else |
| 256 | return SUPER::calculate_serialized_size (opstr); |
| 257 | } |
| 258 | |
| 259 | private: |
| 260 | typedef CFFFontDict_OpSerializer SUPER; |
| 261 | }; |
| 262 | |
Michiharu Ariza | 8c5e03b | 2018-08-30 17:21:56 -0700 | [diff] [blame] | 263 | struct CFF1CSOpSet_Flatten : CFF1CSOpSet<CFF1CSOpSet_Flatten, FlattenParam> |
Michiharu Ariza | 5cde2f5 | 2018-08-17 16:50:13 -0700 | [diff] [blame] | 264 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 265 | static void flush_args_and_op (OpCode op, CFF1CSInterpEnv &env, FlattenParam& param) |
Michiharu Ariza | 5cde2f5 | 2018-08-17 16:50:13 -0700 | [diff] [blame] | 266 | { |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 267 | if (env.arg_start > 0) |
Michiharu Ariza | ebeccf3 | 2018-09-18 17:24:30 -0700 | [diff] [blame] | 268 | flush_width (env, param); |
| 269 | |
Michiharu Ariza | a11420b | 2018-08-29 12:14:30 -0700 | [diff] [blame] | 270 | switch (op) |
| 271 | { |
Michiharu Ariza | 968168b | 2018-08-31 13:28:16 -0700 | [diff] [blame] | 272 | case OpCode_hstem: |
| 273 | case OpCode_hstemhm: |
| 274 | case OpCode_vstem: |
| 275 | case OpCode_vstemhm: |
Michiharu Ariza | a11420b | 2018-08-29 12:14:30 -0700 | [diff] [blame] | 276 | case OpCode_hintmask: |
| 277 | case OpCode_cntrmask: |
Michiharu Ariza | b67a7c7 | 2018-11-16 12:28:24 -0800 | [diff] [blame] | 278 | case OpCode_dotsection: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 279 | if (param.drop_hints) |
| 280 | { |
| 281 | env.clear_args (); |
| 282 | return; |
| 283 | } |
| 284 | HB_FALLTHROUGH; |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 285 | |
Michiharu Ariza | a11420b | 2018-08-29 12:14:30 -0700 | [diff] [blame] | 286 | default: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 287 | SUPER::flush_args_and_op (op, env, param); |
| 288 | break; |
Michiharu Ariza | a11420b | 2018-08-29 12:14:30 -0700 | [diff] [blame] | 289 | } |
Michiharu Ariza | 5cde2f5 | 2018-08-17 16:50:13 -0700 | [diff] [blame] | 290 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 291 | static void flush_args (CFF1CSInterpEnv &env, FlattenParam& param) |
Michiharu Ariza | 5cde2f5 | 2018-08-17 16:50:13 -0700 | [diff] [blame] | 292 | { |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 293 | StrEncoder encoder (param.flatStr); |
| 294 | for (unsigned int i = env.arg_start; i < env.argStack.get_count (); i++) |
| 295 | encoder.encode_num (env.eval_arg (i)); |
| 296 | SUPER::flush_args (env, param); |
Michiharu Ariza | 968168b | 2018-08-31 13:28:16 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 299 | static void flush_op (OpCode op, CFF1CSInterpEnv &env, FlattenParam& param) |
Michiharu Ariza | 968168b | 2018-08-31 13:28:16 -0700 | [diff] [blame] | 300 | { |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 301 | StrEncoder encoder (param.flatStr); |
| 302 | encoder.encode_op (op); |
Michiharu Ariza | 968168b | 2018-08-31 13:28:16 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 305 | static void flush_width (CFF1CSInterpEnv &env, FlattenParam& param) |
Michiharu Ariza | ebeccf3 | 2018-09-18 17:24:30 -0700 | [diff] [blame] | 306 | { |
| 307 | assert (env.has_width); |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 308 | StrEncoder encoder (param.flatStr); |
| 309 | encoder.encode_num (env.width); |
Michiharu Ariza | ebeccf3 | 2018-09-18 17:24:30 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 312 | static void flush_hintmask (OpCode op, CFF1CSInterpEnv &env, FlattenParam& param) |
Michiharu Ariza | 968168b | 2018-08-31 13:28:16 -0700 | [diff] [blame] | 313 | { |
| 314 | SUPER::flush_hintmask (op, env, param); |
Michiharu Ariza | f2d299b | 2018-09-04 10:25:21 -0700 | [diff] [blame] | 315 | if (!param.drop_hints) |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 316 | { |
| 317 | StrEncoder encoder (param.flatStr); |
Michiharu Ariza | f2d299b | 2018-09-04 10:25:21 -0700 | [diff] [blame] | 318 | for (unsigned int i = 0; i < env.hintmask_size; i++) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 319 | encoder.encode_byte (env.substr[i]); |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 320 | } |
Michiharu Ariza | 5cde2f5 | 2018-08-17 16:50:13 -0700 | [diff] [blame] | 321 | } |
Michiharu Ariza | a11420b | 2018-08-29 12:14:30 -0700 | [diff] [blame] | 322 | |
| 323 | private: |
Michiharu Ariza | 8c5e03b | 2018-08-30 17:21:56 -0700 | [diff] [blame] | 324 | typedef CFF1CSOpSet<CFF1CSOpSet_Flatten, FlattenParam> SUPER; |
Michiharu Ariza | 5cde2f5 | 2018-08-17 16:50:13 -0700 | [diff] [blame] | 325 | }; |
| 326 | |
Michiharu Ariza | 1608481 | 2018-09-12 13:22:19 -0700 | [diff] [blame] | 327 | struct RangeList : hb_vector_t<code_pair> |
| 328 | { |
| 329 | /* replace the first glyph ID in the "glyph" field each range with a nLeft value */ |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 330 | bool finalize (unsigned int last_glyph) |
Michiharu Ariza | 1608481 | 2018-09-12 13:22:19 -0700 | [diff] [blame] | 331 | { |
Michiharu Ariza | a6da9b9 | 2018-11-16 17:29:03 -0800 | [diff] [blame] | 332 | bool two_byte = false; |
Michiharu Ariza | 1608481 | 2018-09-12 13:22:19 -0700 | [diff] [blame] | 333 | for (unsigned int i = (*this).len; i > 0; i--) |
| 334 | { |
| 335 | code_pair &pair = (*this)[i - 1]; |
| 336 | unsigned int nLeft = last_glyph - pair.glyph - 1; |
Michiharu Ariza | a6da9b9 | 2018-11-16 17:29:03 -0800 | [diff] [blame] | 337 | if (nLeft >= 0x100) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 338 | two_byte = true; |
Michiharu Ariza | 1608481 | 2018-09-12 13:22:19 -0700 | [diff] [blame] | 339 | last_glyph = pair.glyph; |
| 340 | pair.glyph = nLeft; |
| 341 | } |
Michiharu Ariza | a6da9b9 | 2018-11-16 17:29:03 -0800 | [diff] [blame] | 342 | return two_byte; |
Michiharu Ariza | 1608481 | 2018-09-12 13:22:19 -0700 | [diff] [blame] | 343 | } |
| 344 | }; |
| 345 | |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 346 | struct CFF1CSOpSet_SubrSubset : CFF1CSOpSet<CFF1CSOpSet_SubrSubset, SubrSubsetParam> |
| 347 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 348 | static void process_op (OpCode op, CFF1CSInterpEnv &env, SubrSubsetParam& param) |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 349 | { |
| 350 | switch (op) { |
| 351 | |
| 352 | case OpCode_return: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 353 | param.current_parsed_str->add_op (op, env.substr); |
| 354 | param.current_parsed_str->set_parsed (); |
| 355 | env.returnFromSubr (); |
Michiharu Ariza | 2941208 | 2018-12-11 12:21:24 -0800 | [diff] [blame] | 356 | param.set_current_str (env, false); |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 357 | break; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 358 | |
| 359 | case OpCode_endchar: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 360 | param.current_parsed_str->add_op (op, env.substr); |
| 361 | param.current_parsed_str->set_parsed (); |
| 362 | SUPER::process_op (op, env, param); |
| 363 | break; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 364 | |
| 365 | case OpCode_callsubr: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 366 | process_call_subr (op, CSType_LocalSubr, env, param, env.localSubrs, param.local_closure); |
| 367 | break; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 368 | |
| 369 | case OpCode_callgsubr: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 370 | process_call_subr (op, CSType_GlobalSubr, env, param, env.globalSubrs, param.global_closure); |
| 371 | break; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 372 | |
| 373 | default: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 374 | SUPER::process_op (op, env, param); |
| 375 | param.current_parsed_str->add_op (op, env.substr); |
| 376 | break; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 380 | protected: |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 381 | static void process_call_subr (OpCode op, CSType type, |
| 382 | CFF1CSInterpEnv &env, SubrSubsetParam& param, |
| 383 | CFF1BiasedSubrs& subrs, hb_set_t *closure) |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 384 | { |
| 385 | SubByteStr substr = env.substr; |
| 386 | env.callSubr (subrs, type); |
Michiharu Ariza | 6186dbf | 2018-11-01 17:25:23 -0700 | [diff] [blame] | 387 | param.current_parsed_str->add_call_op (op, substr, env.context.subr_num); |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 388 | hb_set_add (closure, env.context.subr_num); |
Michiharu Ariza | 2941208 | 2018-12-11 12:21:24 -0800 | [diff] [blame] | 389 | param.set_current_str (env, true); |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private: |
| 393 | typedef CFF1CSOpSet<CFF1CSOpSet_SubrSubset, SubrSubsetParam> SUPER; |
| 394 | }; |
| 395 | |
| 396 | struct CFF1SubrSubsetter : SubrSubsetter<CFF1SubrSubsetter, CFF1Subrs, const OT::cff1::accelerator_subset_t, CFF1CSInterpEnv, CFF1CSOpSet_SubrSubset> |
| 397 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 398 | static void finalize_parsed_str (CFF1CSInterpEnv &env, SubrSubsetParam& param, ParsedCStr &charstring) |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 399 | { |
Michiharu Ariza | 191ca0f | 2018-11-03 22:42:22 -0700 | [diff] [blame] | 400 | /* insert width at the beginning of the charstring as necessary */ |
Michiharu Ariza | 1bc710a | 2018-11-02 15:28:01 -0700 | [diff] [blame] | 401 | if (env.has_width) |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 402 | charstring.set_prefix (env.width); |
Michiharu Ariza | 191ca0f | 2018-11-03 22:42:22 -0700 | [diff] [blame] | 403 | |
| 404 | /* subroutines/charstring left on the call stack are legally left unmarked |
| 405 | * unmarked when a subroutine terminates with endchar. mark them. |
| 406 | */ |
| 407 | param.current_parsed_str->set_parsed (); |
| 408 | for (unsigned int i = 0; i < env.callStack.get_count (); i++) |
| 409 | { |
| 410 | ParsedCStr *parsed_str = param.get_parsed_str_for_context (env.callStack[i]); |
| 411 | if (likely (parsed_str != nullptr)) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 412 | parsed_str->set_parsed (); |
Michiharu Ariza | 191ca0f | 2018-11-03 22:42:22 -0700 | [diff] [blame] | 413 | else |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 414 | env.set_error (); |
Michiharu Ariza | 191ca0f | 2018-11-03 22:42:22 -0700 | [diff] [blame] | 415 | } |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 416 | } |
| 417 | }; |
| 418 | |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 419 | struct cff_subset_plan { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 420 | cff_subset_plan () |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 421 | : final_size (0), |
Michiharu Ariza | a11420b | 2018-08-29 12:14:30 -0700 | [diff] [blame] | 422 | offsets (), |
Michiharu Ariza | d7b384a | 2018-10-02 21:08:36 -0700 | [diff] [blame] | 423 | orig_fdcount (0), |
Michiharu Ariza | e9cc71a | 2018-10-02 20:44:30 -0700 | [diff] [blame] | 424 | subset_fdcount (1), |
Michiharu Ariza | 51d5bf4 | 2018-10-02 14:38:06 -0700 | [diff] [blame] | 425 | subset_fdselect_format (0), |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 426 | drop_hints (false), |
| 427 | desubroutinize(false) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 428 | { |
| 429 | topdict_sizes.init (); |
| 430 | topdict_sizes.resize (1); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 431 | topdict_mod.init (); |
Michiharu Ariza | 0f159a3 | 2018-09-12 16:08:54 -0700 | [diff] [blame] | 432 | subset_fdselect_ranges.init (); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 433 | fdmap.init (); |
| 434 | subset_charstrings.init (); |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 435 | subset_globalsubrs.init (); |
| 436 | subset_localsubrs.init (); |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 437 | fontdicts_mod.init (); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 438 | subset_enc_code_ranges.init (); |
| 439 | subset_enc_supp_codes.init (); |
| 440 | subset_charset_ranges.init (); |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 441 | sidmap.init (); |
Michiharu Ariza | c6f75c3 | 2018-09-10 17:02:31 -0700 | [diff] [blame] | 442 | for (unsigned int i = 0; i < NameDictValues::ValCount; i++) |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 443 | topDictModSIDs[i] = CFF_UNDEF_SID; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 446 | ~cff_subset_plan () |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 447 | { |
| 448 | topdict_sizes.fini (); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 449 | topdict_mod.fini (); |
Michiharu Ariza | 0f159a3 | 2018-09-12 16:08:54 -0700 | [diff] [blame] | 450 | subset_fdselect_ranges.fini (); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 451 | fdmap.fini (); |
Michiharu Ariza | a9c305c | 2018-11-01 10:31:21 -0700 | [diff] [blame] | 452 | subset_charstrings.fini_deep (); |
| 453 | subset_globalsubrs.fini_deep (); |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 454 | subset_localsubrs.fini_deep (); |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 455 | fontdicts_mod.fini (); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 456 | subset_enc_code_ranges.fini (); |
| 457 | subset_enc_supp_codes.init (); |
| 458 | subset_charset_ranges.fini (); |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 459 | sidmap.fini (); |
| 460 | fontdicts_mod.fini (); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 463 | unsigned int plan_subset_encoding (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan) |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 464 | { |
| 465 | const Encoding *encoding = acc.encoding; |
| 466 | unsigned int size0, size1, supp_size; |
| 467 | hb_codepoint_t code, last_code = CFF_UNDEF_CODE; |
| 468 | hb_vector_t<hb_codepoint_t> supp_codes; |
| 469 | |
| 470 | subset_enc_code_ranges.resize (0); |
| 471 | supp_size = 0; |
| 472 | supp_codes.init (); |
| 473 | |
| 474 | subset_enc_num_codes = plan->glyphs.len - 1; |
| 475 | unsigned int glyph; |
| 476 | for (glyph = 1; glyph < plan->glyphs.len; glyph++) |
| 477 | { |
| 478 | hb_codepoint_t orig_glyph = plan->glyphs[glyph]; |
| 479 | code = acc.glyph_to_code (orig_glyph); |
| 480 | if (code == CFF_UNDEF_CODE) |
| 481 | { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 482 | subset_enc_num_codes = glyph - 1; |
| 483 | break; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | if (code != last_code + 1) |
| 487 | { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 488 | code_pair pair = { code, glyph }; |
| 489 | subset_enc_code_ranges.push (pair); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 490 | } |
| 491 | last_code = code; |
| 492 | |
| 493 | if (encoding != &Null(Encoding)) |
| 494 | { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 495 | hb_codepoint_t sid = acc.glyph_to_sid (orig_glyph); |
| 496 | encoding->get_supplement_codes (sid, supp_codes); |
| 497 | for (unsigned int i = 0; i < supp_codes.len; i++) |
| 498 | { |
| 499 | code_pair pair = { supp_codes[i], sid }; |
| 500 | subset_enc_supp_codes.push (pair); |
| 501 | } |
| 502 | supp_size += SuppEncoding::static_size * supp_codes.len; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | supp_codes.fini (); |
Michiharu Ariza | 1608481 | 2018-09-12 13:22:19 -0700 | [diff] [blame] | 506 | |
| 507 | subset_enc_code_ranges.finalize (glyph); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 508 | |
| 509 | assert (subset_enc_num_codes <= 0xFF); |
| 510 | size0 = Encoding0::min_size + HBUINT8::static_size * subset_enc_num_codes; |
| 511 | size1 = Encoding1::min_size + Encoding1_Range::static_size * subset_enc_code_ranges.len; |
| 512 | |
| 513 | if (size0 < size1) |
| 514 | subset_enc_format = 0; |
| 515 | else |
| 516 | subset_enc_format = 1; |
| 517 | |
| 518 | return Encoding::calculate_serialized_size ( |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 519 | subset_enc_format, |
| 520 | subset_enc_format? subset_enc_code_ranges.len: subset_enc_num_codes, |
| 521 | subset_enc_supp_codes.len); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 524 | unsigned int plan_subset_charset (const OT::cff1::accelerator_subset_t &acc, hb_subset_plan_t *plan) |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 525 | { |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 526 | unsigned int size0, size_ranges; |
| 527 | hb_codepoint_t sid, last_sid = CFF_UNDEF_CODE; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 528 | |
| 529 | subset_charset_ranges.resize (0); |
| 530 | unsigned int glyph; |
| 531 | for (glyph = 1; glyph < plan->glyphs.len; glyph++) |
| 532 | { |
| 533 | hb_codepoint_t orig_glyph = plan->glyphs[glyph]; |
| 534 | sid = acc.glyph_to_sid (orig_glyph); |
| 535 | |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 536 | if (!acc.is_CID ()) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 537 | sid = sidmap.add (sid); |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 538 | |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 539 | if (sid != last_sid + 1) |
| 540 | { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 541 | code_pair pair = { sid, glyph }; |
| 542 | subset_charset_ranges.push (pair); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 543 | } |
| 544 | last_sid = sid; |
| 545 | } |
| 546 | |
Michiharu Ariza | a6da9b9 | 2018-11-16 17:29:03 -0800 | [diff] [blame] | 547 | bool two_byte = subset_charset_ranges.finalize (glyph); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 548 | |
| 549 | size0 = Charset0::min_size + HBUINT16::static_size * (plan->glyphs.len - 1); |
| 550 | if (!two_byte) |
| 551 | size_ranges = Charset1::min_size + Charset1_Range::static_size * subset_charset_ranges.len; |
| 552 | else |
| 553 | size_ranges = Charset2::min_size + Charset2_Range::static_size * subset_charset_ranges.len; |
| 554 | |
| 555 | if (size0 < size_ranges) |
| 556 | subset_charset_format = 0; |
| 557 | else if (!two_byte) |
| 558 | subset_charset_format = 1; |
| 559 | else |
| 560 | subset_charset_format = 2; |
| 561 | |
| 562 | return Charset::calculate_serialized_size ( |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 563 | subset_charset_format, |
| 564 | subset_charset_format? subset_charset_ranges.len: plan->glyphs.len); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 567 | bool collect_sids_in_dicts (const OT::cff1::accelerator_subset_t &acc) |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 568 | { |
| 569 | if (unlikely (!sidmap.reset (acc.stringIndex->count))) |
| 570 | return false; |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 571 | |
Michiharu Ariza | c6f75c3 | 2018-09-10 17:02:31 -0700 | [diff] [blame] | 572 | for (unsigned int i = 0; i < NameDictValues::ValCount; i++) |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 573 | { |
| 574 | unsigned int sid = acc.topDict.nameSIDs[i]; |
| 575 | if (sid != CFF_UNDEF_SID) |
| 576 | { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 577 | (void)sidmap.add (sid); |
| 578 | topDictModSIDs[i] = sidmap[sid]; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
| 582 | if (acc.fdArray != &Null(CFF1FDArray)) |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 583 | for (unsigned int i = 0; i < orig_fdcount; i++) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 584 | if (fdmap.includes (i)) |
| 585 | (void)sidmap.add (acc.fontDicts[i].fontName); |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 586 | |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 587 | return true; |
| 588 | } |
| 589 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 590 | bool create (const OT::cff1::accelerator_subset_t &acc, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 591 | hb_subset_plan_t *plan) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 592 | { |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 593 | /* make sure notdef is first */ |
Michiharu Ariza | d8c6913 | 2018-11-30 18:58:14 -0800 | [diff] [blame] | 594 | if ((plan->glyphs.len == 0) || (plan->glyphs[0] != 0)) return false; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 595 | |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 596 | final_size = 0; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 597 | num_glyphs = plan->glyphs.len; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 598 | orig_fdcount = acc.fdCount; |
Michiharu Ariza | 5cde2f5 | 2018-08-17 16:50:13 -0700 | [diff] [blame] | 599 | drop_hints = plan->drop_hints; |
Michiharu Ariza | 6186dbf | 2018-11-01 17:25:23 -0700 | [diff] [blame] | 600 | desubroutinize = plan->desubroutinize; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 601 | |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 602 | /* check whether the subset renumbers any glyph IDs */ |
| 603 | gid_renum = false; |
| 604 | for (unsigned int glyph = 0; glyph < plan->glyphs.len; glyph++) |
| 605 | { |
| 606 | if (plan->glyphs[glyph] != glyph) { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 607 | gid_renum = true; |
| 608 | break; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | subset_charset = gid_renum || !acc.is_predef_charset (); |
Michiharu Ariza | ebeccf3 | 2018-09-18 17:24:30 -0700 | [diff] [blame] | 613 | subset_encoding = !acc.is_CID() && !acc.is_predef_encoding (); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 614 | |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 615 | /* CFF header */ |
Michiharu Ariza | e67bb3f | 2018-08-16 00:25:57 -0700 | [diff] [blame] | 616 | final_size += OT::cff1::static_size; |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 617 | |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 618 | /* Name INDEX */ |
| 619 | offsets.nameIndexOffset = final_size; |
| 620 | final_size += acc.nameIndex->get_size (); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 621 | |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 622 | /* top dict INDEX */ |
| 623 | { |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 624 | /* Add encoding/charset to a (copy of) top dict as necessary */ |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 625 | topdict_mod.init (&acc.topDict); |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 626 | bool need_to_add_enc = (subset_encoding && !acc.topDict.has_op (OpCode_Encoding)); |
| 627 | bool need_to_add_set = (subset_charset && !acc.topDict.has_op (OpCode_charset)); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 628 | if (need_to_add_enc || need_to_add_set) |
| 629 | { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 630 | if (need_to_add_enc) |
| 631 | topdict_mod.add_op (OpCode_Encoding); |
| 632 | if (need_to_add_set) |
| 633 | topdict_mod.add_op (OpCode_charset); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 634 | } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 635 | offsets.topDictInfo.offset = final_size; |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 636 | CFF1TopDict_OpSerializer topSzr; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 637 | unsigned int topDictSize = TopDict::calculate_serialized_size (topdict_mod, topSzr); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 638 | offsets.topDictInfo.offSize = calcOffSize(topDictSize); |
Michiharu Ariza | 3335862 | 2018-12-11 12:20:20 -0800 | [diff] [blame] | 639 | if (unlikely (offsets.topDictInfo.offSize > 4)) |
| 640 | return false; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 641 | final_size += CFF1IndexOf<TopDict>::calculate_serialized_size<CFF1TopDictValuesMod> |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 642 | (offsets.topDictInfo.offSize, |
| 643 | &topdict_mod, 1, topdict_sizes, topSzr); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 646 | /* Determine re-mapping of font index as fdmap among other info */ |
Michiharu Ariza | b6903bd | 2018-11-16 13:46:58 -0800 | [diff] [blame] | 647 | if (acc.fdSelect != &Null(CFF1FDSelect)) |
| 648 | { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 649 | if (unlikely (!hb_plan_subset_cff_fdselect (plan->glyphs, |
| 650 | orig_fdcount, |
| 651 | *acc.fdSelect, |
| 652 | subset_fdcount, |
| 653 | offsets.FDSelectInfo.size, |
| 654 | subset_fdselect_format, |
| 655 | subset_fdselect_ranges, |
| 656 | fdmap))) |
| 657 | return false; |
Michiharu Ariza | b6903bd | 2018-11-16 13:46:58 -0800 | [diff] [blame] | 658 | } |
| 659 | else |
| 660 | fdmap.identity (1); |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 661 | |
| 662 | /* remove unused SIDs & reassign SIDs */ |
| 663 | { |
| 664 | /* SIDs for name strings in dicts are added before glyph names so they fit in 16-bit int range */ |
| 665 | if (unlikely (!collect_sids_in_dicts (acc))) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 666 | return false; |
Michiharu Ariza | 3335862 | 2018-12-11 12:20:20 -0800 | [diff] [blame] | 667 | if (unlikely (sidmap.get_count () > 0x8000)) /* assumption: a dict won't reference that many strings */ |
| 668 | return false; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 669 | if (subset_charset) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 670 | offsets.charsetInfo.size = plan_subset_charset (acc, plan); |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 671 | |
| 672 | topdict_mod.reassignSIDs (sidmap); |
| 673 | } |
| 674 | |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 675 | /* String INDEX */ |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 676 | { |
| 677 | offsets.stringIndexInfo.offset = final_size; |
| 678 | offsets.stringIndexInfo.size = acc.stringIndex->calculate_serialized_size (offsets.stringIndexInfo.offSize, sidmap); |
| 679 | final_size += offsets.stringIndexInfo.size; |
| 680 | } |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 681 | |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 682 | if (desubroutinize) |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 683 | { |
Michiharu Ariza | a11420b | 2018-08-29 12:14:30 -0700 | [diff] [blame] | 684 | /* Flatten global & local subrs */ |
Michiharu Ariza | 8c5e03b | 2018-08-30 17:21:56 -0700 | [diff] [blame] | 685 | SubrFlattener<const OT::cff1::accelerator_subset_t, CFF1CSInterpEnv, CFF1CSOpSet_Flatten> |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 686 | flattener(acc, plan->glyphs, plan->drop_hints); |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 687 | if (!flattener.flatten (subset_charstrings)) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 688 | return false; |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 689 | |
Michiharu Ariza | a11420b | 2018-08-29 12:14:30 -0700 | [diff] [blame] | 690 | /* no global/local subroutines */ |
Michiharu Ariza | 1bc710a | 2018-11-02 15:28:01 -0700 | [diff] [blame] | 691 | offsets.globalSubrsInfo.size = CFF1Subrs::calculate_serialized_size (1, 0, 0); |
Michiharu Ariza | a11420b | 2018-08-29 12:14:30 -0700 | [diff] [blame] | 692 | } |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 693 | else |
| 694 | { |
| 695 | /* Subset subrs: collect used subroutines, leaving all unused ones behind */ |
| 696 | if (!subr_subsetter.subset (acc, plan->glyphs, plan->drop_hints)) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 697 | return false; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 698 | |
| 699 | /* encode charstrings, global subrs, local subrs with new subroutine numbers */ |
| 700 | if (!subr_subsetter.encode_charstrings (acc, plan->glyphs, subset_charstrings)) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 701 | return false; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 702 | |
| 703 | if (!subr_subsetter.encode_globalsubrs (subset_globalsubrs)) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 704 | return false; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 705 | |
| 706 | /* global subrs */ |
| 707 | unsigned int dataSize = subset_globalsubrs.total_size (); |
| 708 | offsets.globalSubrsInfo.offSize = calcOffSize (dataSize); |
Michiharu Ariza | 3335862 | 2018-12-11 12:20:20 -0800 | [diff] [blame] | 709 | if (unlikely (offsets.globalSubrsInfo.offSize > 4)) |
| 710 | return false; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 711 | offsets.globalSubrsInfo.size = CFF1Subrs::calculate_serialized_size (offsets.globalSubrsInfo.offSize, subset_globalsubrs.len, dataSize); |
| 712 | |
| 713 | /* local subrs */ |
| 714 | if (!offsets.localSubrsInfos.resize (orig_fdcount)) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 715 | return false; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 716 | if (!subset_localsubrs.resize (orig_fdcount)) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 717 | return false; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 718 | for (unsigned int fd = 0; fd < orig_fdcount; fd++) |
| 719 | { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 720 | subset_localsubrs[fd].init (); |
| 721 | offsets.localSubrsInfos[fd].init (); |
| 722 | if (fdmap.includes (fd)) |
| 723 | { |
| 724 | if (!subr_subsetter.encode_localsubrs (fd, subset_localsubrs[fd])) |
| 725 | return false; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 726 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 727 | unsigned int dataSize = subset_localsubrs[fd].total_size (); |
| 728 | if (dataSize > 0) |
| 729 | { |
| 730 | offsets.localSubrsInfos[fd].offset = final_size; |
| 731 | offsets.localSubrsInfos[fd].offSize = calcOffSize (dataSize); |
Michiharu Ariza | 3335862 | 2018-12-11 12:20:20 -0800 | [diff] [blame] | 732 | if (unlikely (offsets.localSubrsInfos[fd].offSize > 4)) |
| 733 | return false; |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 734 | offsets.localSubrsInfos[fd].size = CFF1Subrs::calculate_serialized_size (offsets.localSubrsInfos[fd].offSize, subset_localsubrs[fd].len, dataSize); |
| 735 | } |
| 736 | } |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 737 | } |
| 738 | } |
Michiharu Ariza | cc52e53 | 2018-09-10 16:27:49 -0700 | [diff] [blame] | 739 | |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 740 | /* global subrs */ |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 741 | offsets.globalSubrsInfo.offset = final_size; |
| 742 | final_size += offsets.globalSubrsInfo.size; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 743 | |
| 744 | /* Encoding */ |
Michiharu Ariza | ecdb77f | 2018-11-15 10:54:15 -0800 | [diff] [blame] | 745 | if (!subset_encoding) |
Michiharu Ariza | 9228db0 | 2018-10-13 17:25:09 -0700 | [diff] [blame] | 746 | offsets.encodingOffset = acc.topDict.EncodingOffset; |
| 747 | else |
Michiharu Ariza | ecdb77f | 2018-11-15 10:54:15 -0800 | [diff] [blame] | 748 | { |
Michiharu Ariza | 9228db0 | 2018-10-13 17:25:09 -0700 | [diff] [blame] | 749 | offsets.encodingOffset = final_size; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 750 | final_size += plan_subset_encoding (acc, plan); |
Michiharu Ariza | ecdb77f | 2018-11-15 10:54:15 -0800 | [diff] [blame] | 751 | } |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 752 | |
| 753 | /* Charset */ |
Michiharu Ariza | 9228db0 | 2018-10-13 17:25:09 -0700 | [diff] [blame] | 754 | if (!subset_charset && acc.is_predef_charset ()) |
| 755 | offsets.charsetInfo.offset = acc.topDict.CharsetOffset; |
| 756 | else |
| 757 | offsets.charsetInfo.offset = final_size; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 758 | final_size += offsets.charsetInfo.size; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 759 | |
| 760 | /* FDSelect */ |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 761 | if (acc.fdSelect != &Null(CFF1FDSelect)) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 762 | { |
| 763 | offsets.FDSelectInfo.offset = final_size; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 764 | final_size += offsets.FDSelectInfo.size; |
| 765 | } |
| 766 | |
| 767 | /* FDArray (FDIndex) */ |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 768 | if (acc.fdArray != &Null(CFF1FDArray)) { |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 769 | offsets.FDArrayInfo.offset = final_size; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 770 | CFF1FontDict_OpSerializer fontSzr; |
| 771 | unsigned int dictsSize = 0; |
| 772 | for (unsigned int i = 0; i < acc.fontDicts.len; i++) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 773 | if (fdmap.includes (i)) |
| 774 | dictsSize += FontDict::calculate_serialized_size (acc.fontDicts[i], fontSzr); |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 775 | |
Michiharu Ariza | 5b453f7 | 2018-09-11 16:20:39 -0700 | [diff] [blame] | 776 | offsets.FDArrayInfo.offSize = calcOffSize (dictsSize); |
Michiharu Ariza | 3335862 | 2018-12-11 12:20:20 -0800 | [diff] [blame] | 777 | if (unlikely (offsets.FDArrayInfo.offSize > 4)) |
| 778 | return false; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 779 | final_size += CFF1Index::calculate_serialized_size (offsets.FDArrayInfo.offSize, subset_fdcount, dictsSize); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /* CharStrings */ |
| 783 | { |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 784 | offsets.charStringsInfo.offset = final_size; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 785 | unsigned int dataSize = subset_charstrings.total_size (); |
Michiharu Ariza | 5b453f7 | 2018-09-11 16:20:39 -0700 | [diff] [blame] | 786 | offsets.charStringsInfo.offSize = calcOffSize (dataSize); |
Michiharu Ariza | 3335862 | 2018-12-11 12:20:20 -0800 | [diff] [blame] | 787 | if (unlikely (offsets.charStringsInfo.offSize > 4)) |
| 788 | return false; |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 789 | final_size += CFF1CharStrings::calculate_serialized_size (offsets.charStringsInfo.offSize, plan->glyphs.len, dataSize); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /* private dicts & local subrs */ |
| 793 | offsets.privateDictInfo.offset = final_size; |
| 794 | for (unsigned int i = 0; i < orig_fdcount; i++) |
| 795 | { |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 796 | if (fdmap.includes (i)) |
Michiharu Ariza | a97ed34 | 2018-08-10 12:55:22 -0700 | [diff] [blame] | 797 | { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 798 | bool has_localsubrs = offsets.localSubrsInfos[i].size > 0; |
| 799 | CFFPrivateDict_OpSerializer privSzr (desubroutinize, plan->drop_hints); |
| 800 | unsigned int priv_size = PrivateDict::calculate_serialized_size (acc.privateDicts[i], privSzr, has_localsubrs); |
| 801 | TableInfo privInfo = { final_size, priv_size, 0 }; |
| 802 | FontDictValuesMod fontdict_mod; |
| 803 | if (!acc.is_CID ()) |
| 804 | fontdict_mod.init ( &Null(CFF1FontDictValues), CFF_UNDEF_SID, privInfo ); |
| 805 | else |
| 806 | fontdict_mod.init ( &acc.fontDicts[i], sidmap[acc.fontDicts[i].fontName], privInfo ); |
| 807 | fontdicts_mod.push (fontdict_mod); |
| 808 | final_size += privInfo.size; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 809 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 810 | if (!plan->desubroutinize && has_localsubrs) |
| 811 | { |
| 812 | offsets.localSubrsInfos[i].offset = final_size; |
| 813 | final_size += offsets.localSubrsInfos[i].size; |
| 814 | } |
Michiharu Ariza | a97ed34 | 2018-08-10 12:55:22 -0700 | [diff] [blame] | 815 | } |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | if (!acc.is_CID ()) |
Michiharu Ariza | d8c6913 | 2018-11-30 18:58:14 -0800 | [diff] [blame] | 819 | offsets.privateDictInfo = fontdicts_mod[0].privateDictInfo; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 820 | |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 821 | return ((subset_charstrings.len == plan->glyphs.len) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 822 | && (fontdicts_mod.len == subset_fdcount)); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 825 | unsigned int get_final_size () const { return final_size; } |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 826 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 827 | unsigned int final_size; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 828 | hb_vector_t<unsigned int> topdict_sizes; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 829 | CFF1TopDictValuesMod topdict_mod; |
| 830 | CFF1SubTableOffsets offsets; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 831 | |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 832 | unsigned int num_glyphs; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 833 | unsigned int orig_fdcount; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 834 | unsigned int subset_fdcount; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 835 | unsigned int subset_fdselect_format; |
Michiharu Ariza | 0f159a3 | 2018-09-12 16:08:54 -0700 | [diff] [blame] | 836 | hb_vector_t<code_pair> subset_fdselect_ranges; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 837 | |
| 838 | /* font dict index remap table from fullset FDArray to subset FDArray. |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 839 | * set to CFF_UNDEF_CODE if excluded from subset */ |
| 840 | Remap fdmap; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 841 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 842 | StrBuffArray subset_charstrings; |
| 843 | StrBuffArray subset_globalsubrs; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 844 | hb_vector_t<StrBuffArray> subset_localsubrs; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 845 | hb_vector_t<FontDictValuesMod> fontdicts_mod; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 846 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 847 | bool drop_hints; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 848 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 849 | bool gid_renum; |
| 850 | bool subset_encoding; |
| 851 | uint8_t subset_enc_format; |
| 852 | unsigned int subset_enc_num_codes; |
| 853 | RangeList subset_enc_code_ranges; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 854 | hb_vector_t<code_pair> subset_enc_supp_codes; |
| 855 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 856 | uint8_t subset_charset_format; |
| 857 | RangeList subset_charset_ranges; |
| 858 | bool subset_charset; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 859 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 860 | RemapSID sidmap; |
| 861 | unsigned int topDictModSIDs[NameDictValues::ValCount]; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 862 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 863 | bool desubroutinize; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 864 | CFF1SubrSubsetter subr_subsetter; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 865 | }; |
| 866 | |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 867 | static inline bool _write_cff1 (const cff_subset_plan &plan, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 868 | const OT::cff1::accelerator_subset_t &acc, |
| 869 | const hb_vector_t<hb_codepoint_t>& glyphs, |
| 870 | unsigned int dest_sz, |
| 871 | void *dest) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 872 | { |
| 873 | hb_serialize_context_t c (dest, dest_sz); |
| 874 | |
Michiharu Ariza | c234839 | 2018-08-15 13:04:43 -0700 | [diff] [blame] | 875 | char RETURN_OP[1] = { OpCode_return }; |
Michiharu Ariza | 270452a | 2018-08-15 13:15:08 -0700 | [diff] [blame] | 876 | const ByteStr NULL_SUBR (RETURN_OP, 1); |
Michiharu Ariza | c234839 | 2018-08-15 13:04:43 -0700 | [diff] [blame] | 877 | |
Michiharu Ariza | e67bb3f | 2018-08-16 00:25:57 -0700 | [diff] [blame] | 878 | OT::cff1 *cff = c.start_serialize<OT::cff1> (); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 879 | if (unlikely (!c.extend_min (*cff))) |
| 880 | return false; |
| 881 | |
| 882 | /* header */ |
| 883 | cff->version.major.set (0x01); |
| 884 | cff->version.minor.set (0x00); |
| 885 | cff->nameIndex.set (cff->min_size); |
| 886 | cff->offSize.set (4); /* unused? */ |
| 887 | |
| 888 | /* name INDEX */ |
| 889 | { |
| 890 | assert (cff->nameIndex == c.head - c.start); |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 891 | CFF1NameIndex *dest = c.start_embed<CFF1NameIndex> (); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 892 | if (unlikely (dest == nullptr)) return false; |
| 893 | if (unlikely (!dest->serialize (&c, *acc.nameIndex))) |
| 894 | { |
| 895 | DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF name INDEX"); |
| 896 | return false; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | /* top dict INDEX */ |
| 901 | { |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 902 | assert (plan.offsets.topDictInfo.offset == c.head - c.start); |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 903 | CFF1IndexOf<TopDict> *dest = c.start_embed< CFF1IndexOf<TopDict> > (); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 904 | if (dest == nullptr) return false; |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 905 | CFF1TopDict_OpSerializer topSzr; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 906 | TopDictModifiers modifier (plan.offsets, plan.topDictModSIDs); |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 907 | if (unlikely (!dest->serialize (&c, plan.offsets.topDictInfo.offSize, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 908 | &plan.topdict_mod, 1, |
| 909 | plan.topdict_sizes, topSzr, modifier))) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 910 | { |
| 911 | DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF top dict"); |
| 912 | return false; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /* String INDEX */ |
| 917 | { |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 918 | assert (plan.offsets.stringIndexInfo.offset == c.head - c.start); |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 919 | CFF1StringIndex *dest = c.start_embed<CFF1StringIndex> (); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 920 | if (unlikely (dest == nullptr)) return false; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 921 | if (unlikely (!dest->serialize (&c, *acc.stringIndex, plan.offsets.stringIndexInfo.offSize, plan.sidmap))) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 922 | { |
| 923 | DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF string INDEX"); |
| 924 | return false; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | /* global subrs */ |
| 929 | { |
Michiharu Ariza | a11420b | 2018-08-29 12:14:30 -0700 | [diff] [blame] | 930 | assert (plan.offsets.globalSubrsInfo.offset != 0); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 931 | assert (plan.offsets.globalSubrsInfo.offset == c.head - c.start); |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 932 | |
Michiharu Ariza | 1bc710a | 2018-11-02 15:28:01 -0700 | [diff] [blame] | 933 | CFF1Subrs *dest = c.start_embed <CFF1Subrs> (); |
| 934 | if (unlikely (dest == nullptr)) return false; |
| 935 | if (unlikely (!dest->serialize (&c, plan.offsets.globalSubrsInfo.offSize, plan.subset_globalsubrs))) |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 936 | { |
Michiharu Ariza | 1bc710a | 2018-11-02 15:28:01 -0700 | [diff] [blame] | 937 | DEBUG_MSG (SUBSET, nullptr, "failed to serialize global subroutines"); |
| 938 | return false; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 939 | } |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* Encoding */ |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 943 | if (plan.subset_encoding) |
| 944 | { |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 945 | assert (plan.offsets.encodingOffset == c.head - c.start); |
| 946 | Encoding *dest = c.start_embed<Encoding> (); |
| 947 | if (unlikely (dest == nullptr)) return false; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 948 | if (unlikely (!dest->serialize (&c, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 949 | plan.subset_enc_format, |
| 950 | plan.subset_enc_num_codes, |
| 951 | plan.subset_enc_code_ranges, |
| 952 | plan.subset_enc_supp_codes))) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 953 | { |
| 954 | DEBUG_MSG (SUBSET, nullptr, "failed to serialize Encoding"); |
| 955 | return false; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | /* Charset */ |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 960 | if (plan.subset_charset) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 961 | { |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 962 | assert (plan.offsets.charsetInfo.offset == c.head - c.start); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 963 | Charset *dest = c.start_embed<Charset> (); |
| 964 | if (unlikely (dest == nullptr)) return false; |
Michiharu Ariza | aca73c9 | 2018-09-06 17:28:15 -0700 | [diff] [blame] | 965 | if (unlikely (!dest->serialize (&c, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 966 | plan.subset_charset_format, |
| 967 | plan.num_glyphs, |
| 968 | plan.subset_charset_ranges))) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 969 | { |
| 970 | DEBUG_MSG (SUBSET, nullptr, "failed to serialize Charset"); |
| 971 | return false; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | /* FDSelect */ |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 976 | if (acc.fdSelect != &Null(CFF1FDSelect)) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 977 | { |
| 978 | assert (plan.offsets.FDSelectInfo.offset == c.head - c.start); |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 979 | |
Michiharu Ariza | 9d0231c | 2018-11-15 15:39:43 -0800 | [diff] [blame] | 980 | if (unlikely (!hb_serialize_cff_fdselect (&c, glyphs.len, *acc.fdSelect, acc.fdCount, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 981 | plan.subset_fdselect_format, plan.offsets.FDSelectInfo.size, |
| 982 | plan.subset_fdselect_ranges))) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 983 | { |
Michiharu Ariza | 9d0231c | 2018-11-15 15:39:43 -0800 | [diff] [blame] | 984 | DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF subset FDSelect"); |
| 985 | return false; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
| 989 | /* FDArray (FD Index) */ |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 990 | if (acc.fdArray != &Null(CFF1FDArray)) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 991 | { |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 992 | assert (plan.offsets.FDArrayInfo.offset == c.head - c.start); |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 993 | CFF1FDArray *fda = c.start_embed<CFF1FDArray> (); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 994 | if (unlikely (fda == nullptr)) return false; |
Michiharu Ariza | 1666b89 | 2018-09-10 16:00:20 -0700 | [diff] [blame] | 995 | CFF1FontDict_OpSerializer fontSzr; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 996 | if (unlikely (!fda->serialize (&c, plan.offsets.FDArrayInfo.offSize, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 997 | plan.fontdicts_mod, |
| 998 | fontSzr))) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 999 | { |
| 1000 | DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF FDArray"); |
| 1001 | return false; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | /* CharStrings */ |
| 1006 | { |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 1007 | assert (plan.offsets.charStringsInfo.offset == c.head - c.start); |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 1008 | CFF1CharStrings *cs = c.start_embed<CFF1CharStrings> (); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1009 | if (unlikely (cs == nullptr)) return false; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 1010 | if (unlikely (!cs->serialize (&c, plan.offsets.charStringsInfo.offSize, plan.subset_charstrings))) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1011 | { |
| 1012 | DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF CharStrings"); |
| 1013 | return false; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /* private dicts & local subrs */ |
| 1018 | assert (plan.offsets.privateDictInfo.offset == c.head - c.start); |
| 1019 | for (unsigned int i = 0; i < acc.privateDicts.len; i++) |
| 1020 | { |
Michiharu Ariza | 1bc710a | 2018-11-02 15:28:01 -0700 | [diff] [blame] | 1021 | if (plan.fdmap.includes (i)) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1022 | { |
Michiharu Ariza | a97ed34 | 2018-08-10 12:55:22 -0700 | [diff] [blame] | 1023 | PrivateDict *pd = c.start_embed<PrivateDict> (); |
| 1024 | if (unlikely (pd == nullptr)) return false; |
Michiharu Ariza | cc52e53 | 2018-09-10 16:27:49 -0700 | [diff] [blame] | 1025 | unsigned int priv_size = plan.fontdicts_mod[plan.fdmap[i]].privateDictInfo.size; |
Michiharu Ariza | 5cde2f5 | 2018-08-17 16:50:13 -0700 | [diff] [blame] | 1026 | bool result; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 1027 | CFFPrivateDict_OpSerializer privSzr (plan.desubroutinize, plan.drop_hints); |
Michiharu Ariza | a97ed34 | 2018-08-10 12:55:22 -0700 | [diff] [blame] | 1028 | /* N.B. local subrs immediately follows its corresponding private dict. i.e., subr offset == private dict size */ |
Michiharu Ariza | 191ca0f | 2018-11-03 22:42:22 -0700 | [diff] [blame] | 1029 | unsigned int subroffset = (plan.offsets.localSubrsInfos[i].size > 0)? priv_size: 0; |
| 1030 | result = pd->serialize (&c, acc.privateDicts[i], privSzr, subroffset); |
Michiharu Ariza | 5cde2f5 | 2018-08-17 16:50:13 -0700 | [diff] [blame] | 1031 | if (unlikely (!result)) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1032 | { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 1033 | DEBUG_MSG (SUBSET, nullptr, "failed to serialize CFF Private Dict[%d]", i); |
| 1034 | return false; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1035 | } |
Michiharu Ariza | 1bc710a | 2018-11-02 15:28:01 -0700 | [diff] [blame] | 1036 | if (plan.offsets.localSubrsInfos[i].size > 0) |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 1037 | { |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 1038 | CFF1Subrs *dest = c.start_embed <CFF1Subrs> (); |
| 1039 | if (unlikely (dest == nullptr)) return false; |
| 1040 | if (unlikely (!dest->serialize (&c, plan.offsets.localSubrsInfos[i].offSize, plan.subset_localsubrs[i]))) |
| 1041 | { |
| 1042 | DEBUG_MSG (SUBSET, nullptr, "failed to serialize local subroutines"); |
| 1043 | return false; |
| 1044 | } |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | } |
| 1048 | |
Michiharu Ariza | cef75ea | 2018-08-17 13:13:18 -0700 | [diff] [blame] | 1049 | assert (c.head == c.end); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1050 | c.end_serialize (); |
| 1051 | |
| 1052 | return true; |
| 1053 | } |
| 1054 | |
| 1055 | static bool |
Michiharu Ariza | e67bb3f | 2018-08-16 00:25:57 -0700 | [diff] [blame] | 1056 | _hb_subset_cff1 (const OT::cff1::accelerator_subset_t &acc, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 1057 | const char *data, |
| 1058 | hb_subset_plan_t *plan, |
| 1059 | hb_blob_t **prime /* OUT */) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1060 | { |
| 1061 | cff_subset_plan cff_plan; |
| 1062 | |
| 1063 | if (unlikely (!cff_plan.create (acc, plan))) |
| 1064 | { |
| 1065 | DEBUG_MSG(SUBSET, nullptr, "Failed to generate a cff subsetting plan."); |
| 1066 | return false; |
| 1067 | } |
| 1068 | |
| 1069 | unsigned int cff_prime_size = cff_plan.get_final_size (); |
| 1070 | char *cff_prime_data = (char *) calloc (1, cff_prime_size); |
| 1071 | |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 1072 | if (unlikely (!_write_cff1 (cff_plan, acc, plan->glyphs, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 1073 | cff_prime_size, cff_prime_data))) { |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1074 | DEBUG_MSG(SUBSET, nullptr, "Failed to write a subset cff."); |
| 1075 | free (cff_prime_data); |
| 1076 | return false; |
| 1077 | } |
| 1078 | |
| 1079 | *prime = hb_blob_create (cff_prime_data, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 1080 | cff_prime_size, |
| 1081 | HB_MEMORY_MODE_READONLY, |
| 1082 | cff_prime_data, |
| 1083 | free); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1084 | return true; |
| 1085 | } |
| 1086 | |
| 1087 | /** |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 1088 | * hb_subset_cff1: |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1089 | * Subsets the CFF table according to a provided plan. |
| 1090 | * |
| 1091 | * Return value: subsetted cff table. |
| 1092 | **/ |
| 1093 | bool |
Michiharu Ariza | fdbfa18 | 2018-08-16 00:13:09 -0700 | [diff] [blame] | 1094 | hb_subset_cff1 (hb_subset_plan_t *plan, |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 1095 | hb_blob_t **prime /* OUT */) |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1096 | { |
Michiharu Ariza | e67bb3f | 2018-08-16 00:25:57 -0700 | [diff] [blame] | 1097 | hb_blob_t *cff_blob = hb_sanitize_context_t().reference_table<CFF::cff1> (plan->source); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1098 | const char *data = hb_blob_get_data(cff_blob, nullptr); |
| 1099 | |
Michiharu Ariza | e67bb3f | 2018-08-16 00:25:57 -0700 | [diff] [blame] | 1100 | OT::cff1::accelerator_subset_t acc; |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1101 | acc.init(plan->source); |
| 1102 | bool result = likely (acc.is_valid ()) && |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 1103 | _hb_subset_cff1 (acc, data, plan, prime); |
Michiharu Ariza | 64c5412 | 2018-08-10 11:07:07 -0700 | [diff] [blame] | 1104 | hb_blob_destroy (cff_blob); |
| 1105 | acc.fini (); |
| 1106 | |
| 1107 | return result; |
| 1108 | } |