Skip to content

Commit

Permalink
allow to configure font rasterisation settings in paragraph
Browse files Browse the repository at this point in the history
fix viewer compilation
  • Loading branch information
SergeevPavel authored and eymar committed Feb 8, 2024
1 parent edffd94 commit 3a00e26
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 6 deletions.
27 changes: 27 additions & 0 deletions modules/skparagraph/include/FontRastrSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2019 Google LLC.
#ifndef FontRastrSettings_DEFINED
#define FontRastrSettings_DEFINED

#include "include/core/SkFont.h"
#include "include/core/SkFontTypes.h"

namespace skia {
namespace textlayout {

struct FontRastrSettings {
FontRastrSettings() {
fEdging = SkFont::Edging::kAntiAlias;
fHinting = SkFontHinting::kSlight;
fSubpixel = true;
}

SkFont::Edging fEdging;
SkFontHinting fHinting;
bool fSubpixel;
};


} // namespace textlayout
} // namespace skia

#endif // Metrics_DEFINED
5 changes: 5 additions & 0 deletions modules/skparagraph/include/ParagraphStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "include/core/SkScalar.h"
#include "include/core/SkString.h"
#include "modules/skparagraph/include/DartTypes.h"
#include "modules/skparagraph/include/FontRastrSettings.h"
#include "modules/skparagraph/include/TextStyle.h"

#include <stddef.h>
Expand Down Expand Up @@ -110,6 +111,9 @@ struct ParagraphStyle {
const StrutStyle& getStrutStyle() const { return fStrutStyle; }
void setStrutStyle(StrutStyle strutStyle) { fStrutStyle = std::move(strutStyle); }

const FontRastrSettings& getFontRastrSettings() const { return fFontRastrSettings; }
void setFontRastrSettings(FontRastrSettings fontRastrSettings) { fFontRastrSettings = fontRastrSettings; }

const TextStyle& getTextStyle() const { return fDefaultTextStyle; }
void setTextStyle(const TextStyle& textStyle) { fDefaultTextStyle = textStyle; }

Expand Down Expand Up @@ -164,6 +168,7 @@ struct ParagraphStyle {
bool fReplaceTabCharacters;
bool fApplyRoundingHack = true;
TextIndent fTextIndent;
FontRastrSettings fFontRastrSettings;
};
} // namespace textlayout
} // namespace skia
Expand Down
5 changes: 5 additions & 0 deletions modules/skparagraph/include/TextStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "include/core/SkScalar.h"
#include "modules/skparagraph/include/DartTypes.h"
#include "modules/skparagraph/include/FontArguments.h"
#include "modules/skparagraph/include/FontRastrSettings.h"
#include "modules/skparagraph/include/ParagraphPainter.h"
#include "modules/skparagraph/include/TextShadow.h"

Expand Down Expand Up @@ -276,6 +277,9 @@ class TextStyle {
sk_sp<SkTypeface> refTypeface() const { return fTypeface; }
void setTypeface(sk_sp<SkTypeface> typeface) { fTypeface = std::move(typeface); }

const FontRastrSettings& getFontRastrSettings() const { return fFontRastrSettings; }
void setFontRastrSettings(FontRastrSettings fontRastrSettings) { fFontRastrSettings = fontRastrSettings; }

SkString getLocale() const { return fLocale; }
void setLocale(const SkString& locale) { fLocale = locale; }

Expand Down Expand Up @@ -327,6 +331,7 @@ class TextStyle {

sk_sp<SkTypeface> fTypeface;
bool fIsPlaceholder = false;
FontRastrSettings fFontRastrSettings;

std::vector<FontFeature> fFontFeatures;

Expand Down
62 changes: 62 additions & 0 deletions modules/skparagraph/slides/ParagraphSlide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "include/core/SkTypeface.h"
#include "include/effects/SkGradientShader.h"
#include "modules/skparagraph/include/Paragraph.h"
#include "modules/skparagraph/include/ParagraphStyle.h"
#include "modules/skparagraph/include/TypefaceFontProvider.h"
#include "modules/skparagraph/src/ParagraphBuilderImpl.h"
#include "modules/skparagraph/src/ParagraphImpl.h"
Expand Down Expand Up @@ -3588,6 +3589,64 @@ class ParagraphSlide68 : public ParagraphSlide_Base {
}
};

class ParagraphSlide69 : public ParagraphSlide_Base {
public:
ParagraphSlide69() { fName = "Paragraph69"; }

void draw(SkCanvas* canvas) override {
canvas->drawColor(SK_ColorWHITE);

auto fontCollection =
sk_make_sp<TestFontCollection>(GetResourcePath("fonts").c_str(), false, true);

auto paragraph1 = paragraphWithRastrSettings(fontCollection, "Lorien Ipsum [kAntiAlias, SkFontHinting::kSlight, subpixel: true] [default]",
SkFont::Edging::kAntiAlias,
SkFontHinting::kSlight,
true);

auto paragraph2 = paragraphWithRastrSettings(fontCollection, "Lorien Ipsum [kSubpixelAntiAlias, SkFontHinting::kFull, subpixel: true]",
SkFont::Edging::kSubpixelAntiAlias,
SkFontHinting::kFull,
true);

auto paragraph3 = paragraphWithRastrSettings(fontCollection, "Lorien Ipsum [kSubpixelAntiAlias, SkFontHinting::kFull, subpixel: false]",
SkFont::Edging::kSubpixelAntiAlias,
SkFontHinting::kFull,
false);

paragraph1->layout(600);
paragraph1->paint(canvas, 0, 0);
paragraph2->layout(600);
paragraph2->paint(canvas, 0, 40);
paragraph3->layout(600);
paragraph3->paint(canvas, 0, 80);
}

protected:
std::unique_ptr<Paragraph> paragraphWithRastrSettings(sk_sp<FontCollection> fontCollection,
const char* text,
SkFont::Edging edging,
SkFontHinting hinting,
bool subpixel) {
FontRastrSettings fontRastrSettings;
fontRastrSettings.fEdging = edging;
fontRastrSettings.fHinting = hinting;
fontRastrSettings.fSubpixel = subpixel;


ParagraphStyle paragraph_style;
paragraph_style.setFontRastrSettings(fontRastrSettings);
TextStyle textStyle;
textStyle.setFontFamilies({SkString("Roboto")});
textStyle.setFontSize(14.0);
textStyle.setColor(SK_ColorBLACK);
ParagraphBuilderImpl builder(paragraph_style, fontCollection);
builder.pushStyle(textStyle);
builder.addText(text);
return builder.Build();
}
};

// Google logo is shown in one style (the first one)
class ParagraphSlide_MultiStyle_Logo : public ParagraphSlide_Base {
public:
Expand Down Expand Up @@ -4382,6 +4441,7 @@ DEF_SLIDE(return new ParagraphSlide64();)
DEF_SLIDE(return new ParagraphSlide66();)
DEF_SLIDE(return new ParagraphSlide67();)
DEF_SLIDE(return new ParagraphSlide68();)
DEF_SLIDE(return new ParagraphSlide69();)
DEF_SLIDE(return new ParagraphSlide_MultiStyle_Logo();)
DEF_SLIDE(return new ParagraphSlide_MultiStyle_FFI();)
DEF_SLIDE(return new ParagraphSlide_MultiStyle_EmojiFamily();)
Expand All @@ -4394,3 +4454,5 @@ DEF_SLIDE(return new ParagraphSlideExperiment();)
DEF_SLIDE(return new ParagraphSlideGlyphs();)
DEF_SLIDE(return new ParagraphSlideEllipsisInRTL();)
DEF_SLIDE(return new ParagraphSlideLast();)
DEF_SLIDE(return new ParagraphSlideEllipsisCases();)
DEF_SLIDE(return new ParagraphSlideLast();)
7 changes: 4 additions & 3 deletions modules/skparagraph/src/OneLineShaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,10 @@ bool OneLineShaper::shape() {

// Create one more font to try
SkFont font(std::move(typeface), block.fStyle.getFontSize());
font.setEdging(SkFont::Edging::kAntiAlias);
font.setHinting(SkFontHinting::kSlight);
font.setSubpixel(true);
auto fontRastrSettings = fParagraph->fParagraphStyle.getFontRastrSettings();
font.setEdging(fontRastrSettings.fEdging);
font.setHinting(fontRastrSettings.fHinting);
font.setSubpixel(fontRastrSettings.fSubpixel);

// Apply fake bold and/or italic settings to the font if the
// typeface's attributes do not match the intended font style.
Expand Down
1 change: 1 addition & 0 deletions modules/skparagraph/src/ParagraphBuilderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ ParagraphBuilderImpl::~ParagraphBuilderImpl() = default;

void ParagraphBuilderImpl::pushStyle(const TextStyle& style) {
fTextStyles.push_back(style);
fTextStyles.back().setFontRastrSettings(fParagraphStyle.getFontRastrSettings());
if (!fStyledBlocks.empty() && fStyledBlocks.back().fRange.end == fUtf8.size() &&
fStyledBlocks.back().fStyle == style) {
// Just continue with the same style
Expand Down
6 changes: 3 additions & 3 deletions modules/skparagraph/src/TextStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ bool TextStyle::matchOneAttribute(StyleType styleType, const TextStyle& other) c

void TextStyle::getFontMetrics(SkFontMetrics* metrics) const {
SkFont font(fTypeface, fFontSize);
font.setEdging(SkFont::Edging::kAntiAlias);
font.setSubpixel(true);
font.setHinting(SkFontHinting::kSlight);
font.setEdging(fFontRastrSettings.fEdging);
font.setSubpixel(fFontRastrSettings.fSubpixel);
font.setHinting(fFontRastrSettings.fHinting);
font.getMetrics(metrics);
if (fHeightOverride) {
auto multiplier = fHeight * fFontSize;
Expand Down

0 comments on commit 3a00e26

Please sign in to comment.