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" |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 12 | #include "SkTextBlob.h" |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 13 | |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 14 | static void test_nulldev(SkCanvas* canvas) { |
| 15 | SkBitmap bm; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 16 | bm.setInfo(SkImageInfo::MakeN32Premul(30, 30)); |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 17 | // notice: no pixels mom! be sure we don't crash |
| 18 | // https://code.google.com/p/chromium/issues/detail?id=352616 |
| 19 | SkCanvas c(bm); |
| 20 | |
| 21 | SkBitmap src; |
| 22 | src.allocN32Pixels(10, 10); |
| 23 | src.eraseColor(SK_ColorRED); |
| 24 | |
| 25 | // ensure we don't crash |
| 26 | c.writePixels(src, 0, 0); |
| 27 | } |
| 28 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 29 | static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font, |
| 30 | SkScalar strokeWidth) { |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 31 | SkPaint p(paint); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 32 | SkPoint loc = { 20, 435 }; |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 33 | |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 34 | if (strokeWidth > 0) { |
| 35 | p.setStyle(SkPaint::kFill_Style); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 36 | canvas->drawSimpleText("P", 1, kUTF8_SkTextEncoding, loc.fX, loc.fY - 225, font, p); |
| 37 | canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 38 | } |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 39 | |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 40 | p.setColor(SK_ColorRED); |
| 41 | p.setStyle(SkPaint::kStroke_Style); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 42 | p.setStrokeWidth(strokeWidth); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 43 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 44 | canvas->drawSimpleText("P", 1, kUTF8_SkTextEncoding, loc.fX, loc.fY - 225, font, p); |
| 45 | canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p); |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 48 | static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) { |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 49 | SkAutoCanvasRestore acr(canvas, true); |
| 50 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 51 | draw_text_stroked(canvas, paint, font, 10); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 52 | |
| 53 | canvas->translate(200, 0); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 54 | draw_text_stroked(canvas, paint, font, 0); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 55 | |
| 56 | const SkScalar intervals[] = { 20, 10, 5, 10 }; |
| 57 | const SkScalar phase = 0; |
| 58 | |
| 59 | canvas->translate(200, 0); |
| 60 | SkPaint p(paint); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 61 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase)); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 62 | draw_text_stroked(canvas, p, font, 10); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 63 | } |
| 64 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 65 | namespace { |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 66 | enum { |
| 67 | kBelowThreshold_TextSize = 255, |
| 68 | kAboveThreshold_TextSize = 257 |
| 69 | }; |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 70 | } |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 71 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 72 | DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) { |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 73 | if (true) { test_nulldev(canvas); } |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 74 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 75 | SkPaint paint; |
| 76 | paint.setAntiAlias(true); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 77 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 78 | SkFont font(sk_tool_utils::create_portable_typeface(), kBelowThreshold_TextSize); |
| 79 | draw_text_set(canvas, paint, font); |
| 80 | |
| 81 | canvas->translate(600, 0); |
| 82 | font.setSize(kAboveThreshold_TextSize); |
| 83 | draw_text_set(canvas, paint, font); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 84 | } |