add drawString to ease the pain of converting to skfont

Bug: skia:
Change-Id: I88418f87883b66204427fbdab471e74c78835ac4
Reviewed-on: https://skia-review.googlesource.com/c/181407
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-05 16:35:13 -05:00 committed by Skia Commit-Bot
parent 82ebe04caf
commit 4de2f1fead
10 changed files with 96 additions and 81 deletions

View File

@ -149,12 +149,9 @@ protected:
canvas->translate(gap, 0);
// Label
SkPaint paint;
paint.setTextSize(SkIntToScalar(24));
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
SkFont font(sk_tool_utils::create_portable_typeface(), 24);
SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
canvas->drawString(str, 0, paint.getTextSize(), paint);
canvas->drawString(str, 0, font.getSize(), font, SkPaint());
}
canvas->translate(0, HH + 12);
}

View File

@ -44,14 +44,15 @@ static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
SkAutoCanvasRestore acr(canvas, true);
SkPaint paint;
paint.setAntiAlias(true);
SkScalar x = 0;
const int scale = 32;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
SkFont font(sk_tool_utils::create_portable_typeface());
const char* name = sk_tool_utils::colortype_name(bm.colorType());
canvas->drawString(name, x, SkIntToScalar(bm.height())*scale*5/8,
paint);
font, paint);
canvas->translate(SkIntToScalar(48), 0);
canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));

View File

@ -16,15 +16,12 @@ DEF_SIMPLE_GM(blurimagevmask, canvas, 700, 1200) {
paint.setAntiAlias(true);
paint.setColor(SK_ColorBLACK);
SkPaint textPaint;
textPaint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&textPaint);
textPaint.setTextSize(SkIntToScalar(25));
SkFont font(sk_tool_utils::create_portable_typeface(), 25);
const double sigmas[] = {3.0, 8.0, 16.0, 24.0, 32.0};
canvas->drawString("mask blur", 285, 50, textPaint);
canvas->drawString("image blur", 285 + 250, 50, textPaint);
canvas->drawString("mask blur", 285, 50, font, paint);
canvas->drawString("image blur", 285 + 250, 50, font, paint);
SkRect r = {35, 100, 135, 200};
@ -34,7 +31,7 @@ DEF_SIMPLE_GM(blurimagevmask, canvas, 700, 1200) {
char out[100];
sprintf(out, "Sigma: %g", sigma);
canvas->drawString(out, r.left(), r.bottom() + 35, textPaint);
canvas->drawString(out, r.left(), r.bottom() + 35, font, paint);
r.offset(250, 0);

View File

@ -14,53 +14,53 @@
#include "SkPath.h"
DEF_SIMPLE_GM_BG(blurs, canvas, 700, 500, 0xFFDDDDDD) {
SkBlurStyle NONE = SkBlurStyle(-999);
const struct {
SkBlurStyle fStyle;
int fCx, fCy;
} gRecs[] = {
{ NONE, 0, 0 },
{ kInner_SkBlurStyle, -1, 0 },
{ kNormal_SkBlurStyle, 0, 1 },
{ kSolid_SkBlurStyle, 0, -1 },
{ kOuter_SkBlurStyle, 1, 0 },
};
SkBlurStyle NONE = SkBlurStyle(-999);
const struct {
SkBlurStyle fStyle;
int fCx, fCy;
} gRecs[] = {
{ NONE, 0, 0 },
{ kInner_SkBlurStyle, -1, 0 },
{ kNormal_SkBlurStyle, 0, 1 },
{ kSolid_SkBlurStyle, 0, -1 },
{ kOuter_SkBlurStyle, 1, 0 },
};
SkPaint paint;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setTextSize(SkIntToScalar(25));
canvas->translate(SkIntToScalar(-40), SkIntToScalar(0));
SkPaint paint;
paint.setAntiAlias(true);
paint.setColor(SK_ColorBLUE);
paint.setColor(SK_ColorBLUE);
for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
if (gRecs[i].fStyle != NONE) {
paint.setMaskFilter(SkMaskFilter::MakeBlur(gRecs[i].fStyle,
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20))));
} else {
paint.setMaskFilter(nullptr);
}
canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100),
SkIntToScalar(200 + gRecs[i].fCy*100),
SkIntToScalar(50),
paint);
}
// draw text
{
paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4))));
SkScalar x = SkIntToScalar(70);
SkScalar y = SkIntToScalar(400);
paint.setColor(SK_ColorBLACK);
canvas->drawString("Hamburgefons Style", x, y, paint);
canvas->drawString("Hamburgefons Style",
x, y + SkIntToScalar(50), paint);
canvas->translate(SkIntToScalar(-40), SkIntToScalar(0));
for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
if (gRecs[i].fStyle != NONE) {
paint.setMaskFilter(SkMaskFilter::MakeBlur(gRecs[i].fStyle,
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20))));
} else {
paint.setMaskFilter(nullptr);
paint.setColor(SK_ColorWHITE);
x -= SkIntToScalar(2);
y -= SkIntToScalar(2);
canvas->drawString("Hamburgefons Style", x, y, paint);
}
canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100),
SkIntToScalar(200 + gRecs[i].fCy*100),
SkIntToScalar(50),
paint);
}
// draw text
{
SkFont font(sk_tool_utils::create_portable_typeface(), 25);
paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4))));
SkScalar x = SkIntToScalar(70);
SkScalar y = SkIntToScalar(400);
paint.setColor(SK_ColorBLACK);
canvas->drawString("Hamburgefons Style", x, y, font, paint);
canvas->drawString("Hamburgefons Style",
x, y + SkIntToScalar(50), font, paint);
paint.setMaskFilter(nullptr);
paint.setColor(SK_ColorWHITE);
x -= SkIntToScalar(2);
y -= SkIntToScalar(2);
canvas->drawString("Hamburgefons Style", x, y, font, paint);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -64,11 +64,12 @@ private:
lm.setTranslateY(330);
SkPaint textPaint;
sk_tool_utils::set_portable_typeface(&textPaint);
textPaint.setAntiAlias(true);
SkPaint bmpPaint(textPaint);
SkFont font(sk_tool_utils::create_portable_typeface());
SkAutoCanvasRestore acr(canvas, true);
for (size_t q = 0; q < SK_ARRAY_COUNT(kQualities); ++q) {
@ -76,7 +77,7 @@ private:
bmpPaint.setShader(SkShader::MakeBitmapShader(fBmp, kTM, kTM, &lm));
bmpPaint.setFilterQuality(kQualities[q].fQuality);
canvas->drawRect(rect, bmpPaint);
canvas->drawString(kQualities[q].fName, 20, 40, textPaint);
canvas->drawString(kQualities[q].fName, 20, 40, font, textPaint);
canvas->translate(250, 0);
}

View File

@ -40,22 +40,22 @@ DEF_SIMPLE_GM(colorwheel, canvas, 256, 256) {
DEF_SIMPLE_GM(colorwheelnative, canvas, 128, 28) {
SkPaint paint;
sk_tool_utils::set_portable_typeface(&paint, "sans-serif", SkFontStyle::Bold());
paint.setTextSize(18.0f);
SkFont font(sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold()), 18);
font.setEdging(SkFont::Edging::kAlias);
canvas->clear(SK_ColorLTGRAY);
paint.setColor(SK_ColorRED);
canvas->drawString("R", 8.0f, 20.0f, paint);
canvas->drawString("R", 8.0f, 20.0f, font, paint);
paint.setColor(SK_ColorGREEN);
canvas->drawString("G", 24.0f, 20.0f, paint);
canvas->drawString("G", 24.0f, 20.0f, font, paint);
paint.setColor(SK_ColorBLUE);
canvas->drawString("B", 40.0f, 20.0f, paint);
canvas->drawString("B", 40.0f, 20.0f, font, paint);
paint.setColor(SK_ColorCYAN);
canvas->drawString("C", 56.0f, 20.0f, paint);
canvas->drawString("C", 56.0f, 20.0f, font, paint);
paint.setColor(SK_ColorMAGENTA);
canvas->drawString("M", 72.0f, 20.0f, paint);
canvas->drawString("M", 72.0f, 20.0f, font, paint);
paint.setColor(SK_ColorYELLOW);
canvas->drawString("Y", 88.0f, 20.0f, paint);
canvas->drawString("Y", 88.0f, 20.0f, font, paint);
paint.setColor(SK_ColorBLACK);
canvas->drawString("K", 104.0f, 20.0f, paint);
canvas->drawString("K", 104.0f, 20.0f, font, paint);
}

View File

@ -52,8 +52,8 @@ protected:
SkPaint paint;
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setTextSize(SkIntToScalar(20));
SkFont font(sk_tool_utils::create_portable_typeface(), 20);
constexpr struct {
SkClipOp fOp;
@ -106,7 +106,7 @@ protected:
doAAB ? "A" : "B",
doInvB ? "I" : "N");
canvas->drawString(str.c_str(), txtX, SkIntToScalar(130), paint);
canvas->drawString(str.c_str(), txtX, SkIntToScalar(130), font, paint);
if (doInvB) {
canvas->translate(SkIntToScalar(150),0);
} else {

View File

@ -57,18 +57,19 @@ static SkBitmap make_bmp(int w, int h) {
mat.postScale(SK_Scalar1 / 3, SK_Scalar1 / 3);
}
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
paint.setTextSize(wScalar / 2.2f);
SkFont font(sk_tool_utils::create_portable_typeface(), wScalar / 2.2f);
paint.setShader(nullptr);
paint.setColor(SK_ColorLTGRAY);
constexpr char kTxt[] = "Skia";
SkPoint texPos = { wScalar / 17, hScalar / 2 + paint.getTextSize() / 2.5f };
canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
SkPoint texPos = { wScalar / 17, hScalar / 2 + font.getSize() / 2.5f };
canvas.drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding,
texPos.fX, texPos.fY, font, paint);
paint.setColor(SK_ColorBLACK);
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(SK_Scalar1);
canvas.drawText(kTxt, SK_ARRAY_COUNT(kTxt)-1, texPos.fX, texPos.fY, paint);
canvas.drawSimpleText(kTxt, SK_ARRAY_COUNT(kTxt)-1, kUTF8_SkTextEncoding,
texPos.fX, texPos.fY, font, paint);
return bmp;
}

View File

@ -560,12 +560,13 @@ DEF_SIMPLE_GM(dashtextcaps, canvas, 512, 512) {
p.setStrokeWidth(10);
p.setStrokeCap(SkPaint::kRound_Cap);
p.setStrokeJoin(SkPaint::kRound_Join);
p.setTextSize(100);
p.setARGB(0xff, 0xbb, 0x00, 0x00);
sk_tool_utils::set_portable_typeface(&p);
SkFont font(sk_tool_utils::create_portable_typeface(), 100);
const SkScalar intervals[] = { 12, 12 };
p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
canvas->drawString("Sausages", 10, 90, p);
canvas->drawString("Sausages", 10, 90, font, p);
canvas->drawLine(8, 120, 456, 120, p);
}

View File

@ -27,6 +27,10 @@
#include "SkSurfaceProps.h"
#include "SkVertices.h"
#ifndef SK_SUPPORT_LEGACY_DRAWSTRING
#define SK_SUPPORT_LEGACY_DRAWSTRING
#endif
class GrContext;
class GrRenderTargetContext;
class SkAndroidFrameworkUtils;
@ -1870,6 +1874,7 @@ public:
void drawSimpleText(const void* text, size_t byteLength, SkTextEncoding encoding,
SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint);
#ifdef SK_SUPPORT_LEGACY_DRAWSTRING
/** Draws null terminated string, with origin at (x, y), using clip, SkMatrix, and
SkPaint paint.
@ -1920,6 +1925,18 @@ public:
@param paint text size, blend, color, and so on, used to draw
*/
void drawString(const SkString& string, SkScalar x, SkScalar y, const SkPaint& paint);
#endif
// Experimental
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont& font,
const SkPaint& paint) {
this->drawSimpleText(str, strlen(str), kUTF8_SkTextEncoding, x, y, font, paint);
}
// Experimental
void drawString(const SkString& str, SkScalar x, SkScalar y, const SkFont& font,
const SkPaint& paint) {
this->drawSimpleText(str.c_str(), str.size(), kUTF8_SkTextEncoding, x, y, font, paint);
}
/** Draws SkTextBlob blob at (x, y), using clip, SkMatrix, and SkPaint paint.