aboutsummaryrefslogtreecommitdiff
path: root/samplecode
diff options
context:
space:
mode:
authorMike Reed <reed@google.com>2019-01-02 12:21:01 -0500
committerSkia Commit-Bot <skia-commit-bot@chromium.org>2019-01-02 17:45:30 +0000
commit919191365b3a41cc817f3c3a82782d5e9df0b72e (patch)
tree965d36db85cc632487609c2ea51789a8541ddcbb /samplecode
parentc11bf8ce0b34cf89fae9ef4a2afee5c57ced025a (diff)
downloadskqp-919191365b3a41cc817f3c3a82782d5e9df0b72e.tar.gz
use font instead of paint for text
Bug: skia: Change-Id: I508ecbd6c4dad41f67f5f2ce6da6c0065dacba6c Reviewed-on: https://skia-review.googlesource.com/c/180365 Reviewed-by: Mike Reed <reed@google.com> Commit-Queue: Mike Reed <reed@google.com> Auto-Submit: Mike Reed <reed@google.com>
Diffstat (limited to 'samplecode')
-rw-r--r--samplecode/SampleAnimatedImage.cpp11
-rw-r--r--samplecode/SampleBitmapRect.cpp10
-rw-r--r--samplecode/SampleComplexClip.cpp16
-rw-r--r--samplecode/SampleGlyphTransform.cpp8
-rw-r--r--samplecode/SampleLCD.cpp15
-rw-r--r--samplecode/SampleMeasure.cpp104
-rw-r--r--samplecode/SamplePolyToPoly.cpp18
-rw-r--r--samplecode/SampleRegion.cpp21
-rw-r--r--samplecode/SampleTextAlpha.cpp89
-rw-r--r--samplecode/SampleUnpremul.cpp14
10 files changed, 62 insertions, 244 deletions
diff --git a/samplecode/SampleAnimatedImage.cpp b/samplecode/SampleAnimatedImage.cpp
index 0d9adad81e..8f739351d4 100644
--- a/samplecode/SampleAnimatedImage.cpp
+++ b/samplecode/SampleAnimatedImage.cpp
@@ -9,6 +9,7 @@
#include "SkAnimatedImage.h"
#include "SkAnimTimer.h"
#include "SkCanvas.h"
+#include "SkFont.h"
#include "SkPaint.h"
#include "SkPictureRecorder.h"
#include "SkRect.h"
@@ -30,19 +31,17 @@ public:
protected:
void onDrawBackground(SkCanvas* canvas) override {
- SkPaint paint;
- paint.setAntiAlias(true);
- constexpr SkScalar kTextSize = 20;
- paint.setTextSize(kTextSize);
+ SkFont font;
+ font.setSize(20);
SkString str = SkStringPrintf("Press '%c' to start/pause; '%c' to reset.",
kPauseKey, kResetKey);
const char* text = str.c_str();
SkRect bounds;
- paint.measureText(text, strlen(text), &bounds);
+ font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
fYOffset = bounds.height();
- canvas->drawText(text, strlen(text), 5, fYOffset, paint);
+ canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, 5, fYOffset, font, SkPaint());
fYOffset *= 2;
}
diff --git a/samplecode/SampleBitmapRect.cpp b/samplecode/SampleBitmapRect.cpp
index 61f33618a3..9dcf98c3cb 100644
--- a/samplecode/SampleBitmapRect.cpp
+++ b/samplecode/SampleBitmapRect.cpp
@@ -9,6 +9,7 @@
#include "SkAnimTimer.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
+#include "SkFont.h"
#include "SkGradientShader.h"
#include "SkGraphics.h"
#include "SkPath.h"
@@ -160,18 +161,17 @@ static void make_big_bitmap(SkBitmap* bm) {
const int BIG_H = 120;
- SkPaint paint;
- paint.setAntiAlias(true);
- paint.setTextSize(SkIntToScalar(BIG_H));
+ SkFont font;
+ font.setSize(SkIntToScalar(BIG_H));
- const int BIG_W = SkScalarRoundToInt(paint.measureText(gText, strlen(gText)));
+ const int BIG_W = SkScalarRoundToInt(font.measureText(gText, strlen(gText), kUTF8_SkTextEncoding));
bm->allocN32Pixels(BIG_W, BIG_H);
bm->eraseColor(SK_ColorWHITE);
SkCanvas canvas(*bm);
- canvas.drawString(gText, 0, paint.getTextSize()*4/5, paint);
+ canvas.drawSimpleText(gText, strlen(gText), kUTF8_SkTextEncoding, 0, font.getSize()*4/5, font, SkPaint());
}
class BitmapRectView2 : public Sample {
diff --git a/samplecode/SampleComplexClip.cpp b/samplecode/SampleComplexClip.cpp
index fea5dd1da0..897b0455ec 100644
--- a/samplecode/SampleComplexClip.cpp
+++ b/samplecode/SampleComplexClip.cpp
@@ -7,6 +7,7 @@
#include "Sample.h"
#include "SkCanvas.h"
+#include "SkFont.h"
#include "SkPath.h"
#include "SkClipOpPriv.h"
@@ -65,6 +66,9 @@ protected:
clipB.close();
SkColor colorB = SK_ColorRED;
+ SkFont font;
+ font.setSize(20);
+
SkPaint paint;
paint.setAntiAlias(true);
@@ -122,19 +126,17 @@ protected:
paint.setColor(colorB);
canvas->drawPath(clipB, paint);
- paint.setTextSize(SkIntToScalar(20));
-
SkScalar txtX = SkIntToScalar(55);
paint.setColor(colorA);
const char* aTxt = invA ? "InverseA " : "A ";
canvas->drawString(aTxt, txtX, SkIntToScalar(220), paint);
- txtX += paint.measureText(aTxt, strlen(aTxt));
+ txtX += font.measureText(aTxt, strlen(aTxt), kUTF8_SkTextEncoding);
paint.setColor(SK_ColorBLACK);
- canvas->drawString(gOps[op].fName,
- txtX, SkIntToScalar(220), paint);
- txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName));
+ canvas->drawSimpleText(gOps[op].fName, strlen(gOps[op].fName), kUTF8_SkTextEncoding,
+ txtX, 220, font, paint);
+ txtX += font.measureText(gOps[op].fName, strlen(gOps[op].fName), kUTF8_SkTextEncoding);
paint.setColor(colorB);
- canvas->drawString("B", txtX, SkIntToScalar(220), paint);
+ canvas->drawSimpleText("B", 1, kUTF8_SkTextEncoding, txtX, 220, font, paint);
canvas->translate(SkIntToScalar(250),0);
}
diff --git a/samplecode/SampleGlyphTransform.cpp b/samplecode/SampleGlyphTransform.cpp
index e8567d1ec9..ff42251723 100644
--- a/samplecode/SampleGlyphTransform.cpp
+++ b/samplecode/SampleGlyphTransform.cpp
@@ -40,7 +40,8 @@ protected:
void onDrawContent(SkCanvas* canvas) override {
SkPaint paint;
- paint.setTypeface(fEmojiFont.fTypeface);
+
+ SkFont font(fEmojiFont.fTypeface);
const char* text = fEmojiFont.fText;
double baseline = this->height() / 2;
@@ -54,8 +55,9 @@ protected:
// d3 by default anchors text around the middle
SkRect bounds;
- paint.measureText(text, strlen(text), &bounds);
- canvas->drawString(text, -bounds.centerX(), -bounds.centerY(), paint);
+ font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds);
+ canvas->drawSimpleText(text, strlen(text), kUTF8_SkTextEncoding, -bounds.centerX(), -bounds.centerY(),
+ font, paint);
}
bool onAnimate(const SkAnimTimer& timer) override {
diff --git a/samplecode/SampleLCD.cpp b/samplecode/SampleLCD.cpp
index 22aaba6262..f209e97089 100644
--- a/samplecode/SampleLCD.cpp
+++ b/samplecode/SampleLCD.cpp
@@ -4,6 +4,7 @@
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
+#include <SkFont.h>
#include "Sample.h"
#include "SkCanvas.h"
#include "SkPaint.h"
@@ -30,7 +31,6 @@ protected:
this->drawBG(canvas);
SkPaint paint;
- paint.setAntiAlias(true);
SkScalar textSize = SkIntToScalar(6);
SkScalar delta = SK_Scalar1;
@@ -40,16 +40,17 @@ protected:
SkScalar x1 = SkIntToScalar(310);
SkScalar y = SkIntToScalar(20);
+ SkFont font;
for (int i = 0; i < 20; i++) {
- paint.setTextSize(textSize);
+ font.setSize(textSize);
textSize += delta;
- paint.setLCDRenderText(false);
- canvas->drawText(text, len, x0, y, paint);
- paint.setLCDRenderText(true);
- canvas->drawText(text, len, x1, y, paint);
+ font.setEdging(SkFont::Edging::kAntiAlias);
+ canvas->drawSimpleText(text, len, kUTF8_SkTextEncoding, x0, y, font, paint);
+ font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
+ canvas->drawSimpleText(text, len, kUTF8_SkTextEncoding, x1, y, font, paint);
- y += paint.getFontSpacing();
+ y += font.getSpacing();
}
}
diff --git a/samplecode/SampleMeasure.cpp b/samplecode/SampleMeasure.cpp
deleted file mode 100644
index 2a4817e418..0000000000
--- a/samplecode/SampleMeasure.cpp
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * Copyright 2011 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-#include "Sample.h"
-#include "SkCanvas.h"
-#include "SkGradientShader.h"
-#include "SkPath.h"
-#include "SkRegion.h"
-#include "SkShader.h"
-#include "SkUTF.h"
-#include "Sk1DPathEffect.h"
-#include "SkCornerPathEffect.h"
-#include "SkPathMeasure.h"
-#include "SkRandom.h"
-#include "SkColorPriv.h"
-#include "SkColorFilter.h"
-
-// exercise scale/linear
-struct Setting {
- bool fLinearText;
-};
-
-static const Setting gSettings[] = {
- { false },
- { true },
-};
-
-static void doMeasure(SkCanvas* canvas, const SkPaint& paint, const char text[]) {
- SkScalar dy = paint.getFontMetrics(nullptr);
-
- size_t len = strlen(text);
- SkAutoTMalloc<SkScalar> autoWidths(len);
- SkScalar* widths = autoWidths.get();
- SkAutoTMalloc<SkRect> autoRects(len);
- SkRect* rects = autoRects.get();
- SkRect bounds;
-
- SkPaint p(paint);
- for (size_t i = 0; i < SK_ARRAY_COUNT(gSettings); i++) {
- p.setLinearText(gSettings[i].fLinearText);
-
- int n = p.getTextWidths(text, len, widths, rects);
- SkScalar w = p.measureText(text, len, &bounds);
-
- p.setStyle(SkPaint::kFill_Style);
- p.setColor(0x8888FF88);
- canvas->drawRect(bounds, p);
- p.setColor(0xFF000000);
- canvas->drawText(text, len, 0, 0, p);
-
- p.setStyle(SkPaint::kStroke_Style);
- p.setStrokeWidth(0);
- p.setColor(0xFFFF0000);
- SkScalar x = 0;
- for (int j = 0; j < n; j++) {
- SkRect r = rects[j];
- r.offset(x, 0);
- canvas->drawRect(r, p);
- x += widths[j];
- }
-
- p.setColor(0xFF0000FF);
- canvas->drawLine(0, 0, w, 0, p);
- p.setStrokeWidth(SkIntToScalar(4));
- canvas->drawPoint(x, 0, p);
-
- canvas->translate(0, dy);
- }
-}
-
-class MeasureView : public Sample {
-public:
- SkPaint fPaint;
-
- MeasureView() {
- fPaint.setAntiAlias(true);
- fPaint.setTextSize(SkIntToScalar(64));
- this->setBGColor(0xFFDDDDDD);
- }
-
-protected:
- virtual bool onQuery(Sample::Event* evt) {
- if (Sample::TitleQ(*evt)) {
- Sample::TitleR(evt, "Measure");
- return true;
- }
- return this->INHERITED::onQuery(evt);
- }
-
- virtual void onDrawContent(SkCanvas* canvas) {
- canvas->translate(fPaint.getTextSize(), fPaint.getTextSize());
- doMeasure(canvas, fPaint, "Hamburgefons");
- }
-
-private:
- typedef Sample INHERITED;
-};
-
-//////////////////////////////////////////////////////////////////////////////
-
-DEF_SAMPLE( return new MeasureView(); )
diff --git a/samplecode/SamplePolyToPoly.cpp b/samplecode/SamplePolyToPoly.cpp
index 24c8b80032..0e25e3c53b 100644
--- a/samplecode/SamplePolyToPoly.cpp
+++ b/samplecode/SamplePolyToPoly.cpp
@@ -75,7 +75,7 @@ protected:
return this->INHERITED::onQuery(evt);
}
- static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[],
+ static void doDraw(SkCanvas* canvas, SkPaint* paint, const SkFont& font, const int isrc[],
const int idst[], int count) {
SkMatrix matrix;
SkPoint src[4], dst[4];
@@ -97,14 +97,14 @@ protected:
canvas->drawLine(0, D, D, 0, *paint);
SkFontMetrics fm;
- paint->getFontMetrics(&fm);
+ font.getMetrics(&fm);
paint->setColor(SK_ColorRED);
paint->setStyle(SkPaint::kFill_Style);
SkScalar x = D/2;
float y = D/2 - (fm.fAscent + fm.fDescent)/2;
SkString str;
str.appendS32(count);
- SkTextUtils::DrawString(canvas, str, x, y, *paint, SkTextUtils::kCenter_Align);
+ SkTextUtils::DrawString(canvas, str.c_str(), x, y, font, *paint, SkTextUtils::kCenter_Align);
canvas->restore();
}
@@ -113,14 +113,16 @@ protected:
SkPaint paint;
paint.setAntiAlias(true);
paint.setStrokeWidth(SkIntToScalar(4));
- paint.setTextSize(SkIntToScalar(40));
+
+ SkFont font;
+ font.setSize(40);
canvas->save();
canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
// translate (1 point)
const int src1[] = { 0, 0 };
const int dst1[] = { 5, 5 };
- doDraw(canvas, &paint, src1, dst1, 1);
+ doDraw(canvas, &paint, font, src1, dst1, 1);
canvas->restore();
canvas->save();
@@ -128,7 +130,7 @@ protected:
// rotate/uniform-scale (2 points)
const int src2[] = { 32, 32, 64, 32 };
const int dst2[] = { 32, 32, 64, 48 };
- doDraw(canvas, &paint, src2, dst2, 2);
+ doDraw(canvas, &paint, font, src2, dst2, 2);
canvas->restore();
canvas->save();
@@ -136,7 +138,7 @@ protected:
// rotate/skew (3 points)
const int src3[] = { 0, 0, 64, 0, 0, 64 };
const int dst3[] = { 0, 0, 96, 0, 24, 64 };
- doDraw(canvas, &paint, src3, dst3, 3);
+ doDraw(canvas, &paint, font, src3, dst3, 3);
canvas->restore();
canvas->save();
@@ -144,7 +146,7 @@ protected:
// perspective (4 points)
const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 };
const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 };
- doDraw(canvas, &paint, src4, dst4, 4);
+ doDraw(canvas, &paint, font, src4, dst4, 4);
canvas->restore();
}
diff --git a/samplecode/SampleRegion.cpp b/samplecode/SampleRegion.cpp
index a9526face8..409222e730 100644
--- a/samplecode/SampleRegion.cpp
+++ b/samplecode/SampleRegion.cpp
@@ -8,6 +8,7 @@
#include "Sample.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
+#include "SkFont.h"
#include "SkGradientShader.h"
#include "SkPath.h"
#include "SkRegion.h"
@@ -53,20 +54,20 @@ static void test_strokerect(SkCanvas* canvas) {
static void drawFadingText(SkCanvas* canvas,
const char* text, size_t len, SkScalar x, SkScalar y,
- const SkPaint& paint) {
+ const SkFont& font, const SkPaint& paint) {
// Need a bounds for the text
SkRect bounds;
SkFontMetrics fm;
- paint.getFontMetrics(&fm);
- bounds.set(x, y + fm.fTop, x + paint.measureText(text, len), y + fm.fBottom);
+ font.getMetrics(&fm);
+ bounds.set(x, y + fm.fTop, x + font.measureText(text, len, kUTF8_SkTextEncoding), y + fm.fBottom);
// may need to outset bounds a little, to account for hinting and/or
// antialiasing
bounds.inset(-SkIntToScalar(2), -SkIntToScalar(2));
canvas->saveLayer(&bounds, nullptr);
- canvas->drawText(text, len, x, y, paint);
+ canvas->drawSimpleText(text, len, kUTF8_SkTextEncoding, x, y, font, paint);
const SkPoint pts[] = {
{ bounds.fLeft, y },
@@ -89,28 +90,30 @@ static void drawFadingText(SkCanvas* canvas,
static void test_text(SkCanvas* canvas) {
SkPaint paint;
paint.setAntiAlias(true);
- paint.setTextSize(20);
+
+ SkFont font;
+ font.setSize(20);
const char* str = "Hamburgefons";
size_t len = strlen(str);
SkScalar x = 20;
SkScalar y = 20;
- canvas->drawText(str, len, x, y, paint);
+ canvas->drawSimpleText(str, len, kUTF8_SkTextEncoding, x, y, font, paint);
y += 20;
- const SkPoint pts[] = { { x, y }, { x + paint.measureText(str, len), y } };
+ const SkPoint pts[] = { { x, y }, { x + font.measureText(str, len, kUTF8_SkTextEncoding), y } };
const SkColor colors[] = { SK_ColorBLACK, SK_ColorBLACK, 0 };
const SkScalar pos[] = { 0, 0.9f, 1 };
paint.setShader(SkGradientShader::MakeLinear(pts, colors, pos,
SK_ARRAY_COUNT(colors),
SkShader::kClamp_TileMode));
- canvas->drawText(str, len, x, y, paint);
+ canvas->drawSimpleText(str, len, kUTF8_SkTextEncoding, x, y, font, paint);
y += 20;
paint.setShader(nullptr);
- drawFadingText(canvas, str, len, x, y, paint);
+ drawFadingText(canvas, str, len, x, y, font, paint);
}
static void scale_rect(SkIRect* dst, const SkIRect& src, float scale) {
diff --git a/samplecode/SampleTextAlpha.cpp b/samplecode/SampleTextAlpha.cpp
deleted file mode 100644
index 14885e8b85..0000000000
--- a/samplecode/SampleTextAlpha.cpp
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Copyright 2011 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "Sample.h"
-#include "SkBlurMask.h"
-#include "SkCanvas.h"
-#include "SkGradientShader.h"
-#include "SkGraphics.h"
-#include "SkMaskFilter.h"
-#include "SkPath.h"
-#include "SkRandom.h"
-#include "SkRegion.h"
-#include "SkShader.h"
-#include "SkUTF.h"
-#include "SkColorPriv.h"
-#include "SkColorFilter.h"
-#include "SkTime.h"
-#include "SkTypeface.h"
-
-#include "SkOSFile.h"
-#include "SkStream.h"
-
-class TextAlphaView : public Sample {
-public:
- TextAlphaView() {
- fByte = 0xFF;
- }
-
-protected:
- bool onQuery(Sample::Event* evt) override {
- if (Sample::TitleQ(*evt)) {
- Sample::TitleR(evt, "TextAlpha");
- return true;
- }
- return this->INHERITED::onQuery(evt);
- }
-
- void onDrawContent(SkCanvas* canvas) override {
- const char* str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- SkPaint paint;
- SkScalar x = SkIntToScalar(10);
- SkScalar y = SkIntToScalar(20);
-
- paint.setFlags(0x105);
-
- paint.setARGB(fByte, 0xFF, 0xFF, 0xFF);
-
- paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
- SkBlurMask::ConvertRadiusToSigma(3)));
-
- SkRandom rand;
-
- for (int ps = 6; ps <= 35; ps++) {
- paint.setColor(rand.nextU() | (0xFF << 24));
- paint.setTextSize(SkIntToScalar(ps));
- paint.setTextSize(SkIntToScalar(24));
- canvas->drawString(str, x, y, paint);
- y += paint.getFontMetrics(nullptr);
- }
- }
-
- Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) override {
- return new Click(this);
- }
-
- bool onClick(Click* click) override {
- int y = click->fICurr.fY;
- if (y < 0) {
- y = 0;
- } else if (y > 255) {
- y = 255;
- }
- fByte = y;
- return true;
- }
-
-private:
- int fByte;
-
- typedef Sample INHERITED;
-};
-
-//////////////////////////////////////////////////////////////////////////////
-
-DEF_SAMPLE( return new TextAlphaView(); )
diff --git a/samplecode/SampleUnpremul.cpp b/samplecode/SampleUnpremul.cpp
index 3e670be05c..981ec9da3e 100644
--- a/samplecode/SampleUnpremul.cpp
+++ b/samplecode/SampleUnpremul.cpp
@@ -75,12 +75,14 @@ protected:
void onDrawContent(SkCanvas* canvas) override {
SkPaint paint;
paint.setAntiAlias(true);
- paint.setTextSize(SkIntToScalar(24));
+
+ SkFont font;
+ font.setSize(24);
auto looper(
SkBlurDrawLooper::Make(SK_ColorBLUE, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(2)),
0, 0));
paint.setLooper(looper);
- SkScalar height = paint.getFontMetrics(nullptr);
+ SkScalar height = font.getMetrics(nullptr);
if (!fDecodeSucceeded) {
SkString failure;
if (fResPath.size() == 0) {
@@ -88,7 +90,7 @@ protected:
} else {
failure.printf("Failed to decode %s", fCurrFile.c_str());
}
- canvas->drawString(failure, 0, height, paint);
+ canvas->drawSimpleText(failure.c_str(), failure.size(), kUTF8_SkTextEncoding, 0, height, font, paint);
return;
}
@@ -96,16 +98,16 @@ protected:
SkString header(SkOSPath::Basename(fCurrFile.c_str()));
header.appendf(" [%dx%d] %s", fBitmap.width(), fBitmap.height(),
(fPremul ? "premultiplied" : "unpremultiplied"));
- canvas->drawString(header, 0, height, paint);
+ canvas->drawSimpleText(header.c_str(), header.size(), kUTF8_SkTextEncoding, 0, height, font, paint);
canvas->translate(0, height);
// Help messages
header.printf("Press '%c' to move to the next image.'", fNextImageChar);
- canvas->drawString(header, 0, height, paint);
+ canvas->drawSimpleText(header.c_str(), header.size(), kUTF8_SkTextEncoding, 0, height, font, paint);
canvas->translate(0, height);
header.printf("Press '%c' to toggle premultiplied decode.", fTogglePremulChar);
- canvas->drawString(header, 0, height, paint);
+ canvas->drawSimpleText(header.c_str(), header.size(), kUTF8_SkTextEncoding, 0, height, font, paint);
// Now draw the image itself.
canvas->translate(height * 2, height * 2);