Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 245x 245x 1x 1x 1x 121x 121x 1x 1x 123x 123x 1x 1x 46x 46x 46x 46x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 1x 1x 1x 1x 1x 1x 1x 33x 33x 33x 33x 33x 33x 33x 33x 33x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 470x 470x 470x 470x 470x 1x 1x 1x 1x 1x 425x 425x 425x 425x 425x 425x 1x 1x 14x 14x 14x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 411x 411x 411x 411x 411x 411x 411x 411x 411x 411x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 1x 26x 26x 26x 26x 26x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 52x 52x 52x 52x 52x 52x 1430x 1430x 127x 127x 1430x 21x 21x 1430x 1282x 1282x 1282x 1282x 1282x 1282x 1282x 1282x 1282x 1430x 1430x 1430x 52x 1x 1x 1x 1x 1x 1x 52x 52x 52x 52x 52x 52x 1282x 52x 52x 52x 52x | /**
* Shared placement and path-walking for the Bravura outlines in
* `smuflGlyphs.ts`.
*
* Two renderers draw the same staff: the on-screen SVG (`MusicStaff.tsx`) and
* the printable PDF (`api/create/music-flashcards/route.ts`). They used to
* carry independent, hand-tuned constants and had drifted — the PDF drew a
* Helvetica "G" for a treble clef, the SVG anchored its bass clef to the middle
* line instead of the F line, and neither drew stems. Everything positional now
* lives here, so a fix lands in both at once.
*
* Coordinate conventions, matching both SVG and jsPDF:
* - y increases downward
* - staff positions count half-spaces from the bottom line: 0 = bottom line,
* 8 = top line, odd numbers are the spaces between, and anything outside
* 0..8 needs ledger lines
*/
import { type Clef, getStemDirection } from './noteUtils'
import { ENGRAVING, type GlyphCommand, SMUFL_GLYPHS, type SmuflGlyph } from './smuflGlyphs'
/**
* How a staff is scaled and placed. `halfSpace` is the distance between
* adjacent staff positions, i.e. HALF the distance between two staff lines —
* both call sites already had this quantity, under the name `lineGap`.
*/
export interface StaffMetrics {
/** y of the top staff line (position 8). */
staffTop: number
halfSpace: number
}
/** A glyph origin in user space, plus the scale to draw its outline at. */
export interface GlyphPlacement {
x: number
y: number
/** User-space length of one staff space. */
scale: number
}
/** Anything that can receive a path. jsPDF's document satisfies this as-is. */
export interface PathSink {
moveTo(x: number, y: number): void
lineTo(x: number, y: number): void
curveTo(x1: number, y1: number, x2: number, y2: number, x: number, y: number): void
close(): void
}
/** y of a staff position. Position 8 is the top line, so positions count down. */
export function positionY(m: StaffMetrics, position: number): number {
return m.staffTop + (8 - position) * m.halfSpace
}
/** User-space size of one staff space (two positions). */
export function staffSpace(m: StaffMetrics): number {
return m.halfSpace * 2
}
export function clefGlyph(clef: Clef): SmuflGlyph {
return clef === 'treble' ? SMUFL_GLYPHS.gClef : SMUFL_GLYPHS.fClef
}
/** The staff position a clef's origin marks — the line the clef names. */
function clefOriginPosition(glyph: SmuflGlyph): number {
if (glyph.originPosition === null) {
throw new Error(`${glyph.smuflName} has no originPosition and cannot be placed as a clef`)
}
return glyph.originPosition
}
/**
* Places a clef with its left edge at `left`.
*
* The vertical placement needs no per-clef tuning: a clef is *defined* by the
* staff line its origin marks (G clef → the G line, F clef → the F line), and
* the extracted glyphs record that as `originPosition`. This is what replaced
* the two renderers' magic multipliers.
*/
export function placeClef(m: StaffMetrics, clef: Clef, left: number): GlyphPlacement {
const glyph = clefGlyph(clef)
const scale = staffSpace(m)
return {
// bbox.left is ~0 for both clefs but is not assumed to be.
x: left - glyph.bbox.left * scale,
y: positionY(m, clefOriginPosition(glyph)),
scale,
}
}
/**
* Places a notehead centred horizontally on `centerX` and vertically on its
* staff position. The glyph origin is at the notehead's left edge and on its
* vertical centre, so only x needs adjusting.
*/
export function placeNotehead(m: StaffMetrics, position: number, centerX: number): GlyphPlacement {
const scale = staffSpace(m)
const glyph = SMUFL_GLYPHS.noteheadBlack
const width = glyph.bbox.right - glyph.bbox.left
return {
x: centerX - (glyph.bbox.left + width / 2) * scale,
y: positionY(m, position),
scale,
}
}
/**
* The stem rectangle for a notehead, or null if the caller wants none.
*
* A filled notehead with no stem is not a valid note of any duration, which is
* what the PDF was drawing. Attachment uses Bravura's own `stemUpSE` /
* `stemDownNW` anchors, so the stem meets the notehead exactly where the font
* designer intended rather than at an eyeballed offset.
*/
export function stemRect(
m: StaffMetrics,
position: number,
head: GlyphPlacement
): { x: number; y: number; width: number; height: number; direction: 'up' | 'down' } {
const glyph = SMUFL_GLYPHS.noteheadBlack
const direction = getStemDirection(position)
const width = ENGRAVING.stemThickness * head.scale
const length = ENGRAVING.stemLength * head.scale
const anchor = direction === 'up' ? glyph.anchors.stemUpSE : glyph.anchors.stemDownNW
const attachX = head.x + anchor[0] * head.scale
const attachY = head.y + anchor[1] * head.scale
return {
// The anchor marks the outer corner of the stem, so the rectangle hangs
// inward from it: leftward for an up-stem, rightward for a down-stem.
x: direction === 'up' ? attachX - width : attachX,
y: direction === 'up' ? attachY - length : attachY,
width,
height: length,
direction,
}
}
/**
* Vertical extent, in staff positions, that something occupies when drawn.
* `top` is the highest position reached, `bottom` the lowest.
*
* Callers need this to size a card *before* choosing a scale, so it is
* expressed in unit-free positions rather than user space. It matters more
* than it looks: a real G clef spans about fourteen half-spaces, nearly twice
* the staff itself, so a layout sized only for the staff and its notes will
* clip the clef off the top of the page.
*/
export interface PositionExtent {
top: number
bottom: number
}
export function unionExtents(...extents: PositionExtent[]): PositionExtent {
return {
top: Math.max(...extents.map((e) => e.top)),
bottom: Math.min(...extents.map((e) => e.bottom)),
}
}
/**
* A glyph's extent around its origin. bbox is y-down in staff spaces while
* positions count upward in half-spaces, hence the negation and the doubling.
*/
function glyphExtent(glyph: SmuflGlyph, originPosition: number): PositionExtent {
return {
top: originPosition - glyph.bbox.top * 2,
bottom: originPosition - glyph.bbox.bottom * 2,
}
}
export function clefExtent(clef: Clef): PositionExtent {
const glyph = clefGlyph(clef)
return glyphExtent(glyph, clefOriginPosition(glyph))
}
/**
* Room enough for either clef.
*
* The two are nothing like the same height — a G clef spills well past the
* staff at both ends while an F clef stays inside it — so a treble card and a
* bass card fitted to their own clefs come out at noticeably different scales.
* The matching game deals both at once, and pairs one of each side by side, so
* a single card cannot be allowed to pick its own scale.
*/
export const ANY_CLEF_EXTENT: PositionExtent = unionExtents(
clefExtent('treble'),
clefExtent('bass')
)
/** Extent of a full note — notehead plus the stem attached to it. */
export function noteExtent(position: number): PositionExtent {
const glyph = SMUFL_GLYPHS.noteheadBlack
const head = glyphExtent(glyph, position)
const direction = getStemDirection(position)
const anchor = direction === 'up' ? glyph.anchors.stemUpSE : glyph.anchors.stemDownNW
// Anchor y is y-down in staff spaces; positions count upward in half-spaces.
const attach = position - anchor[1] * 2
const stemTip =
direction === 'up' ? attach + ENGRAVING.stemLength * 2 : attach - ENGRAVING.stemLength * 2
return unionExtents(head, { top: stemTip, bottom: stemTip })
}
/** The staff itself: bottom line to top line. */
export const STAFF_EXTENT: PositionExtent = { top: 8, bottom: 0 }
/** Extent of every note in an inclusive range of positions. */
export function noteRangeExtent(lowest: number, highest: number): PositionExtent {
const extents: PositionExtent[] = []
for (let p = Math.floor(lowest); p <= Math.ceil(highest); p++) extents.push(noteExtent(p))
return unionExtents(...extents)
}
/**
* The note range a single-note staff reserves room for whether or not the note
* needs it — a ledger line either side of the staff, which is what beginner
* material uses.
*
* Reserving a fixed range is what keeps a row of cards on a common scale. Fit
* each card to its own note instead and the staff visibly changes size and
* height from card to card, which looks broken when several are on screen at
* once and makes two notes harder to compare.
*/
export const COMMON_NOTE_RANGE = { lowest: -3, highest: 11 } as const
/**
* Minimum staff width, counted in staff spaces.
*
* A G clef is 2.7 staff spaces wide, so a staff scaled purely to fill the
* available height ends up only about five spaces wide with the clef covering
* half of it and running over the note. Nine spaces leaves the clef roughly a
* third of the staff, which is about what engraved music looks like.
*/
export const MIN_STAFF_SPACES = 9
/** The largest `halfSpace` at which `staffWidth` still fits a clef and a note. */
export function halfSpaceForWidth(staffWidth: number): number {
return staffWidth / (MIN_STAFF_SPACES * 2)
}
export interface StaffFit {
/** Vertical space the staff and everything around it may occupy. */
availableHeight: number
/** y of the top of that space. */
top: number
/** Everything that will be drawn, so none of it lands outside. */
extent: PositionExtent
/** Additional upper bounds on `halfSpace` — width limits, absolute caps. */
maxHalfSpace?: number[]
}
/**
* Chooses a scale that fits `extent` into the available height and centres it.
*
* Both renderers need exactly this and used to do it separately with different
* constants, which is how the on-screen staff and the printed one drifted
* apart.
*/
export function fitStaff({
availableHeight,
top,
extent,
maxHalfSpace = [],
}: StaffFit): StaffMetrics {
const spanUnits = extent.top - extent.bottom
const halfSpace = Math.min(availableHeight / spanUnits, ...maxHalfSpace)
const contentTop = top + (availableHeight - spanUnits * halfSpace) / 2
// contentTop is the y of the highest drawn position; step down to position 8.
return { halfSpace, staffTop: contentTop + (extent.top - 8) * halfSpace }
}
/** Half-width of a ledger line, from Bravura's `legerLineExtension`. */
export function ledgerHalfWidth(m: StaffMetrics): number {
const glyph = SMUFL_GLYPHS.noteheadBlack
const space = staffSpace(m)
const headWidth = (glyph.bbox.right - glyph.bbox.left) * space
return headWidth / 2 + ENGRAVING.legerLineExtension * space
}
/**
* Walks a glyph outline into a sink.
*
* All subpaths go into a single path so that one fill covers the whole glyph:
* with nonzero winding that is what keeps counters — the eye of a G clef, the
* hole in a half note — hollow. Filling each subpath separately would paint
* them solid.
*/
export function traceGlyph(
commands: readonly GlyphCommand[],
at: GlyphPlacement,
sink: PathSink
): void {
const { x: ox, y: oy, scale: s } = at
for (const c of commands) {
switch (c.t) {
case 'M':
sink.moveTo(ox + c.x * s, oy + c.y * s)
break
case 'L':
sink.lineTo(ox + c.x * s, oy + c.y * s)
break
case 'C':
sink.curveTo(
ox + c.x1 * s,
oy + c.y1 * s,
ox + c.x2 * s,
oy + c.y2 * s,
ox + c.x * s,
oy + c.y * s
)
break
case 'Z':
sink.close()
break
}
}
}
/**
* An SVG `d` string for a glyph. Built through the same walk the PDF uses, so
* the two outputs cannot disagree about a coordinate.
*/
export function glyphPathData(glyph: SmuflGlyph, at: GlyphPlacement): string {
const parts: string[] = []
const n = (v: number) => Number(v.toFixed(3))
traceGlyph(glyph.commands, at, {
moveTo: (x, y) => parts.push(`M${n(x)} ${n(y)}`),
lineTo: (x, y) => parts.push(`L${n(x)} ${n(y)}`),
curveTo: (x1, y1, x2, y2, x, y) =>
parts.push(`C${n(x1)} ${n(y1)} ${n(x2)} ${n(y2)} ${n(x)} ${n(y)}`),
close: () => parts.push('Z'),
})
return parts.join('')
}
|