Commit f537020f authored by Mike Reed's avatar Mike Reed Committed by Commit Bot

Use SkFont instead of SkPaint for SkiaTextMetrics

Bug: skia:2664
Change-Id: I301b41217f901fe69d57dab9e69f7c468c34c0c9
Reviewed-on: https://chromium-review.googlesource.com/c/1338260
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: default avatarFlorin Malita <fmalita@chromium.org>
Cr-Commit-Position: refs/heads/master@{#609132}
parent 320aa0f1
......@@ -7,6 +7,8 @@
#include "build/build_config.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include <SkFont.h>
#include <SkPaint.h>
#include <SkPath.h>
namespace blink {
......@@ -20,10 +22,13 @@ T* advance_by_byte_size(T* p, unsigned byte_size) {
} // namespace
SkiaTextMetrics::SkiaTextMetrics(const SkPaint* paint) : paint_(paint) {
CHECK(paint_->getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
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) {
DCHECK_LE(codepoint, 0xFFFFu);
......@@ -32,8 +37,8 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(hb_codepoint_t codepoint,
SkScalar sk_width;
uint16_t glyph = codepoint;
paint_->getTextWidths(&glyph, sizeof(glyph), &sk_width, nullptr);
if (!paint_->isSubpixelText())
font_.getWidths(&glyph, 1, &sk_width, nullptr);
if (!font_.isSubpixel())
sk_width = SkScalarRoundToInt(sk_width);
*width = SkiaScalarToHarfBuzzPosition(sk_width);
}
......@@ -43,8 +48,8 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count,
unsigned glyph_stride,
hb_position_t* advances,
unsigned advance_stride) {
// Batch the call to getTextWidths because its function entry cost is not
// cheap. getTextWidths accepts multiple glyphd ID, but not from a sparse
// 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.
Vector<Glyph, 256> glyph_array(count);
for (unsigned i = 0; i < count;
......@@ -52,10 +57,9 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count,
glyph_array[i] = *glyphs;
}
Vector<SkScalar, 256> sk_width_array(count);
paint_->getTextWidths(glyph_array.data(), sizeof(Glyph) * count,
sk_width_array.data(), nullptr);
font_.getWidths(glyph_array.data(), count, sk_width_array.data(), nullptr);
if (!paint_->isSubpixelText()) {
if (!font_.isSubpixel()) {
for (unsigned i = 0; i < count; i++)
sk_width_array[i] = SkScalarRoundToInt(sk_width_array[i]);
}
......@@ -75,13 +79,11 @@ void SkiaTextMetrics::GetGlyphExtentsForHarfBuzz(hb_codepoint_t codepoint,
SkRect sk_bounds;
uint16_t glyph = codepoint;
paint_->getTextWidths(&glyph, sizeof(glyph), nullptr, &sk_bounds);
if (!paint_->isSubpixelText()) {
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.
SkIRect ir;
sk_bounds.roundOut(&ir);
sk_bounds.set(ir);
sk_bounds.set(sk_bounds.roundOut());
}
// Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be
......@@ -97,13 +99,13 @@ void SkiaTextMetrics::GetSkiaBoundsForGlyph(Glyph glyph, SkRect* bounds) {
// TODO(drott): Remove this once we have better metrics bounds
// on Mac, https://bugs.chromium.org/p/skia/issues/detail?id=5328
SkPath path;
paint_->getTextPath(&glyph, sizeof(glyph), 0, 0, &path);
font_.getPath(glyph, &path);
*bounds = path.getBounds();
#else
paint_->getTextWidths(&glyph, sizeof(glyph), nullptr, bounds);
font_.getWidths(&glyph, 1, nullptr, bounds);
#endif
if (!paint_->isSubpixelText()) {
if (!font_.isSubpixel()) {
SkIRect ir;
bounds->roundOut(&ir);
bounds->set(ir);
......@@ -118,10 +120,9 @@ void SkiaTextMetrics::GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>& glyphs,
}
#else
static_assert(sizeof(Glyph) == 2, "Skia expects 2 bytes glyph id.");
paint_->getTextWidths(glyphs.data(), sizeof(Glyph) * glyphs.size(), nullptr,
bounds);
font_.getWidths(glyphs.data(), glyphs.size(), nullptr, bounds);
if (!paint_->isSubpixelText()) {
if (!font_.isSubpixel()) {
for (unsigned i = 0; i < glyphs.size(); i++) {
SkIRect ir;
bounds[i].roundOut(&ir);
......@@ -133,9 +134,9 @@ void SkiaTextMetrics::GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>& glyphs,
float SkiaTextMetrics::GetSkiaWidthForGlyph(Glyph glyph) {
SkScalar sk_width;
paint_->getTextWidths(&glyph, sizeof(glyph), &sk_width, nullptr);
font_.getWidths(&glyph, 1, &sk_width, nullptr);
if (!paint_->isSubpixelText())
if (!font_.isSubpixel())
sk_width = SkScalarRoundToInt(sk_width);
return SkScalarToFloat(sk_width);
......
......@@ -7,15 +7,18 @@
#include "third_party/blink/renderer/platform/fonts/glyph.h"
#include <SkPaint.h>
#include <SkFont.h>
#include <hb.h>
#include "third_party/blink/renderer/platform/wtf/vector.h"
class SkPaint;
namespace blink {
class SkiaTextMetrics final {
public:
SkiaTextMetrics(const SkPaint*);
SkiaTextMetrics(const SkPaint* legacy_paint);
SkiaTextMetrics(const SkFont&);
void GetGlyphWidthForHarfBuzz(hb_codepoint_t, hb_position_t* width);
void GetGlyphWidthForHarfBuzz(unsigned count,
......@@ -32,7 +35,7 @@ class SkiaTextMetrics final {
static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value);
private:
const SkPaint* paint_;
const SkFont font_;
};
} // 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