@dsebastien/music-notation 0.1.3 → 0.2.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.
@@ -19,6 +19,12 @@ function svgText(doc, text, attrs = {}) {
19
19
  el.textContent = text;
20
20
  return el;
21
21
  }
22
+ function svgLabel(doc, text, [x, y], cls, anchor = "middle") {
23
+ return svgText(doc, text, { class: cls, x, y: y + 4, "text-anchor": anchor });
24
+ }
25
+ function svgLine(doc, [x1, y1], [x2, y2], cls) {
26
+ return svg(doc, "line", { class: cls, x1, y1, x2, y2 });
27
+ }
22
28
  function html(doc, tag, cls, text) {
23
29
  const el = doc.createElement(tag);
24
30
  if (cls)
@@ -38,6 +44,75 @@ function svgRoot(doc, width, height, cls, label) {
38
44
  });
39
45
  }
40
46
 
47
+ // src/render/layout.ts
48
+ function parseOrientation(text) {
49
+ const value = text.trim().toLowerCase();
50
+ if (value === "vertical" || value === "horizontal")
51
+ return value;
52
+ throw new Error(`Unknown orientation "${text}": use vertical or horizontal`);
53
+ }
54
+ function parseHand(text) {
55
+ const value = text.trim().toLowerCase();
56
+ if (value === "left" || value === "right")
57
+ return value;
58
+ throw new Error(`Unknown hand "${text}": use left or right`);
59
+ }
60
+ function layoutDefaults(value) {
61
+ const result = {};
62
+ if (!value || typeof value !== "object")
63
+ return result;
64
+ const word = (v) => typeof v === "string" ? v.trim().toLowerCase() : undefined;
65
+ try {
66
+ const { orientation, hand } = value;
67
+ const h = word(hand);
68
+ if (h === "left" || h === "right")
69
+ result.hand = h;
70
+ if (orientation && typeof orientation === "object") {
71
+ for (const language of ["chord", "keyboard", "fretboard"]) {
72
+ const o = word(orientation[language]);
73
+ if (o === "vertical" || o === "horizontal")
74
+ (result.orientation ??= {})[language] = o;
75
+ }
76
+ }
77
+ } catch {}
78
+ return result;
79
+ }
80
+ function titleWidth(title) {
81
+ if (!title)
82
+ return 0;
83
+ let width = 8;
84
+ for (const ch of title) {
85
+ width += ch.codePointAt(0) > 591 ? 15 : /[A-Z0-9#♯♭]/.test(ch) ? 10.5 : 8;
86
+ }
87
+ return Math.ceil(width);
88
+ }
89
+ function neckFrame(n) {
90
+ const vertical = n.orientation === "vertical";
91
+ const left = n.hand === "left";
92
+ const content = vertical ? n.low + n.across + n.high : n.before + n.along + n.after;
93
+ const shift = n.minWidth && n.minWidth > content ? (n.minWidth - content) / 2 : 0;
94
+ const height = n.top + (vertical ? n.before + n.along + n.after : n.high + n.across + n.low);
95
+ const at = (across, along) => {
96
+ if (vertical) {
97
+ const x = left ? n.high + (n.across - across) : n.low + across;
98
+ return [shift + x, n.top + n.before + along];
99
+ }
100
+ const x = left ? content - n.before - along : n.before + along;
101
+ return [shift + x, n.top + n.high + (n.across - across)];
102
+ };
103
+ const box = (a1, l1, a2, l2) => {
104
+ const [x1, y1] = at(a1, l1);
105
+ const [x2, y2] = at(a2, l2);
106
+ return {
107
+ x: Math.min(x1, x2),
108
+ y: Math.min(y1, y2),
109
+ width: Math.abs(x2 - x1),
110
+ height: Math.abs(y2 - y1)
111
+ };
112
+ };
113
+ return { width: content + 2 * shift, height, at, box };
114
+ }
115
+
41
116
  // src/chord/guitar.ts
42
117
  var STANDARD_TUNING = [40, 45, 50, 55, 59, 64];
43
118
  function parseFrets(text) {
@@ -82,94 +157,80 @@ function barresOf(voicing) {
82
157
  }
83
158
  var SPACING = 18;
84
159
  var ROW = 20;
85
- var LEFT = 30;
86
- var TITLE = 22;
87
160
  var MARKERS = 16;
161
+ var BASE = 30;
162
+ var TITLE = 22;
88
163
  function renderGuitarDiagram(doc, voicing, options = {}) {
89
164
  const strings = voicing.frets.length;
90
165
  const base = voicing.baseFret ?? baseFretOf(voicing.frets);
91
166
  const fretted = voicing.frets.filter((f) => f !== null && f > 0);
92
167
  const rows = Math.max(4, fretted.length ? Math.max(...fretted) - base + 1 : 0);
93
- const top = (options.title ? TITLE : 4) + MARKERS;
94
- const width = LEFT + (strings - 1) * SPACING + 14;
95
- const height = top + rows * ROW + 10;
96
- const x = (string) => LEFT + string * SPACING;
97
- const y = (row) => top + row * ROW;
168
+ const orientation = options.orientation ?? "vertical";
169
+ const vertical = orientation === "vertical";
170
+ const frame = neckFrame({
171
+ orientation,
172
+ hand: options.hand ?? "right",
173
+ across: (strings - 1) * SPACING,
174
+ along: rows * ROW,
175
+ before: MARKERS,
176
+ after: 10,
177
+ low: vertical || base > 1 ? BASE : 10,
178
+ high: vertical ? 14 : 10,
179
+ top: options.title ? TITLE : 4,
180
+ minWidth: titleWidth(options.title)
181
+ });
182
+ const across = (string) => string * SPACING;
183
+ const along = (row) => row * ROW;
184
+ const at = (string, row) => frame.at(across(string), along(row));
98
185
  const label = options.title ? `${options.title} chord diagram` : "Chord diagram";
99
- const root = svgRoot(doc, width, height, "mn-guitar-chord", label);
186
+ const root = svgRoot(doc, frame.width, frame.height, "mn-guitar-chord", label);
100
187
  if (options.title) {
101
188
  root.appendChild(svgText(doc, options.title, {
102
189
  class: "mn-title",
103
- x: width / 2,
190
+ x: frame.width / 2,
104
191
  y: 15,
105
192
  "text-anchor": "middle"
106
193
  }));
107
194
  }
108
195
  for (let row = 0;row <= rows; row++) {
109
- root.appendChild(svg(doc, "line", {
110
- class: row === 0 && base === 1 ? "mn-nut" : "mn-fret",
111
- x1: x(0),
112
- x2: x(strings - 1),
113
- y1: y(row),
114
- y2: y(row)
115
- }));
196
+ const cls = row === 0 && base === 1 ? "mn-nut" : "mn-fret";
197
+ root.appendChild(svgLine(doc, at(0, row), at(strings - 1, row), cls));
116
198
  }
117
199
  for (let s = 0;s < strings; s++) {
118
- root.appendChild(svg(doc, "line", { class: "mn-string", x1: x(s), x2: x(s), y1: y(0), y2: y(rows) }));
200
+ root.appendChild(svgLine(doc, at(s, 0), at(s, rows), "mn-string"));
119
201
  }
120
202
  if (base > 1) {
121
- root.appendChild(svgText(doc, `${base}fr`, {
122
- class: "mn-base-fret",
123
- x: LEFT - 10,
124
- y: y(0.5) + 4,
125
- "text-anchor": "end"
126
- }));
203
+ const label = `${base}fr`;
204
+ root.appendChild(vertical ? svgLabel(doc, label, frame.at(-10, along(0.5)), "mn-base-fret", options.hand === "left" ? "start" : "end") : svgLabel(doc, label, frame.at(-16, along(0.5)), "mn-base-fret"));
127
205
  }
128
206
  for (const barre of barresOf(voicing)) {
129
- const cy = y(barre.fret - base + 0.5);
207
+ const middle = along(barre.fret - base + 0.5);
130
208
  root.appendChild(svg(doc, "rect", {
131
209
  class: "mn-barre",
132
- x: x(barre.from) - 7,
133
- y: cy - 7,
134
- width: x(barre.to) - x(barre.from) + 14,
135
- height: 14,
210
+ ...frame.box(across(barre.from) - 7, middle - 7, across(barre.to) + 7, middle + 7),
136
211
  rx: 7
137
212
  }));
138
213
  }
139
214
  const tuning = options.tuning ?? (strings === 6 ? STANDARD_TUNING : undefined);
140
215
  voicing.frets.forEach((fret, s) => {
141
- const cx = x(s);
216
+ const mark = frame.at(across(s), -8);
142
217
  if (fret === null) {
143
- root.appendChild(svgText(doc, "×", {
144
- class: "mn-muted",
145
- x: cx,
146
- y: top - 4,
147
- "text-anchor": "middle"
148
- }));
218
+ root.appendChild(svgLabel(doc, "×", mark, "mn-muted"));
149
219
  return;
150
220
  }
151
221
  const open = tuning?.[s];
152
222
  const isRoot = options.root !== undefined && options.root !== null && open !== undefined && (open + fret) % 12 === options.root;
153
223
  if (fret === 0) {
154
- root.appendChild(svg(doc, "circle", {
155
- class: `mn-open${isRoot ? " mn-root" : ""}`,
156
- cx,
157
- cy: top - 8,
158
- r: 4
159
- }));
224
+ const [cx, cy] = mark;
225
+ root.appendChild(svg(doc, "circle", { class: `mn-open${isRoot ? " mn-root" : ""}`, cx, cy, r: 4 }));
160
226
  return;
161
227
  }
162
- const cy = y(fret - base + 0.5);
228
+ const center = at(s, fret - base + 0.5);
229
+ const [cx, cy] = center;
163
230
  root.appendChild(svg(doc, "circle", { class: `mn-dot${isRoot ? " mn-root" : ""}`, cx, cy, r: 7 }));
164
231
  const finger = voicing.fingers[s];
165
- if (finger) {
166
- root.appendChild(svgText(doc, finger, {
167
- class: "mn-dot-text",
168
- x: cx,
169
- y: cy + 4,
170
- "text-anchor": "middle"
171
- }));
172
- }
232
+ if (finger)
233
+ root.appendChild(svgLabel(doc, finger, center, "mn-dot-text"));
173
234
  });
174
235
  return root;
175
236
  }
@@ -572,9 +633,8 @@ function listNoteSet(text) {
572
633
  var STRING_GAP = 20;
573
634
  var FRET_W = 44;
574
635
  var OPEN_W = 30;
575
- var LEFT2 = 24;
636
+ var NUMBERS = 30;
576
637
  var TITLE2 = 22;
577
- var BOTTOM = 22;
578
638
  var SINGLE_INLAYS = new Set([3, 5, 7, 9, 15, 17, 19, 21]);
579
639
  function renderFretboard(doc, set, options = {}) {
580
640
  const tuning = options.tuning ?? STANDARD_TUNING;
@@ -582,96 +642,68 @@ function renderFretboard(doc, set, options = {}) {
582
642
  const first = Math.max(from, 1);
583
643
  const strings = tuning.length;
584
644
  const label = options.label ?? "notes";
585
- const top = (options.title ? TITLE2 : 0) + 12;
645
+ const orientation = options.orientation ?? "horizontal";
646
+ const vertical = orientation === "vertical";
586
647
  const openW = from === 0 ? OPEN_W : 0;
587
- const nutX = LEFT2 + openW;
588
- const width = nutX + (to - first + 1) * FRET_W + 8;
589
- const height = top + (strings - 1) * STRING_GAP + BOTTOM + 8;
590
- const y = (row) => top + row * STRING_GAP;
591
- const wireX = (fret) => nutX + (fret - first + 1) * FRET_W;
592
- const centerX = (fret) => fret === 0 ? LEFT2 + OPEN_W / 2 : wireX(fret) - FRET_W / 2;
593
- const root = svgRoot(doc, width, height, "mn-fretboard", options.title ?? `Fretboard: ${set.title}`);
648
+ const acrossLength = (strings - 1) * STRING_GAP;
649
+ const frame = neckFrame({
650
+ orientation,
651
+ hand: options.hand ?? "right",
652
+ across: acrossLength,
653
+ along: openW + (to - first + 1) * FRET_W,
654
+ before: vertical ? 20 : 24,
655
+ after: 8,
656
+ low: NUMBERS,
657
+ high: 12,
658
+ top: options.title ? TITLE2 : 0,
659
+ minWidth: titleWidth(options.title)
660
+ });
661
+ const across = (string) => string * STRING_GAP;
662
+ const wire = (fret) => openW + (fret - first + 1) * FRET_W;
663
+ const center = (fret) => fret === 0 ? OPEN_W / 2 : wire(fret) - FRET_W / 2;
664
+ const root = svgRoot(doc, frame.width, frame.height, "mn-fretboard", options.title ?? `Fretboard: ${set.title}`);
594
665
  if (options.title) {
595
666
  root.appendChild(svgText(doc, options.title, {
596
667
  class: "mn-title",
597
- x: width / 2,
668
+ x: frame.width / 2,
598
669
  y: 15,
599
670
  "text-anchor": "middle"
600
671
  }));
601
672
  }
602
- const middle = y((strings - 1) / 2);
673
+ const inlay = (a, fret) => {
674
+ const [cx, cy] = frame.at(a, center(fret));
675
+ root.appendChild(svg(doc, "circle", { class: "mn-inlay", cx, cy, r: 4 }));
676
+ };
677
+ const pair = (strings > 4 ? 1.5 : 0.5) * STRING_GAP;
603
678
  for (let fret = first;fret <= to; fret++) {
604
- const cx = wireX(fret) - FRET_W / 2;
605
679
  if (SINGLE_INLAYS.has(fret))
606
- root.appendChild(svg(doc, "circle", { class: "mn-inlay", cx, cy: middle, r: 4 }));
680
+ inlay(acrossLength / 2, fret);
607
681
  if (fret % 12 === 0) {
608
- root.appendChild(svg(doc, "circle", { class: "mn-inlay", cx, cy: y(strings > 3 ? 1.5 : 0.5), r: 4 }));
609
- root.appendChild(svg(doc, "circle", {
610
- class: "mn-inlay",
611
- cx,
612
- cy: y(strings > 3 ? strings - 2.5 : strings - 1.5),
613
- r: 4
614
- }));
682
+ inlay(acrossLength - pair, fret);
683
+ inlay(pair, fret);
615
684
  }
616
- root.appendChild(svgText(doc, String(fret), {
617
- class: "mn-fret-number",
618
- x: cx,
619
- y: y(strings - 1) + BOTTOM,
620
- "text-anchor": "middle"
621
- }));
685
+ root.appendChild(svgLabel(doc, String(fret), frame.at(-18, center(fret)), "mn-fret-number"));
622
686
  }
623
- root.appendChild(svg(doc, "line", {
624
- class: first === 1 ? "mn-nut" : "mn-fret",
625
- x1: nutX,
626
- x2: nutX,
627
- y1: y(0),
628
- y2: y(strings - 1)
629
- }));
687
+ root.appendChild(svgLine(doc, frame.at(acrossLength, openW), frame.at(0, openW), first === 1 ? "mn-nut" : "mn-fret"));
630
688
  for (let fret = first;fret <= to; fret++) {
631
- root.appendChild(svg(doc, "line", {
632
- class: "mn-fret",
633
- x1: wireX(fret),
634
- x2: wireX(fret),
635
- y1: y(0),
636
- y2: y(strings - 1)
637
- }));
689
+ root.appendChild(svgLine(doc, frame.at(acrossLength, wire(fret)), frame.at(0, wire(fret)), "mn-fret"));
638
690
  }
639
- for (let row = 0;row < strings; row++) {
640
- const open = tuning[strings - 1 - row];
641
- root.appendChild(svg(doc, "line", {
642
- class: "mn-string",
643
- x1: nutX - openW,
644
- x2: wireX(to),
645
- y1: y(row),
646
- y2: y(row)
647
- }));
648
- root.appendChild(svgText(doc, nameOf(open, set.preferFlats), {
649
- class: "mn-string-name",
650
- x: LEFT2 - 8,
651
- y: y(row) + 4,
652
- "text-anchor": "end"
653
- }));
691
+ for (let s = strings - 1;s >= 0; s--) {
692
+ const open = tuning[s];
693
+ root.appendChild(svgLine(doc, frame.at(across(s), 0), frame.at(across(s), wire(to)), "mn-string"));
694
+ const name = nameOf(open, set.preferFlats);
695
+ root.appendChild(vertical ? svgLabel(doc, name, frame.at(across(s), -10), "mn-string-name") : svgLabel(doc, name, frame.at(across(s), -8), "mn-string-name", options.hand === "left" ? "start" : "end"));
654
696
  for (let fret = from;fret <= to; fret++) {
655
697
  const member = memberOf(set, open + fret);
656
698
  if (!member)
657
699
  continue;
658
- const cx = centerX(fret);
659
700
  const isRoot = set.root === member.chroma;
660
- root.appendChild(svg(doc, "circle", {
661
- class: `mn-dot${isRoot ? " mn-root" : ""}`,
662
- cx,
663
- cy: y(row),
664
- r: 8
665
- }));
701
+ const point = frame.at(across(s), center(fret));
702
+ const [cx, cy] = point;
703
+ root.appendChild(svg(doc, "circle", { class: `mn-dot${isRoot ? " mn-root" : ""}`, cx, cy, r: 8 }));
666
704
  const text = labelOf(member, label);
667
- if (text) {
668
- root.appendChild(svgText(doc, text, {
669
- class: "mn-dot-text",
670
- x: cx,
671
- y: y(row) + 4,
672
- "text-anchor": "middle"
673
- }));
674
- }
705
+ if (text)
706
+ root.appendChild(svgLabel(doc, text, point, "mn-dot-text"));
675
707
  }
676
708
  }
677
709
  return root;
@@ -752,9 +784,24 @@ function renderKeyboard(doc, set, options = {}) {
752
784
  for (let midi = from;midi <= to; midi++)
753
785
  if (!isBlack(midi))
754
786
  whites.push(midi);
787
+ const vertical = options.orientation === "vertical";
755
788
  const top = options.title ? TITLE3 : 0;
756
- const width = whites.length * WHITE_W + 2;
757
- const height = top + WHITE_H + 2;
789
+ const length = whites.length * WHITE_W;
790
+ const content = (vertical ? WHITE_H : length) + 2;
791
+ const width = Math.max(content, titleWidth(options.title));
792
+ const shift = (width - content) / 2;
793
+ const height = top + (vertical ? length : WHITE_H) + 2;
794
+ const at = (along, depth) => vertical ? [shift + 1 + depth, top + 1 + length - along] : [shift + 1 + along, top + 1 + depth];
795
+ const key = (along, size, depth) => {
796
+ const [x1, y1] = at(along, 0);
797
+ const [x2, y2] = at(along + size, depth);
798
+ return {
799
+ x: Math.min(x1, x2),
800
+ y: Math.min(y1, y2),
801
+ width: Math.abs(x2 - x1),
802
+ height: Math.abs(y2 - y1)
803
+ };
804
+ };
758
805
  const label = options.label ?? "notes";
759
806
  const root = svgRoot(doc, width, height, "mn-keyboard", options.title ?? `Keyboard: ${set.title}`);
760
807
  if (options.title) {
@@ -772,36 +819,37 @@ function renderKeyboard(doc, set, options = {}) {
772
819
  classes.push("mn-on");
773
820
  if (member && set.root === member.chroma)
774
821
  classes.push("mn-root");
775
- return { member, cls: classes.join(" ") };
822
+ return classes.join(" ");
776
823
  };
777
- const caption = (midi, x, y, black) => {
824
+ const caption = (midi, point, black) => {
778
825
  const member = memberOf(set, midi);
779
826
  if (!member)
780
827
  return;
781
828
  const text = labelOf(member, label, options.fingers?.get(midi));
782
829
  if (!text)
783
830
  return;
784
- root.appendChild(svgText(doc, text, {
785
- class: black ? "mn-key-label mn-key-label-black" : "mn-key-label",
786
- x,
787
- y,
788
- "text-anchor": "middle"
789
- }));
831
+ root.appendChild(svgLabel(doc, text, point, black ? "mn-key-label mn-key-label-black" : "mn-key-label"));
790
832
  };
791
833
  whites.forEach((midi, i) => {
792
- const x = 1 + i * WHITE_W;
793
- const { cls } = keyClass(midi, false);
794
- root.appendChild(svg(doc, "rect", { class: cls, x, y: top + 1, width: WHITE_W, height: WHITE_H, rx: 2 }));
795
- caption(midi, x + WHITE_W / 2, top + WHITE_H - 8, false);
834
+ const along = i * WHITE_W;
835
+ root.appendChild(svg(doc, "rect", {
836
+ class: keyClass(midi, false),
837
+ ...key(along, WHITE_W, WHITE_H),
838
+ rx: 2
839
+ }));
840
+ caption(midi, at(along + WHITE_W / 2, WHITE_H - 13), false);
796
841
  });
797
842
  whites.forEach((midi, i) => {
798
843
  const black = midi + 1;
799
844
  if (black > to || !isBlack(black))
800
845
  return;
801
- const x = 1 + (i + 1) * WHITE_W - BLACK_W / 2;
802
- const { cls } = keyClass(black, true);
803
- root.appendChild(svg(doc, "rect", { class: cls, x, y: top + 1, width: BLACK_W, height: BLACK_H, rx: 2 }));
804
- caption(black, x + BLACK_W / 2, top + BLACK_H - 8, true);
846
+ const along = (i + 1) * WHITE_W - BLACK_W / 2;
847
+ root.appendChild(svg(doc, "rect", {
848
+ class: keyClass(black, true),
849
+ ...key(along, BLACK_W, BLACK_H),
850
+ rx: 2
851
+ }));
852
+ caption(black, at(along + BLACK_W / 2, BLACK_H - 13), true);
805
853
  });
806
854
  return root;
807
855
  }
@@ -881,6 +929,21 @@ function parseRange(text) {
881
929
  const to = m[2] === undefined ? from : Number(m[2]);
882
930
  return from <= to ? [from, to] : null;
883
931
  }
932
+ var SEPARATOR = /^\s*-{3,}\s*$/;
933
+ function splitParts(source) {
934
+ const lines = source.split(/\r?\n/);
935
+ if (!lines.some((line) => SEPARATOR.test(line)))
936
+ return null;
937
+ const parts = [[]];
938
+ for (const line of lines) {
939
+ if (SEPARATOR.test(line))
940
+ parts.push([]);
941
+ else
942
+ parts[parts.length - 1].push(line);
943
+ }
944
+ return parts.map((part) => part.join(`
945
+ `)).filter((part) => part.trim() !== "");
946
+ }
884
947
 
885
948
  // src/blocks/note-set-field.ts
886
949
  var LABELS = new Set(["notes", "degrees", "intervals", "none"]);
@@ -918,6 +981,11 @@ function orFail(value, message) {
918
981
  }
919
982
 
920
983
  // src/blocks/renderers.ts
984
+ var SHARED_KEYS = {
985
+ chord: ["orientation", "hand", "labels", "instrument", "tuning", "range"],
986
+ keyboard: ["orientation", "hand", "labels", "range"],
987
+ fretboard: ["orientation", "hand", "labels", "tuning", "frets"]
988
+ };
921
989
  var field = (block, ...names) => {
922
990
  for (const name of names) {
923
991
  const value = block.fields.get(name);
@@ -926,6 +994,29 @@ var field = (block, ...names) => {
926
994
  }
927
995
  return;
928
996
  };
997
+ var MAX_STRINGS = 12;
998
+ var MAX_KEYS = 88;
999
+ var tuningOf = (text) => {
1000
+ const tuning = orFail(parseTuning(text), `Unknown tuning "${text}"`);
1001
+ if (tuning.length > MAX_STRINGS)
1002
+ throw new Error(`At most ${MAX_STRINGS} strings`);
1003
+ return tuning;
1004
+ };
1005
+ var rangeOf = (text) => orFail(parseKeyRange(text), `Invalid range "${text}": use e.g. C3-B4`);
1006
+ var keyRange = (range) => {
1007
+ if (range[1] - range[0] + 1 > MAX_KEYS) {
1008
+ throw new Error(`At most ${MAX_KEYS} keys: give a smaller range, e.g. range: C3-B5`);
1009
+ }
1010
+ return range;
1011
+ };
1012
+ var orientationOf = (block, fallback) => {
1013
+ const text = field(block, "orientation");
1014
+ return text ? parseOrientation(text) : fallback;
1015
+ };
1016
+ var handOf = (block, options) => {
1017
+ const text = field(block, "hand");
1018
+ return text ? parseHand(text) : options.hand;
1019
+ };
929
1020
  var rootOf = (symbol) => {
930
1021
  const parts = splitChordSymbol(symbol);
931
1022
  return parts && parseChord(symbol) ? chromaOf(parts.root) : null;
@@ -962,8 +1053,7 @@ function pianoPitches(symbol) {
962
1053
  pitches.unshift(48 + bass);
963
1054
  return { ...set, pitches };
964
1055
  }
965
- var renderChord = (doc, source, options) => {
966
- const block = parseBlockSource(source);
1056
+ var renderChord = (doc, block, options) => {
967
1057
  const short = shorthand(block.head);
968
1058
  const name = field(block, "name");
969
1059
  const symbol = field(block, "symbol", "chord") ?? short.symbol ?? (name && parseChord(name) ? name : undefined);
@@ -1002,11 +1092,13 @@ var renderChord = (doc, source, options) => {
1002
1092
  });
1003
1093
  }
1004
1094
  const range = field(block, "range");
1095
+ handOf(block, options);
1005
1096
  return renderKeyboard(doc, set, {
1006
1097
  title,
1007
1098
  label: labelField(block, fingers.size ? "fingers" : "notes", fingers.size > 0),
1008
1099
  fingers,
1009
- range: range ? orFail(parseKeyRange(range), `Invalid range "${range}": use e.g. C3-B4`) : keyRangeOf(set)
1100
+ range: keyRange(range ? rangeOf(range) : keyRangeOf(set)),
1101
+ orientation: orientationOf(block, options.orientation?.keyboard)
1010
1102
  });
1011
1103
  }
1012
1104
  if (instrument && instrument !== "guitar" && !field(block, "tuning")) {
@@ -1016,6 +1108,8 @@ var renderChord = (doc, source, options) => {
1016
1108
  let voicing;
1017
1109
  if (fretsText) {
1018
1110
  const frets = orFail(parseFrets(fretsText), `Invalid frets "${fretsText}": use e.g. x02220`);
1111
+ if (frets.length > MAX_STRINGS)
1112
+ throw new Error(`At most ${MAX_STRINGS} strings`);
1019
1113
  const fingersText = field(block, "fingers") ?? short.fingers;
1020
1114
  const fingers = fingersText ? parseFingers(fingersText) : [];
1021
1115
  if (fingersText && fingers.length !== frets.length) {
@@ -1040,25 +1134,31 @@ var renderChord = (doc, source, options) => {
1040
1134
  } else
1041
1135
  voicing.baseFret ??= baseFretOf(voicing.frets);
1042
1136
  const tuningText = field(block, "tuning");
1043
- const tuning = tuningText ? orFail(parseTuning(tuningText), `Unknown tuning "${tuningText}"`) : undefined;
1137
+ const tuning = tuningText ? tuningOf(tuningText) : undefined;
1044
1138
  if (tuning && tuning.length !== voicing.frets.length) {
1045
1139
  throw new Error(`The tuning has ${tuning.length} strings, the frets ${voicing.frets.length}`);
1046
1140
  }
1047
1141
  const root = symbol ? chordNoteSet(symbol)?.root ?? rootOf(symbol) : null;
1048
- return renderGuitarDiagram(doc, voicing, { title, root, tuning });
1142
+ return renderGuitarDiagram(doc, voicing, {
1143
+ title,
1144
+ root,
1145
+ tuning,
1146
+ orientation: orientationOf(block, options.orientation?.chord),
1147
+ hand: handOf(block, options)
1148
+ });
1049
1149
  };
1050
- var renderKeyboardBlock = (doc, source) => {
1051
- const block = parseBlockSource(source);
1150
+ var renderKeyboardBlock = (doc, block, options) => {
1052
1151
  const set = noteSetOf(block);
1053
1152
  const range = field(block, "range");
1153
+ handOf(block, options);
1054
1154
  return renderKeyboard(doc, set, {
1055
1155
  title: field(block, "title"),
1056
1156
  label: labelField(block, "notes"),
1057
- range: range ? orFail(parseKeyRange(range), `Invalid range "${range}": use e.g. C3-B4`) : undefined
1157
+ range: keyRange(range ? rangeOf(range) : keyRangeOf(set)),
1158
+ orientation: orientationOf(block, options.orientation?.keyboard)
1058
1159
  });
1059
1160
  };
1060
- var renderFretboardBlock = (doc, source) => {
1061
- const block = parseBlockSource(source);
1161
+ var renderFretboardBlock = (doc, block, options) => {
1062
1162
  const set = noteSetOf(block);
1063
1163
  const fretsText = field(block, "frets");
1064
1164
  const frets = fretsText ? orFail(parseRange(fretsText), `Invalid frets "${fretsText}": use e.g. 5-8`) : undefined;
@@ -1069,39 +1169,149 @@ var renderFretboardBlock = (doc, source) => {
1069
1169
  title: field(block, "title"),
1070
1170
  label: labelField(block, "notes"),
1071
1171
  frets,
1072
- tuning: tuningText ? orFail(parseTuning(tuningText), `Unknown tuning "${tuningText}"`) : undefined
1172
+ tuning: tuningText ? tuningOf(tuningText) : undefined,
1173
+ orientation: orientationOf(block, options.orientation?.fretboard),
1174
+ hand: handOf(block, options)
1073
1175
  });
1074
1176
  };
1075
- var renderChordsBlock = (doc, source, options) => renderChart(doc, parseChordPro(source), { transpose: options.transpose });
1076
- var RENDERERS = {
1177
+ var DIAGRAMS = {
1077
1178
  chord: renderChord,
1078
- chords: renderChordsBlock,
1079
1179
  keyboard: renderKeyboardBlock,
1080
1180
  fretboard: renderFretboardBlock
1081
1181
  };
1182
+ var fromSource = (render) => (doc, source, options) => render(doc, parseBlockSource(source), options);
1183
+ var renderChordsBlock = (doc, source, options) => renderChart(doc, parseChordPro(source), { transpose: options.transpose });
1184
+ var RENDERERS = {
1185
+ chord: fromSource(renderChord),
1186
+ chords: renderChordsBlock,
1187
+ keyboard: fromSource(renderKeyboardBlock),
1188
+ fretboard: fromSource(renderFretboardBlock)
1189
+ };
1082
1190
 
1083
1191
  // src/render/render-block.ts
1084
1192
  var LANGUAGES = Object.keys(RENDERERS);
1085
1193
  var isNotationLanguage = (language) => Object.prototype.hasOwnProperty.call(RENDERERS, language);
1086
- function renderBlock(language, source, host, options = {}) {
1194
+ var isDiagramLanguage = (language) => Object.prototype.hasOwnProperty.call(DIAGRAMS, language);
1195
+ var MAX_DIAGRAMS = 32;
1196
+ var messageOf = (e) => e instanceof Error ? e.message : String(e);
1197
+ function renderBlock(language, source, host, given = {}) {
1087
1198
  const doc = host.ownerDocument;
1088
1199
  const el = html(doc, "div", `mn-block mn-block-${language}`);
1089
1200
  let error = null;
1090
- if (!source.trim()) {
1201
+ const layout = layoutDefaults(given);
1202
+ const options = {
1203
+ ...given ?? {},
1204
+ orientation: layout.orientation ?? {},
1205
+ hand: layout.hand ?? "right"
1206
+ };
1207
+ const parts = isDiagramLanguage(language) ? splitParts(source) : null;
1208
+ if (!source.trim() || parts?.length === 0) {
1091
1209
  el.classList.add("mn-empty");
1092
1210
  host.appendChild(el);
1093
1211
  return { ok: true, el, error: null, destroy: () => el.remove() };
1094
1212
  }
1095
1213
  try {
1096
- el.appendChild(RENDERERS[language](doc, source, options));
1214
+ if (parts && isDiagramLanguage(language)) {
1215
+ error = renderRow(doc, language, parts, el, options);
1216
+ } else {
1217
+ el.appendChild(RENDERERS[language](doc, source, options));
1218
+ }
1097
1219
  } catch (e) {
1098
- error = e instanceof Error ? e.message : String(e);
1220
+ error = messageOf(e);
1221
+ el.replaceChildren();
1099
1222
  el.classList.add("mn-error");
1100
1223
  el.appendChild(html(doc, "div", "mn-error-message", `${language}: ${error}`));
1101
1224
  }
1102
1225
  host.appendChild(el);
1103
1226
  return { ok: error === null, el, error, destroy: () => el.remove() };
1104
1227
  }
1228
+ var filled = (block) => ({
1229
+ head: block.head,
1230
+ fields: new Map([...block.fields].filter(([, value]) => value !== ""))
1231
+ });
1232
+ function headerOf(language, part) {
1233
+ if (part === undefined)
1234
+ return null;
1235
+ let block;
1236
+ try {
1237
+ block = filled(parseBlockSource(part));
1238
+ } catch {
1239
+ return null;
1240
+ }
1241
+ if (block.head !== null)
1242
+ return null;
1243
+ const shared = SHARED_KEYS[language];
1244
+ const others = [...block.fields.keys()].filter((key) => key !== "columns" && !shared.includes(key));
1245
+ if (!others.length)
1246
+ return block.fields.size ? block : null;
1247
+ if (!block.fields.has("columns"))
1248
+ return null;
1249
+ throw new Error(`"${others[0]}" can't be shared: move it to a diagram. Shared settings: ${shared.join(", ")}, columns`);
1250
+ }
1251
+ function checkShared(header) {
1252
+ const orientation = header.fields.get("orientation");
1253
+ if (orientation !== undefined)
1254
+ parseOrientation(orientation);
1255
+ const hand = header.fields.get("hand");
1256
+ if (hand !== undefined)
1257
+ parseHand(hand);
1258
+ }
1259
+ function sharesSomething(language, part) {
1260
+ try {
1261
+ const block = parseBlockSource(part);
1262
+ return block.head === null && [...block.fields.keys()].some((k) => SHARED_KEYS[language].includes(k));
1263
+ } catch {
1264
+ return false;
1265
+ }
1266
+ }
1267
+ function columnsOf(header) {
1268
+ const text = header?.fields.get("columns");
1269
+ if (text === undefined)
1270
+ return null;
1271
+ const n = Number(text);
1272
+ if (!Number.isInteger(n) || n < 1 || n > 12) {
1273
+ throw new Error(`columns must be a whole number from 1 to 12, got "${text}"`);
1274
+ }
1275
+ return n;
1276
+ }
1277
+ function renderRow(doc, language, parts, el, options) {
1278
+ const header = headerOf(language, parts[0]);
1279
+ const diagrams = header ? parts.slice(1) : parts;
1280
+ if (header && !diagrams.length) {
1281
+ throw new Error('Add a diagram after the shared settings: a "---" line, then the diagram');
1282
+ }
1283
+ if (header)
1284
+ checkShared(header);
1285
+ if (diagrams.length > MAX_DIAGRAMS) {
1286
+ throw new Error(`At most ${MAX_DIAGRAMS} diagrams per block, found ${diagrams.length}`);
1287
+ }
1288
+ const columns = columnsOf(header);
1289
+ const shared = [...header?.fields ?? []].filter(([key]) => key !== "columns");
1290
+ const row = html(doc, "div", columns ? `mn-grid mn-cols-${columns}` : "mn-row");
1291
+ const errors = [];
1292
+ diagrams.forEach((part, i) => {
1293
+ const cell = html(doc, "div", "mn-cell");
1294
+ try {
1295
+ const block = filled(parseBlockSource(part));
1296
+ if (block.fields.has("columns")) {
1297
+ throw new Error('"columns" goes in the shared settings, before the first "---"');
1298
+ }
1299
+ const fields = new Map([...shared, ...block.fields]);
1300
+ cell.appendChild(DIAGRAMS[language](doc, { head: block.head, fields }, options));
1301
+ } catch (e) {
1302
+ let message = `diagram ${i + 1}: ${messageOf(e)}`;
1303
+ if (i === 0 && !header && sharesSomething(language, part)) {
1304
+ message += ` (shared settings before the first "---" may only hold: ${SHARED_KEYS[language].join(", ")}, columns)`;
1305
+ }
1306
+ errors.push(message);
1307
+ cell.classList.add("mn-error");
1308
+ cell.appendChild(html(doc, "div", "mn-error-message", `${language}, ${message}`));
1309
+ }
1310
+ row.appendChild(cell);
1311
+ });
1312
+ el.appendChild(row);
1313
+ return errors.length ? errors.join("; ") : null;
1314
+ }
1105
1315
 
1106
1316
  // src/publish/index.ts
1107
1317
  var SELECTOR = LANGUAGES.map((l) => `pre > code.language-${l}`).join(", ");
@@ -1177,5 +1387,5 @@ export {
1177
1387
  renderAll
1178
1388
  };
1179
1389
 
1180
- //# debugId=C572C1ECDF75A2A264756E2164756E21
1390
+ //# debugId=E68093ACB7219ED764756E2164756E21
1181
1391
  //# sourceMappingURL=index.js.map