Merge pull request #3497 from harfbuzz/vertical-origin

[ot-font] Fix vertical-origin fallback to match FreeType
diff --git a/src/hb-ft.cc b/src/hb-ft.cc
index 40311e1..0cfbb22 100644
--- a/src/hb-ft.cc
+++ b/src/hb-ft.cc
@@ -382,6 +382,7 @@
 
   /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates
    * have a Y growing upward.  Hence the extra negation. */
+
   return (-v + (1<<9)) >> 10;
 }
 #endif
diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc
index 77d3f63..0f44ee4 100644
--- a/src/hb-ot-font.cc
+++ b/src/hb-ot-font.cc
@@ -131,11 +131,25 @@
   const hb_ot_face_t *ot_face = (const hb_ot_face_t *) font_data;
   const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx;
 
-  for (unsigned int i = 0; i < count; i++)
+  if (vmtx.has_data ())
+    for (unsigned int i = 0; i < count; i++)
+    {
+      *first_advance = font->em_scale_y (-(int) vmtx.get_advance (*first_glyph, font));
+      first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
+      first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
+    }
+  else
   {
-    *first_advance = font->em_scale_y (-(int) vmtx.get_advance (*first_glyph, font));
-    first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
-    first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
+    hb_font_extents_t font_extents;
+    font->get_h_extents_with_fallback (&font_extents);
+    hb_position_t advance = -(font_extents.ascender - font_extents.descender);
+
+    for (unsigned int i = 0; i < count; i++)
+    {
+      *first_advance = advance;
+      first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
+      first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
+    }
   }
 }
 #endif
@@ -163,9 +177,19 @@
   hb_glyph_extents_t extents = {0};
   if (ot_face->glyf->get_extents (font, glyph, &extents))
   {
-    const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx;
-    hb_position_t tsb = vmtx.get_side_bearing (font, glyph);
-    *y = extents.y_bearing + font->em_scale_y (tsb);
+    if (ot_face->vmtx->has_data ())
+    {
+      const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx;
+      hb_position_t tsb = vmtx.get_side_bearing (font, glyph);
+      *y = extents.y_bearing + font->em_scale_y (tsb);
+      return true;
+    }
+
+    hb_font_extents_t font_extents;
+    font->get_h_extents_with_fallback (&font_extents);
+    hb_position_t advance = font_extents.ascender - font_extents.descender;
+    int diff = advance - -extents.height;
+    *y = extents.y_bearing + (diff >> 1);
     return true;
   }
 
diff --git a/src/hb-ot-hmtx-table.hh b/src/hb-ot-hmtx-table.hh
index 7487e40..d5e1fc9 100644
--- a/src/hb-ot-hmtx-table.hh
+++ b/src/hb-ot-hmtx-table.hh
@@ -170,13 +170,12 @@
   {
     friend struct hmtxvmtx;
 
-    accelerator_t (hb_face_t *face,
-		   unsigned int default_advance_ = 0)
+    accelerator_t (hb_face_t *face)
     {
       table = hb_sanitize_context_t ().reference_table<hmtxvmtx> (face, T::tableTag);
       var_table = hb_sanitize_context_t ().reference_table<HVARVVAR> (face, T::variationsTag);
 
-      default_advance = default_advance_ ? default_advance_ : hb_face_get_upem (face);
+      default_advance = T::is_horizontal ? hb_face_get_upem (face) / 2 : hb_face_get_upem (face);
 
       /* Populate count variables and sort them out as we go */
 
@@ -220,6 +219,8 @@
       var_table.destroy ();
     }
 
+    bool has_data () const { return (bool) num_bearings; }
+
     int get_side_bearing (hb_codepoint_t glyph) const
     {
       if (glyph < num_long_metrics)
diff --git a/test/api/test-ot-metrics-tt-var.c b/test/api/test-ot-metrics-tt-var.c
index 9871c08..f2587d8 100644
--- a/test/api/test-ot-metrics-tt-var.c
+++ b/test/api/test-ot-metrics-tt-var.c
@@ -80,7 +80,7 @@
   hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);
 
   g_assert_cmpint (x, ==, 0);
-  g_assert_cmpint (y, ==, -1000);
+  g_assert_cmpint (y, ==, -1257);
 
   float coords[1] = { 500.0f };
   hb_font_set_var_coords_design (font, coords, 1);
@@ -92,7 +92,7 @@
   hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);
 
   g_assert_cmpint (x, ==, 0);
-  g_assert_cmpint (y, ==, -1000);
+  g_assert_cmpint (y, ==, -1257);
 
   hb_font_destroy (font);
 }
diff --git a/test/shape/data/in-house/tests/collections.tests b/test/shape/data/in-house/tests/collections.tests
index 50d6d92..defdb2b 100644
--- a/test/shape/data/in-house/tests/collections.tests
+++ b/test/shape/data/in-house/tests/collections.tests
@@ -1,6 +1,6 @@
 ../fonts/DFONT.dfont;--face-index=0 --font-funcs=ot;U+2026,U+0020,U+002E;[ellipsis=0+723|space=1+250|period=2+241]
-../fonts/DFONT.dfont;--face-index=1 --font-funcs=ot;U+2026,U+0020,U+002E;[gid0=0+1000|gid0=1+1000|gid0=2+1000]
-../fonts/DFONT.dfont;--face-index=2 --font-funcs=ot;U+2026,U+0020,U+002E;[gid0=0+1000|gid0=1+1000|gid0=2+1000]
+../fonts/DFONT.dfont;--face-index=1 --font-funcs=ot;U+2026,U+0020,U+002E;[gid0=0+500|gid0=1+500|gid0=2+500]
+../fonts/DFONT.dfont;--face-index=2 --font-funcs=ot;U+2026,U+0020,U+002E;[gid0=0+500|gid0=1+500|gid0=2+500]
 ../fonts/TTC.ttc;--face-index=0 --font-funcs=ot;U+2026,U+0020,U+002E;[ellipsis=0+723|space=1+250|period=2+241]
 ../fonts/TTC.ttc;--face-index=1 --font-funcs=ot;U+2026,U+0020,U+002E;[ellipsis=0+723|space=1+250|period=2+241]
-../fonts/TTC.ttc;--face-index=2 --font-funcs=ot;U+2026,U+0020,U+002E;[gid0=0+1000|gid0=1+1000|gid0=2+1000]
+../fonts/TTC.ttc;--face-index=2 --font-funcs=ot;U+2026,U+0020,U+002E;[gid0=0+500|gid0=1+500|gid0=2+500]
diff --git a/test/shape/data/in-house/tests/indic-decompose.tests b/test/shape/data/in-house/tests/indic-decompose.tests
index 132011f..c06dd35 100644
--- a/test/shape/data/in-house/tests/indic-decompose.tests
+++ b/test/shape/data/in-house/tests/indic-decompose.tests
@@ -1 +1 @@
-../fonts/932ad5132c2761297c74e9976fe25b08e5ffa10b.ttf;--font-funcs=ot;U+09DC,U+0020,U+09DD,U+0020,U+09A1,U+09BC,U+0020,U+09A2,U+09BC;[bn_rha=0+1024|space=1+1024|bn_yya=2+1024|space=3+1024|bn_dda=4+1024|bn_nukta=4+1024|space=6+1024|bn_ddha=7+1024|bn_nukta=7+1024]
+../fonts/932ad5132c2761297c74e9976fe25b08e5ffa10b.ttf;--font-funcs=ot;U+09DC,U+0020,U+09DD,U+0020,U+09A1,U+09BC,U+0020,U+09A2,U+09BC;[bn_rha=0+512|space=1+512|bn_yya=2+512|space=3+512|bn_dda=4+512|bn_nukta=4+512|space=6+512|bn_ddha=7+512|bn_nukta=7+512]
diff --git a/test/shape/data/in-house/tests/spaces.tests b/test/shape/data/in-house/tests/spaces.tests
index 36cfc0f..a73ab14 100644
--- a/test/shape/data/in-house/tests/spaces.tests
+++ b/test/shape/data/in-house/tests/spaces.tests
@@ -15,20 +15,20 @@
 ../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot;U+202F;[gid1=0+280]
 ../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot;U+205F;[gid1=0+455]
 ../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot;U+3000;[gid1=0+2048]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+0020;[gid1=0@-280,0+0,-2048]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+00A0;[gid1=0@-280,0+0,-2048]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+1680;[gid0=0@-346,0+0,-2048]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2000;[gid1=0@-280,0+0,-1024]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2001;[gid1=0@-280,0+0,-2048]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2002;[gid1=0@-280,0+0,-1024]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2003;[gid1=0@-280,0+0,-2048]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2004;[gid1=0@-280,0+0,-683]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2005;[gid1=0@-280,0+0,-512]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2006;[gid1=0@-280,0+0,-341]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2007;[gid1=0@-280,0+0,-2048]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2008;[gid1=0@-280,0+0,-2048]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2009;[gid1=0@-280,0+0,-410]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+200A;[gid1=0@-280,0+0,-128]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+202F;[gid1=0@-280,0+0,-1024]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+205F;[gid1=0@-280,0+0,-455]
-../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+3000;[gid1=0@-280,0+0,-2048]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+0020;[gid1=0@-280,-1263+0,-2526]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+00A0;[gid1=0@-280,-1263+0,-2526]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+1680;[gid0=0@-346,-1263+0,-2526]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2000;[gid1=0@-280,-1263+0,-1024]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2001;[gid1=0@-280,-1263+0,-2048]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2002;[gid1=0@-280,-1263+0,-1024]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2003;[gid1=0@-280,-1263+0,-2048]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2004;[gid1=0@-280,-1263+0,-683]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2005;[gid1=0@-280,-1263+0,-512]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2006;[gid1=0@-280,-1263+0,-341]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2007;[gid1=0@-280,-1263+0,-2526]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2008;[gid1=0@-280,-1263+0,-2526]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+2009;[gid1=0@-280,-1263+0,-410]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+200A;[gid1=0@-280,-1263+0,-128]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+202F;[gid1=0@-280,-1263+0,-1263]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+205F;[gid1=0@-280,-1263+0,-455]
+../fonts/1c2c3fc37b2d4c3cb2ef726c6cdaaabd4b7f3eb9.ttf;--font-funcs=ot --direction=ttb;U+3000;[gid1=0@-280,-1263+0,-2048]
diff --git a/test/shape/data/in-house/tests/vertical.tests b/test/shape/data/in-house/tests/vertical.tests
index 22bf266..0e1b106 100644
--- a/test/shape/data/in-house/tests/vertical.tests
+++ b/test/shape/data/in-house/tests/vertical.tests
@@ -1,4 +1,4 @@
 ../fonts/191826b9643e3f124d865d617ae609db6a2ce203.ttf;--direction=t --font-funcs=ft;U+300C;[uni300C.vert=0@-512,-578+0,-1024]
 ../fonts/f9b1dd4dcb515e757789a22cb4241107746fd3d0.ttf;--direction=t --font-funcs=ft;U+0041,U+0042;[gid1=0@-654,-2128+0,-2789|gid2=1@-665,-2125+0,-2789]
-../fonts/f9b1dd4dcb515e757789a22cb4241107746fd3d0.ttf;--direction=t --font-funcs=ot;U+0041,U+0042;[gid1=0@-654,-1468+0,-2048|gid2=1@-665,-1462+0,-2048]
+../fonts/f9b1dd4dcb515e757789a22cb4241107746fd3d0.ttf;--direction=t --font-funcs=ot;U+0041,U+0042;[gid1=0@-654,-2128+0,-2789|gid2=1@-665,-2125+0,-2789]
 ../fonts/4cbbc461be066fccc611dcc634af6e8cb2705537.ttf;--direction=t --font-funcs=ot;U+FF38;[gid2=0@-500,-867+0,-1000]