2021-02-15 02:27:20 +00:00
|
|
|
import { Point, BBox } from "../vendor/bezier-js";
|
2021-02-15 12:44:22 +00:00
|
|
|
import { getBoundingBoxForBeziers } from "./bounding-box";
|
2021-02-14 01:29:20 +00:00
|
|
|
import * as colors from "./colors";
|
2021-02-14 01:47:28 +00:00
|
|
|
import { pathToShapes } from "./path";
|
2021-02-15 21:40:47 +00:00
|
|
|
import { normalizePoint, subtractPoints } from "./point";
|
2021-02-15 14:56:02 +00:00
|
|
|
import type { SvgSymbolElement } from "./svg-symbol";
|
2021-02-14 01:29:20 +00:00
|
|
|
|
2021-02-15 12:33:48 +00:00
|
|
|
const SPEC_LAYER_ID_RE = /^specs.*/i;
|
2021-02-14 01:29:20 +00:00
|
|
|
|
2021-02-14 17:07:34 +00:00
|
|
|
export type PointWithNormal = {
|
|
|
|
point: Point;
|
|
|
|
normal: Point;
|
|
|
|
};
|
|
|
|
|
2021-02-15 14:13:00 +00:00
|
|
|
type AttachmentPointSpecs = {
|
2021-02-22 02:13:14 +00:00
|
|
|
anchor: PointWithNormal[];
|
2021-02-15 14:13:00 +00:00
|
|
|
tail: PointWithNormal[];
|
|
|
|
leg: PointWithNormal[];
|
|
|
|
arm: PointWithNormal[];
|
|
|
|
horn: PointWithNormal[];
|
|
|
|
crown: PointWithNormal[];
|
2021-02-14 01:29:20 +00:00
|
|
|
};
|
2021-02-15 14:13:00 +00:00
|
|
|
|
|
|
|
type FullSpecs = AttachmentPointSpecs & {
|
|
|
|
nesting: BBox[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Specs = Partial<FullSpecs>;
|
|
|
|
|
|
|
|
export type AttachmentPointType = keyof AttachmentPointSpecs;
|
|
|
|
|
|
|
|
export type AttachmentPoint = PointWithNormal & {
|
|
|
|
type: AttachmentPointType;
|
2021-02-27 18:28:44 +00:00
|
|
|
index: number;
|
2021-02-15 14:13:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const ATTACHMENT_POINT_TYPES: AttachmentPointType[] = [
|
2021-02-22 02:13:14 +00:00
|
|
|
"anchor",
|
2021-02-15 14:13:00 +00:00
|
|
|
"tail",
|
|
|
|
"leg",
|
|
|
|
"arm",
|
|
|
|
"horn",
|
|
|
|
"crown",
|
|
|
|
];
|
|
|
|
|
|
|
|
export function* iterAttachmentPoints(specs: Specs): Iterable<AttachmentPoint> {
|
|
|
|
for (let type of ATTACHMENT_POINT_TYPES) {
|
|
|
|
const points = specs[type];
|
|
|
|
if (points) {
|
2021-02-27 18:28:44 +00:00
|
|
|
let index = 0;
|
2021-02-15 14:13:00 +00:00
|
|
|
for (let point of points) {
|
2021-02-27 18:28:44 +00:00
|
|
|
yield { ...point, type, index };
|
|
|
|
index += 1;
|
2021-02-15 14:13:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 02:27:20 +00:00
|
|
|
const NUM_ARROW_POINTS = 4;
|
|
|
|
const ARROW_TOP_POINT_IDX = 0;
|
|
|
|
const ARROW_BOTTOM_POINT_IDX = 2;
|
|
|
|
|
|
|
|
function getArrowPoints(path: string): PointWithNormal[] {
|
2021-02-14 01:47:28 +00:00
|
|
|
const shapes = pathToShapes(path);
|
2021-02-14 17:07:34 +00:00
|
|
|
const points: PointWithNormal[] = [];
|
2021-02-14 01:47:28 +00:00
|
|
|
|
|
|
|
for (let shape of shapes) {
|
2021-02-15 02:27:20 +00:00
|
|
|
if (shape.length !== NUM_ARROW_POINTS) {
|
|
|
|
throw new Error(
|
|
|
|
`Expected arrow to have ${NUM_ARROW_POINTS} points, not ${shape.length}!`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const point = shape[ARROW_BOTTOM_POINT_IDX].get(0.0);
|
|
|
|
const normal = normalizePoint(
|
|
|
|
subtractPoints(shape[ARROW_TOP_POINT_IDX].get(0.0), point)
|
|
|
|
);
|
2021-02-14 17:07:34 +00:00
|
|
|
points.push({
|
|
|
|
point,
|
2021-02-15 02:27:20 +00:00
|
|
|
normal,
|
2021-02-14 17:07:34 +00:00
|
|
|
});
|
2021-02-14 01:47:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return points;
|
2021-02-14 01:29:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 12:44:52 +00:00
|
|
|
function getBoundingBoxes(path: string): BBox[] {
|
2021-02-14 01:47:28 +00:00
|
|
|
const shapes = pathToShapes(path);
|
2021-02-14 12:44:52 +00:00
|
|
|
const bboxes: BBox[] = [];
|
2021-02-14 01:47:28 +00:00
|
|
|
|
|
|
|
for (let shape of shapes) {
|
|
|
|
bboxes.push(getBoundingBoxForBeziers(shape));
|
|
|
|
}
|
|
|
|
|
|
|
|
return bboxes;
|
2021-02-14 01:29:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 12:04:16 +00:00
|
|
|
/**
|
|
|
|
* Sort points from top to bottom, left to right.
|
|
|
|
*/
|
|
|
|
function sortPoints(a: PointWithNormal, b: PointWithNormal): number {
|
|
|
|
if (a.point.y < b.point.y) return -1;
|
|
|
|
if (a.point.y > b.point.y) return 1;
|
|
|
|
if (a.point.x < b.point.x) return -1;
|
|
|
|
if (a.point.x > b.point.x) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortedPoints(points: PointWithNormal[]): PointWithNormal[] {
|
|
|
|
const copy = [...points];
|
|
|
|
copy.sort(sortPoints);
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2021-02-16 12:17:40 +00:00
|
|
|
function concat<T>(first: T[] | undefined, second: T[]): T[] {
|
|
|
|
return first ? [...first, ...second] : second;
|
2021-02-14 01:29:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 14:13:00 +00:00
|
|
|
const ATTACHMENT_COLOR_MAP = new Map(
|
|
|
|
ATTACHMENT_POINT_TYPES.map((type) => [
|
|
|
|
colors.ATTACHMENT_POINT_COLORS[type],
|
|
|
|
type,
|
|
|
|
])
|
|
|
|
);
|
|
|
|
|
2021-02-14 01:29:20 +00:00
|
|
|
function updateSpecs(fill: string, path: string, specs: Specs): Specs {
|
2021-02-15 14:13:00 +00:00
|
|
|
const attachmentType = ATTACHMENT_COLOR_MAP.get(fill);
|
|
|
|
|
|
|
|
if (attachmentType) {
|
|
|
|
return {
|
|
|
|
...specs,
|
2021-02-16 12:17:40 +00:00
|
|
|
[attachmentType]: sortedPoints(
|
|
|
|
concat(specs[attachmentType], getArrowPoints(path))
|
2021-02-16 12:04:16 +00:00
|
|
|
),
|
2021-02-15 14:13:00 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fill === colors.NESTING_BOUNDING_BOX_COLOR) {
|
|
|
|
return {
|
|
|
|
...specs,
|
2021-02-16 12:17:40 +00:00
|
|
|
nesting: concat(specs.nesting, getBoundingBoxes(path)),
|
2021-02-15 14:13:00 +00:00
|
|
|
};
|
2021-02-14 01:29:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-04 00:50:23 +00:00
|
|
|
console.log(
|
|
|
|
`Not sure what to do with specs path with fill "${fill}", ignoring it.`
|
|
|
|
);
|
|
|
|
|
|
|
|
return specs;
|
2021-02-14 01:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getSpecs(layers: SvgSymbolElement[]): Specs {
|
|
|
|
let specs: Specs = {};
|
|
|
|
|
|
|
|
for (let layer of layers) {
|
|
|
|
if (layer.tagName !== "path") {
|
|
|
|
throw new Error(
|
|
|
|
`Found an unexpected <${layer.tagName}> in the specs layer!`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const { fill, d } = layer.props;
|
|
|
|
if (!(fill && d)) {
|
|
|
|
throw new Error(
|
|
|
|
`Specs layer does not contain 'fill' and/or 'd' attributes!`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
specs = updateSpecs(fill, d, specs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return specs;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function extractSpecs(
|
2021-02-15 02:27:20 +00:00
|
|
|
layers: SvgSymbolElement[]
|
2021-02-14 01:29:20 +00:00
|
|
|
): [Specs | undefined, SvgSymbolElement[]] {
|
|
|
|
const layersWithoutSpecs: SvgSymbolElement[] = [];
|
|
|
|
let specs: Specs | undefined = undefined;
|
|
|
|
|
|
|
|
const setSpecs = (s: Specs | undefined) => {
|
|
|
|
if (s) {
|
|
|
|
if (specs) {
|
|
|
|
throw new Error("Duplicate specs layers found!");
|
|
|
|
}
|
|
|
|
specs = s;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (let layer of layers) {
|
|
|
|
switch (layer.tagName) {
|
|
|
|
case "g":
|
|
|
|
const { id } = layer.props;
|
|
|
|
if (id && SPEC_LAYER_ID_RE.test(id)) {
|
|
|
|
setSpecs(getSpecs(layer.children));
|
|
|
|
} else {
|
2021-02-15 02:27:20 +00:00
|
|
|
let [s, children] = extractSpecs(layer.children);
|
2021-02-14 01:29:20 +00:00
|
|
|
setSpecs(s);
|
|
|
|
layersWithoutSpecs.push({
|
|
|
|
...layer,
|
|
|
|
children,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "path":
|
|
|
|
layersWithoutSpecs.push(layer);
|
2021-02-14 17:07:34 +00:00
|
|
|
break;
|
2021-02-14 01:29:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [specs, layersWithoutSpecs];
|
|
|
|
}
|