tsdraft 0.1.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/dist/format.d.ts CHANGED
@@ -1,2 +1,40 @@
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;
24
+ /**
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.
36
+ *
37
+ * With `ascii`, characters outside ASCII become `\U+XXXX`, which is how DXF
38
+ * versions before 2007 carry them. Later versions are UTF-8 and need no escape.
39
+ */
40
+ export declare function dxfText(value: string, ascii?: boolean): string;
package/dist/format.js CHANGED
@@ -1,5 +1,78 @@
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));
46
+ }
47
+ /**
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.
64
+ *
65
+ * With `ascii`, characters outside ASCII become `\U+XXXX`, which is how DXF
66
+ * versions before 2007 carry them. Later versions are UTF-8 and need no escape.
67
+ */
68
+ export function dxfText(value, ascii = false) {
69
+ const escaped = value.replace(ESCAPE_START, "\\U+005CU+005C").replaceAll("^", "^ ");
70
+ if (!ascii)
71
+ return escaped;
72
+ let out = "";
73
+ for (let i = 0; i < escaped.length; i++) {
74
+ const code = escaped.charCodeAt(i);
75
+ out += code < 0x80 ? escaped[i] : `\\U+${code.toString(16).toUpperCase().padStart(4, "0")}`;
76
+ }
77
+ return out;
5
78
  }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
- export { createDrawing, DrawingBuilder, sweep, type Arc, type Circle, type Drawing, type DrawingOptions, type Entity, type Layer, type LayerOptions, type Line, type Point, type Polyline, type Text, type TextOptions, type Units, } from "./drawing.ts";
2
- export { toDXF } from "./dxf.ts";
1
+ export { createDrawing, DrawingBuilder, sweep, type Arc, type Circle, type Drawing, type DrawingOptions, type DxfVersion, type Entity, type Layer, type LayerOptions, type Line, type Point, type Polyline, type Text, type TextOptions, type Units, } from "./drawing.ts";
2
+ export { toDXF, type DxfOptions } from "./dxf.ts";
3
+ export { DrawingError, lint, type Diagnostic, type LintOptions, type LintRule } from "./lint.ts";
4
+ export type { Aci, LayerColor, LayerName, Positive } from "./rules.ts";
3
5
  export { toSVG, type SvgOptions } from "./svg.ts";
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { createDrawing, DrawingBuilder, sweep, } from "./drawing.js";
2
2
  export { toDXF } from "./dxf.js";
3
+ export { DrawingError, lint } from "./lint.js";
3
4
  export { toSVG } from "./svg.js";
@@ -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[];
package/dist/layers.js ADDED
@@ -0,0 +1,178 @@
1
+ // AutoCAD discards a drawing whose layer table has a name with one of the
2
+ // characters below, as it does for the ASCII punctuation `rules.ts` lists. The
3
+ // exception is the private-use block, which it replaces with `\M+00000`. They
4
+ // are string literals, with every character written as an escape, so that
5
+ // `rules.ts` can also reject them in a layer name written as a literal.
6
+ /** C1 controls and invisible formatting characters, such as the zero-width space. */
7
+ export const REJECTED_FORMAT = "\
8
+ \u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008a\u008b\u008c\u008d\u008e\
9
+ \u008f\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009a\u009b\u009c\u009d\
10
+ \u009e\u009f\u0600\u0601\u0602\u0603\u06dd\u070f\u17b4\u17b5\u200b\u200c\u200d\u200e\u200f\
11
+ \u202a\u202b\u202c\u202d\u202e\u2060\u2061\u2062\u2063\u2064\u206a\u206b\u206c\u206d\u206e\
12
+ \u206f\ufeff\ufff9\ufffa\ufffb";
13
+ /** Superscript and subscript letters, Roman numeral characters and letterlike symbols such as the ohm sign. */
14
+ export const REJECTED_LETTERLIKE = "\
15
+ \u2071\u207f\u2090\u2091\u2092\u2093\u2094\u2102\u2107\u210a\u210b\u210c\u210d\u210e\u210f\
16
+ \u2110\u2111\u2112\u2113\u2115\u2119\u211a\u211b\u211c\u211d\u2124\u2126\u2128\u212a\u212b\
17
+ \u212c\u212d\u212f\u2130\u2131\u2132\u2133\u2134\u2135\u2136\u2137\u2138\u2139\u213c\u213d\
18
+ \u213e\u213f\u2145\u2146\u2147\u2148\u2149\u214e\u2180\u2181\u2182\u2183\u2184\u2185\u2186\
19
+ \u2187\u2188";
20
+ /** Glagolitic letters and the Latin Extended-C block. */
21
+ export const REJECTED_GLAGOLITIC = "\
22
+ \u2c00\u2c01\u2c02\u2c03\u2c04\u2c05\u2c06\u2c07\u2c08\u2c09\u2c0a\u2c0b\u2c0c\u2c0d\u2c0e\
23
+ \u2c0f\u2c10\u2c11\u2c12\u2c13\u2c14\u2c15\u2c16\u2c17\u2c18\u2c19\u2c1a\u2c1b\u2c1c\u2c1d\
24
+ \u2c1e\u2c1f\u2c20\u2c21\u2c22\u2c23\u2c24\u2c25\u2c26\u2c27\u2c28\u2c29\u2c2a\u2c2b\u2c2c\
25
+ \u2c2d\u2c2e\u2c30\u2c31\u2c32\u2c33\u2c34\u2c35\u2c36\u2c37\u2c38\u2c39\u2c3a\u2c3b\u2c3c\
26
+ \u2c3d\u2c3e\u2c3f\u2c40\u2c41\u2c42\u2c43\u2c44\u2c45\u2c46\u2c47\u2c48\u2c49\u2c4a\u2c4b\
27
+ \u2c4c\u2c4d\u2c4e\u2c4f\u2c50\u2c51\u2c52\u2c53\u2c54\u2c55\u2c56\u2c57\u2c58\u2c59\u2c5a\
28
+ \u2c5b\u2c5c\u2c5d\u2c5e\u2c60\u2c61\u2c62\u2c63\u2c64\u2c65\u2c66\u2c67\u2c68\u2c69\u2c6a\
29
+ \u2c6b\u2c6c\u2c6d\u2c6e\u2c6f\u2c71\u2c72\u2c73\u2c74\u2c75\u2c76\u2c77\u2c78\u2c79\u2c7a\
30
+ \u2c7b\u2c7c\u2c7d";
31
+ /** Coptic letters. */
32
+ export const REJECTED_COPTIC = "\
33
+ \u2c80\u2c81\u2c82\u2c83\u2c84\u2c85\u2c86\u2c87\u2c88\u2c89\u2c8a\u2c8b\u2c8c\u2c8d\u2c8e\
34
+ \u2c8f\u2c90\u2c91\u2c92\u2c93\u2c94\u2c95\u2c96\u2c97\u2c98\u2c99\u2c9a\u2c9b\u2c9c\u2c9d\
35
+ \u2c9e\u2c9f\u2ca0\u2ca1\u2ca2\u2ca3\u2ca4\u2ca5\u2ca6\u2ca7\u2ca8\u2ca9\u2caa\u2cab\u2cac\
36
+ \u2cad\u2cae\u2caf\u2cb0\u2cb1\u2cb2\u2cb3\u2cb4\u2cb5\u2cb6\u2cb7\u2cb8\u2cb9\u2cba\u2cbb\
37
+ \u2cbc\u2cbd\u2cbe\u2cbf\u2cc0\u2cc1\u2cc2\u2cc3\u2cc4\u2cc5\u2cc6\u2cc7\u2cc8\u2cc9\u2cca\
38
+ \u2ccb\u2ccc\u2ccd\u2cce\u2ccf\u2cd0\u2cd1\u2cd2\u2cd3\u2cd4\u2cd5\u2cd6\u2cd7\u2cd8\u2cd9\
39
+ \u2cda\u2cdb\u2cdc\u2cdd\u2cde\u2cdf\u2ce0\u2ce1\u2ce2\u2ce3\u2ce4";
40
+ /** Georgian Supplement, Tifinagh and Ethiopic Extended letters. */
41
+ export const REJECTED_GEORGIAN = "\
42
+ \u2d00\u2d01\u2d02\u2d03\u2d04\u2d05\u2d06\u2d07\u2d08\u2d09\u2d0a\u2d0b\u2d0c\u2d0d\u2d0e\
43
+ \u2d0f\u2d10\u2d11\u2d12\u2d13\u2d14\u2d15\u2d16\u2d17\u2d18\u2d19\u2d1a\u2d1b\u2d1c\u2d1d\
44
+ \u2d1e\u2d1f\u2d20\u2d21\u2d22\u2d23\u2d24\u2d25\u2d30\u2d31\u2d32\u2d33\u2d34\u2d35\u2d36\
45
+ \u2d37\u2d38\u2d39\u2d3a\u2d3b\u2d3c\u2d3d\u2d3e\u2d3f\u2d40\u2d41\u2d42\u2d43\u2d44\u2d45\
46
+ \u2d46\u2d47\u2d48\u2d49\u2d4a\u2d4b\u2d4c\u2d4d\u2d4e\u2d4f\u2d50\u2d51\u2d52\u2d53\u2d54\
47
+ \u2d55\u2d56\u2d57\u2d58\u2d59\u2d5a\u2d5b\u2d5c\u2d5d\u2d5e\u2d5f\u2d60\u2d61\u2d62\u2d63\
48
+ \u2d64\u2d65\u2d6f\u2d80\u2d81\u2d82\u2d83\u2d84\u2d85\u2d86\u2d87\u2d88\u2d89\u2d8a\u2d8b\
49
+ \u2d8c\u2d8d\u2d8e\u2d8f\u2d90\u2d91\u2d92\u2d93\u2d94\u2d95\u2d96\u2da0\u2da1\u2da2\u2da3\
50
+ \u2da4\u2da5\u2da6\u2da8\u2da9\u2daa\u2dab\u2dac\u2dad\u2dae\u2db0\u2db1\u2db2\u2db3\u2db4\
51
+ \u2db5\u2db6\u2db8\u2db9\u2dba\u2dbb\u2dbc\u2dbd\u2dbe\u2dc0\u2dc1\u2dc2\u2dc3\u2dc4\u2dc5\
52
+ \u2dc6\u2dc8\u2dc9\u2dca\u2dcb\u2dcc\u2dcd\u2dce\u2dd0\u2dd1\u2dd2\u2dd3\u2dd4\u2dd5\u2dd6\
53
+ \u2dd8\u2dd9\u2dda\u2ddb\u2ddc\u2ddd\u2dde";
54
+ /** The private-use characters U+E800 to U+E8FF. */
55
+ export const REJECTED_PRIVATE = "\
56
+ \ue800\ue801\ue802\ue803\ue804\ue805\ue806\ue807\ue808\ue809\ue80a\ue80b\ue80c\ue80d\ue80e\
57
+ \ue80f\ue810\ue811\ue812\ue813\ue814\ue815\ue816\ue817\ue818\ue819\ue81a\ue81b\ue81c\ue81d\
58
+ \ue81e\ue81f\ue820\ue821\ue822\ue823\ue824\ue825\ue826\ue827\ue828\ue829\ue82a\ue82b\ue82c\
59
+ \ue82d\ue82e\ue82f\ue830\ue831\ue832\ue833\ue834\ue835\ue836\ue837\ue838\ue839\ue83a\ue83b\
60
+ \ue83c\ue83d\ue83e\ue83f\ue840\ue841\ue842\ue843\ue844\ue845\ue846\ue847\ue848\ue849\ue84a\
61
+ \ue84b\ue84c\ue84d\ue84e\ue84f\ue850\ue851\ue852\ue853\ue854\ue855\ue856\ue857\ue858\ue859\
62
+ \ue85a\ue85b\ue85c\ue85d\ue85e\ue85f\ue860\ue861\ue862\ue863\ue864\ue865\ue866\ue867\ue868\
63
+ \ue869\ue86a\ue86b\ue86c\ue86d\ue86e\ue86f\ue870\ue871\ue872\ue873\ue874\ue875\ue876\ue877\
64
+ \ue878\ue879\ue87a\ue87b\ue87c\ue87d\ue87e\ue87f\ue880\ue881\ue882\ue883\ue884\ue885\ue886\
65
+ \ue887\ue888\ue889\ue88a\ue88b\ue88c\ue88d\ue88e\ue88f\ue890\ue891\ue892\ue893\ue894\ue895\
66
+ \ue896\ue897\ue898\ue899\ue89a\ue89b\ue89c\ue89d\ue89e\ue89f\ue8a0\ue8a1\ue8a2\ue8a3\ue8a4\
67
+ \ue8a5\ue8a6\ue8a7\ue8a8\ue8a9\ue8aa\ue8ab\ue8ac\ue8ad\ue8ae\ue8af\ue8b0\ue8b1\ue8b2\ue8b3\
68
+ \ue8b4\ue8b5\ue8b6\ue8b7\ue8b8\ue8b9\ue8ba\ue8bb\ue8bc\ue8bd\ue8be\ue8bf\ue8c0\ue8c1\ue8c2\
69
+ \ue8c3\ue8c4\ue8c5\ue8c6\ue8c7\ue8c8\ue8c9\ue8ca\ue8cb\ue8cc\ue8cd\ue8ce\ue8cf\ue8d0\ue8d1\
70
+ \ue8d2\ue8d3\ue8d4\ue8d5\ue8d6\ue8d7\ue8d8\ue8d9\ue8da\ue8db\ue8dc\ue8dd\ue8de\ue8df\ue8e0\
71
+ \ue8e1\ue8e2\ue8e3\ue8e4\ue8e5\ue8e6\ue8e7\ue8e8\ue8e9\ue8ea\ue8eb\ue8ec\ue8ed\ue8ee\ue8ef\
72
+ \ue8f0\ue8f1\ue8f2\ue8f3\ue8f4\ue8f5\ue8f6\ue8f7\ue8f8\ue8f9\ue8fa\ue8fb\ue8fc\ue8fd\ue8fe\
73
+ \ue8ff";
74
+ const REJECTED = new Set(REJECTED_FORMAT +
75
+ REJECTED_LETTERLIKE +
76
+ REJECTED_GLAGOLITIC +
77
+ REJECTED_COPTIC +
78
+ REJECTED_GEORGIAN +
79
+ REJECTED_PRIVATE);
80
+ /**
81
+ * Finds the first character of a layer name that AutoCAD rejects, beyond the
82
+ * ASCII punctuation and control characters that `layerNameProblem` checks.
83
+ *
84
+ * @param name - The layer name.
85
+ * @returns The character, or undefined when the name has none.
86
+ */
87
+ export function rejectedNameCharacter(name) {
88
+ for (const character of name)
89
+ if (REJECTED.has(character))
90
+ return character;
91
+ return undefined;
92
+ }
93
+ /**
94
+ * The capital letters AutoCAD lower-cases when it looks a layer name up, as
95
+ * hexadecimal code point ranges. `/2` marks a range that takes every second
96
+ * code point. AutoCAD's table is older and smaller than Unicode's: it leaves
97
+ * out Ÿ (U+0178), İ (U+0130) and ϴ (U+03F4), the title-case digraphs such as
98
+ * Dž (U+01C5), and most letters added since Unicode 5.1.
99
+ */
100
+ const FOLDED_RANGES = "41-5a c0-d6 d8-de 100-12e/2 132-136/2 139-147/2 14a-176/2 179-17d/2 181-182 184-186/2 " +
101
+ "187-189/2 18a-18b 18e-191 193-194 196-198 19c-19d 19f-1a0 1a2-1a6/2 1a7-1a9/2 1ac-1ae/2 " +
102
+ "1af-1b1/2 1b2-1b3 1b5-1b7/2 1b8 1bc 1c4 1c7 1ca 1cd-1db/2 1de-1ee/2 1f1 1f4-1f6/2 " +
103
+ "1f7-1f8 1fa-232/2 23b-23d/2 241-243/2 244-246 248-24e/2 370-372/2 376 386-388/2 389-38a " +
104
+ "38c-38e/2 38f-391/2 392-3a1 3a3-3ab 3cf 3d8-3ee/2 3f7-3f9/2 3fa 3fd-42f 460-480/2 " +
105
+ "48a-4c0/2 4c1-4cd/2 4d0-522/2 531-556 1e00-1e94/2 1ea0-1efe/2 1f08-1f0f 1f18-1f1d " +
106
+ "1f28-1f2f 1f38-1f3f 1f48-1f4d 1f59-1f5f/2 1f68-1f6f 1f88-1f8f 1f98-1f9f 1fa8-1faf " +
107
+ "1fb8-1fbc 1fc8-1fcc 1fd8-1fdb 1fe8-1fec 1ff8-1ffc 2160-216f 24b6-24cf a640-a65e/2 " +
108
+ "a662-a66c/2 a680-a696/2 a722-a72e/2 a732-a76e/2 a779-a77d/2 a77e-a786/2 a78b ff21-ff3a";
109
+ const FOLDED = new Set(FOLDED_RANGES.split(" ").flatMap((range) => {
110
+ const [span = "", step = "1"] = range.split("/");
111
+ const [first = "", last = first] = span.split("-");
112
+ const codes = [];
113
+ for (let code = parseInt(first, 16); code <= parseInt(last, 16); code += Number(step)) {
114
+ codes.push(code);
115
+ }
116
+ return codes;
117
+ }));
118
+ /**
119
+ * Pairs of letters that AutoCAD keeps as two layers but its AUDIT reports as a
120
+ * duplicate name, written `from>to` in hexadecimal after `layerKey` has
121
+ * lower-cased the name. They include ÿ and Ÿ, the two lower-case sigmas σ and
122
+ * ς, and ß and ẞ.
123
+ */
124
+ const AUDIT_SAME_PAIRS = "178>ff 1c6>1c5 1c9>1c8 1cc>1cb 1f3>1f2 3c3>3c2 3f5>3b5 527>526 1e9e>df 1fbe>3b9 " +
125
+ "2c7e>23f 2c7f>240 2cec>2ceb 2cee>2ced a791>a790 a7a1>a7a0 a7a3>a7a2 a7a5>a7a4 " +
126
+ "a7a7>a7a6 a7a9>a7a8";
127
+ const AUDIT_SAME = new Map(AUDIT_SAME_PAIRS.split(" ").map((pair) => {
128
+ const [from = "", to = ""] = pair.split(">");
129
+ return [String.fromCharCode(parseInt(from, 16)), String.fromCharCode(parseInt(to, 16))];
130
+ }));
131
+ /**
132
+ * The form of a layer name that AutoCAD looks layers up by. Two names with the
133
+ * same key are one layer to AutoCAD: an entity on `wall` goes on a layer
134
+ * declared as `WALL`, and AutoCAD keeps only the first of two such layers.
135
+ *
136
+ * AutoCAD lower-cases one character at a time from its own table, so this is
137
+ * not `toLowerCase()`: a final sigma ς, a dotless ı and ß stay as they are.
138
+ *
139
+ * @param name - The layer name.
140
+ * @returns The key to compare layer names by.
141
+ */
142
+ export function layerKey(name) {
143
+ let key = "";
144
+ for (let i = 0; i < name.length; i++) {
145
+ const character = name.charAt(i);
146
+ key += FOLDED.has(name.charCodeAt(i)) ? character.toLowerCase() : character;
147
+ }
148
+ return key;
149
+ }
150
+ /**
151
+ * The form of a layer name that AutoCAD's AUDIT compares when it looks for
152
+ * duplicate names. AUDIT takes a few more letters for the same than the layer
153
+ * table does, so two layers can have different `layerKey`s and still be an
154
+ * AUDIT error.
155
+ *
156
+ * This covers the single letters AUDIT adds, and not everything it reports:
157
+ * it also calls `straße` and `strasse`, `æ` and `ae`, and a composed é and its
158
+ * decomposed form duplicates, which this key keeps apart.
159
+ *
160
+ * @param name - The layer name.
161
+ * @returns The key to find AUDIT's duplicate names by.
162
+ */
163
+ export function layerAuditKey(name) {
164
+ let key = "";
165
+ for (const character of layerKey(name))
166
+ key += AUDIT_SAME.get(character) ?? character;
167
+ return key;
168
+ }
169
+ /**
170
+ * Orders layers for a DXF layer table. AutoCAD's AUDIT reports "Layer zero name
171
+ * found at incorrect index" when layer `0` is not the first record.
172
+ *
173
+ * @param layers - The drawing's layers, in any order.
174
+ * @returns The layers with `0` first and the others in their given order.
175
+ */
176
+ export function layerZeroFirst(layers) {
177
+ return [...layers.filter((l) => l.name === "0"), ...layers.filter((l) => l.name !== "0")];
178
+ }
package/dist/lint.d.ts ADDED
@@ -0,0 +1,39 @@
1
+ import type { Drawing, DxfVersion } from "./drawing.ts";
2
+ export type LintRule = "units" | "layer-name" | "duplicate-layer" | "layer-color" | "undefined-layer" | "geometry" | "text" | "zero-length";
3
+ export interface Diagnostic {
4
+ readonly rule: LintRule;
5
+ /** An error means AutoCAD rejects, repairs or garbles the output. `toDXF` refuses to write it. */
6
+ readonly severity: "error" | "warning";
7
+ readonly message: string;
8
+ /** The layer the problem is in, when it is a layer problem. */
9
+ readonly layer?: string;
10
+ /** Index into `drawing.entities`, when it is an entity problem. */
11
+ readonly entity?: number;
12
+ }
13
+ export interface LintOptions {
14
+ /** The DXF version to check against. Defaults to the drawing's own. */
15
+ readonly version?: DxfVersion;
16
+ }
17
+ /**
18
+ * Checks a drawing against the rules AutoCAD enforces when it opens DXF.
19
+ *
20
+ * A drawing made with `createDrawing()` already passes the error rules for its
21
+ * own DXF version, because the builder throws at the offending call. `lint`
22
+ * matters when a `Drawing` was built by hand or by other code, when it is
23
+ * exported as a different version than it was built for, and for warnings.
24
+ */
25
+ export declare function lint(drawing: Drawing, options?: LintOptions): Diagnostic[];
26
+ /**
27
+ * An error that refuses a drawing because of what `lint` found in it. `toDXF`
28
+ * throws it when a drawing has lint errors for the version being written, and
29
+ * other code can throw it to refuse a drawing for its own purpose. Its message
30
+ * lists the first 10 problems and names no file format.
31
+ */
32
+ export declare class DrawingError extends Error {
33
+ /** Every problem the drawing was refused for, including those the message leaves out. */
34
+ readonly diagnostics: readonly Diagnostic[];
35
+ /**
36
+ * @param diagnostics - The problems to refuse the drawing for, as `lint` returns them.
37
+ */
38
+ constructor(diagnostics: readonly Diagnostic[]);
39
+ }
package/dist/lint.js ADDED
@@ -0,0 +1,188 @@
1
+ import { written } from "./format.js";
2
+ import { layerAuditKey, layerKey } from "./layers.js";
3
+ import { arcIsWrittenAsPoint, arcProblem, layerColorProblem, layerNameProblem, layerTwinProblem, magnitudeProblem, sizeProblem, textProblem, textWarning, } from "./rules.js";
4
+ /**
5
+ * Checks a drawing against the rules AutoCAD enforces when it opens DXF.
6
+ *
7
+ * A drawing made with `createDrawing()` already passes the error rules for its
8
+ * own DXF version, because the builder throws at the offending call. `lint`
9
+ * matters when a `Drawing` was built by hand or by other code, when it is
10
+ * exported as a different version than it was built for, and for warnings.
11
+ */
12
+ export function lint(drawing, options = {}) {
13
+ const version = options.version ?? drawing.dxf;
14
+ const out = [];
15
+ if (!["in", "ft", "mm", "cm", "m"].includes(drawing.units)) {
16
+ out.push({
17
+ rule: "units",
18
+ severity: "error",
19
+ message: `Unknown units: ${JSON.stringify(drawing.units)}`,
20
+ });
21
+ }
22
+ // AutoCAD looks layers up by one form of their names, and AUDIT compares another.
23
+ const seen = new Set();
24
+ const audited = new Map();
25
+ for (const layer of drawing.layers) {
26
+ const error = (rule, message) => out.push({ rule, severity: "error", message, layer: layer.name });
27
+ const nameProblem = layerNameProblem(layer.name, version);
28
+ if (nameProblem)
29
+ error("layer-name", `Layer ${JSON.stringify(layer.name)}: ${nameProblem}`);
30
+ const colorProblem = layerColorProblem(layer.color);
31
+ if (colorProblem)
32
+ error("layer-color", `Layer ${JSON.stringify(layer.name)}: ${colorProblem}`);
33
+ const key = layerKey(layer.name);
34
+ const auditKey = layerAuditKey(layer.name);
35
+ const twinProblem = layerTwinProblem(layer.name, audited.get(auditKey) ?? layer.name);
36
+ if (seen.has(key)) {
37
+ error("duplicate-layer", `Layer ${JSON.stringify(layer.name)} is defined twice. CAD ignores case in layer names`);
38
+ }
39
+ else if (twinProblem) {
40
+ error("duplicate-layer", `Layer ${JSON.stringify(layer.name)}: ${twinProblem}`);
41
+ }
42
+ seen.add(key);
43
+ if (!audited.has(auditKey))
44
+ audited.set(auditKey, layer.name);
45
+ }
46
+ if (!seen.has("0")) {
47
+ out.push({ rule: "undefined-layer", severity: "error", message: 'Layer "0" is missing' });
48
+ }
49
+ drawing.entities.forEach((entity, index) => {
50
+ const add = (rule, severity, problem) => out.push({ rule, severity, message: `${entity.kind} #${index}: ${problem}`, entity: index });
51
+ if (!seen.has(layerKey(entity.layer))) {
52
+ add("undefined-layer", "error", `layer ${JSON.stringify(entity.layer)} is not defined`);
53
+ }
54
+ const problems = geometryProblems(entity);
55
+ for (const problem of problems)
56
+ add("geometry", "error", problem);
57
+ if (entity.kind === "text") {
58
+ const problem = textProblem(entity.value);
59
+ if (problem)
60
+ add("text", "error", problem);
61
+ const warning = textWarning(entity.value);
62
+ if (warning)
63
+ add("text", "warning", warning);
64
+ }
65
+ // A shape with a geometry error has no length worth a second message.
66
+ const collapsed = problems.length === 0 ? zeroLength(entity) : undefined;
67
+ if (collapsed)
68
+ add("zero-length", "warning", collapsed);
69
+ });
70
+ return out;
71
+ }
72
+ /**
73
+ * An error that refuses a drawing because of what `lint` found in it. `toDXF`
74
+ * throws it when a drawing has lint errors for the version being written, and
75
+ * other code can throw it to refuse a drawing for its own purpose. Its message
76
+ * lists the first 10 problems and names no file format.
77
+ */
78
+ export class DrawingError extends Error {
79
+ /** Every problem the drawing was refused for, including those the message leaves out. */
80
+ diagnostics;
81
+ /**
82
+ * @param diagnostics - The problems to refuse the drawing for, as `lint` returns them.
83
+ */
84
+ constructor(diagnostics) {
85
+ const count = diagnostics.length === 1 ? "1 problem" : `${diagnostics.length} problems`;
86
+ const shown = diagnostics.slice(0, 10).map((d) => `- ${d.message}`);
87
+ const more = diagnostics.length > 10 ? [`- and ${diagnostics.length - 10} more`] : [];
88
+ super([`The drawing has ${count}:`, ...shown, ...more].join("\n"));
89
+ this.name = "DrawingError";
90
+ this.diagnostics = diagnostics;
91
+ }
92
+ }
93
+ function geometryProblems(e) {
94
+ const out = [];
95
+ const finite = (label, ...values) => {
96
+ if (!values.every(Number.isFinite)) {
97
+ out.push(`${label} must be finite numbers`);
98
+ return;
99
+ }
100
+ const problem = values.map(magnitudeProblem).find((p) => p !== undefined);
101
+ if (problem)
102
+ out.push(`${label}: ${problem}`);
103
+ };
104
+ const size = (label, value) => {
105
+ const problem = Number.isFinite(value) ? sizeProblem(value) : "This value must be finite";
106
+ if (problem)
107
+ out.push(`${label}: ${problem}`);
108
+ };
109
+ switch (e.kind) {
110
+ case "line":
111
+ finite("its points", ...e.from, ...e.to);
112
+ break;
113
+ case "polyline":
114
+ if (e.points.length < 2)
115
+ out.push("a polyline needs at least 2 points");
116
+ // A loop, because a polyline can have more points than a call can take arguments.
117
+ if (!e.points.every((p) => Number.isFinite(p[0]) && Number.isFinite(p[1]))) {
118
+ out.push("its points must be finite numbers");
119
+ }
120
+ else {
121
+ for (const [x, y] of e.points) {
122
+ const problem = magnitudeProblem(x) ?? magnitudeProblem(y);
123
+ if (!problem)
124
+ continue;
125
+ out.push(`its points: ${problem}`);
126
+ break;
127
+ }
128
+ }
129
+ break;
130
+ case "circle":
131
+ finite("its center", ...e.center);
132
+ size("radius", e.radius);
133
+ break;
134
+ case "arc":
135
+ finite("its center", ...e.center);
136
+ size("radius", e.radius);
137
+ if (![e.startAngle, e.endAngle].every((a) => Number.isFinite(a) && a >= 0 && a < 360)) {
138
+ out.push("its angles must be from 0 up to 360");
139
+ }
140
+ else if (e.startAngle === e.endAngle) {
141
+ out.push("its start and end angles must differ");
142
+ }
143
+ else {
144
+ const problem = arcProblem(e.startAngle, e.endAngle);
145
+ if (problem)
146
+ out.push(problem);
147
+ }
148
+ break;
149
+ case "text":
150
+ finite("its position and rotation", ...e.at, e.rotation);
151
+ size("height", e.height);
152
+ break;
153
+ }
154
+ return out;
155
+ }
156
+ /** Says how a valid entity ends up with no length in the file, or returns undefined when it has one. */
157
+ function zeroLength(e) {
158
+ const rounded = "once it is rounded to the 6 decimals tsdraft writes";
159
+ switch (e.kind) {
160
+ case "line":
161
+ if (same(e.from, e.to))
162
+ return "the line has no length";
163
+ return sameWritten(e.from, e.to) ? `the line has no length ${rounded}` : undefined;
164
+ case "polyline": {
165
+ const [first] = e.points;
166
+ if (!first)
167
+ return undefined;
168
+ if (e.points.every((p) => same(p, first)))
169
+ return "the polyline has no length";
170
+ return e.points.every((p) => sameWritten(p, first))
171
+ ? `the polyline has no length ${rounded}`
172
+ : undefined;
173
+ }
174
+ case "arc":
175
+ return arcIsWrittenAsPoint(e.startAngle, e.endAngle)
176
+ ? `the arc has no length ${rounded}`
177
+ : undefined;
178
+ case "circle":
179
+ case "text":
180
+ return undefined;
181
+ }
182
+ }
183
+ function same(a, b) {
184
+ return a[0] === b[0] && a[1] === b[1];
185
+ }
186
+ function sameWritten(a, b) {
187
+ return written(a[0]) === written(b[0]) && written(a[1]) === written(b[1]);
188
+ }