Commit 459ae251 authored by Mike Reed's avatar Mike Reed Committed by Commit Bot

declassify SkiaTextMetrics -- they are all stateless

Bug: skia:2664
Change-Id: I127f51058a61b1113bcac399b4fb0324f2bdd19f
Reviewed-on: https://chromium-review.googlesource.com/c/1343038
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: default avatarFlorin Malita <fmalita@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609449}
parent 29aedda1
...@@ -304,7 +304,7 @@ void OpenTypeVerticalData::GetVerticalTranslationsForGlyphs( ...@@ -304,7 +304,7 @@ void OpenTypeVerticalData::GetVerticalTranslationsForGlyphs(
float top_side_bearing = top_side_bearing_f_unit * size_per_unit_; float top_side_bearing = top_side_bearing_f_unit * size_per_unit_;
SkRect skiaBounds; SkRect skiaBounds;
SkiaTextMetrics(font).GetSkiaBoundsForGlyph(glyph, &skiaBounds); GetBoundsForGlyph(font, glyph, &skiaBounds);
FloatRect bounds(skiaBounds); FloatRect bounds(skiaBounds);
out_xy_array[1] = bounds.Y() - top_side_bearing; out_xy_array[1] = bounds.Y() - top_side_bearing;
continue; continue;
......
...@@ -131,8 +131,7 @@ static hb_position_t HarfBuzzGetGlyphHorizontalAdvance(hb_font_t* hb_font, ...@@ -131,8 +131,7 @@ static hb_position_t HarfBuzzGetGlyphHorizontalAdvance(hb_font_t* hb_font,
reinterpret_cast<HarfBuzzFontData*>(font_data); reinterpret_cast<HarfBuzzFontData*>(font_data);
hb_position_t advance = 0; hb_position_t advance = 0;
SkiaTextMetrics(hb_font_data->font_) GetGlyphWidthForHarfBuzz(hb_font_data->font_, glyph, &advance);
.GetGlyphWidthForHarfBuzz(glyph, &advance);
return advance; return advance;
} }
...@@ -146,9 +145,8 @@ static void HarfBuzzGetGlyphHorizontalAdvances(hb_font_t* font, ...@@ -146,9 +145,8 @@ static void HarfBuzzGetGlyphHorizontalAdvances(hb_font_t* font,
void* user_data) { void* user_data) {
HarfBuzzFontData* hb_font_data = HarfBuzzFontData* hb_font_data =
reinterpret_cast<HarfBuzzFontData*>(font_data); reinterpret_cast<HarfBuzzFontData*>(font_data);
SkiaTextMetrics(hb_font_data->font_) GetGlyphWidthForHarfBuzz(hb_font_data->font_, count, first_glyph,
.GetGlyphWidthForHarfBuzz(count, first_glyph, glyph_stride, first_advance, glyph_stride, first_advance, advance_stride);
advance_stride);
} }
static hb_bool_t HarfBuzzGetGlyphVerticalOrigin(hb_font_t* hb_font, static hb_bool_t HarfBuzzGetGlyphVerticalOrigin(hb_font_t* hb_font,
...@@ -168,8 +166,8 @@ static hb_bool_t HarfBuzzGetGlyphVerticalOrigin(hb_font_t* hb_font, ...@@ -168,8 +166,8 @@ static hb_bool_t HarfBuzzGetGlyphVerticalOrigin(hb_font_t* hb_font,
Glyph the_glyph = glyph; Glyph the_glyph = glyph;
vertical_data->GetVerticalTranslationsForGlyphs(hb_font_data->font_, vertical_data->GetVerticalTranslationsForGlyphs(hb_font_data->font_,
&the_glyph, 1, result); &the_glyph, 1, result);
*x = SkiaTextMetrics::SkiaScalarToHarfBuzzPosition(-result[0]); *x = SkiaScalarToHarfBuzzPosition(-result[0]);
*y = SkiaTextMetrics::SkiaScalarToHarfBuzzPosition(-result[1]); *y = SkiaScalarToHarfBuzzPosition(-result[1]);
return true; return true;
} }
...@@ -182,14 +180,12 @@ static hb_position_t HarfBuzzGetGlyphVerticalAdvance(hb_font_t* hb_font, ...@@ -182,14 +180,12 @@ static hb_position_t HarfBuzzGetGlyphVerticalAdvance(hb_font_t* hb_font,
scoped_refptr<OpenTypeVerticalData> vertical_data = scoped_refptr<OpenTypeVerticalData> vertical_data =
hb_font_data->VerticalData(); hb_font_data->VerticalData();
if (!vertical_data) { if (!vertical_data) {
return SkiaTextMetrics::SkiaScalarToHarfBuzzPosition( return SkiaScalarToHarfBuzzPosition(hb_font_data->height_fallback_);
hb_font_data->height_fallback_);
} }
Glyph the_glyph = glyph; Glyph the_glyph = glyph;
float advance_height = -vertical_data->AdvanceHeight(the_glyph); float advance_height = -vertical_data->AdvanceHeight(the_glyph);
return SkiaTextMetrics::SkiaScalarToHarfBuzzPosition( return SkiaScalarToHarfBuzzPosition(SkFloatToScalar(advance_height));
SkFloatToScalar(advance_height));
} }
static hb_position_t HarfBuzzGetGlyphHorizontalKerning( static hb_position_t HarfBuzzGetGlyphHorizontalKerning(
...@@ -208,8 +204,7 @@ static hb_position_t HarfBuzzGetGlyphHorizontalKerning( ...@@ -208,8 +204,7 @@ static hb_position_t HarfBuzzGetGlyphHorizontalKerning(
int32_t kerning_adjustments[1] = {0}; int32_t kerning_adjustments[1] = {0};
if (typeface->getKerningPairAdjustments(glyphs, 2, kerning_adjustments)) { if (typeface->getKerningPairAdjustments(glyphs, 2, kerning_adjustments)) {
return SkiaTextMetrics::SkiaScalarToHarfBuzzPosition( return SkiaScalarToHarfBuzzPosition(SkIntToScalar(kerning_adjustments[0]) *
SkIntToScalar(kerning_adjustments[0]) *
hb_font_data->SizePerUnit(*typeface)); hb_font_data->SizePerUnit(*typeface));
} }
...@@ -224,8 +219,7 @@ static hb_bool_t HarfBuzzGetGlyphExtents(hb_font_t* hb_font, ...@@ -224,8 +219,7 @@ static hb_bool_t HarfBuzzGetGlyphExtents(hb_font_t* hb_font,
HarfBuzzFontData* hb_font_data = HarfBuzzFontData* hb_font_data =
reinterpret_cast<HarfBuzzFontData*>(font_data); reinterpret_cast<HarfBuzzFontData*>(font_data);
SkiaTextMetrics(hb_font_data->font_) GetGlyphExtentsForHarfBuzz(hb_font_data->font_, glyph, extents);
.GetGlyphExtentsForHarfBuzz(glyph, extents);
return true; return true;
} }
...@@ -435,8 +429,7 @@ hb_font_t* HarfBuzzFace::GetScaledFont( ...@@ -435,8 +429,7 @@ hb_font_t* HarfBuzzFace::GetScaledFont(
harfbuzz_font_data_->UpdateFallbackMetricsAndScale(*platform_data_, harfbuzz_font_data_->UpdateFallbackMetricsAndScale(*platform_data_,
vertical_layout); vertical_layout);
int scale = int scale = SkiaScalarToHarfBuzzPosition(platform_data_->size());
SkiaTextMetrics::SkiaScalarToHarfBuzzPosition(platform_data_->size());
hb_font_set_scale(unscaled_font_, scale, scale); hb_font_set_scale(unscaled_font_, scale, scale);
hb_font_set_ptem(unscaled_font_, platform_data_->size() / kCssPixelsPerPoint); hb_font_set_ptem(unscaled_font_, platform_data_->size() / kCssPixelsPerPoint);
......
...@@ -348,7 +348,7 @@ FloatRect SimpleFontData::PlatformBoundsForGlyph(Glyph glyph) const { ...@@ -348,7 +348,7 @@ FloatRect SimpleFontData::PlatformBoundsForGlyph(Glyph glyph) const {
static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated."); static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated.");
SkRect bounds; SkRect bounds;
SkiaTextMetrics(font_).GetSkiaBoundsForGlyph(glyph, &bounds); GetBoundsForGlyph(font_, glyph, &bounds);
return FloatRect(bounds); return FloatRect(bounds);
} }
...@@ -360,7 +360,7 @@ void SimpleFontData::BoundsForGlyphs(const Vector<Glyph, 256>& glyphs, ...@@ -360,7 +360,7 @@ void SimpleFontData::BoundsForGlyphs(const Vector<Glyph, 256>& glyphs,
return; return;
DCHECK_EQ(bounds->size(), glyphs.size()); DCHECK_EQ(bounds->size(), glyphs.size());
SkiaTextMetrics(font_).GetSkiaBoundsForGlyphs(glyphs, bounds->data()); GetBoundsForGlyphs(font_, glyphs, bounds->data());
} }
float SimpleFontData::PlatformWidthForGlyph(Glyph glyph) const { float SimpleFontData::PlatformWidthForGlyph(Glyph glyph) const {
...@@ -369,7 +369,7 @@ float SimpleFontData::PlatformWidthForGlyph(Glyph glyph) const { ...@@ -369,7 +369,7 @@ float SimpleFontData::PlatformWidthForGlyph(Glyph glyph) const {
static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated."); static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated.");
return SkiaTextMetrics(font_).GetSkiaWidthForGlyph(glyph); return GetWidthForGlyph(font_, glyph);
} }
} // namespace blink } // namespace blink
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "third_party/blink/renderer/platform/wtf/math_extras.h" #include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include <SkFont.h> #include <SkFont.h>
#include <SkPaint.h>
#include <SkPath.h> #include <SkPath.h>
namespace blink { namespace blink {
...@@ -22,14 +21,8 @@ T* advance_by_byte_size(T* p, unsigned byte_size) { ...@@ -22,14 +21,8 @@ T* advance_by_byte_size(T* p, unsigned byte_size) {
} // namespace } // namespace
SkiaTextMetrics::SkiaTextMetrics(const SkPaint* paint) void GetGlyphWidthForHarfBuzz(const SkFont& font,
: font_(SkFont::LEGACY_ExtractFromPaint(*paint)) { hb_codepoint_t codepoint,
CHECK(paint->getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
}
SkiaTextMetrics::SkiaTextMetrics(const SkFont& font) : font_(font) {}
void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(hb_codepoint_t codepoint,
hb_position_t* width) { hb_position_t* width) {
DCHECK_LE(codepoint, 0xFFFFu); DCHECK_LE(codepoint, 0xFFFFu);
CHECK(width); CHECK(width);
...@@ -37,13 +30,14 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(hb_codepoint_t codepoint, ...@@ -37,13 +30,14 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(hb_codepoint_t codepoint,
SkScalar sk_width; SkScalar sk_width;
uint16_t glyph = codepoint; uint16_t glyph = codepoint;
font_.getWidths(&glyph, 1, &sk_width, nullptr); font.getWidths(&glyph, 1, &sk_width, nullptr);
if (!font_.isSubpixel()) if (!font.isSubpixel())
sk_width = SkScalarRoundToInt(sk_width); sk_width = SkScalarRoundToInt(sk_width);
*width = SkiaScalarToHarfBuzzPosition(sk_width); *width = SkiaScalarToHarfBuzzPosition(sk_width);
} }
void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count, void GetGlyphWidthForHarfBuzz(const SkFont& font,
unsigned count,
hb_codepoint_t* glyphs, hb_codepoint_t* glyphs,
unsigned glyph_stride, unsigned glyph_stride,
hb_position_t* advances, hb_position_t* advances,
...@@ -57,9 +51,9 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count, ...@@ -57,9 +51,9 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count,
glyph_array[i] = *glyphs; glyph_array[i] = *glyphs;
} }
Vector<SkScalar, 256> sk_width_array(count); Vector<SkScalar, 256> sk_width_array(count);
font_.getWidths(glyph_array.data(), count, sk_width_array.data(), nullptr); font.getWidths(glyph_array.data(), count, sk_width_array.data(), nullptr);
if (!font_.isSubpixel()) { if (!font.isSubpixel()) {
for (unsigned i = 0; i < count; i++) for (unsigned i = 0; i < count; i++)
sk_width_array[i] = SkScalarRoundToInt(sk_width_array[i]); sk_width_array[i] = SkScalarRoundToInt(sk_width_array[i]);
} }
...@@ -71,7 +65,8 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count, ...@@ -71,7 +65,8 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count,
} }
} }
void SkiaTextMetrics::GetGlyphExtentsForHarfBuzz(hb_codepoint_t codepoint, void GetGlyphExtentsForHarfBuzz(const SkFont& font,
hb_codepoint_t codepoint,
hb_glyph_extents_t* extents) { hb_glyph_extents_t* extents) {
DCHECK_LE(codepoint, 0xFFFFu); DCHECK_LE(codepoint, 0xFFFFu);
CHECK(extents); CHECK(extents);
...@@ -79,8 +74,8 @@ void SkiaTextMetrics::GetGlyphExtentsForHarfBuzz(hb_codepoint_t codepoint, ...@@ -79,8 +74,8 @@ void SkiaTextMetrics::GetGlyphExtentsForHarfBuzz(hb_codepoint_t codepoint,
SkRect sk_bounds; SkRect sk_bounds;
uint16_t glyph = codepoint; uint16_t glyph = codepoint;
font_.getWidths(&glyph, 1, nullptr, &sk_bounds); font.getWidths(&glyph, 1, nullptr, &sk_bounds);
if (!font_.isSubpixel()) { if (!font.isSubpixel()) {
// Use roundOut() rather than round() to avoid rendering glyphs // Use roundOut() rather than round() to avoid rendering glyphs
// outside the visual overflow rect. crbug.com/452914. // outside the visual overflow rect. crbug.com/452914.
sk_bounds.set(sk_bounds.roundOut()); sk_bounds.set(sk_bounds.roundOut());
...@@ -94,35 +89,36 @@ void SkiaTextMetrics::GetGlyphExtentsForHarfBuzz(hb_codepoint_t codepoint, ...@@ -94,35 +89,36 @@ void SkiaTextMetrics::GetGlyphExtentsForHarfBuzz(hb_codepoint_t codepoint,
extents->height = SkiaScalarToHarfBuzzPosition(-sk_bounds.height()); extents->height = SkiaScalarToHarfBuzzPosition(-sk_bounds.height());
} }
void SkiaTextMetrics::GetSkiaBoundsForGlyph(Glyph glyph, SkRect* bounds) { void GetBoundsForGlyph(const SkFont& font, Glyph glyph, SkRect* bounds) {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// TODO(drott): Remove this once we have better metrics bounds // TODO(drott): Remove this once we have better metrics bounds
// on Mac, https://bugs.chromium.org/p/skia/issues/detail?id=5328 // on Mac, https://bugs.chromium.org/p/skia/issues/detail?id=5328
SkPath path; SkPath path;
font_.getPath(glyph, &path); font.getPath(glyph, &path);
*bounds = path.getBounds(); *bounds = path.getBounds();
#else #else
font_.getWidths(&glyph, 1, nullptr, bounds); font.getWidths(&glyph, 1, nullptr, bounds);
#endif #endif
if (!font_.isSubpixel()) { if (!font.isSubpixel()) {
SkIRect ir; SkIRect ir;
bounds->roundOut(&ir); bounds->roundOut(&ir);
bounds->set(ir); bounds->set(ir);
} }
} }
void SkiaTextMetrics::GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>& glyphs, void GetBoundsForGlyphs(const SkFont& font,
const Vector<Glyph, 256>& glyphs,
SkRect* bounds) { SkRect* bounds) {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
for (unsigned i = 0; i < glyphs.size(); i++) { for (unsigned i = 0; i < glyphs.size(); i++) {
GetSkiaBoundsForGlyph(glyphs[i], &bounds[i]); GetBoundsForGlyph(font, glyphs[i], &bounds[i]);
} }
#else #else
static_assert(sizeof(Glyph) == 2, "Skia expects 2 bytes glyph id."); static_assert(sizeof(Glyph) == 2, "Skia expects 2 bytes glyph id.");
font_.getWidths(glyphs.data(), glyphs.size(), nullptr, bounds); font.getWidths(glyphs.data(), glyphs.size(), nullptr, bounds);
if (!font_.isSubpixel()) { if (!font.isSubpixel()) {
for (unsigned i = 0; i < glyphs.size(); i++) { for (unsigned i = 0; i < glyphs.size(); i++) {
SkIRect ir; SkIRect ir;
bounds[i].roundOut(&ir); bounds[i].roundOut(&ir);
...@@ -132,17 +128,17 @@ void SkiaTextMetrics::GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>& glyphs, ...@@ -132,17 +128,17 @@ void SkiaTextMetrics::GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>& glyphs,
#endif #endif
} }
float SkiaTextMetrics::GetSkiaWidthForGlyph(Glyph glyph) { float GetWidthForGlyph(const SkFont& font, Glyph glyph) {
SkScalar sk_width; SkScalar sk_width;
font_.getWidths(&glyph, 1, &sk_width, nullptr); font.getWidths(&glyph, 1, &sk_width, nullptr);
if (!font_.isSubpixel()) if (!font.isSubpixel())
sk_width = SkScalarRoundToInt(sk_width); sk_width = SkScalarRoundToInt(sk_width);
return SkScalarToFloat(sk_width); return SkScalarToFloat(sk_width);
} }
hb_position_t SkiaTextMetrics::SkiaScalarToHarfBuzzPosition(SkScalar value) { hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value) {
// We treat HarfBuzz hb_position_t as 16.16 fixed-point. // We treat HarfBuzz hb_position_t as 16.16 fixed-point.
static const int kHbPosition1 = 1 << 16; static const int kHbPosition1 = 1 << 16;
return clampTo<int>(value * kHbPosition1); return clampTo<int>(value * kHbPosition1);
......
...@@ -7,36 +7,32 @@ ...@@ -7,36 +7,32 @@
#include "third_party/blink/renderer/platform/fonts/glyph.h" #include "third_party/blink/renderer/platform/fonts/glyph.h"
#include <SkFont.h>
#include <hb.h> #include <hb.h>
#include "third_party/blink/renderer/platform/wtf/vector.h" #include "third_party/blink/renderer/platform/wtf/vector.h"
#include "third_party/skia/include/core/SkRect.h"
class SkPaint; class SkFont;
namespace blink { namespace blink {
class SkiaTextMetrics final { void GetGlyphWidthForHarfBuzz(const SkFont&,
public: hb_codepoint_t,
SkiaTextMetrics(const SkPaint* legacy_paint); hb_position_t* width);
SkiaTextMetrics(const SkFont&); void GetGlyphWidthForHarfBuzz(const SkFont&,
unsigned count,
void GetGlyphWidthForHarfBuzz(hb_codepoint_t, hb_position_t* width);
void GetGlyphWidthForHarfBuzz(unsigned count,
hb_codepoint_t* first_glyph, hb_codepoint_t* first_glyph,
unsigned glyph_stride, unsigned glyph_stride,
hb_position_t* first_advance, hb_position_t* first_advance,
unsigned advance_stride); unsigned advance_stride);
void GetGlyphExtentsForHarfBuzz(hb_codepoint_t, hb_glyph_extents_t*); void GetGlyphExtentsForHarfBuzz(const SkFont&,
hb_codepoint_t,
void GetSkiaBoundsForGlyph(Glyph, SkRect* bounds); hb_glyph_extents_t*);
void GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>&, SkRect*);
float GetSkiaWidthForGlyph(Glyph);
static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value); void GetBoundsForGlyph(const SkFont&, Glyph, SkRect* bounds);
void GetBoundsForGlyphs(const SkFont&, const Vector<Glyph, 256>&, SkRect*);
float GetWidthForGlyph(const SkFont&, Glyph);
private: hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value);
const SkFont font_;
};
} // namespace blink } // namespace blink
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment