Skip to content

Commit

Permalink
style: format script dir
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlj95 committed Sep 28, 2023
1 parent fae1ce3 commit 7be2ae3
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 199 deletions.
81 changes: 48 additions & 33 deletions scripts/src/generate-font-subsets.mts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import * as fs from 'fs';
import subsetFont from 'subset-font';
import { isMain, Log } from './utils.mjs';
import MaterialSymbols from '../../src/app/material-symbols.js';
import * as fs from "fs";
import subsetFont from "subset-font";
import MaterialSymbols from "../../src/app/material-symbols.js";
import { isMain, Log } from "./utils.mjs";

async function generateFonts() {
Log.info("Generating font subset for Material Symbols Outlined")
const materialSymbolsFont = fs.readFileSync('assets/material-symbols-outlined.woff2');
Log.info("Generating font subset for Material Symbols Outlined");
const materialSymbolsFont = fs.readFileSync(
"assets/material-symbols-outlined.woff2",
);
const fontBuffer = Buffer.from(materialSymbolsFont);

// If using ligatures, file size increases by mystery
const glyphs = Object.values(MaterialSymbols);
Log.info("%d glyphs to include in font", glyphs.length)
Log.info("%d glyphs to include in font", glyphs.length);

const glyphText = glyphs.join('');
const glyphText = glyphs.join("");

Log.info("Output")
const baseFilename = 'assets/material-symbols-outlined-subset';
const formats = ['ttf', 'woff', 'woff2']
Log.info("Output");
const baseFilename = "assets/material-symbols-outlined-subset";
const formats = ["ttf", "woff", "woff2"];
Log.item("Base filename: '%s'", baseFilename);
Log.item("Formats: '%s'", formats);

Expand All @@ -26,44 +28,57 @@ async function generateFonts() {
// woff, ttf just to support IE users
// https://caniuse.com/woff
// https://caniuse.com/ttf
formats: ['truetype', 'woff', 'woff2'],
formats: ["truetype", "woff", "woff2"],
filename: baseFilename,
});
Log.ok('Done')
Log.ok("Done");
}

async function generateFontSubsets(fontBuffer: Buffer, {
text,
formats,
filename,
}: Omit<GenerateFontSubsetOptions, 'format'> & { formats: Array<FontFormat> }) {
async function generateFontSubsets(
fontBuffer: Buffer,
{
text,
formats,
filename,
}: Omit<GenerateFontSubsetOptions, "format"> & { formats: Array<FontFormat> },
) {
await Promise.all(
formats.map((format) => generateFontSubset(fontBuffer, {
text: text,
format: format,
filename: filename,
})),
)
formats.map((format) =>
generateFontSubset(fontBuffer, {
text: text,
format: format,
filename: filename,
}),
),
);
}

interface GenerateFontSubsetOptions {
text: string,
format: FontFormat,
filename: string
text: string;
format: FontFormat;
filename: string;
}

type FontFormat = 'truetype' | 'woff' | 'woff2'; // any of the supported ones (see SubsetFontOptions type)
type FontFormat = "truetype" | "woff" | "woff2"; // any of the supported ones (see SubsetFontOptions type)

async function generateFontSubset(fontBuffer: Buffer, {text, format, filename}: GenerateFontSubsetOptions) {
const subsetBuffer = await subsetFont(fontBuffer, text, {targetFormat: format});
fs.writeFileSync(`${filename}.${getExtensionFromFormat(format)}`, subsetBuffer);
async function generateFontSubset(
fontBuffer: Buffer,
{ text, format, filename }: GenerateFontSubsetOptions,
) {
const subsetBuffer = await subsetFont(fontBuffer, text, {
targetFormat: format,
});
fs.writeFileSync(
`${filename}.${getExtensionFromFormat(format)}`,
subsetBuffer,
);
}

function getExtensionFromFormat(format: FontFormat): string {
if (format == 'truetype') return 'ttf';
if (format == "truetype") return "ttf";
return format;
}

if (isMain(import.meta.url)) {
await generateFonts()
await generateFonts();
}
Loading

0 comments on commit 7be2ae3

Please sign in to comment.