blob: ec939d193692d19911eb79a7002fc30d557edb2e [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
8#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000010#include "SkCanvas.h"
commit-bot@chromium.org74266812014-05-22 17:41:41 +000011#include "SkDashPathEffect.h"
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000012
reed@google.com0d30c512014-03-14 14:02:58 +000013static void test_nulldev(SkCanvas* canvas) {
14 SkBitmap bm;
reed6c225732014-06-09 19:52:07 -070015 bm.setInfo(SkImageInfo::MakeN32Premul(30, 30));
reed@google.com0d30c512014-03-14 14:02:58 +000016 // notice: no pixels mom! be sure we don't crash
17 // https://code.google.com/p/chromium/issues/detail?id=352616
18 SkCanvas c(bm);
19
20 SkBitmap src;
21 src.allocN32Pixels(10, 10);
22 src.eraseColor(SK_ColorRED);
23
24 // ensure we don't crash
25 c.writePixels(src, 0, 0);
26}
27
commit-bot@chromium.org74266812014-05-22 17:41:41 +000028static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, SkScalar strokeWidth) {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000029 SkPaint p(paint);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000030 SkPoint loc = { 20, 435 };
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000031
commit-bot@chromium.org74266812014-05-22 17:41:41 +000032 if (strokeWidth > 0) {
33 p.setStyle(SkPaint::kFill_Style);
Cary Clark2a475ea2017-04-28 15:35:12 -040034 canvas->drawString("P", loc.fX, loc.fY - 225, p);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000035 canvas->drawPosText("P", 1, &loc, p);
36 }
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000037
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000038 p.setColor(SK_ColorRED);
39 p.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000040 p.setStrokeWidth(strokeWidth);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000041
Cary Clark2a475ea2017-04-28 15:35:12 -040042 canvas->drawString("P", loc.fX, loc.fY - 225, p);
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000043 canvas->drawPosText("P", 1, &loc, p);
44}
45
commit-bot@chromium.org74266812014-05-22 17:41:41 +000046static void draw_text_set(SkCanvas* canvas, const SkPaint& paint) {
47 SkAutoCanvasRestore acr(canvas, true);
48
49 draw_text_stroked(canvas, paint, 10);
50
51 canvas->translate(200, 0);
52 draw_text_stroked(canvas, paint, 0);
53
54 const SkScalar intervals[] = { 20, 10, 5, 10 };
55 const SkScalar phase = 0;
56
57 canvas->translate(200, 0);
58 SkPaint p(paint);
reeda4393342016-03-18 11:22:57 -070059 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase));
reed@google.com7cb5e472014-05-22 17:59:51 +000060 draw_text_stroked(canvas, p, 10);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000061}
62
halcanary2a243382015-09-09 08:16:41 -070063namespace {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000064 enum {
65 kBelowThreshold_TextSize = 255,
66 kAboveThreshold_TextSize = 257
67 };
halcanary2a243382015-09-09 08:16:41 -070068}
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000069
halcanary2a243382015-09-09 08:16:41 -070070DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) {
reed@google.com0d30c512014-03-14 14:02:58 +000071 if (true) { test_nulldev(canvas); }
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000072 SkPaint paint;
73 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070074 sk_tool_utils::set_portable_typeface(&paint);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000075
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000076 paint.setTextSize(kBelowThreshold_TextSize);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000077 draw_text_set(canvas, paint);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000078
commit-bot@chromium.org74266812014-05-22 17:41:41 +000079 canvas->translate(600, 0);
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000080 paint.setTextSize(kAboveThreshold_TextSize);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000081 draw_text_set(canvas, paint);
halcanary2a243382015-09-09 08:16:41 -070082}