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 | |
| 40 | SkPaint paint; |
| 41 | sk_tool_utils::set_portable_typeface(&paint); |
| 42 | paint.setTextSize(32); |
| 43 | const char* text = "able was I ere I saw elba"; |
| 44 | paint.setAntiAlias(true); |
| 45 | paint.setSubpixelText(true); |
| 46 | paint.setLCDRenderText(true); |
| 47 | sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 48 | fBlob = builder.make(); |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | SkISize onISize() override { return SkISize::Make(kWidth, kHeight); } |
| 52 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 53 | void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode, |
| 54 | SkBlendMode mode2) { |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 55 | const SkColor colors[] { |
| 56 | SK_ColorRED, |
| 57 | SK_ColorGREEN, |
| 58 | SK_ColorBLUE, |
| 59 | SK_ColorYELLOW, |
| 60 | SK_ColorCYAN, |
| 61 | SK_ColorMAGENTA, |
| 62 | }; |
| 63 | |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 64 | for (size_t i = 0; i < SK_ARRAY_COUNT(colors); i++) { |
| 65 | canvas->save(); |
| 66 | canvas->translate(x, y); |
| 67 | canvas->rotate(360.0f / SK_ARRAY_COUNT(colors) * i); |
| 68 | canvas->translate(-fBlob->bounds().width() / 2.0f + 0.5f, 0); |
| 69 | |
| 70 | SkPaint textPaint; |
| 71 | textPaint.setColor(colors[i]); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 72 | textPaint.setBlendMode(i % 2 == 0 ? mode : mode2); |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 73 | canvas->drawTextBlob(fBlob, 0, 0, textPaint); |
| 74 | canvas->restore(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void onDraw(SkCanvas* canvas) override { |
| 79 | SkScalar offsetX = kWidth / 4.0f; |
| 80 | SkScalar offsetY = kHeight / 4.0f; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 81 | drawTestCase(canvas, offsetX, offsetY, SkBlendMode::kSrc, SkBlendMode::kSrc); |
| 82 | drawTestCase(canvas, 3 * offsetX, offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrcOver); |
| 83 | drawTestCase(canvas, offsetX, 3 * offsetY, SkBlendMode::kHardLight, |
| 84 | SkBlendMode::kLuminosity); |
| 85 | drawTestCase(canvas, 3 * offsetX, 3 * offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrc); |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | private: |
| 89 | SkScalar fTextHeight; |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 90 | sk_sp<SkTextBlob> fBlob; |
joshualitt | 496d29f | 2015-09-18 12:03:13 -0700 | [diff] [blame] | 91 | typedef skiagm::GM INHERITED; |
| 92 | }; |
| 93 | |
| 94 | ////////////////////////////////////////////////////////////////////////////// |
| 95 | |
| 96 | DEF_GM( return new LcdOverlapGM; ) |
| 97 | } |