Cleanup of geometry code
This commit is contained in:
parent
a096544d2f
commit
2d84803cec
8 changed files with 127 additions and 103 deletions
|
@ -3,7 +3,7 @@ import zlib from "zlib";
|
|||
|
||||
import { encode, decode } from "@msgpack/msgpack";
|
||||
|
||||
const Edition = 26;
|
||||
const Edition = 27;
|
||||
const MAX_AGE = 16;
|
||||
class GfEntry {
|
||||
constructor(age, value) {
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import { Ot } from "ot-builder";
|
||||
|
||||
import { Vec2 } from "../../support/geometry/point.mjs";
|
||||
|
||||
export function convertGsub(table, glyphs) {
|
||||
return ConvertGsubGposImpl(GsubHandlers, Ot.Gsub.Table, table, glyphs);
|
||||
}
|
||||
|
@ -304,7 +306,7 @@ function convertMarkRecords(marks, mm, store) {
|
|||
const g = store.glyphs.queryByName(gn);
|
||||
if (!g) continue;
|
||||
let markAnchors = [];
|
||||
markAnchors[mm.get(mark.class)] = { x: mark.x, y: mark.y };
|
||||
markAnchors[mm.get(mark.class)] = Vec2.from(mark);
|
||||
out.set(g, { markAnchors: markAnchors });
|
||||
}
|
||||
return out;
|
||||
|
|
|
@ -153,6 +153,8 @@ export class DiSpiroGeometry extends GeometryBase {
|
|||
expander.initializeNormals();
|
||||
expander.iterateNormals();
|
||||
expander.iterateNormals();
|
||||
expander.iterateNormals();
|
||||
expander.iterateNormals();
|
||||
this.m_cachedExpansionResults = expander.expand();
|
||||
return this.m_cachedExpansionResults;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,16 @@
|
|||
import { mix } from "../utils.mjs";
|
||||
|
||||
export class Vec2 {
|
||||
constructor(x, y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
static from(z) {
|
||||
return new Vec2(z.x, z.y);
|
||||
}
|
||||
}
|
||||
|
||||
export class Point {
|
||||
constructor(type, x, y) {
|
||||
this.type = type;
|
||||
|
|
|
@ -186,3 +186,10 @@ export function Interpolator(blender, restParameters) {
|
|||
for (const prop in restParameters) interpolator[prop] = restParameters[prop];
|
||||
return interpolator;
|
||||
}
|
||||
|
||||
export class ImportanceControlKnot extends ControlKnot {
|
||||
constructor(type, x, y, unimportant) {
|
||||
super(type, x, y, null);
|
||||
this.unimportant = unimportant;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
import * as SpiroJs from "spiro";
|
||||
|
||||
import { linreg } from "../utils.mjs";
|
||||
import { linreg, mix } from "../utils.mjs";
|
||||
|
||||
import { Vec2 } from "./point.mjs";
|
||||
import { ControlKnot } from "./spiro-control.mjs";
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
@ -9,109 +12,92 @@ export class SpiroExpander {
|
|||
this.m_gizmo = gizmo;
|
||||
this.m_contrast = contrast;
|
||||
this.m_closed = closed;
|
||||
this.m_biKnots = [];
|
||||
for (const knot of biKnots) {
|
||||
this.m_biKnots.push(knot.withGizmo(gizmo));
|
||||
}
|
||||
|
||||
this.m_biKnotsU = Array.from(biKnots);
|
||||
this.m_biKnotsT = biKnots.map(k => k.withGizmo(gizmo));
|
||||
}
|
||||
initializeNormals() {
|
||||
const normalRectifier = new NormalRectifier(this.m_biKnots, this.m_gizmo);
|
||||
SpiroJs.spiroToArcsOnContext(this.m_biKnots, this.m_closed, normalRectifier);
|
||||
const normalRectifier = new NormalRectifier(this.m_biKnotsT, this.m_gizmo);
|
||||
SpiroJs.spiroToArcsOnContext(this.m_biKnotsT, this.m_closed, normalRectifier);
|
||||
}
|
||||
iterateNormals() {
|
||||
const centerBone = this.getPass2Knots();
|
||||
const normalRectifier = new NormalRectifier(this.m_biKnots, this.m_gizmo);
|
||||
const normalRectifier = new NormalRectifier(this.m_biKnotsT, this.m_gizmo);
|
||||
SpiroJs.spiroToArcsOnContext(centerBone, this.m_closed, normalRectifier);
|
||||
}
|
||||
getPass2Knots() {
|
||||
const expanded = this.expand(this.m_contrast);
|
||||
const middles = [];
|
||||
for (let j = 0; j < this.m_biKnots.length; j++) {
|
||||
const lhs = this.m_gizmo.unapply(expanded.lhs[j]);
|
||||
const rhs = this.m_gizmo.unapply(expanded.rhs[j]);
|
||||
middles[j] = {
|
||||
x: 0.5 * (lhs.x + rhs.x),
|
||||
y: 0.5 * (lhs.y + rhs.y),
|
||||
type: this.m_biKnots[j].type,
|
||||
unimportant: this.m_biKnots[j].unimportant
|
||||
};
|
||||
for (let j = 0; j < this.m_biKnotsT.length; j++) {
|
||||
const lhs = expanded.lhs[j];
|
||||
const rhs = expanded.rhs[j];
|
||||
middles[j] = new ControlKnot(
|
||||
this.m_biKnotsT[j].type,
|
||||
mix(lhs.x, rhs.x, 0.5),
|
||||
mix(lhs.y, rhs.y, 0.5)
|
||||
);
|
||||
}
|
||||
return middles;
|
||||
}
|
||||
expand() {
|
||||
const lhs = [],
|
||||
rhs = [];
|
||||
// Initialize knots
|
||||
for (let j = 0; j < this.m_biKnots.length; j++) {
|
||||
const knot = this.m_biKnots[j];
|
||||
lhs[j] = {
|
||||
type: knot.type,
|
||||
unimportant: knot.unimportant,
|
||||
x: 0,
|
||||
y: 0
|
||||
};
|
||||
rhs[j] = {
|
||||
type: reverseKnotType(knot.type),
|
||||
unimportant: knot.unimportant,
|
||||
x: 0,
|
||||
y: 0
|
||||
};
|
||||
}
|
||||
// Create important knots
|
||||
for (let j = 0; j < this.m_biKnots.length; j++) {
|
||||
const knot = this.m_biKnots[j];
|
||||
if (knot.unimportant) continue;
|
||||
let dx, dy;
|
||||
if (knot.proposedNormal) {
|
||||
dx = knot.proposedNormal.x;
|
||||
dy = knot.proposedNormal.y;
|
||||
} else {
|
||||
dx = normalX(knot.origTangent, this.m_contrast);
|
||||
dy = normalY(knot.origTangent, this.m_contrast);
|
||||
}
|
||||
lhs[j].x = knot.x + knot.d1 * dx;
|
||||
lhs[j].y = knot.y + knot.d1 * dy;
|
||||
rhs[j].x = knot.x - knot.d2 * dx;
|
||||
rhs[j].y = knot.y - knot.d2 * dy;
|
||||
}
|
||||
this.interpolateUnimportantKnots(lhs, rhs);
|
||||
|
||||
const lhsUntransformed = [],
|
||||
rhs = [],
|
||||
lhsUntransformed = [],
|
||||
rhsUntransformed = [];
|
||||
for (const z of lhs) {
|
||||
const u = this.m_gizmo.unapply(z);
|
||||
lhsUntransformed.push({ type: z.type, x: u.x, y: u.y });
|
||||
|
||||
for (let j = 0; j < this.m_biKnotsT.length; j++) {
|
||||
const knot = this.m_biKnotsT[j];
|
||||
lhs[j] = new ControlKnot(knot.type, 0, 0);
|
||||
rhs[j] = new ControlKnot(reverseKnotType(knot.type), 0, 0);
|
||||
lhsUntransformed[j] = new ControlKnot(knot.type, 0, 0);
|
||||
rhsUntransformed[j] = new ControlKnot(reverseKnotType(knot.type), 0, 0);
|
||||
}
|
||||
for (const z of rhs) {
|
||||
const u = this.m_gizmo.unapply(z);
|
||||
rhsUntransformed.push({ type: z.type, x: u.x, y: u.y });
|
||||
|
||||
for (let j = 0; j < this.m_biKnotsT.length; j++) {
|
||||
const knotT = this.m_biKnotsT[j];
|
||||
if (knotT.unimportant) continue;
|
||||
let dx, dy;
|
||||
if (knotT.proposedNormal) {
|
||||
dx = knotT.proposedNormal.x;
|
||||
dy = knotT.proposedNormal.y;
|
||||
} else {
|
||||
dx = normalX(knotT.origTangent, this.m_contrast);
|
||||
dy = normalY(knotT.origTangent, this.m_contrast);
|
||||
}
|
||||
lhs[j].x = knotT.x + knotT.d1 * dx;
|
||||
lhs[j].y = knotT.y + knotT.d1 * dy;
|
||||
rhs[j].x = knotT.x - knotT.d2 * dx;
|
||||
rhs[j].y = knotT.y - knotT.d2 * dy;
|
||||
|
||||
this.m_gizmo.unapplyToSink(lhs[j], lhsUntransformed[j]);
|
||||
this.m_gizmo.unapplyToSink(rhs[j], rhsUntransformed[j]);
|
||||
}
|
||||
|
||||
this.interpolateUnimportantKnots(lhs, rhs, lhsUntransformed, rhsUntransformed);
|
||||
return { lhs, rhs, lhsUntransformed, rhsUntransformed };
|
||||
}
|
||||
interpolateUnimportantKnots(lhs, rhs) {
|
||||
for (let j = 0; j < this.m_biKnots.length; j++) {
|
||||
const knot = this.m_biKnots[j];
|
||||
if (!knot.unimportant) continue;
|
||||
interpolateUnimportantKnots(lhsT, rhsT, lhsU, rhsU) {
|
||||
for (let j = 0; j < this.m_biKnotsU.length; j++) {
|
||||
const knotU = this.m_biKnotsU[j];
|
||||
if (!knotU.unimportant) continue;
|
||||
let jBefore, jAfter;
|
||||
for (jBefore = j - 1; cyNth(this.m_biKnots, jBefore).unimportant; jBefore--);
|
||||
for (jAfter = j + 1; cyNth(this.m_biKnots, jAfter).unimportant; jAfter++);
|
||||
const knotBefore = this.m_gizmo.unapply(cyNth(this.m_biKnots, jBefore)),
|
||||
knotAfter = this.m_gizmo.unapply(cyNth(this.m_biKnots, jAfter)),
|
||||
ref = this.m_gizmo.unapply(knot),
|
||||
lhsBefore = this.m_gizmo.unapply(cyNth(lhs, jBefore)),
|
||||
lhsAfter = this.m_gizmo.unapply(cyNth(lhs, jAfter)),
|
||||
rhsBefore = this.m_gizmo.unapply(cyNth(rhs, jBefore)),
|
||||
rhsAfter = this.m_gizmo.unapply(cyNth(rhs, jAfter));
|
||||
const lhsTf = this.m_gizmo.applyXY(
|
||||
linreg(knotBefore.x, lhsBefore.x, knotAfter.x, lhsAfter.x, ref.x),
|
||||
linreg(knotBefore.y, lhsBefore.y, knotAfter.y, lhsAfter.y, ref.y)
|
||||
);
|
||||
const rhsTf = this.m_gizmo.applyXY(
|
||||
linreg(knotBefore.x, rhsBefore.x, knotAfter.x, rhsAfter.x, ref.x),
|
||||
linreg(knotBefore.y, rhsBefore.y, knotAfter.y, rhsAfter.y, ref.y)
|
||||
);
|
||||
(lhs[j].x = lhsTf.x), (lhs[j].y = lhsTf.y);
|
||||
(rhs[j].x = rhsTf.x), (rhs[j].y = rhsTf.y);
|
||||
for (jBefore = j - 1; cyNth(this.m_biKnotsU, jBefore).unimportant; jBefore--);
|
||||
for (jAfter = j + 1; cyNth(this.m_biKnotsU, jAfter).unimportant; jAfter++);
|
||||
|
||||
const knotUBefore = cyNth(this.m_biKnotsU, jBefore),
|
||||
knotUAfter = cyNth(this.m_biKnotsU, jAfter),
|
||||
lhsUBefore = cyNth(lhsU, jBefore),
|
||||
lhsUAfter = cyNth(lhsU, jAfter),
|
||||
rhsUBefore = cyNth(rhsU, jBefore),
|
||||
rhsUAfter = cyNth(rhsU, jAfter);
|
||||
|
||||
lhsU[j].x = linreg(knotUBefore.x, lhsUBefore.x, knotUAfter.x, lhsUAfter.x, knotU.x);
|
||||
lhsU[j].y = linreg(knotUBefore.y, lhsUBefore.y, knotUAfter.y, lhsUAfter.y, knotU.y);
|
||||
rhsU[j].x = linreg(knotUBefore.x, rhsUBefore.x, knotUAfter.x, rhsUAfter.x, knotU.x);
|
||||
rhsU[j].y = linreg(knotUBefore.y, rhsUBefore.y, knotUAfter.y, rhsUAfter.y, knotU.y);
|
||||
|
||||
this.m_gizmo.applyToSink(lhsU[j], lhsT[j]);
|
||||
this.m_gizmo.applyToSink(rhsU[j], rhsT[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -128,7 +114,7 @@ class NormalRectifier {
|
|||
}
|
||||
arcTo(arc, x, y) {
|
||||
if (this.m_nKnotsProcessed === 1) {
|
||||
const d = this.m_gizmo.applyOffsetXY(arc.deriveX0, arc.deriveY0);
|
||||
const d = new Vec2(arc.deriveX0, arc.deriveY0);
|
||||
if (isTangentValid(d)) {
|
||||
this.m_biKnots[0].origTangent = d;
|
||||
} else {
|
||||
|
@ -136,7 +122,7 @@ class NormalRectifier {
|
|||
}
|
||||
}
|
||||
if (this.m_biKnots[this.m_nKnotsProcessed]) {
|
||||
const d = this.m_gizmo.applyOffsetXY(arc.deriveX1, arc.deriveY1);
|
||||
const d = new Vec2(arc.deriveX1, arc.deriveY1);
|
||||
if (isTangentValid(d)) {
|
||||
this.m_biKnots[this.m_nKnotsProcessed].origTangent = d;
|
||||
} else {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import { Vec2 } from "./point.mjs";
|
||||
|
||||
export class Transform {
|
||||
constructor(xx, yx, xy, yy, x, y) {
|
||||
this.xx = xx;
|
||||
|
@ -13,15 +15,24 @@ export class Transform {
|
|||
static Translate(x, y) {
|
||||
return new Transform(1, 0, 0, 1, x, y);
|
||||
}
|
||||
|
||||
applyX(x, y) {
|
||||
return x * this.xx + y * this.yx + this.x;
|
||||
}
|
||||
applyY(x, y) {
|
||||
return x * this.xy + y * this.yy + this.y;
|
||||
}
|
||||
applyXY(x, y) {
|
||||
return new Vec2(this.applyX(x, y), this.applyY(x, y));
|
||||
}
|
||||
applyToSink(pt, sink) {
|
||||
sink.x = this.applyX(pt.x, pt.y);
|
||||
sink.y = this.applyY(pt.x, pt.y);
|
||||
}
|
||||
apply(pt) {
|
||||
return this.applyXY(pt.x, pt.y);
|
||||
}
|
||||
applyXY(x, y) {
|
||||
return {
|
||||
x: x * this.xx + y * this.yx + this.x,
|
||||
y: x * this.xy + y * this.yy + this.y
|
||||
};
|
||||
}
|
||||
|
||||
applyOffset(delta) {
|
||||
return this.applyOffsetXY(delta.x, delta.y);
|
||||
}
|
||||
|
@ -31,14 +42,18 @@ export class Transform {
|
|||
y: deltaX * this.xy + deltaY * this.yy
|
||||
};
|
||||
}
|
||||
unapply(pt) {
|
||||
|
||||
unapplyToSink(pt, sink) {
|
||||
const xx = pt.x - this.x;
|
||||
const yy = pt.y - this.y;
|
||||
const denom = this.xx * this.yy - this.xy * this.yx;
|
||||
return {
|
||||
x: (xx * this.yy - yy * this.yx) / denom,
|
||||
y: (yy * this.xx - xx * this.xy) / denom
|
||||
};
|
||||
sink.x = (xx * this.yy - yy * this.yx) / denom;
|
||||
sink.y = (yy * this.xx - xx * this.xy) / denom;
|
||||
}
|
||||
unapply(pt) {
|
||||
let sink = new Vec2(0, 0);
|
||||
this.unapplyToSink(pt, sink);
|
||||
return sink;
|
||||
}
|
||||
inverse() {
|
||||
const denom = this.xx * this.yy - this.xy * this.yx;
|
||||
|
@ -51,6 +66,7 @@ export class Transform {
|
|||
-(-this.x * this.xy + this.y * this.xx) / denom
|
||||
);
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `[[${this.xx} ${this.xy}] [${this.yx} ${this.yy}]] + [[${this.x}] [${this.y}]]`;
|
||||
}
|
||||
|
@ -61,9 +77,9 @@ export class Transform {
|
|||
return this.isTranslate(tfm) && tfm.x === 0 && tfm.y === 0;
|
||||
}
|
||||
static Combine(...tfms) {
|
||||
let z00 = { x: 0, y: 0 };
|
||||
let z10 = { x: 1, y: 0 };
|
||||
let z01 = { x: 0, y: 1 };
|
||||
let z00 = new Vec2(0, 0);
|
||||
let z10 = new Vec2(1, 0);
|
||||
let z01 = new Vec2(0, 1);
|
||||
for (const tfm of tfms) {
|
||||
z00 = tfm.apply(z00);
|
||||
z10 = tfm.apply(z10);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Anchor } from "../geometry/anchor.mjs";
|
||||
import * as Geom from "../geometry/index.mjs";
|
||||
import { Point } from "../geometry/point.mjs";
|
||||
import { Point, Vec2 } from "../geometry/point.mjs";
|
||||
import { Transform } from "../geometry/transform.mjs";
|
||||
|
||||
export class Glyph {
|
||||
|
@ -76,7 +76,7 @@ export class Glyph {
|
|||
includeGlyph(g, copyAnchors, copyWidth) {
|
||||
if (g instanceof Function) throw new Error("Unreachable");
|
||||
// Combine anchors and get offset
|
||||
let shift = { x: 0, y: 0 };
|
||||
let shift = new Vec2(0, 0);
|
||||
this.combineMarks(g, shift);
|
||||
this.includeGlyphImpl(g, shift.x, shift.y);
|
||||
if (g.isMarkSet) throw new Error("Invalid component to be introduced.");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue