159 QStringView numStr = QGuiSvg::trimmed(str, tiny12FeaturesOnly);
161 if (numStr.isEmpty()) {
167 if (numStr.endsWith(QLatin1Char(
'%'))) {
170 }
else if (numStr.endsWith(QLatin1String(
"px"))) {
173 }
else if (numStr.endsWith(QLatin1String(
"pc"))) {
176 }
else if (numStr.endsWith(QLatin1String(
"pt"))) {
179 }
else if (numStr.endsWith(QLatin1String(
"mm"))) {
182 }
else if (numStr.endsWith(QLatin1String(
"cm"))) {
185 }
else if (numStr.endsWith(QLatin1String(
"in"))) {
192 qreal len = toDouble(numStr, ok);
233 QStringView numStr = QGuiSvg::trimmed(str, tiny12FeaturesOnly);
235 if (numStr.isEmpty())
238 qreal unitFactor = 1.0;
239 if (numStr.endsWith(QLatin1String(
"deg"))) {
242 }
else if (numStr.endsWith(QLatin1String(
"grad"))) {
246 }
else if (numStr.endsWith(QLatin1String(
"rad"))) {
248 unitFactor = 180.0 / Q_PI;
249 }
else if (numStr.endsWith(QLatin1String(
"turn"))) {
258 qreal angle = numStr.toDouble(&ok);
262 return angle * unitFactor;
297 if (dataStr.isEmpty())
301 constexpr int maxElementCount = 0x7fff;
304 constexpr auto paramCountFor = [](
char16_t c) {
306 case 'm':
case 'l':
case 't':
return 2;
308 case 'h':
case 'v':
return 1;
309 case 's':
case 'q':
return 4;
319 char16_t lastMode = 0;
322 while (!dataStr.isEmpty()) {
323 while (dataStr.first().isSpace() && dataStr.length() > 1)
326 const char16_t rawCmd = dataStr.first().unicode();
327 char16_t cmd = rawCmd | 0x20;
329 int paramCount = paramCountFor(cmd);
330 if (paramCount == -1)
333 const bool isRelative = rawCmd >=
'a' && rawCmd <=
'z';
336 const char *pattern =
nullptr;
339 QVarLengthArray<qreal, 8> arg;
341 const qreal *num = arg.constData();
342 int count = arg.size();
345 if (count < paramCount)
348 QPointF offset = isRelative ? currPoint : QPointF {0., 0.};
351 currPoint = startPoint = QPointF {num[0], num[1]} + offset;
352 path.moveTo(startPoint);
361 currPoint = startPoint;
366 currPoint = QPointF {num[0], num[1]} + offset;
367 path.lineTo(currPoint);
371 currPoint.setX(num[0] + offset.x());
372 path.lineTo(currPoint);
376 currPoint.setY(num[0] + offset.y());
377 path.lineTo(currPoint);
381 QPointF c1 = QPointF {num[0], num[1]} + offset;
382 QPointF c2 = QPointF {num[2], num[3]} + offset;
383 QPointF e = QPointF {num[4], num[5]} + offset;
384 path.cubicTo(c1, c2, e);
391 if (lastMode ==
'c' || lastMode ==
's')
392 c1 = 2 * currPoint - ctrlPoint;
395 QPointF c2 = QPointF {num[0], num[1]} + offset;
396 QPointF e = QPointF {num[2], num[3]} + offset;
398 path.cubicTo(c1, c2, e);
404 QPointF c = QPointF {num[0], num[1]} + offset;
405 QPointF e = QPointF {num[2], num[3]} + offset;
412 QPointF e = QPointF {num[0], num[1]} + offset;
414 if (lastMode ==
'q' || lastMode ==
't')
415 c = 2 * currPoint - ctrlPoint;
426 qreal xAxisRotation = num[2];
427 qreal largeArcFlag = num[3];
428 qreal sweepFlag = num[4];
429 qreal ex = num[5] + offset.x();
430 qreal ey = num[6] + offset.y();
431 qreal curx = currPoint.x();
432 qreal cury = currPoint.y();
433 pathArc(path, rx, ry, xAxisRotation,
int(largeArcFlag),
434 int(sweepFlag), ex, ey, curx, cury);
436 currPoint = QPointF {ex, ey};
440 Q_ASSERT_X(
false,
"parsePath",
"command table out of sync with switch");
445 if (limitLength && path.elementCount() > maxElementCount) {
446 qCWarning(lcGuiSvg,
"Untrusted SVG path limit exceeded. Path truncated.");
452 if (cmd ==
'z' && count != 0)
489void pathArc(QPainterPath &path, qreal rx, qreal ry,
490 qreal x_axis_rotation,
int large_arc_flag,
491 int sweep_flag, qreal x, qreal y,
492 qreal curx, qreal cury)
495 if (QPointF(curx, cury) == QPointF(x, y))
498 const qreal Pr1 = rx * rx;
499 const qreal Pr2 = ry * ry;
502 if (qFuzzyIsNull(Pr1) || qFuzzyIsNull(Pr2)) {
510 qreal sin_th, cos_th;
511 qreal a00, a01, a10, a11;
512 qreal x0, y0, x1, y1, xc, yc;
513 qreal d, sfactor, sfactor_sq;
514 qreal th0, th1, th_arc;
516 qreal dx, dy, dx1, dy1, Px, Py, check;
521 sin_th = qSin(x_axis_rotation * (Q_PI / 180.0));
522 cos_th = qCos(x_axis_rotation * (Q_PI / 180.0));
524 dx = (curx - x) / 2.0;
525 dy = (cury - y) / 2.0;
526 dx1 = cos_th * dx + sin_th * dy;
527 dy1 = -sin_th * dx + cos_th * dy;
531 check = Px / Pr1 + Py / Pr2;
533 rx = rx * qSqrt(check);
534 ry = ry * qSqrt(check);
541 x0 = a00 * curx + a01 * cury;
542 y0 = a10 * curx + a11 * cury;
543 x1 = a00 * x + a01 * y;
544 y1 = a10 * x + a11 * y;
546
547
548
549
550 d = (x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0);
553 sfactor_sq = 1.0 / d - 0.25;
554 if (sfactor_sq < 0) sfactor_sq = 0;
555 sfactor = qSqrt(sfactor_sq);
556 if (sweep_flag == large_arc_flag) sfactor = -sfactor;
557 xc = 0.5 * (x0 + x1) - sfactor * (y1 - y0);
558 yc = 0.5 * (y0 + y1) + sfactor * (x1 - x0);
561 th0 = qAtan2(y0 - yc, x0 - xc);
562 th1 = qAtan2(y1 - yc, x1 - xc);
565 if (th_arc < 0 && sweep_flag)
567 else if (th_arc > 0 && !sweep_flag)
570 n_segs = qCeil(qAbs(th_arc / (Q_PI * 0.5 + 0.001)));
572 for (i = 0; i < n_segs; i++) {
573 pathArcSegment(path, xc, yc,
574 th0 + i * th_arc / n_segs,
575 th0 + (i + 1) * th_arc / n_segs,
576 rx, ry, x_axis_rotation);