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 @@ ...@@ -7,6 +7,8 @@
#include "build/build_config.h" #include "build/build_config.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h" #include "third_party/blink/renderer/platform/wtf/math_extras.h"
#include <SkFont.h>
#include <SkPaint.h>
#include <SkPath.h> #include <SkPath.h>
namespace blink { namespace blink {
...@@ -20,10 +22,13 @@ T* advance_by_byte_size(T* p, unsigned byte_size) { ...@@ -20,10 +22,13 @@ T* advance_by_byte_size(T* p, unsigned byte_size) {
} // namespace } // namespace
SkiaTextMetrics::SkiaTextMetrics(const SkPaint* paint) : paint_(paint) { SkiaTextMetrics::SkiaTextMetrics(const SkPaint* paint)
CHECK(paint_->getTextEncoding() == SkPaint::kGlyphID_TextEncoding); : 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, void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(hb_codepoint_t codepoint,
hb_position_t* width) { hb_position_t* width) {
DCHECK_LE(codepoint, 0xFFFFu); DCHECK_LE(codepoint, 0xFFFFu);
...@@ -32,8 +37,8 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(hb_codepoint_t codepoint, ...@@ -32,8 +37,8 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(hb_codepoint_t codepoint,
SkScalar sk_width; SkScalar sk_width;
uint16_t glyph = codepoint; uint16_t glyph = codepoint;
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); sk_width = SkScalarRoundToInt(sk_width);
*width = SkiaScalarToHarfBuzzPosition(sk_width); *width = SkiaScalarToHarfBuzzPosition(sk_width);
} }
...@@ -43,8 +48,8 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count, ...@@ -43,8 +48,8 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count,
unsigned glyph_stride, unsigned glyph_stride,
hb_position_t* advances, hb_position_t* advances,
unsigned advance_stride) { unsigned advance_stride) {
// Batch the call to getTextWidths because its function entry cost is not // Batch the call to getWidths because its function entry cost is not
// cheap. getTextWidths accepts multiple glyphd ID, but not from a sparse // cheap. getWidths accepts multiple glyphd ID, but not from a sparse
// array that copy them to a regular array. // array that copy them to a regular array.
Vector<Glyph, 256> glyph_array(count); Vector<Glyph, 256> glyph_array(count);
for (unsigned i = 0; i < count; for (unsigned i = 0; i < count;
...@@ -52,10 +57,9 @@ void SkiaTextMetrics::GetGlyphWidthForHarfBuzz(unsigned count, ...@@ -52,10 +57,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);
paint_->getTextWidths(glyph_array.data(), sizeof(Glyph) * count, font_.getWidths(glyph_array.data(), count, sk_width_array.data(), nullptr);
sk_width_array.data(), nullptr);
if (!paint_->isSubpixelText()) { 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]);
} }
...@@ -75,13 +79,11 @@ void SkiaTextMetrics::GetGlyphExtentsForHarfBuzz(hb_codepoint_t codepoint, ...@@ -75,13 +79,11 @@ void SkiaTextMetrics::GetGlyphExtentsForHarfBuzz(hb_codepoint_t codepoint,
SkRect sk_bounds; SkRect sk_bounds;
uint16_t glyph = codepoint; uint16_t glyph = codepoint;
paint_->getTextWidths(&glyph, sizeof(glyph), nullptr, &sk_bounds); font_.getWidths(&glyph, 1, nullptr, &sk_bounds);
if (!paint_->isSubpixelText()) { 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.
SkIRect ir; sk_bounds.set(sk_bounds.roundOut());
sk_bounds.roundOut(&ir);
sk_bounds.set(ir);
} }
// Invert y-axis because Skia is y-grows-down but we set up HarfBuzz to be // 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) { ...@@ -97,13 +99,13 @@ void SkiaTextMetrics::GetSkiaBoundsForGlyph(Glyph glyph, SkRect* bounds) {
// 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;
paint_->getTextPath(&glyph, sizeof(glyph), 0, 0, &path); font_.getPath(glyph, &path);
*bounds = path.getBounds(); *bounds = path.getBounds();
#else #else
paint_->getTextWidths(&glyph, sizeof(glyph), nullptr, bounds); font_.getWidths(&glyph, 1, nullptr, bounds);
#endif #endif
if (!paint_->isSubpixelText()) { if (!font_.isSubpixel()) {
SkIRect ir; SkIRect ir;
bounds->roundOut(&ir); bounds->roundOut(&ir);
bounds->set(ir); bounds->set(ir);
...@@ -118,10 +120,9 @@ void SkiaTextMetrics::GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>& glyphs, ...@@ -118,10 +120,9 @@ void SkiaTextMetrics::GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>& glyphs,
} }
#else #else
static_assert(sizeof(Glyph) == 2, "Skia expects 2 bytes glyph id."); static_assert(sizeof(Glyph) == 2, "Skia expects 2 bytes glyph id.");
paint_->getTextWidths(glyphs.data(), sizeof(Glyph) * glyphs.size(), nullptr, font_.getWidths(glyphs.data(), glyphs.size(), nullptr, bounds);
bounds);
if (!paint_->isSubpixelText()) { 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);
...@@ -133,9 +134,9 @@ void SkiaTextMetrics::GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>& glyphs, ...@@ -133,9 +134,9 @@ void SkiaTextMetrics::GetSkiaBoundsForGlyphs(const Vector<Glyph, 256>& glyphs,
float SkiaTextMetrics::GetSkiaWidthForGlyph(Glyph glyph) { float SkiaTextMetrics::GetSkiaWidthForGlyph(Glyph glyph) {
SkScalar sk_width; 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); sk_width = SkScalarRoundToInt(sk_width);
return SkScalarToFloat(sk_width); return SkScalarToFloat(sk_width);
......
...@@ -7,15 +7,18 @@ ...@@ -7,15 +7,18 @@
#include "third_party/blink/renderer/platform/fonts/glyph.h" #include "third_party/blink/renderer/platform/fonts/glyph.h"
#include <SkPaint.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"
class SkPaint;
namespace blink { namespace blink {
class SkiaTextMetrics final { class SkiaTextMetrics final {
public: public:
SkiaTextMetrics(const SkPaint*); SkiaTextMetrics(const SkPaint* legacy_paint);
SkiaTextMetrics(const SkFont&);
void GetGlyphWidthForHarfBuzz(hb_codepoint_t, hb_position_t* width); void GetGlyphWidthForHarfBuzz(hb_codepoint_t, hb_position_t* width);
void GetGlyphWidthForHarfBuzz(unsigned count, void GetGlyphWidthForHarfBuzz(unsigned count,
...@@ -32,7 +35,7 @@ class SkiaTextMetrics final { ...@@ -32,7 +35,7 @@ class SkiaTextMetrics final {
static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value); static hb_position_t SkiaScalarToHarfBuzzPosition(SkScalar value);
private: private:
const SkPaint* paint_; 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