Skip to content

Commit

Permalink
feat(convertPathData): allow converting q to t in more cases (#1889)
Browse files Browse the repository at this point in the history
  • Loading branch information
KTibow authored Dec 27, 2023
1 parent 967d2f1 commit f238d6a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
52 changes: 45 additions & 7 deletions plugins/convertPathData.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,13 @@ function filters(
relSubpoint = [0, 0],
pathBase = [0, 0],
prev = {};
/** @type {Point | undefined} */
let qControlPoint;

path = path.filter(function (item, index, path) {
const qPoint = qControlPoint;
qControlPoint = undefined;

let command = item.command;
let data = item.args;
let next = path[index + 1];
Expand Down Expand Up @@ -795,14 +800,24 @@ function filters(
// t + q → t + t
else if (
// @ts-ignore
prev.command === 't' &&
// @ts-ignore
Math.abs(data[2] - prev.args[0]) < error &&
// @ts-ignore
Math.abs(data[3] - prev.args[1]) < error
prev.command === 't'
) {
command = 't';
data = data.slice(2);
// @ts-ignore
const predictedControlPoint = reflectPoint(qPoint, item.base);
const realControlPoint = [
// @ts-ignore
data[0] + item.base[0],
// @ts-ignore
data[1] + item.base[1],
];
if (
Math.abs(predictedControlPoint[0] - realControlPoint[0]) <
error &&
Math.abs(predictedControlPoint[1] - realControlPoint[1]) < error
) {
command = 't';
data = data.slice(2);
}
}
}
}
Expand Down Expand Up @@ -873,6 +888,18 @@ function filters(
)
return false;

if (command === 'q') {
// @ts-ignore
qControlPoint = [data[0] + item.base[0], data[1] + item.base[1]];
} else if (command === 't') {
if (qPoint) {
// @ts-ignore
qControlPoint = reflectPoint(qPoint, item.base);
} else {
// @ts-ignore
qControlPoint = item.coords;
}
}
prev = item;
return true;
});
Expand Down Expand Up @@ -1138,6 +1165,17 @@ function getDistance(point1, point2) {
return Math.hypot(point1[0] - point2[0], point1[1] - point2[1]);
}

/**
* Reflects point across another point
*
* @param {Point} input
* @param {Point} base
* @returns {Point}
*/
function reflectPoint(input, base) {
return [2 * base[0] - input[0], 2 * base[1] - input[1]];
}

/**
* Returns coordinates of the curve point corresponding to the certain t
* a·(1 - t)³·p1 + b·(1 - t)²·t·p2 + c·(1 - t)·t²·p3 + d·t³·p4,
Expand Down
16 changes: 16 additions & 0 deletions test/plugins/convertPathData.34.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f238d6a

Please sign in to comment.