| 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/gm.h" |
| 9 |
#include "include/core/SkCanvas.h" |
| 10 |
#include "include/core/SkColor.h" |
| 11 |
#include "include/core/SkFont.h" |
| 12 |
#include "include/core/SkPaint.h" |
| 13 |
#include "include/core/SkPathBuilder.h" |
| 14 |
#include "include/core/SkPoint.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkString.h" |
| 19 |
#include "include/core/SkTypeface.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "src/base/SkRandom.h" |
| 22 |
#include "tools/ToolUtils.h" |
| 23 |
#include "tools/fonts/FontToolUtils.h" |
| 24 |
|
| 25 |
namespace skiagm { |
| 26 |
|
| 27 |
class EmptyPathGM : public GM { |
| 28 |
SkString getName() const override { return SkString("emptypath"); } |
| 29 |
|
| 30 |
SkISize getISize() override { return {600, 280}; } |
| 31 |
|
| 32 |
void drawEmpty(SkCanvas* canvas, |
| 33 |
SkColor color, |
| 34 |
const SkRect& clip, |
| 35 |
SkPaint::Style style, |
| 36 |
SkPathFillType fill) { |
| 37 |
SkPath path; |
| 38 |
path.setFillType(fill); |
| 39 |
SkPaint paint; |
| 40 |
paint.setColor(color); |
| 41 |
paint.setStyle(style); |
| 42 |
canvas->save(); |
| 43 |
canvas->clipRect(clip); |
| 44 |
canvas->drawPath(path, paint); |
| 45 |
canvas->restore(); |
| 46 |
} |
| 47 |
|
| 48 |
void onDraw(SkCanvas* canvas) override { |
| 49 |
struct FillAndName { |
| 50 |
SkPathFillType fFill; |
| 51 |
const char* fName; |
| 52 |
}; |
| 53 |
constexpr FillAndName gFills[] = { |
| 54 |
{SkPathFillType::kWinding, "Winding"}, |
| 55 |
{SkPathFillType::kEvenOdd, "Even / Odd"}, |
| 56 |
{SkPathFillType::kInverseWinding, "Inverse Winding"}, |
| 57 |
{SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"}, |
| 58 |
}; |
| 59 |
struct StyleAndName { |
| 60 |
SkPaint::Style fStyle; |
| 61 |
const char* fName; |
| 62 |
}; |
| 63 |
constexpr StyleAndName gStyles[] = { |
| 64 |
{SkPaint::kFill_Style, "Fill"}, |
| 65 |
{SkPaint::kStroke_Style, "Stroke"}, |
| 66 |
{SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, |
| 67 |
}; |
| 68 |
|
| 69 |
SkFont font(ToolUtils::DefaultPortableTypeface(), 15); |
| 70 |
const char title[] = "Empty Paths Drawn Into Rectangle Clips With " |
| 71 |
"Indicated Style and Fill"; |
| 72 |
canvas->drawString(title, 20.0f, 20.0f, font, SkPaint()); |
| 73 |
|
| 74 |
SkRandom rand; |
| 75 |
SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); |
| 76 |
int i = 0; |
| 77 |
canvas->save(); |
| 78 |
canvas->translate(10 * SK_Scalar1, 0); |
| 79 |
canvas->save(); |
| 80 |
for (size_t style = 0; style < std::size(gStyles); ++style) { |
| 81 |
for (size_t fill = 0; fill < std::size(gFills); ++fill) { |
| 82 |
if (0 == i % 4) { |
| 83 |
canvas->restore(); |
| 84 |
canvas->translate(0, rect.height() + 40 * SK_Scalar1); |
| 85 |
canvas->save(); |
| 86 |
} else { |
| 87 |
canvas->translate(rect.width() + 40 * SK_Scalar1, 0); |
| 88 |
} |
| 89 |
++i; |
| 90 |
|
| 91 |
|
| 92 |
SkColor color = rand.nextU(); |
| 93 |
color = 0xff000000 | color; // force solid |
| 94 |
color = ToolUtils::color_to_565(color); |
| 95 |
this->drawEmpty(canvas, color, rect, |
| 96 |
gStyles[style].fStyle, gFills[fill].fFill); |
| 97 |
|
| 98 |
SkPaint rectPaint; |
| 99 |
rectPaint.setColor(SK_ColorBLACK); |
| 100 |
rectPaint.setStyle(SkPaint::kStroke_Style); |
| 101 |
rectPaint.setStrokeWidth(-1); |
| 102 |
rectPaint.setAntiAlias(true); |
| 103 |
canvas->drawRect(rect, rectPaint); |
| 104 |
|
| 105 |
SkPaint labelPaint; |
| 106 |
labelPaint.setColor(color); |
| 107 |
SkFont labelFont(ToolUtils::DefaultPortableTypeface(), 12); |
| 108 |
canvas->drawString(gStyles[style].fName, 0, rect.height() + 15.0f, |
| 109 |
labelFont, labelPaint); |
| 110 |
canvas->drawString(gFills[fill].fName, 0, rect.height() + 28.0f, |
| 111 |
labelFont, labelPaint); |
| 112 |
} |
| 113 |
} |
| 114 |
canvas->restore(); |
| 115 |
canvas->restore(); |
| 116 |
} |
| 117 |
}; |
| 118 |
DEF_GM( return new EmptyPathGM; ) |
| 119 |
|
| 120 |
////////////////////////////////////////////////////////////////////////////// |
| 121 |
|
| 122 |
static constexpr SkPoint kPts[] = { |
| 123 |
{40, 40}, |
| 124 |
{80, 40}, |
| 125 |
{120, 40}, |
| 126 |
}; |
| 127 |
|
| 128 |
static SkPath make_path_move() { |
| 129 |
SkPathBuilder builder; |
| 130 |
for (SkPoint p : kPts) { |
| 131 |
builder.moveTo(p); |
| 132 |
} |
| 133 |
return builder.detach(); |
| 134 |
} |
| 135 |
|
| 136 |
static SkPath make_path_move_close() { |
| 137 |
SkPathBuilder builder; |
| 138 |
for (SkPoint p : kPts) { |
| 139 |
builder.moveTo(p).close(); |
| 140 |
} |
| 141 |
return builder.detach(); |
| 142 |
} |
| 143 |
|
| 144 |
static SkPath make_path_move_line() { |
| 145 |
SkPathBuilder builder; |
| 146 |
for (SkPoint p : kPts) { |
| 147 |
builder.moveTo(p).lineTo(p); |
| 148 |
} |
| 149 |
return builder.detach(); |
| 150 |
} |
| 151 |
|
| 152 |
static SkPath make_path_move_mix() { |
| 153 |
return SkPathBuilder().moveTo(kPts[0]) |
| 154 |
.moveTo(kPts[1]).close() |
| 155 |
.moveTo(kPts[2]).lineTo(kPts[2]) |
| 156 |
.detach(); |
| 157 |
} |
| 158 |
|
| 159 |
class EmptyStrokeGM : public GM { |
| 160 |
SkString getName() const override { return SkString("emptystroke"); } |
| 161 |
|
| 162 |
SkISize getISize() override { return {200, 240}; } |
| 163 |
|
| 164 |
void onDraw(SkCanvas* canvas) override { |
| 165 |
static constexpr SkPath (*kProcs[])() = { |
| 166 |
make_path_move, // expect red red red |
| 167 |
make_path_move_close, // expect black black black |
| 168 |
make_path_move_line, // expect black black black |
| 169 |
make_path_move_mix, // expect red black black, |
| 170 |
}; |
| 171 |
|
| 172 |
SkPaint strokePaint; |
| 173 |
strokePaint.setStyle(SkPaint::kStroke_Style); |
| 174 |
strokePaint.setStrokeWidth(21); |
| 175 |
strokePaint.setStrokeCap(SkPaint::kSquare_Cap); |
| 176 |
|
| 177 |
SkPaint dotPaint; |
| 178 |
dotPaint.setColor(SK_ColorRED); |
| 179 |
strokePaint.setStyle(SkPaint::kStroke_Style); |
| 180 |
dotPaint.setStrokeWidth(7); |
| 181 |
|
| 182 |
for (auto proc : kProcs) { |
| 183 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, kPts, dotPaint); |
| 184 |
canvas->drawPath(proc(), strokePaint); |
| 185 |
canvas->translate(0, 40); |
| 186 |
} |
| 187 |
} |
| 188 |
}; |
| 189 |
DEF_GM( return new EmptyStrokeGM; ) |
| 190 |
|
| 191 |
} // namespace skiagm |