Skip to content

Unicode Staff Grid

The fretboard post ended with β€œthese approaches also apply to musical staves.” This version does just that, but keeps one constraint: start with the Unicode staff glyph, measure the source units once, and derive the placement grid from those measurements instead of nudging pixels by hand.

The relevant character is 𝄚, U+1D11A MUSICAL SYMBOL FIVE-LINE STAFF.

The source file in notofonts/music is sources/NotoMusic.glyphs. A small Python script now builds one measurement table across the 4-string fretboard, 5-string fretboard, 5-line staff, stemless notehead, quarter note, and half note.

Minimal Python to download the source and print the table:

1
2
3
4
5
6
7
8
from subprocess import check_output
from urllib.request import urlretrieve

path, _ = urlretrieve(
    'https://raw.githubusercontent.com/notofonts/music/main/sources/NotoMusic.glyphs',
    '/tmp/NotoMusic.glyphs',
)
print(check_output(['python', 'scripts/measure_noto_music.py', path], text=True))

The full table is longer, but for this post the staff, quarter-note, and half-note rows are the ones that matter:

Glyph Codepoint Glyph name Advance Bounds (l,b)-(r,t) Size Center Details
5-line staff U+1D11A u1D11A 1100 (50, 0)-(1050, 1000) 1000x1000 (550.0, 500.0) 5 lines; thickness 24; gap 220; center step 244; note step 122
Quarter note U+1D15F u1D15F 400 (51, 11)-(350, 1009) 299x998 (200.5, 510.0) measured from built TTF glyph bounds
Half note U+1D15E u1D15E 397 (50, -1)-(347, 1009) 297x1010 (198.5, 504.0) measured from built TTF glyph bounds

The useful CSS variables fall straight out of those measurements:

1
2
3
4
5
6
7
.staff-demo {
  --glyph-scale: 20rem;
  --staff-left: calc(var(--glyph-scale) * 50 / 1000);
  --staff-width: calc(var(--glyph-scale) * 2);
  --staff-top-line: calc(var(--glyph-scale) * 12 / 1000);
  --staff-step: calc(var(--glyph-scale) * 122 / 1000);
}

50 / 1000 is the left inset, 12 / 1000 is the top-line center, and 122 / 1000 is half of the 244 unit line-center spacing, so it lands on both lines and spaces.

For the marks themselves, quarter notes and half notes are easier to read than bare noteheads. The overlay uses Noto Music glyphs for both from the same musical-symbol family: π…Ÿ, U+1D15F MUSICAL SYMBOL QUARTER NOTE, and π…ž, U+1D15E MUSICAL SYMBOL HALF NOTE.

With that grid, a sparse data-pos overlay can treat one scaled staff glyph as one 16-slot measure block and place the treble clef, the stacked 4/4, and then the note characters. Every character uses exactly one row letter plus a two-digit base-10 column: the clef centers on G, column 02 is left empty because the treble clef is wide, the time-signature digits sit on a and c, and the notes use their pitch rows. The last two digits give the column from 01 through 16. Uppercase and lowercase distinguish lower and higher pitches. The first measure of β€œTwinkle, Twinkle, Little Star” is G G d d; the next measure starts a new staff block when the 16 slots run out.

<div class="staff-demo">
  <div class="staff-items">
    <span data-pos="G01">π„ž</span>
    <span data-pos="a03">4</span>
    <span data-pos="c03">4</span>
    <span data-pos="g04"><span class="code-music">π…Ÿ</span></span>
    <span data-pos="g08"><span class="code-music">π…Ÿ</span></span>
    <span data-pos="d12"><span class="code-music">π…Ÿ</span></span>
    <span data-pos="d16"><span class="code-music">π…Ÿ</span></span>
  </div>
</div>

This is the part I wanted from the start: one Unicode background glyph, one measured grid, and then ordinary markup on top. The fretboard glyph and the staff glyph both work better once the source units become the API.