letterSpacing: the space after a hyphenated word disappears ("LOW-COST PLAN" renders as "LOW-COSTPLAN")
Bug
With letterSpacing, the space after a word that contains a hyphen (or a slash) disappears. LOW-COST PLAN renders as LOW-COSTPLAN. Without the hyphen, LOWCOST PLAN renders correctly.
- satori 0.33.5 (latest), Node 24, font Lexend Regular (TTF),
fontSize: 40,letterSpacing: 20. - Rendered with
@resvg/resvg-js, the blank gap beforePLANmeasures 36 px forLOWCOST PLANbut 26 px forLOW-COST PLAN, which is the normal gap between two letters. WithletterSpacing: 0both gaps are 16 px. - The same happens with
/(A/B TEST). Centred and right-aligned lines also end up shifted.
Reproduction
import satori from "satori";
import { Resvg } from "@resvg/resvg-js";
import { readFileSync, writeFileSync } from "node:fs";
const font = readFileSync("./Lexend-Regular.ttf");
for (const text of ["LOWCOST PLAN", "LOW-COST PLAN"]) {
const svg = await satori(
{
type: "div",
props: {
style: { display: "flex", fontFamily: "Lexend", fontSize: 40, letterSpacing: 20, width: 900, height: 80, alignItems: "center", paddingLeft: 10, background: "#fff" },
children: text,
},
},
{ width: 900, height: 80, fonts: [{ name: "Lexend", data: font, weight: 400, style: "normal" }] },
);
writeFileSync(`${text.replace(/\W/g, "_")}.png`, new Resvg(svg).render().asPng());
}Expected: the same word space in both images. Actual: LOW-COST PLAN has no visible space between COST and PLAN.
Likely cause
As far as we can tell from the built code, the line layout splits the text at every line-break opportunity, including right after a hyphen or slash (LOW- | COST ). Each piece's width is measured with letterSpacing × (glyphs − 1), so the spacing between two pieces joined without a space is never counted. When drawing, the pieces are merged into one glyph run and that spacing is added, so everything after the hyphen is drawn one letterSpacing further right than the layout placed it. The following space absorbs the difference. Adding letterSpacing between two adjacent pieces on the same line, when the first one does not end in whitespace, makes the layout match the drawing (and Chrome) in our tests.
Source: vercel/satori