blob: 4ca103ff7f7639fb465cec3735a7226fbe3f31f5 [file] [log] [blame]
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#include "include/core/SkColor.h"
12#include "include/core/SkFont.h"
13#include "include/core/SkFontTypes.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkPaint.h"
16#include "include/core/SkPathEffect.h"
17#include "include/core/SkPoint.h"
18#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkTextBlob.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/core/SkTypeface.h"
21#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/effects/SkDashPathEffect.h"
23#include "tools/ToolUtils.h"
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000024
reed@google.com0d30c512014-03-14 14:02:58 +000025static void test_nulldev(SkCanvas* canvas) {
26 SkBitmap bm;
reed6c225732014-06-09 19:52:07 -070027 bm.setInfo(SkImageInfo::MakeN32Premul(30, 30));
reed@google.com0d30c512014-03-14 14:02:58 +000028 // notice: no pixels mom! be sure we don't crash
29 // https://code.google.com/p/chromium/issues/detail?id=352616
30 SkCanvas c(bm);
31
32 SkBitmap src;
33 src.allocN32Pixels(10, 10);
34 src.eraseColor(SK_ColorRED);
35
36 // ensure we don't crash
37 c.writePixels(src, 0, 0);
38}
39
Mike Reed088b74e2018-12-24 14:52:46 -050040static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font,
41 SkScalar strokeWidth) {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000042 SkPaint p(paint);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000043 SkPoint loc = { 20, 435 };
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000044
commit-bot@chromium.org74266812014-05-22 17:41:41 +000045 if (strokeWidth > 0) {
46 p.setStyle(SkPaint::kFill_Style);
Ben Wagner51e15a62019-05-07 15:38:46 -040047 canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
Mike Reed088b74e2018-12-24 14:52:46 -050048 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000049 }
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000050
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000051 p.setColor(SK_ColorRED);
52 p.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000053 p.setStrokeWidth(strokeWidth);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000054
Ben Wagner51e15a62019-05-07 15:38:46 -040055 canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
Mike Reed088b74e2018-12-24 14:52:46 -050056 canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000057}
58
Mike Reed088b74e2018-12-24 14:52:46 -050059static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
commit-bot@chromium.org74266812014-05-22 17:41:41 +000060 SkAutoCanvasRestore acr(canvas, true);
61
Mike Reed088b74e2018-12-24 14:52:46 -050062 draw_text_stroked(canvas, paint, font, 10);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000063
64 canvas->translate(200, 0);
Mike Reed088b74e2018-12-24 14:52:46 -050065 draw_text_stroked(canvas, paint, font, 0);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000066
67 const SkScalar intervals[] = { 20, 10, 5, 10 };
68 const SkScalar phase = 0;
69
70 canvas->translate(200, 0);
71 SkPaint p(paint);
reeda4393342016-03-18 11:22:57 -070072 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase));
Mike Reed088b74e2018-12-24 14:52:46 -050073 draw_text_stroked(canvas, p, font, 10);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000074}
75
halcanary2a243382015-09-09 08:16:41 -070076namespace {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000077 enum {
78 kBelowThreshold_TextSize = 255,
79 kAboveThreshold_TextSize = 257
80 };
halcanary2a243382015-09-09 08:16:41 -070081}
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000082
halcanary2a243382015-09-09 08:16:41 -070083DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) {
Mike Reed088b74e2018-12-24 14:52:46 -050084 if (true) { test_nulldev(canvas); }
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000085
Mike Reed088b74e2018-12-24 14:52:46 -050086 SkPaint paint;
87 paint.setAntiAlias(true);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000088
Mike Kleinea3f0142019-03-20 11:12:10 -050089 SkFont font(ToolUtils::create_portable_typeface(), kBelowThreshold_TextSize);
Mike Reed088b74e2018-12-24 14:52:46 -050090 draw_text_set(canvas, paint, font);
91
92 canvas->translate(600, 0);
93 font.setSize(kAboveThreshold_TextSize);
94 draw_text_set(canvas, paint, font);
halcanary2a243382015-09-09 08:16:41 -070095}