treewide: dart format + analyze + some other fixes

This commit is contained in:
2025-08-04 21:11:37 +02:00
parent 9f9757d710
commit c83821fa38
10 changed files with 863 additions and 844 deletions

View File

@@ -1,6 +1,7 @@
/// SVG Path specification parser
///
/// See https://pypi.org/project/svg.path/ for the original implementation.
library;
import '../common/point.dart';
import 'path.dart';
@@ -25,7 +26,7 @@ const _commands = {
'T',
't',
'A',
'a'
'a',
};
// const _uppercaseCommands = {'M', 'Z', 'L', 'H', 'V', 'C', 'S', 'Q', 'T', 'A'};
@@ -181,8 +182,9 @@ class Token {
other is Token &&
command == other.command &&
args.length == other.args.length &&
![for (int i = 0; i < args.length; i++) args[i] == other.args[i]]
.any((b) => !b);
![
for (int i = 0; i < args.length; i++) args[i] == other.args[i],
].any((b) => !b);
@override
int get hashCode => command.hashCode ^ args.hashCode;
@@ -358,7 +360,8 @@ Path parsePath(String pathdef) {
// The control point is assumed to be the reflection of
// the control point on the previous command relative
// to the current point.
control = currentPos +
control =
currentPos +
currentPos -
(segments.last as QuadraticBezier).control;
} else {

View File

@@ -1,5 +1,6 @@
/// This file contains classes for the different types of SVG path segments as
/// well as a Path object that contains a sequence of path segments.
library;
import 'dart:collection';
import 'dart:math' as math;
@@ -69,10 +70,7 @@ abstract class SvgPath {
final Point start;
final Point end;
const SvgPath({
required this.start,
required this.end,
});
const SvgPath({required this.start, required this.end});
@override
bool operator ==(Object other) =>
@@ -89,10 +87,7 @@ abstract class SvgPath {
}
abstract class Bezier extends SvgPath {
const Bezier({
required Point start,
required Point end,
}) : super(start: start, end: end);
const Bezier({required super.start, required super.end});
@override
bool operator ==(Object other) => other is Bezier && super == other;
@@ -107,10 +102,7 @@ abstract class Bezier extends SvgPath {
/// A straight line
/// The base for Line() and Close().
class Linear extends SvgPath {
const Linear({
required Point start,
required Point end,
}) : super(start: start, end: end);
const Linear({required super.start, required super.end});
@override
bool operator ==(Object other) => other is Linear && super == other;
@@ -129,10 +121,7 @@ class Linear extends SvgPath {
}
class Line extends Linear {
const Line({
required Point start,
required Point end,
}) : super(start: start, end: end);
const Line({required super.start, required super.end});
@override
bool operator ==(Object other) => other is Line && super == other;
@@ -151,11 +140,11 @@ class CubicBezier extends Bezier {
final Point control2;
const CubicBezier({
required Point start,
required super.start,
required this.control1,
required this.control2,
required Point end,
}) : super(start: start, end: end);
required super.end,
});
@override
bool operator ==(Object other) =>
@@ -168,13 +157,14 @@ class CubicBezier extends Bezier {
int get hashCode => super.hashCode ^ control1.hashCode ^ control2.hashCode;
@override
String toString() => "CubicBezier(start=$start, control1=$control1, "
String toString() =>
"CubicBezier(start=$start, control1=$control1, "
"control2=$control2, end=$end)";
@override
bool isSmoothFrom(Object? previous) => previous is CubicBezier
? start == previous.end &&
control1 - start == previous.end - previous.control2
control1 - start == previous.end - previous.control2
: control1 == start;
@override
@@ -205,10 +195,10 @@ class QuadraticBezier extends Bezier {
final Point control;
const QuadraticBezier({
required Point start,
required Point end,
required super.start,
required super.end,
required this.control,
}) : super(start: start, end: end);
});
@override
bool operator ==(Object other) =>
@@ -224,7 +214,7 @@ class QuadraticBezier extends Bezier {
@override
bool isSmoothFrom(Object? previous) => previous is QuadraticBezier
? start == previous.end &&
(control - start) == (previous.end - previous.control)
(control - start) == (previous.end - previous.control)
: control == start;
@override
@@ -258,7 +248,8 @@ class QuadraticBezier extends Bezier {
final num c2 = 2 * sqrt(C);
final num bA = B / a2;
s = (a32 * sabc +
s =
(a32 * sabc +
a2 * B * (sabc - c2) +
(4 * C * A - (B * B)) * log((2 * a2 + bA + sabc) / (bA + c2))) /
(4 * a32);
@@ -280,13 +271,13 @@ class Arc extends SvgPath {
// late num delta;
const Arc({
required Point start,
required Point end,
required super.start,
required super.end,
required this.radius,
required this.rotation,
required this.arc,
required this.sweep,
}) : super(start: start, end: end);
});
@override
bool operator ==(Object other) =>
@@ -306,10 +297,10 @@ class Arc extends SvgPath {
sweep.hashCode;
@override
String toString() => 'Arc(start=$start, radius=$radius, rotation=$rotation, '
String toString() =>
'Arc(start=$start, radius=$radius, rotation=$rotation, '
'arc=$arc, sweep=$sweep, end=$end)';
// Conversion from endpoint to center parameterization
// http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
num get _cosr => cos(radians(rotation));
@@ -337,16 +328,17 @@ class Arc extends SvgPath {
num get _cyprim => -c * _ry * _x1prim / _rx;
num get radiusScale {
final rs = (_x1primSq / (radius.x * radius.x)) +
final rs =
(_x1primSq / (radius.x * radius.x)) +
(_y1primSq / (radius.y * radius.y));
return rs > 1 ? sqrt(rs) : 1;
}
Point get center => Point(
(_cosr * _cxprim - _sinr * _cyprim) + ((start.x + end.x) / 2),
(_sinr * _cxprim + _cosr * _cyprim) + ((start.y + end.y) / 2),
);
(_cosr * _cxprim - _sinr * _cyprim) + ((start.x + end.x) / 2),
(_sinr * _cxprim + _cosr * _cyprim) + ((start.y + end.y) / 2),
);
num get theta {
final num n = sqrt(_ux * _ux + _uy * _uy);
final num p = _ux;
@@ -365,7 +357,8 @@ class Arc extends SvgPath {
d = -1.0;
}
return ((((_ux * _vy - _uy * _vx) < 0) ? -1 : 1) * degrees(acos(d))) % 360 - (!sweep ? 360 : 0);
return ((((_ux * _vy - _uy * _vx) < 0) ? -1 : 1) * degrees(acos(d))) % 360 -
(!sweep ? 360 : 0);
}
@override
@@ -375,7 +368,7 @@ class Arc extends SvgPath {
// This should be treated as a straight line
if (this.radius.x == 0 || this.radius.y == 0) {
return start + (end - start) * pos;
return start + (end - start).times(pos);
}
final angle = radians(theta + pos * delta);
@@ -415,14 +408,15 @@ class Arc extends SvgPath {
final startPoint = point(0);
final endPoint = point(1);
return segmentLength(
curve: this,
start: 0,
end: 1,
startPoint: startPoint,
endPoint: endPoint,
error: error,
minDepth: minDepth,
depth: 0);
curve: this,
start: 0,
end: 1,
startPoint: startPoint,
endPoint: endPoint,
error: error,
minDepth: minDepth,
depth: 0,
);
}
}
@@ -449,10 +443,7 @@ class Move extends SvgPath {
/// Represents the closepath command
class Close extends Linear {
const Close({
required Point start,
required Point end,
}) : super(start: start, end: end);
const Close({required super.start, required super.end});
@override
bool operator ==(Object other) => other is Close && super == other;
@@ -502,12 +493,14 @@ class Path extends ListBase<SvgPath> {
String toString() =>
'Path(${[for (final s in segments) s.toString()].join(", ")})';
void _calcLengths(
{num error = defaultError, int minDepth = defaultMinDepth}) {
void _calcLengths({
num error = defaultError,
int minDepth = defaultMinDepth,
}) {
if (_memoizedLength != null) return;
final lengths = [
for (final s in segments) s!.size(error: error, minDepth: minDepth)
for (final s in segments) s!.size(error: error, minDepth: minDepth),
];
_memoizedLength = lengths.reduce((a, b) => a + b);
if (_memoizedLength == 0) {
@@ -552,7 +545,7 @@ class Path extends ListBase<SvgPath> {
return segments[i]!.point(segmentPos);
}
num size({error = defaultError, minDepth = defaultMinDepth}) {
num size({double error = defaultError, int minDepth = defaultMinDepth}) {
_calcLengths(error: error, minDepth: minDepth);
return _memoizedLength!;
}