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