jpfun 0.4.1 → 0.6.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/README.md +53 -113
- package/dist/converter/midi/convert.js +14 -7
- package/dist/converter/musicxml/features.d.ts +57 -16
- package/dist/converter/musicxml/features.js +204 -42
- package/dist/converter/musicxml/index.d.ts +7 -2
- package/dist/converter/musicxml/index.js +256 -268
- package/dist/converter/musicxml/model.d.ts +36 -7
- package/dist/converter/musicxml/resolve.d.ts +10 -0
- package/dist/converter/musicxml/resolve.js +152 -0
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/fraction.js +7 -1
- package/dist/functions/ASTtypes.d.ts +6 -3
- package/dist/functions/ASTtypes.js +30 -23
- package/dist/functions/adjust/index.js +10 -50
- package/dist/functions/arpeggio/index.js +40 -25
- package/dist/functions/beam/auto.js +2 -11
- package/dist/functions/box/index.js +132 -82
- package/dist/functions/dash/index.js +20 -11
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +1 -1
- package/dist/functions/div/index.js +9 -10
- package/dist/functions/dot/index.js +4 -5
- package/dist/functions/dyn/index.js +28 -32
- package/dist/functions/grace/index.d.ts +23 -8
- package/dist/functions/grace/index.js +138 -119
- package/dist/functions/head/index.js +11 -4
- package/dist/functions/key/index.js +1 -1
- package/dist/functions/meter/index.js +40 -7
- package/dist/functions/meter/shape.d.ts +3 -0
- package/dist/functions/meter/shape.js +43 -0
- package/dist/functions/note/index.d.ts +6 -1
- package/dist/functions/note/index.js +60 -57
- package/dist/functions/note/source-transform.d.ts +21 -0
- package/dist/functions/note/source-transform.js +164 -0
- package/dist/functions/page/index.d.ts +5 -0
- package/dist/functions/page/index.js +8 -5
- package/dist/functions/rest/index.d.ts +2 -0
- package/dist/functions/rest/index.js +233 -0
- package/dist/functions/set/index.js +5 -1
- package/dist/functions/symbol/index.d.ts +2 -0
- package/dist/functions/symbol/index.js +1 -1
- package/dist/functions/symbol/ornament.d.ts +3 -3
- package/dist/functions/symbol/ornament.js +21 -35
- package/dist/functions/symbol/symbols/accent.js +5 -5
- package/dist/functions/symbol/symbols/fermata.js +5 -4
- package/dist/functions/temporal.d.ts +21 -7
- package/dist/functions/temporal.js +14 -4
- package/dist/functions/tie/index.js +36 -42
- package/dist/functions/tuplet/index.js +20 -24
- package/dist/functions/up/index.js +54 -34
- package/dist/functions/voice/connections.d.ts +19 -0
- package/dist/functions/voice/connections.js +88 -0
- package/dist/functions/voice/index.js +248 -102
- package/dist/functions/volta/index.js +7 -12
- package/dist/jpfun.core.min.js +61 -41
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +70 -50
- package/dist/layout/engine.d.ts +13 -1
- package/dist/layout/engine.js +458 -129
- package/dist/layout/model.js +22 -66
- package/dist/layout/page.js +3 -1
- package/dist/layout/range.d.ts +26 -0
- package/dist/layout/range.js +75 -0
- package/dist/layout/types.d.ts +63 -49
- package/dist/lowering/loweringContext.d.ts +1 -1
- package/dist/lowering/loweringContext.js +34 -4
- package/dist/lowering/types.d.ts +5 -0
- package/dist/parser/parse-utils/length-utils.js +2 -0
- package/dist/parser/parse-utils/note-utils.d.ts +2 -0
- package/dist/parser/parse-utils/note-utils.js +6 -2
- package/dist/parser/parserContext.js +5 -9
- package/dist/parser/preprocess.js +4 -6
- package/dist/playback/compile.d.ts +2 -9
- package/dist/playback/compile.js +297 -186
- package/dist/playback/event.d.ts +8 -16
- package/dist/playback/event.js +13 -3
- package/dist/playback/types.d.ts +76 -18
- package/dist/playback/types.js +1 -0
- package/dist/render/index.d.ts +1 -1
- package/dist/render/index.js +1 -1
- package/dist/render/paint.js +1 -1
- package/dist/render/recording.d.ts +1 -0
- package/dist/render/recording.js +21 -0
- package/dist/render/translate.d.ts +14 -0
- package/dist/render/translate.js +48 -0
- package/grammar.md +200 -250
- package/package.json +4 -4
package/dist/playback/event.d.ts
CHANGED
|
@@ -48,7 +48,7 @@ interface PlaybackDraftEventBase {
|
|
|
48
48
|
order: number;
|
|
49
49
|
origins: PlaybackOrigin[];
|
|
50
50
|
}
|
|
51
|
-
/**
|
|
51
|
+
/** 声音展开后生成的点事件;最终输出前会校验并剥离内部字段 */
|
|
52
52
|
export interface PlaybackDraftNoteOnEvent extends PlaybackDraftEventBase {
|
|
53
53
|
kind: "note-on";
|
|
54
54
|
noteId: PlaybackNoteId;
|
|
@@ -78,26 +78,18 @@ export interface PlaybackDraftProgramChangeEvent extends PlaybackDraftEventBase
|
|
|
78
78
|
program: number;
|
|
79
79
|
}
|
|
80
80
|
export type PlaybackDraftEvent = PlaybackDraftNoteOnEvent | PlaybackDraftNoteOffEvent | PlaybackDraftTempoEvent | PlaybackDraftTimeSignatureEvent | PlaybackDraftProgramChangeEvent;
|
|
81
|
-
export
|
|
82
|
-
kind: "note-on";
|
|
83
|
-
at: Fraction;
|
|
84
|
-
noteId: PlaybackNoteId;
|
|
85
|
-
midi: number;
|
|
86
|
-
velocity: number;
|
|
87
|
-
percussion?: true;
|
|
88
|
-
transpose?: (steps: number) => number;
|
|
89
|
-
} | {
|
|
90
|
-
kind: "note-off";
|
|
91
|
-
at: Fraction;
|
|
92
|
-
noteId: PlaybackNoteId;
|
|
93
|
-
} | {
|
|
81
|
+
export interface PlaybackEventInput {
|
|
94
82
|
kind: "time-signature";
|
|
95
83
|
at: Fraction;
|
|
96
84
|
numerator: number;
|
|
97
85
|
denominator: number;
|
|
98
|
-
}
|
|
86
|
+
}
|
|
87
|
+
/** 同刻先处理系统状态,再关音和开音;相同类别保持原发布次序 */
|
|
99
88
|
export declare function comparePlaybackDraftEvents(left: PlaybackDraftEvent, right: PlaybackDraftEvent): number;
|
|
100
|
-
/**
|
|
89
|
+
/**
|
|
90
|
+
* 校验配对、压实发声 Track 编号并剥掉编译期字段,得到稳定、可序列化的公开事件
|
|
91
|
+
* 输入已经稳定排序;这里保留次序,将编译期的 Track 对象和音符配对转换为公开字段
|
|
92
|
+
*/
|
|
101
93
|
export declare function finalizePlaybackEvents(events: readonly PlaybackDraftEvent[], trackOrder: readonly Track[]): {
|
|
102
94
|
events: PlaybackEvent[];
|
|
103
95
|
tracks: Track[];
|
package/dist/playback/event.js
CHANGED
|
@@ -5,14 +5,19 @@ const EVENT_PRIORITY = {
|
|
|
5
5
|
"note-off": 3,
|
|
6
6
|
"note-on": 4,
|
|
7
7
|
};
|
|
8
|
+
/** 同刻先处理系统状态,再关音和开音;相同类别保持原发布次序 */
|
|
8
9
|
export function comparePlaybackDraftEvents(left, right) {
|
|
9
10
|
const byTime = left.at.compare(right.at);
|
|
10
11
|
return byTime || EVENT_PRIORITY[left.kind] - EVENT_PRIORITY[right.kind] || left.order - right.order;
|
|
11
12
|
}
|
|
12
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* 校验配对、压实发声 Track 编号并剥掉编译期字段,得到稳定、可序列化的公开事件
|
|
15
|
+
* 输入已经稳定排序;这里保留次序,将编译期的 Track 对象和音符配对转换为公开字段
|
|
16
|
+
*/
|
|
13
17
|
export function finalizePlaybackEvents(events, trackOrder) {
|
|
14
18
|
const noteOns = new Map();
|
|
15
19
|
const noteOffs = new Map();
|
|
20
|
+
// 同一音符的两端不一定相邻,先按 noteId 建立索引并拒绝重复的开音或关音
|
|
16
21
|
for (const event of events) {
|
|
17
22
|
if (event.kind === "note-on") {
|
|
18
23
|
if (noteOns.has(event.noteId))
|
|
@@ -25,6 +30,7 @@ export function finalizePlaybackEvents(events, trackOrder) {
|
|
|
25
30
|
noteOffs.set(event.noteId, event);
|
|
26
31
|
}
|
|
27
32
|
}
|
|
33
|
+
// 物理合并完成后仍必须一一配对,并保留正时值和可追溯的来源
|
|
28
34
|
for (const [noteId, on] of noteOns) {
|
|
29
35
|
const off = noteOffs.get(noteId);
|
|
30
36
|
if (!off)
|
|
@@ -34,25 +40,28 @@ export function finalizePlaybackEvents(events, trackOrder) {
|
|
|
34
40
|
if (on.origins.length === 0 || off.origins.length === 0)
|
|
35
41
|
throw new Error(`Note ${noteId} has no origin`);
|
|
36
42
|
}
|
|
43
|
+
// 再从关音侧反查,避免只检查开音时漏掉孤立的 NoteOff
|
|
37
44
|
for (const noteId of noteOffs.keys()) {
|
|
38
45
|
if (!noteOns.has(noteId))
|
|
39
46
|
throw new Error(`NoteOff ${noteId} has no NoteOn`);
|
|
40
47
|
}
|
|
41
48
|
const audible = new Set();
|
|
49
|
+
// 只从实际 NoteOn 收集轨道,无声区间和纯布局轨道不占输出编号
|
|
42
50
|
for (const on of noteOns.values())
|
|
43
51
|
audible.add(on.track);
|
|
44
|
-
// 保留 lowering
|
|
52
|
+
// 保留 lowering 的稳定轨道顺序,只过滤未实际发声的轨道
|
|
45
53
|
const trackIds = new Map();
|
|
46
54
|
for (const track of trackOrder) {
|
|
47
55
|
if (audible.has(track))
|
|
48
56
|
trackIds.set(track, trackIds.size);
|
|
49
57
|
}
|
|
50
|
-
// 可信扩展若替换成 lowering 之外的 Track,仍按首次 NoteOn
|
|
58
|
+
// 可信扩展若替换成 lowering 之外的 Track,仍按首次 NoteOn 顺序追加
|
|
51
59
|
for (const track of audible) {
|
|
52
60
|
if (!trackIds.has(track))
|
|
53
61
|
trackIds.set(track, trackIds.size);
|
|
54
62
|
}
|
|
55
63
|
const output = [];
|
|
64
|
+
// 维持输入的稳定顺序,并显式挑选公开字段,防止内部身份和变换数据泄漏到输出
|
|
56
65
|
for (const event of events) {
|
|
57
66
|
if (event.kind === "tempo") {
|
|
58
67
|
output.push({ kind: "tempo", at: event.at, bpm: event.bpm });
|
|
@@ -78,6 +87,7 @@ export function finalizePlaybackEvents(events, trackOrder) {
|
|
|
78
87
|
});
|
|
79
88
|
continue;
|
|
80
89
|
}
|
|
90
|
+
// NoteOff 从配对的 NoteOn 取得轨道和键号,保证两端始终使用相同的最终身份
|
|
81
91
|
const on = event.kind === "note-on" ? event : noteOns.get(event.noteId);
|
|
82
92
|
const track = trackIds.get(on.track);
|
|
83
93
|
if (event.kind === "note-on") {
|
package/dist/playback/types.d.ts
CHANGED
|
@@ -3,7 +3,8 @@ import type { Fraction } from "../fraction.js";
|
|
|
3
3
|
import type { LoweringAttachment } from "../lowering/types.js";
|
|
4
4
|
import type { TemporalNodeBase } from "../functions/temporal.js";
|
|
5
5
|
import type { Track } from "../lowering/track.js";
|
|
6
|
-
import type {
|
|
6
|
+
import type { SourceSpan } from "../parser/types.js";
|
|
7
|
+
import type { PlaybackEvent, PlaybackEventInput, PlaybackOrigin } from "./event.js";
|
|
7
8
|
export type * from "./event.js";
|
|
8
9
|
/**
|
|
9
10
|
* 播放列游标
|
|
@@ -63,18 +64,60 @@ export interface PlaybackSystemSnapshot {
|
|
|
63
64
|
}
|
|
64
65
|
/** 在指定演奏时刻修改系统状态;所有同刻控制执行完后,core 据最终状态生成 Tempo 等事件 */
|
|
65
66
|
export type PlaybackControl = (state: PlaybackSystemState) => void;
|
|
66
|
-
/**
|
|
67
|
+
/** 一个完整时间区间;时间位于演奏 QN 轴 */
|
|
68
|
+
export interface PlaybackSpanInput {
|
|
69
|
+
start: Fraction;
|
|
70
|
+
end: Fraction;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 编译期区间;有声与无声目标共享结构边界及来源
|
|
74
|
+
* 结构阶段允许修改 start/end,完成后核心会冻结区间、时间对象和来源元数据
|
|
75
|
+
*/
|
|
76
|
+
export interface PlaybackSpan extends PlaybackSpanInput {
|
|
77
|
+
readonly track: Track;
|
|
78
|
+
readonly origins: readonly PlaybackOrigin[];
|
|
79
|
+
readonly sourceSpans: readonly Readonly<SourceSpan>[];
|
|
80
|
+
}
|
|
81
|
+
/** 一个完整音段的发声声明 */
|
|
82
|
+
export interface PlaybackNoteInput extends PlaybackSpanInput {
|
|
83
|
+
midi: number;
|
|
84
|
+
velocity: number;
|
|
85
|
+
percussion?: true;
|
|
86
|
+
transpose?: (steps: number) => number;
|
|
87
|
+
}
|
|
88
|
+
/** 编译期音段;对象身份与来源在连接及声音展开期间保持独立 */
|
|
89
|
+
export interface PlaybackNote extends PlaybackNoteInput, PlaybackSpan {
|
|
90
|
+
}
|
|
91
|
+
/** 当前节点发布完成后,修改此前音段的结构;此时尚未生成系统状态 */
|
|
67
92
|
export type PlaybackHook = (context: PlaybackHookContext) => void;
|
|
68
|
-
/**
|
|
69
|
-
export type PlaybackTransform = (context:
|
|
93
|
+
/** 在最终状态下展开一个音段;结果须保持原音段的起止边界 */
|
|
94
|
+
export type PlaybackTransform = (context: PlaybackTransformContext, notes: PlaybackNote[]) => PlaybackNote[] | void;
|
|
95
|
+
/** 结构阶段的已发布前缀;集合只读,其中区间的边界仍可调整 */
|
|
70
96
|
export interface PlaybackHookContext {
|
|
71
|
-
/**
|
|
72
|
-
readonly
|
|
97
|
+
/** 此前发布的全部时间区间;包括无声目标,有声音段与 notes 共享对象 */
|
|
98
|
+
readonly spans: readonly PlaybackSpan[];
|
|
99
|
+
/** 按发布顺序排列,仅包含当前位置此前已发布的有声音段 */
|
|
100
|
+
readonly notes: readonly PlaybackNote[];
|
|
101
|
+
readonly diagnostics: Diagnostic[];
|
|
102
|
+
}
|
|
103
|
+
/** 声音阶段只查询最终状态,接口不提供全局区间的写入口 */
|
|
104
|
+
export interface PlaybackTransformContext {
|
|
73
105
|
readonly diagnostics: Diagnostic[];
|
|
74
|
-
|
|
106
|
+
/** 查询最终系统状态;返回值与内部时间线隔离 */
|
|
75
107
|
stateAt(time: Fraction): PlaybackSystemSnapshot;
|
|
76
108
|
}
|
|
77
|
-
/**
|
|
109
|
+
/** 连接阶段使用冻结后的完整音段集合,匹配规则由关系声明者负责 */
|
|
110
|
+
export interface PlaybackRelationContext {
|
|
111
|
+
/** 全部结构已确定的音段;连接保留各音段的区间和修饰 */
|
|
112
|
+
readonly notes: readonly Readonly<PlaybackNote>[];
|
|
113
|
+
readonly diagnostics: Diagnostic[];
|
|
114
|
+
/**
|
|
115
|
+
* 尝试连接当前计划内的相接音段;外来对象或不相接的边界会抛出诊断
|
|
116
|
+
* 每段最多一个前驱和后继;既有配对冲突返回 false,重复声明同一连接返回 true
|
|
117
|
+
*/
|
|
118
|
+
connect(from: Readonly<PlaybackNote>, to: Readonly<PlaybackNote>): boolean;
|
|
119
|
+
}
|
|
120
|
+
/** 具体 Temporal 发布音段、系统事件、结构处理与声音展开声明 */
|
|
78
121
|
export interface PlaybackEmitter {
|
|
79
122
|
/** 当前访问在演奏 QN 轴上的起点 */
|
|
80
123
|
readonly start: Fraction;
|
|
@@ -83,25 +126,40 @@ export interface PlaybackEmitter {
|
|
|
83
126
|
* 和 t+T 不一定相等,例如 up 中的折叠节点
|
|
84
127
|
*/
|
|
85
128
|
readonly end: Fraction;
|
|
86
|
-
/**
|
|
129
|
+
/** 当前访问发声所在的 Track,折叠成员沿用正文事件的轨;最终输出时转换为 PlaybackPlan.tracks 的索引 */
|
|
87
130
|
readonly track: Track;
|
|
88
|
-
/**
|
|
89
|
-
|
|
90
|
-
/**
|
|
131
|
+
/** 发布无声区间,参与结构及速度效果处理,不生成音符事件 */
|
|
132
|
+
span(span: PlaybackSpanInput): void;
|
|
133
|
+
/** 发布完整音段;core 自动补轨道和来源,最终统一分配 NoteOn/NoteOff 身份 */
|
|
134
|
+
note(note: PlaybackNoteInput): void;
|
|
135
|
+
/**
|
|
136
|
+
* 用当前相接区间延长已有区间,继承的速度效果从新增部分开始
|
|
137
|
+
* 保留原区间的对象身份和声音变换,不创建新的结构目标
|
|
138
|
+
*/
|
|
139
|
+
extend(span: PlaybackSpan): void;
|
|
140
|
+
/** 发布系统事件;core 自动补来源和稳定次序 */
|
|
91
141
|
emit(event: PlaybackEventInput): void;
|
|
92
142
|
/** 在指定时刻登记系统状态修改;同刻控制全部执行后才生成最终状态事件 */
|
|
93
143
|
control(at: Fraction, apply: PlaybackControl): void;
|
|
94
|
-
/**
|
|
144
|
+
/**
|
|
145
|
+
* 缩放后续有声及无声区间的 BPM;同 key 对应固定比例,重叠或相邻范围按并集生效
|
|
146
|
+
* 默认只覆盖各区间自身;followConnections 可延续到逻辑连接链尾,效果起点保持不变
|
|
147
|
+
*/
|
|
148
|
+
scaleFollowingBpm(key: object, numerator: number, denominator?: number, options?: {
|
|
149
|
+
followConnections?: boolean;
|
|
150
|
+
}): void;
|
|
151
|
+
/** 对同一 play frame 的后续音段登记声音展开,内部新增修饰不泄漏到外层 */
|
|
95
152
|
affectFollowing(transform: PlaybackTransform): void;
|
|
96
|
-
/**
|
|
153
|
+
/** 当前节点发布完成后,在当前位置处理此前已发布的音段 */
|
|
97
154
|
defer(hook: PlaybackHook): void;
|
|
98
|
-
/** 递归发布折叠的子节点;未指定区间时继承当前 start 和 duration */
|
|
155
|
+
/** 递归发布折叠的子节点;未指定区间时继承当前 start 和 duration,并始终在当前轨上发声 */
|
|
99
156
|
play(child: TemporalNodeBase, start?: Fraction, duration?: Fraction): void;
|
|
100
157
|
}
|
|
101
|
-
/**
|
|
158
|
+
/** 结构 hook 完成后,attachment 声明音段之间的连接 */
|
|
102
159
|
export interface PlaybackRelation extends LoweringAttachment {
|
|
103
|
-
applyPlayback(context:
|
|
160
|
+
applyPlayback(context: PlaybackRelationContext): void;
|
|
104
161
|
}
|
|
162
|
+
/** 按能力识别播放关系,避免核心依赖具体附件类 */
|
|
105
163
|
export declare function isPlaybackRelation(attachment: LoweringAttachment): attachment is PlaybackRelation;
|
|
106
164
|
/** 控制流展开后的演奏位置到记谱位置映射 */
|
|
107
165
|
export interface PlaybackScorePoint {
|
|
@@ -114,7 +172,7 @@ export interface PlaybackScorePoint {
|
|
|
114
172
|
* 完整且可查询的演奏计划
|
|
115
173
|
*
|
|
116
174
|
* 刻意不做成 generator:随机定位、总时长和 MIDI 导出都需要完整计划,
|
|
117
|
-
*
|
|
175
|
+
* 跨音段关系也需要完整的演奏位置。无限反复属于播放器的循环控制,
|
|
118
176
|
* 应表示为有限计划加循环点,不是无限序列。
|
|
119
177
|
*/
|
|
120
178
|
export interface PlaybackPlan {
|
package/dist/playback/types.js
CHANGED
package/dist/render/index.d.ts
CHANGED
package/dist/render/index.js
CHANGED
package/dist/render/paint.js
CHANGED
|
@@ -12,7 +12,7 @@ export function paintLayout(result, painter) {
|
|
|
12
12
|
for (const node of result.objects) {
|
|
13
13
|
node.paint(painter);
|
|
14
14
|
for (const decoration of node.decorations)
|
|
15
|
-
decoration.paint(painter);
|
|
15
|
+
decoration.paint?.(painter);
|
|
16
16
|
}
|
|
17
17
|
for (const attachment of result.attachments) {
|
|
18
18
|
if (attachment.layer === "foreground")
|
|
@@ -38,6 +38,7 @@ export type RecordedPaintCommand = {
|
|
|
38
38
|
*/
|
|
39
39
|
export declare class RecordingPainter implements Painter {
|
|
40
40
|
commands: RecordedPaintCommand[];
|
|
41
|
+
replay(painter: Painter): void;
|
|
41
42
|
drawText(text: string, x: number, y: number, style: TextStyle): void;
|
|
42
43
|
drawLine(x1: number, y1: number, x2: number, y2: number, style?: PaintStyle): void;
|
|
43
44
|
drawRect(x: number, y: number, w: number, h: number, style?: PaintStyle): void;
|
package/dist/render/recording.js
CHANGED
|
@@ -6,6 +6,27 @@
|
|
|
6
6
|
*/
|
|
7
7
|
export class RecordingPainter {
|
|
8
8
|
commands = [];
|
|
9
|
+
replay(painter) {
|
|
10
|
+
for (const command of this.commands) {
|
|
11
|
+
switch (command.kind) {
|
|
12
|
+
case "text":
|
|
13
|
+
painter.drawText(command.text, command.x, command.y, command.style);
|
|
14
|
+
break;
|
|
15
|
+
case "line":
|
|
16
|
+
painter.drawLine(command.x1, command.y1, command.x2, command.y2, command.style);
|
|
17
|
+
break;
|
|
18
|
+
case "rect":
|
|
19
|
+
painter.drawRect(command.x, command.y, command.w, command.h, command.style);
|
|
20
|
+
break;
|
|
21
|
+
case "circle":
|
|
22
|
+
painter.drawCircle(command.cx, command.cy, command.r, command.style);
|
|
23
|
+
break;
|
|
24
|
+
case "path":
|
|
25
|
+
painter.drawPath(command.commands, command.style);
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
9
30
|
drawText(text, x, y, style) {
|
|
10
31
|
this.commands.push({
|
|
11
32
|
kind: "text",
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PaintStyle, Painter, PathCommand, PathTransform, TextStyle } from "./types.js";
|
|
2
|
+
/** Painter 没有变换栈,整体平移的几何只能逐条命令平移 */
|
|
3
|
+
export declare class TranslatingPainter implements Painter {
|
|
4
|
+
private readonly target;
|
|
5
|
+
private readonly dx;
|
|
6
|
+
private readonly dy;
|
|
7
|
+
constructor(target: Painter, dx: number, dy: number);
|
|
8
|
+
drawText(text: string, x: number, y: number, style: TextStyle): void;
|
|
9
|
+
drawLine(x1: number, y1: number, x2: number, y2: number, style?: PaintStyle): void;
|
|
10
|
+
drawRect(x: number, y: number, w: number, h: number, style?: PaintStyle): void;
|
|
11
|
+
drawCircle(cx: number, cy: number, r: number, style?: PaintStyle): void;
|
|
12
|
+
drawPath(commands: readonly PathCommand[], style?: PaintStyle, transform?: PathTransform): void;
|
|
13
|
+
private translateCommand;
|
|
14
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** Painter 没有变换栈,整体平移的几何只能逐条命令平移 */
|
|
2
|
+
export class TranslatingPainter {
|
|
3
|
+
target;
|
|
4
|
+
dx;
|
|
5
|
+
dy;
|
|
6
|
+
constructor(target, dx, dy) {
|
|
7
|
+
this.target = target;
|
|
8
|
+
this.dx = dx;
|
|
9
|
+
this.dy = dy;
|
|
10
|
+
}
|
|
11
|
+
drawText(text, x, y, style) {
|
|
12
|
+
this.target.drawText(text, x + this.dx, y + this.dy, style);
|
|
13
|
+
}
|
|
14
|
+
drawLine(x1, y1, x2, y2, style) {
|
|
15
|
+
this.target.drawLine(x1 + this.dx, y1 + this.dy, x2 + this.dx, y2 + this.dy, style);
|
|
16
|
+
}
|
|
17
|
+
drawRect(x, y, w, h, style) {
|
|
18
|
+
this.target.drawRect(x + this.dx, y + this.dy, w, h, style);
|
|
19
|
+
}
|
|
20
|
+
drawCircle(cx, cy, r, style) {
|
|
21
|
+
this.target.drawCircle(cx + this.dx, cy + this.dy, r, style);
|
|
22
|
+
}
|
|
23
|
+
drawPath(commands, style, transform) {
|
|
24
|
+
if (transform) {
|
|
25
|
+
const moved = { ...transform, x: transform.x + this.dx, y: transform.y + this.dy };
|
|
26
|
+
this.target.drawPath(commands, style, moved);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
this.target.drawPath(commands.map(command => this.translateCommand(command)), style);
|
|
30
|
+
}
|
|
31
|
+
translateCommand(command) {
|
|
32
|
+
switch (command.op) {
|
|
33
|
+
case "Z": return command;
|
|
34
|
+
case "Q": return {
|
|
35
|
+
op: "Q",
|
|
36
|
+
cx: command.cx + this.dx, cy: command.cy + this.dy,
|
|
37
|
+
x: command.x + this.dx, y: command.y + this.dy,
|
|
38
|
+
};
|
|
39
|
+
case "C": return {
|
|
40
|
+
op: "C",
|
|
41
|
+
cx1: command.cx1 + this.dx, cy1: command.cy1 + this.dy,
|
|
42
|
+
cx2: command.cx2 + this.dx, cy2: command.cy2 + this.dy,
|
|
43
|
+
x: command.x + this.dx, y: command.y + this.dy,
|
|
44
|
+
};
|
|
45
|
+
default: return { op: command.op, x: command.x + this.dx, y: command.y + this.dy };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|