blob: 52bab03dd19fa3f160504b0cc15e7de7cbe13996 [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"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040023#include "tools/fonts/FontToolUtils.h"
joshualitt496d29f2015-09-18 12:03:13 -070024
25namespace skiagm {
26
mtkleindbfd7ab2016-09-01 11:24:54 -070027constexpr int kWidth = 750;
28constexpr int kHeight = 750;
joshualitt496d29f2015-09-18 12:03:13 -070029
30class LcdOverlapGM : public skiagm::GM {
31public:
32 LcdOverlapGM() {
33 const int kPointSize = 25;
34 fTextHeight = SkIntToScalar(kPointSize);
35 }
36
37protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000038 SkString getName() const override { return SkString("lcdoverlap"); }
joshualitt496d29f2015-09-18 12:03:13 -070039
40 void onOnceBeforeDraw() override {
41 // build text blob
42 SkTextBlobBuilder builder;
43
Kevin Lubicke836c3a2023-10-20 06:55:35 -040044 SkFont font(ToolUtils::DefaultPortableTypeface(), 32);
joshualitt496d29f2015-09-18 12:03:13 -070045 const char* text = "able was I ere I saw elba";
Mike Reed28bd8822018-12-22 22:29:45 -050046 font.setSubpixel(true);
47 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
Mike Kleinea3f0142019-03-20 11:12:10 -050048 ToolUtils::add_to_text_blob(&builder, text, font, 0, 0);
fmalita37283c22016-09-13 10:00:23 -070049 fBlob = builder.make();
joshualitt496d29f2015-09-18 12:03:13 -070050 }
51
Leandro Lovisolo8f023882023-08-15 21:13:52 +000052 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
joshualitt496d29f2015-09-18 12:03:13 -070053
reed374772b2016-10-05 17:33:02 -070054 void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode,
55 SkBlendMode mode2) {
joshualitt496d29f2015-09-18 12:03:13 -070056 const SkColor colors[] {
57 SK_ColorRED,
58 SK_ColorGREEN,
59 SK_ColorBLUE,
60 SK_ColorYELLOW,
61 SK_ColorCYAN,
62 SK_ColorMAGENTA,
63 };
64
Herb Derbyc37b3862022-06-21 09:49:17 -040065 for (size_t i = 0; i < std::size(colors); i++) {
joshualitt496d29f2015-09-18 12:03:13 -070066 canvas->save();
67 canvas->translate(x, y);
Herb Derbyc37b3862022-06-21 09:49:17 -040068 canvas->rotate(360.0f / std::size(colors) * i);
Ben Wagner12857d42020-09-25 14:17:30 -040069 canvas->translate(-fBlob->bounds().width() / 2.0f - fBlob->bounds().left() + 0.5f, 0);
joshualitt496d29f2015-09-18 12:03:13 -070070
71 SkPaint textPaint;
72 textPaint.setColor(colors[i]);
reed374772b2016-10-05 17:33:02 -070073 textPaint.setBlendMode(i % 2 == 0 ? mode : mode2);
joshualitt496d29f2015-09-18 12:03:13 -070074 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;
reed374772b2016-10-05 17:33:02 -070082 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);
joshualitt496d29f2015-09-18 12:03:13 -070087 }
88
89private:
90 SkScalar fTextHeight;
fmalita37283c22016-09-13 10:00:23 -070091 sk_sp<SkTextBlob> fBlob;
John Stiles7571f9e2020-09-02 22:42:33 -040092 using INHERITED = skiagm::GM;
joshualitt496d29f2015-09-18 12:03:13 -070093};
94
95//////////////////////////////////////////////////////////////////////////////
96
97DEF_GM( return new LcdOverlapGM; )
John Stilesa6841be2020-08-06 14:11:56 -040098} // namespace skiagm