Cache the entire geometry flattener

This commit is contained in:
be5invis 2021-04-17 15:42:07 -07:00
parent edbe5ff47e
commit 4c8de9f025
4 changed files with 95 additions and 56 deletions

View file

@ -1,6 +1,7 @@
"use strict";
const TypoGeom = require("typo-geom");
const Geom = require("../../support/geometry");
const Point = require("../../support/point");
const CurveUtil = require("../../support/curve-util");
const util = require("util");
@ -17,14 +18,7 @@ function finalizeGlyphs(cache, para, glyphStore) {
function regulateGlyphStore(cache, skew, glyphStore) {
for (const g of glyphStore.glyphs()) {
if (g.geometry.isEmpty()) continue;
if (!regulateCompositeGlyph(glyphStore, g)) {
const cs = g.geometry.asContours();
g.clearGeometry();
g.includeContours(cs, 0, 0);
}
}
for (const g of glyphStore.glyphs()) {
if (!g.geometry.asReferences()) regulateSimpleGlyph(cache, skew, g);
if (!regulateCompositeGlyph(glyphStore, g)) flattenSimpleGlyph(cache, skew, g);
}
}
@ -40,26 +34,28 @@ function regulateCompositeGlyph(glyphStore, g) {
return true;
}
function regulateSimpleGlyph(cache, skew, g) {
let cs = g.geometry.asContours();
for (const contour of cs) for (const z of contour) z.x -= z.y * skew;
cs = simplifyContours(cache, cs);
for (const contour of cs) for (const z of contour) z.x += z.y * skew;
function flattenSimpleGlyph(cache, skew, g) {
const ck = Geom.hashGeometry(g.geometry);
const cached = cache.getGF(ck);
if (ck && cached) {
g.clearGeometry();
g.includeContours(CurveUtil.repToShape(cached), 0, 0);
cache.saveGF(ck, cached);
} else {
let cs = g.geometry.asContours();
for (const contour of cs) for (const z of contour) z.x -= z.y * skew;
cs = simplifyContours(cs);
for (const contour of cs) for (const z of contour) z.x += z.y * skew;
g.clearGeometry();
g.includeContours(cs, 0, 0);
g.clearGeometry();
g.includeContours(cs, 0, 0);
if (ck) cache.saveGF(ck, CurveUtil.shapeToRep(cs));
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
function simplifyContours(cache, source) {
const ck = CurveUtil.hashShape(source);
const cached = cache.getPT(ck);
if (cached) {
cache.savePT(ck, cached);
return CurveUtil.repToShape(cached);
}
function simplifyContours(source) {
const sink = new FairizedShapeSink();
TypoGeom.ShapeConv.transferGenericShape(
TypoGeom.Fairize.fairizeBezierShape(
@ -72,8 +68,6 @@ function simplifyContours(cache, source) {
sink,
CurveUtil.GEOMETRY_PRECISION
);
cache.savePT(ck, CurveUtil.shapeToRep(sink.contours));
return sink.contours;
}