Skip to content

Commit 85eca82

Browse files
authored
new scaler code
1 parent 5a27a85 commit 85eca82

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

src/addons/addons/custom-block-shape/userscript.js

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,54 @@ export default async function ({ addon, console }) {
77

88
const { GRID_UNIT } = BlockSvg;
99

10-
function scalePathXY(path, scaleX, scaleY) {
11-
const util = BlockSvg.CUSTOM_NOTCH_UTIL;
12-
const tokens = util.path2TokenList(path);
13-
const result = [];
14-
let i = 0;
15-
while (i < tokens.length) {
16-
const cmd = tokens[i++];
17-
result.push(cmd);
18-
19-
const expected = util.supportedCommands[cmd];
20-
const xIndexes = util.commandXpos[cmd] || [];
21-
22-
while (i + expected <= tokens.length && !/^[a-z]$/i.test(tokens[i])) {
23-
for (let j = 0; j < expected; j++) {
24-
let val = parseFloat(tokens[i + j]);
25-
if (isNaN(val)) {
26-
if (tokens[i + j] === "z") {
27-
result.push("z");
28-
break;
29-
}
30-
continue;
31-
}
32-
33-
if (xIndexes.includes(j)) val *= scaleX;
34-
else val *= scaleY;
35-
result.push(+val.toFixed(6));
10+
function path2SegmentList(path) {
11+
const cmds = structuredClone(BlockSvg.CUSTOM_NOTCH_UTIL.supportedCommands);
12+
cmds.z = 0;
13+
const segment = /([astvzqmhlc])([^astvzqmhlc]*)/ig;
14+
const data = [];
15+
path.replace(segment, (_, command, args) => {
16+
let type = command.toLowerCase();
17+
const numbers = args.match(/-?[0-9]*\.?[0-9]+(?:e[-+]?\d+)?/ig);
18+
args = numbers ? numbers.map(Number) : [];
19+
if (type == "m" && args.length > 2) {
20+
data.push([command].concat(args.splice(0, 2)));
21+
type = "l";
22+
command = command == "m" ? "l" : "L";
23+
}
24+
25+
while (true) {
26+
if (args.length == cmds[type]) {
27+
args.unshift(command);
28+
return data.push(args);
3629
}
37-
i += expected;
30+
if (args.length < cmds[type]) throw new Error("malformed path data");
31+
data.push([command].concat(args.splice(0, cmds[type])));
3832
}
39-
}
40-
return result.join(" ");
33+
});
34+
return data;
35+
}
36+
37+
function scalePathXY (path, scaleX, scaleY) {
38+
const segments = path2SegmentList(path);
39+
return segments.map((segment) => {
40+
const name = segment[0].toLowerCase();
41+
if (name === "v") {
42+
segment[1] *= scaleY;
43+
return segment;
44+
}
45+
if (name === "a") {
46+
segment[1] *= scaleX;
47+
segment[2] *= scaleY;
48+
segment[6] *= scaleX;
49+
segment[7] *= scaleY;
50+
return segment;
51+
}
52+
53+
return segment.map((val, i) => {
54+
if (!i) return val;
55+
return val *= i % 2 ? scaleX : scaleY;
56+
});
57+
}).flat().join(" ");
4158
}
4259

4360
function updateAllBlocks() {

0 commit comments

Comments
 (0)