Make outline simplification a kind of geometry, as well as skew correction (#961).
This commit is contained in:
parent
340d3096c3
commit
902722a1a1
3 changed files with 50 additions and 21 deletions
|
@ -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,20 +58,44 @@ function flattenSimpleGlyph(cache, skew, g) {
|
|||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
function simplifyContours(source) {
|
||||
const sink = new FairizedShapeSink();
|
||||
TypoGeom.ShapeConv.transferGenericShape(
|
||||
TypoGeom.Fairize.fairizeBezierShape(
|
||||
TypoGeom.Boolean.removeOverlap(
|
||||
CurveUtil.convertShapeToArcs(source),
|
||||
TypoGeom.Boolean.PolyFillType.pftNonZero,
|
||||
CurveUtil.BOOLE_RESOLUTION
|
||||
)
|
||||
),
|
||||
sink,
|
||||
CurveUtil.GEOMETRY_PRECISION
|
||||
);
|
||||
return sink.contours;
|
||||
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(
|
||||
TypoGeom.Boolean.removeOverlap(
|
||||
CurveUtil.convertShapeToArcs(source),
|
||||
TypoGeom.Boolean.PolyFillType.pftNonZero,
|
||||
CurveUtil.BOOLE_RESOLUTION
|
||||
)
|
||||
),
|
||||
sink,
|
||||
CurveUtil.GEOMETRY_PRECISION
|
||||
);
|
||||
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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue