| 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/SkPath.h" |
| 14 |
#include "include/core/SkPathBuilder.h" |
| 15 |
#include "include/core/SkRect.h" |
| 16 |
#include "include/core/SkScalar.h" |
| 17 |
#include "include/core/SkTypeface.h" |
| 18 |
#include "include/core/SkTypes.h" |
| 19 |
#include "src/base/SkRandom.h" |
| 20 |
#include "tools/ToolUtils.h" |
| 21 |
#include "tools/fonts/FontToolUtils.h" |
| 22 |
|
| 23 |
static void drawPath(SkPath& path,SkCanvas* canvas,SkColor color, |
| 24 |
const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join, |
| 25 |
SkPaint::Style style, SkPathFillType fill, |
| 26 |
SkScalar strokeWidth) { |
| 27 |
path.setFillType(fill); |
| 28 |
SkPaint paint; |
| 29 |
paint.setStrokeCap(cap); |
| 30 |
paint.setStrokeWidth(strokeWidth); |
| 31 |
paint.setStrokeJoin(join); |
| 32 |
paint.setColor(color); |
| 33 |
paint.setStyle(style); |
| 34 |
canvas->save(); |
| 35 |
canvas->clipRect(clip); |
| 36 |
canvas->drawPath(path, paint); |
| 37 |
canvas->restore(); |
| 38 |
} |
| 39 |
|
| 40 |
static void draw(SkCanvas* canvas, bool doClose) { |
| 41 |
struct FillAndName { |
| 42 |
SkPathFillType fFill; |
| 43 |
const char* fName; |
| 44 |
}; |
| 45 |
constexpr FillAndName gFills[] = { |
| 46 |
{SkPathFillType::kWinding, "Winding"}, |
| 47 |
{SkPathFillType::kEvenOdd, "Even / Odd"}, |
| 48 |
{SkPathFillType::kInverseWinding, "Inverse Winding"}, |
| 49 |
{SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"}, |
| 50 |
}; |
| 51 |
struct StyleAndName { |
| 52 |
SkPaint::Style fStyle; |
| 53 |
const char* fName; |
| 54 |
}; |
| 55 |
constexpr StyleAndName gStyles[] = { |
| 56 |
{SkPaint::kFill_Style, "Fill"}, |
| 57 |
{SkPaint::kStroke_Style, "Stroke"}, |
| 58 |
{SkPaint::kStrokeAndFill_Style, "Stroke And Fill"}, |
| 59 |
}; |
| 60 |
struct CapAndName { |
| 61 |
SkPaint::Cap fCap; |
| 62 |
SkPaint::Join fJoin; |
| 63 |
const char* fName; |
| 64 |
}; |
| 65 |
constexpr CapAndName gCaps[] = { |
| 66 |
{SkPaint::kButt_Cap, SkPaint::kBevel_Join, "Butt"}, |
| 67 |
{SkPaint::kRound_Cap, SkPaint::kRound_Join, "Round"}, |
| 68 |
{SkPaint::kSquare_Cap, SkPaint::kBevel_Join, "Square"} |
| 69 |
}; |
| 70 |
struct PathAndName { |
| 71 |
SkPath fPath; |
| 72 |
const char* fName; |
| 73 |
}; |
| 74 |
SkPathBuilder builder; |
| 75 |
builder.moveTo(25*SK_Scalar1, 15*SK_Scalar1); |
| 76 |
builder.lineTo(75*SK_Scalar1, 15*SK_Scalar1); |
| 77 |
PathAndName path; |
| 78 |
if (doClose) { |
| 79 |
builder.close(); |
| 80 |
path.fName = "moveTo-line-close"; |
| 81 |
} else { |
| 82 |
path.fName = "moveTo-line"; |
| 83 |
} |
| 84 |
path.fPath = builder.detach(); |
| 85 |
|
| 86 |
SkPaint titlePaint; |
| 87 |
titlePaint.setColor(SK_ColorBLACK); |
| 88 |
titlePaint.setAntiAlias(true); |
| 89 |
|
| 90 |
SkFont font(ToolUtils::DefaultPortableTypeface(), 15.0f); |
| 91 |
|
| 92 |
const char titleNoClose[] = "Line Drawn Into Rectangle Clips With " |
| 93 |
"Indicated Style, Fill and Linecaps, with stroke width 10"; |
| 94 |
const char titleClose[] = "Line Closed Drawn Into Rectangle Clips With " |
| 95 |
"Indicated Style, Fill and Linecaps, with stroke width 10"; |
| 96 |
const char* title = doClose ? titleClose : titleNoClose; |
| 97 |
canvas->drawString(title, 20.0f, 20.0f, font, titlePaint); |
| 98 |
|
| 99 |
SkRandom rand; |
| 100 |
SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1); |
| 101 |
canvas->save(); |
| 102 |
canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1); |
| 103 |
canvas->save(); |
| 104 |
for (size_t cap = 0; cap < std::size(gCaps); ++cap) { |
| 105 |
if (0 < cap) { |
| 106 |
canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0); |
| 107 |
} |
| 108 |
canvas->save(); |
| 109 |
for (size_t fill = 0; fill < std::size(gFills); ++fill) { |
| 110 |
if (0 < fill) { |
| 111 |
canvas->translate(0, rect.height() + 40 * SK_Scalar1); |
| 112 |
} |
| 113 |
canvas->save(); |
| 114 |
for (size_t style = 0; style < std::size(gStyles); ++style) { |
| 115 |
if (0 < style) { |
| 116 |
canvas->translate(rect.width() + 40 * SK_Scalar1, 0); |
| 117 |
} |
| 118 |
|
| 119 |
SkColor color = ToolUtils::color_to_565(0xff007000); |
| 120 |
drawPath(path.fPath, canvas, color, rect, |
| 121 |
gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle, |
| 122 |
gFills[fill].fFill, SK_Scalar1*10); |
| 123 |
|
| 124 |
SkPaint rectPaint; |
| 125 |
rectPaint.setColor(SK_ColorBLACK); |
| 126 |
rectPaint.setStyle(SkPaint::kStroke_Style); |
| 127 |
rectPaint.setStrokeWidth(-1); |
| 128 |
rectPaint.setAntiAlias(true); |
| 129 |
canvas->drawRect(rect, rectPaint); |
| 130 |
|
| 131 |
SkPaint labelPaint; |
| 132 |
labelPaint.setColor(color); |
| 133 |
font.setSize(10); |
| 134 |
canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f, |
| 135 |
font, labelPaint); |
| 136 |
canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f, |
| 137 |
font, labelPaint); |
| 138 |
canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f, |
| 139 |
font, labelPaint); |
| 140 |
} |
| 141 |
canvas->restore(); |
| 142 |
} |
| 143 |
canvas->restore(); |
| 144 |
} |
| 145 |
canvas->restore(); |
| 146 |
canvas->restore(); |
| 147 |
} |
| 148 |
DEF_SIMPLE_GM(linepath, canvas, 1240, 390) { |
| 149 |
draw(canvas, false); |
| 150 |
} |
| 151 |
DEF_SIMPLE_GM(lineclosepath, canvas, 1240, 390) { |
| 152 |
draw(canvas, true); |
| 153 |
} |