@barmark/render 0.14.0 → 0.15.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.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,32 @@ package uses [semantic versioning](https://semver.org/).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.15.0] - 2026-11-07
10
+
11
+ Needs `barmark` 0.17.0.
12
+
13
+ ### Added
14
+
15
+ - **Voice lines and lyrics** (barmark's grammar 4.3, 7.7): the line a part
16
+ sings words on draws its notes as pitch names, spaced by beat, with
17
+ each verse's syllables under them: hyphens within a word, an extender
18
+ for a held syllable (`:/`), verses numbered when there are several. A
19
+ lyric row on no melody draws its words in the bars it covers, spaced
20
+ by beat, so they wrap with their bars. A screen reader hears each bar's
21
+ words after its notes. The model's rows gain `sings`, `verses` and
22
+ `lyrics`; `--barmark-lyrics` colours the words, and `::part(lyrics)`
23
+ styles each verse's line. A `lyrics:` line followed by a
24
+ bar line, which drew as a line of text, now draws its words on the
25
+ beats.
26
+
27
+ ### Changed
28
+
29
+ - A `---` break draws nothing, and a text line written with a leading `\`
30
+ shows without the backslash.
31
+ - A part's bare line with its named lines written after it (`Vox` over
32
+ `Vox.Mel`, barmark's amended grammar 7.6) draws in the same system, as
33
+ the part's first row.
34
+
9
35
  ## [0.14.0] - 2026-11-04
10
36
 
11
37
  Needs `barmark` 0.16.0.
@@ -349,7 +375,8 @@ that needs no script or as the `<barmark-chart>` web component. Requires
349
375
  zoom without sideways scrolling, the element renders and re-renders, and
350
376
  a screen reader hears "bar 9: D". Run them with `bun run test:browser`.
351
377
 
352
- [Unreleased]: https://github.com/kevinpeckham/barmark/compare/render-v0.14.0...HEAD
378
+ [Unreleased]: https://github.com/kevinpeckham/barmark/compare/render-v0.15.0...HEAD
379
+ [0.15.0]: https://github.com/kevinpeckham/barmark/compare/render-v0.14.0...render-v0.15.0
353
380
  [0.14.0]: https://github.com/kevinpeckham/barmark/compare/render-v0.13.0...render-v0.14.0
354
381
  [0.13.0]: https://github.com/kevinpeckham/barmark/compare/render-v0.12.0...render-v0.13.0
355
382
  [0.12.0]: https://github.com/kevinpeckham/barmark/compare/render-v0.11.0...render-v0.12.0
package/README.md CHANGED
@@ -129,6 +129,7 @@ Set these custom properties on the element or anything above it:
129
129
  | `--barmark-accent` | errors |
130
130
  | `--barmark-directive` | mid-chart changes (default the muted colour) |
131
131
  | `--barmark-note` | `###` note lines (default the ink colour) |
132
+ | `--barmark-lyrics` | syllables under a voice line and in lyric rows (default the ink colour) |
132
133
 
133
134
  ```css
134
135
  barmark-chart {
@@ -142,7 +143,8 @@ barmark-chart::part(heading) {
142
143
  ```
143
144
 
144
145
  `::part()` names: `title`, `meta`, `heading`, `bar`, `chord`, `text`,
145
- `bar-number`, `rehearsal`. These, the custom properties and the
146
+ `bar-number`, `rehearsal`, `lyrics` (each verse's line of words, inside
147
+ its `bar`). These, the custom properties and the
146
148
  attributes are the public surface: changing them is a breaking change.
147
149
 
148
150
  ## Light, dark and print
@@ -196,6 +198,24 @@ shape; a tutti line is one row. The header lists the parts' names, and
196
198
  each part row is announced by name to a screen reader. On a screen too
197
199
  narrow for a section, its rows wrap one by one, as any row does.
198
200
 
201
+ A voice line (barmark's grammar 4.3 and 7.7: the line a part sings on,
202
+ such as `Vox.Mel` under a lead sheet's bare `Vox` chord line) draws each
203
+ note as its pitch name (every note of a set, without octaves) and
204
+ under it, in italics, the syllable each verse sings there: verse 1 from
205
+ the syllables on the notes, then each `lyrics N:` row, numbered "1.",
206
+ "2." left of the first bar when there are several. A hyphen follows a
207
+ syllable whose word goes on, and a held syllable (`:/`) draws an extender
208
+ line. The notes are spaced by beat, each as wide as the widest note or
209
+ syllable. Verses come from the parser as played, placed under the written
210
+ bars: a repeat's verse 2 is drawn under the bars it is sung on, and a
211
+ replay draws its own words. A voice line with no words draws as any part
212
+ line does. A lyric row on no melody (the chart's words, or a singer who
213
+ writes no notes there) draws its words inside the bars it covers, a line
214
+ per verse spaced by beat, so on a narrow screen they wrap with their
215
+ bars; under a system they go in its last row. A screen reader hears the
216
+ words after each bar's notes: "bar 2: note G four, hold, …, verse 1
217
+ “maz-ing”, verse 2 “grace that”".
218
+
199
219
  ## License
200
220
 
201
221
  MIT. Edwin is © MuseScore Limited, under the SIL Open Font License 1.1.
package/dist/html.js CHANGED
@@ -9,8 +9,9 @@ import { creditsText } from "./credits.js";
9
9
  import { escapeHtml } from "./escape.js";
10
10
  import { fontFaceCss } from "./font.js";
11
11
  import { isTacet as isTacetCell, layoutChart } from "./model.js";
12
- import { speakBar } from "./speech.js";
12
+ import { speakBar, speakVerses } from "./speech.js";
13
13
  import { FIT_STEPS, MIN_BAR, stylesheet } from "./styles.js";
14
+ import { beatLinesHtml, pitchHtml, sungBar, sungSlotHtml, verseNumbersHtml, verseWidth, } from "./verses-html.js";
14
15
  export function renderChart(text, options = {}) {
15
16
  return `${fontStyle(options)}<barmark-chart${elementAttributes(options)}><template shadowrootmode="open">${shadowContent(text, options)}</template></barmark-chart>`;
16
17
  }
@@ -115,12 +116,16 @@ function blockHtml(block, settings) {
115
116
  */
116
117
  function fitWidth(block) {
117
118
  const widths = [];
119
+ const widen = (bars) => {
120
+ for (const [i, w] of bars.entries())
121
+ widths[i] = Math.max(widths[i] ?? 0, w);
122
+ };
118
123
  for (const item of block.items) {
119
- if (item.kind !== "row")
120
- continue;
121
- item.cells.forEach((cell, i) => {
122
- widths[i] = Math.max(widths[i] ?? 0, cellWidth(cell));
123
- });
124
+ if (item.kind === "row")
125
+ widen(item.cells.map(cellWidth));
126
+ const verses = item.kind === "row" ? [...(item.verses ?? []), ...(item.lyrics ?? [])] : [];
127
+ for (const v of verses)
128
+ widen(v.cells.map(verseWidth));
124
129
  }
125
130
  const total = 5 + widths.reduce((a, b) => a + b, 0);
126
131
  return FIT_STEPS.find((step) => step >= total) ?? FIT_STEPS.at(-1) ?? 0;
@@ -210,22 +215,36 @@ function gutterHtml(row, settings, part) {
210
215
  function rowCells(row, numerals, ending) {
211
216
  let n = row.barNumber;
212
217
  const firstTacet = row.cells.findIndex(isTacetCell);
218
+ const firstBar = row.cells.findIndex((c) => c.kind === "slots");
213
219
  return row.cells.map((cell, i) => {
214
220
  const marks = repeatMarks(row, i) + (i === 0 ? ending : "");
221
+ const sung = {
222
+ pitches: row.sings === true,
223
+ words: [wordsOn(row.verses, i, "sung"), wordsOn(row.lyrics, i, "words")]
224
+ .filter(Boolean)
225
+ .join(", "),
226
+ bar: sungBar(row.verses, i),
227
+ first: i === firstBar,
228
+ beats: beatLinesHtml(row.lyrics, i, i === firstBar),
229
+ };
215
230
  const html = isTacetCell(cell)
216
231
  ? tacetHtml(cell, n, marks, i === firstTacet)
217
- : cellHtml(cell, n, marks, numerals);
232
+ : cellHtml(cell, n, marks, numerals, sung);
218
233
  if (n !== null && cell.kind === "slots")
219
234
  n++;
220
235
  return html;
221
236
  });
222
237
  }
238
+ /** The words on a row's cell, as a screen reader hears them after the bar's notes. */
239
+ const wordsOn = (verses = [], i, label) => speakVerses(verses.map((v) => ({ verse: v.verse, syllables: v.cells[i]?.flat() ?? [] })), verses.length > 1, label);
223
240
  const sr = (text) => (text ? `<span class="bm-sr">${text}</span>` : "");
224
241
  /** The row's class list. */
225
242
  function rowClasses(row, part) {
226
243
  const classes = ["bm-row"];
227
244
  if (part)
228
245
  classes.push("bm-part-row");
246
+ if (row.sings)
247
+ classes.push("bm-sings");
229
248
  if (row.cells.some(isTacetCell))
230
249
  classes.push("bm-tacet");
231
250
  if (row.ending)
@@ -268,14 +287,19 @@ function rowRepeatHtml(row) {
268
287
  return row.closes ? `<span class="bm-sr">repeat end, play 2 times</span>` : "";
269
288
  return repeatHtml(row.repeat).trim();
270
289
  }
271
- function cellHtml(cell, barNumber, marks, numerals) {
272
- const view = numerals && cell.kind === "slots" && cell.key ? { style: numerals, key: cell.key } : null;
273
- const spoken = `<span class="bm-sr">${escapeHtml(speakBar(barNumber, cell, view !== null))}</span>`;
290
+ function cellHtml(cell, barNumber, marks, numerals, sung) {
291
+ const numeral = numerals && cell.kind === "slots" && cell.key ? { style: numerals, key: cell.key } : null;
292
+ const said = [speakBar(barNumber, cell, numeral !== null), sung.words].filter(Boolean);
293
+ const spoken = `<span class="bm-sr">${escapeHtml(said.join(", "))}</span>`;
274
294
  if (cell.kind !== "slots")
275
295
  return betweenBarsHtml(cell, spoken, marks);
276
296
  const change = cell.tempoChange ? ` data-tempo-change="${cell.tempoChange}"` : "";
277
- const [body, end] = barBody(cell, view);
278
- return `<div class="bm-bar" part="bar"${cell.cut ? " data-cut" : ""}${change}${marks}>${spoken}<span class="bm-bar-body" aria-hidden="true">${body}</span>${end}</div>`;
297
+ const [body, end] = barBody(cell, { numeral, pitches: sung.pitches, sung: sung.bar });
298
+ const numbers = sung.first ? verseNumbersHtml(sung.bar) : "";
299
+ const drawn = `<span class="bm-bar-body" aria-hidden="true">${body}</span>`;
300
+ // A lyric row's lines go under the bar body, in a stack that wraps with the bar.
301
+ const stack = sung.beats ? `<span class="bm-bar-stack">${drawn}${sung.beats}</span>` : drawn;
302
+ return `<div class="bm-bar" part="bar"${cell.cut ? " data-cut" : ""}${change}${marks}>${spoken}${numbers}${stack}${end}</div>`;
279
303
  }
280
304
  /** What a bar shows: its meter, its slots (or `%`), and "cut"; then the words after its last slot. */
281
305
  function barBody(cell, view) {
@@ -349,6 +373,11 @@ const dataOf = (marks) => Object.entries(marks)
349
373
  .join("");
350
374
  /** A slot with its push mark before it, its accent or hit above it and its pedal mark below. */
351
375
  function slotHtml(slot, view) {
376
+ const html = markedSlotHtml(slot, view);
377
+ // On a singing line each note, a group's members each, has its syllables under it.
378
+ return view.sung && slot.kind !== "group" ? sungSlotHtml(html, view.sung) : html;
379
+ }
380
+ function markedSlotHtml(slot, view) {
352
381
  const body = unmarkedSlotHtml(slot, view);
353
382
  const marks = marksOf(slot);
354
383
  const { push, articulation, pedal } = marks;
@@ -392,12 +421,15 @@ function unmarkedSlotHtml(slot, view) {
392
421
  }
393
422
  }
394
423
  function chordHtml(record, view) {
424
+ if (record.kind === "notes" && view.pitches)
425
+ return pitchHtml(record);
395
426
  if (record.kind === "notes" || !record.parts)
396
427
  return notesHtml(record);
397
- const numeral = view ? typesetNumeral(record.parts, view.key, view.style) : null;
398
- if (!numeral)
428
+ const { numeral } = view;
429
+ const pieces = numeral ? typesetNumeral(record.parts, numeral.key, numeral.style) : null;
430
+ if (!pieces)
399
431
  return piecesHtml(typesetChord(record.parts), "");
400
- return piecesHtml(numeral, ` data-numeral="${view?.style}"`);
432
+ return piecesHtml(pieces, ` data-numeral="${numeral?.style}"`);
401
433
  }
402
434
  function piecesHtml(p, attributes) {
403
435
  const quality = p.quality ? `<span class="bm-quality">${p.quality}</span>` : "";
package/dist/model.js CHANGED
@@ -5,6 +5,7 @@
5
5
  * bar numbers and the slots `%` copies.
6
6
  */
7
7
  import { extractPart, parseChart, } from "barmark";
8
+ import { landings, rowVerses, singerOf, sungVerses } from "./verses.js";
8
9
  /** Whether a tutti bar's parts play different slots, as `%` or `/` on a tutti line can. */
9
10
  const playedApart = (b, line) => {
10
11
  const own = (b.parts ?? []).filter((p) => p?.line === line).map((p) => JSON.stringify(p?.slots));
@@ -210,28 +211,64 @@ function layoutLines(lines, occurrence, ctx) {
210
211
  let previous = null;
211
212
  for (const [i, l] of lines.entries()) {
212
213
  const system = systemOf(l);
213
- if (system !== null) {
214
- // A system is laid out once, at its first line.
215
- if (systemOf(lines[i - 1]) === system)
216
- continue;
217
- const item = layoutSystem(systemLines(lines, i), occurrence, previous, ctx);
218
- previous = lastOfSystem(item) ?? previous;
219
- items.push(item);
214
+ // A system is laid out once, at its first line.
215
+ if (system !== null && systemOf(lines[i - 1]) === system)
220
216
  continue;
221
- }
222
- if (ctx.errors.has(l.line)) {
223
- items.push(errorItem(l.line, ctx));
224
- continue;
225
- }
226
- const item = layoutLine(l, occurrence, previous, ctx);
227
- if (!item)
228
- continue;
229
- if (item.kind === "row")
230
- previous = lastSlots(item.cells) ?? previous;
231
- items.push(item);
217
+ const laid = system === null
218
+ ? layoutOne(l, occurrence, previous, ctx)
219
+ : layoutSystemAt(systemLines(lines, i), occurrence, previous, ctx);
220
+ previous = laid.previous;
221
+ items.push(...laid.items);
232
222
  }
233
223
  return items;
234
224
  }
225
+ /** A system, and the words under it that no line in it sings. */
226
+ function layoutSystemAt(lines, occurrence, previous, ctx) {
227
+ const item = layoutSystem(lines, occurrence, previous, ctx);
228
+ const lyrics = lyricRows(lines, occurrence, ctx);
229
+ // The words go under the system: in its last row that plays.
230
+ const last = item.rows.findLastIndex((p) => p.row.kind === "row" && !p.row.cells.some(isTacet));
231
+ const rows = item.rows.map((p, i) => i === last && p.row.kind === "row" && lyrics.length ? { ...p, row: { ...p.row, lyrics } } : p);
232
+ return { items: [{ ...item, rows }], previous: lastOfSystem(item) ?? previous };
233
+ }
234
+ /** A line outside a system: as written when it has an error; a bar line with the words under it. */
235
+ function layoutOne(l, occurrence, previous, ctx) {
236
+ if (ctx.errors.has(l.line))
237
+ return { items: [errorItem(l.line, ctx)], previous };
238
+ const item = layoutLine(l, occurrence, previous, ctx);
239
+ if (!item)
240
+ return { items: [], previous };
241
+ if (item.kind !== "row" || l.kind !== "bars")
242
+ return { items: [item], previous };
243
+ const lyrics = lyricRows([l], occurrence, ctx);
244
+ const row = lyrics.length ? { ...item, lyrics } : item;
245
+ return { items: [row], previous: lastSlots(item.cells) ?? previous };
246
+ }
247
+ /** The written bars a bar line draws, as played in the occurrence: a cut bar as written. */
248
+ function landingsOn(l, played, ctx) {
249
+ const written = ctx.bars.filter((b) => b.line === l.line);
250
+ let bar = 0;
251
+ const bars = l.cells.map((cell) => {
252
+ if (cell.kind !== "slots" && cell.kind !== "simile")
253
+ return undefined;
254
+ const i = bar++;
255
+ return played[i] ?? written[i];
256
+ });
257
+ return landings(bars, played.length ? played : written);
258
+ }
259
+ /**
260
+ * The words under a row or system that no line in it sings under its
261
+ * slots (barmark's grammar 7.7): a lyric row on no melody, spaced by beat.
262
+ */
263
+ function lyricRows(lines, occurrence, ctx) {
264
+ const drawn = lines.filter((l) => l.kind === "bars" && !ctx.errors.has(l.line));
265
+ const ref = drawn[0];
266
+ if (!ref)
267
+ return [];
268
+ const sung = new Set(drawn.flatMap((l) => singerOf(l, ctx.parse) ?? []));
269
+ const lands = landingsOn(ref, playedOn(ref.line, occurrence, ctx.bars), ctx);
270
+ return rowVerses(lands, sung);
271
+ }
235
272
  const systemOf = (l) => (l?.kind === "bars" || l?.kind === "invalid") && l.system !== undefined ? l.system : null;
236
273
  /** The lines of the system that starts at `i`. */
237
274
  function systemLines(lines, i) {
@@ -358,8 +395,18 @@ function layoutRow(l, played, previous, ctx) {
358
395
  repeat: l.repeat,
359
396
  ending: l.ending,
360
397
  cells,
398
+ ...singing(l, cells, played, ctx),
361
399
  };
362
400
  }
401
+ /** A singing line's fields: its note sets as pitch names, and the verses under its slots (barmark's grammar 7.7). */
402
+ function singing(l, cells, played, ctx) {
403
+ const singer = singerOf(l, ctx.parse);
404
+ if (singer === null)
405
+ return {};
406
+ const verses = sungVerses(cells, landingsOn(l, played, ctx), singer);
407
+ // A line with no words draws as any part line does.
408
+ return verses.length ? { sings: true, verses } : {};
409
+ }
363
410
  /** A cell as drawn: `%` becomes the bar it copies, its slots as the parser resolved them; `previous` lends only its meter. */
364
411
  function layoutCell(cell, played, previous) {
365
412
  if (cell.kind === "fermata")
package/dist/speech.d.ts CHANGED
@@ -2,10 +2,19 @@
2
2
  * Spoken names: how a screen reader reads a chart as music. Built from the
3
3
  * parsed structure, never from the symbol's text.
4
4
  */
5
- import { type ChordRecord, type Key, type Slot } from "barmark";
5
+ import { type ChordRecord, type Key, type Slot, type Syllable } from "barmark";
6
6
  import type { CellModel } from "./types.js";
7
7
  /** A chord's name; with a key, its degree first: "five, A seven". */
8
8
  export declare function speakChord(record: ChordRecord, key?: Key): string;
9
9
  export declare function speakSlot(slot: Slot, key?: Key): string;
10
10
  /** "bar 9: G, hold, D, hold"; a fermata is held until cued. With `numerals`, each chord's degree comes first. */
11
11
  export declare function speakBar(barNumber: number | null, cell: CellModel, numerals?: boolean): string;
12
+ /**
13
+ * The words on a bar, each verse's: `sung “maz-ing”` (or `words “…”` in a
14
+ * lyric row on no melody), or with several verses `verse 1 “maz-ing”,
15
+ * verse 2 “grace that”`. Empty when nothing is sung there.
16
+ */
17
+ export declare function speakVerses(verses: {
18
+ verse: number;
19
+ syllables: Syllable[];
20
+ }[], numbered: boolean, label: "sung" | "words"): string;
package/dist/speech.js CHANGED
@@ -168,3 +168,23 @@ export function speakBar(barNumber, cell, numerals = false) {
168
168
  const spoken = [...slots, ...words(cell.slots.length)].join(", ");
169
169
  return `bar${barNumber === null ? "" : ` ${barNumber}`}${meter}: ${spoken}${cut}`;
170
170
  }
171
+ /** A verse's syllables as words: "maz-ing"; elided syllables apart, a held one silent. */
172
+ function speakSyllables(syllables) {
173
+ let words = "";
174
+ for (const s of syllables)
175
+ if (s.kind === "sung")
176
+ words += `${s.parts.join(" ")}${s.continues ? "-" : " "}`;
177
+ return words.trim();
178
+ }
179
+ /**
180
+ * The words on a bar, each verse's: `sung “maz-ing”` (or `words “…”` in a
181
+ * lyric row on no melody), or with several verses `verse 1 “maz-ing”,
182
+ * verse 2 “grace that”`. Empty when nothing is sung there.
183
+ */
184
+ export function speakVerses(verses, numbered, label) {
185
+ return verses
186
+ .map((v) => ({ verse: v.verse, words: speakSyllables(v.syllables) }))
187
+ .filter((v) => v.words)
188
+ .map((v) => `${numbered ? `verse ${v.verse}` : label} “${v.words}”`)
189
+ .join(", ");
190
+ }
package/dist/styles.js CHANGED
@@ -18,6 +18,7 @@ const SIZES = {
18
18
  text: 0.9,
19
19
  noChord: 1.03,
20
20
  noteNames: 0.9,
21
+ syllables: 0.9,
21
22
  superscript: 0.84,
22
23
  octave: 0.79,
23
24
  tuplet: 0.84,
@@ -39,6 +40,14 @@ const ROW_GAP = 0.35;
39
40
  * nothing is painted over it and any paper shows through.
40
41
  */
41
42
  const BRACKET = { top: 0.6, hook: 0.3, line: 0.06 };
43
+ /**
44
+ * A singing line's lines, in chord sizes: the note's (the chart's line
45
+ * height), then each verse's under it. The verse numbers left of the first
46
+ * bar sit on the verses' lines, below the bar's top padding and the note's.
47
+ * A singing bar spaces its slots by beat: equal grid columns, each as wide
48
+ * as the widest note or syllable.
49
+ */
50
+ const SUNG = { note: 1.3, verse: 1.25, barTop: 0.65 };
42
51
  /** The single note's ring, in its own em: padding, and the border it spills past. */
43
52
  const RING = { y: 0.21, x: 0.5, border: 0.07 };
44
53
  const em = (n) => `${Number(n.toFixed(3))}em`;
@@ -148,6 +157,25 @@ const css = `
148
157
  .bm-bar[data-tempo-change]::before{content:"";position:absolute;inset:.15em 0 auto;border-top:.08em dashed var(--barmark-directive,var(--bm-muted))}
149
158
  .bm-bar[data-cut]{outline:.08em dashed var(--bm-muted);outline-offset:-.25em}
150
159
  .bm-cut{position:absolute;top:.3em;right:.45em;font:${em(SIZES.cut)}/1 ${SANS};color:var(--bm-muted)}
160
+ .bm-pitch{font-size:${em(SIZES.noteNames)}}
161
+ .bm-sung{display:inline-flex;flex-direction:column}
162
+ .bm-sung-note{height:${em(SUNG.note)}}
163
+ .bm-sings .bm-bar-body{display:grid;grid-auto-flow:column;grid-auto-columns:1fr;column-gap:.6em;align-items:baseline}
164
+ .bm-sings .bm-bar-body:has(>.bm-meter){grid-template-columns:auto}
165
+ .bm-sings .bm-meter{align-self:start}
166
+ .bm-under{height:${at(SIZES.syllables, SUNG.verse)}}
167
+ .bm-verse-numbers{position:absolute;right:100%;top:${em(SUNG.barTop + SUNG.note)};display:flex;flex-direction:column;align-items:flex-end;margin-right:.45em}
168
+ .bm-verse-numbers>.bm-verse{height:${at(SIZES.barNumber, SUNG.verse)};line-height:${at(SIZES.barNumber, SUNG.verse)}}
169
+ .bm-verse{font:italic ${em(SIZES.barNumber)} ${SANS};color:var(--bm-muted)}
170
+ .bm-bar-stack{display:flex;flex:1;flex-direction:column}
171
+ .bm-bar-stack>.bm-bar-body{flex:none}
172
+ .bm-beats{position:relative;display:flex;align-items:baseline;gap:.3em;margin-top:.15em}
173
+ .bm-beats>.bm-syllable-slot{flex:1 1 0;min-width:max-content}
174
+ .bm-beats>.bm-verse{position:absolute;right:calc(100% + ${at(SIZES.barNumber, 0.7 + 0.45)})}
175
+ :is(.bm-syllable-slot,.bm-under){display:flex;align-items:baseline;gap:.3em;font:italic ${em(SIZES.syllables)}/${SUNG.verse / SIZES.syllables} ${TEXT};color:var(--barmark-lyrics,var(--bm-ink));white-space:nowrap}
176
+ .bm-syllable{display:flex;flex:1;align-items:baseline}
177
+ .bm-hyphen{flex:1;text-align:center;padding-inline:.15em}
178
+ .bm-extender{flex:1;min-width:.8em;align-self:flex-end;margin-bottom:.3em;border-bottom:.07em solid currentColor}
151
179
  .bm-text{grid-column:1/-1;margin:.2em 0 .5em ${at(SIZES.text, GUTTER)};font-family:${TEXT};font-size:${em(SIZES.text)}}
152
180
  .bm-note{font-style:italic;color:var(--barmark-note,var(--bm-ink))}
153
181
  .bm-error{grid-column:1/-1;margin:.3em 0}
@@ -156,7 +184,7 @@ const css = `
156
184
  .bm-diagnostics .bm-error{color:var(--bm-accent)}
157
185
  @media (forced-colors:active){
158
186
  .bm-bar,.bm-row>div.bm-bar:first-of-type,.bm-row>div.bm-bar[data-repeat-start],.bm-row>div.bm-bar[data-repeat-end]{border-color:CanvasText}
159
- .bm-ring,.bm-tuplet::before,.bm-tuplet::after{border-color:CanvasText}
187
+ .bm-ring,.bm-tuplet::before,.bm-tuplet::after,.bm-extender{border-color:CanvasText}
160
188
  .bm-silence{background:CanvasText;forced-color-adjust:none}
161
189
  .bm-bar[data-cut]{outline-color:CanvasText}
162
190
  }
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ChartDiagnostic, Credits, DeclaredPart, Direction, Directives, Key, Meter, Repeat, Slot, Words } from "barmark";
1
+ import type { ChartDiagnostic, Credits, DeclaredPart, Direction, Directives, Key, Meter, Repeat, Slot, Syllable, Words } from "barmark";
2
2
  /** `part`: draw only the part with this label (barmark's grammar 7.4), as a single-part chart. */
3
3
  export type LayoutOptions = {
4
4
  rehearsalLetters?: boolean;
@@ -44,6 +44,16 @@ export type BlockItem = {
44
44
  /** The passes an ending plays on; null for any other row. */
45
45
  ending: number[] | null;
46
46
  cells: CellModel[];
47
+ /** Set on a line that sings words (barmark's grammar 7.7): its note sets draw as pitch names over them. */
48
+ sings?: true;
49
+ /** The verses sung on a singing line's bars, in verse order, each syllable under its slot. */
50
+ verses?: Verse[];
51
+ /**
52
+ * A lyric row on no melody under this row, or under the system whose
53
+ * last row this is (barmark's grammar 7.7): each verse's syllables in
54
+ * the bars they cover, spaced by beat.
55
+ */
56
+ lyrics?: Verse[];
47
57
  }
48
58
  /**
49
59
  * A system (barmark's grammar 7.4): a row per declared part, in score
@@ -69,6 +79,17 @@ export type BlockItem = {
69
79
  kind: "directive";
70
80
  set: Directives;
71
81
  } | ErrorItem;
82
+ /**
83
+ * One verse's syllables on a row's bars, as the parser places them: per
84
+ * cell (a cell that isn't a bar has none), per position, the syllables
85
+ * there. Under a singing line the positions are the bar's slots, a group's
86
+ * members each one; in a lyric row they are equal steps of the bar, as fine
87
+ * as its syllables need.
88
+ */
89
+ export type Verse = {
90
+ verse: number;
91
+ cells: Syllable[][][];
92
+ };
72
93
  export type ErrorItem = {
73
94
  kind: "error";
74
95
  line: number;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * The words as HTML (barmark's grammar 4.3, 7.7). A singing line draws
3
+ * each note as its pitch name with each verse's syllable under it, in one
4
+ * column per slot, so words stay under their notes however the bar is laid
5
+ * out or wrapped. A lyric row on no melody draws a line per verse in each
6
+ * bar it covers, spaced by beat, so its words wrap with their bars. Every
7
+ * syllable is escaped text.
8
+ */
9
+ import type { ChordRecord, Syllable } from "barmark";
10
+ import type { Verse } from "./types.js";
11
+ /** A note set on a singing line: its notes' names, without octaves (a divisi shows each). */
12
+ export declare function pitchHtml(record: ChordRecord): string;
13
+ /**
14
+ * A singing line's bar as it draws: each verse's syllables by place (a
15
+ * slot, a group's members each one), taken in slot order as the slots draw.
16
+ */
17
+ export type SungBar = {
18
+ verses: {
19
+ verse: number;
20
+ places: Syllable[][];
21
+ }[];
22
+ next: number;
23
+ };
24
+ /** A singing row's bar `i`; null when nothing is sung on the row. */
25
+ export declare function sungBar(verses: Verse[] | undefined, i: number): SungBar | null;
26
+ /** A slot of a singing line: its note, and under it a line per verse with what that verse sings there. */
27
+ export declare function sungSlotHtml(slot: string, bar: SungBar): string;
28
+ /** The verse numbers, "1.", "2.", left of a singing row's first bar, each on its verse's line; none for one verse. */
29
+ export declare const verseNumbersHtml: (bar: SungBar | null) => string;
30
+ /**
31
+ * A lyric row's words on no melody in the bar `i` they cover, a line per
32
+ * verse spaced by beat, each numbered at the row's first bar when there
33
+ * are several. Empty when the row has none.
34
+ */
35
+ export declare function beatLinesHtml(verses: Verse[] | undefined, i: number, first: boolean): string;
36
+ /** Roughly how many em a verse's bar needs: 0.55em a character, and the gap after each place. */
37
+ export declare const verseWidth: (places: Syllable[][]) => number;
@@ -0,0 +1,57 @@
1
+ import { splitPitch } from "./chord.js";
2
+ import { escapeHtml } from "./escape.js";
3
+ /** A note set on a singing line: its notes' names, without octaves (a divisi shows each). */
4
+ export function pitchHtml(record) {
5
+ const names = record.pitches.map((p) => splitPitch(p).name).join(" ");
6
+ return `<span class="bm-chord bm-pitch" part="chord">${escapeHtml(names)}</span>`;
7
+ }
8
+ /** Elided syllables (`the~end`) join under a tie, as a lead sheet prints them. */
9
+ const ELISION = "‿";
10
+ /** A syllable, with a hyphen after it when the word goes on; a held one is an extender line. */
11
+ function syllableHtml(s) {
12
+ if (s.kind === "held")
13
+ return `<span class="bm-extender"></span>`;
14
+ const hyphen = s.continues ? `<span class="bm-hyphen"> –</span>` : "";
15
+ return `<span class="bm-syllable">${escapeHtml(s.parts.join(ELISION))}${hyphen}</span>`;
16
+ }
17
+ const syllablesHtml = (syllables) => syllables.map(syllableHtml).join(" ");
18
+ /** A singing row's bar `i`; null when nothing is sung on the row. */
19
+ export function sungBar(verses, i) {
20
+ if (!verses?.length)
21
+ return null;
22
+ return { verses: verses.map((v) => ({ verse: v.verse, places: v.cells[i] ?? [] })), next: 0 };
23
+ }
24
+ /** A slot of a singing line: its note, and under it a line per verse with what that verse sings there. */
25
+ export function sungSlotHtml(slot, bar) {
26
+ const k = bar.next++;
27
+ const lines = bar.verses.map((v) => `<span class="bm-under" part="lyrics" data-verse="${v.verse}">${syllablesHtml(v.places[k] ?? [])}</span>`);
28
+ return `<span class="bm-sung"><span class="bm-sung-note">${slot}</span>${lines.join("")}</span>`;
29
+ }
30
+ /** The verse numbers, "1.", "2.", left of a singing row's first bar, each on its verse's line; none for one verse. */
31
+ export const verseNumbersHtml = (bar) => bar && bar.verses.length > 1
32
+ ? `<span class="bm-verse-numbers" aria-hidden="true">${bar.verses.map((v) => `<span class="bm-verse">${v.verse}.</span>`).join("")}</span>`
33
+ : "";
34
+ /** One step of a lyric row's bar. */
35
+ const stepHtml = (syllables) => `<span class="bm-syllable-slot">${syllablesHtml(syllables)}</span>`;
36
+ /**
37
+ * A lyric row's words on no melody in the bar `i` they cover, a line per
38
+ * verse spaced by beat, each numbered at the row's first bar when there
39
+ * are several. Empty when the row has none.
40
+ */
41
+ export function beatLinesHtml(verses, i, first) {
42
+ const numbered = (verses?.length ?? 0) > 1;
43
+ return (verses ?? [])
44
+ .map((v) => {
45
+ const number = numbered && first ? `<span class="bm-verse">${v.verse}.</span>` : "";
46
+ const steps = (v.cells[i] ?? []).map(stepHtml).join("");
47
+ return `<span class="bm-beats" part="lyrics" data-verse="${v.verse}" aria-hidden="true">${number}${steps}</span>`;
48
+ })
49
+ .join("");
50
+ }
51
+ /** Roughly how many em a verse's bar needs: 0.55em a character, and the gap after each place. */
52
+ export const verseWidth = (places) => places.length
53
+ ? 1.5 +
54
+ places.reduce((w, p) => w +
55
+ 0.6 +
56
+ p.reduce((c, s) => c + 0.55 * (s.kind === "sung" ? s.parts.join(" ").length + 1 : 1), 0), 0)
57
+ : 0;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Where the words go (barmark's grammar 4.3, 7.7). The parser places every
3
+ * syllable: a verse's syllables on a played bar, each at its onset. This
4
+ * module only sorts them under the written bars a row draws: under a
5
+ * singing line's slots, or spaced by beat in a lyric row on no melody.
6
+ */
7
+ import { type ChartParse, type Line, type PlayedBar, type PlayedLyrics } from "barmark";
8
+ import type { CellModel, Verse } from "./types.js";
9
+ type BarsLine = Extract<Line, {
10
+ kind: "bars";
11
+ }>;
12
+ /** A bar a row draws, as played, and every verse sung on its written bar; null for a cell that isn't a bar. */
13
+ export type Landing = {
14
+ bar: PlayedBar;
15
+ lyrics: PlayedLyrics[];
16
+ } | null;
17
+ /**
18
+ * Whose words a row sings under its slots: the label of the part whose
19
+ * singing line it is ("" in a chart that sings with no part declared), or
20
+ * null when the line doesn't sing (barmark's grammar 7.7).
21
+ */
22
+ export declare function singerOf(l: BarsLine, parse: ChartParse): string | null;
23
+ /**
24
+ * What lands on each cell a row draws: its bar as played, with the verses
25
+ * of every pass the occurrence plays over that written bar, each part's
26
+ * verse once (a jump's pass back sings pass 1's words again).
27
+ */
28
+ export declare function landings(bars: (PlayedBar | undefined)[], passes: PlayedBar[]): Landing[];
29
+ /** The verses a singing line's part sings over its bars, each syllable under its slot. */
30
+ export declare function sungVerses(cells: CellModel[], lands: Landing[], key: string): Verse[];
31
+ /** The verses no row sings under its slots: the chart's words, or a singer's with no melody here, by beat. */
32
+ export declare function rowVerses(lands: Landing[], sung: Set<string>): Verse[];
33
+ export {};
package/dist/verses.js ADDED
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Where the words go (barmark's grammar 4.3, 7.7). The parser places every
3
+ * syllable: a verse's syllables on a played bar, each at its onset. This
4
+ * module only sorts them under the written bars a row draws: under a
5
+ * singing line's slots, or spaced by beat in a lyric row on no melody.
6
+ */
7
+ import { memberShares, } from "barmark";
8
+ /** Within a rounding error: onsets are sums of fractions of a bar. */
9
+ const near = (a, b) => Math.abs(a - b) < 1e-9;
10
+ const same = (a, b) => a.toLowerCase() === b.toLowerCase();
11
+ const barLength = (m) => m.n / m.d;
12
+ /** Whose words a verse is: its part's label, or "" for the chart's. */
13
+ const keyOf = (v) => v.part ?? "";
14
+ /**
15
+ * Whose words a row sings under its slots: the label of the part whose
16
+ * singing line it is ("" in a chart that sings with no part declared), or
17
+ * null when the line doesn't sing (barmark's grammar 7.7).
18
+ */
19
+ export function singerOf(l, parse) {
20
+ const { parts } = parse;
21
+ if (!parts.length)
22
+ return l.part === undefined && parse.bars.some(onNotes) ? "" : null;
23
+ const p = l.part === undefined
24
+ ? parts.length === 1
25
+ ? parts[0]
26
+ : undefined
27
+ : parts.find((x) => same(x.label ?? "", l.part ?? ""));
28
+ if (p?.sings === undefined)
29
+ return null;
30
+ // A bare line plays as the part's first line (7.6).
31
+ const name = l.lineName ?? p.lines?.[0] ?? "";
32
+ return same(name, p.sings) ? (p.label ?? "") : null;
33
+ }
34
+ /** Whether a chart with no parts sings: its verse 1 sits on notes. */
35
+ const onNotes = (b) => (b.lyrics ?? []).some((v) => v.syllables.some((s) => s.note));
36
+ /**
37
+ * What lands on each cell a row draws: its bar as played, with the verses
38
+ * of every pass the occurrence plays over that written bar, each part's
39
+ * verse once (a jump's pass back sings pass 1's words again).
40
+ */
41
+ export function landings(bars, passes) {
42
+ return bars.map((bar) => {
43
+ if (!bar)
44
+ return null;
45
+ const seen = new Set();
46
+ const lyrics = passes
47
+ .filter((p) => p.written === bar.written)
48
+ .flatMap((p) => p.lyrics ?? [])
49
+ .filter((v) => {
50
+ const key = `${keyOf(v)}\u0000${v.verse}`;
51
+ if (seen.has(key))
52
+ return false;
53
+ seen.add(key);
54
+ return true;
55
+ });
56
+ return { bar, lyrics };
57
+ });
58
+ }
59
+ /** One part's verse in a bar, or none. */
60
+ const verseIn = (landing, key, verse) => landing?.lyrics.find((v) => keyOf(v) === key && v.verse === verse)?.syllables ?? [];
61
+ /** The verses some cell sings for `key`, or for anyone not in `skip`, in verse order. */
62
+ function versesOf(cells, wanted) {
63
+ const found = new Map();
64
+ for (const v of cells.flatMap((c) => c?.lyrics ?? [])) {
65
+ if (!wanted(keyOf(v)) || !v.syllables.length)
66
+ continue;
67
+ found.set(`${keyOf(v)}\u0000${v.verse}`, [keyOf(v), v.verse]);
68
+ }
69
+ return [...found.values()].sort((a, b) => a[1] - b[1]);
70
+ }
71
+ /** Where each of a bar's simple slots starts, as a fraction of the bar (grammar 2.1, 2.2). */
72
+ function slotStarts(slots) {
73
+ const starts = [];
74
+ slots.forEach((s, i) => {
75
+ const members = s.kind === "group" ? s.members : [s];
76
+ let at = i / slots.length;
77
+ for (const share of memberShares(members)) {
78
+ starts.push(at);
79
+ at += share / slots.length;
80
+ }
81
+ });
82
+ return starts;
83
+ }
84
+ /** A verse's syllables in a bar under its slots: each on the slot sounding at its onset. */
85
+ function underSlots(cell, landing, key, verse) {
86
+ if (cell.kind !== "slots" || !landing)
87
+ return [];
88
+ const sung = verseIn(landing, key, verse);
89
+ const starts = slotStarts(cell.slots);
90
+ // A `%` drawn as itself has no slots to sit under: its syllables sit in a row.
91
+ if (!starts.length)
92
+ return sung.length ? [sung.map((s) => s.syllable)] : [];
93
+ const out = starts.map(() => []);
94
+ const length = barLength(landing.bar.meter);
95
+ for (const s of sung) {
96
+ const i = starts.findLastIndex((at) => at < s.at / length || near(at, s.at / length));
97
+ out[Math.max(0, i)]?.push(s.syllable);
98
+ }
99
+ return out;
100
+ }
101
+ /** The verses a singing line's part sings over its bars, each syllable under its slot. */
102
+ export function sungVerses(cells, lands, key) {
103
+ return versesOf(lands, (k) => k === key).map(([, verse]) => ({
104
+ verse,
105
+ cells: cells.map((c, i) => underSlots(c, lands[i] ?? null, key, verse)),
106
+ }));
107
+ }
108
+ /** The finest steps a bar is cut into: enough for a triplet of sixteenth triplets. */
109
+ const MAX_STEPS = 96;
110
+ /** The fewest equal steps of a bar that every onset falls on. */
111
+ function stepsFor(fractions) {
112
+ for (let n = 1; n < MAX_STEPS; n++)
113
+ if (fractions.every((f) => near(f * n, Math.round(f * n))))
114
+ return n;
115
+ return MAX_STEPS;
116
+ }
117
+ /** A verse's syllables in a bar on its beats: equal steps, each syllable at its onset's. */
118
+ function onBeats(landing, key, verse) {
119
+ if (!landing)
120
+ return [];
121
+ const sung = verseIn(landing, key, verse);
122
+ const length = barLength(landing.bar.meter);
123
+ const steps = stepsFor(sung.map((s) => s.at / length));
124
+ const out = Array.from({ length: steps }, () => []);
125
+ for (const s of sung)
126
+ out[Math.min(steps - 1, Math.round((s.at / length) * steps))]?.push(s.syllable);
127
+ return out;
128
+ }
129
+ /** The verses no row sings under its slots: the chart's words, or a singer's with no melody here, by beat. */
130
+ export function rowVerses(lands, sung) {
131
+ return versesOf(lands, (k) => !sung.has(k)).map(([key, verse]) => ({
132
+ verse,
133
+ cells: lands.map((l) => onBeats(l, key, verse)),
134
+ }));
135
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barmark/render",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "Render barmark chord charts as a clean, accessible bar grid: HTML with Declarative Shadow DOM, or the <barmark-chart> web component.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -47,7 +47,7 @@
47
47
  "url": "https://github.com/kevinpeckham/barmark/issues"
48
48
  },
49
49
  "dependencies": {
50
- "barmark": "^0.16.0"
50
+ "barmark": "^0.17.0"
51
51
  },
52
52
  "scripts": {
53
53
  "build": "rm -rf dist && tsc -p tsconfig.build.json",