2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "sk_tool_utils.h"
9 #include "SampleCode.h"
17 #include "SkSurface.h"
18 #include "SkClipOpPriv.h"
20 #define FAT_PIXEL_COLOR SK_ColorBLACK
21 #define PIXEL_CENTER_SIZE 3
22 #define WIRE_FRAME_COLOR 0xFFFF0000 /*0xFF00FFFF*/
23 #define WIRE_FRAME_SIZE 1.5f
25 static SkScalar apply_grid(SkScalar x) {
26 const SkScalar grid = 2;
27 return SkScalarRoundToScalar(x * grid) / grid;
30 static void apply_grid(SkPoint pts[], int count) {
31 for (int i = 0; i < count; ++i) {
32 pts[i].set(apply_grid(pts[i].fX), apply_grid(pts[i].fY));
36 static void erase(SkSurface* surface) {
37 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
50 fStrokeCap = SkPaint::kButt_Cap;
52 fClipRect.set(2, 2, 11, 8 );
55 int getZoom() const { return fZoom; }
57 bool getAA() const { return fAA; }
58 void setAA(bool aa) { fAA = aa; }
60 bool getGrid() const { return fGrid; }
61 void setGrid(bool g) { fGrid = g; }
63 bool getShowSkeleton() const { return fShowSkeleton; }
64 void setShowSkeleton(bool ss) { fShowSkeleton = ss; }
66 bool getTriangle() const { return fUseTriangle; }
67 void setTriangle(bool ut) { fUseTriangle = ut; }
69 void toggleRectAsOval() { fRectAsOval = !fRectAsOval; }
71 void togglePixelColors() {
72 if (fShader == fShader0) {
79 bool getUseClip() const { return fUseClip; }
80 void setUseClip(bool uc) { fUseClip = uc; }
86 Style getStyle() const { return fStyle; }
87 void setStyle(Style s) { fStyle = s; }
89 void setWHZ(int width, int height, int zoom) {
93 fBounds.set(0, 0, SkIntToScalar(width * zoom), SkIntToScalar(height * zoom));
94 fMatrix.setScale(SkIntToScalar(zoom), SkIntToScalar(zoom));
95 fInverse.setScale(SK_Scalar1 / zoom, SK_Scalar1 / zoom);
96 fShader0 = sk_tool_utils::create_checkerboard_shader(0xFFDDDDDD, 0xFFFFFFFF, zoom);
97 fShader1 = SkShader::MakeColorShader(SK_ColorWHITE);
100 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
101 fMinSurface = SkSurface::MakeRaster(info);
102 info = info.makeWH(width * zoom, height * zoom);
103 fMaxSurface = SkSurface::MakeRaster(info);
106 void drawBG(SkCanvas*);
107 void drawFG(SkCanvas*);
108 void drawLine(SkCanvas*, SkPoint pts[2]);
109 void drawRect(SkCanvas* canvas, SkPoint pts[2]);
110 void drawTriangle(SkCanvas* canvas, SkPoint pts[3]);
112 SkPaint::Cap fStrokeCap;
115 bool fAA, fGrid, fShowSkeleton, fUseClip, fRectAsOval, fUseTriangle;
118 SkMatrix fMatrix, fInverse;
119 SkRect fBounds, fClipRect;
120 sk_sp<SkShader> fShader0;
121 sk_sp<SkShader> fShader1;
122 sk_sp<SkShader> fShader;
123 sk_sp<SkSurface> fMinSurface;
124 sk_sp<SkSurface> fMaxSurface;
126 void setupPaint(SkPaint* paint) {
127 bool aa = this->getAA();
128 paint->setStrokeCap(fStrokeCap);
131 paint->setStrokeWidth(0);
134 paint->setStrokeWidth(SK_Scalar1);
137 paint->setAntiAlias(aa);
140 void setupSkeletonPaint(SkPaint* paint) {
141 paint->setStyle(SkPaint::kStroke_Style);
142 paint->setStrokeWidth(WIRE_FRAME_SIZE);
143 paint->setColor(fShowSkeleton ? WIRE_FRAME_COLOR : 0);
144 paint->setAntiAlias(true);
147 void drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]);
148 void drawLineSkeleton(SkCanvas* max, const SkPoint pts[]);
149 void drawRectSkeleton(SkCanvas* max, const SkRect& r) {
151 this->setupSkeletonPaint(&paint);
154 fRectAsOval ? path.addOval(r) : path.addRect(r);
155 max->drawPath(path, paint);
158 void copyMinToMax() {
159 erase(fMaxSurface.get());
160 SkCanvas* canvas = fMaxSurface->getCanvas();
162 canvas->concat(fMatrix);
163 fMinSurface->draw(canvas, 0, 0, nullptr);
167 paint.setBlendMode(SkBlendMode::kClear);
168 for (int iy = 1; iy < fH; ++iy) {
169 SkScalar y = SkIntToScalar(iy * fZoom);
170 canvas->drawLine(0, y - SK_ScalarHalf, 999, y - SK_ScalarHalf, paint);
172 for (int ix = 1; ix < fW; ++ix) {
173 SkScalar x = SkIntToScalar(ix * fZoom);
174 canvas->drawLine(x - SK_ScalarHalf, 0, x - SK_ScalarHalf, 999, paint);
179 void FatBits::drawBG(SkCanvas* canvas) {
182 paint.setShader(fShader);
183 canvas->drawRect(fBounds, paint);
184 paint.setShader(nullptr);
187 void FatBits::drawFG(SkCanvas* canvas) {
188 SkPaint inner, outer;
190 inner.setAntiAlias(true);
191 inner.setColor(SK_ColorBLACK);
192 inner.setStrokeWidth(PIXEL_CENTER_SIZE);
194 outer.setAntiAlias(true);
195 outer.setColor(SK_ColorWHITE);
196 outer.setStrokeWidth(PIXEL_CENTER_SIZE + 2);
198 SkScalar half = SkIntToScalar(fZoom) / 2;
199 for (int iy = 0; iy < fH; ++iy) {
200 SkScalar y = SkIntToScalar(iy * fZoom) + half;
201 for (int ix = 0; ix < fW; ++ix) {
202 SkScalar x = SkIntToScalar(ix * fZoom) + half;
204 canvas->drawPoint(x, y, outer);
205 canvas->drawPoint(x, y, inner);
211 p.setStyle(SkPaint::kStroke_Style);
212 p.setColor(SK_ColorLTGRAY);
214 fClipRect.fLeft * fZoom,
215 fClipRect.fTop * fZoom,
216 fClipRect.fRight * fZoom,
217 fClipRect.fBottom * fZoom
219 canvas->drawRect(r, p);
223 void FatBits::drawLineSkeleton(SkCanvas* max, const SkPoint pts[]) {
225 this->setupSkeletonPaint(&paint);
231 if (fStyle == kStroke_Style) {
233 p.setStyle(SkPaint::kStroke_Style);
234 p.setStrokeWidth(SK_Scalar1 * fZoom);
235 p.setStrokeCap(fStrokeCap);
237 p.getFillPath(path, &dst);
243 max->drawPath(path, paint);
246 void FatBits::drawLine(SkCanvas* canvas, SkPoint pts[]) {
249 fInverse.mapPoints(pts, 2);
255 erase(fMinSurface.get());
256 this->setupPaint(&paint);
257 paint.setColor(FAT_PIXEL_COLOR);
259 fMinSurface->getCanvas()->save();
260 SkRect r = fClipRect;
261 r.inset(SK_Scalar1/3, SK_Scalar1/3);
262 fMinSurface->getCanvas()->clipRect(r, kIntersect_SkClipOp, true);
264 fMinSurface->getCanvas()->drawLine(pts[0], pts[1], paint);
266 fMinSurface->getCanvas()->restore();
268 this->copyMinToMax();
270 SkCanvas* max = fMaxSurface->getCanvas();
272 fMatrix.mapPoints(pts, 2);
273 this->drawLineSkeleton(max, pts);
275 fMaxSurface->draw(canvas, 0, 0, nullptr);
278 void FatBits::drawRect(SkCanvas* canvas, SkPoint pts[2]) {
281 fInverse.mapPoints(pts, 2);
290 erase(fMinSurface.get());
291 this->setupPaint(&paint);
292 paint.setColor(FAT_PIXEL_COLOR);
294 SkCanvas* c = fMinSurface->getCanvas();
295 fRectAsOval ? c->drawOval(r, paint) : c->drawRect(r, paint);
297 this->copyMinToMax();
299 SkCanvas* max = fMaxSurface->getCanvas();
301 fMatrix.mapPoints(pts, 2);
303 this->drawRectSkeleton(max, r);
305 fMaxSurface->draw(canvas, 0, 0, nullptr);
308 void FatBits::drawTriangleSkeleton(SkCanvas* max, const SkPoint pts[]) {
310 this->setupSkeletonPaint(&paint);
318 max->drawPath(path, paint);
321 void FatBits::drawTriangle(SkCanvas* canvas, SkPoint pts[3]) {
324 fInverse.mapPoints(pts, 3);
336 erase(fMinSurface.get());
337 this->setupPaint(&paint);
338 paint.setColor(FAT_PIXEL_COLOR);
339 fMinSurface->getCanvas()->drawPath(path, paint);
340 this->copyMinToMax();
342 SkCanvas* max = fMaxSurface->getCanvas();
344 fMatrix.mapPoints(pts, 3);
345 this->drawTriangleSkeleton(max, pts);
347 fMaxSurface->draw(canvas, 0, 0, nullptr);
350 ///////////////////////////////////////////////////////////////////////////////////////////////////
352 class IndexClick : public SkView::Click {
355 IndexClick(SkView* v, int index) : SkView::Click(v), fIndex(index) {}
357 static int GetIndex(SkView::Click* click) {
358 return ((IndexClick*)click)->fIndex;
362 class DrawLineView : public SampleView {
369 fFB.setWHZ(24*2, 16*2, fZoom);
373 SkMatrix::MakeScale(SkIntToScalar(fZoom)).mapPoints(fPts, 3);
377 void setStyle(FatBits::Style s) {
379 this->inval(nullptr);
383 bool onQuery(SkEvent* evt) override {
384 if (SampleCode::TitleQ(*evt)) {
385 SampleCode::TitleR(evt, "FatBits");
389 if (SampleCode::CharQ(*evt, &uni)) {
392 fFB.setUseClip(!fFB.getUseClip());
393 this->inval(nullptr);
397 this->inval(nullptr);
400 fFB.toggleRectAsOval();
401 this->inval(nullptr);
404 fFB.setGrid(!fFB.getGrid());
405 this->inval(nullptr);
408 if (FatBits::kStroke_Style == fFB.getStyle()) {
409 this->setStyle(FatBits::kHair_Style);
411 this->setStyle(FatBits::kStroke_Style);
415 const SkPaint::Cap caps[] = {
416 SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap,
418 fFB.fStrokeCap = caps[(fFB.fStrokeCap + 1) % 3];
419 this->inval(nullptr);
423 fFB.setAA(!fFB.getAA());
424 this->inval(nullptr);
427 fFB.setShowSkeleton(!fFB.getShowSkeleton());
428 this->inval(nullptr);
431 fFB.togglePixelColors();
432 this->inval(nullptr);
435 fFB.setTriangle(!fFB.getTriangle());
436 this->inval(nullptr);
440 return this->INHERITED::onQuery(evt);
443 void onDrawContent(SkCanvas* canvas) override {
445 if (fFB.getTriangle()) {
446 fFB.drawTriangle(canvas, fPts);
449 fFB.drawRect(canvas, fPts);
451 fFB.drawLine(canvas, fPts);
457 str.printf("%s %s %s",
458 fFB.getAA() ? "AA" : "BW",
459 FatBits::kHair_Style == fFB.getStyle() ? "Hair" : "Stroke",
460 fFB.getUseClip() ? "clip" : "noclip");
462 paint.setAntiAlias(true);
463 paint.setTextSize(16);
464 paint.setColor(SK_ColorBLUE);
465 canvas->drawString(str, 10, 16, paint);
469 SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override {
470 SkPoint pt = { x, y };
472 int count = fFB.getTriangle() ? 3 : 2;
475 for (int i = 0; i < count; ++i) {
476 if (fPts[i].equalsWithinTolerance(pt, tol)) {
481 return new IndexClick(this, index);
484 bool onClick(Click* click) override {
485 int index = IndexClick::GetIndex(click);
486 if (index >= 0 && index <= 2) {
487 fPts[index] = click->fCurr;
489 SkScalar dx = click->fCurr.fX - click->fPrev.fX;
490 SkScalar dy = click->fCurr.fY - click->fPrev.fY;
491 fPts[0].offset(dx, dy);
492 fPts[1].offset(dx, dy);
493 fPts[2].offset(dx, dy);
495 this->inval(nullptr);
501 typedef SampleView INHERITED;
504 //////////////////////////////////////////////////////////////////////////////
506 static SkView* MyFactory() { return new DrawLineView; }
507 static SkViewRegister reg(MyFactory);