| 1 |
/* |
| 2 |
* Copyright 2017 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 "include/core/SkTypes.h" |
| 9 |
|
| 10 |
#if defined(SK_GANESH) |
| 11 |
|
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#include "include/core/SkFont.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPath.h" |
| 16 |
#include "include/core/SkPathBuilder.h" |
| 17 |
#include "src/core/SkGeometry.h" |
| 18 |
#include "tools/fonts/FontToolUtils.h" |
| 19 |
#include "tools/viewer/ClickHandlerSlide.h" |
| 20 |
|
| 21 |
enum class VerbType { |
| 22 |
kTriangles, |
| 23 |
kQuadratics, |
| 24 |
kCubics, |
| 25 |
kConics |
| 26 |
}; |
| 27 |
|
| 28 |
static const char* verb_type_name(VerbType verbType) { |
| 29 |
switch (verbType) { |
| 30 |
case VerbType::kTriangles: return "kTriangles"; |
| 31 |
case VerbType::kQuadratics: return "kQuadratics"; |
| 32 |
case VerbType::kCubics: return "kCubics"; |
| 33 |
case VerbType::kConics: return "kConics"; |
| 34 |
} |
| 35 |
SkUNREACHABLE; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* This sample visualizes simple strokes. |
| 40 |
*/ |
| 41 |
class StrokeVerbSlide : public ClickHandlerSlide { |
| 42 |
public: |
| 43 |
StrokeVerbSlide() { fName = "StrokeVerb"; } |
| 44 |
|
| 45 |
void load(SkScalar w, SkScalar h) override { this->updatePath(); } |
| 46 |
|
| 47 |
void draw(SkCanvas*) override; |
| 48 |
|
| 49 |
bool onChar(SkUnichar) override; |
| 50 |
|
| 51 |
protected: |
| 52 |
class Click; |
| 53 |
|
| 54 |
ClickHandlerSlide::Click* onFindClickHandler(SkScalar x, SkScalar y, |
| 55 |
skui::ModifierKey) override; |
| 56 |
bool onClick(ClickHandlerSlide::Click*) override; |
| 57 |
|
| 58 |
|
| 59 |
private: |
| 60 |
void updateAndInval() { this->updatePath(); } |
| 61 |
|
| 62 |
void updatePath(); |
| 63 |
|
| 64 |
VerbType fVerbType = VerbType::kCubics; |
| 65 |
|
| 66 |
SkPoint fPoints[4] = { |
| 67 |
{100.05f, 100.05f}, {400.75f, 100.05f}, {400.75f, 300.95f}, {100.05f, 300.95f}}; |
| 68 |
|
| 69 |
float fConicWeight = .5; |
| 70 |
float fStrokeWidth = 40; |
| 71 |
SkPaint::Join fStrokeJoin = SkPaint::kMiter_Join; |
| 72 |
SkPaint::Cap fStrokeCap = SkPaint::kButt_Cap; |
| 73 |
|
| 74 |
SkPath fPath; |
| 75 |
}; |
| 76 |
|
| 77 |
void StrokeVerbSlide::draw(SkCanvas* canvas) { |
| 78 |
canvas->clear(SK_ColorBLACK); |
| 79 |
|
| 80 |
SkPaint outlinePaint; |
| 81 |
outlinePaint.setColor(0xff808080); |
| 82 |
outlinePaint.setStyle(SkPaint::kStroke_Style); |
| 83 |
outlinePaint.setStrokeWidth(fStrokeWidth); |
| 84 |
outlinePaint.setStrokeJoin(fStrokeJoin); |
| 85 |
outlinePaint.setStrokeCap(fStrokeCap); |
| 86 |
outlinePaint.setAntiAlias(true); |
| 87 |
canvas->drawPath(fPath, outlinePaint); |
| 88 |
|
| 89 |
SkString caption; |
| 90 |
caption.appendf("VerbType_%s", verb_type_name(fVerbType)); |
| 91 |
if (VerbType::kCubics == fVerbType) { |
| 92 |
caption.appendf(" (%s)", SkCubicTypeName(SkClassifyCubic(fPoints))); |
| 93 |
} else if (VerbType::kConics == fVerbType) { |
| 94 |
caption.appendf(" (w=%f)", fConicWeight); |
| 95 |
} |
| 96 |
|
| 97 |
caption.appendf(" (stroke_width=%f)", fStrokeWidth); |
| 98 |
|
| 99 |
SkPaint pointsPaint; |
| 100 |
pointsPaint.setColor(SK_ColorBLUE); |
| 101 |
pointsPaint.setStrokeWidth(8); |
| 102 |
pointsPaint.setAntiAlias(true); |
| 103 |
|
| 104 |
if (VerbType::kCubics == fVerbType) { |
| 105 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, {fPoints, 4}, pointsPaint); |
| 106 |
} else { |
| 107 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, {fPoints, 2}, pointsPaint); |
| 108 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, {fPoints + 3, 1}, pointsPaint); |
| 109 |
} |
| 110 |
|
| 111 |
SkFont font(ToolUtils::DefaultTypeface(), 20); |
| 112 |
SkPaint captionPaint; |
| 113 |
captionPaint.setColor(SK_ColorWHITE); |
| 114 |
canvas->drawString(caption, 10, 30, font, captionPaint); |
| 115 |
} |
| 116 |
|
| 117 |
void StrokeVerbSlide::updatePath() { |
| 118 |
SkPathBuilder builder; |
| 119 |
builder.moveTo(fPoints[0]); |
| 120 |
switch (fVerbType) { |
| 121 |
case VerbType::kCubics: |
| 122 |
builder.cubicTo(fPoints[1], fPoints[2], fPoints[3]); |
| 123 |
break; |
| 124 |
case VerbType::kQuadratics: |
| 125 |
builder.quadTo(fPoints[1], fPoints[3]); |
| 126 |
break; |
| 127 |
case VerbType::kConics: |
| 128 |
builder.conicTo(fPoints[1], fPoints[3], fConicWeight); |
| 129 |
break; |
| 130 |
case VerbType::kTriangles: |
| 131 |
builder.lineTo(fPoints[1]); |
| 132 |
builder.lineTo(fPoints[3]); |
| 133 |
builder.close(); |
| 134 |
break; |
| 135 |
} |
| 136 |
fPath = builder.detach(); |
| 137 |
} |
| 138 |
|
| 139 |
class StrokeVerbSlide::Click : public ClickHandlerSlide::Click { |
| 140 |
public: |
| 141 |
Click(int ptIdx) : fPtIdx(ptIdx) {} |
| 142 |
|
| 143 |
void doClick(SkPoint points[]) { |
| 144 |
if (fPtIdx >= 0) { |
| 145 |
points[fPtIdx] += fCurr - fPrev; |
| 146 |
} else { |
| 147 |
for (int i = 0; i < 4; ++i) { |
| 148 |
points[i] += fCurr - fPrev; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
private: |
| 154 |
int fPtIdx; |
| 155 |
}; |
| 156 |
|
| 157 |
ClickHandlerSlide::Click* StrokeVerbSlide::onFindClickHandler(SkScalar x, SkScalar y, |
| 158 |
skui::ModifierKey) { |
| 159 |
for (int i = 0; i < 4; ++i) { |
| 160 |
if (VerbType::kCubics != fVerbType && 2 == i) { |
| 161 |
continue; |
| 162 |
} |
| 163 |
if (fabs(x - fPoints[i].x()) < 20 && fabsf(y - fPoints[i].y()) < 20) { |
| 164 |
return new Click(i); |
| 165 |
} |
| 166 |
} |
| 167 |
return new Click(-1); |
| 168 |
} |
| 169 |
|
| 170 |
bool StrokeVerbSlide::onClick(ClickHandlerSlide::Click* click) { |
| 171 |
Click* myClick = (Click*)click; |
| 172 |
myClick->doClick(fPoints); |
| 173 |
this->updateAndInval(); |
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
bool StrokeVerbSlide::onChar(SkUnichar unichar) { |
| 178 |
if (unichar >= '1' && unichar <= '4') { |
| 179 |
fVerbType = VerbType(unichar - '1'); |
| 180 |
this->updateAndInval(); |
| 181 |
return true; |
| 182 |
} |
| 183 |
float* valueToScale = nullptr; |
| 184 |
if (VerbType::kConics == fVerbType) { |
| 185 |
valueToScale = &fConicWeight; |
| 186 |
} else { |
| 187 |
valueToScale = &fStrokeWidth; |
| 188 |
} |
| 189 |
if (valueToScale) { |
| 190 |
if (unichar == '+') { |
| 191 |
*valueToScale *= 2; |
| 192 |
this->updateAndInval(); |
| 193 |
return true; |
| 194 |
} |
| 195 |
if (unichar == '=') { |
| 196 |
*valueToScale *= 5/4.f; |
| 197 |
this->updateAndInval(); |
| 198 |
return true; |
| 199 |
} |
| 200 |
if (unichar == '-') { |
| 201 |
*valueToScale *= 4/5.f; |
| 202 |
this->updateAndInval(); |
| 203 |
return true; |
| 204 |
} |
| 205 |
if (unichar == '_') { |
| 206 |
*valueToScale *= .5f; |
| 207 |
this->updateAndInval(); |
| 208 |
return true; |
| 209 |
} |
| 210 |
} |
| 211 |
if (unichar == 'D') { |
| 212 |
SkDebugf(" SkPoint fPoints[4] = {\n"); |
| 213 |
SkDebugf(" {%ff, %ff},\n", fPoints[0].x(), fPoints[0].y()); |
| 214 |
SkDebugf(" {%ff, %ff},\n", fPoints[1].x(), fPoints[1].y()); |
| 215 |
SkDebugf(" {%ff, %ff},\n", fPoints[2].x(), fPoints[2].y()); |
| 216 |
SkDebugf(" {%ff, %ff}\n", fPoints[3].x(), fPoints[3].y()); |
| 217 |
SkDebugf(" };\n"); |
| 218 |
return true; |
| 219 |
} |
| 220 |
if (unichar == 'J') { |
| 221 |
fStrokeJoin = (SkPaint::Join)((fStrokeJoin + 1) % 3); |
| 222 |
this->updateAndInval(); |
| 223 |
return true; |
| 224 |
} |
| 225 |
if (unichar == 'C') { |
| 226 |
fStrokeCap = (SkPaint::Cap)((fStrokeCap + 1) % 3); |
| 227 |
this->updateAndInval(); |
| 228 |
return true; |
| 229 |
} |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
DEF_SLIDE(return new StrokeVerbSlide;) |
| 234 |
|
| 235 |
#endif // defined(SK_GANESH) |