blob: 9cfcbb10312cf19bd004d5ed9791f8c8fa5f9bc0 [file] [log] [blame]
joshualitt496d29f2015-09-18 12:03:13 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkTextBlob.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/core/SkTypeface.h"
21#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "tools/ToolUtils.h"
joshualitt496d29f2015-09-18 12:03:13 -070023
24namespace skiagm {
25
mtkleindbfd7ab2016-09-01 11:24:54 -070026constexpr int kWidth = 750;
27constexpr int kHeight = 750;
joshualitt496d29f2015-09-18 12:03:13 -070028
29class LcdOverlapGM : public skiagm::GM {
30public:
31 LcdOverlapGM() {
32 const int kPointSize = 25;
33 fTextHeight = SkIntToScalar(kPointSize);
34 }
35
36protected:
37 SkString onShortName() override {
38 return SkString("lcdoverlap");
39 }
40
41 void onOnceBeforeDraw() override {
42 // build text blob
43 SkTextBlobBuilder builder;
44
Mike Kleinea3f0142019-03-20 11:12:10 -050045 SkFont font(ToolUtils::create_portable_typeface(), 32);
joshualitt496d29f2015-09-18 12:03:13 -070046 const char* text = "able was I ere I saw elba";
Mike Reed28bd8822018-12-22 22:29:45 -050047 font.setSubpixel(true);
48 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
Mike Kleinea3f0142019-03-20 11:12:10 -050049 ToolUtils::add_to_text_blob(&builder, text, font, 0, 0);
fmalita37283c22016-09-13 10:00:23 -070050 fBlob = builder.make();
joshualitt496d29f2015-09-18 12:03:13 -070051 }
52
53 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
54
reed374772b2016-10-05 17:33:02 -070055 void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode,
56 SkBlendMode mode2) {
joshualitt496d29f2015-09-18 12:03:13 -070057 const SkColor colors[] {
58 SK_ColorRED,
59 SK_ColorGREEN,
60 SK_ColorBLUE,
61 SK_ColorYELLOW,
62 SK_ColorCYAN,
63 SK_ColorMAGENTA,
64 };
65
joshualitt496d29f2015-09-18 12:03:13 -070066 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); i++) {
67 canvas->save();
68 canvas->translate(x, y);
69 canvas->rotate(360.0f / SK_ARRAY_COUNT(colors) * i);
Ben Wagner12857d42020-09-25 14:17:30 -040070 canvas->translate(-fBlob->bounds().width() / 2.0f - fBlob->bounds().left() + 0.5f, 0);
joshualitt496d29f2015-09-18 12:03:13 -070071
72 SkPaint textPaint;
73 textPaint.setColor(colors[i]);
reed374772b2016-10-05 17:33:02 -070074 textPaint.setBlendMode(i % 2 == 0 ? mode : mode2);
joshualitt496d29f2015-09-18 12:03:13 -070075 canvas->drawTextBlob(fBlob, 0, 0, textPaint);
76 canvas->restore();
77 }
78 }
79
80 void onDraw(SkCanvas* canvas) override {
81 SkScalar offsetX = kWidth / 4.0f;
82 SkScalar offsetY = kHeight / 4.0f;
reed374772b2016-10-05 17:33:02 -070083 drawTestCase(canvas, offsetX, offsetY, SkBlendMode::kSrc, SkBlendMode::kSrc);
84 drawTestCase(canvas, 3 * offsetX, offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrcOver);
85 drawTestCase(canvas, offsetX, 3 * offsetY, SkBlendMode::kHardLight,
86 SkBlendMode::kLuminosity);
87 drawTestCase(canvas, 3 * offsetX, 3 * offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrc);
joshualitt496d29f2015-09-18 12:03:13 -070088 }
89
90private:
91 SkScalar fTextHeight;
fmalita37283c22016-09-13 10:00:23 -070092 sk_sp<SkTextBlob> fBlob;
John Stiles7571f9e2020-09-02 22:42:33 -040093 using INHERITED = skiagm::GM;
joshualitt496d29f2015-09-18 12:03:13 -070094};
95
96//////////////////////////////////////////////////////////////////////////////
97
98DEF_GM( return new LcdOverlapGM; )
John Stilesa6841be2020-08-06 14:11:56 -040099} // namespace skiagm