| 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/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkFont.h" |
| 12 |
#include "include/core/SkFontStyle.h" |
| 13 |
#include "include/core/SkPaint.h" |
| 14 |
#include "include/core/SkPoint.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkRefCnt.h" |
| 17 |
#include "include/core/SkScalar.h" |
| 18 |
#include "include/core/SkShader.h" |
| 19 |
#include "include/core/SkSize.h" |
| 20 |
#include "include/core/SkString.h" |
| 21 |
#include "include/core/SkTileMode.h" |
| 22 |
#include "include/core/SkTypeface.h" |
| 23 |
#include "include/core/SkTypes.h" |
| 24 |
#include "include/effects/SkGradient.h" |
| 25 |
#include "tools/fonts/FontToolUtils.h" |
| 26 |
|
| 27 |
static sk_sp<SkShader> make_heatGradient(const SkPoint pts[2]) { |
| 28 |
const SkColor4f bw[] = { SkColors::kBlack, SkColors::kWhite }; |
| 29 |
|
| 30 |
return SkShaders::LinearGradient(pts, {{bw, {}, SkTileMode::kClamp}, {}}); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
Test a set of clipping problems discovered while writing blitAntiRect, |
| 35 |
and test all the code paths through the clipping blitters. |
| 36 |
Each region should show as a blue center surrounded by a 2px green |
| 37 |
border, with no red. |
| 38 |
*/ |
| 39 |
|
| 40 |
#define HEIGHT 480 |
| 41 |
|
| 42 |
class GammaTextGM : public skiagm::GM { |
| 43 |
protected: |
| 44 |
SkString getName() const override { return SkString("gammatext"); } |
| 45 |
|
| 46 |
SkISize getISize() override { return SkISize::Make(1024, HEIGHT); } |
| 47 |
|
| 48 |
static void drawGrad(SkCanvas* canvas) { |
| 49 |
const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } }; |
| 50 |
|
| 51 |
canvas->clear(SK_ColorRED); |
| 52 |
SkPaint paint; |
| 53 |
paint.setShader(make_heatGradient(pts)); |
| 54 |
SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) }; |
| 55 |
canvas->drawRect(r, paint); |
| 56 |
} |
| 57 |
|
| 58 |
void onDraw(SkCanvas* canvas) override { |
| 59 |
drawGrad(canvas); |
| 60 |
|
| 61 |
const SkColor fg[] = { |
| 62 |
0xFFFFFFFF, |
| 63 |
0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, |
| 64 |
0xFFFF0000, 0xFF00FF00, 0xFF0000FF, |
| 65 |
0xFF000000, |
| 66 |
}; |
| 67 |
|
| 68 |
const char* text = "Hamburgefons"; |
| 69 |
|
| 70 |
SkPaint paint; |
| 71 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 72 |
font.setSize(16); |
| 73 |
font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 74 |
|
| 75 |
SkScalar x = SkIntToScalar(10); |
| 76 |
for (size_t i = 0; i < std::size(fg); ++i) { |
| 77 |
paint.setColor(fg[i]); |
| 78 |
|
| 79 |
SkScalar y = SkIntToScalar(40); |
| 80 |
SkScalar stopy = SkIntToScalar(HEIGHT); |
| 81 |
while (y < stopy) { |
| 82 |
canvas->drawString(text, x, y, font, paint); |
| 83 |
y += font.getSize() * 2; |
| 84 |
} |
| 85 |
x += SkIntToScalar(1024) / std::size(fg); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
private: |
| 90 |
using INHERITED = skiagm::GM; |
| 91 |
}; |
| 92 |
|
| 93 |
DEF_GM( return new GammaTextGM; ) |
| 94 |
|
| 95 |
////////////////////////////////////////////////////////////////////////////// |
| 96 |
|
| 97 |
static sk_sp<SkShader> make_gradient(SkColor c) { |
| 98 |
const SkPoint pts[] = { { 0, 0 }, { 240, 0 } }; |
| 99 |
SkColor4f colors[2]; |
| 100 |
colors[0] = SkColor4f::FromColor(c); |
| 101 |
colors[1] = SkColor4f::FromColor(SkColorSetA(c, 0)); |
| 102 |
return SkShaders::LinearGradient(pts, {{colors, {}, SkTileMode::kClamp}, {}}); |
| 103 |
} |
| 104 |
|
| 105 |
static void draw_pair(SkCanvas* canvas, const SkFont& font, SkColor color, |
| 106 |
const sk_sp<SkShader>& shader) { |
| 107 |
static const char text[] = "Now is the time for all good"; |
| 108 |
SkPaint paint; |
| 109 |
paint.setColor(color); |
| 110 |
canvas->drawString(text, 10, 20, font, paint); |
| 111 |
paint.setShader(SkShaders::Color(paint.getColor())); |
| 112 |
canvas->drawString(text, 10, 40, font, paint); |
| 113 |
paint.setShader(shader); |
| 114 |
canvas->drawString(text, 10, 60, font, paint); |
| 115 |
} |
| 116 |
|
| 117 |
class GammaShaderTextGM : public skiagm::GM { |
| 118 |
sk_sp<SkShader> fShaders[3]; |
| 119 |
SkColor fColors[3]; |
| 120 |
|
| 121 |
public: |
| 122 |
GammaShaderTextGM() { |
| 123 |
const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE }; |
| 124 |
for (size_t i = 0; i < std::size(fShaders); ++i) { |
| 125 |
fColors[i] = colors[i]; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
protected: |
| 130 |
SkString getName() const override { return SkString("gammagradienttext"); } |
| 131 |
|
| 132 |
SkISize getISize() override { return SkISize::Make(300, 300); } |
| 133 |
|
| 134 |
void onOnceBeforeDraw() override { |
| 135 |
for (size_t i = 0; i < std::size(fShaders); ++i) { |
| 136 |
fShaders[i] = make_gradient(fColors[i]); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void onDraw(SkCanvas* canvas) override { |
| 141 |
SkPaint paint; |
| 142 |
paint.setAntiAlias(true); |
| 143 |
sk_sp<SkTypeface> tf = ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Italic()); |
| 144 |
SkASSERT(tf); |
| 145 |
SkFont font(tf, 18); |
| 146 |
font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 147 |
|
| 148 |
for (size_t i = 0; i < std::size(fShaders); ++i) { |
| 149 |
draw_pair(canvas, font, fColors[i], fShaders[i]); |
| 150 |
canvas->translate(0, 80); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
private: |
| 155 |
using INHERITED = skiagm::GM; |
| 156 |
}; |
| 157 |
|
| 158 |
DEF_GM( return new GammaShaderTextGM; ) |
| 159 |
|
| 160 |
DEF_SIMPLE_GM_BG(gammatext_color_shader, canvas, 300, 275, SK_ColorGRAY) { |
| 161 |
const char* kText = "ABCDEFG"; |
| 162 |
sk_sp<SkTypeface> tf = ToolUtils::CreatePortableTypeface("serif", SkFontStyle()); |
| 163 |
SkASSERT(tf); |
| 164 |
SkFont font(tf, 18); |
| 165 |
font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 166 |
|
| 167 |
canvas->translate(10, 30); |
| 168 |
for (int i = 0; i < 256; i += 20) { |
| 169 |
SkColor color = SkColorSetRGB(i, i, i); |
| 170 |
SkPaint paint; |
| 171 |
paint.setColor(color); |
| 172 |
canvas->drawString(kText, 0, 0, font, paint); |
| 173 |
paint.setShader(SkShaders::Color(color)); |
| 174 |
canvas->drawString(kText, 100, 0, font, paint); |
| 175 |
paint.setShader(SkShaders::Color(SkColor4f::FromColor(color), SkColorSpace::MakeSRGB())); |
| 176 |
canvas->drawString(kText, 200, 0, font, paint); |
| 177 |
canvas->translate(0, 20); |
| 178 |
} |
| 179 |
} |