1 /* 2 * Copyright 2013 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/SkCanvas.h" 10 #include "include/core/SkColor.h" 11 #include "include/core/SkPaint.h" 12 #include "include/core/SkRect.h" 13 #include "include/core/SkSize.h" 14 #include "include/core/SkString.h" 15 #include "include/core/SkTypes.h" 16 17 namespace skiagm { 18 19 // Draw various width thin rects at 1/8 horizontal pixel increments 20 class ThinRectsGM : public GM { 21 public: ThinRectsGM()22 ThinRectsGM() { 23 this->setBGColor(0xFF000000); 24 } 25 26 protected: onShortName()27 SkString onShortName() override { 28 return SkString("thinrects"); 29 } 30 onISize()31 SkISize onISize() override { 32 return SkISize::Make(240, 320); 33 } 34 onDraw(SkCanvas * canvas)35 void onDraw(SkCanvas* canvas) override { 36 37 SkPaint white; 38 white.setColor(SK_ColorWHITE); 39 white.setAntiAlias(true); 40 41 SkPaint green; 42 green.setColor(SK_ColorGREEN); 43 green.setAntiAlias(true); 44 45 for (int i = 0; i < 8; ++i) { 46 canvas->save(); 47 canvas->translate(i*0.125f, i*40.0f); 48 DrawVertRects(canvas, white); 49 50 canvas->translate(40.0f, 0.0f); 51 DrawVertRects(canvas, green); 52 canvas->restore(); 53 54 canvas->save(); 55 canvas->translate(80.0f, i*40.0f + i*0.125f); 56 DrawHorizRects(canvas, white); 57 58 canvas->translate(40.0f, 0.0f); 59 DrawHorizRects(canvas, green); 60 canvas->restore(); 61 62 canvas->save(); 63 canvas->translate(160.0f + i*0.125f, 64 i*40.0f + i*0.125f); 65 DrawSquares(canvas, white); 66 67 canvas->translate(40.0f, 0.0f); 68 DrawSquares(canvas, green); 69 canvas->restore(); 70 } 71 } 72 73 private: DrawVertRects(SkCanvas * canvas,const SkPaint & p)74 static void DrawVertRects(SkCanvas* canvas, const SkPaint& p) { 75 constexpr SkRect vertRects[] = { 76 { 1, 1, 5.0f, 21 }, // 4 pix wide 77 { 8, 1, 10.0f, 21 }, // 2 pix wide 78 { 13, 1, 14.0f, 21 }, // 1 pix wide 79 { 17, 1, 17.5f, 21 }, // 1/2 pix wide 80 { 21, 1, 21.25f, 21 }, // 1/4 pix wide 81 { 25, 1, 25.125f, 21 }, // 1/8 pix wide 82 { 29, 1, 29.0f, 21 } // 0 pix wide 83 }; 84 85 for (size_t j = 0; j < SK_ARRAY_COUNT(vertRects); ++j) { 86 canvas->drawRect(vertRects[j], p); 87 } 88 } 89 DrawHorizRects(SkCanvas * canvas,const SkPaint & p)90 static void DrawHorizRects(SkCanvas* canvas, const SkPaint& p) { 91 constexpr SkRect horizRects[] = { 92 { 1, 1, 21, 5.0f }, // 4 pix high 93 { 1, 8, 21, 10.0f }, // 2 pix high 94 { 1, 13, 21, 14.0f }, // 1 pix high 95 { 1, 17, 21, 17.5f }, // 1/2 pix high 96 { 1, 21, 21, 21.25f }, // 1/4 pix high 97 { 1, 25, 21, 25.125f }, // 1/8 pix high 98 { 1, 29, 21, 29.0f } // 0 pix high 99 }; 100 101 for (size_t j = 0; j < SK_ARRAY_COUNT(horizRects); ++j) { 102 canvas->drawRect(horizRects[j], p); 103 } 104 } 105 DrawSquares(SkCanvas * canvas,const SkPaint & p)106 static void DrawSquares(SkCanvas* canvas, const SkPaint& p) { 107 constexpr SkRect squares[] = { 108 { 1, 1, 5.0f, 5.0f }, // 4 pix 109 { 8, 8, 10.0f, 10.0f }, // 2 pix 110 { 13, 13, 14.0f, 14.0f }, // 1 pix 111 { 17, 17, 17.5f, 17.5f }, // 1/2 pix 112 { 21, 21, 21.25f, 21.25f }, // 1/4 pix 113 { 25, 25, 25.125f, 25.125f }, // 1/8 pix 114 { 29, 29, 29.0f, 29.0f } // 0 pix 115 }; 116 117 for (size_t j = 0; j < SK_ARRAY_COUNT(squares); ++j) { 118 canvas->drawRect(squares[j], p); 119 } 120 } 121 122 typedef GM INHERITED; 123 }; 124 125 ////////////////////////////////////////////////////////////////////////////// 126 127 DEF_GM( return new ThinRectsGM; ) 128 129 } 130