tsdraft 0.2.0 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,66 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.1
4
+
5
+ Every fix below was found by opening tsdraft's output in AutoCAD 2025.
6
+
7
+ - Fixed: text holding `\U+XXXX` or `\M+NXXXX` came out in AutoCAD as the
8
+ character those escapes stand for, so `\U+00B0` became a degree sign and
9
+ `\U+000A` put a line feed inside a single-line text. Both DXF versions now
10
+ write such a backslash so that AutoCAD holds the text as the drawing has it
11
+ when it opens the file. AutoCAD decodes the text after all once it has saved
12
+ the drawing itself and opened it again: `\M+NXXXX` after a save as DWG, and
13
+ `\U+XXXX` too after a save as DXF.
14
+ - Fixed: a size too small for the 6 decimals tsdraft writes was written as 0.
15
+ AutoCAD gave such a text its default height, and held such a circle or arc
16
+ with radius 0. A radius, text height or rectangle size that would be written
17
+ as 0 is now an error, in the compiler for a literal and when the code runs.
18
+ `rect()` also throws when a side is lost because two corners round together.
19
+ - Fixed: AutoCAD reads only two exponent digits from DXF, so it took a number
20
+ written as `1e+100` for `1e+10`, with no `AUDIT` error. A coordinate, size or
21
+ text rotation of 1e100 or more is now an error, in the compiler for a size
22
+ written as a literal and when the code runs. The R2018 writer also caps the
23
+ extents and the view height it works out, which could pass 1e100 for a
24
+ drawing whose own numbers are all smaller.
25
+ - Fixed: an arc within 0.000001 degrees of a full turn was written with equal
26
+ angles, which AutoCAD holds as an arc of no length. It is now an error. An arc
27
+ angle that rounds up to 360 is written as 0.
28
+ - Fixed: `toDXF` wrote layer `0` wherever the drawing listed it, and `AUDIT`
29
+ reported "Layer zero name found at incorrect index" when another layer came
30
+ first. Layer `0` is now written first.
31
+ - Fixed: `lint` matched layer names with JavaScript's `toLowerCase()`, which
32
+ AutoCAD does not use. It accepted an entity on `οδος` for a layer `ΟΔΟΣ`,
33
+ where AutoCAD makes a second layer. `lint` and the builder now compare names
34
+ as AutoCAD does. The README lists the pairs that differ.
35
+ - Fixed: two layers such as `ΟΔΟΣ` and `οδος`, which AutoCAD keeps apart but
36
+ its `AUDIT` reports as a duplicate name, are now an error.
37
+ - Fixed: AutoCAD discarded a drawing whose layer name had a zero-width space,
38
+ another invisible formatting character, a letterlike symbol such as the ohm
39
+ sign U+2126, or a Glagolitic, Coptic, Tifinagh, Georgian Supplement, Latin
40
+ Extended-C or Ethiopic Extended letter. Those names are now errors, in the
41
+ compiler for a literal and when the code runs. The README lists them.
42
+ - Fixed: `lint` threw a `RangeError` on a polyline of about 100,000 points or
43
+ more.
44
+ - Fixed: a layer name that starts or ends with a no-break space, or another
45
+ space outside ASCII, threw when the code ran, though the compiler accepted
46
+ the literal and AutoCAD accepts the name. Only an ASCII space is refused now.
47
+ - Fixed: AutoCAD replaced the private-use characters U+E800 to U+E8FF in text
48
+ and layer names. They are now errors.
49
+ - Fixed: `toSVG` drew nothing for an arc so close to a full turn that its ends
50
+ rounded to one point, and ignored a layer's color when an entity spelled the
51
+ layer's name in another case.
52
+ - Changed: a `DrawingError` message starts "The drawing has 3 problems:" and no
53
+ longer names DXF, because code other than `toDXF` uses it to refuse a drawing.
54
+ - Changed: `lint` messages for a bad radius or height read "radius: This value
55
+ must be greater than 0", the wording the compiler uses for a literal.
56
+ - `lint` warns about a polyline or an arc with no length, as it does for a line,
57
+ and about a line, polyline or arc that loses its length to rounding.
58
+ - `lint` warns about text that contains `%%`, which AutoCAD stores as written
59
+ but draws as a control code: `%%d` is drawn as a degree sign.
60
+ - `pnpm test:cad` covers each fix above, and checks every character `lint`
61
+ accepts in text and in layer names, and each form of number the writers
62
+ print, against AutoCAD.
63
+
3
64
  ## 0.2.0
4
65
 
5
66
  0.1.1 was never published, so its changes ship here too.
package/README.md CHANGED
@@ -46,6 +46,12 @@ Coordinates are in drawing units with Y pointing up, as in CAD. Angles are in
46
46
  degrees, counterclockwise from +X. Layer colors are AutoCAD Color Index numbers.
47
47
  Entities go on layer `0` unless they are drawn inside a `layer()` callback.
48
48
 
49
+ Numbers are written with 6 decimals, in DXF and in SVG. The checks below judge
50
+ a drawing as it will be written, so a size too small to survive the rounding is
51
+ an error and a line that rounds down to a point is a warning. Every number must
52
+ also be under 1e100 in size. AutoCAD reads only two exponent digits from DXF, so
53
+ it would take `1e+100` for `1e+10` and report nothing.
54
+
49
55
  ## DXF versions
50
56
 
51
57
  A drawing is written for one DXF version. `createDrawing()` targets R12, the
@@ -98,15 +104,101 @@ The rules:
98
104
  - Layer names cannot be empty, start or end with a space, or contain a line
99
105
  break or any of ``< > / \ " : ; ? * | = ` ^ ,``. R12 names have the tighter
100
106
  limits in the table above.
107
+ - Layer names cannot contain the other characters AutoCAD refuses in a name. It
108
+ discards the drawing over any of them, and they are listed under
109
+ [Layer name characters](#layer-name-characters).
101
110
  - `wall` and `WALL` are the same layer, as in CAD. The first spelling is kept.
111
+ tsdraft compares names the way AutoCAD does, which is not the way JavaScript
112
+ does: see [Layer names and case](#layer-names-and-case).
102
113
  - Layer colors are whole numbers from 1 to 255.
103
- - Radii, text heights and rectangle sizes are greater than 0.
114
+ - Radii, text heights and rectangle sizes are greater than 0, and large enough
115
+ not to be written as 0. Anything from 0.000001 up is safe. AutoCAD gives text
116
+ of height 0 its default height.
104
117
  - Text is a single line. It cannot contain emoji or other characters above
105
- U+FFFF, because AutoCAD cannot read them from DXF.
106
- - Arc angles are stored from 0 up to 360, so -90 becomes 270.
107
-
108
- `lint` also returns warnings, such as a line with no length. Warnings do not
109
- stop an export.
118
+ U+FFFF, because AutoCAD cannot read them from DXF, or the private-use
119
+ characters U+E800 to U+E8FF, which AutoCAD replaces.
120
+ - Coordinates, sizes and text rotations are under 1e100 in size, because
121
+ AutoCAD reads only two exponent digits from DXF.
122
+ - Arc angles are stored from 0 up to 360, so -90 becomes 270. An arc cannot come
123
+ within 0.000001 degrees of a full turn, because its two angles would be
124
+ written the same and AutoCAD holds such an arc as having no length. Draw a
125
+ circle instead.
126
+
127
+ `lint` also returns warnings. Warnings do not stop an export. They are:
128
+
129
+ - A line, a polyline or an arc that has no length, or has none once its numbers
130
+ are rounded.
131
+ - Text that contains `%%`. AutoCAD stores the text as written but reads `%%` as
132
+ the start of a control code when it draws it: `%%d` is drawn as a degree sign
133
+ and `%%%` as one percent sign.
134
+
135
+ `toDXF` throws a `DrawingError` for errors only. Its `diagnostics` property holds
136
+ them, and other code may throw one to refuse a drawing for its own reasons.
137
+
138
+ ### Text
139
+
140
+ When AutoCAD opens tsdraft's DXF, it holds each text exactly as the drawing has
141
+ it. tsdraft escapes what DXF would otherwise read as something else: a caret,
142
+ and a backslash that starts `\U+XXXX` or `\M+NXXXX`, which AutoCAD decodes into
143
+ a character. A text `\U+00B0` reaches AutoCAD as those seven characters and not
144
+ as a degree sign. Write the character itself, `°`, to get the character.
145
+
146
+ That lasts until AutoCAD saves the drawing, because AutoCAD does not escape these
147
+ sequences in what it writes. After it saves as DWG and opens the file again, a
148
+ text `\M+18140` holds the character U+3000. After it saves as DXF and opens
149
+ that, `\U+00B0` holds a degree sign as well. tsdraft cannot change this.
150
+
151
+ ### Layer names and case
152
+
153
+ AutoCAD lower-cases a layer name one character at a time, from a table of its
154
+ own that is older and smaller than Unicode's. An entity on `mixedcase`, `café`,
155
+ `αβγ` or `СЛОЙ` goes on a layer declared as `MixedCase`, `CAFÉ`, `ΑΒΓ` or `Слой`,
156
+ and `lint` accepts it. AutoCAD does not pair these, and `lint` reports the
157
+ entity's layer as undefined:
158
+
159
+ - `ΟΔΟΣ` and `οδος`, because of the final sigma `ς`. `οδοσ` does match.
160
+ - `STRASSE` and `straße`, and `TITLE` and `tıtle` with a dotless `ı`.
161
+ - `Ÿ` and `ÿ`, `İ` and `i`, and the title-case digraphs such as `Dž`.
162
+ - Georgian, Cherokee and most other letters that gained a case after Unicode
163
+ 5.1.
164
+
165
+ Declaring both spellings as layers is an error too for some of these pairs,
166
+ among them `ΟΔΟΣ` and `οδος`, `Ÿ` and `ÿ`, and `ß` and `ẞ`. AutoCAD keeps two
167
+ layers, and its `AUDIT` then reports a duplicate name.
168
+
169
+ `AUDIT` compares names more loosely than `lint` can follow in full. It reports
170
+ each of these pairs as a duplicate name while AutoCAD keeps both layers, and
171
+ `lint` does not catch them:
172
+
173
+ - A letter and the two letters it stands for: `ß` and `ss`, `æ` and `ae`, `œ`
174
+ and `oe`, `ij` and `ij`, `fi` and `fi`, `dž` and `dž`, `þ` and `th`.
175
+ - A composed letter such as `é` and its decomposed form.
176
+ - Names that differ only by a soft hyphen.
177
+
178
+ ### Layer name characters
179
+
180
+ AutoCAD refuses these characters in a layer name, so `lint` does too. Over any
181
+ of them but the last row's, AutoCAD discards the whole drawing. All are outside
182
+ ASCII, so they only concern R2018 drawings.
183
+
184
+ | Characters | What they are |
185
+ | -------------------------------------------------------------------------- | ---------------------------------------------------------- |
186
+ | U+0080 to U+009F | C1 control characters |
187
+ | U+0600 to U+0603, U+06DD, U+070F, U+17B4, U+17B5 | Arabic, Syriac and Khmer formatting characters |
188
+ | U+200B to U+200F, U+202A to U+202E, U+2060 to U+2064, U+206A to U+206F | Zero-width spaces and joiners, and text direction controls |
189
+ | U+FEFF, U+FFF9 to U+FFFB | The byte order mark and annotation characters |
190
+ | U+2071, U+207F, U+2090 to U+2094 | Superscript and subscript letters |
191
+ | Letters in U+2102 to U+214E, such as `ℓ`, `ℕ`, the ohm sign and the kelvin | Letterlike symbols. Use the Greek `Ω`, U+03A9, for ohms. |
192
+ | U+2180 to U+2188 | Roman numeral characters |
193
+ | U+2C00 to U+2C5E, except U+2C2F | Glagolitic |
194
+ | U+2C60 to U+2C7D, except U+2C70 | Latin Extended-C |
195
+ | U+2C80 to U+2CE4 | Coptic |
196
+ | U+2D00 to U+2D25 | Georgian Supplement |
197
+ | U+2D30 to U+2D65, U+2D6F | Tifinagh |
198
+ | Letters in U+2D80 to U+2DDE | Ethiopic Extended |
199
+ | U+E800 to U+E8FF | Private use. AutoCAD replaces these and keeps the drawing. |
200
+
201
+ `src/layers.ts` has the exact list.
110
202
 
111
203
  ## Development
112
204
 
@@ -124,7 +216,8 @@ pnpm example # writes a site plan to examples/out/ as DXF and SVG
124
216
  that has AutoCAD. Each test drawing runs in AutoCAD's headless console, which
125
217
  runs `AUDIT` and saves the drawing back as DXF. The script then compares that
126
218
  file with what was drawn. Run it after any change to a DXF writer, because
127
- AutoCAD rejects files that DXF parsing libraries accept.
219
+ AutoCAD rejects files that DXF parsing libraries accept. Some of its cases write
220
+ a file `toDXF` would refuse, to show that AutoCAD does what the rule says.
128
221
 
129
222
  `pnpm test:package` packs the package, installs the tarball into a throwaway
130
223
  project and checks that it type-checks and runs there. `pnpm build` compiles
package/dist/drawing.d.ts CHANGED
@@ -105,6 +105,10 @@ export declare class DrawingBuilder<V extends DxfVersion = "R12"> {
105
105
  * Arc from `startAngle` counterclockwise to `endAngle`. Angles are stored
106
106
  * from 0 up to 360, so -90 becomes 270, because AutoCAD's AUDIT rejects
107
107
  * angles outside that range.
108
+ *
109
+ * Angles are written with 6 decimals. An arc within 0.000001 degrees of a
110
+ * full turn would be written with equal angles, which AutoCAD holds as an
111
+ * arc of no length, so it throws: draw a circle instead.
108
112
  */
109
113
  arc<R extends number>(center: Point, radius: Positive<R>, startAngle: number, endAngle: number): this;
110
114
  text<H extends number = number>(at: Point, value: string, options?: TextOptions<H>): this;
package/dist/drawing.js CHANGED
@@ -1,4 +1,6 @@
1
- import { layerColorProblem, layerNameProblem, textProblem, } from "./rules.js";
1
+ import { fmt, written } from "./format.js";
2
+ import { layerAuditKey, layerKey } from "./layers.js";
3
+ import { arcProblem, layerColorProblem, magnitudeProblem, layerNameProblem, layerTwinProblem, sizeProblem, textProblem, } from "./rules.js";
2
4
  const UNITS = ["in", "ft", "mm", "cm", "m"];
3
5
  const DXF_VERSIONS = ["R12", "R2018"];
4
6
  /**
@@ -12,8 +14,10 @@ const DXF_VERSIONS = ["R12", "R2018"];
12
14
  export class DrawingBuilder {
13
15
  units;
14
16
  dxf;
15
- /** Keyed by lower-cased name, because CAD ignores case in layer names. */
17
+ /** Keyed by `layerKey`, because AutoCAD ignores case in layer names. */
16
18
  #layers = new Map([["0", { name: "0", color: 7 }]]);
19
+ /** The first layer name for each `layerAuditKey`, to find names AUDIT would call duplicates. */
20
+ #audited = new Map([["0", "0"]]);
17
21
  #entities = [];
18
22
  #current = "0";
19
23
  constructor(options = {}) {
@@ -37,14 +41,20 @@ export class DrawingBuilder {
37
41
  const nameProblem = layerNameProblem(requested, this.dxf);
38
42
  if (nameProblem)
39
43
  throw new Error(`Layer ${JSON.stringify(requested)}: ${nameProblem}`);
40
- const key = requested.toLowerCase();
44
+ const key = layerKey(requested);
41
45
  const existing = this.#layers.get(key);
46
+ const auditKey = layerAuditKey(requested);
47
+ const twinProblem = layerTwinProblem(requested, this.#audited.get(auditKey) ?? requested);
48
+ if (twinProblem)
49
+ throw new Error(`Layer ${JSON.stringify(requested)}: ${twinProblem}`);
42
50
  const color = options.color ?? existing?.color ?? 7;
43
51
  const colorProblem = layerColorProblem(color);
44
52
  if (colorProblem)
45
53
  throw new Error(`Layer ${JSON.stringify(requested)}: ${colorProblem}`);
46
54
  const layer = { name: existing?.name ?? requested, color };
47
55
  this.#layers.set(key, layer);
56
+ if (!this.#audited.has(auditKey))
57
+ this.#audited.set(auditKey, layer.name);
48
58
  if (draw) {
49
59
  const previous = this.#current;
50
60
  this.#current = layer.name;
@@ -70,6 +80,11 @@ export class DrawingBuilder {
70
80
  const [x, y] = pt(corner);
71
81
  const w = positive(width, "width");
72
82
  const h = positive(height, "height");
83
+ // A side can still vanish in the file: a corner and the one across from it may round together.
84
+ if (written(x + w) === written(x))
85
+ throw new Error(lostSide("width", "x", x));
86
+ if (written(y + h) === written(y))
87
+ throw new Error(lostSide("height", "y", y));
73
88
  return this.polyline([
74
89
  [x, y],
75
90
  [x + w, y],
@@ -89,12 +104,19 @@ export class DrawingBuilder {
89
104
  * Arc from `startAngle` counterclockwise to `endAngle`. Angles are stored
90
105
  * from 0 up to 360, so -90 becomes 270, because AutoCAD's AUDIT rejects
91
106
  * angles outside that range.
107
+ *
108
+ * Angles are written with 6 decimals. An arc within 0.000001 degrees of a
109
+ * full turn would be written with equal angles, which AutoCAD holds as an
110
+ * arc of no length, so it throws: draw a circle instead.
92
111
  */
93
112
  arc(center, radius, startAngle, endAngle) {
94
113
  const start = turn(num(startAngle, "startAngle"));
95
114
  const end = turn(num(endAngle, "endAngle"));
96
115
  if (start === end)
97
116
  throw new Error("An arc's start and end angles must differ");
117
+ const problem = arcProblem(start, end);
118
+ if (problem)
119
+ throw new Error(problem);
98
120
  return this.#add({
99
121
  kind: "arc",
100
122
  layer: this.#current,
@@ -114,7 +136,7 @@ export class DrawingBuilder {
114
136
  at: pt(at),
115
137
  value,
116
138
  height: positive(options.height ?? 0.125, "height"),
117
- rotation: num(options.rotation ?? 0, "rotation"),
139
+ rotation: kept(options.rotation ?? 0, "rotation"),
118
140
  });
119
141
  }
120
142
  build() {
@@ -153,10 +175,21 @@ function positive(value, label) {
153
175
  if (typeof value !== "number" || !Number.isFinite(value)) {
154
176
  throw new Error(`${label} must be a finite number`);
155
177
  }
156
- if (value <= 0)
157
- throw new Error(`${label} must be greater than 0`);
178
+ const problem = sizeProblem(value);
179
+ if (problem)
180
+ throw new Error(`${label}: ${problem}`);
181
+ return value;
182
+ }
183
+ function lostSide(size, axis, at) {
184
+ return `${size}: This value is too small at ${axis} = ${at}. Both sides of the rectangle are written as ${axis} = ${fmt(at)}, because tsdraft writes 6 decimals`;
185
+ }
186
+ /** A number the drawing keeps and the writers print, which AutoCAD must be able to read. */
187
+ function kept(value, label) {
188
+ const problem = magnitudeProblem(num(value, label));
189
+ if (problem)
190
+ throw new Error(`${label}: ${problem}`);
158
191
  return value;
159
192
  }
160
193
  function pt(point) {
161
- return [num(point[0], "x"), num(point[1], "y")];
194
+ return [kept(point[0], "x"), kept(point[1], "y")];
162
195
  }
package/dist/dxf-r12.d.ts CHANGED
@@ -6,5 +6,6 @@ import type { Drawing } from "./drawing.ts";
6
6
  * R12 has no header variable for drawing units, so coordinates are written as
7
7
  * plain numbers in the drawing's units and `drawing.units` is not written.
8
8
  * R12 predates Unicode DXF, so text outside ASCII is written as `\U+XXXX`.
9
+ * Layer `0` is written first, wherever it is in `drawing.layers`.
9
10
  */
10
11
  export declare function toDXFR12(drawing: Drawing): string;
package/dist/dxf-r12.js CHANGED
@@ -1,4 +1,5 @@
1
- import { dxfText, fmt } from "./format.js";
1
+ import { dxfText, fmt, writtenAngle } from "./format.js";
2
+ import { layerZeroFirst } from "./layers.js";
2
3
  /**
3
4
  * Writes an ASCII DXF (AC1009, R12). R12 is the most widely readable DXF:
4
5
  * AutoCAD, BricsCAD, LibreCAD, QCAD and most converters open it.
@@ -6,6 +7,7 @@ import { dxfText, fmt } from "./format.js";
6
7
  * R12 has no header variable for drawing units, so coordinates are written as
7
8
  * plain numbers in the drawing's units and `drawing.units` is not written.
8
9
  * R12 predates Unicode DXF, so text outside ASCII is written as `\U+XXXX`.
10
+ * Layer `0` is written first, wherever it is in `drawing.layers`.
9
11
  */
10
12
  export function toDXFR12(drawing) {
11
13
  const out = [];
@@ -33,7 +35,7 @@ export function toDXFR12(drawing) {
33
35
  g(0, "TABLE");
34
36
  g(2, "LAYER");
35
37
  g(70, String(drawing.layers.length));
36
- for (const layer of drawing.layers) {
38
+ for (const layer of layerZeroFirst(drawing.layers)) {
37
39
  g(0, "LAYER");
38
40
  g(2, layer.name);
39
41
  g(70, "0");
@@ -95,8 +97,8 @@ function writeEntity(e, g) {
95
97
  g(20, e.center[1]);
96
98
  g(30, 0);
97
99
  g(40, e.radius);
98
- g(50, e.startAngle);
99
- g(51, e.endAngle);
100
+ g(50, writtenAngle(e.startAngle));
101
+ g(51, writtenAngle(e.endAngle));
100
102
  return;
101
103
  case "text":
102
104
  g(0, "TEXT");
package/dist/dxf-r2018.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { extents } from "./extents.js";
2
- import { dxfText, fmt } from "./format.js";
2
+ import { dxfText, fmt, readable, writtenAngle } from "./format.js";
3
+ import { layerZeroFirst } from "./layers.js";
3
4
  /** `$INSUNITS` codes from the DXF Reference. */
4
5
  const INSUNITS = { in: 1, ft: 2, mm: 4, cm: 5, m: 6 };
5
6
  /**
@@ -21,7 +22,14 @@ export function toDXFR2018(drawing) {
21
22
  const normalPlotStyle = handle();
22
23
  const modelSpace = handle();
23
24
  const paperSpace = handle();
24
- const [minX, minY, maxX, maxY] = extents(drawing.entities);
25
+ // The extents are worked out here, so they are brought into the range AutoCAD reads.
26
+ const [left, bottom, right, top] = extents(drawing.entities);
27
+ const [minX, minY, maxX, maxY] = [
28
+ readable(left),
29
+ readable(bottom),
30
+ readable(right),
31
+ readable(top),
32
+ ];
25
33
  g(0, "SECTION");
26
34
  g(2, "CLASSES");
27
35
  g(0, "ENDSEC");
@@ -52,7 +60,9 @@ export function toDXFR2018(drawing) {
52
60
  g(40, 0);
53
61
  },
54
62
  })));
55
- writeTable(g, handle, "LAYER", drawing.layers.map((layer) => ({
63
+ writeTable(g, handle, "LAYER",
64
+ // AUDIT reports "Layer zero name found at incorrect index" unless layer 0 comes first.
65
+ layerZeroFirst(drawing.layers).map((layer) => ({
56
66
  type: "LAYER",
57
67
  subclass: "AcDbLayerTableRecord",
58
68
  name: layer.name,
@@ -222,7 +232,8 @@ function writeTable(g, handle, name, records) {
222
232
  /** The *Active viewport, zoomed to the drawing's extents so the file opens on the drawing. */
223
233
  function writeActiveViewport(g, minX, minY, maxX, maxY) {
224
234
  const aspect = 1.6;
225
- const height = Math.max(maxY - minY, (maxX - minX) / aspect, 1) * 1.1;
235
+ // A drawing can span more than AutoCAD can read as one number, so the height is capped.
236
+ const height = readable(Math.max(maxY - minY, (maxX - minX) / aspect, 1) * 1.1);
226
237
  g(10, 0); // lower-left corner of the viewport on screen
227
238
  g(20, 0);
228
239
  g(11, 1); // upper-right corner
@@ -315,8 +326,8 @@ function writeEntity(g, e, handle, owner) {
315
326
  point(g, 10, ...e.center);
316
327
  g(40, e.radius);
317
328
  g(100, "AcDbArc");
318
- g(50, e.startAngle);
319
- g(51, e.endAngle);
329
+ g(50, writtenAngle(e.startAngle));
330
+ g(51, writtenAngle(e.endAngle));
320
331
  return;
321
332
  case "text":
322
333
  begin("TEXT", "AcDbText");
package/dist/format.d.ts CHANGED
@@ -1,9 +1,38 @@
1
+ /**
2
+ * A number as the writers print it: rounded to 6 decimals. Two values that are
3
+ * equal here are the same number in the file, whatever they were in the drawing.
4
+ */
5
+ export declare function written(value: number): number;
6
+ /**
7
+ * An arc angle in degrees as the DXF writers print it: rounded to 6 decimals,
8
+ * with 0 in place of an angle that rounds up to 360.
9
+ */
10
+ export declare function writtenAngle(degrees: number): number;
11
+ /**
12
+ * True for a number AutoCAD would misread from DXF, because it is 1e100 or
13
+ * more in size. `lint` refuses these wherever a drawing holds one.
14
+ */
15
+ export declare function isTooLarge(value: number): boolean;
16
+ /**
17
+ * Brings a number a writer works out for itself, such as an extent or the
18
+ * height of the view, into the range AutoCAD reads. `lint` cannot vouch for
19
+ * those: two coordinates under 1e100 can be more than 1e100 apart.
20
+ */
21
+ export declare function readable(value: number): number;
1
22
  /** Formats a coordinate with at most 6 decimals, no float noise, and no "-0". */
2
23
  export declare function fmt(value: number): string;
3
24
  /**
4
- * Escapes a string value for DXF. A caret starts a control-character escape in
5
- * DXF, and AutoCAD discards the drawing when it meets a bare one, so a literal
6
- * caret is written as a caret and a space.
25
+ * Escapes a string value for DXF, so that AutoCAD holds the string as given.
26
+ *
27
+ * A caret starts a control-character escape in DXF, and AutoCAD discards the
28
+ * drawing when it meets a bare one, so a literal caret is written as a caret
29
+ * and a space.
30
+ *
31
+ * AutoCAD decodes `\U+XXXX` and `\M+NXXXX` in a string, and it does so twice.
32
+ * A backslash that would start one is written as `\U+005CU+005C`: the first
33
+ * pass leaves `\U+005C` and the second leaves the backslash, with nothing left
34
+ * to decode what follows it. This lasts until AutoCAD saves the drawing:
35
+ * AutoCAD writes the text unescaped, so it decodes it on a later open.
7
36
  *
8
37
  * With `ascii`, characters outside ASCII become `\U+XXXX`, which is how DXF
9
38
  * versions before 2007 carry them. Later versions are UTF-8 and need no escape.
package/dist/format.js CHANGED
@@ -1,18 +1,72 @@
1
+ /** The number of decimals the writers keep. */
2
+ const DECIMALS = 6;
3
+ /**
4
+ * A number as the writers print it: rounded to 6 decimals. Two values that are
5
+ * equal here are the same number in the file, whatever they were in the drawing.
6
+ */
7
+ export function written(value) {
8
+ const rounded = Number(value.toFixed(DECIMALS));
9
+ return Object.is(rounded, -0) ? 0 : rounded;
10
+ }
11
+ /**
12
+ * An arc angle in degrees as the DXF writers print it: rounded to 6 decimals,
13
+ * with 0 in place of an angle that rounds up to 360.
14
+ */
15
+ export function writtenAngle(degrees) {
16
+ const rounded = written(degrees);
17
+ return rounded >= 360 ? rounded - 360 : rounded;
18
+ }
19
+ /**
20
+ * The size from which a number prints with three exponent digits, as `1e+100`.
21
+ * AutoCAD reads only two from DXF: it takes `1e+100` for `1e+10` and
22
+ * `1e+308` for `1e+30`, with no AUDIT error. Writing the digits out does not
23
+ * help, because AutoCAD takes a number of 101 digits for 0.
24
+ */
25
+ const TOO_LARGE = 1e100;
26
+ /** The largest number under `TOO_LARGE`. */
27
+ const LARGEST = 9.999999999999998e99;
28
+ /**
29
+ * True for a number AutoCAD would misread from DXF, because it is 1e100 or
30
+ * more in size. `lint` refuses these wherever a drawing holds one.
31
+ */
32
+ export function isTooLarge(value) {
33
+ return Math.abs(value) >= TOO_LARGE;
34
+ }
35
+ /**
36
+ * Brings a number a writer works out for itself, such as an extent or the
37
+ * height of the view, into the range AutoCAD reads. `lint` cannot vouch for
38
+ * those: two coordinates under 1e100 can be more than 1e100 apart.
39
+ */
40
+ export function readable(value) {
41
+ return Math.max(-LARGEST, Math.min(LARGEST, value));
42
+ }
1
43
  /** Formats a coordinate with at most 6 decimals, no float noise, and no "-0". */
2
44
  export function fmt(value) {
3
- const rounded = Number(value.toFixed(6));
4
- return Object.is(rounded, -0) ? "0" : String(rounded);
45
+ return String(written(value));
5
46
  }
6
47
  /**
7
- * Escapes a string value for DXF. A caret starts a control-character escape in
8
- * DXF, and AutoCAD discards the drawing when it meets a bare one, so a literal
9
- * caret is written as a caret and a space.
48
+ * A backslash that starts a `\U+XXXX` or `\M+NXXXX` sequence. AutoCAD decodes
49
+ * both forms in a DXF string, in either letter case.
50
+ */
51
+ const ESCAPE_START = /\\(?=[Uu]\+[0-9A-Fa-f]{4}|[Mm]\+[1-5][0-9A-Fa-f]{4})/g;
52
+ /**
53
+ * Escapes a string value for DXF, so that AutoCAD holds the string as given.
54
+ *
55
+ * A caret starts a control-character escape in DXF, and AutoCAD discards the
56
+ * drawing when it meets a bare one, so a literal caret is written as a caret
57
+ * and a space.
58
+ *
59
+ * AutoCAD decodes `\U+XXXX` and `\M+NXXXX` in a string, and it does so twice.
60
+ * A backslash that would start one is written as `\U+005CU+005C`: the first
61
+ * pass leaves `\U+005C` and the second leaves the backslash, with nothing left
62
+ * to decode what follows it. This lasts until AutoCAD saves the drawing:
63
+ * AutoCAD writes the text unescaped, so it decodes it on a later open.
10
64
  *
11
65
  * With `ascii`, characters outside ASCII become `\U+XXXX`, which is how DXF
12
66
  * versions before 2007 carry them. Later versions are UTF-8 and need no escape.
13
67
  */
14
68
  export function dxfText(value, ascii = false) {
15
- const escaped = value.replaceAll("^", "^ ");
69
+ const escaped = value.replace(ESCAPE_START, "\\U+005CU+005C").replaceAll("^", "^ ");
16
70
  if (!ascii)
17
71
  return escaped;
18
72
  let out = "";
@@ -0,0 +1,55 @@
1
+ import type { Layer } from "./drawing.ts";
2
+ /** C1 controls and invisible formatting characters, such as the zero-width space. */
3
+ export declare const REJECTED_FORMAT = "\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008D\u008E\u008F\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009D\u009E\u009F\u0600\u0601\u0602\u0603\u06DD\u070F\u17B4\u17B5\u200B\u200C\u200D\u200E\u200F\u202A\u202B\u202C\u202D\u202E\u2060\u2061\u2062\u2063\u2064\u206A\u206B\u206C\u206D\u206E\u206F\uFEFF\uFFF9\uFFFA\uFFFB";
4
+ /** Superscript and subscript letters, Roman numeral characters and letterlike symbols such as the ohm sign. */
5
+ export declare const REJECTED_LETTERLIKE = "\u2071\u207F\u2090\u2091\u2092\u2093\u2094\u2102\u2107\u210A\u210B\u210C\u210D\u210E\u210F\u2110\u2111\u2112\u2113\u2115\u2119\u211A\u211B\u211C\u211D\u2124\u2126\u2128\u212A\u212B\u212C\u212D\u212F\u2130\u2131\u2132\u2133\u2134\u2135\u2136\u2137\u2138\u2139\u213C\u213D\u213E\u213F\u2145\u2146\u2147\u2148\u2149\u214E\u2180\u2181\u2182\u2183\u2184\u2185\u2186\u2187\u2188";
6
+ /** Glagolitic letters and the Latin Extended-C block. */
7
+ export declare const REJECTED_GLAGOLITIC = "\u2C00\u2C01\u2C02\u2C03\u2C04\u2C05\u2C06\u2C07\u2C08\u2C09\u2C0A\u2C0B\u2C0C\u2C0D\u2C0E\u2C0F\u2C10\u2C11\u2C12\u2C13\u2C14\u2C15\u2C16\u2C17\u2C18\u2C19\u2C1A\u2C1B\u2C1C\u2C1D\u2C1E\u2C1F\u2C20\u2C21\u2C22\u2C23\u2C24\u2C25\u2C26\u2C27\u2C28\u2C29\u2C2A\u2C2B\u2C2C\u2C2D\u2C2E\u2C30\u2C31\u2C32\u2C33\u2C34\u2C35\u2C36\u2C37\u2C38\u2C39\u2C3A\u2C3B\u2C3C\u2C3D\u2C3E\u2C3F\u2C40\u2C41\u2C42\u2C43\u2C44\u2C45\u2C46\u2C47\u2C48\u2C49\u2C4A\u2C4B\u2C4C\u2C4D\u2C4E\u2C4F\u2C50\u2C51\u2C52\u2C53\u2C54\u2C55\u2C56\u2C57\u2C58\u2C59\u2C5A\u2C5B\u2C5C\u2C5D\u2C5E\u2C60\u2C61\u2C62\u2C63\u2C64\u2C65\u2C66\u2C67\u2C68\u2C69\u2C6A\u2C6B\u2C6C\u2C6D\u2C6E\u2C6F\u2C71\u2C72\u2C73\u2C74\u2C75\u2C76\u2C77\u2C78\u2C79\u2C7A\u2C7B\u2C7C\u2C7D";
8
+ /** Coptic letters. */
9
+ export declare const REJECTED_COPTIC = "\u2C80\u2C81\u2C82\u2C83\u2C84\u2C85\u2C86\u2C87\u2C88\u2C89\u2C8A\u2C8B\u2C8C\u2C8D\u2C8E\u2C8F\u2C90\u2C91\u2C92\u2C93\u2C94\u2C95\u2C96\u2C97\u2C98\u2C99\u2C9A\u2C9B\u2C9C\u2C9D\u2C9E\u2C9F\u2CA0\u2CA1\u2CA2\u2CA3\u2CA4\u2CA5\u2CA6\u2CA7\u2CA8\u2CA9\u2CAA\u2CAB\u2CAC\u2CAD\u2CAE\u2CAF\u2CB0\u2CB1\u2CB2\u2CB3\u2CB4\u2CB5\u2CB6\u2CB7\u2CB8\u2CB9\u2CBA\u2CBB\u2CBC\u2CBD\u2CBE\u2CBF\u2CC0\u2CC1\u2CC2\u2CC3\u2CC4\u2CC5\u2CC6\u2CC7\u2CC8\u2CC9\u2CCA\u2CCB\u2CCC\u2CCD\u2CCE\u2CCF\u2CD0\u2CD1\u2CD2\u2CD3\u2CD4\u2CD5\u2CD6\u2CD7\u2CD8\u2CD9\u2CDA\u2CDB\u2CDC\u2CDD\u2CDE\u2CDF\u2CE0\u2CE1\u2CE2\u2CE3\u2CE4";
10
+ /** Georgian Supplement, Tifinagh and Ethiopic Extended letters. */
11
+ export declare const REJECTED_GEORGIAN = "\u2D00\u2D01\u2D02\u2D03\u2D04\u2D05\u2D06\u2D07\u2D08\u2D09\u2D0A\u2D0B\u2D0C\u2D0D\u2D0E\u2D0F\u2D10\u2D11\u2D12\u2D13\u2D14\u2D15\u2D16\u2D17\u2D18\u2D19\u2D1A\u2D1B\u2D1C\u2D1D\u2D1E\u2D1F\u2D20\u2D21\u2D22\u2D23\u2D24\u2D25\u2D30\u2D31\u2D32\u2D33\u2D34\u2D35\u2D36\u2D37\u2D38\u2D39\u2D3A\u2D3B\u2D3C\u2D3D\u2D3E\u2D3F\u2D40\u2D41\u2D42\u2D43\u2D44\u2D45\u2D46\u2D47\u2D48\u2D49\u2D4A\u2D4B\u2D4C\u2D4D\u2D4E\u2D4F\u2D50\u2D51\u2D52\u2D53\u2D54\u2D55\u2D56\u2D57\u2D58\u2D59\u2D5A\u2D5B\u2D5C\u2D5D\u2D5E\u2D5F\u2D60\u2D61\u2D62\u2D63\u2D64\u2D65\u2D6F\u2D80\u2D81\u2D82\u2D83\u2D84\u2D85\u2D86\u2D87\u2D88\u2D89\u2D8A\u2D8B\u2D8C\u2D8D\u2D8E\u2D8F\u2D90\u2D91\u2D92\u2D93\u2D94\u2D95\u2D96\u2DA0\u2DA1\u2DA2\u2DA3\u2DA4\u2DA5\u2DA6\u2DA8\u2DA9\u2DAA\u2DAB\u2DAC\u2DAD\u2DAE\u2DB0\u2DB1\u2DB2\u2DB3\u2DB4\u2DB5\u2DB6\u2DB8\u2DB9\u2DBA\u2DBB\u2DBC\u2DBD\u2DBE\u2DC0\u2DC1\u2DC2\u2DC3\u2DC4\u2DC5\u2DC6\u2DC8\u2DC9\u2DCA\u2DCB\u2DCC\u2DCD\u2DCE\u2DD0\u2DD1\u2DD2\u2DD3\u2DD4\u2DD5\u2DD6\u2DD8\u2DD9\u2DDA\u2DDB\u2DDC\u2DDD\u2DDE";
12
+ /** The private-use characters U+E800 to U+E8FF. */
13
+ export declare const REJECTED_PRIVATE = "\uE800\uE801\uE802\uE803\uE804\uE805\uE806\uE807\uE808\uE809\uE80A\uE80B\uE80C\uE80D\uE80E\uE80F\uE810\uE811\uE812\uE813\uE814\uE815\uE816\uE817\uE818\uE819\uE81A\uE81B\uE81C\uE81D\uE81E\uE81F\uE820\uE821\uE822\uE823\uE824\uE825\uE826\uE827\uE828\uE829\uE82A\uE82B\uE82C\uE82D\uE82E\uE82F\uE830\uE831\uE832\uE833\uE834\uE835\uE836\uE837\uE838\uE839\uE83A\uE83B\uE83C\uE83D\uE83E\uE83F\uE840\uE841\uE842\uE843\uE844\uE845\uE846\uE847\uE848\uE849\uE84A\uE84B\uE84C\uE84D\uE84E\uE84F\uE850\uE851\uE852\uE853\uE854\uE855\uE856\uE857\uE858\uE859\uE85A\uE85B\uE85C\uE85D\uE85E\uE85F\uE860\uE861\uE862\uE863\uE864\uE865\uE866\uE867\uE868\uE869\uE86A\uE86B\uE86C\uE86D\uE86E\uE86F\uE870\uE871\uE872\uE873\uE874\uE875\uE876\uE877\uE878\uE879\uE87A\uE87B\uE87C\uE87D\uE87E\uE87F\uE880\uE881\uE882\uE883\uE884\uE885\uE886\uE887\uE888\uE889\uE88A\uE88B\uE88C\uE88D\uE88E\uE88F\uE890\uE891\uE892\uE893\uE894\uE895\uE896\uE897\uE898\uE899\uE89A\uE89B\uE89C\uE89D\uE89E\uE89F\uE8A0\uE8A1\uE8A2\uE8A3\uE8A4\uE8A5\uE8A6\uE8A7\uE8A8\uE8A9\uE8AA\uE8AB\uE8AC\uE8AD\uE8AE\uE8AF\uE8B0\uE8B1\uE8B2\uE8B3\uE8B4\uE8B5\uE8B6\uE8B7\uE8B8\uE8B9\uE8BA\uE8BB\uE8BC\uE8BD\uE8BE\uE8BF\uE8C0\uE8C1\uE8C2\uE8C3\uE8C4\uE8C5\uE8C6\uE8C7\uE8C8\uE8C9\uE8CA\uE8CB\uE8CC\uE8CD\uE8CE\uE8CF\uE8D0\uE8D1\uE8D2\uE8D3\uE8D4\uE8D5\uE8D6\uE8D7\uE8D8\uE8D9\uE8DA\uE8DB\uE8DC\uE8DD\uE8DE\uE8DF\uE8E0\uE8E1\uE8E2\uE8E3\uE8E4\uE8E5\uE8E6\uE8E7\uE8E8\uE8E9\uE8EA\uE8EB\uE8EC\uE8ED\uE8EE\uE8EF\uE8F0\uE8F1\uE8F2\uE8F3\uE8F4\uE8F5\uE8F6\uE8F7\uE8F8\uE8F9\uE8FA\uE8FB\uE8FC\uE8FD\uE8FE\uE8FF";
14
+ /**
15
+ * Finds the first character of a layer name that AutoCAD rejects, beyond the
16
+ * ASCII punctuation and control characters that `layerNameProblem` checks.
17
+ *
18
+ * @param name - The layer name.
19
+ * @returns The character, or undefined when the name has none.
20
+ */
21
+ export declare function rejectedNameCharacter(name: string): string | undefined;
22
+ /**
23
+ * The form of a layer name that AutoCAD looks layers up by. Two names with the
24
+ * same key are one layer to AutoCAD: an entity on `wall` goes on a layer
25
+ * declared as `WALL`, and AutoCAD keeps only the first of two such layers.
26
+ *
27
+ * AutoCAD lower-cases one character at a time from its own table, so this is
28
+ * not `toLowerCase()`: a final sigma ς, a dotless ı and ß stay as they are.
29
+ *
30
+ * @param name - The layer name.
31
+ * @returns The key to compare layer names by.
32
+ */
33
+ export declare function layerKey(name: string): string;
34
+ /**
35
+ * The form of a layer name that AutoCAD's AUDIT compares when it looks for
36
+ * duplicate names. AUDIT takes a few more letters for the same than the layer
37
+ * table does, so two layers can have different `layerKey`s and still be an
38
+ * AUDIT error.
39
+ *
40
+ * This covers the single letters AUDIT adds, and not everything it reports:
41
+ * it also calls `straße` and `strasse`, `æ` and `ae`, and a composed é and its
42
+ * decomposed form duplicates, which this key keeps apart.
43
+ *
44
+ * @param name - The layer name.
45
+ * @returns The key to find AUDIT's duplicate names by.
46
+ */
47
+ export declare function layerAuditKey(name: string): string;
48
+ /**
49
+ * Orders layers for a DXF layer table. AutoCAD's AUDIT reports "Layer zero name
50
+ * found at incorrect index" when layer `0` is not the first record.
51
+ *
52
+ * @param layers - The drawing's layers, in any order.
53
+ * @returns The layers with `0` first and the others in their given order.
54
+ */
55
+ export declare function layerZeroFirst(layers: readonly Layer[]): readonly Layer[];