blob: 6dd54a0b0104be477927c456bacad6a6afdc6ba4 [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
8
9/*
10 * Tests overlapping LCD text
11 */
12
13#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -040014#include "sk_tool_utils.h"
joshualitt496d29f2015-09-18 12:03:13 -070015#include "SkCanvas.h"
16#include "SkSurface.h"
17#include "SkTextBlob.h"
18
19namespace skiagm {
20
mtkleindbfd7ab2016-09-01 11:24:54 -070021constexpr int kWidth = 750;
22constexpr int kHeight = 750;
joshualitt496d29f2015-09-18 12:03:13 -070023
24class LcdOverlapGM : public skiagm::GM {
25public:
26 LcdOverlapGM() {
27 const int kPointSize = 25;
28 fTextHeight = SkIntToScalar(kPointSize);
29 }
30
31protected:
32 SkString onShortName() override {
33 return SkString("lcdoverlap");
34 }
35
36 void onOnceBeforeDraw() override {
37 // build text blob
38 SkTextBlobBuilder builder;
39
Mike Reed28bd8822018-12-22 22:29:45 -050040 SkFont font(sk_tool_utils::create_portable_typeface(), 32);
joshualitt496d29f2015-09-18 12:03:13 -070041 const char* text = "able was I ere I saw elba";
Mike Reed28bd8822018-12-22 22:29:45 -050042 font.setSubpixel(true);
43 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
44 // If we use SkTextBlob::MakeFromText, we get very different positioning ... why?
45 sk_tool_utils::add_to_text_blob(&builder, text, font, 0, 0);
fmalita37283c22016-09-13 10:00:23 -070046 fBlob = builder.make();
joshualitt496d29f2015-09-18 12:03:13 -070047 }
48
49 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
50
reed374772b2016-10-05 17:33:02 -070051 void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode,
52 SkBlendMode mode2) {
joshualitt496d29f2015-09-18 12:03:13 -070053 const SkColor colors[] {
54 SK_ColorRED,
55 SK_ColorGREEN,
56 SK_ColorBLUE,
57 SK_ColorYELLOW,
58 SK_ColorCYAN,
59 SK_ColorMAGENTA,
60 };
61
joshualitt496d29f2015-09-18 12:03:13 -070062 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); i++) {
63 canvas->save();
64 canvas->translate(x, y);
65 canvas->rotate(360.0f / SK_ARRAY_COUNT(colors) * i);
66 canvas->translate(-fBlob->bounds().width() / 2.0f + 0.5f, 0);
67
68 SkPaint textPaint;
69 textPaint.setColor(colors[i]);
reed374772b2016-10-05 17:33:02 -070070 textPaint.setBlendMode(i % 2 == 0 ? mode : mode2);
joshualitt496d29f2015-09-18 12:03:13 -070071 canvas->drawTextBlob(fBlob, 0, 0, textPaint);
72 canvas->restore();
73 }
74 }
75
76 void onDraw(SkCanvas* canvas) override {
77 SkScalar offsetX = kWidth / 4.0f;
78 SkScalar offsetY = kHeight / 4.0f;
reed374772b2016-10-05 17:33:02 -070079 drawTestCase(canvas, offsetX, offsetY, SkBlendMode::kSrc, SkBlendMode::kSrc);
80 drawTestCase(canvas, 3 * offsetX, offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrcOver);
81 drawTestCase(canvas, offsetX, 3 * offsetY, SkBlendMode::kHardLight,
82 SkBlendMode::kLuminosity);
83 drawTestCase(canvas, 3 * offsetX, 3 * offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrc);
joshualitt496d29f2015-09-18 12:03:13 -070084 }
85
86private:
87 SkScalar fTextHeight;
fmalita37283c22016-09-13 10:00:23 -070088 sk_sp<SkTextBlob> fBlob;
joshualitt496d29f2015-09-18 12:03:13 -070089 typedef skiagm::GM INHERITED;
90};
91
92//////////////////////////////////////////////////////////////////////////////
93
94DEF_GM( return new LcdOverlapGM; )
95}