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

start to use SkFont api (paint's equivalents are deprecated)

TBR=cjgrant

Bug: skia:2664
Change-Id: Ie2b941409bcd84d61c3bdad22d57f45c7089af70
Reviewed-on: https://chromium-review.googlesource.com/c/1306513
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: default avatarDominik Röttsches <drott@chromium.org>
Reviewed-by: default avatarFlorin Malita <fmalita@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605322}
parent 89d73d56
...@@ -1397,21 +1397,20 @@ sk_sp<SkTextBlob> BuildTextBlob( ...@@ -1397,21 +1397,20 @@ sk_sp<SkTextBlob> BuildTextBlob(
typeface = SkTypeface::MakeFromName("monospace", SkFontStyle()); typeface = SkTypeface::MakeFromName("monospace", SkFontStyle());
} }
SkPaint paint; SkFont font;
paint.setTypeface(typeface); font.setTypeface(typeface);
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); font.setHinting(SkFont::kNormal_Hinting);
paint.setHinting(SkPaint::kNormal_Hinting); font.setSize(1u);
paint.setTextSize(1u);
if (use_lcd_text) { if (use_lcd_text) {
paint.setAntiAlias(true); font.DEPRECATED_setAntiAlias(true);
paint.setSubpixelText(true); font.setSubpixel(true);
paint.setLCDRenderText(true); font.DEPRECATED_setLCDRender(true);
} }
SkTextBlobBuilder builder; SkTextBlobBuilder builder;
SkRect bounds = SkRect::MakeWH(100, 100); SkRect bounds = SkRect::MakeWH(100, 100);
const int glyphCount = 10; const int glyphCount = 10;
const auto& runBuffer = builder.allocRunPosH(paint, glyphCount, 0, &bounds); const auto& runBuffer = builder.allocRunPosH(font, glyphCount, 0, &bounds);
for (int i = 0; i < glyphCount; i++) { for (int i = 0; i < glyphCount; i++) {
runBuffer.glyphs[i] = static_cast<SkGlyphID>(i); runBuffer.glyphs[i] = static_cast<SkGlyphID>(i);
runBuffer.pos[i] = SkIntToScalar(i); runBuffer.pos[i] = SkIntToScalar(i);
......
...@@ -1162,8 +1162,7 @@ std::vector<std::vector<sk_sp<SkTypeface>>> test_typefaces = { ...@@ -1162,8 +1162,7 @@ std::vector<std::vector<sk_sp<SkTypeface>>> test_typefaces = {
std::vector<sk_sp<SkTextBlob>> test_paint_blobs = { std::vector<sk_sp<SkTextBlob>> test_paint_blobs = {
[] { [] {
SkPaint font; SkFont font;
font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
font.setTypeface(test_typefaces[0][0]); font.setTypeface(test_typefaces[0][0]);
SkTextBlobBuilder builder; SkTextBlobBuilder builder;
...@@ -1175,8 +1174,7 @@ std::vector<sk_sp<SkTextBlob>> test_paint_blobs = { ...@@ -1175,8 +1174,7 @@ std::vector<sk_sp<SkTextBlob>> test_paint_blobs = {
return builder.make(); return builder.make();
}(), }(),
[] { [] {
SkPaint font; SkFont font;
font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
font.setTypeface(test_typefaces[1][0]); font.setTypeface(test_typefaces[1][0]);
SkTextBlobBuilder builder; SkTextBlobBuilder builder;
......
...@@ -47,8 +47,7 @@ class CachedFont { ...@@ -47,8 +47,7 @@ class CachedFont {
auto& supported = supported_scripts_[script]; auto& supported = supported_scripts_[script];
if (supported != UNDEFINED) if (supported != UNDEFINED)
return supported == KNOWN; return supported == KNOWN;
uint16_t glyph_id; uint16_t glyph_id = typeface_->unicharToGlyph(character);
paint_.textToGlyphs(&character, sizeof(UChar32), &glyph_id);
supported = glyph_id ? KNOWN : UNKNOWN; supported = glyph_id ? KNOWN : UNKNOWN;
return supported == KNOWN; return supported == KNOWN;
} }
...@@ -59,11 +58,10 @@ class CachedFont { ...@@ -59,11 +58,10 @@ class CachedFont {
SkString sk_name; SkString sk_name;
skia_face->getFamilyName(&sk_name); skia_face->getFamilyName(&sk_name);
name_ = std::string(sk_name.c_str(), sk_name.size()); name_ = std::string(sk_name.c_str(), sk_name.size());
paint_.setTypeface(std::move(skia_face)); typeface_ = std::move(skia_face);
paint_.setTextEncoding(SkPaint::kUTF32_TextEncoding);
} }
SkPaint paint_; sk_sp<SkTypeface> typeface_;
std::map<UScriptCode, KnownGlyph> supported_scripts_; std::map<UScriptCode, KnownGlyph> supported_scripts_;
std::string name_; std::string name_;
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
#include "third_party/blink/public/platform/web_string.h" #include "third_party/blink/public/platform/web_string.h"
#include "third_party/skia/include/core/SkRefCnt.h" #include "third_party/skia/include/core/SkRefCnt.h"
class SkFont;
class SkFontMgr; class SkFontMgr;
namespace blink { namespace blink {
...@@ -72,6 +73,7 @@ struct WebFontRenderStyle { ...@@ -72,6 +73,7 @@ struct WebFontRenderStyle {
void OverrideWith(const WebFontRenderStyle& other); void OverrideWith(const WebFontRenderStyle& other);
void ApplyToSkPaint(SkPaint&, float device_scale_factor) const; void ApplyToSkPaint(SkPaint&, float device_scale_factor) const;
void ApplyToSkFont(SkFont*, float device_scale_factor) const;
// Each of the use* members below can take one of three values: // Each of the use* members below can take one of three values:
// 0: off // 0: off
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "third_party/blink/renderer/platform/fonts/font_platform_data.h" #include "third_party/blink/renderer/platform/fonts/font_platform_data.h"
#include "SkFont.h"
#include "SkTypeface.h" #include "SkTypeface.h"
#include "build/build_config.h" #include "build/build_config.h"
#include "hb-ot.h" #include "hb-ot.h"
...@@ -281,13 +282,9 @@ unsigned FontPlatformData::GetHash() const { ...@@ -281,13 +282,9 @@ unsigned FontPlatformData::GetHash() const {
#if !defined(OS_MACOSX) #if !defined(OS_MACOSX)
bool FontPlatformData::FontContainsCharacter(UChar32 character) { bool FontPlatformData::FontContainsCharacter(UChar32 character) {
SkPaint paint; SkFont font;
SetupSkPaint(&paint); SetupSkFont(&font);
paint.setTextEncoding(SkPaint::kUTF32_TextEncoding); return font.unicharToGlyph(character);
uint16_t glyph;
paint.textToGlyphs(&character, sizeof(character), &glyph);
return glyph;
} }
#endif #endif
...@@ -327,6 +324,20 @@ void FontPlatformData::SetupSkPaint(SkPaint* font, ...@@ -327,6 +324,20 @@ void FontPlatformData::SetupSkPaint(SkPaint* font,
font->setEmbeddedBitmapText(!avoid_embedded_bitmaps_); font->setEmbeddedBitmapText(!avoid_embedded_bitmaps_);
} }
void FontPlatformData::SetupSkFont(SkFont* font,
float device_scale_factor,
const Font*) const {
style_.ApplyToSkFont(font, device_scale_factor);
const float ts = text_size_ >= 0 ? text_size_ : 12;
font->setSize(SkFloatToScalar(ts));
font->setTypeface(typeface_);
font->setEmbolden(synthetic_bold_);
font->setSkewX(synthetic_italic_ ? -SK_Scalar1 / 4 : 0);
font->setEmbeddedBitmaps(!avoid_embedded_bitmaps_);
}
#endif #endif
} // namespace blink } // namespace blink
...@@ -65,6 +65,7 @@ inline NSFont* toNSFont(CTFontRef ctFontRef) { ...@@ -65,6 +65,7 @@ inline NSFont* toNSFont(CTFontRef ctFontRef) {
} }
#endif // defined(OS_MACOSX) #endif // defined(OS_MACOSX)
class SkFont;
class SkTypeface; class SkTypeface;
typedef uint32_t SkFontID; typedef uint32_t SkFontID;
...@@ -153,9 +154,14 @@ class PLATFORM_EXPORT FontPlatformData { ...@@ -153,9 +154,14 @@ class PLATFORM_EXPORT FontPlatformData {
const WebFontRenderStyle& GetFontRenderStyle() const { return style_; } const WebFontRenderStyle& GetFontRenderStyle() const { return style_; }
#endif #endif
// TODO(reed): SetupSkPaint is deprecated. Remove this once all call sites
// are moved to SetupSkFont.
void SetupSkPaint(SkPaint*, void SetupSkPaint(SkPaint*,
float device_scale_factor = 1, float device_scale_factor = 1,
const Font* = nullptr) const; const Font* = nullptr) const;
void SetupSkFont(SkFont*,
float device_scale_factor = 1,
const Font* = nullptr) const;
#if defined(OS_WIN) #if defined(OS_WIN)
int PaintTextFlags() const { return paint_text_flags_; } int PaintTextFlags() const { return paint_text_flags_; }
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
#import "third_party/blink/renderer/platform/layout_test_support.h" #import "third_party/blink/renderer/platform/layout_test_support.h"
#import "third_party/blink/renderer/platform/wtf/retain_ptr.h" #import "third_party/blink/renderer/platform/wtf/retain_ptr.h"
#import "third_party/blink/renderer/platform/wtf/text/wtf_string.h" #import "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#import "third_party/skia/include/core/SkFont.h"
#import "third_party/skia/include/core/SkStream.h" #import "third_party/skia/include/core/SkStream.h"
#import "third_party/skia/include/ports/SkTypeface_mac.h" #import "third_party/skia/include/ports/SkTypeface_mac.h"
...@@ -163,6 +164,58 @@ void FontPlatformData::SetupSkPaint(SkPaint* paint, ...@@ -163,6 +164,58 @@ void FontPlatformData::SetupSkPaint(SkPaint* paint,
paint->setHinting(SkPaint::kNo_Hinting); paint->setHinting(SkPaint::kNo_Hinting);
} }
void FontPlatformData::SetupSkFont(SkFont* skfont,
float,
const Font* font) const {
bool should_smooth_fonts = true;
bool should_antialias = true;
bool should_subpixel_position = true;
if (font) {
switch (font->GetFontDescription().FontSmoothing()) {
case kAntialiased:
should_smooth_fonts = false;
break;
case kSubpixelAntialiased:
break;
case kNoSmoothing:
should_antialias = false;
should_smooth_fonts = false;
break;
case kAutoSmoothing:
// For the AutoSmooth case, don't do anything! Keep the default
// settings.
break;
}
}
if (LayoutTestSupport::IsRunningLayoutTest()) {
should_smooth_fonts = false;
should_antialias = should_antialias &&
LayoutTestSupport::IsFontAntialiasingEnabledForTest();
should_subpixel_position =
LayoutTestSupport::IsTextSubpixelPositioningAllowedForTest();
}
skfont->DEPRECATED_setAntiAlias(should_antialias);
skfont->setEmbeddedBitmaps(false);
const float ts = text_size_ >= 0 ? text_size_ : 12;
skfont->setSize(SkFloatToScalar(ts));
skfont->setTypeface(typeface_);
skfont->setEmbolden(synthetic_bold_);
skfont->setSkewX(synthetic_italic_ ? -SK_Scalar1 / 4 : 0);
skfont->DEPRECATED_setLCDRender(should_smooth_fonts);
skfont->setSubpixel(should_subpixel_position);
// When rendering using CoreGraphics, disable hinting when
// webkit-font-smoothing:antialiased or text-rendering:geometricPrecision is
// used. See crbug.com/152304
if (font &&
(font->GetFontDescription().FontSmoothing() == kAntialiased ||
font->GetFontDescription().TextRendering() == kGeometricPrecision))
skfont->setHinting(SkFont::kNo_Hinting);
}
FontPlatformData::FontPlatformData(NSFont* ns_font, FontPlatformData::FontPlatformData(NSFont* ns_font,
float size, float size,
bool synthetic_bold, bool synthetic_bold,
......
...@@ -39,10 +39,9 @@ void ShapeResultBloberizer::CommitPendingRun() { ...@@ -39,10 +39,9 @@ void ShapeResultBloberizer::CommitPendingRun() {
builder_rotation_ = pending_canvas_rotation_; builder_rotation_ = pending_canvas_rotation_;
} }
SkPaint run_font; SkFont run_font;
run_font.setTextEncoding(SkPaint::kGlyphID_TextEncoding); pending_font_data_->PlatformData().SetupSkFont(&run_font,
pending_font_data_->PlatformData().SetupSkPaint(&run_font, device_scale_factor_, &font_);
device_scale_factor_, &font_);
const auto run_size = pending_glyphs_.size(); const auto run_size = pending_glyphs_.size();
const auto& buffer = HasPendingVerticalOffsets() const auto& buffer = HasPendingVerticalOffsets()
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
#include "third_party/blink/renderer/platform/fonts/font_description.h" #include "third_party/blink/renderer/platform/fonts/font_description.h"
#include "third_party/blink/renderer/platform/layout_test_support.h" #include "third_party/blink/renderer/platform/layout_test_support.h"
#include <SkFont.h>
namespace blink { namespace blink {
namespace { namespace {
...@@ -115,4 +117,23 @@ void WebFontRenderStyle::ApplyToSkPaint(SkPaint& font, ...@@ -115,4 +117,23 @@ void WebFontRenderStyle::ApplyToSkPaint(SkPaint& font,
font.setSubpixelText(force_subpixel_positioning || use_subpixel_positioning); font.setSubpixelText(force_subpixel_positioning || use_subpixel_positioning);
} }
void WebFontRenderStyle::ApplyToSkFont(SkFont* font,
float device_scale_factor) const {
auto sk_hint_style = static_cast<SkFont::Hinting>(hint_style);
font->DEPRECATED_setAntiAlias(use_anti_alias);
font->setHinting(sk_hint_style);
font->setEmbeddedBitmaps(use_bitmaps);
font->setForceAutoHinting(use_auto_hint);
if (use_anti_alias)
font->DEPRECATED_setLCDRender(use_subpixel_rendering);
// Force-enable subpixel positioning, except when full hinting is requested on
// low-dpi screen or when running layout tests.
bool force_subpixel_positioning =
!LayoutTestSupport::IsRunningLayoutTest() &&
(sk_hint_style != SkFont::kFull_Hinting || device_scale_factor > 1.0f);
font->setSubpixel(force_subpixel_positioning || use_subpixel_positioning);
}
} // namespace blink } // namespace blink
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include "third_party/blink/renderer/platform/fonts/font_platform_data.h" #include "third_party/blink/renderer/platform/fonts/font_platform_data.h"
#include <windows.h> #include <windows.h>
#include "SkFont.h"
#include "SkTypeface.h" #include "SkTypeface.h"
#include "third_party/blink/renderer/platform/fonts/font_cache.h" #include "third_party/blink/renderer/platform/fonts/font_cache.h"
#include "third_party/blink/renderer/platform/layout_test_support.h" #include "third_party/blink/renderer/platform/layout_test_support.h"
...@@ -72,6 +73,35 @@ void FontPlatformData::SetupSkPaint(SkPaint* font, float, const Font*) const { ...@@ -72,6 +73,35 @@ void FontPlatformData::SetupSkPaint(SkPaint* font, float, const Font*) const {
font->setEmbeddedBitmapText(!avoid_embedded_bitmaps_); font->setEmbeddedBitmapText(!avoid_embedded_bitmaps_);
} }
void FontPlatformData::SetupSkFont(SkFont* font, float, const Font*) const {
font->setSize(SkFloatToScalar(text_size_));
font->setTypeface(typeface_);
font->setEmbolden(synthetic_bold_);
font->setSkewX(synthetic_italic_ ? -SK_Scalar1 / 4 : 0);
uint32_t text_flags = PaintTextFlags();
font->DEPRECATED_setAntiAlias(
SkToBool(text_flags & SkPaint::kAntiAlias_Flag));
font->DEPRECATED_setLCDRender(
SkToBool(text_flags & SkPaint::kLCDRenderText_Flag));
font->setSubpixel(SkToBool(text_flags & SkPaint::kSubpixelText_Flag));
// Only use sub-pixel positioning if anti aliasing is enabled. Otherwise,
// without font smoothing, subpixel text positioning leads to uneven spacing
// since subpixel test placement coordinates would be passed to Skia, which
// only has non-antialiased glyphs to draw, so they necessarily get clamped at
// pixel positions, which leads to uneven spacing, either too close or too far
// away from adjacent glyphs. We avoid this by linking the two flags.
if (text_flags & SkPaint::kAntiAlias_Flag)
font->setSubpixel(true);
if (LayoutTestSupport::IsRunningLayoutTest() &&
!LayoutTestSupport::IsTextSubpixelPositioningAllowedForTest())
font->setSubpixel(false);
font->setEmbeddedBitmaps(!avoid_embedded_bitmaps_);
}
static bool IsWebFont(const String& family_name) { static bool IsWebFont(const String& family_name) {
// Web-fonts have artifical names constructed to always be: // Web-fonts have artifical names constructed to always be:
// 1. 24 characters, followed by a '\0' // 1. 24 characters, followed by a '\0'
......
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