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