A card in any font

readme-stats can draw any of its cards in any font on Google Fonts, and making that work took more than passing a font name along. Here's a link:
It asks for a project card in a wide pixel font, with a pink accent and a bigger name. At first, readme-stats would have ignored all of it and drawn the card in JetBrains Mono, the one style I liked. But "a style I like" is the part nobody else shares, so I made the look something a card's link can say: font=, five colours, corners, borders, padding and three text sizes.
Adding the options was quick. Making a card survive them took the rest of the day. The easiest way to show why is to follow that one link through the server.
Stop 1: finding the font
A card in a README is an image, and an image can't load anything, fonts included. The cards already carried JetBrains Mono inside them as base64. For Press Start 2P, the server has to get the font itself before it draws anything.
Google Fonts was the obvious place, and it had a few surprises. Its CSS API picks the file format by browser, so the server asks as Chrome to get woff2, split into one file per script, and keeps only the one marked /* latin */. Family names are case-sensitive: Inter works, inter is a 400. A weight a family doesn't have is a 400 too, and Press Start 2P has only one style, so it answers for regular and fails for bold.
So for each of the four weights the cards use, the server asks once, keeps what it gets on disk, and remembers names Google doesn't know for an hour. A weight the family doesn't have is filled in with the nearest one it does:
Without that, the browser would make up its own bold for 800, wider than the one the card was measured with.
Stop 2: measuring the name
The project's name has to fit, or be cut off neatly. That's where I found out how much the code assumed JetBrains Mono. Every width on every card came from one line:
That's how names were cut off, descriptions wrapped and chips sized. In a wide pixel font, or in Inter, where an "i" is a third of an "m", all of it was wrong.
Now the widths come from the font itself. fontkit reads the woff2 file, and laying out a string gives its real width, kerning included:
Measuring the same text over and over adds up, so each font keeps the widths it has measured. The same "Commits" and "Pull requests" come up on every card.

Stop 3: putting things where they go
Inter looked right as soon as widths were measured. Press Start 2P, with its tall capitals, didn't. The streak's "LONGEST" and "CONTRIBUTIONS" ran into each other, and the banner's label sat on top of its title.
Widths were measured now, but heights weren't. Every card was placed by hand: the label at 38, the name at 65, the description at 94. Those numbers were right for JetBrains Mono at one size, and nothing else. And my link also asks for title-size=lg, which no fixed number could know about.
So each card now places its text as a chain of gaps, each one growing with the text it belongs to. The project card:
l, t and s scale with the label, title and text sizes, and p with the padding. cap is how tall the font's capitals are next to JetBrains Mono's, so a tall font pushes what's under it down instead of into it. The odd decimals are there on purpose: at the default sizes in JetBrains Mono, the chain adds up to 38, 65 and 94, the card exactly as it was.

Where something still doesn't fit, it's cut off with an ellipsis rather than overlapping. And where a shorter word says the same, the card uses it: in a wide font, the streak's "CONTRIBUTIONS" becomes "TOTAL".
Stop 4: one colour becomes a dozen
My link names one colour, the accent. A card uses about a dozen: dividers, chip backgrounds, five steps of the contribution graph, the banner's grid. Asking for all of them would be silly, so you give up to five, and the rest are mixed from those the way the built-in themes were made: dividers 7% of the way from the background to the text, muted text 65%, and the graph's steps 30, 55, 80 and 100% of the way to the accent.
One case needed more than mixing. bg=0b1021 on the light theme kept the light theme's dark text, on a dark background. Now a background on its own keeps the theme's text only if it can be read on it, and takes the other theme's otherwise.
Later, colours learned to be see-through: two more digits, so bg=12121800 is no background at all. Mixing toward a background you can't see would give colours that only look right on one page, so on a see-through card the colours nobody names are the text and accent colours, see-through by as much. They look the same on whatever page the card sits on.

Stop 5: the cards without a link like that
The last stop isn't for my pink pixel card. It's for every card that has no options at all, mine included. All of this rewrote every card, and I didn't want my own profile to change by a pixel.
So before starting, I saved my real profile's data once, and rendered every card from it. After each step, I rendered them all again and compared the files.
At the default settings they came out the same, byte for byte, apart from the font's name in the CSS and a few numbers now rounded to two decimals. The one card that did change was a good change: with eight languages, the profile card's list used to run past the bottom edge. The new layout made the card grow to fit, and the comparison is how I noticed it had ever been cut off.
Where links like that end up
The README has a gallery of looks made only from links: a green terminal in VT323, an editorial card in Playfair Display, a pink skills card in Fredoka, a synthwave banner in Orbitron, a Swiss card in black, white and one red, and a banner so plain it reads like a heading. None of them needed a line of code.

Since then, nobody has to write a link like the one at the top by hand. The server's own page has a builder: pick a card, start from a preset or press Random, give the dark and the light version their own colours, and copy the snippet. My cards still look like mine, and yours can look like yours.





No comments yet. Be the first!