blob: 1ba970707275fd60d7104f2118743ec24b500c2e [file] [log] [blame]
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -04001/*
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-private.hh"
30
Behdad Esfahbod113393e2017-01-21 15:12:03 -080031#include "hb-face-private.hh"
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -040032#include "hb-open-file-private.hh"
33#include "hb-ot-head-table.hh"
34#include "hb-ot-maxp-table.hh"
35
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -040036
37/*
38 * hb_face_t
39 */
40
41const hb_face_t _hb_face_nil = {
42 HB_OBJECT_HEADER_STATIC,
43
44 true, /* immutable */
45
46 NULL, /* reference_table_func */
47 NULL, /* user_data */
48 NULL, /* destroy */
49
50 0, /* index */
51 1000, /* upem */
52 0, /* num_glyphs */
53
54 {
55#define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
56#include "hb-shaper-list.hh"
57#undef HB_SHAPER_IMPLEMENT
58 },
59
60 NULL, /* shape_plans */
61};
62
63
Behdad Esfahbod288f2892013-09-06 15:40:22 -040064/**
65 * hb_face_create_for_tables:
Behdad Esfahbodace5c7e2013-09-13 20:34:42 -040066 * @reference_table_func: (closure user_data) (destroy destroy) (scope notified):
Behdad Esfahbod288f2892013-09-06 15:40:22 -040067 * @user_data:
68 * @destroy:
69 *
70 *
71 *
72 * Return value: (transfer full)
73 *
Behdad Esfahbod5d74ff02015-09-03 14:55:59 +043074 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -040075 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -040076hb_face_t *
77hb_face_create_for_tables (hb_reference_table_func_t reference_table_func,
78 void *user_data,
79 hb_destroy_func_t destroy)
80{
81 hb_face_t *face;
82
83 if (!reference_table_func || !(face = hb_object_create<hb_face_t> ())) {
84 if (destroy)
85 destroy (user_data);
86 return hb_face_get_empty ();
87 }
88
89 face->reference_table_func = reference_table_func;
90 face->user_data = user_data;
91 face->destroy = destroy;
92
93 face->upem = 0;
94 face->num_glyphs = (unsigned int) -1;
95
96 return face;
97}
98
99
100typedef struct hb_face_for_data_closure_t {
101 hb_blob_t *blob;
102 unsigned int index;
103} hb_face_for_data_closure_t;
104
105static hb_face_for_data_closure_t *
106_hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
107{
108 hb_face_for_data_closure_t *closure;
109
Behdad Esfahboda5efaac2015-10-02 08:02:29 +0100110 closure = (hb_face_for_data_closure_t *) calloc (1, sizeof (hb_face_for_data_closure_t));
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400111 if (unlikely (!closure))
112 return NULL;
113
114 closure->blob = blob;
115 closure->index = index;
116
117 return closure;
118}
119
120static void
121_hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure)
122{
123 hb_blob_destroy (closure->blob);
124 free (closure);
125}
126
127static hb_blob_t *
128_hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
129{
130 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
131
132 if (tag == HB_TAG_NONE)
133 return hb_blob_reference (data->blob);
134
135 const OT::OpenTypeFontFile &ot_file = *OT::Sanitizer<OT::OpenTypeFontFile>::lock_instance (data->blob);
136 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
137
138 const OT::OpenTypeTable &table = ot_face.get_table_by_tag (tag);
139
140 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
141
142 return blob;
143}
144
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400145/**
Behdad Esfahbod085d4292013-09-12 17:14:33 -0400146 * hb_face_create: (Xconstructor)
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400147 * @blob:
148 * @index:
149 *
150 *
151 *
152 * Return value: (transfer full):
153 *
Behdad Esfahbod5d74ff02015-09-03 14:55:59 +0430154 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400155 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400156hb_face_t *
157hb_face_create (hb_blob_t *blob,
158 unsigned int index)
159{
160 hb_face_t *face;
161
Behdad Esfahbodeb0bf3a2014-08-06 15:36:41 -0400162 if (unlikely (!blob))
163 blob = hb_blob_get_empty ();
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400164
165 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (OT::Sanitizer<OT::OpenTypeFontFile>::sanitize (hb_blob_reference (blob)), index);
166
167 if (unlikely (!closure))
168 return hb_face_get_empty ();
169
170 face = hb_face_create_for_tables (_hb_face_for_data_reference_table,
171 closure,
172 (hb_destroy_func_t) _hb_face_for_data_closure_destroy);
173
174 hb_face_set_index (face, index);
175
176 return face;
177}
178
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400179/**
180 * hb_face_get_empty:
181 *
182 *
183 *
184 * Return value: (transfer full)
185 *
Behdad Esfahbod5d74ff02015-09-03 14:55:59 +0430186 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400187 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400188hb_face_t *
189hb_face_get_empty (void)
190{
191 return const_cast<hb_face_t *> (&_hb_face_nil);
192}
193
194
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400195/**
196 * hb_face_reference: (skip)
197 * @face: a face.
198 *
199 *
200 *
201 * Return value:
202 *
Behdad Esfahbod5d74ff02015-09-03 14:55:59 +0430203 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400204 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400205hb_face_t *
206hb_face_reference (hb_face_t *face)
207{
208 return hb_object_reference (face);
209}
210
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400211/**
212 * hb_face_destroy: (skip)
213 * @face: a face.
214 *
215 *
216 *
Behdad Esfahbod5d74ff02015-09-03 14:55:59 +0430217 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400218 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400219void
220hb_face_destroy (hb_face_t *face)
221{
222 if (!hb_object_destroy (face)) return;
223
224 for (hb_face_t::plan_node_t *node = face->shape_plans; node; )
225 {
226 hb_face_t::plan_node_t *next = node->next;
227 hb_shape_plan_destroy (node->shape_plan);
228 free (node);
229 node = next;
230 }
231
232#define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, face);
233#include "hb-shaper-list.hh"
234#undef HB_SHAPER_IMPLEMENT
235
236 if (face->destroy)
237 face->destroy (face->user_data);
238
239 free (face);
240}
241
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400242/**
243 * hb_face_set_user_data: (skip)
244 * @face: a face.
245 * @key:
246 * @data:
247 * @destroy:
248 * @replace:
249 *
250 *
251 *
252 * Return value:
253 *
Behdad Esfahbod5d74ff02015-09-03 14:55:59 +0430254 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400255 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400256hb_bool_t
257hb_face_set_user_data (hb_face_t *face,
258 hb_user_data_key_t *key,
259 void * data,
260 hb_destroy_func_t destroy,
261 hb_bool_t replace)
262{
263 return hb_object_set_user_data (face, key, data, destroy, replace);
264}
265
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400266/**
267 * hb_face_get_user_data: (skip)
268 * @face: a face.
269 * @key:
270 *
271 *
272 *
273 * Return value: (transfer none):
274 *
Behdad Esfahbod5d74ff02015-09-03 14:55:59 +0430275 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400276 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400277void *
278hb_face_get_user_data (hb_face_t *face,
279 hb_user_data_key_t *key)
280{
281 return hb_object_get_user_data (face, key);
282}
283
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400284/**
285 * hb_face_make_immutable:
286 * @face: a face.
287 *
288 *
289 *
Behdad Esfahbod5d74ff02015-09-03 14:55:59 +0430290 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400291 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400292void
293hb_face_make_immutable (hb_face_t *face)
294{
Behdad Esfahbod3f310dc2014-07-22 16:26:27 -0400295 if (unlikely (hb_object_is_inert (face)))
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400296 return;
297
298 face->immutable = true;
299}
300
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400301/**
302 * hb_face_is_immutable:
303 * @face: a face.
304 *
305 *
306 *
307 * Return value:
308 *
Behdad Esfahbod5d74ff02015-09-03 14:55:59 +0430309 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400310 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400311hb_bool_t
312hb_face_is_immutable (hb_face_t *face)
313{
314 return face->immutable;
315}
316
317
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400318/**
319 * hb_face_reference_table:
320 * @face: a face.
321 * @tag:
322 *
323 *
324 *
325 * Return value: (transfer full):
326 *
Behdad Esfahbod5d74ff02015-09-03 14:55:59 +0430327 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400328 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400329hb_blob_t *
330hb_face_reference_table (hb_face_t *face,
331 hb_tag_t tag)
332{
333 return face->reference_table (tag);
334}
335
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400336/**
337 * hb_face_reference_blob:
338 * @face: a face.
339 *
340 *
341 *
342 * Return value: (transfer full):
343 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200344 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400345 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400346hb_blob_t *
347hb_face_reference_blob (hb_face_t *face)
348{
349 return face->reference_table (HB_TAG_NONE);
350}
351
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400352/**
353 * hb_face_set_index:
354 * @face: a face.
355 * @index:
356 *
357 *
358 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200359 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400360 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400361void
362hb_face_set_index (hb_face_t *face,
363 unsigned int index)
364{
Behdad Esfahbod3f310dc2014-07-22 16:26:27 -0400365 if (face->immutable)
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400366 return;
367
368 face->index = index;
369}
370
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400371/**
372 * hb_face_get_index:
373 * @face: a face.
374 *
375 *
376 *
377 * Return value:
378 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200379 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400380 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400381unsigned int
382hb_face_get_index (hb_face_t *face)
383{
384 return face->index;
385}
386
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400387/**
388 * hb_face_set_upem:
389 * @face: a face.
390 * @upem:
391 *
392 *
393 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200394 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400395 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400396void
397hb_face_set_upem (hb_face_t *face,
398 unsigned int upem)
399{
Behdad Esfahbod3f310dc2014-07-22 16:26:27 -0400400 if (face->immutable)
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400401 return;
402
403 face->upem = upem;
404}
405
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400406/**
407 * hb_face_get_upem:
408 * @face: a face.
409 *
410 *
411 *
412 * Return value:
413 *
Behdad Esfahbod5d74ff02015-09-03 14:55:59 +0430414 * Since: 0.9.2
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400415 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400416unsigned int
417hb_face_get_upem (hb_face_t *face)
418{
419 return face->get_upem ();
420}
421
422void
423hb_face_t::load_upem (void) const
424{
425 hb_blob_t *head_blob = OT::Sanitizer<OT::head>::sanitize (reference_table (HB_OT_TAG_head));
426 const OT::head *head_table = OT::Sanitizer<OT::head>::lock_instance (head_blob);
427 upem = head_table->get_upem ();
428 hb_blob_destroy (head_blob);
429}
430
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400431/**
432 * hb_face_set_glyph_count:
433 * @face: a face.
434 * @glyph_count:
435 *
436 *
437 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200438 * Since: 0.9.7
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400439 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400440void
441hb_face_set_glyph_count (hb_face_t *face,
442 unsigned int glyph_count)
443{
Behdad Esfahbod3f310dc2014-07-22 16:26:27 -0400444 if (face->immutable)
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400445 return;
446
447 face->num_glyphs = glyph_count;
448}
449
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400450/**
451 * hb_face_get_glyph_count:
452 * @face: a face.
453 *
454 *
455 *
456 * Return value:
457 *
Sascha Brawer01c3a882015-06-01 13:22:01 +0200458 * Since: 0.9.7
Behdad Esfahbod288f2892013-09-06 15:40:22 -0400459 **/
Behdad Esfahbod2e3a07a2013-08-26 18:49:07 -0400460unsigned int
461hb_face_get_glyph_count (hb_face_t *face)
462{
463 return face->get_num_glyphs ();
464}
465
466void
467hb_face_t::load_num_glyphs (void) const
468{
469 hb_blob_t *maxp_blob = OT::Sanitizer<OT::maxp>::sanitize (reference_table (HB_OT_TAG_maxp));
470 const OT::maxp *maxp_table = OT::Sanitizer<OT::maxp>::lock_instance (maxp_blob);
471 num_glyphs = maxp_table->get_num_glyphs ();
472 hb_blob_destroy (maxp_blob);
473}
474
475