[font] Remove division when scaling
authorBehdad Esfahbod <behdad@behdad.org>
Fri, 5 Jul 2019 20:56:45 +0000 (13:56 -0700)
committerBehdad Esfahbod <behdad@behdad.org>
Fri, 5 Jul 2019 20:56:45 +0000 (13:56 -0700)
Yoohoo.  This seems to be precise enough!  Let's see if it sticks.
I'm asking Dominik to run this in Chrome test suite and report.

Fixes https://github.com/harfbuzz/harfbuzz/issues/1801

src/hb-font.hh
src/hb-ot-math-table.hh

index e379f12..4adf6ae 100644 (file)
@@ -129,16 +129,16 @@ struct hb_font_t
 
 
   /* Convert from font-space to user-space */
-  int dir_scale (hb_direction_t direction)
-  { return HB_DIRECTION_IS_VERTICAL(direction) ? y_scale : x_scale; }
-  hb_position_t em_scale_x (int16_t v) { return em_scale (v, x_scale); }
-  hb_position_t em_scale_y (int16_t v) { return em_scale (v, y_scale); }
-  hb_position_t em_scalef_x (float v) { return em_scalef (v, this->x_scale); }
-  hb_position_t em_scalef_y (float v) { return em_scalef (v, this->y_scale); }
+  int64_t dir_mult (hb_direction_t direction)
+  { return HB_DIRECTION_IS_VERTICAL(direction) ? y_mult : x_mult; }
+  hb_position_t em_scale_x (int16_t v) { return em_mult (v, x_mult); }
+  hb_position_t em_scale_y (int16_t v) { return em_mult (v, y_mult); }
+  hb_position_t em_scalef_x (float v) { return em_scalef (v, x_scale); }
+  hb_position_t em_scalef_y (float v) { return em_scalef (v, y_scale); }
   float em_fscale_x (int16_t v) { return em_fscale (v, x_scale); }
   float em_fscale_y (int16_t v) { return em_fscale (v, y_scale); }
   hb_position_t em_scale_dir (int16_t v, hb_direction_t direction)
-  { return em_scale (v, dir_scale (direction)); }
+  { return em_mult (v, dir_mult (direction)); }
 
   /* Convert from parent-font user-space to our user-space */
   hb_position_t parent_scale_x_distance (hb_position_t v)
@@ -616,14 +616,9 @@ struct hb_font_t
     y_mult = ((int64_t) y_scale << 16) / upem;
   }
 
-  hb_position_t em_scale (int16_t v, int scale)
+  hb_position_t em_mult (int16_t v, int64_t mult)
   {
-    signed upem = face->get_upem ();
-    signed upem2 = upem >> 1;
-    int64_t scaled = v * (int64_t) scale;
-
-    scaled += scaled >= 0 ? upem2 : -upem2; /* Round. */
-    return (hb_position_t) (scaled / upem);
+    return (hb_position_t) ((v * mult) >> 16);
   }
   hb_position_t em_scalef (float v, int scale)
   { return (hb_position_t) roundf (v * scale / face->get_upem ()); }
index 0b16e0b..b4592cc 100644 (file)
@@ -453,14 +453,14 @@ struct MathGlyphPartRecord
   }
 
   void extract (hb_ot_math_glyph_part_t &out,
-               int scale,
+               int64_t mult,
                hb_font_t *font) const
   {
     out.glyph                  = glyph;
 
-    out.start_connector_length = font->em_scale (startConnectorLength, scale);
-    out.end_connector_length   = font->em_scale (endConnectorLength, scale);
-    out.full_advance           = font->em_scale (fullAdvance, scale);
+    out.start_connector_length = font->em_mult (startConnectorLength, mult);
+    out.end_connector_length   = font->em_mult (endConnectorLength, mult);
+    out.full_advance           = font->em_mult (fullAdvance, mult);
 
     static_assert ((unsigned int) HB_OT_MATH_GLYPH_PART_FLAG_EXTENDER ==
                   (unsigned int) PartFlags::Extender, "");
@@ -508,11 +508,11 @@ struct MathGlyphAssembly
   {
     if (parts_count)
     {
-      int scale = font->dir_scale (direction);
+      int64_t mult = font->dir_mult (direction);
       hb_array_t<const MathGlyphPartRecord> arr = partRecords.sub_array (start_offset, parts_count);
       unsigned int count = arr.length;
       for (unsigned int i = 0; i < count; i++)
-       arr[i].extract (parts[i], scale, font);
+       arr[i].extract (parts[i], mult, font);
     }
 
     if (italics_correction)
@@ -553,13 +553,13 @@ struct MathGlyphConstruction
   {
     if (variants_count)
     {
-      int scale = font->dir_scale (direction);
+      int64_t mult = font->dir_mult (direction);
       hb_array_t<const MathGlyphVariantRecord> arr = mathGlyphVariantRecord.sub_array (start_offset, variants_count);
       unsigned int count = arr.length;
       for (unsigned int i = 0; i < count; i++)
       {
        variants[i].glyph = arr[i].variantGlyph;
-       variants[i].advance = font->em_scale (arr[i].advanceMeasurement, scale);
+       variants[i].advance = font->em_mult (arr[i].advanceMeasurement, mult);
       }
     }
     return mathGlyphVariantRecord.len;