reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkCanvas.h" |
| 3 | #include "SkParsePath.h" |
| 4 | #include "SkPath.h" |
| 5 | #include "SkRandom.h" |
| 6 | #include "SkView.h" |
| 7 | |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 8 | #include "SkBlurMaskFilter.h" |
reed@android.com | 4913b77 | 2009-09-21 00:27:39 +0000 | [diff] [blame] | 9 | |
reed@android.com | 04d86c6 | 2010-01-25 22:02:44 +0000 | [diff] [blame] | 10 | static void test_huge_stroke(SkCanvas* canvas) { |
| 11 | SkRect srcR = { 0, 0, 72000, 54000 }; |
| 12 | SkRect dstR = { 0, 0, 640, 480 }; |
| 13 | |
| 14 | SkPath path; |
| 15 | path.moveTo(17600, 8000); |
| 16 | path.lineTo(52800, 8000); |
| 17 | path.lineTo(52800, 41600); |
| 18 | path.lineTo(17600, 41600); |
| 19 | path.close(); |
| 20 | |
| 21 | SkPaint paint; |
| 22 | paint.setAntiAlias(true); |
| 23 | paint.setStrokeWidth(8000); |
| 24 | paint.setStrokeMiter(10); |
| 25 | paint.setStrokeCap(SkPaint::kButt_Cap); |
| 26 | paint.setStrokeJoin(SkPaint::kRound_Join); |
| 27 | paint.setStyle(SkPaint::kStroke_Style); |
| 28 | |
| 29 | SkMatrix matrix; |
| 30 | matrix.setRectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit); |
| 31 | canvas->concat(matrix); |
| 32 | |
| 33 | canvas->drawPath(path, paint); |
| 34 | } |
| 35 | |
reed@android.com | 7ab2cf9 | 2009-09-21 16:01:32 +0000 | [diff] [blame] | 36 | #if 0 |
| 37 | #include "SkBlurMask.h" |
reed@android.com | 4913b77 | 2009-09-21 00:27:39 +0000 | [diff] [blame] | 38 | static void test_blur() { |
| 39 | uint8_t cell[9]; |
| 40 | memset(cell, 0xFF, sizeof(cell)); |
| 41 | SkMask src; |
| 42 | src.fImage = cell; |
| 43 | src.fFormat = SkMask::kA8_Format; |
| 44 | SkMask dst; |
| 45 | |
| 46 | for (int y = 1; y <= 3; y++) { |
| 47 | for (int x = 1; x <= 3; x++) { |
| 48 | src.fBounds.set(0, 0, x, y); |
| 49 | src.fRowBytes = src.fBounds.width(); |
| 50 | |
| 51 | SkScalar radius = 1.f; |
| 52 | |
| 53 | printf("src [%d %d %d %d] radius %g\n", src.fBounds.fLeft, src.fBounds.fTop, |
| 54 | src.fBounds.fRight, src.fBounds.fBottom, radius); |
| 55 | |
| 56 | SkBlurMask::Blur(&dst, src, radius, SkBlurMask::kNormal_Style); |
| 57 | uint8_t* dstPtr = dst.fImage; |
| 58 | |
| 59 | for (int y = 0; y < dst.fBounds.height(); y++) { |
| 60 | for (int x = 0; x < dst.fBounds.width(); x++) { |
| 61 | printf(" %02X", dstPtr[x]); |
| 62 | } |
| 63 | printf("\n"); |
| 64 | dstPtr += dst.fRowBytes; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
reed@android.com | 7ab2cf9 | 2009-09-21 16:01:32 +0000 | [diff] [blame] | 69 | #endif |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 70 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 71 | static void scale_to_width(SkPath* path, SkScalar dstWidth) { |
| 72 | const SkRect& bounds = path->getBounds(); |
| 73 | SkScalar scale = dstWidth / bounds.width(); |
| 74 | SkMatrix matrix; |
| 75 | |
| 76 | matrix.setScale(scale, scale); |
| 77 | path->transform(matrix); |
| 78 | } |
| 79 | |
| 80 | static const struct { |
| 81 | SkPaint::Style fStyle; |
| 82 | SkPaint::Join fJoin; |
| 83 | int fStrokeWidth; |
| 84 | } gRec[] = { |
| 85 | { SkPaint::kFill_Style, SkPaint::kMiter_Join, 0 }, |
| 86 | { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 0 }, |
| 87 | { SkPaint::kStroke_Style, SkPaint::kMiter_Join, 10 }, |
| 88 | { SkPaint::kStrokeAndFill_Style, SkPaint::kMiter_Join, 10 }, |
| 89 | }; |
| 90 | |
| 91 | class StrokePathView : public SkView { |
| 92 | SkScalar fWidth; |
| 93 | SkPath fPath; |
| 94 | public: |
| 95 | StrokePathView() { |
reed@android.com | 7ab2cf9 | 2009-09-21 16:01:32 +0000 | [diff] [blame] | 96 | // test_blur(); |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 97 | fWidth = SkIntToScalar(120); |
| 98 | |
| 99 | #if 0 |
| 100 | const char str[] = |
| 101 | "M 0, 3" |
| 102 | "C 10, -10, 30, -10, 0, 28" |
| 103 | "C -30, -10, -10, -10, 0, 3" |
| 104 | "Z"; |
| 105 | SkParsePath::FromSVGString(str, &fPath); |
| 106 | #else |
| 107 | fPath.addCircle(0, 0, SkIntToScalar(50), SkPath::kCW_Direction); |
| 108 | fPath.addCircle(0, SkIntToScalar(-50), SkIntToScalar(30), SkPath::kCW_Direction); |
| 109 | #endif |
| 110 | |
| 111 | scale_to_width(&fPath, fWidth); |
| 112 | const SkRect& bounds = fPath.getBounds(); |
| 113 | fPath.offset(-bounds.fLeft, -bounds.fTop); |
| 114 | } |
| 115 | |
| 116 | protected: |
| 117 | // overrides from SkEventSink |
| 118 | virtual bool onQuery(SkEvent* evt) { |
| 119 | if (SampleCode::TitleQ(*evt)) { |
| 120 | SampleCode::TitleR(evt, "StrokePath"); |
| 121 | return true; |
| 122 | } |
| 123 | return this->INHERITED::onQuery(evt); |
| 124 | } |
| 125 | |
| 126 | void drawBG(SkCanvas* canvas) { |
| 127 | canvas->drawColor(0xFFDDDDDD); |
| 128 | } |
| 129 | |
| 130 | SkRandom rand; |
| 131 | |
| 132 | void drawSet(SkCanvas* canvas, SkPaint* paint) { |
| 133 | SkAutoCanvasRestore acr(canvas, true); |
| 134 | |
| 135 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
| 136 | paint->setStyle(gRec[i].fStyle); |
| 137 | paint->setStrokeJoin(gRec[i].fJoin); |
| 138 | paint->setStrokeWidth(SkIntToScalar(gRec[i].fStrokeWidth)); |
| 139 | canvas->drawPath(fPath, *paint); |
| 140 | canvas->translate(fWidth * 5 / 4, 0); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | virtual void onDraw(SkCanvas* canvas) { |
| 145 | drawBG(canvas); |
reed@android.com | 4913b77 | 2009-09-21 00:27:39 +0000 | [diff] [blame] | 146 | //return; |
reed@android.com | 04d86c6 | 2010-01-25 22:02:44 +0000 | [diff] [blame] | 147 | test_huge_stroke(canvas); return; |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 148 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 149 | |
| 150 | SkPaint paint; |
| 151 | paint.setAntiAlias(true); |
reed@android.com | da449a3 | 2009-09-18 20:57:05 +0000 | [diff] [blame] | 152 | |
| 153 | if (true) { |
| 154 | canvas->drawColor(SK_ColorBLACK); |
| 155 | |
| 156 | paint.setTextSize(24); |
| 157 | paint.setColor(SK_ColorWHITE); |
| 158 | canvas->translate(10, 30); |
| 159 | |
| 160 | static const SkBlurMaskFilter::BlurStyle gStyle[] = { |
| 161 | SkBlurMaskFilter::kNormal_BlurStyle, |
| 162 | SkBlurMaskFilter::kInner_BlurStyle, |
| 163 | SkBlurMaskFilter::kOuter_BlurStyle, |
| 164 | SkBlurMaskFilter::kSolid_BlurStyle, |
| 165 | }; |
| 166 | for (int x = 0; x < 5; x++) { |
| 167 | SkMaskFilter* mf; |
| 168 | SkScalar radius = 4; |
| 169 | for (int y = 0; y < 10; y++) { |
| 170 | if (x) { |
| 171 | mf = SkBlurMaskFilter::Create(radius, gStyle[x - 1]); |
| 172 | paint.setMaskFilter(mf)->unref(); |
| 173 | } |
| 174 | canvas->drawText("Title Bar", 9, x*100, y*30, paint); |
| 175 | radius *= 0.75f; |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | return; |
| 180 | } |
| 181 | |
reed@android.com | ed881c2 | 2009-09-15 14:10:42 +0000 | [diff] [blame] | 182 | paint.setColor(SK_ColorBLUE); |
| 183 | |
| 184 | #if 1 |
| 185 | SkPath p; |
| 186 | float r = rand.nextUScalar1() + 0.5f; |
| 187 | SkScalar x = 0, y = 0; |
| 188 | p.moveTo(x, y); |
| 189 | #if 0 |
| 190 | p.cubicTo(x-75*r, y+75*r, x-40*r, y+125*r, x, y+85*r); |
| 191 | p.cubicTo(x+40*r, y+125*r, x+75*r, y+75*r, x, y); |
| 192 | #else |
| 193 | p.cubicTo(x+75*r, y+75*r, x+40*r, y+125*r, x, y+85*r); |
| 194 | p.cubicTo(x-40*r, y+125*r, x-75*r, y+75*r, x, y); |
| 195 | #endif |
| 196 | p.close(); |
| 197 | fPath = p; |
| 198 | fPath.offset(100, 0); |
| 199 | #endif |
| 200 | |
| 201 | fPath.setFillType(SkPath::kWinding_FillType); |
| 202 | drawSet(canvas, &paint); |
| 203 | |
| 204 | canvas->translate(0, fPath.getBounds().height() * 5 / 4); |
| 205 | fPath.setFillType(SkPath::kEvenOdd_FillType); |
| 206 | drawSet(canvas, &paint); |
| 207 | } |
| 208 | |
| 209 | virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { |
| 210 | this->inval(NULL); |
| 211 | return this->INHERITED::onFindClickHandler(x, y); |
| 212 | } |
| 213 | private: |
| 214 | typedef SkView INHERITED; |
| 215 | }; |
| 216 | |
| 217 | ////////////////////////////////////////////////////////////////////////////// |
| 218 | |
| 219 | static SkView* MyFactory() { return new StrokePathView; } |
| 220 | static SkViewRegister reg(MyFactory); |
| 221 | |