Skip to content

Commit

Permalink
Introduce FontPlatformData::CreateSkFont()
Browse files Browse the repository at this point in the history
This patch introduces `FontPlatformData::CreateSkFont()` to return
`SkFont` as replacement of `SetupSk()` modifies passed `SkFont`
because most of callers to pass just constructed `SkFont` to reduce
source size for improving code health.

Note: This patch is a preparation of the CL[1].

[1] http://crrev.com/c/3211416 Parallel Text Shaping

Bug: 1264280
Change-Id: I9a992683fd3a21a1fcfbb796feb051c84a4f6e6e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3565630
Commit-Queue: Yoshifumi Inoue <yosin@chromium.org>
Reviewed-by: Koji Ishii <kojii@chromium.org>
Commit-Queue: Koji Ishii <kojii@chromium.org>
Cr-Commit-Position: refs/heads/main@{#988260}
  • Loading branch information
yosinch authored and Chromium LUCI CQ committed Apr 2, 2022
1 parent 6144262 commit 7636c2f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 48 deletions.
24 changes: 12 additions & 12 deletions third_party/blink/renderer/platform/fonts/font_platform_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,7 @@ unsigned FontPlatformData::GetHash() const {

#if !BUILDFLAG(IS_MAC)
bool FontPlatformData::FontContainsCharacter(UChar32 character) {
SkFont font;
SetupSkFont(&font);
return font.unicharToGlyph(character);
return CreateSkFont().unicharToGlyph(character);
}
#endif

Expand All @@ -236,18 +234,20 @@ WebFontRenderStyle FontPlatformData::QuerySystemRenderStyle(
return result;
}

void FontPlatformData::SetupSkFont(SkFont* font,
float device_scale_factor,
const FontDescription*) const {
style_.ApplyToSkFont(font, device_scale_factor);
SkFont FontPlatformData::CreateSkFont(float device_scale_factor,
const FontDescription*) const {
SkFont font;
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.setSize(SkFloatToScalar(ts));
font.setTypeface(typeface_);
font.setEmbolden(synthetic_bold_);
font.setSkewX(synthetic_italic_ ? -SK_Scalar1 / 4 : 0);

font.setEmbeddedBitmaps(!avoid_embedded_bitmaps_);

font->setEmbeddedBitmaps(!avoid_embedded_bitmaps_);
return font;
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ class PLATFORM_EXPORT FontPlatformData {
const WebFontRenderStyle& GetFontRenderStyle() const { return style_; }
#endif

void SetupSkFont(SkFont*,
float device_scale_factor = 1,
const FontDescription* = nullptr) const;
SkFont CreateSkFont(float device_scale_factor = 1,
const FontDescription* = nullptr) const;

// Computes a digest from the typeface. The digest only depends on the
// underlying font itself, and does not vary by the style (size, weight,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,7 @@ static CFDictionaryRef CascadeToLastResortFontAttributes() {
return make_typeface_fontplatformdata();
}

void FontPlatformData::SetupSkFont(
SkFont* skfont,
SkFont FontPlatformData::CreateSkFont(
float,
const FontDescription* font_description) const {
bool should_smooth_fonts = true;
Expand Down Expand Up @@ -292,33 +291,35 @@ static CFDictionaryRef CascadeToLastResortFontAttributes() {
WebTestSupport::IsTextSubpixelPositioningAllowedForTest();
}

SkFont skfont;
if (should_antialias && should_smooth_fonts) {
skfont->setEdging(SkFont::Edging::kSubpixelAntiAlias);
skfont.setEdging(SkFont::Edging::kSubpixelAntiAlias);
} else if (should_antialias) {
skfont->setEdging(SkFont::Edging::kAntiAlias);
skfont.setEdging(SkFont::Edging::kAntiAlias);
} else {
skfont->setEdging(SkFont::Edging::kAlias);
skfont.setEdging(SkFont::Edging::kAlias);
}
skfont->setEmbeddedBitmaps(false);
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->setSubpixel(should_subpixel_position);
skfont.setSize(SkFloatToScalar(ts));
skfont.setTypeface(typeface_);
skfont.setEmbolden(synthetic_bold_);
skfont.setSkewX(synthetic_italic_ ? -SK_Scalar1 / 4 : 0);
skfont.setSubpixel(should_subpixel_position);

// CoreText always provides linear metrics if it can, so the linear metrics
// flag setting doesn't affect typefaces backed by CoreText. However, it
// does affect FreeType backed typefaces, so set the flag for consistency.
skfont->setLinearMetrics(should_subpixel_position);
skfont.setLinearMetrics(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_description &&
(font_description->FontSmoothing() == kAntialiased ||
font_description->TextRendering() == kGeometricPrecision))
skfont->setHinting(SkFontHinting::kNone);
skfont.setHinting(SkFontHinting::kNone);
return skfont;
}

} // namespace blink
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ struct HarfBuzzFontData : public RefCounted<HarfBuzzFontData> {
unsigned dummy_ascent_inflation = 0;
unsigned dummy_descent_inflation = 0;

font_ = SkFont();
platform_data.SetupSkFont(&font_);
font_ = platform_data.CreateSkFont();

if (UNLIKELY(vertical_layout == HarfBuzzFace::kPrepareForVerticalLayout)) {
FontMetrics::AscentDescentWithHacks(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,8 @@ void ShapeResultBloberizer::CommitPendingRun() {
if (UNLIKELY(!current_character_indexes_.IsEmpty()))
CommitText();

SkFont run_font;
pending_font_data_->PlatformData().SetupSkFont(
&run_font, device_scale_factor_, &font_description_);
SkFont run_font = pending_font_data_->PlatformData().CreateSkFont(
device_scale_factor_, &font_description_);

const auto run_size = pending_glyphs_.size();
const auto text_size = pending_utf8_.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ void SimpleFontData::PlatformInit(bool subpixel_ascent_descent,

SkFontMetrics metrics;

font_ = SkFont();
platform_data_.SetupSkFont(&font_);
font_ = platform_data_.CreateSkFont();
font_.getMetrics(&metrics);

float ascent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,19 @@

namespace blink {

void FontPlatformData::SetupSkFont(SkFont* font,
float,
const FontDescription*) const {
font->setSize(SkFloatToScalar(text_size_));
font->setTypeface(typeface_);
font->setEmbolden(synthetic_bold_);
font->setSkewX(synthetic_italic_ ? -SK_Scalar1 / 4 : 0);
SkFont FontPlatformData::CreateSkFont(float, const FontDescription*) const {
SkFont font;
font.setSize(SkFloatToScalar(text_size_));
font.setTypeface(typeface_);
font.setEmbolden(synthetic_bold_);
font.setSkewX(synthetic_italic_ ? -SK_Scalar1 / 4 : 0);

if (style_.use_subpixel_rendering) {
font->setEdging(SkFont::Edging::kSubpixelAntiAlias);
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
} else if (style_.use_anti_alias) {
font->setEdging(SkFont::Edging::kAntiAlias);
font.setEdging(SkFont::Edging::kAntiAlias);
} else {
font->setEdging(SkFont::Edging::kAlias);
font.setEdging(SkFont::Edging::kAlias);
}

// Only use sub-pixel positioning if anti aliasing is enabled. Otherwise,
Expand All @@ -62,13 +61,14 @@ void FontPlatformData::SetupSkFont(SkFont* font,
// 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 (style_.use_anti_alias)
font->setSubpixel(true);
font.setSubpixel(true);

if (WebTestSupport::IsRunningWebTest() &&
!WebTestSupport::IsTextSubpixelPositioningAllowedForTest())
font->setSubpixel(false);
font.setSubpixel(false);

font->setEmbeddedBitmaps(!avoid_embedded_bitmaps_);
font.setEmbeddedBitmaps(!avoid_embedded_bitmaps_);
return font;
}

WebFontRenderStyle FontPlatformData::QuerySystemForRenderStyle() {
Expand Down

0 comments on commit 7636c2f

Please sign in to comment.