| 1 |
/* |
| 2 |
* Copyright 2015 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/SkBlendMode.h" |
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkFont.h" |
| 13 |
#include "include/core/SkPaint.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkRefCnt.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkString.h" |
| 19 |
#include "include/core/SkTextBlob.h" |
| 20 |
#include "include/core/SkTypeface.h" |
| 21 |
#include "include/core/SkTypes.h" |
| 22 |
#include "tools/ToolUtils.h" |
| 23 |
#include "tools/fonts/FontToolUtils.h" |
| 24 |
|
| 25 |
namespace skiagm { |
| 26 |
|
| 27 |
constexpr int kWidth = 750; |
| 28 |
constexpr int kHeight = 750; |
| 29 |
|
| 30 |
class LcdOverlapGM : public skiagm::GM { |
| 31 |
public: |
| 32 |
LcdOverlapGM() { |
| 33 |
const int kPointSize = 25; |
| 34 |
fTextHeight = SkIntToScalar(kPointSize); |
| 35 |
} |
| 36 |
|
| 37 |
protected: |
| 38 |
SkString getName() const override { return SkString("lcdoverlap"); } |
| 39 |
|
| 40 |
void onOnceBeforeDraw() override { |
| 41 |
// build text blob |
| 42 |
SkTextBlobBuilder builder; |
| 43 |
|
| 44 |
SkFont font(ToolUtils::DefaultPortableTypeface(), 32); |
| 45 |
const char* text = "able was I ere I saw elba"; |
| 46 |
font.setSubpixel(true); |
| 47 |
font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 48 |
ToolUtils::add_to_text_blob(&builder, text, font, 0, 0); |
| 49 |
fBlob = builder.make(); |
| 50 |
} |
| 51 |
|
| 52 |
SkISize getISize() override { return SkISize::Make(kWidth, kHeight); } |
| 53 |
|
| 54 |
void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode, |
| 55 |
SkBlendMode mode2) { |
| 56 |
const SkColor colors[] { |
| 57 |
SK_ColorRED, |
| 58 |
SK_ColorGREEN, |
| 59 |
SK_ColorBLUE, |
| 60 |
SK_ColorYELLOW, |
| 61 |
SK_ColorCYAN, |
| 62 |
SK_ColorMAGENTA, |
| 63 |
}; |
| 64 |
|
| 65 |
for (size_t i = 0; i < std::size(colors); i++) { |
| 66 |
canvas->save(); |
| 67 |
canvas->translate(x, y); |
| 68 |
canvas->rotate(360.0f / std::size(colors) * i); |
| 69 |
canvas->translate(-fBlob->bounds().width() / 2.0f - fBlob->bounds().left() + 0.5f, 0); |
| 70 |
|
| 71 |
SkPaint textPaint; |
| 72 |
textPaint.setColor(colors[i]); |
| 73 |
textPaint.setBlendMode(i % 2 == 0 ? mode : mode2); |
| 74 |
canvas->drawTextBlob(fBlob, 0, 0, textPaint); |
| 75 |
canvas->restore(); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
void onDraw(SkCanvas* canvas) override { |
| 80 |
SkScalar offsetX = kWidth / 4.0f; |
| 81 |
SkScalar offsetY = kHeight / 4.0f; |
| 82 |
drawTestCase(canvas, offsetX, offsetY, SkBlendMode::kSrc, SkBlendMode::kSrc); |
| 83 |
drawTestCase(canvas, 3 * offsetX, offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrcOver); |
| 84 |
drawTestCase(canvas, offsetX, 3 * offsetY, SkBlendMode::kHardLight, |
| 85 |
SkBlendMode::kLuminosity); |
| 86 |
drawTestCase(canvas, 3 * offsetX, 3 * offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrc); |
| 87 |
} |
| 88 |
|
| 89 |
private: |
| 90 |
SkScalar fTextHeight; |
| 91 |
sk_sp<SkTextBlob> fBlob; |
| 92 |
using INHERITED = skiagm::GM; |
| 93 |
}; |
| 94 |
|
| 95 |
////////////////////////////////////////////////////////////////////////////// |
| 96 |
|
| 97 |
DEF_GM( return new LcdOverlapGM; ) |
| 98 |
} // namespace skiagm |