jpfun 0.2.0 → 0.3.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.
Files changed (112) hide show
  1. package/README.md +69 -11
  2. package/dist/diagnostic.js +5 -2
  3. package/dist/fraction.d.ts +24 -0
  4. package/dist/fraction.js +132 -0
  5. package/dist/functions/ASTtypes.d.ts +24 -7
  6. package/dist/functions/ASTtypes.js +35 -16
  7. package/dist/functions/adjust/index.d.ts +2 -0
  8. package/dist/functions/adjust/index.js +193 -0
  9. package/dist/functions/bar/index.d.ts +8 -0
  10. package/dist/functions/bar/index.js +39 -10
  11. package/dist/functions/beam/auto.js +32 -15
  12. package/dist/functions/beam/index.js +10 -6
  13. package/dist/functions/beam/layout.d.ts +3 -2
  14. package/dist/functions/beam/layout.js +42 -64
  15. package/dist/functions/box/index.js +35 -34
  16. package/dist/functions/br/index.js +9 -7
  17. package/dist/functions/dash/index.js +33 -1
  18. package/dist/functions/default.js +14 -7
  19. package/dist/functions/div/index.d.ts +16 -0
  20. package/dist/functions/div/index.js +45 -17
  21. package/dist/functions/dot/index.js +11 -3
  22. package/dist/functions/grace/index.d.ts +8 -11
  23. package/dist/functions/grace/index.js +82 -76
  24. package/dist/functions/head/index.d.ts +2 -0
  25. package/dist/functions/head/index.js +492 -0
  26. package/dist/functions/key/index.js +97 -3
  27. package/dist/functions/meter/index.d.ts +2 -0
  28. package/dist/functions/meter/index.js +154 -0
  29. package/dist/functions/note/accidentals.d.ts +24 -3
  30. package/dist/functions/note/accidentals.js +31 -1
  31. package/dist/functions/note/index.js +45 -65
  32. package/dist/functions/page/index.d.ts +9 -2
  33. package/dist/functions/page/index.js +50 -4
  34. package/dist/functions/set/index.js +37 -29
  35. package/dist/functions/stack/index.js +26 -22
  36. package/dist/functions/symbol/index.d.ts +21 -0
  37. package/dist/functions/symbol/index.js +94 -0
  38. package/dist/functions/symbol/ornament.d.ts +15 -0
  39. package/dist/functions/symbol/ornament.js +73 -0
  40. package/dist/functions/symbol/shape.d.ts +22 -0
  41. package/dist/functions/symbol/shape.js +48 -0
  42. package/dist/functions/symbol/symbols/accent.d.ts +2 -0
  43. package/dist/functions/symbol/symbols/accent.js +24 -0
  44. package/dist/functions/symbol/symbols/f.d.ts +2 -0
  45. package/dist/functions/symbol/symbols/f.js +55 -0
  46. package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
  47. package/dist/functions/symbol/symbols/fermata.js +24 -0
  48. package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
  49. package/dist/functions/symbol/symbols/mordent.js +43 -0
  50. package/dist/functions/symbol/symbols/p.d.ts +2 -0
  51. package/dist/functions/symbol/symbols/p.js +61 -0
  52. package/dist/functions/symbol/symbols/tr.d.ts +2 -0
  53. package/dist/functions/symbol/symbols/tr.js +75 -0
  54. package/dist/functions/tempo/glyph.d.ts +12 -0
  55. package/dist/functions/tempo/glyph.js +30 -0
  56. package/dist/functions/tempo/index.js +53 -2
  57. package/dist/functions/text/index.js +76 -24
  58. package/dist/functions/tie/index.js +102 -127
  59. package/dist/functions/tuplet/index.d.ts +2 -0
  60. package/dist/functions/tuplet/index.js +211 -0
  61. package/dist/functions/up/index.d.ts +1 -0
  62. package/dist/functions/up/index.js +245 -171
  63. package/dist/functions/voice/index.js +86 -68
  64. package/dist/functions/volta/index.d.ts +2 -0
  65. package/dist/functions/volta/index.js +227 -0
  66. package/dist/index.d.ts +4 -0
  67. package/dist/index.js +4 -0
  68. package/dist/jpfun.min.js +143 -0
  69. package/dist/layout/engine.d.ts +4 -11
  70. package/dist/layout/engine.js +65 -59
  71. package/dist/layout/model.d.ts +8 -1
  72. package/dist/layout/model.js +16 -6
  73. package/dist/layout/path.d.ts +19 -0
  74. package/dist/layout/path.js +101 -0
  75. package/dist/layout/types.d.ts +51 -18
  76. package/dist/layout/types.js +3 -1
  77. package/dist/lowering/loweringContext.d.ts +18 -20
  78. package/dist/lowering/loweringContext.js +132 -94
  79. package/dist/lowering/track.d.ts +1 -1
  80. package/dist/lowering/track.js +1 -1
  81. package/dist/lowering/types.d.ts +49 -12
  82. package/dist/lowering/types.js +9 -3
  83. package/dist/parser/parse-utils/call-utils.d.ts +0 -2
  84. package/dist/parser/parse-utils/call-utils.js +0 -6
  85. package/dist/parser/parse-utils/note-utils.d.ts +8 -0
  86. package/dist/parser/parse-utils/note-utils.js +35 -13
  87. package/dist/parser/parse-utils/string-utils.d.ts +8 -0
  88. package/dist/parser/parse-utils/string-utils.js +20 -0
  89. package/dist/parser/parserContext.d.ts +8 -5
  90. package/dist/parser/parserContext.js +38 -33
  91. package/dist/parser/preprocess.js +17 -11
  92. package/dist/playback/compile.d.ts +18 -0
  93. package/dist/playback/compile.js +401 -0
  94. package/dist/playback/event.d.ts +91 -0
  95. package/dist/playback/event.js +87 -0
  96. package/dist/playback/time.d.ts +14 -0
  97. package/dist/playback/time.js +81 -0
  98. package/dist/playback/types.d.ts +128 -0
  99. package/dist/playback/types.js +3 -0
  100. package/dist/render/canvas.d.ts +4 -4
  101. package/dist/render/canvas.js +20 -5
  102. package/dist/render/index.d.ts +1 -0
  103. package/dist/render/index.js +1 -0
  104. package/dist/render/paint.d.ts +8 -0
  105. package/dist/render/paint.js +101 -0
  106. package/dist/render/svg.d.ts +3 -3
  107. package/dist/render/svg.js +10 -8
  108. package/dist/render/text.d.ts +17 -0
  109. package/dist/render/text.js +35 -0
  110. package/dist/render/types.d.ts +1 -1
  111. package/grammar.md +93 -10
  112. package/package.json +10 -6
@@ -1,6 +1,7 @@
1
1
  import { ASTNodeBase, ASTBraceNode, ASTFunctionNode, ASTTextNode } from "../ASTtypes.js";
2
2
  import { Diagnostic, ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
3
- import { findRightParen, removeQuote } from "../../parser/parse-utils/call-utils.js";
3
+ import { findRightParen } from "../../parser/parse-utils/call-utils.js";
4
+ import { findClosingQuote, removeQuote } from "../../parser/parse-utils/string-utils.js";
4
5
  import { ParserContext, skipSpaces } from "../../parser/parserContext.js";
5
6
  import { isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
6
7
  const WHITEPACE_RE = /\s/;
@@ -94,44 +95,31 @@ L: ...
94
95
  }
95
96
  else {
96
97
  // 字符串收集
97
- let lystart = pos = skipSpaces(source, pos, end);
98
+ pos = skipSpaces(source, pos, end);
98
99
  if (pos >= end)
99
100
  return null; // 没有内容了 不去糖
100
- let lyend = end;
101
- const quote = source[pos];
101
+ let lyric;
102
102
  // 双引号才是字符串,单引号不是
103
- if (quote === '"') {
104
- // 有引号的 直接以引号为界切分
105
- lystart = ++pos;
106
- let escaped = false;
107
- for (; pos < end; pos++) {
108
- const ch = source[pos];
109
- if (escaped)
110
- escaped = false;
111
- else if (ch === "\\")
112
- escaped = true;
113
- else if (ch === quote)
114
- break;
115
- }
116
- if (pos >= end)
117
- throw Diagnostic.error.UnterminatedString({
118
- start: lystart - 1, end
119
- });
120
- lyend = pos++; // 跳过结尾引号
103
+ if (source[pos] === '"') {
104
+ // 未闭合就不去糖,词法层每次击键都跑,不能抛
105
+ const close = findClosingQuote(source, pos, end);
106
+ if (close < 0)
107
+ return null;
108
+ lyric = removeQuote(source.slice(pos, close + 1));
109
+ pos = close + 1;
121
110
  }
122
111
  else {
123
112
  // 没有引号的 以换行符为界切分 预处理已经跳过了转义的换行符了
124
- for (; pos < end; pos++) {
125
- if (source[pos] === '\n')
126
- break;
127
- }
128
- lyend = pos;
113
+ const from = pos;
114
+ while (pos < end && source[pos] !== '\n')
115
+ pos++;
116
+ lyric = source.slice(from, pos).trim();
129
117
  }
130
118
  const node = {
131
119
  kind: "sugar",
132
120
  data: {
133
121
  class: VoiceFunction,
134
- lyric: source.slice(lystart, lyend).trim(),
122
+ lyric,
135
123
  name,
136
124
  },
137
125
  span: { start, end: pos },
@@ -300,9 +288,10 @@ L: ...
300
288
  this.addLyric(key, value);
301
289
  continue;
302
290
  }
303
- const v = ctx.parseArgWithType(value.start, value.end, "string", span.start);
291
+ const arg = value;
292
+ const v = ctx.parseArgWithType(arg.valueSpan, "string", span.start);
304
293
  if (v === null) {
305
- ctx.diagnostics.push(new WarningDiagnostic("W_VOICE_INVALID_LYRIC", `@voice 的歌词参数值解析失败, 参数[${key}]将被忽略`, value));
294
+ ctx.diagnostics.push(new WarningDiagnostic("W_VOICE_INVALID_LYRIC", `@voice 的歌词参数值解析失败, 参数[${key}]将被忽略`, arg.valueSpan));
306
295
  }
307
296
  else
308
297
  this.addLyric(key, v);
@@ -441,12 +430,12 @@ L: la la la
441
430
  }
442
431
  continue;
443
432
  }
444
- // 是用 SourceSpan 体现的原始参数
445
- const v = ctx.parseArgWithType(value.start, value.end, "content", span.start);
433
+ const arg = value;
434
+ const v = ctx.parseArgWithType(arg.valueSpan, "content", span.start);
446
435
  if (v instanceof VoiceFunction)
447
436
  this.addVoice(v);
448
437
  else {
449
- throw new ErrorDiagnostic("E_VOICES_INVALID_CHILD", `@voices 的参数必须是 @voice 函数,但发现了其他类型 ${v?.constructor.name}`, value);
438
+ throw new ErrorDiagnostic("E_VOICES_INVALID_CHILD", `@voices 的参数必须是 @voice 函数,但发现了其他类型 ${v?.constructor.name}`, arg.valueSpan);
450
439
  }
451
440
  }
452
441
  if (this.voices.length === 0) {
@@ -490,7 +479,6 @@ class VoiceNameTemporal extends TemporalNodeBase {
490
479
  super();
491
480
  this.ast = ast;
492
481
  this.braceSpace = braceSpace;
493
- this.T = 0;
494
482
  this.mergeKey = -2;
495
483
  // 既没有名称、也不需要括号空间和标签列时只保留不可见的占位事件:
496
484
  // 它不进入可见对象,也不让空声部失去默认槽位高度
@@ -566,34 +554,30 @@ const BRACE_HOOK_COMMANDS = [
566
554
  * 因此不需要反查任何轨道信息。
567
555
  */
568
556
  class VoicesBraceAttachment {
569
- box = { x: 0, y: 0, w: 0, h: 0 };
570
557
  layer = "background";
571
558
  names;
572
559
  ast;
573
- bars = [];
574
- hooks = [];
560
+ get sourceSpan() { return this.ast.sourceSpan; }
575
561
  constructor(names, ast) {
576
562
  this.names = names;
577
563
  this.ast = ast;
578
564
  }
579
- layout() {
580
- this.bars = [];
581
- this.hooks = [];
565
+ createGeometry() {
582
566
  if (this.names.length < 2)
583
- return [];
567
+ return { regions: [], paint() { } };
584
568
  const first = this.names[0].box;
585
569
  const last = this.names[this.names.length - 1].box;
586
570
  const em = this.ast.size;
587
571
  const top = first.y + first.visualAxis - em * 0.5;
588
572
  const bottom = last.y + last.visualAxis + em * 0.5;
589
573
  if (bottom - top < 1e-6)
590
- return [];
574
+ return { regions: [], paint() { } };
591
575
  // 各部分尺寸都以粗竖线宽度为单位,比例取自常见简谱软件的括线
592
576
  const stem = Math.max(2.5, em * 0.19);
593
577
  const reach = stem * BRACE_HOOK_REACH;
594
578
  const drop = stem * BRACE_HOOK_DROP;
595
579
  const x = first.x + first.anchor + this.ast.braceSpace * 0.3 + stem / 2;
596
- this.bars = [
580
+ const bars = [
597
581
  { x: x - stem / 2, y: top, w: stem, h: bottom - top },
598
582
  {
599
583
  x: x + stem * 1.33 - stem * 0.165,
@@ -602,57 +586,90 @@ class VoicesBraceAttachment {
602
586
  h: bottom - top - stem * 1.34,
603
587
  },
604
588
  ];
605
- this.hooks = [
589
+ const hooks = [
606
590
  { x, y: top, scale: stem, dir: -1 },
607
591
  { x, y: bottom, scale: stem, dir: 1 },
608
592
  ];
609
593
  // 括线画在声部名左侧的空白里,只参与画布边界,不抢任何轨道的纵向空间
610
- return [{
594
+ const regions = [{
611
595
  x: x - stem / 2,
612
596
  y: top - drop,
613
597
  w: reach + stem / 2,
614
598
  h: bottom - top + drop * 2,
615
599
  }];
616
- }
617
- paint(painter) {
618
- for (const bar of this.bars) {
619
- painter.drawRect(bar.x, bar.y, bar.w, bar.h, { fill: "#000" });
620
- }
621
- for (const hook of this.hooks) {
622
- painter.drawPath(BRACE_HOOK_COMMANDS, { fill: "#000" }, { x: hook.x, y: hook.y, scaleX: hook.scale, scaleY: hook.scale * hook.dir });
623
- }
600
+ return {
601
+ regions,
602
+ paint(painter) {
603
+ for (const bar of bars) {
604
+ painter.drawRect(bar.x, bar.y, bar.w, bar.h, { fill: "#000" });
605
+ }
606
+ for (const hook of hooks) {
607
+ painter.drawPath(BRACE_HOOK_COMMANDS, { fill: "#000" }, { x: hook.x, y: hook.y, scaleX: hook.scale, scaleY: hook.scale * hook.dir });
608
+ }
609
+ },
610
+ };
624
611
  }
625
612
  }
626
613
  class VoiceLyricsAttachment {
627
- box = { x: 0, y: 0, w: 0, h: 0 };
628
614
  layer = "foreground";
629
615
  /** lowering 会持续向这个数组加入 voice 内容产生的 temporal */
630
616
  temporalMembers;
631
617
  /** 同一个 voice 的声部名事件,歌词行名称向它的对齐点右对齐 */
632
618
  nameHost;
633
- preparedText = [];
619
+ get sourceSpan() { return this.nameHost.ast.sourceSpan; }
634
620
  constructor(temporalMembers, nameHost) {
635
621
  this.temporalMembers = temporalMembers;
636
622
  this.nameHost = nameHost;
637
623
  }
638
- layout(context) {
639
- this.updateGeometry(context);
640
- // 同一行同一轨的多行歌词由引擎合并成一段占用
641
- return this.preparedText;
642
- }
643
- paint(painter) {
644
- for (const item of this.preparedText) {
645
- painter.drawText(item.text, item.x, item.y + item.textBaselineY, item.style);
624
+ prepareHorizontal(lines, context) {
625
+ const targets = this.temporalMembers
626
+ .filter(isVisualTemporalNode)
627
+ .filter(node => node.ports?.["lyric"]);
628
+ const { lyrics, size } = this.nameHost.ast;
629
+ if (targets.length === 0 || lyrics.length === 0)
630
+ return;
631
+ const style = { fontSize: size * LYRIC_SIZE_RATIO, fill: "#000" };
632
+ for (let i = 0; i < targets.length; i++) {
633
+ const target = targets[i];
634
+ let halfWidth = 0;
635
+ for (const lyric of lyrics) {
636
+ const text = lyric.tokens[i];
637
+ if (text)
638
+ halfWidth = Math.max(halfWidth, context.textMeasurer.measureText(text, style).w / 2);
639
+ }
640
+ if (halfWidth === 0)
641
+ continue;
642
+ // 只扩大求解占用;视觉盒和端口不动,音符内部几何因此保持原位。
643
+ lines[target.layoutLine]?.registerHorizontalLayoutHook(target, target, ({ columns, start }) => {
644
+ const element = columns[start].find(item => item.time === target);
645
+ if (!element)
646
+ return;
647
+ const lyricOffset = target.ports["lyric"].x - target.box.anchor;
648
+ element.WL = Math.max(element.WL, halfWidth - lyricOffset);
649
+ element.WR = Math.max(element.WR, halfWidth + lyricOffset);
650
+ });
646
651
  }
647
652
  }
648
- updateGeometry(context) {
653
+ createGeometry(context) {
654
+ const preparedText = this.prepareGeometry(context);
655
+ // 同一行同一轨的多行歌词由引擎合并成一段占用
656
+ return {
657
+ regions: preparedText,
658
+ paint(painter) {
659
+ for (const item of preparedText) {
660
+ painter.drawText(item.text, item.x, item.y + item.textBaselineY, item.style);
661
+ }
662
+ },
663
+ };
664
+ }
665
+ prepareGeometry(context) {
649
666
  const { lyrics, size } = this.nameHost.ast;
650
- this.preparedText.length = 0;
667
+ const preparedText = [];
651
668
  const targets = this.temporalMembers
652
669
  .filter(isVisualTemporalNode)
653
670
  .filter(node => node.ports?.["lyric"]);
654
671
  if (targets.length === 0 || lyrics.length === 0)
655
- return;
672
+ return preparedText;
656
673
  // 每个 system+track 的歌词共用一条基线
657
674
  // 装饰高度先汇总为下边界,不进入单个 token 的 y 计算
658
675
  const contentBottom = new Map();
@@ -682,7 +699,7 @@ class VoiceLyricsAttachment {
682
699
  if (!text || bottom === undefined)
683
700
  continue;
684
701
  const metrics = context.textMeasurer.measureText(text, lyricStyle);
685
- this.preparedText.push({
702
+ preparedText.push({
686
703
  text,
687
704
  style: lyricStyle,
688
705
  textBaselineY: metrics.baseline,
@@ -697,7 +714,7 @@ class VoiceLyricsAttachment {
697
714
  if (!lyric.name || !labelBox || labelBottom === undefined)
698
715
  continue;
699
716
  const metrics = context.textMeasurer.measureText(lyric.name, nameStyle);
700
- this.preparedText.push({
717
+ preparedText.push({
701
718
  text: lyric.name,
702
719
  style: nameStyle,
703
720
  textBaselineY: metrics.baseline,
@@ -709,5 +726,6 @@ class VoiceLyricsAttachment {
709
726
  track: labelTrack,
710
727
  });
711
728
  }
729
+ return preparedText;
712
730
  }
713
731
  }
@@ -0,0 +1,2 @@
1
+ import { type ASTFunctionClass } from "../ASTtypes.js";
2
+ export declare const VoltaNode: ASTFunctionClass;
@@ -0,0 +1,227 @@
1
+ import { ANCHOR_KEY, isVisualTemporalNode, } from "../../lowering/types.js";
2
+ import { repeatPass } from "../bar/index.js";
3
+ import { ASTFunctionNode, } from "../ASTtypes.js";
4
+ import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
5
+ class VoltaFunction extends ASTFunctionNode {
6
+ static def = {
7
+ name: ["volta"],
8
+ description: "反复房子",
9
+ example: `@volta(from, to, pass, ...) 只在指定遍数演奏的一段区间
10
+ from 和 to 是区间首末音符的标签,因此房子可以横跨谱面行
11
+ pass 是遍数:整段谱面演奏到第几遍时才播这里,必填正整数
12
+ 再写几个遍数就是几遍共用一个房子,顺序随意,标签按升序写成 1.2.
13
+
14
+ |: 1 2 | 3@a 4@b :| 5@c 6@d | 7
15
+ @volta(a, b, 1) @volta(c, d, 2)`,
16
+ allowExtraArgs: true,
17
+ extraArgType: "number",
18
+ args: [
19
+ {
20
+ name: "from",
21
+ type: "label",
22
+ default: null,
23
+ },
24
+ {
25
+ name: "to",
26
+ type: "label",
27
+ default: null,
28
+ },
29
+ {
30
+ // 不给默认值:房子上的数字本来就是谱面的一部分,漏写会让整段被静默跳过
31
+ name: "pass",
32
+ type: "number",
33
+ default: null,
34
+ },
35
+ ],
36
+ };
37
+ from;
38
+ to;
39
+ /** 进入本房子的遍数,升序去重 */
40
+ passes;
41
+ /** 区间没有可见宿主时括线的尺寸基准 */
42
+ size;
43
+ /** volta 不推进时间,只把 AST 端点解析成稳定的 temporal 引用 */
44
+ loweringEnter(ctx) {
45
+ let from = ctx.getTemporalNodes(this.from).at(0);
46
+ let to = ctx.getTemporalNodes(this.to).at(-1);
47
+ while (from?.foldedInto)
48
+ from = from.foldedInto;
49
+ while (to?.foldedInto)
50
+ to = to.foldedInto;
51
+ if (!from || !to) {
52
+ ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("volta", this.sourceSpan));
53
+ return [];
54
+ }
55
+ ctx.addAttachment(new VoltaAttachment(this, from, to));
56
+ return [];
57
+ }
58
+ constructor(span, args, ctx, parent = null) {
59
+ super(span, parent);
60
+ const [from, to, pass] = this.getArgValue(args, ctx);
61
+ // 第一个遍数走 def.args,其余的落在 extraArgType 里,按位置索引取
62
+ const passes = new Set([pass]);
63
+ for (let i = 3; args.has(i); i++)
64
+ passes.add(args.get(i));
65
+ this.from = from;
66
+ this.to = to;
67
+ this.passes = [...passes].sort((a, b) => a - b);
68
+ this.size = ctx.fontSize;
69
+ if (this.passes.some(value => !Number.isSafeInteger(value) || value <= 0)) {
70
+ throw new ErrorDiagnostic("E_VOLTA_INVALID_PASS", "@volta 的遍数必须是正整数", span);
71
+ }
72
+ }
73
+ toString(source) {
74
+ const name = (node) => node.label ?? node.toString(source);
75
+ return `@volta(${name(this.from)}, ${name(this.to)}, ${this.passes.join(", ")})`;
76
+ }
77
+ }
78
+ export const VoltaNode = VoltaFunction;
79
+ /** 取 ANCHOR 列的几何宿主 */
80
+ function barIn(column) {
81
+ if (column?.[0]?.mergeKey !== ANCHOR_KEY)
82
+ return undefined;
83
+ return column[0];
84
+ }
85
+ class VoltaAttachment {
86
+ ast;
87
+ from;
88
+ to;
89
+ layer = "foreground";
90
+ /** 首末端点列最上方的对象、区间最大字号与紧邻的小节线,横向准备时填入 */
91
+ head;
92
+ tail;
93
+ span = 0;
94
+ leftBar;
95
+ rightBar;
96
+ /** 逐行记下覆盖列内各轨的最高点,相对该轨视觉轴;轴要等纵向求解才知道 */
97
+ topsByLine = [];
98
+ constructor(ast, from, to) {
99
+ this.ast = ast;
100
+ this.from = from;
101
+ this.to = to;
102
+ }
103
+ get sourceSpan() { return this.ast.sourceSpan; }
104
+ /** 房子只决定跳不跳过本列;遍数怎么数、什么时候回跳都归小节线 */
105
+ playbackFlow(columnOf) {
106
+ const from = columnOf(this.from);
107
+ const to = columnOf(this.to);
108
+ if (from === undefined || to === undefined)
109
+ return;
110
+ const start = Math.min(from, to);
111
+ const end = Math.max(from, to);
112
+ return {
113
+ range: [start, end],
114
+ // 这里还是会和 bar 耦合。但只有bar有反复功能,不值得抽取为系统机制
115
+ run: cursor => this.ast.passes.includes(repeatPass(cursor, start))
116
+ ? undefined
117
+ : { kind: "jump", column: end + 1 },
118
+ };
119
+ }
120
+ prepareHorizontal(lines) {
121
+ if (!isVisualTemporalNode(this.from) || !isVisualTemporalNode(this.to))
122
+ return;
123
+ const locate = (node) => {
124
+ const view = lines[node.layoutLine];
125
+ return { line: node.layoutLine, view, column: view.columnOf(node) };
126
+ };
127
+ // 两个标签谁写在前面不一定,按谱面顺序摆正
128
+ let [from, to] = [locate(this.from), locate(this.to)];
129
+ if (from.line > to.line || (from.line === to.line && from.column > to.column)) {
130
+ [from, to] = [to, from];
131
+ }
132
+ if (from.column < 0 || to.column < 0)
133
+ return;
134
+ this.head = from.view.columns[from.column][0];
135
+ this.tail = to.view.columns[to.column][0];
136
+ this.leftBar = barIn(from.view.columns[from.column - 1]);
137
+ this.rightBar = barIn(to.view.columns[to.column + 1]);
138
+ this.span = 0;
139
+ this.topsByLine.length = 0;
140
+ // 括线盖住的是:首行从起点列向右、末行向左到终点列、中间行整行
141
+ for (let line = from.line; line <= to.line; line++) {
142
+ const view = lines[line];
143
+ const start = line === from.line ? from.column : 0;
144
+ const end = line === to.line ? to.column : view.columns.length - 1;
145
+ const tops = new Map();
146
+ for (let column = start; column <= end; column++) {
147
+ const hosts = view.columns[column];
148
+ this.span = Math.max(this.span, hosts[0].ast.size);
149
+ // 此刻 visualAxis 已冻结,纵向求解后 box.y 就是 该轨视觉轴 - visualAxis
150
+ for (const host of hosts) {
151
+ const top = -host.box.visualAxis;
152
+ const known = tops.get(host.track);
153
+ if (known === undefined || top < known)
154
+ tops.set(host.track, top);
155
+ }
156
+ }
157
+ this.topsByLine.push(tops);
158
+ }
159
+ }
160
+ createGeometry(context) {
161
+ const { head, tail, leftBar, rightBar } = this;
162
+ if (!head || !tail)
163
+ return { regions: [], paint() { } };
164
+ const firstLine = head.layoutLine;
165
+ const lastLine = tail.layoutLine;
166
+ const size = this.span || this.ast.size;
167
+ const style = {
168
+ fontSize: size * 0.6,
169
+ textAlign: "left",
170
+ fill: "#000",
171
+ };
172
+ const text = this.ast.passes.map(pass => `${pass}.`).join("");
173
+ const metrics = context.textMeasurer.measureText(text, style);
174
+ const strokeWidth = Math.max(1, size * 0.055);
175
+ const halfStroke = strokeWidth / 2;
176
+ const hook = metrics.h * 1.2;
177
+ // 标签在下折钩的高度里垂直居中
178
+ const textY = (hook - metrics.h) / 2 + metrics.baseline;
179
+ // 中间行可能没有本房子的可见对象,仍要补满,所以按行号连续遍历
180
+ const draws = Array.from({ length: lastLine - firstLine + 1 }, (_, offset) => {
181
+ const line = firstLine + offset;
182
+ // 跨行断开的那一侧一直画到页边,接上下一行
183
+ const left = line !== firstLine ? context.originX
184
+ : leftBar?.layoutLine === line ? leftBar.box.x + leftBar.box.anchor + 5 // 5是防止前后房子竖线重合
185
+ : head.box.x + (head.ports["body.left"]?.x ?? 0);
186
+ const right = line !== lastLine ? context.originX + context.width
187
+ : rightBar?.layoutLine === line ? rightBar.box.x + rightBar.box.anchor
188
+ : tail.box.x + (tail.ports["body.right"]?.x ?? tail.box.w);
189
+ let topTrack = head.track;
190
+ let hostTop = Infinity;
191
+ for (const [track, top] of this.topsByLine[offset]) {
192
+ const y = context.getVisualAxis(line, track) + top;
193
+ if (y < hostTop) {
194
+ topTrack = track;
195
+ hostTop = y;
196
+ }
197
+ }
198
+ // 整行空白时没有主体可以参照,退到轨道轴上
199
+ if (hostTop === Infinity)
200
+ hostTop = context.getVisualAxis(line, topTrack);
201
+ return { left, right, lineY: hostTop - size * 0.2 - hook, line, track: topTrack };
202
+ });
203
+ return {
204
+ regions: draws.map(({ left, right, lineY, line, track }) => ({
205
+ x: left - halfStroke,
206
+ y: lineY - halfStroke,
207
+ w: right - left + strokeWidth,
208
+ h: hook + strokeWidth,
209
+ line,
210
+ track,
211
+ })),
212
+ paint(painter) {
213
+ const lineStyle = { stroke: "#000", strokeWidth };
214
+ for (const { left, right, lineY, line } of draws) {
215
+ painter.drawLine(left, lineY, right, lineY, lineStyle);
216
+ // 只有房子真正开始和结束的那一端才下折
217
+ if (line === firstLine) {
218
+ painter.drawLine(left, lineY, left, lineY + hook, lineStyle);
219
+ painter.drawText(text, left + size * 0.15, lineY + textY + 2, style);
220
+ }
221
+ if (line === lastLine)
222
+ painter.drawLine(right, lineY, right, lineY + hook, lineStyle);
223
+ }
224
+ },
225
+ };
226
+ }
227
+ }
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from "./pipeline.js";
2
2
  export * from "./functions/ASTtypes.js";
3
3
  export * from "./functions/default.js";
4
4
  export * from "./diagnostic.js";
5
+ export * from "./fraction.js";
5
6
  export * from "./parser/types.js";
6
7
  export * from "./parser/grammarType.js";
7
8
  export * from "./parser/parserContext.js";
@@ -9,6 +10,9 @@ export * from "./parser/preprocess.js";
9
10
  export * from "./lowering/types.js";
10
11
  export * from "./lowering/loweringContext.js";
11
12
  export * from "./lowering/track.js";
13
+ export * from "./playback/types.js";
14
+ export * from "./playback/time.js";
15
+ export * from "./playback/compile.js";
12
16
  export * from "./layout/types.js";
13
17
  export * from "./layout/model.js";
14
18
  export * from "./layout/default.js";
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ export * from "./pipeline.js";
2
2
  export * from "./functions/ASTtypes.js";
3
3
  export * from "./functions/default.js";
4
4
  export * from "./diagnostic.js";
5
+ export * from "./fraction.js";
5
6
  export * from "./parser/types.js";
6
7
  export * from "./parser/grammarType.js";
7
8
  export * from "./parser/parserContext.js";
@@ -9,6 +10,9 @@ export * from "./parser/preprocess.js";
9
10
  export * from "./lowering/types.js";
10
11
  export * from "./lowering/loweringContext.js";
11
12
  export * from "./lowering/track.js";
13
+ export * from "./playback/types.js";
14
+ export * from "./playback/time.js";
15
+ export * from "./playback/compile.js";
12
16
  export * from "./layout/types.js";
13
17
  export * from "./layout/model.js";
14
18
  export * from "./layout/default.js";