joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 14 | #include "sk_tool_utils.h" |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 15 | #include "SkCanvas.h" |
| 16 | #include "SkSurface.h" |
| 17 | #include "SkTextBlob.h" |
| 18 | |
| 19 | namespace skiagm { |
| 20 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 21 | constexpr int kWidth = 750; |
| 22 | constexpr int kHeight = 750; |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 23 | |
| 24 | class LcdOverlapGM : public skiagm::GM { |
| 25 | public: |
| 26 | LcdOverlapGM() { |
| 27 | const int kPointSize = 25; |
| 28 | fTextHeight = SkIntToScalar(kPointSize); |
| 29 | } |
| 30 | |
| 31 | protected: |
| 32 | SkString onShortName() override { |
| 33 | return SkString("lcdoverlap"); |
| 34 | } |
| 35 | |
| 36 | void onOnceBeforeDraw() override { |
| 37 | // build text blob |
| 38 | SkTextBlobBuilder builder; |
| 39 | |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 40 | SkFont font(sk_tool_utils::create_portable_typeface(), 32); |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 41 | const char* text = "able was I ere I saw elba"; |
Mike Reed | 28bd882 | 2018-12-22 22:29:45 -0500 | [diff] [blame] | 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); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 46 | fBlob = builder.make(); |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | SkISize onISize() override { return SkISize::Make(kWidth, kHeight); } |
| 50 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 51 | void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode, |
| 52 | SkBlendMode mode2) { |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 53 | const SkColor colors[] { |
| 54 | SK_ColorRED, |
| 55 | SK_ColorGREEN, |
| 56 | SK_ColorBLUE, |
| 57 | SK_ColorYELLOW, |
| 58 | SK_ColorCYAN, |
| 59 | SK_ColorMAGENTA, |
| 60 | }; |
| 61 | |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 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]); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 70 | textPaint.setBlendMode(i % 2 == 0 ? mode : mode2); |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 71 | 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; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 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); |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private: |
| 87 | SkScalar fTextHeight; |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 88 | sk_sp<SkTextBlob> fBlob; |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 89 | typedef skiagm::GM INHERITED; |
| 90 | }; |
| 91 | |
| 92 | ////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | DEF_GM( return new LcdOverlapGM; ) |
| 95 | } |