@dsebastien/music-notation 0.1.2 → 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.
@@ -1,12 +1,3 @@
1
- import {
2
- canonicalSuffix,
3
- chromaOf,
4
- isFlat,
5
- midiOf,
6
- nameOf,
7
- splitChordSymbol
8
- } from "../chunks/index-26cddyf5.js";
9
-
10
1
  // src/render/dom.ts
11
2
  var SVG_NS = "http://www.w3.org/2000/svg";
12
3
  var apply = (el, attrs) => {
@@ -28,6 +19,12 @@ function svgText(doc, text, attrs = {}) {
28
19
  el.textContent = text;
29
20
  return el;
30
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
+ }
31
28
  function html(doc, tag, cls, text) {
32
29
  const el = doc.createElement(tag);
33
30
  if (cls)
@@ -47,6 +44,75 @@ function svgRoot(doc, width, height, cls, label) {
47
44
  });
48
45
  }
49
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
+
50
116
  // src/chord/guitar.ts
51
117
  var STANDARD_TUNING = [40, 45, 50, 55, 59, 64];
52
118
  function parseFrets(text) {
@@ -91,94 +157,80 @@ function barresOf(voicing) {
91
157
  }
92
158
  var SPACING = 18;
93
159
  var ROW = 20;
94
- var LEFT = 30;
95
- var TITLE = 22;
96
160
  var MARKERS = 16;
161
+ var BASE = 30;
162
+ var TITLE = 22;
97
163
  function renderGuitarDiagram(doc, voicing, options = {}) {
98
164
  const strings = voicing.frets.length;
99
165
  const base = voicing.baseFret ?? baseFretOf(voicing.frets);
100
166
  const fretted = voicing.frets.filter((f) => f !== null && f > 0);
101
167
  const rows = Math.max(4, fretted.length ? Math.max(...fretted) - base + 1 : 0);
102
- const top = (options.title ? TITLE : 4) + MARKERS;
103
- const width = LEFT + (strings - 1) * SPACING + 14;
104
- const height = top + rows * ROW + 10;
105
- const x = (string) => LEFT + string * SPACING;
106
- 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));
107
185
  const label = options.title ? `${options.title} chord diagram` : "Chord diagram";
108
- const root = svgRoot(doc, width, height, "mn-guitar-chord", label);
186
+ const root = svgRoot(doc, frame.width, frame.height, "mn-guitar-chord", label);
109
187
  if (options.title) {
110
188
  root.appendChild(svgText(doc, options.title, {
111
189
  class: "mn-title",
112
- x: width / 2,
190
+ x: frame.width / 2,
113
191
  y: 15,
114
192
  "text-anchor": "middle"
115
193
  }));
116
194
  }
117
195
  for (let row = 0;row <= rows; row++) {
118
- root.appendChild(svg(doc, "line", {
119
- class: row === 0 && base === 1 ? "mn-nut" : "mn-fret",
120
- x1: x(0),
121
- x2: x(strings - 1),
122
- y1: y(row),
123
- y2: y(row)
124
- }));
196
+ const cls = row === 0 && base === 1 ? "mn-nut" : "mn-fret";
197
+ root.appendChild(svgLine(doc, at(0, row), at(strings - 1, row), cls));
125
198
  }
126
199
  for (let s = 0;s < strings; s++) {
127
- 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"));
128
201
  }
129
202
  if (base > 1) {
130
- root.appendChild(svgText(doc, `${base}fr`, {
131
- class: "mn-base-fret",
132
- x: LEFT - 10,
133
- y: y(0.5) + 4,
134
- "text-anchor": "end"
135
- }));
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"));
136
205
  }
137
206
  for (const barre of barresOf(voicing)) {
138
- const cy = y(barre.fret - base + 0.5);
207
+ const middle = along(barre.fret - base + 0.5);
139
208
  root.appendChild(svg(doc, "rect", {
140
209
  class: "mn-barre",
141
- x: x(barre.from) - 7,
142
- y: cy - 7,
143
- width: x(barre.to) - x(barre.from) + 14,
144
- height: 14,
210
+ ...frame.box(across(barre.from) - 7, middle - 7, across(barre.to) + 7, middle + 7),
145
211
  rx: 7
146
212
  }));
147
213
  }
148
214
  const tuning = options.tuning ?? (strings === 6 ? STANDARD_TUNING : undefined);
149
215
  voicing.frets.forEach((fret, s) => {
150
- const cx = x(s);
216
+ const mark = frame.at(across(s), -8);
151
217
  if (fret === null) {
152
- root.appendChild(svgText(doc, "×", {
153
- class: "mn-muted",
154
- x: cx,
155
- y: top - 4,
156
- "text-anchor": "middle"
157
- }));
218
+ root.appendChild(svgLabel(doc, "×", mark, "mn-muted"));
158
219
  return;
159
220
  }
160
221
  const open = tuning?.[s];
161
222
  const isRoot = options.root !== undefined && options.root !== null && open !== undefined && (open + fret) % 12 === options.root;
162
223
  if (fret === 0) {
163
- root.appendChild(svg(doc, "circle", {
164
- class: `mn-open${isRoot ? " mn-root" : ""}`,
165
- cx,
166
- cy: top - 8,
167
- r: 4
168
- }));
224
+ const [cx, cy] = mark;
225
+ root.appendChild(svg(doc, "circle", { class: `mn-open${isRoot ? " mn-root" : ""}`, cx, cy, r: 4 }));
169
226
  return;
170
227
  }
171
- const cy = y(fret - base + 0.5);
228
+ const center = at(s, fret - base + 0.5);
229
+ const [cx, cy] = center;
172
230
  root.appendChild(svg(doc, "circle", { class: `mn-dot${isRoot ? " mn-root" : ""}`, cx, cy, r: 7 }));
173
231
  const finger = voicing.fingers[s];
174
- if (finger) {
175
- root.appendChild(svgText(doc, finger, {
176
- class: "mn-dot-text",
177
- x: cx,
178
- y: cy + 4,
179
- "text-anchor": "middle"
180
- }));
181
- }
232
+ if (finger)
233
+ root.appendChild(svgLabel(doc, finger, center, "mn-dot-text"));
182
234
  });
183
235
  return root;
184
236
  }
@@ -301,6 +353,52 @@ function parseChordPro(source) {
301
353
  return song;
302
354
  }
303
355
 
356
+ // src/theory/pitch.ts
357
+ var SHARP_NAMES = [
358
+ "C",
359
+ "C#",
360
+ "D",
361
+ "D#",
362
+ "E",
363
+ "F",
364
+ "F#",
365
+ "G",
366
+ "G#",
367
+ "A",
368
+ "A#",
369
+ "B"
370
+ ];
371
+ var FLAT_NAMES = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
372
+ var LETTER_CHROMA = { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 };
373
+ function chromaOf(name) {
374
+ const m = /^([A-Ga-g])([#♯b♭]*)$/.exec(name.trim());
375
+ if (!m)
376
+ return null;
377
+ let chroma = LETTER_CHROMA[m[1].toUpperCase()];
378
+ for (const accidental of m[2])
379
+ chroma += accidental === "#" || accidental === "♯" ? 1 : -1;
380
+ return (chroma % 12 + 12) % 12;
381
+ }
382
+ function midiOf(note) {
383
+ const m = /^([A-Ga-g][#♯b♭]*)(-?\d)$/.exec(note.trim());
384
+ if (!m)
385
+ return null;
386
+ const chroma = chromaOf(m[1]);
387
+ if (chroma === null)
388
+ return null;
389
+ const letter = LETTER_CHROMA[m[1][0].toUpperCase()];
390
+ const shift = Math.round((chroma - letter) / 12);
391
+ return (Number(m[2]) + 1) * 12 + chroma - shift * 12;
392
+ }
393
+ var nameOf = (chroma, preferFlats = false) => (preferFlats ? FLAT_NAMES : SHARP_NAMES)[((Math.round(chroma) || 0) % 12 + 12) % 12];
394
+ function splitChordSymbol(symbol) {
395
+ const normalized = symbol.trim().replace(/♯/g, "#").replace(/♭/g, "b");
396
+ const m = /^([A-G][#b]?)(.*?)(?:\/([A-G][#b]?))?$/.exec(normalized);
397
+ return m ? { root: m[1], suffix: m[2], bass: m[3] ?? null } : null;
398
+ }
399
+ var noteOf = (midi, preferFlats = false) => `${nameOf(midi, preferFlats)}${Math.floor(midi / 12) - 1}`;
400
+ var isFlat = (name) => /^[A-Ga-g][b♭]/.test(name.trim());
401
+
304
402
  // src/theory/transpose.ts
305
403
  var FLAT_KEYS = new Set([5, 10, 3, 8, 1]);
306
404
  var FLAT_MINOR_KEYS = new Set([2, 7, 0, 5, 10, 3]);
@@ -406,6 +504,41 @@ function renderChart(doc, song, options = {}) {
406
504
 
407
505
  // src/theory/note-set.ts
408
506
  import { Chord, Scale } from "tonal";
507
+
508
+ // src/theory/suffix.ts
509
+ var CANONICAL = new Map([
510
+ ["M", ""],
511
+ ["maj", ""],
512
+ ["min", "m"],
513
+ ["-", "m"],
514
+ ["M7", "maj7"],
515
+ ["Δ", "maj7"],
516
+ ["Δ7", "maj7"],
517
+ ["M9", "maj9"],
518
+ ["Δ9", "maj9"],
519
+ ["min6", "m6"],
520
+ ["-6", "m6"],
521
+ ["min7", "m7"],
522
+ ["-7", "m7"],
523
+ ["min9", "m9"],
524
+ ["-9", "m9"],
525
+ ["ø", "m7b5"],
526
+ ["ø7", "m7b5"],
527
+ ["m7-5", "m7b5"],
528
+ ["mmaj7", "mMaj7"],
529
+ ["mM7", "mMaj7"],
530
+ ["m(maj7)", "mMaj7"],
531
+ ["-(maj7)", "mMaj7"],
532
+ ["°", "dim"],
533
+ ["°7", "dim7"],
534
+ ["+", "aug"],
535
+ ["sus", "sus4"],
536
+ ["add2", "add9"],
537
+ ["69", "6/9"]
538
+ ]);
539
+ var canonicalSuffix = (suffix) => CANONICAL.get(suffix) ?? suffix;
540
+
541
+ // src/theory/note-set.ts
409
542
  var memberFor = (set, chroma) => set.members.find((m) => m.chroma === chroma);
410
543
  var memberOf = (set, midi) => set.pitches && !set.pitches.includes(midi) ? undefined : memberFor(set, (midi % 12 + 12) % 12);
411
544
  function intervalLabel(interval) {
@@ -500,9 +633,8 @@ function listNoteSet(text) {
500
633
  var STRING_GAP = 20;
501
634
  var FRET_W = 44;
502
635
  var OPEN_W = 30;
503
- var LEFT2 = 24;
636
+ var NUMBERS = 30;
504
637
  var TITLE2 = 22;
505
- var BOTTOM = 22;
506
638
  var SINGLE_INLAYS = new Set([3, 5, 7, 9, 15, 17, 19, 21]);
507
639
  function renderFretboard(doc, set, options = {}) {
508
640
  const tuning = options.tuning ?? STANDARD_TUNING;
@@ -510,96 +642,68 @@ function renderFretboard(doc, set, options = {}) {
510
642
  const first = Math.max(from, 1);
511
643
  const strings = tuning.length;
512
644
  const label = options.label ?? "notes";
513
- const top = (options.title ? TITLE2 : 0) + 12;
645
+ const orientation = options.orientation ?? "horizontal";
646
+ const vertical = orientation === "vertical";
514
647
  const openW = from === 0 ? OPEN_W : 0;
515
- const nutX = LEFT2 + openW;
516
- const width = nutX + (to - first + 1) * FRET_W + 8;
517
- const height = top + (strings - 1) * STRING_GAP + BOTTOM + 8;
518
- const y = (row) => top + row * STRING_GAP;
519
- const wireX = (fret) => nutX + (fret - first + 1) * FRET_W;
520
- const centerX = (fret) => fret === 0 ? LEFT2 + OPEN_W / 2 : wireX(fret) - FRET_W / 2;
521
- 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}`);
522
665
  if (options.title) {
523
666
  root.appendChild(svgText(doc, options.title, {
524
667
  class: "mn-title",
525
- x: width / 2,
668
+ x: frame.width / 2,
526
669
  y: 15,
527
670
  "text-anchor": "middle"
528
671
  }));
529
672
  }
530
- 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;
531
678
  for (let fret = first;fret <= to; fret++) {
532
- const cx = wireX(fret) - FRET_W / 2;
533
679
  if (SINGLE_INLAYS.has(fret))
534
- root.appendChild(svg(doc, "circle", { class: "mn-inlay", cx, cy: middle, r: 4 }));
680
+ inlay(acrossLength / 2, fret);
535
681
  if (fret % 12 === 0) {
536
- root.appendChild(svg(doc, "circle", { class: "mn-inlay", cx, cy: y(strings > 3 ? 1.5 : 0.5), r: 4 }));
537
- root.appendChild(svg(doc, "circle", {
538
- class: "mn-inlay",
539
- cx,
540
- cy: y(strings > 3 ? strings - 2.5 : strings - 1.5),
541
- r: 4
542
- }));
682
+ inlay(acrossLength - pair, fret);
683
+ inlay(pair, fret);
543
684
  }
544
- root.appendChild(svgText(doc, String(fret), {
545
- class: "mn-fret-number",
546
- x: cx,
547
- y: y(strings - 1) + BOTTOM,
548
- "text-anchor": "middle"
549
- }));
685
+ root.appendChild(svgLabel(doc, String(fret), frame.at(-18, center(fret)), "mn-fret-number"));
550
686
  }
551
- root.appendChild(svg(doc, "line", {
552
- class: first === 1 ? "mn-nut" : "mn-fret",
553
- x1: nutX,
554
- x2: nutX,
555
- y1: y(0),
556
- y2: y(strings - 1)
557
- }));
687
+ root.appendChild(svgLine(doc, frame.at(acrossLength, openW), frame.at(0, openW), first === 1 ? "mn-nut" : "mn-fret"));
558
688
  for (let fret = first;fret <= to; fret++) {
559
- root.appendChild(svg(doc, "line", {
560
- class: "mn-fret",
561
- x1: wireX(fret),
562
- x2: wireX(fret),
563
- y1: y(0),
564
- y2: y(strings - 1)
565
- }));
689
+ root.appendChild(svgLine(doc, frame.at(acrossLength, wire(fret)), frame.at(0, wire(fret)), "mn-fret"));
566
690
  }
567
- for (let row = 0;row < strings; row++) {
568
- const open = tuning[strings - 1 - row];
569
- root.appendChild(svg(doc, "line", {
570
- class: "mn-string",
571
- x1: nutX - openW,
572
- x2: wireX(to),
573
- y1: y(row),
574
- y2: y(row)
575
- }));
576
- root.appendChild(svgText(doc, nameOf(open, set.preferFlats), {
577
- class: "mn-string-name",
578
- x: LEFT2 - 8,
579
- y: y(row) + 4,
580
- "text-anchor": "end"
581
- }));
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"));
582
696
  for (let fret = from;fret <= to; fret++) {
583
697
  const member = memberOf(set, open + fret);
584
698
  if (!member)
585
699
  continue;
586
- const cx = centerX(fret);
587
700
  const isRoot = set.root === member.chroma;
588
- root.appendChild(svg(doc, "circle", {
589
- class: `mn-dot${isRoot ? " mn-root" : ""}`,
590
- cx,
591
- cy: y(row),
592
- r: 8
593
- }));
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 }));
594
704
  const text = labelOf(member, label);
595
- if (text) {
596
- root.appendChild(svgText(doc, text, {
597
- class: "mn-dot-text",
598
- x: cx,
599
- y: y(row) + 4,
600
- "text-anchor": "middle"
601
- }));
602
- }
705
+ if (text)
706
+ root.appendChild(svgLabel(doc, text, point, "mn-dot-text"));
603
707
  }
604
708
  }
605
709
  return root;
@@ -680,9 +784,24 @@ function renderKeyboard(doc, set, options = {}) {
680
784
  for (let midi = from;midi <= to; midi++)
681
785
  if (!isBlack(midi))
682
786
  whites.push(midi);
787
+ const vertical = options.orientation === "vertical";
683
788
  const top = options.title ? TITLE3 : 0;
684
- const width = whites.length * WHITE_W + 2;
685
- 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
+ };
686
805
  const label = options.label ?? "notes";
687
806
  const root = svgRoot(doc, width, height, "mn-keyboard", options.title ?? `Keyboard: ${set.title}`);
688
807
  if (options.title) {
@@ -700,36 +819,37 @@ function renderKeyboard(doc, set, options = {}) {
700
819
  classes.push("mn-on");
701
820
  if (member && set.root === member.chroma)
702
821
  classes.push("mn-root");
703
- return { member, cls: classes.join(" ") };
822
+ return classes.join(" ");
704
823
  };
705
- const caption = (midi, x, y, black) => {
824
+ const caption = (midi, point, black) => {
706
825
  const member = memberOf(set, midi);
707
826
  if (!member)
708
827
  return;
709
828
  const text = labelOf(member, label, options.fingers?.get(midi));
710
829
  if (!text)
711
830
  return;
712
- root.appendChild(svgText(doc, text, {
713
- class: black ? "mn-key-label mn-key-label-black" : "mn-key-label",
714
- x,
715
- y,
716
- "text-anchor": "middle"
717
- }));
831
+ root.appendChild(svgLabel(doc, text, point, black ? "mn-key-label mn-key-label-black" : "mn-key-label"));
718
832
  };
719
833
  whites.forEach((midi, i) => {
720
- const x = 1 + i * WHITE_W;
721
- const { cls } = keyClass(midi, false);
722
- root.appendChild(svg(doc, "rect", { class: cls, x, y: top + 1, width: WHITE_W, height: WHITE_H, rx: 2 }));
723
- 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);
724
841
  });
725
842
  whites.forEach((midi, i) => {
726
843
  const black = midi + 1;
727
844
  if (black > to || !isBlack(black))
728
845
  return;
729
- const x = 1 + (i + 1) * WHITE_W - BLACK_W / 2;
730
- const { cls } = keyClass(black, true);
731
- root.appendChild(svg(doc, "rect", { class: cls, x, y: top + 1, width: BLACK_W, height: BLACK_H, rx: 2 }));
732
- 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);
733
853
  });
734
854
  return root;
735
855
  }
@@ -809,6 +929,21 @@ function parseRange(text) {
809
929
  const to = m[2] === undefined ? from : Number(m[2]);
810
930
  return from <= to ? [from, to] : null;
811
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
+ }
812
947
 
813
948
  // src/blocks/note-set-field.ts
814
949
  var LABELS = new Set(["notes", "degrees", "intervals", "none"]);
@@ -846,6 +981,11 @@ function orFail(value, message) {
846
981
  }
847
982
 
848
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
+ };
849
989
  var field = (block, ...names) => {
850
990
  for (const name of names) {
851
991
  const value = block.fields.get(name);
@@ -854,6 +994,29 @@ var field = (block, ...names) => {
854
994
  }
855
995
  return;
856
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
+ };
857
1020
  var rootOf = (symbol) => {
858
1021
  const parts = splitChordSymbol(symbol);
859
1022
  return parts && parseChord(symbol) ? chromaOf(parts.root) : null;
@@ -890,8 +1053,7 @@ function pianoPitches(symbol) {
890
1053
  pitches.unshift(48 + bass);
891
1054
  return { ...set, pitches };
892
1055
  }
893
- var renderChord = (doc, source, options) => {
894
- const block = parseBlockSource(source);
1056
+ var renderChord = (doc, block, options) => {
895
1057
  const short = shorthand(block.head);
896
1058
  const name = field(block, "name");
897
1059
  const symbol = field(block, "symbol", "chord") ?? short.symbol ?? (name && parseChord(name) ? name : undefined);
@@ -930,11 +1092,13 @@ var renderChord = (doc, source, options) => {
930
1092
  });
931
1093
  }
932
1094
  const range = field(block, "range");
1095
+ handOf(block, options);
933
1096
  return renderKeyboard(doc, set, {
934
1097
  title,
935
1098
  label: labelField(block, fingers.size ? "fingers" : "notes", fingers.size > 0),
936
1099
  fingers,
937
- 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)
938
1102
  });
939
1103
  }
940
1104
  if (instrument && instrument !== "guitar" && !field(block, "tuning")) {
@@ -944,6 +1108,8 @@ var renderChord = (doc, source, options) => {
944
1108
  let voicing;
945
1109
  if (fretsText) {
946
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`);
947
1113
  const fingersText = field(block, "fingers") ?? short.fingers;
948
1114
  const fingers = fingersText ? parseFingers(fingersText) : [];
949
1115
  if (fingersText && fingers.length !== frets.length) {
@@ -968,25 +1134,31 @@ var renderChord = (doc, source, options) => {
968
1134
  } else
969
1135
  voicing.baseFret ??= baseFretOf(voicing.frets);
970
1136
  const tuningText = field(block, "tuning");
971
- const tuning = tuningText ? orFail(parseTuning(tuningText), `Unknown tuning "${tuningText}"`) : undefined;
1137
+ const tuning = tuningText ? tuningOf(tuningText) : undefined;
972
1138
  if (tuning && tuning.length !== voicing.frets.length) {
973
1139
  throw new Error(`The tuning has ${tuning.length} strings, the frets ${voicing.frets.length}`);
974
1140
  }
975
1141
  const root = symbol ? chordNoteSet(symbol)?.root ?? rootOf(symbol) : null;
976
- 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
+ });
977
1149
  };
978
- var renderKeyboardBlock = (doc, source) => {
979
- const block = parseBlockSource(source);
1150
+ var renderKeyboardBlock = (doc, block, options) => {
980
1151
  const set = noteSetOf(block);
981
1152
  const range = field(block, "range");
1153
+ handOf(block, options);
982
1154
  return renderKeyboard(doc, set, {
983
1155
  title: field(block, "title"),
984
1156
  label: labelField(block, "notes"),
985
- 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)
986
1159
  });
987
1160
  };
988
- var renderFretboardBlock = (doc, source) => {
989
- const block = parseBlockSource(source);
1161
+ var renderFretboardBlock = (doc, block, options) => {
990
1162
  const set = noteSetOf(block);
991
1163
  const fretsText = field(block, "frets");
992
1164
  const frets = fretsText ? orFail(parseRange(fretsText), `Invalid frets "${fretsText}": use e.g. 5-8`) : undefined;
@@ -997,38 +1169,149 @@ var renderFretboardBlock = (doc, source) => {
997
1169
  title: field(block, "title"),
998
1170
  label: labelField(block, "notes"),
999
1171
  frets,
1000
- 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)
1001
1175
  });
1002
1176
  };
1003
- var renderChordsBlock = (doc, source, options) => renderChart(doc, parseChordPro(source), { transpose: options.transpose });
1004
- var RENDERERS = {
1177
+ var DIAGRAMS = {
1005
1178
  chord: renderChord,
1006
- chords: renderChordsBlock,
1007
1179
  keyboard: renderKeyboardBlock,
1008
1180
  fretboard: renderFretboardBlock
1009
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
+ };
1010
1190
 
1011
1191
  // src/render/render-block.ts
1012
1192
  var LANGUAGES = Object.keys(RENDERERS);
1013
- function renderBlock(language, source, host, options = {}) {
1193
+ var isNotationLanguage = (language) => Object.prototype.hasOwnProperty.call(RENDERERS, language);
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 = {}) {
1014
1198
  const doc = host.ownerDocument;
1015
1199
  const el = html(doc, "div", `mn-block mn-block-${language}`);
1016
1200
  let error = null;
1017
- 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) {
1018
1209
  el.classList.add("mn-empty");
1019
1210
  host.appendChild(el);
1020
1211
  return { ok: true, el, error: null, destroy: () => el.remove() };
1021
1212
  }
1022
1213
  try {
1023
- 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
+ }
1024
1219
  } catch (e) {
1025
- error = e instanceof Error ? e.message : String(e);
1220
+ error = messageOf(e);
1221
+ el.replaceChildren();
1026
1222
  el.classList.add("mn-error");
1027
1223
  el.appendChild(html(doc, "div", "mn-error-message", `${language}: ${error}`));
1028
1224
  }
1029
1225
  host.appendChild(el);
1030
1226
  return { ok: error === null, el, error, destroy: () => el.remove() };
1031
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
+ }
1032
1315
 
1033
1316
  // src/publish/index.ts
1034
1317
  var SELECTOR = LANGUAGES.map((l) => `pre > code.language-${l}`).join(", ");
@@ -1104,5 +1387,5 @@ export {
1104
1387
  renderAll
1105
1388
  };
1106
1389
 
1107
- //# debugId=0A178BB05886C8DB64756E2164756E21
1390
+ //# debugId=E68093ACB7219ED764756E2164756E21
1108
1391
  //# sourceMappingURL=index.js.map