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" |
| 14 | #include "SkCanvas.h" |
| 15 | #include "SkSurface.h" |
| 16 | #include "SkTextBlob.h" |
| 17 | |
| 18 | namespace skiagm { |
| 19 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 20 | constexpr int kWidth = 750; |
| 21 | constexpr int kHeight = 750; |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 22 | |
| 23 | class LcdOverlapGM : public skiagm::GM { |
| 24 | public: |
| 25 | LcdOverlapGM() { |
| 26 | const int kPointSize = 25; |
| 27 | fTextHeight = SkIntToScalar(kPointSize); |
| 28 | } |
| 29 | |
| 30 | protected: |
| 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); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 47 | fBlob = builder.make(); |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | SkISize onISize() override { return SkISize::Make(kWidth, kHeight); } |
| 51 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 52 | void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode, |
| 53 | SkBlendMode mode2) { |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 54 | const SkColor colors[] { |
| 55 | SK_ColorRED, |
| 56 | SK_ColorGREEN, |
| 57 | SK_ColorBLUE, |
| 58 | SK_ColorYELLOW, |
| 59 | SK_ColorCYAN, |
| 60 | SK_ColorMAGENTA, |
| 61 | }; |
| 62 | |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 63 | for (size_t i = 0; i < SK_ARRAY_COUNT(colors); i++) { |
| 64 | canvas->save(); |
| 65 | canvas->translate(x, y); |
| 66 | canvas->rotate(360.0f / SK_ARRAY_COUNT(colors) * i); |
| 67 | canvas->translate(-fBlob->bounds().width() / 2.0f + 0.5f, 0); |
| 68 | |
| 69 | SkPaint textPaint; |
| 70 | textPaint.setColor(colors[i]); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 71 | textPaint.setBlendMode(i % 2 == 0 ? mode : mode2); |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 72 | canvas->drawTextBlob(fBlob, 0, 0, textPaint); |
| 73 | canvas->restore(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void onDraw(SkCanvas* canvas) override { |
| 78 | SkScalar offsetX = kWidth / 4.0f; |
| 79 | SkScalar offsetY = kHeight / 4.0f; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 80 | drawTestCase(canvas, offsetX, offsetY, SkBlendMode::kSrc, SkBlendMode::kSrc); |
| 81 | drawTestCase(canvas, 3 * offsetX, offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrcOver); |
| 82 | drawTestCase(canvas, offsetX, 3 * offsetY, SkBlendMode::kHardLight, |
| 83 | SkBlendMode::kLuminosity); |
| 84 | drawTestCase(canvas, 3 * offsetX, 3 * offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrc); |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private: |
| 88 | SkScalar fTextHeight; |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 89 | sk_sp<SkTextBlob> fBlob; |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 90 | typedef skiagm::GM INHERITED; |
| 91 | }; |
| 92 | |
| 93 | ////////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | DEF_GM( return new LcdOverlapGM; ) |
| 96 | } |