reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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" |
| 10 | #include "SkPath.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkTypeface.h" |
| 13 | |
| 14 | static SkShader* make_heatGradient(const SkPoint pts[2]) { |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 15 | const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE }; |
| 16 | |
| 17 | return SkGradientShader::CreateLinear(pts, bw, NULL, |
| 18 | SK_ARRAY_COUNT(bw), |
| 19 | SkShader::kClamp_TileMode); |
| 20 | } |
| 21 | |
| 22 | static bool setFont(SkPaint* paint, const char name[]) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 23 | SkTypeface* tf = sk_tool_utils::create_portable_typeface(name, SkTypeface::kNormal); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 24 | if (tf) { |
| 25 | paint->setTypeface(tf)->unref(); |
| 26 | return true; |
| 27 | } |
reed@google.com | ce151d0 | 2012-03-08 16:10:57 +0000 | [diff] [blame] | 28 | return false; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 29 | } |
| 30 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 31 | /** |
| 32 | Test a set of clipping problems discovered while writing blitAntiRect, |
| 33 | and test all the code paths through the clipping blitters. |
| 34 | Each region should show as a blue center surrounded by a 2px green |
| 35 | border, with no red. |
| 36 | */ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 37 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 38 | #define HEIGHT 480 |
| 39 | |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 40 | class GammaTextGM : public skiagm::GM { |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 41 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | SkString onShortName() override { |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 43 | return SkString("gammatext"); |
| 44 | } |
| 45 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 47 | return SkISize::Make(1024, HEIGHT); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void drawGrad(SkCanvas* canvas) { |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 51 | SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } }; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 52 | SkShader* s = make_heatGradient(pts); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 53 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 54 | canvas->clear(SK_ColorRED); |
| 55 | SkPaint paint; |
| 56 | paint.setShader(s)->unref(); |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 57 | SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) }; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 58 | canvas->drawRect(r, paint); |
| 59 | } |
| 60 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 62 | drawGrad(canvas); |
| 63 | |
| 64 | const SkColor fg[] = { |
| 65 | 0xFFFFFFFF, |
| 66 | 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, |
| 67 | 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, |
| 68 | 0xFF000000, |
| 69 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 70 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 71 | const char* text = "Hamburgefons"; |
| 72 | size_t len = strlen(text); |
| 73 | |
| 74 | SkPaint paint; |
| 75 | setFont(&paint, "Times"); |
| 76 | paint.setTextSize(SkIntToScalar(16)); |
| 77 | paint.setAntiAlias(true); |
| 78 | paint.setLCDRenderText(true); |
| 79 | |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 80 | SkScalar x = SkIntToScalar(10); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 81 | for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 82 | paint.setColor(fg[i]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 83 | |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 84 | SkScalar y = SkIntToScalar(40); |
| 85 | SkScalar stopy = SkIntToScalar(HEIGHT); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 86 | while (y < stopy) { |
reed | 04f7e14 | 2014-11-13 12:46:24 -0800 | [diff] [blame] | 87 | canvas->drawText(text, len, x, y, paint); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 88 | y += paint.getTextSize() * 2; |
| 89 | } |
| 90 | x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg); |
| 91 | } |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | private: |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 95 | typedef skiagm::GM INHERITED; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
bungeman | 67b21a7 | 2015-02-19 07:45:14 -0800 | [diff] [blame] | 98 | DEF_GM( return new GammaTextGM; ) |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 99 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 100 | ////////////////////////////////////////////////////////////////////////////// |
| 101 | |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 102 | static SkShader* make_gradient(SkColor c) { |
| 103 | const SkPoint pts[] = { { 0, 0 }, { 240, 0 } }; |
| 104 | SkColor colors[2]; |
| 105 | colors[0] = c; |
| 106 | colors[1] = SkColorSetA(c, 0); |
| 107 | return SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 108 | } |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 109 | |
| 110 | static void set_face(SkPaint* paint) { |
| 111 | SkTypeface* face = SkTypeface::CreateFromName("serif", SkTypeface::kItalic); |
| 112 | SkSafeUnref(paint->setTypeface(face)); |
| 113 | } |
| 114 | |
| 115 | static void draw_pair(SkCanvas* canvas, SkPaint* paint, SkShader* shader) { |
| 116 | const char text[] = "Now is the time for all good"; |
| 117 | const size_t len = strlen(text); |
| 118 | |
| 119 | paint->setShader(NULL); |
| 120 | canvas->drawText(text, len, 10, 20, *paint); |
| 121 | paint->setShader(SkShader::CreateColorShader(paint->getColor()))->unref(); |
| 122 | canvas->drawText(text, len, 10, 40, *paint); |
| 123 | paint->setShader(shader); |
| 124 | canvas->drawText(text, len, 10, 60, *paint); |
| 125 | } |
| 126 | |
| 127 | class GammaShaderTextGM : public skiagm::GM { |
| 128 | SkShader* fShaders[3]; |
| 129 | SkColor fColors[3]; |
| 130 | |
| 131 | public: |
| 132 | GammaShaderTextGM() { |
| 133 | const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE }; |
| 134 | for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 135 | fShaders[i] = NULL; |
| 136 | fColors[i] = colors[i]; |
| 137 | } |
| 138 | } |
| 139 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 140 | ~GammaShaderTextGM() override { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 141 | for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 142 | SkSafeUnref(fShaders[i]); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 147 | SkString onShortName() override { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 148 | return SkString("gammagradienttext"); |
| 149 | } |
| 150 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 151 | SkISize onISize() override { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 152 | return SkISize::Make(300, 300); |
| 153 | } |
| 154 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 155 | void onOnceBeforeDraw() override { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 156 | for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 157 | fShaders[i] = make_gradient(fColors[i]); |
| 158 | } |
| 159 | } |
| 160 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 161 | void onDraw(SkCanvas* canvas) override { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 162 | SkPaint paint; |
| 163 | paint.setAntiAlias(true); |
| 164 | paint.setLCDRenderText(true); |
| 165 | paint.setTextSize(18); |
| 166 | set_face(&paint); |
| 167 | |
| 168 | for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 169 | paint.setColor(fColors[i]); |
| 170 | draw_pair(canvas, &paint, fShaders[i]); |
| 171 | canvas->translate(0, 80); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private: |
| 176 | typedef skiagm::GM INHERITED; |
| 177 | }; |
| 178 | |
| 179 | DEF_GM( return new GammaShaderTextGM; ) |
| 180 | |