rism-digital / verovio

🎵 Music notation engraving library for MEI with MusicXML and Humdrum support and various toolkits (JavaScript, Python)
https://www.verovio.org
GNU Lesser General Public License v3.0
685 stars 185 forks source link

[Python] fonts not correctly applied #3811

Closed Daniel63656 closed 1 month ago

Daniel63656 commented 1 month ago

When specifying a font like Gootville or Leland as font option, the resulting svg is not created properly. Notes do not attach to stems (see attached image). This is caused by not applying the font correctly. In fact, one can inspect the created svg file to see, that it still uses Leipzig when searching for "font-family". Strangely, this works just fine when using Bravura.

import verovio
from IPython.display import display, SVG

tk = verovio.toolkit()
tk.loadFile("Bella Ciao.musicxml")
options = {"font": "Gootville"}
tk.setOptions(options)

# Loop through all pages and render each as SVG
for page in range(1, tk.getPageCount() + 1):
    svg = tk.renderToSVG()  # Render the current page to SVG
    with open("output.svg", "w") as svg_file:
        svg_file.write(svg)
    display(SVG(svg))  # Display the rendered SVG

Expected behavior I would expect rendering to work independently of the font string provided, given that the appropriate font is installed on the system. If I say font="xyz", "xyz" should show up consistently in SVG font-family declarations, regardless of whether "xyz" exists or not.

output

ahankinson commented 1 month ago

Verovio doesn’t quite work like that. It uses the fonts internally but then expands them to the actual SVG shapes when compiling the music. It does this because it needs to know the exact dimensions of each symbol when doing the layout, which you can’t do if you simply replace the font in the SVG with CSS.

You can try calling redoLayout() after changing the font to see if that helps.