Make outline simplification a kind of geometry, as well as skew correction (#961).

This commit is contained in:
be5invis 2021-04-19 18:03:15 -07:00
parent 340d3096c3
commit 902722a1a1
3 changed files with 50 additions and 21 deletions

View file

@ -3,7 +3,7 @@
const fs = require("fs-extra");
const zlib = require("zlib");
const Edition = 1;
const Edition = 2;
class Cache {
constructor() {

View file

@ -3,6 +3,7 @@
const TypoGeom = require("typo-geom");
const Geom = require("../../support/geometry");
const Point = require("../../support/point");
const Transform = require("../../support/transform");
const CurveUtil = require("../../support/curve-util");
const util = require("util");
@ -42,11 +43,13 @@ function flattenSimpleGlyph(cache, skew, g) {
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;
const tfBack = new Transform(1, -skew, 0, 1, 0, 0);
const tfForward = new Transform(1, +skew, 0, 1, 0, 0);
const g1 = new Geom.TransformedGeometry(
new SimplifyGeometry(new Geom.TransformedGeometry(g.geometry, tfBack)),
tfForward
);
const cs = g1.asContours();
g.clearGeometry();
g.includeContours(cs, 0, 0);
if (ck) cache.saveGF(ck, CurveUtil.shapeToRep(cs));
@ -55,7 +58,13 @@ function flattenSimpleGlyph(cache, skew, g) {
///////////////////////////////////////////////////////////////////////////////////////////////////
function simplifyContours(source) {
class SimplifyGeometry extends Geom.GeometryBase {
constructor(g) {
super();
this.m_geom = g;
}
asContours() {
const source = this.m_geom.asContours();
const sink = new FairizedShapeSink();
TypoGeom.ShapeConv.transferGenericShape(
TypoGeom.Fairize.fairizeBezierShape(
@ -70,6 +79,24 @@ function simplifyContours(source) {
);
return sink.contours;
}
asReferences() {
return null;
}
filterTag(fn) {
return this.m_geom.filterTag(fn);
}
isEmpty() {
return this.m_geom.isEmpty();
}
measureComplexity() {
return this.m_geom.measureComplexity();
}
toShapeStringOrNull() {
const sTarget = this.m_geom.toShapeStringOrNull();
if (!sTarget) return null;
return `SimplifyGeometry{${sTarget}}`;
}
}
class FairizedShapeSink {
constructor() {

View file

@ -38,7 +38,9 @@ class ContourGeometry extends GeometryBase {
}
asContours() {
if (this.isEmpty()) return [];
else return [this.m_points];
let c1 = [];
for (const z of this.m_points) c1.push(Point.from(z.type, z));
return [c1];
}
asReferences() {
return null;