change textutils to require font param

Bug: skia:
Change-Id: I8ba252e3fc5c0970fd9d83f73c32b68f3c68452e
Reviewed-on: https://skia-review.googlesource.com/c/180771
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
Auto-Submit: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2019-01-03 15:45:53 -05:00 committed by Skia Commit-Bot
parent 4683942a45
commit b579f07110
17 changed files with 78 additions and 142 deletions

View File

@ -68,10 +68,9 @@ protected:
}
void onOnceBeforeDraw() override {
fLabelPaint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&fLabelPaint);
fLabelPaint.setTextSize(5 * kShapeSize/8);
fLabelPaint.setSubpixelText(true);
fLabelFont.setTypeface(sk_tool_utils::create_portable_typeface());
fLabelFont.setSize(5 * kShapeSize/8);
fLabelFont.setSubpixel(true);
constexpr SkScalar radius = -1.4f * kShapeSize/2;
SkPoint pts[4] = {
@ -109,12 +108,12 @@ protected:
if (kShape_Pass == drawingPass) {
SkTextUtils::DrawString(canvas, "Src Unknown",
kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2,
kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint,
kSubtitleSpacing / 2 + fLabelFont.getSize() / 3, fLabelFont, SkPaint(),
SkTextUtils::kCenter_Align);
SkTextUtils::DrawString(canvas, "Src Opaque",
kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 +
kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3,
fLabelPaint, SkTextUtils::kCenter_Align);
kPaintSpacing, kSubtitleSpacing / 2 + fLabelFont.getSize() / 3,
fLabelFont, SkPaint(), SkTextUtils::kCenter_Align);
}
canvas->translate(0, kSubtitleSpacing + kShapeSpacing/2);
@ -177,16 +176,16 @@ protected:
canvas->translate(kMargin, kMargin);
draw_pass(canvas, kBackground_Pass);
SkPaint titlePaint(fLabelPaint);
titlePaint.setTextSize(9 * titlePaint.getTextSize() / 8);
titlePaint.setFakeBoldText(true);
SkFont titleFont(fLabelFont);
titleFont.setSize(9 * titleFont.getSize() / 8);
titleFont.setEmbolden(true);
SkTextUtils::DrawString(canvas, "Porter Duff",
kLabelSpacing + 4 * kShapeTypeSpacing,
kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint,
kTitleSpacing / 2 + titleFont.getSize() / 3, titleFont, SkPaint(),
SkTextUtils::kCenter_Align);
SkTextUtils::DrawString(canvas, "Advanced",
kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSpacing,
kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint,
kTitleSpacing / 2 + titleFont.getSize() / 3, titleFont, SkPaint(),
SkTextUtils::kCenter_Align);
draw_pass(canvas, kShape_Pass);
@ -196,7 +195,7 @@ protected:
void drawModeName(SkCanvas* canvas, SkBlendMode mode) {
const char* modeName = SkBlendMode_Name(mode);
SkTextUtils::DrawString(canvas, modeName, kLabelSpacing - kShapeSize / 4,
fLabelPaint.getTextSize() / 4, fLabelPaint,
fLabelFont.getSize() / 4, fLabelFont, SkPaint(),
SkTextUtils::kRight_Align);
}
@ -266,7 +265,7 @@ protected:
}
private:
SkPaint fLabelPaint;
SkFont fLabelFont;
SkPath fOval;
SkPath fConcave;

View File

@ -114,14 +114,11 @@ protected:
auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode, &m);
SkPaint labelP;
labelP.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&labelP);
SkFont labelFont(sk_tool_utils::create_portable_typeface());
SkPaint textP;
textP.setAntiAlias(true);
textP.setTypeface(fColorType);
textP.setTextSize(SkIntToScalar(70));
SkFont textFont(fColorType, 70);
const int W = 5;
@ -146,15 +143,15 @@ protected:
SkAutoCanvasRestore arc(canvas, true);
canvas->clipRect(r);
textP.setBlendMode(gModes[i]);
textP.setTextEncoding(kUTF32_SkTextEncoding);
const char* text = sk_tool_utils::emoji_sample_text();
SkUnichar unichar = SkUTF::NextUTF8(&text, text + strlen(text));
SkASSERT(unichar >= 0);
canvas->drawText(&unichar, 4, x+ w/10.f, y + 7.f*h/8.f, textP);
canvas->drawSimpleText(&unichar, 4, kUTF32_SkTextEncoding, x+ w/10.f, y + 7.f*h/8.f,
textFont, textP);
}
#if 1
const char* label = SkBlendMode_Name(gModes[i]);
SkTextUtils::DrawString(canvas, label, x + w/2, y - labelP.getTextSize()/2, labelP,
SkTextUtils::DrawString(canvas, label, x + w/2, y - labelFont.getSize()/2, labelFont, SkPaint(),
SkTextUtils::kCenter_Align);
#endif
x += w + SkIntToScalar(10);

View File

@ -40,12 +40,11 @@ static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> im
paint.setImageFilter(std::move(imf));
paint.setColor(SK_ColorGREEN);
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setTextSize(r.height()/2);
SkFont font(sk_tool_utils::create_portable_typeface(), r.height()/2);
canvas->save();
canvas->clipRect(r);
SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), paint,
SkTextUtils::kCenter_Align);
SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint, SkTextUtils::kCenter_Align);
canvas->restore();
}

View File

@ -43,11 +43,9 @@ static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> im
SkPaint paint;
paint.setImageFilter(std::move(imf));
paint.setColor(SK_ColorGREEN);
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setTextSize(r.height()/2);
SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), paint,
SkTextUtils::kCenter_Align);
SkFont font(sk_tool_utils::create_portable_typeface(), r.height()/2);
SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint, SkTextUtils::kCenter_Align);
}
static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {

View File

@ -36,7 +36,6 @@ static void test_draw(SkCanvas* canvas, const char label[]) {
SkPaint paint;
paint.setAntiAlias(true);
paint.setLCDRenderText(true);
paint.setDither(true);
paint.setShader(make_shader());
@ -44,9 +43,10 @@ static void test_draw(SkCanvas* canvas, const char label[]) {
paint.setShader(nullptr);
paint.setColor(SK_ColorWHITE);
paint.setTextSize(32);
sk_tool_utils::set_portable_typeface(&paint);
SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, paint, SkTextUtils::kCenter_Align);
SkFont font(sk_tool_utils::create_portable_typeface(), 32);
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, font, paint,
SkTextUtils::kCenter_Align);
}
class SurfacePropsGM : public skiagm::GM {

View File

@ -99,15 +99,13 @@ protected:
SkScalar y = SkIntToScalar(24);
SkScalar x = SkIntToScalar(10)/scale;
SkFont font(sk_tool_utils::create_portable_typeface());
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
SkPaint p;
SkString str;
p.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&p);
str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
SkTextUtils::DrawString(canvas, str, scale*(x + r.width()/2), y, p,
SkTextUtils::DrawString(canvas, str.c_str(), scale*(x + r.width()/2), y, font, SkPaint(),
SkTextUtils::kCenter_Align);
x += r.width() * 4 / 3;
@ -222,13 +220,12 @@ protected:
SkScalar y = SkIntToScalar(24);
SkScalar x = SkIntToScalar(66);
SkPaint p;
p.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&p);
SkFont font(sk_tool_utils::create_portable_typeface());
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {
SkString str(gModeNames[kx]);
SkTextUtils::DrawString(canvas, str, x + r.width()/2, y, p, SkTextUtils::kCenter_Align);
SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, SkPaint(),
SkTextUtils::kCenter_Align);
x += r.width() * 4 / 3;
}
@ -238,7 +235,7 @@ protected:
x = SkIntToScalar(16) + w;
SkString str(gModeNames[ky]);
SkTextUtils::DrawString(canvas, str, x, y + h/2, p, SkTextUtils::kRight_Align);
SkTextUtils::DrawString(canvas, str.c_str(), x, y + h/2, font, SkPaint(), SkTextUtils::kRight_Align);
x += SkIntToScalar(50);
for (size_t kx = 0; kx < SK_ARRAY_COUNT(gModes); kx++) {

View File

@ -227,7 +227,8 @@ protected:
SkPaint labelP;
labelP.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&labelP);
SkFont font(sk_tool_utils::create_portable_typeface());
const int W = 5;
@ -258,8 +259,8 @@ protected:
#if 1
const char* label = SkBlendMode_Name(gModes[i].fMode);
SkTextUtils::DrawString(canvas, label, x + w/2, y - labelP.getTextSize()/2,
labelP, SkTextUtils::kCenter_Align);
SkTextUtils::DrawString(canvas, label, x + w/2, y - font.getSize()/2,
font, labelP, SkTextUtils::kCenter_Align);
#endif
x += w + SkIntToScalar(10);
if ((i % W) == W - 1) {

View File

@ -33,9 +33,7 @@ protected:
const SkScalar w = SkIntToScalar(kSize);
const SkScalar h = SkIntToScalar(kSize);
SkPaint labelP;
labelP.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&labelP);
SkFont font(sk_tool_utils::create_portable_typeface());
const int W = 6;
@ -72,8 +70,7 @@ protected:
canvas->restore();
#if 1
SkTextUtils::DrawString(canvas, SkBlendMode_Name(mode),
x + w/2, y - labelP.getTextSize()/2, labelP,
SkTextUtils::DrawString(canvas, SkBlendMode_Name(mode), x + w/2, y - font.getSize()/2, font, SkPaint(),
SkTextUtils::kCenter_Align);
#endif
x += w + SkIntToScalar(10);

View File

@ -32,26 +32,6 @@ public:
Draw(canvas, text, strlen(text), kUTF8_SkTextEncoding, x, y, font, paint, align);
}
#if 1
static void DrawString(SkCanvas* canvas, const char text[], SkScalar x, SkScalar y,
const SkPaint& paint, Align align = kLeft_Align) {
SkASSERT(paint.getTextEncoding() == kUTF8_SkTextEncoding);
Draw(canvas, text, strlen(text), kUTF8_SkTextEncoding, x, y,
SkFont::LEGACY_ExtractFromPaint(paint), paint, align);
}
static void DrawText(SkCanvas* canvas, const void* text, size_t size, SkScalar x, SkScalar y,
const SkPaint& paint, Align align = kLeft_Align) {
Draw(canvas, text, size, paint.getTextEncoding(), x, y,
SkFont::LEGACY_ExtractFromPaint(paint), paint, align);
}
static void DrawString(SkCanvas* canvas, const SkString& str, SkScalar x, SkScalar y,
const SkPaint& paint, Align align = kLeft_Align) {
DrawText(canvas, str.c_str(), str.size(), x, y, paint, align);
}
#endif
static void GetPath(const void* text, size_t length, SkTextEncoding, SkScalar x, SkScalar y,
const SkFont&, SkPath*);
};

View File

@ -491,7 +491,7 @@ struct ButtonPaints {
static const int kMaxStateCount = 3;
SkPaint fDisabled;
SkPaint fStates[kMaxStateCount];
SkPaint fLabel;
SkFont fLabelFont;
ButtonPaints() {
fStates[0].setAntiAlias(true);
@ -501,9 +501,7 @@ struct ButtonPaints {
fStates[1].setStrokeWidth(3);
fStates[2] = fStates[1];
fStates[2].setColor(0xFFcf0000);
fLabel.setAntiAlias(true);
fLabel.setTextSize(25.0f);
fLabel.setStyle(SkPaint::kFill_Style);
fLabelFont.setSize(25.0f);
}
};
@ -542,8 +540,8 @@ struct Button {
return;
}
canvas->drawRect(fBounds, paints.fStates[fState]);
SkTextUtils::DrawText(canvas, &fLabel, 1, fBounds.centerX(), fBounds.fBottom - 5,
paints.fLabel, SkTextUtils::kCenter_Align);
SkTextUtils::Draw(canvas, &fLabel, 1, kUTF8_SkTextEncoding, fBounds.centerX(), fBounds.fBottom - 5,
paints.fLabelFont, SkPaint(), SkTextUtils::kCenter_Align);
}
void toggle() {
@ -791,8 +789,8 @@ class AAGeometryView : public Sample {
SkPaint fActivePaint;
SkPaint fComplexPaint;
SkPaint fCoveragePaint;
SkPaint fLegendLeftPaint;
SkPaint fLegendRightPaint;
SkFont fLegendLeftFont;
SkFont fLegendRightFont;
SkPaint fPointPaint;
SkPaint fSkeletonPaint;
SkPaint fLightSkeletonPaint;
@ -866,9 +864,8 @@ public:
fActivePaint.setStrokeWidth(5);
fComplexPaint = fActivePaint;
fComplexPaint.setColor(SK_ColorBLUE);
fLegendLeftPaint.setAntiAlias(true);
fLegendLeftPaint.setTextSize(13);
fLegendRightPaint = fLegendLeftPaint;
fLegendLeftFont.setSize(13);
fLegendRightFont = fLegendLeftFont;
construct_path(fPath);
fFillButton.fVisible = fSkeletonButton.fVisible = fFilterButton.fVisible
= fBisectButton.fVisible = fJoinButton.fVisible = fInOutButton.fVisible = true;
@ -1815,12 +1812,11 @@ void AAGeometryView::draw_legend(SkCanvas* canvas) {
SkScalar bottomOffset = this->height() - 10;
for (int index = kKeyCommandCount - 1; index >= 0; --index) {
bottomOffset -= 15;
SkTextUtils::DrawString(canvas, kKeyCommandList[index].fDescriptionL,
this->width() - 160, bottomOffset,
fLegendLeftPaint);
SkTextUtils::DrawString(canvas, kKeyCommandList[index].fDescriptionL, this->width() - 160, bottomOffset,
fLegendLeftFont, SkPaint());
SkTextUtils::DrawString(canvas, kKeyCommandList[index].fDescriptionR,
this->width() - 20, bottomOffset,
fLegendRightPaint, SkTextUtils::kRight_Align);
fLegendRightFont, SkPaint(), SkTextUtils::kRight_Align);
}
}

View File

@ -104,15 +104,13 @@ protected:
}
static void DrawLabel(SkCanvas* canvas, const SkRect& rect, SkScalar start, SkScalar sweep) {
SkPaint paint;
paint.setAntiAlias(true);
SkFont font;
SkString str;
str.appendScalar(start);
str.append(", ");
str.appendScalar(sweep);
SkTextUtils::DrawString(canvas, str, rect.centerX(),
rect.fBottom + paint.getTextSize() * 5/4, paint,
SkTextUtils::DrawString(canvas, str.c_str(), rect.centerX(),
rect.fBottom + font.getSize() * 5/4, font, SkPaint(),
SkTextUtils::kCenter_Align);
}

View File

@ -45,7 +45,6 @@ static sk_sp<SkImage> make_atlas(int atlasSize, int cellSize) {
SkCanvas* canvas = surface->getCanvas();
SkPaint paint;
paint.setAntiAlias(true);
SkRandom rand;
const SkScalar half = cellSize * SK_ScalarHalf;
@ -57,8 +56,8 @@ static sk_sp<SkImage> make_atlas(int atlasSize, int cellSize) {
paint.setColor(rand.nextU());
paint.setAlpha(0xFF);
int index = i % strlen(s);
SkTextUtils::DrawText(canvas, &s[index], 1, x + half, y + half + half/2, paint,
SkTextUtils::kCenter_Align);
SkTextUtils::Draw(canvas, &s[index], 1, kUTF8_SkTextEncoding, x + half, y + half + half/2, SkFont(), paint,
SkTextUtils::kCenter_Align);
i += 1;
}
}

View File

@ -80,25 +80,21 @@ protected:
x = SkScalarRoundToScalar(x);
y = SkScalarRoundToScalar(y);
canvas->drawBitmap(fBitmaps[i], x, y, &paint);
SkFont font;
font.setSize(SkIntToScalar(18));
if (i == 0) {
SkPaint p;
p.setAntiAlias(true);
p.setTextSize(SkIntToScalar(18));
SkString s("dither=");
s.appendS32(paint.isDither());
s.append(" filter=");
s.appendS32(paint.getFilterQuality() != kNone_SkFilterQuality);
SkTextUtils::DrawString(canvas, s, x + W/2, y - p.getTextSize(), p,
SkTextUtils::DrawString(canvas, s.c_str(), x + W/2, y - font.getSize(), font, SkPaint(),
SkTextUtils::kCenter_Align);
}
if (k+j == 2) {
SkPaint p;
p.setAntiAlias(true);
p.setTextSize(SkIntToScalar(18));
SkString s;
s.append(" depth=");
s.appendS32(fBitmaps[i].colorType() == kRGB_565_SkColorType ? 16 : 32);
SkTextUtils::DrawString(canvas, s, x + W + SkIntToScalar(4), y + H/2, p);
SkTextUtils::DrawString(canvas, s.c_str(), x + W + SkIntToScalar(4), y + H/2, font, SkPaint());
}
}
}

View File

@ -503,11 +503,13 @@ protected:
paint.setStyle(SkPaint::kStroke_Style);
paint.setColor(button.fEnabled ? 0xFF3F0000 : 0x6F3F0000);
canvas->drawRect(button.fBounds, paint);
paint.setTextSize(25.0f);
paint.setColor(button.fEnabled ? 0xFF3F0000 : 0x6F3F0000);
paint.setStyle(SkPaint::kFill_Style);
SkTextUtils::DrawText(canvas, &button.fLabel, 1, button.fBounds.centerX(), button.fBounds.fBottom - 5,
paint, SkTextUtils::kCenter_Align);
SkFont font;
font.setSize(25.0f);
SkTextUtils::Draw(canvas, &button.fLabel, 1, kUTF8_SkTextEncoding,
button.fBounds.centerX(), button.fBounds.fBottom - 5,
font, paint, SkTextUtils::kCenter_Align);
}
void draw_control(SkCanvas* canvas, const SkRect& bounds, SkScalar value,

View File

@ -106,12 +106,11 @@ protected:
for (size_t ky = 0; ky < SK_ARRAY_COUNT(gModes); ky++) {
SkPaint p;
SkString str;
p.setAntiAlias(true);
p.setDither(true);
p.setLooper(fLooper);
str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
SkTextUtils::DrawString(textCanvas, str, x + r.width()/2, y, p,
SkTextUtils::DrawString(textCanvas, str.c_str(), x + r.width()/2, y, SkFont(), p,
SkTextUtils::kCenter_Align);
x += r.width() * 4 / 3;

View File

@ -52,10 +52,11 @@ public:
canvas->drawRoundRect(fRect, 8, 8, paint);
paint.setColor(0xFFFFFFFF);
paint.setTextSize(16);
paint.setLCDRenderText(true);
SkTextUtils::DrawString(canvas, fLabel, fRect.centerX(), fRect.fTop + 0.68f * fRect.height(),
paint, SkTextUtils::kCenter_Align);
SkFont font;
font.setSize(16);
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
SkTextUtils::DrawString(canvas, fLabel.c_str(), fRect.centerX(), fRect.fTop + 0.68f * fRect.height(),
font, paint, SkTextUtils::kCenter_Align);
}
bool hitTest(SkScalar x, SkScalar y) {

View File

@ -10,6 +10,7 @@
#include "SkBlurMask.h"
#include "SkCanvas.h"
#include "SkCornerPathEffect.h"
#include "SkFont.h"
#include "SkGradientShader.h"
#include "SkGraphics.h"
#include "SkPath.h"
@ -27,8 +28,8 @@
#include "SkColorPriv.h"
#include "SkBlurMaskFilter.h"
static void setNamedTypeface(SkPaint* paint, const char name[]) {
paint->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle()));
static void setNamedTypeface(SkFont* font, const char name[]) {
font->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle()));
}
static uint16_t gBG[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
@ -83,29 +84,6 @@ protected:
virtual void onDrawContent(SkCanvas* canvas) {
canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
if (false) {
SkPaint paint;
paint.setAntiAlias(true);
paint.setTextSize(50);
paint.setTypeface(SkTypeface::MakeFromName("Arial Unicode MS", SkFontStyle()));
char buffer[10];
size_t len = SkUTF::ToUTF8(0x8500, buffer);
canvas->drawText(buffer, len, 40, 40, paint);
return;
}
if (false) {
SkPaint paint;
paint.setAntiAlias(true);
SkRect r0 = { 0, 0, 10.5f, 20 };
SkRect r1 = { 10.5f, 10, 20, 30 };
paint.setColor(SK_ColorRED);
canvas->drawRect(r0, paint);
paint.setColor(SK_ColorBLUE);
canvas->drawRect(r1, paint);
return;
}
const SkBlendMode gModes[] = {
SkBlendMode::kClear,
SkBlendMode::kSrc,
@ -129,10 +107,9 @@ protected:
auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode, &m);
SkPaint labelP;
labelP.setAntiAlias(true);
labelP.setLCDRenderText(true);
setNamedTypeface(&labelP, "Menlo Regular");
SkFont font;
font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
setNamedTypeface(&font, "Menlo Regular");
const int W = 5;
@ -158,7 +135,7 @@ protected:
canvas->drawRect(r, p);
const char* label = SkBlendMode_Name(gModes[i]);
SkTextUtils::DrawString(canvas, label, x + w/2, y - labelP.getTextSize()/2, labelP,
SkTextUtils::DrawString(canvas, label, x + w/2, y - font.getSize()/2, font, SkPaint(),
SkTextUtils::kCenter_Align);
x += w + SkIntToScalar(10);
if ((i % W) == W - 1) {