Move out geometry class

This commit is contained in:
be5invis 2021-02-10 19:52:54 -08:00
parent b7c2b2a3b5
commit 5b1b362cfa
5 changed files with 155 additions and 159 deletions

View file

@ -3,6 +3,7 @@
const Transform = require("./transform");
const Point = require("./point");
const Anchor = require("./anchor");
const GeometryStore = require("./geometry");
module.exports = class Glyph {
constructor(_identifier) {
@ -114,9 +115,8 @@ module.exports = class Glyph {
includeContours(cs, shiftX, shiftY) {
for (const contour of cs) {
let c = [];
c.tag = contour.tag || cs.tag || this.defaultTag;
for (const z of contour) c.push(Point.translated(z, shiftX, shiftY));
this.geometry.addContour(c);
this.geometry.addContour(c, contour.tag || cs.tag || this.defaultTag);
}
}
@ -195,99 +195,3 @@ module.exports = class Glyph {
delete this.markAnchors[id];
}
};
class GeometryStore {
constructor() {
this.m_contours = [];
this.m_references = [];
}
addContour(c) {
this.m_contours.push(c);
}
addReference(glyph, x, y) {
this.m_references.push({ glyph, x, y });
}
asContours() {
let result = [];
for (const c of this.m_contours) {
const c1 = [...c];
if (c.tag) c1.tag = c.tag;
result.push(c1);
}
for (const r of this.m_references) {
for (const c of r.glyph.geometry.asContours()) {
let c1 = [];
for (const z of c) c1.push(Point.fromXY(z.type, z.x + r.x, z.y + r.y));
if (c.tag) c1.tag = c.tag;
result.push(c1);
}
}
return result;
}
asReferences() {
if (this.m_contours && this.m_contours.length) return null;
if (!this.m_references.length) return null;
return this.m_references;
}
applyTranslate(shiftX, shiftY) {
for (const c of this.m_contours) {
for (let k = 0; k < c.length; k++) {
c[k] = Point.translated(c[k], shiftX, shiftY);
}
}
for (const r of this.m_references) {
r.x += shiftX;
r.y += shiftY;
}
}
applyTransform(tfm) {
const cs = this.asContours();
for (const c of cs) {
for (let k = 0; k < c.length; k++) {
c[k] = Point.transformed(tfm, c[k]);
}
}
this.m_contours = cs;
this.m_references.length = 0;
}
reTagContour(oldTag, newTag) {
for (const c of this.m_contours) {
if (c.tag === oldTag) c.tag = newTag;
}
}
ejectContour(tag) {
const cs = this.asContours();
let i = 0,
j = 0;
for (; i < cs.length; i++) if (!cs[i].tag || cs[i].tag !== tag) cs[j++] = cs[i];
cs.length = j;
this.m_contours = cs;
this.m_references = [];
}
suppressNaN() {
let broken = false,
complexity = 0;
for (const c of this.m_contours) {
for (const z of c) {
complexity++;
if (!isFinite(z.x)) {
broken = true;
z.x = 0;
}
if (!isFinite(z.y)) {
broken = true;
z.y = 0;
}
}
}
return broken ? 0xffff : complexity;
}
isEmpty() {
return !this.m_contours.length && !this.m_references.length;
}
}