| 1 |
/* |
| 2 |
* Copyright 2006 The Android Open Source Project |
| 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 |
#include "src/core/SkStrokerPriv.h" |
| 8 |
|
| 9 |
#include "include/core/SkMatrix.h" |
| 10 |
#include "include/core/SkPathBuilder.h" |
| 11 |
#include "include/private/base/SkAssert.h" |
| 12 |
#include "src/core/SkGeometry.h" |
| 13 |
#include "src/core/SkPointPriv.h" |
| 14 |
|
| 15 |
#include <utility> |
| 16 |
|
| 17 |
static void ButtCapper(SkPathBuilder* sink, const SkPoint& pivot, const SkVector& normal, |
| 18 |
const SkPoint& stop, bool) { |
| 19 |
sink->lineTo(stop.fX, stop.fY); |
| 20 |
} |
| 21 |
|
| 22 |
static void RoundCapper(SkPathBuilder* sink, const SkPoint& pivot, const SkVector& normal, |
| 23 |
const SkPoint& stop, bool) { |
| 24 |
SkVector parallel; |
| 25 |
SkPointPriv::RotateCW(normal, ¶llel); |
| 26 |
|
| 27 |
SkPoint projectedCenter = pivot + parallel; |
| 28 |
|
| 29 |
sink->conicTo(projectedCenter + normal, projectedCenter, SK_ScalarRoot2Over2); |
| 30 |
sink->conicTo(projectedCenter - normal, stop, SK_ScalarRoot2Over2); |
| 31 |
} |
| 32 |
|
| 33 |
static void SquareCapper(SkPathBuilder* sink, const SkPoint& pivot, const SkVector& normal, |
| 34 |
const SkPoint& stop, bool extendLastPt) { |
| 35 |
SkVector parallel; |
| 36 |
SkPointPriv::RotateCW(normal, ¶llel); |
| 37 |
|
| 38 |
if (extendLastPt) { |
| 39 |
sink->setLastPoint(pivot + normal + parallel); |
| 40 |
sink->lineTo(pivot - normal + parallel); |
| 41 |
} else { |
| 42 |
sink->lineTo(pivot + normal + parallel); |
| 43 |
sink->lineTo(pivot - normal + parallel); |
| 44 |
sink->lineTo(stop); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
///////////////////////////////////////////////////////////////////////////// |
| 49 |
|
| 50 |
static bool is_clockwise(const SkVector& before, const SkVector& after) { |
| 51 |
return before.fX * after.fY > before.fY * after.fX; |
| 52 |
} |
| 53 |
|
| 54 |
enum AngleType { |
| 55 |
kNearly180_AngleType, |
| 56 |
kSharp_AngleType, |
| 57 |
kShallow_AngleType, |
| 58 |
kNearlyLine_AngleType |
| 59 |
}; |
| 60 |
|
| 61 |
static AngleType Dot2AngleType(SkScalar dot) { |
| 62 |
// need more precise fixed normalization |
| 63 |
// SkASSERT(SkScalarAbs(dot) <= SK_Scalar1 + SK_ScalarNearlyZero); |
| 64 |
|
| 65 |
if (dot >= 0) { // shallow or line |
| 66 |
return SkScalarNearlyZero(SK_Scalar1 - dot) ? kNearlyLine_AngleType : kShallow_AngleType; |
| 67 |
} else { // sharp or 180 |
| 68 |
return SkScalarNearlyZero(SK_Scalar1 + dot) ? kNearly180_AngleType : kSharp_AngleType; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
static void HandleInnerJoin(SkPathBuilder* inner, const SkPoint& pivot, const SkVector& after) { |
| 73 |
#if 1 |
| 74 |
/* In the degenerate case that the stroke radius is larger than our segments |
| 75 |
just connecting the two inner segments may "show through" as a funny |
| 76 |
diagonal. To pseudo-fix this, we go through the pivot point. This adds |
| 77 |
an extra point/edge, but I can't see a cheap way to know when this is |
| 78 |
not needed :( |
| 79 |
*/ |
| 80 |
inner->lineTo(pivot.fX, pivot.fY); |
| 81 |
#endif |
| 82 |
|
| 83 |
inner->lineTo(pivot.fX - after.fX, pivot.fY - after.fY); |
| 84 |
} |
| 85 |
|
| 86 |
static void BluntJoiner(SkPathBuilder* outer, SkPathBuilder* inner, |
| 87 |
const SkVector& beforeUnitNormal, |
| 88 |
const SkPoint& pivot, const SkVector& afterUnitNormal, |
| 89 |
SkScalar radius, SkScalar invMiterLimit, bool, bool) { |
| 90 |
SkVector after; |
| 91 |
afterUnitNormal.scale(radius, &after); |
| 92 |
|
| 93 |
if (!is_clockwise(beforeUnitNormal, afterUnitNormal)) { |
| 94 |
using std::swap; |
| 95 |
swap(outer, inner); |
| 96 |
after.negate(); |
| 97 |
} |
| 98 |
|
| 99 |
outer->lineTo(pivot.fX + after.fX, pivot.fY + after.fY); |
| 100 |
HandleInnerJoin(inner, pivot, after); |
| 101 |
} |
| 102 |
|
| 103 |
static void RoundJoiner(SkPathBuilder* outer, SkPathBuilder* inner, |
| 104 |
const SkVector& beforeUnitNormal, |
| 105 |
const SkPoint& pivot, const SkVector& afterUnitNormal, |
| 106 |
SkScalar radius, SkScalar invMiterLimit, bool, bool) { |
| 107 |
SkScalar dotProd = SkPoint::DotProduct(beforeUnitNormal, afterUnitNormal); |
| 108 |
AngleType angleType = Dot2AngleType(dotProd); |
| 109 |
|
| 110 |
if (angleType == kNearlyLine_AngleType) |
| 111 |
return; |
| 112 |
|
| 113 |
SkVector before = beforeUnitNormal; |
| 114 |
SkVector after = afterUnitNormal; |
| 115 |
SkPathDirection dir = SkPathDirection::kCW; |
| 116 |
|
| 117 |
if (!is_clockwise(before, after)) { |
| 118 |
using std::swap; |
| 119 |
swap(outer, inner); |
| 120 |
before.negate(); |
| 121 |
after.negate(); |
| 122 |
dir = SkPathDirection::kCCW; |
| 123 |
} |
| 124 |
|
| 125 |
SkMatrix matrix; |
| 126 |
matrix.setScale(radius, radius); |
| 127 |
matrix.postTranslate(pivot.fX, pivot.fY); |
| 128 |
SkConic conics[SkConic::kMaxConicsForArc]; |
| 129 |
int count = SkConic::BuildUnitArc(before, after, dir, &matrix, conics); |
| 130 |
if (count > 0) { |
| 131 |
for (int i = 0; i < count; ++i) { |
| 132 |
outer->conicTo(conics[i].fPts[1], conics[i].fPts[2], conics[i].fW); |
| 133 |
} |
| 134 |
after.scale(radius); |
| 135 |
HandleInnerJoin(inner, pivot, after); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
#define kOneOverSqrt2 (0.707106781f) |
| 140 |
|
| 141 |
static void MiterJoiner(SkPathBuilder* outer, SkPathBuilder* inner, |
| 142 |
const SkVector& beforeUnitNormal, |
| 143 |
const SkPoint& pivot, const SkVector& afterUnitNormal, |
| 144 |
SkScalar radius, SkScalar invMiterLimit, |
| 145 |
bool prevIsLine, bool currIsLine) { |
| 146 |
// negate the dot since we're using normals instead of tangents |
| 147 |
SkScalar dotProd = SkPoint::DotProduct(beforeUnitNormal, afterUnitNormal); |
| 148 |
AngleType angleType = Dot2AngleType(dotProd); |
| 149 |
SkVector before = beforeUnitNormal; |
| 150 |
SkVector after = afterUnitNormal; |
| 151 |
SkVector mid; |
| 152 |
SkScalar sinHalfAngle; |
| 153 |
bool ccw; |
| 154 |
|
| 155 |
if (angleType == kNearlyLine_AngleType) { |
| 156 |
return; |
| 157 |
} |
| 158 |
if (angleType == kNearly180_AngleType) { |
| 159 |
currIsLine = false; |
| 160 |
goto DO_BLUNT; |
| 161 |
} |
| 162 |
|
| 163 |
ccw = !is_clockwise(before, after); |
| 164 |
if (ccw) { |
| 165 |
using std::swap; |
| 166 |
swap(outer, inner); |
| 167 |
before.negate(); |
| 168 |
after.negate(); |
| 169 |
} |
| 170 |
|
| 171 |
/* Before we enter the world of square-roots and divides, |
| 172 |
check if we're trying to join an upright right angle |
| 173 |
(common case for stroking rectangles). If so, special case |
| 174 |
that (for speed an accuracy). |
| 175 |
Note: we only need to check one normal if dot==0 |
| 176 |
*/ |
| 177 |
if (0 == dotProd && invMiterLimit <= kOneOverSqrt2) { |
| 178 |
mid = (before + after) * radius; |
| 179 |
goto DO_MITER; |
| 180 |
} |
| 181 |
|
| 182 |
/* midLength = radius / sinHalfAngle |
| 183 |
if (midLength > miterLimit * radius) abort |
| 184 |
if (radius / sinHalf > miterLimit * radius) abort |
| 185 |
if (1 / sinHalf > miterLimit) abort |
| 186 |
if (1 / miterLimit > sinHalf) abort |
| 187 |
My dotProd is opposite sign, since it is built from normals and not tangents |
| 188 |
hence 1 + dot instead of 1 - dot in the formula |
| 189 |
*/ |
| 190 |
sinHalfAngle = SkScalarSqrt(SkScalarHalf(SK_Scalar1 + dotProd)); |
| 191 |
if (sinHalfAngle < invMiterLimit) { |
| 192 |
currIsLine = false; |
| 193 |
goto DO_BLUNT; |
| 194 |
} |
| 195 |
|
| 196 |
// choose the most accurate way to form the initial mid-vector |
| 197 |
if (angleType == kSharp_AngleType) { |
| 198 |
mid.set(after.fY - before.fY, before.fX - after.fX); |
| 199 |
if (ccw) { |
| 200 |
mid.negate(); |
| 201 |
} |
| 202 |
} else { |
| 203 |
mid = before + after; |
| 204 |
} |
| 205 |
|
| 206 |
mid.setLength(radius / sinHalfAngle); |
| 207 |
DO_MITER: |
| 208 |
if (prevIsLine) { |
| 209 |
outer->setLastPoint(pivot + mid); |
| 210 |
} else { |
| 211 |
outer->lineTo(pivot + mid); |
| 212 |
} |
| 213 |
|
| 214 |
DO_BLUNT: |
| 215 |
after.scale(radius); |
| 216 |
if (!currIsLine) { |
| 217 |
outer->lineTo(pivot.fX + after.fX, pivot.fY + after.fY); |
| 218 |
} |
| 219 |
HandleInnerJoin(inner, pivot, after); |
| 220 |
} |
| 221 |
|
| 222 |
///////////////////////////////////////////////////////////////////////////// |
| 223 |
|
| 224 |
SkStrokerPriv::CapProc SkStrokerPriv::CapFactory(SkPaint::Cap cap) { |
| 225 |
const SkStrokerPriv::CapProc gCappers[] = { |
| 226 |
ButtCapper, RoundCapper, SquareCapper |
| 227 |
}; |
| 228 |
|
| 229 |
SkASSERT((unsigned)cap < SkPaint::kCapCount); |
| 230 |
return gCappers[cap]; |
| 231 |
} |
| 232 |
|
| 233 |
SkStrokerPriv::JoinProc SkStrokerPriv::JoinFactory(SkPaint::Join join) { |
| 234 |
const SkStrokerPriv::JoinProc gJoiners[] = { |
| 235 |
MiterJoiner, RoundJoiner, BluntJoiner |
| 236 |
}; |
| 237 |
|
| 238 |
SkASSERT((unsigned)join < SkPaint::kJoinCount); |
| 239 |
return gJoiners[join]; |
| 240 |
} |