blob: aae27d8c1aa5a3e42c7225091174504bc096d44d [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"
14#include "SkCanvas.h"
15#include "SkSurface.h"
16#include "SkTextBlob.h"
17
18namespace skiagm {
19
20static const int kWidth = 750;
21static const int kHeight = 750;
22
23class LcdOverlapGM : public skiagm::GM {
24public:
25 LcdOverlapGM() {
26 const int kPointSize = 25;
27 fTextHeight = SkIntToScalar(kPointSize);
28 }
29
30protected:
31 SkString onShortName() override {
32 return SkString("lcdoverlap");
33 }
34
35 void onOnceBeforeDraw() override {
36 // build text blob
37 SkTextBlobBuilder builder;
38
39 SkPaint paint;
40 sk_tool_utils::set_portable_typeface(&paint);
41 paint.setTextSize(32);
42 const char* text = "able was I ere I saw elba";
43 paint.setAntiAlias(true);
44 paint.setSubpixelText(true);
45 paint.setLCDRenderText(true);
46 sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0);
47 fBlob.reset(builder.build());
48 }
49
50 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
51
52 void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkXfermode::Mode mode,
53 SkXfermode::Mode mode2) {
54 const SkColor colors[] {
55 SK_ColorRED,
56 SK_ColorGREEN,
57 SK_ColorBLUE,
58 SK_ColorYELLOW,
59 SK_ColorCYAN,
60 SK_ColorMAGENTA,
61 };
62
reedcfb6bdf2016-03-29 11:32:50 -070063 sk_sp<SkXfermode> xfermode(SkXfermode::Make(mode));
64 sk_sp<SkXfermode> xfermode2(SkXfermode::Make(mode2));
joshualitt496d29f2015-09-18 12:03:13 -070065 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); i++) {
66 canvas->save();
67 canvas->translate(x, y);
68 canvas->rotate(360.0f / SK_ARRAY_COUNT(colors) * i);
69 canvas->translate(-fBlob->bounds().width() / 2.0f + 0.5f, 0);
70
71 SkPaint textPaint;
72 textPaint.setColor(colors[i]);
73 textPaint.setXfermode(i % 2 == 0 ? xfermode : xfermode2);
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, SkXfermode::kSrc_Mode, SkXfermode::kSrc_Mode);
83 drawTestCase(canvas, 3 * offsetX, offsetY, SkXfermode::kSrcOver_Mode,
84 SkXfermode::kSrcOver_Mode);
85 drawTestCase(canvas, offsetX, 3 * offsetY, SkXfermode::kHardLight_Mode,
86 SkXfermode::kLuminosity_Mode);
87 drawTestCase(canvas, 3 * offsetX, 3 * offsetY, SkXfermode::kSrcOver_Mode,
88 SkXfermode::kSrc_Mode);
89 }
90
91private:
92 SkScalar fTextHeight;
93 SkAutoTUnref<const SkTextBlob> fBlob;
94 typedef skiagm::GM INHERITED;
95};
96
97//////////////////////////////////////////////////////////////////////////////
98
99DEF_GM( return new LcdOverlapGM; )
100}