Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: src/core/SkPath.cpp

Issue 1613303002: Add svg path arcto (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add int-to-float (good catch, windows!) Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkBuffer.h" 8 #include "SkBuffer.h"
9 #include "SkCubicClipper.h" 9 #include "SkCubicClipper.h"
10 #include "SkErrorInternals.h" 10 #include "SkErrorInternals.h"
(...skipping 1239 matching lines...) Expand 10 before | Expand all | Expand 10 after
1250 const SkPoint& pt = conics[0].fPts[0]; 1250 const SkPoint& pt = conics[0].fPts[0];
1251 forceMoveTo ? this->moveTo(pt) : this->lineTo(pt); 1251 forceMoveTo ? this->moveTo(pt) : this->lineTo(pt);
1252 for (int i = 0; i < count; ++i) { 1252 for (int i = 0; i < count; ++i) {
1253 this->conicTo(conics[i].fPts[1], conics[i].fPts[2], conics[i].fW); 1253 this->conicTo(conics[i].fPts[1], conics[i].fPts[2], conics[i].fW);
1254 } 1254 }
1255 } else { 1255 } else {
1256 forceMoveTo ? this->moveTo(singlePt) : this->lineTo(singlePt); 1256 forceMoveTo ? this->moveTo(singlePt) : this->lineTo(singlePt);
1257 } 1257 }
1258 } 1258 }
1259 1259
1260 // This converts the SVG arc to conics.
1261 // Partly adapted from Niko's code in kdelibs/kdecore/svgicons.
1262 // Then transcribed from webkit/chrome's SVGPathNormalizer::decomposeArcToCubic( )
1263 // See also SVG implementation notes:
1264 // http://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter
1265 void SkPath::arcTo(SkScalar rx, SkScalar ry, SkScalar angle, bool arcLarge, bool arcSweep,
1266 SkScalar x, SkScalar y) {
1267 SkPoint srcPts[2];
1268 this->getLastPt(&srcPts[0]);
1269 // If rx = 0 or ry = 0 then this arc is treated as a straight line segment ( a "lineto")
1270 // joining the endpoints.
1271 // http://www.w3.org/TR/SVG/implnote.html#ArcOutOfRangeParameters
1272 if (!rx || !ry) {
1273 return;
1274 }
1275 // If the current point and target point for the arc are identical, it shoul d be treated as a
1276 // zero length path. This ensures continuity in animations.
1277 srcPts[1].set(x, y);
1278 if (srcPts[0] == srcPts[1]) {
1279 return;
1280 }
1281 rx = SkScalarAbs(rx);
1282 ry = SkScalarAbs(ry);
1283 SkVector midPointDistance = srcPts[0] - srcPts[1];
1284 midPointDistance *= 0.5f;
1285
1286 SkMatrix pointTransform;
1287 pointTransform.setRotate(-angle);
1288
1289 SkPoint transformedMidPoint;
1290 pointTransform.mapPoints(&transformedMidPoint, &midPointDistance, 1);
1291 SkScalar squareRx = rx * rx;
1292 SkScalar squareRy = ry * ry;
1293 SkScalar squareX = transformedMidPoint.fX * transformedMidPoint.fX;
1294 SkScalar squareY = transformedMidPoint.fY * transformedMidPoint.fY;
1295
1296 // Check if the radii are big enough to draw the arc, scale radii if not.
1297 // http://www.w3.org/TR/SVG/implnote.html#ArcCorrectionOutOfRangeRadii
1298 SkScalar radiiScale = squareX / squareRx + squareY / squareRy;
1299 if (radiiScale > 1) {
1300 radiiScale = SkScalarSqrt(radiiScale);
1301 rx *= radiiScale;
1302 ry *= radiiScale;
1303 }
1304
1305 pointTransform.setScale(1 / rx, 1 / ry);
1306 pointTransform.preRotate(-angle);
1307
1308 SkPoint unitPts[2];
1309 pointTransform.mapPoints(unitPts, srcPts, (int) SK_ARRAY_COUNT(unitPts));
1310 SkVector delta = unitPts[1] - unitPts[0];
1311
1312 SkScalar d = delta.fX * delta.fX + delta.fY * delta.fY;
1313 SkScalar scaleFactorSquared = SkTMax(1 / d - 0.25f, 0.f);
1314
1315 SkScalar scaleFactor = SkScalarSqrt(scaleFactorSquared);
1316 if (arcSweep == arcLarge) {
1317 scaleFactor = -scaleFactor;
1318 }
1319 delta.scale(scaleFactor);
1320 SkPoint centerPoint = unitPts[0] + unitPts[1];
1321 centerPoint *= 0.5f;
1322 centerPoint.offset(-delta.fY, delta.fX);
1323 unitPts[0] -= centerPoint;
1324 unitPts[1] -= centerPoint;
1325 SkScalar theta1 = SkScalarATan2(unitPts[0].fY, unitPts[0].fX);
1326 SkScalar theta2 = SkScalarATan2(unitPts[1].fY, unitPts[1].fX);
1327 SkScalar thetaArc = theta2 - theta1;
1328 if (thetaArc < 0 && arcSweep) {
1329 thetaArc += SK_ScalarPI * 2;
1330 } else if (thetaArc > 0 && !arcSweep) {
1331 thetaArc -= SK_ScalarPI * 2;
1332 }
1333 pointTransform.setRotate(angle);
1334 pointTransform.preScale(rx, ry);
1335
1336 int segments = SkScalarCeilToInt(SkScalarAbs(thetaArc / (SK_ScalarPI / 2)));
1337 SkScalar thetaWidth = thetaArc / segments;
1338 SkScalar t = SkScalarTan(0.5f * thetaWidth);
1339 if (!SkScalarIsFinite(t)) {
1340 return;
1341 }
1342 SkScalar startTheta = theta1;
1343 SkScalar w = SkScalarSqrt(SK_ScalarHalf + SkScalarCos(thetaWidth) * SK_Scala rHalf);
1344 for (int i = 0; i < segments; ++i) {
1345 SkScalar endTheta = startTheta + thetaWidth;
1346 SkScalar cosEndTheta, sinEndTheta = SkScalarSinCos(endTheta, &cosEndThet a);
1347
1348 unitPts[1].set(cosEndTheta, sinEndTheta);
1349 unitPts[1] += centerPoint;
1350 unitPts[0] = unitPts[1];
1351 unitPts[0].offset(t * sinEndTheta, -t * cosEndTheta);
1352 SkPoint mapped[2];
1353 pointTransform.mapPoints(mapped, unitPts, (int) SK_ARRAY_COUNT(unitPts)) ;
1354 this->conicTo(mapped[0], mapped[1], w);
1355 startTheta = endTheta;
1356 }
1357 }
1358
1359 void SkPath::rArcTo(SkScalar rx, SkScalar ry, SkScalar xAxisRotate, bool largeAr c, bool sweep,
1360 SkScalar dx, SkScalar dy) {
1361 SkPoint currentPoint;
1362 this->getLastPt(&currentPoint);
1363 this->arcTo(rx, ry, xAxisRotate, largeArc, sweep, currentPoint.fX + dx, curr entPoint.fY + dy);
1364 }
1365
1260 void SkPath::addArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle ) { 1366 void SkPath::addArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle ) {
1261 if (oval.isEmpty() || 0 == sweepAngle) { 1367 if (oval.isEmpty() || 0 == sweepAngle) {
1262 return; 1368 return;
1263 } 1369 }
1264 1370
1265 const SkScalar kFullCircleAngle = SkIntToScalar(360); 1371 const SkScalar kFullCircleAngle = SkIntToScalar(360);
1266 1372
1267 if (sweepAngle >= kFullCircleAngle || sweepAngle <= -kFullCircleAngle) { 1373 if (sweepAngle >= kFullCircleAngle || sweepAngle <= -kFullCircleAngle) {
1268 this->addOval(oval, sweepAngle > 0 ? kCW_Direction : kCCW_Direction); 1374 this->addOval(oval, sweepAngle > 0 ? kCW_Direction : kCCW_Direction);
1269 } else { 1375 } else {
(...skipping 1826 matching lines...) Expand 10 before | Expand all | Expand 10 after
3096 } 3202 }
3097 } while (!done); 3203 } while (!done);
3098 return SkToBool(tangents.count()) ^ isInverse; 3204 return SkToBool(tangents.count()) ^ isInverse;
3099 } 3205 }
3100 3206
3101 int SkPath::ConvertConicToQuads(const SkPoint& p0, const SkPoint& p1, const SkPo int& p2, 3207 int SkPath::ConvertConicToQuads(const SkPoint& p0, const SkPoint& p1, const SkPo int& p2,
3102 SkScalar w, SkPoint pts[], int pow2) { 3208 SkScalar w, SkPoint pts[], int pow2) {
3103 const SkConic conic(p0, p1, p2, w); 3209 const SkConic conic(p0, p1, p2, w);
3104 return conic.chopIntoQuadsPOW2(pts, pow2); 3210 return conic.chopIntoQuadsPOW2(pts, pow2);
3105 } 3211 }
OLDNEW
« include/core/SkPath.h ('K') | « include/core/SkPath.h ('k') | src/utils/SkParsePath.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698