Imported Upstream version 2.6.7
[platform/upstream/harfbuzz.git] / src / hb-subset-cff-common.cc
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  * hb_plan_subset_cff_fdselect
43  * Determine an optimal FDSelect format according to a provided plan.
44  *
45  * Return value: FDSelect format, size, and ranges for the most compact subset FDSelect
46  * along with a font index remapping table
47  **/
48
49 bool
50 hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan,
51                              unsigned int fdCount,
52                              const FDSelect &src, /* IN */
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 {
59   subset_fd_count = 0;
60   subset_fdselect_size = 0;
61   subset_fdselect_format = 0;
62   unsigned int num_ranges = 0;
63
64   unsigned int subset_num_glyphs = plan->num_output_glyphs ();
65   if (subset_num_glyphs == 0)
66     return true;
67
68   {
69     /* use hb_set to determine the subset of font dicts */
70     hb_set_t *set = hb_set_create ();
71     if (unlikely (set == &Null (hb_set_t))) return false;
72     hb_codepoint_t prev_fd = CFF_UNDEF_CODE;
73     for (hb_codepoint_t i = 0; i < subset_num_glyphs; i++)
74     {
75       hb_codepoint_t glyph;
76       hb_codepoint_t fd;
77       if (!plan->old_gid_for_new_gid (i, &glyph))
78       {
79         /* fonttools retains FDSelect & font dicts for missing glyphs. do the same */
80         glyph = i;
81       }
82       fd = src.get_fd (glyph);
83       set->add (fd);
84
85       if (fd != prev_fd)
86       {
87         num_ranges++;
88         prev_fd = fd;
89         code_pair_t pair = { fd, i };
90         fdselect_ranges.push (pair);
91       }
92     }
93
94     subset_fd_count = set->get_population ();
95     if (subset_fd_count == fdCount)
96     {
97       /* all font dicts belong to the subset. no need to subset FDSelect & FDArray */
98       fdmap.identity (fdCount);
99       hb_set_destroy (set);
100     }
101     else
102     {
103       /* create a fdmap */
104       fdmap.reset ();
105
106       hb_codepoint_t fd = CFF_UNDEF_CODE;
107       while (set->next (&fd))
108         fdmap.add (fd);
109       hb_set_destroy (set);
110       if (unlikely (fdmap.get_population () != subset_fd_count))
111         return false;
112     }
113
114     /* update each font dict index stored as "code" in fdselect_ranges */
115     for (unsigned int i = 0; i < fdselect_ranges.length; i++)
116       fdselect_ranges[i].code = fdmap[fdselect_ranges[i].code];
117   }
118
119   /* determine which FDSelect format is most compact */
120   if (subset_fd_count > 0xFF)
121   {
122     if (unlikely (src.format != 4))
123       return false;
124     subset_fdselect_format = 4;
125     subset_fdselect_size = FDSelect::min_size + FDSelect4::min_size + FDSelect4_Range::static_size * num_ranges + HBUINT32::static_size;
126   }
127   else
128   {
129 #if CFF_SERIALIZE_FDSELECT_0
130     unsigned int format0_size = FDSelect::min_size + FDSelect0::min_size + HBUINT8::static_size * subset_num_glyphs;
131 #endif
132     unsigned int format3_size = FDSelect::min_size + FDSelect3::min_size + FDSelect3_Range::static_size * num_ranges + HBUINT16::static_size;
133
134 #if CFF_SERIALIZE_FDSELECT_0
135     if (format0_size <= format3_size)
136     {
137       // subset_fdselect_format = 0;
138       subset_fdselect_size = format0_size;
139     }
140     else
141 #endif
142     {
143       subset_fdselect_format = 3;
144       subset_fdselect_size = format3_size;
145     }
146   }
147
148   return true;
149 }
150
151 template <typename FDSELECT3_4>
152 static inline bool
153 serialize_fdselect_3_4 (hb_serialize_context_t *c,
154                         const unsigned int num_glyphs,
155                         const FDSelect &src,
156                         unsigned int size,
157                         const hb_vector_t<code_pair_t> &fdselect_ranges)
158 {
159   TRACE_SERIALIZE (this);
160   FDSELECT3_4 *p = c->allocate_size<FDSELECT3_4> (size);
161   if (unlikely (!p)) return_trace (false);
162   p->nRanges () = fdselect_ranges.length;
163   for (unsigned int i = 0; i < fdselect_ranges.length; i++)
164   {
165     p->ranges[i].first = fdselect_ranges[i].glyph;
166     p->ranges[i].fd = fdselect_ranges[i].code;
167   }
168   p->sentinel () = num_glyphs;
169   return_trace (true);
170 }
171
172 /**
173  * hb_serialize_cff_fdselect
174  * Serialize a subset FDSelect format planned above.
175  **/
176 bool
177 hb_serialize_cff_fdselect (hb_serialize_context_t *c,
178                            const unsigned int num_glyphs,
179                            const FDSelect &src,
180                            unsigned int fd_count,
181                            unsigned int fdselect_format,
182                            unsigned int size,
183                            const hb_vector_t<code_pair_t> &fdselect_ranges)
184 {
185   TRACE_SERIALIZE (this);
186   FDSelect *p = c->allocate_min<FDSelect> ();
187   if (unlikely (!p)) return_trace (false);
188   p->format = fdselect_format;
189   size -= FDSelect::min_size;
190
191   switch (fdselect_format)
192   {
193 #if CFF_SERIALIZE_FDSELECT_0
194   case 0:
195   {
196     FDSelect0 *p = c->allocate_size<FDSelect0> (size);
197     if (unlikely (!p)) return_trace (false);
198     unsigned int range_index = 0;
199     unsigned int fd = fdselect_ranges[range_index++].code;
200     for (unsigned int i = 0; i < num_glyphs; i++)
201     {
202       if ((range_index < fdselect_ranges.len) &&
203           (i >= fdselect_ranges[range_index].glyph))
204       {
205         fd = fdselect_ranges[range_index++].code;
206       }
207       p->fds[i] = fd;
208     }
209     return_trace (true);
210   }
211 #endif /* CFF_SERIALIZE_FDSELECT_0 */
212
213   case 3:
214     return serialize_fdselect_3_4<FDSelect3> (c, num_glyphs, src,
215                                               size, fdselect_ranges);
216
217   case 4:
218     return serialize_fdselect_3_4<FDSelect4> (c, num_glyphs, src,
219                                               size, fdselect_ranges);
220
221   default:
222     return_trace (false);
223   }
224 }
225
226
227 #endif