jpfun 0.8.0 → 0.9.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 (97) hide show
  1. package/dist/converter/midi/convert.js +8 -55
  2. package/dist/converter/musicxml/features.d.ts +1 -1
  3. package/dist/converter/musicxml/features.js +1 -1
  4. package/dist/converter/musicxml/index.js +8 -18
  5. package/dist/converter/source.d.ts +4 -7
  6. package/dist/converter/source.js +11 -9
  7. package/dist/functions/ASTtypes.d.ts +1 -14
  8. package/dist/functions/ASTtypes.js +5 -36
  9. package/dist/functions/adjust/index.js +45 -33
  10. package/dist/functions/arpeggio/index.js +42 -45
  11. package/dist/functions/arpeggio/shape.d.ts +2 -3
  12. package/dist/functions/arpeggio/shape.js +20 -50
  13. package/dist/functions/bar/index.js +3 -8
  14. package/dist/functions/beam/auto.js +54 -123
  15. package/dist/functions/beam/index.js +4 -9
  16. package/dist/functions/box/index.js +0 -6
  17. package/dist/functions/dash/index.js +3 -4
  18. package/dist/functions/div/index.d.ts +2 -4
  19. package/dist/functions/div/index.js +10 -10
  20. package/dist/functions/dot/index.js +3 -9
  21. package/dist/functions/dyn/index.js +6 -26
  22. package/dist/functions/gliss/index.js +95 -89
  23. package/dist/functions/grace/index.d.ts +2 -5
  24. package/dist/functions/grace/index.js +44 -89
  25. package/dist/functions/head/index.js +9 -6
  26. package/dist/functions/key/index.js +6 -6
  27. package/dist/functions/meter/index.js +3 -3
  28. package/dist/functions/note/index.js +1 -1
  29. package/dist/functions/note/source-transform.js +2 -5
  30. package/dist/functions/ottava/index.js +11 -32
  31. package/dist/functions/page/index.js +13 -16
  32. package/dist/functions/rest/index.js +2 -4
  33. package/dist/functions/slide/index.js +0 -1
  34. package/dist/functions/stack/index.js +8 -57
  35. package/dist/functions/temporal.d.ts +2 -2
  36. package/dist/functions/temporal.js +7 -0
  37. package/dist/functions/text/index.js +24 -30
  38. package/dist/functions/tie/index.js +14 -37
  39. package/dist/functions/tremolo/index.js +0 -1
  40. package/dist/functions/tuplet/index.js +0 -6
  41. package/dist/functions/up/index.js +79 -83
  42. package/dist/functions/voice/connections.d.ts +2 -1
  43. package/dist/functions/voice/connections.js +48 -10
  44. package/dist/functions/voice/index.d.ts +2 -3
  45. package/dist/functions/voice/index.js +2 -970
  46. package/dist/functions/voice/lyrics.d.ts +30 -0
  47. package/dist/functions/voice/lyrics.js +289 -0
  48. package/dist/functions/voice/voice.d.ts +63 -0
  49. package/dist/functions/voice/voice.js +373 -0
  50. package/dist/functions/voice/voices.d.ts +106 -0
  51. package/dist/functions/voice/voices.js +423 -0
  52. package/dist/functions/volta/index.js +5 -23
  53. package/dist/jpfun.core.min.js +65 -56
  54. package/dist/jpfun.from-midi.min.js +5 -5
  55. package/dist/jpfun.from-musicxml.min.js +7 -7
  56. package/dist/jpfun.min.js +68 -59
  57. package/dist/layout/engine.js +30 -18
  58. package/dist/layout/model.d.ts +0 -1
  59. package/dist/layout/model.js +1 -4
  60. package/dist/layout/page.d.ts +2 -1
  61. package/dist/layout/page.js +14 -14
  62. package/dist/layout/range.d.ts +7 -6
  63. package/dist/layout/range.js +23 -14
  64. package/dist/layout/types.d.ts +9 -28
  65. package/dist/lowering/loweringContext.d.ts +8 -11
  66. package/dist/lowering/loweringContext.js +7 -13
  67. package/dist/lowering/track.d.ts +8 -1
  68. package/dist/lowering/types.d.ts +12 -8
  69. package/dist/parser/parse-utils/call-utils.d.ts +1 -2
  70. package/dist/parser/parse-utils/call-utils.js +6 -34
  71. package/dist/parser/parse-utils/note-utils.d.ts +3 -3
  72. package/dist/parser/parse-utils/note-utils.js +17 -28
  73. package/dist/parser/parse-utils/string-utils.d.ts +1 -1
  74. package/dist/parser/parse-utils/string-utils.js +8 -6
  75. package/dist/parser/parserContext.d.ts +9 -9
  76. package/dist/parser/parserContext.js +54 -10
  77. package/dist/pipeline.js +2 -2
  78. package/dist/playback/compile.js +13 -14
  79. package/dist/playback/event.d.ts +6 -6
  80. package/dist/playback/event.js +13 -49
  81. package/dist/render/canvas.d.ts +0 -2
  82. package/dist/render/canvas.js +0 -21
  83. package/dist/render/svg.d.ts +0 -1
  84. package/dist/render/svg.js +33 -31
  85. package/dist/render/text.d.ts +36 -1
  86. package/dist/render/text.js +134 -0
  87. package/dist/render/types.d.ts +0 -2
  88. package/grammar.md +16 -13
  89. package/package.json +1 -1
  90. package/dist/iife/core.d.ts +0 -1
  91. package/dist/iife/core.js +0 -3
  92. package/dist/iife/from-midi.d.ts +0 -1
  93. package/dist/iife/from-midi.js +0 -15
  94. package/dist/iife/from-musicxml.d.ts +0 -1
  95. package/dist/iife/from-musicxml.js +0 -3
  96. package/dist/iife/full.d.ts +0 -1
  97. package/dist/iife/full.js +0 -3
@@ -1,5 +1,5 @@
1
1
  import { layoutPageBounds, paintLayoutPages } from "./paint.js";
2
- import { DEFAULT_TEXT_FONT } from "./text.js";
2
+ import { DEFAULT_TEXT_FONT, JIANPU_NUMBER_FONT } from "./text.js";
3
3
  function number(value) {
4
4
  if (!Number.isFinite(value))
5
5
  return "0";
@@ -10,9 +10,9 @@ function escapeXml(value) {
10
10
  .replace(/&/g, "&")
11
11
  .replace(/</g, "&lt;")
12
12
  .replace(/>/g, "&gt;")
13
- .replace(/"/g, "&quot;")
14
- .replace(/'/g, "&apos;");
13
+ .replace(/"/g, "&quot;");
15
14
  }
15
+ const DEFAULT_SVG_FONT_ATTR = escapeXml(JIANPU_NUMBER_FONT);
16
16
  /**
17
17
  * 把结构化路径命令转换为 SVG path data
18
18
  * 可选缩放和平移用于直接生成最终坐标
@@ -47,11 +47,6 @@ function pathCommandsToSvg(commands, transform) {
47
47
  }
48
48
  return parts.join(" ");
49
49
  }
50
- function opacityAttribute(style) {
51
- if (style?.opacity === void 0)
52
- return "";
53
- return ` opacity="${number(style.opacity)}"`;
54
- }
55
50
  /** 生成独立 SVG 字符串的 Painter;路径直接输出最终坐标 */
56
51
  export class SvgPainter {
57
52
  body = [];
@@ -62,38 +57,42 @@ export class SvgPainter {
62
57
  const textAnchor = style.textAlign === "center"
63
58
  ? "middle"
64
59
  : style.textAlign === "right" ? "end" : "start";
65
- this.body.push(`<text x="${number(x)}" y="${number(y)}" `
66
- + `font-family="${escapeXml(family)}" font-size="${number(style.fontSize)}" `
67
- + `font-weight="${escapeXml(String(weight))}" text-anchor="${textAnchor}" `
68
- + (style.fontStyle ? `font-style="${escapeXml(style.fontStyle)}" ` : "")
69
- + `fill="${escapeXml(fill)}" xml:space="preserve"`
70
- + `${opacityAttribute(style)}>${escapeXml(text)}</text>`);
60
+ this.body.push(`<text x="${number(x)}" y="${number(y)}"`
61
+ + (family !== JIANPU_NUMBER_FONT ? ` font-family="${escapeXml(family)}"` : "")
62
+ + ` font-size="${number(style.fontSize)}"`
63
+ + (weight !== "normal" ? ` font-weight="${escapeXml(String(weight))}"` : "")
64
+ + (textAnchor !== "start" ? ` text-anchor="${textAnchor}"` : "")
65
+ + (style.fontStyle && style.fontStyle !== "normal" ? ` font-style="${escapeXml(style.fontStyle)}"` : "")
66
+ + (fill !== "#000" ? ` fill="${escapeXml(fill)}"` : "")
67
+ + (/\s/.test(text) ? ` xml:space="preserve"` : "")
68
+ + `>${escapeXml(text)}</text>`);
71
69
  }
72
70
  drawLine(x1, y1, x2, y2, style) {
73
71
  const stroke = style?.stroke ?? style?.fill ?? "#000";
74
72
  const strokeWidth = style?.strokeWidth ?? 1;
75
73
  this.body.push(`<line x1="${number(x1)}" y1="${number(y1)}" `
76
74
  + `x2="${number(x2)}" y2="${number(y2)}" `
77
- + `stroke="${escapeXml(stroke)}" stroke-width="${number(strokeWidth)}" stroke-linecap="round"`
78
- + `${opacityAttribute(style)} />`);
75
+ + `stroke="${escapeXml(stroke)}" stroke-width="${number(strokeWidth)}" stroke-linecap="round" />`);
79
76
  }
80
77
  drawRect(x, y, w, h, style) {
81
78
  const fill = style?.fill ?? "none";
82
79
  const stroke = style?.stroke ?? "none";
83
80
  const strokeWidth = style?.strokeWidth ?? 1;
84
81
  this.body.push(`<rect x="${number(x)}" y="${number(y)}" `
85
- + `width="${number(Math.max(0, w))}" height="${number(Math.max(0, h))}" `
86
- + `fill="${escapeXml(fill)}" stroke="${escapeXml(stroke)}" stroke-width="${number(strokeWidth)}"`
87
- + `${opacityAttribute(style)} />`);
82
+ + `width="${number(Math.max(0, w))}" height="${number(Math.max(0, h))}"`
83
+ + (fill !== "#000" ? ` fill="${escapeXml(fill)}"` : "")
84
+ + (stroke !== "none" ? ` stroke="${escapeXml(stroke)}" stroke-width="${number(strokeWidth)}"` : "")
85
+ + ` />`);
88
86
  }
89
87
  drawCircle(cx, cy, r, style) {
90
88
  const fill = style?.fill ?? "#000";
91
89
  const stroke = style?.stroke ?? "none";
92
90
  const strokeWidth = style?.strokeWidth ?? 1;
93
91
  this.body.push(`<circle cx="${number(cx)}" cy="${number(cy)}" `
94
- + `r="${number(Math.max(0, r))}" fill="${escapeXml(fill)}" `
95
- + `stroke="${escapeXml(stroke)}" stroke-width="${number(strokeWidth)}"`
96
- + `${opacityAttribute(style)} />`);
92
+ + `r="${number(Math.max(0, r))}"`
93
+ + (fill !== "#000" ? ` fill="${escapeXml(fill)}"` : "")
94
+ + (stroke !== "none" ? ` stroke="${escapeXml(stroke)}" stroke-width="${number(strokeWidth)}"` : "")
95
+ + ` />`);
97
96
  }
98
97
  drawPath(commands, style, transform) {
99
98
  if (commands.length === 0)
@@ -102,21 +101,24 @@ export class SvgPainter {
102
101
  const fill = style?.fill ?? "none";
103
102
  const stroke = style?.stroke ?? "none";
104
103
  const strokeWidth = style?.strokeWidth ?? 1;
105
- this.body.push(`<path d="${d}" fill="${escapeXml(fill)}" stroke="${escapeXml(stroke)}" `
106
- + `stroke-width="${number(strokeWidth)}" stroke-linecap="round" stroke-linejoin="round"`
107
- + `${opacityAttribute(style)} />`);
104
+ this.body.push(`<path d="${d}"`
105
+ + (fill !== "#000" ? ` fill="${escapeXml(fill)}"` : "")
106
+ + (stroke !== "none"
107
+ ? ` stroke="${escapeXml(stroke)}" stroke-width="${number(strokeWidth)}" stroke-linecap="round" stroke-linejoin="round"`
108
+ : "")
109
+ + ` />`);
108
110
  }
109
111
  toSvg(bounds, options = {}) {
110
- const padding = Math.max(0, options.padding ?? 0);
111
- const left = bounds.x - padding;
112
- const top = bounds.y - padding;
113
- const width = Math.max(1, bounds.w + padding * 2);
114
- const height = Math.max(1, bounds.h + padding * 2);
112
+ const left = bounds.x;
113
+ const top = bounds.y;
114
+ const width = Math.max(1, bounds.w);
115
+ const height = Math.max(1, bounds.h);
115
116
  const background = options.background
116
117
  ? `<rect x="${number(left)}" y="${number(top)}" width="${number(width)}" height="${number(height)}" fill="${escapeXml(options.background)}" />`
117
118
  : "";
118
119
  return `<svg xmlns="http://www.w3.org/2000/svg" width="${number(width)}" height="${number(height)}" `
119
- + `viewBox="${number(left)} ${number(top)} ${number(width)} ${number(height)}">`
120
+ + `viewBox="${number(left)} ${number(top)} ${number(width)} ${number(height)}" `
121
+ + `font-family="${DEFAULT_SVG_FONT_ATTR}" font-weight="normal" fill="#000">`
120
122
  + `${background}${this.body.join("")}</svg>`;
121
123
  }
122
124
  }
@@ -1,8 +1,43 @@
1
- import type { GlyphMetrics, TextMeasurer, TextStyle } from "./types.js";
1
+ import type { GlyphMetrics, Painter, TextMeasurer, TextStyle } from "./types.js";
2
2
  /** 未指定字体族时,普通文本使用的字体栈 */
3
3
  export declare const DEFAULT_TEXT_FONT = "sans-serif";
4
4
  /** 简谱数字及同类谱面数字使用的字体栈 */
5
5
  export declare const JIANPU_NUMBER_FONT = "'Cascadia Mono', Consolas, 'Liberation Mono', 'Noto Sans Mono', monospace";
6
+ /** 同一行内具有相同样式掩码的连续纯文本片段 */
7
+ export interface RichTextRun {
8
+ text: string;
9
+ mask: number;
10
+ }
11
+ /** 向片段序列末尾追加字符;掩码相同时原地合并 */
12
+ export declare function appendRun(runs: RichTextRun[], text: string, mask: number): void;
13
+ /**
14
+ * 按状态机扫描 `**`、`*`、`_`、`^` 样式开关并逐字符回调 `(ch, mask, escaped)`。
15
+ * 遇到标记即异或切换对应样式位;若未闭合则后续文本(含跨行、跨音符槽)持续保持该样式。
16
+ */
17
+ export declare function scanMarkdown(source: string, onChar: (ch: string, mask: number, escaped: boolean) => void, escapable?: string): void;
18
+ /** 解析多行富文本:单遍跨行扫描样式状态,遇换行切分为每行的片段列表 */
19
+ export declare function parseRichText(source: string): RichTextRun[][];
20
+ /** 将前一片段与下一片段的样式掩码差值(`prevMask ^ nextMask`)还原为状态切换标记 */
21
+ export declare function markdownToggles(maskDiff: number): string;
22
+ /** 单行富文本的完整排版结果,供布局与绘制阶段直接复用 */
23
+ export interface PreparedRichLine extends GlyphMetrics {
24
+ runs: (RichTextRun & {
25
+ style: TextStyle;
26
+ w: number;
27
+ })[];
28
+ style: TextStyle;
29
+ }
30
+ /**
31
+ * 度量单行富文本(可传原始字符串或已解析片段)并汇总整行尺寸。
32
+ * 行高 h 与 baseline 始终取基准字号(非上标)的度量,避免行内含有上标时导致同行基线错位。
33
+ */
34
+ export declare function prepareRichLine(source: string | readonly RichTextRun[], baseStyle: TextStyle, measurer: TextMeasurer): PreparedRichLine;
35
+ /**
36
+ * 绘制已度量的单行富文本及其下划线装饰。
37
+ * 单片段时保留原 textAlign 直接绘制;多片段时从整行左缘起按段累加推进,
38
+ * 并将同一基线高度上相邻的下划线片段合并为单条连续线段。
39
+ */
40
+ export declare function paintRichLine(painter: Painter, line: PreparedRichLine, x: number, baselineY: number): void;
6
41
  /** Canvas 的 font 简写;测量与绘制必须用同一份,否则量到的不是将要画出的字形 */
7
42
  export declare function canvasFont(style: TextStyle): string;
8
43
  /** 默认文本测量保持确定性,不依赖浏览器和操作系统字体。 */
@@ -2,6 +2,140 @@
2
2
  export const DEFAULT_TEXT_FONT = "sans-serif";
3
3
  /** 简谱数字及同类谱面数字使用的字体栈 */
4
4
  export const JIANPU_NUMBER_FONT = "'Cascadia Mono', Consolas, 'Liberation Mono', 'Noto Sans Mono', monospace";
5
+ /** 富文本样式位掩码:四种内联样式正交叠加,通过异或翻转状态 */
6
+ const STYLE_BOLD = 1;
7
+ const STYLE_ITALIC = 2;
8
+ const STYLE_UNDERLINE = 4;
9
+ const STYLE_SUP = 8;
10
+ /** 向片段序列末尾追加字符;掩码相同时原地合并 */
11
+ export function appendRun(runs, text, mask) {
12
+ const last = runs.at(-1);
13
+ if (last && last.mask === mask)
14
+ last.text += text;
15
+ else
16
+ runs.push({ text, mask });
17
+ }
18
+ /**
19
+ * 按状态机扫描 `**`、`*`、`_`、`^` 样式开关并逐字符回调 `(ch, mask, escaped)`。
20
+ * 遇到标记即异或切换对应样式位;若未闭合则后续文本(含跨行、跨音符槽)持续保持该样式。
21
+ */
22
+ export function scanMarkdown(source, onChar, escapable = "\\*_^") {
23
+ let mask = 0;
24
+ for (let i = 0; i < source.length; i++) {
25
+ const ch = String.fromCodePoint(source.codePointAt(i));
26
+ i += ch.length - 1;
27
+ if (ch === "\\" && i + 1 < source.length && escapable.includes(source[i + 1])) {
28
+ onChar(source[++i], mask, true);
29
+ continue;
30
+ }
31
+ if (ch === "*") {
32
+ mask ^= source[i + 1] === "*" ? (i++, STYLE_BOLD) : STYLE_ITALIC;
33
+ continue;
34
+ }
35
+ if (ch === "_") {
36
+ mask ^= STYLE_UNDERLINE;
37
+ continue;
38
+ }
39
+ if (ch === "^") {
40
+ mask ^= STYLE_SUP;
41
+ continue;
42
+ }
43
+ onChar(ch, mask, false);
44
+ }
45
+ }
46
+ /** 解析多行富文本:单遍跨行扫描样式状态,遇换行切分为每行的片段列表 */
47
+ export function parseRichText(source) {
48
+ const lines = [[]];
49
+ scanMarkdown(source, (ch, mask) => {
50
+ if (ch === "\n")
51
+ lines.push([]);
52
+ else
53
+ appendRun(lines.at(-1), ch, mask);
54
+ });
55
+ return lines;
56
+ }
57
+ /** 将前一片段与下一片段的样式掩码差值(`prevMask ^ nextMask`)还原为状态切换标记 */
58
+ export function markdownToggles(maskDiff) {
59
+ return (maskDiff & STYLE_BOLD ? "**" : "")
60
+ + (maskDiff & STYLE_ITALIC ? "*" : "")
61
+ + (maskDiff & STYLE_UNDERLINE ? "_" : "")
62
+ + (maskDiff & STYLE_SUP ? "^" : "");
63
+ }
64
+ /** 上标相对基准字号的缩放比例与基线上移比例 */
65
+ const SUP_SCALE = 0.7;
66
+ const SUP_SHIFT = 0.35;
67
+ /**
68
+ * 度量单行富文本(可传原始字符串或已解析片段)并汇总整行尺寸。
69
+ * 行高 h 与 baseline 始终取基准字号(非上标)的度量,避免行内含有上标时导致同行基线错位。
70
+ */
71
+ export function prepareRichLine(source, baseStyle, measurer) {
72
+ const runs = [];
73
+ if (typeof source === "string")
74
+ scanMarkdown(source, (ch, mask) => appendRun(runs, ch, mask));
75
+ else
76
+ runs.push(...source);
77
+ let totalW = 0;
78
+ let lineMetrics;
79
+ const forceLeft = runs.length > 1 && baseStyle.textAlign && baseStyle.textAlign !== "left";
80
+ const preparedRuns = runs.map(run => {
81
+ const style = !forceLeft && (run.mask & (STYLE_BOLD | STYLE_ITALIC | STYLE_SUP)) === 0
82
+ ? baseStyle
83
+ : {
84
+ ...baseStyle,
85
+ ...(forceLeft ? { textAlign: "left" } : {}),
86
+ ...(run.mask & STYLE_SUP ? { fontSize: baseStyle.fontSize * SUP_SCALE } : {}),
87
+ ...(run.mask & STYLE_BOLD ? { fontWeight: "bold" } : {}),
88
+ ...(run.mask & STYLE_ITALIC ? { fontStyle: "italic" } : {}),
89
+ };
90
+ const metrics = measurer.measureText(run.text, style);
91
+ if ((run.mask & STYLE_SUP) === 0)
92
+ lineMetrics ??= metrics;
93
+ totalW += metrics.w;
94
+ return { ...run, style, w: metrics.w };
95
+ });
96
+ lineMetrics ??= measurer.measureText("", baseStyle);
97
+ return {
98
+ runs: preparedRuns,
99
+ style: baseStyle,
100
+ w: runs.length ? totalW : lineMetrics.w,
101
+ h: lineMetrics.h,
102
+ baseline: lineMetrics.baseline,
103
+ };
104
+ }
105
+ /**
106
+ * 绘制已度量的单行富文本及其下划线装饰。
107
+ * 单片段时保留原 textAlign 直接绘制;多片段时从整行左缘起按段累加推进,
108
+ * 并将同一基线高度上相邻的下划线片段合并为单条连续线段。
109
+ */
110
+ export function paintRichLine(painter, line, x, baselineY) {
111
+ const { runs, style: baseStyle, w } = line;
112
+ const { textAlign } = baseStyle;
113
+ const leftX = textAlign === "center" ? x - w / 2 : textAlign === "right" ? x - w : x;
114
+ const runY = (mask) => mask & STYLE_SUP ? baselineY - baseStyle.fontSize * SUP_SHIFT : baselineY;
115
+ let cursorX = leftX;
116
+ for (const run of runs) {
117
+ painter.drawText(run.text, runs.length === 1 ? x : cursorX, runY(run.mask), run.style);
118
+ cursorX += run.w;
119
+ }
120
+ // Canvas 2D 原生不支持 text-decoration,统一用 drawLine 绘制下划线以保证 SVG/Canvas 一致
121
+ for (let i = 0, cursor = leftX; i < runs.length;) {
122
+ const { mask, style, w: runW } = runs[i++];
123
+ if (!(mask & STYLE_UNDERLINE)) {
124
+ cursor += runW;
125
+ continue;
126
+ }
127
+ let endX = cursor + runW;
128
+ while (i < runs.length && (runs[i].mask & (STYLE_UNDERLINE | STYLE_SUP)) === (mask & (STYLE_UNDERLINE | STYLE_SUP))) {
129
+ endX += runs[i++].w;
130
+ }
131
+ const y = runY(mask) + style.fontSize * 0.12;
132
+ painter.drawLine(cursor, y, endX, y, {
133
+ stroke: baseStyle.fill ?? "#000",
134
+ strokeWidth: Math.max(1, style.fontSize * 0.06),
135
+ });
136
+ cursor = endX;
137
+ }
138
+ }
5
139
  /** Canvas 的 font 简写;测量与绘制必须用同一份,否则量到的不是将要画出的字形 */
6
140
  export function canvasFont(style) {
7
141
  const slant = style.fontStyle ? `${style.fontStyle} ` : "";
@@ -18,7 +18,6 @@ export interface PaintStyle {
18
18
  fill?: string;
19
19
  stroke?: string;
20
20
  strokeWidth?: number;
21
- opacity?: number;
22
21
  }
23
22
  /**
24
23
  * 任意文本的排版样式
@@ -33,7 +32,6 @@ export interface TextStyle {
33
32
  fontStyle?: "normal" | "italic" | "oblique";
34
33
  textAlign?: "left" | "center" | "right";
35
34
  fill?: string;
36
- opacity?: number;
37
35
  }
38
36
  /**
39
37
  * 通用路径命令
package/grammar.md CHANGED
@@ -25,10 +25,10 @@ sidebar:
25
25
  | [关系型:连线、反复房子、八度号、渐强渐弱](#关系型) | `@beam` `@tie` `@gliss` `@volta` `@ottava` `@dyn` |
26
26
 
27
27
  ## 2. 基本类型
28
- - **字符串**:双引号 `"..."`,无歧义时可省;含分隔符须加引号,如 `@note("b6,")`。
28
+ - **字符串**:双引号 `"..."`,无歧义时可省;含分隔符须加引号,如 `@note("b6,")`。有无引号共享同一套转义规则:
29
29
  - 可跨行;引号须配对,孤立 `"` 按普通字符处理。
30
- - 引号内仅支持 JSON 风格的 Unicode 转义:`\uXXXX` 为恰好四位十六进制码元,例如 `"\uE050"`;补充平面字符用 `"\uD834\uDD1E"` 这样的代理对表示。
31
- - 其他 `\X` 仍转为 `X`,如 `\"`、`\\`;不新增 `\n`、`\t` 等语义转义,换行和制表符直接输入。转义只解码一次,`"\\uE050"` 显示字面文本 `\uE050`。
30
+ - 支持 JSON 风格的 Unicode 转义:`\uXXXX` 为恰好四位十六进制码元,例如 `"\uE050"`;补充平面字符用 `"\uD834\uDD1E"` 这样的代理对表示。
31
+ - 用 `\"`、`\\` 输入双引号和反斜杠,用 `\*`、`\_`、`\^`、`\-`、`\@`、`\{`、`\}` 显示符号本身;不支持 `\n`、`\t`,换行和制表符直接输入。转义只处理一次,`"\\uE050"` 显示 `\uE050`。
32
32
  - **布尔值**:`true` / `false`。
33
33
  - **数字**:有限整数或小数。
34
34
  - **长度**:数字加 `px`(默认)或 `em`(相对当前字号)。
@@ -58,10 +58,11 @@ sidebar:
58
58
 
59
59
  ### 页面与谱头
60
60
 
61
- - `@page(width, height, top, bottom, left, right, gap, numbering, barNumbering, font, fillRatio)`:页面布局。
61
+ - `@page(width, height, top, bottom, left, right, gap, minGap, numbering, barNumbering, font, fillRatio)`:页面布局。
62
62
  - 仅顶层首次声明有效,其余警告并忽略;`em` 按声明时字号计算。
63
- - 默认:宽 `794px`、高 `0px`、上 `45px`、下 `48px`、左右 `40px`、行距 `1em`、`fillRatio=0.5`。
64
- - `height <= 0` 不自动分页,仍可手动换页;正值按整行分页,单行超高报错。自动封页均分剩余高度,末页保持最小行距。
63
+ - 默认:宽 `794px`、高 `0px`、上 `45px`、下 `48px`、左右 `40px`、行距 `1em`、最小行距 `0.5em`、`fillRatio=0.5`。
64
+ - `gap` 为上一行下沿到下一行主旋律上沿(基线减半字高)的标准行距;顶部外扩图形优先借用行距,但与上一行下沿至少保留 `minGap`(不超过 `gap`)。
65
+ - `height <= 0` 不自动分页,仍可手动换页;正值按整行分页,单行超高报错。自动封页均分剩余高度,末页保持自然行距。
65
66
  - `fillRatio`:自然宽度达到版心宽度的该比例时撑满;负值始终撑满,`>= 1` 不自动拉开。
66
67
  - `numbering`:页码格式,如 `"1/1"`,默认 `""` 不显示;`font`:页码/小节号字体,默认 `""` 沿用当前字体。
67
68
  - `barNumbering`:从第二行起每行行首的小节号格式,如 `"1"`、`"(1)"`,默认 `""` 不显示。
@@ -107,6 +108,7 @@ sidebar:
107
108
  - `@text(字符串, size=1em, lineheight=1.25, align=left, font="")`:文本标记。
108
109
  - 简写 `"渐强"`;可直接换行,`lineheight` 为自身字号的倍数。
109
110
  - `align=left/center/right`:左对齐 / 居中 / 右对齐,锚点为首行首字中心 / 整块中心 / 首行末字中心。
111
+ - 文字样式:`*斜体*`、`**粗体**`、`_下划线_`、`^上标^`(再次出现即关闭,不关闭则一直生效;写 `\*`、`\_`、`\^` 显示原字符。谱头、歌词与各类名称同样支持)。
110
112
  - `font` 指定字体,空值沿用当前字体。宿主加载音乐字体后可写 `@text("\uE050", font="Bravura")`;Unicode 转义只决定字符,不会自动选择或加载字体。
111
113
 
112
114
  - `@symbol(name, size=0.5em)` / `@$(...)`:乐谱符号,简写 `$name`。
@@ -136,14 +138,16 @@ sidebar:
136
138
  - 简写:`N(声部名): 内容`、`L(歌词行名): 歌词`,名称均可省。
137
139
  - `N:` 到同层换行、下一条 `N/L/V` 或块末结束;跨源码行须用 `{}` 包裹。
138
140
  - `name` 后每个字符串参数或 `L:` 对应一行歌词;命名参数名为歌词行名。`L:` 属于前一声部,无所属声部则报错。
139
- - 按空格、连字符和非 ASCII 字符分词;`{你好}` 合为一个音符槽,显示时去掉花括号。
141
+ - 按空格、连字符和非 ASCII 字符分词;`{你好}` 合为一个音符槽,显示时去掉花括号。`*`、`**`、`_`、`^` 样式可跨多个音符(如 `L: *春眠不觉晓*`),标记本身不占音符位置。
140
142
 
141
- - `@voices(...声部, connect="[-]")` / `@vs(...)`:声部按书写顺序自上而下并行。
143
+ - `@voices(...声部, connect="[-]", gap=0.75em, minGap=0.35em)` / `@vs(...)`:声部按书写顺序自上而下并行。
142
144
  - 位置参数须为 `@voice`,各占独立音轨;连续 `N:` 及所属 `L:` 自动成组,空行、作用域结束或普通非成员内容结束组。
145
+ - **gap / minGap(仅命名传参)**:`gap` 为上一声部下沿到下一声部主旋律上沿(基线减半字高)的标准间距(默认 `0.75em`);顶部外扩记号优先借用这段间距,但与上一声部下沿至少保留 `minGap`(默认 `0.35em`,不超过 `gap`);两者均须为非负有限长度。同一宿主下声部数相同的组始终复用音轨,间距不一致时取较大值并产生警告。
143
146
  - **connect(仅命名传参)**:只改外观。`"[-]"` 全组带钩括线,`"{-}"` 全组大括号,`""` 或纯空白字符串仅公共细连谱线;单声部组不画连接。
144
147
  - 范围可组合,如 `"[1-4]{5-7}"`;从 1 编号,只数直接声部,含两端,省略端点取首 / 末声部。
145
148
  - 端点为正整数,不可反向或越界;单成员可写 `[3]` 或 `{3}`,分别等价于 `[3-3]` 或 `{3-3}`;可重叠,不自动嵌套或避让。
146
- - **连接声明**:`V{}:` 大括号、`V[]:` 带钩括线、`V|:` 仅细线;不限行首,后须有 `N:`,可同行或换行。
149
+ - 范围后可用 `:名称` 指定分组总名称(如 `"{1-2:钢琴}[3-4:合唱]"`、`"{-:钢琴}"`、`"{3:独奏}"`),含 `[]{}:`、引号、换行或首尾空白时用双引号包裹;显示在对应连谱号垂直中心左侧,组内有声部名或歌词行名时自动向左错开,重叠连谱号各自按自身中心绘制、不相互避让。
150
+ - **连接声明**:`V{}:`(或 `V{组名}:`)大括号、`V[]:`(或 `V[组名]:`)带钩括线、`V|:` 仅细线;不限行首,后须有 `N:`,可同行或换行。
147
151
  - 到下一 `V` 或组末结束,不拆分并行组;相邻同形声明仍独立,`L:` 不跨声明寻找声部。
148
152
  - **默认值**:省略 `connect` 时取 `@set` 预设或内置 `"[-]"`;显式参数或本组 `V` 声明优先。任一 `V` 完整替换本组预设,未声明部分仅细线,不影响后续组。
149
153
 
@@ -153,11 +157,11 @@ sidebar:
153
157
  - 各分支同时开始、独立计时,可含旋律;首分支留在宿主轨,其余使用分支轨并依次向上排列。
154
158
  - 与 `@up` 不同,整体不能作为单个连音线端点。
155
159
 
156
- - `@up(...)`:和弦,简写 `^`,自下而上排列。
157
- - 折叠为一个事件,时值、对齐和减时线取首成员(宿主);每个参数须恰好产生一个可见对象。
160
+ - `@up(..., gap=0.12em)`:和弦,简写 `^`,自下而上排列。
161
+ - 折叠为一个事件,时值、对齐和减时线取首成员(宿主);每个参数须恰好产生一个可见对象;仅命名参数 `gap` 指定相邻成员间距(默认 `0.12em`,须为非负有限长度)。
158
162
  - 至少一个成员可标注时,整体才可标注;`{1@x ^ 3}` 定位成员,`{1 ^ 3}@x` 定位整体,后者的连音线从最上方成员起跳。
159
163
 
160
- - `@down(...)`:向下堆叠,简写 `_`,与 `^` 共用宿主及折叠规则。
164
+ - `@down(..., gap=0.12em)`:向下堆叠,简写 `_`,与 `^` 共用宿主及折叠规则。
161
165
  - `1^2_3^4_5`:宿主 `1`,上方 `2 4`,下方 `3 5`;横向占位取宿主,附属成员撑开行高。
162
166
  - `1 ^ $accent`、`1 _ $accent` 均修饰宿主,`$accent ^ 1` 无效。
163
167
  - `1 ^ $p`、`1 _ $p` 从宿主生效;`$p ^ 1` 的宿主力度不变,之后的音才改变。
@@ -208,7 +212,6 @@ sidebar:
208
212
  - 两端音高均为有限数值时,同轨按音高绘制,并在起点(或所标增时线及其后续延音)区间内按半音阶离散步进至终点音高;无法比较音高时发出 `W_GLISS_NO_PITCH` 警告并画水平箭头,每条关系只警告一次。
209
213
  - 从被标记对象右侧连接到终点左侧,避让附点与升降号,不自动移至最后一根增时线。
210
214
  - 如 `1 - -@a 5@b @gliss(a,b)`;增时线缺少自身音高时,由 gliss 查找前方音高,并从该增时线起点开始半音步进。
211
- - 同一谱面行内可跨轨;跨轨始终连接实际端点,斜率不代表音高方向。不支持跨行、跨页、同一倚音组内部连接或复杂绕障。
212
215
  - `@beam(...端点标签)`:手工连梁,不受 `div.autobeam` 影响。
213
216
  - 端点须按时间顺序指向同轨、同谱面行的相邻可见元素,可跨零时长小节线;和弦仅宿主参与。
214
217
  - `@tie(...端点标签, height=0)`:连音线 / 延音线。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jpfun",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "A functional numbered musical notation typesetting engine",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1 +0,0 @@
1
- export {};
package/dist/iife/core.js DELETED
@@ -1,3 +0,0 @@
1
- import * as core from "../core.js";
2
- const target = globalThis;
3
- Object.assign(target.jpfun ??= {}, core);
@@ -1 +0,0 @@
1
- export {};
@@ -1,15 +0,0 @@
1
- import { convertMidiJsonToJpFun, } from "../converter/midi/convert.js";
2
- const target = globalThis;
3
- const api = target.jpfun ??= {};
4
- Object.assign(api, {
5
- midiJsonToJpFun(input, options = {}) {
6
- const layoutCapability = (options.barsPerLine ?? 0) <= 0
7
- ? {
8
- compileScore: api.compileScore,
9
- anchorKey: api.ANCHOR_KEY,
10
- page: api.DEFAULT_PAGE_CONFIG,
11
- }
12
- : undefined;
13
- return convertMidiJsonToJpFun(input, options, layoutCapability);
14
- },
15
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import { musicXmlToJpFun } from "../converter/musicxml/index.js";
2
- const target = globalThis;
3
- Object.assign(target.jpfun ??= {}, { musicXmlToJpFun });
@@ -1 +0,0 @@
1
- export {};
package/dist/iife/full.js DELETED
@@ -1,3 +0,0 @@
1
- import * as full from "../index.js";
2
- const target = globalThis;
3
- Object.assign(target.jpfun ??= {}, full);