Skip to content

Commit

Permalink
Round <line> coordinates. (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkenny54 authored Oct 15, 2024
1 parent b13be8a commit ecd7bb3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
26 changes: 24 additions & 2 deletions plugins/round.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const NULL_COORD_CONTEXT = {
yDigits: null,
};

/** @type {Object<string,Set<string>>} */
const ROUNDABLE_XY_ELEMENTS = {
x1: new Set(['line']),
y1: new Set(['line']),
x2: new Set(['line']),
y2: new Set(['line']),
};

/**
* @type {import('./plugins-types.js').Plugin<'round'>}
*/
Expand Down Expand Up @@ -103,12 +111,26 @@ export const fn = (root, params, info) => {
);
break;
case 'x':
case 'x1':
case 'x2':
case 'width':
newVal = roundCoord(attValue, coordContext.xDigits);
if (
!ROUNDABLE_XY_ELEMENTS[attName] ||
ROUNDABLE_XY_ELEMENTS[attName].has(element.name)
) {
newVal = roundCoord(attValue, coordContext.xDigits);
}
break;
case 'y':
case 'y1':
case 'y2':
case 'height':
newVal = roundCoord(attValue, coordContext.yDigits);
if (
!ROUNDABLE_XY_ELEMENTS[attName] ||
ROUNDABLE_XY_ELEMENTS[attName].has(element.name)
) {
newVal = roundCoord(attValue, coordContext.yDigits);
}
break;
}
if (newVal) {
Expand Down
17 changes: 17 additions & 0 deletions test/plugins/round.09.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Round <line> coordinates.

===

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">
<line x1="1.234567" y1="2.345678" x2="6.456789" y2="8.567891" style="stroke:black"/>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">
<line x1="1.235" y1="2.346" x2="6.457" y2="8.568" style="stroke:black"/>
</svg>

@@@

{"coordDigits":2}

0 comments on commit ecd7bb3

Please sign in to comment.