2 * Copyright © 2018 Adobe Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Adobe Author(s): Michiharu Ariza
29 #ifndef HB_NO_SUBSET_CFF
31 #include "hb-ot-cff-common.hh"
32 #include "hb-ot-cff2-table.hh"
33 #include "hb-subset-cff-common.hh"
35 /* Disable FDSelect format 0 for compatibility with fonttools which doesn't seem choose it.
36 * Rarely any/much smaller than format 3 anyway. */
37 #define CFF_SERIALIZE_FDSELECT_0 0
42 /* Determine an optimal FDSelect format according to a provided plan.
44 * Return value: FDSelect format, size, and ranges for the most compact subset FDSelect
45 * along with a font index remapping table
49 hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan,
51 const FDSelect &src, /* IN */
52 unsigned int &subset_fd_count /* OUT */,
53 unsigned int &subset_fdselect_size /* OUT */,
54 unsigned int &subset_fdselect_format /* OUT */,
55 hb_vector_t<code_pair_t> &fdselect_ranges /* OUT */,
56 hb_inc_bimap_t &fdmap /* OUT */)
59 subset_fdselect_size = 0;
60 subset_fdselect_format = 0;
61 unsigned int num_ranges = 0;
63 unsigned int subset_num_glyphs = plan->num_output_glyphs ();
64 if (subset_num_glyphs == 0)
68 /* use hb_set to determine the subset of font dicts */
70 hb_codepoint_t prev_fd = CFF_UNDEF_CODE;
71 hb_pair_t<unsigned, hb_codepoint_t> last_range {0, 0};
72 auto it = hb_iter (plan->new_to_old_gid_list);
74 for (hb_codepoint_t gid = 0; gid < subset_num_glyphs; gid++)
76 hb_codepoint_t old_glyph;
84 /* fonttools retains FDSelect & font dicts for missing glyphs. do the same */
87 if (old_glyph >= last_range.second)
88 last_range = src.get_fd_range (old_glyph);
89 unsigned fd = last_range.first;
96 fdselect_ranges.push (code_pair_t { fd, gid });
99 gid = hb_min (_.first - 1, last_range.second - 1);
103 subset_fd_count = set.get_population ();
104 if (subset_fd_count == fdCount)
106 /* all font dicts belong to the subset. no need to subset FDSelect & FDArray */
107 fdmap.identity (fdCount);
114 hb_codepoint_t fd = CFF_UNDEF_CODE;
115 while (set.next (&fd))
117 if (unlikely (fdmap.get_population () != subset_fd_count))
121 /* update each font dict index stored as "code" in fdselect_ranges */
122 for (unsigned int i = 0; i < fdselect_ranges.length; i++)
123 fdselect_ranges[i].code = fdmap[fdselect_ranges[i].code];
126 /* determine which FDSelect format is most compact */
127 if (subset_fd_count > 0xFF)
129 if (unlikely (src.format != 4))
131 subset_fdselect_format = 4;
132 subset_fdselect_size = FDSelect::min_size + FDSelect4::min_size + FDSelect4_Range::static_size * num_ranges + HBUINT32::static_size;
136 #if CFF_SERIALIZE_FDSELECT_0
137 unsigned int format0_size = FDSelect::min_size + FDSelect0::min_size + HBUINT8::static_size * subset_num_glyphs;
139 unsigned int format3_size = FDSelect::min_size + FDSelect3::min_size + FDSelect3_Range::static_size * num_ranges + HBUINT16::static_size;
141 #if CFF_SERIALIZE_FDSELECT_0
142 if (format0_size <= format3_size)
144 // subset_fdselect_format = 0;
145 subset_fdselect_size = format0_size;
150 subset_fdselect_format = 3;
151 subset_fdselect_size = format3_size;
158 template <typename FDSELECT3_4>
160 serialize_fdselect_3_4 (hb_serialize_context_t *c,
161 const unsigned int num_glyphs,
164 const hb_vector_t<code_pair_t> &fdselect_ranges)
166 TRACE_SERIALIZE (this);
167 FDSELECT3_4 *p = c->allocate_size<FDSELECT3_4> (size);
168 if (unlikely (!p)) return_trace (false);
169 p->nRanges () = fdselect_ranges.length;
170 for (unsigned int i = 0; i < fdselect_ranges.length; i++)
172 p->ranges[i].first = fdselect_ranges[i].glyph;
173 p->ranges[i].fd = fdselect_ranges[i].code;
175 p->sentinel () = num_glyphs;
179 /* Serialize a subset FDSelect format planned above. */
181 hb_serialize_cff_fdselect (hb_serialize_context_t *c,
182 const unsigned int num_glyphs,
184 unsigned int fd_count,
185 unsigned int fdselect_format,
187 const hb_vector_t<code_pair_t> &fdselect_ranges)
189 TRACE_SERIALIZE (this);
190 FDSelect *p = c->allocate_min<FDSelect> ();
191 if (unlikely (!p)) return_trace (false);
192 p->format = fdselect_format;
193 size -= FDSelect::min_size;
195 switch (fdselect_format)
197 #if CFF_SERIALIZE_FDSELECT_0
200 FDSelect0 *p = c->allocate_size<FDSelect0> (size);
201 if (unlikely (!p)) return_trace (false);
202 unsigned int range_index = 0;
203 unsigned int fd = fdselect_ranges[range_index++].code;
204 for (unsigned int i = 0; i < num_glyphs; i++)
206 if ((range_index < fdselect_ranges.len) &&
207 (i >= fdselect_ranges[range_index].glyph))
209 fd = fdselect_ranges[range_index++].code;
215 #endif /* CFF_SERIALIZE_FDSELECT_0 */
218 return serialize_fdselect_3_4<FDSelect3> (c, num_glyphs, src,
219 size, fdselect_ranges);
222 return serialize_fdselect_3_4<FDSelect4> (c, num_glyphs, src,
223 size, fdselect_ranges);
226 return_trace (false);