blob: 8a91efa263bf9899d647b0a5fdd67ca1b3a89a04 [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);
49 // If we use SkTextBlob::MakeFromText, we get very different positioning ... why?
Mike Kleinea3f0142019-03-20 11:12:10 -050050 ToolUtils::add_to_text_blob(&builder, text, font, 0, 0);
fmalita37283c22016-09-13 10:00:23 -070051 fBlob = builder.make();
joshualitt496d29f2015-09-18 12:03:13 -070052 }
53
54 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
55
reed374772b2016-10-05 17:33:02 -070056 void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode,
57 SkBlendMode mode2) {
joshualitt496d29f2015-09-18 12:03:13 -070058 const SkColor colors[] {
59 SK_ColorRED,
60 SK_ColorGREEN,
61 SK_ColorBLUE,
62 SK_ColorYELLOW,
63 SK_ColorCYAN,
64 SK_ColorMAGENTA,
65 };
66
joshualitt496d29f2015-09-18 12:03:13 -070067 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); i++) {
68 canvas->save();
69 canvas->translate(x, y);
70 canvas->rotate(360.0f / SK_ARRAY_COUNT(colors) * i);
71 canvas->translate(-fBlob->bounds().width() / 2.0f + 0.5f, 0);
72
73 SkPaint textPaint;
74 textPaint.setColor(colors[i]);
reed374772b2016-10-05 17:33:02 -070075 textPaint.setBlendMode(i % 2 == 0 ? mode : mode2);
joshualitt496d29f2015-09-18 12:03:13 -070076 canvas->drawTextBlob(fBlob, 0, 0, textPaint);
77 canvas->restore();
78 }
79 }
80
81 void onDraw(SkCanvas* canvas) override {
82 SkScalar offsetX = kWidth / 4.0f;
83 SkScalar offsetY = kHeight / 4.0f;
reed374772b2016-10-05 17:33:02 -070084 drawTestCase(canvas, offsetX, offsetY, SkBlendMode::kSrc, SkBlendMode::kSrc);
85 drawTestCase(canvas, 3 * offsetX, offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrcOver);
86 drawTestCase(canvas, offsetX, 3 * offsetY, SkBlendMode::kHardLight,
87 SkBlendMode::kLuminosity);
88 drawTestCase(canvas, 3 * offsetX, 3 * offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrc);
joshualitt496d29f2015-09-18 12:03:13 -070089 }
90
91private:
92 SkScalar fTextHeight;
fmalita37283c22016-09-13 10:00:23 -070093 sk_sp<SkTextBlob> fBlob;
joshualitt496d29f2015-09-18 12:03:13 -070094 typedef skiagm::GM INHERITED;
95};
96
97//////////////////////////////////////////////////////////////////////////////
98
99DEF_GM( return new LcdOverlapGM; )
100}