@barmark/render
Draws the bar grid, as HTML from renderChart or as the <barmark-chart> element.
When to use it: Showing a chart on a page. Light, with no engraver.
- Version
0.26.0- Install
npm install @barmark/render
Example
The example song, drawn by the renderer on this page.
example-song.chart.md
# Example Song
key: D, time: 4/4, tempo: 145
## Verse
|: D | A | Bm | G :|
| Em | / | A | A7 |
## Chorus
| G / D / | A | Bm / / A | G |
Rendered
Renders Barmark chord charts as a clean, evenly spaced bar grid that looks the same on any page, follows light and dark mode, scales with the reader's text size, and reads to a screen reader as music.
Install
npm install @barmark/render
As HTML (no browser JavaScript needed)
import { renderChart } from "@barmark/render";
const html = renderChart(chartText, { fontUrl: "/fonts/Edwin-Roman.otf" });
renderChart returns a <barmark-chart> element holding a declarative
shadow root: put it in any page, server-rendered or static, and it shows
the chart with no script. The shadow root keeps the page's CSS out, so a
blog's resets and fonts cannot rearrange the bars. Browsers attach a
declarative shadow root only while parsing HTML: to insert the markup from
script, use setHTMLUnsafe (or the element below), not innerHTML.
renderChartUnwrapped(text, options) returns the same markup without the
shadow root (classes prefixed bm-), for pages that include stylesheet
themselves.
As a web component
Without a bundler, the page's import map must also map @barmark/fretboard
(beside barmark): the package imports it whether or not diagrams are on,
so without it neither the element nor renderChart loads.
<script type="module">import "@barmark/render/element";</script>
<barmark-chart src="/charts/peaceful-dreams.chart.md"></barmark-chart>
<barmark-chart theme="dark" rehearsal-letters>
<script type="text/barmark">
## Verse
| D | A | Bm | G |
</script>
</barmark-chart>
The element renders from its text property, its src URL, or a child
<script type="text/barmark"> (indentation is removed), and renders again
whenever one changes; it fetches a src once, and renders once however
many properties and attributes change together. A chart rendered on the
server by renderChart keeps its music until it has a source of its own,
and still follows theme, bar-numbers, wrap and columns (its bar
numbers hide and show again, but stay written or played as they were
drawn: changing between the two needs the chart's text); a change to
numerals, rehearsal-letters, melody, show-part, chord-diagrams,
diagram-hand or chord-notes needs the chart's text, so it waits for a
source.
For a live editor, set text:
const chart = document.querySelector("barmark-chart");
editor.addEventListener("input", () => { chart.text = editor.value; });
While text is a string it wins over src and the script child, and no
fetch happens; set it to null to go back to them. Any string is safe:
the chart escapes it, and problems show as diagnostics, never errors.
There is no text attribute: chart text belongs in the property or the
script child.
The element registers the font itself, from the fonts/ folder beside its
own module. If your bundler moves that file, register it yourself before
the element loads, with <style data-barmark-font> holding
fontFaceCss(yourUrl); the element then leaves it alone.
Options and attributes
renderChart option |
Element attribute (property) | Default | |
|---|---|---|---|
theme |
theme (theme) |
"auto" |
"auto" follows the system; "light" or "dark" force one |
barNumbers |
bar-numbers="off" or "played" (barNumbers) |
true |
bar numbers at the start of each row: true the bars counted down the page, a replayed section counted as written again; false or "off" none; "played" numbers in play order (below). Any other attribute value is the default |
rehearsalLetters |
rehearsal-letters (rehearsalLetters) |
false |
boxed A, B, C… on each section as played; none when the chart writes a rehearsal mark of its own (below) |
numerals |
numerals (numerals) |
none | "nashville" (6m⁷) or "roman" (vi⁷): each chord as its degree in its bar's key |
wrap |
wrap="off" (wrap) |
true |
wrap a section's rows when it is wider than the chart's box, and a system as a whole, every part breaking at the same bar (see What it draws); off, rows and systems keep their columns and the chart overflows, for its page to scroll |
melody |
melody="off" (melody) |
true |
false leaves a voice part's singing line out of the grid; its words stay, drawn by beat under the chords. The melody is left out only beside a line that draws its bars; a melody on its own stays, and so does a line with a problem or one that writes chord names, N.C. or ?. A melisma's held place is not drawn where the melody is left out, so the words take the space they would with no melody written. In a one-part lead sheet the chord line then draws as a plain row with no label. @barmark/score's score view still shows it |
columns |
columns="row" (columns) |
"shared" |
"shared": a section's rows share column tracks, so bar lines line up down the section; "row": each row sizes its own bars (below). Any other value is "shared" |
part |
show-part (showPart) |
none | draw only the part with this label, as one player reads it: tutti bars, its own bars, and "tacet" where it rests. The attribute isn't part, which HTML keeps for ::part() styling |
chordDiagrams |
chord-diagrams="guitar" (chordDiagrams) |
none | "guitar": a strip of guitar chord diagrams after the header, one for each distinct chord (below). Any other value is off |
diagramHand |
diagram-hand="left" (diagramHand) |
"right" |
"left" mirrors the diagrams for a left hand: the low string on the right. Any other value is right |
chordNotes |
chord-notes (chordNotes) |
false |
show a chord symbol's notes when it is pointed at, or when its bar has focus or is tapped (below) |
diagnostics |
false |
list the chart's errors and warnings below it | |
fontUrl |
none | emit Edwin's @font-face rule before the chart |
|
(the text argument) |
(text) |
null |
the chart to draw; wins over src and the script child |
Rehearsal letters
rehearsalLetters letters each section occurrence A, B, C… in play order,
for a chart that marks no places itself. When any bar in the chart carries
its own rehearsal mark (@A, Barmark's grammar 2.5), it adds no letters at
all: the chart's marks stand alone, so no heading is marked twice, and a
section the chart leaves unmarked stays unmarked.
Columns
By default a section's rows share their columns: bar lines line up down
the section, and the widest bar in a column sets that column's width in
every row, so one busy bar (| G7 | G7 C6/G Bbmaj7/F Fmaj9 |) widens
column two all the way down. With columns: "row" (columns="row" on the
element) each row sizes its own bars to their content instead: the busy
bar widens only its own row, and bar lines no longer line up between rows.
Every row still starts at the first bar line, and a system's parts keep
their bar lines together. Only the layout changes: the music, the markup
inside each bar and what a screen reader hears are the same. A section
wraps on a narrow screen as before, once its widest row no longer fits.
Played bar numbers
With barNumbers: "played" (bar-numbers="played" on the element) each
row shows where its first bar falls in play order, for lining a chart up
with a player or a recording that counts bars as they are played. The
numbers come from the parser's play order (parseChart(text).bars); the
grid expands nothing itself. A row played once shows one number. A row
played more than once where it is drawn (inside |: :|, under x N, or
where a D.S. or D.C. returns) shows its first bar's number on each pass,
1 · 5, up to three; four or more show the first and the last, 9 … 37.
A row no pass plays shows none. A pickup's first pass is 0 and never
shown, as in the default; a later pass of its row (inside |: :|,
or where a D.C. returns) shows its number. An open repeat (x ...) is
counted once, since its passes are unknown: every number after it is
approximate and shows as ~11. A screen reader hears each bar's own
numbers: "bar 1 and bar 5", "bars 1, 5 and 9", "bars 9 to 37, 8 times",
"about bar 11", "about bars 12, 14 and 16". The gutter widens to the
longest numbers on one line, and a section wraps sooner to make room for
it. In a chart without part labels, headings, text lines and directives
start where the bars do, as in the default; with part labels they
keep their place over the label column, as in the default. Played markup
carries the few rules that do this, so a page that includes stylesheet
needs nothing more. The bars counted down the page stay the default
because they are what a printed chart and a band use; the model gives played numbers as
playedNumbers on each row and bar ({ bar, approximate }, a pass each)
only in this mode.
A played number is PlayedBar.index, less one when parse.pickup (the
pickup's first pass is 0). @barmark/sync's timing map numbers bars by
PlayedBar.index, so its pickup is map bar 1: a host showing barAt().bar
beside a played chart subtracts one on a pickup chart to match. A chart
rendered on the server is drawn in one mode: bar-numbers can hide its
numbers and show them again, but changing between written and played
renders it again from its text.
Numerals
With numerals, each chord shows as its degree in the key its bar is
played in, so a modulation renumbers from its next bar. Degrees count
from the key's tonic against the major scale, in every mode: in key: Am, Am is 1m and C is ♭3. Nashville keeps the chord's quality and
superscript after the number (Bm7 in D is 6m⁷, D/F♯ is 1/3); Roman
shows minor and diminished in lower case (ii, vii°, ♭VII). Both raise a
power chord's 5 into the superscript (D5 in D is 1⁵ and I⁵, never 15).
A bar with no key, note sets, N.C., ? and -- look as they always
do, and a screen reader hears both: "bar 5: five, A seven". Numbered
chords carry data-numeral. It's a view: the music and bar numbers don't change.
Chord diagrams
With chordDiagrams: "guitar" (or chord-diagrams="guitar" on the
element), a strip of guitar chord diagrams follows the header: one for
each distinct chord, in the order the chart first writes it, wrapping to
as many rows as the width needs. The shapes come from
@barmark/fretboard
(see its README for what it cannot find); this package draws them and
copies none. Under each diagram is the chord's name in letters; with
numerals on, its degree sits beneath, in the key where the chord first
appears. A chord with no shape in standard tuning says so instead of a
drawing.
renderChart("key: C\n| C | Am7 | {E4 A4 C#5 E5} |", { chordDiagrams: "guitar", numerals: "nashville" });
// <barmark-chart numerals="nashville" chord-diagrams="guitar"><template shadowrootmode="open">…
// diagram 1: aria-label "one, C: muted, 3, 2, open, 1, open"
// diagram 3: aria-label "notes E four, A four, C sharp five, E five: muted, muted, 14, 14, 14, 12"
A voicing the chart writes is shown exactly when it fits standard tuning
(a note set is a diagram of those notes); when it does not, the diagram
shows the chord's own shape and a note saying the written voicing does
not fit. diagramHand: "left" writes diagram-hand="left" on the
element and mirrors the strings only: the low string is drawn on the
right and a fret number stays at the left. Each diagram has a label that
speaks the strings, low to high ("muted, 3, 2, open, 1, open"). Style the
strip through ::part(diagrams) (the strip) and ::part(diagram) (one
chord's diagram, its name and note). A chord's name in the strip carries
the same chord part as the names in the grid, so one ::part(chord) rule
styles both (a note set's name in the strip does not). The dots and
strings follow the chart's colours, light, dark and print, and take the
system text colour in forced-colours mode. It is a view: the chart's music and bar numbers do not
change, and with the option off a chart renders as it always did.
The strip adds about 6.6 KB gzipped to the element (most of it the shape
finder). On a chart rendered on the server, setting chord-diagrams later
needs the chart's text, as the web component section says for numerals.
Chord notes
With chordNotes: true (chord-notes on the element), a reader can see
the notes of a chord symbol:
- Pointing at a chord shows its notes in a small label under it. The label stays while the pointer is on the label itself.
- The keyboard reaches each bar that holds a chord symbol: it is one tab stop, and while it has focus a box under the bar shows every chord there with its notes, in order. Tab onward and they go.
- A tap on a bar focuses it, so it shows the same box; a tap elsewhere hides it.
- Escape hides what is shown and leaves focus where it is, on
<barmark-chart>and<barmark-score>only: it takes their script, so the static HTML fromrenderChartcannot do it. Everything else needs no script.
The notes are the parser's reading of the symbol (ChordRecord.pitches),
as it spells and orders them: the root first, then the chord's tones, and
a slash chord's bass last when it is not one of them (C/E is C E G, C/B♭ is
C E G B♭). The renderer adds and reorders nothing, so C7alt shows its shell,
C E B♭, and C11 has no third, as the parser reads them. A transposed chart
(transpose it with transposeChart before rendering) shows the transposed
chord's notes; in a numeral view the notes are still the chord's real
notes. Holds, % drawn as its sign, N.C., stops, ?, a slot that can't
be read and note sets (which show their notes already) have none; a bar of
only those is not a tab stop. Labels are never printed. Style them through
::part(chord-notes). The feature adds about 1.2 KB gzipped to the element.
renderChart("| C7 / F/A G |", { chordNotes: true });
// <barmark-chart chord-notes>… the bar: tabindex="0" role="group" aria-describedby="…"
// its description: "C seven: C, E, G, B flat; F over A: F, A, C; G: G, B, D"
Spoken names
speakChord(record, key?) gives a chord the name a screen reader hears
in the chart, for other tools that want to say a chord aloud:
import { parseChord } from "barmark";
import { speakChord } from "@barmark/render";
const r = parseChord("Cø");
if (r.ok) speakChord(r.value); // "C minor seven flat five"
With a key, it speaks the degree too, as the numerals view does.
Styling on purpose
Set these custom properties on the element or anything above it:
| Property | What it sets |
|---|---|
--barmark-font |
the chords' typeface |
--barmark-text-font |
lyrics and ### note lines (default Georgia) |
--barmark-label-font |
the meta line, mid-chart changes, headings and diagnostics (default the system sans) |
--barmark-size |
the chord size everything scales from (default 1rem) |
--barmark-ink |
chords and text |
--barmark-paper |
the background |
--barmark-muted |
holds, bar numbers, braces, meta lines |
--barmark-rule |
bar lines |
--barmark-accent |
errors, and slots that can't be read |
--barmark-directive |
mid-chart changes (default the muted colour) |
--barmark-note |
### note lines (default the ink colour) |
--barmark-lyrics |
syllables under a voice line and in lyric rows (default the ink colour) |
barmark-chart {
--barmark-size: 0.9rem;
--barmark-paper: transparent;
--barmark-text-font: "Noto Serif", Georgia, serif;
}
barmark-chart::part(heading) {
color: teal;
}
::part() names: title, meta, heading, bar, chord, text,
bar-number, rehearsal, diagrams, diagram, chord-notes (a chord's
notes, and a focused bar's box of them), lyrics (each verse's line of words, inside
its bar), unread (a slot that can't be read, drawn as written). These, the custom properties and the
attributes are the public surface: changing them is a breaking change.
Light, dark and print
Both palettes meet WCAG AA contrast (4.5:1 for text). theme="auto"
follows prefers-color-scheme; a page with its own dark-mode switch sets
theme or the colour properties. Printing always uses the light palette,
and Windows High Contrast keeps bar lines and marks visible.
Accessibility
- Everything is sized in em from
--barmark-size, so text size and zoom scale the chart; a section too wide for its container wraps its rows instead of scrolling sideways, and a system of several parts wraps as a whole. The markup stays part by part, so a screen reader still reads each part's bars in order and Tab (withchordNotes) follows that order, while the eye reads a wrapped system line by line. A system whose label column and widest bar together are wider than the box, or a system in a browser older than Chrome 125, Firefox 118 or Safari 16, makes the chart, on screen, a region that scrolls sideways. Chromium and Firefox let the keyboard reach such a region by default (Tab stops on it, then the arrow keys scroll it); Safari does not. The chart adds notabindexof its own, since that would change its markup. - Every bar has a spoken name built from the parsed chart: "bar 9: A seven sus four, hold", "triplet: D, E, F sharp minor", "note E", "C over E".
- With
chordNoteson, every bar that holds a chord symbol is a tab stop of its own: a 60-bar chart has 60, and there is no way to skip past them. With it off there are none. Each such bar is exposed as a group with no name of its own (role="group",tabindex="0"), described (aria-describedby) by its chords' notes in words: "C seven: C, E, G, B flat; F over A: F, A, C". The description's element ishidden, so it is not in the reading order. The chart's text, its order and the bar's spoken sentence are the same as with the option off, and the labels drawn on screen are hidden from assistive technology, as the drawn chords are. What a screen reader announces when a bar takes focus (the group, its contents, the description) depends on the screen reader, and this has not been checked with a real one. Escape dismisses the notes in the elements (WCAG 1.4.13); the static HTML cannot. Sections are labelled by their headings, and repeats are spoken ("play 2 times").
The font
Edwin, MuseScore's text font for music, ships unmodified in
@barmark/render/fonts/Edwin-Roman.otf with its SIL Open Font License
(fonts/OFL.txt). It is registered under the family name "barmark
Edwin". Serve the file and pass its URL as fontUrl, or call
fontFaceCss(url) once per page; without it the chart uses Georgia or
another serif.
What it draws
Bar numbers sit in a gutter at the left edge; section headings, lyrics,
note lines and mid-chart changes start at the first bar line. Chords start
at the left of their share of the bar. A change written mid-chart names
itself: time: 3/4, tempo: 120 draws as "→ Time 3/4 · Tempo ♩ = 120".
A tempo shows the beat tempo: counts (Barmark's grammar 2.1), from the
time: in force at the bar it applies to (a time: on the next line
counts): the bottom number in a simple meter
(♩ in 4/4, 𝅗𝅥 in 2/2, ♪ in 3/8) and the dotted quarter in 6/8, 9/8 and
12/8, so 6/8 with tempo: 40 shows "♩. = 40" and a rit. to 90 there
"rit. to ♩. = 90". A one-bar meter never changes it (grammar 2.4). The
note is spoken by name ("dotted quarter note"), and the model gives it as
tempoBeat on the chart (for the header) and on each directive item.
At the default size chords are 16px and no label is under 12px.
Chords appear in their canonical form whatever the author typed (Cø
shows as Cm⁷(♭5)). % is drawn as the bar it copies (as the % sign
where no pass plays it); a replayed section
is drawn in full, and its own lyrics replace the written section's. Bar
numbers count the bars down the page, each once, as a band counts them: a
repeat, an ending or a jump doesn't move them, and a replayed section
counts as written again where its heading is, so its rows go on from the
count and the numbers after it never repeat (the parser's pageShift). A pickup is bar 0: its
row shows no number in the gutter (barNumber is 0 in the model) and
is spoken as "pickup", and the bars after it are 1, 2, and so on; drawn again by a replay, the pickup's row is a bar like any other, numbered and spoken with its place in the count. A line with an error shows as written, underlined,
with its message, unless its only errors are slots that can't be read
(CM7): that line draws as usual, each such slot as written in the
error colour with its message as a tooltip and in the bar's spoken name. layoutChart(text) returns all of this as data, if
you want to draw it yourself, and typesetChord(parts) with glyphs
gives a chord's pieces as the page sets them (root and quality, the
raised extension, the bass after a slash, ♯ and ♭), and
typesetNumeral(parts, key, style) its Nashville or Roman numeral, so a
PDF or canvas drawer matches it. A section block's bars counts its
widest row's bars (0 when it has none), where columns also counts its
fermata, direction and rehearsal cells.
A chart of several parts (Barmark's grammar 7.4) draws each system as stacked rows, one per part in score order, with the part's label in the gutter and one bar number on the top row. A part tacet in a system gets an empty row that says "tacet" in its first bar, so every system keeps its shape; a tutti line is one row. The header lists the parts' names, and each part row is announced by name to a screen reader.
A system too wide for its box wraps as a whole: every part's line breaks at the same bar, so each line of the system holds the same bars of every part, one part under another, with their bar lines together and a wider gap before the next line than between parts. On a phone a two-part, four-bar system that fits two bars a line reads:
1 Gtr | Am7 | D7 |
Pno | Cmaj9 | Dm11 |
3 Gtr | Gm7 | C7 |
Pno | Ebmaj7 | Db9 |
Every later line starts as the first does: with the part labels, and with
the number of its own first bar in the same gutter and type (in the drawing
above, Gtr and Pno again, and 3). The number is the one the bar is
spoken by, so a line after a jump or a repeat shows that bar's own number,
and with barNumbers: "played" its played numbers (3 · 7 · 11). With bar
numbers off, the labels still repeat and no line has a number. A plain row
wraps the same way, by the same arithmetic: as few lines as its widest bar
allows at the box's width (counted in whole em of the box), the bars shared
evenly among them (four bars on two lines are 2 + 2, eight on three are 3,
3, 2), each later line starting with its first bar's number. Repeat signs,
endings, words, lyrics and chord notes stay with their bars; a fermata,
segno or jump after a row's last bar stays beside it on the last bar's
line where that line has room; else they flow after it as cells like any
other, and the last bar moves to a new line only where that puts it and
them on one line (whenever they are fewer than a line's bars, they share
the last bar's line). A repeat count follows the last cell on its line, or
takes a line of its own where even one bar and the count are wider than
the box. A
system wraps by the same fit steps and
width estimates as a section (with shared columns, estimated from its whole
section, since an unwrapped system shares the section's columns; with
columns: "row", from its own parts, which still share theirs), then puts
as many bars on a line as its widest bar leaves room for. While a system
fits it keeps the section's columns, lined up with the plain rows'. The
repeated labels and numbers are drawn, not written: a screen reader hears
each bar's number once, with the bar, as before.
Known limits of that arithmetic: one busy bar sets the count for the
whole system, so a twelve-bar system with one four-chord bar draws one bar
a line on a 320 to 400 px phone where three would fit; the estimate runs a
little high (a system of two-chord bars can go to one bar a line where two
would fit); a plain row's count, too, comes from its widest bar, so a row
with one busy bar can take a line more than its bars need, and a row with
a repeat count keeps room for it on every line; the bars of a wrapped
system or row are shared evenly, so two parts of four wide bars that once
broke 3 + 1 now break 2 + 2; a plain
row counts from the box's width to the em while a system counts from its
coarser fit step, so beside a plain row a system can break sooner, at other
bars. Where one bar beside the gutter is already wider than the box (very
large text), a plain row's first number takes a line of its own above its
bars, and its later lines show no number. A bar is never
drawn narrower than its chords: where the label column and the widest bar
together are wider than the box (Shenandoah's Vox.Mel label on a 320 to
360 px phone, though its widest bar alone fits), the chart scrolls sideways
inside its own box, and no chord runs into the next bar. Where a single
chord is wider than the box (very large text on a phone), its bar is too,
and the page scrolls by the difference, where flexible wrapping let the
chord run past its bar line. Text, notes, directives and unreadable rows
beside or inside a system still wrap at the chart's box. A printed chart
wraps its systems the same way. With wrap: false nothing wraps. Wrapping
needs subgrid, mod() and round() (Chrome 125, Firefox 118, Safari
16); an older browser keeps the system's columns and scrolls the chart
sideways instead, and wraps a plain row where its bars run out of room,
with no number on its later lines. ::part(bar-number) styles the first
line's number only: a later line's number is generated text and takes the
stylesheet's own size and colour. Only the layout changes: the markup stays part by part
(it gains attributes the stylesheet reads), and what a screen reader hears
is the same.
A voice line (Barmark's grammar 4.3 and 7.7: the line a part sings on,
such as Vox.Mel under a lead sheet's bare Vox chord line) draws each
note as its pitch name (every note of a set, without octaves) and
under it, in italics, the syllable each verse sings there: verse 1 from
the syllables on the notes, then each lyrics N: row, numbered "1.",
"2." left of the first bar when there are several. A hyphen follows a
syllable whose word goes on, and a held syllable (:/) draws an extender
line. The notes are spaced by beat, each as wide as the widest note or
syllable. Verses come from the parser as played, placed under the written
bars: a repeat's verse 2 is drawn under the bars it is sung on, and a
replay draws its own words. A voice line with no words draws as any part
line does. A lyric row on no melody (the chart's words, or a singer who
writes no notes there) draws its words inside the bars it covers, a line
per verse spaced by beat, so on a narrow screen they wrap with their
bars; under a system they go in its last row. A screen reader hears the
words after each bar's notes: "bar 2: note G four, hold, …, verse 1
“maz-ing”, verse 2 “grace that”".
License
MIT. Edwin is © MuseScore Limited, under the SIL Open Font License 1.1.