/src/harfbuzz/src/hb-face-builder.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2009 Red Hat, Inc. |
3 | | * Copyright © 2012 Google, Inc. |
4 | | * |
5 | | * This is part of HarfBuzz, a text shaping library. |
6 | | * |
7 | | * Permission is hereby granted, without written agreement and without |
8 | | * license or royalty fees, to use, copy, modify, and distribute this |
9 | | * software and its documentation for any purpose, provided that the |
10 | | * above copyright notice and the following two paragraphs appear in |
11 | | * all copies of this software. |
12 | | * |
13 | | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
14 | | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
15 | | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
16 | | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
17 | | * DAMAGE. |
18 | | * |
19 | | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
20 | | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
21 | | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
22 | | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
23 | | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
24 | | * |
25 | | * Red Hat Author(s): Behdad Esfahbod |
26 | | * Google Author(s): Behdad Esfahbod |
27 | | */ |
28 | | |
29 | | #include "hb.hh" |
30 | | |
31 | | #include "hb-face.hh" |
32 | | |
33 | | #include "hb-map.hh" |
34 | | #include "hb-open-file.hh" |
35 | | #include "hb-serialize.hh" |
36 | | |
37 | | |
38 | | /* |
39 | | * face-builder: A face that has add_table(). |
40 | | */ |
41 | | |
42 | | struct face_table_info_t |
43 | | { |
44 | | hb_blob_t* data; |
45 | | unsigned order; |
46 | | }; |
47 | | |
48 | | struct hb_face_builder_data_t |
49 | | { |
50 | | hb_hashmap_t<hb_tag_t, face_table_info_t> tables; |
51 | | /* Table tags in sorted order, built on first use and dropped when a |
52 | | * table is added, so paging through get_table_tags() sorts once. |
53 | | * Holds tables.get_population () tags. */ |
54 | | hb_atomic_t<hb_tag_t *> sorted_tags; |
55 | | }; |
56 | | |
57 | | static int |
58 | | compare_tags (const void *pa, const void *pb) |
59 | 0 | { |
60 | 0 | hb_tag_t a = * (const hb_tag_t *) pa; |
61 | 0 | hb_tag_t b = * (const hb_tag_t *) pb; |
62 | 0 | return (a > b) - (a < b); |
63 | 0 | } |
64 | | |
65 | | static int compare_entries (const void* pa, const void* pb) |
66 | 0 | { |
67 | 0 | const auto& a = * (const hb_pair_t<hb_tag_t, face_table_info_t> *) pa; |
68 | 0 | const auto& b = * (const hb_pair_t<hb_tag_t, face_table_info_t> *) pb; |
69 | | |
70 | | /* Order by blob size first (smallest to largest) and then table tag */ |
71 | |
|
72 | 0 | if (a.second.order != b.second.order) |
73 | 0 | return a.second.order < b.second.order ? -1 : +1; |
74 | | |
75 | 0 | if (a.second.data->length != b.second.data->length) |
76 | 0 | return a.second.data->length < b.second.data->length ? -1 : +1; |
77 | | |
78 | 0 | return a.first < b.first ? -1 : a.first == b.first ? 0 : +1; |
79 | 0 | } |
80 | | |
81 | | static hb_face_builder_data_t * |
82 | | _hb_face_builder_data_create () |
83 | 0 | { |
84 | 0 | hb_face_builder_data_t *data = (hb_face_builder_data_t *) hb_calloc (1, sizeof (hb_face_builder_data_t)); |
85 | 0 | if (unlikely (!data)) |
86 | 0 | return nullptr; |
87 | | |
88 | 0 | data->tables.init (); |
89 | 0 | data->sorted_tags.init (); |
90 | |
|
91 | 0 | return data; |
92 | 0 | } |
93 | | |
94 | | static void |
95 | | _hb_face_builder_data_destroy (void *user_data) |
96 | 0 | { |
97 | 0 | hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data; |
98 | |
|
99 | 0 | for (auto info : data->tables.values()) |
100 | 0 | hb_blob_destroy (info.data); |
101 | |
|
102 | 0 | data->tables.fini (); |
103 | 0 | hb_free (data->sorted_tags.get_relaxed ()); |
104 | |
|
105 | 0 | hb_free (data); |
106 | 0 | } |
107 | | |
108 | | static hb_blob_t * |
109 | | _hb_face_builder_data_reference_blob (hb_face_builder_data_t *data) |
110 | 0 | { |
111 | |
|
112 | 0 | unsigned int table_count = data->tables.get_population (); |
113 | 0 | unsigned int face_length = table_count * 16 + 12; |
114 | |
|
115 | 0 | for (auto info : data->tables.values()) |
116 | 0 | if (unlikely (hb_unsigned_add_overflows (face_length, |
117 | 0 | hb_ceil_to_4 (hb_blob_get_length (info.data)), |
118 | 0 | &face_length))) |
119 | 0 | return nullptr; |
120 | | |
121 | | /* hb_blob_create() refuses such a length; do not assemble the font |
122 | | * only to throw it away. Tables referenced from a font file can |
123 | | * overlap in it, so the sum is not bounded by the size of that file. */ |
124 | 0 | if (unlikely (face_length >= 1u << 31)) |
125 | 0 | return nullptr; |
126 | | |
127 | 0 | char *buf = (char *) hb_malloc (face_length); |
128 | 0 | if (unlikely (!buf)) |
129 | 0 | return nullptr; |
130 | | |
131 | 0 | hb_serialize_context_t c (buf, face_length); |
132 | 0 | c.propagate_error (data->tables); |
133 | 0 | OT::OpenTypeFontFile *f = c.start_serialize<OT::OpenTypeFontFile> (); |
134 | |
|
135 | 0 | bool is_cff = (data->tables.has (HB_TAG ('C','F','F',' ')) |
136 | 0 | || data->tables.has (HB_TAG ('C','F','F','2'))); |
137 | 0 | hb_tag_t sfnt_tag = is_cff ? OT::OpenTypeFontFile::CFFTag : OT::OpenTypeFontFile::TrueTypeTag; |
138 | | |
139 | | // Sort the tags so that produced face is deterministic. |
140 | 0 | hb_vector_t<hb_pair_t <hb_tag_t, face_table_info_t>> sorted_entries; |
141 | 0 | data->tables.iter () | hb_sink (sorted_entries); |
142 | 0 | if (unlikely (sorted_entries.in_error ())) |
143 | 0 | { |
144 | 0 | hb_free (buf); |
145 | 0 | return nullptr; |
146 | 0 | } |
147 | | |
148 | 0 | sorted_entries.qsort (compare_entries); |
149 | |
|
150 | 0 | bool ret = f->serialize_single (&c, |
151 | 0 | sfnt_tag, |
152 | 0 | + sorted_entries.iter() |
153 | 0 | | hb_map ([&] (hb_pair_t<hb_tag_t, face_table_info_t> _) { |
154 | 0 | return hb_pair_t<hb_tag_t, hb_blob_t*> (_.first, _.second.data); |
155 | 0 | })); |
156 | |
|
157 | 0 | c.end_serialize (); |
158 | |
|
159 | 0 | if (unlikely (!ret)) |
160 | 0 | { |
161 | 0 | hb_free (buf); |
162 | 0 | return nullptr; |
163 | 0 | } |
164 | | |
165 | 0 | return hb_blob_create (buf, face_length, HB_MEMORY_MODE_WRITABLE, buf, hb_free); |
166 | 0 | } |
167 | | |
168 | | static hb_blob_t * |
169 | | _hb_face_builder_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data) |
170 | 0 | { |
171 | 0 | hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data; |
172 | |
|
173 | 0 | if (!tag) |
174 | 0 | return _hb_face_builder_data_reference_blob (data); |
175 | | |
176 | 0 | return hb_blob_reference (data->tables[tag].data); |
177 | 0 | } |
178 | | |
179 | | static unsigned |
180 | | _hb_face_builder_get_table_tags (const hb_face_t *face HB_UNUSED, |
181 | | unsigned int start_offset, |
182 | | unsigned int *table_count, |
183 | | hb_tag_t *table_tags, |
184 | | void *user_data) |
185 | 0 | { |
186 | 0 | hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data; |
187 | |
|
188 | 0 | unsigned population = data->tables.get_population (); |
189 | |
|
190 | 0 | if (!table_count) |
191 | 0 | return population; |
192 | | |
193 | 0 | if (unlikely (start_offset >= population)) |
194 | 0 | { |
195 | 0 | *table_count = 0; |
196 | 0 | return population; |
197 | 0 | } |
198 | | |
199 | 0 | hb_tag_t *sorted_tags = data->sorted_tags.get_acquire (); |
200 | 0 | if (!sorted_tags) |
201 | 0 | { |
202 | 0 | sorted_tags = (hb_tag_t *) hb_malloc (population * sizeof (hb_tag_t)); |
203 | 0 | if (unlikely (!sorted_tags)) |
204 | 0 | { |
205 | 0 | *table_count = 0; |
206 | 0 | return population; |
207 | 0 | } |
208 | 0 | unsigned i = 0; |
209 | 0 | for (hb_tag_t tag : data->tables.keys ()) |
210 | 0 | sorted_tags[i++] = tag; |
211 | 0 | hb_qsort (sorted_tags, population, sizeof (hb_tag_t), compare_tags); |
212 | |
|
213 | 0 | if (unlikely (!data->sorted_tags.cmpexch (nullptr, sorted_tags))) |
214 | 0 | { |
215 | 0 | hb_free (sorted_tags); |
216 | 0 | sorted_tags = data->sorted_tags.get_acquire (); |
217 | 0 | } |
218 | 0 | } |
219 | | |
220 | 0 | auto array = hb_array (sorted_tags, population).sub_array (start_offset, table_count); |
221 | 0 | if (table_tags) |
222 | 0 | { |
223 | 0 | auto out = hb_array (table_tags, *table_count); |
224 | |
|
225 | 0 | + array.iter () |
226 | 0 | | hb_sink (out) |
227 | 0 | ; |
228 | 0 | } |
229 | |
|
230 | 0 | return population; |
231 | 0 | } |
232 | | |
233 | | |
234 | | /** |
235 | | * hb_face_builder_create: |
236 | | * |
237 | | * Creates a #hb_face_t that can be used with hb_face_builder_add_table(). |
238 | | * After tables are added to the face, it can be compiled to a binary |
239 | | * font file by calling hb_face_reference_blob(). |
240 | | * |
241 | | * Return value: (transfer full): New face, or hb_face_get_empty() on |
242 | | * allocation failure. |
243 | | * |
244 | | * Since: 1.9.0 |
245 | | **/ |
246 | | hb_face_t * |
247 | | hb_face_builder_create () |
248 | 0 | { |
249 | 0 | hb_face_builder_data_t *data = _hb_face_builder_data_create (); |
250 | 0 | if (unlikely (!data)) return hb_face_get_empty (); |
251 | | |
252 | 0 | hb_face_t *face = hb_face_create_for_tables (_hb_face_builder_reference_table, |
253 | 0 | data, |
254 | 0 | _hb_face_builder_data_destroy); |
255 | |
|
256 | 0 | hb_face_set_get_table_tags_func (face, |
257 | 0 | _hb_face_builder_get_table_tags, |
258 | 0 | data, |
259 | 0 | nullptr); |
260 | |
|
261 | 0 | return face; |
262 | 0 | } |
263 | | |
264 | | /** |
265 | | * hb_face_builder_add_table: |
266 | | * @face: A face object created with hb_face_builder_create() |
267 | | * @tag: The #hb_tag_t of the table to add |
268 | | * @blob: The blob containing the table data to add |
269 | | * |
270 | | * Add table for @tag with data provided by @blob to the face. @face must |
271 | | * be created using hb_face_builder_create(). A face can hold at most |
272 | | * 65535 tables; adding more fails. |
273 | | * |
274 | | * Since: 1.9.0 |
275 | | **/ |
276 | | hb_bool_t |
277 | | hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob) |
278 | 0 | { |
279 | 0 | if (unlikely (face->destroy != (hb_destroy_func_t) _hb_face_builder_data_destroy)) |
280 | 0 | return false; |
281 | | |
282 | 0 | if (tag == HB_MAP_VALUE_INVALID) |
283 | 0 | return false; |
284 | | |
285 | 0 | hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data; |
286 | | |
287 | | /* numTables in the font file's table directory is 16bit. */ |
288 | 0 | if (unlikely (data->tables.get_population () >= 0xFFFFu && |
289 | 0 | !data->tables.has (tag))) |
290 | 0 | return false; |
291 | | |
292 | 0 | hb_blob_t* previous = data->tables.get (tag).data; |
293 | 0 | if (!data->tables.set (tag, face_table_info_t {hb_blob_reference (blob), (unsigned) -1})) |
294 | 0 | { |
295 | 0 | hb_blob_destroy (blob); |
296 | 0 | return false; |
297 | 0 | } |
298 | | |
299 | 0 | hb_blob_destroy (previous); |
300 | 0 | hb_free (data->sorted_tags.get_relaxed ()); |
301 | 0 | data->sorted_tags.set_relaxed (nullptr); |
302 | 0 | return true; |
303 | 0 | } |
304 | | |
305 | | /** |
306 | | * hb_face_builder_sort_tables: |
307 | | * @face: A face object created with hb_face_builder_create() |
308 | | * @tags: (array zero-terminated=1): ordered list of table tags terminated by |
309 | | * %HB_TAG_NONE |
310 | | * |
311 | | * Set the ordering of tables for serialization. Any tables not |
312 | | * specified in the tags list will be ordered after the tables in |
313 | | * tags, ordered by the default sort ordering. |
314 | | * |
315 | | * Since: 5.3.0 |
316 | | **/ |
317 | | void |
318 | | hb_face_builder_sort_tables (hb_face_t *face, |
319 | | const hb_tag_t *tags) |
320 | 0 | { |
321 | 0 | if (unlikely (face->destroy != (hb_destroy_func_t) _hb_face_builder_data_destroy)) |
322 | 0 | return; |
323 | | |
324 | 0 | hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data; |
325 | | |
326 | | // Sort all unspecified tables after any specified tables. |
327 | 0 | for (auto& info : data->tables.values_ref()) |
328 | 0 | info.order = (unsigned) -1; |
329 | |
|
330 | 0 | signed order = 0; |
331 | 0 | for (const hb_tag_t* tag = tags; |
332 | 0 | *tag; |
333 | 0 | tag++) |
334 | 0 | { |
335 | 0 | face_table_info_t* info; |
336 | 0 | if (!data->tables.has (*tag, &info)) continue; |
337 | 0 | info->order = order++; |
338 | 0 | } |
339 | 0 | } |