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 20 static const int kWidth = 750; 21 static const int kHeight = 750; 22 23 class LcdOverlapGM : public skiagm::GM { 24 public: LcdOverlapGM()25 LcdOverlapGM() { 26 const int kPointSize = 25; 27 fTextHeight = SkIntToScalar(kPointSize); 28 } 29 30 protected: onShortName()31 SkString onShortName() override { 32 return SkString("lcdoverlap"); 33 } 34 onOnceBeforeDraw()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); 47 fBlob.reset(builder.build()); 48 } 49 onISize()50 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); } 51 drawTestCase(SkCanvas * canvas,SkScalar x,SkScalar y,SkXfermode::Mode mode,SkXfermode::Mode mode2)52 void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkXfermode::Mode mode, 53 SkXfermode::Mode mode2) { 54 const SkColor colors[] { 55 SK_ColorRED, 56 SK_ColorGREEN, 57 SK_ColorBLUE, 58 SK_ColorYELLOW, 59 SK_ColorCYAN, 60 SK_ColorMAGENTA, 61 }; 62 63 SkAutoTUnref<SkXfermode> xfermode(SkXfermode::Create(mode)); 64 SkAutoTUnref<SkXfermode> xfermode2(SkXfermode::Create(mode2)); 65 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); i++) { 66 canvas->save(); 67 canvas->translate(x, y); 68 canvas->rotate(360.0f / SK_ARRAY_COUNT(colors) * i); 69 canvas->translate(-fBlob->bounds().width() / 2.0f + 0.5f, 0); 70 71 SkPaint textPaint; 72 textPaint.setColor(colors[i]); 73 textPaint.setXfermode(i % 2 == 0 ? xfermode : xfermode2); 74 canvas->drawTextBlob(fBlob, 0, 0, textPaint); 75 canvas->restore(); 76 } 77 } 78 onDraw(SkCanvas * canvas)79 void onDraw(SkCanvas* canvas) override { 80 SkScalar offsetX = kWidth / 4.0f; 81 SkScalar offsetY = kHeight / 4.0f; 82 drawTestCase(canvas, offsetX, offsetY, SkXfermode::kSrc_Mode, SkXfermode::kSrc_Mode); 83 drawTestCase(canvas, 3 * offsetX, offsetY, SkXfermode::kSrcOver_Mode, 84 SkXfermode::kSrcOver_Mode); 85 drawTestCase(canvas, offsetX, 3 * offsetY, SkXfermode::kHardLight_Mode, 86 SkXfermode::kLuminosity_Mode); 87 drawTestCase(canvas, 3 * offsetX, 3 * offsetY, SkXfermode::kSrcOver_Mode, 88 SkXfermode::kSrc_Mode); 89 } 90 91 private: 92 SkScalar fTextHeight; 93 SkAutoTUnref<const SkTextBlob> fBlob; 94 typedef skiagm::GM INHERITED; 95 }; 96 97 ////////////////////////////////////////////////////////////////////////////// 98 99 DEF_GM( return new LcdOverlapGM; ) 100 } 101