partforge 0.19.0 → 0.20.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/docs/AUTHORING-PARTS.md +97 -1
- package/docs/ERROR-PATTERNS.md +6 -0
- package/package.json +3 -1
- package/src/app-bracket.js +9 -0
- package/src/app-embed-test.js +68 -0
- package/src/app-nameplate.js +9 -0
- package/src/app-text-smoke.js +10 -0
- package/src/bracket-worker.js +3 -0
- package/src/framework/app.css +23 -6
- package/src/framework/cutaway-controls.js +155 -0
- package/src/framework/cutaway-gizmo.js +686 -0
- package/src/framework/cutaway-math.js +53 -0
- package/src/framework/cutaway-render.js +338 -0
- package/src/framework/cutaway.js +469 -0
- package/src/framework/fonts.js +36 -0
- package/src/framework/geometry/curve-fill.js +86 -0
- package/src/framework/geometry/fonts/Roboto-LICENSE.txt +93 -0
- package/src/framework/geometry/fonts/Roboto-Regular.ttf +0 -0
- package/src/framework/geometry/fonts/default-font.js +3 -0
- package/src/framework/geometry/kernel-front.js +53 -0
- package/src/framework/geometry/kernel.js +1 -1
- package/src/framework/geometry/text2d.js +98 -0
- package/src/framework/geometry-service.js +21 -2
- package/src/framework/jobs.js +9 -0
- package/src/framework/mount.js +278 -223
- package/src/framework/selection/hover.js +102 -36
- package/src/framework/selection/raycast.js +4 -1
- package/src/framework/tooltip.js +282 -0
- package/src/framework/viewer-controls.js +25 -2
- package/src/framework/viewer-lighting.js +13 -0
- package/src/framework/viewer.js +83 -10
- package/src/nameplate-worker.js +3 -0
- package/src/parts/bracket.js +76 -0
- package/src/parts/nameplate.js +74 -0
- package/src/parts/text-smoke.js +21 -0
- package/src/testing/manifold.js +6 -2
- package/src/testing/occt.js +6 -2
- package/src/text-smoke-worker.js +3 -0
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
|
|
3
|
+
import { createCutawayGizmo } from "./cutaway-gizmo.js";
|
|
4
|
+
import {
|
|
5
|
+
initialCutawayPose,
|
|
6
|
+
planeFromPose,
|
|
7
|
+
pointSurvivesPlane,
|
|
8
|
+
} from "./cutaway-math.js";
|
|
9
|
+
import { createSectionRenderSet } from "./cutaway-render.js";
|
|
10
|
+
|
|
11
|
+
const IDLE_DELAY_MS = 800;
|
|
12
|
+
|
|
13
|
+
function defaultSchedule(callback, delay) {
|
|
14
|
+
const timer = setTimeout(callback, delay);
|
|
15
|
+
return () => clearTimeout(timer);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function validBounds(getBounds) {
|
|
19
|
+
try {
|
|
20
|
+
const bounds = getBounds?.();
|
|
21
|
+
return bounds?.isBox3 && !bounds.isEmpty() ? bounds : null;
|
|
22
|
+
} catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function createCutaway({
|
|
28
|
+
renderer,
|
|
29
|
+
scene,
|
|
30
|
+
camera,
|
|
31
|
+
orbitControls,
|
|
32
|
+
domElement,
|
|
33
|
+
getBounds,
|
|
34
|
+
edgeColor,
|
|
35
|
+
schedule = defaultSchedule,
|
|
36
|
+
}) {
|
|
37
|
+
let supported = false;
|
|
38
|
+
try {
|
|
39
|
+
supported = Boolean(
|
|
40
|
+
renderer.getContext().getContextAttributes().stencil,
|
|
41
|
+
);
|
|
42
|
+
} catch {
|
|
43
|
+
supported = false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const plane = new THREE.Plane();
|
|
47
|
+
const planeNormal = new THREE.Vector3();
|
|
48
|
+
const capGeometry = new THREE.PlaneGeometry(1, 1);
|
|
49
|
+
const overlayScene = new THREE.Scene();
|
|
50
|
+
const renderSets = new Map();
|
|
51
|
+
const auxiliaryMaterials = new Map();
|
|
52
|
+
let selectedNames = null;
|
|
53
|
+
let nextOrder = 0;
|
|
54
|
+
let enabled = false;
|
|
55
|
+
let flipped = false;
|
|
56
|
+
let theme = "dark";
|
|
57
|
+
let hatchInk = edgeColor;
|
|
58
|
+
let pose = null;
|
|
59
|
+
let viewportSize = null;
|
|
60
|
+
let cancelIdle = null;
|
|
61
|
+
let previousLocalClippingEnabled;
|
|
62
|
+
let disposed = false;
|
|
63
|
+
let disabling = false;
|
|
64
|
+
let hoveredHandle = null;
|
|
65
|
+
const handleHoverSubscribers = new Set();
|
|
66
|
+
const pendingHandlePublications = [];
|
|
67
|
+
let publishingHandleHover = false;
|
|
68
|
+
|
|
69
|
+
function reportHandleHoverError(error) {
|
|
70
|
+
try {
|
|
71
|
+
console.error("Cutaway handle hover subscriber failed", error);
|
|
72
|
+
} catch {
|
|
73
|
+
// Reporting must not let application callbacks interrupt controller work.
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function drainHandleHoverPublications() {
|
|
78
|
+
while (pendingHandlePublications.length > 0) {
|
|
79
|
+
const publication = pendingHandlePublications.shift();
|
|
80
|
+
for (const record of publication.subscribers) {
|
|
81
|
+
if (!record.active) continue;
|
|
82
|
+
try {
|
|
83
|
+
record.listener(publication.handle);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
reportHandleHoverError(error);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function publishHandleHover(nextHandle) {
|
|
92
|
+
const normalized = nextHandle === "translate"
|
|
93
|
+
|| nextHandle === "rotate-x"
|
|
94
|
+
|| nextHandle === "rotate-y"
|
|
95
|
+
? nextHandle
|
|
96
|
+
: null;
|
|
97
|
+
if (normalized === hoveredHandle) return;
|
|
98
|
+
hoveredHandle = normalized;
|
|
99
|
+
pendingHandlePublications.push({
|
|
100
|
+
handle: normalized,
|
|
101
|
+
subscribers: [...handleHoverSubscribers],
|
|
102
|
+
});
|
|
103
|
+
if (publishingHandleHover) return;
|
|
104
|
+
|
|
105
|
+
publishingHandleHover = true;
|
|
106
|
+
try {
|
|
107
|
+
drainHandleHoverPublications();
|
|
108
|
+
} finally {
|
|
109
|
+
publishingHandleHover = false;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function onHandleHoverChange(listener) {
|
|
114
|
+
if (disposed || typeof listener !== "function") return () => {};
|
|
115
|
+
const record = { listener, active: true };
|
|
116
|
+
handleHoverSubscribers.add(record);
|
|
117
|
+
try {
|
|
118
|
+
listener(hoveredHandle);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
record.active = false;
|
|
121
|
+
handleHoverSubscribers.delete(record);
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
return () => {
|
|
125
|
+
if (!record.active) return;
|
|
126
|
+
record.active = false;
|
|
127
|
+
handleHoverSubscribers.delete(record);
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function clearHandleHoverSubscribers() {
|
|
132
|
+
for (const record of handleHoverSubscribers) record.active = false;
|
|
133
|
+
handleHoverSubscribers.clear();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function selected(name) {
|
|
137
|
+
return selectedNames == null || selectedNames.has(name);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function applyCapPose(renderSet) {
|
|
141
|
+
if (!pose) return;
|
|
142
|
+
renderSet.setCapPose({
|
|
143
|
+
position: pose.position,
|
|
144
|
+
quaternion: pose.quaternion,
|
|
145
|
+
size: pose.size,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function cancelIdleFade() {
|
|
150
|
+
if (!cancelIdle) return;
|
|
151
|
+
cancelIdle();
|
|
152
|
+
cancelIdle = null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function showActive() {
|
|
156
|
+
cancelIdleFade();
|
|
157
|
+
gizmo.setActiveAppearance(true);
|
|
158
|
+
const scheduled = schedule(() => {
|
|
159
|
+
cancelIdle = null;
|
|
160
|
+
if (enabled && !disposed) gizmo.setActiveAppearance(false);
|
|
161
|
+
}, IDLE_DELAY_MS);
|
|
162
|
+
cancelIdle = typeof scheduled === "function"
|
|
163
|
+
? scheduled
|
|
164
|
+
: () => clearTimeout(scheduled);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function setMaterialClippingPlanes(material, clippingPlanes) {
|
|
168
|
+
if (material.clippingPlanes === clippingPlanes) return;
|
|
169
|
+
material.clippingPlanes = clippingPlanes;
|
|
170
|
+
material.needsUpdate = true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function syncAuxiliaryMaterial(material, entry) {
|
|
174
|
+
if (enabled) {
|
|
175
|
+
if (
|
|
176
|
+
!Array.isArray(material.clippingPlanes)
|
|
177
|
+
|| material.clippingPlanes.length !== 1
|
|
178
|
+
|| material.clippingPlanes[0] !== plane
|
|
179
|
+
) {
|
|
180
|
+
setMaterialClippingPlanes(material, [plane]);
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
setMaterialClippingPlanes(material, entry.originalClippingPlanes);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function syncAuxiliaryMaterials() {
|
|
188
|
+
for (const [material, entry] of auxiliaryMaterials) {
|
|
189
|
+
syncAuxiliaryMaterial(material, entry);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function applyPose(nextPose, { resetFlip = false, activeAppearance = false } = {}) {
|
|
194
|
+
pose = {
|
|
195
|
+
position: nextPose.position.clone(),
|
|
196
|
+
quaternion: nextPose.quaternion.clone(),
|
|
197
|
+
size: nextPose.size,
|
|
198
|
+
};
|
|
199
|
+
if (resetFlip) flipped = false;
|
|
200
|
+
planeFromPose(plane, planeNormal, pose.position, pose.quaternion, flipped);
|
|
201
|
+
for (const { renderSet } of renderSets.values()) applyCapPose(renderSet);
|
|
202
|
+
gizmo.setFlipped(flipped);
|
|
203
|
+
gizmo.setPose(pose);
|
|
204
|
+
if (activeAppearance) showActive();
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function onPoseChange(nextPose) {
|
|
208
|
+
if (!enabled || disposed) return;
|
|
209
|
+
applyPose(nextPose, { activeAppearance: true });
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const gizmo = createCutawayGizmo({
|
|
213
|
+
scene,
|
|
214
|
+
overlayScene,
|
|
215
|
+
camera,
|
|
216
|
+
domElement,
|
|
217
|
+
orbitControls,
|
|
218
|
+
onPoseChange,
|
|
219
|
+
onActivity: showActive,
|
|
220
|
+
onHandleHoverChange: publishHandleHover,
|
|
221
|
+
});
|
|
222
|
+
gizmo.setVisible(false);
|
|
223
|
+
gizmo.setTheme(theme);
|
|
224
|
+
|
|
225
|
+
function setSubpart(name, mesh, edgeLines) {
|
|
226
|
+
if (disposed) return false;
|
|
227
|
+
const previous = renderSets.get(name);
|
|
228
|
+
const order = previous?.order ?? nextOrder++;
|
|
229
|
+
previous?.renderSet.dispose();
|
|
230
|
+
|
|
231
|
+
const renderSet = createSectionRenderSet({
|
|
232
|
+
scene,
|
|
233
|
+
mesh,
|
|
234
|
+
edgeLines,
|
|
235
|
+
plane,
|
|
236
|
+
capGeometry,
|
|
237
|
+
order,
|
|
238
|
+
inkColor: hatchInk,
|
|
239
|
+
});
|
|
240
|
+
renderSets.set(name, { renderSet, mesh, edgeLines, order });
|
|
241
|
+
if (viewportSize) {
|
|
242
|
+
renderSet.setViewportSize(
|
|
243
|
+
viewportSize.width,
|
|
244
|
+
viewportSize.height,
|
|
245
|
+
viewportSize.pixelRatio,
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
applyCapPose(renderSet);
|
|
249
|
+
renderSet.setVisible(enabled && selected(name));
|
|
250
|
+
renderSet.setEnabled(enabled);
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function updateGeometry(name, geometry) {
|
|
255
|
+
const entry = renderSets.get(name);
|
|
256
|
+
if (!entry || disposed) return false;
|
|
257
|
+
entry.renderSet.setGeometry(geometry);
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function setVisible(names) {
|
|
262
|
+
if (disposed) return false;
|
|
263
|
+
selectedNames = new Set(typeof names === "string" ? [names] : names ?? []);
|
|
264
|
+
for (const [name, { renderSet }] of renderSets) {
|
|
265
|
+
renderSet.setVisible(enabled && selected(name));
|
|
266
|
+
}
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function disable() {
|
|
271
|
+
if (disabling) return true;
|
|
272
|
+
disabling = true;
|
|
273
|
+
let firstError = null;
|
|
274
|
+
const attempt = (callback) => {
|
|
275
|
+
try {
|
|
276
|
+
callback();
|
|
277
|
+
} catch (error) {
|
|
278
|
+
firstError ??= error;
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
const wasEnabled = enabled;
|
|
282
|
+
enabled = false;
|
|
283
|
+
try {
|
|
284
|
+
attempt(cancelIdleFade);
|
|
285
|
+
attempt(() => gizmo.setVisible(false));
|
|
286
|
+
publishHandleHover(null);
|
|
287
|
+
if (wasEnabled) {
|
|
288
|
+
for (const { renderSet } of renderSets.values()) {
|
|
289
|
+
attempt(() => renderSet.setVisible(false));
|
|
290
|
+
attempt(() => renderSet.setEnabled(false));
|
|
291
|
+
}
|
|
292
|
+
for (const [material, entry] of auxiliaryMaterials) {
|
|
293
|
+
attempt(() => syncAuxiliaryMaterial(material, entry));
|
|
294
|
+
}
|
|
295
|
+
attempt(() => {
|
|
296
|
+
renderer.localClippingEnabled = previousLocalClippingEnabled;
|
|
297
|
+
});
|
|
298
|
+
previousLocalClippingEnabled = undefined;
|
|
299
|
+
}
|
|
300
|
+
} finally {
|
|
301
|
+
disabling = false;
|
|
302
|
+
}
|
|
303
|
+
if (firstError) throw firstError;
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function setEnabled(on) {
|
|
308
|
+
if (disposed || disabling) return false;
|
|
309
|
+
if (!on) return disable();
|
|
310
|
+
if (enabled) return true;
|
|
311
|
+
if (!supported) return false;
|
|
312
|
+
const bounds = validBounds(getBounds);
|
|
313
|
+
if (!bounds) return false;
|
|
314
|
+
|
|
315
|
+
const initialPose = initialCutawayPose(bounds, camera);
|
|
316
|
+
previousLocalClippingEnabled = renderer.localClippingEnabled;
|
|
317
|
+
enabled = true;
|
|
318
|
+
flipped = false;
|
|
319
|
+
applyPose(initialPose);
|
|
320
|
+
renderer.localClippingEnabled = true;
|
|
321
|
+
for (const [name, { renderSet }] of renderSets) {
|
|
322
|
+
renderSet.setVisible(selected(name));
|
|
323
|
+
renderSet.setEnabled(true);
|
|
324
|
+
}
|
|
325
|
+
syncAuxiliaryMaterials();
|
|
326
|
+
gizmo.setVisible(true);
|
|
327
|
+
gizmo.updateForCamera();
|
|
328
|
+
showActive();
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function reset() {
|
|
333
|
+
if (!enabled || disposed) return false;
|
|
334
|
+
const bounds = validBounds(getBounds);
|
|
335
|
+
if (!bounds) return false;
|
|
336
|
+
applyPose(initialCutawayPose(bounds, camera), {
|
|
337
|
+
resetFlip: true,
|
|
338
|
+
activeAppearance: true,
|
|
339
|
+
});
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function flip() {
|
|
344
|
+
if (!enabled || disposed || !pose) return false;
|
|
345
|
+
flipped = !flipped;
|
|
346
|
+
planeFromPose(plane, planeNormal, pose.position, pose.quaternion, flipped);
|
|
347
|
+
gizmo.setFlipped(flipped);
|
|
348
|
+
showActive();
|
|
349
|
+
return true;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function setTheme(mode, edgeColor) {
|
|
353
|
+
if (disposed) return false;
|
|
354
|
+
theme = mode;
|
|
355
|
+
if (edgeColor != null) hatchInk = edgeColor;
|
|
356
|
+
gizmo.setTheme(mode);
|
|
357
|
+
for (const { renderSet } of renderSets.values()) {
|
|
358
|
+
renderSet.refreshSourceMaterial();
|
|
359
|
+
renderSet.setHatchInk(hatchInk);
|
|
360
|
+
}
|
|
361
|
+
return true;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function setViewportSize(width, height, pixelRatio = 1) {
|
|
365
|
+
if (disposed) return false;
|
|
366
|
+
viewportSize = { width, height, pixelRatio };
|
|
367
|
+
for (const { renderSet } of renderSets.values()) {
|
|
368
|
+
renderSet.setViewportSize(width, height, pixelRatio);
|
|
369
|
+
}
|
|
370
|
+
return true;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function isPointVisible(point) {
|
|
374
|
+
return !enabled || pointSurvivesPlane(plane, point);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function registerClippableMaterial(material) {
|
|
378
|
+
if (disposed || !material) return () => {};
|
|
379
|
+
let entry = auxiliaryMaterials.get(material);
|
|
380
|
+
if (entry) {
|
|
381
|
+
entry.count += 1;
|
|
382
|
+
} else {
|
|
383
|
+
entry = {
|
|
384
|
+
count: 1,
|
|
385
|
+
originalClippingPlanes: material.clippingPlanes,
|
|
386
|
+
};
|
|
387
|
+
auxiliaryMaterials.set(material, entry);
|
|
388
|
+
}
|
|
389
|
+
syncAuxiliaryMaterial(material, entry);
|
|
390
|
+
let registered = true;
|
|
391
|
+
return () => {
|
|
392
|
+
if (!registered) return;
|
|
393
|
+
registered = false;
|
|
394
|
+
const current = auxiliaryMaterials.get(material);
|
|
395
|
+
if (!current) return;
|
|
396
|
+
current.count -= 1;
|
|
397
|
+
if (current.count > 0) {
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
auxiliaryMaterials.delete(material);
|
|
401
|
+
setMaterialClippingPlanes(material, current.originalClippingPlanes);
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function updateForCamera() {
|
|
406
|
+
if (enabled && !disposed) gizmo.updateForCamera();
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function renderOverlay(targetRenderer, targetCamera) {
|
|
410
|
+
if (!enabled || disposed) return false;
|
|
411
|
+
const previousAutoClear = targetRenderer.autoClear;
|
|
412
|
+
try {
|
|
413
|
+
targetRenderer.autoClear = false;
|
|
414
|
+
targetRenderer.clearDepth();
|
|
415
|
+
targetRenderer.render(overlayScene, targetCamera);
|
|
416
|
+
} finally {
|
|
417
|
+
targetRenderer.autoClear = previousAutoClear;
|
|
418
|
+
}
|
|
419
|
+
return true;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function dispose() {
|
|
423
|
+
if (disposed) return;
|
|
424
|
+
disposed = true;
|
|
425
|
+
let firstError = null;
|
|
426
|
+
const attempt = (callback) => {
|
|
427
|
+
try {
|
|
428
|
+
callback();
|
|
429
|
+
} catch (error) {
|
|
430
|
+
firstError ??= error;
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
attempt(disable);
|
|
434
|
+
for (const { renderSet } of renderSets.values()) {
|
|
435
|
+
attempt(() => renderSet.dispose());
|
|
436
|
+
}
|
|
437
|
+
renderSets.clear();
|
|
438
|
+
for (const [material, entry] of auxiliaryMaterials) {
|
|
439
|
+
attempt(() => setMaterialClippingPlanes(material, entry.originalClippingPlanes));
|
|
440
|
+
}
|
|
441
|
+
auxiliaryMaterials.clear();
|
|
442
|
+
attempt(() => gizmo.dispose());
|
|
443
|
+
publishHandleHover(null);
|
|
444
|
+
drainHandleHoverPublications();
|
|
445
|
+
clearHandleHoverSubscribers();
|
|
446
|
+
attempt(() => overlayScene.clear());
|
|
447
|
+
attempt(() => capGeometry.dispose());
|
|
448
|
+
if (firstError) throw firstError;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return {
|
|
452
|
+
get isSupported() { return supported; },
|
|
453
|
+
get isEnabled() { return enabled; },
|
|
454
|
+
setSubpart,
|
|
455
|
+
updateGeometry,
|
|
456
|
+
setVisible,
|
|
457
|
+
setEnabled,
|
|
458
|
+
reset,
|
|
459
|
+
flip,
|
|
460
|
+
setTheme,
|
|
461
|
+
setViewportSize,
|
|
462
|
+
isPointVisible,
|
|
463
|
+
registerClippableMaterial,
|
|
464
|
+
updateForCamera,
|
|
465
|
+
renderOverlay,
|
|
466
|
+
onHandleHoverChange,
|
|
467
|
+
dispose,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Resolve a part's declared `fonts` ({ name: source }) to ArrayBuffers, before the
|
|
2
|
+
// synchronous build. A source is: an ArrayBuffer/Uint8Array (bytes), a URL string
|
|
3
|
+
// (fetched — a Vite `import('./x.ttf')` yields { default: url }), or a thunk
|
|
4
|
+
// returning any of those (possibly async). Memoized process-wide by source so
|
|
5
|
+
// repeated builds don't refetch. DOM-free (uses global fetch, present in workers).
|
|
6
|
+
const cache = new Map(); // source (string|object) → Promise<ArrayBuffer>
|
|
7
|
+
|
|
8
|
+
function toBuffer(v) {
|
|
9
|
+
if (v instanceof ArrayBuffer) return v;
|
|
10
|
+
// A view may not span its whole backing buffer — slice to its exact range (Node Buffer
|
|
11
|
+
// pooling makes byteOffset>0 common for small files; v.buffer alone would be garbage).
|
|
12
|
+
if (ArrayBuffer.isView(v)) return v.buffer.slice(v.byteOffset, v.byteOffset + v.byteLength);
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function resolveOne(source) {
|
|
17
|
+
if (cache.has(source)) return cache.get(source);
|
|
18
|
+
const p = (async () => {
|
|
19
|
+
let v = source;
|
|
20
|
+
if (typeof v === "function") v = await v();
|
|
21
|
+
if (v && typeof v === "object" && "default" in v && !toBuffer(v)) v = v.default; // dynamic-import module
|
|
22
|
+
const buf = toBuffer(v);
|
|
23
|
+
if (buf) return buf;
|
|
24
|
+
if (typeof v === "string") return await (await fetch(v)).arrayBuffer();
|
|
25
|
+
throw new Error("resolveFonts: a font source must be bytes, a URL string, or a thunk returning one");
|
|
26
|
+
})();
|
|
27
|
+
cache.set(source, p);
|
|
28
|
+
return p;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function resolveFonts(fontsDecl) {
|
|
32
|
+
const out = new Map();
|
|
33
|
+
if (!fontsDecl) return out;
|
|
34
|
+
await Promise.all(Object.entries(fontsDecl).map(async ([name, src]) => out.set(name, await resolveOne(src))));
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Resolve raw glyph outlines (self-intersecting / overlapping cubic contours) into
|
|
2
|
+
// simple, correctly-nested {outer,holes} curve regions under the requested font fill
|
|
3
|
+
// rule. Beziers are split where needed but never flattened.
|
|
4
|
+
//
|
|
5
|
+
// The required recipe is:
|
|
6
|
+
// 1. resolveCrossings() each contour individually;
|
|
7
|
+
// 2. CompoundPath of all the simple sub-paths;
|
|
8
|
+
// 3. set the font's nonzero/evenodd rule;
|
|
9
|
+
// 4. unite(self) to normalize overlaps and crossings into simple paths.
|
|
10
|
+
import paper from "paper/dist/paper-core.js";
|
|
11
|
+
|
|
12
|
+
// Never use paper's package-global project: another consumer in the same worker may import
|
|
13
|
+
// paper too. This resolver owns and clears only this private, headless scope.
|
|
14
|
+
const scope = new paper.PaperScope();
|
|
15
|
+
scope.setup(new scope.Size(1, 1));
|
|
16
|
+
|
|
17
|
+
function toPaperPath(contour) {
|
|
18
|
+
const path = new scope.Path({ insert: false });
|
|
19
|
+
path.moveTo(new scope.Point(contour.start[0], contour.start[1]));
|
|
20
|
+
for (const s of contour.segments) {
|
|
21
|
+
if (s.c1) path.cubicCurveTo(
|
|
22
|
+
new scope.Point(s.c1[0], s.c1[1]),
|
|
23
|
+
new scope.Point(s.c2[0], s.c2[1]),
|
|
24
|
+
new scope.Point(s.to[0], s.to[1]));
|
|
25
|
+
else path.lineTo(new scope.Point(s.to[0], s.to[1]));
|
|
26
|
+
}
|
|
27
|
+
path.closePath();
|
|
28
|
+
return path;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function toContour(path) {
|
|
32
|
+
const segs = path.segments;
|
|
33
|
+
const start = [segs[0].point.x, segs[0].point.y];
|
|
34
|
+
const out = { start, segments: [] };
|
|
35
|
+
for (let i = 0; i < segs.length; i++) {
|
|
36
|
+
const a = segs[i], b = segs[(i + 1) % segs.length];
|
|
37
|
+
const straight = a.handleOut.isZero() && b.handleIn.isZero();
|
|
38
|
+
const closing = i === segs.length - 1;
|
|
39
|
+
if (closing && straight) continue; // implicit straight close
|
|
40
|
+
const to = [b.point.x, b.point.y];
|
|
41
|
+
if (straight) out.segments.push({ to });
|
|
42
|
+
else out.segments.push({ to, c1: [a.point.x + a.handleOut.x, a.point.y + a.handleOut.y], c2: [b.point.x + b.handleIn.x, b.point.y + b.handleIn.y] });
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Group while paths are still Paper geometry. Path.area includes cubic handles and
|
|
48
|
+
// interiorPoint is guaranteed to lie inside the curve; never reduce curves to endpoint rings.
|
|
49
|
+
function groupPaperPaths(paths) {
|
|
50
|
+
const largest = paths.reduce((a, b) => Math.abs(b.area) > Math.abs(a.area) ? b : a);
|
|
51
|
+
const outerClockwise = largest.clockwise;
|
|
52
|
+
const outers = paths.filter((p) => p.clockwise === outerClockwise)
|
|
53
|
+
.map((path) => ({ path, holes: [] }));
|
|
54
|
+
for (const hole of paths.filter((p) => p.clockwise !== outerClockwise)) {
|
|
55
|
+
const home = outers.filter((o) => o.path.contains(hole.interiorPoint))
|
|
56
|
+
.sort((a, b) => Math.abs(a.path.area) - Math.abs(b.path.area))[0];
|
|
57
|
+
if (!home) throw new Error("curve-fill: resolved hole has no containing outer");
|
|
58
|
+
home.holes.push(hole);
|
|
59
|
+
}
|
|
60
|
+
return outers.map(({ path, holes }) => ({
|
|
61
|
+
outer: toContour(path),
|
|
62
|
+
holes: holes.map(toContour),
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function resolveCurveFill(contours, { fillRule = "nonzero" } = {}) {
|
|
67
|
+
if (fillRule !== "nonzero" && fillRule !== "evenodd")
|
|
68
|
+
throw new Error('curve-fill: fillRule must be "nonzero" or "evenodd"');
|
|
69
|
+
if (!contours || contours.length === 0) return [];
|
|
70
|
+
try {
|
|
71
|
+
const simple = [];
|
|
72
|
+
for (const ct of contours) {
|
|
73
|
+
const resolved = toPaperPath(ct).resolveCrossings();
|
|
74
|
+
const kids = resolved.className === "CompoundPath" ? resolved.children : [resolved];
|
|
75
|
+
for (const k of kids) if (k.segments && k.segments.length >= 2) simple.push(k.clone({ insert: false }));
|
|
76
|
+
}
|
|
77
|
+
if (simple.length === 0) return [];
|
|
78
|
+
const compound = new scope.CompoundPath({ children: simple, fillRule });
|
|
79
|
+
const united = compound.unite(compound, { insert: false });
|
|
80
|
+
const paths = (united.className === "CompoundPath" ? united.children : [united])
|
|
81
|
+
.filter((p) => p.segments && p.segments.length >= 2 && Math.abs(p.area) > 1e-9);
|
|
82
|
+
return paths.length ? groupPaperPaths(paths) : [];
|
|
83
|
+
} finally {
|
|
84
|
+
scope.project.clear();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Copyright 2011 The Roboto Project Authors (https://github.com/googlefonts/roboto-classic)
|
|
2
|
+
|
|
3
|
+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
|
4
|
+
This license is copied below, and is also available with a FAQ at:
|
|
5
|
+
https://openfontlicense.org
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
-----------------------------------------------------------
|
|
9
|
+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
10
|
+
-----------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
PREAMBLE
|
|
13
|
+
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
14
|
+
development of collaborative font projects, to support the font creation
|
|
15
|
+
efforts of academic and linguistic communities, and to provide a free and
|
|
16
|
+
open framework in which fonts may be shared and improved in partnership
|
|
17
|
+
with others.
|
|
18
|
+
|
|
19
|
+
The OFL allows the licensed fonts to be used, studied, modified and
|
|
20
|
+
redistributed freely as long as they are not sold by themselves. The
|
|
21
|
+
fonts, including any derivative works, can be bundled, embedded,
|
|
22
|
+
redistributed and/or sold with any software provided that any reserved
|
|
23
|
+
names are not used by derivative works. The fonts and derivatives,
|
|
24
|
+
however, cannot be released under any other type of license. The
|
|
25
|
+
requirement for fonts to remain under this license does not apply
|
|
26
|
+
to any document created using the fonts or their derivatives.
|
|
27
|
+
|
|
28
|
+
DEFINITIONS
|
|
29
|
+
"Font Software" refers to the set of files released by the Copyright
|
|
30
|
+
Holder(s) under this license and clearly marked as such. This may
|
|
31
|
+
include source files, build scripts and documentation.
|
|
32
|
+
|
|
33
|
+
"Reserved Font Name" refers to any names specified as such after the
|
|
34
|
+
copyright statement(s).
|
|
35
|
+
|
|
36
|
+
"Original Version" refers to the collection of Font Software components as
|
|
37
|
+
distributed by the Copyright Holder(s).
|
|
38
|
+
|
|
39
|
+
"Modified Version" refers to any derivative made by adding to, deleting,
|
|
40
|
+
or substituting -- in part or in whole -- any of the components of the
|
|
41
|
+
Original Version, by changing formats or by porting the Font Software to a
|
|
42
|
+
new environment.
|
|
43
|
+
|
|
44
|
+
"Author" refers to any designer, engineer, programmer, technical
|
|
45
|
+
writer or other person who contributed to the Font Software.
|
|
46
|
+
|
|
47
|
+
PERMISSION & CONDITIONS
|
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
49
|
+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
|
50
|
+
redistribute, and sell modified and unmodified copies of the Font
|
|
51
|
+
Software, subject to the following conditions:
|
|
52
|
+
|
|
53
|
+
1) Neither the Font Software nor any of its individual components,
|
|
54
|
+
in Original or Modified Versions, may be sold by itself.
|
|
55
|
+
|
|
56
|
+
2) Original or Modified Versions of the Font Software may be bundled,
|
|
57
|
+
redistributed and/or sold with any software, provided that each copy
|
|
58
|
+
contains the above copyright notice and this license. These can be
|
|
59
|
+
included either as stand-alone text files, human-readable headers or
|
|
60
|
+
in the appropriate machine-readable metadata fields within text or
|
|
61
|
+
binary files as long as those fields can be easily viewed by the user.
|
|
62
|
+
|
|
63
|
+
3) No Modified Version of the Font Software may use the Reserved Font
|
|
64
|
+
Name(s) unless explicit written permission is granted by the corresponding
|
|
65
|
+
Copyright Holder. This restriction only applies to the primary font name as
|
|
66
|
+
presented to the users.
|
|
67
|
+
|
|
68
|
+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
69
|
+
Software shall not be used to promote, endorse or advertise any
|
|
70
|
+
Modified Version, except to acknowledge the contribution(s) of the
|
|
71
|
+
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
72
|
+
permission.
|
|
73
|
+
|
|
74
|
+
5) The Font Software, modified or unmodified, in part or in whole,
|
|
75
|
+
must be distributed entirely under this license, and must not be
|
|
76
|
+
distributed under any other license. The requirement for fonts to
|
|
77
|
+
remain under this license does not apply to any document created
|
|
78
|
+
using the Font Software.
|
|
79
|
+
|
|
80
|
+
TERMINATION
|
|
81
|
+
This license becomes null and void if any of the above conditions are
|
|
82
|
+
not met.
|
|
83
|
+
|
|
84
|
+
DISCLAIMER
|
|
85
|
+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
86
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
87
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
88
|
+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
89
|
+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
90
|
+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
91
|
+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
92
|
+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
93
|
+
OTHER DEALINGS IN THE FONT SOFTWARE.
|
|
Binary file
|