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 #include "gm/gm.h" 9 #include "include/core/SkBlendMode.h" 10 #include "include/core/SkCanvas.h" 11 #include "include/core/SkColor.h" 12 #include "include/core/SkFont.h" 13 #include "include/core/SkPaint.h" 14 #include "include/core/SkRect.h" 15 #include "include/core/SkRefCnt.h" 16 #include "include/core/SkScalar.h" 17 #include "include/core/SkSize.h" 18 #include "include/core/SkString.h" 19 #include "include/core/SkTextBlob.h" 20 #include "include/core/SkTypeface.h" 21 #include "include/core/SkTypes.h" 22 #include "tools/ToolUtils.h" 23 24 namespace skiagm { 25 26 constexpr int kWidth = 750; 27 constexpr int kHeight = 750; 28 29 class LcdOverlapGM : public skiagm::GM { 30 public: LcdOverlapGM()31 LcdOverlapGM() { 32 const int kPointSize = 25; 33 fTextHeight = SkIntToScalar(kPointSize); 34 } 35 36 protected: onShortName()37 SkString onShortName() override { 38 return SkString("lcdoverlap"); 39 } 40 onOnceBeforeDraw()41 void onOnceBeforeDraw() override { 42 // build text blob 43 SkTextBlobBuilder builder; 44 45 SkFont font(ToolUtils::create_portable_typeface(), 32); 46 const char* text = "able was I ere I saw elba"; 47 font.setSubpixel(true); 48 font.setEdging(SkFont::Edging::kSubpixelAntiAlias); 49 // If we use SkTextBlob::MakeFromText, we get very different positioning ... why? 50 ToolUtils::add_to_text_blob(&builder, text, font, 0, 0); 51 fBlob = builder.make(); 52 } 53 onISize()54 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); } 55 drawTestCase(SkCanvas * canvas,SkScalar x,SkScalar y,SkBlendMode mode,SkBlendMode mode2)56 void drawTestCase(SkCanvas* canvas, SkScalar x, SkScalar y, SkBlendMode mode, 57 SkBlendMode mode2) { 58 const SkColor colors[] { 59 SK_ColorRED, 60 SK_ColorGREEN, 61 SK_ColorBLUE, 62 SK_ColorYELLOW, 63 SK_ColorCYAN, 64 SK_ColorMAGENTA, 65 }; 66 67 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); i++) { 68 canvas->save(); 69 canvas->translate(x, y); 70 canvas->rotate(360.0f / SK_ARRAY_COUNT(colors) * i); 71 canvas->translate(-fBlob->bounds().width() / 2.0f + 0.5f, 0); 72 73 SkPaint textPaint; 74 textPaint.setColor(colors[i]); 75 textPaint.setBlendMode(i % 2 == 0 ? mode : mode2); 76 canvas->drawTextBlob(fBlob, 0, 0, textPaint); 77 canvas->restore(); 78 } 79 } 80 onDraw(SkCanvas * canvas)81 void onDraw(SkCanvas* canvas) override { 82 SkScalar offsetX = kWidth / 4.0f; 83 SkScalar offsetY = kHeight / 4.0f; 84 drawTestCase(canvas, offsetX, offsetY, SkBlendMode::kSrc, SkBlendMode::kSrc); 85 drawTestCase(canvas, 3 * offsetX, offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrcOver); 86 drawTestCase(canvas, offsetX, 3 * offsetY, SkBlendMode::kHardLight, 87 SkBlendMode::kLuminosity); 88 drawTestCase(canvas, 3 * offsetX, 3 * offsetY, SkBlendMode::kSrcOver, SkBlendMode::kSrc); 89 } 90 91 private: 92 SkScalar fTextHeight; 93 sk_sp<SkTextBlob> fBlob; 94 typedef skiagm::GM INHERITED; 95 }; 96 97 ////////////////////////////////////////////////////////////////////////////// 98 99 DEF_GM( return new LcdOverlapGM; ) 100 } 101