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(
float top_side_bearing = top_side_bearing_f_unit * size_per_unit_;
SkRect skiaBounds;
SkiaTextMetrics(font).GetSkiaBoundsForGlyph(glyph, &skiaBounds);
GetBoundsForGlyph(font, glyph, &skiaBounds);
FloatRect bounds(skiaBounds);
out_xy_array[1] = bounds.Y() - top_side_bearing;
continue;
......
......@@ -131,8 +131,7 @@ static hb_position_t HarfBuzzGetGlyphHorizontalAdvance(hb_font_t* hb_font,
reinterpret_cast<HarfBuzzFontData*>(font_data);
hb_position_t advance = 0;
SkiaTextMetrics(hb_font_data->font_)
.GetGlyphWidthForHarfBuzz(glyph, &advance);
GetGlyphWidthForHarfBuzz(hb_font_data->font_, glyph, &advance);
return advance;
}
......@@ -146,9 +145,8 @@ static void HarfBuzzGetGlyphHorizontalAdvances(hb_font_t* font,
void* user_data) {
HarfBuzzFontData* hb_font_data =
reinterpret_cast<HarfBuzzFontData*>(font_data);
SkiaTextMetrics(hb_font_data->font_)
.GetGlyphWidthForHarfBuzz(count, first_glyph, glyph_stride, first_advance,
advance_stride);
GetGlyphWidthForHarfBuzz(hb_font_data->font_, count, first_glyph,
glyph_stride, first_advance, advance_stride);
}
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;
vertical_data->GetVerticalTranslationsForGlyphs(hb_font_data->font_,
&the_glyph, 1, result);
*x = SkiaTextMetrics::SkiaScalarToHarfBuzzPosition(-result[0]);
*y = SkiaTextMetrics::SkiaScalarToHarfBuzzPosition(-result[1]);
*x = SkiaScalarToHarfBuzzPosition(-result[0]);
*y = SkiaScalarToHarfBuzzPosition(-result[1]);
return true;
}
......@@ -182,14 +180,12 @@ static hb_position_t HarfBuzzGetGlyphVerticalAdvance(hb_font_t* hb_font,
scoped_refptr<OpenTypeVerticalData> vertical_data =
hb_font_data->VerticalData();
if (!vertical_data) {
return SkiaTextMetrics::SkiaScalarToHarfBuzzPosition(
hb_font_data->height_fallback_);
return SkiaScalarToHarfBuzzPosition(hb_font_data->height_fallback_);
}
Glyph the_glyph = glyph;
float advance_height = -vertical_data->AdvanceHeight(the_glyph);
return SkiaTextMetrics::SkiaScalarToHarfBuzzPosition(
SkFloatToScalar(advance_height));
return SkiaScalarToHarfBuzzPosition(SkFloatToScalar(advance_height));
}
static hb_position_t HarfBuzzGetGlyphHorizontalKerning(
......@@ -208,9 +204,8 @@ static hb_position_t HarfBuzzGetGlyphHorizontalKerning(
int32_t kerning_adjustments[1] = {0};
if (typeface->getKerningPairAdjustments(glyphs, 2, kerning_adjustments)) {
return SkiaTextMetrics::SkiaScalarToHarfBuzzPosition(
SkIntToScalar(kerning_adjustments[0]) *
hb_font_data->SizePerUnit(*typeface));
return SkiaScalarToHarfBuzzPosition(SkIntToScalar(kerning_adjustments[0]) *
hb_font_data->SizePerUnit(*typeface));
}
return 0;
......@@ -224,8 +219,7 @@ static hb_bool_t HarfBuzzGetGlyphExtents(hb_font_t* hb_font,
HarfBuzzFontData* hb_font_data =
reinterpret_cast<HarfBuzzFontData*>(font_data);
SkiaTextMetrics(hb_font_data->font_)
.GetGlyphExtentsForHarfBuzz(glyph, extents);
GetGlyphExtentsForHarfBuzz(hb_font_data->font_, glyph, extents);
return true;
}
......@@ -435,8 +429,7 @@ hb_font_t* HarfBuzzFace::GetScaledFont(
harfbuzz_font_data_->UpdateFallbackMetricsAndScale(*platform_data_,
vertical_layout);
int scale =
SkiaTextMetrics::SkiaScalarToHarfBuzzPosition(platform_data_->size());
int scale = SkiaScalarToHarfBuzzPosition(platform_data_->size());
hb_font_set_scale(unscaled_font_, scale, scale);
hb_font_set_ptem(unscaled_font_, platform_data_->size() / kCssPixelsPerPoint);
......
......@@ -348,7 +348,7 @@ FloatRect SimpleFontData::PlatformBoundsForGlyph(Glyph glyph) const {
static_assert(sizeof(glyph) == 2, "Glyph id should not be truncated.");
SkRect bounds;
SkiaTextMetrics(font_).GetSkiaBoundsForGlyph(glyph, &bounds);
GetBoundsForGlyph(font_, glyph, &bounds);
return FloatRect(bounds);
}
......@@ -360,7 +360,7 @@ void SimpleFontData::BoundsForGlyphs(const Vector<Glyph, 256>& glyphs,
return;
DCHECK_EQ(bounds->size(), glyphs.size());
SkiaTextMetrics(font_).GetSkiaBoundsForGlyphs(glyphs, bounds->data());
GetBoundsForGlyphs(font_, glyphs, bounds->data());
}
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.");
return SkiaTextMetrics(font_).GetSkiaWidthForGlyph(glyph);
return GetWidthForGlyph(font_, glyph);
}
} // namespace blink
......@@ -8,7 +8,6 @@
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include <SkFont.h>
#include <SkPaint.h>
#include <SkPath.h>
namespace blink {
......@@ -22,32 +21,27 @@ T* advance_by_byte_size(T* p, unsigned byte_size) {
} // namespace
SkiaTextMetrics::SkiaTextMetrics(const SkPaint* paint)
: font_(SkFont::LEGACY_ExtractFromPaint(*paint)) {
CHECK(paint->getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
}
SkiaTextMetrics::SkiaTextMetrics(const SkFont& font) : font_(font) {}
void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(hb_codepoint_t codepoint,
hb_position_t* width) {
void GetGlyphWidthForHarfBuzz(const SkFont& font,
hb_codepoint_t codepoint,
hb_position_t* width) {
DCHECK_LE(codepoint, 0xFFFFu);
CHECK(width);
SkScalar sk_width;
uint16_t glyph = codepoint;
font_.getWidths(&glyph, 1, &sk_width, nullptr);
if (!font_.isSubpixel())
font.getWidths(&glyph, 1, &sk_width, nullptr);
if (!font.isSubpixel())
sk_width = SkScalarRoundToInt(sk_width);
*width = SkiaScalarToHarfBuzzPosition(sk_width);
}
void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count,
hb_codepoint_t* glyphs,
unsigned glyph_stride,
hb_position_t* advances,
unsigned advance_stride) {
void GetGlyphWidthForHarfBuzz(const SkFont& font,
unsigned count,
hb_codepoint_t* glyphs,
unsigned glyph_stride,
hb_position_t* advances,
unsigned advance_stride) {
// Batch the call to getWidths because its function entry cost is not
// cheap. getWidths accepts multiple glyphd ID, but not from a sparse
// array that copy them to a regular array.
......@@ -57,9 +51,9 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count,
glyph_array[i] = *glyphs;
}
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++)
sk_width_array[i] = SkScalarRoundToInt(sk_width_array[i]);
}
......@@ -71,16 +65,17 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count,
}
}
void SkiaTextMetrics::GetGlyphExtentsForHarfBuzz(hb_codepoint_t codepoint,
hb_glyph_extents_t* extents) {
void GetGlyphExtentsForHarfBuzz(const SkFont& font,
hb_codepoint_t codepoint,
hb_glyph_extents_t* extents) {
DCHECK_LE(codepoint, 0xFFFFu);
CHECK(extents);
SkRect sk_bounds;
uint16_t glyph = codepoint;
font_.getWidths(&glyph, 1, nullptr, &sk_bounds);
if (!font_.isSubpixel()) {
font.getWidths(&glyph, 1, nullptr, &sk_bounds);
if (!font.isSubpixel()) {
// Use roundOut() rather than round() to avoid rendering glyphs
// outside the visual overflow rect. crbug.com/452914.
sk_bounds.set(sk_bounds.roundOut());
......@@ -94,35 +89,36 @@ void SkiaTextMetrics::GetGlyphExtentsForHarfBuzz(hb_codepoint_t codepoint,
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)
// TODO(drott): Remove this once we have better metrics bounds
// on Mac, https://bugs.chromium.org/p/skia/issues/detail?id=5328
SkPath path;
font_.getPath(glyph, &path);
font.getPath(glyph, &path);
*bounds = path.getBounds();
#else
font_.getWidths(&glyph, 1, nullptr, bounds);
font.getWidths(&glyph, 1, nullptr, bounds);
#endif
if (!font_.isSubpixel()) {
if (!font.isSubpixel()) {
SkIRect ir;
bounds->roundOut(&ir);
bounds->set(ir);
}
}
void SkiaTextMetrics::GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>& glyphs,
SkRect* bounds) {
void GetBoundsForGlyphs(const SkFont& font,
const Vector<Glyph, 256>& glyphs,
SkRect* bounds) {
#if defined(OS_MACOSX)
for (unsigned i = 0; i < glyphs.size(); i++) {
GetSkiaBoundsForGlyph(glyphs[i], &bounds[i]);
GetBoundsForGlyph(font, glyphs[i], &bounds[i]);
}
#else
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++) {
SkIRect ir;
bounds[i].roundOut(&ir);
......@@ -132,17 +128,17 @@ void SkiaTextMetrics::GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>& glyphs,
#endif
}
float SkiaTextMetrics::GetSkiaWidthForGlyph(Glyph glyph) {
float GetWidthForGlyph(const SkFont& font, Glyph glyph) {
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);
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.
static const int kHbPosition1 = 1 << 16;
return clampTo<int>(value * kHbPosition1);
......
......@@ -7,36 +7,32 @@
#include "third_party/blink/renderer/platform/fonts/glyph.h"
#include <SkFont.h>
#include <hb.h>
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "third_party/skia/include/core/SkRect.h"
class SkPaint;
class SkFont;
namespace blink {
class SkiaTextMetrics final {
public:
SkiaTextMetrics(const SkPaint* legacy_paint);
SkiaTextMetrics(const SkFont&);
void GetGlyphWidthForHarfBuzz(hb_codepoint_t, hb_position_t* width);
void GetGlyphWidthForHarfBuzz(unsigned count,
hb_codepoint_t* first_glyph,
unsigned glyph_stride,
hb_position_t* first_advance,
unsigned advance_stride);
void GetGlyphExtentsForHarfBuzz(hb_codepoint_t, hb_glyph_extents_t*);
void GetSkiaBoundsForGlyph(Glyph, SkRect* bounds);
void GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>&, SkRect*);
float GetSkiaWidthForGlyph(Glyph);
static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value);
private:
const SkFont font_;
};
void GetGlyphWidthForHarfBuzz(const SkFont&,
hb_codepoint_t,
hb_position_t* width);
void GetGlyphWidthForHarfBuzz(const SkFont&,
unsigned count,
hb_codepoint_t* first_glyph,
unsigned glyph_stride,
hb_position_t* first_advance,
unsigned advance_stride);
void GetGlyphExtentsForHarfBuzz(const SkFont&,
hb_codepoint_t,
hb_glyph_extents_t*);
void GetBoundsForGlyph(const SkFont&, Glyph, SkRect* bounds);
void GetBoundsForGlyphs(const SkFont&, const Vector<Glyph, 256>&, SkRect*);
float GetWidthForGlyph(const SkFont&, Glyph);
hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value);
} // 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