michael@0: /* michael@0: * Copyright © 2009 Red Hat, Inc. michael@0: * Copyright © 2011 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: #ifndef HB_FONT_PRIVATE_HH michael@0: #define HB_FONT_PRIVATE_HH michael@0: michael@0: #include "hb-private.hh" michael@0: michael@0: #include "hb-object-private.hh" michael@0: #include "hb-face-private.hh" michael@0: #include "hb-shaper-private.hh" michael@0: michael@0: michael@0: michael@0: /* michael@0: * hb_font_funcs_t michael@0: */ michael@0: michael@0: #define HB_FONT_FUNCS_IMPLEMENT_CALLBACKS \ michael@0: HB_FONT_FUNC_IMPLEMENT (glyph) \ michael@0: HB_FONT_FUNC_IMPLEMENT (glyph_h_advance) \ michael@0: HB_FONT_FUNC_IMPLEMENT (glyph_v_advance) \ michael@0: HB_FONT_FUNC_IMPLEMENT (glyph_h_origin) \ michael@0: HB_FONT_FUNC_IMPLEMENT (glyph_v_origin) \ michael@0: HB_FONT_FUNC_IMPLEMENT (glyph_h_kerning) \ michael@0: HB_FONT_FUNC_IMPLEMENT (glyph_v_kerning) \ michael@0: HB_FONT_FUNC_IMPLEMENT (glyph_extents) \ michael@0: HB_FONT_FUNC_IMPLEMENT (glyph_contour_point) \ michael@0: HB_FONT_FUNC_IMPLEMENT (glyph_name) \ michael@0: HB_FONT_FUNC_IMPLEMENT (glyph_from_name) \ michael@0: /* ^--- Add new callbacks here */ michael@0: michael@0: struct hb_font_funcs_t { michael@0: hb_object_header_t header; michael@0: ASSERT_POD (); michael@0: michael@0: hb_bool_t immutable; michael@0: michael@0: /* Don't access these directly. Call hb_font_get_*() instead. */ michael@0: michael@0: struct { michael@0: #define HB_FONT_FUNC_IMPLEMENT(name) hb_font_get_##name##_func_t name; michael@0: HB_FONT_FUNCS_IMPLEMENT_CALLBACKS michael@0: #undef HB_FONT_FUNC_IMPLEMENT michael@0: } get; michael@0: michael@0: struct { michael@0: #define HB_FONT_FUNC_IMPLEMENT(name) void *name; michael@0: HB_FONT_FUNCS_IMPLEMENT_CALLBACKS michael@0: #undef HB_FONT_FUNC_IMPLEMENT michael@0: } user_data; michael@0: michael@0: struct { michael@0: #define HB_FONT_FUNC_IMPLEMENT(name) hb_destroy_func_t name; michael@0: HB_FONT_FUNCS_IMPLEMENT_CALLBACKS michael@0: #undef HB_FONT_FUNC_IMPLEMENT michael@0: } destroy; michael@0: }; michael@0: michael@0: michael@0: michael@0: /* michael@0: * hb_font_t michael@0: */ michael@0: michael@0: struct hb_font_t { michael@0: hb_object_header_t header; michael@0: ASSERT_POD (); michael@0: michael@0: hb_bool_t immutable; michael@0: michael@0: hb_font_t *parent; michael@0: hb_face_t *face; michael@0: michael@0: int x_scale; michael@0: int y_scale; michael@0: michael@0: unsigned int x_ppem; michael@0: unsigned int y_ppem; michael@0: michael@0: hb_font_funcs_t *klass; michael@0: void *user_data; michael@0: hb_destroy_func_t destroy; michael@0: michael@0: struct hb_shaper_data_t shaper_data; michael@0: michael@0: michael@0: /* Convert from font-space to user-space */ michael@0: inline hb_position_t em_scale_x (int16_t v) { return em_scale (v, this->x_scale); } michael@0: inline hb_position_t em_scale_y (int16_t v) { return em_scale (v, this->y_scale); } michael@0: michael@0: /* Convert from parent-font user-space to our user-space */ michael@0: inline hb_position_t parent_scale_x_distance (hb_position_t v) { michael@0: if (unlikely (parent && parent->x_scale != x_scale)) michael@0: return (hb_position_t) (v * (int64_t) this->x_scale / this->parent->x_scale); michael@0: return v; michael@0: } michael@0: inline hb_position_t parent_scale_y_distance (hb_position_t v) { michael@0: if (unlikely (parent && parent->y_scale != y_scale)) michael@0: return (hb_position_t) (v * (int64_t) this->y_scale / this->parent->y_scale); michael@0: return v; michael@0: } michael@0: inline hb_position_t parent_scale_x_position (hb_position_t v) { michael@0: return parent_scale_x_distance (v); michael@0: } michael@0: inline hb_position_t parent_scale_y_position (hb_position_t v) { michael@0: return parent_scale_y_distance (v); michael@0: } michael@0: michael@0: inline void parent_scale_distance (hb_position_t *x, hb_position_t *y) { michael@0: *x = parent_scale_x_distance (*x); michael@0: *y = parent_scale_y_distance (*y); michael@0: } michael@0: inline void parent_scale_position (hb_position_t *x, hb_position_t *y) { michael@0: *x = parent_scale_x_position (*x); michael@0: *y = parent_scale_y_position (*y); michael@0: } michael@0: michael@0: michael@0: /* Public getters */ michael@0: michael@0: inline hb_bool_t has_glyph (hb_codepoint_t unicode) michael@0: { michael@0: hb_codepoint_t glyph; michael@0: return get_glyph (unicode, 0, &glyph); michael@0: } michael@0: michael@0: inline hb_bool_t get_glyph (hb_codepoint_t unicode, hb_codepoint_t variation_selector, michael@0: hb_codepoint_t *glyph) michael@0: { michael@0: *glyph = 0; michael@0: return klass->get.glyph (this, user_data, michael@0: unicode, variation_selector, glyph, michael@0: klass->user_data.glyph); michael@0: } michael@0: michael@0: inline hb_position_t get_glyph_h_advance (hb_codepoint_t glyph) michael@0: { michael@0: return klass->get.glyph_h_advance (this, user_data, michael@0: glyph, michael@0: klass->user_data.glyph_h_advance); michael@0: } michael@0: michael@0: inline hb_position_t get_glyph_v_advance (hb_codepoint_t glyph) michael@0: { michael@0: return klass->get.glyph_v_advance (this, user_data, michael@0: glyph, michael@0: klass->user_data.glyph_v_advance); michael@0: } michael@0: michael@0: inline hb_bool_t get_glyph_h_origin (hb_codepoint_t glyph, michael@0: hb_position_t *x, hb_position_t *y) michael@0: { michael@0: *x = *y = 0; michael@0: return klass->get.glyph_h_origin (this, user_data, michael@0: glyph, x, y, michael@0: klass->user_data.glyph_h_origin); michael@0: } michael@0: michael@0: inline hb_bool_t get_glyph_v_origin (hb_codepoint_t glyph, michael@0: hb_position_t *x, hb_position_t *y) michael@0: { michael@0: *x = *y = 0; michael@0: return klass->get.glyph_v_origin (this, user_data, michael@0: glyph, x, y, michael@0: klass->user_data.glyph_v_origin); michael@0: } michael@0: michael@0: inline hb_position_t get_glyph_h_kerning (hb_codepoint_t left_glyph, hb_codepoint_t right_glyph) michael@0: { michael@0: return klass->get.glyph_h_kerning (this, user_data, michael@0: left_glyph, right_glyph, michael@0: klass->user_data.glyph_h_kerning); michael@0: } michael@0: michael@0: inline hb_position_t get_glyph_v_kerning (hb_codepoint_t top_glyph, hb_codepoint_t bottom_glyph) michael@0: { michael@0: return klass->get.glyph_v_kerning (this, user_data, michael@0: top_glyph, bottom_glyph, michael@0: klass->user_data.glyph_v_kerning); michael@0: } michael@0: michael@0: inline hb_bool_t get_glyph_extents (hb_codepoint_t glyph, michael@0: hb_glyph_extents_t *extents) michael@0: { michael@0: memset (extents, 0, sizeof (*extents)); michael@0: return klass->get.glyph_extents (this, user_data, michael@0: glyph, michael@0: extents, michael@0: klass->user_data.glyph_extents); michael@0: } michael@0: michael@0: inline hb_bool_t get_glyph_contour_point (hb_codepoint_t glyph, unsigned int point_index, michael@0: hb_position_t *x, hb_position_t *y) michael@0: { michael@0: *x = *y = 0; michael@0: return klass->get.glyph_contour_point (this, user_data, michael@0: glyph, point_index, michael@0: x, y, michael@0: klass->user_data.glyph_contour_point); michael@0: } michael@0: michael@0: inline hb_bool_t get_glyph_name (hb_codepoint_t glyph, michael@0: char *name, unsigned int size) michael@0: { michael@0: if (size) *name = '\0'; michael@0: return klass->get.glyph_name (this, user_data, michael@0: glyph, michael@0: name, size, michael@0: klass->user_data.glyph_name); michael@0: } michael@0: michael@0: inline hb_bool_t get_glyph_from_name (const char *name, int len, /* -1 means nul-terminated */ michael@0: hb_codepoint_t *glyph) michael@0: { michael@0: *glyph = 0; michael@0: if (len == -1) len = strlen (name); michael@0: return klass->get.glyph_from_name (this, user_data, michael@0: name, len, michael@0: glyph, michael@0: klass->user_data.glyph_from_name); michael@0: } michael@0: michael@0: michael@0: /* A bit higher-level, and with fallback */ michael@0: michael@0: inline void get_glyph_advance_for_direction (hb_codepoint_t glyph, michael@0: hb_direction_t direction, michael@0: hb_position_t *x, hb_position_t *y) michael@0: { michael@0: if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) { michael@0: *x = get_glyph_h_advance (glyph); michael@0: *y = 0; michael@0: } else { michael@0: *x = 0; michael@0: *y = get_glyph_v_advance (glyph); michael@0: } michael@0: } michael@0: michael@0: /* Internal only */ michael@0: inline void guess_v_origin_minus_h_origin (hb_codepoint_t glyph, michael@0: hb_position_t *x, hb_position_t *y) michael@0: { michael@0: *x = get_glyph_h_advance (glyph) / 2; michael@0: michael@0: /* TODO use font_metics.ascent */ michael@0: *y = y_scale; michael@0: } michael@0: michael@0: inline void get_glyph_origin_for_direction (hb_codepoint_t glyph, michael@0: hb_direction_t direction, michael@0: hb_position_t *x, hb_position_t *y) michael@0: { michael@0: if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) michael@0: { michael@0: if (!get_glyph_h_origin (glyph, x, y) && michael@0: get_glyph_v_origin (glyph, x, y)) michael@0: { michael@0: hb_position_t dx, dy; michael@0: guess_v_origin_minus_h_origin (glyph, &dx, &dy); michael@0: *x -= dx; *y -= dy; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: if (!get_glyph_v_origin (glyph, x, y) && michael@0: get_glyph_h_origin (glyph, x, y)) michael@0: { michael@0: hb_position_t dx, dy; michael@0: guess_v_origin_minus_h_origin (glyph, &dx, &dy); michael@0: *x += dx; *y += dy; michael@0: } michael@0: } michael@0: } michael@0: michael@0: inline void add_glyph_origin_for_direction (hb_codepoint_t glyph, michael@0: hb_direction_t direction, michael@0: hb_position_t *x, hb_position_t *y) michael@0: { michael@0: hb_position_t origin_x, origin_y; michael@0: michael@0: get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y); michael@0: michael@0: *x += origin_x; michael@0: *y += origin_y; michael@0: } michael@0: michael@0: inline void subtract_glyph_origin_for_direction (hb_codepoint_t glyph, michael@0: hb_direction_t direction, michael@0: hb_position_t *x, hb_position_t *y) michael@0: { michael@0: hb_position_t origin_x, origin_y; michael@0: michael@0: get_glyph_origin_for_direction (glyph, direction, &origin_x, &origin_y); michael@0: michael@0: *x -= origin_x; michael@0: *y -= origin_y; michael@0: } michael@0: michael@0: inline void get_glyph_kerning_for_direction (hb_codepoint_t first_glyph, hb_codepoint_t second_glyph, michael@0: hb_direction_t direction, michael@0: hb_position_t *x, hb_position_t *y) michael@0: { michael@0: if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) { michael@0: *x = get_glyph_h_kerning (first_glyph, second_glyph); michael@0: *y = 0; michael@0: } else { michael@0: *x = 0; michael@0: *y = get_glyph_v_kerning (first_glyph, second_glyph); michael@0: } michael@0: } michael@0: michael@0: inline hb_bool_t get_glyph_extents_for_origin (hb_codepoint_t glyph, michael@0: hb_direction_t direction, michael@0: hb_glyph_extents_t *extents) michael@0: { michael@0: hb_bool_t ret = get_glyph_extents (glyph, extents); michael@0: michael@0: if (ret) michael@0: subtract_glyph_origin_for_direction (glyph, direction, &extents->x_bearing, &extents->y_bearing); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: inline hb_bool_t get_glyph_contour_point_for_origin (hb_codepoint_t glyph, unsigned int point_index, michael@0: hb_direction_t direction, michael@0: hb_position_t *x, hb_position_t *y) michael@0: { michael@0: hb_bool_t ret = get_glyph_contour_point (glyph, point_index, x, y); michael@0: michael@0: if (ret) michael@0: subtract_glyph_origin_for_direction (glyph, direction, x, y); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: /* Generates gidDDD if glyph has no name. */ michael@0: inline void michael@0: glyph_to_string (hb_codepoint_t glyph, michael@0: char *s, unsigned int size) michael@0: { michael@0: if (get_glyph_name (glyph, s, size)) return; michael@0: michael@0: if (size && snprintf (s, size, "gid%u", glyph) < 0) michael@0: *s = '\0'; michael@0: } michael@0: michael@0: /* Parses gidDDD and uniUUUU strings automatically. */ michael@0: inline hb_bool_t michael@0: glyph_from_string (const char *s, int len, /* -1 means nul-terminated */ michael@0: hb_codepoint_t *glyph) michael@0: { michael@0: if (get_glyph_from_name (s, len, glyph)) return true; michael@0: michael@0: if (len == -1) len = strlen (s); michael@0: michael@0: /* Straight glyph index. */ michael@0: if (hb_codepoint_parse (s, len, 10, glyph)) michael@0: return true; michael@0: michael@0: if (len > 3) michael@0: { michael@0: /* gidDDD syntax for glyph indices. */ michael@0: if (0 == strncmp (s, "gid", 3) && michael@0: hb_codepoint_parse (s + 3, len - 3, 10, glyph)) michael@0: return true; michael@0: michael@0: /* uniUUUU and other Unicode character indices. */ michael@0: hb_codepoint_t unichar; michael@0: if (0 == strncmp (s, "uni", 3) && michael@0: hb_codepoint_parse (s + 3, len - 3, 16, &unichar) && michael@0: get_glyph (unichar, 0, glyph)) michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: private: michael@0: inline hb_position_t em_scale (int16_t v, int scale) { return (hb_position_t) (v * (int64_t) scale / face->get_upem ()); } michael@0: }; michael@0: michael@0: #define HB_SHAPER_DATA_CREATE_FUNC_EXTRA_ARGS michael@0: #define HB_SHAPER_IMPLEMENT(shaper) HB_SHAPER_DATA_PROTOTYPE(shaper, font); michael@0: #include "hb-shaper-list.hh" michael@0: #undef HB_SHAPER_IMPLEMENT michael@0: #undef HB_SHAPER_DATA_CREATE_FUNC_EXTRA_ARGS michael@0: michael@0: michael@0: #endif /* HB_FONT_PRIVATE_HH */