Caching code cleanup

This commit is contained in:
be5invis 2021-04-17 14:58:59 -07:00
parent e28ce0fd68
commit edbe5ff47e
6 changed files with 68 additions and 31 deletions

46
font-src/caching/index.js Normal file
View file

@ -0,0 +1,46 @@
"use strict";
const fs = require("fs-extra");
const zlib = require("zlib");
const EDITION = "Iosevka.PTCache.1";
class Cache {
constructor() {
this.ptSource = {};
this.ptSink = {};
}
loadRep(rep) {
if (!rep || rep.edition !== EDITION) return;
this.ptSource = rep.pt;
}
toRep() {
return { edition: EDITION, pt: this.ptSink };
}
// Path conversion cache
getPT(ck) {
return this.ptSource[ck];
}
savePT(ck, val) {
this.ptSink[ck] = val;
}
}
exports.load = async function (argv) {
let cache = new Cache();
if (argv.oCache && fs.existsSync(argv.oCache)) {
cache.loadRep(unzip(await fs.readFile(argv.oCache)));
}
return cache;
};
exports.save = async function savePTCache(argv, cache) {
if (argv.oCache) await fs.writeFile(argv.oCache, zip(cache.toRep()));
};
function unzip(buf) {
return JSON.parse(zlib.gunzipSync(buf));
}
function zip(obj) {
return zlib.gzipSync(Buffer.from(JSON.stringify(obj), "utf-8"));
}

View file

@ -9,7 +9,7 @@ const { buildOtl } = require("../otl/index");
const { assignFontNames } = require("../meta/naming"); const { assignFontNames } = require("../meta/naming");
const { copyFontMetrics } = require("../meta/aesthetics"); const { copyFontMetrics } = require("../meta/aesthetics");
module.exports = async function (argv, para, ptCache) { module.exports = async function (cache, para) {
const otd = EmptyFont(); const otd = EmptyFont();
const gs = buildGlyphs(para); const gs = buildGlyphs(para);
@ -29,7 +29,7 @@ module.exports = async function (argv, para, ptCache) {
} }
} }
const finalGs = finalizeFont(para, gs.glyphStore, excludeChars, otd, ptCache); const finalGs = finalizeFont(cache, para, gs.glyphStore, excludeChars, otd);
const font = convertOtd(otd, finalGs); const font = convertOtd(otd, finalGs);
return { font, glyphStore: finalGs }; return { font, glyphStore: finalGs };
}; };

View file

@ -6,15 +6,15 @@ const CurveUtil = require("../../support/curve-util");
const util = require("util"); const util = require("util");
module.exports = finalizeGlyphs; module.exports = finalizeGlyphs;
function finalizeGlyphs(para, glyphStore, ptCache) { function finalizeGlyphs(cache, para, glyphStore) {
const skew = Math.tan(((para.slopeAngle || 0) / 180) * Math.PI); const skew = Math.tan(((para.slopeAngle || 0) / 180) * Math.PI);
regulateGlyphStore(skew, glyphStore, ptCache); regulateGlyphStore(cache, skew, glyphStore);
return glyphStore; return glyphStore;
} }
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
function regulateGlyphStore(skew, glyphStore, ptCache) { function regulateGlyphStore(cache, skew, glyphStore) {
for (const g of glyphStore.glyphs()) { for (const g of glyphStore.glyphs()) {
if (g.geometry.isEmpty()) continue; if (g.geometry.isEmpty()) continue;
if (!regulateCompositeGlyph(glyphStore, g)) { if (!regulateCompositeGlyph(glyphStore, g)) {
@ -24,7 +24,7 @@ function regulateGlyphStore(skew, glyphStore, ptCache) {
} }
} }
for (const g of glyphStore.glyphs()) { for (const g of glyphStore.glyphs()) {
if (!g.geometry.asReferences()) regulateSimpleGlyph(g, skew, ptCache); if (!g.geometry.asReferences()) regulateSimpleGlyph(cache, skew, g);
} }
} }
@ -40,10 +40,10 @@ function regulateCompositeGlyph(glyphStore, g) {
return true; return true;
} }
function regulateSimpleGlyph(g, skew, ptCache) { function regulateSimpleGlyph(cache, skew, g) {
let cs = g.geometry.asContours(); let cs = g.geometry.asContours();
for (const contour of cs) for (const z of contour) z.x -= z.y * skew; for (const contour of cs) for (const z of contour) z.x -= z.y * skew;
cs = simplifyContours(cs, ptCache); cs = simplifyContours(cache, cs);
for (const contour of cs) for (const z of contour) z.x += z.y * skew; for (const contour of cs) for (const z of contour) z.x += z.y * skew;
g.clearGeometry(); g.clearGeometry();
@ -52,11 +52,12 @@ function regulateSimpleGlyph(g, skew, ptCache) {
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
function simplifyContours(source, ptCache) { function simplifyContours(cache, source) {
const ck = CurveUtil.hashShape(source); const ck = CurveUtil.hashShape(source);
if (ptCache.source[ck]) { const cached = cache.getPT(ck);
ptCache.sink[ck] = ptCache.source[ck]; if (cached) {
return CurveUtil.repToShape(ptCache.source[ck]); cache.savePT(ck, cached);
return CurveUtil.repToShape(cached);
} }
const sink = new FairizedShapeSink(); const sink = new FairizedShapeSink();
@ -72,7 +73,7 @@ function simplifyContours(source, ptCache) {
CurveUtil.GEOMETRY_PRECISION CurveUtil.GEOMETRY_PRECISION
); );
ptCache.sink[ck] = CurveUtil.shapeToRep(sink.contours); cache.savePT(ck, CurveUtil.shapeToRep(sink.contours));
return sink.contours; return sink.contours;
} }

View file

@ -3,9 +3,9 @@
const finalizeGlyphs = require("./glyphs"); const finalizeGlyphs = require("./glyphs");
const gcFont = require("./gc"); const gcFont = require("./gc");
module.exports = function finalizeFont(para, glyphStore, excludedCodePoints, restFont, ptCache) { module.exports = function finalizeFont(cache, para, glyphStore, excludedCodePoints, restFont) {
glyphStore = gcFont(glyphStore, excludedCodePoints, restFont, {}); glyphStore = gcFont(glyphStore, excludedCodePoints, restFont, {});
glyphStore = finalizeGlyphs(para, glyphStore, ptCache); glyphStore = finalizeGlyphs(cache, para, glyphStore);
validateMonospace(para, glyphStore); validateMonospace(para, glyphStore);
return glyphStore; return glyphStore;
}; };

View file

@ -7,6 +7,7 @@ const zlib = require("zlib");
const { FontIo } = require("ot-builder"); const { FontIo } = require("ot-builder");
const Toml = require("@iarna/toml"); const Toml = require("@iarna/toml");
const Caching = require("./caching/index");
const BuildFont = require("./gen/build-font.js"); const BuildFont = require("./gen/build-font.js");
const Parameters = require("./support/parameters"); const Parameters = require("./support/parameters");
const VariantData = require("./support/variant-data"); const VariantData = require("./support/variant-data");
@ -16,26 +17,15 @@ const { createGrDisplaySheet } = require("./support/gr");
module.exports = async function main(argv) { module.exports = async function main(argv) {
const paraT = await getParameters(); const paraT = await getParameters();
const ptCache = await loadPTCache(argv); const cache = await Caching.load(argv);
const { font, glyphStore } = await BuildFont(argv, paraT(argv), ptCache); const { font, glyphStore } = await BuildFont(cache, paraT(argv));
if (argv.oCharMap) await saveCharMap(argv, glyphStore); if (argv.oCharMap) await saveCharMap(argv, glyphStore);
if (argv.o) await saveTTF(argv, font); if (argv.o) await saveTTF(argv, font);
await savePTCache(argv, ptCache); await Caching.save(argv, cache);
}; };
async function loadPTCache(argv) {
let ptCache = { source: {}, sink: {} };
if (argv.ptCache && fs.existsSync(argv.ptCache)) {
ptCache.source = unzip(await fs.readFile(argv.ptCache));
}
return ptCache;
}
async function savePTCache(argv, ptCache) {
if (argv.ptCache) await fs.writeFile(argv.ptCache, zip(ptCache.sink));
}
// Parameter preparation // Parameter preparation
async function getParameters() { async function getParameters() {
const PARAMETERS_TOML = path.resolve(__dirname, "../params/parameters.toml"); const PARAMETERS_TOML = path.resolve(__dirname, "../params/parameters.toml");

View file

@ -277,13 +277,13 @@ const DistUnhintedTTF = file.make(
await target.need(Scripts, Parameters, Dependencies); await target.need(Scripts, Parameters, Dependencies);
const charMapDir = `${BUILD}/ttf/${gr}`; const charMapDir = `${BUILD}/ttf/${gr}`;
const charMapPath = `${charMapDir}/${fn}.cm.gz`; const charMapPath = `${charMapDir}/${fn}.cm.gz`;
const ptCachePath = `${charMapDir}/${fn}.ptCache.gz`; const cachePath = `${charMapDir}/${fn}.cache.gz`;
const [fi] = await target.need(FontInfoOf(fn), de(out.dir), de(charMapDir)); const [fi] = await target.need(FontInfoOf(fn), de(out.dir), de(charMapDir));
echo.action(echo.hl.command(`Create TTF`), fn, echo.hl.operator("->"), out.full); echo.action(echo.hl.command(`Create TTF`), fn, echo.hl.operator("->"), out.full);
await silently.node("font-src/index", { await silently.node("font-src/index", {
o: out.full, o: out.full,
oCharMap: charMapPath, oCharMap: charMapPath,
ptCache: ptCachePath, oCache: cachePath,
...fi ...fi
}); });
} }