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/SkCanvas.h" 10 #include "include/core/SkPaint.h" 11 #include "include/core/SkPath.h" 12 #include "include/core/SkRect.h" 13 #include "include/core/SkScalar.h" 14 #include "include/core/SkSize.h" 15 #include "include/core/SkString.h" 16 17 class ScaledStrokesGM : public skiagm::GM { 18 public: ScaledStrokesGM()19 ScaledStrokesGM() {} 20 21 protected: 22 onShortName()23 SkString onShortName() override { 24 return SkString("scaledstrokes"); 25 } 26 onISize()27 SkISize onISize() override { 28 return SkISize::Make(640, 320); 29 } 30 draw_path(SkScalar size,SkCanvas * canvas,SkPaint paint)31 static void draw_path(SkScalar size, SkCanvas* canvas, SkPaint paint) { 32 SkScalar c = 0.551915024494f * size; 33 SkPath path; 34 path.moveTo(0.0f, size); 35 path.cubicTo(c, size, size, c, size, 0.0f); 36 path.cubicTo(size, -c, c, -size, 0.0f, -size); 37 path.cubicTo(-c, -size, -size, -c, -size, 0.0f); 38 path.cubicTo(-size, c, -c, size, 0.0f, size); 39 canvas->drawPath(path, paint); 40 } 41 onDraw(SkCanvas * canvas)42 void onDraw(SkCanvas* canvas) override { 43 SkPaint paint; 44 SkPath path; 45 paint.setStyle(SkPaint::Style::kStroke_Style); 46 canvas->translate(5.0f, 5.0f); 47 const SkScalar size = 60.0f; 48 for (int i = 0; i < 2; i++) { 49 paint.setAntiAlias(i == 1); 50 for (int j = 0; j < 4; j++) { 51 SkScalar scale = 4.0f - j; 52 paint.setStrokeWidth(4.0f / scale); 53 canvas->save(); 54 canvas->translate(size / 2.0f, size / 2.0f); 55 canvas->scale(scale, scale); 56 draw_path(size / 2.0f / scale, canvas, paint); 57 canvas->restore(); 58 59 canvas->save(); 60 canvas->translate(size / 2.0f, 80.0f + size / 2.0f); 61 canvas->scale(scale, scale); 62 canvas->drawCircle(0.0f, 0.0f, size / 2.0f / scale, paint); 63 canvas->restore(); 64 65 canvas->save(); 66 canvas->translate(0.0f, 160.0f); 67 canvas->scale(scale, scale); 68 canvas->drawRect(SkRect::MakeXYWH(0.0f, 0.0f, size / scale, size / scale), paint); 69 canvas->restore(); 70 71 canvas->save(); 72 canvas->translate(0.0f, 240.0f); 73 canvas->scale(scale, scale); 74 canvas->drawLine(0.0f, 0.0f, size / scale, size / scale, paint); 75 canvas->restore(); 76 77 canvas->translate(80.0f, 0.0f); 78 } 79 } 80 81 } 82 83 private: 84 typedef GM INHERITED; 85 }; 86 87 DEF_GM( return new ScaledStrokesGM; ) 88