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" |
| 9 | #include "SkCanvas.h" |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 10 | #include "SkDashPathEffect.h" |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 11 | |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 12 | static void test_nulldev(SkCanvas* canvas) { |
| 13 | SkBitmap bm; |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 14 | bm.setInfo(SkImageInfo::MakeN32Premul(30, 30)); |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 15 | // 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.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 27 | 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] | 28 | SkPaint p(paint); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 29 | SkPoint loc = { 20, 435 }; |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 30 | |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 31 | 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.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 36 | |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 37 | p.setColor(SK_ColorRED); |
| 38 | p.setStyle(SkPaint::kStroke_Style); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 39 | p.setStrokeWidth(strokeWidth); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 40 | |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 41 | canvas->drawText("P", 1, loc.fX, loc.fY - 225, p); |
| 42 | canvas->drawPosText("P", 1, &loc, p); |
| 43 | } |
| 44 | |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 45 | static 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.com | 7cb5e47 | 2014-05-22 17:59:51 +0000 | [diff] [blame] | 59 | draw_text_stroked(canvas, p, 10); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 60 | } |
| 61 | |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 62 | class StrokeTextGM : public skiagm::GM { |
| 63 | // Skia has a threshold above which it draws text via paths instead of using scalercontext |
| 64 | // and caching the glyph. This GM wants to ensure that we draw stroking correctly on both |
| 65 | // sides of this threshold. |
| 66 | enum { |
| 67 | kBelowThreshold_TextSize = 255, |
| 68 | kAboveThreshold_TextSize = 257 |
| 69 | }; |
| 70 | public: |
| 71 | StrokeTextGM() {} |
| 72 | |
| 73 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 74 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 75 | SkString onShortName() override { |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 76 | return SkString("stroketext"); |
| 77 | } |
| 78 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 79 | SkISize onISize() override { |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 80 | return SkISize::Make(1200, 480); |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 81 | } |
| 82 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 83 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 0d30c51 | 2014-03-14 14:02:58 +0000 | [diff] [blame] | 84 | if (true) { test_nulldev(canvas); } |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 85 | SkPaint paint; |
| 86 | paint.setAntiAlias(true); |
caryclark | 85693c1 | 2015-07-20 10:48:01 -0700 | [diff] [blame] | 87 | sk_tool_utils::set_portable_typeface_always(&paint); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 88 | |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 89 | paint.setTextSize(kBelowThreshold_TextSize); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 90 | draw_text_set(canvas, paint); |
skia.committer@gmail.com | affa77d | 2014-03-13 03:02:23 +0000 | [diff] [blame] | 91 | |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 92 | canvas->translate(600, 0); |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 93 | paint.setTextSize(kAboveThreshold_TextSize); |
commit-bot@chromium.org | 7426681 | 2014-05-22 17:41:41 +0000 | [diff] [blame] | 94 | draw_text_set(canvas, paint); |
commit-bot@chromium.org | 641e33b | 2014-03-12 20:31:24 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | private: |
| 98 | typedef skiagm::GM INHERITED; |
| 99 | }; |
| 100 | |
| 101 | DEF_GM( return SkNEW(StrokeTextGM); ) |