2 * Copyright © 2018 Adobe Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Adobe Author(s): Michiharu Ariza
27 #include "hb-ot-cff-common.hh"
28 #include "hb-ot-cff2-table.hh"
29 #include "hb-subset-cff-common.hh"
31 /* Disable FDSelect format 0 for compatibility with fonttools which doesn't seem choose it.
32 * Rarely any/much smaller than format 3 anyway. */
33 #define CFF_SERIALIZE_FDSELECT_0 0
38 * hb_plan_subset_cff_fdselect
39 * Determine an optimal FDSelect format according to a provided plan.
41 * Return value: FDSelect format, size, and ranges for the most compact subset FDSelect
42 * along with a font index remapping table
46 hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan,
48 const FDSelect &src, /* IN */
49 unsigned int &subset_fd_count /* OUT */,
50 unsigned int &subset_fdselect_size /* OUT */,
51 unsigned int &subset_fdselect_format /* OUT */,
52 hb_vector_t<code_pair_t> &fdselect_ranges /* OUT */,
53 remap_t &fdmap /* OUT */)
56 subset_fdselect_size = 0;
57 subset_fdselect_format = 0;
58 unsigned int num_ranges = 0;
60 unsigned int subset_num_glyphs = plan->num_output_glyphs ();
61 if (subset_num_glyphs == 0)
65 /* use hb_set to determine the subset of font dicts */
66 hb_set_t *set = hb_set_create ();
67 if (set == &Null (hb_set_t))
69 hb_codepoint_t prev_fd = CFF_UNDEF_CODE;
70 for (hb_codepoint_t i = 0; i < subset_num_glyphs; i++)
74 if (!plan->old_gid_for_new_gid (i, &glyph))
76 /* fonttools retains FDSelect & font dicts for missing glyphs. do the same */
79 fd = src.get_fd (glyph);
86 code_pair_t pair = { fd, i };
87 fdselect_ranges.push (pair);
91 subset_fd_count = set->get_population ();
92 if (subset_fd_count == fdCount)
94 /* all font dicts belong to the subset. no need to subset FDSelect & FDArray */
95 fdmap.identity (fdCount);
101 if (!fdmap.reset (fdCount))
103 hb_set_destroy (set);
107 hb_codepoint_t fd = CFF_UNDEF_CODE;
108 while (set->next (&fd))
110 hb_set_destroy (set);
111 if (unlikely (fdmap.get_count () != subset_fd_count))
115 /* update each font dict index stored as "code" in fdselect_ranges */
116 for (unsigned int i = 0; i < fdselect_ranges.length; i++)
117 fdselect_ranges[i].code = fdmap[fdselect_ranges[i].code];
120 /* determine which FDSelect format is most compact */
121 if (subset_fd_count > 0xFF)
123 if (unlikely (src.format != 4))
125 subset_fdselect_format = 4;
126 subset_fdselect_size = FDSelect::min_size + FDSelect4::min_size + FDSelect4_Range::static_size * num_ranges + HBUINT32::static_size;
130 #if CFF_SERIALIZE_FDSELECT_0
131 unsigned int format0_size = FDSelect::min_size + FDSelect0::min_size + HBUINT8::static_size * subset_num_glyphs;
133 unsigned int format3_size = FDSelect::min_size + FDSelect3::min_size + FDSelect3_Range::static_size * num_ranges + HBUINT16::static_size;
135 #if CFF_SERIALIZE_FDSELECT_0
136 if (format0_size <= format3_size)
138 // subset_fdselect_format = 0;
139 subset_fdselect_size = format0_size;
144 subset_fdselect_format = 3;
145 subset_fdselect_size = format3_size;
152 template <typename FDSELECT3_4>
154 serialize_fdselect_3_4 (hb_serialize_context_t *c,
155 const unsigned int num_glyphs,
158 const hb_vector_t<code_pair_t> &fdselect_ranges)
160 TRACE_SERIALIZE (this);
161 FDSELECT3_4 *p = c->allocate_size<FDSELECT3_4> (size);
162 if (unlikely (p == nullptr)) return_trace (false);
163 p->nRanges ().set (fdselect_ranges.length);
164 for (unsigned int i = 0; i < fdselect_ranges.length; i++)
166 p->ranges[i].first.set (fdselect_ranges[i].glyph);
167 p->ranges[i].fd.set (fdselect_ranges[i].code);
169 p->sentinel().set (num_glyphs);
174 * hb_serialize_cff_fdselect
175 * Serialize a subset FDSelect format planned above.
178 hb_serialize_cff_fdselect (hb_serialize_context_t *c,
179 const unsigned int num_glyphs,
181 unsigned int fd_count,
182 unsigned int fdselect_format,
184 const hb_vector_t<code_pair_t> &fdselect_ranges)
186 TRACE_SERIALIZE (this);
187 FDSelect *p = c->allocate_min<FDSelect> ();
188 if (unlikely (p == nullptr)) return_trace (false);
189 p->format.set (fdselect_format);
190 size -= FDSelect::min_size;
192 switch (fdselect_format)
194 #if CFF_SERIALIZE_FDSELECT_0
197 FDSelect0 *p = c->allocate_size<FDSelect0> (size);
198 if (unlikely (p == nullptr)) return_trace (false);
199 unsigned int range_index = 0;
200 unsigned int fd = fdselect_ranges[range_index++].code;
201 for (unsigned int i = 0; i < num_glyphs; i++)
203 if ((range_index < fdselect_ranges.len) &&
204 (i >= fdselect_ranges[range_index].glyph))
206 fd = fdselect_ranges[range_index++].code;
212 #endif /* CFF_SERIALIZE_FDSELECT_0 */
215 return serialize_fdselect_3_4<FDSelect3> (c,
222 return serialize_fdselect_3_4<FDSelect4> (c,