ultra-text 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +168 -85
- package/THIRD_PARTY_NOTICES.md +3 -0
- package/dist/assets/{cpuSdf.worker-Du9o8q4p.js → cpuSdf.worker-xS57yQbP.js} +13 -11
- package/dist/assets/cpuSdf.worker-xS57yQbP.js.map +1 -0
- package/dist/atlas/appendAtlas.js +2 -1
- package/dist/atlas/appendAtlas.js.map +1 -1
- package/dist/atlas/buildAtlas.js +3 -14
- package/dist/atlas/buildAtlas.js.map +1 -1
- package/dist/atlas/cpuSdf.js +12 -10
- package/dist/atlas/cpuSdf.js.map +1 -1
- package/dist/atlas/cpuWorker.js +1 -1
- package/dist/atlas/cpuWorker.js.map +1 -1
- package/dist/atlas/gpuAtlas.d.ts +7 -6
- package/dist/atlas/gpuAtlas.js +280 -145
- package/dist/atlas/gpuAtlas.js.map +1 -1
- package/dist/atlas/jfaWgsl.js +201 -73
- package/dist/atlas/jfaWgsl.js.map +1 -1
- package/dist/atlas/rasterize.d.ts +2 -0
- package/dist/atlas/rasterize.js +2 -87
- package/dist/atlas/rasterize.js.map +1 -1
- package/dist/dom/DomSdf.js +66 -15
- package/dist/dom/DomSdf.js.map +1 -1
- package/dist/dom/DomSlug.d.ts +26 -0
- package/dist/dom/DomSlug.js +640 -0
- package/dist/dom/DomSlug.js.map +1 -0
- package/dist/dom/SlugText.d.ts +18 -0
- package/dist/dom/SlugText.js +94 -0
- package/dist/dom/SlugText.js.map +1 -0
- package/dist/dom/Text.js +4 -67
- package/dist/dom/Text.js.map +1 -1
- package/dist/dom/fontCanvas.d.ts +2 -0
- package/dist/dom/fontCanvas.js +101 -0
- package/dist/dom/fontCanvas.js.map +1 -0
- package/dist/dom/layoutSignature.js +29 -2
- package/dist/dom/layoutSignature.js.map +1 -1
- package/dist/dom/snapshot.js +251 -96
- package/dist/dom/snapshot.js.map +1 -1
- package/dist/dom/textHost.d.ts +11 -0
- package/dist/dom/textHost.js +68 -0
- package/dist/dom/textHost.js.map +1 -0
- package/dist/slug/SlugMaterial.d.ts +3 -2
- package/dist/slug/SlugMaterial.js +28 -2
- package/dist/slug/SlugMaterial.js.map +1 -1
- package/dist/slug/coverage.d.ts +1 -1
- package/dist/slug/coverage.js +1 -1
- package/dist/slug/coverage.js.map +1 -1
- package/dist/slug/preprocessCore.d.ts +1 -2
- package/dist/slug/preprocessCore.js +39 -16
- package/dist/slug/preprocessCore.js.map +1 -1
- package/dist/slug/runtime.js +6 -4
- package/dist/slug/runtime.js.map +1 -1
- package/dist/slug/standaloneRuntime.d.ts +39 -0
- package/dist/slug/standaloneRuntime.js +140 -0
- package/dist/slug/standaloneRuntime.js.map +1 -0
- package/dist/slug/truetype.d.ts +22 -0
- package/dist/slug/truetype.js +494 -0
- package/dist/slug/truetype.js.map +1 -0
- package/dist/slug-vanilla.d.ts +364 -0
- package/dist/slug-vanilla.js +1461 -0
- package/dist/slug-vanilla.js.map +1 -0
- package/dist/slug.d.ts +21 -0
- package/dist/slug.js +19 -0
- package/dist/text/GlyphTextMaterial.d.ts +11 -11
- package/dist/text/GlyphTextMaterial.js +98 -29
- package/dist/text/GlyphTextMaterial.js.map +1 -1
- package/dist/text/SdfTextMaterial.d.ts +5 -4
- package/dist/text/SdfTextMaterial.js +22 -4
- package/dist/text/SdfTextMaterial.js.map +1 -1
- package/dist/types.d.ts +63 -14
- package/dist/vanilla.d.ts +3 -0
- package/dist/vanilla.js +132 -59
- package/dist/vanilla.js.map +1 -1
- package/dist/windfoil/WindfoilMaterial.d.ts +2 -1
- package/dist/windfoil/WindfoilMaterial.js +8 -1
- package/dist/windfoil/WindfoilMaterial.js.map +1 -1
- package/dist/windfoil/abortable.d.ts +2 -0
- package/dist/windfoil/abortable.js +26 -0
- package/dist/windfoil/abortable.js.map +1 -0
- package/dist/windfoil/color.js +2 -1
- package/dist/windfoil/color.js.map +1 -1
- package/dist/windfoil/coverage.d.ts +3 -20
- package/dist/windfoil/coverage.js +7 -20
- package/dist/windfoil/coverage.js.map +1 -1
- package/dist/windfoil/emoji.js +1 -1
- package/dist/windfoil/emoji.js.map +1 -1
- package/dist/windfoil/emojiMetrics.js +1 -1
- package/dist/windfoil/emojiMetrics.js.map +1 -1
- package/dist/windfoil/geometry.js +100 -33
- package/dist/windfoil/geometry.js.map +1 -1
- package/dist/windfoil/layerGrid.js +14 -10
- package/dist/windfoil/layerGrid.js.map +1 -1
- package/dist/windfoil/layerNormalization.d.ts +1 -1
- package/dist/windfoil/layerNormalization.js +1 -1
- package/dist/windfoil/layerNormalization.js.map +1 -1
- package/dist/windfoil/outlineValidation.d.ts +86 -0
- package/dist/windfoil/outlineValidation.js +65 -0
- package/dist/windfoil/outlineValidation.js.map +1 -0
- package/dist/windfoil/preprocess.js +3 -2
- package/dist/windfoil/preprocess.js.map +1 -1
- package/dist/windfoil/preprocessCore.d.ts +4 -85
- package/dist/windfoil/preprocessCore.js +210 -206
- package/dist/windfoil/preprocessCore.js.map +1 -1
- package/dist/windfoil/requests.js +1 -1
- package/dist/windfoil/requests.js.map +1 -1
- package/dist/windfoil/runtime.d.ts +2 -1
- package/dist/windfoil/runtime.js +3 -2
- package/dist/windfoil/runtime.js.map +1 -1
- package/dist/windfoil/shapeResources.js +2 -1
- package/dist/windfoil/shapeResources.js.map +1 -1
- package/dist/windfoil/shapes.js +1 -1
- package/dist/windfoil/skirt.d.ts +22 -0
- package/dist/windfoil/skirt.js +20 -0
- package/dist/windfoil/skirt.js.map +1 -0
- package/dist/windfoil/storage.d.ts +2 -2
- package/dist/windfoil/storage.js +1 -22
- package/dist/windfoil/storage.js.map +1 -1
- package/dist/windfoil/trace/contours.d.ts +0 -5
- package/dist/windfoil/trace/contours.js +81 -114
- package/dist/windfoil/trace/contours.js.map +1 -1
- package/dist/windfoil/trace/fit.d.ts +0 -6
- package/dist/windfoil/trace/fit.js +304 -308
- package/dist/windfoil/trace/fit.js.map +1 -1
- package/dist/windfoil/trace/outline.js +10 -10
- package/dist/windfoil/trace/outline.js.map +1 -1
- package/dist/windfoil/trace/types.d.ts +5 -3
- package/dist/windfoil/tracedOutlines.d.ts +37 -1
- package/dist/windfoil/tracedOutlines.js +172 -33
- package/dist/windfoil/tracedOutlines.js.map +1 -1
- package/package.json +57 -8
- package/skills/ultra-text/SKILL.md +114 -0
- package/skills/ultra-text/references/frameworks.md +43 -0
- package/skills/ultra-text/references/three-tsl.md +78 -0
- package/skills/ultra-text/references/vanilla-webgpu.md +95 -0
- package/dist/assets/cpuSdf.worker-Du9o8q4p.js.map +0 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { type BufferAttribute, type WebGPURenderer } from 'three/webgpu';
|
|
2
2
|
import type { DomSdfOptions } from '../types.js';
|
|
3
|
+
import { abortable, throwIfAborted } from './abortable.js';
|
|
3
4
|
import type { WindfoilStorage } from './WindfoilMaterial.js';
|
|
5
|
+
export { abortable, throwIfAborted };
|
|
4
6
|
/** The text effects of `DomSdfOptions` that an SDF underlay draws for analytic text. */
|
|
5
7
|
export type TextEffects = Pick<DomSdfOptions, 'outline' | 'dropShadow'>;
|
|
6
8
|
/**
|
|
@@ -9,8 +11,6 @@ export type TextEffects = Pick<DomSdfOptions, 'outline' | 'dropShadow'>;
|
|
|
9
11
|
* rejects, such as a negative width or a NaN, stay for it to reject.
|
|
10
12
|
*/
|
|
11
13
|
export declare function drawnEffects({ outline, dropShadow }: TextEffects): TextEffects | null;
|
|
12
|
-
export declare function throwIfAborted(signal?: AbortSignal): void;
|
|
13
|
-
export declare function abortable<T>(promise: PromiseLike<T>, signal?: AbortSignal): Promise<T>;
|
|
14
14
|
export declare function disposeWindfoilStorage(renderer: WebGPURenderer, attribute: BufferAttribute): void;
|
|
15
15
|
export declare function createWindfoilStorage(renderer: WebGPURenderer, data: {
|
|
16
16
|
curves: Float32Array;
|
package/dist/windfoil/storage.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { abortable, throwIfAborted } from "./abortable.js";
|
|
1
2
|
import { StorageBufferAttribute } from "three/webgpu";
|
|
2
3
|
import { storage } from "three/tsl";
|
|
3
4
|
// src/windfoil/storage.ts
|
|
@@ -12,28 +13,6 @@ function drawnEffects({ outline, dropShadow }) {
|
|
|
12
13
|
if (dropShadow && !((dropShadow.opacity ?? .5) <= 0)) drawn.dropShadow = dropShadow;
|
|
13
14
|
return drawn.outline ?? drawn.dropShadow ? drawn : null;
|
|
14
15
|
}
|
|
15
|
-
function throwIfAborted(signal) {
|
|
16
|
-
if (signal?.aborted) throw signal.reason ?? new DOMException("The Windfoil build was aborted", "AbortError");
|
|
17
|
-
}
|
|
18
|
-
function abortable(promise, signal) {
|
|
19
|
-
throwIfAborted(signal);
|
|
20
|
-
if (!signal) return Promise.resolve(promise);
|
|
21
|
-
return new Promise((resolve, reject) => {
|
|
22
|
-
const cleanup = () => signal.removeEventListener("abort", abort);
|
|
23
|
-
const abort = () => {
|
|
24
|
-
cleanup();
|
|
25
|
-
reject(signal.reason ?? new DOMException("The Windfoil build was aborted", "AbortError"));
|
|
26
|
-
};
|
|
27
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
28
|
-
Promise.resolve(promise).then((value) => {
|
|
29
|
-
cleanup();
|
|
30
|
-
resolve(value);
|
|
31
|
-
}, (error) => {
|
|
32
|
-
cleanup();
|
|
33
|
-
reject(error);
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
16
|
function disposeWindfoilStorage(renderer, attribute) {
|
|
38
17
|
attribute.dispose();
|
|
39
18
|
const backend = renderer.backend;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.js","names":[],"sources":["../../src/windfoil/storage.ts"],"sourcesContent":["import { StorageBufferAttribute, type BufferAttribute, type WebGPURenderer } from 'three/webgpu'\nimport { storage } from 'three/tsl'\nimport type { DomSdfOptions } from '../types.js'\nimport type { WindfoilStorage } from './WindfoilMaterial.js'\n/** The text effects of `DomSdfOptions` that an SDF underlay draws for analytic text. */\nexport type TextEffects = Pick<DomSdfOptions, 'outline' | 'dropShadow'>\n/**\n * The effects that draw anything, or null: an outline unless its width is zero or its opacity\n * at most zero, a drop shadow unless its opacity is at most zero. Values the SDF material\n * rejects, such as a negative width or a NaN, stay for it to reject.\n */\nexport function drawnEffects({ outline, dropShadow }: TextEffects): TextEffects | null {\n const drawn: TextEffects = {}\n if (outline && outline.width !== 0 && !((outline.opacity ?? 1) <= 0)) drawn.outline = outline\n if (dropShadow && !((dropShadow.opacity ?? 0.5) <= 0)) drawn.dropShadow = dropShadow\n return (drawn.outline ?? drawn.dropShadow) ? drawn : null\n}\nexport function
|
|
1
|
+
{"version":3,"file":"storage.js","names":[],"sources":["../../src/windfoil/storage.ts"],"sourcesContent":["import { StorageBufferAttribute, type BufferAttribute, type WebGPURenderer } from 'three/webgpu'\nimport { storage } from 'three/tsl'\nimport type { DomSdfOptions } from '../types.js'\nimport { abortable, throwIfAborted } from './abortable.js'\nimport type { WindfoilStorage } from './WindfoilMaterial.js'\n\nexport { abortable, throwIfAborted }\n\n/** The text effects of `DomSdfOptions` that an SDF underlay draws for analytic text. */\nexport type TextEffects = Pick<DomSdfOptions, 'outline' | 'dropShadow'>\n/**\n * The effects that draw anything, or null: an outline unless its width is zero or its opacity\n * at most zero, a drop shadow unless its opacity is at most zero. Values the SDF material\n * rejects, such as a negative width or a NaN, stay for it to reject.\n */\nexport function drawnEffects({ outline, dropShadow }: TextEffects): TextEffects | null {\n const drawn: TextEffects = {}\n if (outline && outline.width !== 0 && !((outline.opacity ?? 1) <= 0)) drawn.outline = outline\n if (dropShadow && !((dropShadow.opacity ?? 0.5) <= 0)) drawn.dropShadow = dropShadow\n return (drawn.outline ?? drawn.dropShadow) ? drawn : null\n}\nexport function disposeWindfoilStorage(renderer: WebGPURenderer, attribute: BufferAttribute): void {\n attribute.dispose()\n const backend = renderer.backend as unknown as {\n has(object: object): boolean\n destroyAttribute(attribute: BufferAttribute): void\n }\n if (backend.has(attribute)) {\n backend.destroyAttribute(attribute)\n renderer.info.destroyAttribute(attribute)\n }\n}\nexport function createWindfoilStorage(\n renderer: WebGPURenderer,\n data: { curves: Float32Array; rows: Uint32Array },\n): WindfoilStorage & { storageBytes: number; dispose(): void } {\n const attributes: StorageBufferAttribute[] = []\n let disposed = false\n const dispose = (): void => {\n if (disposed) return\n disposed = true\n for (const attribute of attributes) disposeWindfoilStorage(renderer, attribute)\n }\n try {\n const curveAttribute = new StorageBufferAttribute(\n data.curves.length ? data.curves : new Float32Array(2),\n 2,\n )\n attributes.push(curveAttribute)\n const rowAttribute = new StorageBufferAttribute(\n data.rows.length ? data.rows : new Uint32Array(1),\n 1,\n )\n attributes.push(rowAttribute)\n return {\n storageBytes: data.curves.byteLength + data.rows.byteLength,\n curves: storage(curveAttribute, 'vec2', curveAttribute.count)\n .toReadOnly()\n .setName('ultraWindfoilCurves'),\n rows: storage(rowAttribute, 'uint', rowAttribute.count)\n .toReadOnly()\n .setName('ultraWindfoilRows'),\n dispose,\n }\n } catch (error) {\n dispose()\n throw error\n }\n}\n"],"mappings":";;;;;;;;;AAeA,SAAgB,aAAa,EAAE,SAAS,cAA+C;CACrF,MAAM,QAAqB,CAAC;CAC5B,IAAI,WAAW,QAAQ,UAAU,KAAK,GAAG,QAAQ,WAAW,MAAM,IAAI,MAAM,UAAU;CACtF,IAAI,cAAc,GAAG,WAAW,WAAW,OAAQ,IAAI,MAAM,aAAa;CAC1E,OAAQ,MAAM,WAAW,MAAM,aAAc,QAAQ;AACvD;AACA,SAAgB,uBAAuB,UAA0B,WAAkC;CACjG,UAAU,QAAQ;CAClB,MAAM,UAAU,SAAS;CAIzB,IAAI,QAAQ,IAAI,SAAS,GAAG;EAC1B,QAAQ,iBAAiB,SAAS;EAClC,SAAS,KAAK,iBAAiB,SAAS;CAC1C;AACF;AACA,SAAgB,sBACd,UACA,MAC6D;CAC7D,MAAM,aAAuC,CAAC;CAC9C,IAAI,WAAW;CACf,MAAM,gBAAsB;EAC1B,IAAI,UAAU;EACd,WAAW;EACX,KAAK,MAAM,aAAa,YAAY,uBAAuB,UAAU,SAAS;CAChF;CACA,IAAI;EACF,MAAM,iBAAiB,IAAI,uBACzB,KAAK,OAAO,SAAS,KAAK,yBAAS,IAAI,aAAa,CAAC,GACrD,CACF;EACA,WAAW,KAAK,cAAc;EAC9B,MAAM,eAAe,IAAI,uBACvB,KAAK,KAAK,SAAS,KAAK,uBAAO,IAAI,YAAY,CAAC,GAChD,CACF;EACA,WAAW,KAAK,YAAY;EAC5B,OAAO;GACL,cAAc,KAAK,OAAO,aAAa,KAAK,KAAK;GACjD,QAAQ,QAAQ,gBAAgB,QAAQ,eAAe,KAAK,CAAC,CAC1D,WAAW,CAAC,CACZ,QAAQ,qBAAqB;GAChC,MAAM,QAAQ,cAAc,QAAQ,aAAa,KAAK,CAAC,CACpD,WAAW,CAAC,CACZ,QAAQ,mBAAmB;GAC9B;EACF;CACF,SAAS,OAAO;EACd,QAAQ;EACR,MAAM;CACR;AACF"}
|
|
@@ -1,7 +1,2 @@
|
|
|
1
1
|
import type { CoverageMask, Polyline, TraceScratch } from './types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Closed 0.5 contours of the coverage `mask`, in mask pixel coordinates (y down): outer
|
|
4
|
-
* boundaries have positive signed area, holes negative. Empty without ink. `scratch` is grown
|
|
5
|
-
* as needed and every edge mark it holds is cleared again before this returns.
|
|
6
|
-
*/
|
|
7
2
|
export declare function marchingSquares(mask: CoverageMask, scratch: TraceScratch): Polyline[];
|
|
@@ -1,51 +1,24 @@
|
|
|
1
1
|
// src/windfoil/trace/contours.ts
|
|
2
|
-
/** A coverage within this much of 0 or 1 is pure (after subtracting the 0.5 iso level). */
|
|
3
2
|
var PURE = .499;
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
[0, 3],
|
|
7
|
-
[1, 0],
|
|
8
|
-
[1, 3],
|
|
9
|
-
[2, 1],
|
|
10
|
-
[],
|
|
11
|
-
[2, 0],
|
|
12
|
-
[2, 3],
|
|
13
|
-
[3, 2],
|
|
14
|
-
[0, 2],
|
|
15
|
-
[],
|
|
16
|
-
[1, 2],
|
|
17
|
-
[3, 1],
|
|
18
|
-
[0, 1],
|
|
19
|
-
[3, 0],
|
|
20
|
-
[]
|
|
21
|
-
];
|
|
22
|
-
var JOIN_5 = [
|
|
23
|
-
0,
|
|
24
|
-
1,
|
|
25
|
-
2,
|
|
26
|
-
3
|
|
27
|
-
];
|
|
28
|
-
var SPLIT_5 = [
|
|
29
|
-
0,
|
|
3
|
+
var SINGLE_EDGE = new Int8Array([
|
|
4
|
+
-1,
|
|
30
5
|
3,
|
|
6
|
+
4,
|
|
7
|
+
7,
|
|
8
|
+
9,
|
|
9
|
+
-1,
|
|
10
|
+
8,
|
|
11
|
+
11,
|
|
12
|
+
14,
|
|
31
13
|
2,
|
|
32
|
-
1
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
3,
|
|
36
|
-
0,
|
|
37
|
-
1,
|
|
38
|
-
2
|
|
39
|
-
];
|
|
40
|
-
var SPLIT_10 = [
|
|
14
|
+
-1,
|
|
15
|
+
6,
|
|
16
|
+
13,
|
|
41
17
|
1,
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
function edgeAt(vertex, columns, side) {
|
|
47
|
-
return side === 0 ? 2 * vertex : side === 1 ? 2 * (vertex + 1) + 1 : side === 2 ? 2 * (vertex + columns) : 2 * vertex + 1;
|
|
48
|
-
}
|
|
18
|
+
12,
|
|
19
|
+
-1
|
|
20
|
+
]);
|
|
21
|
+
var edgeAt = (v, cols, s) => s === 2 ? 2 * (v + cols) : 2 * v + (s ? 4 - s : 0);
|
|
49
22
|
function collinear(points, a, b, c) {
|
|
50
23
|
const dx = points[b] - points[a];
|
|
51
24
|
const dy = points[b + 1] - points[a + 1];
|
|
@@ -53,94 +26,88 @@ function collinear(points, a, b, c) {
|
|
|
53
26
|
const ey = points[c + 1] - points[b + 1];
|
|
54
27
|
return dx * ey === dy * ex && dx * ex + dy * ey >= 0;
|
|
55
28
|
}
|
|
56
|
-
/** Appends a point, dropping exact straight continuations; no geometric tolerance. */
|
|
57
29
|
function appendPoint(points, x, y) {
|
|
58
30
|
points.push(x, y);
|
|
59
|
-
while (points.length >= 6 && collinear(points, points.length - 6, points.length - 4, points.length - 2))
|
|
60
|
-
points[points.length - 4] = points[points.length - 2];
|
|
61
|
-
points[points.length - 3] = points[points.length - 1];
|
|
62
|
-
points.length -= 2;
|
|
63
|
-
}
|
|
31
|
+
while (points.length >= 6 && collinear(points, points.length - 6, points.length - 4, points.length - 2)) points.splice(-4, 2);
|
|
64
32
|
}
|
|
65
33
|
function closeContour(points) {
|
|
66
34
|
while (points.length >= 6 && collinear(points, points.length - 4, points.length - 2, 0)) points.length -= 2;
|
|
67
|
-
while (points.length >= 6 && collinear(points, points.length - 2, 0, 2))
|
|
68
|
-
points.copyWithin(0, 2);
|
|
69
|
-
points.length -= 2;
|
|
70
|
-
}
|
|
35
|
+
while (points.length >= 6 && collinear(points, points.length - 2, 0, 2)) points.splice(0, 2);
|
|
71
36
|
}
|
|
72
|
-
/**
|
|
73
|
-
* Closed 0.5 contours of the coverage `mask`, in mask pixel coordinates (y down): outer
|
|
74
|
-
* boundaries have positive signed area, holes negative. Empty without ink. `scratch` is grown
|
|
75
|
-
* as needed and every edge mark it holds is cleared again before this returns.
|
|
76
|
-
*/
|
|
77
37
|
function marchingSquares(mask, scratch) {
|
|
78
38
|
const { alpha, width, height } = mask;
|
|
39
|
+
if (width <= 0 || height <= 0) return [];
|
|
79
40
|
const stride = mask.stride ?? 1;
|
|
80
41
|
const offset = mask.offset ?? 0;
|
|
81
|
-
const
|
|
42
|
+
const rowStride = mask.rowStride ?? width * stride;
|
|
43
|
+
const isFloat = alpha instanceof Float32Array;
|
|
44
|
+
const scale = isFloat ? 1 : 1 / 255;
|
|
45
|
+
const thresh = isFloat ? .5 : 128;
|
|
82
46
|
const columns = width + 2;
|
|
83
47
|
const vertices = columns * (height + 2);
|
|
84
|
-
if (scratch.value.length < vertices) scratch.value = new Float32Array(vertices);
|
|
85
|
-
const value = scratch.value;
|
|
86
|
-
value.fill(-.5, 0, columns);
|
|
87
|
-
value.fill(-.5, vertices - columns, vertices);
|
|
88
|
-
let inkLeft = width;
|
|
89
|
-
let inkTop = height;
|
|
90
|
-
let inkRight = -1;
|
|
91
|
-
let inkBottom = -1;
|
|
92
|
-
for (let y = 0; y < height; y++) {
|
|
93
|
-
const target = (y + 1) * columns + 1;
|
|
94
|
-
value[target - 1] = -.5;
|
|
95
|
-
value[target + width] = -.5;
|
|
96
|
-
let source = y * width * stride + offset;
|
|
97
|
-
let first = -1;
|
|
98
|
-
let last = -1;
|
|
99
|
-
for (let x = 0; x < width; x++, source += stride) {
|
|
100
|
-
const v = alpha[source] * scale - .5;
|
|
101
|
-
value[target + x] = v;
|
|
102
|
-
if (v >= 0) {
|
|
103
|
-
if (first < 0) first = x;
|
|
104
|
-
last = x;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
if (first >= 0) {
|
|
108
|
-
if (first < inkLeft) inkLeft = first;
|
|
109
|
-
if (last > inkRight) inkRight = last;
|
|
110
|
-
if (y < inkTop) inkTop = y;
|
|
111
|
-
inkBottom = y;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
if (inkRight < 0) return [];
|
|
115
48
|
if (scratch.next.length < 2 * vertices) scratch.next = new Int32Array(2 * vertices);
|
|
116
49
|
const next = scratch.next;
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
50
|
+
let active = scratch.active;
|
|
51
|
+
let activeLen = 0;
|
|
52
|
+
const pushPair = (vertex, pair) => {
|
|
53
|
+
const from = edgeAt(vertex, columns, pair >>> 2);
|
|
54
|
+
next[from] = edgeAt(vertex, columns, pair & 3) + 1;
|
|
55
|
+
if (activeLen === active.length) {
|
|
56
|
+
const grown = new Int32Array(Math.max(active.length * 2, 1024));
|
|
57
|
+
grown.set(active);
|
|
58
|
+
active = scratch.active = grown;
|
|
59
|
+
}
|
|
60
|
+
active[activeLen++] = from;
|
|
61
|
+
};
|
|
62
|
+
const sample = (gx, gy) => gx <= 0 || gy <= 0 || gx > width || gy > height ? -.5 : Math.fround(alpha[(gy - 1) * rowStride + (gx - 1) * stride + offset] * scale - .5);
|
|
63
|
+
const words = (columns + 31 >>> 5) + 1;
|
|
64
|
+
let prevBits = new Uint32Array(words);
|
|
65
|
+
let currBits = new Uint32Array(words);
|
|
66
|
+
let prevOr = 0;
|
|
67
|
+
for (let gy = 0; gy <= height; gy++) {
|
|
68
|
+
currBits.fill(0);
|
|
69
|
+
let currOr = 0;
|
|
70
|
+
if (gy < height) {
|
|
71
|
+
for (let gx = 1, src = gy * rowStride + offset; gx <= width; gx++, src += stride) if (alpha[src] >= thresh) {
|
|
72
|
+
const bit = 1 << (gx & 31);
|
|
73
|
+
currBits[gx >>> 5] |= bit;
|
|
74
|
+
currOr |= bit;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if ((prevOr | currOr) !== 0) {
|
|
78
|
+
const rowBase = gy * columns;
|
|
79
|
+
for (let w = 0; w < words - 1; w++) {
|
|
80
|
+
const top0 = prevBits[w];
|
|
81
|
+
const bot0 = currBits[w];
|
|
82
|
+
const top1 = top0 >>> 1 | prevBits[w + 1] << 31;
|
|
83
|
+
const bot1 = bot0 >>> 1 | currBits[w + 1] << 31;
|
|
84
|
+
let activeWords = top0 ^ top1 | bot0 ^ bot1 | top0 ^ bot0;
|
|
85
|
+
while (activeWords !== 0) {
|
|
86
|
+
const lsb = activeWords & -activeWords;
|
|
87
|
+
const bit = 31 - Math.clz32(lsb);
|
|
88
|
+
activeWords ^= lsb;
|
|
89
|
+
const gx = (w << 5) + bit;
|
|
90
|
+
const vertex = rowBase + gx;
|
|
91
|
+
const cell = top0 >>> bit & 1 | (top1 >>> bit & 1) << 1 | (bot1 >>> bit & 1) << 2 | (bot0 >>> bit & 1) << 3;
|
|
92
|
+
const pair = SINGLE_EDGE[cell];
|
|
93
|
+
if (pair >= 0) pushPair(vertex, pair);
|
|
94
|
+
else {
|
|
95
|
+
const saddle = sample(gx, gy) * sample(gx + 1, gy + 1) - sample(gx + 1, gy) * sample(gx, gy + 1);
|
|
96
|
+
const p = cell === 5 ? saddle > 0 ? 1 : 3 : saddle < 0 ? 12 : 4;
|
|
97
|
+
pushPair(vertex, p);
|
|
98
|
+
pushPair(vertex, p ^ 10);
|
|
99
|
+
}
|
|
135
100
|
}
|
|
136
101
|
}
|
|
137
|
-
a = b;
|
|
138
|
-
d = c;
|
|
139
|
-
maskAD = maskBC >>> 1 & 1 | maskBC << 1 & 8;
|
|
140
102
|
}
|
|
103
|
+
const swap = prevBits;
|
|
104
|
+
prevBits = currBits;
|
|
105
|
+
currBits = swap;
|
|
106
|
+
prevOr = currOr;
|
|
141
107
|
}
|
|
142
108
|
const contours = [];
|
|
143
|
-
for (
|
|
109
|
+
for (let idx = 0; idx < activeLen; idx++) {
|
|
110
|
+
const start = active[idx];
|
|
144
111
|
if (next[start] === 0) continue;
|
|
145
112
|
const points = [];
|
|
146
113
|
let edge = start;
|
|
@@ -149,8 +116,8 @@ function marchingSquares(mask, scratch) {
|
|
|
149
116
|
const vertical = edge & 1;
|
|
150
117
|
const y = vertex / columns | 0;
|
|
151
118
|
const x = vertex - y * columns;
|
|
152
|
-
const a =
|
|
153
|
-
const b =
|
|
119
|
+
const a = sample(x, y);
|
|
120
|
+
const b = vertical ? sample(x, y + 1) : sample(x + 1, y);
|
|
154
121
|
let t = -a / (b - a);
|
|
155
122
|
const high = a >= 0 ? a : b;
|
|
156
123
|
const low = a >= 0 ? b : a;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contours.js","names":[],"sources":["../../../src/windfoil/trace/contours.ts"],"sourcesContent":["// Outline tracer: marching-squares contours of a box-filtered coverage mask at 0.5, with the\n// area-exact crossing rule.\n//\n// Conventions\n// - Sample (x, y) of the mask sits at the texel centre (x + 0.5, y + 0.5); it is inside when its\n// coverage is at least 0.5.\n// - The mask is surrounded by one implicit ring of empty samples (at -0.5 and size + 0.5), so\n// every contour closes even when ink touches the mask's edge.\n// - Contours are closed polylines `[x0, y0, x1, y1, ...]` in mask coordinates (y down); the last\n// point joins the first. Inside is kept on the right of the direction of travel, so outer\n// boundaries have POSITIVE signed area `sum(x[i]*y[i+1] - x[i+1]*y[i]) / 2` and holes NEGATIVE:\n// once Y points up, outer contours run clockwise and holes counter-clockwise, as in TrueType.\n// - Ambiguous cells use the bilinear saddle rule, so a hairline diagonal one texel wide stays one\n// stroke instead of breaking at every step; exact ties leave the two inside samples separate.\nimport type { CoverageMask, Polyline, TraceScratch } from './types.js'\n\n/** A coverage within this much of 0 or 1 is pure (after subtracting the 0.5 iso level). */\nconst PURE = 0.499\n\n// Directed edges keep inside samples on the right in y-down coordinates.\nconst CONNECTIONS: readonly (readonly number[])[] = [\n [],\n [0, 3],\n [1, 0],\n [1, 3],\n [2, 1],\n [],\n [2, 0],\n [2, 3],\n [3, 2],\n [0, 2],\n [],\n [1, 2],\n [3, 1],\n [0, 1],\n [3, 0],\n [],\n]\nconst JOIN_5: readonly number[] = [0, 1, 2, 3]\nconst SPLIT_5: readonly number[] = [0, 3, 2, 1]\nconst JOIN_10: readonly number[] = [3, 0, 1, 2]\nconst SPLIT_10: readonly number[] = [1, 0, 3, 2]\n\nfunction edgeAt(vertex: number, columns: number, side: number): number {\n return side === 0\n ? 2 * vertex\n : side === 1\n ? 2 * (vertex + 1) + 1\n : side === 2\n ? 2 * (vertex + columns)\n : 2 * vertex + 1\n}\n\nfunction collinear(points: readonly number[], a: number, b: number, c: number): boolean {\n const dx = points[b]! - points[a]!\n const dy = points[b + 1]! - points[a + 1]!\n const ex = points[c]! - points[b]!\n const ey = points[c + 1]! - points[b + 1]!\n return dx * ey === dy * ex && dx * ex + dy * ey >= 0\n}\n\n/** Appends a point, dropping exact straight continuations; no geometric tolerance. */\nfunction appendPoint(points: Polyline, x: number, y: number): void {\n points.push(x, y)\n while (\n points.length >= 6 &&\n collinear(points, points.length - 6, points.length - 4, points.length - 2)\n ) {\n points[points.length - 4] = points[points.length - 2]!\n points[points.length - 3] = points[points.length - 1]!\n points.length -= 2\n }\n}\n\nfunction closeContour(points: Polyline): void {\n while (points.length >= 6 && collinear(points, points.length - 4, points.length - 2, 0))\n points.length -= 2\n while (points.length >= 6 && collinear(points, points.length - 2, 0, 2)) {\n points.copyWithin(0, 2)\n points.length -= 2\n }\n}\n\n/**\n * Closed 0.5 contours of the coverage `mask`, in mask pixel coordinates (y down): outer\n * boundaries have positive signed area, holes negative. Empty without ink. `scratch` is grown\n * as needed and every edge mark it holds is cleared again before this returns.\n */\nexport function marchingSquares(mask: CoverageMask, scratch: TraceScratch): Polyline[] {\n const { alpha, width, height } = mask\n const stride = mask.stride ?? 1\n const offset = mask.offset ?? 0\n const scale = alpha instanceof Float32Array ? 1 : 1 / 255\n const columns = width + 2\n const vertices = columns * (height + 2)\n if (scratch.value.length < vertices) scratch.value = new Float32Array(vertices)\n const value = scratch.value\n value.fill(-0.5, 0, columns)\n value.fill(-0.5, vertices - columns, vertices)\n let inkLeft = width\n let inkTop = height\n let inkRight = -1\n let inkBottom = -1\n for (let y = 0; y < height; y++) {\n const target = (y + 1) * columns + 1\n value[target - 1] = -0.5\n value[target + width] = -0.5\n let source = y * width * stride + offset\n let first = -1\n let last = -1\n for (let x = 0; x < width; x++, source += stride) {\n const v = alpha[source]! * scale - 0.5\n value[target + x] = v\n if (v >= 0) {\n if (first < 0) first = x\n last = x\n }\n }\n if (first >= 0) {\n if (first < inkLeft) inkLeft = first\n if (last > inkRight) inkRight = last\n if (y < inkTop) inkTop = y\n inkBottom = y\n }\n }\n if (inkRight < 0) return []\n if (scratch.next.length < 2 * vertices) scratch.next = new Int32Array(2 * vertices)\n const next = scratch.next\n const active: number[] = []\n for (let y = inkTop; y <= inkBottom + 1; y++) {\n let vertex = y * columns + inkLeft\n let a = value[vertex]!\n let d = value[vertex + columns]!\n let maskAD = (a >= 0 ? 1 : 0) | (d >= 0 ? 8 : 0)\n for (let x = inkLeft; x <= inkRight + 1; x++, vertex++) {\n const b = value[vertex + 1]!\n const c = value[vertex + columns + 1]!\n const maskBC = (b >= 0 ? 2 : 0) | (c >= 0 ? 4 : 0)\n const cell = maskAD | maskBC\n if (cell !== 0 && cell !== 15) {\n const saddle = a * c - b * d\n const connections =\n cell === 5\n ? saddle > 0\n ? JOIN_5\n : SPLIT_5\n : cell === 10\n ? saddle < 0\n ? JOIN_10\n : SPLIT_10\n : CONNECTIONS[cell]!\n for (let i = 0; i < connections.length; i += 2) {\n const from = edgeAt(vertex, columns, connections[i]!)\n next[from] = edgeAt(vertex, columns, connections[i + 1]!) + 1\n active.push(from)\n }\n }\n a = b\n d = c\n maskAD = ((maskBC >>> 1) & 1) | ((maskBC << 1) & 8)\n }\n }\n const contours: Polyline[] = []\n for (const start of active) {\n if (next[start] === 0) continue\n const points: Polyline = []\n let edge = start\n do {\n const vertex = edge >>> 1\n const vertical = edge & 1\n const y = (vertex / columns) | 0\n const x = vertex - y * columns\n const a = value[vertex]!\n const b = value[vertex + (vertical ? columns : 1)]!\n let t = -a / (b - a)\n // Box-filtered coverage: when one of the two samples is pure (fully in or out), the edge\n // crosses this grid line inside the other one, and the two coverages add up to its\n // position. Linear interpolation is off by up to 0.09 texel there.\n const high = a >= 0 ? a : b\n const low = a >= 0 ? b : a\n if (high >= PURE || low <= -PURE) {\n let fromHigh = high + low + 0.5\n fromHigh = fromHigh < 0 ? 0 : fromHigh > 1 ? 1 : fromHigh\n t = a >= 0 ? fromHigh : 1 - fromHigh\n }\n appendPoint(points, x - 0.5 + (vertical ? 0 : t), y - 0.5 + (vertical ? t : 0))\n const successor = next[edge]! - 1\n next[edge] = 0\n edge = successor\n } while (edge !== start)\n closeContour(points)\n if (points.length >= 6) contours.push(points)\n }\n return contours\n}\n"],"mappings":";;AAiBA,IAAM,OAAO;AAGb,IAAM,cAA8C;CAClD,CAAC;CACD,CAAC,GAAG,CAAC;CACL,CAAC,GAAG,CAAC;CACL,CAAC,GAAG,CAAC;CACL,CAAC,GAAG,CAAC;CACL,CAAC;CACD,CAAC,GAAG,CAAC;CACL,CAAC,GAAG,CAAC;CACL,CAAC,GAAG,CAAC;CACL,CAAC,GAAG,CAAC;CACL,CAAC;CACD,CAAC,GAAG,CAAC;CACL,CAAC,GAAG,CAAC;CACL,CAAC,GAAG,CAAC;CACL,CAAC,GAAG,CAAC;CACL,CAAC;AACH;AACA,IAAM,SAA4B;CAAC;CAAG;CAAG;CAAG;AAAC;AAC7C,IAAM,UAA6B;CAAC;CAAG;CAAG;CAAG;AAAC;AAC9C,IAAM,UAA6B;CAAC;CAAG;CAAG;CAAG;AAAC;AAC9C,IAAM,WAA8B;CAAC;CAAG;CAAG;CAAG;AAAC;AAE/C,SAAS,OAAO,QAAgB,SAAiB,MAAsB;CACrE,OAAO,SAAS,IACZ,IAAI,SACJ,SAAS,IACP,KAAK,SAAS,KAAK,IACnB,SAAS,IACP,KAAK,SAAS,WACd,IAAI,SAAS;AACvB;AAEA,SAAS,UAAU,QAA2B,GAAW,GAAW,GAAoB;CACtF,MAAM,KAAK,OAAO,KAAM,OAAO;CAC/B,MAAM,KAAK,OAAO,IAAI,KAAM,OAAO,IAAI;CACvC,MAAM,KAAK,OAAO,KAAM,OAAO;CAC/B,MAAM,KAAK,OAAO,IAAI,KAAM,OAAO,IAAI;CACvC,OAAO,KAAK,OAAO,KAAK,MAAM,KAAK,KAAK,KAAK,MAAM;AACrD;;AAGA,SAAS,YAAY,QAAkB,GAAW,GAAiB;CACjE,OAAO,KAAK,GAAG,CAAC;CAChB,OACE,OAAO,UAAU,KACjB,UAAU,QAAQ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AI;GACJ,SAAW,WAAW,IAAK,IAAO,UAAU,IAAK;EACnD;CACF;CACA,MAAM,WAAuB,CAAC;CAC9B,KAAK,MAAM,SAAS,QAAQ;EAC1B,IAAI,KAAK,WAAW,GAAG;EACvB,MAAM,SAAmB,CAAC;EAC1B,IAAI,OAAO;EACX,GAAG;GACD,MAAM,SAAS,SAAS;GACxB,MAAM,WAAW,OAAO;GACxB,MAAM,IAAK,SAAS,UAAW;GAC/B,MAAM,IAAI,SAAS,IAAI;GACvB,MAAM,IAAI,MAAM;GAChB,MAAM,IAAI,MAAM,UAAU,WAAW,UAAU;GAC/C,IAAI,IAAI,CAAC,KAAK,IAAI;GAIlB,MAAM,OAAO,KAAK,IAAI,IAAI;GAC1B,MAAM,MAAM,KAAK,IAAI,IAAI;GACzB,IAAI,QAAQ,QAAQ,OAAO,OAAO;IAChC,IAAI,WAAW,OAAO,MAAM;IAC5B,WAAW,WAAW,IAAI,IAAI,WAAW,IAAI,IAAI;IACjD,IAAI,KAAK,IAAI,WAAW,IAAI;GAC9B;GACA,YAAY,QAAQ,IAAI,MAAO,WAAW,IAAI,IAAI,IAAI,MAAO,WAAW,IAAI,EAAE;GAC9E,MAAM,YAAY,KAAK,QAAS;GAChC,KAAK,QAAQ;GACb,OAAO;EACT,SAAS,SAAS;EAClB,aAAa,MAAM;EACnB,IAAI,OAAO,UAAU,GAAG,SAAS,KAAK,MAAM;CAC9C;CACA,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"contours.js","names":[],"sources":["../../../src/windfoil/trace/contours.ts"],"sourcesContent":["// Outline tracer: marching-squares contours of a box-filtered coverage mask at 0.5, with the\n// area-exact crossing rule.\nimport type { CoverageMask, Polyline, TraceScratch } from './types.js'\n\nconst PURE = 0.499\nconst SINGLE_EDGE = new Int8Array([-1, 3, 4, 7, 9, -1, 8, 11, 14, 2, -1, 6, 13, 1, 12, -1])\n\nconst edgeAt = (v: number, cols: number, s: number): number =>\n s === 2 ? 2 * (v + cols) : 2 * v + (s ? 4 - s : 0)\n\nfunction collinear(points: readonly number[], a: number, b: number, c: number): boolean {\n const dx = points[b]! - points[a]!\n const dy = points[b + 1]! - points[a + 1]!\n const ex = points[c]! - points[b]!\n const ey = points[c + 1]! - points[b + 1]!\n return dx * ey === dy * ex && dx * ex + dy * ey >= 0\n}\n\nfunction appendPoint(points: Polyline, x: number, y: number): void {\n points.push(x, y)\n while (\n points.length >= 6 &&\n collinear(points, points.length - 6, points.length - 4, points.length - 2)\n )\n points.splice(-4, 2)\n}\n\nfunction closeContour(points: Polyline): void {\n while (points.length >= 6 && collinear(points, points.length - 4, points.length - 2, 0))\n points.length -= 2\n while (points.length >= 6 && collinear(points, points.length - 2, 0, 2)) points.splice(0, 2)\n}\n\nexport function marchingSquares(mask: CoverageMask, scratch: TraceScratch): Polyline[] {\n const { alpha, width, height } = mask\n if (width <= 0 || height <= 0) return []\n const stride = mask.stride ?? 1\n const offset = mask.offset ?? 0\n const rowStride = mask.rowStride ?? width * stride\n const isFloat = alpha instanceof Float32Array\n const scale = isFloat ? 1 : 1 / 255\n const thresh = isFloat ? 0.5 : 128\n const columns = width + 2\n const vertices = columns * (height + 2)\n if (scratch.next.length < 2 * vertices) scratch.next = new Int32Array(2 * vertices)\n const next = scratch.next\n let active = scratch.active\n let activeLen = 0\n const pushPair = (vertex: number, pair: number): void => {\n const from = edgeAt(vertex, columns, pair >>> 2)\n next[from] = edgeAt(vertex, columns, pair & 3) + 1\n if (activeLen === active.length) {\n const grown = new Int32Array(Math.max(active.length * 2, 1024))\n grown.set(active)\n active = scratch.active = grown\n }\n active[activeLen++] = from\n }\n\n const sample = (gx: number, gy: number): number =>\n gx <= 0 || gy <= 0 || gx > width || gy > height\n ? -0.5\n : Math.fround(alpha[(gy - 1) * rowStride + (gx - 1) * stride + offset]! * scale - 0.5)\n\n const words = ((columns + 31) >>> 5) + 1\n let prevBits = new Uint32Array(words)\n let currBits = new Uint32Array(words)\n let prevOr = 0\n\n for (let gy = 0; gy <= height; gy++) {\n currBits.fill(0)\n let currOr = 0\n if (gy < height) {\n for (let gx = 1, src = gy * rowStride + offset; gx <= width; gx++, src += stride) {\n if (alpha[src]! >= thresh) {\n const bit = 1 << (gx & 31)\n currBits[gx >>> 5]! |= bit\n currOr |= bit\n }\n }\n }\n if ((prevOr | currOr) !== 0) {\n const rowBase = gy * columns\n for (let w = 0; w < words - 1; w++) {\n const top0 = prevBits[w]!\n const bot0 = currBits[w]!\n const top1 = (top0 >>> 1) | (prevBits[w + 1]! << 31)\n const bot1 = (bot0 >>> 1) | (currBits[w + 1]! << 31)\n let activeWords = (top0 ^ top1) | (bot0 ^ bot1) | (top0 ^ bot0)\n while (activeWords !== 0) {\n const lsb = activeWords & -activeWords\n const bit = 31 - Math.clz32(lsb)\n activeWords ^= lsb\n const gx = (w << 5) + bit\n const vertex = rowBase + gx\n const cell =\n ((top0 >>> bit) & 1) |\n (((top1 >>> bit) & 1) << 1) |\n (((bot1 >>> bit) & 1) << 2) |\n (((bot0 >>> bit) & 1) << 3)\n const pair = SINGLE_EDGE[cell]!\n if (pair >= 0) {\n pushPair(vertex, pair)\n } else {\n const saddle =\n sample(gx, gy) * sample(gx + 1, gy + 1) - sample(gx + 1, gy) * sample(gx, gy + 1)\n const p = cell === 5 ? (saddle > 0 ? 1 : 3) : saddle < 0 ? 12 : 4\n pushPair(vertex, p)\n pushPair(vertex, p ^ 10)\n }\n }\n }\n }\n const swap = prevBits\n prevBits = currBits\n currBits = swap\n prevOr = currOr\n }\n\n const contours: Polyline[] = []\n for (let idx = 0; idx < activeLen; idx++) {\n const start = active[idx]!\n if (next[start] === 0) continue\n const points: Polyline = []\n let edge = start\n do {\n const vertex = edge >>> 1\n const vertical = edge & 1\n const y = (vertex / columns) | 0\n const x = vertex - y * columns\n const a = sample(x, y)\n const b = vertical ? sample(x, y + 1) : sample(x + 1, y)\n let t = -a / (b - a)\n const high = a >= 0 ? a : b\n const low = a >= 0 ? b : a\n if (high >= PURE || low <= -PURE) {\n let fromHigh = high + low + 0.5\n fromHigh = fromHigh < 0 ? 0 : fromHigh > 1 ? 1 : fromHigh\n t = a >= 0 ? fromHigh : 1 - fromHigh\n }\n appendPoint(points, x - 0.5 + (vertical ? 0 : t), y - 0.5 + (vertical ? t : 0))\n const successor = next[edge]! - 1\n next[edge] = 0\n edge = successor\n } while (edge !== start)\n closeContour(points)\n if (points.length >= 6) contours.push(points)\n }\n return contours\n}\n"],"mappings":";AAIA,IAAM,OAAO;AACb,IAAM,cAAc,IAAI,UAAU;CAAC;CAAI;CAAG;CAAG;CAAG;CAAG;CAAI;CAAG;CAAI;CAAI;CAAG;CAAI;CAAG;CAAI;CAAG;CAAI;AAAE,CAAC;AAE1F,IAAM,UAAU,GAAW,MAAc,MACvC,MAAM,IAAI,KAAK,IAAI,QAAQ,IAAI,KAAK,IAAI,IAAI,IAAI;AAElD,SAAS,UAAU,QAA2B,GAAW,GAAW,GAAoB;CACtF,MAAM,KAAK,OAAO,KAAM,OAAO;CAC/B,MAAM,KAAK,OAAO,IAAI,KAAM,OAAO,IAAI;CACvC,MAAM,KAAK,OAAO,KAAM,OAAO;CAC/B,MAAM,KAAK,OAAO,IAAI,KAAM,OAAO,IAAI;CACvC,OAAO,KAAK,OAAO,KAAK,MAAM,KAAK,KAAK,KAAK,MAAM;AACrD;AAEA,SAAS,YAAY,QAAkB,GAAW,GAAiB;CACjE,OAAO,KAAK,GAAG,CAAC;CAChB,OACE,OAAO,UAAU,KACjB,UAAU,QAAQ,OAAO,SAAS,GAAG,OAAO,SAAS,GAAG,OAAO,SAAS,CAAC,GAEzE,OAAO,OAAO,IAAI,CAAC;AACvB;AAEA,SAAS,aAAa,QAAwB;CAC5C,OAAO,OAAO,UAAU,KAAK,UAAU,QAAQ,OAAO,SAAS,GAAG,OAAO,SAAS,GAAG,CAAC,GACpF,OAAO,UAAU;CACnB,OAAO,OAAO,UAAU,KAAK,UAAU,QAAQ,OAAO,SAAS,GAAG,GAAG,CAAC,GAAG,OAAO,OAAO,GAA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AI,MAAM,GAAG,MAAM,WAAW,OAAO;EACxC,MAAM,QAAQ,OAAO;EACrB,IAAI,KAAK,WAAW,GAAG;EACvB,MAAM,SAAmB,CAAC;EAC1B,IAAI,OAAO;EACX,GAAG;GACD,MAAM,SAAS,SAAS;GACxB,MAAM,WAAW,OAAO;GACxB,MAAM,IAAK,SAAS,UAAW;GAC/B,MAAM,IAAI,SAAS,IAAI;GACvB,MAAM,IAAI,OAAO,GAAG,CAAC;GACrB,MAAM,IAAI,WAAW,OAAO,GAAG,IAAI,CAAC,IAAI,OAAO,IAAI,GAAG,CAAC;GACvD,IAAI,IAAI,CAAC,KAAK,IAAI;GAClB,MAAM,OAAO,KAAK,IAAI,IAAI;GAC1B,MAAM,MAAM,KAAK,IAAI,IAAI;GACzB,IAAI,QAAQ,QAAQ,OAAO,OAAO;IAChC,IAAI,WAAW,OAAO,MAAM;IAC5B,WAAW,WAAW,IAAI,IAAI,WAAW,IAAI,IAAI;IACjD,IAAI,KAAK,IAAI,WAAW,IAAI;GAC9B;GACA,YAAY,QAAQ,IAAI,MAAO,WAAW,IAAI,IAAI,IAAI,MAAO,WAAW,IAAI,EAAE;GAC9E,MAAM,YAAY,KAAK,QAAS;GAChC,KAAK,QAAQ;GACb,OAAO;EACT,SAAS,SAAS;EAClB,aAAa,MAAM;EACnB,IAAI,OAAO,UAAU,GAAG,SAAS,KAAK,MAAM;CAC9C;CACA,OAAO;AACT"}
|
|
@@ -1,8 +1,2 @@
|
|
|
1
1
|
import type { FittedOutline, TraceScratch } from './types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Fits a closed polyline (flat `[x, y, ...]`, the last point joins the first) with quadratic
|
|
4
|
-
* Béziers whose distance from the resampled polyline stays within `tolerance` texels
|
|
5
|
-
* (round-off within a texel of a corner is exempt). Orientation is the input's. Null when the
|
|
6
|
-
* polyline is degenerate.
|
|
7
|
-
*/
|
|
8
2
|
export declare function fitClosedPolyline(points: ArrayLike<number>, tolerance: number, scratch: TraceScratch): FittedOutline | null;
|