Repository navigation
Replies: 2 comments
|
Update: in the end I used a regex replace on the rendered SVG output instead of vendoring the entire template. Less work but feels a bit hacky.. |
|
Until something like import re
from rich._export_format import CONSOLE_SVG_FORMAT
MY_FONT_FACE = """@font-face {{
font-family: "JetBrains Mono";
src: url("data:font/woff2;base64,...") format("woff2");
font-weight: 400 700;
}}""" # doubled braces: the template goes through str.format()
# drop the two Fira Code blocks, then insert ours right after <style>
template, n = re.subn(r"(?s)@font-face \{\{.*?\}\}\s*", "", CONSOLE_SVG_FORMAT, count=2)
assert n == 2, "Rich changed CONSOLE_SVG_FORMAT; check the @font-face replacement"
template = template.replace("<style>\n", "<style>\n\n " + MY_FONT_FACE + "\n", 1)
template, n = re.subn(r"font-family: Fira Code, monospace;",
"font-family: JetBrains Mono, monospace;", template)
assert n == 1
svg = console.export_svg(title="demo", code_format=template, font_aspect_ratio=0.6)Checked with Rich 15.0.0: the output has one
For the |
Uh oh!
There was an error while loading. Please reload this page.
Hi folks,
Here to suggest a minor new feature addition. Happy to put up an issue + PR if folks think it's worthwhile.
Issue text initially drafted by AI (Claude Opus 5) and edited / added to by hand ✍🏻
Problem
The SVG export feature uses
export_svg(), which references Fira Code vialocal()plus acdnjsURL. When an SVG loads through an<img>tag browsers often block external resource fetches, fonts included. This means that the webfont never loads. Viewers without Fira Code installed get a substitute, and since Rich positions each text chunk at an absolutexderived fromfont_aspect_ratio, glyph advances drift within the chunk and the box drawing can comes apart.There are a few issues on the repo that have come up due to this:
And the reason I ended up here, a related issue in my derivative project
rich-codex:The solutions here as I see them are to either:
None of these are really viable in
richitself, my request isn't for that - it's a new minor feature to allow consumers ofrichto do it themselves in an easier way: a new field, rather than needing to chuck out + duplicate the entire SVG template.What I'd like
A
font_face/font_familypair of optional parameters onexport_svg()andsave_svg(), defaulting to current behaviour:font_facereplaces the two@font-faceblocks;font_familyreplaces the value in.{unique_id}-matrix. No behaviour change unless set.font_aspect_ratiois already a parameter, so there's precedent for font knobs in that signature.Why not just third-party
It basically already is third-party:
code_formatlets me pass the whole template, and that's what I'll be doing now, so this isn't a blocker. The suggestion is only to make it cleaner and simpler to do so. Currently, to change three lines of CSS I'll need to vendor the entireCONSOLE_SVG_FORMATstring and re-syncing it whenever Rich touches the template (which does happen periodically).Happy to open the issue + PR if this seems reasonable.
All reactions