diff --git a/CLAUDE.md b/CLAUDE.md index 05b8ed8..5843c12 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -3,6 +3,8 @@ **Read before writing or reviewing code:** [context/coding-standards.md](./context/coding-standards.md). +The share card and its `?v=` rule: [context/share-card.md](./context/share-card.md). + ## Git — read before any `git` or `gh` command **Default: never stage, commit, branch, push or open a PR on your own diff --git a/apps/site/assets/share/README.md b/apps/site/assets/share/README.md new file mode 100644 index 0000000..01337fe --- /dev/null +++ b/apps/site/assets/share/README.md @@ -0,0 +1,23 @@ +# Share images + +Upload-ready images for the places that take a picture by hand. + +| File | Size | Use | +| -------------------- | ---------- | ---------------------------------------------------------------------------------------- | +| `og-1200x630.png` | 1200 x 630 | The site card, the same bytes as `public/og.png`. For profiles and listings. | +| `og-sky.png` | 1200 x 630 | The previous card. The script reads its aurora as the sky; not for upload. | + +The card stacks the shield, the wordmark and the line in a column centred in the middle +630 pixels, so an app that crops a square keeps all of it. It is a palette PNG under 300 KB, +the most WhatsApp loads. + +Regenerate it, with the same bytes each time (it writes `public/og.png` and `og-1200x630.png`): + +```sh +node scripts/build-og-card.mjs +``` + +Upload `og-1200x630.png` where a card is set by hand: the GitHub organisation profile and +any directory listing. The site itself serves `public/og.png`. + +This folder is outside `public/`, so the site build does not publish it. diff --git a/apps/site/assets/share/og-1200x630.png b/apps/site/assets/share/og-1200x630.png new file mode 100644 index 0000000..95bbe91 Binary files /dev/null and b/apps/site/assets/share/og-1200x630.png differ diff --git a/apps/site/assets/share/og-sky.png b/apps/site/assets/share/og-sky.png new file mode 100644 index 0000000..088ec4f Binary files /dev/null and b/apps/site/assets/share/og-sky.png differ diff --git a/apps/site/astro.config.mjs b/apps/site/astro.config.mjs index 8f47e6b..eb91f16 100644 --- a/apps/site/astro.config.mjs +++ b/apps/site/astro.config.mjs @@ -1,10 +1,20 @@ +import { createHash } from "node:crypto"; +import { readFileSync } from "node:fs"; import { defineConfig } from "astro/config"; import sitemap from "@astrojs/sitemap"; +// The first 12 hex digits of the share card's SHA-256, hashed once per build. +// Base.astro ends the card's URL in `?v=` plus this (context/share-card.md). +const ogCardHash = createHash("sha256") + .update(readFileSync(new URL("./public/og.png", import.meta.url))) + .digest("hex") + .slice(0, 12); + // Static output; the Worker in wrangler.jsonc serves `dist/` as assets only. export default defineConfig({ site: "https://northguild.dev", output: "static", // @astrojs/sitemap leaves the 404 and 500 pages out of the sitemap itself. integrations: [sitemap()], + vite: { define: { __OG_CARD_HASH__: JSON.stringify(ogCardHash) } }, }); diff --git a/apps/site/public/og.png b/apps/site/public/og.png index 088ec4f..95bbe91 100644 Binary files a/apps/site/public/og.png and b/apps/site/public/og.png differ diff --git a/apps/site/public/robots.txt b/apps/site/public/robots.txt index 7570d83..20a0826 100644 --- a/apps/site/public/robots.txt +++ b/apps/site/public/robots.txt @@ -7,3 +7,4 @@ Disallow: /gmt/api/ Sitemap: https://northguild.dev/sitemap-index.xml Sitemap: https://northguild.dev/gmt/sitemap-index.xml +Sitemap: https://northguild.dev/worktree/sitemap-index.xml diff --git a/apps/site/src/env.d.ts b/apps/site/src/env.d.ts new file mode 100644 index 0000000..04ee7fa --- /dev/null +++ b/apps/site/src/env.d.ts @@ -0,0 +1,2 @@ +/** The share card's hash, set by `vite.define` in astro.config.mjs. */ +declare const __OG_CARD_HASH__: string; diff --git a/apps/site/src/layouts/Base.astro b/apps/site/src/layouts/Base.astro index 27693bd..950c886 100644 --- a/apps/site/src/layouts/Base.astro +++ b/apps/site/src/layouts/Base.astro @@ -26,9 +26,11 @@ const { noindex = false, } = Astro.props; -// Social cards need absolute URLs. +// Social cards need absolute URLs. The image URL carries the card's hash +// (astro.config.mjs), so a new card gets a new address and platforms that +// cache by URL (Facebook, LinkedIn) fetch it again. const pageUrl = new URL(Astro.url.pathname, Astro.site); -const ogImage = new URL("/og.png", Astro.site); +const ogImage = new URL(`/og.png?v=${__OG_CARD_HASH__}`, Astro.site); const ogAlt = "NorthGuild: open-source tools for developers, under the northern lights."; --- @@ -53,6 +55,7 @@ const ogAlt = } + diff --git a/context/share-card.md b/context/share-card.md new file mode 100644 index 0000000..c40952b --- /dev/null +++ b/context/share-card.md @@ -0,0 +1,41 @@ +# Share card + +The picture that link-preview apps show for `northguild.dev/` and its 404 page: +`apps/site/public/og.png`, 1200 x 630, served from `/og.png`. + +## What it is + +Aurora sky, with a dark glass panel in the middle. On the panel the shield sits above the +"NorthGuild" wordmark (Michroma) and the line "Open-source tools for developers.". + +- **Square-safe.** Some apps cut a 630-pixel square from the middle of the card + (x 285 to 915). The column and the panel sit inside it with a margin, so nothing is cut. + The aurora outside the square is decoration only. +- **Under 300 KB.** WhatsApp loads no preview image over 300 KB. The card is a palette PNG + (about 145 KB). +- **Text contrast** is at least 4.5:1; the script fails below that. + +## Regenerate + +```sh +eval "$(fnm env)" && fnm use && node scripts/build-og-card.mjs +``` + +Run it by hand, never in the build. It is deterministic: the same bytes every run. It writes +`apps/site/public/og.png` and the same bytes to `apps/site/assets/share/og-1200x630.png` +(the copy to upload by hand; see the README there). + +Its sources: `apps/site/assets/share/og-sky.png` (the previous card, whose aurora is the +sky; keep it, because `og.png` is overwritten), the shield paths of +`NorthGuildMark.astro`, `scripts/fonts/Michroma-Regular.ttf` (SIL OFL text beside it) and +the `@northguild/theme` colours as RGB values. `sharp` and `canvaskit-wasm` are root dev +dependencies. + +## The `?v=` rule + +`Base.astro` ends `og:image` and `twitter:image` in `?v=` plus the first 12 hex digits of the +SHA-256 of `public/og.png`. `astro.config.mjs` hashes the file once per build and hands the +value to `Base.astro` as `__OG_CARD_HASH__`. Platforms such as Facebook and LinkedIn cache +a card by URL, so a new card needs a new URL. Change the card, rebuild, and the address +changes by itself; never edit the hash by hand. After a deploy, ask each platform's debugger +to scrape the page again. diff --git a/package.json b/package.json index 9f6b013..b998c6f 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,12 @@ "devDependencies": { "@changesets/cli": "^3.0.2", "@northguild/gmt-oxlint": "^1.2.1", + "canvaskit-wasm": "0.42.0", "geotiff": "3.0.5", "jpeg-js": "^0.4.4", "oxfmt": "^0.63.0", - "oxlint": "^1.78.0" + "oxlint": "^1.78.0", + "sharp": "0.35.5" }, "engines": { "node": ">=24" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 354095c..57f043b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@northguild/gmt-oxlint': specifier: ^1.2.1 version: 1.2.1(oxlint@1.89.0) + canvaskit-wasm: + specifier: 0.42.0 + version: 0.42.0 geotiff: specifier: 3.0.5 version: 3.0.5 @@ -26,6 +29,9 @@ importers: oxlint: specifier: ^1.78.0 version: 1.89.0 + sharp: + specifier: 0.35.5 + version: 0.35.5(@types/node@24.19.2) apps/site: dependencies: @@ -1534,6 +1540,9 @@ packages: '@vscode/l10n@0.0.18': resolution: {integrity: sha512-KYSIHVmslkaCDyw013pphY+d7x1qV8IZupYfeIfzNA+nsaWHbn5uPuQRvdRFsa9zFzGeudPuoGoZ1Op4jrJXIQ==} + '@webgpu/types@0.1.21': + resolution: {integrity: sha512-pUrWq3V5PiSGFLeLxoGqReTZmiiXwY3jRkIG5sLLKjyqNxrwm/04b4nw7LSmGWJcKk59XOM/YRTUwOzo4MMlow==} + ajv-draft-04@1.0.0: resolution: {integrity: sha512-mv00Te6nmYbRp5DCwclxtt7yV/joXJPGS7nM+97GdxvuttCOfgI3K4U25zboyeX0O+myI8ERluxQe5wljMmVIw==} peerDependencies: @@ -1607,6 +1616,9 @@ packages: resolution: {integrity: sha512-LMDU/U2yA4IPIY8KT0NsNPWEYZvAMkbgyspxJ6btJaYV+WMDOHAZCmB0lsTObiHaRS1V/ztKAl7+TSkTqXyeoA==} engines: {node: '>=20.19.0'} + canvaskit-wasm@0.42.0: + resolution: {integrity: sha512-N3ikotmzYwDVAcpQKDMptBT2P5pIEWS9ywdmrQA2awwaMF9cxKEFuxZZHv9ay1Em9kDjxDsPtwUzDm1uNj136g==} + ccount@2.0.1: resolution: {integrity: sha512-eyrF0jiFpY+3drT6383f1qhkbGsLSifNAjA61IUjZjmLCWjItY6LB9ft9YhoDgwfmclB2zhu51Lc7+95b8NRAg==} @@ -3792,6 +3804,8 @@ snapshots: '@vscode/l10n@0.0.18': {} + '@webgpu/types@0.1.21': {} + ajv-draft-04@1.0.0(ajv@8.20.0): optionalDependencies: ajv: 8.20.0 @@ -3931,6 +3945,10 @@ snapshots: cac@7.1.0: {} + canvaskit-wasm@0.42.0: + dependencies: + '@webgpu/types': 0.1.21 + ccount@2.0.1: {} chalk@4.1.2: @@ -4629,7 +4647,6 @@ snapshots: '@img/sharp-win32-ia32': 0.35.5 '@img/sharp-win32-x64': 0.35.5 '@types/node': 24.19.2 - optional: true shell-quote@1.12.0: {} diff --git a/scripts/build-og-card.mjs b/scripts/build-og-card.mjs new file mode 100644 index 0000000..db60cc4 --- /dev/null +++ b/scripts/build-og-card.mjs @@ -0,0 +1,486 @@ +// Draws the share card for https://northguild.dev/ and its 404 page: 1200 by 630. Writes +// apps/site/public/og.png (what the site serves) and the same bytes to +// apps/site/assets/share/og-1200x630.png (the copy to upload by hand). +// +// Why it is made this way: a link-preview app may cut the 630-pixel square from the middle of +// the card (x 285 to 915), and WhatsApp loads no preview image over 300 KB. So the shield, the +// wordmark and the line stack in one column centred in that square, with a margin, and the +// card is written as a palette PNG. +// +// Sources, all in this repo: +// - the sky: the aurora in apps/site/assets/share/og-sky.png, the card this one replaced. Its +// old glass panel, shield and text are painted out (a smooth blend of the sky around them, +// plus a few stars), so the aurora stays as decoration on both sides of the square. Keep +// that file: og.png is overwritten on every run. +// - the shield: the paths of apps/site/src/components/NorthGuildMark.astro. +// - the wordmark and the line: Michroma (SIL OFL), scripts/fonts/Michroma-Regular.ttf, the +// same font @northguild/theme ships as woff2. +// - colours: --gmt-ice, --gmt-ice-dim, --gmt-cyan and --gmt-void of +// @northguild/theme, as RGB triples. +// +// Run: node scripts/build-og-card.mjs (by hand, never in the build; needs sharp and +// canvaskit-wasm, both root dev dependencies). It is deterministic: the same bytes every run. +// Prints the WCAG contrast of each text against the brightest pixel behind it, and fails +// below 4.5. + +import { readFileSync, writeFileSync } from "node:fs"; +import { createRequire } from "node:module"; +import { dirname, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import CanvasKitInit from "canvaskit-wasm/full"; +import sharp from "sharp"; + +const here = dirname(fileURLToPath(import.meta.url)); +const require = createRequire(import.meta.url); +const SKY = resolve(here, "../apps/site/assets/share/og-sky.png"); +const FONT = resolve(here, "fonts/Michroma-Regular.ttf"); +const OUT_SITE = resolve(here, "../apps/site/public/og.png"); +const OUT_UPLOAD = resolve(here, "../apps/site/assets/share/og-1200x630.png"); + +const W = 1200; +const H = 630; +/** The square a link-preview app cuts from the middle of the card. */ +const SAFE_LEFT = 285; +const SAFE_RIGHT = 915; +const CENTER = W / 2; + +const VOID = [3, 8, 12]; +const ICE = [207, 234, 242]; +const ICE_DIM = [152, 178, 190]; +const CYAN = [34, 211, 238]; + +/** The shield's shapes, from NorthGuildMark.astro, in a 1000 by 1317 box. */ +const SHIELD = { + w: 1000, + h: 1317, + outline: + "M500 0 L0 179.1 L0 709.1 C0 1018.6 373.1 1206.2 500 1317 C626.9 1206.2 1000 1018.6 1000 709.1 L1000 179.1 Z", + lit: "M500 0 L0 179.1 L0 709.1 C0 1018.6 373.1 1206.2 500 1317 Z", + field: + "M93.3 243.9 L500 99 L906.7 243.9 L906.7 686.5 L709.3 390.2 L654.9 470.3 L500 239.5 L345.1 470.3 L290.7 390.2 L93.3 686.5 Z", + ridge: + "M93.3 686.5 L290.7 390.2 L345.1 470.3 L500 239.5 L654.9 470.3 L709.3 390.2 L906.7 686.5", + glyph: + "M366 640 L212 736.5 L366 833 M634 640 L788 736.5 L634 833 M560 560 L443 910", +}; + +/** The column, top to bottom, in pixels. The block is centred on the card's middle row. */ +const SHIELD_HEIGHT = 190; +const GAP_SHIELD = 26; +const GAP_LINE = 20; +/** The widest the wordmark and the line may be: the square less a margin each side. */ +const MARGIN = 48; +const TEXT_WIDTH = SAFE_RIGHT - SAFE_LEFT - 2 * MARGIN; +const WORDMARK = "NorthGuild"; +const LINE = "Open-source tools for developers."; +/** Panel behind the column. */ +const PANEL = { x: SAFE_LEFT + 14, w: SAFE_RIGHT - SAFE_LEFT - 28 }; +/** The 45-degree cut on each of the panel's four corners: the theme's bevel (.gmt-glass uses + 16px at page scale), enlarged to read at card size. */ +const CHAMFER = 26; +/** Width of the panel's edge and hairline, in card pixels. */ +const PANEL_LINE = 1.5; + +/** Relative luminance of an sRGB colour (WCAG 2.2). */ +function linearChannel(v) { + const c = v / 255; + return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; +} +function luminance(r, g, b) { + return ( + 0.2126 * linearChannel(r) + + 0.7152 * linearChannel(g) + + 0.0722 * linearChannel(b) + ); +} + +/** A small seeded generator, so the stars are the same on every run. */ +function seededRandom(seed) { + let a = seed >>> 0; + return function next() { + a = (a + 0x6d2b79f5) >>> 0; + let t = a; + t = Math.imul(t ^ (t >>> 15), t | 1); + t ^= t + Math.imul(t ^ (t >>> 7), t | 61); + return ((t ^ (t >>> 14)) >>> 0) / 4294967296; + }; +} + +/** + * The sky with the old panel, shield and text painted out: the region inside the four anchor + * lines is a Coons patch of the pixels on those lines (each smoothed along its length), so it + * is the aurora's own colours fading from side to side and top to bottom. + */ +async function cleanSky() { + const { data } = await sharp(SKY) + .ensureAlpha() + .raw() + .toBuffer({ resolveWithObject: true }); + const px = Uint8Array.from(data); + const x0 = 86; + const x1 = 1114; + const y0 = 128; + const y1 = 502; + function channelAt(x, y, c) { + return px[(y * W + x) * 4 + c]; + } + /** Each value replaced by the mean of its neighbours within `radius`. */ + function smooth(values, radius) { + return values.map(function meanAround(_, i) { + let sum = 0; + let n = 0; + for (let k = -radius; k <= radius; k++) { + const j = i + k; + if (j >= 0 && j < values.length) { + sum += values[j]; + n++; + } + } + return sum / n; + }); + } + function sampleLine(count, get) { + return Array.from({ length: count }, function sample(_, i) { + return get(i); + }); + } + const edges = [0, 1, 2].map((c) => ({ + top: smooth( + sampleLine( + x1 - x0 + 1, + (i) => (channelAt(x0 + i, y0, c) + channelAt(x0 + i, y0 - 1, c)) / 2, + ), + 9, + ), + bottom: smooth( + sampleLine( + x1 - x0 + 1, + (i) => (channelAt(x0 + i, y1, c) + channelAt(x0 + i, y1 + 1, c)) / 2, + ), + 9, + ), + left: smooth( + sampleLine( + y1 - y0 + 1, + (j) => (channelAt(x0, y0 + j, c) + channelAt(x0 - 1, y0 + j, c)) / 2, + ), + 9, + ), + right: smooth( + sampleLine( + y1 - y0 + 1, + (j) => (channelAt(x1, y0 + j, c) + channelAt(x1 + 1, y0 + j, c)) / 2, + ), + 9, + ), + })); + for (let y = y0; y <= y1; y++) { + const t = (y - y0) / (y1 - y0); + for (let x = x0; x <= x1; x++) { + const s = (x - x0) / (x1 - x0); + const i = (y * W + x) * 4; + for (let c = 0; c < 3; c++) { + const e = edges[c]; + const v = + (1 - s) * e.left[y - y0] + + s * e.right[y - y0] + + (1 - t) * e.top[x - x0] + + t * e.bottom[x - x0] - + ((1 - s) * (1 - t) * e.left[0] + + s * (1 - t) * e.right[0] + + (1 - s) * t * e.left[y1 - y0] + + s * t * e.right[y1 - y0]); + px[i + c] = Math.max(0, Math.min(255, Math.round(v))); + } + px[i + 3] = 255; + } + } + // A few stars where the old card's were painted out; the same each run. + const random = seededRandom(20260511); + for (let n = 0; n < 70; n++) { + const x = Math.round(x0 + 4 + random() * (x1 - x0 - 8)); + const y = Math.round(y0 + 4 + random() * (y1 - y0 - 8)); + const power = 0.25 + random() * 0.55; + for (let c = 0; c < 3; c++) { + const i = (y * W + x) * 4 + c; + px[i] = Math.round(px[i] + (ICE[c] - px[i]) * power); + } + } + return px; +} + +const CanvasKit = await CanvasKitInit({ + locateFile: (file) => require.resolve(`canvaskit-wasm/bin/full/${file}`), +}); +const fontBytes = readFileSync(FONT); +const fontMgr = CanvasKit.FontMgr.FromData( + fontBytes.buffer.slice( + fontBytes.byteOffset, + fontBytes.byteOffset + fontBytes.byteLength, + ), +); +if (!fontMgr) throw new Error("cannot load Michroma"); + +function rgba(c, a = 1) { + return CanvasKit.Color(c[0], c[1], c[2], a); +} + +function paragraph(text, size, color) { + const style = new CanvasKit.ParagraphStyle({ + textAlign: CanvasKit.TextAlign.Center, + textStyle: { + color: rgba(color), + fontFamilies: ["Michroma"], + fontSize: size, + heightMultiplier: 1.2, + }, + }); + const builder = CanvasKit.ParagraphBuilder.Make(style, fontMgr); + builder.addText(text); + const built = builder.build(); + builder.delete(); + built.layout(2000); + return built; +} + +/** The largest whole size at which `text` is no wider than `width`. */ +function fitSize(text, width, max, min) { + for (let size = max; size >= min; size--) { + const built = paragraph(text, size, ICE); + const w = built.getLongestLine(); + built.delete(); + if (w <= width) return size; + } + throw new Error(`"${text}" does not fit ${width}px`); +} + +const wordmarkSize = fitSize(WORDMARK, TEXT_WIDTH, 120, 40); +const lineSize = fitSize(LINE, TEXT_WIDTH, 40, 14); + +function measure(text, size) { + const built = paragraph(text, size, ICE); + const out = { + width: built.getLongestLine(), + height: built.getHeight(), + }; + built.delete(); + return out; +} +const wm = measure(WORDMARK, wordmarkSize); +const ln = measure(LINE, lineSize); +const shieldHeight = SHIELD_HEIGHT; +const shieldWidth = (SHIELD.w / SHIELD.h) * shieldHeight; +const block = shieldHeight + GAP_SHIELD + wm.height + GAP_LINE + ln.height; +const top = Math.round((H - 8 - block) / 2); +const shieldY = top; +const wordmarkY = shieldY + shieldHeight + GAP_SHIELD; +const lineY = wordmarkY + wm.height + GAP_LINE; +const panelY = top - 34; +const panelH = block + 68; + +const sky = await cleanSky(); +const skyImage = CanvasKit.MakeImage( + { + width: W, + height: H, + colorType: CanvasKit.ColorType.RGBA_8888, + alphaType: CanvasKit.AlphaType.Unpremul, + colorSpace: CanvasKit.ColorSpace.SRGB, + }, + sky, + W * 4, +); +const surface = CanvasKit.MakeSurface(W, H); +const canvas = surface.getCanvas(); +canvas.drawImage(skyImage, 0, 0); + +// The glass panel the column sits on, as .gmt-glass in @northguild/theme draws it: a dark +// wash, a faint cyan edge, a hairline that glints on the top-left and bottom-right edges, and +// a soft highlight along the top. Four straight sides and four 45-degree cuts; no arcs. +const panelRight = PANEL.x + PANEL.w; +const panelBottom = panelY + panelH; +const panel = CanvasKit.Path.MakeFromSVGString( + [ + `M${PANEL.x + CHAMFER} ${panelY}`, + `L${panelRight - CHAMFER} ${panelY}`, + `L${panelRight} ${panelY + CHAMFER}`, + `L${panelRight} ${panelBottom - CHAMFER}`, + `L${panelRight - CHAMFER} ${panelBottom}`, + `L${PANEL.x + CHAMFER} ${panelBottom}`, + `L${PANEL.x} ${panelBottom - CHAMFER}`, + `L${PANEL.x} ${panelY + CHAMFER}`, + "Z", + ].join(" "), +); +const wash = new CanvasKit.Paint(); +wash.setAntiAlias(true); +wash.setColor(rgba(VOID, 0.55)); +canvas.drawPath(panel, wash); +const edge = new CanvasKit.Paint(); +edge.setAntiAlias(true); +edge.setStyle(CanvasKit.PaintStyle.Stroke); +edge.setStrokeWidth(PANEL_LINE); +edge.setStrokeJoin(CanvasKit.StrokeJoin.Miter); +edge.setColor(rgba(CYAN, 0.18)); +canvas.drawPath(panel, edge); +// The hairline: CSS's 135deg gradient runs corner to corner of the box. +const reach = ((PANEL.w + panelH) * Math.SQRT1_2) / 2; +const midX = PANEL.x + PANEL.w / 2; +const midY = panelY + panelH / 2; +const glint = new CanvasKit.Paint(); +glint.setAntiAlias(true); +glint.setStyle(CanvasKit.PaintStyle.Stroke); +glint.setStrokeWidth(PANEL_LINE); +glint.setStrokeJoin(CanvasKit.StrokeJoin.Miter); +glint.setShader( + CanvasKit.Shader.MakeLinearGradient( + [midX - reach * Math.SQRT1_2, midY - reach * Math.SQRT1_2], + [midX + reach * Math.SQRT1_2, midY + reach * Math.SQRT1_2], + [rgba(CYAN, 0.42), rgba(CYAN, 0), rgba(CYAN, 0), rgba(CYAN, 0.42)], + [0, 0.4, 0.6, 1], + CanvasKit.TileMode.Clamp, + ), +); +canvas.drawPath(panel, glint); +const highlight = new CanvasKit.Paint(); +highlight.setShader( + CanvasKit.Shader.MakeLinearGradient( + [PANEL.x + CHAMFER, 0], + [panelRight - CHAMFER, 0], + [rgba(ICE, 0), rgba(ICE, 0.08), rgba(ICE, 0.08), rgba(ICE, 0)], + [0, 0.3, 0.7, 1], + CanvasKit.TileMode.Clamp, + ), +); +canvas.drawRect( + CanvasKit.XYWHRect(PANEL.x + CHAMFER, panelY + 1, PANEL.w - 2 * CHAMFER, 1), + highlight, +); + +// Brightest pixel behind each text box, read after the panel and before the text. +function readBack() { + return canvas.readPixels(0, 0, { + width: W, + height: H, + colorType: CanvasKit.ColorType.RGBA_8888, + alphaType: CanvasKit.AlphaType.Unpremul, + colorSpace: CanvasKit.ColorSpace.SRGB, + }); +} +const behind = readBack(); +function worstContrast(box, color) { + let max = 0; + for (let y = Math.floor(box.y); y < Math.ceil(box.y + box.height); y++) { + for (let x = Math.floor(box.x); x < Math.ceil(box.x + box.width); x++) { + const i = (y * W + x) * 4; + max = Math.max(max, luminance(behind[i], behind[i + 1], behind[i + 2])); + } + } + return (luminance(...color) + 0.05) / (max + 0.05); +} +const wordmarkBox = { + x: CENTER - wm.width / 2, + y: wordmarkY, + width: wm.width, + height: wm.height, +}; +const lineBox = { + x: CENTER - ln.width / 2, + y: lineY, + width: ln.width, + height: ln.height, +}; +const contrast = { + wordmark: worstContrast(wordmarkBox, ICE), + line: worstContrast(lineBox, ICE_DIM), +}; + +// Shield. +canvas.save(); +canvas.translate(CENTER - shieldWidth / 2, shieldY); +canvas.scale(shieldWidth / SHIELD.w, shieldHeight / SHIELD.h); +function pathFromSvg(d) { + return CanvasKit.Path.MakeFromSVGString(d); +} +function fill(d, shader, alpha = 1) { + const p = new CanvasKit.Paint(); + p.setAntiAlias(true); + p.setShader(shader); + p.setAlphaf(alpha); + const shape = pathFromSvg(d); + canvas.drawPath(shape, p); + shape.delete(); + p.delete(); +} +function linearGradient(c, from, to, x2, y2) { + return CanvasKit.Shader.MakeLinearGradient( + [0, 0], + [x2, y2], + [rgba(c, from), rgba(c, to)], + [0, 1], + CanvasKit.TileMode.Clamp, + ); +} +fill(SHIELD.outline, linearGradient(ICE, 0.2, 0.03, 0, SHIELD.h)); +fill(SHIELD.lit, linearGradient(ICE, 0.14, 0, SHIELD.w * 0.7, SHIELD.h * 0.7)); +fill(SHIELD.field, linearGradient(ICE, 0.2, 0.03, 0, SHIELD.h)); +// Hairlines are one device pixel, so divide by the scale. +function stroke(d, color, alpha, widthPx, cap = true) { + const p = new CanvasKit.Paint(); + p.setAntiAlias(true); + p.setStyle(CanvasKit.PaintStyle.Stroke); + p.setStrokeWidth(widthPx); + p.setStrokeJoin(CanvasKit.StrokeJoin.Round); + if (cap) p.setStrokeCap(CanvasKit.StrokeCap.Round); + p.setColor(rgba(color, alpha)); + const shape = pathFromSvg(d); + canvas.drawPath(shape, p); + shape.delete(); + p.delete(); +} +const unit = SHIELD.w / shieldWidth; +stroke(SHIELD.outline, ICE, 0.85, 2.2 * unit); +stroke(SHIELD.ridge, CYAN, 0.9, 2.2 * unit); +stroke(SHIELD.glyph, ICE, 0.95, 36); +canvas.restore(); + +// Text. +const wordmarkText = paragraph(WORDMARK, wordmarkSize, ICE); +canvas.drawParagraph(wordmarkText, CENTER - 1000, wordmarkY); +const lineText = paragraph(LINE, lineSize, ICE_DIM); +canvas.drawParagraph(lineText, CENTER - 1000, lineY); +wordmarkText.delete(); +lineText.delete(); + +const pixels = Uint8Array.from(readBack()); +surface.dispose(); + +const png = await sharp(pixels, { raw: { width: W, height: H, channels: 4 } }) + .removeAlpha() + .png({ + palette: true, + quality: 100, + effort: 1, + dither: 0, + compressionLevel: 9, + }) + .toBuffer(); +// Check before writing, so a failing card never replaces the good one. +if (contrast.wordmark < 4.5 || contrast.line < 4.5) { + throw new Error( + `a text falls below 4.5:1 (wordmark ${contrast.wordmark.toFixed(2)}, line ${contrast.line.toFixed(2)}); nothing written`, + ); +} +writeFileSync(OUT_SITE, png); +writeFileSync(OUT_UPLOAD, png); + +console.log(`wrote ${OUT_SITE} and ${OUT_UPLOAD} (${png.length} bytes)`); +console.log( + `wordmark ${wordmarkSize}px, ${wm.width.toFixed(0)} wide; line ${lineSize}px, ${ln.width.toFixed(0)} wide`, +); +console.log( + `column y ${shieldY}..${Math.round(lineY + ln.height)}, panel x ${PANEL.x}..${PANEL.x + PANEL.w}`, +); +console.log( + `contrast: wordmark ${contrast.wordmark.toFixed(2)}, line ${contrast.line.toFixed(2)}`, +); diff --git a/scripts/fonts/Michroma-OFL.txt b/scripts/fonts/Michroma-OFL.txt new file mode 100644 index 0000000..2d0610c --- /dev/null +++ b/scripts/fonts/Michroma-OFL.txt @@ -0,0 +1,93 @@ +Copyright 2011 The Michroma Project Authors (https://github.com/googlefonts/Michroma-font) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/scripts/fonts/Michroma-Regular.ttf b/scripts/fonts/Michroma-Regular.ttf new file mode 100644 index 0000000..59e8c53 Binary files /dev/null and b/scripts/fonts/Michroma-Regular.ttf differ