jpfun 0.8.0 → 1.0.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 +10 -15
- package/dist/diagnostic.d.ts +29 -2
- package/dist/diagnostic.js +5 -4
- package/dist/functions/ASTtypes.d.ts +1 -14
- package/dist/functions/ASTtypes.js +5 -36
- package/dist/functions/adjust/index.js +45 -33
- package/dist/functions/arpeggio/index.js +44 -48
- package/dist/functions/arpeggio/shape.d.ts +2 -3
- package/dist/functions/arpeggio/shape.js +20 -50
- package/dist/functions/bar/index.d.ts +3 -0
- package/dist/functions/bar/index.js +21 -11
- package/dist/functions/beam/auto.js +54 -123
- package/dist/functions/beam/index.js +4 -9
- package/dist/functions/beam/layout.js +3 -2
- package/dist/functions/box/index.js +1 -6
- package/dist/functions/dash/index.js +6 -4
- package/dist/functions/div/index.d.ts +3 -5
- package/dist/functions/div/index.js +14 -12
- package/dist/functions/dot/index.js +3 -9
- package/dist/functions/dyn/index.js +7 -26
- package/dist/functions/gliss/index.js +99 -91
- package/dist/functions/grace/index.d.ts +2 -5
- package/dist/functions/grace/index.js +47 -94
- package/dist/functions/head/index.js +32 -36
- package/dist/functions/key/index.js +9 -12
- package/dist/functions/meter/index.d.ts +1 -0
- package/dist/functions/meter/index.js +6 -3
- package/dist/functions/meter/shape.d.ts +1 -1
- package/dist/functions/note/accidentals.d.ts +3 -3
- package/dist/functions/note/accidentals.js +29 -16
- package/dist/functions/note/index.js +12 -14
- package/dist/functions/note/source-transform.d.ts +15 -0
- package/dist/functions/note/source-transform.js +75 -71
- package/dist/functions/ottava/index.js +12 -32
- package/dist/functions/page/index.js +15 -17
- package/dist/functions/rest/index.js +10 -10
- package/dist/functions/slide/index.js +1 -1
- package/dist/functions/stack/index.js +8 -57
- package/dist/functions/temporal.d.ts +11 -5
- package/dist/functions/temporal.js +22 -2
- package/dist/functions/text/index.js +24 -30
- package/dist/functions/tie/index.js +42 -41
- package/dist/functions/tremolo/index.js +1 -1
- package/dist/functions/tuplet/index.js +1 -6
- package/dist/functions/up/index.js +82 -88
- package/dist/functions/voice/connections.d.ts +3 -2
- package/dist/functions/voice/connections.js +48 -10
- package/dist/functions/voice/index.d.ts +2 -3
- package/dist/functions/voice/index.js +2 -970
- package/dist/functions/voice/lyrics.d.ts +34 -0
- package/dist/functions/voice/lyrics.js +393 -0
- package/dist/functions/voice/voice.d.ts +67 -0
- package/dist/functions/voice/voice.js +405 -0
- package/dist/functions/voice/voices.d.ts +106 -0
- package/dist/functions/voice/voices.js +424 -0
- package/dist/functions/volta/index.js +6 -23
- package/dist/index.d.ts +97 -3
- package/dist/index.js +97 -3
- package/dist/jpfun.min.js +63 -67
- package/dist/layout/engine.d.ts +4 -3
- package/dist/layout/engine.js +190 -24
- package/dist/layout/model.d.ts +12 -4
- package/dist/layout/model.js +16 -11
- package/dist/layout/page.d.ts +2 -1
- package/dist/layout/page.js +15 -15
- package/dist/layout/path.js +1 -1
- package/dist/layout/range.d.ts +7 -6
- package/dist/layout/range.js +23 -14
- package/dist/layout/types.d.ts +19 -30
- package/dist/lowering/loweringContext.d.ts +8 -11
- package/dist/lowering/loweringContext.js +7 -13
- package/dist/lowering/track.d.ts +8 -1
- package/dist/lowering/types.d.ts +12 -8
- package/dist/parser/grammarType.d.ts +3 -3
- package/dist/parser/parse-utils/brace-utils.js +7 -17
- package/dist/parser/parse-utils/call-utils.d.ts +4 -3
- package/dist/parser/parse-utils/call-utils.js +23 -66
- package/dist/parser/parse-utils/note-utils.d.ts +7 -3
- package/dist/parser/parse-utils/note-utils.js +25 -40
- package/dist/parser/parse-utils/string-utils.d.ts +1 -1
- package/dist/parser/parse-utils/string-utils.js +8 -6
- package/dist/parser/parserContext.d.ts +17 -11
- package/dist/parser/parserContext.js +111 -65
- package/dist/parser/preprocess.d.ts +7 -0
- package/dist/parser/preprocess.js +21 -19
- package/dist/pipeline.d.ts +1 -1
- package/dist/pipeline.js +2 -2
- package/dist/playback/compile.js +14 -15
- package/dist/playback/event.d.ts +6 -6
- package/dist/playback/event.js +13 -49
- package/dist/render/canvas.d.ts +0 -2
- package/dist/render/canvas.js +50 -56
- package/dist/render/index.d.ts +1 -1
- package/dist/render/index.js +1 -1
- package/dist/render/paint.d.ts +7 -0
- package/dist/render/paint.js +70 -20
- package/dist/render/recording.d.ts +8 -1
- package/dist/render/recording.js +12 -0
- package/dist/render/svg.d.ts +5 -2
- package/dist/render/svg.js +70 -48
- package/dist/render/text.d.ts +37 -2
- package/dist/render/text.js +136 -1
- package/dist/render/translate.d.ts +3 -1
- package/dist/render/translate.js +6 -0
- package/dist/render/types.d.ts +9 -2
- package/grammar.md +18 -14
- package/package.json +8 -18
- package/dist/converter/midi/convert.d.ts +0 -42
- package/dist/converter/midi/convert.js +0 -729
- package/dist/converter/midi/index.d.ts +0 -5
- package/dist/converter/midi/index.js +0 -11
- package/dist/converter/midi/json.d.ts +0 -38
- package/dist/converter/midi/json.js +0 -1
- package/dist/converter/midi/triplet.d.ts +0 -30
- package/dist/converter/midi/triplet.js +0 -75
- package/dist/converter/musicxml/dom.d.ts +0 -25
- package/dist/converter/musicxml/dom.js +0 -65
- package/dist/converter/musicxml/features.d.ts +0 -114
- package/dist/converter/musicxml/features.js +0 -501
- package/dist/converter/musicxml/index.d.ts +0 -14
- package/dist/converter/musicxml/index.js +0 -1295
- package/dist/converter/musicxml/model.d.ts +0 -176
- package/dist/converter/musicxml/model.js +0 -1
- package/dist/converter/musicxml/resolve.d.ts +0 -10
- package/dist/converter/musicxml/resolve.js +0 -152
- package/dist/converter/source.d.ts +0 -22
- package/dist/converter/source.js +0 -51
- package/dist/core.d.ts +0 -24
- package/dist/core.js +0 -24
- package/dist/iife/core.d.ts +0 -1
- package/dist/iife/core.js +0 -3
- package/dist/iife/from-midi.d.ts +0 -1
- package/dist/iife/from-midi.js +0 -15
- package/dist/iife/from-musicxml.d.ts +0 -1
- package/dist/iife/from-musicxml.js +0 -3
- package/dist/iife/full.d.ts +0 -1
- package/dist/iife/full.js +0 -3
- package/dist/jpfun.core.min.js +0 -123
- package/dist/jpfun.from-midi.min.js +0 -9
- package/dist/jpfun.from-musicxml.min.js +0 -12
package/dist/layout/range.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isVisualTemporalNode, TemporalNodeBase } from "../functions/temporal.js";
|
|
2
|
-
const EMPTY = { start: 0, end: -1
|
|
2
|
+
const EMPTY = { start: 0, end: -1 };
|
|
3
3
|
/**
|
|
4
4
|
* 把轨归入以 root 为根的 Track 树
|
|
5
5
|
*
|
|
@@ -37,35 +37,27 @@ function columnOf(view, host) {
|
|
|
37
37
|
/**
|
|
38
38
|
* 在正文行或子域的视图里解析一次范围查询
|
|
39
39
|
*
|
|
40
|
-
*
|
|
41
|
-
* 指定 track 时两个端点都必须位于该轨,私有轨先经 lift 归入本域
|
|
40
|
+
* 对象端点可以逆序或跨行,按本行截取。
|
|
42
41
|
*/
|
|
43
|
-
export function resolveRange(view, range
|
|
42
|
+
export function resolveRange(view, range) {
|
|
44
43
|
if (!range)
|
|
45
|
-
return { start: 0, end: view.columns.length - 1
|
|
46
|
-
if (typeof range[0] === "number") {
|
|
47
|
-
return { start: Math.max(0, range[0]), end: Math.min(view.columns.length - 1, range[1]), wholeLine: false };
|
|
48
|
-
}
|
|
44
|
+
return { start: 0, end: view.columns.length - 1 };
|
|
49
45
|
let [from, to] = range;
|
|
50
46
|
if (from.layoutLine > to.layoutLine)
|
|
51
47
|
[from, to] = [to, from];
|
|
52
48
|
if (view.index < from.layoutLine || view.index > to.layoutLine)
|
|
53
49
|
return EMPTY;
|
|
54
|
-
const target = track && lift(track);
|
|
55
|
-
if (target && (lift(from.track) !== target || lift(to.track) !== target)) {
|
|
56
|
-
throw new Error("Range endpoints must belong to the selected track");
|
|
57
|
-
}
|
|
58
50
|
const start = from.layoutLine < view.index ? 0 : columnOf(view, from);
|
|
59
51
|
const end = to.layoutLine > view.index ? view.columns.length - 1 : columnOf(view, to);
|
|
60
52
|
if (start < 0 || end < 0)
|
|
61
53
|
return EMPTY;
|
|
62
|
-
return { start: Math.min(start, end), end: Math.max(start, end)
|
|
54
|
+
return { start: Math.min(start, end), end: Math.max(start, end) };
|
|
63
55
|
}
|
|
64
56
|
/** 范围内的列;指定 track 时只保留该轨主体,空列照样保留 */
|
|
65
57
|
export function rangeColumns(view, range, track, lift) {
|
|
66
58
|
if (!view)
|
|
67
59
|
return [];
|
|
68
|
-
const { start, end } = resolveRange(view, range
|
|
60
|
+
const { start, end } = resolveRange(view, range);
|
|
69
61
|
const columns = view.columns.slice(start, end + 1);
|
|
70
62
|
if (!track)
|
|
71
63
|
return columns;
|
|
@@ -73,3 +65,20 @@ export function rangeColumns(view, range, track, lift) {
|
|
|
73
65
|
const onTarget = (host) => host.track === target;
|
|
74
66
|
return columns.every(column => column.every(onTarget)) ? columns : columns.map(column => column.filter(onTarget));
|
|
75
67
|
}
|
|
68
|
+
/** 跳过行首零时值系统前缀,取该行指定轨首个占时值主体的左边界 */
|
|
69
|
+
export function lineEntryX(context, line, track, fallback = context.originX) {
|
|
70
|
+
const host = context.lines[line]?.trackRuns.get(track)?.find(item => !item.T.isZero());
|
|
71
|
+
return host?.box.x ?? fallback;
|
|
72
|
+
}
|
|
73
|
+
/** 取该行所有可见主体的最大右边界,空行回退到版心右沿 */
|
|
74
|
+
export function lineRightX(context, line) {
|
|
75
|
+
const runs = context.lines[line]?.trackRuns;
|
|
76
|
+
if (!runs || runs.size === 0)
|
|
77
|
+
return context.originX + context.width;
|
|
78
|
+
let right = context.originX;
|
|
79
|
+
for (const run of runs.values()) {
|
|
80
|
+
for (const host of run)
|
|
81
|
+
right = Math.max(right, host.box.x + host.box.w);
|
|
82
|
+
}
|
|
83
|
+
return right;
|
|
84
|
+
}
|
package/dist/layout/types.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { Painter, TextMeasurer } from "../render/types.js";
|
|
2
|
-
import type { Track } from "../lowering/track.js";
|
|
2
|
+
import type { Extent, Track } from "../lowering/track.js";
|
|
3
3
|
import type { Fraction } from "../fraction.js";
|
|
4
4
|
import type { TemporalNodeBase, VisualTemporalNode } from "../functions/temporal.js";
|
|
5
5
|
import type { SourceSpan } from "../parser/types.js";
|
|
6
6
|
import type { HorizontalLayoutHook } from "./model.js";
|
|
7
|
-
import type { LoweringAttachment, LoweringContent } from "../lowering/types.js";
|
|
7
|
+
import type { LoweringAttachment, LoweringContent, PageConfig } from "../lowering/types.js";
|
|
8
|
+
export type { Extent, PageConfig };
|
|
8
9
|
/** 轴对齐矩形,是所有排版几何的公共部分 */
|
|
9
10
|
export interface Rect {
|
|
10
11
|
x: number;
|
|
@@ -12,14 +13,6 @@ export interface Rect {
|
|
|
12
13
|
w: number;
|
|
13
14
|
h: number;
|
|
14
15
|
}
|
|
15
|
-
/**
|
|
16
|
-
* 相对某条基线的纵向占用
|
|
17
|
-
* 约定 top <= 0 <= bottom
|
|
18
|
-
*/
|
|
19
|
-
export interface Extent {
|
|
20
|
-
top: number;
|
|
21
|
-
bottom: number;
|
|
22
|
-
}
|
|
23
16
|
/**
|
|
24
17
|
* 一个排版对象的定位矩形和两个对齐基准
|
|
25
18
|
*
|
|
@@ -47,17 +40,6 @@ export interface TimeLineEvent {
|
|
|
47
40
|
T: Fraction;
|
|
48
41
|
track: Track;
|
|
49
42
|
}
|
|
50
|
-
/** 文档页面的固化尺寸,所有值均为 px */
|
|
51
|
-
export interface PageConfig {
|
|
52
|
-
width: number;
|
|
53
|
-
height: number;
|
|
54
|
-
marginTop: number;
|
|
55
|
-
marginBottom: number;
|
|
56
|
-
marginLeft: number;
|
|
57
|
-
marginRight: number;
|
|
58
|
-
lineGap: number;
|
|
59
|
-
fillRatio: number;
|
|
60
|
-
}
|
|
61
43
|
/**
|
|
62
44
|
* 排版前的共享资源
|
|
63
45
|
*
|
|
@@ -85,7 +67,7 @@ export interface SubdomainLayout {
|
|
|
85
67
|
/** 与 columns.flat() 一一对应,不含成员 onPlaced 追加的平移 */
|
|
86
68
|
positions: readonly LayoutPoint[];
|
|
87
69
|
}
|
|
88
|
-
export type LayoutRange = readonly [from:
|
|
70
|
+
export type LayoutRange = readonly [from: LayoutHost, to: LayoutHost];
|
|
89
71
|
/** attachment 根据当前视觉轴生成几何时需要的完整页面信息 */
|
|
90
72
|
export interface AttachmentLayoutContext extends LayoutPrepareContext {
|
|
91
73
|
/** 占用测量使用布局位置,最终绘制才应用后置平移 */
|
|
@@ -102,18 +84,18 @@ export interface AttachmentLayoutContext extends LayoutPrepareContext {
|
|
|
102
84
|
/**
|
|
103
85
|
* 本行闭区间内的已定占用;全轨返回按 Track 分组的 Map,单轨直接返回 Extent 或 undefined
|
|
104
86
|
*
|
|
105
|
-
*
|
|
87
|
+
* 对象端点沿 foldedInto 上溯到本域的列,跨行对象区间自动截取本行部分。
|
|
106
88
|
* 子域对外是一个主体,正文查询不进入它的内部。
|
|
107
89
|
* 主体按查询列筛选;attachment 按这些列最终盒子的横向范围筛选,并遵守 track 过滤。
|
|
108
90
|
* attachment 部分与 getAttachmentBox 同一条可见性规则:只看得见比自己先注册的,
|
|
109
91
|
* 也就是「后声明的排在外层」。避让型 attachment 用它把自己排到已有内容之外。
|
|
110
|
-
*
|
|
92
|
+
* 省略范围表示整行,供跨行 attachment 查询没有可见列的中间行。
|
|
111
93
|
*/
|
|
112
|
-
getRangeExtents(line: number,
|
|
113
|
-
getRangeExtents(line: number,
|
|
94
|
+
getRangeExtents(line: number, range?: LayoutRange): ReadonlyMap<Track, Readonly<Extent>>;
|
|
95
|
+
getRangeExtents(line: number, range: LayoutRange | undefined, track: Track): Readonly<Extent> | undefined;
|
|
114
96
|
/** 按全局 x 区间筛选横向相交的主体及已测附件,返回各轨轴局部占用;空区间返回空 Map */
|
|
115
97
|
getHorizontalExtents(line: number, left: number, right: number): ReadonlyMap<Track, Readonly<Extent>>;
|
|
116
|
-
/** 读取本轮已完成的 attachment
|
|
98
|
+
/** 读取本轮已完成的 attachment 边界;外层分组在 endLoweringGroup 后才注册自身附件,因而组内对象必然排在分组之前 */
|
|
117
99
|
getAttachmentBox(attachment: LayoutAttachment): Readonly<Rect>;
|
|
118
100
|
/** 读取已测附件的有效占用,保留显式空数组;尚未测量时返回 undefined */
|
|
119
101
|
getAttachmentOccupancy(attachment: LayoutAttachment): readonly LayoutRegion[] | undefined;
|
|
@@ -225,6 +207,11 @@ export interface AttachmentGeometry {
|
|
|
225
207
|
* 实例只保存语义输入和一次横向准备状态,不保存最终 box、regions 或绘制几何。
|
|
226
208
|
*/
|
|
227
209
|
export interface LayoutAttachment extends LoweringAttachment {
|
|
210
|
+
/**
|
|
211
|
+
* 绘制分组的语义类型,SVG 输出为 `<g data-kind>`;缺省或空串表示不开组
|
|
212
|
+
* 同一个值只对应一种图形,不应与时值对象的 kind 重名;取值规则见 render 文档
|
|
213
|
+
*/
|
|
214
|
+
readonly kind?: string;
|
|
228
215
|
/** 后置平移仅影响绘制,不参与占用求解 */
|
|
229
216
|
placementOffset?: LayoutPoint;
|
|
230
217
|
/** 几何端点;端点全部落在同一子域的成员上时,附件属于该子域。不表示内容包含或播放端点投影 */
|
|
@@ -235,7 +222,6 @@ export interface LayoutAttachment extends LoweringAttachment {
|
|
|
235
222
|
readonly sourceSpan?: SourceSpan;
|
|
236
223
|
/**
|
|
237
224
|
* 横向求解前:可测量资源、调整弹簧参数或注册横向布局 hook,不得改变对象/列顺序
|
|
238
|
-
*
|
|
239
225
|
* 此时 box 的固有尺寸 w/h/anchor/visualAxis 已是终值,只有 x/y 未定
|
|
240
226
|
*/
|
|
241
227
|
prepareHorizontal?(lines: HorizontalLineView[], context: LayoutPrepareContext): void;
|
|
@@ -244,7 +230,8 @@ export interface LayoutAttachment extends LoweringAttachment {
|
|
|
244
230
|
export declare function isLayoutAttachment(attachment: LoweringAttachment): attachment is LayoutAttachment;
|
|
245
231
|
/** layoutDocument 输出的最终 attachment 快照 */
|
|
246
232
|
export interface PlacedAttachment {
|
|
247
|
-
|
|
233
|
+
/** 取自 LayoutAttachment.kind */
|
|
234
|
+
readonly kind?: string;
|
|
248
235
|
readonly regions: readonly LayoutRegion[];
|
|
249
236
|
/** 相对于 Temporal 主体的绘制层;background 比内容先绘制 */
|
|
250
237
|
readonly layer: "background" | "foreground";
|
|
@@ -257,7 +244,9 @@ export interface PlacedAttachment {
|
|
|
257
244
|
* 总是计入外接盒;同时声明 line 和 track 时,还会折算成该轨道的纵向占用
|
|
258
245
|
* 只想影响画布边界、不想撑高行的图形(括线、边框)省略归属即可
|
|
259
246
|
*/
|
|
260
|
-
export type LayoutRegion = Rect &
|
|
247
|
+
export type LayoutRegion = Rect & {
|
|
248
|
+
sourceSpan?: SourceSpan;
|
|
249
|
+
} & ({
|
|
261
250
|
line: number;
|
|
262
251
|
track: Track;
|
|
263
252
|
} | {
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { Diagnostic } from "../diagnostic.js";
|
|
2
2
|
import { Fraction } from "../fraction.js";
|
|
3
3
|
import { ASTNodeBase } from "../functions/ASTtypes.js";
|
|
4
|
-
import type { PageConfig } from "../layout/types.js";
|
|
5
4
|
import { TemporalNodeBase } from "../functions/temporal.js";
|
|
6
|
-
import type { LoweringFinalizer, LoweringGroup, LoweringAttachment, LoweringResult } from "./types.js";
|
|
5
|
+
import type { LoweringFinalizer, LoweringGroup, LoweringAttachment, LoweringResult, PageConfig } from "./types.js";
|
|
7
6
|
import { Track } from "./track.js";
|
|
8
7
|
/**
|
|
9
8
|
* 把 AST 展开并固化为时间列、attachment 和页面配置
|
|
@@ -40,31 +39,29 @@ export declare class LoweringContext {
|
|
|
40
39
|
*/
|
|
41
40
|
private activeLoweringGroups;
|
|
42
41
|
/** 收集本轮 lowering 产生的诊断信息 */
|
|
43
|
-
|
|
44
|
-
constructor(diagnostics?: Diagnostic[]);
|
|
42
|
+
diagnostics: Diagnostic[];
|
|
45
43
|
/** 供 page 函数调用 */
|
|
46
44
|
setPageConfig(page: PageConfig): void;
|
|
47
45
|
/**
|
|
48
46
|
* 完整 lowering 入口
|
|
49
47
|
*
|
|
50
|
-
*
|
|
51
|
-
* 已注册的后处理 hook 会保留,可以复用同一个 LoweringContext
|
|
48
|
+
* 每次调用都会重置本轮产生的数据,可以复用同一个 LoweringContext
|
|
52
49
|
*/
|
|
53
|
-
lowerDocument(node: ASTNodeBase): LoweringResult;
|
|
50
|
+
lowerDocument(node: ASTNodeBase, diagnostics?: Diagnostic[]): LoweringResult;
|
|
54
51
|
/**
|
|
55
52
|
* 固化谱面行号、时间状态和最终时长
|
|
56
53
|
*/
|
|
57
54
|
private solidifyColumns;
|
|
58
55
|
/**
|
|
59
56
|
* 开始一个 lowering 分组作用域
|
|
60
|
-
* group
|
|
57
|
+
* group 可以观察当前子树产生的 TemporalNode 和 attachment
|
|
61
58
|
*/
|
|
62
59
|
beginLoweringGroup(owner: ASTNodeBase, group: LoweringGroup): void;
|
|
63
60
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
61
|
+
* 结束最近的分组并返回它
|
|
62
|
+
* 调用方可在退出分组后按需注册自身 attachment,天然实现由内向外计算边界
|
|
66
63
|
*/
|
|
67
|
-
endLoweringGroup(owner: ASTNodeBase):
|
|
64
|
+
endLoweringGroup<T extends LoweringGroup = LoweringGroup>(owner: ASTNodeBase): T;
|
|
68
65
|
/** 按显式 order 升序执行本轮登记的收尾回调 */
|
|
69
66
|
private postprocessResult;
|
|
70
67
|
/** 登记本轮需要执行的收尾回调;order 越小越先执行,默认 0 */
|
|
@@ -37,10 +37,7 @@ export class LoweringContext {
|
|
|
37
37
|
*/
|
|
38
38
|
activeLoweringGroups = [];
|
|
39
39
|
/** 收集本轮 lowering 产生的诊断信息 */
|
|
40
|
-
diagnostics;
|
|
41
|
-
constructor(diagnostics = []) {
|
|
42
|
-
this.diagnostics = diagnostics;
|
|
43
|
-
}
|
|
40
|
+
diagnostics = [];
|
|
44
41
|
/** 供 page 函数调用 */
|
|
45
42
|
setPageConfig(page) {
|
|
46
43
|
this.page = page;
|
|
@@ -48,10 +45,9 @@ export class LoweringContext {
|
|
|
48
45
|
/**
|
|
49
46
|
* 完整 lowering 入口
|
|
50
47
|
*
|
|
51
|
-
*
|
|
52
|
-
* 已注册的后处理 hook 会保留,可以复用同一个 LoweringContext
|
|
48
|
+
* 每次调用都会重置本轮产生的数据,可以复用同一个 LoweringContext
|
|
53
49
|
*/
|
|
54
|
-
lowerDocument(node) {
|
|
50
|
+
lowerDocument(node, diagnostics = []) {
|
|
55
51
|
this.cnt = 0;
|
|
56
52
|
this.usedTracks.clear();
|
|
57
53
|
this.activeFinalizers.clear();
|
|
@@ -59,6 +55,7 @@ export class LoweringContext {
|
|
|
59
55
|
this.page = void 0;
|
|
60
56
|
this.astToTemporal = new Map();
|
|
61
57
|
this.activeLoweringGroups = [];
|
|
58
|
+
this.diagnostics = diagnostics;
|
|
62
59
|
const rootTrack = new Track();
|
|
63
60
|
const duration = new Fraction();
|
|
64
61
|
const columns = this.trackedEvents(node, duration, rootTrack);
|
|
@@ -109,7 +106,6 @@ export class LoweringContext {
|
|
|
109
106
|
column.shiftTime(timeOffset);
|
|
110
107
|
let insertion;
|
|
111
108
|
for (const node of column) {
|
|
112
|
-
state.currentEvent = node;
|
|
113
109
|
state.velocity = velocityOf(node.track);
|
|
114
110
|
state.program = programOf(node.track);
|
|
115
111
|
const offset = node.onTimeState?.(state);
|
|
@@ -146,14 +142,14 @@ export class LoweringContext {
|
|
|
146
142
|
}
|
|
147
143
|
/**
|
|
148
144
|
* 开始一个 lowering 分组作用域
|
|
149
|
-
* group
|
|
145
|
+
* group 可以观察当前子树产生的 TemporalNode 和 attachment
|
|
150
146
|
*/
|
|
151
147
|
beginLoweringGroup(owner, group) {
|
|
152
148
|
this.activeLoweringGroups.push({ owner, group });
|
|
153
149
|
}
|
|
154
150
|
/**
|
|
155
|
-
*
|
|
156
|
-
*
|
|
151
|
+
* 结束最近的分组并返回它
|
|
152
|
+
* 调用方可在退出分组后按需注册自身 attachment,天然实现由内向外计算边界
|
|
157
153
|
*/
|
|
158
154
|
endLoweringGroup(owner) {
|
|
159
155
|
const active = this.activeLoweringGroups.pop();
|
|
@@ -161,8 +157,6 @@ export class LoweringContext {
|
|
|
161
157
|
throw new Error("No active lowering group to end");
|
|
162
158
|
if (active.owner !== owner)
|
|
163
159
|
throw new Error("Lowering groups must end in reverse order");
|
|
164
|
-
if (active.group.attachment)
|
|
165
|
-
this.addAttachment(active.group.attachment);
|
|
166
160
|
return active.group;
|
|
167
161
|
}
|
|
168
162
|
/** 按显式 order 升序执行本轮登记的收尾回调 */
|
package/dist/lowering/track.d.ts
CHANGED
package/dist/lowering/types.d.ts
CHANGED
|
@@ -3,8 +3,19 @@ import type { TemporalNodeBase } from "../functions/temporal.js";
|
|
|
3
3
|
import type { Diagnostic } from "../diagnostic.js";
|
|
4
4
|
import type { SourceSpan } from "../parser/types.js";
|
|
5
5
|
import type { Fraction } from "../fraction.js";
|
|
6
|
-
import type { PageConfig } from "../layout/types.js";
|
|
7
6
|
import type { Track, TrackArrangement } from "./track.js";
|
|
7
|
+
/** 文档页面的固化尺寸,所有值均为 px */
|
|
8
|
+
export interface PageConfig {
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
marginTop: number;
|
|
12
|
+
marginBottom: number;
|
|
13
|
+
marginLeft: number;
|
|
14
|
+
marginRight: number;
|
|
15
|
+
lineGap: number;
|
|
16
|
+
minLineGap: number;
|
|
17
|
+
fillRatio: number;
|
|
18
|
+
}
|
|
8
19
|
/** lowering 产生的不推进时间的附属对象;具体能力由 layout/playback 子协议声明 */
|
|
9
20
|
export interface LoweringAttachment {
|
|
10
21
|
readonly sourceSpan?: SourceSpan;
|
|
@@ -40,13 +51,6 @@ export interface LoweringResult {
|
|
|
40
51
|
* 回调按当前作用域栈由内向外执行;onTemporal 在事件加入时间列、推进时间游标前调用。
|
|
41
52
|
*/
|
|
42
53
|
export interface LoweringGroup {
|
|
43
|
-
/**
|
|
44
|
-
* 当前分组退出后会自动注册的 attachment
|
|
45
|
-
*
|
|
46
|
-
* attachment 在 layout 的时候会按照顺序求解,对于 box 这样的必须让子元素先被测量,才能确定尺寸,所以需要最后加入
|
|
47
|
-
* 之所以有这个字段(而不是在 endLoweringGroup 之后手动 push)是为了收集到的信息可以直接传递,参考 box
|
|
48
|
-
*/
|
|
49
|
-
attachment?: LoweringAttachment;
|
|
50
54
|
/** 观察组内生成的事件 */
|
|
51
55
|
onTemporal?(node: TemporalNodeBase): void;
|
|
52
56
|
/** 观察组内注册的附属对象 */
|
|
@@ -4,8 +4,8 @@ export type GrammarNode = GrammarBraceNode | GrammarLabelNode | GrammarCallNode
|
|
|
4
4
|
export interface GrammarNodeBase {
|
|
5
5
|
kind: "brace" | "label" | "call" | "sugar";
|
|
6
6
|
span: SourceSpan;
|
|
7
|
-
/** 语法高亮类型;缺省为 operator */
|
|
8
|
-
syntaxKind?: SyntaxTokenKind;
|
|
7
|
+
/** 语法高亮类型;缺省为 operator,复合语法糖可直接给出多段 SyntaxToken */
|
|
8
|
+
syntaxKind?: SyntaxTokenKind | SyntaxToken[];
|
|
9
9
|
}
|
|
10
10
|
export interface GrammarBraceNode extends GrammarNodeBase {
|
|
11
11
|
kind: "brace";
|
|
@@ -42,7 +42,7 @@ export interface CallInfo {
|
|
|
42
42
|
name: string;
|
|
43
43
|
/** 整个调用表达式在源码中的范围 */
|
|
44
44
|
span: SourceSpan;
|
|
45
|
-
/**
|
|
45
|
+
/** 调用名在源码中的范围,包含 `@` 前缀 */
|
|
46
46
|
nameSpan: SourceSpan;
|
|
47
47
|
/** 左括号 `(` 在源码中的范围 */
|
|
48
48
|
openParenSpan: SourceSpan;
|
|
@@ -1,29 +1,19 @@
|
|
|
1
|
+
import { findClosingQuote } from "./string-utils.js";
|
|
1
2
|
// 找到 } 的位置
|
|
2
3
|
export function readBrace(source, lBracePos, end) {
|
|
3
4
|
// 要求 source[lBracePos]==='{'(外部检验)
|
|
4
5
|
let depth = 1;
|
|
5
|
-
let quote = null;
|
|
6
|
-
let escaped = false;
|
|
7
6
|
for (let i = lBracePos + 1; i < end; i++) {
|
|
8
7
|
const ch = source[i];
|
|
9
|
-
if (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
escaped = true;
|
|
14
|
-
else if (ch === quote)
|
|
15
|
-
quote = null;
|
|
16
|
-
continue;
|
|
8
|
+
if (ch === '"') {
|
|
9
|
+
i = findClosingQuote(source, i, end);
|
|
10
|
+
if (i < 0)
|
|
11
|
+
break;
|
|
17
12
|
}
|
|
18
|
-
if (ch === '"')
|
|
19
|
-
quote = ch;
|
|
20
13
|
else if (ch === "{")
|
|
21
14
|
depth++;
|
|
22
|
-
else if (ch === "}")
|
|
23
|
-
|
|
24
|
-
if (depth === 0)
|
|
25
|
-
return i;
|
|
26
|
-
}
|
|
15
|
+
else if (ch === "}" && --depth === 0)
|
|
16
|
+
return i;
|
|
27
17
|
}
|
|
28
18
|
return -1;
|
|
29
19
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SourceSpan } from "../types.js";
|
|
2
|
-
import type { CallInfo } from "../grammarType.js";
|
|
2
|
+
import type { CallArgumentInfo, CallInfo } from "../grammarType.js";
|
|
3
3
|
/**
|
|
4
4
|
* 尝试在 `source` 的 `atPos` 位置读取一个以 `@` 开头的调用表达式,例如 `@name(...)`
|
|
5
5
|
* @param source 原始文本
|
|
@@ -13,10 +13,11 @@ import type { CallInfo } from "../grammarType.js";
|
|
|
13
13
|
* - 正确处理字符串字面量(支持转义)
|
|
14
14
|
* - 支持嵌套小括号 `(...)`,但被大括号 `{...}` 包裹的小括号不会影响外层匹配
|
|
15
15
|
* - 忽略大括号内的小括号深度变化
|
|
16
|
-
* -
|
|
16
|
+
* - 在顶层小括号内以逗号分割参数,同时记录每个参数的首个顶层 `=` 及其在 `source` 中的起始/结束位置
|
|
17
17
|
* - 若找到匹配右括号,记录 closeParenSpan;否则在 end 处结束,让上层决定诊断/恢复策略
|
|
18
18
|
*/
|
|
19
19
|
export declare function readCall(source: string, atPos: number, end: number): CallInfo | null;
|
|
20
|
+
/** 实参的小写参数名;位置参数返回 undefined */
|
|
21
|
+
export declare function callArgName(source: string, arg?: CallArgumentInfo): string | undefined;
|
|
20
22
|
export declare function trimRange(source: string, start: number, end: number): SourceSpan;
|
|
21
|
-
export declare function findTopLevelEquals(text: string, start?: number, end?: number): number;
|
|
22
23
|
export declare function findRightParen(source: string, start: number, end: number): number;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { findClosingQuote } from "./string-utils.js";
|
|
1
2
|
// 函数名:允许字母/下划线,以及符号别名(@/ @. @-)
|
|
2
3
|
const CALL_NAME_CHAR_RE = /[A-Za-z0-9_./-]/;
|
|
3
4
|
/**
|
|
@@ -13,7 +14,7 @@ const CALL_NAME_CHAR_RE = /[A-Za-z0-9_./-]/;
|
|
|
13
14
|
* - 正确处理字符串字面量(支持转义)
|
|
14
15
|
* - 支持嵌套小括号 `(...)`,但被大括号 `{...}` 包裹的小括号不会影响外层匹配
|
|
15
16
|
* - 忽略大括号内的小括号深度变化
|
|
16
|
-
* -
|
|
17
|
+
* - 在顶层小括号内以逗号分割参数,同时记录每个参数的首个顶层 `=` 及其在 `source` 中的起始/结束位置
|
|
17
18
|
* - 若找到匹配右括号,记录 closeParenSpan;否则在 end 处结束,让上层决定诊断/恢复策略
|
|
18
19
|
*/
|
|
19
20
|
export function readCall(source, atPos, end) {
|
|
@@ -30,18 +31,16 @@ export function readCall(source, atPos, end) {
|
|
|
30
31
|
const openPos = i;
|
|
31
32
|
let parenDepth = 1;
|
|
32
33
|
let braceDepth = 0;
|
|
33
|
-
let quote = null;
|
|
34
|
-
let escaped = false;
|
|
35
34
|
// 参数相关
|
|
36
35
|
const argInfo = [];
|
|
37
36
|
let lastCommaPos = openPos + 1;
|
|
37
|
+
let equals = -1;
|
|
38
38
|
// 按照 `key = value` 或者 `value` 的模式匹配各个token的span
|
|
39
39
|
// keepEmpty=true 用于保留 @func(,key=val) 这种情况的空参数
|
|
40
40
|
const pushArg = (end, commaPos, keepEmpty = false) => {
|
|
41
41
|
const span = trimRange(source, lastCommaPos, end);
|
|
42
42
|
if (!keepEmpty && span.start >= span.end)
|
|
43
43
|
return;
|
|
44
|
-
const equals = findTopLevelEquals(source, span.start, span.end);
|
|
45
44
|
const nameSpan = equals > span.start ? trimRange(source, span.start, equals) : undefined;
|
|
46
45
|
const valueSpan = equals > span.start ? trimRange(source, equals + 1, span.end) : span;
|
|
47
46
|
argInfo.push({
|
|
@@ -62,22 +61,13 @@ export function readCall(source, atPos, end) {
|
|
|
62
61
|
});
|
|
63
62
|
for (i++; i < end; i++) {
|
|
64
63
|
const ch = source[i];
|
|
65
|
-
//
|
|
66
|
-
if (quote) {
|
|
67
|
-
if (escaped)
|
|
68
|
-
escaped = false;
|
|
69
|
-
else if (ch === "\\")
|
|
70
|
-
escaped = true;
|
|
71
|
-
else if (ch === quote)
|
|
72
|
-
quote = null;
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
64
|
+
// 跳过字符串;忽略大括号内的小括号 防止大括号内小括号的不闭合影响外部
|
|
75
65
|
if (ch === '"') {
|
|
76
|
-
|
|
77
|
-
|
|
66
|
+
i = findClosingQuote(source, i, end);
|
|
67
|
+
if (i < 0)
|
|
68
|
+
break;
|
|
78
69
|
}
|
|
79
|
-
|
|
80
|
-
if (ch === "{")
|
|
70
|
+
else if (ch === "{")
|
|
81
71
|
braceDepth++;
|
|
82
72
|
else if (ch === "}")
|
|
83
73
|
braceDepth = Math.max(0, braceDepth - 1);
|
|
@@ -95,12 +85,21 @@ export function readCall(source, atPos, end) {
|
|
|
95
85
|
pushArg(i, i, true); // 即使为空也有位置信息
|
|
96
86
|
// 跳过逗号
|
|
97
87
|
lastCommaPos = i + 1;
|
|
88
|
+
equals = -1;
|
|
89
|
+
}
|
|
90
|
+
else if (ch === "=" && equals === -1 && parenDepth === 1 && braceDepth === 0) {
|
|
91
|
+
equals = i;
|
|
98
92
|
}
|
|
99
93
|
}
|
|
100
94
|
// 未闭合调用仍保留已有结构;ParserContext 根据 closeParenSpan 生成诊断
|
|
101
95
|
pushArg(end);
|
|
102
96
|
return callInfo(end);
|
|
103
97
|
}
|
|
98
|
+
/** 实参的小写参数名;位置参数返回 undefined */
|
|
99
|
+
export function callArgName(source, arg) {
|
|
100
|
+
const span = arg?.nameSpan;
|
|
101
|
+
return span && source.slice(span.start, span.end).toLowerCase();
|
|
102
|
+
}
|
|
104
103
|
const SPACE_RE = /\s/;
|
|
105
104
|
export function trimRange(source, start, end) {
|
|
106
105
|
let s = start;
|
|
@@ -111,62 +110,20 @@ export function trimRange(source, start, end) {
|
|
|
111
110
|
e -= 1;
|
|
112
111
|
return { start: s, end: e };
|
|
113
112
|
}
|
|
114
|
-
export function findTopLevelEquals(text, start = 0, end = text.length) {
|
|
115
|
-
let parenDepth = 0;
|
|
116
|
-
let braceDepth = 0;
|
|
117
|
-
let quote = null;
|
|
118
|
-
let escaped = false;
|
|
119
|
-
for (let i = start; i < end; i++) {
|
|
120
|
-
const ch = text[i];
|
|
121
|
-
if (quote) {
|
|
122
|
-
if (escaped)
|
|
123
|
-
escaped = false;
|
|
124
|
-
else if (ch === "\\")
|
|
125
|
-
escaped = true;
|
|
126
|
-
else if (ch === quote)
|
|
127
|
-
quote = null;
|
|
128
|
-
continue;
|
|
129
|
-
}
|
|
130
|
-
if (ch === '"')
|
|
131
|
-
quote = ch;
|
|
132
|
-
// 依旧忽略大括号里面的小括号
|
|
133
|
-
else if (ch === "(" && braceDepth === 0)
|
|
134
|
-
parenDepth++;
|
|
135
|
-
else if (ch === ")" && braceDepth === 0)
|
|
136
|
-
parenDepth = Math.max(0, parenDepth - 1);
|
|
137
|
-
else if (ch === "{")
|
|
138
|
-
braceDepth++;
|
|
139
|
-
else if (ch === "}")
|
|
140
|
-
braceDepth = Math.max(0, braceDepth - 1);
|
|
141
|
-
else if (ch === "=" && parenDepth === 0 && braceDepth === 0)
|
|
142
|
-
return i;
|
|
143
|
-
}
|
|
144
|
-
return -1;
|
|
145
|
-
}
|
|
146
113
|
// 不管大括号 适用于小括号里传递字符串但没加引号的情况
|
|
147
114
|
export function findRightParen(source, start, end) {
|
|
148
115
|
let parenDepth = 1;
|
|
149
|
-
let quote = null;
|
|
150
|
-
let escaped = false;
|
|
151
116
|
for (let i = start; i < end; i++) {
|
|
152
117
|
const ch = source[i];
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
escaped = true;
|
|
158
|
-
else if (ch === quote)
|
|
159
|
-
quote = null;
|
|
160
|
-
continue;
|
|
118
|
+
if (ch === '"') {
|
|
119
|
+
i = findClosingQuote(source, i, end);
|
|
120
|
+
if (i < 0)
|
|
121
|
+
break;
|
|
161
122
|
}
|
|
162
|
-
if (ch === '"')
|
|
163
|
-
quote = ch;
|
|
164
123
|
else if (ch === "(")
|
|
165
124
|
parenDepth++;
|
|
166
|
-
else if (ch === ")")
|
|
167
|
-
|
|
168
|
-
return i;
|
|
169
|
-
}
|
|
125
|
+
else if (ch === ")" && --parenDepth === 0)
|
|
126
|
+
return i;
|
|
170
127
|
}
|
|
171
128
|
return -1;
|
|
172
129
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export declare const SHARP_NOTE_NAMES: readonly ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
|
|
2
2
|
export declare const FLAT_NOTE_NAMES: readonly ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
|
|
3
|
+
export declare const NOTE_LETTERS = "CDEFGAB";
|
|
3
4
|
/** 同一音高的两种拼写升在前、降在后,反查拼写时依赖这个顺序 */
|
|
4
5
|
export declare const NoteNameMap: Record<string, number>;
|
|
5
6
|
export interface RelativeJianpuPitch {
|
|
@@ -7,14 +8,17 @@ export interface RelativeJianpuPitch {
|
|
|
7
8
|
renderAcc: string;
|
|
8
9
|
renderOctave: number;
|
|
9
10
|
}
|
|
10
|
-
export declare function tonality2Midi(tonality: string
|
|
11
|
+
export declare function tonality2Midi(tonality: string): number | null;
|
|
11
12
|
export declare function acc2Offset(acc: string, ignoreNatural?: boolean, initOffset?: number): number;
|
|
12
13
|
export declare function resolveNoteMidi(name: string, acc: string, octave: number, keySignature: string): number | null;
|
|
13
14
|
/**
|
|
14
15
|
* 固化音符在当前大调中的位置,返回“沿调内音级移动”的闭包
|
|
15
16
|
*
|
|
16
17
|
* 交出闭包而不是数据结构,播放层因此无需认识任何调性模型。
|
|
17
|
-
*
|
|
18
|
+
* 基准位置基于已归一化的简谱级数与相对八度,不包含主体的临时升降号。
|
|
18
19
|
*/
|
|
19
|
-
export declare function createDiatonicTranspose(name: string,
|
|
20
|
+
export declare function createDiatonicTranspose(name: string, octave: number, keySignature: string): ((steps: number) => number) | undefined;
|
|
21
|
+
export declare function wrapPitchClass(value: number): number;
|
|
22
|
+
export declare function normalizeAccidentalOffset(value: number): number;
|
|
23
|
+
export declare function offsetToAccidental(offset: number): string;
|
|
20
24
|
export declare function resolveLetterNameToJianpu(name: string, acc: string, octave: number, keySignature: string): RelativeJianpuPitch | null;
|