[font] Centralize glyph-extents scaling

Needs more testing...

Some rounding was removed, namely in cff1 and cff2.
This commit is contained in:
Behdad Esfahbod 2022-12-22 07:26:14 -07:00
parent 4622be7f84
commit b0abbfd868
7 changed files with 40 additions and 32 deletions

View File

@ -247,19 +247,14 @@ struct glyf_accelerator_t
extents->y_bearing = 0; extents->y_bearing = 0;
return; return;
} }
if (scaled)
{
extents->x_bearing = font->em_scalef_x (min_x);
extents->width = font->em_scalef_x (max_x) - extents->x_bearing;
extents->y_bearing = font->em_scalef_y (max_y);
extents->height = font->em_scalef_y (min_y) - extents->y_bearing;
}
else
{ {
extents->x_bearing = roundf (min_x); extents->x_bearing = roundf (min_x);
extents->width = roundf (max_x - extents->x_bearing); extents->width = roundf (max_x - extents->x_bearing);
extents->y_bearing = roundf (max_y); extents->y_bearing = roundf (max_y);
extents->height = roundf (min_y - extents->y_bearing); extents->height = roundf (min_y - extents->y_bearing);
if (scaled)
font->scale_glyph_extents (extents);
} }
} }

View File

@ -179,6 +179,14 @@ struct hb_font_t
*y = parent_scale_y_position (*y); *y = parent_scale_y_position (*y);
} }
void scale_glyph_extents (hb_glyph_extents_t *extents)
{
extents->x_bearing = em_scale_x (extents->x_bearing);
extents->y_bearing = em_scale_x (extents->y_bearing);
extents->width = em_scale_x (extents->width);
extents->height = em_scale_x (extents->height);
}
/* Public getters */ /* Public getters */

View File

@ -422,8 +422,8 @@ bool OT::cff1::accelerator_t::get_extents (hb_font_t *font, hb_codepoint_t glyph
} }
else else
{ {
extents->x_bearing = font->em_scalef_x (bounds.min.x.to_real ()); extents->x_bearing = bounds.min.x.to_int ();
extents->width = font->em_scalef_x (bounds.max.x.to_real ()) - extents->x_bearing; extents->width = bounds.max.x.to_int () - extents->x_bearing;
} }
if (bounds.min.y >= bounds.max.y) if (bounds.min.y >= bounds.max.y)
{ {
@ -432,10 +432,12 @@ bool OT::cff1::accelerator_t::get_extents (hb_font_t *font, hb_codepoint_t glyph
} }
else else
{ {
extents->y_bearing = font->em_scalef_y (bounds.max.y.to_real ()); extents->y_bearing = bounds.max.y.to_int ();
extents->height = font->em_scalef_y (bounds.min.y.to_real ()) - extents->y_bearing; extents->height = bounds.min.y.to_int () - extents->y_bearing;
} }
font->scale_glyph_extents (extents);
return true; return true;
} }

View File

@ -124,8 +124,8 @@ bool OT::cff2::accelerator_t::get_extents (hb_font_t *font,
} }
else else
{ {
extents->x_bearing = font->em_scalef_x (param.min_x.to_real ()); extents->x_bearing = param.min_x.to_int ();
extents->width = font->em_scalef_x (param.max_x.to_real ()) - extents->x_bearing; extents->width = param.max_x.to_int () - extents->x_bearing;
} }
if (param.min_y >= param.max_y) if (param.min_y >= param.max_y)
{ {
@ -134,10 +134,12 @@ bool OT::cff2::accelerator_t::get_extents (hb_font_t *font,
} }
else else
{ {
extents->y_bearing = font->em_scalef_y (param.max_y.to_real ()); extents->y_bearing = param.max_y.to_int ();
extents->height = font->em_scalef_y (param.min_y.to_real ()) - extents->y_bearing; extents->height = param.min_y.to_int () - extents->y_bearing;
} }
font->scale_glyph_extents (extents);
return true; return true;
} }

View File

@ -82,10 +82,12 @@ struct SmallGlyphMetrics
void get_extents (hb_font_t *font, hb_glyph_extents_t *extents) const void get_extents (hb_font_t *font, hb_glyph_extents_t *extents) const
{ {
extents->x_bearing = font->em_scale_x (bearingX); extents->x_bearing = bearingX;
extents->y_bearing = font->em_scale_y (bearingY); extents->y_bearing = bearingY;
extents->width = font->em_scale_x (width); extents->width = width;
extents->height = font->em_scale_y (-static_cast<int>(height)); extents->height = -static_cast<int> (height);
font->scale_glyph_extents (extents);
} }
HBUINT8 height; HBUINT8 height;

View File

@ -1603,10 +1603,7 @@ struct COLR
extents, extents,
instancer)) instancer))
{ {
extents->x_bearing = font->em_scale_x (extents->x_bearing); font->scale_glyph_extents (extents);
extents->y_bearing = font->em_scale_x (extents->y_bearing);
extents->width = font->em_scale_x (extents->width);
extents->height = font->em_scale_x (extents->height);
return true; return true;
} }

View File

@ -313,19 +313,21 @@ struct sbix
if (strike_ppem) if (strike_ppem)
{ {
float scale = font->face->get_upem () / (float) strike_ppem; float scale = font->face->get_upem () / (float) strike_ppem;
extents->x_bearing = font->em_scalef_x (extents->x_bearing * scale); extents->x_bearing = roundf (extents->x_bearing * scale);
extents->y_bearing = font->em_scalef_y (extents->y_bearing * scale); extents->y_bearing = roundf (extents->y_bearing * scale);
extents->width = font->em_scalef_x (extents->width * scale); extents->width = roundf (extents->width * scale);
extents->height = font->em_scalef_y (extents->height * scale); extents->height = roundf (extents->height * scale);
} }
else else
{ {
extents->x_bearing = font->em_scale_x (extents->x_bearing); extents->x_bearing = extents->x_bearing;
extents->y_bearing = font->em_scale_y (extents->y_bearing); extents->y_bearing = extents->y_bearing;
extents->width = font->em_scale_x (extents->width); extents->width = extents->width;
extents->height = font->em_scale_y (extents->height); extents->height = extents->height;
} }
font->scale_glyph_extents (extents);
hb_blob_destroy (blob); hb_blob_destroy (blob);
return strike_ppem; return strike_ppem;