* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm/gm.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPath.h"
#include "include/core/SkScalar.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkTypes.h"
namespace skiagm {
class NonClosedPathsGM: public GM {
public:
NonClosedPathsGM() {}
enum ClosureType {
TotallyNonClosed,
FakeCloseCorner,
FakeCloseMiddle,
kClosureTypeCount
};
protected:
SkString onShortName() override {
return SkString("nonclosedpaths");
}
SkISize onISize() override {
return SkISize::Make(1220, 1920);
}
static void MakePath(SkPath* path, ClosureType type) {
if (FakeCloseMiddle == type) {
path->moveTo(30, 50);
path->lineTo(30, 30);
} else {
path->moveTo(30, 30);
}
path->lineTo(70, 30);
path->lineTo(70, 70);
path->lineTo(30, 70);
path->lineTo(30, 50);
if (FakeCloseCorner == type) {
path->lineTo(30, 30);
}
}
static void SetLocation(SkCanvas* canvas, int counter, int lineNum) {
SkScalar x = SK_Scalar1 * 100 * (counter % lineNum) + 10 + SK_Scalar1 / 4;
SkScalar y = SK_Scalar1 * 100 * (counter / lineNum) + 10 + 3 * SK_Scalar1 / 4;
canvas->translate(x, y);
}
void onDraw(SkCanvas* canvas) override {
constexpr int kStrokeWidth[] = {0, 10, 40, 50};
int numWidths = SK_ARRAY_COUNT(kStrokeWidth);
constexpr SkPaint::Style kStyle[] = {
SkPaint::kStroke_Style, SkPaint::kStrokeAndFill_Style
};
constexpr SkPaint::Cap kCap[] = {
SkPaint::kButt_Cap, SkPaint::kRound_Cap, SkPaint::kSquare_Cap
};
constexpr SkPaint::Join kJoin[] = {
SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
};
constexpr ClosureType kType[] = {
TotallyNonClosed, FakeCloseCorner, FakeCloseMiddle
};
int counter = 0;
SkPaint paint;
paint.setAntiAlias(true);
for (size_t type = 0; type < kClosureTypeCount; ++type) {
for (size_t style = 0; style < SK_ARRAY_COUNT(kStyle); ++style) {
for (size_t cap = 0; cap < SK_ARRAY_COUNT(kCap); ++cap) {
for (size_t join = 0; join < SK_ARRAY_COUNT(kJoin); ++join) {
for (int width = 0; width < numWidths; ++width) {
canvas->save();
SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths);
SkPath path;
MakePath(&path, kType[type]);
paint.setStyle(kStyle[style]);
paint.setStrokeCap(kCap[cap]);
paint.setStrokeJoin(kJoin[join]);
paint.setStrokeWidth(SkIntToScalar(kStrokeWidth[width]));
canvas->drawPath(path, paint);
canvas->restore();
++counter;
}
}
}
}
}
paint.setStyle(SkPaint::kFill_Style);
for (size_t type = 0; type < kClosureTypeCount; ++type) {
canvas->save();
SetLocation(canvas, counter, SkPaint::kJoinCount * numWidths);
SkPath path;
MakePath(&path, kType[type]);
canvas->drawPath(path, paint);
canvas->restore();
++counter;
}
}
private:
typedef GM INHERITED;
};
DEF_GM(return new NonClosedPathsGM;)
}