Move caching into font builder

This commit is contained in:
be5invis 2021-04-26 23:53:47 -07:00
parent 6dfcc92cc9
commit 5025aa1d7b
3 changed files with 6 additions and 8 deletions

View file

@ -4,12 +4,13 @@ const EmptyFont = require("./empty-font");
const buildGlyphs = require("../glyphs/index");
const finalizeFont = require("./finalize/index");
const convertOtd = require("./otd-conv/index");
const Caching = require("./caching/index");
const { buildOtl } = require("../otl/index");
const { assignFontNames } = require("../meta/naming");
const { copyFontMetrics } = require("../meta/aesthetics");
module.exports = async function (cache, para) {
module.exports = async function (argv, para) {
const otd = EmptyFont();
const gs = buildGlyphs(para);
@ -29,7 +30,10 @@ module.exports = async function (cache, para) {
}
}
const cache = await Caching.load(argv);
const finalGs = finalizeFont(cache, para, gs.glyphStore, excludeChars, otd);
await Caching.save(argv, cache);
const font = convertOtd(otd, finalGs);
return { font, glyphStore: finalGs };
};

View file

@ -0,0 +1,66 @@
"use strict";
const fs = require("fs-extra");
const zlib = require("zlib");
const { encode, decode } = require("@msgpack/msgpack");
const Edition = 3;
const MAX_AGE = 5;
class GfEntry {
constructor(age, value) {
this.age = age;
this.value = value;
}
}
class Cache {
constructor() {
this.gf = new Map();
}
loadRep(version, rep) {
if (!rep || rep.version !== version + "@" + Edition) return;
for (const [k, e] of Object.entries(rep.gf)) {
this.gf.set(k, new GfEntry((e.age || 0) + 1, e.value));
}
}
toRep(version) {
let gfRep = {};
for (const [k, e] of this.gf) {
if (e.age < MAX_AGE) gfRep[k] = { age: e.age, value: e.value };
}
return { version: version + "@" + Edition, gf: gfRep };
}
// Geometry flattening conversion cache
getGF(k) {
const entry = this.gf.get(k);
if (!entry) return undefined;
else return entry.value;
}
refreshGF(k) {
const entry = this.gf.get(k);
if (!entry) return;
entry.age = 0;
}
saveGF(k, v) {
this.gf.set(k, new GfEntry(0, v));
}
}
exports.load = async function (argv) {
let cache = new Cache();
if (argv.oCache && fs.existsSync(argv.oCache)) {
const buf = zlib.gunzipSync(await fs.readFile(argv.oCache));
cache.loadRep(argv.menu.version, decode(buf));
}
return cache;
};
exports.save = async function savePTCache(argv, cache) {
if (argv.oCache) {
const buf = encode(cache.toRep(argv.menu.version));
const bufZip = zlib.gzipSync(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength));
await fs.writeFile(argv.oCache, bufZip);
}
};