From d73cdcf3612ae6114a0f828e0f667d447ed1a964 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Sun, 6 Oct 2019 18:09:14 +0330 Subject: [PATCH] [ot-font] Apply font scaling before turning to int --- src/hb-ot-cff1-table.cc | 12 +++---- src/hb-ot-cff1-table.hh | 2 +- src/hb-ot-cff2-table.cc | 8 ++--- src/hb-ot-color-cbdt-table.hh | 14 ++++---- src/hb-ot-color-sbix-table.hh | 8 ++--- src/hb-ot-font.cc | 12 +++---- src/hb-ot-glyf-table.hh | 58 ++++++++++++++++------------------ src/hb-ot-hmtx-table.hh | 2 +- src/hb-ot-var-hvar-table.hh | 5 ++- test/api/test-ot-extents-cff.c | 20 ++++++------ 10 files changed, 66 insertions(+), 75 deletions(-) diff --git a/src/hb-ot-cff1-table.cc b/src/hb-ot-cff1-table.cc index 5e09af4e5..54bfe5dff 100644 --- a/src/hb-ot-cff1-table.cc +++ b/src/hb-ot-cff1-table.cc @@ -306,14 +306,14 @@ bool _get_bounds (const OT::cff1::accelerator_t *cff, hb_codepoint_t glyph, boun 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 /* XXX Remove check when this code moves to .hh file. */ return true; #endif - bounds_t bounds; + bounds_t bounds; if (!_get_bounds (this, glyph, bounds)) return false; @@ -325,8 +325,8 @@ bool OT::cff1::accelerator_t::get_extents (hb_codepoint_t glyph, hb_glyph_extent } else { - extents->x_bearing = (int32_t)bounds.min.x.floor (); - extents->width = (int32_t)bounds.max.x.ceil () - extents->x_bearing; + extents->x_bearing = font->em_scalef_x (bounds.min.x.to_real ()); + extents->width = font->em_scalef_x (bounds.max.x.to_real () - extents->x_bearing); } 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 { - extents->y_bearing = (int32_t)bounds.max.y.ceil (); - extents->height = (int32_t)bounds.min.y.floor () - extents->y_bearing; + extents->y_bearing = font->em_scalef_y (bounds.max.y.to_real ()); + extents->height = font->em_scalef_x (bounds.min.y.to_real () - extents->y_bearing); } return true; diff --git a/src/hb-ot-cff1-table.hh b/src/hb-ot-cff1-table.hh index cd98c4477..ad206511c 100644 --- a/src/hb-ot-cff1-table.hh +++ b/src/hb-ot-cff1-table.hh @@ -1196,7 +1196,7 @@ struct cff1 struct accelerator_t : accelerator_templ_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; }; diff --git a/src/hb-ot-cff2-table.cc b/src/hb-ot-cff2-table.cc index b28055036..e69070e18 100644 --- a/src/hb-ot-cff2-table.cc +++ b/src/hb-ot-cff2-table.cc @@ -125,8 +125,8 @@ bool OT::cff2::accelerator_t::get_extents (hb_font_t *font, } else { - extents->x_bearing = (int32_t)param.min_x.floor (); - extents->width = (int32_t)param.max_x.ceil () - extents->x_bearing; + extents->x_bearing = font->em_scalef_x (param.min_x.to_real ()); + extents->width = font->em_scalef_x (param.max_x.to_real () - extents->x_bearing); } if (param.min_y >= param.max_y) { @@ -135,8 +135,8 @@ bool OT::cff2::accelerator_t::get_extents (hb_font_t *font, } else { - extents->y_bearing = (int32_t)param.max_y.ceil (); - extents->height = (int32_t)param.min_y.floor () - extents->y_bearing; + extents->y_bearing = font->em_scalef_y (param.max_y.to_real ()); + extents->height = font->em_scalef_y (param.min_y.to_real () - extents->y_bearing); } return true; diff --git a/src/hb-ot-color-cbdt-table.hh b/src/hb-ot-color-cbdt-table.hh index 06da55600..3498d3b36 100644 --- a/src/hb-ot-color-cbdt-table.hh +++ b/src/hb-ot-color-cbdt-table.hh @@ -51,12 +51,12 @@ struct SmallGlyphMetrics 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->y_bearing = bearingY; - extents->width = width; - extents->height = - (hb_position_t) height; + extents->x_bearing = font->em_scale_x (bearingX); + extents->y_bearing = font->em_scale_y (bearingY); + extents->width = font->em_scale_x (width); + extents->height = font->em_scale_y (-height); } HBUINT8 height; @@ -424,7 +424,7 @@ struct CBDT return false; const GlyphBitmapDataFormat17& glyphFormat17 = StructAtOffset (this->cbdt, image_offset); - glyphFormat17.glyphMetrics.get_extents (extents); + glyphFormat17.glyphMetrics.get_extents (font, extents); break; } case 18: { @@ -432,7 +432,7 @@ struct CBDT return false; const GlyphBitmapDataFormat18& glyphFormat18 = StructAtOffset (this->cbdt, image_offset); - glyphFormat18.glyphMetrics.get_extents (extents); + glyphFormat18.glyphMetrics.get_extents (font, extents); break; } default: diff --git a/src/hb-ot-color-sbix-table.hh b/src/hb-ot-color-sbix-table.hh index 6089027f8..6008e7e3d 100644 --- a/src/hb-ot-color-sbix-table.hh +++ b/src/hb-ot-color-sbix-table.hh @@ -243,10 +243,10 @@ struct sbix if (strike_ppem) { float scale = font->face->get_upem () / (float) strike_ppem; - extents->x_bearing = roundf (extents->x_bearing * scale); - extents->y_bearing = roundf (extents->y_bearing * scale); - extents->width = roundf (extents->width * scale); - extents->height = roundf (extents->height * scale); + extents->x_bearing = font->em_scalef_x (extents->x_bearing * scale); + extents->y_bearing = font->em_scalef_y (extents->y_bearing * scale); + extents->width = font->em_scalef_x (extents->width * scale); + extents->height = font->em_scalef_y (extents->height * scale); } hb_blob_destroy (blob); diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc index 303cd4c68..6924955ba 100644 --- a/src/hb-ot-font.cc +++ b/src/hb-ot-font.cc @@ -164,7 +164,7 @@ hb_ot_get_glyph_v_origin (hb_font_t *font, { const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx; 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; } @@ -190,7 +190,7 @@ hb_ot_get_glyph_extents (hb_font_t *font, #endif if (!ret) ret = ot_face->glyf->get_extents (font, glyph, extents); #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); #endif #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 // 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; } @@ -319,13 +315,13 @@ hb_ot_font_set_funcs (hb_font_t *font) int 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 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 diff --git a/src/hb-ot-glyf-table.hh b/src/hb-ot-glyf-table.hh index 48ee387c6..0443ea01f 100644 --- a/src/hb-ot-glyf-table.hh +++ b/src/hb-ot-glyf-table.hh @@ -803,13 +803,12 @@ struct glyf return true; } - bool get_var_extents_and_phantoms (hb_codepoint_t glyph, - const int *coords, unsigned int coord_count, - hb_glyph_extents_t *extents=nullptr /* OUt */, + bool get_var_extents_and_phantoms (hb_font_t *font, hb_codepoint_t glyph, + hb_glyph_extents_t *extents=nullptr /* OUT */, contour_point_vector_t *phantoms=nullptr /* OUT */) const { 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; /* Undocumented rasterizer behavior: @@ -832,8 +831,8 @@ struct glyf } else { - extents->x_bearing = (int) floor (bounds.min.x); - extents->width = (int) ceil (bounds.max.x) - extents->x_bearing; + extents->x_bearing = font->em_scalef_x (bounds.min.x); + extents->width = font->em_scalef_x (bounds.max.x) - extents->x_bearing; } if (bounds.min.y > bounds.max.y) { @@ -842,8 +841,8 @@ struct glyf } else { - extents->y_bearing = (int) ceil (bounds.max.y); - extents->height = (int) floor (bounds.min.y) - extents->y_bearing; + extents->y_bearing = font->em_scalef_y (bounds.max.y); + extents->height = font->em_scalef_y (bounds.min.y) - extents->y_bearing; } } if (phantoms != nullptr) @@ -854,15 +853,13 @@ struct glyf return true; } - bool get_var_metrics (hb_codepoint_t glyph, - const int *coords, unsigned int coord_count, + bool get_var_metrics (hb_font_t *font, hb_codepoint_t glyph, 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, - const int *coords, unsigned int coord_count, + bool get_extents_var (hb_font_t *font, hb_codepoint_t glyph, 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 public: @@ -966,36 +963,35 @@ struct glyf } #ifndef HB_NO_VAR - unsigned int get_advance_var (hb_codepoint_t glyph, - const int *coords, unsigned int coord_count, - bool vertical) const + unsigned int get_advance_var (hb_font_t *font, hb_codepoint_t glyph, + bool is_vertical) const { bool success = false; contour_point_vector_t phantoms; phantoms.resize (PHANTOM_COUNT); - if (likely (coord_count == face->table.gvar->get_axis_count ())) - success = get_var_metrics (glyph, coords, coord_count, phantoms); + if (likely (font->num_coords == face->table.gvar->get_axis_count ())) + success = get_var_metrics (font, glyph, phantoms); 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); else 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; - contour_point_vector_t phantoms; + contour_point_vector_t phantoms; phantoms.resize (PHANTOM_COUNT); - if (unlikely (!get_var_extents_and_phantoms (glyph, coords, coord_count, &extents, &phantoms))) - return vertical ? face->table.vmtx->get_side_bearing (glyph) : face->table.hmtx->get_side_bearing (glyph); + if (unlikely (!get_var_extents_and_phantoms (font, glyph, &extents, &phantoms))) + 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 @@ -1005,7 +1001,7 @@ struct glyf unsigned int 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 ()) - return get_extents_var (glyph, coords, coord_count, extents); + return get_extents_var (font, glyph, extents); #endif 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 */ /* extents->x_bearing = hb_min (glyph_header.xMin, glyph_header.xMax); */ - extents->x_bearing = face->table.hmtx->get_side_bearing (glyph); - extents->y_bearing = 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->height = hb_min (glyph_header.yMin, glyph_header.yMax) - extents->y_bearing; + extents->x_bearing = font->em_scale_x (face->table.hmtx->get_side_bearing (glyph)); + extents->y_bearing = font->em_scale_x (hb_max (glyph_header.yMin, glyph_header.yMax)); + extents->width = font->em_scale_x (hb_max (glyph_header.xMin, glyph_header.xMax) - hb_min (glyph_header.xMin, glyph_header.xMax)); + extents->height = font->em_scale_x (hb_min (glyph_header.yMin, glyph_header.yMax) - extents->y_bearing); return true; } diff --git a/src/hb-ot-hmtx-table.hh b/src/hb-ot-hmtx-table.hh index 9d179330c..ce8f261bc 100644 --- a/src/hb-ot-hmtx-table.hh +++ b/src/hb-ot-hmtx-table.hh @@ -252,7 +252,7 @@ struct hmtxvmtx 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 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 return advance; #endif diff --git a/src/hb-ot-var-hvar-table.hh b/src/hb-ot-var-hvar-table.hh index ba1f2dac2..223430fb8 100644 --- a/src/hb-ot-var-hvar-table.hh +++ b/src/hb-ot-var-hvar-table.hh @@ -114,11 +114,10 @@ struct HVARVVAR rsbMap.sanitize (c, this)); } - float get_advance_var (hb_codepoint_t glyph, - const int *coords, unsigned int coord_count) const + float get_advance_var (hb_font_t *font, hb_codepoint_t glyph) const { 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, diff --git a/test/api/test-ot-extents-cff.c b/test/api/test-ot-extents-cff.c index 6810ea457..e6aeb0d6e 100644 --- a/test/api/test-ot-extents-cff.c +++ b/test/api/test-ot-extents-cff.c @@ -146,8 +146,8 @@ test_extents_cff2 (void) g_assert_cmpint (extents.x_bearing, ==, 38); g_assert_cmpint (extents.y_bearing, ==, 493); - g_assert_cmpint (extents.width, ==, 481); - g_assert_cmpint (extents.height, ==, -508); + g_assert_cmpint (extents.width, ==, 480); + g_assert_cmpint (extents.height, ==, -507); 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); g_assert (result); - g_assert_cmpint (extents.x_bearing, ==, 11); - g_assert_cmpint (extents.y_bearing, ==, 656); - g_assert_cmpint (extents.width, ==, 653); - g_assert_cmpint (extents.height, ==, -656); + g_assert_cmpint (extents.x_bearing, ==, 12); + g_assert_cmpint (extents.y_bearing, ==, 655); + g_assert_cmpint (extents.width, ==, 651); + g_assert_cmpint (extents.height, ==, -655); result = hb_font_get_glyph_extents (font, 2, &extents); 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.width, ==, 650); + g_assert_cmpint (extents.width, ==, 648); g_assert_cmpint (extents.height, ==, -669); 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); 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.width, ==, 653); + g_assert_cmpint (extents.width, ==, 652); g_assert_cmpint (extents.height, ==, -652); result = hb_font_get_glyph_extents (font, 2, &extents);