Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(convertPathData): use the sagitta of arcs to round and convert to lines when applicable #1873

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/03-plugins/convert-path-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ svgo:
transformPrecision:
description: Number of decimal places to round to, using conventional rounding rules.
default: 5
smartArcRounding:
description: Round the radius of circular arcs when the effective change is under the error. The effective change is determined using the <a href="https://wikipedia.org/wiki/Sagitta_(geometry)" target="_blank">sagitta</a> of the arc.
default: true
removeUseless:
description: Remove redundant path commands that don't draw anything.
default: true
Expand Down
44 changes: 43 additions & 1 deletion plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ let arcTolerance;
* curveSmoothShorthands: boolean,
* floatPrecision: number | false,
* transformPrecision: number,
* smartArcRounding: boolean,
* removeUseless: boolean,
* collapseRepeated: boolean,
* utilizeAbsolute: boolean,
Expand Down Expand Up @@ -100,6 +101,7 @@ exports.fn = (root, params) => {
curveSmoothShorthands = true,
floatPrecision = 3,
transformPrecision = 5,
smartArcRounding = true,
removeUseless = true,
collapseRepeated = true,
utilizeAbsolute = true,
Expand All @@ -122,6 +124,7 @@ exports.fn = (root, params) => {
curveSmoothShorthands,
floatPrecision,
transformPrecision,
smartArcRounding,
removeUseless,
collapseRepeated,
utilizeAbsolute,
Expand Down Expand Up @@ -638,6 +641,24 @@ function filters(
}
}

// round arc radius more accurately
// eg m 0 0 a 1234.567 1234.567 0 0 1 10 0 -> m 0 0 a 1235 1235 0 0 1 10 0
const sagitta = command === 'a' ? calculateSagitta(data) : undefined;
if (params.smartArcRounding && sagitta !== undefined && precision) {
for (let precisionNew = precision; precisionNew >= 0; precisionNew--) {
const radius = toFixed(data[0], precisionNew);
const sagittaNew = /** @type {number} */ (
calculateSagitta([radius, radius, ...data.slice(2)])
);
if (Math.abs(sagitta - sagittaNew) < error) {
data[0] = radius;
data[1] = radius;
} else {
break;
}
}
}

// convert straight curves into lines segments
if (params.straightCurves) {
if (
Expand All @@ -660,7 +681,12 @@ function filters(
) {
command = 'l';
data = data.slice(-2);
} else if (command === 'a' && (data[0] === 0 || data[1] === 0)) {
} else if (
command === 'a' &&
(data[0] === 0 ||
data[1] === 0 ||
(sagitta !== undefined && sagitta < error))
) {
command = 'l';
data = data.slice(-2);
}
Expand Down Expand Up @@ -1064,6 +1090,22 @@ function isCurveStraightLine(data) {
return true;
}

/**
* Calculates the sagitta of an arc if possible.
*
* @type {(data: number[]) => number | undefined}
* @see https://wikipedia.org/wiki/Sagitta_(geometry)#Formulas
*/
function calculateSagitta(data) {
if (data[3] === 1) return undefined;

const [rx, ry] = data;
if (Math.abs(rx - ry) > error) return undefined;
const chord = Math.sqrt(data[5] ** 2 + data[6] ** 2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use Math.hypot

Copy link
Member

@SethFalco SethFalco Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related: #1913 (comment)

Welcome to comment there too if you're interested. I forgot to make the change, but in another discussion, the conclusion was also to use Math.hypot instead of Math.sqrt(Math.pow, Math.pow). I'll update instances of this next time I'm working on SVGO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see, all the reasons were mentioned there. Notably, hypot has better precision which is more crucial for SVGO. Aside for being more convenient.

if (chord > rx * 2) return undefined;
return rx - Math.sqrt(rx ** 2 - 0.25 * chord ** 2);
}

/**
* Converts next curve from shorthand to full form using the current curve data.
*
Expand Down
1 change: 1 addition & 0 deletions plugins/plugins-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type DefaultPlugins = {
curveSmoothShorthands?: boolean;
floatPrecision?: number | false;
transformPrecision?: number;
smartArcRounding?: boolean;
removeUseless?: boolean;
collapseRepeated?: boolean;
utilizeAbsolute?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions test/plugins/convertPathData.14.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/convertPathData.30.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/plugins/convertPathData.31.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/plugins/convertPathData.32.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/plugins/convertPathData.33.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.