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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 11 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkTextBlob.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 20 | #include "include/core/SkTypeface.h" |
| 21 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/effects/SkDashPathEffect.h" |
| 23 | #include "tools/ToolUtils.h" |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 24 | |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 25 | static void test_nulldev(SkCanvas* canvas) { |
| 26 | SkBitmap bm; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 27 | bm.setInfo(SkImageInfo::MakeN32Premul(30, 30)); |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 28 | // 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 Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 40 | static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font, |
| 41 | SkScalar strokeWidth) { |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 42 | SkPaint p(paint); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 43 | SkPoint loc = { 20, 435 }; |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 44 | |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 45 | if (strokeWidth > 0) { |
| 46 | p.setStyle(SkPaint::kFill_Style); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 47 | canvas->drawSimpleText("P", 1, kUTF8_SkTextEncoding, loc.fX, loc.fY - 225, font, p); |
| 48 | 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] | 49 | } |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 50 | |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 51 | p.setColor(SK_ColorRED); |
| 52 | p.setStyle(SkPaint::kStroke_Style); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 53 | p.setStrokeWidth(strokeWidth); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 54 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 55 | canvas->drawSimpleText("P", 1, kUTF8_SkTextEncoding, loc.fX, loc.fY - 225, font, p); |
| 56 | 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] | 57 | } |
| 58 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 59 | 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] | 60 | SkAutoCanvasRestore acr(canvas, true); |
| 61 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 62 | draw_text_stroked(canvas, paint, font, 10); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 63 | |
| 64 | canvas->translate(200, 0); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 65 | draw_text_stroked(canvas, paint, font, 0); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 66 | |
| 67 | const SkScalar intervals[] = { 20, 10, 5, 10 }; |
| 68 | const SkScalar phase = 0; |
| 69 | |
| 70 | canvas->translate(200, 0); |
| 71 | SkPaint p(paint); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 72 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase)); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 73 | draw_text_stroked(canvas, p, font, 10); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 74 | } |
| 75 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 76 | namespace { |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 77 | enum { |
| 78 | kBelowThreshold_TextSize = 255, |
| 79 | kAboveThreshold_TextSize = 257 |
| 80 | }; |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 81 | } |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 82 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 83 | DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) { |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 84 | if (true) { test_nulldev(canvas); } |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 85 | |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 86 | SkPaint paint; |
| 87 | paint.setAntiAlias(true); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 88 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 89 | SkFont font(ToolUtils::create_portable_typeface(), kBelowThreshold_TextSize); |
Mike Reed | 088b74e | 2018-12-24 14:52:46 -0500 | [diff] [blame] | 90 | draw_text_set(canvas, paint, font); |
| 91 | |
| 92 | canvas->translate(600, 0); |
| 93 | font.setSize(kAboveThreshold_TextSize); |
| 94 | draw_text_set(canvas, paint, font); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 95 | } |