3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkCornerPathEffect.h"
13 #include "SkReadBuffer.h"
14 #include "SkWriteBuffer.h"
16 SkCornerPathEffect::SkCornerPathEffect(SkScalar radius) : fRadius(radius) {}
17 SkCornerPathEffect::~SkCornerPathEffect() {}
19 static bool ComputeStep(const SkPoint& a, const SkPoint& b, SkScalar radius,
21 SkScalar dist = SkPoint::Distance(a, b);
23 step->set(b.fX - a.fX, b.fY - a.fY);
25 if (dist <= radius * 2) {
26 step->scale(SK_ScalarHalf);
29 step->scale(SkScalarDiv(radius, dist));
34 bool SkCornerPathEffect::filterPath(SkPath* dst, const SkPath& src,
35 SkStrokeRec*, const SkRect*) const {
40 SkPath::Iter iter(src, false);
41 SkPath::Verb verb, prevVerb = (SkPath::Verb)-1;
45 SkPoint moveTo, lastCorner;
46 SkVector firstStep, step;
47 bool prevIsValid = true;
55 switch (verb = iter.next(pts, false)) {
56 case SkPath::kMove_Verb:
57 // close out the previous (open) contour
58 if (SkPath::kLine_Verb == prevVerb) {
59 dst->lineTo(lastCorner);
61 closed = iter.isClosedContour();
70 case SkPath::kLine_Verb: {
71 bool drawSegment = ComputeStep(pts[0], pts[1], fRadius, &step);
74 dst->moveTo(moveTo + step);
77 dst->quadTo(pts[0].fX, pts[0].fY, pts[0].fX + step.fX,
81 dst->lineTo(pts[1].fX - step.fX, pts[1].fY - step.fY);
87 case SkPath::kQuad_Verb:
88 // TBD - just replicate the curve for now
93 dst->quadTo(pts[1], pts[2]);
97 case SkPath::kCubic_Verb:
102 // TBD - just replicate the curve for now
103 dst->cubicTo(pts[1], pts[2], pts[3]);
107 case SkPath::kClose_Verb:
108 if (firstStep.fX || firstStep.fY) {
109 dst->quadTo(lastCorner.fX, lastCorner.fY,
110 lastCorner.fX + firstStep.fX,
111 lastCorner.fY + firstStep.fY);
115 case SkPath::kConic_Verb:
118 case SkPath::kDone_Verb:
122 if (SkPath::kMove_Verb == prevVerb) {
131 SkFlattenable* SkCornerPathEffect::CreateProc(SkReadBuffer& buffer) {
132 return SkCornerPathEffect::Create(buffer.readScalar());
135 void SkCornerPathEffect::flatten(SkWriteBuffer& buffer) const {
136 buffer.writeScalar(fRadius);
139 #ifdef SK_SUPPORT_LEGACY_DEEPFLATTENING
140 SkCornerPathEffect::SkCornerPathEffect(SkReadBuffer& buffer) {
141 fRadius = buffer.readScalar();