#include <utility>
#include "src/core/SkStrokerPriv.h"
#include "include/private/SkMacros.h"
#include "include/private/SkTo.h"
#include "src/core/SkGeometry.h"
#include "src/core/SkPathPriv.h"
#include "src/core/SkPointPriv.h"
#include "src/core/SkPaintDefaults.h"
namespace pk {
enum {
kTangent_RecursiveLimit,
kCubic_RecursiveLimit,
kConic_RecursiveLimit,
kQuad_RecursiveLimit
};
static const int kRecursiveLimits[] = { 5*3, 24, 11*3, 11*3 };
static_assert(0 == kTangent_RecursiveLimit, "cubic_stroke_relies_on_tangent_equalling_zero");
static_assert(1 == kCubic_RecursiveLimit, "cubic_stroke_relies_on_cubic_equalling_one");
static_assert(PK_ARRAY_COUNT(kRecursiveLimits) == kQuad_RecursiveLimit + 1,
"recursive_limits_mismatch");
#ifndef DEBUG_QUAD_STROKER
#define DEBUG_QUAD_STROKER 0
#endif
#if DEBUG_QUAD_STROKER
#define STROKER_RESULT(resultType, depth, quadPts, format, ...) \
SkDebugf("[%d] %s " format "\n", depth, __FUNCTION__, __VA_ARGS__), \
SkDebugf(" " #resultType " t=(%g,%g)\n", quadPts->fStartT, quadPts->fEndT), \
resultType
#define STROKER_DEBUG_PARAMS(...) , __VA_ARGS__
#else
#define STROKER_RESULT(resultType, depth, quadPts, format, ...) \
resultType
#define STROKER_DEBUG_PARAMS(...)
#endif
#ifndef DEBUG_CUBIC_RECURSION_DEPTHS
#define DEBUG_CUBIC_RECURSION_DEPTHS 0
#endif
#define DEBUG_CUBIC_RECURSION_TRACK_DEPTH(depth) (void)(depth)
static inline bool degenerate_vector(const SkVector& v) {
return !SkPointPriv::CanNormalize(v.fX, v.fY);
}
static bool set_normal_unitnormal(const SkPoint& before, const SkPoint& after, SkScalar scale,
SkScalar radius,
SkVector* normal, SkVector* unitNormal) {
if (!unitNormal->setNormalize((after.fX - before.fX) * scale,
(after.fY - before.fY) * scale)) {
return false;
}
SkPointPriv::RotateCCW(unitNormal);
unitNormal->scale(radius, normal);
return true;
}
static bool set_normal_unitnormal(const SkVector& vec,
SkScalar radius,
SkVector* normal, SkVector* unitNormal) {
if (!unitNormal->setNormalize(vec.fX, vec.fY)) {
return false;
}
SkPointPriv::RotateCCW(unitNormal);
unitNormal->scale(radius, normal);
return true;
}
struct SkQuadConstruct { SkPoint fQuad[3]; SkPoint fTangentStart; SkPoint fTangentEnd; SkScalar fStartT; SkScalar fMidT; SkScalar fEndT; bool fStartSet; bool fEndSet; bool fOppositeTangents;
bool init(SkScalar start, SkScalar end) {
fStartT = start;
fMidT = (start + end) * PK_ScalarHalf;
fEndT = end;
fStartSet = fEndSet = false;
return fStartT < fMidT && fMidT < fEndT;
}
bool initWithStart(SkQuadConstruct* parent) {
if (!init(parent->fStartT, parent->fMidT)) {
return false;
}
fQuad[0] = parent->fQuad[0];
fTangentStart = parent->fTangentStart;
fStartSet = true;
return true;
}
bool initWithEnd(SkQuadConstruct* parent) {
if (!init(parent->fMidT, parent->fEndT)) {
return false;
}
fQuad[2] = parent->fQuad[2];
fTangentEnd = parent->fTangentEnd;
fEndSet = true;
return true;
}
};
class SkPathStroker {
public:
SkPathStroker(const SkPath& src,
SkScalar radius, SkScalar miterLimit, SkPaint::Cap,
SkPaint::Join, SkScalar resScale,
bool canIgnoreCenter);
bool hasOnlyMoveTo() const { return 0 == fSegmentCount; }
SkPoint moveToPt() const { return fFirstPt; }
void moveTo(const SkPoint&);
void lineTo(const SkPoint&, const SkPath::Iter* iter = nullptr);
void quadTo(const SkPoint&, const SkPoint&);
void conicTo(const SkPoint&, const SkPoint&, SkScalar weight);
void cubicTo(const SkPoint&, const SkPoint&, const SkPoint&);
void close(bool isLine) { this->finishContour(true, isLine); }
void done(SkPath* dst, bool isLine) {
this->finishContour(false, isLine);
dst->swap(fOuter);
}
SkScalar getResScale() const { return fResScale; }
bool isCurrentContourEmpty() const {
return fInner.isZeroLengthSincePoint(0) &&
fOuter.isZeroLengthSincePoint(fFirstOuterPtIndexInContour);
}
private:
SkScalar fRadius;
SkScalar fInvMiterLimit;
SkScalar fResScale;
SkScalar fInvResScale;
SkScalar fInvResScaleSquared;
SkVector fFirstNormal, fPrevNormal, fFirstUnitNormal, fPrevUnitNormal;
SkPoint fFirstPt, fPrevPt; SkPoint fFirstOuterPt;
int fFirstOuterPtIndexInContour;
int fSegmentCount;
bool fPrevIsLine;
bool fCanIgnoreCenter;
SkStrokerPriv::CapProc fCapper;
SkStrokerPriv::JoinProc fJoiner;
SkPath fInner, fOuter, fCusper;
enum StrokeType {
kOuter_StrokeType = 1, kInner_StrokeType = -1
} fStrokeType;
enum ResultType {
kSplit_ResultType, kDegenerate_ResultType, kQuad_ResultType, };
enum ReductionType {
kPoint_ReductionType, kLine_ReductionType, kQuad_ReductionType, kDegenerate_ReductionType, kDegenerate2_ReductionType, kDegenerate3_ReductionType, };
enum IntersectRayType {
kCtrlPt_RayType,
kResultType_RayType,
};
int fRecursionDepth; bool fFoundTangents; bool fJoinCompleted;
void addDegenerateLine(const SkQuadConstruct* );
static ReductionType CheckConicLinear(const SkConic& , SkPoint* reduction);
static ReductionType CheckCubicLinear(const SkPoint cubic[4], SkPoint reduction[3],
const SkPoint** tanPtPtr);
static ReductionType CheckQuadLinear(const SkPoint quad[3], SkPoint* reduction);
ResultType compareQuadConic(const SkConic& , SkQuadConstruct* ) const;
ResultType compareQuadCubic(const SkPoint cubic[4], SkQuadConstruct* );
ResultType compareQuadQuad(const SkPoint quad[3], SkQuadConstruct* );
void conicPerpRay(const SkConic& , SkScalar t, SkPoint* tPt, SkPoint* onPt,
SkPoint* tangent) const;
void conicQuadEnds(const SkConic& , SkQuadConstruct* ) const;
bool conicStroke(const SkConic& , SkQuadConstruct* );
bool cubicMidOnLine(const SkPoint cubic[4], const SkQuadConstruct* ) const;
void cubicPerpRay(const SkPoint cubic[4], SkScalar t, SkPoint* tPt, SkPoint* onPt,
SkPoint* tangent) const;
void cubicQuadEnds(const SkPoint cubic[4], SkQuadConstruct* );
void cubicQuadMid(const SkPoint cubic[4], const SkQuadConstruct* , SkPoint* mid) const;
bool cubicStroke(const SkPoint cubic[4], SkQuadConstruct* );
void init(StrokeType strokeType, SkQuadConstruct* , SkScalar tStart, SkScalar tEnd);
ResultType intersectRay(SkQuadConstruct* , IntersectRayType STROKER_DEBUG_PARAMS(int) ) const;
bool ptInQuadBounds(const SkPoint quad[3], const SkPoint& pt) const;
void quadPerpRay(const SkPoint quad[3], SkScalar t, SkPoint* tPt, SkPoint* onPt,
SkPoint* tangent) const;
bool quadStroke(const SkPoint quad[3], SkQuadConstruct* );
void setConicEndNormal(const SkConic& ,
const SkVector& normalAB, const SkVector& unitNormalAB,
SkVector* normalBC, SkVector* unitNormalBC);
void setCubicEndNormal(const SkPoint cubic[4],
const SkVector& normalAB, const SkVector& unitNormalAB,
SkVector* normalCD, SkVector* unitNormalCD);
void setQuadEndNormal(const SkPoint quad[3],
const SkVector& normalAB, const SkVector& unitNormalAB,
SkVector* normalBC, SkVector* unitNormalBC);
void setRayPts(const SkPoint& tPt, SkVector* dxy, SkPoint* onPt, SkPoint* tangent) const;
ResultType strokeCloseEnough(const SkPoint stroke[3], const SkPoint ray[2],
SkQuadConstruct* STROKER_DEBUG_PARAMS(int depth) ) const;
ResultType tangentsMeet(const SkPoint cubic[4], SkQuadConstruct* );
void finishContour(bool close, bool isLine);
bool preJoinTo(const SkPoint&, SkVector* normal, SkVector* unitNormal,
bool isLine);
void postJoinTo(const SkPoint&, const SkVector& normal,
const SkVector& unitNormal);
void line_to(const SkPoint& currPt, const SkVector& normal);
};
bool SkPathStroker::preJoinTo(const SkPoint& currPt, SkVector* normal,
SkVector* unitNormal, bool currIsLine) {
SkScalar prevX = fPrevPt.fX;
SkScalar prevY = fPrevPt.fY;
if (!set_normal_unitnormal(fPrevPt, currPt, fResScale, fRadius, normal, unitNormal)) {
if (SkStrokerPriv::CapFactory(SkPaint::kButt_Cap) == fCapper) {
return false;
}
normal->set(fRadius, 0);
unitNormal->set(1, 0);
}
if (fSegmentCount == 0) {
fFirstNormal = *normal;
fFirstUnitNormal = *unitNormal;
fFirstOuterPt.set(prevX + normal->fX, prevY + normal->fY);
fOuter.moveTo(fFirstOuterPt.fX, fFirstOuterPt.fY);
fInner.moveTo(prevX - normal->fX, prevY - normal->fY);
} else { fJoiner(&fOuter, &fInner, fPrevUnitNormal, fPrevPt, *unitNormal,
fRadius, fInvMiterLimit, fPrevIsLine, currIsLine);
}
fPrevIsLine = currIsLine;
return true;
}
void SkPathStroker::postJoinTo(const SkPoint& currPt, const SkVector& normal,
const SkVector& unitNormal) {
fJoinCompleted = true;
fPrevPt = currPt;
fPrevUnitNormal = unitNormal;
fPrevNormal = normal;
fSegmentCount += 1;
}
void SkPathStroker::finishContour(bool close, bool currIsLine) {
if (fSegmentCount > 0) {
SkPoint pt;
if (close) {
fJoiner(&fOuter, &fInner, fPrevUnitNormal, fPrevPt,
fFirstUnitNormal, fRadius, fInvMiterLimit,
fPrevIsLine, currIsLine);
fOuter.close();
if (fCanIgnoreCenter) {
if (fInner.getBounds().contains(fOuter.getBounds())) {
fInner.swap(fOuter);
}
} else {
fInner.getLastPt(&pt);
fOuter.moveTo(pt.fX, pt.fY);
fOuter.reversePathTo(fInner);
fOuter.close();
}
} else { fInner.getLastPt(&pt);
fCapper(&fOuter, fPrevPt, fPrevNormal, pt,
currIsLine ? &fInner : nullptr);
fOuter.reversePathTo(fInner);
fCapper(&fOuter, fFirstPt, -fFirstNormal, fFirstOuterPt,
fPrevIsLine ? &fInner : nullptr);
fOuter.close();
}
if (!fCusper.isEmpty()) {
fOuter.addPath(fCusper);
fCusper.rewind();
}
}
fInner.rewind();
fSegmentCount = -1;
fFirstOuterPtIndexInContour = fOuter.countPoints();
}
SkPathStroker::SkPathStroker(const SkPath& src,
SkScalar radius, SkScalar miterLimit,
SkPaint::Cap cap, SkPaint::Join join, SkScalar resScale,
bool canIgnoreCenter)
: fRadius(radius)
, fResScale(resScale)
, fCanIgnoreCenter(canIgnoreCenter) {
fInvMiterLimit = 0;
if (join == SkPaint::kMiter_Join) {
if (miterLimit <= PK_Scalar1) {
join = SkPaint::kBevel_Join;
} else {
fInvMiterLimit = PkScalarInvert(miterLimit);
}
}
fCapper = SkStrokerPriv::CapFactory(cap);
fJoiner = SkStrokerPriv::JoinFactory(join);
fSegmentCount = -1;
fFirstOuterPtIndexInContour = 0;
fPrevIsLine = false;
fOuter.incReserve(src.countPoints() * 3);
fInner.incReserve(src.countPoints());
fInvResScale = PkScalarInvert(resScale * 4);
fInvResScaleSquared = fInvResScale * fInvResScale;
fRecursionDepth = 0;
}
void SkPathStroker::moveTo(const SkPoint& pt) {
if (fSegmentCount > 0) {
this->finishContour(false, false);
}
fSegmentCount = 0;
fFirstPt = fPrevPt = pt;
fJoinCompleted = false;
}
void SkPathStroker::line_to(const SkPoint& currPt, const SkVector& normal) {
fOuter.lineTo(currPt.fX + normal.fX, currPt.fY + normal.fY);
fInner.lineTo(currPt.fX - normal.fX, currPt.fY - normal.fY);
}
static bool has_valid_tangent(const SkPath::Iter* iter) {
SkPath::Iter copy = *iter;
SkPath::Verb verb;
SkPoint pts[4];
while ((verb = copy.next(pts))) {
switch (verb) {
case SkPath::kMove_Verb:
return false;
case SkPath::kLine_Verb:
if (pts[0] == pts[1]) {
continue;
}
return true;
case SkPath::kQuad_Verb:
case SkPath::kConic_Verb:
if (pts[0] == pts[1] && pts[0] == pts[2]) {
continue;
}
return true;
case SkPath::kCubic_Verb:
if (pts[0] == pts[1] && pts[0] == pts[2] && pts[0] == pts[3]) {
continue;
}
return true;
case SkPath::kClose_Verb:
case SkPath::kDone_Verb:
return false;
}
}
return false;
}
void SkPathStroker::lineTo(const SkPoint& currPt, const SkPath::Iter* iter) {
bool teenyLine = SkPointPriv::EqualsWithinTolerance(fPrevPt, currPt, PK_ScalarNearlyZero * fInvResScale);
if (SkStrokerPriv::CapFactory(SkPaint::kButt_Cap) == fCapper && teenyLine) {
return;
}
if (teenyLine && (fJoinCompleted || (iter && has_valid_tangent(iter)))) {
return;
}
SkVector normal, unitNormal;
if (!this->preJoinTo(currPt, &normal, &unitNormal, true)) {
return;
}
this->line_to(currPt, normal);
this->postJoinTo(currPt, normal, unitNormal);
}
void SkPathStroker::setQuadEndNormal(const SkPoint quad[3], const SkVector& normalAB,
const SkVector& unitNormalAB, SkVector* normalBC, SkVector* unitNormalBC) {
if (!set_normal_unitnormal(quad[1], quad[2], fResScale, fRadius, normalBC, unitNormalBC)) {
*normalBC = normalAB;
*unitNormalBC = unitNormalAB;
}
}
void SkPathStroker::setConicEndNormal(const SkConic& conic, const SkVector& normalAB,
const SkVector& unitNormalAB, SkVector* normalBC, SkVector* unitNormalBC) {
setQuadEndNormal(conic.fPts, normalAB, unitNormalAB, normalBC, unitNormalBC);
}
void SkPathStroker::setCubicEndNormal(const SkPoint cubic[4], const SkVector& normalAB,
const SkVector& unitNormalAB, SkVector* normalCD, SkVector* unitNormalCD) {
SkVector ab = cubic[1] - cubic[0];
SkVector cd = cubic[3] - cubic[2];
bool degenerateAB = degenerate_vector(ab);
bool degenerateCD = degenerate_vector(cd);
if (degenerateAB && degenerateCD) {
goto DEGENERATE_NORMAL;
}
if (degenerateAB) {
ab = cubic[2] - cubic[0];
degenerateAB = degenerate_vector(ab);
}
if (degenerateCD) {
cd = cubic[3] - cubic[1];
degenerateCD = degenerate_vector(cd);
}
if (degenerateAB || degenerateCD) {
DEGENERATE_NORMAL:
*normalCD = normalAB;
*unitNormalCD = unitNormalAB;
return;
}
PkAssertResult(set_normal_unitnormal(cd, fRadius, normalCD, unitNormalCD));
}
void SkPathStroker::init(StrokeType strokeType, SkQuadConstruct* quadPts, SkScalar tStart,
SkScalar tEnd) {
fStrokeType = strokeType;
fFoundTangents = false;
quadPts->init(tStart, tEnd);
}
static SkScalar pt_to_line(const SkPoint& pt, const SkPoint& lineStart, const SkPoint& lineEnd) {
SkVector dxy = lineEnd - lineStart;
SkVector ab0 = pt - lineStart;
SkScalar numer = dxy.dot(ab0);
SkScalar denom = dxy.dot(dxy);
SkScalar t = sk_ieee_float_divide(numer, denom);
if (t >= 0 && t <= 1) {
SkPoint hit;
hit.fX = lineStart.fX * (1 - t) + lineEnd.fX * t;
hit.fY = lineStart.fY * (1 - t) + lineEnd.fY * t;
return SkPointPriv::DistanceToSqd(hit, pt);
} else {
return SkPointPriv::DistanceToSqd(pt, lineStart);
}
}
static bool cubic_in_line(const SkPoint cubic[4]) {
SkScalar ptMax = -1;
int outer1 PK_INIT_TO_AVOID_WARNING;
int outer2 PK_INIT_TO_AVOID_WARNING;
for (int index = 0; index < 3; ++index) {
for (int inner = index + 1; inner < 4; ++inner) {
SkVector testDiff = cubic[inner] - cubic[index];
SkScalar testMax = std::max(PkScalarAbs(testDiff.fX), PkScalarAbs(testDiff.fY));
if (ptMax < testMax) {
outer1 = index;
outer2 = inner;
ptMax = testMax;
}
}
}
int mid1 = (1 + (2 >> outer2)) >> outer1;
int mid2 = outer1 ^ outer2 ^ mid1;
SkScalar lineSlop = ptMax * ptMax * 0.00001f; return pt_to_line(cubic[mid1], cubic[outer1], cubic[outer2]) <= lineSlop
&& pt_to_line(cubic[mid2], cubic[outer1], cubic[outer2]) <= lineSlop;
}
static bool quad_in_line(const SkPoint quad[3]) {
SkScalar ptMax = -1;
int outer1 PK_INIT_TO_AVOID_WARNING;
int outer2 PK_INIT_TO_AVOID_WARNING;
for (int index = 0; index < 2; ++index) {
for (int inner = index + 1; inner < 3; ++inner) {
SkVector testDiff = quad[inner] - quad[index];
SkScalar testMax = std::max(PkScalarAbs(testDiff.fX), PkScalarAbs(testDiff.fY));
if (ptMax < testMax) {
outer1 = index;
outer2 = inner;
ptMax = testMax;
}
}
}
int mid = outer1 ^ outer2 ^ 3;
const float kCurvatureSlop = 0.000005f; SkScalar lineSlop = ptMax * ptMax * kCurvatureSlop;
return pt_to_line(quad[mid], quad[outer1], quad[outer2]) <= lineSlop;
}
static bool conic_in_line(const SkConic& conic) {
return quad_in_line(conic.fPts);
}
SkPathStroker::ReductionType SkPathStroker::CheckCubicLinear(const SkPoint cubic[4],
SkPoint reduction[3], const SkPoint** tangentPtPtr) {
bool degenerateAB = degenerate_vector(cubic[1] - cubic[0]);
bool degenerateBC = degenerate_vector(cubic[2] - cubic[1]);
bool degenerateCD = degenerate_vector(cubic[3] - cubic[2]);
if (degenerateAB & degenerateBC & degenerateCD) {
return kPoint_ReductionType;
}
if (degenerateAB + degenerateBC + degenerateCD == 2) {
return kLine_ReductionType;
}
if (!cubic_in_line(cubic)) {
*tangentPtPtr = degenerateAB ? &cubic[2] : &cubic[1];
return kQuad_ReductionType;
}
SkScalar tValues[3];
int count = SkFindCubicMaxCurvature(cubic, tValues);
int rCount = 0;
for (int index = 0; index < count; ++index) {
SkScalar t = tValues[index];
if (0 >= t || t >= 1) {
continue;
}
SkEvalCubicAt(cubic, t, &reduction[rCount], nullptr, nullptr);
if (reduction[rCount] != cubic[0] && reduction[rCount] != cubic[3]) {
++rCount;
}
}
if (rCount == 0) {
return kLine_ReductionType;
}
static_assert(kQuad_ReductionType + 1 == kDegenerate_ReductionType, "enum_out_of_whack");
static_assert(kQuad_ReductionType + 2 == kDegenerate2_ReductionType, "enum_out_of_whack");
static_assert(kQuad_ReductionType + 3 == kDegenerate3_ReductionType, "enum_out_of_whack");
return (ReductionType) (kQuad_ReductionType + rCount);
}
SkPathStroker::ReductionType SkPathStroker::CheckConicLinear(const SkConic& conic,
SkPoint* reduction) {
bool degenerateAB = degenerate_vector(conic.fPts[1] - conic.fPts[0]);
bool degenerateBC = degenerate_vector(conic.fPts[2] - conic.fPts[1]);
if (degenerateAB & degenerateBC) {
return kPoint_ReductionType;
}
if (degenerateAB | degenerateBC) {
return kLine_ReductionType;
}
if (!conic_in_line(conic)) {
return kQuad_ReductionType;
}
SkScalar t = SkFindQuadMaxCurvature(conic.fPts);
if (0 == t) {
return kLine_ReductionType;
}
conic.evalAt(t, reduction, nullptr);
return kDegenerate_ReductionType;
}
SkPathStroker::ReductionType SkPathStroker::CheckQuadLinear(const SkPoint quad[3],
SkPoint* reduction) {
bool degenerateAB = degenerate_vector(quad[1] - quad[0]);
bool degenerateBC = degenerate_vector(quad[2] - quad[1]);
if (degenerateAB & degenerateBC) {
return kPoint_ReductionType;
}
if (degenerateAB | degenerateBC) {
return kLine_ReductionType;
}
if (!quad_in_line(quad)) {
return kQuad_ReductionType;
}
SkScalar t = SkFindQuadMaxCurvature(quad);
if (0 == t || 1 == t) {
return kLine_ReductionType;
}
*reduction = SkEvalQuadAt(quad, t);
return kDegenerate_ReductionType;
}
void SkPathStroker::conicTo(const SkPoint& pt1, const SkPoint& pt2, SkScalar weight) {
const SkConic conic(fPrevPt, pt1, pt2, weight);
SkPoint reduction;
ReductionType reductionType = CheckConicLinear(conic, &reduction);
if (kPoint_ReductionType == reductionType) {
this->lineTo(pt2);
return;
}
if (kLine_ReductionType == reductionType) {
this->lineTo(pt2);
return;
}
if (kDegenerate_ReductionType == reductionType) {
this->lineTo(reduction);
SkStrokerPriv::JoinProc saveJoiner = fJoiner;
fJoiner = SkStrokerPriv::JoinFactory(SkPaint::kRound_Join);
this->lineTo(pt2);
fJoiner = saveJoiner;
return;
}
SkVector normalAB, unitAB, normalBC, unitBC;
if (!this->preJoinTo(pt1, &normalAB, &unitAB, false)) {
this->lineTo(pt2);
return;
}
SkQuadConstruct quadPts;
this->init(kOuter_StrokeType, &quadPts, 0, 1);
(void) this->conicStroke(conic, &quadPts);
this->init(kInner_StrokeType, &quadPts, 0, 1);
(void) this->conicStroke(conic, &quadPts);
this->setConicEndNormal(conic, normalAB, unitAB, &normalBC, &unitBC);
this->postJoinTo(pt2, normalBC, unitBC);
}
void SkPathStroker::quadTo(const SkPoint& pt1, const SkPoint& pt2) {
const SkPoint quad[3] = { fPrevPt, pt1, pt2 };
SkPoint reduction;
ReductionType reductionType = CheckQuadLinear(quad, &reduction);
if (kPoint_ReductionType == reductionType) {
this->lineTo(pt2);
return;
}
if (kLine_ReductionType == reductionType) {
this->lineTo(pt2);
return;
}
if (kDegenerate_ReductionType == reductionType) {
this->lineTo(reduction);
SkStrokerPriv::JoinProc saveJoiner = fJoiner;
fJoiner = SkStrokerPriv::JoinFactory(SkPaint::kRound_Join);
this->lineTo(pt2);
fJoiner = saveJoiner;
return;
}
SkVector normalAB, unitAB, normalBC, unitBC;
if (!this->preJoinTo(pt1, &normalAB, &unitAB, false)) {
this->lineTo(pt2);
return;
}
SkQuadConstruct quadPts;
this->init(kOuter_StrokeType, &quadPts, 0, 1);
(void) this->quadStroke(quad, &quadPts);
this->init(kInner_StrokeType, &quadPts, 0, 1);
(void) this->quadStroke(quad, &quadPts);
this->setQuadEndNormal(quad, normalAB, unitAB, &normalBC, &unitBC);
this->postJoinTo(pt2, normalBC, unitBC);
}
void SkPathStroker::setRayPts(const SkPoint& tPt, SkVector* dxy, SkPoint* onPt,
SkPoint* tangent) const {
if (!dxy->setLength(fRadius)) {
dxy->set(fRadius, 0);
}
SkScalar axisFlip = PkIntToScalar(fStrokeType); onPt->fX = tPt.fX + axisFlip * dxy->fY;
onPt->fY = tPt.fY - axisFlip * dxy->fX;
if (tangent) {
tangent->fX = onPt->fX + dxy->fX;
tangent->fY = onPt->fY + dxy->fY;
}
}
void SkPathStroker::conicPerpRay(const SkConic& conic, SkScalar t, SkPoint* tPt, SkPoint* onPt,
SkPoint* tangent) const {
SkVector dxy;
conic.evalAt(t, tPt, &dxy);
if (dxy.fX == 0 && dxy.fY == 0) {
dxy = conic.fPts[2] - conic.fPts[0];
}
this->setRayPts(*tPt, &dxy, onPt, tangent);
}
void SkPathStroker::conicQuadEnds(const SkConic& conic, SkQuadConstruct* quadPts) const {
if (!quadPts->fStartSet) {
SkPoint conicStartPt;
this->conicPerpRay(conic, quadPts->fStartT, &conicStartPt, &quadPts->fQuad[0],
&quadPts->fTangentStart);
quadPts->fStartSet = true;
}
if (!quadPts->fEndSet) {
SkPoint conicEndPt;
this->conicPerpRay(conic, quadPts->fEndT, &conicEndPt, &quadPts->fQuad[2],
&quadPts->fTangentEnd);
quadPts->fEndSet = true;
}
}
void SkPathStroker::cubicPerpRay(const SkPoint cubic[4], SkScalar t, SkPoint* tPt, SkPoint* onPt,
SkPoint* tangent) const {
SkVector dxy;
SkPoint chopped[7];
SkEvalCubicAt(cubic, t, tPt, &dxy, nullptr);
if (dxy.fX == 0 && dxy.fY == 0) {
const SkPoint* cPts = cubic;
if (SkScalarNearlyZero(t)) {
dxy = cubic[2] - cubic[0];
} else if (SkScalarNearlyZero(1 - t)) {
dxy = cubic[3] - cubic[1];
} else {
SkChopCubicAt(cubic, chopped, t);
dxy = chopped[3] - chopped[2];
if (dxy.fX == 0 && dxy.fY == 0) {
dxy = chopped[3] - chopped[1];
cPts = chopped;
}
}
if (dxy.fX == 0 && dxy.fY == 0) {
dxy = cPts[3] - cPts[0];
}
}
setRayPts(*tPt, &dxy, onPt, tangent);
}
void SkPathStroker::cubicQuadEnds(const SkPoint cubic[4], SkQuadConstruct* quadPts) {
if (!quadPts->fStartSet) {
SkPoint cubicStartPt;
this->cubicPerpRay(cubic, quadPts->fStartT, &cubicStartPt, &quadPts->fQuad[0],
&quadPts->fTangentStart);
quadPts->fStartSet = true;
}
if (!quadPts->fEndSet) {
SkPoint cubicEndPt;
this->cubicPerpRay(cubic, quadPts->fEndT, &cubicEndPt, &quadPts->fQuad[2],
&quadPts->fTangentEnd);
quadPts->fEndSet = true;
}
}
void SkPathStroker::cubicQuadMid(const SkPoint cubic[4], const SkQuadConstruct* quadPts,
SkPoint* mid) const {
SkPoint cubicMidPt;
this->cubicPerpRay(cubic, quadPts->fMidT, &cubicMidPt, mid, nullptr);
}
void SkPathStroker::quadPerpRay(const SkPoint quad[3], SkScalar t, SkPoint* tPt, SkPoint* onPt,
SkPoint* tangent) const {
SkVector dxy;
SkEvalQuadAt(quad, t, tPt, &dxy);
if (dxy.fX == 0 && dxy.fY == 0) {
dxy = quad[2] - quad[0];
}
setRayPts(*tPt, &dxy, onPt, tangent);
}
SkPathStroker::ResultType SkPathStroker::intersectRay(SkQuadConstruct* quadPts,
IntersectRayType intersectRayType STROKER_DEBUG_PARAMS(int depth)) const {
const SkPoint& start = quadPts->fQuad[0];
const SkPoint& end = quadPts->fQuad[2];
SkVector aLen = quadPts->fTangentStart - start;
SkVector bLen = quadPts->fTangentEnd - end;
SkScalar denom = aLen.cross(bLen);
if (denom == 0 || !SkScalarIsFinite(denom)) {
quadPts->fOppositeTangents = aLen.dot(bLen) < 0;
return STROKER_RESULT(kDegenerate_ResultType, depth, quadPts, "denom == 0");
}
quadPts->fOppositeTangents = false;
SkVector ab0 = start - end;
SkScalar numerA = bLen.cross(ab0);
SkScalar numerB = aLen.cross(ab0);
if ((numerA >= 0) == (numerB >= 0)) { SkScalar dist1 = pt_to_line(start, end, quadPts->fTangentEnd);
SkScalar dist2 = pt_to_line(end, start, quadPts->fTangentStart);
if (std::max(dist1, dist2) <= fInvResScaleSquared) {
return STROKER_RESULT(kDegenerate_ResultType, depth, quadPts,
"std::max(dist1=%g, dist2=%g) <= fInvResScaleSquared", dist1, dist2);
}
return STROKER_RESULT(kSplit_ResultType, depth, quadPts,
"(numerA=%g >= 0) == (numerB=%g >= 0)", numerA, numerB);
}
numerA /= denom;
bool validDivide = numerA > numerA - 1;
if (validDivide) {
if (kCtrlPt_RayType == intersectRayType) {
SkPoint* ctrlPt = &quadPts->fQuad[1];
ctrlPt->fX = start.fX * (1 - numerA) + quadPts->fTangentStart.fX * numerA;
ctrlPt->fY = start.fY * (1 - numerA) + quadPts->fTangentStart.fY * numerA;
}
return STROKER_RESULT(kQuad_ResultType, depth, quadPts,
"(numerA=%g >= 0) != (numerB=%g >= 0)", numerA, numerB);
}
quadPts->fOppositeTangents = aLen.dot(bLen) < 0;
return STROKER_RESULT(kDegenerate_ResultType, depth, quadPts,
"SkScalarNearlyZero(denom=%g)", denom);
}
SkPathStroker::ResultType SkPathStroker::tangentsMeet(const SkPoint cubic[4],
SkQuadConstruct* quadPts) {
this->cubicQuadEnds(cubic, quadPts);
return this->intersectRay(quadPts, kResultType_RayType STROKER_DEBUG_PARAMS(fRecursionDepth));
}
static int intersect_quad_ray(const SkPoint line[2], const SkPoint quad[3], SkScalar roots[2]) {
SkVector vec = line[1] - line[0];
SkScalar r[3];
for (int n = 0; n < 3; ++n) {
r[n] = (quad[n].fY - line[0].fY) * vec.fX - (quad[n].fX - line[0].fX) * vec.fY;
}
SkScalar A = r[2];
SkScalar B = r[1];
SkScalar C = r[0];
A += C - 2 * B; B -= C; return SkFindUnitQuadRoots(A, 2 * B, C, roots);
}
bool SkPathStroker::ptInQuadBounds(const SkPoint quad[3], const SkPoint& pt) const {
SkScalar xMin = std::min(std::min(quad[0].fX, quad[1].fX), quad[2].fX);
if (pt.fX + fInvResScale < xMin) {
return false;
}
SkScalar xMax = std::max(std::max(quad[0].fX, quad[1].fX), quad[2].fX);
if (pt.fX - fInvResScale > xMax) {
return false;
}
SkScalar yMin = std::min(std::min(quad[0].fY, quad[1].fY), quad[2].fY);
if (pt.fY + fInvResScale < yMin) {
return false;
}
SkScalar yMax = std::max(std::max(quad[0].fY, quad[1].fY), quad[2].fY);
if (pt.fY - fInvResScale > yMax) {
return false;
}
return true;
}
static bool points_within_dist(const SkPoint& nearPt, const SkPoint& farPt, SkScalar limit) {
return SkPointPriv::DistanceToSqd(nearPt, farPt) <= limit * limit;
}
static bool sharp_angle(const SkPoint quad[3]) {
SkVector smaller = quad[1] - quad[0];
SkVector larger = quad[1] - quad[2];
SkScalar smallerLen = SkPointPriv::LengthSqd(smaller);
SkScalar largerLen = SkPointPriv::LengthSqd(larger);
if (smallerLen > largerLen) {
using std::swap;
swap(smaller, larger);
largerLen = smallerLen;
}
if (!smaller.setLength(largerLen)) {
return false;
}
SkScalar dot = smaller.dot(larger);
return dot > 0;
}
SkPathStroker::ResultType SkPathStroker::strokeCloseEnough(const SkPoint stroke[3],
const SkPoint ray[2], SkQuadConstruct* quadPts STROKER_DEBUG_PARAMS(int depth)) const {
SkPoint strokeMid = SkEvalQuadAt(stroke, PK_ScalarHalf);
if (points_within_dist(ray[0], strokeMid, fInvResScale)) { if (sharp_angle(quadPts->fQuad)) {
return STROKER_RESULT(kSplit_ResultType, depth, quadPts,
"sharp_angle (1) =%g,%g, %g,%g, %g,%g",
quadPts->fQuad[0].fX, quadPts->fQuad[0].fY,
quadPts->fQuad[1].fX, quadPts->fQuad[1].fY,
quadPts->fQuad[2].fX, quadPts->fQuad[2].fY);
}
return STROKER_RESULT(kQuad_ResultType, depth, quadPts,
"points_within_dist(ray[0]=%g,%g, strokeMid=%g,%g, fInvResScale=%g)",
ray[0].fX, ray[0].fY, strokeMid.fX, strokeMid.fY, fInvResScale);
}
if (!ptInQuadBounds(stroke, ray[0])) { return STROKER_RESULT(kSplit_ResultType, depth, quadPts,
"!pt_in_quad_bounds(stroke=(%g,%g %g,%g %g,%g), ray[0]=%g,%g)",
stroke[0].fX, stroke[0].fY, stroke[1].fX, stroke[1].fY, stroke[2].fX, stroke[2].fY,
ray[0].fX, ray[0].fY);
}
SkScalar roots[2];
int rootCount = intersect_quad_ray(ray, stroke, roots);
if (rootCount != 1) {
return STROKER_RESULT(kSplit_ResultType, depth, quadPts,
"rootCount=%d != 1", rootCount);
}
SkPoint quadPt = SkEvalQuadAt(stroke, roots[0]);
SkScalar error = fInvResScale * (PK_Scalar1 - PkScalarAbs(roots[0] - 0.5f) * 2);
if (points_within_dist(ray[0], quadPt, error)) { if (sharp_angle(quadPts->fQuad)) {
return STROKER_RESULT(kSplit_ResultType, depth, quadPts,
"sharp_angle (2) =%g,%g, %g,%g, %g,%g",
quadPts->fQuad[0].fX, quadPts->fQuad[0].fY,
quadPts->fQuad[1].fX, quadPts->fQuad[1].fY,
quadPts->fQuad[2].fX, quadPts->fQuad[2].fY);
}
return STROKER_RESULT(kQuad_ResultType, depth, quadPts,
"points_within_dist(ray[0]=%g,%g, quadPt=%g,%g, error=%g)",
ray[0].fX, ray[0].fY, quadPt.fX, quadPt.fY, error);
}
return STROKER_RESULT(kSplit_ResultType, depth, quadPts, "%s", "fall through");
}
SkPathStroker::ResultType SkPathStroker::compareQuadCubic(const SkPoint cubic[4],
SkQuadConstruct* quadPts) {
this->cubicQuadEnds(cubic, quadPts);
ResultType resultType = this->intersectRay(quadPts, kCtrlPt_RayType
STROKER_DEBUG_PARAMS(fRecursionDepth) );
if (resultType != kQuad_ResultType) {
return resultType;
}
SkPoint ray[2]; this->cubicPerpRay(cubic, quadPts->fMidT, &ray[1], &ray[0], nullptr);
return this->strokeCloseEnough(quadPts->fQuad, ray, quadPts
STROKER_DEBUG_PARAMS(fRecursionDepth));
}
SkPathStroker::ResultType SkPathStroker::compareQuadConic(const SkConic& conic,
SkQuadConstruct* quadPts) const {
this->conicQuadEnds(conic, quadPts);
ResultType resultType = this->intersectRay(quadPts, kCtrlPt_RayType
STROKER_DEBUG_PARAMS(fRecursionDepth) );
if (resultType != kQuad_ResultType) {
return resultType;
}
SkPoint ray[2]; this->conicPerpRay(conic, quadPts->fMidT, &ray[1], &ray[0], nullptr);
return this->strokeCloseEnough(quadPts->fQuad, ray, quadPts
STROKER_DEBUG_PARAMS(fRecursionDepth));
}
SkPathStroker::ResultType SkPathStroker::compareQuadQuad(const SkPoint quad[3],
SkQuadConstruct* quadPts) {
if (!quadPts->fStartSet) {
SkPoint quadStartPt;
this->quadPerpRay(quad, quadPts->fStartT, &quadStartPt, &quadPts->fQuad[0],
&quadPts->fTangentStart);
quadPts->fStartSet = true;
}
if (!quadPts->fEndSet) {
SkPoint quadEndPt;
this->quadPerpRay(quad, quadPts->fEndT, &quadEndPt, &quadPts->fQuad[2],
&quadPts->fTangentEnd);
quadPts->fEndSet = true;
}
ResultType resultType = this->intersectRay(quadPts, kCtrlPt_RayType
STROKER_DEBUG_PARAMS(fRecursionDepth));
if (resultType != kQuad_ResultType) {
return resultType;
}
SkPoint ray[2];
this->quadPerpRay(quad, quadPts->fMidT, &ray[1], &ray[0], nullptr);
return this->strokeCloseEnough(quadPts->fQuad, ray, quadPts
STROKER_DEBUG_PARAMS(fRecursionDepth));
}
void SkPathStroker::addDegenerateLine(const SkQuadConstruct* quadPts) {
const SkPoint* quad = quadPts->fQuad;
SkPath* path = fStrokeType == kOuter_StrokeType ? &fOuter : &fInner;
path->lineTo(quad[2].fX, quad[2].fY);
}
bool SkPathStroker::cubicMidOnLine(const SkPoint cubic[4], const SkQuadConstruct* quadPts) const {
SkPoint strokeMid;
this->cubicQuadMid(cubic, quadPts, &strokeMid);
SkScalar dist = pt_to_line(strokeMid, quadPts->fQuad[0], quadPts->fQuad[2]);
return dist < fInvResScaleSquared;
}
bool SkPathStroker::cubicStroke(const SkPoint cubic[4], SkQuadConstruct* quadPts) {
if (!fFoundTangents) {
ResultType resultType = this->tangentsMeet(cubic, quadPts);
if (kQuad_ResultType != resultType) {
if ((kDegenerate_ResultType == resultType
|| points_within_dist(quadPts->fQuad[0], quadPts->fQuad[2],
fInvResScale)) && cubicMidOnLine(cubic, quadPts)) {
addDegenerateLine(quadPts);
DEBUG_CUBIC_RECURSION_TRACK_DEPTH(fRecursionDepth);
return true;
}
} else {
fFoundTangents = true;
}
}
if (fFoundTangents) {
ResultType resultType = this->compareQuadCubic(cubic, quadPts);
if (kQuad_ResultType == resultType) {
SkPath* path = fStrokeType == kOuter_StrokeType ? &fOuter : &fInner;
const SkPoint* stroke = quadPts->fQuad;
path->quadTo(stroke[1].fX, stroke[1].fY, stroke[2].fX, stroke[2].fY);
DEBUG_CUBIC_RECURSION_TRACK_DEPTH(fRecursionDepth);
return true;
}
if (kDegenerate_ResultType == resultType) {
if (!quadPts->fOppositeTangents) {
addDegenerateLine(quadPts);
DEBUG_CUBIC_RECURSION_TRACK_DEPTH(fRecursionDepth);
return true;
}
}
}
if (!SkScalarIsFinite(quadPts->fQuad[2].fX) || !SkScalarIsFinite(quadPts->fQuad[2].fY)) {
DEBUG_CUBIC_RECURSION_TRACK_DEPTH(fRecursionDepth);
return false; }
if (++fRecursionDepth > kRecursiveLimits[fFoundTangents]) {
DEBUG_CUBIC_RECURSION_TRACK_DEPTH(fRecursionDepth);
return false; }
SkQuadConstruct half;
if (!half.initWithStart(quadPts)) {
addDegenerateLine(quadPts);
DEBUG_CUBIC_RECURSION_TRACK_DEPTH(fRecursionDepth);
--fRecursionDepth;
return true;
}
if (!this->cubicStroke(cubic, &half)) {
return false;
}
if (!half.initWithEnd(quadPts)) {
addDegenerateLine(quadPts);
DEBUG_CUBIC_RECURSION_TRACK_DEPTH(fRecursionDepth);
--fRecursionDepth;
return true;
}
if (!this->cubicStroke(cubic, &half)) {
return false;
}
--fRecursionDepth;
return true;
}
bool SkPathStroker::conicStroke(const SkConic& conic, SkQuadConstruct* quadPts) {
ResultType resultType = this->compareQuadConic(conic, quadPts);
if (kQuad_ResultType == resultType) {
const SkPoint* stroke = quadPts->fQuad;
SkPath* path = fStrokeType == kOuter_StrokeType ? &fOuter : &fInner;
path->quadTo(stroke[1].fX, stroke[1].fY, stroke[2].fX, stroke[2].fY);
return true;
}
if (kDegenerate_ResultType == resultType) {
addDegenerateLine(quadPts);
return true;
}
if (++fRecursionDepth > kRecursiveLimits[kConic_RecursiveLimit]) {
return false; }
SkQuadConstruct half;
(void) half.initWithStart(quadPts);
if (!this->conicStroke(conic, &half)) {
return false;
}
(void) half.initWithEnd(quadPts);
if (!this->conicStroke(conic, &half)) {
return false;
}
--fRecursionDepth;
return true;
}
bool SkPathStroker::quadStroke(const SkPoint quad[3], SkQuadConstruct* quadPts) {
ResultType resultType = this->compareQuadQuad(quad, quadPts);
if (kQuad_ResultType == resultType) {
const SkPoint* stroke = quadPts->fQuad;
SkPath* path = fStrokeType == kOuter_StrokeType ? &fOuter : &fInner;
path->quadTo(stroke[1].fX, stroke[1].fY, stroke[2].fX, stroke[2].fY);
return true;
}
if (kDegenerate_ResultType == resultType) {
addDegenerateLine(quadPts);
return true;
}
if (++fRecursionDepth > kRecursiveLimits[kQuad_RecursiveLimit]) {
return false; }
SkQuadConstruct half;
(void) half.initWithStart(quadPts);
if (!this->quadStroke(quad, &half)) {
return false;
}
(void) half.initWithEnd(quadPts);
if (!this->quadStroke(quad, &half)) {
return false;
}
--fRecursionDepth;
return true;
}
void SkPathStroker::cubicTo(const SkPoint& pt1, const SkPoint& pt2,
const SkPoint& pt3) {
const SkPoint cubic[4] = { fPrevPt, pt1, pt2, pt3 };
SkPoint reduction[3];
const SkPoint* tangentPt;
ReductionType reductionType = CheckCubicLinear(cubic, reduction, &tangentPt);
if (kPoint_ReductionType == reductionType) {
this->lineTo(pt3);
return;
}
if (kLine_ReductionType == reductionType) {
this->lineTo(pt3);
return;
}
if (kDegenerate_ReductionType <= reductionType && kDegenerate3_ReductionType >= reductionType) {
this->lineTo(reduction[0]);
SkStrokerPriv::JoinProc saveJoiner = fJoiner;
fJoiner = SkStrokerPriv::JoinFactory(SkPaint::kRound_Join);
if (kDegenerate2_ReductionType <= reductionType) {
this->lineTo(reduction[1]);
}
if (kDegenerate3_ReductionType == reductionType) {
this->lineTo(reduction[2]);
}
this->lineTo(pt3);
fJoiner = saveJoiner;
return;
}
SkVector normalAB, unitAB, normalCD, unitCD;
if (!this->preJoinTo(*tangentPt, &normalAB, &unitAB, false)) {
this->lineTo(pt3);
return;
}
SkScalar tValues[2];
int count = SkFindCubicInflections(cubic, tValues);
SkScalar lastT = 0;
for (int index = 0; index <= count; ++index) {
SkScalar nextT = index < count ? tValues[index] : 1;
SkQuadConstruct quadPts;
this->init(kOuter_StrokeType, &quadPts, lastT, nextT);
(void) this->cubicStroke(cubic, &quadPts);
this->init(kInner_StrokeType, &quadPts, lastT, nextT);
(void) this->cubicStroke(cubic, &quadPts);
lastT = nextT;
}
SkScalar cusp = SkFindCubicCusp(cubic);
if (cusp > 0) {
SkPoint cuspLoc;
SkEvalCubicAt(cubic, cusp, &cuspLoc, nullptr, nullptr);
fCusper.addCircle(cuspLoc.fX, cuspLoc.fY, fRadius);
}
this->setCubicEndNormal(cubic, normalAB, unitAB, &normalCD, &unitCD);
this->postJoinTo(pt3, normalCD, unitCD);
}
SkStroke::SkStroke() {
fWidth = PK_Scalar1;
fMiterLimit = PkPaintDefaults_MiterLimit;
fResScale = 1;
fCap = SkPaint::kDefault_Cap;
fJoin = SkPaint::kDefault_Join;
fDoFill = false;
}
void SkStroke::setWidth(SkScalar width) {
fWidth = width;
}
void SkStroke::setMiterLimit(SkScalar miterLimit) {
fMiterLimit = miterLimit;
}
void SkStroke::setCap(SkPaint::Cap cap) {
fCap = SkToU8(cap);
}
void SkStroke::setJoin(SkPaint::Join join) {
fJoin = SkToU8(join);
}
class AutoTmpPath {
public:
AutoTmpPath(const SkPath& src, SkPath** dst) : fSrc(src) {
if (&src == *dst) {
*dst = &fTmpDst;
fSwapWithSrc = true;
} else {
(*dst)->reset();
fSwapWithSrc = false;
}
}
~AutoTmpPath() {
if (fSwapWithSrc) {
fTmpDst.swap(*const_cast<SkPath*>(&fSrc));
}
}
private:
SkPath fTmpDst;
const SkPath& fSrc;
bool fSwapWithSrc;
};
void SkStroke::strokePath(const SkPath& src, SkPath* dst) const {
SkScalar radius = PkScalarHalf(fWidth);
AutoTmpPath tmp(src, &dst);
if (radius <= 0) {
return;
}
{
SkRect rect;
bool isClosed = false;
SkPathDirection dir;
if (src.isRect(&rect, &isClosed, &dir) && isClosed) {
this->strokeRect(rect, dst, dir);
if (src.isInverseFillType()) {
dst->toggleInverseFillType();
}
return;
}
}
bool ignoreCenter = fDoFill && (src.getSegmentMasks() == SkPath::kLine_SegmentMask) &&
src.isLastContourClosed() && src.isConvex();
SkPathStroker stroker(src, radius, fMiterLimit, this->getCap(), this->getJoin(),
fResScale, ignoreCenter);
SkPath::Iter iter(src, false);
SkPath::Verb lastSegment = SkPath::kMove_Verb;
for (;;) {
SkPoint pts[4];
switch (iter.next(pts)) {
case SkPath::kMove_Verb:
stroker.moveTo(pts[0]);
break;
case SkPath::kLine_Verb:
stroker.lineTo(pts[1], &iter);
lastSegment = SkPath::kLine_Verb;
break;
case SkPath::kQuad_Verb:
stroker.quadTo(pts[1], pts[2]);
lastSegment = SkPath::kQuad_Verb;
break;
case SkPath::kConic_Verb: {
stroker.conicTo(pts[1], pts[2], iter.conicWeight());
lastSegment = SkPath::kConic_Verb;
break;
} break;
case SkPath::kCubic_Verb:
stroker.cubicTo(pts[1], pts[2], pts[3]);
lastSegment = SkPath::kCubic_Verb;
break;
case SkPath::kClose_Verb:
if (SkPaint::kButt_Cap != this->getCap()) {
if (stroker.hasOnlyMoveTo()) {
stroker.lineTo(stroker.moveToPt());
goto ZERO_LENGTH;
}
if (stroker.isCurrentContourEmpty()) {
ZERO_LENGTH:
lastSegment = SkPath::kLine_Verb;
break;
}
}
stroker.close(lastSegment == SkPath::kLine_Verb);
break;
case SkPath::kDone_Verb:
goto DONE;
}
}
DONE:
stroker.done(dst, lastSegment == SkPath::kLine_Verb);
if (fDoFill && !ignoreCenter) {
if (SkPathPriv::ComputeFirstDirection(src) == SkPathFirstDirection::kCCW) {
dst->reverseAddPath(src);
} else {
dst->addPath(src);
}
} else {
#if 0#endif
#if 0#endif
}
if (src.isInverseFillType()) {
dst->toggleInverseFillType();
}
}
static SkPathDirection reverse_direction(SkPathDirection dir) {
static const SkPathDirection gOpposite[] = { SkPathDirection::kCCW, SkPathDirection::kCW };
return gOpposite[(int)dir];
}
static void addBevel(SkPath* path, const SkRect& r, const SkRect& outer, SkPathDirection dir) {
SkPoint pts[8];
if (SkPathDirection::kCW == dir) {
pts[0].set(r.fLeft, outer.fTop);
pts[1].set(r.fRight, outer.fTop);
pts[2].set(outer.fRight, r.fTop);
pts[3].set(outer.fRight, r.fBottom);
pts[4].set(r.fRight, outer.fBottom);
pts[5].set(r.fLeft, outer.fBottom);
pts[6].set(outer.fLeft, r.fBottom);
pts[7].set(outer.fLeft, r.fTop);
} else {
pts[7].set(r.fLeft, outer.fTop);
pts[6].set(r.fRight, outer.fTop);
pts[5].set(outer.fRight, r.fTop);
pts[4].set(outer.fRight, r.fBottom);
pts[3].set(r.fRight, outer.fBottom);
pts[2].set(r.fLeft, outer.fBottom);
pts[1].set(outer.fLeft, r.fBottom);
pts[0].set(outer.fLeft, r.fTop);
}
path->addPoly(pts, 8, true);
}
void SkStroke::strokeRect(const SkRect& origRect, SkPath* dst,
SkPathDirection dir) const {
dst->reset();
SkScalar radius = PkScalarHalf(fWidth);
if (radius <= 0) {
return;
}
SkScalar rw = origRect.width();
SkScalar rh = origRect.height();
if ((rw < 0) ^ (rh < 0)) {
dir = reverse_direction(dir);
}
SkRect rect(origRect);
rect.sort();
rw = rect.width();
rh = rect.height();
SkRect r(rect);
r.outset(radius, radius);
SkPaint::Join join = (SkPaint::Join)fJoin;
if (SkPaint::kMiter_Join == join && fMiterLimit < PK_ScalarSqrt2) {
join = SkPaint::kBevel_Join;
}
switch (join) {
case SkPaint::kMiter_Join:
dst->addRect(r, dir);
break;
case SkPaint::kBevel_Join:
addBevel(dst, rect, r, dir);
break;
case SkPaint::kRound_Join:
dst->addRoundRect(r, radius, radius, dir);
break;
default:
break;
}
if (fWidth < std::min(rw, rh) && !fDoFill) {
r = rect;
r.inset(radius, radius);
dst->addRect(r, reverse_direction(dir));
}
}
}