| reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPath.h" |
| 11 | #include "SkTypeface.h" |
| 12 | |
| 13 | static void test10(SkCanvas* canvas) { |
| 14 | SkPaint paint; |
| 15 | const char text[] = "Hello"; // "Hello"; |
| 16 | const size_t len = sizeof(text) - 1; |
| 17 | paint.setAntiAlias(true); |
| 18 | paint.setTextSize(SkIntToScalar(100)); |
| 19 | SkTypeface* hira = SkTypeface::CreateFromName("Hiragino Maru Gothic Pro", |
| 20 | SkTypeface::kNormal); |
| 21 | paint.setTypeface(hira); |
| 22 | SkScalar x = 180; |
| 23 | SkScalar y = 88; |
| 24 | |
| 25 | canvas->drawText(text, len, x, y, paint); |
| 26 | paint.setFakeBoldText(true); |
| 27 | canvas->drawText(text, len, x, y + 100, paint); |
| 28 | paint.setStyle(SkPaint::kStrokeAndFill_Style); |
| 29 | paint.setStrokeWidth(5); |
| 30 | |
| 31 | SkPath path; |
| 32 | path.setFillType(SkPath::kWinding_FillType); |
| 33 | path.addCircle(x, y + 200, 50, SkPath::kCW_Direction); |
| 34 | path.addCircle(x, y + 200, 40, SkPath::kCCW_Direction); |
| 35 | canvas->drawPath(path, paint); |
| 36 | |
| 37 | SkPath path2; |
| 38 | path2.setFillType(SkPath::kWinding_FillType); |
| 39 | path2.addCircle(x + 120, y + 200, 50, SkPath::kCCW_Direction); |
| 40 | path2.addCircle(x + 120, y + 200, 40, SkPath::kCW_Direction); |
| 41 | canvas->drawPath(path2, paint); |
| 42 | |
| 43 | path2.reset(); |
| 44 | path2.addCircle(x + 240, y + 200, 50, SkPath::kCCW_Direction); |
| 45 | canvas->drawPath(path2, paint); |
| reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame^] | 46 | SkASSERT(path2.cheapIsDirection(SkPath::kCCW_Direction)); |
| reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 47 | |
| 48 | path2.reset(); |
| reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame^] | 49 | SkASSERT(!path2.cheapComputeDirection(NULL)); |
| reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 50 | path2.addCircle(x + 360, y + 200, 50, SkPath::kCW_Direction); |
| reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame^] | 51 | SkASSERT(path2.cheapIsDirection(SkPath::kCW_Direction)); |
| reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 52 | canvas->drawPath(path2, paint); |
| 53 | } |
| 54 | |
| 55 | static void test_path(SkCanvas* canvas, const SkPath& path) { |
| 56 | SkPaint paint; |
| 57 | paint.setAntiAlias(true); |
| 58 | canvas->drawPath(path, paint); |
| 59 | |
| 60 | paint.setStyle(SkPaint::kStroke_Style); |
| 61 | paint.setColor(SK_ColorRED); |
| 62 | canvas->drawPath(path, paint); |
| 63 | } |
| 64 | |
| 65 | static void test_rev(SkCanvas* canvas, const SkPath& path) { |
| 66 | test_path(canvas, path); |
| 67 | |
| 68 | SkPath rev; |
| 69 | rev.reverseAddPath(path); |
| 70 | canvas->save(); |
| 71 | canvas->translate(150, 0); |
| 72 | test_path(canvas, rev); |
| 73 | canvas->restore(); |
| 74 | } |
| 75 | |
| 76 | static void test_rev(SkCanvas* canvas) { |
| 77 | SkRect r = { 10, 10, 100, 60 }; |
| 78 | |
| 79 | SkPath path; |
| 80 | |
| 81 | path.addRect(r); test_rev(canvas, path); |
| 82 | |
| 83 | canvas->translate(0, 100); |
| 84 | path.offset(20, 20); |
| 85 | path.addRect(r); test_rev(canvas, path); |
| 86 | |
| 87 | canvas->translate(0, 100); |
| 88 | path.reset(); |
| 89 | path.moveTo(10, 10); path.lineTo(30, 30); |
| 90 | path.addOval(r); |
| 91 | r.offset(50, 20); |
| 92 | path.addOval(r); |
| 93 | test_rev(canvas, path); |
| 94 | |
| 95 | SkPaint paint; |
| 96 | paint.setTextSize(SkIntToScalar(100)); |
| 97 | SkTypeface* hira = SkTypeface::CreateFromName("Hiragino Maru Gothic Pro", SkTypeface::kNormal); |
| 98 | SkSafeUnref(paint.setTypeface(hira)); |
| 99 | path.reset(); |
| 100 | paint.getTextPath("e", 1, 50, 50, &path); |
| 101 | canvas->translate(0, 100); |
| 102 | test_rev(canvas, path); |
| 103 | } |
| 104 | |
| 105 | namespace skiagm { |
| 106 | |
| 107 | class StrokeFillGM : public GM { |
| 108 | public: |
| 109 | StrokeFillGM() { |
| 110 | |
| 111 | } |
| 112 | |
| 113 | protected: |
| 114 | virtual SkString onShortName() { |
| 115 | return SkString("stroke-fill"); |
| 116 | } |
| 117 | |
| 118 | virtual SkISize onISize() { |
| 119 | return make_isize(640, 480); |
| 120 | } |
| 121 | |
| 122 | virtual void onDraw(SkCanvas* canvas) { |
| reed@google.com | 69a9943 | 2012-01-10 18:00:10 +0000 | [diff] [blame^] | 123 | test10(canvas); |
| 124 | // test_rev(canvas); |
| reed@google.com | 63d7374 | 2012-01-10 15:33:12 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | private: |
| 128 | typedef GM INHERITED; |
| 129 | }; |
| 130 | |
| 131 | ////////////////////////////////////////////////////////////////////////////// |
| 132 | |
| 133 | static GM* MyFactory(void*) { return new StrokeFillGM; } |
| 134 | static GMRegistry reg(MyFactory); |
| 135 | |
| 136 | } |