#!/bin/sh # Fetch Timeless Sans, the face Base is set in. # # The Timeless Free Font License 1.2 allows embedding in a website but # forbids putting the fonts in a public repository, so the repo holds this # fetch step and a pinned checksum instead of the font. The file lands in # internal/look/static/fonts/timeless/, which is git-ignored, and `go build` # embeds it from there. Without it the site still builds and runs, set in # DM Sans. # # A release is never built without it: `make release` runs this script and # then `hack/fonts.sh check`, which fails unless the file is there and is # exactly the pinned font. # # make fonts fetch it (needs the network: media.timeless.co) # FORCE=1 make fonts fetch it again even if it is already there # hack/fonts.sh check check the file that is there; never downloads set -eu VERSION=1.094 URL="https://media.timeless.co/type/Timeless-Type-Family-$VERSION.zip" SHA256=510685dfce6010b74a1a250af7de1204e1b9901be152a39f5dd6be85fc6d6084 MEMBER=Timeless-Type-Family-$VERSION/Sans-Grotesk/TimelessSansVF.woff2 # The extracted file, from the zip above, so a file already in place can be # checked without the network. FONT_SHA256=353b3fbb75b8e9b927db12d520ff8d446d89c36897b47a90d70dbe36e9eeb23a root=$(cd "$(dirname "$0")/.." && pwd) dest="$root/internal/look/static/fonts/timeless/$VERSION" font="$dest/TimelessSansVF.woff2" sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 else shasum -a 256 "$1" | cut -d' ' -f1 fi } # verify FILE WANT fails loudly unless FILE's SHA-256 is WANT. verify() { got=$(sha256 "$1") if [ "$got" != "$2" ]; then echo "fonts: checksum mismatch for $1" >&2 echo "fonts: want $2" >&2 echo "fonts: got $got" >&2 return 1 fi } case "${1:-}" in check) if [ ! -f "$font" ]; then echo "fonts: $font is not there; run make fonts" >&2 exit 1 fi verify "$font" "$FONT_SHA256" || { echo "fonts: FORCE=1 make fonts fetches it again" >&2; exit 1; } echo "fonts: $font is the pinned Timeless Sans" exit 0 ;; "") ;; *) echo "usage: hack/fonts.sh [check]" >&2 exit 2 ;; esac if [ -f "$font" ] && [ -z "${FORCE:-}" ]; then verify "$font" "$FONT_SHA256" || { echo "fonts: FORCE=1 make fonts fetches it again" >&2; exit 1; } echo "fonts: $font is already there (FORCE=1 to fetch again)" exit 0 fi # Scratch space on disk, never a bare /tmp, which on shared machines is a # RAM disk everyone shares: $TMPDIR when set, else a directory in the user # cache. Both the scratch and that directory, if this made it, go at exit. if [ -n "${TMPDIR:-}" ]; then scratch=$TMPDIR made= else scratch="${XDG_CACHE_HOME:-$HOME/.cache}/base-fonts" made= [ -d "$scratch" ] || made=1 mkdir -p "$scratch" fi work=$(mktemp -d "$scratch/base-fonts.XXXXXX") cleanup() { rm -rf "$work" if [ -n "$made" ]; then rmdir "$scratch" 2>/dev/null || true; fi } trap cleanup EXIT trap 'exit 1' INT TERM echo "fonts: downloading $URL" curl --fail --silent --show-error --location --proto '=https' --output "$work/timeless.zip" "$URL" verify "$work/timeless.zip" "$SHA256" || { echo "fonts: (downloaded from $URL)" >&2; exit 1; } # Only the one file comes out of the zip, into a scratch directory first, # so a failed extraction leaves nothing half-installed. mkdir "$work/out" if command -v unzip >/dev/null 2>&1; then unzip -q -j -o "$work/timeless.zip" "$MEMBER" -d "$work/out" else python3 -I -c ' import sys, os, zipfile zf, member, out = sys.argv[1:4] with zipfile.ZipFile(zf) as z, z.open(member) as src: with open(os.path.join(out, os.path.basename(member)), "wb") as dst: dst.write(src.read()) ' "$work/timeless.zip" "$MEMBER" "$work/out" fi verify "$work/out/TimelessSansVF.woff2" "$FONT_SHA256" mkdir -p "$dest" mv "$work/out/TimelessSansVF.woff2" "$font" echo "fonts: wrote $font"