2 * Copyright © 2009 Red Hat, Inc.
3 * Copyright © 2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
29 #include "hb-private.hh"
31 #include "hb-face-private.hh"
32 #include "hb-blob-private.hh"
33 #include "hb-open-file-private.hh"
34 #include "hb-ot-head-table.hh"
35 #include "hb-ot-maxp-table.hh"
40 * hb_face_count: Get number of faces on the blob
45 * Return value: Number of faces on the blob
50 hb_face_count (hb_blob_t *blob)
55 hb_blob_t *sanitized = OT::Sanitizer<OT::OpenTypeFontFile> ().sanitize (blob);
56 const OT::OpenTypeFontFile& ot = *sanitized->as<OT::OpenTypeFontFile> ();
58 return ot.get_face_count ();
65 const hb_face_t _hb_face_nil = {
66 HB_OBJECT_HEADER_STATIC,
70 nullptr, /* reference_table_func */
71 nullptr, /* user_data */
72 nullptr, /* destroy */
79 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID,
80 #include "hb-shaper-list.hh"
81 #undef HB_SHAPER_IMPLEMENT
84 nullptr, /* shape_plans */
89 * hb_face_create_for_tables:
90 * @reference_table_func: (closure user_data) (destroy destroy) (scope notified):
96 * Return value: (transfer full)
101 hb_face_create_for_tables (hb_reference_table_func_t reference_table_func,
103 hb_destroy_func_t destroy)
107 if (!reference_table_func || !(face = hb_object_create<hb_face_t> ())) {
110 return hb_face_get_empty ();
113 face->reference_table_func = reference_table_func;
114 face->user_data = user_data;
115 face->destroy = destroy;
118 face->num_glyphs = (unsigned int) -1;
124 typedef struct hb_face_for_data_closure_t {
127 } hb_face_for_data_closure_t;
129 static hb_face_for_data_closure_t *
130 _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index)
132 hb_face_for_data_closure_t *closure;
134 closure = (hb_face_for_data_closure_t *) calloc (1, sizeof (hb_face_for_data_closure_t));
135 if (unlikely (!closure))
138 closure->blob = blob;
139 closure->index = index;
145 _hb_face_for_data_closure_destroy (void *data)
147 hb_face_for_data_closure_t *closure = (hb_face_for_data_closure_t *) data;
149 hb_blob_destroy (closure->blob);
154 _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
156 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data;
158 if (tag == HB_TAG_NONE)
159 return hb_blob_reference (data->blob);
161 const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
162 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
164 const OT::OpenTypeTable &table = ot_face.get_table_by_tag (tag);
166 hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length);
172 * hb_face_create: (Xconstructor)
178 * Return value: (transfer full):
183 hb_face_create (hb_blob_t *blob,
188 if (unlikely (!blob))
189 blob = hb_blob_get_empty ();
191 hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (OT::Sanitizer<OT::OpenTypeFontFile>().sanitize (hb_blob_reference (blob)), index);
193 if (unlikely (!closure))
194 return hb_face_get_empty ();
196 face = hb_face_create_for_tables (_hb_face_for_data_reference_table,
198 _hb_face_for_data_closure_destroy);
210 * Return value: (transfer full)
215 hb_face_get_empty (void)
217 return const_cast<hb_face_t *> (&_hb_face_nil);
222 * hb_face_reference: (skip)
232 hb_face_reference (hb_face_t *face)
234 return hb_object_reference (face);
238 * hb_face_destroy: (skip)
246 hb_face_destroy (hb_face_t *face)
248 if (!hb_object_destroy (face)) return;
250 for (hb_face_t::plan_node_t *node = face->shape_plans; node; )
252 hb_face_t::plan_node_t *next = node->next;
253 hb_shape_plan_destroy (node->shape_plan);
258 #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, face);
259 #include "hb-shaper-list.hh"
260 #undef HB_SHAPER_IMPLEMENT
263 face->destroy (face->user_data);
269 * hb_face_set_user_data: (skip)
283 hb_face_set_user_data (hb_face_t *face,
284 hb_user_data_key_t *key,
286 hb_destroy_func_t destroy,
289 return hb_object_set_user_data (face, key, data, destroy, replace);
293 * hb_face_get_user_data: (skip)
299 * Return value: (transfer none):
304 hb_face_get_user_data (hb_face_t *face,
305 hb_user_data_key_t *key)
307 return hb_object_get_user_data (face, key);
311 * hb_face_make_immutable:
319 hb_face_make_immutable (hb_face_t *face)
321 if (unlikely (hb_object_is_inert (face)))
324 face->immutable = true;
328 * hb_face_is_immutable:
338 hb_face_is_immutable (hb_face_t *face)
340 return face->immutable;
345 * hb_face_reference_table:
351 * Return value: (transfer full):
356 hb_face_reference_table (hb_face_t *face,
359 return face->reference_table (tag);
363 * hb_face_reference_blob:
368 * Return value: (transfer full):
373 hb_face_reference_blob (hb_face_t *face)
375 return face->reference_table (HB_TAG_NONE);
388 hb_face_set_index (hb_face_t *face,
408 hb_face_get_index (hb_face_t *face)
423 hb_face_set_upem (hb_face_t *face,
443 hb_face_get_upem (hb_face_t *face)
445 return face->get_upem ();
449 hb_face_t::load_upem (void) const
451 hb_blob_t *head_blob = OT::Sanitizer<OT::head>().sanitize (reference_table (HB_OT_TAG_head));
452 const OT::head *head_table = head_blob->as<OT::head> ();
453 upem = head_table->get_upem ();
454 hb_blob_destroy (head_blob);
458 * hb_face_set_glyph_count:
467 hb_face_set_glyph_count (hb_face_t *face,
468 unsigned int glyph_count)
473 face->num_glyphs = glyph_count;
477 * hb_face_get_glyph_count:
487 hb_face_get_glyph_count (hb_face_t *face)
489 return face->get_num_glyphs ();
493 hb_face_t::load_num_glyphs (void) const
495 hb_blob_t *maxp_blob = OT::Sanitizer<OT::maxp>().sanitize (reference_table (HB_OT_TAG_maxp));
496 const OT::maxp *maxp_table = maxp_blob->as<OT::maxp> ();
497 num_glyphs = maxp_table->get_num_glyphs ();
498 hb_blob_destroy (maxp_blob);
502 * hb_face_get_table_tags:
505 * Retrieves table tags for a face, if possible.
507 * Return value: total number of tables, or 0 if not possible to list.
512 hb_face_get_table_tags (hb_face_t *face,
513 unsigned int start_offset,
514 unsigned int *table_count, /* IN/OUT */
515 hb_tag_t *table_tags /* OUT */)
517 if (face->destroy != (hb_destroy_func_t) _hb_face_for_data_closure_destroy)
524 hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) face->user_data;
526 const OT::OpenTypeFontFile &ot_file = *data->blob->as<OT::OpenTypeFontFile> ();
527 const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index);
529 return ot_face.get_table_tags (start_offset, table_count, table_tags);