blob: d0a9882256ba5a12fb0cb0badd3bd91a63a57eb7 [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"
9#include "SkCanvas.h"
commit-bot@chromium.org74266812014-05-22 17:41:41 +000010#include "SkDashPathEffect.h"
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000011
reed@google.com0d30c512014-03-14 14:02:58 +000012static void test_nulldev(SkCanvas* canvas) {
13 SkBitmap bm;
reed6c225732014-06-09 19:52:07 -070014 bm.setInfo(SkImageInfo::MakeN32Premul(30, 30));
reed@google.com0d30c512014-03-14 14:02:58 +000015 // notice: no pixels mom! be sure we don't crash
16 // https://code.google.com/p/chromium/issues/detail?id=352616
17 SkCanvas c(bm);
18
19 SkBitmap src;
20 src.allocN32Pixels(10, 10);
21 src.eraseColor(SK_ColorRED);
22
23 // ensure we don't crash
24 c.writePixels(src, 0, 0);
25}
26
commit-bot@chromium.org74266812014-05-22 17:41:41 +000027static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, SkScalar strokeWidth) {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000028 SkPaint p(paint);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000029 SkPoint loc = { 20, 435 };
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000030
commit-bot@chromium.org74266812014-05-22 17:41:41 +000031 if (strokeWidth > 0) {
32 p.setStyle(SkPaint::kFill_Style);
33 canvas->drawText("P", 1, loc.fX, loc.fY - 225, p);
34 canvas->drawPosText("P", 1, &loc, p);
35 }
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000036
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000037 p.setColor(SK_ColorRED);
38 p.setStyle(SkPaint::kStroke_Style);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000039 p.setStrokeWidth(strokeWidth);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000040
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000041 canvas->drawText("P", 1, loc.fX, loc.fY - 225, p);
42 canvas->drawPosText("P", 1, &loc, p);
43}
44
commit-bot@chromium.org74266812014-05-22 17:41:41 +000045static void draw_text_set(SkCanvas* canvas, const SkPaint& paint) {
46 SkAutoCanvasRestore acr(canvas, true);
47
48 draw_text_stroked(canvas, paint, 10);
49
50 canvas->translate(200, 0);
51 draw_text_stroked(canvas, paint, 0);
52
53 const SkScalar intervals[] = { 20, 10, 5, 10 };
54 const SkScalar phase = 0;
55
56 canvas->translate(200, 0);
57 SkPaint p(paint);
58 p.setPathEffect(SkDashPathEffect::Create(intervals, SK_ARRAY_COUNT(intervals), phase))->unref();
reed@google.com7cb5e472014-05-22 17:59:51 +000059 draw_text_stroked(canvas, p, 10);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000060}
61
halcanary2a243382015-09-09 08:16:41 -070062namespace {
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000063 enum {
64 kBelowThreshold_TextSize = 255,
65 kAboveThreshold_TextSize = 257
66 };
halcanary2a243382015-09-09 08:16:41 -070067}
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000068
halcanary2a243382015-09-09 08:16:41 -070069DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) {
reed@google.com0d30c512014-03-14 14:02:58 +000070 if (true) { test_nulldev(canvas); }
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000071 SkPaint paint;
72 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070073 sk_tool_utils::set_portable_typeface(&paint);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000074
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000075 paint.setTextSize(kBelowThreshold_TextSize);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000076 draw_text_set(canvas, paint);
skia.committer@gmail.comaffa77d2014-03-13 03:02:23 +000077
commit-bot@chromium.org74266812014-05-22 17:41:41 +000078 canvas->translate(600, 0);
commit-bot@chromium.org641e33b2014-03-12 20:31:24 +000079 paint.setTextSize(kAboveThreshold_TextSize);
commit-bot@chromium.org74266812014-05-22 17:41:41 +000080 draw_text_set(canvas, paint);
halcanary2a243382015-09-09 08:16:41 -070081}