chadtmiller.com/monolith
An atproto PDS
0
0
// The card a link to the site unfurls into (og:image, pages/index.astro):
// the landing's photograph at Open Graph's 1200 by 630, cropped about its
// centre, which keeps the monolith and its hilltop, with the wordmark on the
// ground below it as the landing sets it (`.logo` there). The letters are
// drawn as outlines from the site's own JetBrains Mono, so the card looks
// the same wherever it is built, with no font installed.
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import type { APIRoute } from 'astro'
import { create, type Font } from 'fontkitten'
import sharp from 'sharp'
const W = 1200
const H = 630
/** The wordmark's size, and its tagline's distance from the card's foot, in px. */
const SIZE = 26
const BOTTOM = 36
const INK = '#a8a8a2'
// From the site's root, which the build runs in: this module is bundled
// elsewhere before it runs, so its own location does not lead to the files.
const font = create(readFileSync(join(process.cwd(), 'public/fonts/jetbrains-mono-latin.woff2'))) as Font
const scale = SIZE / font.unitsPerEm
// Where the baseline sits in a line box of `line-height: 1`, as a browser sets it.
const baseline = (SIZE - (font.ascent - font.descent) * scale) / 2 + font.ascent * scale
/** `text` as SVG path data, centred across the card, its line box's top at `top`, `spacing` em between letters. */
function line(text: string, top: number, spacing = 0): string {
const glyphs = font.glyphsForString(text)
const gap = spacing * font.unitsPerEm
const width = (glyphs.reduce((w, g) => w + g.advanceWidth, 0) + gap * (glyphs.length - 1)) * scale
let x = (W - width) / 2
let d = ''
for (const g of glyphs) {
d += g.path.transform(scale, 0, 0, -scale, x, top + baseline).toSVG()
x += (g.advanceWidth + gap) * scale
}
return d
}
export const GET: APIRoute = async () => {
const tagTop = H - BOTTOM - SIZE
const nameTop = tagTop - 0.5 * SIZE - SIZE
const d = line('MONOLITH', nameTop, 0.5) + line('an atproto PDS', tagTop)
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}"><path fill="${INK}" d="${d}"/></svg>`
const jpeg = await sharp(join(process.cwd(), 'src/assets/hero.jpg'))
.resize(W, H, { fit: 'cover' })
.composite([{ input: Buffer.from(svg) }])
.jpeg({ quality: 82 })
.toBuffer()
return new Response(new Uint8Array(jpeg), { headers: { 'Content-Type': 'image/jpeg' } })
}