blog/lib/tasks/fonts.ts

34 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

import tmp from "tmp";
2024-06-27 04:22:58 +00:00
import hashPaths from "../hash.ts";
import { dest, fromGlob } from "../rx-utils.ts";
2024-06-27 06:51:56 +00:00
import { lastValueFrom, mergeMap } from "rxjs";
2024-06-27 08:06:39 +00:00
import { VFile } from "vfile";
2024-06-27 04:22:58 +00:00
const FONT_PRESETS: { [variant: string]: { ranges: string[] } } = {
mono: { ranges: ["20-7F", "2205", "2E22-2E25", "2713", "2717"] },
text: { ranges: ["20-7F", "A0-FF", "2000-206F", "20AC"] },
};
2024-06-27 08:06:39 +00:00
async function compileFont(font: VFile): Promise<VFile> {
const [, variant, weight] = /([A-Z][a-z]+)-(\w+)\.ttf$/.exec(font.basename!) as string[];
2024-06-26 21:42:33 +00:00
const tmpOutput = tmp.fileSync({ discardDescriptor: true });
const unicodes = FONT_PRESETS[variant.toLowerCase()].ranges;
2024-06-27 07:03:53 +00:00
await new Deno.Command("pyftsubset", {
args: [font.path, `--unicodes=${unicodes.join(",")}`, `--output-file=${tmpOutput.name}`, "--flavor=woff2"],
}).output();
2024-06-27 07:03:53 +00:00
font.path = `iosevka-adaedra-${variant.toLowerCase()}-${weight.toLowerCase()}.woff2`;
2024-06-27 08:06:39 +00:00
font.value = await Deno.readFile(tmpOutput.name);
2024-06-27 07:03:53 +00:00
await Deno.remove(tmpOutput.name);
return font;
}
2024-06-27 06:51:56 +00:00
export const fonts = async () => {
await lastValueFrom(
fromGlob("vendor/*.ttf")
.pipe(mergeMap(compileFont), hashPaths("fonts.manifest"), mergeMap(dest("dist/_assets"))),
);
};