// 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";
import type { ChartResult } from "./types";
import { LABEL_STYLE, WHEEL_UNITS, wheelSvg } from "./wheelSvg";
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 (
{/* eslint-disable-next-line @next/next/no-img-element */}
.toString("base64")}`})
{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 (
{style.font === "DejaVu Sans" ? l.text : printable(l.text)}
);
})}
);
}
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 {
return new ImageResponse(
(
{printable(eyebrow)}
{printable(title)}
{text && (
{printable(text)}
)}
{lines.map((l) => (
{l.glyph.replace(/︎/g, "")}
{printable(l.label)}
))}
sorsfonalak
),
{ 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 {
const PHOTO_W = 440;
const RING = 400;
const box = photo && coverBox(photo, PHOTO_W, H);
return new ImageResponse(
(
{photo && box && (
{/* eslint-disable-next-line @next/next/no-img-element */}
)}
HÍRES EMBEREK HOROSZKÓPJA
{printable(name)}
{subtitle && (
{printable(subtitle)}
)}
{lines.map((l) => (
{l.glyph.replace(/︎/g, "")}
{printable(l.label)}
))}
sorsfonalak
),
{ width: W, height: H, fonts: await loadFonts(), headers: { "Cache-Control": cacheControl } },
);
}