From 18963168bbbbc0b514d321ec09e1675bc8cc35a8 Mon Sep 17 00:00:00 2001 From: Rogee Date: Tue, 22 Sep 2026 16:51:44 +0800 Subject: [PATCH] fix: render editable score at readable scale --- src/layout/svg-renderer.ts | 28 ++++++++++++++++------------ test/m3.test.mjs | 4 ++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/layout/svg-renderer.ts b/src/layout/svg-renderer.ts index 7672b3f..193335d 100644 --- a/src/layout/svg-renderer.ts +++ b/src/layout/svg-renderer.ts @@ -10,19 +10,23 @@ function u(value: number): string { return (value * MM_SCALE).toFixed(2).replace(/\.00$/, ""); } +function fontSizePx(value: number): string { + return u(value * 25.4 / 96); +} + function renderTitle(page: PageLayout, snapshot: LayoutSnapshot): string { if (page.pageNumber !== 1 || !snapshot.pages[0]) return ""; const title = snapshot.pages[0].titleRectMm; const text = snapshot.documentTitle || snapshot.sourceDocumentId; - return `${esc(text)}`; + return `${esc(text)}`; } function renderSymbol(symbol: SymbolLayout): string { const attrs = `data-id="${esc(symbol.id)}" data-kind="${esc(symbol.kind)}" data-measure-id="${esc(symbol.measureId)}"${symbol.voiceId ? ` data-voice-id="${esc(symbol.voiceId)}"` : ""}`; const hidden = symbol.hidden ? " visibility=\"hidden\"" : ""; - const note = `${esc(symbol.text)}`; - const decorations = (symbol.decorationText ?? []).map((text, index) => `${esc(text)}`).join(""); - const chord = (symbol.chordText ?? []).map((text, index) => `${esc(text)}`).join(""); + const note = `${esc(symbol.text)}`; + const decorations = (symbol.decorationText ?? []).map((text, index) => `${esc(text)}`).join(""); + const chord = (symbol.chordText ?? []).map((text, index) => `${esc(text)}`).join(""); return `${note}${decorations}${chord}`; } @@ -30,8 +34,8 @@ function renderBarline(x: number, y: number, height: number, style: string, id: if (style === "unknown") return ""; const dashed = style === "dashed" ? ` stroke-dasharray="2 1"` : ""; const double = style === "||" || style === "::" || style === ":|" || style === "|:"; - const lines = `${double ? `` : ""}`; - const repeat = style === ":|" || style === "|:" || style === "::" ? `` : ""; + const lines = `${double ? `` : ""}`; + const repeat = style === ":|" || style === "|:" || style === "::" ? `` : ""; return `${lines}${repeat}`; } @@ -48,19 +52,19 @@ function renderAttachment(attachment: AttachmentLayout): string { const lines = attachment.lines?.length ? attachment.lines : [attachment.raw]; const kind = attachment.kind ?? "attachment"; const curve = /^(?:Arc|Extend|Branch|Slur|Tie)$/i.test(kind) - ? `` - : ""; + ? `` + : ""; const text = /^(?:Arc|Extend|Branch|Slur|Tie)$/i.test(kind) ? "" : attachment.syllableMapping - ? (attachment.syllables ?? []).map((syllable) => `${esc(syllable.text)}`).join("") - : lines.map((line, index) => `${esc(line)}`).join(""); + ? (attachment.syllables ?? []).map((syllable) => `${esc(syllable.text)}`).join("") + : lines.map((line, index) => `${esc(line)}`).join(""); return `${curve}${text}`; } function renderPageObject(object: PageObjectLayout): string { const hidden = object.visible === false ? " visibility=\"hidden\"" : ""; - return `${esc(object.raw)}`; + return `${esc(object.raw)}`; } export function renderPageSvg(snapshot: LayoutSnapshot, page: PageLayout, includeAssistiveLayer = false): string { @@ -68,7 +72,7 @@ export function renderPageSvg(snapshot: LayoutSnapshot, page: PageLayout, includ const measures = page.measures.map(renderMeasure).join(""); const attachments = page.attachments.map(renderAttachment).join(""); const pageObjects = page.pageObjects.map(renderPageObject).join(""); - const assistive = includeAssistiveLayer ? `辅助层:${snapshot.warnings.length} 条布局诊断` : ""; + const assistive = includeAssistiveLayer ? `辅助层:${snapshot.warnings.length} 条布局诊断` : ""; return `JPW7 page ${page.pageNumber}${renderTitle(page, snapshot)}${measures}${symbols}${attachments}${pageObjects}${assistive}`; } diff --git a/test/m3.test.mjs b/test/m3.test.mjs index 2991b41..1b8cefe 100644 --- a/test/m3.test.mjs +++ b/test/m3.test.mjs @@ -59,6 +59,10 @@ test("M3 SVG renderer preserves A4 physical units, semantic IDs and layer separa assert.equal(probe.hasScoreLayer, true); assert.equal(probe.hasAssistiveLayer, false); assert.ok(probe.dataIds.length >= Object.keys(snapshot.symbols).length); + const titleFontSize = Number(svg.match(/data-role="document-id"[^>]*font-size="([0-9.]+)"/)?.[1] ?? 0); + const noteFontSize = Number(svg.match(/data-kind="note"[^>]*>[\s\S]*?font-size="([0-9.]+)"/)?.[1] ?? 0); + assert.ok(titleFontSize > 100); + assert.ok(noteFontSize > 100); const accessibleProbe = probeSvg(renderSnapshotSvg(snapshot, true)[0]); assert.equal(accessibleProbe.hasAssistiveLayer, true); });