1 /*
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 "sk_tool_utils.h"
15 #include "SkCanvas.h"
16 #include "SkSurface.h"
17 #include "SkTextBlob.h"
18 
19 namespace skiagm {
20 
21 constexpr int kWidth = 750;
22 constexpr int kHeight = 750;
23 
24 class LcdOverlapGM : public skiagm::GM {
25 public:
LcdOverlapGM()26     LcdOverlapGM() {
27         const int kPointSize = 25;
28         fTextHeight = SkIntToScalar(kPointSize);
29     }
30 
31 protected:
onShortName()32     SkString onShortName() override {
33         return SkString("lcdoverlap");
34     }
35 
onOnceBeforeDraw()36     void onOnceBeforeDraw() override {
37         // build text blob
38         SkTextBlobBuilder builder;
39 
40         SkFont font(sk_tool_utils::create_portable_typeface(), 32);
41         const char* text = "able was I ere I saw elba";
42         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);
46         fBlob = builder.make();
47     }
48 
onISize()49     SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
50 
drawTestCase(SkCanvas * canvas,SkScalar x,SkScalar y,SkBlendMode mode,SkBlendMode mode2)51     void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode,
52                       SkBlendMode mode2) {
53         const SkColor colors[] {
54                 SK_ColorRED,
55                 SK_ColorGREEN,
56                 SK_ColorBLUE,
57                 SK_ColorYELLOW,
58                 SK_ColorCYAN,
59                 SK_ColorMAGENTA,
60         };
61 
62         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]);
70             textPaint.setBlendMode(i % 2 == 0 ? mode : mode2);
71             canvas->drawTextBlob(fBlob, 0, 0, textPaint);
72             canvas->restore();
73         }
74     }
75 
onDraw(SkCanvas * canvas)76     void onDraw(SkCanvas* canvas) override {
77         SkScalar offsetX = kWidth / 4.0f;
78         SkScalar offsetY = kHeight / 4.0f;
79         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);
84     }
85 
86 private:
87     SkScalar fTextHeight;
88     sk_sp<SkTextBlob> fBlob;
89     typedef skiagm::GM INHERITED;
90 };
91 
92 //////////////////////////////////////////////////////////////////////////////
93 
94 DEF_GM( return new LcdOverlapGM; )
95 }
96