/src/harfbuzz/src/hb-face-builder.cc
Line | Count | Source (jump to first uncovered line) |
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 | | }; |
52 | | |
53 | | static int compare_entries (const void* pa, const void* pb) |
54 | 230k | { |
55 | 230k | const auto& a = * (const hb_pair_t<hb_tag_t, face_table_info_t> *) pa; |
56 | 230k | const auto& b = * (const hb_pair_t<hb_tag_t, face_table_info_t> *) pb; |
57 | | |
58 | | /* Order by blob size first (smallest to largest) and then table tag */ |
59 | | |
60 | 230k | if (a.second.order != b.second.order) |
61 | 0 | return a.second.order < b.second.order ? -1 : +1; |
62 | | |
63 | 230k | if (a.second.data->length != b.second.data->length) |
64 | 107k | return a.second.data->length < b.second.data->length ? -1 : +1; |
65 | | |
66 | 122k | return a.first < b.first ? -1 : a.first == b.first ? 0 : +1; |
67 | 230k | } |
68 | | |
69 | | static hb_face_builder_data_t * |
70 | | _hb_face_builder_data_create () |
71 | 63.7k | { |
72 | 63.7k | hb_face_builder_data_t *data = (hb_face_builder_data_t *) hb_calloc (1, sizeof (hb_face_builder_data_t)); |
73 | 63.7k | if (unlikely (!data)) |
74 | 281 | return nullptr; |
75 | | |
76 | 63.4k | data->tables.init (); |
77 | | |
78 | 63.4k | return data; |
79 | 63.7k | } |
80 | | |
81 | | static void |
82 | | _hb_face_builder_data_destroy (void *user_data) |
83 | 63.4k | { |
84 | 63.4k | hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data; |
85 | | |
86 | 63.4k | for (auto info : data->tables.values()) |
87 | 215k | hb_blob_destroy (info.data); |
88 | | |
89 | 63.4k | data->tables.fini (); |
90 | | |
91 | 63.4k | hb_free (data); |
92 | 63.4k | } |
93 | | |
94 | | static hb_blob_t * |
95 | | _hb_face_builder_data_reference_blob (hb_face_builder_data_t *data) |
96 | 21.9k | { |
97 | | |
98 | 21.9k | unsigned int table_count = data->tables.get_population (); |
99 | 21.9k | unsigned int face_length = table_count * 16 + 12; |
100 | | |
101 | 21.9k | for (auto info : data->tables.values()) |
102 | 89.8k | face_length += hb_ceil_to_4 (hb_blob_get_length (info.data)); |
103 | | |
104 | 21.9k | char *buf = (char *) hb_malloc (face_length); |
105 | 21.9k | if (unlikely (!buf)) |
106 | 11 | return nullptr; |
107 | | |
108 | 21.9k | hb_serialize_context_t c (buf, face_length); |
109 | 21.9k | c.propagate_error (data->tables); |
110 | 21.9k | OT::OpenTypeFontFile *f = c.start_serialize<OT::OpenTypeFontFile> (); |
111 | | |
112 | 21.9k | bool is_cff = (data->tables.has (HB_TAG ('C','F','F',' ')) |
113 | 21.9k | || data->tables.has (HB_TAG ('C','F','F','2'))); |
114 | 21.9k | hb_tag_t sfnt_tag = is_cff ? OT::OpenTypeFontFile::CFFTag : OT::OpenTypeFontFile::TrueTypeTag; |
115 | | |
116 | | // Sort the tags so that produced face is deterministic. |
117 | 21.9k | hb_vector_t<hb_pair_t <hb_tag_t, face_table_info_t>> sorted_entries; |
118 | 21.9k | data->tables.iter () | hb_sink (sorted_entries); |
119 | 21.9k | if (unlikely (sorted_entries.in_error ())) |
120 | 12 | { |
121 | 12 | hb_free (buf); |
122 | 12 | return nullptr; |
123 | 12 | } |
124 | | |
125 | 21.9k | sorted_entries.qsort (compare_entries); |
126 | | |
127 | 21.9k | bool ret = f->serialize_single (&c, |
128 | 21.9k | sfnt_tag, |
129 | 21.9k | + sorted_entries.iter() |
130 | 89.6k | | hb_map ([&] (hb_pair_t<hb_tag_t, face_table_info_t> _) { |
131 | 89.6k | return hb_pair_t<hb_tag_t, hb_blob_t*> (_.first, _.second.data); |
132 | 89.6k | })); |
133 | | |
134 | 21.9k | c.end_serialize (); |
135 | | |
136 | 21.9k | if (unlikely (!ret)) |
137 | 26 | { |
138 | 26 | hb_free (buf); |
139 | 26 | return nullptr; |
140 | 26 | } |
141 | | |
142 | 21.9k | return hb_blob_create (buf, face_length, HB_MEMORY_MODE_WRITABLE, buf, hb_free); |
143 | 21.9k | } |
144 | | |
145 | | static hb_blob_t * |
146 | | _hb_face_builder_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data) |
147 | 22.1k | { |
148 | 22.1k | hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data; |
149 | | |
150 | 22.1k | if (!tag) |
151 | 21.9k | return _hb_face_builder_data_reference_blob (data); |
152 | | |
153 | 139 | return hb_blob_reference (data->tables[tag].data); |
154 | 22.1k | } |
155 | | |
156 | | static unsigned |
157 | | _hb_face_builder_get_table_tags (const hb_face_t *face HB_UNUSED, |
158 | | unsigned int start_offset, |
159 | | unsigned int *table_count, |
160 | | hb_tag_t *table_tags, |
161 | | void *user_data) |
162 | 105 | { |
163 | 105 | hb_face_builder_data_t *data = (hb_face_builder_data_t *) user_data; |
164 | | |
165 | 105 | unsigned population = data->tables.get_population (); |
166 | | |
167 | 105 | if (!table_count) |
168 | 59 | return population; |
169 | | |
170 | 46 | if (unlikely (start_offset >= population)) |
171 | 0 | { |
172 | 0 | *table_count = 0; |
173 | 0 | return population; |
174 | 0 | } |
175 | | |
176 | | // Sort the tags. |
177 | 46 | hb_vector_t<hb_tag_t> sorted_tags; |
178 | 46 | data->tables.keys () | hb_sink (sorted_tags); |
179 | 46 | if (unlikely (sorted_tags.in_error ())) |
180 | 4 | { |
181 | | // Not much to do... |
182 | 4 | } |
183 | 231 | sorted_tags.qsort ([] (const void* a, const void* b) { |
184 | 231 | return * (hb_tag_t *) a < * (hb_tag_t *) b ? -1 : |
185 | 231 | * (hb_tag_t *) a == * (hb_tag_t *) b ? 0 : |
186 | 134 | +1; |
187 | 231 | }); |
188 | | |
189 | 46 | auto array = sorted_tags.as_array ().sub_array (start_offset, table_count); |
190 | 46 | auto out = hb_array (table_tags, *table_count); |
191 | | |
192 | 46 | + array.iter () |
193 | 46 | | hb_sink (out) |
194 | 46 | ; |
195 | | |
196 | 46 | return population; |
197 | 46 | } |
198 | | |
199 | | |
200 | | /** |
201 | | * hb_face_builder_create: |
202 | | * |
203 | | * Creates a #hb_face_t that can be used with hb_face_builder_add_table(). |
204 | | * After tables are added to the face, it can be compiled to a binary |
205 | | * font file by calling hb_face_reference_blob(). |
206 | | * |
207 | | * Return value: (transfer full): New face. |
208 | | * |
209 | | * Since: 1.9.0 |
210 | | **/ |
211 | | hb_face_t * |
212 | | hb_face_builder_create () |
213 | 63.7k | { |
214 | 63.7k | hb_face_builder_data_t *data = _hb_face_builder_data_create (); |
215 | 63.7k | if (unlikely (!data)) return hb_face_get_empty (); |
216 | | |
217 | 63.4k | hb_face_t *face = hb_face_create_for_tables (_hb_face_builder_reference_table, |
218 | 63.4k | data, |
219 | 63.4k | _hb_face_builder_data_destroy); |
220 | | |
221 | 63.4k | hb_face_set_get_table_tags_func (face, |
222 | 63.4k | _hb_face_builder_get_table_tags, |
223 | 63.4k | data, |
224 | 63.4k | nullptr); |
225 | | |
226 | 63.4k | return face; |
227 | 63.7k | } |
228 | | |
229 | | /** |
230 | | * hb_face_builder_add_table: |
231 | | * @face: A face object created with hb_face_builder_create() |
232 | | * @tag: The #hb_tag_t of the table to add |
233 | | * @blob: The blob containing the table data to add |
234 | | * |
235 | | * Add table for @tag with data provided by @blob to the face. @face must |
236 | | * be created using hb_face_builder_create(). |
237 | | * |
238 | | * Since: 1.9.0 |
239 | | **/ |
240 | | hb_bool_t |
241 | | hb_face_builder_add_table (hb_face_t *face, hb_tag_t tag, hb_blob_t *blob) |
242 | 220k | { |
243 | 220k | if (unlikely (face->destroy != (hb_destroy_func_t) _hb_face_builder_data_destroy)) |
244 | 78 | return false; |
245 | | |
246 | 220k | if (tag == HB_MAP_VALUE_INVALID) |
247 | 0 | return false; |
248 | | |
249 | 220k | hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data; |
250 | | |
251 | 220k | hb_blob_t* previous = data->tables.get (tag).data; |
252 | 220k | if (!data->tables.set (tag, face_table_info_t {hb_blob_reference (blob), (unsigned) -1})) |
253 | 26 | { |
254 | 26 | hb_blob_destroy (blob); |
255 | 26 | return false; |
256 | 26 | } |
257 | | |
258 | 220k | hb_blob_destroy (previous); |
259 | 220k | return true; |
260 | 220k | } |
261 | | |
262 | | /** |
263 | | * hb_face_builder_sort_tables: |
264 | | * @face: A face object created with hb_face_builder_create() |
265 | | * @tags: (array zero-terminated=1): ordered list of table tags terminated by |
266 | | * %HB_TAG_NONE |
267 | | * |
268 | | * Set the ordering of tables for serialization. Any tables not |
269 | | * specified in the tags list will be ordered after the tables in |
270 | | * tags, ordered by the default sort ordering. |
271 | | * |
272 | | * Since: 5.3.0 |
273 | | **/ |
274 | | void |
275 | | hb_face_builder_sort_tables (hb_face_t *face, |
276 | | const hb_tag_t *tags) |
277 | 0 | { |
278 | 0 | if (unlikely (face->destroy != (hb_destroy_func_t) _hb_face_builder_data_destroy)) |
279 | 0 | return; |
280 | | |
281 | 0 | hb_face_builder_data_t *data = (hb_face_builder_data_t *) face->user_data; |
282 | | |
283 | | // Sort all unspecified tables after any specified tables. |
284 | 0 | for (auto& info : data->tables.values_ref()) |
285 | 0 | info.order = (unsigned) -1; |
286 | |
|
287 | 0 | signed order = 0; |
288 | 0 | for (const hb_tag_t* tag = tags; |
289 | 0 | *tag; |
290 | 0 | tag++) |
291 | 0 | { |
292 | 0 | face_table_info_t* info; |
293 | 0 | if (!data->tables.has (*tag, &info)) continue; |
294 | 0 | info->order = order++; |
295 | 0 | } |
296 | 0 | } |