Optimize positioning direction calculations
authorBehdad Esfahbod <behdad@behdad.org>
Thu, 5 Nov 2015 03:28:17 +0000 (19:28 -0800)
committerBehdad Esfahbod <behdad@behdad.org>
Thu, 5 Nov 2015 03:28:17 +0000 (19:28 -0800)
It makes the binary smaller AND faster.  Yumm!

src/hb-font-private.hh
src/hb-ot-shape.cc

index c05499d..952682d 100644 (file)
@@ -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)
index be9c438..455ebab 100644 (file)
@@ -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;
   }