From c2c903ebce1c8a9acd987a06a63e733b352d20e8 Mon Sep 17 00:00:00 2001 From: be5invis Date: Wed, 22 Dec 2021 19:51:58 -0800 Subject: [PATCH] Make webfont formats customizable (#1266). --- changes/11.2.3.md | 1 + doc/custom-build.md | 1 + private-build-plans.sample.toml | 2 ++ utility/make-webfont-css.js | 11 +++++++++-- verdafile.js | 23 +++++++++-------------- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/changes/11.2.3.md b/changes/11.2.3.md index f37b62359..117e4c362 100644 --- a/changes/11.2.3.md +++ b/changes/11.2.3.md @@ -1 +1,2 @@ + * Make webfont formats customizable (#1266). * Fix localization of Macedonian GJE (#1267). \ No newline at end of file diff --git a/doc/custom-build.md b/doc/custom-build.md index a83e3cfef..9b6e38003 100644 --- a/doc/custom-build.md +++ b/doc/custom-build.md @@ -63,6 +63,7 @@ Inside the plan, top-level properties include: * `no-cv-ss`: Optional, Boolean, disables `cv##` and `ss##` OpenType features. * `no-ligation`: Optional, Boolean, disables ligations. * `export-glyph-names`: Optional, Boolean, whether to export glyph names into the fonts. Setting this to `true` will increase file footprint, however this is necessary for ligature support in [Kitty](https://sw.kovidgoyal.net/kitty/). +* `webfont-formats`: Optional, Array of String. Controls the formats needed to be exported into the webfont CSS. Valid options are `'ttf'` and `'woff2'`, or including both. Build plan could have 5 optional subsections: `ligations`, `variants`, `weights`, `widths` and `slopes`. diff --git a/private-build-plans.sample.toml b/private-build-plans.sample.toml index 3bdafb7a7..e0614e1d4 100644 --- a/private-build-plans.sample.toml +++ b/private-build-plans.sample.toml @@ -3,6 +3,8 @@ family = "Iosevka Custom" # Font menu family name spacing = "normal" # Optional; Values: `normal`, `term`, `fontconfig-mono`, or `fixed` serifs = "sans" # Optional; Values: `sans` or `slab` +webfont-formats = ['ttf', 'woff2'] + ################################################################################################### # Configure variants diff --git a/utility/make-webfont-css.js b/utility/make-webfont-css.js index 39f7d2c0e..0cfd6de66 100644 --- a/utility/make-webfont-css.js +++ b/utility/make-webfont-css.js @@ -2,11 +2,18 @@ const fs = require("fs"); +const WebfontFormatMap = new Map([ + ["woff2", "woff2"], + ["ttf", "truetype"] +]); module.exports = function (output, family, hs, formats) { let ans = ``; + for (const ext of formats) { + if (!WebfontFormatMap.get(ext)) throw new TypeError("Invalid webfont file format " + ext); + } for (const term of hs) { - let src = formats - .map(([ext, format]) => `url('${ext}/${term.name}.${ext}') format('${format}')`) + const src = formats + .map(ext => `url('${ext}/${term.name}.${ext}') format('${WebfontFormatMap.get(ext)}')`) .join(", "); ans += ` @font-face { diff --git a/verdafile.js b/verdafile.js index 7b31c992f..7144d5d19 100644 --- a/verdafile.js +++ b/verdafile.js @@ -26,12 +26,10 @@ const ARCHIVE_DIR = "release-archives"; const PATEL_C = ["node", "node_modules/patel/bin/patel-c"]; const TTCIZE = ["node", "node_modules/otb-ttc-bundle/bin/otb-ttc-bundle"]; -const webfontFormats = [ - ["woff2", "woff2"], - ["ttf", "truetype"] -]; -const webfontFormatsFast = [["ttf", "truetype"]]; -const webfontFormatsPages = [["woff2", "woff2"]]; + +const defaultWebFontFormats = ["ttf", "woff2"]; +const webfontFormatsFast = ["ttf"]; +const webfontFormatsPages = ["woff2"]; const WIDTH_NORMAL = "normal"; const WEIGHT_NORMAL = "regular"; @@ -129,7 +127,7 @@ const BuildPlans = computed("metadata:build-plans", async target => { for (const prefix in rawBuildPlans) { const bp = { ...rawBuildPlans[prefix] }; validateAndShimBuildPlans(prefix, bp, rp.weights, rp.slopes, rp.widths); - + bp.webfontFormats = bp["webfont-formats"] || defaultWebFontFormats; bp.targets = []; const weights = bp.weights, slopes = bp.slopes, @@ -314,8 +312,9 @@ const GroupContents = task.group("contents", async (target, gr) => { const DistWebFontCSS = file.make( gr => `${DIST}/${gr}/${gr}.css`, async (target, out, gr) => { + const [plan] = await target.need(BuildPlanOf(gr)); await target.need(de(out.dir)); - await createWebFontCssImpl(target, out.full, gr, webfontFormats); + await createWebFontCssImpl(target, out.full, gr, plan.webfontFormats); } ); async function createWebFontCssImpl(target, output, gr, formats) { @@ -547,14 +546,10 @@ const GroupTtfUnhintedArchiveFile = file.make( const GroupWebArchiveFile = file.make( (gr, version) => `${ARCHIVE_DIR}/webfont-${gr}-${version}.zip`, async (target, out, gr) => { + const [plan] = await target.need(BuildPlanOf(gr)); await target.need(de`${out.dir}`); await target.need(GroupContents(gr)); - await CreateGroupArchiveFile( - `${DIST}/${gr}`, - out, - "*.css", - ...webfontFormats.map(([suffix, cssFormat]) => suffix) - ); + await CreateGroupArchiveFile(`${DIST}/${gr}`, out, "*.css", ...plan.webfontFormats); } );