jpfun 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +134 -0
- package/dist/diagnostic.d.ts +27 -0
- package/dist/diagnostic.js +141 -0
- package/dist/functions/ASTtypes.d.ts +115 -0
- package/dist/functions/ASTtypes.js +187 -0
- package/dist/functions/bar/index.d.ts +2 -0
- package/dist/functions/bar/index.js +180 -0
- package/dist/functions/beam/auto.d.ts +4 -0
- package/dist/functions/beam/auto.js +202 -0
- package/dist/functions/beam/index.d.ts +2 -0
- package/dist/functions/beam/index.js +54 -0
- package/dist/functions/beam/layout.d.ts +9 -0
- package/dist/functions/beam/layout.js +233 -0
- package/dist/functions/box/index.d.ts +2 -0
- package/dist/functions/box/index.js +202 -0
- package/dist/functions/br/index.d.ts +2 -0
- package/dist/functions/br/index.js +83 -0
- package/dist/functions/dash/index.d.ts +2 -0
- package/dist/functions/dash/index.js +61 -0
- package/dist/functions/default.d.ts +2 -0
- package/dist/functions/default.js +32 -0
- package/dist/functions/div/index.d.ts +50 -0
- package/dist/functions/div/index.js +190 -0
- package/dist/functions/dot/index.d.ts +2 -0
- package/dist/functions/dot/index.js +160 -0
- package/dist/functions/grace/index.d.ts +84 -0
- package/dist/functions/grace/index.js +464 -0
- package/dist/functions/key/index.d.ts +2 -0
- package/dist/functions/key/index.js +40 -0
- package/dist/functions/note/accidentals.d.ts +10 -0
- package/dist/functions/note/accidentals.js +60 -0
- package/dist/functions/note/index.d.ts +2 -0
- package/dist/functions/note/index.js +284 -0
- package/dist/functions/note/noteNameFSM.d.ts +8 -0
- package/dist/functions/note/noteNameFSM.js +192 -0
- package/dist/functions/over/index.d.ts +2 -0
- package/dist/functions/over/index.js +307 -0
- package/dist/functions/page/index.d.ts +20 -0
- package/dist/functions/page/index.js +63 -0
- package/dist/functions/set/index.d.ts +2 -0
- package/dist/functions/set/index.js +55 -0
- package/dist/functions/stack/index.d.ts +2 -0
- package/dist/functions/stack/index.js +164 -0
- package/dist/functions/tempo/index.d.ts +2 -0
- package/dist/functions/tempo/index.js +37 -0
- package/dist/functions/text/index.d.ts +2 -0
- package/dist/functions/text/index.js +65 -0
- package/dist/functions/tie/index.d.ts +2 -0
- package/dist/functions/tie/index.js +301 -0
- package/dist/functions/voice/index.d.ts +3 -0
- package/dist/functions/voice/index.js +714 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/layout/default.d.ts +12 -0
- package/dist/layout/default.js +27 -0
- package/dist/layout/engine.d.ts +47 -0
- package/dist/layout/engine.js +410 -0
- package/dist/layout/model.d.ts +73 -0
- package/dist/layout/model.js +422 -0
- package/dist/layout/page.d.ts +27 -0
- package/dist/layout/page.js +121 -0
- package/dist/layout/types.d.ts +197 -0
- package/dist/layout/types.js +1 -0
- package/dist/lowering/loweringContext.d.ts +126 -0
- package/dist/lowering/loweringContext.js +625 -0
- package/dist/lowering/track.d.ts +61 -0
- package/dist/lowering/track.js +35 -0
- package/dist/lowering/types.d.ts +176 -0
- package/dist/lowering/types.js +82 -0
- package/dist/parser/grammarType.d.ts +81 -0
- package/dist/parser/grammarType.js +1 -0
- package/dist/parser/parse-utils/brace-utils.d.ts +1 -0
- package/dist/parser/parse-utils/brace-utils.js +29 -0
- package/dist/parser/parse-utils/call-utils.d.ts +24 -0
- package/dist/parser/parse-utils/call-utils.js +178 -0
- package/dist/parser/parse-utils/label-utils.d.ts +4 -0
- package/dist/parser/parse-utils/label-utils.js +17 -0
- package/dist/parser/parse-utils/length-utils.d.ts +3 -0
- package/dist/parser/parse-utils/length-utils.js +20 -0
- package/dist/parser/parse-utils/note-utils.d.ts +10 -0
- package/dist/parser/parse-utils/note-utils.js +111 -0
- package/dist/parser/parserContext.d.ts +93 -0
- package/dist/parser/parserContext.js +542 -0
- package/dist/parser/preprocess.d.ts +6 -0
- package/dist/parser/preprocess.js +193 -0
- package/dist/parser/types.d.ts +31 -0
- package/dist/parser/types.js +2 -0
- package/dist/pipeline.d.ts +36 -0
- package/dist/pipeline.js +51 -0
- package/dist/render/canvas.d.ts +19 -0
- package/dist/render/canvas.js +121 -0
- package/dist/render/index.d.ts +5 -0
- package/dist/render/index.js +5 -0
- package/dist/render/recording.d.ts +46 -0
- package/dist/render/recording.js +76 -0
- package/dist/render/svg.d.ts +18 -0
- package/dist/render/svg.js +126 -0
- package/dist/render/text.d.ts +6 -0
- package/dist/render/text.js +17 -0
- package/dist/render/types.d.ts +86 -0
- package/dist/render/types.js +1 -0
- package/grammar.md +123 -0
- package/package.json +58 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
+
import { TemporalNodeBase } from "../../lowering/types.js";
|
|
3
|
+
class TextFunction extends ASTFunctionNode {
|
|
4
|
+
static def = {
|
|
5
|
+
name: ["text"],
|
|
6
|
+
description: "文本标记。单独写在谱中是文本对象;也可作为 up 的单个可见成员",
|
|
7
|
+
example: `@text(进入主题)`,
|
|
8
|
+
allowExtraArgs: false,
|
|
9
|
+
args: [
|
|
10
|
+
{
|
|
11
|
+
type: "string",
|
|
12
|
+
default: null,
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
name: "size",
|
|
16
|
+
type: "length",
|
|
17
|
+
default: {
|
|
18
|
+
value: 1,
|
|
19
|
+
unit: "em",
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
],
|
|
23
|
+
};
|
|
24
|
+
text;
|
|
25
|
+
size;
|
|
26
|
+
constructor(sourceSpan, args, ctx, parent = null) {
|
|
27
|
+
super(sourceSpan, parent);
|
|
28
|
+
const [txt, sz] = this.getArgValue(args, ctx);
|
|
29
|
+
this.text = txt;
|
|
30
|
+
this.size = ctx.length2px(sz);
|
|
31
|
+
}
|
|
32
|
+
loweringEnter() { return [new TextTemporalNode(this)]; }
|
|
33
|
+
toString() { return `@text(${this.text})`; }
|
|
34
|
+
}
|
|
35
|
+
export const TextNode = TextFunction;
|
|
36
|
+
class TextTemporalNode extends TemporalNodeBase {
|
|
37
|
+
textBaselineY = 0;
|
|
38
|
+
constructor(ast) {
|
|
39
|
+
super();
|
|
40
|
+
this.ast = ast;
|
|
41
|
+
this.T = 0;
|
|
42
|
+
this.type = 2 /* ColType.DEFAULT */;
|
|
43
|
+
this.initLayoutBox();
|
|
44
|
+
}
|
|
45
|
+
prepareLayout(context) {
|
|
46
|
+
const style = {
|
|
47
|
+
fontSize: this.ast.size,
|
|
48
|
+
fill: "#000",
|
|
49
|
+
};
|
|
50
|
+
const metrics = context.textMeasurer.measureText(this.ast.text, style);
|
|
51
|
+
const firstChar = [...this.ast.text][0] ?? "";
|
|
52
|
+
const firstCharWidth = firstChar ? context.textMeasurer.measureText(firstChar, style).w : 0;
|
|
53
|
+
this.box.w = metrics.w;
|
|
54
|
+
this.box.h = metrics.h;
|
|
55
|
+
this.box.anchor = firstCharWidth / 2;
|
|
56
|
+
this.box.visualAxis = metrics.h / 2;
|
|
57
|
+
this.textBaselineY = metrics.baseline;
|
|
58
|
+
}
|
|
59
|
+
paint(painter) {
|
|
60
|
+
painter.drawText(this.ast.text, this.box.x, this.box.y + this.textBaselineY, {
|
|
61
|
+
fontSize: this.ast.size,
|
|
62
|
+
fill: "#000",
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
+
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
|
+
import { isVisualTemporalNode, } from "../../lowering/types.js";
|
|
4
|
+
/** 三次贝塞尔控制点的抬高系数:两个控制点同高时,实际弧高恰好是抬高的 3/4 */
|
|
5
|
+
const CUBIC_LIFT = 4 / 3;
|
|
6
|
+
/** 弧带中部最大厚度与字号之比 */
|
|
7
|
+
const THICKNESS_RATIO = 0.09;
|
|
8
|
+
class TieFunction extends ASTFunctionNode {
|
|
9
|
+
static def = {
|
|
10
|
+
name: ["tie"],
|
|
11
|
+
description: "连音线",
|
|
12
|
+
example: `@tie(label1, label2, ..., height=0.5em)
|
|
13
|
+
将端点依次连接;同行用一条弧线,跨行拆成分段连接;若不传端点则找最近的`,
|
|
14
|
+
allowExtraArgs: true,
|
|
15
|
+
extraArgType: "label",
|
|
16
|
+
args: []
|
|
17
|
+
};
|
|
18
|
+
endPoints = [];
|
|
19
|
+
height;
|
|
20
|
+
constructor(sourceSpan, args, ctx, parent = null) {
|
|
21
|
+
super(sourceSpan, parent);
|
|
22
|
+
let height = ctx.fontSize * 0.5;
|
|
23
|
+
for (const [key, value] of args) {
|
|
24
|
+
if (key === "height") {
|
|
25
|
+
const length = ctx.parseArgWithType(value.start, value.end, "length", sourceSpan.start);
|
|
26
|
+
if (length)
|
|
27
|
+
height = Math.max(0, ctx.length2px(length));
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const v = value instanceof ASTFunctionNode
|
|
31
|
+
? value
|
|
32
|
+
: ctx.parseArgWithType(value.start, value.end, "label", sourceSpan.start);
|
|
33
|
+
if (v !== null)
|
|
34
|
+
this.endPoints.push(v);
|
|
35
|
+
}
|
|
36
|
+
// 数目不足,则找最近的
|
|
37
|
+
let k = ctx.labelableNodes.length - 1;
|
|
38
|
+
for (let i = this.endPoints.length; i < 2; i++) {
|
|
39
|
+
while (k >= 0 && this.endPoints.includes(ctx.labelableNodes[k]))
|
|
40
|
+
k--;
|
|
41
|
+
if (k < 0)
|
|
42
|
+
break;
|
|
43
|
+
this.endPoints[1 - i] = ctx.labelableNodes[k--]; // 保持顺序
|
|
44
|
+
}
|
|
45
|
+
if (this.endPoints.length < 2)
|
|
46
|
+
throw new ErrorDiagnostic("E_NOT_ENOUGH_ARGS", "@tie 连音线需要至少两个端点", sourceSpan);
|
|
47
|
+
this.height = height;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* tie 不推进时间
|
|
51
|
+
* lowering 只把 AST 端点解析成稳定的 temporal 对象引用
|
|
52
|
+
*/
|
|
53
|
+
loweringEnter(ctx) {
|
|
54
|
+
const endPoints = [];
|
|
55
|
+
for (const ast of this.endPoints) {
|
|
56
|
+
const temporal = ctx.getTemporalNodes(ast).at(-1);
|
|
57
|
+
if (!temporal || !isVisualTemporalNode(temporal))
|
|
58
|
+
continue;
|
|
59
|
+
endPoints.push(temporal);
|
|
60
|
+
}
|
|
61
|
+
if (endPoints.length < 2)
|
|
62
|
+
return [];
|
|
63
|
+
ctx.addLayoutAttachment(new TieLayoutAttachment(endPoints, this.height));
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
toString(source) {
|
|
67
|
+
return `@tie(${this.endPoints.map(p => `${p.label ?? 'anon'}:[${p.toString(source)}]`).join(", ")})`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export const TieNode = TieFunction;
|
|
71
|
+
/** 二次贝塞尔在某一维上的内部极值 */
|
|
72
|
+
function quadExtremum(p0, p1, p2) {
|
|
73
|
+
const d = p0 - 2 * p1 + p2;
|
|
74
|
+
if (Math.abs(d) < 1e-9)
|
|
75
|
+
return null;
|
|
76
|
+
const t = (p0 - p1) / d;
|
|
77
|
+
if (t <= 0 || t >= 1)
|
|
78
|
+
return null;
|
|
79
|
+
const u = 1 - t;
|
|
80
|
+
return u * u * p0 + 2 * u * t * p1 + t * t * p2;
|
|
81
|
+
}
|
|
82
|
+
/** 三次贝塞尔在某一维上的内部极值,最多两个 */
|
|
83
|
+
function cubicExtrema(p0, p1, p2, p3, out) {
|
|
84
|
+
// 导数为 3(qa*t^2 + qb*t + a),下面的 a 就是常数项
|
|
85
|
+
const a = p1 - p0;
|
|
86
|
+
const b = p2 - p1;
|
|
87
|
+
const c = p3 - p2;
|
|
88
|
+
const qa = a - 2 * b + c;
|
|
89
|
+
const qb = 2 * (b - a);
|
|
90
|
+
const push = (t) => {
|
|
91
|
+
if (t <= 0 || t >= 1)
|
|
92
|
+
return;
|
|
93
|
+
const u = 1 - t;
|
|
94
|
+
out.push(u * u * u * p0 + 3 * u * u * t * p1 + 3 * u * t * t * p2 + t * t * t * p3);
|
|
95
|
+
};
|
|
96
|
+
if (Math.abs(qa) < 1e-9) {
|
|
97
|
+
if (Math.abs(qb) > 1e-9)
|
|
98
|
+
push(-a / qb);
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const disc = qb * qb - 4 * qa * a;
|
|
102
|
+
if (disc < 0)
|
|
103
|
+
return;
|
|
104
|
+
const root = Math.sqrt(disc);
|
|
105
|
+
push((-qb + root) / (2 * qa));
|
|
106
|
+
push((-qb - root) / (2 * qa));
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 精确求出路径的外接矩形
|
|
110
|
+
*
|
|
111
|
+
* 控制点不直接计入:贝塞尔曲线并不会到达控制点,
|
|
112
|
+
* 直接用它会把弧顶高估约三分之一,从而让上方的轨道被无谓推开
|
|
113
|
+
*/
|
|
114
|
+
function pathBounds(commands) {
|
|
115
|
+
let left = Infinity;
|
|
116
|
+
let top = Infinity;
|
|
117
|
+
let right = -Infinity;
|
|
118
|
+
let bottom = -Infinity;
|
|
119
|
+
const includeX = (x) => { left = Math.min(left, x); right = Math.max(right, x); };
|
|
120
|
+
const includeY = (y) => { top = Math.min(top, y); bottom = Math.max(bottom, y); };
|
|
121
|
+
let cx = 0;
|
|
122
|
+
let cy = 0;
|
|
123
|
+
const extrema = [];
|
|
124
|
+
for (const command of commands) {
|
|
125
|
+
if (command.op === "Z")
|
|
126
|
+
continue;
|
|
127
|
+
if (command.op === "Q") {
|
|
128
|
+
const ex = quadExtremum(cx, command.cx, command.x);
|
|
129
|
+
if (ex !== null)
|
|
130
|
+
includeX(ex);
|
|
131
|
+
const ey = quadExtremum(cy, command.cy, command.y);
|
|
132
|
+
if (ey !== null)
|
|
133
|
+
includeY(ey);
|
|
134
|
+
}
|
|
135
|
+
else if (command.op === "C") {
|
|
136
|
+
extrema.length = 0;
|
|
137
|
+
cubicExtrema(cx, command.cx1, command.cx2, command.x, extrema);
|
|
138
|
+
for (const value of extrema)
|
|
139
|
+
includeX(value);
|
|
140
|
+
extrema.length = 0;
|
|
141
|
+
cubicExtrema(cy, command.cy1, command.cy2, command.y, extrema);
|
|
142
|
+
for (const value of extrema)
|
|
143
|
+
includeY(value);
|
|
144
|
+
}
|
|
145
|
+
includeX(cx = command.x);
|
|
146
|
+
includeY(cy = command.y);
|
|
147
|
+
}
|
|
148
|
+
if (!Number.isFinite(left))
|
|
149
|
+
return { left: 0, top: 0, right: 0, bottom: 0 };
|
|
150
|
+
return { left, top, right, bottom };
|
|
151
|
+
}
|
|
152
|
+
class TieLayoutAttachment {
|
|
153
|
+
box = {
|
|
154
|
+
x: 0,
|
|
155
|
+
y: 0,
|
|
156
|
+
w: 0,
|
|
157
|
+
h: 0,
|
|
158
|
+
};
|
|
159
|
+
layer = "foreground";
|
|
160
|
+
endPoints;
|
|
161
|
+
height;
|
|
162
|
+
segments = [];
|
|
163
|
+
/** 弧带中部厚度;两端收尖到 0 */
|
|
164
|
+
thickness = 1;
|
|
165
|
+
constructor(endPoints, height) {
|
|
166
|
+
this.endPoints = endPoints;
|
|
167
|
+
this.height = height;
|
|
168
|
+
}
|
|
169
|
+
layout(context) {
|
|
170
|
+
this.updateGeometry(context);
|
|
171
|
+
return this.segments.map(segment => ({
|
|
172
|
+
x: segment.left,
|
|
173
|
+
y: segment.top,
|
|
174
|
+
w: segment.right - segment.left,
|
|
175
|
+
h: segment.bottom - segment.top,
|
|
176
|
+
line: segment.line,
|
|
177
|
+
track: segment.track,
|
|
178
|
+
}));
|
|
179
|
+
}
|
|
180
|
+
paint(painter) {
|
|
181
|
+
for (const segment of this.segments) {
|
|
182
|
+
// 弧带本身就是闭合轮廓,直接填充就能得到两端收尖、中部较粗的乐谱弧线
|
|
183
|
+
painter.drawPath(segment.commands, { fill: "#000" });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
absolutePort(node, name) {
|
|
187
|
+
const port = node.ports[name];
|
|
188
|
+
if (port) {
|
|
189
|
+
return {
|
|
190
|
+
x: node.box.x + port.x,
|
|
191
|
+
y: node.box.y + port.y,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
x: node.box.x + node.box.anchor,
|
|
196
|
+
y: node.box.y,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/** 根据端点固化后的行号选择同行弧线或跨行分段。 */
|
|
200
|
+
updateGeometry(context) {
|
|
201
|
+
const size = this.endPoints.reduce((current, endpoint) => Math.max(current, endpoint.ast.size), 0);
|
|
202
|
+
this.segments.length = 0;
|
|
203
|
+
this.thickness = Math.max(1.2, size * THICKNESS_RATIO);
|
|
204
|
+
for (let i = 1; i < this.endPoints.length; i++) {
|
|
205
|
+
let startNode = this.endPoints[i - 1];
|
|
206
|
+
let endNode = this.endPoints[i];
|
|
207
|
+
// 标签书写顺序不保证时间顺序,先按谱面行升序规范化
|
|
208
|
+
if (startNode.layoutLine > endNode.layoutLine)
|
|
209
|
+
[startNode, endNode] = [endNode, startNode];
|
|
210
|
+
const start = this.absolutePort(startNode, "tie.top");
|
|
211
|
+
const end = this.absolutePort(endNode, "tie.top");
|
|
212
|
+
if (startNode.layoutLine === endNode.layoutLine) {
|
|
213
|
+
const startTop = context.getVisualAxis(startNode.layoutLine, startNode.track)
|
|
214
|
+
+ (context.getHostExtent(startNode.layoutLine, startNode.track)?.top ?? 0);
|
|
215
|
+
const endTop = context.getVisualAxis(endNode.layoutLine, endNode.track)
|
|
216
|
+
+ (context.getHostExtent(endNode.layoutLine, endNode.track)?.top ?? 0);
|
|
217
|
+
this.addArc(start, end, startNode.layoutLine, startTop <= endTop ? startNode.track : endNode.track);
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
// 首行先起弧,再沿水平段延伸到当前谱面行最右侧
|
|
221
|
+
this.addOpeningSegment(start, context.originX + context.width, this.plateauY(context, startNode.layoutLine, startNode.track, start.y), startNode.layoutLine, startNode.track);
|
|
222
|
+
// 中间行可能没有任何可见对象,仍然需要逐行补一段水平线
|
|
223
|
+
for (let line = startNode.layoutLine + 1; line < endNode.layoutLine; line++) {
|
|
224
|
+
this.addHorizontalSegment(context.originX, context.originX + context.width, this.plateauY(context, line, startNode.track), line, startNode.track);
|
|
225
|
+
}
|
|
226
|
+
// 末行从最左侧进入,经过水平段后落弧到终点
|
|
227
|
+
this.addClosingSegment(context.originX, end, this.plateauY(context, endNode.layoutLine, endNode.track, end.y), endNode.layoutLine, endNode.track);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
/** 沿用普通端点的抬高量,但基准至少是当前 Track 的最高主体。 */
|
|
231
|
+
plateauY(context, line, track, endpointY = Infinity) {
|
|
232
|
+
const axis = context.getVisualAxis(line, track);
|
|
233
|
+
const hostTop = context.getHostExtent(line, track)?.top ?? 0;
|
|
234
|
+
return Math.min(endpointY, axis + hostTop) - this.height;
|
|
235
|
+
}
|
|
236
|
+
/** 一条三次贝塞尔的外缘配上反向内缘,得到两端收尖、中部最厚的乐谱弧线 */
|
|
237
|
+
addArc(startInput, endInput, line, track) {
|
|
238
|
+
let start = startInput;
|
|
239
|
+
let end = endInput;
|
|
240
|
+
if (start.x > end.x)
|
|
241
|
+
[start, end] = [end, start];
|
|
242
|
+
const run = (end.x - start.x) * 0.25;
|
|
243
|
+
const leftX = start.x + run;
|
|
244
|
+
const rightX = end.x - run;
|
|
245
|
+
const lift = this.height * CUBIC_LIFT;
|
|
246
|
+
const inner = Math.max(0, lift - this.thickness * CUBIC_LIFT);
|
|
247
|
+
const commands = [
|
|
248
|
+
{ op: "M", x: start.x, y: start.y },
|
|
249
|
+
{ op: "C", cx1: leftX, cy1: start.y - lift, cx2: rightX, cy2: end.y - lift, x: end.x, y: end.y },
|
|
250
|
+
{ op: "C", cx1: rightX, cy1: end.y - inner, cx2: leftX, cy2: start.y - inner, x: start.x, y: start.y },
|
|
251
|
+
{ op: "Z" },
|
|
252
|
+
];
|
|
253
|
+
this.addSegment(commands, line, track);
|
|
254
|
+
}
|
|
255
|
+
/** 跨行首段:从端点起弧,随后水平延伸到右边界 */
|
|
256
|
+
addOpeningSegment(start, right, plateauY, line, track) {
|
|
257
|
+
const span = Math.max(0, right - start.x);
|
|
258
|
+
const run = Math.min(this.height * 1.2, span * 0.4);
|
|
259
|
+
const curveX = start.x + run;
|
|
260
|
+
const controlX = start.x + run * 0.45;
|
|
261
|
+
const bottom = plateauY + this.thickness;
|
|
262
|
+
this.addSegment([
|
|
263
|
+
{ op: "M", x: start.x, y: start.y },
|
|
264
|
+
{ op: "Q", cx: controlX, cy: plateauY, x: curveX, y: plateauY },
|
|
265
|
+
{ op: "L", x: right, y: plateauY },
|
|
266
|
+
{ op: "L", x: right, y: bottom },
|
|
267
|
+
{ op: "L", x: curveX, y: bottom },
|
|
268
|
+
{ op: "Q", cx: controlX, cy: bottom, x: start.x, y: start.y },
|
|
269
|
+
{ op: "Z" },
|
|
270
|
+
], line, track);
|
|
271
|
+
}
|
|
272
|
+
/** 跨行末段:从左边界水平进入,最后落弧到端点 */
|
|
273
|
+
addClosingSegment(left, end, plateauY, line, track) {
|
|
274
|
+
const span = Math.max(0, end.x - left);
|
|
275
|
+
const run = Math.min(this.height * 1.2, span * 0.4);
|
|
276
|
+
const curveX = end.x - run;
|
|
277
|
+
const controlX = end.x - run * 0.45;
|
|
278
|
+
const bottom = plateauY + this.thickness;
|
|
279
|
+
this.addSegment([
|
|
280
|
+
{ op: "M", x: left, y: plateauY },
|
|
281
|
+
{ op: "L", x: curveX, y: plateauY },
|
|
282
|
+
{ op: "Q", cx: controlX, cy: plateauY, x: end.x, y: end.y },
|
|
283
|
+
{ op: "Q", cx: controlX, cy: bottom, x: curveX, y: bottom },
|
|
284
|
+
{ op: "L", x: left, y: bottom },
|
|
285
|
+
{ op: "Z" },
|
|
286
|
+
], line, track);
|
|
287
|
+
}
|
|
288
|
+
addHorizontalSegment(left, right, y, line, track) {
|
|
289
|
+
this.addSegment([
|
|
290
|
+
{ op: "M", x: left, y },
|
|
291
|
+
{ op: "L", x: right, y },
|
|
292
|
+
{ op: "L", x: right, y: y + this.thickness },
|
|
293
|
+
{ op: "L", x: left, y: y + this.thickness },
|
|
294
|
+
{ op: "Z" },
|
|
295
|
+
], line, track);
|
|
296
|
+
}
|
|
297
|
+
addSegment(commands, line, track) {
|
|
298
|
+
const bounds = pathBounds(commands);
|
|
299
|
+
this.segments.push({ commands, line, track, ...bounds });
|
|
300
|
+
}
|
|
301
|
+
}
|