[ot-font] Apply font scaling before turning to int

This commit is contained in:
Ebrahim Byagowi 2019-10-06 18:09:14 +03:30
parent f2339964b7
commit d73cdcf361
10 changed files with 66 additions and 75 deletions

View File

@ -306,14 +306,14 @@ bool _get_bounds (const OT::cff1::accelerator_t *cff, hb_codepoint_t glyph, boun
return true; return true;
} }
bool OT::cff1::accelerator_t::get_extents (hb_codepoint_t glyph, hb_glyph_extents_t *extents) const bool OT::cff1::accelerator_t::get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents) const
{ {
#ifdef HB_NO_OT_FONT_CFF #ifdef HB_NO_OT_FONT_CFF
/* XXX Remove check when this code moves to .hh file. */ /* XXX Remove check when this code moves to .hh file. */
return true; return true;
#endif #endif
bounds_t bounds; bounds_t bounds;
if (!_get_bounds (this, glyph, bounds)) if (!_get_bounds (this, glyph, bounds))
return false; return false;
@ -325,8 +325,8 @@ bool OT::cff1::accelerator_t::get_extents (hb_codepoint_t glyph, hb_glyph_extent
} }
else else
{ {
extents->x_bearing = (int32_t)bounds.min.x.floor (); extents->x_bearing = font->em_scalef_x (bounds.min.x.to_real ());
extents->width = (int32_t)bounds.max.x.ceil () - extents->x_bearing; extents->width = font->em_scalef_x (bounds.max.x.to_real () - extents->x_bearing);
} }
if (bounds.min.y >= bounds.max.y) if (bounds.min.y >= bounds.max.y)
{ {
@ -335,8 +335,8 @@ bool OT::cff1::accelerator_t::get_extents (hb_codepoint_t glyph, hb_glyph_extent
} }
else else
{ {
extents->y_bearing = (int32_t)bounds.max.y.ceil (); extents->y_bearing = font->em_scalef_y (bounds.max.y.to_real ());
extents->height = (int32_t)bounds.min.y.floor () - extents->y_bearing; extents->height = font->em_scalef_x (bounds.min.y.to_real () - extents->y_bearing);
} }
return true; return true;

View File

@ -1196,7 +1196,7 @@ struct cff1
struct accelerator_t : accelerator_templ_t<cff1_private_dict_opset_t, cff1_private_dict_values_t> struct accelerator_t : accelerator_templ_t<cff1_private_dict_opset_t, cff1_private_dict_values_t>
{ {
HB_INTERNAL bool get_extents (hb_codepoint_t glyph, hb_glyph_extents_t *extents) const; HB_INTERNAL bool get_extents (hb_font_t *font, hb_codepoint_t glyph, hb_glyph_extents_t *extents) const;
HB_INTERNAL bool get_seac_components (hb_codepoint_t glyph, hb_codepoint_t *base, hb_codepoint_t *accent) const; HB_INTERNAL bool get_seac_components (hb_codepoint_t glyph, hb_codepoint_t *base, hb_codepoint_t *accent) const;
}; };

View File

@ -125,8 +125,8 @@ bool OT::cff2::accelerator_t::get_extents (hb_font_t *font,
} }
else else
{ {
extents->x_bearing = (int32_t)param.min_x.floor (); extents->x_bearing = font->em_scalef_x (param.min_x.to_real ());
extents->width = (int32_t)param.max_x.ceil () - extents->x_bearing; extents->width = font->em_scalef_x (param.max_x.to_real () - extents->x_bearing);
} }
if (param.min_y >= param.max_y) if (param.min_y >= param.max_y)
{ {
@ -135,8 +135,8 @@ bool OT::cff2::accelerator_t::get_extents (hb_font_t *font,
} }
else else
{ {
extents->y_bearing = (int32_t)param.max_y.ceil (); extents->y_bearing = font->em_scalef_y (param.max_y.to_real ());
extents->height = (int32_t)param.min_y.floor () - extents->y_bearing; extents->height = font->em_scalef_y (param.min_y.to_real () - extents->y_bearing);
} }
return true; return true;

View File

@ -51,12 +51,12 @@ struct SmallGlyphMetrics
return_trace (c->check_struct (this)); return_trace (c->check_struct (this));
} }
void get_extents (hb_glyph_extents_t *extents) const void get_extents (hb_font_t *font, hb_glyph_extents_t *extents) const
{ {
extents->x_bearing = bearingX; extents->x_bearing = font->em_scale_x (bearingX);
extents->y_bearing = bearingY; extents->y_bearing = font->em_scale_y (bearingY);
extents->width = width; extents->width = font->em_scale_x (width);
extents->height = - (hb_position_t) height; extents->height = font->em_scale_y (-height);
} }
HBUINT8 height; HBUINT8 height;
@ -424,7 +424,7 @@ struct CBDT
return false; return false;
const GlyphBitmapDataFormat17& glyphFormat17 = const GlyphBitmapDataFormat17& glyphFormat17 =
StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset); StructAtOffset<GlyphBitmapDataFormat17> (this->cbdt, image_offset);
glyphFormat17.glyphMetrics.get_extents (extents); glyphFormat17.glyphMetrics.get_extents (font, extents);
break; break;
} }
case 18: { case 18: {
@ -432,7 +432,7 @@ struct CBDT
return false; return false;
const GlyphBitmapDataFormat18& glyphFormat18 = const GlyphBitmapDataFormat18& glyphFormat18 =
StructAtOffset<GlyphBitmapDataFormat18> (this->cbdt, image_offset); StructAtOffset<GlyphBitmapDataFormat18> (this->cbdt, image_offset);
glyphFormat18.glyphMetrics.get_extents (extents); glyphFormat18.glyphMetrics.get_extents (font, extents);
break; break;
} }
default: default:

View File

@ -243,10 +243,10 @@ 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 = roundf (extents->x_bearing * scale); extents->x_bearing = font->em_scalef_x (extents->x_bearing * scale);
extents->y_bearing = roundf (extents->y_bearing * scale); extents->y_bearing = font->em_scalef_y (extents->y_bearing * scale);
extents->width = roundf (extents->width * scale); extents->width = font->em_scalef_x (extents->width * scale);
extents->height = roundf (extents->height * scale); extents->height = font->em_scalef_y (extents->height * scale);
} }
hb_blob_destroy (blob); hb_blob_destroy (blob);

View File

@ -164,7 +164,7 @@ hb_ot_get_glyph_v_origin (hb_font_t *font,
{ {
const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx; const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx;
hb_position_t tsb = vmtx.get_side_bearing (font, glyph); hb_position_t tsb = vmtx.get_side_bearing (font, glyph);
*y = font->em_scale_y (extents.y_bearing + tsb); *y = extents.y_bearing + font->em_scale_y (tsb);
return true; return true;
} }
@ -190,7 +190,7 @@ hb_ot_get_glyph_extents (hb_font_t *font,
#endif #endif
if (!ret) ret = ot_face->glyf->get_extents (font, glyph, extents); if (!ret) ret = ot_face->glyf->get_extents (font, glyph, extents);
#ifndef HB_NO_OT_FONT_CFF #ifndef HB_NO_OT_FONT_CFF
if (!ret) ret = ot_face->cff1->get_extents (glyph, extents); if (!ret) ret = ot_face->cff1->get_extents (font, glyph, extents);
if (!ret) ret = ot_face->cff2->get_extents (font, glyph, extents); if (!ret) ret = ot_face->cff2->get_extents (font, glyph, extents);
#endif #endif
#if !defined(HB_NO_OT_FONT_BITMAP) && !defined(HB_NO_COLOR) #if !defined(HB_NO_OT_FONT_BITMAP) && !defined(HB_NO_COLOR)
@ -198,10 +198,6 @@ hb_ot_get_glyph_extents (hb_font_t *font,
#endif #endif
// TODO Hook up side-bearings variations. // TODO Hook up side-bearings variations.
extents->x_bearing = font->em_scale_x (extents->x_bearing);
extents->y_bearing = font->em_scale_y (extents->y_bearing);
extents->width = font->em_scale_x (extents->width);
extents->height = font->em_scale_y (extents->height);
return ret; return ret;
} }
@ -319,13 +315,13 @@ hb_ot_font_set_funcs (hb_font_t *font)
int int
hb_ot_get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical) hb_ot_get_side_bearing_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical)
{ {
return font->face->table.glyf->get_side_bearing_var (glyph, font->coords, font->num_coords, is_vertical); return font->face->table.glyf->get_side_bearing_var (font, glyph, is_vertical);
} }
unsigned unsigned
hb_ot_get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical) hb_ot_get_advance_var_tt (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical)
{ {
return font->face->table.glyf->get_advance_var (glyph, font->coords, font->num_coords, is_vertical); return font->face->table.glyf->get_advance_var (font, glyph, is_vertical);
} }
#endif #endif

View File

@ -803,13 +803,12 @@ struct glyf
return true; return true;
} }
bool get_var_extents_and_phantoms (hb_codepoint_t glyph, bool get_var_extents_and_phantoms (hb_font_t *font, hb_codepoint_t glyph,
const int *coords, unsigned int coord_count, hb_glyph_extents_t *extents=nullptr /* OUT */,
hb_glyph_extents_t *extents=nullptr /* OUt */,
contour_point_vector_t *phantoms=nullptr /* OUT */) const contour_point_vector_t *phantoms=nullptr /* OUT */) const
{ {
contour_point_vector_t all_points; contour_point_vector_t all_points;
if (unlikely (!get_points_var (glyph, coords, coord_count, all_points) || if (unlikely (!get_points_var (glyph, font->coords, font->num_coords, all_points) ||
all_points.length < PHANTOM_COUNT)) return false; all_points.length < PHANTOM_COUNT)) return false;
/* Undocumented rasterizer behavior: /* Undocumented rasterizer behavior:
@ -832,8 +831,8 @@ struct glyf
} }
else else
{ {
extents->x_bearing = (int) floor (bounds.min.x); extents->x_bearing = font->em_scalef_x (bounds.min.x);
extents->width = (int) ceil (bounds.max.x) - extents->x_bearing; extents->width = font->em_scalef_x (bounds.max.x) - extents->x_bearing;
} }
if (bounds.min.y > bounds.max.y) if (bounds.min.y > bounds.max.y)
{ {
@ -842,8 +841,8 @@ struct glyf
} }
else else
{ {
extents->y_bearing = (int) ceil (bounds.max.y); extents->y_bearing = font->em_scalef_y (bounds.max.y);
extents->height = (int) floor (bounds.min.y) - extents->y_bearing; extents->height = font->em_scalef_y (bounds.min.y) - extents->y_bearing;
} }
} }
if (phantoms != nullptr) if (phantoms != nullptr)
@ -854,15 +853,13 @@ struct glyf
return true; return true;
} }
bool get_var_metrics (hb_codepoint_t glyph, bool get_var_metrics (hb_font_t *font, hb_codepoint_t glyph,
const int *coords, unsigned int coord_count,
contour_point_vector_t &phantoms) const contour_point_vector_t &phantoms) const
{ return get_var_extents_and_phantoms (glyph, coords, coord_count, nullptr, &phantoms); } { return get_var_extents_and_phantoms (font, glyph, nullptr, &phantoms); }
bool get_extents_var (hb_codepoint_t glyph, bool get_extents_var (hb_font_t *font, hb_codepoint_t glyph,
const int *coords, unsigned int coord_count,
hb_glyph_extents_t *extents) const hb_glyph_extents_t *extents) const
{ return get_var_extents_and_phantoms (glyph, coords, coord_count, extents); } { return get_var_extents_and_phantoms (font, glyph, extents); }
#endif #endif
public: public:
@ -966,36 +963,35 @@ struct glyf
} }
#ifndef HB_NO_VAR #ifndef HB_NO_VAR
unsigned int get_advance_var (hb_codepoint_t glyph, unsigned int get_advance_var (hb_font_t *font, hb_codepoint_t glyph,
const int *coords, unsigned int coord_count, bool is_vertical) const
bool vertical) const
{ {
bool success = false; bool success = false;
contour_point_vector_t phantoms; contour_point_vector_t phantoms;
phantoms.resize (PHANTOM_COUNT); phantoms.resize (PHANTOM_COUNT);
if (likely (coord_count == face->table.gvar->get_axis_count ())) if (likely (font->num_coords == face->table.gvar->get_axis_count ()))
success = get_var_metrics (glyph, coords, coord_count, phantoms); success = get_var_metrics (font, glyph, phantoms);
if (unlikely (!success)) if (unlikely (!success))
return vertical ? face->table.vmtx->get_advance (glyph) : face->table.hmtx->get_advance (glyph); return is_vertical ? face->table.vmtx->get_advance (glyph) : face->table.hmtx->get_advance (glyph);
if (vertical) if (is_vertical)
return roundf (phantoms[PHANTOM_TOP].y - phantoms[PHANTOM_BOTTOM].y); return roundf (phantoms[PHANTOM_TOP].y - phantoms[PHANTOM_BOTTOM].y);
else else
return roundf (phantoms[PHANTOM_RIGHT].x - phantoms[PHANTOM_LEFT].x); return roundf (phantoms[PHANTOM_RIGHT].x - phantoms[PHANTOM_LEFT].x);
} }
int get_side_bearing_var (hb_codepoint_t glyph, const int *coords, unsigned int coord_count, bool vertical) const int get_side_bearing_var (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical) const
{ {
hb_glyph_extents_t extents; hb_glyph_extents_t extents;
contour_point_vector_t phantoms; contour_point_vector_t phantoms;
phantoms.resize (PHANTOM_COUNT); phantoms.resize (PHANTOM_COUNT);
if (unlikely (!get_var_extents_and_phantoms (glyph, coords, coord_count, &extents, &phantoms))) if (unlikely (!get_var_extents_and_phantoms (font, glyph, &extents, &phantoms)))
return vertical ? face->table.vmtx->get_side_bearing (glyph) : face->table.hmtx->get_side_bearing (glyph); return is_vertical ? face->table.vmtx->get_side_bearing (glyph) : face->table.hmtx->get_side_bearing (glyph);
return vertical ? ceil (phantoms[PHANTOM_TOP].y) - extents.y_bearing : floor (phantoms[PHANTOM_LEFT].x); return is_vertical ? ceil (phantoms[PHANTOM_TOP].y) - extents.y_bearing : floor (phantoms[PHANTOM_LEFT].x);
} }
#endif #endif
@ -1005,7 +1001,7 @@ struct glyf
unsigned int coord_count; unsigned int coord_count;
const int *coords = hb_font_get_var_coords_normalized (font, &coord_count); const int *coords = hb_font_get_var_coords_normalized (font, &coord_count);
if (coords && coord_count > 0 && coord_count == face->table.gvar->get_axis_count ()) if (coords && coord_count > 0 && coord_count == face->table.gvar->get_axis_count ())
return get_extents_var (glyph, coords, coord_count, extents); return get_extents_var (font, glyph, extents);
#endif #endif
unsigned int start_offset, end_offset; unsigned int start_offset, end_offset;
@ -1019,10 +1015,10 @@ struct glyf
/* Undocumented rasterizer behavior: shift glyph to the left by (lsb - xMin), i.e., xMin = lsb */ /* Undocumented rasterizer behavior: shift glyph to the left by (lsb - xMin), i.e., xMin = lsb */
/* extents->x_bearing = hb_min (glyph_header.xMin, glyph_header.xMax); */ /* extents->x_bearing = hb_min (glyph_header.xMin, glyph_header.xMax); */
extents->x_bearing = face->table.hmtx->get_side_bearing (glyph); extents->x_bearing = font->em_scale_x (face->table.hmtx->get_side_bearing (glyph));
extents->y_bearing = hb_max (glyph_header.yMin, glyph_header.yMax); extents->y_bearing = font->em_scale_x (hb_max (glyph_header.yMin, glyph_header.yMax));
extents->width = hb_max (glyph_header.xMin, glyph_header.xMax) - hb_min (glyph_header.xMin, glyph_header.xMax); extents->width = font->em_scale_x (hb_max (glyph_header.xMin, glyph_header.xMax) - hb_min (glyph_header.xMin, glyph_header.xMax));
extents->height = hb_min (glyph_header.yMin, glyph_header.yMax) - extents->y_bearing; extents->height = font->em_scale_x (hb_min (glyph_header.yMin, glyph_header.yMax) - extents->y_bearing);
return true; return true;
} }

View File

@ -252,7 +252,7 @@ struct hmtxvmtx
if (var_table.get_blob () == &Null (hb_blob_t)) if (var_table.get_blob () == &Null (hb_blob_t))
return hb_ot_get_advance_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx); return hb_ot_get_advance_var_tt (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
return advance + roundf (var_table->get_advance_var (glyph, font->coords, font->num_coords)); // TODO Optimize?! return advance + roundf (var_table->get_advance_var (font, glyph)); // TODO Optimize?!
#else #else
return advance; return advance;
#endif #endif

View File

@ -114,11 +114,10 @@ struct HVARVVAR
rsbMap.sanitize (c, this)); rsbMap.sanitize (c, this));
} }
float get_advance_var (hb_codepoint_t glyph, float get_advance_var (hb_font_t *font, hb_codepoint_t glyph) const
const int *coords, unsigned int coord_count) const
{ {
unsigned int varidx = (this+advMap).map (glyph); unsigned int varidx = (this+advMap).map (glyph);
return (this+varStore).get_delta (varidx, coords, coord_count); return (this+varStore).get_delta (varidx, font->coords, font->num_coords);
} }
float get_side_bearing_var (hb_codepoint_t glyph, float get_side_bearing_var (hb_codepoint_t glyph,

View File

@ -146,8 +146,8 @@ test_extents_cff2 (void)
g_assert_cmpint (extents.x_bearing, ==, 38); g_assert_cmpint (extents.x_bearing, ==, 38);
g_assert_cmpint (extents.y_bearing, ==, 493); g_assert_cmpint (extents.y_bearing, ==, 493);
g_assert_cmpint (extents.width, ==, 481); g_assert_cmpint (extents.width, ==, 480);
g_assert_cmpint (extents.height, ==, -508); g_assert_cmpint (extents.height, ==, -507);
hb_font_destroy (font); hb_font_destroy (font);
} }
@ -168,17 +168,17 @@ test_extents_cff2_vsindex (void)
hb_bool_t result = hb_font_get_glyph_extents (font, 1, &extents); hb_bool_t result = hb_font_get_glyph_extents (font, 1, &extents);
g_assert (result); g_assert (result);
g_assert_cmpint (extents.x_bearing, ==, 11); g_assert_cmpint (extents.x_bearing, ==, 12);
g_assert_cmpint (extents.y_bearing, ==, 656); g_assert_cmpint (extents.y_bearing, ==, 655);
g_assert_cmpint (extents.width, ==, 653); g_assert_cmpint (extents.width, ==, 651);
g_assert_cmpint (extents.height, ==, -656); g_assert_cmpint (extents.height, ==, -655);
result = hb_font_get_glyph_extents (font, 2, &extents); result = hb_font_get_glyph_extents (font, 2, &extents);
g_assert (result); g_assert (result);
g_assert_cmpint (extents.x_bearing, ==, 7); g_assert_cmpint (extents.x_bearing, ==, 8);
g_assert_cmpint (extents.y_bearing, ==, 669); g_assert_cmpint (extents.y_bearing, ==, 669);
g_assert_cmpint (extents.width, ==, 650); g_assert_cmpint (extents.width, ==, 648);
g_assert_cmpint (extents.height, ==, -669); g_assert_cmpint (extents.height, ==, -669);
hb_font_destroy (font); hb_font_destroy (font);
@ -199,9 +199,9 @@ test_extents_cff2_vsindex_named_instance (void)
hb_bool_t result = hb_font_get_glyph_extents (font, 1, &extents); hb_bool_t result = hb_font_get_glyph_extents (font, 1, &extents);
g_assert (result); g_assert (result);
g_assert_cmpint (extents.x_bearing, ==, 12); g_assert_cmpint (extents.x_bearing, ==, 13);
g_assert_cmpint (extents.y_bearing, ==, 652); g_assert_cmpint (extents.y_bearing, ==, 652);
g_assert_cmpint (extents.width, ==, 653); g_assert_cmpint (extents.width, ==, 652);
g_assert_cmpint (extents.height, ==, -652); g_assert_cmpint (extents.height, ==, -652);
result = hb_font_get_glyph_extents (font, 2, &extents); result = hb_font_get_glyph_extents (font, 2, &extents);