aboutsummaryrefslogtreecommitdiff
path: root/samplecode
diff options
context:
space:
mode:
authorMike Reed <reed@google.com>2018-12-21 22:22:31 -0500
committerSkia Commit-Bot <skia-commit-bot@chromium.org>2018-12-22 12:23:07 +0000
commit12a6d452b1260d8f8c34711fda06c03e27b0a54d (patch)
treed1694a928baa26715e393676e359ce241be49f07 /samplecode
parentc983480b117f413c808081d76087afc25a90b762 (diff)
downloadskqp-12a6d452b1260d8f8c34711fda06c03e27b0a54d.tar.gz
use font for measuring
Bug: skia: Change-Id: I451c61d5b98cb42440a4eaa889d9404638a72f5c Reviewed-on: https://skia-review.googlesource.com/c/179980 Reviewed-by: Mike Reed <reed@google.com> Commit-Queue: Mike Reed <reed@google.com>
Diffstat (limited to 'samplecode')
-rw-r--r--samplecode/SampleAnimatedText.cpp23
-rw-r--r--samplecode/SampleChineseFling.cpp32
2 files changed, 21 insertions, 34 deletions
diff --git a/samplecode/SampleAnimatedText.cpp b/samplecode/SampleAnimatedText.cpp
index 1aa50e2fb9..a7e86d6b44 100644
--- a/samplecode/SampleAnimatedText.cpp
+++ b/samplecode/SampleAnimatedText.cpp
@@ -24,11 +24,10 @@
SkRandom gRand;
static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkScalar x, SkScalar y,
- const SkPaint& paint) {
- SkPaint p(paint);
-
- p.setSubpixelText(true);
- canvas->drawText(text, length, x, y, p);
+ const SkFont& font, const SkPaint& paint) {
+ SkFont f(font);
+ f.setSubpixel(true);
+ canvas->drawSimpleText(text, length, kUTF8_SkTextEncoding, x, y, f, paint);
}
// This sample demonstrates the cache behavior of bitmap vs. distance field text
@@ -69,8 +68,9 @@ protected:
}
void onDrawContent(SkCanvas* canvas) override {
+ SkFont font(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
+
SkPaint paint;
- paint.setTypeface(SkTypeface::MakeFromFile("/skimages/samplefont.ttf"));
paint.setAntiAlias(true);
paint.setFilterQuality(kMedium_SkFilterQuality);
@@ -118,15 +118,16 @@ protected:
SkScalar y = SkIntToScalar(0);
for (int i = 12; i <= 26; i++) {
- paint.setTextSize(SkIntToScalar(i*fSizeScale));
- y += paint.getFontSpacing();
- DrawTheText(canvas, text, length, SkIntToScalar(110), y, paint);
+ font.setSize(SkIntToScalar(i*fSizeScale));
+ y += font.getSpacing();
+ DrawTheText(canvas, text, length, SkIntToScalar(110), y, font, paint);
}
canvas->restore();
- paint.setTextSize(16);
+ font.setSize(16);
// canvas->drawString(outString, 512.f, 540.f, paint);
- canvas->drawString(modeString, 768.f, 540.f, paint);
+ canvas->drawSimpleText(modeString.c_str(), modeString.size(), kUTF8_SkTextEncoding,
+ 768.f, 540.f, font, paint);
}
bool onAnimate(const SkAnimTimer& timer) override {
diff --git a/samplecode/SampleChineseFling.cpp b/samplecode/SampleChineseFling.cpp
index b8110f34fa..3b73bb213d 100644
--- a/samplecode/SampleChineseFling.cpp
+++ b/samplecode/SampleChineseFling.cpp
@@ -20,16 +20,6 @@
#include "GrContextPriv.h"
#endif
-static void make_paint(SkPaint* paint, sk_sp<SkTypeface> typeface) {
- static const int kTextSize = 56;
-
- paint->setAntiAlias(true);
- paint->setColor(0xDE000000);
- paint->setTypeface(typeface);
- paint->setTextSize(kTextSize);
- paint->setTextEncoding(kUTF32_SkTextEncoding);
-}
-
static sk_sp<SkTypeface> chinese_typeface() {
#ifdef SK_BUILD_FOR_ANDROID
return MakeResourceAsTypeface("fonts/NotoSansCJK-Regular.ttc");
@@ -68,7 +58,7 @@ protected:
canvas->clear(0xFFDDDDDD);
SkPaint paint;
- make_paint(&paint, fTypeface);
+ paint.setColor(0xDE000000);
// draw a consistent run of the 'words' - one word per line
int index = fIndex;
@@ -93,10 +83,8 @@ private:
void init() {
fTypeface = chinese_typeface();
- SkPaint paint;
- make_paint(&paint, fTypeface);
-
- paint.getFontMetrics(&fMetrics);
+ SkFont font(fTypeface, 56);
+ font.getMetrics(&fMetrics);
SkUnichar glyphs[kWordLength];
for (int32_t i = 0; i < kNumBlobs; ++i) {
@@ -104,7 +92,7 @@ private:
SkTextBlobBuilder builder;
sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs, kWordLength*4,
- paint, 0, 0);
+ kUTF32_SkTextEncoding, font, 0, 0);
fBlobs.emplace_back(builder.make());
}
@@ -215,14 +203,11 @@ private:
void init() {
fTypeface = chinese_typeface();
+ SkFont font(fTypeface, 11);
+ font.getMetrics(&fMetrics);
+
SkPaint paint;
- paint.setAntiAlias(true);
paint.setColor(0xDE000000);
- paint.setTypeface(fTypeface);
- paint.setTextSize(11);
- paint.setTextEncoding(kUTF32_SkTextEncoding);
-
- paint.getFontMetrics(&fMetrics);
SkUnichar glyphs[45];
for (int32_t i = 0; i < kNumBlobs; ++i) {
@@ -234,7 +219,8 @@ private:
this->createRandomLine(glyphs, currentLineLength);
sk_tool_utils::add_to_text_blob_w_len(&builder, (const char*) glyphs,
- currentLineLength*4, paint, 0, y);
+ currentLineLength*4, kUTF32_SkTextEncoding,
+ font, 0, y);
y += fMetrics.fDescent - fMetrics.fAscent + fMetrics.fLeading;
paragraphLength -= 45;
}