Files
sorsfonalak/services/frontend/src/lib/ogCard.tsx
T

232 lines
8.5 KiB
TypeScript
Raw Normal View History

2026-11-19 11:39:32 +00:00
// Link preview images (og:image): 1200×630 PNG with a chart wheel on the left
// and text on the right, in the site's look. Used by the shared charts and
// the home page.
import "server-only";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { ImageResponse } from "next/og";
import { coverBox } from "./celebrityPhoto";
2026-11-19 11:39:32 +00:00
import type { ChartResult } from "./types";
import { LABEL_STYLE, WHEEL_UNITS, wheelSvg } from "./wheelSvg";
2026-11-19 11:39:32 +00:00
const W = 1200;
const H = 630;
const WHEEL = 590;
const FONTS = join(process.cwd(), "src/assets/og");
let fonts: Promise<{ name: string; data: Buffer; weight: 400 | 600; style: "normal" }[]> | null = null;
function loadFonts() {
fonts ??= Promise.all(
(
[
["Cormorant Garamond", "CormorantGaramond-SemiBold.ttf", 600],
["Inter", "Inter-Regular.ttf", 400],
["Inter", "Inter-SemiBold.ttf", 600],
["DejaVu Sans", "DejaVuSans.ttf", 400],
] as const
).map(async ([name, file, weight]) => ({
name,
data: await readFile(join(FONTS, file)),
weight,
style: "normal" as const,
})),
);
return fonts;
}
/**
* Letters, digits, punctuation and spaces only: a character missing from the
* fonts would make the renderer download a font from the internet.
*/
export const printable = (s: string) => s.replace(/[^\p{L}\p{N}\p{P}\p{Zs}+=<>°]/gu, "");
/**
* The chart wheel, `size` pixels: the SVG image without its labels, and the
* labels drawn on top with the card's fonts (inside an SVG image the renderer
* has no fonts, so glyphs and numbers would come out as boxes).
*/
function Wheel({ chart, timeKnown, size }: { chart: ChartResult; timeKnown: boolean; size: number }) {
const { svg, labels } = wheelSvg(chart, timeKnown, size);
const k = size / WHEEL_UNITS;
return (
<div style={{ display: "flex", position: "relative", width: size, height: size }}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={`data:image/svg+xml;base64,${Buffer.from(svg).toString("base64")}`} width={size} height={size} alt="" />
{labels.map((l, i) => {
const style = l.className.split(" ").map((c) => LABEL_STYLE[c]).find(Boolean);
if (!style) return null;
const fontSize = style.size * k;
const w = fontSize * 6;
const h = fontSize * 1.4;
return (
<div
key={i}
style={{
display: "flex",
position: "absolute",
left: l.x * k - w / 2,
top: l.y * k - h / 2,
width: w,
height: h,
alignItems: "center",
justifyContent: "center",
fontFamily: style.font,
fontWeight: style.weight,
fontSize,
lineHeight: 1,
color: style.color,
// No undefined values: the renderer stumbles on them.
...(style.halo ? { textShadow: "0 0 3px #111729" } : {}),
}}
>
{style.font === "DejaVu Sans" ? l.text : printable(l.text)}
</div>
);
})}
</div>
);
}
2026-11-19 11:39:32 +00:00
export interface CardLine {
/** A zodiac or planet glyph (DejaVu Sans has them all). */
glyph: string;
label: string;
}
/** The preview image as a response with the given cache header. */
export async function chartCard({
chart,
timeKnown,
eyebrow,
title,
text,
lines,
cacheControl,
}: {
chart: ChartResult;
timeKnown: boolean;
eyebrow: string;
title: string;
text: string;
lines: CardLine[];
cacheControl: string;
}): Promise<Response> {
return new ImageResponse(
(
<div style={{ display: "flex", width: W, height: H, background: "#0d1220", color: "#ece6d8", fontFamily: "Inter" }}>
<div style={{ display: "flex", width: WHEEL + 40, height: H, alignItems: "center", justifyContent: "center" }}>
<Wheel chart={chart} timeKnown={timeKnown} size={WHEEL} />
2026-11-19 11:39:32 +00:00
</div>
<div style={{ display: "flex", flexDirection: "column", flex: 1, padding: "56px 56px 44px 8px", gap: 18 }}>
<div style={{ display: "flex", fontSize: 17, fontWeight: 600, letterSpacing: 2.5, color: "#d9b46a" }}>
{printable(eyebrow)}
</div>
<div style={{ display: "flex", fontFamily: "Cormorant Garamond", fontSize: 56, lineHeight: 1.05 }}>
{printable(title)}
</div>
{text && (
<div style={{ display: "flex", fontSize: 23, lineHeight: 1.45, color: "#c9cfdc" }}>{printable(text)}</div>
)}
<div style={{ display: "flex", flexWrap: "wrap", marginTop: "auto", gap: "8px 22px", fontSize: 21, color: "#c9cfdc" }}>
{lines.map((l) => (
<div key={l.label} style={{ display: "flex", alignItems: "center", gap: 8 }}>
<span style={{ fontFamily: "DejaVu Sans", color: "#d9b46a" }}>{l.glyph.replace(/︎/g, "")}</span>
<span>{printable(l.label)}</span>
</div>
))}
</div>
<div style={{ display: "flex", fontFamily: "Cormorant Garamond", fontSize: 30, color: "#ece6d8" }}>
sors<span style={{ color: "#d9b46a" }}>fonalak</span>
</div>
</div>
</div>
),
{ width: W, height: H, fonts: await loadFonts(), headers: { "Cache-Control": cacheControl } },
);
}
/**
* A famous person's preview image: the photo on the left (cropped around the
* face point), the name, the Sun, Moon and Ascendant and the chart wheel on
* the right. `photo`: the stored JPEG as bytes, with its size and face point.
*/
export async function celebrityCard({
chart,
timeKnown,
name,
subtitle,
lines,
photo,
cacheControl,
}: {
chart: ChartResult;
timeKnown: boolean;
name: string;
subtitle: string | null;
lines: CardLine[];
photo: { data: ArrayBuffer; width: number; height: number; focus_x: number; focus_y: number } | null;
cacheControl: string;
}): Promise<Response> {
const PHOTO_W = 440;
const RING = 400;
const box = photo && coverBox(photo, PHOTO_W, H);
return new ImageResponse(
(
<div style={{ display: "flex", width: W, height: H, background: "#0d1220", color: "#ece6d8", fontFamily: "Inter" }}>
{photo && box && (
<div style={{ display: "flex", position: "relative", width: PHOTO_W, height: H, overflow: "hidden" }}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={`data:image/jpeg;base64,${Buffer.from(photo.data).toString("base64")}`}
width={box.width}
height={box.height}
style={{ position: "absolute", left: box.left, top: box.top }}
alt=""
/>
<div
style={{
position: "absolute",
right: 0,
top: 0,
width: 90,
height: H,
background: "linear-gradient(90deg, rgba(13,18,32,0) 0%, #0d1220 100%)",
}}
/>
</div>
)}
<div style={{ display: "flex", flexDirection: "column", flex: 1, padding: "44px 48px 36px 36px" }}>
<div style={{ display: "flex", fontSize: 17, fontWeight: 600, letterSpacing: 2.5, color: "#d9b46a" }}>
HÍRES EMBEREK HOROSZKÓPJA
</div>
<div style={{ display: "flex", fontFamily: "Cormorant Garamond", fontSize: 58, lineHeight: 1.05, marginTop: 12 }}>
{printable(name)}
</div>
{subtitle && (
<div style={{ display: "flex", fontSize: 22, color: "#c9cfdc", marginTop: 6 }}>{printable(subtitle)}</div>
)}
<div style={{ display: "flex", flex: 1, alignItems: "center", gap: 28, marginTop: 8 }}>
<Wheel chart={chart} timeKnown={timeKnown} size={RING} />
<div style={{ display: "flex", flexDirection: "column", gap: 14, fontSize: 24, color: "#c9cfdc" }}>
{lines.map((l) => (
<div key={l.label} style={{ display: "flex", alignItems: "center", gap: 10 }}>
<span style={{ fontFamily: "DejaVu Sans", color: "#d9b46a" }}>{l.glyph.replace(/︎/g, "")}</span>
<span>{printable(l.label)}</span>
</div>
))}
<div style={{ display: "flex", fontFamily: "Cormorant Garamond", fontSize: 32, color: "#ece6d8", marginTop: 18 }}>
sors<span style={{ color: "#d9b46a" }}>fonalak</span>
</div>
</div>
</div>
</div>
</div>
),
{ width: W, height: H, fonts: await loadFonts(), headers: { "Cache-Control": cacheControl } },
);
}