@bitbybit-dev/base 1.7.0 → 1.8.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 (91) hide show
  1. package/NOTICE +24 -0
  2. package/README.md +3 -1
  3. package/lib/api/GlobalCDNProvider.js +1 -1
  4. package/lib/api/inputs/base-inputs.d.ts +20 -0
  5. package/lib/api/inputs/base-inputs.js +22 -0
  6. package/lib/api/inputs/io-inputs.d.ts +408 -8
  7. package/lib/api/inputs/io-inputs.js +344 -7
  8. package/lib/api/inputs/mesh-inputs.d.ts +20 -1
  9. package/lib/api/inputs/mesh-inputs.js +23 -0
  10. package/lib/api/inputs/recipes-inputs.d.ts +13 -5
  11. package/lib/api/inputs/recipes-inputs.js +10 -5
  12. package/lib/api/kernel-calls/index.d.ts +1 -0
  13. package/lib/api/kernel-calls/index.js +1 -0
  14. package/lib/api/kernel-calls/resolve-dto.d.ts +3 -1
  15. package/lib/api/kernel-calls/resolve-dto.js +7 -2
  16. package/lib/api/kernel-calls/settings.d.ts +19 -0
  17. package/lib/api/kernel-calls/up-axis-aliases.d.ts +25 -0
  18. package/lib/api/kernel-calls/up-axis-aliases.js +70 -0
  19. package/lib/api/models/simplex.d.ts +2 -0
  20. package/lib/api/models/simplex.js +10 -1
  21. package/lib/api/resolved-inputs/index.d.ts +11 -2
  22. package/lib/api/services/helpers/curves/conic-arcs.d.ts +3 -1
  23. package/lib/api/services/helpers/curves/conic-arcs.js +4 -2
  24. package/lib/api/services/helpers/files/drawing/drawing-budget.js +16 -6
  25. package/lib/api/services/helpers/files/drawing/drawing-dimension.constants.d.ts +14 -0
  26. package/lib/api/services/helpers/files/drawing/drawing-dimension.constants.js +14 -0
  27. package/lib/api/services/helpers/files/drawing/drawing-dimension.d.ts +4 -0
  28. package/lib/api/services/helpers/files/drawing/drawing-dimension.js +271 -0
  29. package/lib/api/services/helpers/files/drawing/drawing-geometry.d.ts +3 -0
  30. package/lib/api/services/helpers/files/drawing/drawing-geometry.js +28 -11
  31. package/lib/api/services/helpers/files/drawing/drawing-hatch.d.ts +10 -0
  32. package/lib/api/services/helpers/files/drawing/drawing-hatch.js +123 -0
  33. package/lib/api/services/helpers/files/drawing/drawing-loops.d.ts +3 -0
  34. package/lib/api/services/helpers/files/drawing/drawing-loops.js +91 -0
  35. package/lib/api/services/helpers/files/drawing/drawing-model.d.ts +6 -1
  36. package/lib/api/services/helpers/files/drawing/drawing-model.js +97 -13
  37. package/lib/api/services/helpers/files/drawing/drawing-scale.d.ts +2 -0
  38. package/lib/api/services/helpers/files/drawing/drawing-scale.js +47 -0
  39. package/lib/api/services/helpers/files/drawing/drawing-text.d.ts +9 -0
  40. package/lib/api/services/helpers/files/drawing/drawing-text.js +25 -0
  41. package/lib/api/services/helpers/files/drawing/drawing-types.d.ts +57 -0
  42. package/lib/api/services/helpers/files/drawing/drawing.constants.d.ts +13 -0
  43. package/lib/api/services/helpers/files/drawing/drawing.constants.js +17 -0
  44. package/lib/api/services/helpers/files/dxf/dxf-annotations.d.ts +5 -0
  45. package/lib/api/services/helpers/files/dxf/dxf-annotations.js +96 -0
  46. package/lib/api/services/helpers/files/dxf/dxf-entities.d.ts +5 -1
  47. package/lib/api/services/helpers/files/dxf/dxf-entities.js +5 -2
  48. package/lib/api/services/helpers/files/dxf/dxf-generator.js +26 -10
  49. package/lib/api/services/helpers/files/dxf/dxf-reader-annotations.d.ts +9 -0
  50. package/lib/api/services/helpers/files/dxf/dxf-reader-annotations.js +312 -0
  51. package/lib/api/services/helpers/files/dxf/dxf-reader-dtos.js +37 -3
  52. package/lib/api/services/helpers/files/dxf/dxf-reader-segments.d.ts +9 -0
  53. package/lib/api/services/helpers/files/dxf/dxf-reader-segments.js +151 -0
  54. package/lib/api/services/helpers/files/dxf/dxf-reader-types.d.ts +27 -0
  55. package/lib/api/services/helpers/files/dxf/dxf-reader.constants.d.ts +63 -0
  56. package/lib/api/services/helpers/files/dxf/dxf-reader.constants.js +17 -0
  57. package/lib/api/services/helpers/files/dxf/dxf-reader.d.ts +1 -1
  58. package/lib/api/services/helpers/files/dxf/dxf-reader.js +71 -181
  59. package/lib/api/services/helpers/files/dxf/dxf-tables.d.ts +2 -0
  60. package/lib/api/services/helpers/files/dxf/dxf-tables.js +5 -4
  61. package/lib/api/services/helpers/files/dxf/dxf-tokens.d.ts +2 -0
  62. package/lib/api/services/helpers/files/dxf/dxf-tokens.js +25 -1
  63. package/lib/api/services/helpers/files/dxf/dxf.constants.d.ts +11 -0
  64. package/lib/api/services/helpers/files/dxf/dxf.constants.js +13 -0
  65. package/lib/api/services/helpers/files/svg/svg-strokes.d.ts +1 -1
  66. package/lib/api/services/helpers/files/svg/svg-strokes.js +47 -23
  67. package/lib/api/services/helpers/files/svg/svg-writer.d.ts +2 -0
  68. package/lib/api/services/helpers/files/svg/svg-writer.js +100 -23
  69. package/lib/api/services/helpers/files/svg/svg.constants.d.ts +2 -1
  70. package/lib/api/services/helpers/files/svg/svg.constants.js +2 -1
  71. package/lib/api/services/helpers/geometry/loop-fill.d.ts +2 -0
  72. package/lib/api/services/helpers/geometry/loop-fill.js +37 -0
  73. package/lib/api/services/helpers/helper-types.d.ts +33 -0
  74. package/lib/api/services/helpers/text/stroke-font.d.ts +6 -0
  75. package/lib/api/services/helpers/text/stroke-font.js +177 -0
  76. package/lib/api/services/helpers/text/stroke-letters.constants.d.ts +2 -0
  77. package/lib/api/services/helpers/text/stroke-letters.constants.js +18 -0
  78. package/lib/api/services/helpers/text/stroke-marks.constants.d.ts +14 -0
  79. package/lib/api/services/helpers/text/stroke-marks.constants.js +34 -0
  80. package/lib/api/services/io/drawing.d.ts +198 -0
  81. package/lib/api/services/io/drawing.js +242 -0
  82. package/lib/api/services/io/dxf.d.ts +3 -3
  83. package/lib/api/services/io/dxf.js +4 -4
  84. package/lib/api/services/io/svg.d.ts +13 -11
  85. package/lib/api/services/io/svg.js +15 -12
  86. package/lib/api/services/mesh.d.ts +19 -0
  87. package/lib/api/services/mesh.js +40 -0
  88. package/lib/api/services/recipes.js +3 -2
  89. package/lib/api/services/text.d.ts +3 -3
  90. package/lib/api/services/text.js +7 -25
  91. package/package.json +1 -1
@@ -1,6 +1,7 @@
1
1
  import { LINE_TYPE_PATTERNS } from "../drawing/drawing.constants";
2
2
  import { nearestAciIndex, trueColorOf } from "./dxf-colors";
3
- import { DXF_GRID_SPACING_CODES, DXF_IMPERIAL_MEASUREMENT, DXF_INSUNITS, DXF_LENS_LENGTH, DXF_METRIC_MEASUREMENT, DXF_OTHER_POINT_CODES, DXF_POINT_CODES, DXF_SNAP_BASE_CODES, DXF_SNAP_SPACING_CODES, DXF_VIEW_ASPECT, DXF_VIEW_CENTER_CODES, DXF_VIEW_DIRECTION_CODES, DXF_VIEW_MARGIN, DXF_VIEW_TARGET_CODES, } from "./dxf.constants";
3
+ import { DXF_GRID_SPACING_CODES, DXF_IMPERIAL_MEASUREMENT, DXF_INSUNITS, DXF_LENS_LENGTH, DXF_METRIC_MEASUREMENT, DXF_OTHER_POINT_CODES, DXF_POINT_CODES, DXF_SNAP_BASE_CODES, DXF_SNAP_SPACING_CODES, DXF_TEXT_STYLE, DXF_VIEW_ASPECT, DXF_VIEW_CENTER_CODES, DXF_VIEW_DIRECTION_CODES, DXF_VIEW_MARGIN, DXF_VIEW_TARGET_CODES, } from "./dxf.constants";
4
+ export const dxfMeasurementOf = (unit) => unit === "in" ? "imperial" : "metric";
4
5
  const ORIGIN = [0, 0, 0];
5
6
  const isModern = (layout) => layout.version === "AC1015";
6
7
  const variable = (text, name) => text.pair("9", name);
@@ -26,7 +27,7 @@ export const writeHeader = (text, layout) => {
26
27
  variable(text, "$INSUNITS");
27
28
  text.pair("70", DXF_INSUNITS[layout.unit]);
28
29
  variable(text, "$MEASUREMENT");
29
- text.pair("70", layout.unit === "in" ? DXF_IMPERIAL_MEASUREMENT : DXF_METRIC_MEASUREMENT);
30
+ text.pair("70", dxfMeasurementOf(layout.unit) === "imperial" ? DXF_IMPERIAL_MEASUREMENT : DXF_METRIC_MEASUREMENT);
30
31
  }
31
32
  variable(text, "$HANDSEED");
32
33
  handseed = text.reserve("5");
@@ -143,7 +144,7 @@ const writeLayers = (text, layout) => {
143
144
  const writeStyles = (text, layout) => {
144
145
  const table = openTable(text, layout, "STYLE", 1);
145
146
  openRecord(text, layout, "STYLE", table, "AcDbTextStyleTableRecord");
146
- for (const [code, value] of [["2", "Standard"], ["70", "0"], ["40", "0.0"], ["41", "1.0"], ["50", "0.0"], ["71", "0"], ["42", "2.5"], ["3", "txt"], ["4", ""]]) {
147
+ for (const [code, value] of [["2", DXF_TEXT_STYLE], ["70", "0"], ["40", "0.0"], ["41", "1.0"], ["50", "0.0"], ["71", "0"], ["42", "2.5"], ["3", "txt"], ["4", ""]]) {
147
148
  text.pair(code, value);
148
149
  }
149
150
  closeTable(text);
@@ -167,7 +168,7 @@ const writeDimensionStyles = (text, layout) => {
167
168
  text.pair("340", text.peekHandle());
168
169
  }
169
170
  openRecord(text, layout, "DIMSTYLE", table, "AcDbDimStyleTableRecord", "105");
170
- text.pair("2", "Standard");
171
+ text.pair("2", DXF_TEXT_STYLE);
171
172
  text.pair("70", "0");
172
173
  closeTable(text);
173
174
  };
@@ -7,3 +7,5 @@ export declare const textOf: (record: DxfEntityRecord, code: number, fallback?:
7
7
  export declare const numberOf: (record: DxfEntityRecord, code: number, fallback: number) => number;
8
8
  export declare const numbersOf: (record: DxfEntityRecord, code: number) => number[];
9
9
  export declare const decodedText: (value: string) => string;
10
+ export declare const sectionsOf: (groups: readonly DxfGroup[]) => Map<string, DxfGroup[]>;
11
+ export declare const headerGroupOf: (header: readonly DxfGroup[], name: string) => DxfGroup | undefined;
@@ -1,5 +1,5 @@
1
1
  import { InputError } from "../../../../kernel-calls";
2
- import { BINARY_SENTINEL, QUOTED_LENGTH } from "./dxf-reader.constants";
2
+ import { BINARY_SENTINEL, GROUP, QUOTED_LENGTH } from "./dxf-reader.constants";
3
3
  import { DXF_HEX_RADIX } from "./dxf.constants";
4
4
  export const dxfGroupsOf = (text) => {
5
5
  if (text.startsWith(BINARY_SENTINEL)) {
@@ -63,3 +63,27 @@ export const numberOf = (record, code, fallback) => {
63
63
  };
64
64
  export const numbersOf = (record, code) => record.groups.filter(group => group.code === code).map(numberIn);
65
65
  export const decodedText = (value) => value.replace(/\\U\+([0-9A-Fa-f]{4})/g, (_match, hex) => String.fromCharCode(parseInt(hex, DXF_HEX_RADIX)));
66
+ export const sectionsOf = (groups) => {
67
+ var _a, _b;
68
+ const sections = new Map();
69
+ let current;
70
+ for (let at = 0; at < groups.length; at++) {
71
+ const group = groups[at];
72
+ if (group.code === 0 && group.value.trim() === "SECTION") {
73
+ current = [];
74
+ sections.set((_b = (_a = groups[at + 1]) === null || _a === void 0 ? void 0 : _a.value.trim()) !== null && _b !== void 0 ? _b : "", current);
75
+ at++;
76
+ }
77
+ else if (group.code === 0 && group.value.trim() === "ENDSEC") {
78
+ current = undefined;
79
+ }
80
+ else {
81
+ current === null || current === void 0 ? void 0 : current.push(group);
82
+ }
83
+ }
84
+ return sections;
85
+ };
86
+ export const headerGroupOf = (header, name) => {
87
+ const at = header.findIndex(group => group.code === GROUP.variable && group.value.trim() === name);
88
+ return at < 0 ? undefined : header[at + 1];
89
+ };
@@ -1,3 +1,4 @@
1
+ import type { DxfMeasurement } from "./dxf-tables";
1
2
  export declare const DXF_FIRST_HANDLE = 32;
2
3
  export declare const DXF_INSUNITS: Readonly<Record<string, string>>;
3
4
  export declare const DXF_IMPERIAL_MEASUREMENT = "0";
@@ -33,3 +34,13 @@ export declare const DXF_CLOSED_FLAG = 1;
33
34
  export declare const DXF_RATIONAL_FLAG = 4;
34
35
  export declare const DXF_POLYLINE_3D_FLAG = 8;
35
36
  export declare const DXF_MAX_NAME_LENGTH = 255;
37
+ export declare const DXF_LINE_WEIGHTS: number[];
38
+ export declare const DXF_LINE_WEIGHTS_PER_MILLIMETRE = 100;
39
+ export declare const DXF_TEXT_STYLE = "Standard";
40
+ export declare const DXF_CODED_BACKSLASH = "\\U+005C";
41
+ export declare const DXF_CHARACTER_CODE_START: RegExp;
42
+ export declare const DXF_FIT_JUSTIFICATION = "5";
43
+ export declare const DXF_POLYLINE_BOUNDARY = "3";
44
+ export declare const DXF_HATCH_NAMES: Readonly<Record<string, string>>;
45
+ export declare const DXF_HATCH_NATIVE_ANGLE = 45;
46
+ export declare const DXF_HATCH_NATIVE_SPACING: Readonly<Record<DxfMeasurement, Readonly<Record<string, number>>>>;
@@ -35,3 +35,16 @@ export const DXF_CLOSED_FLAG = 1;
35
35
  export const DXF_RATIONAL_FLAG = 4;
36
36
  export const DXF_POLYLINE_3D_FLAG = 8;
37
37
  export const DXF_MAX_NAME_LENGTH = 255;
38
+ export const DXF_LINE_WEIGHTS = [0, 5, 9, 13, 15, 18, 20, 25, 30, 35, 40, 50, 53, 60, 70, 80, 90, 100, 106, 120, 140, 158, 200, 211];
39
+ export const DXF_LINE_WEIGHTS_PER_MILLIMETRE = 100;
40
+ export const DXF_TEXT_STYLE = "Standard";
41
+ export const DXF_CODED_BACKSLASH = "\\U+005C";
42
+ export const DXF_CHARACTER_CODE_START = /\\(?=[UM]\+)/g;
43
+ export const DXF_FIT_JUSTIFICATION = "5";
44
+ export const DXF_POLYLINE_BOUNDARY = "3";
45
+ export const DXF_HATCH_NAMES = { solid: "SOLID", ansi31: "ANSI31", ansi32: "ANSI32", ansi37: "ANSI37" };
46
+ export const DXF_HATCH_NATIVE_ANGLE = 45;
47
+ export const DXF_HATCH_NATIVE_SPACING = {
48
+ imperial: { solid: 1, ansi31: 0.125, ansi32: 0.375, ansi37: 0.125 },
49
+ metric: { solid: 1, ansi31: 3.175, ansi32: 9.525, ansi37: 3.175 },
50
+ };
@@ -32,7 +32,7 @@ export interface SvgStroke {
32
32
  readonly moves: readonly SvgMove[];
33
33
  readonly closed: boolean;
34
34
  }
35
- export declare const strokeOf: (segment: DrawingSegment, tolerance: number, budget?: WorkBudget) => SvgStroke;
35
+ export declare const strokeOf: (segment: DrawingSegment, tolerance: number, budget?: WorkBudget, cubicsOnly?: boolean) => SvgStroke;
36
36
  export declare const reversedStroke: (stroke: SvgStroke) => SvgStroke;
37
37
  export declare const strokeEnd: (stroke: SvgStroke) => Point2;
38
38
  export {};
@@ -1,18 +1,20 @@
1
1
  import { cubicsOfPiece } from "../../curves/bezier-pieces";
2
- import { conicArcExtremes, principalArcOf } from "../../curves/conic-arcs";
2
+ import { conicArcAsNurbs, conicArcExtremes, ellipseFramePoint, principalArcOf } from "../../curves/conic-arcs";
3
3
  import { bezierPiecesOf, isRationalPiece } from "../../curves/nurbs-forms";
4
- import { add2, cross2, dot2, length2, scale2 } from "../../geometry/vectors";
4
+ import { add2, between2, cross2, dot2, length2, scale2 } from "../../geometry/vectors";
5
5
  import { bulgeArcOfSpan, circleArcOf, ellipseArcOf, polylineSpanEnd, polylineSpans } from "../drawing/drawing-geometry";
6
+ import { QUADRATIC_TO_CUBIC_SHARE } from "./svg-path-data.constants";
6
7
  import { CUBIC_DEGREE, EDGE_ON_SHARE, QUADRATIC_DEGREE, SVG_ARC_PIECE } from "./svg.constants";
7
8
  const flat = (point) => [point[0], point[1]];
8
9
  const shadowOf = (point) => [point[0], point[1], 0];
10
+ const shadowArcOf = (arc) => ({ frame: { center: shadowOf(arc.frame.center), xAxis: shadowOf(arc.frame.xAxis), yAxis: shadowOf(arc.frame.yAxis) }, start: arc.start, sweep: arc.sweep });
9
11
  const endOf = (stroke) => stroke.moves.length === 0 ? stroke.start : stroke.moves[stroke.moves.length - 1].to;
10
12
  const conicStroke = (arc) => {
11
13
  const center = flat(arc.frame.center);
12
14
  const x = flat(arc.frame.xAxis);
13
15
  const y = flat(arc.frame.yAxis);
14
16
  const closed = Math.abs(arc.sweep) >= 2 * Math.PI;
15
- const shadow = { frame: { center: shadowOf(arc.frame.center), xAxis: shadowOf(arc.frame.xAxis), yAxis: shadowOf(arc.frame.yAxis) }, start: arc.start, sweep: arc.sweep };
17
+ const shadow = shadowArcOf(arc);
16
18
  if (Math.abs(cross2(x, y)) <= EDGE_ON_SHARE * Math.max(dot2(x, x), dot2(y, y))) {
17
19
  const along = length2([x[0], y[0]]) >= length2([x[1], y[1]]) ? 0 : 1;
18
20
  const turns = conicArcExtremes(shadow, [along]).map(flat);
@@ -38,20 +40,6 @@ const conicStroke = (arc) => {
38
40
  }));
39
41
  return { start: pointAt(from), moves, closed };
40
42
  };
41
- const bulgeMove = (polyline, span) => {
42
- const to = flat(polylineSpanEnd(polyline, span));
43
- const arc = bulgeArcOfSpan(polyline, span);
44
- if (arc === undefined) {
45
- return { kind: "L", to };
46
- }
47
- const radius = Math.abs(arc.frame.xAxis[0]);
48
- return { kind: "A", radiusX: radius, radiusY: radius, rotation: 0, large: Math.abs(arc.sweep) > Math.PI, counterclockwise: arc.sweep > 0, to };
49
- };
50
- const polylineStroke = (polyline) => ({
51
- start: flat(polyline.points[0]),
52
- moves: Array.from({ length: polylineSpans(polyline) }, (_, span) => bulgeMove(polyline, span)),
53
- closed: polyline.closed,
54
- });
55
43
  const flatPiece = (piece) => ({ points: piece.points.map((point) => [point[0], point[1], 0]), weights: piece.weights });
56
44
  const pieceMoves = (piece, tolerance, budget) => {
57
45
  const points = piece.points.map(flat);
@@ -67,21 +55,57 @@ const pieceMoves = (piece, tolerance, budget) => {
67
55
  }
68
56
  return cubicsOfPiece(flatPiece(piece), tolerance, budget).map((cubic) => ({ kind: "C", control1: flat(cubic.control1), control2: flat(cubic.control2), to: flat(cubic.end) }));
69
57
  };
70
- export const strokeOf = (segment, tolerance, budget) => {
58
+ const shadowCubics = (shadow, fit) => bezierPiecesOf(conicArcAsNurbs(shadow), fit.budget)
59
+ .flatMap(piece => cubicsOfPiece(piece, fit.tolerance, fit.budget))
60
+ .map((cubic) => ({ kind: "C", control1: flat(cubic.control1), control2: flat(cubic.control2), to: flat(cubic.end) }));
61
+ const conicCubics = (arc, fit) => shadowCubics(shadowArcOf(arc), fit);
62
+ const conicCubicStroke = (arc, fit) => {
63
+ const shadow = shadowArcOf(arc);
64
+ return { start: flat(ellipseFramePoint(shadow.frame, shadow.start)), moves: shadowCubics(shadow, fit), closed: Math.abs(arc.sweep) >= 2 * Math.PI };
65
+ };
66
+ const bulgeMoves = (polyline, span, fit) => {
67
+ const to = flat(polylineSpanEnd(polyline, span));
68
+ const arc = bulgeArcOfSpan(polyline, span);
69
+ if (arc === undefined) {
70
+ return [{ kind: "L", to }];
71
+ }
72
+ if (fit.cubicsOnly) {
73
+ return conicCubics(arc, fit);
74
+ }
75
+ const radius = Math.abs(arc.frame.xAxis[0]);
76
+ return [{ kind: "A", radiusX: radius, radiusY: radius, rotation: 0, large: Math.abs(arc.sweep) > Math.PI, counterclockwise: arc.sweep > 0, to }];
77
+ };
78
+ const polylineStroke = (polyline, fit) => ({
79
+ start: flat(polyline.points[0]),
80
+ moves: Array.from({ length: polylineSpans(polyline) }, (_, span) => bulgeMoves(polyline, span, fit)).flat(),
81
+ closed: polyline.closed,
82
+ });
83
+ const raised = (from, move) => move.kind !== "Q" ? move : {
84
+ kind: "C",
85
+ control1: between2(from, move.control, QUADRATIC_TO_CUBIC_SHARE),
86
+ control2: between2(move.to, move.control, QUADRATIC_TO_CUBIC_SHARE),
87
+ to: move.to,
88
+ };
89
+ const splineMoves = (start, pieces, fit) => {
90
+ const moves = pieces.flatMap(piece => pieceMoves(piece, fit.tolerance, fit.budget));
91
+ return fit.cubicsOnly ? moves.map((move, at) => raised(at === 0 ? start : moves[at - 1].to, move)) : moves;
92
+ };
93
+ export const strokeOf = (segment, tolerance, budget, cubicsOnly = false) => {
94
+ const fit = { tolerance, budget, cubicsOnly };
71
95
  switch (segment.kind) {
72
96
  case "line":
73
97
  return { start: flat(segment.start), moves: [{ kind: "L", to: flat(segment.end) }], closed: false };
74
98
  case "circle":
75
99
  case "arc":
76
- return conicStroke(circleArcOf(segment));
100
+ return cubicsOnly ? conicCubicStroke(circleArcOf(segment), fit) : conicStroke(circleArcOf(segment));
77
101
  case "ellipse":
78
- return conicStroke(ellipseArcOf(segment));
102
+ return cubicsOnly ? conicCubicStroke(ellipseArcOf(segment), fit) : conicStroke(ellipseArcOf(segment));
79
103
  case "polyline":
80
- return polylineStroke(segment);
104
+ return polylineStroke(segment, fit);
81
105
  case "spline": {
82
106
  const pieces = bezierPiecesOf(segment.curve, budget);
83
- const start = pieces.length === 0 ? segment.curve.points[0] : pieces[0].points[0];
84
- return { start: flat(start), moves: pieces.flatMap(piece => pieceMoves(piece, tolerance, budget)), closed: false };
107
+ const start = flat(pieces.length === 0 ? segment.curve.points[0] : pieces[0].points[0]);
108
+ return { start, moves: splineMoves(start, pieces, fit), closed: false };
85
109
  }
86
110
  }
87
111
  };
@@ -3,11 +3,13 @@ export interface SvgDocumentOptions {
3
3
  readonly unit: string | undefined;
4
4
  readonly strokeWidth: number | undefined;
5
5
  readonly margin: number | undefined;
6
+ readonly lineTypeScale: number | undefined;
6
7
  readonly tolerance: number;
7
8
  readonly property: string;
8
9
  }
9
10
  export interface SvgPathOptions {
10
11
  readonly flipY: boolean;
12
+ readonly cubicsOnly: boolean;
11
13
  readonly tolerance: number;
12
14
  readonly property: string;
13
15
  }
@@ -1,12 +1,15 @@
1
1
  import * as Inputs from "../../../../inputs";
2
2
  import { InputError } from "../../../../kernel-calls";
3
+ import { angleWithinTurn } from "../../curves/conic-arcs";
3
4
  import { degreesOf } from "../../geometry/scalars";
4
- import { distance2 } from "../../geometry/vectors";
5
- import { drawingBounds, drawingSize, patternUnitOf } from "../drawing/drawing-geometry";
5
+ import { crc32 } from "../crc32";
6
+ import { drawingBounds, drawingSize, meetsReversed, patternUnitOf, pointsMeet } from "../drawing/drawing-geometry";
6
7
  import { writerBudgetOf } from "../drawing/drawing-budget";
7
- import { DEFAULT_LAYER, LEAST_TOLERANCE_SHARE, LINE_TYPE_PATTERNS } from "../drawing/drawing.constants";
8
+ import { hatchFamiliesOf } from "../drawing/drawing-hatch";
9
+ import { textStrokesOf } from "../drawing/drawing-text";
10
+ import { DEFAULT_LAYER, FULL_TURN_DEGREES, LEAST_TOLERANCE_SHARE, LINE_TYPE_PATTERNS, QUARTER_TURN_DEGREES } from "../drawing/drawing.constants";
8
11
  import { reversedStroke, strokeEnd, strokeOf } from "./svg-strokes";
9
- import { ID_UNSAFE, LEAST_VIEW_SPAN, MARGIN_SHARE, PATH_COLOR, STROKE_SHARE, SVG_DECIMALS, SVG_JOIN_DISTANCE, SVG_UNITS, TRAILING_FRACTION_ZEROS } from "./svg.constants";
12
+ import { ID_UNSAFE, LEAST_VIEW_SPAN, MARGIN_SHARE, PATH_COLOR, PATTERN_ID_DIGITS, PATTERN_ID_RADIX, STROKE_SHARE, SVG_DECIMALS, SVG_UNITS, TRAILING_FRACTION_ZEROS } from "./svg.constants";
10
13
  export const svgNumber = (value) => {
11
14
  const text = value.toFixed(SVG_DECIMALS).replace(TRAILING_FRACTION_ZEROS, "$1");
12
15
  return text === "-0" ? "0" : text;
@@ -42,20 +45,19 @@ const moveText = (move, writing) => {
42
45
  }
43
46
  }
44
47
  };
45
- const near = (a, b) => a !== undefined && distance2(a, b) <= SVG_JOIN_DISTANCE;
46
48
  const pathText = (path, writing) => {
47
49
  const words = [];
48
50
  let current;
49
51
  let subpathStart;
50
52
  for (const segment of path.segments) {
51
- let stroke = strokeOf(segment, writing.tolerance, writing.budget);
53
+ let stroke = strokeOf(segment, writing.tolerance, writing.budget, writing.cubicsOnly);
52
54
  if (writing.budget.exhausted) {
53
55
  throw tooLarge(writing);
54
56
  }
55
- if (!near(current, stroke.start) && !stroke.closed && near(current, strokeEnd(stroke))) {
57
+ if (!stroke.closed && meetsReversed(current, stroke.start, strokeEnd(stroke))) {
56
58
  stroke = reversedStroke(stroke);
57
59
  }
58
- if (!near(current, stroke.start)) {
60
+ if (!pointsMeet(current, stroke.start)) {
59
61
  words.push(charged(writing, `M${pointText(stroke.start, writing)}`));
60
62
  subpathStart = stroke.start;
61
63
  }
@@ -63,25 +65,25 @@ const pathText = (path, writing) => {
63
65
  words.push(charged(writing, moveText(move, writing)));
64
66
  }
65
67
  current = strokeEnd(stroke);
66
- if (stroke.moves.length > 0 && near(subpathStart, current)) {
68
+ if (stroke.moves.length > 0 && pointsMeet(subpathStart, current)) {
67
69
  words.push(charged(writing, "Z"));
68
70
  current = subpathStart;
69
71
  }
70
72
  }
71
73
  return joined(writing, words, " ");
72
74
  };
73
- const boundedWriting = (parts, flipY, tolerance, property) => {
75
+ const boundedWriting = (parts, flipY, tolerance, property, cubicsOnly = false) => {
74
76
  const budget = writerBudgetOf(parts);
75
77
  const bounds = drawingBounds(parts, tolerance, budget);
76
- const writing = { flipY, tolerance: Math.max(tolerance, LEAST_TOLERANCE_SHARE * drawingSize(bounds)), budget, property };
78
+ const writing = { flipY, cubicsOnly, tolerance: Math.max(tolerance, LEAST_TOLERANCE_SHARE * drawingSize(bounds)), budget, property };
77
79
  if (budget.exhausted) {
78
80
  throw tooLarge(writing);
79
81
  }
80
82
  return [writing, bounds];
81
83
  };
82
84
  export const pathDataOf = (path, options) => {
83
- const part = { layer: DEFAULT_LAYER, color: PATH_COLOR, lineType: Inputs.IO.lineTypeEnum.continuous, paths: [path] };
84
- return pathText(path, boundedWriting([part], options.flipY, options.tolerance, options.property)[0]);
85
+ const part = { layer: DEFAULT_LAYER, color: PATH_COLOR, lineType: Inputs.IO.lineTypeEnum.continuous, lineWeight: undefined, paths: [path], texts: [], hatches: [] };
86
+ return pathText(path, boundedWriting([part], options.flipY, options.tolerance, options.property, options.cubicsOnly)[0]);
85
87
  };
86
88
  const escaped = (text) => text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
87
89
  const viewSpanOf = (from, extent, margin, least) => {
@@ -119,8 +121,63 @@ const dashText = (lineType, unit) => {
119
121
  const elements = LINE_TYPE_PATTERNS[lineType].elements;
120
122
  return elements.length === 0 ? "" : ` stroke-dasharray="${elements.map(element => svgNumber(Math.abs(element) * unit)).join(" ")}"`;
121
123
  };
124
+ const patternLines = (hatch) => {
125
+ const side = svgNumber(hatch.spacing);
126
+ return hatchFamiliesOf(hatch).map(family => {
127
+ const at = svgNumber(hatch.spacing - family.phase);
128
+ return family.turn === QUARTER_TURN_DEGREES ? `<line x1="${at}" y1="0" x2="${at}" y2="${side}"/>` : `<line x1="0" y1="${at}" x2="${side}" y2="${at}"/>`;
129
+ }).join("");
130
+ };
131
+ const patternIdOf = (key, taken) => {
132
+ const named = `hatch-${crc32(Uint8Array.from(key, character => character.charCodeAt(0))).toString(PATTERN_ID_RADIX).padStart(PATTERN_ID_DIGITS, "0")}`;
133
+ let id = named;
134
+ for (let count = 2; taken.has(id); count++) {
135
+ id = `${named}-${count}`;
136
+ }
137
+ return id;
138
+ };
139
+ const lineWeightText = (weight) => svgNumber(Math.max(weight, LEAST_VIEW_SPAN));
140
+ const hatchFillOf = (hatch, part, strokeWidth, patterns, writing) => {
141
+ var _a;
142
+ if (hatch.pattern === Inputs.IO.hatchPatternEnum.solid) {
143
+ return part.color;
144
+ }
145
+ const weight = lineWeightText((_a = part.lineWeight) !== null && _a !== void 0 ? _a : strokeWidth);
146
+ const side = svgNumber(hatch.spacing);
147
+ const turn = svgNumber(-angleWithinTurn(hatch.angle, FULL_TURN_DEGREES));
148
+ const key = [hatch.pattern, side, turn, part.color, weight].join(" ");
149
+ let id = patterns.ids.get(key);
150
+ if (id === undefined) {
151
+ id = patternIdOf(key, patterns.taken);
152
+ patterns.ids.set(key, id);
153
+ patterns.taken.add(id);
154
+ patterns.definitions.push(charged(writing, ` <pattern id="${id}" patternUnits="userSpaceOnUse" width="${side}" height="${side}" patternTransform="rotate(${turn})"><g fill="none" stroke="${part.color}" stroke-width="${weight}">${patternLines(hatch)}</g></pattern>`));
155
+ }
156
+ return `url(#${id})`;
157
+ };
158
+ const checkSpacings = (parts, property) => {
159
+ parts.forEach((part, at) => part.hatches.forEach((hatch, index) => {
160
+ if (hatch.pattern !== Inputs.IO.hatchPatternEnum.solid && hatch.spacing < LEAST_VIEW_SPAN) {
161
+ const named = `${property}[${at}].hatches[${index}].spacing`;
162
+ throw new InputError(`\`${named}\` is ${hatch.spacing}, finer than ${svgNumber(LEAST_VIEW_SPAN)}, the finest an SVG file draws here.`, named);
163
+ }
164
+ }));
165
+ };
166
+ const hatchText = (hatch, part, strokeWidth, patterns, writing) => {
167
+ const data = joined(writing, hatch.boundaries.map(boundary => pathText(boundary, writing)).filter(text => text !== ""), " ");
168
+ return data === "" ? "" : charged(writing, ` <path d="${data}" fill="${hatchFillOf(hatch, part, strokeWidth, patterns, writing)}" fill-rule="evenodd" stroke="none"/>`, data.length);
169
+ };
170
+ const textText = (text, writing) => {
171
+ const words = [];
172
+ for (const stroke of textStrokesOf(text)) {
173
+ stroke.forEach((point, at) => words.push(charged(writing, `${at === 0 ? "M" : "L"}${pointText([point[0], point[1]], writing)}`)));
174
+ }
175
+ const data = joined(writing, words, " ");
176
+ return data === "" ? "" : charged(writing, ` <path d="${data}"/>`, data.length);
177
+ };
122
178
  export const svgDocumentOf = (parts, options) => {
123
- var _a, _b, _c, _d;
179
+ var _a, _b, _c, _d, _e;
180
+ checkSpacings(parts, options.property);
124
181
  const [writing, bounds] = boundedWriting(parts, true, options.tolerance, options.property);
125
182
  const size = drawingSize(bounds);
126
183
  const margin = (_a = options.margin) !== null && _a !== void 0 ? _a : size * MARGIN_SHARE;
@@ -129,25 +186,45 @@ export const svgDocumentOf = (parts, options) => {
129
186
  const down = viewSpanOf(-((_d = bounds === null || bounds === void 0 ? void 0 : bounds.max[1]) !== null && _d !== void 0 ? _d : 0), bounds === undefined ? 0 : bounds.max[1] - bounds.min[1], margin, strokeWidth);
130
187
  const unit = options.unit === undefined ? undefined : SVG_UNITS[options.unit];
131
188
  const sized = unit === undefined ? "" : ` width="${svgNumber(across.span * unit[1])}${unit[0]}" height="${svgNumber(down.span * unit[1])}${unit[0]}"`;
132
- const patternUnit = patternUnitOf(bounds);
133
- const lines = [
189
+ const patternUnit = (_e = options.lineTypeScale) !== null && _e !== void 0 ? _e : patternUnitOf(bounds);
190
+ const patterns = { ids: new Map(), taken: new Set(), definitions: [] };
191
+ const head = [
134
192
  "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>",
135
193
  `<svg xmlns="http://www.w3.org/2000/svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" version="1.1"${sized} viewBox="${[across.from, down.from, across.span, down.span].map(svgNumber).join(" ")}">`,
136
194
  ].map(line => charged(writing, line));
195
+ const body = [];
137
196
  for (const layer of layersOf(parts)) {
138
- lines.push(charged(writing, ` <g id="${escaped(layer.id)}" inkscape:groupmode="layer" inkscape:label="${escaped(layer.label)}" fill="none" stroke-width="${svgNumber(strokeWidth)}" stroke-linecap="round" stroke-linejoin="round">`));
197
+ body.push(charged(writing, ` <g id="${escaped(layer.id)}" inkscape:groupmode="layer" inkscape:label="${escaped(layer.label)}" fill="none" stroke-width="${svgNumber(strokeWidth)}" stroke-linecap="round" stroke-linejoin="round">`));
139
198
  for (const part of layer.parts) {
140
- lines.push(charged(writing, ` <g stroke="${part.color}"${dashText(part.lineType, patternUnit)}>`));
199
+ const weight = part.lineWeight === undefined ? "" : ` stroke-width="${lineWeightText(part.lineWeight)}"`;
200
+ body.push(charged(writing, ` <g stroke="${part.color}"${weight}${dashText(part.lineType, patternUnit)}>`));
201
+ for (const hatch of part.hatches) {
202
+ const line = hatchText(hatch, part, strokeWidth, patterns, writing);
203
+ if (line !== "") {
204
+ body.push(line);
205
+ }
206
+ }
141
207
  for (const path of part.paths) {
142
208
  const data = pathText(path, writing);
143
209
  if (data !== "") {
144
- lines.push(charged(writing, ` <path d="${data}"/>`, data.length));
210
+ body.push(charged(writing, ` <path d="${data}"/>`, data.length));
145
211
  }
146
212
  }
147
- lines.push(charged(writing, " </g>"));
213
+ const texts = part.texts.map(text => textText(text, writing)).filter(line => line !== "");
214
+ const dashed = texts.length > 0 && part.lineType !== Inputs.IO.lineTypeEnum.continuous;
215
+ if (dashed) {
216
+ body.push(charged(writing, " <g stroke-dasharray=\"none\">"));
217
+ }
218
+ for (const line of texts) {
219
+ body.push(dashed ? ` ${line}` : line);
220
+ }
221
+ if (dashed) {
222
+ body.push(charged(writing, " </g>"));
223
+ }
224
+ body.push(charged(writing, " </g>"));
148
225
  }
149
- lines.push(charged(writing, " </g>"));
226
+ body.push(charged(writing, " </g>"));
150
227
  }
151
- lines.push(charged(writing, "</svg>"), "");
152
- return joined(writing, lines, "\n");
228
+ const definitions = patterns.definitions.length === 0 ? [] : [charged(writing, " <defs>")].concat(patterns.definitions, [charged(writing, " </defs>")]);
229
+ return joined(writing, head.concat(definitions, body, [charged(writing, "</svg>"), ""]), "\n");
153
230
  };
@@ -5,9 +5,10 @@ export declare const EDGE_ON_SHARE = 1e-12;
5
5
  export declare const SVG_DECIMALS = 6;
6
6
  export declare const TRAILING_FRACTION_ZEROS: RegExp;
7
7
  export declare const LEAST_VIEW_SPAN: number;
8
- export declare const SVG_JOIN_DISTANCE = 1e-9;
9
8
  export declare const STROKE_SHARE: number;
10
9
  export declare const MARGIN_SHARE: number;
11
10
  export declare const SVG_UNITS: Readonly<Record<string, readonly [string, number]>>;
12
11
  export declare const ID_UNSAFE: RegExp;
13
12
  export declare const PATH_COLOR = "#000000";
13
+ export declare const PATTERN_ID_RADIX = 16;
14
+ export declare const PATTERN_ID_DIGITS = 8;
@@ -5,9 +5,10 @@ export const EDGE_ON_SHARE = 1e-12;
5
5
  export const SVG_DECIMALS = 6;
6
6
  export const TRAILING_FRACTION_ZEROS = /(\.\d*?[1-9])0+$|\.0+$/;
7
7
  export const LEAST_VIEW_SPAN = Math.pow(10, -SVG_DECIMALS);
8
- export const SVG_JOIN_DISTANCE = 1e-9;
9
8
  export const STROKE_SHARE = 1 / 500;
10
9
  export const MARGIN_SHARE = 1 / 40;
11
10
  export const SVG_UNITS = { mm: ["mm", 1], cm: ["cm", 1], m: ["cm", 100], in: ["in", 1] };
12
11
  export const ID_UNSAFE = /[^A-Za-z0-9_.-]+/g;
13
12
  export const PATH_COLOR = "#000000";
13
+ export const PATTERN_ID_RADIX = 16;
14
+ export const PATTERN_ID_DIGITS = 8;
@@ -0,0 +1,2 @@
1
+ import { type FillRule2 } from "./polygon-nesting";
2
+ export declare function filledLoopTriangles(positions: ArrayLike<number>, loops: readonly (readonly number[])[], rule: FillRule2): number[];
@@ -0,0 +1,37 @@
1
+ import { filledRegions2, nestedOutlines2, nestingOutlineOf2, overlappingBoxPairs2 } from "./polygon-nesting";
2
+ import { newellNormal, triangulateFace } from "./triangulation";
3
+ const POINT = 3;
4
+ const LEAST_CORNERS = 3;
5
+ const sameCorner = (positions, a, b) => positions[a * POINT] === positions[b * POINT]
6
+ && positions[a * POINT + 1] === positions[b * POINT + 1]
7
+ && positions[a * POINT + 2] === positions[b * POINT + 2];
8
+ const distinctCorners = (positions, loop) => loop.filter((vertex, at) => loop.length === 1 || !sameCorner(positions, vertex, loop[at === 0 ? loop.length - 1 : at - 1]));
9
+ const weightOf = ([x, y, z]) => x * x + y * y + z * z;
10
+ export function filledLoopTriangles(positions, loops, rule) {
11
+ const kept = loops.map((loop) => distinctCorners(positions, loop)).filter((loop) => loop.length >= LEAST_CORNERS);
12
+ const normals = kept.map((loop) => newellNormal(positions, loop));
13
+ const reference = normals.reduce((best, normal) => best === undefined || weightOf(normal) > weightOf(best) ? normal : best, undefined);
14
+ if (reference === undefined) {
15
+ return [];
16
+ }
17
+ const magnitudes = reference.map(Math.abs);
18
+ const dropped = magnitudes.indexOf(Math.max(...magnitudes));
19
+ if (!(magnitudes[dropped] > 0)) {
20
+ return [];
21
+ }
22
+ const u = (dropped + 1) % POINT;
23
+ const v = (dropped + 2) % POINT;
24
+ const sense = reference[dropped] < 0 ? -1 : 1;
25
+ const outlines = kept.map((loop) => nestingOutlineOf2(loop.map((vertex) => [positions[vertex * POINT + u] * sense, positions[vertex * POINT + v]])));
26
+ const ancestors = nestedOutlines2(outlines, overlappingBoxPairs2(outlines.map((outline) => outline.box)));
27
+ const triangles = [];
28
+ for (const region of filledRegions2(outlines, ancestors, rule)) {
29
+ const outer = kept[region.outer];
30
+ const facingReference = outlines[region.outer].area > 0 ? outer : [...outer].reverse();
31
+ const filled = triangulateFace(positions, [facingReference, ...region.holes.map((hole) => kept[hole])]);
32
+ for (const corner of filled) {
33
+ triangles.push(corner);
34
+ }
35
+ }
36
+ return triangles;
37
+ }
@@ -109,3 +109,36 @@ export interface GltfJson {
109
109
  readonly nodes?: readonly GltfNode[];
110
110
  }
111
111
  export type UnitOfLength = `${Inputs.Base.unitOfLengthEnum}`;
112
+ export type StrokePoint = readonly [number, number];
113
+ export interface StrokeGlyph {
114
+ readonly advance: number;
115
+ readonly strokes: readonly (readonly StrokePoint[])[];
116
+ }
117
+ export interface StrokeFontMarks {
118
+ readonly height: number;
119
+ readonly above: Readonly<Record<number, readonly (number | undefined)[]>>;
120
+ readonly below: Readonly<Record<number, readonly (number | undefined)[]>>;
121
+ }
122
+ export interface StrokeOverlay {
123
+ readonly base: number;
124
+ readonly strokes: readonly (number | undefined)[];
125
+ }
126
+ export interface StrokeFontLetters {
127
+ readonly height: number;
128
+ readonly overlaid: Readonly<Record<number, StrokeOverlay>>;
129
+ readonly drawn: Readonly<Record<number, readonly (number | undefined)[]>>;
130
+ }
131
+ export interface StrokeComposition {
132
+ readonly base: number;
133
+ readonly above: number | undefined;
134
+ readonly below: number | undefined;
135
+ }
136
+ export interface StrokeFontInk {
137
+ readonly lowest: number;
138
+ readonly highest: number;
139
+ }
140
+ export interface StrokeFontFacts {
141
+ readonly lowest: number;
142
+ readonly highest: number;
143
+ readonly glyphs: Map<number, StrokeGlyph>;
144
+ }
@@ -0,0 +1,6 @@
1
+ import type { VectorFont } from "../../../models/simplex";
2
+ import type { StrokeFontInk, StrokeGlyph, StrokePoint } from "../helper-types";
3
+ export declare function strokeGlyph(font: VectorFont, code: number): StrokeGlyph;
4
+ export declare function strokeFontInk(font: VectorFont): StrokeFontInk;
5
+ export declare function strokeTextAdvance(font: VectorFont, text: string): number;
6
+ export declare function strokeTextStrokes(font: VectorFont, text: string): StrokePoint[][];