michael@0: /* michael@0: * Copyright © 2009 Red Hat, Inc. michael@0: * Copyright © 2012 Google, Inc. michael@0: * michael@0: * This is part of HarfBuzz, a text shaping library. michael@0: * michael@0: * Permission is hereby granted, without written agreement and without michael@0: * license or royalty fees, to use, copy, modify, and distribute this michael@0: * software and its documentation for any purpose, provided that the michael@0: * above copyright notice and the following two paragraphs appear in michael@0: * all copies of this software. michael@0: * michael@0: * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR michael@0: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES michael@0: * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN michael@0: * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH michael@0: * DAMAGE. michael@0: * michael@0: * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, michael@0: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND michael@0: * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS michael@0: * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO michael@0: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. michael@0: * michael@0: * Red Hat Author(s): Behdad Esfahbod michael@0: * Google Author(s): Behdad Esfahbod michael@0: */ michael@0: michael@0: #include "hb-private.hh" michael@0: michael@0: #include "hb-ot-layout-private.hh" michael@0: michael@0: #include "hb-font-private.hh" michael@0: #include "hb-open-file-private.hh" michael@0: #include "hb-ot-head-table.hh" michael@0: #include "hb-ot-maxp-table.hh" michael@0: michael@0: #include "hb-cache-private.hh" michael@0: michael@0: #include michael@0: michael@0: michael@0: /* michael@0: * hb_face_t michael@0: */ michael@0: michael@0: const hb_face_t _hb_face_nil = { michael@0: HB_OBJECT_HEADER_STATIC, michael@0: michael@0: true, /* immutable */ michael@0: michael@0: NULL, /* reference_table_func */ michael@0: NULL, /* user_data */ michael@0: NULL, /* destroy */ michael@0: michael@0: 0, /* index */ michael@0: 1000, /* upem */ michael@0: 0, /* num_glyphs */ michael@0: michael@0: { michael@0: #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_INVALID, michael@0: #include "hb-shaper-list.hh" michael@0: #undef HB_SHAPER_IMPLEMENT michael@0: }, michael@0: michael@0: NULL, /* shape_plans */ michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * hb_face_create_for_tables: michael@0: * @reference_table_func: (closure user_data) (destroy destroy) (scope notified): michael@0: * @user_data: michael@0: * @destroy: michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer full) michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_face_t * michael@0: hb_face_create_for_tables (hb_reference_table_func_t reference_table_func, michael@0: void *user_data, michael@0: hb_destroy_func_t destroy) michael@0: { michael@0: hb_face_t *face; michael@0: michael@0: if (!reference_table_func || !(face = hb_object_create ())) { michael@0: if (destroy) michael@0: destroy (user_data); michael@0: return hb_face_get_empty (); michael@0: } michael@0: michael@0: face->reference_table_func = reference_table_func; michael@0: face->user_data = user_data; michael@0: face->destroy = destroy; michael@0: michael@0: face->upem = 0; michael@0: face->num_glyphs = (unsigned int) -1; michael@0: michael@0: return face; michael@0: } michael@0: michael@0: michael@0: typedef struct hb_face_for_data_closure_t { michael@0: hb_blob_t *blob; michael@0: unsigned int index; michael@0: } hb_face_for_data_closure_t; michael@0: michael@0: static hb_face_for_data_closure_t * michael@0: _hb_face_for_data_closure_create (hb_blob_t *blob, unsigned int index) michael@0: { michael@0: hb_face_for_data_closure_t *closure; michael@0: michael@0: closure = (hb_face_for_data_closure_t *) malloc (sizeof (hb_face_for_data_closure_t)); michael@0: if (unlikely (!closure)) michael@0: return NULL; michael@0: michael@0: closure->blob = blob; michael@0: closure->index = index; michael@0: michael@0: return closure; michael@0: } michael@0: michael@0: static void michael@0: _hb_face_for_data_closure_destroy (hb_face_for_data_closure_t *closure) michael@0: { michael@0: hb_blob_destroy (closure->blob); michael@0: free (closure); michael@0: } michael@0: michael@0: static hb_blob_t * michael@0: _hb_face_for_data_reference_table (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data) michael@0: { michael@0: hb_face_for_data_closure_t *data = (hb_face_for_data_closure_t *) user_data; michael@0: michael@0: if (tag == HB_TAG_NONE) michael@0: return hb_blob_reference (data->blob); michael@0: michael@0: const OT::OpenTypeFontFile &ot_file = *OT::Sanitizer::lock_instance (data->blob); michael@0: const OT::OpenTypeFontFace &ot_face = ot_file.get_face (data->index); michael@0: michael@0: const OT::OpenTypeTable &table = ot_face.get_table_by_tag (tag); michael@0: michael@0: hb_blob_t *blob = hb_blob_create_sub_blob (data->blob, table.offset, table.length); michael@0: michael@0: return blob; michael@0: } michael@0: michael@0: /** michael@0: * hb_face_create: (Xconstructor) michael@0: * @blob: michael@0: * @index: michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer full): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_face_t * michael@0: hb_face_create (hb_blob_t *blob, michael@0: unsigned int index) michael@0: { michael@0: hb_face_t *face; michael@0: michael@0: if (unlikely (!blob || !hb_blob_get_length (blob))) michael@0: return hb_face_get_empty (); michael@0: michael@0: hb_face_for_data_closure_t *closure = _hb_face_for_data_closure_create (OT::Sanitizer::sanitize (hb_blob_reference (blob)), index); michael@0: michael@0: if (unlikely (!closure)) michael@0: return hb_face_get_empty (); michael@0: michael@0: face = hb_face_create_for_tables (_hb_face_for_data_reference_table, michael@0: closure, michael@0: (hb_destroy_func_t) _hb_face_for_data_closure_destroy); michael@0: michael@0: hb_face_set_index (face, index); michael@0: michael@0: return face; michael@0: } michael@0: michael@0: /** michael@0: * hb_face_get_empty: michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer full) michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_face_t * michael@0: hb_face_get_empty (void) michael@0: { michael@0: return const_cast (&_hb_face_nil); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * hb_face_reference: (skip) michael@0: * @face: a face. michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_face_t * michael@0: hb_face_reference (hb_face_t *face) michael@0: { michael@0: return hb_object_reference (face); michael@0: } michael@0: michael@0: /** michael@0: * hb_face_destroy: (skip) michael@0: * @face: a face. michael@0: * michael@0: * michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_face_destroy (hb_face_t *face) michael@0: { michael@0: if (!hb_object_destroy (face)) return; michael@0: michael@0: for (hb_face_t::plan_node_t *node = face->shape_plans; node; ) michael@0: { michael@0: hb_face_t::plan_node_t *next = node->next; michael@0: hb_shape_plan_destroy (node->shape_plan); michael@0: free (node); michael@0: node = next; michael@0: } michael@0: michael@0: #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_DESTROY(shaper, face); michael@0: #include "hb-shaper-list.hh" michael@0: #undef HB_SHAPER_IMPLEMENT michael@0: michael@0: if (face->destroy) michael@0: face->destroy (face->user_data); michael@0: michael@0: free (face); michael@0: } michael@0: michael@0: /** michael@0: * hb_face_set_user_data: (skip) michael@0: * @face: a face. michael@0: * @key: michael@0: * @data: michael@0: * @destroy: michael@0: * @replace: michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_bool_t michael@0: hb_face_set_user_data (hb_face_t *face, michael@0: hb_user_data_key_t *key, michael@0: void * data, michael@0: hb_destroy_func_t destroy, michael@0: hb_bool_t replace) michael@0: { michael@0: return hb_object_set_user_data (face, key, data, destroy, replace); michael@0: } michael@0: michael@0: /** michael@0: * hb_face_get_user_data: (skip) michael@0: * @face: a face. michael@0: * @key: michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer none): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void * michael@0: hb_face_get_user_data (hb_face_t *face, michael@0: hb_user_data_key_t *key) michael@0: { michael@0: return hb_object_get_user_data (face, key); michael@0: } michael@0: michael@0: /** michael@0: * hb_face_make_immutable: michael@0: * @face: a face. michael@0: * michael@0: * michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_face_make_immutable (hb_face_t *face) michael@0: { michael@0: if (hb_object_is_inert (face)) michael@0: return; michael@0: michael@0: face->immutable = true; michael@0: } michael@0: michael@0: /** michael@0: * hb_face_is_immutable: michael@0: * @face: a face. michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_bool_t michael@0: hb_face_is_immutable (hb_face_t *face) michael@0: { michael@0: return face->immutable; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * hb_face_reference_table: michael@0: * @face: a face. michael@0: * @tag: michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer full): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_blob_t * michael@0: hb_face_reference_table (hb_face_t *face, michael@0: hb_tag_t tag) michael@0: { michael@0: return face->reference_table (tag); michael@0: } michael@0: michael@0: /** michael@0: * hb_face_reference_blob: michael@0: * @face: a face. michael@0: * michael@0: * michael@0: * michael@0: * Return value: (transfer full): michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: hb_blob_t * michael@0: hb_face_reference_blob (hb_face_t *face) michael@0: { michael@0: return face->reference_table (HB_TAG_NONE); michael@0: } michael@0: michael@0: /** michael@0: * hb_face_set_index: michael@0: * @face: a face. michael@0: * @index: michael@0: * michael@0: * michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_face_set_index (hb_face_t *face, michael@0: unsigned int index) michael@0: { michael@0: if (hb_object_is_inert (face)) michael@0: return; michael@0: michael@0: face->index = index; michael@0: } michael@0: michael@0: /** michael@0: * hb_face_get_index: michael@0: * @face: a face. michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: unsigned int michael@0: hb_face_get_index (hb_face_t *face) michael@0: { michael@0: return face->index; michael@0: } michael@0: michael@0: /** michael@0: * hb_face_set_upem: michael@0: * @face: a face. michael@0: * @upem: michael@0: * michael@0: * michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_face_set_upem (hb_face_t *face, michael@0: unsigned int upem) michael@0: { michael@0: if (hb_object_is_inert (face)) michael@0: return; michael@0: michael@0: face->upem = upem; michael@0: } michael@0: michael@0: /** michael@0: * hb_face_get_upem: michael@0: * @face: a face. michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: unsigned int michael@0: hb_face_get_upem (hb_face_t *face) michael@0: { michael@0: return face->get_upem (); michael@0: } michael@0: michael@0: void michael@0: hb_face_t::load_upem (void) const michael@0: { michael@0: hb_blob_t *head_blob = OT::Sanitizer::sanitize (reference_table (HB_OT_TAG_head)); michael@0: const OT::head *head_table = OT::Sanitizer::lock_instance (head_blob); michael@0: upem = head_table->get_upem (); michael@0: hb_blob_destroy (head_blob); michael@0: } michael@0: michael@0: /** michael@0: * hb_face_set_glyph_count: michael@0: * @face: a face. michael@0: * @glyph_count: michael@0: * michael@0: * michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: void michael@0: hb_face_set_glyph_count (hb_face_t *face, michael@0: unsigned int glyph_count) michael@0: { michael@0: if (hb_object_is_inert (face)) michael@0: return; michael@0: michael@0: face->num_glyphs = glyph_count; michael@0: } michael@0: michael@0: /** michael@0: * hb_face_get_glyph_count: michael@0: * @face: a face. michael@0: * michael@0: * michael@0: * michael@0: * Return value: michael@0: * michael@0: * Since: 1.0 michael@0: **/ michael@0: unsigned int michael@0: hb_face_get_glyph_count (hb_face_t *face) michael@0: { michael@0: return face->get_num_glyphs (); michael@0: } michael@0: michael@0: void michael@0: hb_face_t::load_num_glyphs (void) const michael@0: { michael@0: hb_blob_t *maxp_blob = OT::Sanitizer::sanitize (reference_table (HB_OT_TAG_maxp)); michael@0: const OT::maxp *maxp_table = OT::Sanitizer::lock_instance (maxp_blob); michael@0: num_glyphs = maxp_table->get_num_glyphs (); michael@0: hb_blob_destroy (maxp_blob); michael@0: } michael@0: michael@0: