34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
import tmp from "tmp";
|
|
import hashPaths from "../hash.ts";
|
|
import { dest, fromGlob } from "../rx-utils.ts";
|
|
import { lastValueFrom, mergeMap } from "rxjs";
|
|
import { VFile } from "vfile";
|
|
|
|
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"] },
|
|
};
|
|
|
|
async function compileFont(font: VFile): Promise<VFile> {
|
|
const [, variant, weight] = /([A-Z][a-z]+)-(\w+)\.ttf$/.exec(font.basename!) as string[];
|
|
const tmpOutput = tmp.fileSync({ discardDescriptor: true });
|
|
const unicodes = FONT_PRESETS[variant.toLowerCase()].ranges;
|
|
|
|
await new Deno.Command("pyftsubset", {
|
|
args: [font.path, `--unicodes=${unicodes.join(",")}`, `--output-file=${tmpOutput.name}`, "--flavor=woff2"],
|
|
}).output();
|
|
|
|
font.path = `iosevka-adaedra-${variant.toLowerCase()}-${weight.toLowerCase()}.woff2`;
|
|
font.value = await Deno.readFile(tmpOutput.name);
|
|
|
|
await Deno.remove(tmpOutput.name);
|
|
return font;
|
|
}
|
|
|
|
export const fonts = async () => {
|
|
await lastValueFrom(
|
|
fromGlob("vendor/*.ttf")
|
|
.pipe(mergeMap(compileFont), hashPaths("fonts.manifest"), mergeMap(dest("dist/_assets"))),
|
|
);
|
|
};
|