@rangefoundry/svg-drawings 0.0.0 → 0.1.0

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.
Files changed (68) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +21 -1
  3. package/dist/bullet/density.d.ts +65 -0
  4. package/dist/bullet/density.js +91 -0
  5. package/dist/bullet/figure.d.ts +34 -0
  6. package/dist/bullet/figure.js +50 -0
  7. package/dist/bullet/index.d.ts +4 -0
  8. package/dist/bullet/index.js +5 -0
  9. package/dist/bullet/outline.d.ts +144 -0
  10. package/dist/bullet/outline.js +683 -0
  11. package/dist/bullet/profile.d.ts +76 -0
  12. package/dist/bullet/profile.js +257 -0
  13. package/dist/cartridge/callouts.d.ts +18 -0
  14. package/dist/cartridge/callouts.js +153 -0
  15. package/dist/cartridge/detail.d.ts +34 -0
  16. package/dist/cartridge/detail.js +99 -0
  17. package/dist/cartridge/figure.d.ts +59 -0
  18. package/dist/cartridge/figure.js +113 -0
  19. package/dist/cartridge/figures.d.ts +47 -0
  20. package/dist/cartridge/figures.js +54 -0
  21. package/dist/cartridge/focus.d.ts +3 -0
  22. package/dist/cartridge/focus.js +7 -0
  23. package/dist/cartridge/index.d.ts +8 -0
  24. package/dist/cartridge/index.js +8 -0
  25. package/dist/cartridge/profile.d.ts +46 -0
  26. package/dist/cartridge/profile.js +630 -0
  27. package/dist/cartridge/regions.d.ts +28 -0
  28. package/dist/cartridge/regions.js +50 -0
  29. package/dist/core/advances.json +106 -0
  30. package/dist/core/annotate.d.ts +11 -0
  31. package/dist/core/annotate.js +715 -0
  32. package/dist/core/cache.d.ts +4 -0
  33. package/dist/core/cache.js +16 -0
  34. package/dist/core/face.d.ts +56 -0
  35. package/dist/core/face.js +54 -0
  36. package/dist/core/fillet.d.ts +7 -0
  37. package/dist/core/fillet.js +42 -0
  38. package/dist/core/geometry.d.ts +22 -0
  39. package/dist/core/geometry.js +136 -0
  40. package/dist/core/layout.d.ts +46 -0
  41. package/dist/core/layout.js +69 -0
  42. package/dist/core/placer.d.ts +60 -0
  43. package/dist/core/placer.js +291 -0
  44. package/dist/core/section.d.ts +32 -0
  45. package/dist/core/section.js +75 -0
  46. package/dist/core/text.d.ts +19 -0
  47. package/dist/core/text.js +38 -0
  48. package/dist/core/types.d.ts +185 -0
  49. package/dist/core/types.js +2 -0
  50. package/dist/core/units.d.ts +18 -0
  51. package/dist/core/units.js +26 -0
  52. package/dist/core/vector.d.ts +8 -0
  53. package/dist/core/vector.js +23 -0
  54. package/dist/index.d.ts +9 -0
  55. package/dist/index.js +10 -0
  56. package/dist/seating/chamber.d.ts +21 -0
  57. package/dist/seating/chamber.js +23 -0
  58. package/dist/seating/index.d.ts +3 -0
  59. package/dist/seating/index.js +4 -0
  60. package/dist/seating/preview.d.ts +12 -0
  61. package/dist/seating/preview.js +30 -0
  62. package/dist/seating/seating.d.ts +210 -0
  63. package/dist/seating/seating.js +284 -0
  64. package/dist/target/face.d.ts +68 -0
  65. package/dist/target/face.js +50 -0
  66. package/dist/target/index.d.ts +1 -0
  67. package/dist/target/index.js +2 -0
  68. package/package.json +60 -4
@@ -0,0 +1,715 @@
1
+ import * as face from './face.js';
2
+ import { boxDistance } from './geometry.js';
3
+ import { createPlacer } from './placer.js';
4
+ import { radiusAt } from './section.js';
5
+ import { labelText, textWidth } from './text.js';
6
+ import { add, pairs, polar, unit } from './vector.js';
7
+ const arcSegs = (c, n = 12) => Array.from({ length: n }, (_, k) => ({
8
+ a: polar(c.c, c.r, c.from + ((c.to - c.from) * k) / n),
9
+ b: polar(c.c, c.r, c.from + ((c.to - c.from) * (k + 1)) / n),
10
+ kind: 'rule',
11
+ owner: c.owner,
12
+ }));
13
+ /**
14
+ * Lays out the dimensions of one side at text size `em` (mm) in the part frame: the length stack
15
+ * on +r, S and the diameter labels on -r, head lengths and head diameters behind the face. Each
16
+ * text takes the first of its places where it clears every other text and every line of another
17
+ * dimension, and its own lines and arrowheads clear every other text and arrowhead. Where that
18
+ * leaves a conflict, the layout without the corner rooms of the radii is taken if it has fewer.
19
+ * `horizontal` is the part-frame direction that reads level on the medium (angle labels).
20
+ */
21
+ export function annotate(part, em, horizontal, axis) {
22
+ const best = (frontOnAxis) => {
23
+ const kept = layOut(part, em, horizontal, axis, true, frontOnAxis);
24
+ if (kept.residual < 1)
25
+ return kept;
26
+ // keeping the diameters off the radius corners can push a text into a conflict elsewhere
27
+ const plain = layOut(part, em, horizontal, axis, false, frontOnAxis);
28
+ const better = plain.residual < kept.residual ? plain : kept;
29
+ if (better.residual < 1)
30
+ return better;
31
+ // an angle text can take the place a radius needs: the angles placed first
32
+ const angled = layOut(part, em, horizontal, axis, true, frontOnAxis, true);
33
+ return angled.residual < better.residual ? angled : better;
34
+ };
35
+ const radii = new Set(part.annotation.radii.map((r) => r.symbol));
36
+ const leaders = ({ layout }) => layout.segs.filter((s) => s.leader && radii.has(s.owner));
37
+ const crossing = (l) => leaders(l).filter((s) => s.a[1] * s.b[1] < 0).length;
38
+ const long = (l) => leaders(l).filter((s) => Math.hypot(s.b[0] - s.a[0], s.b[1] - s.a[1]) >
39
+ (3 * face.radiusLeaderLength * em) / face.baseEm).length;
40
+ const first = best(false);
41
+ if (!crossing(first) || !part.annotation.dimensions.some((d) => d.front))
42
+ return first.layout;
43
+ // a G1 text beside the mouth can leave a radius only the mirrored corner; on the axis past S it does not
44
+ const onAxis = best(true);
45
+ return crossing(onAxis) < crossing(first) &&
46
+ long(onAxis) <= long(first) &&
47
+ onAxis.residual <= first.residual
48
+ ? onAxis.layout
49
+ : first.layout;
50
+ }
51
+ /** What still runs into something, for the choice between the two layouts. */
52
+ function residualOf({ placements, commit, uncommit, textConflicts }) {
53
+ let residual = 0;
54
+ for (const p of placements) {
55
+ uncommit(p.chosen);
56
+ residual += textConflicts(p.chosen);
57
+ commit(p.chosen);
58
+ }
59
+ return residual;
60
+ }
61
+ /** The arcs and diameter tips of the chosen places. */
62
+ function collectArcs({ placements, arcs, across }) {
63
+ for (const { chosen } of placements) {
64
+ if (chosen.arc)
65
+ arcs.push(chosen.arc);
66
+ if (chosen.across)
67
+ across.push(chosen.across);
68
+ }
69
+ }
70
+ function layOut(part, em, horizontal, axis, keepCorners, frontOnAxis, anglesFirst = false) {
71
+ const placer = createPlacer(part, em, horizontal, axis, keepCorners);
72
+ const { labels, segs, arcs, tips, across, spans, commit, pick, place } = placer;
73
+ layoutDiameters(placer, layoutLanes(placer, frontOnAxis));
74
+ // angles first where radii first left a text in a conflict (`annotate`)
75
+ for (const pass of anglesFirst ? [layoutAngles, layoutRadii] : [layoutRadii, layoutAngles])
76
+ pass(placer);
77
+ // note flags and detail letters: on a leader from the feature, the shortest clear one first
78
+ for (const c of part.callouts ?? [])
79
+ if (!c.letter)
80
+ place(calloutCandidates(placer, c));
81
+ settle(placer);
82
+ collectArcs(placer);
83
+ const box = layoutBox(placer);
84
+ const residual = residualOf(placer);
85
+ // detail letters last, outside the box and the residual: they move nothing else
86
+ for (const c of part.callouts ?? [])
87
+ if (c.letter)
88
+ commit(pick(calloutCandidates(placer, c)));
89
+ const layout = {
90
+ labels,
91
+ segs: segs.filter((s) => s.owner !== 'axis'),
92
+ arcs,
93
+ arrowheads: tips,
94
+ box,
95
+ diameterTips: across,
96
+ spans,
97
+ };
98
+ return { layout, residual };
99
+ }
100
+ /** The dimensions by where they go: the length stack, head lengths, S, diameters across, below and in front. */
101
+ function splitDimensions(dims, zBreak) {
102
+ const span = (d) => Math.min(d.to, zBreak) - d.from;
103
+ const stack = dims
104
+ .filter((d) => d.kind !== 'diameter' && !d.behindHeadFace && !d.far)
105
+ .sort((a, b) => span(a) - span(b));
106
+ const heads = dims
107
+ .filter((d) => d.kind !== 'diameter' && d.behindHeadFace)
108
+ .sort((a, b) => span(a) - span(b));
109
+ const far = dims.filter((d) => d.kind !== 'diameter' && d.far);
110
+ const acrossDims = dims
111
+ .filter((d) => d.kind === 'diameter' && !d.behindHeadFace && !d.front)
112
+ .sort((a, b) => a.from - b.from);
113
+ const below = dims
114
+ .filter((d) => d.kind === 'diameter' && d.behindHeadFace)
115
+ .sort((a, b) => a.radius - b.radius);
116
+ const front = dims.filter((d) => d.kind === 'diameter' && d.front);
117
+ return { stack, heads, far, acrossDims, below, front };
118
+ }
119
+ /** Shortest first, each in the innermost lane where its span and its label (outside when it does not fit between the arrows) clear the others in that lane. */
120
+ function stackLanes({ lengthUnit, fixedDecimals, em, arrowLen, tailLen, zBreak, gap }, stack) {
121
+ const lanes = [];
122
+ const laneOf = new Map();
123
+ for (const d of stack) {
124
+ const [from, to] = [d.from, Math.min(d.to, zBreak)];
125
+ const w = textWidth(labelText(d, lengthUnit, fixedDecimals), em);
126
+ const outside = to - from < face.shortSpan * arrowLen;
127
+ const fits = w + 2 * arrowLen < to - from;
128
+ const reach = [
129
+ from - (outside ? tailLen + em : 0),
130
+ to + (outside ? tailLen + em : 0) + (fits ? 0 : gap + w + em),
131
+ ];
132
+ // spans may touch: a chain continues from the end of the length before it
133
+ let n = lanes.findIndex((lane) => lane.every(([a, b]) => reach[1] <= a || b <= reach[0]));
134
+ let lane = lanes[n];
135
+ if (!lane) {
136
+ lane = [];
137
+ n = lanes.push(lane) - 1;
138
+ }
139
+ lane.push(reach);
140
+ laneOf.set(d, n);
141
+ }
142
+ return { lanes, laneOf };
143
+ }
144
+ /** The lengths in their lanes (the stack on +r, S and the head lengths on -r), the head diameters behind the face, a diameter past the mouth, then the datum: every fixed line first, then their texts, so each text sees all of them; returns what the diameters across the part are placed past. */
145
+ function layoutLanes(placer, frontOnAxis) {
146
+ const { annotation, lengthUnit, fixedDecimals, em, lane0, pitch, rMax, zBreak, segs } = placer;
147
+ const dims = annotation.dimensions;
148
+ const { stack, heads, far, acrossDims, below, front } = splitDimensions(dims, zBreak);
149
+ const { lanes, laneOf } = stackLanes(placer, stack);
150
+ const stackBase = rMax + lane0;
151
+ const stackOuter = stackBase + Math.max(lanes.length - 1, 0) * pitch;
152
+ const diameterW = Math.max(0, ...acrossDims.map((d) => textWidth(labelText(d, lengthUnit, fixedDecimals), em)));
153
+ const farBase = rMax + lane0 + Math.max(heads.length * pitch, acrossDims.length ? diameterW + lane0 : 0);
154
+ const farOuter = farBase + Math.max(far.length - 1, 0) * pitch;
155
+ const pending = [];
156
+ // the length stack on +r, its labels inside the part side of the rules, at one row where they fit
157
+ const band = 0.7 *
158
+ Math.min(...stack.filter((d) => d.from === 0).map((d) => Math.min(d.to, zBreak)), Infinity);
159
+ for (const d of stack)
160
+ laneLength(placer, pending, d, stackBase + laneOf.get(d) * pitch, -1, Number.isFinite(band) ? band : undefined);
161
+ // S on -r beyond the head lanes and the diameter labels, its label on the part side
162
+ far.forEach((d, n) => {
163
+ laneLength(placer, pending, d, -(farBase + n * pitch), 1);
164
+ });
165
+ headLengths(placer, pending, heads);
166
+ headDiameters(placer, pending, below);
167
+ frontDiameters(placer, pending, front, frontOnAxis);
168
+ // the datum along the head face over every lane
169
+ const lanesOut = Math.max(heads.length ? rMax + lane0 + (heads.length - 1) * pitch : 0, far.length ? farOuter : 0);
170
+ if (dims.length)
171
+ segs.push({
172
+ a: [0, -(Math.max(rMax, lanesOut) + face.overrun)],
173
+ b: [0, (stack.length ? stackOuter : rMax) + face.overrun],
174
+ kind: 'thin',
175
+ owner: 'datum',
176
+ });
177
+ for (const f of pending)
178
+ f();
179
+ return { acrossDims, stackOuter, farOuter };
180
+ }
181
+ /** A length in a lane at signed distance y; the label on the side `labelSide` of its rule. */
182
+ function laneLength(placer, pending, d, y, labelSide, band) {
183
+ const { lengthUnit, fixedDecimals, em, arrowLen, tailLen, zBreak, gap, segs, spans, start, label, place, rule, broken, } = placer;
184
+ const owner = d.symbol;
185
+ const sign = Math.sign(y);
186
+ const isBroken = d.to > zBreak;
187
+ const [from, to] = [d.from, isBroken ? zBreak : d.to];
188
+ const outside = !isBroken && to - from < face.shortSpan * arrowLen;
189
+ spans.push({ owner, length: to - from, outside, broken: isBroken });
190
+ for (const z of isBroken ? [from] : [from, to])
191
+ if (z > 1e-9)
192
+ segs.push({ a: [z, sign * start(z)], b: [z, y + sign * face.overrun], kind: 'thin', owner });
193
+ if (isBroken)
194
+ broken([from, y], [to, y], owner);
195
+ else
196
+ rule([from, y], [to, y], owner, outside);
197
+ pending.push(() => {
198
+ const text = labelText(d, lengthUnit, fixedDecimals);
199
+ const w = textWidth(text, em);
200
+ const ly = y + labelSide * (0.5 * em + gap);
201
+ const ends = outside ? tailLen : 0;
202
+ const inside = w + 2 * arrowLen < to - from;
203
+ const within = (x) => x - w / 2 > from + arrowLen && x + w / 2 < to - arrowLen;
204
+ const xs = [
205
+ ...(inside
206
+ ? [
207
+ band !== undefined && within(band) ? band : (from + to) / 2,
208
+ ...Array.from({ length: 9 }, (_, n) => from + ((to - from) * (n + 1)) / 10).filter(within),
209
+ ]
210
+ : []),
211
+ to + ends + gap + w / 2,
212
+ from - ends - gap - w / 2,
213
+ ...Array.from({ length: 8 }, (_, n) => to + ends + gap + w / 2 + (n + 1) * em),
214
+ ];
215
+ place([ly, y - labelSide * (0.5 * em + gap)].flatMap((yy) => xs.map((x) => ({ l: label(text, [x, yy], [1, 0], owner) }))));
216
+ });
217
+ }
218
+ /** Head lengths on -r behind the face, texts before the face in line with the rule. */
219
+ function headLengths(placer, pending, heads) {
220
+ const { lengthUnit, fixedDecimals, em, lane0, pitch, arrowLen, tailLen, rMax, gap, segs, spans, start, label, place, rule, } = placer;
221
+ heads.forEach((d, n) => {
222
+ const owner = d.symbol;
223
+ const y = -(rMax + lane0 + n * pitch);
224
+ const outside = d.to - d.from < face.shortSpan * arrowLen;
225
+ spans.push({ owner, length: d.to - d.from, outside, broken: false });
226
+ for (const z of [d.from, d.to])
227
+ if (z > 1e-9)
228
+ segs.push({ a: [z, -start(z)], b: [z, y - face.overrun], kind: 'thin', owner });
229
+ rule([d.from, y], [d.to, y], owner, outside);
230
+ pending.push(() => {
231
+ const text = labelText(d, lengthUnit, fixedDecimals);
232
+ const w = textWidth(text, em);
233
+ const x = Math.min(d.from, d.to) - (outside ? tailLen : 0) - gap - w / 2;
234
+ place([0, -1, 1, -2, 2].flatMap((m) => [x, x - w - em].map((xx) => ({
235
+ l: label(text, [xx, y + m * (0.5 * em + gap)], [1, 0], owner),
236
+ }))));
237
+ });
238
+ });
239
+ }
240
+ /** Head diameters behind the face, smallest radius innermost; texts centered or beside. */
241
+ function headDiameters(placer, pending, below) {
242
+ const { points, lengthUnit, fixedDecimals, em, lane0, pitch, arrowLen, tailLen, rMax, gap, segs, label, place, rule, } = placer;
243
+ below.forEach((d, n) => {
244
+ const owner = d.symbol;
245
+ const r = d.radius;
246
+ const z = -(lane0 + n * pitch);
247
+ // a belt wider than the rim (or a rim behind a chamfer) is projected from where the outline first reaches it
248
+ const zFrom = reach(points, r);
249
+ for (const s of [1, -1])
250
+ segs.push({
251
+ a: [zFrom - face.extensionGap, s * r],
252
+ b: [z - face.overrun, s * r],
253
+ kind: 'thin',
254
+ owner,
255
+ });
256
+ rule([z, -r], [z, r], owner, 2 * r < face.shortSpan * arrowLen);
257
+ pending.push(() => {
258
+ const text = labelText(d, lengthUnit, fixedDecimals);
259
+ const w = textWidth(text, em);
260
+ const outer = Math.max(...below.map((b) => b.radius)) + tailLen + gap;
261
+ place([z - 0.5 * em - gap, z + 0.5 * em + gap].flatMap((lz) => [0, outer + w / 2, -(outer + w / 2), rMax + lane0 + w / 2, -(rMax + lane0 + w / 2)].map((y) => ({ l: label(text, [lz, y], [0, 1], owner) }))));
262
+ });
263
+ });
264
+ }
265
+ /** A diameter past the mouth (the cartridge G1), extension lines from the mouth face. */
266
+ function frontDiameters(placer, pending, front, frontOnAxis) {
267
+ const { lengthUnit, fixedDecimals, em, lane0, pitch, arrowLen, tailLen, rMax, zMax, gap, segs, label, place, rule, } = placer;
268
+ front.forEach((d, n) => {
269
+ const owner = d.symbol;
270
+ const r = d.radius;
271
+ const z = zMax + lane0 + n * pitch;
272
+ for (const s of [1, -1])
273
+ segs.push({
274
+ a: [zMax + face.extensionGap, s * r],
275
+ b: [z + face.overrun, s * r],
276
+ kind: 'thin',
277
+ owner,
278
+ });
279
+ rule([z, -r], [z, r], owner, 2 * r < face.shortSpan * arrowLen);
280
+ pending.push(() => {
281
+ const text = labelText(d, lengthUnit, fixedDecimals);
282
+ const w = textWidth(text, em);
283
+ const beside = r + tailLen + gap + w / 2;
284
+ const places = [z + 0.5 * em + gap, z - 0.5 * em - gap, z + pitch, z + 2 * pitch].flatMap((lz) => [0, beside, -beside, rMax + lane0 + w / 2, -(rMax + lane0 + w / 2)].map((y) => ({
285
+ l: label(text, [lz, y], [0, 1], owner),
286
+ })));
287
+ // frontOnAxis: every place on the axis before the places beside it
288
+ place(frontOnAxis
289
+ ? places.sort((p, q) => Number(p.l.c[1] !== 0) - Number(q.l.c[1] !== 0))
290
+ : places);
291
+ });
292
+ });
293
+ }
294
+ /** Diameters across the part, arrows touching the outline, the text at one end. */
295
+ function layoutDiameters(placer, { acrossDims, stackOuter, farOuter, }) {
296
+ const { points, annotation, lengthUnit, fixedDecimals, em, lane0, arrowLen, rMax, gap, place } = placer;
297
+ for (const d of acrossDims) {
298
+ const text = labelText(d, lengthUnit, fixedDecimals);
299
+ const w = textWidth(text, em);
300
+ const columns = [
301
+ [-1, rMax + lane0],
302
+ [1, stackOuter + lane0],
303
+ [-1, farOuter + lane0 + em],
304
+ [1, stackOuter + lane0 + w + gap],
305
+ [-1, farOuter + lane0 + 2 * em + w],
306
+ ];
307
+ // in a crowded column the label moves along the axis, its line kinked before it
308
+ const shifts = [0, 1, -1, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6].map((n) => n * 1.15 * em);
309
+ // at each station arrows from outside, then from inside pointing out where an outside one runs into another dimension or a radius corner's room (inside, the two arrowheads of a small diameter would meet)
310
+ const ways = d.radius > 1.5 * arrowLen ? [false, true] : [false];
311
+ const zs = stations(points, d, annotation);
312
+ place(columns.flatMap(([side, at]) => shifts.flatMap((shift) => zs.flatMap((z) => ways.map((inward) => acrossCandidate(placer, d, { text, w, side, at, shift, z, inward }))))));
313
+ }
314
+ }
315
+ /** One place of a diameter: its line to the text column, arrowheads on the outline at the station. */
316
+ function acrossCandidate({ points, lane0, tailLen, gap, label }, d, { text, w, side, at, shift, z, inward }) {
317
+ const owner = d.symbol;
318
+ // the tip on the outline; at a step, where the outline has two radii, the stored one
319
+ const outline = radiusAt(points, z);
320
+ const r = Math.abs(outline - d.radius) <= 0.05 ? outline : d.radius;
321
+ const knee = at - 0.6 * lane0;
322
+ const from = -side * (inward ? r : r + tailLen);
323
+ const line = shift
324
+ ? [
325
+ { a: [z, from], b: [z, side * knee], kind: 'rule', owner },
326
+ { a: [z, side * knee], b: [z + shift, side * at], kind: 'rule', owner },
327
+ ]
328
+ : [{ a: [z, from], b: [z, side * at], kind: 'rule', owner }];
329
+ const out = inward ? -1 : 1;
330
+ return {
331
+ across: {
332
+ owner,
333
+ tips: [
334
+ [z, r],
335
+ [z, -r],
336
+ ],
337
+ },
338
+ l: label(text, [z + shift, side * (at + gap + w / 2)], [0, 1], owner),
339
+ segs: line,
340
+ tips: [
341
+ { at: [z, r], dir: [0, -out], owner },
342
+ { at: [z, -r], dir: [0, out], owner },
343
+ ],
344
+ };
345
+ }
346
+ function radiusLeader({ em, horizontal, gap, leaderLength, label }, { corner, text, w, owner, how, sz, sr, side, scale }) {
347
+ const at = [corner[0], side * corner[1]];
348
+ const dir = [sz / Math.SQRT2, (side * sr) / Math.SQRT2];
349
+ const end = add(at, dir, leaderLength * scale);
350
+ // the level direction is an axis of the part frame, so it meets the leader at 45 deg
351
+ const toward = Math.sign(dir[0] * horizontal[0] + dir[1] * horizontal[1]);
352
+ const c = how === 'along'
353
+ ? add(end, dir, gap + w / 2)
354
+ : how === 'level'
355
+ ? add(end, dir, gap + w / 2 / Math.SQRT2 + em / 2)
356
+ : add(end, horizontal, toward * (gap + w / 2));
357
+ // a level text's box: the leader runs on to a gap before the box edge it meets at 45 deg
358
+ const b = how === 'level' ? add(c, dir, -(gap + Math.min(w, em) / Math.SQRT2)) : end;
359
+ return {
360
+ l: label(text, c, how === 'along' ? dir : horizontal, owner),
361
+ segs: [{ a: at, b, kind: 'thin', owner, leader: true }],
362
+ tips: [{ at, dir: [-dir[0], -dir[1]], owner }],
363
+ };
364
+ }
365
+ /** Fillet radii: a 45 degree leader to the unrounded corner, the text along it. */
366
+ function layoutRadii(placer) {
367
+ const { part, points, annotation, lengthUnit, fixedDecimals, em, gap, label, place } = placer;
368
+ for (const c of annotation.radii) {
369
+ const owner = c.symbol;
370
+ const corner = [c.corner[1], c.corner[0]];
371
+ const text = labelText({ kind: 'radius', symbol: c.symbol, value: c.value }, lengthUnit, fixedDecimals);
372
+ const w = textWidth(text, em);
373
+ const options = [
374
+ [-1, -1],
375
+ [1, -1],
376
+ [-1, 1],
377
+ [1, 1],
378
+ ];
379
+ // a chamber radius may also sit in the steel band beside its corner, level along the axis
380
+ const band = part.outer === undefined
381
+ ? []
382
+ : [1, -1].flatMap((side) => [1, -1, 1.5, -1.5, 2, -2, 3, -3, 4, -4, 5, -5, 6, -6].flatMap((n) => {
383
+ const z = corner[0] + n * (w / 2 + em);
384
+ const bore = radiusAt(points, z);
385
+ if (bore === undefined || part.outer - bore < em + gap)
386
+ return [];
387
+ const c = [z, (side * (bore + part.outer)) / 2];
388
+ const near = [z - Math.sign(n) * (w / 2 + gap), c[1]];
389
+ return [
390
+ {
391
+ l: label(text, c, [1, 0], owner),
392
+ segs: [
393
+ {
394
+ a: [corner[0], side * corner[1]],
395
+ b: near,
396
+ kind: 'thin',
397
+ owner,
398
+ leader: true,
399
+ },
400
+ ],
401
+ tips: [
402
+ {
403
+ at: [corner[0], side * corner[1]],
404
+ dir: unit(near, [corner[0], side * corner[1]]),
405
+ owner,
406
+ },
407
+ ],
408
+ },
409
+ ];
410
+ }));
411
+ // the label along the leader, level on the medium past its end, or level beside its end
412
+ const leaders = (scale) => ['along', 'level', 'beside'].flatMap((how) => options.flatMap(([sz, sr]) => [1, -1].map((side) => radiusLeader(placer, { corner, text, w, owner, how, sz, sr, side, scale }))));
413
+ // the face's leader, the steel band beside the corner, a shorter leader, then longer ones
414
+ place([...leaders(1), ...band, ...[0.6, 1.6, 2.4, 3.4, 4.6, 6, 8].flatMap(leaders)]);
415
+ }
416
+ }
417
+ function ledCandidate({ em, horizontal, gap, label }, { text, w, owner, v, radius, far, along, onArc, lines, arcTips, arc }) {
418
+ const [dx, dy] = horizontal;
419
+ const start = polar(v, radius, onArc);
420
+ const end = along ? [far[0], start[1]] : far;
421
+ const u = unit(along ? v : start, far);
422
+ // the text box's edge, where the leader's line meets it, a gap past the leader end
423
+ const reach = Math.min(w / 2 / Math.max(Math.abs(u[0] * dx + u[1] * dy), 1e-9), em / 2 / Math.max(Math.abs(u[0] * dy - u[1] * dx), 1e-9));
424
+ const c = add(end, u, gap + reach);
425
+ // a text across the axis stays centered on it where the leader still meets the text
426
+ const across = (Math.abs(dy) * w) / 2 + (Math.abs(dx) * em) / 2;
427
+ if (along && Math.abs(start[1]) <= across - em / 4)
428
+ c[1] = 0;
429
+ const leader = { a: start, b: end, kind: 'thin', owner, leader: true };
430
+ return {
431
+ l: label(text, c, horizontal, owner),
432
+ segs: [...lines, leader],
433
+ tips: arcTips,
434
+ arc,
435
+ };
436
+ }
437
+ const tangent = (t, s) => [-Math.sin(t) * s, Math.cos(t) * s];
438
+ /** The arcs an angle may take, each with the thin lines that go with it. */
439
+ function angleShapes({ points, em, k, lane0, rMax, zMax, gap }, a, onAxis, { v, f0, t0, t1 }) {
440
+ const owner = a.symbol;
441
+ if (a.symbol === 'alpha' && onAxis) {
442
+ // the shoulder: an arc behind L1 at 1.6 times the vertex distance, at most so far that its flanks end before the texts of lane 0 and its middle stays 0.35 zMax off the head; the flanks run from the corners at L1 to just past the arc
443
+ const reachOut = (rMax + lane0 - em - 2 * gap) / Math.max(Math.abs(Math.sin(t0)), 1e-6) - 0.5 * k;
444
+ const radius = Math.max(Math.min(face.alphaArc * (v[0] - f0[0]), v[0] - 0.35 * zMax, reachOut), 2 * k);
445
+ const corner = Math.hypot(f0[0] - v[0], f0[1] - v[1]);
446
+ return [
447
+ {
448
+ radius,
449
+ thin: [t0, t1].map((t) => ({
450
+ a: polar(v, Math.min(corner, radius), t),
451
+ b: polar(v, radius + 0.5 * k, t),
452
+ kind: 'thin',
453
+ owner,
454
+ })),
455
+ },
456
+ ];
457
+ }
458
+ if (onAxis) {
459
+ // a cone on the axis (alpha1, table VII): the arc across the middle of the drawn cone
460
+ const cone = coneMiddle(points, f0, v);
461
+ return [{ radius: Math.hypot(cone[0] - v[0], cone[1] - v[1]), thin: [] }];
462
+ }
463
+ // a short arc, the slanted flank drawn on
464
+ const t = Math.abs(Math.sin(t0)) > Math.abs(Math.sin(t1)) ? t0 : t1;
465
+ return [1, 1.5, 0.7].map((scale) => ({
466
+ radius: face.smallArc * k * scale,
467
+ thin: [{ a: v, b: polar(v, face.smallFlank * k * scale, t), kind: 'thin', owner }],
468
+ }));
469
+ }
470
+ /** An arc of an angle with its lines and arrowheads: the arrows inside when the arc is long enough. */
471
+ function angleVariant({ arrowLen, tailLen }, owner, { v, t0, t1 }, { radius, thin }) {
472
+ const length = Math.abs(t1 - t0) * radius;
473
+ const short = length < face.shortSpan * arrowLen;
474
+ const sweep = Math.sign(t1 - t0) || 1;
475
+ const arcTips = short
476
+ ? [
477
+ { at: polar(v, radius, t0), dir: tangent(t0, sweep), owner },
478
+ { at: polar(v, radius, t1), dir: tangent(t1, -sweep), owner },
479
+ ]
480
+ : [
481
+ { at: polar(v, radius, t0), dir: tangent(t0, -sweep), owner },
482
+ { at: polar(v, radius, t1), dir: tangent(t1, sweep), owner },
483
+ ];
484
+ const arc = short
485
+ ? {
486
+ c: v,
487
+ r: radius,
488
+ from: t0 - (sweep * tailLen) / radius,
489
+ to: t1 + (sweep * tailLen) / radius,
490
+ owner,
491
+ }
492
+ : { c: v, r: radius, from: t0, to: t1, owner };
493
+ return { v, t0, t1, sweep, radius, arc, lines: [...thin, ...arcSegs(arc)], arcTips };
494
+ }
495
+ /** The arcs of an angle on one side of the axis. */
496
+ function angleVariants(placer, a, onAxis, side) {
497
+ const v = [a.vertex[1], side * a.vertex[0]];
498
+ const flank = ([r, z]) => [z, side * r];
499
+ const [f0, f1] = [flank(a.flanks[0]), flank(a.flanks[1])];
500
+ let t0 = Math.atan2(f0[1] - v[1], f0[0] - v[0]);
501
+ let t1 = Math.atan2(f1[1] - v[1], f1[0] - v[0]);
502
+ if (t1 - t0 > Math.PI)
503
+ t1 -= 2 * Math.PI;
504
+ if (t0 - t1 > Math.PI)
505
+ t0 -= 2 * Math.PI;
506
+ const frame = { v, f0, t0, t1 };
507
+ return angleShapes(placer, a, onAxis, frame).map((shape) => angleVariant(placer, a.symbol, frame, shape));
508
+ }
509
+ /** Angles about their vertex: an arc between the flanks, the text near it or on a leader. */
510
+ function layoutAngles(placer) {
511
+ const { annotation, em, horizontal, rMax, gap, label, place } = placer;
512
+ // alpha1 first: fewer places are free near its arc in the narrow mouth than near the shoulder's
513
+ const angles = [...annotation.angles].sort((p, q) => Number(q.symbol === 'alpha1') - Number(p.symbol === 'alpha1'));
514
+ for (const a of angles) {
515
+ const owner = a.symbol;
516
+ const onAxis = Math.abs(a.vertex[0]) < 1e-9;
517
+ const text = labelText(a);
518
+ const w = textWidth(text, em);
519
+ // an angle at the outline (delta, i) may go on either side; one on the axis is symmetric
520
+ const variants = (onAxis ? [1] : [1, -1]).flatMap((side) => angleVariants(placer, a, onAxis, side));
521
+ const candidates = variants.flatMap(({ v, t0, t1, sweep, radius, arc, lines, arcTips }) => {
522
+ const mid = (t0 + t1) / 2;
523
+ const ring = Array.from({ length: 25 }, (_, n) => polar(v, radius, arc.from + ((arc.to - arc.from) * n) / 24));
524
+ const direct = [
525
+ radius - 1.1 * em - w / 2,
526
+ radius + 0.9 * em + w / 2,
527
+ radius + 2 * em + w,
528
+ radius - 2.2 * em - w,
529
+ radius + 3.5 * em + w,
530
+ ].flatMap((dist) => [mid, t0 - sweep * 0.3, t1 + sweep * 0.3, t0 - sweep * 0.7, t1 + sweep * 0.7].map((t) => ({
531
+ l: label(text, polar(v, Math.max(dist, w / 2 + em), t), horizontal, owner),
532
+ segs: lines,
533
+ tips: arcTips,
534
+ arc,
535
+ })));
536
+ // all around the arc, a gap or more off it on either side
537
+ const [hx, hy] = horizontal;
538
+ const around = Array.from({ length: 24 }, (_, j) => (j * Math.PI) / 12).flatMap((t) => {
539
+ const u = [Math.cos(t), Math.sin(t)];
540
+ // from the text's center to its box edge along u
541
+ const half = Math.min(w / 2 / Math.max(Math.abs(u[0] * hx + u[1] * hy), 1e-9), em / 2 / Math.max(Math.abs(u[0] * hy - u[1] * hx), 1e-9));
542
+ return [radius + gap + half, radius + 0.5 * em + half, radius - gap - half]
543
+ .filter((dist) => dist > 0)
544
+ .map((dist) => ({
545
+ l: label(text, polar(v, dist, t), horizontal, owner),
546
+ segs: lines,
547
+ tips: arcTips,
548
+ arc,
549
+ }));
550
+ });
551
+ // where no place near the arc is free: further out, on a leader from the arc
552
+ const led = [1.5, 2.5, 4, 6, 9, 13, 18].flatMap((n) => Array.from({ length: 21 }, (_, j) => (j % 2 ? 1 : -1) * Math.ceil(j / 2)).flatMap((m) => {
553
+ const t = mid + m * 0.15;
554
+ const far = polar(v, radius + n * em, t);
555
+ // a leader along the axis would vanish in the dash-dot line: from an arc on the axis it starts on the arc toward either end and runs parallel to the axis
556
+ const along = onAxis && Math.abs(Math.sin(t)) < 0.1;
557
+ const from = along
558
+ ? [t0 + 0.15 * (t1 - t0), t1 - 0.15 * (t1 - t0)]
559
+ : [Math.min(Math.max(t, Math.min(t0, t1)), Math.max(t0, t1))];
560
+ return from.map((onArc) => ledCandidate(placer, {
561
+ text,
562
+ w,
563
+ owner,
564
+ v,
565
+ radius,
566
+ far,
567
+ along,
568
+ onArc,
569
+ lines,
570
+ arcTips,
571
+ arc,
572
+ }));
573
+ }));
574
+ // within the part the nearest to the arc first, past it the nearest to the axis, as a text far out widens the drawing; a text without a leader stays within 2 em of its arc, so it reads as that arc's
575
+ const out = ({ l }) => {
576
+ const reach = Math.abs(l.c[1]) + Math.abs(l.dir[1] * l.w) / 2;
577
+ if (reach > rMax)
578
+ return 1e6 + reach;
579
+ return Math.min(...ring.map(([x, y]) => Math.hypot(x - l.c[0], y - l.c[1])));
580
+ };
581
+ const near = ({ l }) => Math.min(...ring.map((q) => boxDistance(l, q))) <= face.nearArc * em;
582
+ // the keys once per candidate, not per comparison; the stable sort keeps the order
583
+ const keyed = [...direct.slice(2).filter(near), ...around.filter(near), ...led].map((c) => ({
584
+ c,
585
+ key: out(c),
586
+ }));
587
+ return [
588
+ ...direct.slice(0, 2).filter(near),
589
+ ...keyed.sort((p, q) => p.key - q.key).map(({ c }) => c),
590
+ ];
591
+ });
592
+ place(candidates);
593
+ }
594
+ }
595
+ /** The places of a note flag or a detail letter on a leader from its feature, shortest first. */
596
+ function calloutCandidates(placer, c) {
597
+ const { em, horizontal, gap, leaderLength } = placer;
598
+ const { owner, text, letter } = c;
599
+ const w = letter ? textWidth(text, em) : Math.max(1.3 * em, textWidth(text, em) + 0.6 * em);
600
+ const h = letter ? em : w;
601
+ const [dx, dy] = horizontal;
602
+ return [0.3, 0.5, 0.8, 1.2, 1.8, 2.6, 3.6, 5, 7].flatMap((n) => [1, -1].flatMap((side) => Array.from({ length: 8 }, (_, j) => {
603
+ const at = [c.at[1], side * c.at[0]];
604
+ const u = [Math.cos((j * Math.PI) / 4), Math.sin((j * Math.PI) / 4)];
605
+ const end = add(at, u, n * leaderLength);
606
+ const reach = Math.min(w / 2 / Math.max(Math.abs(u[0] * dx + u[1] * dy), 1e-9), h / 2 / Math.max(Math.abs(u[0] * dy - u[1] * dx), 1e-9));
607
+ return {
608
+ l: {
609
+ text,
610
+ c: add(end, u, gap + reach),
611
+ dir: horizontal,
612
+ w,
613
+ h,
614
+ owner,
615
+ ...(letter ? {} : { ring: true }),
616
+ },
617
+ segs: [{ a: at, b: end, kind: 'thin', owner, leader: true }],
618
+ };
619
+ })));
620
+ }
621
+ /** A text that still runs into something tries its places again, now that all are set. */
622
+ function settle(placer) {
623
+ const { placements, conflicts, pick, commit, uncommit } = placer;
624
+ for (let pass = 0; pass < 6; pass++) {
625
+ let moved = false;
626
+ for (const p of placements) {
627
+ uncommit(p.chosen);
628
+ if (conflicts(p.chosen) > 0) {
629
+ const next = pick(p.candidates);
630
+ moved ||= next !== p.chosen;
631
+ p.chosen = next;
632
+ }
633
+ commit(p.chosen);
634
+ }
635
+ if (!moved)
636
+ break;
637
+ }
638
+ }
639
+ /** The box around every line, arc and text placed and the part: [x0, y0, x1, y1]. */
640
+ function layoutBox(placer) {
641
+ const { points, rMax, labels, segs, arcs } = placer;
642
+ const xs = [];
643
+ const ys = [];
644
+ const grow = ([x, y]) => {
645
+ xs.push(x);
646
+ ys.push(y);
647
+ };
648
+ for (const s of segs)
649
+ if (s.owner !== 'axis')
650
+ [s.a, s.b].forEach(grow);
651
+ for (const a of arcs)
652
+ for (const s of arcSegs(a))
653
+ [s.a, s.b].forEach(grow);
654
+ const signs = [
655
+ [1, 1],
656
+ [1, -1],
657
+ [-1, 1],
658
+ [-1, -1],
659
+ ];
660
+ for (const l of labels) {
661
+ const [ux, uy] = l.dir;
662
+ for (const [s, t] of signs)
663
+ grow([
664
+ l.c[0] + (s * l.w * ux) / 2 - (t * l.h * uy) / 2,
665
+ l.c[1] + (s * l.w * uy) / 2 + (t * l.h * ux) / 2,
666
+ ]);
667
+ }
668
+ for (const [r, z] of points) {
669
+ grow([z, rMax]);
670
+ grow([z, -rMax]);
671
+ grow([z, r]);
672
+ }
673
+ return [Math.min(...xs), Math.min(...ys), Math.max(...xs), Math.max(...ys)];
674
+ }
675
+ /** Nearest first: its station, then up to 3 mm along its own segment (`toward`) where the outline stays within 0.01 mm of the stored radius, off a step and off a convention. */
676
+ function stations(points, d, annotation) {
677
+ const [z, r, s] = [d.from, d.radius, d.toward];
678
+ const zEnd = Math.max(...points.map(([, pz]) => pz));
679
+ const onConvention = (q) => annotation.conventions.some((c) => q > c.from - 1e-9 && q <= c.to);
680
+ const found = [z];
681
+ if (s === undefined)
682
+ return found;
683
+ for (const step of [0.3, 0.6, 1, 1.5, 2, 3]) {
684
+ const q = z + s * step;
685
+ if (q <= 0 || q >= zEnd || onConvention(q))
686
+ continue;
687
+ const at = radiusAt(points, q);
688
+ // no outline point between z and q, so the move stays on one straight run
689
+ const between = points.some(([, pz]) => (pz - z) * s > 1e-9 && (pz - q) * s < 1e-9);
690
+ if (at !== undefined && Math.abs(at - r) <= 0.01 && !between)
691
+ found.push(q);
692
+ }
693
+ // off a corner first: the station is often where the outline turns
694
+ return found.length > 1 ? [...found.slice(1, 3), z, ...found.slice(3)] : found;
695
+ }
696
+ /** z where the half profile first reaches radius r (within 0.01 mm at a point), across a slope. */
697
+ function reach(points, r) {
698
+ for (const [[r0, z0], [r1, z1]] of pairs(points)) {
699
+ if (r1 >= r)
700
+ return z0 + ((r - r0) / (r1 - r0)) * (z1 - z0);
701
+ if (r1 >= r - 0.01)
702
+ return z1;
703
+ }
704
+ return 0;
705
+ }
706
+ /** The middle of the drawn cone segment from flank point f toward the vertex v. */
707
+ function coneMiddle(points, f, v) {
708
+ const r = Math.abs(f[1]);
709
+ const k = points.findIndex(([pr, pz]) => Math.abs(pr - r) < 1e-9 && Math.abs(pz - f[0]) < 1e-9);
710
+ const next = k >= 0 ? points[k + 1] : undefined;
711
+ if (!next)
712
+ return [(f[0] + v[0]) / 2, f[1] / 2];
713
+ return [(f[0] + next[1]) / 2, (Math.sign(f[1]) * (r + next[0])) / 2];
714
+ }
715
+ //# sourceMappingURL=annotate.js.map