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 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); 48 fBlob = builder.make(); 49 } 50 onISize()51 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); } 52 drawTestCase(SkCanvas * canvas,SkScalar x,SkScalar y,SkBlendMode mode,SkBlendMode mode2)53 void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode, 54 SkBlendMode mode2) { 55 const SkColor colors[] { 56 SK_ColorRED, 57 SK_ColorGREEN, 58 SK_ColorBLUE, 59 SK_ColorYELLOW, 60 SK_ColorCYAN, 61 SK_ColorMAGENTA, 62 }; 63 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]); 72 textPaint.setBlendMode(i % 2 == 0 ? mode : mode2); 73 canvas->drawTextBlob(fBlob, 0, 0, textPaint); 74 canvas->restore(); 75 } 76 } 77 onDraw(SkCanvas * canvas)78 void onDraw(SkCanvas* canvas) override { 79 SkScalar offsetX = kWidth / 4.0f; 80 SkScalar offsetY = kHeight / 4.0f; 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); 86 } 87 88 private: 89 SkScalar fTextHeight; 90 sk_sp<SkTextBlob> fBlob; 91 typedef skiagm::GM INHERITED; 92 }; 93 94 ////////////////////////////////////////////////////////////////////////////// 95 96 DEF_GM( return new LcdOverlapGM; ) 97 } 98