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
@@ -6,6 +6,7 @@ import { type DocumentLayoutPage } from "./page.js";
6
6
  import type { HorizontalLineView, LayoutHost, LayoutPrepareContext, PlacedAttachment, Rect } from "./types.js";
7
7
  export interface DocumentLayoutOptions extends SolverOptions {
8
8
  rowGap?: number;
9
+ horizontalOnly?: boolean;
9
10
  }
10
11
  export { PageLayoutError } from "./page.js";
11
12
  export type { DocumentLayoutPage } from "./page.js";
@@ -134,7 +134,7 @@ export function layoutDocument(result, context, options = {}) {
134
134
  },
135
135
  };
136
136
  // 1. 按 layoutLine 切行,并生成固有尺寸与装饰尺寸
137
- const lines = splitLayoutLines(result);
137
+ const { lines, pageBreaks } = splitLayoutLines(result);
138
138
  const objects = [];
139
139
  for (const line of lines) {
140
140
  for (const column of line.columns) {
@@ -157,6 +157,17 @@ export function layoutDocument(result, context, options = {}) {
157
157
  node.box.x += originX;
158
158
  }
159
159
  }
160
+ // midi 导入进行自动断行的快通道
161
+ if (options.horizontalOnly) {
162
+ return {
163
+ diagnostics: result.diagnostics,
164
+ objects,
165
+ attachments: [],
166
+ bounds: { x: 0, y: 0, w: 0, h: 0 },
167
+ lineCount: lines.length,
168
+ pages: [],
169
+ };
170
+ }
160
171
  // host 申报纵向占用
161
172
  // 主体占用只依赖固有尺寸,整个纵向布局中保持不变
162
173
  for (const node of objects) {
@@ -176,7 +187,7 @@ export function layoutDocument(result, context, options = {}) {
176
187
  let pages;
177
188
  let lineTops;
178
189
  try {
179
- ({ pages, lineTops } = paginateLayoutLines(heights, page));
190
+ ({ pages, lineTops } = paginateLayoutLines(heights, page, pageBreaks));
180
191
  }
181
192
  catch (error) {
182
193
  if (!(error instanceof PageLayoutError))
@@ -315,6 +326,7 @@ function splitLayoutLines(result) {
315
326
  attachmentExtents: new Map(),
316
327
  });
317
328
  const lines = [];
329
+ const pageBreaks = [];
318
330
  let currentLine = createLine();
319
331
  for (const column of result.columns) {
320
332
  const visibleColumn = []; // 从列中提取可见元素
@@ -324,16 +336,20 @@ function splitLayoutLines(result) {
324
336
  currentLine = createLine();
325
337
  }
326
338
  for (const node of column) {
339
+ const pageBreak = node.addon?.pageBreakBefore;
340
+ if (pageBreak !== undefined) {
341
+ pageBreaks[lines.length] = (pageBreaks[lines.length] ?? true) && pageBreak;
342
+ }
327
343
  if (isVisualTemporalNode(node))
328
344
  visibleColumn.push(node);
329
345
  }
330
346
  if (visibleColumn.length)
331
347
  currentLine.columns.push(visibleColumn);
332
348
  }
333
- // 至少要有一行,哪怕它没有任何可见对象
334
- if (currentLine.columns.length > 0 || lines.length === 0)
349
+ // 至少保留一行;强制换页新开的尾页也不能因没有可见内容而丢弃
350
+ if (currentLine.columns.length > 0 || pageBreaks[lines.length] !== undefined || lines.length === 0)
335
351
  lines.push(currentLine);
336
- return lines;
352
+ return { lines, pageBreaks };
337
353
  }
338
354
  /** 把新的上下界原地并入已有纵向占用范围 */
339
355
  function includeExtent(extent, top, bottom) {
@@ -355,6 +371,7 @@ export function prepareHorizontalLine(columns, index, options = {}, compact = fa
355
371
  const hooks = [];
356
372
  const columnIndex = new Map();
357
373
  const trackRuns = new Map();
374
+ let trackIndex;
358
375
  for (let column = 0; column < columns.length; column++) {
359
376
  for (const node of columns[column]) {
360
377
  columnIndex.set(node, column);
@@ -371,6 +388,16 @@ export function prepareHorizontalLine(columns, index, options = {}, compact = fa
371
388
  columns,
372
389
  trackRuns,
373
390
  columnOf: host => columnIndex.get(host) ?? -1,
391
+ trackIndexOf(host) {
392
+ if (!trackIndex) {
393
+ trackIndex = new Map();
394
+ for (const run of trackRuns.values()) {
395
+ for (let i = 0; i < run.length; i++)
396
+ trackIndex.set(run[i], i);
397
+ }
398
+ }
399
+ return trackIndex.get(host) ?? -1;
400
+ },
374
401
  registerHorizontalLayoutHook(from, to, hook) {
375
402
  const start = columnIndex.get(from);
376
403
  const end = columnIndex.get(to);
@@ -611,11 +638,12 @@ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredEl
611
638
  }
612
639
  return item;
613
640
  }
614
- function getRangeExtents(line, columns, track) {
641
+ function getExtents(line, range, track) {
615
642
  const view = baseContext.lines[line];
616
643
  const extents = track === undefined ? new Map() : undefined;
617
644
  let extent;
618
- if (!view)
645
+ const horizontal = range && "left" in range ? range : undefined;
646
+ if (!view || (horizontal && horizontal.left >= horizontal.right))
619
647
  return extents;
620
648
  const include = (owner, top, bottom) => {
621
649
  if (extents)
@@ -626,14 +654,16 @@ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredEl
626
654
  extent = { top, bottom };
627
655
  };
628
656
  // 主体和附件共用范围筛选,仅累加结果的容器随查询模式变化。
629
- const { start, end, wholeLine } = resolveRange(view, columns, track, lift);
657
+ const { start, end, wholeLine } = resolveRange(view, range && !("left" in range) ? range : undefined, track, lift);
630
658
  const target = track && lift(track);
631
- let left = wholeLine ? baseContext.originX : Infinity;
632
- let right = wholeLine ? baseContext.originX + baseContext.width : -Infinity;
659
+ let left = horizontal?.left ?? (wholeLine ? baseContext.originX : Infinity);
660
+ let right = horizontal?.right ?? (wholeLine ? baseContext.originX + baseContext.width : -Infinity);
633
661
  for (let column = start; column <= end; column++) {
634
662
  for (const host of view.columns[column]) {
635
663
  if (target && host.track !== target)
636
664
  continue;
665
+ if (horizontal && (host.box.x + host.box.w <= left || host.box.x >= right))
666
+ continue;
637
667
  if (!wholeLine) {
638
668
  if (host.box.x < left)
639
669
  left = host.box.x;
@@ -657,7 +687,8 @@ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredEl
657
687
  }
658
688
  const context = {
659
689
  ...baseContext,
660
- getRangeExtents,
690
+ getRangeExtents: getExtents,
691
+ getHorizontalExtents: (line, left, right) => getExtents(line, { left, right }),
661
692
  getAttachmentOccupancy(dependency) {
662
693
  const geometry = resolveAttachment(dependency)?.geometry;
663
694
  return geometry && (geometry.occupancy ?? geometry.regions);
@@ -19,9 +19,10 @@ export declare const DEFAULT_PAGE_CONFIG: Readonly<PageConfig>;
19
19
  */
20
20
  export declare function normalizePageConfig(source?: Partial<PageConfig>): PageConfig;
21
21
  /**
22
- * 把已经测好自然高度的谱面行分页,并给出每行的全局顶部坐标
22
+ * 把已经测好自然高度的谱面行分页,并给出每行的全局顶部坐标。
23
+ * pageBreaks 按行号索引:undefined 不强制换页,true/false 强制换页并指定上一页是否拉伸。
23
24
  */
24
- export declare function paginateLayoutLines(heights: readonly number[], page: PageConfig): {
25
+ export declare function paginateLayoutLines(heights: readonly number[], page: PageConfig, pageBreaks?: readonly (boolean | undefined)[]): {
25
26
  pages: DocumentLayoutPage[];
26
27
  lineTops: number[];
27
28
  };
@@ -64,9 +64,10 @@ export function normalizePageConfig(source = {}) {
64
64
  return page;
65
65
  }
66
66
  /**
67
- * 把已经测好自然高度的谱面行分页,并给出每行的全局顶部坐标
67
+ * 把已经测好自然高度的谱面行分页,并给出每行的全局顶部坐标。
68
+ * pageBreaks 按行号索引:undefined 不强制换页,true/false 强制换页并指定上一页是否拉伸。
68
69
  */
69
- export function paginateLayoutLines(heights, page) {
70
+ export function paginateLayoutLines(heights, page, pageBreaks = []) {
70
71
  const availableHeight = page.height - page.marginTop - page.marginBottom;
71
72
  const pages = [];
72
73
  const lineTops = new Array(heights.length).fill(0);
@@ -111,9 +112,9 @@ export function paginateLayoutLines(heights, page) {
111
112
  if (Number.isFinite(page.height) && height > availableHeight + 1e-6) {
112
113
  throw new PageLayoutError(i, height, availableHeight);
113
114
  }
114
- // 当前行放不下时先封口已有内容;每页因此至少包含一行
115
- if (Number.isFinite(page.height) && i > start && used + page.lineGap + height > availableHeight + 1e-6) {
116
- closePage(i, true);
115
+ // 强制边界与自然溢出统一封页;无限页高不分配余白
116
+ if (i > start && (pageBreaks[i] !== undefined || Number.isFinite(page.height) && used + page.lineGap + height > availableHeight + 1e-6)) {
117
+ closePage(i, Number.isFinite(page.height) && (pageBreaks[i] ?? true));
117
118
  }
118
119
  used += (i > start ? page.lineGap : 0) + height;
119
120
  contentHeight += height;
@@ -111,6 +111,8 @@ export interface AttachmentLayoutContext extends LayoutPrepareContext {
111
111
  */
112
112
  getRangeExtents(line: number, columns?: LayoutRange): ReadonlyMap<Track, Readonly<Extent>>;
113
113
  getRangeExtents(line: number, columns: LayoutRange | undefined, track: Track): Readonly<Extent> | undefined;
114
+ /** 按全局 x 区间筛选横向相交的主体及已测附件,返回各轨轴局部占用;空区间返回空 Map */
115
+ getHorizontalExtents(line: number, left: number, right: number): ReadonlyMap<Track, Readonly<Extent>>;
114
116
  /** 读取本轮已完成的 attachment 边界;分组在 endLoweringGroup 才注册,因而组内对象必然排在分组之前 */
115
117
  getAttachmentBox(attachment: LayoutAttachment): Readonly<Rect>;
116
118
  /** 读取已测附件的有效占用,保留显式空数组;尚未测量时返回 undefined */
@@ -156,6 +158,8 @@ export interface HorizontalLineView {
156
158
  readonly trackRuns: ReadonlyMap<Track, readonly LayoutHost[]>;
157
159
  /** 主体所在时间列下标;不在本行时返回 -1 */
158
160
  columnOf(host: LayoutHost): number;
161
+ /** 主体在所属 Track 可见序列中的下标;不在本行时返回 -1 */
162
+ trackIndexOf(host: LayoutHost): number;
159
163
  /** 注册横向布局 hook;同一行内按跨度从小到大执行 */
160
164
  registerHorizontalLayoutHook(from: LayoutHost, to: LayoutHost, hook: HorizontalLayoutHook): void;
161
165
  }
@@ -51,8 +51,9 @@ export class LoweringContext {
51
51
  for (const functionClass of functionClasses) {
52
52
  if (functionClass.loweringAugment)
53
53
  augmenters.push(functionClass.loweringAugment);
54
- if (functionClass.loweringFinalize)
55
- finalizers.push(functionClass.loweringFinalize);
54
+ const finalize = functionClass.loweringFinalize;
55
+ if (finalize && !finalizers.includes(finalize))
56
+ finalizers.push(finalize); // 判重是因为pb直接引用了br的实现
56
57
  }
57
58
  }
58
59
  /** 供 page 函数调用 */
@@ -96,8 +97,9 @@ export class LoweringContext {
96
97
  for (const column of columns) {
97
98
  let offset = 0;
98
99
  for (const node of column) {
99
- if (node.breakBefore > offset)
100
- offset = node.breakBefore;
100
+ const requested = node.addon?.breakBefore ?? 0;
101
+ if (requested > offset)
102
+ offset = requested;
101
103
  }
102
104
  line += offset;
103
105
  // 换行列本身归属于换行之后的那一行
@@ -121,6 +123,7 @@ export class LoweringContext {
121
123
  column.shiftTime(timeOffset);
122
124
  let insertion;
123
125
  for (const node of column) {
126
+ state.currentEvent = node;
124
127
  state.velocity = velocityOf(node.track);
125
128
  state.program = programOf(node.track);
126
129
  const offset = node.onTimeState?.(state);
@@ -342,169 +345,6 @@ export class LoweringContext {
342
345
  if (l === 1)
343
346
  return tracks[0];
344
347
  const result = [];
345
- if (l === 2) {
346
- // 双轨特化:与下面的堆归并语义等价,改规则必须两边同时改
347
- const track0 = tracks[0], track1 = tracks[1];
348
- const l0 = track0.length, l1 = track1.length;
349
- let i = 0, j = 0;
350
- const idt = new Fraction(), jdt = new Fraction(); // 应用时间偏移一定要紧在 push 前
351
- let a = track0[0], b = track1[0];
352
- const t0 = a.t.clone(), t1 = b.t.clone();
353
- while (true) {
354
- if (a.mergeKey === ANCHOR_KEY) {
355
- // 等待b也遇到anchor
356
- if (b.mergeKey !== ANCHOR_KEY) {
357
- result.push(b);
358
- b = track1[++j];
359
- while (j < l1 && b.mergeKey !== ANCHOR_KEY) {
360
- if (!jdt.isZero())
361
- b.shiftTime(jdt);
362
- result.push(b);
363
- b = track1[++j];
364
- }
365
- if (b) {
366
- b.shiftTime(jdt);
367
- t1.copyFrom(b.t);
368
- }
369
- }
370
- if (j < l1) { // b 也是 anchor
371
- const maxT = t0.compare(t1) >= 0 ? t0 : t1;
372
- idt.add(maxT).sub(t0);
373
- jdt.add(maxT).sub(t1);
374
- a.push(...b);
375
- a.t = maxT;
376
- b = track1[++j];
377
- }
378
- result.push(a);
379
- a = track0[++i];
380
- // 准备下一个
381
- if (a) {
382
- a.shiftTime(idt);
383
- t0.copyFrom(a.t);
384
- }
385
- if (b) {
386
- b.shiftTime(jdt);
387
- t1.copyFrom(b.t);
388
- }
389
- if (!a || !b)
390
- break;
391
- }
392
- if (b.mergeKey === ANCHOR_KEY) {
393
- if (a.mergeKey !== ANCHOR_KEY) {
394
- result.push(a);
395
- a = track0[++i];
396
- while (i < l0 && a.mergeKey !== ANCHOR_KEY) {
397
- if (!idt.isZero())
398
- a.shiftTime(idt);
399
- result.push(a);
400
- a = track0[++i];
401
- }
402
- if (a) {
403
- a.shiftTime(idt);
404
- t0.copyFrom(a.t);
405
- }
406
- }
407
- if (i < l0) {
408
- const maxT = t0.compare(t1) >= 0 ? t0 : t1;
409
- idt.add(maxT).sub(t0);
410
- jdt.add(maxT).sub(t1);
411
- b.push(...a);
412
- b.t = maxT;
413
- a = track0[++i];
414
- }
415
- result.push(b);
416
- b = track1[++j];
417
- if (a) {
418
- a.shiftTime(idt);
419
- t0.copyFrom(a.t);
420
- }
421
- if (b) {
422
- b.shiftTime(jdt);
423
- t1.copyFrom(b.t);
424
- }
425
- if (!a || !b)
426
- break;
427
- }
428
- if (t0.equals(t1)) {
429
- if (a.mergeKey === b.mergeKey) {
430
- a.push(...b);
431
- result.push(a);
432
- a = track0[++i];
433
- b = track1[++j];
434
- if (a) {
435
- a.shiftTime(idt);
436
- t0.copyFrom(a.t);
437
- }
438
- if (b) {
439
- b.shiftTime(jdt);
440
- t1.copyFrom(b.t);
441
- }
442
- if (!a || !b)
443
- break;
444
- }
445
- else if (a.mergeKey < b.mergeKey) {
446
- result.push(a);
447
- a = track0[++i];
448
- if (a) {
449
- a.shiftTime(idt);
450
- t0.copyFrom(a.t);
451
- }
452
- else
453
- break;
454
- }
455
- else {
456
- result.push(b);
457
- b = track1[++j];
458
- if (b) {
459
- b.shiftTime(jdt);
460
- t1.copyFrom(b.t);
461
- }
462
- else
463
- break;
464
- }
465
- continue;
466
- }
467
- if (t0.compare(t1) < 0) {
468
- result.push(a);
469
- a = track0[++i];
470
- if (a) {
471
- a.shiftTime(idt);
472
- t0.copyFrom(a.t);
473
- continue;
474
- }
475
- else
476
- break;
477
- }
478
- if (t1.compare(t0) < 0) {
479
- result.push(b);
480
- b = track1[++j];
481
- if (b) {
482
- b.shiftTime(jdt);
483
- t1.copyFrom(b.t);
484
- continue;
485
- }
486
- else
487
- break;
488
- }
489
- }
490
- if (a)
491
- result.push(a);
492
- while (++i < l0) {
493
- a = track0[i];
494
- if (!idt.isZero())
495
- a.shiftTime(idt);
496
- result.push(a);
497
- }
498
- if (b)
499
- result.push(b);
500
- while (++j < l1) {
501
- b = track1[j];
502
- if (!jdt.isZero())
503
- b.shiftTime(jdt);
504
- result.push(b);
505
- }
506
- return result;
507
- }
508
348
  // 归并多个轨道
509
349
  const p = new Uint16Array(l); // 当前指向第几个
510
350
  const dt = Array.from({ length: l }, () => new Fraction()); // 每个轨道的偏移量
@@ -16,6 +16,8 @@ export declare class ParserContext {
16
16
  readonly source: string;
17
17
  /** 词法层产物,只由 parseSyntax 填充;所有子上下文共享 */
18
18
  readonly syntax: SyntaxAnalysis;
19
+ /** syntax.tokens 前缀注释区间的扫描游标 */
20
+ private commentIndex;
19
21
  /** 解析过程中产生的诊断信息,包含错误和警告 */
20
22
  diagnostics: Diagnostic[];
21
23
  /** 变量表,存储 `@set` 定义的变量,供解析过程中查询和修改,具有局部作用域 */
@@ -39,6 +39,8 @@ export class ParserContext {
39
39
  source;
40
40
  /** 词法层产物,只由 parseSyntax 填充;所有子上下文共享 */
41
41
  syntax;
42
+ /** syntax.tokens 前缀注释区间的扫描游标 */
43
+ commentIndex;
42
44
  /** 解析过程中产生的诊断信息,包含错误和警告 */
43
45
  diagnostics;
44
46
  /** 变量表,存储 `@set` 定义的变量,供解析过程中查询和修改,具有局部作用域 */
@@ -62,6 +64,7 @@ export class ParserContext {
62
64
  this.source = ctx.source;
63
65
  this.diagnostics = ctx.diagnostics; // 诊断信息全局共享
64
66
  this.syntax = ctx.syntax;
67
+ this.commentIndex = ctx.commentIndex;
65
68
  this.variables = { ...ctx.variables }; // 继承但不修改父上下文的变量
66
69
  this.functions = ctx.functions; // 函数定义全局共享
67
70
  this.deSugarAtomFns = ctx.deSugarAtomFns; // 去糖方法全局共享
@@ -78,6 +81,7 @@ export class ParserContext {
78
81
  tokens: (ctx.commentSpans ?? []).map(span => ({ kind: "comment", span })),
79
82
  calls: [],
80
83
  };
84
+ this.commentIndex = 0;
81
85
  this.variables = Object.fromEntries(Object.entries(ParserContext.systemVariables).map(([name, definition]) => [name, definition.default]));
82
86
  Object.assign(this.variables, ctx.variables);
83
87
  this.functions = ctx.functions ?? new Map();
@@ -159,10 +163,27 @@ export class ParserContext {
159
163
  }
160
164
  return this.syntax;
161
165
  }
162
- // syntax 路径独占这些 span,AST 路径不会读取或改写它们
166
+ // syntax 路径独占这些 span,AST 路径不会读取或改写它们;跨注释区间按前缀 comment token 切开以保证互不重叠
163
167
  addSyntax(kind, span) {
164
- if (span && span.start < span.end) {
165
- this.syntax.tokens.push({ kind, span });
168
+ if (!span || span.start >= span.end)
169
+ return;
170
+ let start = span.start;
171
+ const tokens = this.syntax.tokens;
172
+ while (tokens[this.commentIndex]?.kind === "comment" && tokens[this.commentIndex].span.end <= start) {
173
+ this.commentIndex++;
174
+ }
175
+ for (let i = this.commentIndex; i < tokens.length; i++) {
176
+ const token = tokens[i];
177
+ if (token.kind !== "comment" || token.span.start >= span.end)
178
+ break;
179
+ if (start < token.span.start) {
180
+ tokens.push({ kind, span: { start, end: token.span.start } });
181
+ }
182
+ start = token.span.end;
183
+ this.commentIndex = i + 1;
184
+ }
185
+ if (start < span.end) {
186
+ tokens.push({ kind, span: { start, end: span.end } });
166
187
  }
167
188
  }
168
189
  /** 把已识别的 GrammarNode 映射为源码 token;content/brace 在这里继续递归 */
@@ -170,9 +191,9 @@ export class ParserContext {
170
191
  if (node.kind === "brace") {
171
192
  this.addSyntax("punctuation", { start: node.span.start, end: node.span.start + 1 });
172
193
  const close = Math.min(this.source.length, node.span.end - 1);
194
+ new ParserContext(this).parseSyntax(node.span.start + 1, close);
173
195
  if (this.source[close] === "}")
174
196
  this.addSyntax("punctuation", { start: close, end: close + 1 });
175
- new ParserContext(this).parseSyntax(node.span.start + 1, close);
176
197
  return;
177
198
  }
178
199
  if (node.kind === "label") {
@@ -188,13 +209,11 @@ export class ParserContext {
188
209
  this.syntax.calls.push(call);
189
210
  this.addSyntax("function", call.nameSpan);
190
211
  this.addSyntax("punctuation", call.openParenSpan);
191
- this.addSyntax("punctuation", call.closeParenSpan);
192
212
  const def = this.functions.get(call.name.toLowerCase())?.def;
193
213
  for (let index = 0; index < call.args.length; index++) {
194
214
  const arg = call.args[index];
195
215
  this.addSyntax("property", arg.nameSpan);
196
216
  this.addSyntax("punctuation", arg.equalsSpan);
197
- this.addSyntax("punctuation", arg.commaSpan);
198
217
  const name = arg.nameSpan
199
218
  ? this.source.slice(arg.nameSpan.start, arg.nameSpan.end).toLowerCase()
200
219
  : void 0;
@@ -208,7 +227,9 @@ export class ParserContext {
208
227
  if (kind)
209
228
  this.addSyntax(kind, arg.valueSpan);
210
229
  }
230
+ this.addSyntax("punctuation", arg.commaSpan);
211
231
  }
232
+ this.addSyntax("punctuation", call.closeParenSpan);
212
233
  }
213
234
  /**
214
235
  * 第二轮去糖 & 函数节点创建
@@ -21,11 +21,20 @@ function linearizeColumns(lowering, diagnostics, maxFlowSteps) {
21
21
  }
22
22
  // 折叠成员不单独占列,沿 foldedInto 找到最近的已入列宿主
23
23
  // 标记和关系端点都使用这个映射,保持控制流与实际时间列一致
24
- const resolveColumn = (node) => {
24
+ const resolveColumn = (node, edge) => {
25
25
  for (let at = node; at; at = at.foldedInto) {
26
26
  const index = columnOf.get(at);
27
- if (index !== void 0)
27
+ if (index === undefined)
28
+ continue;
29
+ if (edge !== "end" || at.T.compare(0) <= 0)
28
30
  return index;
31
+ // 一个被标签引用的事件,其完整时值覆盖了多个时间列;终点查询不包含结束时刻的列。
32
+ const finish = at.t.clone().add(at.T);
33
+ let end = index;
34
+ // 用顺序向后找而不是二分因为基本就在后一个(@rest除外)
35
+ while (end + 1 < columns.length && columns[end + 1][0].t.compare(finish) < 0)
36
+ end++;
37
+ return end;
29
38
  }
30
39
  return void 0;
31
40
  };
@@ -30,8 +30,8 @@ export type PlaybackFlowAction = {
30
30
  } | {
31
31
  kind: "stop";
32
32
  };
33
- /** 把事件映射到它在播放时间流中的列;折叠成员会映射到宿主 */
34
- export type PlaybackColumnOf = (node: TemporalNodeBase) => number | undefined;
33
+ /** 默认查询起点列;end 查询宿主时值内的最后一列,不包含结束时刻的列。折叠成员映射到宿主 */
34
+ export type PlaybackColumnOf = (node: TemporalNodeBase, edge?: "start" | "end") => number | undefined;
35
35
  /** 控制流在编译开始时生成的执行声明;range 是运行的升序闭区间,缺省表示每列都运行 */
36
36
  export interface PlaybackFlowHook {
37
37
  readonly range?: readonly [number, number];
@@ -51,6 +51,8 @@ export interface PlaybackState {
51
51
  readonly bpm?: number;
52
52
  readonly velocity?: number;
53
53
  readonly program?: number;
54
+ /** 只改变发声的半音偏移,不改变记谱音高及其源码改写 */
55
+ readonly pitchShift?: number;
54
56
  }
55
57
  /** 按线性化时间扫描的可变系统状态 */
56
58
  export interface PlaybackSystemState {
@@ -65,6 +65,7 @@ export class SvgPainter {
65
65
  this.body.push(`<text x="${number(x)}" y="${number(y)}" `
66
66
  + `font-family="${escapeXml(family)}" font-size="${number(style.fontSize)}" `
67
67
  + `font-weight="${escapeXml(String(weight))}" text-anchor="${textAnchor}" `
68
+ + (style.fontStyle ? `font-style="${escapeXml(style.fontStyle)}" ` : "")
68
69
  + `fill="${escapeXml(fill)}" xml:space="preserve"`
69
70
  + `${opacityAttribute(style)}>${escapeXml(text)}</text>`);
70
71
  }
@@ -21,7 +21,7 @@ export declare class CanvasTextMeasurer implements TextMeasurer {
21
21
  constructor(context: CanvasRenderingContext2D);
22
22
  /**
23
23
  * 字体名称不变但实际资源变化时调用,例如 Web 字体加载完成后,旧缓存仍是回退字体的宽度。
24
- * 清理后宿主需重新编译、布局和绘制;仅切换字体族、字号或字重无需清理,它们已包含在缓存键中。
24
+ * 清理后宿主需重新编译、布局和绘制;切换字体族、字号、字重或斜体无需清理,它们已包含在缓存键中。
25
25
  */
26
26
  clearCache(): void;
27
27
  measureText(text: string, style: TextStyle): GlyphMetrics;
@@ -4,7 +4,8 @@ export const DEFAULT_TEXT_FONT = "sans-serif";
4
4
  export const JIANPU_NUMBER_FONT = "'Cascadia Mono', Consolas, 'Liberation Mono', 'Noto Sans Mono', monospace";
5
5
  /** Canvas 的 font 简写;测量与绘制必须用同一份,否则量到的不是将要画出的字形 */
6
6
  export function canvasFont(style) {
7
- return `${style.fontWeight ?? "normal"} ${style.fontSize}px ${style.fontFamily ?? DEFAULT_TEXT_FONT}`;
7
+ const slant = style.fontStyle ? `${style.fontStyle} ` : "";
8
+ return `${slant}${style.fontWeight ?? "normal"} ${style.fontSize}px ${style.fontFamily ?? DEFAULT_TEXT_FONT}`;
8
9
  }
9
10
  /** 默认文本测量保持确定性,不依赖浏览器和操作系统字体。 */
10
11
  export class DefaultTextMeasurer {
@@ -36,7 +37,7 @@ export class CanvasTextMeasurer {
36
37
  }
37
38
  /**
38
39
  * 字体名称不变但实际资源变化时调用,例如 Web 字体加载完成后,旧缓存仍是回退字体的宽度。
39
- * 清理后宿主需重新编译、布局和绘制;仅切换字体族、字号或字重无需清理,它们已包含在缓存键中。
40
+ * 清理后宿主需重新编译、布局和绘制;切换字体族、字号、字重或斜体无需清理,它们已包含在缓存键中。
40
41
  */
41
42
  clearCache() {
42
43
  this.widths.clear();
@@ -30,6 +30,7 @@ export interface TextStyle {
30
30
  fontSize: number;
31
31
  fontFamily?: string;
32
32
  fontWeight?: string | number;
33
+ fontStyle?: "normal" | "italic" | "oblique";
33
34
  textAlign?: "left" | "center" | "right";
34
35
  fill?: string;
35
36
  opacity?: number;