commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 11 | #include "SkDashPathEffect.h" |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 12 | |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 13 | static void test_nulldev(SkCanvas* canvas) { |
| 14 | SkBitmap bm; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 15 | bm.setInfo(SkImageInfo::MakeN32Premul(30, 30)); |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 16 | // 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.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 28 | static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, SkScalar strokeWidth) { |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 29 | SkPaint p(paint); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 30 | SkPoint loc = { 20, 435 }; |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 31 | |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 32 | if (strokeWidth > 0) { |
| 33 | p.setStyle(SkPaint::kFill_Style); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 34 | canvas->drawString("P", loc.fX, loc.fY - 225, p); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 35 | canvas->drawPosText("P", 1, &loc, p); |
| 36 | } |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 37 | |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 38 | p.setColor(SK_ColorRED); |
| 39 | p.setStyle(SkPaint::kStroke_Style); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 40 | p.setStrokeWidth(strokeWidth); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 41 | |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 42 | canvas->drawString("P", loc.fX, loc.fY - 225, p); |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 43 | canvas->drawPosText("P", 1, &loc, p); |
| 44 | } |
| 45 | |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 46 | static 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); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 59 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase)); |
reed@google.com | 7cb5e47 | 2014-05-22 17:59:51 +0000 | [diff] [blame] | 60 | draw_text_stroked(canvas, p, 10); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 61 | } |
| 62 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 63 | namespace { |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 64 | enum { |
| 65 | kBelowThreshold_TextSize = 255, |
| 66 | kAboveThreshold_TextSize = 257 |
| 67 | }; |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 68 | } |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 69 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 70 | DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) { |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 71 | if (true) { test_nulldev(canvas); } |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 72 | SkPaint paint; |
| 73 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 74 | sk_tool_utils::set_portable_typeface(&paint); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 75 | |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 76 | paint.setTextSize(kBelowThreshold_TextSize); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 77 | draw_text_set(canvas, paint); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 78 | |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 79 | canvas->translate(600, 0); |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 80 | paint.setTextSize(kAboveThreshold_TextSize); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 81 | draw_text_set(canvas, paint); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 82 | } |