2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2006 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#include "SkCornerPathEffect.h"
|
|
|
|
#include "SkPath.h"
|
|
|
|
#include "SkPoint.h"
|
2014-01-30 18:58:24 +00:00
|
|
|
#include "SkReadBuffer.h"
|
|
|
|
#include "SkWriteBuffer.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2012-12-18 16:12:09 +00:00
|
|
|
SkCornerPathEffect::SkCornerPathEffect(SkScalar radius) : fRadius(radius) {}
|
|
|
|
SkCornerPathEffect::~SkCornerPathEffect() {}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
static bool ComputeStep(const SkPoint& a, const SkPoint& b, SkScalar radius,
|
|
|
|
SkPoint* step) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkScalar dist = SkPoint::Distance(a, b);
|
|
|
|
|
2015-05-12 17:37:34 +00:00
|
|
|
*step = b - a;
|
2008-12-17 15:59:43 +00:00
|
|
|
if (dist <= radius * 2) {
|
2015-05-12 17:37:34 +00:00
|
|
|
*step *= SK_ScalarHalf;
|
2008-12-17 15:59:43 +00:00
|
|
|
return false;
|
2011-04-20 11:39:28 +00:00
|
|
|
} else {
|
2015-05-12 17:37:34 +00:00
|
|
|
*step *= radius / dist;
|
2008-12-17 15:59:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
bool SkCornerPathEffect::filterPath(SkPath* dst, const SkPath& src,
|
2013-01-24 21:03:11 +00:00
|
|
|
SkStrokeRec*, const SkRect*) const {
|
2012-12-18 16:12:09 +00:00
|
|
|
if (0 == fRadius) {
|
2008-12-17 15:59:43 +00:00
|
|
|
return false;
|
2011-04-20 11:39:28 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
SkPath::Iter iter(src, false);
|
|
|
|
SkPath::Verb verb, prevVerb = (SkPath::Verb)-1;
|
|
|
|
SkPoint pts[4];
|
|
|
|
|
|
|
|
bool closed;
|
|
|
|
SkPoint moveTo, lastCorner;
|
|
|
|
SkVector firstStep, step;
|
|
|
|
bool prevIsValid = true;
|
|
|
|
|
|
|
|
// to avoid warnings
|
2015-05-12 17:37:34 +00:00
|
|
|
step.set(0, 0);
|
2008-12-17 15:59:43 +00:00
|
|
|
moveTo.set(0, 0);
|
|
|
|
firstStep.set(0, 0);
|
|
|
|
lastCorner.set(0, 0);
|
|
|
|
|
|
|
|
for (;;) {
|
2012-05-16 17:16:46 +00:00
|
|
|
switch (verb = iter.next(pts, false)) {
|
2011-04-20 11:39:28 +00:00
|
|
|
case SkPath::kMove_Verb:
|
|
|
|
// close out the previous (open) contour
|
|
|
|
if (SkPath::kLine_Verb == prevVerb) {
|
|
|
|
dst->lineTo(lastCorner);
|
|
|
|
}
|
|
|
|
closed = iter.isClosedContour();
|
|
|
|
if (closed) {
|
|
|
|
moveTo = pts[0];
|
|
|
|
prevIsValid = false;
|
|
|
|
} else {
|
|
|
|
dst->moveTo(pts[0]);
|
|
|
|
prevIsValid = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SkPath::kLine_Verb: {
|
2008-12-17 15:59:43 +00:00
|
|
|
bool drawSegment = ComputeStep(pts[0], pts[1], fRadius, &step);
|
|
|
|
// prev corner
|
|
|
|
if (!prevIsValid) {
|
|
|
|
dst->moveTo(moveTo + step);
|
|
|
|
prevIsValid = true;
|
2011-04-20 11:39:28 +00:00
|
|
|
} else {
|
|
|
|
dst->quadTo(pts[0].fX, pts[0].fY, pts[0].fX + step.fX,
|
|
|
|
pts[0].fY + step.fY);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
if (drawSegment) {
|
|
|
|
dst->lineTo(pts[1].fX - step.fX, pts[1].fY - step.fY);
|
|
|
|
}
|
|
|
|
lastCorner = pts[1];
|
|
|
|
prevIsValid = true;
|
2011-04-20 11:39:28 +00:00
|
|
|
break;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-04-20 11:39:28 +00:00
|
|
|
case SkPath::kQuad_Verb:
|
|
|
|
// TBD - just replicate the curve for now
|
|
|
|
if (!prevIsValid) {
|
|
|
|
dst->moveTo(pts[0]);
|
|
|
|
prevIsValid = true;
|
|
|
|
}
|
|
|
|
dst->quadTo(pts[1], pts[2]);
|
|
|
|
lastCorner = pts[2];
|
|
|
|
firstStep.set(0, 0);
|
|
|
|
break;
|
2014-12-17 13:50:55 +00:00
|
|
|
case SkPath::kConic_Verb:
|
|
|
|
// TBD - just replicate the curve for now
|
|
|
|
if (!prevIsValid) {
|
|
|
|
dst->moveTo(pts[0]);
|
|
|
|
prevIsValid = true;
|
|
|
|
}
|
|
|
|
dst->conicTo(pts[1], pts[2], iter.conicWeight());
|
|
|
|
lastCorner = pts[2];
|
|
|
|
firstStep.set(0, 0);
|
|
|
|
break;
|
2011-04-20 11:39:28 +00:00
|
|
|
case SkPath::kCubic_Verb:
|
|
|
|
if (!prevIsValid) {
|
|
|
|
dst->moveTo(pts[0]);
|
|
|
|
prevIsValid = true;
|
|
|
|
}
|
|
|
|
// TBD - just replicate the curve for now
|
|
|
|
dst->cubicTo(pts[1], pts[2], pts[3]);
|
|
|
|
lastCorner = pts[3];
|
|
|
|
firstStep.set(0, 0);
|
|
|
|
break;
|
|
|
|
case SkPath::kClose_Verb:
|
|
|
|
if (firstStep.fX || firstStep.fY) {
|
|
|
|
dst->quadTo(lastCorner.fX, lastCorner.fY,
|
|
|
|
lastCorner.fX + firstStep.fX,
|
|
|
|
lastCorner.fY + firstStep.fY);
|
|
|
|
}
|
|
|
|
dst->close();
|
2014-12-17 13:50:55 +00:00
|
|
|
prevIsValid = false;
|
2013-05-31 15:17:50 +00:00
|
|
|
break;
|
2011-04-20 11:39:28 +00:00
|
|
|
case SkPath::kDone_Verb:
|
2014-12-17 13:50:55 +00:00
|
|
|
if (prevIsValid) {
|
|
|
|
dst->lineTo(lastCorner);
|
|
|
|
}
|
2011-04-20 11:39:28 +00:00
|
|
|
goto DONE;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2011-04-20 11:39:28 +00:00
|
|
|
if (SkPath::kMove_Verb == prevVerb) {
|
2008-12-17 15:59:43 +00:00
|
|
|
firstStep = step;
|
2011-04-20 11:39:28 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
prevVerb = verb;
|
|
|
|
}
|
|
|
|
DONE:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-03 16:11:13 +00:00
|
|
|
sk_sp<SkFlattenable> SkCornerPathEffect::CreateProc(SkReadBuffer& buffer) {
|
|
|
|
return SkCornerPathEffect::Make(buffer.readScalar());
|
2014-08-21 14:59:51 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 18:58:24 +00:00
|
|
|
void SkCornerPathEffect::flatten(SkWriteBuffer& buffer) const {
|
2008-12-17 15:59:43 +00:00
|
|
|
buffer.writeScalar(fRadius);
|
|
|
|
}
|
2015-01-26 14:08:52 +00:00
|
|
|
|
|
|
|
#ifndef SK_IGNORE_TO_STRING
|
|
|
|
void SkCornerPathEffect::toString(SkString* str) const {
|
|
|
|
str->appendf("SkCornerPathEffect: (");
|
|
|
|
str->appendf("radius: %.2f", fRadius);
|
|
|
|
str->appendf(")");
|
|
|
|
}
|
|
|
|
#endif
|