/work/workdir/UnpackedTarball/harfbuzz/src/hb-subset-cff-common.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2018 Adobe Inc. |
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 | | |
27 | | #include "hb.hh" |
28 | | |
29 | | #ifndef HB_NO_SUBSET_CFF |
30 | | |
31 | | #include "hb-ot-cff-common.hh" |
32 | | #include "hb-ot-cff2-table.hh" |
33 | | #include "hb-subset-cff-common.hh" |
34 | | |
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 |
38 | | |
39 | | using namespace CFF; |
40 | | |
41 | | |
42 | | /* Determine an optimal FDSelect format according to a provided plan. |
43 | | * |
44 | | * Return value: FDSelect format, size, and ranges for the most compact subset FDSelect |
45 | | * along with a font index remapping table |
46 | | */ |
47 | | |
48 | | bool |
49 | | hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, |
50 | | unsigned int fdCount, |
51 | | const FDSelect &src, /* IN */ |
52 | | unsigned int src_num_glyphs, |
53 | | unsigned int &subset_fd_count /* OUT */, |
54 | | unsigned int &subset_fdselect_size /* OUT */, |
55 | | unsigned int &subset_fdselect_format /* OUT */, |
56 | | hb_vector_t<code_pair_t> &fdselect_ranges /* OUT */, |
57 | | hb_inc_bimap_t &fdmap /* OUT */) |
58 | 0 | { |
59 | 0 | subset_fd_count = 0; |
60 | 0 | subset_fdselect_size = 0; |
61 | 0 | subset_fdselect_format = 0; |
62 | 0 | unsigned int num_ranges = 0; |
63 | |
|
64 | 0 | unsigned int subset_num_glyphs = plan->num_output_glyphs (); |
65 | 0 | if (subset_num_glyphs == 0) |
66 | 0 | return true; |
67 | | |
68 | 0 | { |
69 | | /* use hb_set to determine the subset of font dicts */ |
70 | 0 | hb_set_t set; |
71 | 0 | hb_codepoint_t prev_fd = CFF_UNDEF_CODE; |
72 | 0 | auto it = hb_iter (plan->new_to_old_gid_list); |
73 | 0 | auto _ = *it; |
74 | 0 | for (hb_codepoint_t gid = 0; gid < subset_num_glyphs; gid++) |
75 | 0 | { |
76 | 0 | hb_codepoint_t old_glyph; |
77 | 0 | if (gid == _.first) |
78 | 0 | { |
79 | 0 | old_glyph = _.second; |
80 | 0 | _ = *++it; |
81 | 0 | } |
82 | 0 | else |
83 | 0 | { |
84 | | /* fonttools retains FDSelect & font dicts for missing glyphs. do the same */ |
85 | 0 | old_glyph = gid; |
86 | 0 | } |
87 | | /* FDSelect may only cover the CharStrings glyphs; see subset_get_fd (). */ |
88 | 0 | hb_pair_t<unsigned, hb_codepoint_t> fd_range = |
89 | 0 | likely (old_glyph < src_num_glyphs) ? |
90 | 0 | src.get_fd_range (old_glyph) : |
91 | 0 | hb_pair_t<unsigned, hb_codepoint_t> {0, HB_CODEPOINT_INVALID}; |
92 | 0 | if (old_glyph < src_num_glyphs) |
93 | 0 | fd_range.second = hb_min (fd_range.second, src_num_glyphs); |
94 | 0 | unsigned fd = fd_range.first; |
95 | |
|
96 | 0 | if (fd != prev_fd) |
97 | 0 | { |
98 | 0 | set.add (fd); |
99 | 0 | num_ranges++; |
100 | 0 | prev_fd = fd; |
101 | 0 | fdselect_ranges.push (code_pair_t { fd, gid }); |
102 | |
|
103 | 0 | if (gid == old_glyph) |
104 | 0 | gid = hb_min (_.first - 1, fd_range.second - 1); |
105 | 0 | } |
106 | 0 | } |
107 | |
|
108 | 0 | subset_fd_count = set.get_population (); |
109 | 0 | if (subset_fd_count == fdCount) |
110 | 0 | { |
111 | | /* all font dicts belong to the subset. no need to subset FDSelect & FDArray */ |
112 | 0 | fdmap.identity (fdCount); |
113 | 0 | } |
114 | 0 | else |
115 | 0 | { |
116 | | /* create a fdmap */ |
117 | 0 | fdmap.reset (); |
118 | |
|
119 | 0 | hb_codepoint_t fd = CFF_UNDEF_CODE; |
120 | 0 | while (set.next (&fd)) |
121 | 0 | fdmap.add (fd); |
122 | 0 | if (unlikely (fdmap.get_population () != subset_fd_count)) |
123 | 0 | return false; |
124 | 0 | } |
125 | | |
126 | | /* update each font dict index stored as "code" in fdselect_ranges */ |
127 | 0 | for (unsigned int i = 0; i < fdselect_ranges.length; i++) |
128 | 0 | fdselect_ranges[i].code = fdmap[fdselect_ranges[i].code]; |
129 | 0 | } |
130 | | |
131 | | /* determine which FDSelect format is most compact */ |
132 | 0 | if (subset_fd_count > 0xFF) |
133 | 0 | { |
134 | 0 | if (unlikely (src.format != 4)) |
135 | 0 | return false; |
136 | 0 | subset_fdselect_format = 4; |
137 | 0 | subset_fdselect_size = FDSelect::min_size + FDSelect4::min_size + FDSelect4_Range::static_size * num_ranges + HBUINT32::static_size; |
138 | 0 | } |
139 | 0 | else |
140 | 0 | { |
141 | | #if CFF_SERIALIZE_FDSELECT_0 |
142 | | unsigned int format0_size = FDSelect::min_size + FDSelect0::min_size + HBUINT8::static_size * subset_num_glyphs; |
143 | | #endif |
144 | 0 | unsigned int format3_size = FDSelect::min_size + FDSelect3::min_size + FDSelect3_Range::static_size * num_ranges + HBUINT16::static_size; |
145 | |
|
146 | | #if CFF_SERIALIZE_FDSELECT_0 |
147 | | if (format0_size <= format3_size) |
148 | | { |
149 | | // subset_fdselect_format = 0; |
150 | | subset_fdselect_size = format0_size; |
151 | | } |
152 | | else |
153 | | #endif |
154 | 0 | { |
155 | 0 | subset_fdselect_format = 3; |
156 | 0 | subset_fdselect_size = format3_size; |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | 0 | return true; |
161 | 0 | } |
162 | | |
163 | | template <typename FDSELECT3_4> |
164 | | static inline bool |
165 | | serialize_fdselect_3_4 (hb_serialize_context_t *c, |
166 | | const unsigned int num_glyphs, |
167 | | const FDSelect &src, |
168 | | unsigned int size, |
169 | | const hb_vector_t<code_pair_t> &fdselect_ranges) |
170 | 0 | { |
171 | 0 | TRACE_SERIALIZE (this); |
172 | 0 | FDSELECT3_4 *p = c->allocate_size<FDSELECT3_4> (size); |
173 | 0 | if (unlikely (!p)) return_trace (false); |
174 | 0 | p->nRanges () = fdselect_ranges.length; |
175 | 0 | for (unsigned int i = 0; i < fdselect_ranges.length; i++) |
176 | 0 | { |
177 | 0 | p->ranges[i].first = fdselect_ranges[i].glyph; |
178 | 0 | p->ranges[i].fd = fdselect_ranges[i].code; |
179 | 0 | } |
180 | 0 | p->sentinel () = num_glyphs; |
181 | 0 | return_trace (true); |
182 | 0 | } Unexecuted instantiation: hb-subset-cff-common.cc:bool serialize_fdselect_3_4<CFF::FDSelect3_4<OT::NumType<true, unsigned short, 2u>, OT::NumType<true, unsigned char, 1u> > >(hb_serialize_context_t*, unsigned int, CFF::FDSelect const&, unsigned int, hb_vector_t<CFF::code_pair_t, false> const&) Unexecuted instantiation: hb-subset-cff-common.cc:bool serialize_fdselect_3_4<CFF::FDSelect3_4<OT::NumType<true, unsigned int, 4u>, OT::NumType<true, unsigned short, 2u> > >(hb_serialize_context_t*, unsigned int, CFF::FDSelect const&, unsigned int, hb_vector_t<CFF::code_pair_t, false> const&) |
183 | | |
184 | | /* Serialize a subset FDSelect format planned above. */ |
185 | | bool |
186 | | hb_serialize_cff_fdselect (hb_serialize_context_t *c, |
187 | | const unsigned int num_glyphs, |
188 | | const FDSelect &src, |
189 | | unsigned int fd_count, |
190 | | unsigned int fdselect_format, |
191 | | unsigned int size, |
192 | | const hb_vector_t<code_pair_t> &fdselect_ranges) |
193 | 0 | { |
194 | 0 | TRACE_SERIALIZE (this); |
195 | 0 | FDSelect *p = c->allocate_min<FDSelect> (); |
196 | 0 | if (unlikely (!p)) return_trace (false); |
197 | 0 | p->format = fdselect_format; |
198 | 0 | size -= FDSelect::min_size; |
199 | |
|
200 | 0 | switch (fdselect_format) |
201 | 0 | { |
202 | | #if CFF_SERIALIZE_FDSELECT_0 |
203 | | case 0: |
204 | | { |
205 | | FDSelect0 *p = c->allocate_size<FDSelect0> (size); |
206 | | if (unlikely (!p)) return_trace (false); |
207 | | unsigned int range_index = 0; |
208 | | unsigned int fd = fdselect_ranges[range_index++].code; |
209 | | for (unsigned int i = 0; i < num_glyphs; i++) |
210 | | { |
211 | | if ((range_index < fdselect_ranges.len) && |
212 | | (i >= fdselect_ranges[range_index].glyph)) |
213 | | { |
214 | | fd = fdselect_ranges[range_index++].code; |
215 | | } |
216 | | p->fds[i] = fd; |
217 | | } |
218 | | return_trace (true); |
219 | | } |
220 | | #endif /* CFF_SERIALIZE_FDSELECT_0 */ |
221 | | |
222 | 0 | case 3: |
223 | 0 | return serialize_fdselect_3_4<FDSelect3> (c, num_glyphs, src, |
224 | 0 | size, fdselect_ranges); |
225 | | |
226 | 0 | case 4: |
227 | 0 | return serialize_fdselect_3_4<FDSelect4> (c, num_glyphs, src, |
228 | 0 | size, fdselect_ranges); |
229 | | |
230 | 0 | default: |
231 | 0 | return_trace (false); |
232 | 0 | } |
233 | 0 | } |
234 | | |
235 | | |
236 | | #endif |