From bee901b38ef3c26a04f69c299c8d5e028a0090e9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 4 Nov 2015 19:28:17 -0800 Subject: [PATCH] Optimize positioning direction calculations It makes the binary smaller AND faster. Yumm! --- src/hb-font-private.hh | 40 +++++++++++++++++++++++++++++++++++++++ src/hb-ot-shape.cc | 51 +++++++++++++++++++++++++++----------------------- 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/src/hb-font-private.hh b/src/hb-font-private.hh index c05499d..952682d 100644 --- a/src/hb-font-private.hh +++ b/src/hb-font-private.hh @@ -298,6 +298,26 @@ struct hb_font_t { } } + inline void add_glyph_h_origin (hb_codepoint_t glyph, + hb_position_t *x, hb_position_t *y) + { + hb_position_t origin_x, origin_y; + + get_glyph_h_origin (glyph, &origin_x, &origin_y); + + *x += origin_x; + *y += origin_y; + } + inline void add_glyph_v_origin (hb_codepoint_t glyph, + hb_position_t *x, hb_position_t *y) + { + hb_position_t origin_x, origin_y; + + get_glyph_v_origin (glyph, &origin_x, &origin_y); + + *x += origin_x; + *y += origin_y; + } inline void add_glyph_origin_for_direction (hb_codepoint_t glyph, hb_direction_t direction, hb_position_t *x, hb_position_t *y) @@ -310,6 +330,26 @@ struct hb_font_t { *y += origin_y; } + inline void subtract_glyph_h_origin (hb_codepoint_t glyph, + hb_position_t *x, hb_position_t *y) + { + hb_position_t origin_x, origin_y; + + get_glyph_h_origin (glyph, &origin_x, &origin_y); + + *x -= origin_x; + *y -= origin_y; + } + inline void subtract_glyph_v_origin (hb_codepoint_t glyph, + hb_position_t *x, hb_position_t *y) + { + hb_position_t origin_x, origin_y; + + get_glyph_v_origin (glyph, &origin_x, &origin_y); + + *x -= origin_x; + *y -= origin_y; + } inline void subtract_glyph_origin_for_direction (hb_codepoint_t glyph, hb_direction_t direction, hb_position_t *x, hb_position_t *y) diff --git a/src/hb-ot-shape.cc b/src/hb-ot-shape.cc index be9c438..455ebab 100644 --- a/src/hb-ot-shape.cc +++ b/src/hb-ot-shape.cc @@ -650,17 +650,26 @@ hb_ot_position_default (hb_ot_shape_context_t *c) unsigned int count = c->buffer->len; hb_glyph_info_t *info = c->buffer->info; hb_glyph_position_t *pos = c->buffer->pos; - for (unsigned int i = 0; i < count; i++) - { - c->font->get_glyph_advance_for_direction (info[i].codepoint, - direction, - &pos[i].x_advance, - &pos[i].y_advance); - c->font->subtract_glyph_origin_for_direction (info[i].codepoint, - direction, - &pos[i].x_offset, - &pos[i].y_offset); + if (HB_DIRECTION_IS_HORIZONTAL (direction)) + { + for (unsigned int i = 0; i < count; i++) + { + pos[i].x_advance = c->font->get_glyph_h_advance (info[i].codepoint); + c->font->subtract_glyph_h_origin (info[i].codepoint, + &pos[i].x_offset, + &pos[i].y_offset); + } + } + else + { + for (unsigned int i = 0; i < count; i++) + { + pos[i].y_advance = c->font->get_glyph_v_advance (info[i].codepoint); + c->font->subtract_glyph_v_origin (info[i].codepoint, + &pos[i].x_offset, + &pos[i].y_offset); + } } if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK) _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer); @@ -708,23 +717,19 @@ hb_ot_position_complex (hb_ot_shape_context_t *c) hb_glyph_info_t *info = c->buffer->info; hb_glyph_position_t *pos = c->buffer->pos; - /* Change glyph origin to what GPOS expects, apply GPOS, change it back. */ + /* Change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */ - for (unsigned int i = 0; i < count; i++) { - c->font->add_glyph_origin_for_direction (info[i].codepoint, - HB_DIRECTION_LTR, - &pos[i].x_offset, - &pos[i].y_offset); - } + for (unsigned int i = 0; i < count; i++) + c->font->add_glyph_h_origin (info[i].codepoint, + &pos[i].x_offset, + &pos[i].y_offset); c->plan->position (c->font, c->buffer); - for (unsigned int i = 0; i < count; i++) { - c->font->subtract_glyph_origin_for_direction (info[i].codepoint, - HB_DIRECTION_LTR, - &pos[i].x_offset, - &pos[i].y_offset); - } + for (unsigned int i = 0; i < count; i++) + c->font->subtract_glyph_h_origin (info[i].codepoint, + &pos[i].x_offset, + &pos[i].y_offset); ret = true; } -- 2.7.4