blob: f240a90d727555b0f2ea106fcadd378345e40a16 [file] [log] [blame]
Chris Dalton09a7bb22018-08-31 19:53:15 +08001/*
2 * Copyright 2018 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkColor.h"
11#include "include/core/SkMatrix.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkPaint.h"
13#include "include/core/SkPath.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkPoint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
18#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkGeometry.h"
Chris Dalton09a7bb22018-08-31 19:53:15 +080020
Chris Dalton71e21262020-08-20 09:42:12 -060021static constexpr float kStrokeWidth = 30;
Chris Dalton09a7bb22018-08-31 19:53:15 +080022static constexpr int kCellSize = 200;
Chris Daltonc2ae19c2020-09-09 08:35:13 -060023static constexpr int kNumCols = 5;
Chris Dalton71e21262020-08-20 09:42:12 -060024static constexpr int kNumRows = 4;
Chris Dalton09a7bb22018-08-31 19:53:15 +080025
Chris Dalton71e21262020-08-20 09:42:12 -060026enum class CellFillMode {
27 kStretch,
28 kCenter
29};
30
31struct TrickyCubic {
32 SkPoint fPoints[4];
33 CellFillMode fFillMode;
34 float fScale = 1;
35};
36
37static const TrickyCubic kTrickyCubics[] = {
38 {{{122, 737}, {348, 553}, {403, 761}, {400, 760}}, CellFillMode::kStretch},
39 {{{244, 520}, {244, 518}, {1141, 634}, {394, 688}}, CellFillMode::kStretch},
40 {{{550, 194}, {138, 130}, {1035, 246}, {288, 300}}, CellFillMode::kStretch},
41 {{{226, 733}, {556, 779}, {-43, 471}, {348, 683}}, CellFillMode::kStretch},
42 {{{268, 204}, {492, 304}, {352, 23}, {433, 412}}, CellFillMode::kStretch},
43 {{{172, 480}, {396, 580}, {256, 299}, {338, 677}}, CellFillMode::kStretch},
44 {{{731, 340}, {318, 252}, {1026, -64}, {367, 265}}, CellFillMode::kStretch},
45 {{{475, 708}, {62, 620}, {770, 304}, {220, 659}}, CellFillMode::kStretch},
46 {{{0, 0}, {128, 128}, {128, 0}, {0, 128}}, CellFillMode::kCenter}, // Perfect cusp
47 {{{0,.01f}, {128,127.999f}, {128,.01f}, {0,127.99f}}, CellFillMode::kCenter}, // Near-cusp
48 {{{0,-.01f}, {128,128.001f}, {128,-.01f}, {0,128.001f}}, CellFillMode::kCenter}, // Near-cusp
49 {{{0,0}, {0,-10}, {0,-10}, {0,10}}, CellFillMode::kCenter, 1.098283f}, // Flat line with 180
50 {{{10,0}, {0,0}, {20,0}, {10,0}}, CellFillMode::kStretch}, // Flat line with 2 180s
51 {{{39,-39}, {40,-40}, {40,-40}, {0,0}}, CellFillMode::kStretch}, // Flat diagonal with 180
52 {{{40, 40}, {0, 0}, {200, 200}, {0, 0}}, CellFillMode::kStretch}, // Diag with an internal 180
53 {{{0,0}, {1e-2f,0}, {-1e-2f,0}, {0,0}}, CellFillMode::kCenter}, // Circle
Chris Daltonc2ae19c2020-09-09 08:35:13 -060054 {{{400.75f,100.05f}, {400.75f,100.05f}, {100.05f,300.95f}, {100.05f,300.95f}},
55 CellFillMode::kStretch}, // Flat line with no turns
56 {{{0.5f,0}, {0,0}, {20,0}, {10,0}}, CellFillMode::kStretch}, // Flat line with 2 180s
57 {{{10,0}, {0,0}, {10,0}, {10,0}}, CellFillMode::kStretch}, // Flat line with a 180
Chris Dalton09a7bb22018-08-31 19:53:15 +080058};
59
60static SkRect calc_tight_cubic_bounds(const SkPoint P[4], int depth=5) {
61 if (0 == depth) {
62 SkRect bounds;
Brian Osman788b9162020-02-07 10:36:46 -050063 bounds.fLeft = std::min(std::min(P[0].x(), P[1].x()), std::min(P[2].x(), P[3].x()));
64 bounds.fTop = std::min(std::min(P[0].y(), P[1].y()), std::min(P[2].y(), P[3].y()));
65 bounds.fRight = std::max(std::max(P[0].x(), P[1].x()), std::max(P[2].x(), P[3].x()));
66 bounds.fBottom = std::max(std::max(P[0].y(), P[1].y()), std::max(P[2].y(), P[3].y()));
Chris Dalton09a7bb22018-08-31 19:53:15 +080067 return bounds;
68 }
69
70 SkPoint chopped[7];
71 SkChopCubicAt(P, chopped, .5f);
72 SkRect bounds = calc_tight_cubic_bounds(chopped, depth - 1);
73 bounds.join(calc_tight_cubic_bounds(chopped+3, depth - 1));
74 return bounds;
75}
76
Chris Dalton71e21262020-08-20 09:42:12 -060077enum class FillMode {
78 kCenter,
79 kScale
80};
81
Chris Dalton09a7bb22018-08-31 19:53:15 +080082// This is a compilation of cubics that have given strokers grief. Feel free to add more.
83class TrickyCubicStrokesGM : public skiagm::GM {
84public:
85 TrickyCubicStrokesGM() {}
86
87protected:
88
89 SkString onShortName() override {
90 return SkString("trickycubicstrokes");
91 }
92
93 SkISize onISize() override {
Chris Dalton71e21262020-08-20 09:42:12 -060094 return SkISize::Make(kNumCols * kCellSize, kNumRows * kCellSize);
Chris Dalton09a7bb22018-08-31 19:53:15 +080095 }
96
97 void onOnceBeforeDraw() override {
98 fStrokePaint.setAntiAlias(true);
99 fStrokePaint.setStrokeWidth(kStrokeWidth);
100 fStrokePaint.setColor(SK_ColorGREEN);
101 fStrokePaint.setStyle(SkPaint::kStroke_Style);
102 }
103
104 void onDraw(SkCanvas* canvas) override {
105 canvas->clear(SK_ColorBLACK);
106
Chris Dalton71e21262020-08-20 09:42:12 -0600107 for (size_t i = 0; i < SK_ARRAY_COUNT(kTrickyCubics); ++i) {
108 const auto& trickyCubic = kTrickyCubics[i];
109 SkPoint p[7];
110 memcpy(p, trickyCubic.fPoints, sizeof(SkPoint) * 4);
111 for (int j = 0; j < 4; ++j) {
112 p[j] *= trickyCubic.fScale;
113 }
114 this->drawStroke(canvas, p, i, trickyCubic.fFillMode);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800115 }
116 }
117
Chris Dalton71e21262020-08-20 09:42:12 -0600118 void drawStroke(SkCanvas* canvas, const SkPoint p[4], int cellID, CellFillMode fillMode) {
119 auto cellRect = SkRect::MakeXYWH((cellID % kNumCols) * kCellSize,
120 (cellID / kNumCols) * kCellSize,
121 kCellSize, kCellSize);
122
123 SkRect strokeBounds = calc_tight_cubic_bounds(p);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800124 strokeBounds.outset(kStrokeWidth, kStrokeWidth);
125
126 SkMatrix matrix;
Chris Dalton71e21262020-08-20 09:42:12 -0600127 if (fillMode == CellFillMode::kStretch) {
128 matrix.setRectToRect(strokeBounds, cellRect, SkMatrix::kCenter_ScaleToFit);
129 } else {
130 matrix.setTranslate(cellRect.x() + kStrokeWidth +
131 (cellRect.width() - strokeBounds.width()) / 2,
132 cellRect.y() + kStrokeWidth +
133 (cellRect.height() - strokeBounds.height()) / 2);
134 }
Chris Dalton09a7bb22018-08-31 19:53:15 +0800135
136 SkAutoCanvasRestore acr(canvas, true);
137 canvas->concat(matrix);
Chris Dalton71e21262020-08-20 09:42:12 -0600138 fStrokePaint.setStrokeWidth(kStrokeWidth / matrix.getMaxScale());
139 canvas->drawPath(SkPath().moveTo(p[0]).cubicTo(p[1], p[2], p[3]), fStrokePaint);
Chris Dalton09a7bb22018-08-31 19:53:15 +0800140 }
141
142private:
143 SkPaint fStrokePaint;
John Stiles7571f9e2020-09-02 22:42:33 -0400144 using INHERITED = GM;
Chris Dalton09a7bb22018-08-31 19:53:15 +0800145};
146
147DEF_GM( return new TrickyCubicStrokesGM; )