-
-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrong spacing in thumbnail/embed SVG with Consolas font #265
Wrong spacing in thumbnail/embed SVG with Consolas font #265
Comments
Here's a hack that "fixes" such an SVG in any font (heck, it even works passably for most variable-width fonts), by means of wrapping each char in its own span:
Script: for (const tspan of document.querySelectorAll('tspan')) {
if (tspan.querySelector('*')) continue
const tspans = [...tspan.textContent.trim()].map((char, i) => {
const t = tspan.cloneNode(true)
t.textContent = char
let x = parseFloat(tspan.getAttribute('x'))
x += i * 8.4
if (i) t.removeAttribute('dy')
t.setAttribute('x', x)
return t
})
tspan.replaceWith(...tspans)
} Limitations:
Results applied to my thumbnail: And here are the results with the font changed to the decidedly non-monospace Papyrus: |
Less hacky fix: add a @font-face {
font-family: "Consolas";
src: local("Consolas");
size-adjust: 109.5%;
} As before, 109.5 is a magic number (109.5% of 14px ~= 15.3px). Works perfectly in latest Chrome, Firefox, Edge, but notably fails (has no effect) if you open the SVG in Inkscape. Result: |
OK, here's a third fix, much better and less hacky than either of the others. The In addition, CSS
Tested in latest Chrome, FF, Edge, and it even looks fine in Inkscape (despite no Also tested on an asciicast with different Unicode-width chars — no issues there either, as asciinema already renders each char with Unicode-width other than 1 in its own span, so they just stay where they are. File size increase in my case is 10.2 -> 13.1 kB, though I cheated a little by rounding each Dev-tools-based demo: document.querySelector('svg style').textContent += `\n text { font-size-adjust: ch-width 0.6; }`
// https://github.com/asciinema/asciinema-server/blob/fbbb2ce35272d516ce2a6debfbc04c9a7c60a149/lib/asciinema_web/controllers/recording_svg.ex#L399
const CELL_WIDTH = 8.42333333
for (const tspan of document.querySelectorAll('tspan')) {
if (tspan.childElementCount) continue
const offset = parseFloat(tspan.getAttribute('x'))
const xPerGrapheme = [...new Intl.Segmenter('en-US', { granularity: 'grapheme' }).segment(tspan.textContent.trim())].map((_, i) => {
return offset + (i * CELL_WIDTH)
})
tspan.setAttribute('x', xPerGrapheme.map((x) => x.toFixed(1)).join(' '))
} |
Turns out it breaks in Chrome when a |
Edit: I realized this issue is in the wrong repo, feel free to move to asciinema/asciinema-server. PR at asciinema/asciinema-server#450
Describe the bug
With this recording: https://asciinema.org/a/XxTbS0aAtTA6BZ9tdzKardxJs
The recording itself (including the freeze frame of the frame used for the thumbnail) looks perfect, stunning, gorgeous.
However, the spacing is off in the SVG used for the thumbnail/embed image when displayed on Windows 11 with the Consolas font installed (from examining the SVG, the font stack is "Consolas, Menlo, 'Bitstream Vera Sans Mono', monospace, 'Powerline Symbols'").
Changing the font to Courier New fixes the problem (but it also makes it kinda ugly cuz... Courier New). Changing the font size to
15.3px
(magic number) also seems to fix it, but presumably would break the other fonts.Screenshots:
Direct link to the SVG
To Reproduce
Steps to reproduce the behavior:
There are prominent examples at https://asciinema.org/explore that also reproduce the problem, e.g. https://asciinema.org/a/666780
Expected behavior
Thumbnail to look identical (or near-identical) to screenshotted freeze frame, just crisper due to being an SVG.
Versions:
Additional context
N/A
The text was updated successfully, but these errors were encountered: