Skip to content

Commit

Permalink
add drawString helper to canvas
Browse files Browse the repository at this point in the history
Many tests and examples use drawText with
a guess of how long the text is in bytes,
or a call to strlen(). Add a helper to
SkCanvas to simplify these examples.

Add another helper for SkString.

R=reed@google.com

Change-Id: I0204a31e938f065606f08ee7cd9a6b36db791ee2
Reviewed-on: https://skia-review.googlesource.com/13642
Commit-Queue: Cary Clark <caryclark@google.com>
Reviewed-by: Cary Clark <caryclark@google.com>
Reviewed-by: Mike Reed <reed@google.com>
Reviewed-by: Cary Clark <caryclark@skia.org>
  • Loading branch information
cary-clark authored and Skia Commit-Bot committed Apr 28, 2017
1 parent ec138b4 commit 2a475ea
Show file tree
Hide file tree
Showing 124 changed files with 367 additions and 370 deletions.
2 changes: 1 addition & 1 deletion bench/DisplacementBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DisplacementBaseBench : public Benchmark {
paint.setColor(0xFF884422);
paint.setTextSize(SkIntToScalar(96));
const char* str = "g";
canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(55), paint);
canvas.drawString(str, SkIntToScalar(15), SkIntToScalar(55), paint);
}

void makeCheckerboard() {
Expand Down
2 changes: 1 addition & 1 deletion bench/FontScalerBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FontScalerBench : public Benchmark {

for (int ps = 9; ps <= 24; ps += 2) {
paint.setTextSize(SkIntToScalar(ps));
canvas->drawText(fText.c_str(), fText.size(),
canvas->drawString(fText,
0, SkIntToScalar(20), paint);
}
}
Expand Down
2 changes: 1 addition & 1 deletion bench/MergeBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static sk_sp<SkImage> make_bitmap() {
paint.setColor(0xFF884422);
paint.setTextSize(SkIntToScalar(96));
const char* str = "g";
surface->getCanvas()->drawText(str, strlen(str), 15, 55, paint);
surface->getCanvas()->drawString(str, 15, 55, paint);
return surface->makeImageSnapshot();
}

Expand Down
4 changes: 2 additions & 2 deletions bench/ShaderMaskBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ class ShaderMaskBench : public Benchmark {
for (int i = 0; i < loops; i++) {
SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
canvas->drawString(fText, x, y, paint);
}

paint.setTextSize(SkIntToScalar(48));
for (int i = 0; i < loops / 4 ; i++) {
SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
canvas->drawString(fText, x, y, paint);
}
}

Expand Down
2 changes: 1 addition & 1 deletion bench/TextBench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class TextBench : public Benchmark {
} else {
SkScalar x = x0 + rand.nextUScalar1() * dim.fX;
SkScalar y = y0 + rand.nextUScalar1() * dim.fY;
canvas->drawText(fText.c_str(), fText.size(), x, y, paint);
canvas->drawString(fText, x, y, paint);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions fuzz/FilterFuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ static void make_g_bitmap(SkBitmap& bitmap) {
paint.setColor(0xFF884422);
paint.setTextSize(SkIntToScalar(kBitmapSize/2));
const char* str = "g";
canvas.drawText(str, strlen(str), SkIntToScalar(kBitmapSize/8),
canvas.drawString(str, SkIntToScalar(kBitmapSize/8),
SkIntToScalar(kBitmapSize/4), paint);
}

Expand Down Expand Up @@ -325,7 +325,7 @@ static void drawSomething(SkCanvas* canvas) {
canvas->drawCircle(SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/3), paint);
paint.setColor(SK_ColorBLACK);
paint.setTextSize(SkIntToScalar(kBitmapSize/3));
canvas->drawText("Picture", 7, SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
canvas->drawString("Picture", SkIntToScalar(kBitmapSize/2), SkIntToScalar(kBitmapSize/4), paint);
}

static sk_sp<SkColorFilter> make_color_filter() {
Expand Down
10 changes: 5 additions & 5 deletions gm/aaxfermodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ class AAXfermodesGM : public skiagm::GM {

if (kShape_Pass == drawingPass) {
fLabelPaint.setTextAlign(SkPaint::kCenter_Align);
canvas->drawText("Src Unknown", sizeof("Src Unknown") - 1,
canvas->drawString("Src Unknown",
kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2,
kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3, fLabelPaint);
canvas->drawText("Src Opaque", sizeof("Src Opaque") - 1,
canvas->drawString("Src Opaque",
kLabelSpacing + kShapeTypeSpacing * 1.5f + kShapeSpacing / 2 +
kPaintSpacing, kSubtitleSpacing / 2 + fLabelPaint.getTextSize() / 3,
fLabelPaint);
Expand Down Expand Up @@ -180,10 +180,10 @@ class AAXfermodesGM : public skiagm::GM {
titlePaint.setTextSize(9 * titlePaint.getTextSize() / 8);
titlePaint.setFakeBoldText(true);
titlePaint.setTextAlign(SkPaint::kCenter_Align);
canvas->drawText("Porter Duff", sizeof("Porter Duff") - 1,
canvas->drawString("Porter Duff",
kLabelSpacing + 4 * kShapeTypeSpacing,
kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);
canvas->drawText("Advanced", sizeof("Advanced") - 1,
canvas->drawString("Advanced",
kXfermodeTypeSpacing + kLabelSpacing + 4 * kShapeTypeSpacing,
kTitleSpacing / 2 + titlePaint.getTextSize() / 3, titlePaint);

Expand All @@ -194,7 +194,7 @@ class AAXfermodesGM : public skiagm::GM {
void drawModeName(SkCanvas* canvas, SkBlendMode mode) {
const char* modeName = SkBlendMode_Name(mode);
fLabelPaint.setTextAlign(SkPaint::kRight_Align);
canvas->drawText(modeName, strlen(modeName), kLabelSpacing - kShapeSize / 4,
canvas->drawString(modeName, kLabelSpacing - kShapeSize / 4,
fLabelPaint.getTextSize() / 4, fLabelPaint);
}

Expand Down
2 changes: 1 addition & 1 deletion gm/all_bitmap_configs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ static void draw(SkCanvas* canvas,
const char text[]) {
SkASSERT(src.colorType() == colorType);
canvas->drawBitmap(src, 0.0f, 0.0f);
canvas->drawText(text, strlen(text), 0.0f, 12.0f, p);
canvas->drawString(text, 0.0f, 12.0f, p);
}

DEF_SIMPLE_GM(all_bitmap_configs, canvas, SCALE, 6 * SCALE) {
Expand Down
2 changes: 1 addition & 1 deletion gm/animatedGif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace {
SkPaint paint;
SkRect bounds;
paint.measureText(errorText.c_str(), errorText.size(), &bounds);
canvas->drawText(errorText.c_str(), errorText.size(), kOffset, bounds.height() + kOffset,
canvas->drawString(errorText, kOffset, bounds.height() + kOffset,
paint);
}
}
Expand Down
4 changes: 2 additions & 2 deletions gm/arithmode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static void show_k_text(SkCanvas* canvas, SkScalar x, SkScalar y, const SkScalar
SkString str;
str.appendScalar(k[i]);
SkScalar width = paint.measureText(str.c_str(), str.size());
canvas->drawText(str.c_str(), str.size(), x, y + paint.getTextSize(), paint);
canvas->drawString(str, x, y + paint.getTextSize(), paint);
x += width + SkIntToScalar(10);
}
}
Expand Down Expand Up @@ -152,7 +152,7 @@ class ArithmodeGM : public skiagm::GM {
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
SkString str(enforcePMColor ? "enforcePM" : "no enforcePM");
canvas->drawText(str.c_str(), str.size(), 0, paint.getTextSize(), paint);
canvas->drawString(str, 0, paint.getTextSize(), paint);
}
canvas->translate(0, HH + 12);
}
Expand Down
2 changes: 1 addition & 1 deletion gm/bigtext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BigTextGM : public skiagm::GM {
};

paint.setColor(SK_ColorRED);
canvas->drawText("/", 1, pos.fX, pos.fY, paint);
canvas->drawString("/", pos.fX, pos.fY, paint);

paint.setColor(SK_ColorBLUE);
canvas->drawPosText("\\", 1, &pos, paint);
Expand Down
2 changes: 1 addition & 1 deletion gm/bitmapcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class BitmapCopyGM : public GM {
SkScalar textWidth = paint.measureText(name, strlen(name));
SkScalar x = (width - textWidth) / SkScalar(2);
SkScalar y = paint.getFontSpacing() / SkScalar(2);
canvas->drawText(name, strlen(name), x, y, paint);
canvas->drawString(name, x, y, paint);

// Draw destination bitmap
canvas->translate(0, vertOffset);
Expand Down
2 changes: 1 addition & 1 deletion gm/bitmapfilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
paint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&paint);
const char* name = sk_tool_utils::colortype_name(bm.colorType());
canvas->drawText(name, strlen(name), x, SkIntToScalar(bm.height())*scale*5/8,
canvas->drawString(name, x, SkIntToScalar(bm.height())*scale*5/8,
paint);
canvas->translate(SkIntToScalar(48), 0);

Expand Down
6 changes: 3 additions & 3 deletions gm/blurs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ DEF_SIMPLE_GM_BG(blurs, canvas, 700, 500, sk_tool_utils::color_to_565(0xFFDDDDDD
SkScalar x = SkIntToScalar(70);
SkScalar y = SkIntToScalar(400);
paint.setColor(SK_ColorBLACK);
canvas->drawText("Hamburgefons Style", 18, x, y, paint);
canvas->drawText("Hamburgefons Style", 18,
canvas->drawString("Hamburgefons Style", x, y, paint);
canvas->drawString("Hamburgefons Style",
x, y + SkIntToScalar(50), paint);
paint.setMaskFilter(nullptr);
paint.setColor(SK_ColorWHITE);
x -= SkIntToScalar(2);
y -= SkIntToScalar(2);
canvas->drawText("Hamburgefons Style", 18, x, y, paint);
canvas->drawString("Hamburgefons Style", x, y, paint);
}
canvas->restore();
flags = SkBlurMaskFilter::kHighQuality_BlurFlag;
Expand Down
2 changes: 1 addition & 1 deletion gm/bmpfilterqualityrepeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class BmpFilterQualityRepeat : public skiagm::GM {
bmpPaint.setShader(SkShader::MakeBitmapShader(fBmp, kTM, kTM, &lm));
bmpPaint.setFilterQuality(kQualities[q].fQuality);
canvas->drawRect(rect, bmpPaint);
canvas->drawText(kQualities[q].fName, strlen(kQualities[q].fName), 20, 40, textPaint);
canvas->drawString(kQualities[q].fName, 20, 40, textPaint);
canvas->translate(250, 0);
}

Expand Down
8 changes: 4 additions & 4 deletions gm/coloremoji.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ColorEmojiGM : public GM {
paint.setTextSize(textSize);
paint.getFontMetrics(&metrics);
y += -metrics.fAscent;
canvas->drawText(text, strlen(text), 10, y, paint);
canvas->drawString(text, 10, y, paint);
y += metrics.fDescent + metrics.fLeading;
}

Expand Down Expand Up @@ -124,7 +124,7 @@ class ColorEmojiGM : public GM {
shaderPaint.setTextSize(30);
shaderPaint.getFontMetrics(&metrics);
y += -metrics.fAscent;
canvas->drawText(text, strlen(text), 380, y, shaderPaint);
canvas->drawString(text, 380, y, shaderPaint);
y += metrics.fDescent + metrics.fLeading;
}
}
Expand Down Expand Up @@ -162,10 +162,10 @@ class ColorEmojiGM : public GM {
canvas->save();
canvas->drawRect(clipRect, clipHairline);
paint.setAlpha(0x20);
canvas->drawText(text, strlen(text), 0, 0, paint);
canvas->drawString(text, 0, 0, paint);
canvas->clipRect(clipRect);
paint.setAlpha(0xFF);
canvas->drawText(text, strlen(text), 0, 0, paint);
canvas->drawString(text, 0, 0, paint);
canvas->restore();
canvas->translate(0, SkIntToScalar(25));
}
Expand Down
2 changes: 1 addition & 1 deletion gm/coloremoji_blendmodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class ColorEmojiBlendModesGM : public GM {
}
#if 1
const char* label = SkBlendMode_Name(gModes[i]);
canvas->drawText(label, strlen(label), x + w/2, y - labelP.getTextSize()/2, labelP);
canvas->drawString(label, x + w/2, y - labelP.getTextSize()/2, labelP);
#endif
x += w + SkIntToScalar(10);
if ((i % W) == W - 1) {
Expand Down
14 changes: 7 additions & 7 deletions gm/colorwheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ DEF_SIMPLE_GM(colorwheelnative, canvas, 128, 28) {

canvas->clear(sk_tool_utils::color_to_565(SK_ColorLTGRAY));
paint.setColor(SK_ColorRED);
canvas->drawText("R", 1, 8.0f, 20.0f, paint);
canvas->drawString("R", 8.0f, 20.0f, paint);
paint.setColor(SK_ColorGREEN);
canvas->drawText("G", 1, 24.0f, 20.0f, paint);
canvas->drawString("G", 24.0f, 20.0f, paint);
paint.setColor(SK_ColorBLUE);
canvas->drawText("B", 1, 40.0f, 20.0f, paint);
canvas->drawString("B", 40.0f, 20.0f, paint);
paint.setColor(SK_ColorCYAN);
canvas->drawText("C", 1, 56.0f, 20.0f, paint);
canvas->drawString("C", 56.0f, 20.0f, paint);
paint.setColor(SK_ColorMAGENTA);
canvas->drawText("M", 1, 72.0f, 20.0f, paint);
canvas->drawString("M", 72.0f, 20.0f, paint);
paint.setColor(SK_ColorYELLOW);
canvas->drawText("Y", 1, 88.0f, 20.0f, paint);
canvas->drawString("Y", 88.0f, 20.0f, paint);
paint.setColor(SK_ColorBLACK);
canvas->drawText("K", 1, 104.0f, 20.0f, paint);
canvas->drawString("K", 104.0f, 20.0f, paint);
}
7 changes: 3 additions & 4 deletions gm/complexclip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,14 @@ class ComplexClipGM : public GM {
SkScalar txtX = SkIntToScalar(45);
paint.setColor(gClipAColor);
const char* aTxt = doInvA ? "InvA " : "A ";
canvas->drawText(aTxt, strlen(aTxt), txtX, SkIntToScalar(220), paint);
canvas->drawString(aTxt, txtX, SkIntToScalar(220), paint);
txtX += paint.measureText(aTxt, strlen(aTxt));
paint.setColor(SK_ColorBLACK);
canvas->drawText(gOps[op].fName, strlen(gOps[op].fName),
txtX, SkIntToScalar(220), paint);
canvas->drawString(gOps[op].fName, txtX, SkIntToScalar(220), paint);
txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName));
paint.setColor(gClipBColor);
const char* bTxt = doInvB ? "InvB " : "B ";
canvas->drawText(bTxt, strlen(bTxt), txtX, SkIntToScalar(220), paint);
canvas->drawString(bTxt, txtX, SkIntToScalar(220), paint);

canvas->translate(SkIntToScalar(250),0);
}
Expand Down
3 changes: 1 addition & 2 deletions gm/complexclip3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ class ComplexClip3GM : public GM {
doAAB ? "A" : "B",
doInvB ? "I" : "N");

canvas->drawText(str.c_str(), strlen(str.c_str()), txtX, SkIntToScalar(130),
paint);
canvas->drawString(str.c_str(), txtX, SkIntToScalar(130), paint);
if (doInvB) {
canvas->translate(SkIntToScalar(150),0);
} else {
Expand Down
4 changes: 2 additions & 2 deletions gm/constcolorprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class ConstColorProcessor : public GM {
// get the bounds of the text in order to position it
labelPaint.measureText(inputLabel.c_str(), inputLabel.size(),
&inputLabelBounds);
canvas->drawText(inputLabel.c_str(), inputLabel.size(),
canvas->drawString(inputLabel,
renderRect.fRight + kPad,
-inputLabelBounds.fTop, labelPaint);
// update the bounds to reflect the offset we used to draw it.
Expand All @@ -144,7 +144,7 @@ class ConstColorProcessor : public GM {
SkRect procLabelBounds;
labelPaint.measureText(procLabel.c_str(), procLabel.size(),
&procLabelBounds);
canvas->drawText(procLabel.c_str(), procLabel.size(),
canvas->drawString(procLabel,
renderRect.fRight + kPad,
inputLabelBounds.fBottom + 2.f - procLabelBounds.fTop,
labelPaint);
Expand Down
58 changes: 26 additions & 32 deletions gm/cubicpaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ class CubicPathGM : public skiagm::GM {
titlePaint.setTextSize(15 * SK_Scalar1);
const char title[] = "Cubic Drawn Into Rectangle Clips With "
"Indicated Style, Fill and Linecaps, with stroke width 10";
canvas->drawText(title, strlen(title),
20 * SK_Scalar1,
20 * SK_Scalar1,
titlePaint);
canvas->drawString(title,
20 * SK_Scalar1,
20 * SK_Scalar1,
titlePaint);

SkRandom rand;
SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
Expand Down Expand Up @@ -244,18 +244,15 @@ class CubicPathGM : public skiagm::GM {
labelPaint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&labelPaint);
labelPaint.setTextSize(10 * SK_Scalar1);
canvas->drawText(gStyles[style].fName,
strlen(gStyles[style].fName),
0, rect.height() + 12 * SK_Scalar1,
labelPaint);
canvas->drawText(gFills[fill].fName,
strlen(gFills[fill].fName),
0, rect.height() + 24 * SK_Scalar1,
labelPaint);
canvas->drawText(gCaps[cap].fName,
strlen(gCaps[cap].fName),
0, rect.height() + 36 * SK_Scalar1,
labelPaint);
canvas->drawString(gStyles[style].fName,
0, rect.height() + 12 * SK_Scalar1,
labelPaint);
canvas->drawString(gFills[fill].fName,
0, rect.height() + 24 * SK_Scalar1,
labelPaint);
canvas->drawString(gCaps[cap].fName,
0, rect.height() + 36 * SK_Scalar1,
labelPaint);
}
canvas->restore();
}
Expand Down Expand Up @@ -347,10 +344,10 @@ class CubicClosePathGM : public skiagm::GM {
titlePaint.setTextSize(15 * SK_Scalar1);
const char title[] = "Cubic Closed Drawn Into Rectangle Clips With "
"Indicated Style, Fill and Linecaps, with stroke width 10";
canvas->drawText(title, strlen(title),
20 * SK_Scalar1,
20 * SK_Scalar1,
titlePaint);
canvas->drawString(title,
20 * SK_Scalar1,
20 * SK_Scalar1,
titlePaint);

SkRandom rand;
SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
Expand Down Expand Up @@ -389,18 +386,15 @@ class CubicClosePathGM : public skiagm::GM {
labelPaint.setAntiAlias(true);
sk_tool_utils::set_portable_typeface(&labelPaint);
labelPaint.setTextSize(10 * SK_Scalar1);
canvas->drawText(gStyles[style].fName,
strlen(gStyles[style].fName),
0, rect.height() + 12 * SK_Scalar1,
labelPaint);
canvas->drawText(gFills[fill].fName,
strlen(gFills[fill].fName),
0, rect.height() + 24 * SK_Scalar1,
labelPaint);
canvas->drawText(gCaps[cap].fName,
strlen(gCaps[cap].fName),
0, rect.height() + 36 * SK_Scalar1,
labelPaint);
canvas->drawString(gStyles[style].fName,
0, rect.height() + 12 * SK_Scalar1,
labelPaint);
canvas->drawString(gFills[fill].fName,
0, rect.height() + 24 * SK_Scalar1,
labelPaint);
canvas->drawString(gCaps[cap].fName,
0, rect.height() + 36 * SK_Scalar1,
labelPaint);
}
canvas->restore();
}
Expand Down
Loading

0 comments on commit 2a475ea

Please sign in to comment.