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 1 commit
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 more when the effective change (measured with the sagitta) is under the error.
KTibow marked this conversation as resolved.
Show resolved Hide resolved
default: true
removeUseless:
description: Remove redundant path commands that don't draw anything.
default: true
Expand Down
49 changes: 48 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 @@ -404,6 +407,8 @@ function filters(
var sdata = data,
circle;

const sagitta = command === 'a' ? calculateSagitta(data) : undefined;
KTibow marked this conversation as resolved.
Show resolved Hide resolved

if (command === 's') {
sdata = [0, 0].concat(data);

Expand Down Expand Up @@ -638,6 +643,28 @@ 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
if (
params.smartArcRounding &&
command === 'a' &&
KTibow marked this conversation as resolved.
Show resolved Hide resolved
sagitta !== undefined &&
precision
) {
for (let precisionNew = precision; precisionNew >= 0; precisionNew--) {
const radius = toFixed(data[0], precisionNew);
const sagittaNew = /** @type {number} */ (
calculateSagitta([radius, radius].concat(data.slice(2)))
KTibow marked this conversation as resolved.
Show resolved Hide resolved
);
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 +687,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 +1096,21 @@ function isCurveStraightLine(data) {
return true;
}

/**
* Calculates the sagitta of an arc if possible.
*
* @type {(data: number[]) => number | undefined}
*/
KTibow marked this conversation as resolved.
Show resolved Hide resolved
function calculateSagitta(data) {
const rx = data[0],
ry = data[1],
distance = Math.sqrt(Math.pow(data[5], 2) + Math.pow(data[6], 2));
if (Math.abs(rx - ry) > error) return undefined;
if (distance / 2 > rx) return undefined;
KTibow marked this conversation as resolved.
Show resolved Hide resolved
if (data[3] == 1) return undefined;
KTibow marked this conversation as resolved.
Show resolved Hide resolved
return rx - Math.sqrt(Math.pow(rx, 2) - 0.25 * Math.pow(distance, 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: 4 additions & 0 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.