jpfun 0.6.0 → 0.7.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 (64) hide show
  1. package/dist/converter/midi/convert.d.ts +3 -1
  2. package/dist/converter/midi/convert.js +78 -66
  3. package/dist/converter/musicxml/features.d.ts +19 -5
  4. package/dist/converter/musicxml/features.js +46 -8
  5. package/dist/converter/musicxml/index.js +142 -23
  6. package/dist/converter/musicxml/model.d.ts +15 -0
  7. package/dist/diagnostic.js +1 -1
  8. package/dist/functions/arpeggio/index.js +2 -5
  9. package/dist/functions/arpeggio/shape.d.ts +1 -1
  10. package/dist/functions/arpeggio/shape.js +27 -21
  11. package/dist/functions/bar/index.js +37 -36
  12. package/dist/functions/beam/auto.js +16 -13
  13. package/dist/functions/beam/layout.js +15 -23
  14. package/dist/functions/br/index.js +3 -4
  15. package/dist/functions/default.js +8 -4
  16. package/dist/functions/gliss/index.d.ts +2 -0
  17. package/dist/functions/gliss/index.js +180 -0
  18. package/dist/functions/grace/index.js +3 -6
  19. package/dist/functions/head/index.js +40 -59
  20. package/dist/functions/meter/index.js +19 -18
  21. package/dist/functions/note/accidentals.js +10 -16
  22. package/dist/functions/note/index.js +13 -3
  23. package/dist/functions/ottava/index.d.ts +2 -0
  24. package/dist/functions/ottava/index.js +217 -0
  25. package/dist/functions/page/index.js +3 -3
  26. package/dist/functions/pagebreak/index.d.ts +2 -0
  27. package/dist/functions/pagebreak/index.js +36 -0
  28. package/dist/functions/rest/index.js +12 -14
  29. package/dist/functions/slide/index.d.ts +2 -0
  30. package/dist/functions/slide/index.js +132 -0
  31. package/dist/functions/stack/index.js +8 -15
  32. package/dist/functions/symbol/index.d.ts +9 -0
  33. package/dist/functions/symbol/index.js +41 -12
  34. package/dist/functions/symbol/ornament.js +14 -19
  35. package/dist/functions/symbol/shape.js +4 -1
  36. package/dist/functions/symbol/symbols/fine.js +1 -1
  37. package/dist/functions/symbol/symbols/tr.js +46 -1
  38. package/dist/functions/temporal.d.ts +16 -6
  39. package/dist/functions/temporal.js +16 -6
  40. package/dist/functions/tie/index.js +35 -25
  41. package/dist/functions/up/index.js +5 -7
  42. package/dist/functions/voice/connections.js +7 -5
  43. package/dist/functions/voice/index.js +2 -1
  44. package/dist/functions/volta/index.js +41 -21
  45. package/dist/jpfun.core.min.js +44 -68
  46. package/dist/jpfun.from-midi.min.js +5 -5
  47. package/dist/jpfun.from-musicxml.min.js +8 -8
  48. package/dist/jpfun.min.js +53 -77
  49. package/dist/layout/engine.d.ts +1 -0
  50. package/dist/layout/engine.js +42 -11
  51. package/dist/layout/page.d.ts +3 -2
  52. package/dist/layout/page.js +6 -5
  53. package/dist/layout/types.d.ts +4 -0
  54. package/dist/lowering/loweringContext.js +7 -167
  55. package/dist/parser/parserContext.d.ts +2 -0
  56. package/dist/parser/parserContext.js +27 -6
  57. package/dist/playback/compile.js +11 -2
  58. package/dist/playback/types.d.ts +4 -2
  59. package/dist/render/svg.js +1 -0
  60. package/dist/render/text.d.ts +1 -1
  61. package/dist/render/text.js +3 -2
  62. package/dist/render/types.d.ts +1 -0
  63. package/grammar.md +30 -9
  64. package/package.json +1 -1
@@ -11,7 +11,7 @@
11
11
  import { Fraction } from "../../fraction.js";
12
12
  import { NoteNameMap } from "../../parser/parse-utils/note-utils.js";
13
13
  import { child, children, nameOf, number, text, } from "./dom.js";
14
- import { MusicXmlReader, acceptTuplets, directionDynamic, directionTexts, endingPasses, metronomeBpm, mergeArpeggio, noteArpeggio, noteLyrics, noteModifiers, parsePitch, parseTimeSignature, partMeasures, readInstrument, reportDiagnostic, scoreMetadata, timeModification, transposePitch, } from "./features.js";
14
+ import { MusicXmlReader, acceptTuplets, directionDynamic, directionTexts, endingPasses, metronomeBpm, mergeArpeggio, noteArpeggio, noteGlissandi, noteLyrics, noteModifiers, parsePitch, parseTimeSignature, partMeasures, readInstrument, reportDiagnostic, scoreMetadata, timeModification, transposePitch, } from "./features.js";
15
15
  import { attachAbove, quote, renderHead, renderPitch, renderSystems } from "../source.js";
16
16
  import { matchingLanes, resolveRelations, resolveVoices } from "./resolve.js";
17
17
  const MAJOR_KEYS = ["Cb", "Gb", "Db", "Ab", "Eb", "Bb", "F", "C", "G", "D", "A", "E", "B", "F#", "C#"];
@@ -67,7 +67,7 @@ function setBar(bars, at, token) {
67
67
  else if (token === "|:" && current.includes(":|")) {
68
68
  bars.set(key, current === ":|" ? ":|:" : `${current} |:`);
69
69
  }
70
- else if (token === "||" && !current.includes("|:") && !current.includes(":|")) {
70
+ else if (token === "|||" && !current.includes("|:") && !current.includes(":|")) {
71
71
  bars.set(key, token);
72
72
  }
73
73
  }
@@ -108,6 +108,7 @@ function parseScore(root, diagnostics) {
108
108
  const bars = new Map();
109
109
  const measureBoundaries = new Set();
110
110
  const lineBreaks = new Set();
111
+ const multipleRests = [];
111
112
  let scoreEnd = new Fraction();
112
113
  let order = 0;
113
114
  // 主时间解析:逐 part、逐小节扫描 attributes/direction/note/barline
@@ -128,6 +129,8 @@ function parseScore(root, diagnostics) {
128
129
  const pitches = [];
129
130
  const instrumentChanges = [];
130
131
  const eventInstruments = new Map();
132
+ const measureStarts = [];
133
+ const firstRest = multipleRests.length;
131
134
  const instrumentAt = (id, at) => {
132
135
  const value = { ...(partInstruments?.byId.get(id) ?? partInstruments?.fallback) };
133
136
  for (const change of instrumentChanges) {
@@ -138,6 +141,7 @@ function parseScore(root, diagnostics) {
138
141
  };
139
142
  // 小节游标从零开始,partTime 保存当前小节在全谱中的绝对起点
140
143
  for (let measureIndex = 0; measureIndex < measures.length; measureIndex++) {
144
+ measureStarts.push(partTime.clone());
141
145
  const { body: measure, container: measureContainer } = measures[measureIndex];
142
146
  const measureLocation = {
143
147
  partId, measureIndex: measureIndex + 1,
@@ -171,7 +175,7 @@ function parseScore(root, diagnostics) {
171
175
  setBar(bars, at, new Array(times - 1).fill(":|").join(" "));
172
176
  }
173
177
  else if (text(barline, "bar-style") === "light-heavy")
174
- setBar(bars, at, "||");
178
+ setBar(bars, at, "|||");
175
179
  const ending = child(barline, "ending");
176
180
  const endingType = ending?.getAttribute("type");
177
181
  if (ending && (endingType === "start" || endingType === "stop" || endingType === "discontinue")) {
@@ -221,6 +225,20 @@ function parseScore(root, diagnostics) {
221
225
  mode: text(key, "mode") || "major",
222
226
  });
223
227
  }
228
+ for (const style of children(item, "measure-style")) {
229
+ if (!child(style, "multiple-rest"))
230
+ continue;
231
+ const count = number(style, "multiple-rest");
232
+ if (!Number.isSafeInteger(count) || count <= 0 || count > 0xffffffff) {
233
+ throw new RangeError("MusicXML multiple-rest must be a positive integer in 1..4294967295");
234
+ }
235
+ multipleRests.push({
236
+ location: { ...measureLocation, element: "multiple-rest",
237
+ staff: style.getAttribute("number") || undefined },
238
+ start: at.clone(),
239
+ count,
240
+ });
241
+ }
224
242
  continue;
225
243
  }
226
244
  // backup 和 forward 只重定位当前小节游标,用于交错 voice
@@ -318,6 +336,7 @@ function parseScore(root, diagnostics) {
318
336
  const notationElements = children(item, "notations");
319
337
  const notations = notationElements.length ? new MusicXmlReader(notationElements) : undefined;
320
338
  const pitch = parsePitch(item, location, notations);
339
+ noteGlissandi(notations, pitch, diagnostics);
321
340
  const rest = child(item, "rest") !== undefined;
322
341
  if (!pitch && !rest) {
323
342
  throw new TypeError("MusicXML note must contain pitch, unpitched, or rest");
@@ -391,7 +410,7 @@ function parseScore(root, diagnostics) {
391
410
  throw new RangeError("MusicXML non-grace notes require a positive duration");
392
411
  let event = chord ? previousEvent : undefined;
393
412
  const lyrics = noteLyrics(item);
394
- const modifiers = noteModifiers(notations, diagnostics, location);
413
+ const { modifiers, slides } = noteModifiers(notations, diagnostics, location);
395
414
  const arpeggio = noteArpeggio(notations, location);
396
415
  const tuplets = notations?.readAll("tuplet");
397
416
  // chord 成员只有起点和时值都一致时才合并到前一事件
@@ -408,6 +427,7 @@ function parseScore(root, diagnostics) {
408
427
  postGraces: [],
409
428
  lyrics,
410
429
  arpeggio,
430
+ slides,
411
431
  timeModification: timeModification(item, tuplets),
412
432
  };
413
433
  pendingGraces.delete(laneKey);
@@ -417,6 +437,11 @@ function parseScore(root, diagnostics) {
417
437
  }
418
438
  else {
419
439
  event.arpeggio = mergeArpeggio(event.arpeggio, arpeggio);
440
+ for (const slide of slides) {
441
+ if (!event.slides?.some(item => item.side === slide.side && item.direction === slide.direction)) {
442
+ (event.slides ??= []).push(slide);
443
+ }
444
+ }
420
445
  for (const modifier of modifiers) {
421
446
  if (!event.modifiers.some(item => item.name === modifier.name && item.placement === modifier.placement)) {
422
447
  event.modifiers.push(modifier);
@@ -463,6 +488,10 @@ function parseScore(root, diagnostics) {
463
488
  if (scoreEnd.compare(partTime) < 0)
464
489
  scoreEnd = partTime.clone();
465
490
  }
491
+ measureStarts.push(partTime.clone());
492
+ for (const rest of multipleRests.slice(firstRest)) {
493
+ rest.end = measureStarts[rest.location.measureIndex - 1 + rest.count];
494
+ }
466
495
  for (const groups of pendingGraces.values())
467
496
  for (const group of groups)
468
497
  for (const pitch of group) {
@@ -476,7 +505,7 @@ function parseScore(root, diagnostics) {
476
505
  }
477
506
  for (const [event, id] of eventInstruments)
478
507
  event.program = instrumentAt(id, event.start).program;
479
- setBar(bars, partTime, "||");
508
+ setBar(bars, partTime, "|||");
480
509
  }
481
510
  const laneList = [...lanes.values()];
482
511
  const { wedges, endings } = resolveRelations(wedgePoints, [...endingPoints.values()], diagnostics);
@@ -504,6 +533,7 @@ function parseScore(root, diagnostics) {
504
533
  bars,
505
534
  measureBoundaries,
506
535
  lineBreaks,
536
+ multipleRests,
507
537
  end: scoreEnd,
508
538
  };
509
539
  }
@@ -580,6 +610,9 @@ function eventSource(event, mode, key, metadata, diagnostics, suffix = "") {
580
610
  else
581
611
  reportDiagnostic(diagnostics, "arpeggioOutput", event.arpeggio.location);
582
612
  }
613
+ for (const slide of event.slides ?? []) {
614
+ source = `@slide(${source}, side=${slide.side}, direction=${slide.direction})`;
615
+ }
583
616
  const graceSource = (groups) => groups.map(group => group.length === 1
584
617
  ? pitchSource(group[0], mode, key, metadata)
585
618
  : `{${group.map(pitch => pitchSource(pitch, mode, key, metadata)).join(" ^ ")}}`);
@@ -796,12 +829,36 @@ function labelRelations(score, diagnostics) {
796
829
  endpoint.label = `mx${labelIndex++}`;
797
830
  return `${source}@${endpoint.label}`;
798
831
  },
832
+ labelRest(source, events, end) {
833
+ const labeled = events.find(event => metadata.labels.has(event));
834
+ const endpoint = endingLabels.get(keyOf(end));
835
+ if (!labeled && !endpoint)
836
+ return source;
837
+ const label = labelOf(labeled ?? events[0]);
838
+ for (const event of events)
839
+ if (metadata.labels.has(event))
840
+ metadata.labels.set(event, label);
841
+ if (endpoint) {
842
+ endpoint.at = events[0].start;
843
+ endpoint.label = label;
844
+ }
845
+ return `${source}@${label}`;
846
+ },
799
847
  };
800
848
  const labelOf = (target) => {
801
849
  const label = metadata.labels.get(target) ?? `mx${labelIndex++}`;
802
850
  metadata.labels.set(target, label);
803
851
  return label;
804
852
  };
853
+ const appendRelation = (event, source) => {
854
+ let after = metadata.afterSources.get(event);
855
+ if (!after)
856
+ metadata.afterSources.set(event, after = []);
857
+ after.push(source);
858
+ };
859
+ const activeGlissandi = new Map();
860
+ const glissLineBreaks = [...score.lineBreaks].map(fractionFromKey);
861
+ const glissandi = [];
805
862
  const active = new Map();
806
863
  const unmatchedTies = new Set();
807
864
  const endingActive = new Map();
@@ -826,13 +883,13 @@ function labelRelations(score, diagnostics) {
826
883
  ...event.postGraces.flat(),
827
884
  ];
828
885
  for (const pitch of orderedPitches) {
886
+ if (pitch.glissandi?.length)
887
+ glissandi.push({ event, pitch });
829
888
  const key = `${lane.partId}\0${lane.staff}\0${lane.voice}\0${pitch.step}\0${pitch.alter}\0${pitch.octave}`;
830
889
  const previous = localActive.get(key) ?? (ending ? active.get(key) : undefined);
831
890
  if (pitch.tieStop && previous) {
832
891
  unmatchedTies.delete(previous);
833
- const after = metadata.afterSources.get(event) ?? [];
834
- after.push(`@tie(${labelOf(previous)}, ${labelOf(pitch)})`);
835
- metadata.afterSources.set(event, after);
892
+ appendRelation(event, `@tie(${labelOf(previous)}, ${labelOf(pitch)})`);
836
893
  }
837
894
  else if (pitch.tieStop) {
838
895
  reportDiagnostic(diagnostics, "tieStart", pitch.location);
@@ -848,6 +905,28 @@ function labelRelations(score, diagnostics) {
848
905
  }
849
906
  for (const pitch of unmatchedTies)
850
907
  reportDiagnostic(diagnostics, "tieStop", pitch.location);
908
+ // 复用上面的音高遍历,仅排序滑音端点;无滑音时不额外遍历或排序全谱
909
+ for (const { event, pitch } of glissandi.sort((a, b) => a.event.start.compare(b.event.start) || a.event.order - b.event.order))
910
+ for (const mark of pitch.glissandi ?? []) {
911
+ const key = `${pitch.location.partId}\0${mark.kind}\0${mark.number}`;
912
+ const previous = activeGlissandi.get(key);
913
+ if (mark.type === "start") {
914
+ if (previous)
915
+ reportDiagnostic(diagnostics, "glissReplaced", previous.pitch.location);
916
+ activeGlissandi.set(key, { pitch, event });
917
+ continue;
918
+ }
919
+ activeGlissandi.delete(key);
920
+ if (!previous)
921
+ reportDiagnostic(diagnostics, "glissStart", pitch.location);
922
+ else if (previous.event.start.equals(event.start) || glissLineBreaks.some(time => time.compare(previous.event.start) > 0 && time.compare(event.start) <= 0)) {
923
+ reportDiagnostic(diagnostics, "glissEndpoints", pitch.location);
924
+ }
925
+ else
926
+ appendRelation(event, `@gliss(${labelOf(previous.pitch)}, ${labelOf(pitch)})`);
927
+ }
928
+ for (const { pitch } of activeGlissandi.values())
929
+ reportDiagnostic(diagnostics, "glissStop", pitch.location);
851
930
  const eventLabel = (event) => labelOf(event.pitches.length === 1 ? event.pitches[0] : event);
852
931
  for (const wedge of score.wedges) {
853
932
  // 连续楔形区间落到实际起音端点后再输出离散的 dyn 关系
@@ -887,10 +966,10 @@ function labelRelations(score, diagnostics) {
887
966
  }
888
967
  const from = events[fromIndex];
889
968
  const to = events[toIndex - 1];
890
- const fromLabel = eventLabel(from);
969
+ eventLabel(from);
891
970
  const endpoint = endingLabels.get(keyOf(ending.end)) ?? { at: to.start, label: eventLabel(to) };
892
971
  endingLabels.set(keyOf(ending.end), endpoint);
893
- relations.push(() => `@volta(${fromLabel}, ${endpoint.label}, ${ending.from.passes.join(", ")})`);
972
+ relations.push(() => `@volta(${eventLabel(from)}, ${endpoint.label}, ${ending.from.passes.join(", ")})`);
894
973
  }
895
974
  return { relations, metadata };
896
975
  }
@@ -950,8 +1029,6 @@ function renderScore(score, options, diagnostics) {
950
1029
  }
951
1030
  return programs;
952
1031
  });
953
- // 关系先分配标签,随后 eventSource 才能把标签写进正确音符
954
- const { relations, metadata } = labelRelations(score, diagnostics);
955
1032
  const barTimes = [...score.bars.keys()].map(fractionFromKey)
956
1033
  .sort((left, right) => left.compare(right));
957
1034
  const measureBars = [...score.measureBoundaries].map(fractionFromKey)
@@ -965,28 +1042,70 @@ function renderScore(score, options, diagnostics) {
965
1042
  }
966
1043
  }
967
1044
  }
1045
+ // 换行确定后分配关系标签,随后 eventSource 写进正确音符
1046
+ const { relations, metadata } = labelRelations(score, diagnostics);
968
1047
  const page = score.page
969
1048
  ? `@page(width=${score.page.width}px, height=${score.page.height}px, top=${score.page.top}px, bottom=${score.page.bottom}px, left=${score.page.left}px, right=${score.page.right}px)\n`
970
1049
  : "";
971
- // 明确的 MusicXML 换行优先;若会切开 tuplet,则丢弃该断点以保持时值组完整
1050
+ // 明确的 MusicXML 换行优先;若会切开 tuplet 或多小节休止,则保留完整块
972
1051
  const blocksByLane = score.lanes.map((lane, index) => renderBlocks(lane, pitchMode, score, keys, adjustmentsByLane[index], programsByLane[index], metadata, diagnostics));
973
- const tupletSpans = blocksByLane.flatMap(blocks => blocks
1052
+ const acceptedRests = new Set();
1053
+ for (const rest of score.multipleRests) {
1054
+ const { start, end, count } = rest;
1055
+ const id = `${start}:${end}:${count}`;
1056
+ if (acceptedRests.has(id))
1057
+ continue;
1058
+ const interior = (at) => !!end && at.compare(start) > 0 && at.compare(end) < 0;
1059
+ const meter = pointAt(meters, start);
1060
+ const measureDuration = new Fraction(meter.numerator * 4, meter.denominator);
1061
+ // @rest 是全局休止列,只有所有声部同步休止才不会把其他声部的起音推迟。
1062
+ const covered = blocksByLane.map(blocks => blocks.slice(lowerBoundAt(blocks, start, block => block.start), lowerBoundAt(blocks, end ?? start, block => block.start)));
1063
+ const safe = end && end.clone().sub(start).equals(measureDuration.clone().mul(count))
1064
+ && !meters.some(point => interior(point.at))
1065
+ && ![...score.bars].some(([at, bar]) => interior(fractionFromKey(at)) && bar !== "|")
1066
+ && !score.endings.some(span => interior(span.from.at) || interior(span.end))
1067
+ && adjustmentsByLane.every(points => ![...points.values()].some(point => interior(point.at)))
1068
+ && programsByLane.every(points => ![...points.keys()].some(at => interior(fractionFromKey(at))))
1069
+ && covered.length > 0 && covered.every(blocks => blocks.length === count
1070
+ && blocks.every((block, index) => {
1071
+ const event = block.event;
1072
+ return event?.rest && event.pitches.length === 0 && !event.arpeggio && !event.slides?.length
1073
+ && event.modifiers.length === 0 && event.annotations.length === 0
1074
+ && event.preGraces.length === 0 && event.postGraces.length === 0
1075
+ && event.lyrics.size === 0 && !metadata.afterSources.has(event)
1076
+ && block.start.equals(start.clone().add(measureDuration.clone().mul(index)))
1077
+ && block.end.clone().sub(block.start).equals(measureDuration);
1078
+ }));
1079
+ if (!safe || !end) {
1080
+ reportDiagnostic(diagnostics, "multipleRest", rest.location);
1081
+ continue;
1082
+ }
1083
+ acceptedRests.add(id);
1084
+ for (const [lane, blocks] of blocksByLane.entries()) {
1085
+ const program = programsByLane[lane].get(keyOf(start));
1086
+ const events = covered[lane].map(block => block.event);
1087
+ const source = (program === undefined ? "" : `@program(${program}) `)
1088
+ + attachAdjustment(metadata.labelRest(`@rest(${count})`, events, end), adjustmentsByLane[lane].get(keyOf(start)));
1089
+ blocks.splice(lowerBoundAt(blocks, start, block => block.start), count, { start, end, source });
1090
+ }
1091
+ }
1092
+ const atomicSpans = blocksByLane.flatMap(blocks => blocks
974
1093
  .filter(block => block.source)
975
1094
  .map(block => ({ start: block.start, end: block.end })))
976
1095
  .sort((left, right) => left.start.compare(right.start) || left.end.compare(right.end));
977
- const mergedTupletSpans = [];
978
- for (const span of tupletSpans) {
979
- const previous = mergedTupletSpans.at(-1);
1096
+ const mergedAtomicSpans = [];
1097
+ for (const span of atomicSpans) {
1098
+ const previous = mergedAtomicSpans.at(-1);
980
1099
  if (previous && span.start.compare(previous.end) < 0) {
981
1100
  if (span.end.compare(previous.end) > 0)
982
1101
  previous.end = span.end;
983
1102
  }
984
1103
  else
985
- mergedTupletSpans.push(span);
1104
+ mergedAtomicSpans.push(span);
986
1105
  }
987
1106
  const lineBreaks = new Map([...score.lineBreaks]
988
1107
  .map(value => [value, fractionFromKey(value)])
989
- .filter(([, at]) => !insideSpan(mergedTupletSpans, at)));
1108
+ .filter(([, at]) => !insideSpan(mergedAtomicSpans, at)));
990
1109
  // 每条 lane 独立沿同一组时间点输出;最后再按系统转置成完整 N: 组
991
1110
  const voices = score.lanes.map((lane, laneIndex) => {
992
1111
  const verses = new Set();
@@ -1007,7 +1126,7 @@ function renderScore(score, options, diagnostics) {
1007
1126
  }
1008
1127
  };
1009
1128
  const blocks = blocksByLane[laneIndex];
1010
- const tupletBlocks = blocks.filter(block => block.source);
1129
+ const atomicBlocks = blocks.filter(block => block.source);
1011
1130
  // 所有事件边界与控制点汇成一条时间线,每个相邻点只输出一个连续片段
1012
1131
  const points = new Map();
1013
1132
  points.set("0", new Fraction());
@@ -1017,13 +1136,13 @@ function renderScore(score, options, diagnostics) {
1017
1136
  points.set(keyOf(block.end), block.end);
1018
1137
  }
1019
1138
  for (const at of barTimes)
1020
- if (!insideSpan(tupletBlocks, at))
1139
+ if (!insideSpan(atomicBlocks, at))
1021
1140
  points.set(keyOf(at), at);
1022
1141
  for (const [value, at] of lineBreaks)
1023
1142
  points.set(value, at);
1024
1143
  const adjustments = adjustmentsByLane[laneIndex];
1025
1144
  for (const [value, point] of adjustments) {
1026
- if (!insideSpan(tupletBlocks, point.at))
1145
+ if (!insideSpan(atomicBlocks, point.at))
1027
1146
  points.set(value, point.at);
1028
1147
  }
1029
1148
  const timeline = [...points.values()].sort((left, right) => left.compare(right));
@@ -1050,7 +1169,7 @@ function renderScore(score, options, diagnostics) {
1050
1169
  const block = blocks[blockIndex];
1051
1170
  if (block?.source) {
1052
1171
  if (!block.start.equals(at) || !block.end.equals(next)) {
1053
- throw new RangeError("MusicXML tuplet cannot cross a barline or state change");
1172
+ throw new RangeError("MusicXML atomic block cannot cross a barline or state change");
1054
1173
  }
1055
1174
  outputs.at(-1).push(block.source);
1056
1175
  for (const item of block.lyricEvents ?? []) {
@@ -31,6 +31,11 @@ export interface MusicXmlPitch {
31
31
  octave: number;
32
32
  tieStart: boolean;
33
33
  tieStop: boolean;
34
+ glissandi?: {
35
+ kind: "slide" | "glissando";
36
+ number: string;
37
+ type: "start" | "stop";
38
+ }[];
34
39
  }
35
40
  /** 一个 lane 上的原子事件,同起止音高已合并为一个和弦事件 */
36
41
  export interface MusicXmlEvent {
@@ -52,6 +57,10 @@ export interface MusicXmlEvent {
52
57
  postGraces: MusicXmlPitch[][];
53
58
  lyrics: Map<string, string>;
54
59
  arpeggio?: MusicXmlArpeggio;
60
+ slides?: {
61
+ side: "pre" | "post";
62
+ direction: "up" | "down";
63
+ }[];
55
64
  program?: number;
56
65
  timeModification?: {
57
66
  actual: number;
@@ -147,6 +156,12 @@ export interface ParsedMusicXmlScore {
147
156
  bars: Map<string, string>;
148
157
  measureBoundaries: Set<string>;
149
158
  lineBreaks: Set<string>;
159
+ multipleRests: {
160
+ location: MusicXmlPartLocation;
161
+ start: Fraction;
162
+ end?: Fraction;
163
+ count: number;
164
+ }[];
150
165
  end: Fraction;
151
166
  }
152
167
  export {};
@@ -89,7 +89,7 @@ Diagnostic.error = {
89
89
  return new ErrorDiagnostic("E_INVALID_TONALITY", `无效的调性: "${tonality}",请提供有效的调性名称,必须 [一个大写字母][变音记号#bn只能一个][绝对八度数字],后二者可选`, span);
90
90
  },
91
91
  BreakInsideEvent: (span) => {
92
- return new ErrorDiagnostic("E_BREAK_INSIDE_EVENT", `换行切断了一个仍在持续的事件;请把长音写成多个事件,或调整 @br 的位置`, span);
92
+ return new ErrorDiagnostic("E_BREAK_INSIDE_EVENT", `换行或换页切断了一个仍在持续的事件;请把长音写成多个事件,或调整换行、换页的位置`, span);
93
93
  },
94
94
  InvalidPageConfig: (message, span) => {
95
95
  return new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", message, span);
@@ -2,7 +2,7 @@ import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
2
2
  import { Fraction } from "../../fraction.js";
3
3
  import { getLayoutBounds, prepareLayoutHost } from "../../layout/engine.js";
4
4
  import { ASTFunctionNode, } from "../ASTtypes.js";
5
- import { TemporalNodeBase } from "../temporal.js";
5
+ import { transferFunctionAddons, TemporalNodeBase } from "../temporal.js";
6
6
  import { prepareArpeggioShape } from "./shape.js";
7
7
  /** Fold 保持模块私有;琶音只在自己的边界读取它已经公开在实例上的结构。 */
8
8
  function readFold(node) {
@@ -115,10 +115,7 @@ class ArpeggioTemporal extends TemporalNodeBase {
115
115
  this.host.prepareHorizontal?.(line);
116
116
  }
117
117
  prepareLayout(context) {
118
- if (this.addon) {
119
- this.host.addon = this.addon;
120
- this.addon = void 0;
121
- }
118
+ transferFunctionAddons(this, this.host);
122
119
  prepareLayoutHost(this.host, context);
123
120
  this.springConfig = { ...this.host.springConfig, ...this.springConfig };
124
121
  const em = this.ast.size;
@@ -1,6 +1,6 @@
1
1
  import type { Rect } from "../../layout/types.js";
2
2
  import type { PathCommand } from "../../render/types.js";
3
- /** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界。 */
3
+ /** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界 */
4
4
  export declare function prepareArpeggioShape(height: number, em: number, direction: "" | "up" | "down"): {
5
5
  commands: PathCommand[];
6
6
  bounds: Rect;
@@ -6,7 +6,7 @@ const ARROW_HEIGHT = 93.6719;
6
6
  const ARROW_X = -27.4688;
7
7
  const ARROW_Y = -7.9375;
8
8
  const ARROW_BOTTOM = ARROW_Y + ARROW_HEIGHT;
9
- /** MuseScore 4.7.4 导出的琶音波形块,已旋转到自上而下的局部坐标。 */
9
+ /** MuseScore 4.7.4 导出的琶音波形块,已旋转到自上而下的局部坐标 */
10
10
  const TILE = [
11
11
  { op: "M", x: 28.1406, y: 86.06255 },
12
12
  { op: "C", cx1: 28.1406, cy1: 86.06255, cx2: 30.4531, cy2: 84.07815, x: 32.7656, y: 81.42185 },
@@ -60,9 +60,9 @@ const DOWN_ARROW = [
60
60
  { op: "C", cx1: 58.9063, cy1: 93.6719, cx2: 61.8907, cy2: 92.3438, x: 63.2188, y: 90.3594 },
61
61
  { op: "L", x: 111.8751, y: 21.5156 },
62
62
  ];
63
- function appendShape(output, shape, width, height, offsetX, offsetY, scale, reverse) {
64
- const x = (value) => offsetX + (reverse ? width - value : value) * scale;
65
- const y = (value) => offsetY + (reverse ? height - value : value) * scale;
63
+ function appendShape(output, shape, offsetX, offsetY, scale, reverse) {
64
+ const x = (value) => offsetX + (reverse ? TILE_WIDTH - value : value) * scale;
65
+ const y = (value) => offsetY + (reverse ? TILE_HEIGHT - value : value) * scale;
66
66
  for (const command of shape) {
67
67
  if (command.op === "Z")
68
68
  output.push(command);
@@ -83,7 +83,7 @@ function appendShape(output, shape, width, height, offsetX, offsetY, scale, reve
83
83
  output.push({ op: command.op, x: x(command.x), y: y(command.y) });
84
84
  }
85
85
  }
86
- /** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界。 */
86
+ /** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界 */
87
87
  export function prepareArpeggioShape(height, em, direction) {
88
88
  const arrow = direction === "up" || direction === "down";
89
89
  const segments = Math.max(1, Math.round(height / (em * 0.3)));
@@ -94,29 +94,35 @@ export function prepareArpeggioShape(height, em, direction) {
94
94
  const scale = height / naturalHeight;
95
95
  const commands = [];
96
96
  for (let index = 0; index < regularCount; index++) {
97
- appendShape(commands, TILE, TILE_WIDTH, TILE_HEIGHT, 0, index * TILE_STEP * scale, scale, direction === "");
97
+ appendShape(commands, TILE, 0, index * TILE_STEP * scale, scale, direction === "");
98
98
  }
99
99
  if (arrow) {
100
- appendShape(commands, DOWN_ARROW, 113.5313, ARROW_HEIGHT, ARROW_X * scale, (regularCount * TILE_STEP + ARROW_Y) * scale, scale, false);
100
+ appendShape(commands, DOWN_ARROW, ARROW_X * scale, (regularCount * TILE_STEP + ARROW_Y) * scale, scale, false);
101
101
  }
102
102
  const bounds = pathBounds(commands);
103
103
  if (direction !== "up")
104
104
  return { commands, bounds };
105
- const rotated = commands.map(command => command.op === "Z" ? command : command.op === "C" ? {
106
- op: "C",
107
- cx1: bounds.x + bounds.w - (command.cx1 - bounds.x), cy1: height - command.cy1,
108
- cx2: bounds.x + bounds.w - (command.cx2 - bounds.x), cy2: height - command.cy2,
109
- x: bounds.x + bounds.w - (command.x - bounds.x), y: height - command.y,
110
- } : command.op === "Q" ? {
111
- op: "Q",
112
- cx: bounds.x + bounds.w - (command.cx - bounds.x), cy: height - command.cy,
113
- x: bounds.x + bounds.w - (command.x - bounds.x), y: height - command.y,
114
- } : {
115
- op: command.op,
116
- x: bounds.x + bounds.w - (command.x - bounds.x), y: height - command.y,
117
- });
105
+ const right = bounds.x + bounds.w;
106
+ const mirrorX = (value) => right - (value - bounds.x);
107
+ // 命令尚未发布,直接镜像,避免再分配整条路径及每个命令对象
108
+ for (const command of commands) {
109
+ if (command.op === "Z")
110
+ continue;
111
+ if (command.op === "C") {
112
+ command.cx1 = mirrorX(command.cx1);
113
+ command.cy1 = height - command.cy1;
114
+ command.cx2 = mirrorX(command.cx2);
115
+ command.cy2 = height - command.cy2;
116
+ }
117
+ else if (command.op === "Q") {
118
+ command.cx = mirrorX(command.cx);
119
+ command.cy = height - command.cy;
120
+ }
121
+ command.x = mirrorX(command.x);
122
+ command.y = height - command.y;
123
+ }
118
124
  return {
119
- commands: rotated,
125
+ commands,
120
126
  bounds: { x: bounds.x, y: height - bounds.y - bounds.h, w: bounds.w, h: bounds.h },
121
127
  };
122
128
  }
@@ -13,7 +13,7 @@ class BarFunction extends ASTFunctionNode {
13
13
  args: [
14
14
  {
15
15
  name: "type",
16
- description: "0:普通线 `|`;1:终止线 `||`(左细右粗);2:反复起点 `|:`;3:反复终点 `:|`;4:左右反复 `:|:`",
16
+ description: "0:普通线 `|`;1:双竖线 `||`;2:终止线 `|||`(左细右粗);3:反复起点 `|:`;4:反复终点 `:|`;5:左右反复 `:|:`",
17
17
  type: "number",
18
18
  default: 0,
19
19
  },
@@ -31,16 +31,19 @@ class BarFunction extends ASTFunctionNode {
31
31
  static deSugarAtom(source, start, _end) {
32
32
  let pos = start;
33
33
  let type = 0;
34
+ const slice3 = source.slice(pos, pos + 3);
34
35
  const slice2 = source.slice(pos, pos + 2);
35
- if (slice2 === "||")
36
+ if (slice3 === "|||")
37
+ type = 2, pos += 3;
38
+ else if (slice2 === "||")
36
39
  type = 1, pos += 2;
37
40
  else if (slice2 === "|:")
38
- type = 2, pos += 2;
41
+ type = 3, pos += 2;
39
42
  else if (slice2 === ":|") {
40
43
  if (source[pos + 2] === ":")
41
- type = 4, pos += 3;
44
+ type = 5, pos += 3;
42
45
  else
43
- type = 3, pos += 2;
46
+ type = 4, pos += 2;
44
47
  }
45
48
  else if (source[pos] === "|")
46
49
  type = 0, pos += 1;
@@ -68,7 +71,9 @@ class BarFunction extends ASTFunctionNode {
68
71
  this.size = ctx.length2px(size);
69
72
  }
70
73
  loweringEnter() {
71
- return [this.type >= 2 ? new RepeatBarTemporalNode(this) : new BarTemporalNode(this)];
74
+ return [this.type >= 3 && this.type <= 5
75
+ ? new RepeatBarTemporalNode(this)
76
+ : new BarTemporalNode(this)];
72
77
  }
73
78
  toString() {
74
79
  return `@bar(${this.type}, ${this.size}px)`;
@@ -99,30 +104,20 @@ class BarTemporalNode extends TemporalNodeBase {
99
104
  if (!isVisualTemporalNode(host))
100
105
  return;
101
106
  const run = line.trackRuns.get(host.track);
102
- const index = run?.indexOf(host) ?? -1;
107
+ const index = line.trackIndexOf(host);
103
108
  if (!run || index < 0)
104
109
  return;
105
- line.registerHorizontalLayoutHook(host, host, ({ columns, start }) => {
106
- const current = columns[start].find(element => element.time === host);
107
- if (!current)
108
- return;
109
- const elementOf = (node) => {
110
- const column = line.columnOf(node);
111
- return column < 0 ? undefined : columns[column].find(element => element.time === node);
112
- };
113
- const left = index > 0 ? elementOf(run[index - 1]) : undefined;
114
- if (left) {
115
- left.config.mu_R *= 4;
116
- current.config.mu_L *= 4;
117
- }
118
- const rightNode = run[index + 1];
119
- const right = rightNode ? elementOf(rightNode) : undefined;
120
- // 相邻两个 bar 的中间 gap 由右侧 bar 处理,避免重复增强
121
- if (right && rightNode.mergeKey !== ANCHOR_KEY) {
122
- current.config.mu_R *= 4;
123
- right.config.mu_L *= 4;
124
- }
125
- });
110
+ const left = run[index - 1];
111
+ if (left) {
112
+ left.springConfig.mu_R *= 4;
113
+ host.springConfig.mu_L *= 4;
114
+ }
115
+ const right = run[index + 1];
116
+ // 相邻两个 bar 的中间 gap 由右侧 bar 处理,避免重复增强
117
+ if (right && right.mergeKey !== ANCHOR_KEY) {
118
+ host.springConfig.mu_R *= 4;
119
+ right.springConfig.mu_L *= 4;
120
+ }
126
121
  }
127
122
  prepareLayout() {
128
123
  const { type, size } = this.ast;
@@ -134,16 +129,22 @@ class BarTemporalNode extends TemporalNodeBase {
134
129
  const gap = thin * 1.8;
135
130
  const dotRadius = thin * 0.8;
136
131
  const dotGap = gap + dotRadius;
137
- const hasLeftDots = type === 3 || type === 4;
138
- const hasRightDots = type === 2 || type === 4;
132
+ const hasLeftDots = type === 4 || type === 5;
133
+ const hasRightDots = type === 3 || type === 5;
139
134
  let x = hasLeftDots ? dotRadius * 2 + dotGap : 0;
140
- if (type === 1 || type === 3 || type === 4) {
135
+ if (type === 1) {
136
+ this.lines.push({ x, w: thin });
137
+ x += thin + gap;
138
+ this.lines.push({ x, w: thin });
139
+ x += thin;
140
+ }
141
+ else if (type === 2 || type === 4 || type === 5) {
141
142
  this.lines.push({ x, w: thin });
142
143
  x += thin + gap;
143
144
  this.lines.push({ x, w: thick });
144
145
  x += thick;
145
146
  }
146
- else if (type === 2) {
147
+ else if (type === 3) {
147
148
  this.lines.push({ x, w: thick });
148
149
  x += thick + gap;
149
150
  this.lines.push({ x, w: thin });
@@ -166,7 +167,7 @@ class BarTemporalNode extends TemporalNodeBase {
166
167
  }
167
168
  this.box.w = x;
168
169
  this.box.h = h;
169
- const anchorLine = type >= 2 ? this.lines[0] : undefined;
170
+ const anchorLine = type >= 3 && type <= 5 ? this.lines[0] : undefined;
170
171
  this.box.anchor = anchorLine ? anchorLine.x + anchorLine.w / 2 : x / 2;
171
172
  this.box.visualAxis = h / 2;
172
173
  for (const line of this.lines)
@@ -195,13 +196,13 @@ export function repeatPass(cursor, column) {
195
196
  }
196
197
  /** 只有反复线参与播放顺序;普通小节线太多,进控制流扫描是纯开销 */
197
198
  class RepeatBarTemporalNode extends BarTemporalNode {
198
- /** 只在 type>=2 时创建,所以剩下的三种是:2 段首、3 段尾、4 两者都是 */
199
+ /** 只在 type 3–5 时创建,所以三种分别是:3 段首、4 段尾、5 两者都是 */
199
200
  playbackMarks() {
200
- return this.ast.type === 3 ? [] : [REPEAT_START];
201
+ return this.ast.type === 4 ? [] : [REPEAT_START];
201
202
  }
202
203
  /** 每条反复线只回跳一次,所以连写几条就是几遍 */
203
204
  playbackFlow(columnOf) {
204
- if (this.ast.type === 2)
205
+ if (this.ast.type === 3)
205
206
  return;
206
207
  const at = columnOf(this);
207
208
  if (at === undefined)