@dirkwa/s-100 0.0.0-stage → 0.0.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/LICENSE +15 -0
- package/README.md +89 -2
- package/dist/geometry.d.ts +27 -0
- package/dist/geometry.js +85 -0
- package/dist/instructions.d.ts +45 -0
- package/dist/instructions.js +165 -0
- package/dist/iso8211.d.ts +43 -0
- package/dist/iso8211.js +208 -0
- package/dist/lua-data.d.ts +17 -0
- package/dist/lua-data.js +126 -0
- package/dist/portray-client.d.ts +25 -0
- package/dist/portray-client.js +59 -0
- package/dist/portray.d.ts +29 -0
- package/dist/portray.js +59 -0
- package/dist/render.d.ts +60 -0
- package/dist/render.js +350 -0
- package/dist/s101-records.d.ts +96 -0
- package/dist/s101-records.js +157 -0
- package/dist/s101-update.d.ts +43 -0
- package/dist/s101-update.js +321 -0
- package/dist/s101.d.ts +103 -0
- package/dist/s101.js +131 -0
- package/dist/worker.d.ts +34 -0
- package/dist/worker.js +54 -0
- package/lua/host.lua +182 -0
- package/package.json +73 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
you may not use this file except in compliance with the License.
|
|
7
|
+
You may obtain a copy of the License at
|
|
8
|
+
|
|
9
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
|
|
11
|
+
Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
See the License for the specific language governing permissions and
|
|
15
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
-
#
|
|
1
|
+
# s-100
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
S-100 charts for Signal K: a reader for IHO S-100 datasets and the IHO
|
|
4
|
+
portrayal that draws them, for chart provider plugins and chart plotters.
|
|
5
|
+
|
|
6
|
+
**Status: proof of concept.** It reads S-101 ENC cells and draws them with
|
|
7
|
+
the official IHO S-101 portrayal catalogue, unmodified, in the browser.
|
|
8
|
+
Not for navigation.
|
|
9
|
+
|
|
10
|
+
## What it does
|
|
11
|
+
|
|
12
|
+
- `src/iso8211.ts`, `src/s101.ts`: ISO 8211 reader and the S-101 dataset
|
|
13
|
+
(features, information types, attributes, spatial records).
|
|
14
|
+
- `src/s101-records.ts`, `src/s101-update.ts`: update files (`.001`,
|
|
15
|
+
`.002`, ...) merged into their base cell as S-100 Part 10a describes:
|
|
16
|
+
records inserted, deleted or modified, down to single attributes,
|
|
17
|
+
associations and coordinates; a cancelled cell is left empty. An update
|
|
18
|
+
that does not fit the cell is rejected with an error. The demo loads the
|
|
19
|
+
updates it finds beside a cell.
|
|
20
|
+
- `src/lua-data.ts`, `lua/host.lua`, `src/portray.ts`: runs the catalogue's
|
|
21
|
+
Lua rules (S-100 Part 9a) in [wasmoon](https://github.com/ceifa/wasmoon)
|
|
22
|
+
(Lua 5.4 in WebAssembly). The dataset is handed to Lua as tables, and
|
|
23
|
+
`host.lua` answers the catalogue's `Host*` calls from them.
|
|
24
|
+
- `src/instructions.ts`, `src/geometry.ts`, `src/render.ts`: the drawing
|
|
25
|
+
instructions as OpenLayers features and styles: SVG symbols coloured per
|
|
26
|
+
palette (Day, Dusk, Night), line styles, area fills, text, viewing groups,
|
|
27
|
+
scale minimum.
|
|
28
|
+
- `src/worker.ts`, `src/portray-client.ts`: the same in a web worker.
|
|
29
|
+
- `demo/`: a page that loads a cell, portrays it and shows the timings;
|
|
30
|
+
the safety contour can be changed live.
|
|
31
|
+
|
|
32
|
+
## Measurements
|
|
33
|
+
|
|
34
|
+
Two CHS sample cells near Québec, Chromium on a desktop, and with the CPU
|
|
35
|
+
throttled 4x (roughly a Raspberry Pi 5):
|
|
36
|
+
|
|
37
|
+
| | desktop | 4x throttled |
|
|
38
|
+
| ------------------------------------------------------ | ----------- | ------------ |
|
|
39
|
+
| 1,195 objects: parse, portray, draw | 0.7 s | 2.8 s |
|
|
40
|
+
| 4,889 objects: parse, portray, draw | 1.3 s | 6.0 s |
|
|
41
|
+
| safety contour changed, portrayed again | 0.07-0.34 s | 0.4-1.8 s |
|
|
42
|
+
| dragging the map, slowest 5 % of frames (image layer) | 17 ms | 17 ms |
|
|
43
|
+
| dragging the map, slowest 5 % of frames (vector layer) | 33 ms | 200 ms |
|
|
44
|
+
|
|
45
|
+
Parsing and portrayal run in a web worker (`src/worker.ts`,
|
|
46
|
+
`src/portray-client.ts`), so the page stays responsive meanwhile.
|
|
47
|
+
Chromium cannot throttle workers, so the throttled load times above were
|
|
48
|
+
measured with everything on the main thread. The demo draws with a vector
|
|
49
|
+
image layer: the chart is drawn once and the picture moves while panning;
|
|
50
|
+
`#layer=vector` switches to the plain vector layer.
|
|
51
|
+
|
|
52
|
+
## Use
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
npm install s-100
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Each module is its own entry point, `s-100/<module>` (`s-100/s101`,
|
|
59
|
+
`s-100/portray`, `s-100/render`, …), built to JavaScript with type
|
|
60
|
+
declarations; `s-100/lua/host.lua` is the portrayal host. OpenLayers is a
|
|
61
|
+
peer dependency, needed only by `render`, `geometry` and the worker client.
|
|
62
|
+
|
|
63
|
+
## Not yet
|
|
64
|
+
|
|
65
|
+
Line style symbols, S-102/S-104/S-111 (HDF5) and the GML products.
|
|
66
|
+
|
|
67
|
+
## Setup
|
|
68
|
+
|
|
69
|
+
The catalogue and the data are not in this repository.
|
|
70
|
+
|
|
71
|
+
1. Portrayal and feature catalogue: clone
|
|
72
|
+
[OpenS100](https://github.com/S-100ExpertTeam/OpenS100) (MPL-2.0); they
|
|
73
|
+
are in `ProgramData/PC/S101_Portrayal` and `ProgramData/FC/S-101_FC.xml`.
|
|
74
|
+
The licence of the IHO catalogues themselves is still to be confirmed.
|
|
75
|
+
2. `S101_FC=<path to S-101_FC.xml> npm run fc` and
|
|
76
|
+
`S101_PC=<path to S101_Portrayal> npm run assets`.
|
|
77
|
+
3. Sample data: the Canadian Hydrographic Service's unencrypted S-100
|
|
78
|
+
datasets; put `.000` cells in `demo/data/` and list them in
|
|
79
|
+
`demo/index.html`.
|
|
80
|
+
4. `npm run demo`, then open http://localhost:8765/.
|
|
81
|
+
|
|
82
|
+
`npm test` runs the tests; the update fixtures are GDAL's (MIT), see
|
|
83
|
+
`test/data/gdal/README.md`.
|
|
84
|
+
|
|
85
|
+
`tools/bench.mjs` runs the demo in Playwright, optionally CPU throttled:
|
|
86
|
+
`node tools/bench.mjs <cell> <throttle> <tag>`.
|
|
87
|
+
|
|
88
|
+
## Licence
|
|
89
|
+
|
|
90
|
+
Apache-2.0
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type S101Dataset, type SpatialRef } from './s101.ts';
|
|
2
|
+
export type Coord = [number, number];
|
|
3
|
+
export type Geom = {
|
|
4
|
+
type: 'Point';
|
|
5
|
+
coord: Coord;
|
|
6
|
+
} | {
|
|
7
|
+
type: 'MultiPoint';
|
|
8
|
+
coords: Coord[];
|
|
9
|
+
z: number[];
|
|
10
|
+
} | {
|
|
11
|
+
type: 'LineString';
|
|
12
|
+
lines: Coord[][];
|
|
13
|
+
} | {
|
|
14
|
+
type: 'Polygon';
|
|
15
|
+
rings: Coord[][];
|
|
16
|
+
};
|
|
17
|
+
export declare function lineCoords(ds: S101Dataset, ref: {
|
|
18
|
+
rrnm: number;
|
|
19
|
+
rrid: number;
|
|
20
|
+
ornt: number;
|
|
21
|
+
}): Coord[];
|
|
22
|
+
export declare function parseSpatialId(id: string): {
|
|
23
|
+
rrnm: number;
|
|
24
|
+
rrid: number;
|
|
25
|
+
};
|
|
26
|
+
export declare function spatialGeom(ds: S101Dataset, ref: SpatialRef): Geom | undefined;
|
|
27
|
+
export declare function featureGeom(ds: S101Dataset, spas: SpatialRef[]): Geom | undefined;
|
package/dist/geometry.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Feature geometry in lon/lat from the dataset's spatial records.
|
|
2
|
+
import { RCNM } from "./s101.js";
|
|
3
|
+
function curveCoords(ds, rrid) {
|
|
4
|
+
const c = ds.curves.get(rrid);
|
|
5
|
+
if (!c)
|
|
6
|
+
return [];
|
|
7
|
+
// A curve's own coordinates already start and end on its boundary points (PTAS).
|
|
8
|
+
const out = [];
|
|
9
|
+
for (let i = 0; i < c.coords.length; i += 2)
|
|
10
|
+
out.push([c.coords[i], c.coords[i + 1]]);
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
13
|
+
export function lineCoords(ds, ref) {
|
|
14
|
+
let coords;
|
|
15
|
+
if (ref.rrnm === RCNM.curve)
|
|
16
|
+
coords = curveCoords(ds, ref.rrid);
|
|
17
|
+
else if (ref.rrnm === RCNM.composite) {
|
|
18
|
+
coords = [];
|
|
19
|
+
for (const r of ds.composites.get(ref.rrid)?.refs ?? []) {
|
|
20
|
+
const part = lineCoords(ds, r);
|
|
21
|
+
if (coords.length && part.length)
|
|
22
|
+
part.shift();
|
|
23
|
+
coords.push(...part);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else
|
|
27
|
+
coords = [];
|
|
28
|
+
return ref.ornt === 2 ? coords.slice().reverse() : coords;
|
|
29
|
+
}
|
|
30
|
+
const KIND_RCNM = {
|
|
31
|
+
Point: RCNM.point,
|
|
32
|
+
MultiPoint: RCNM.multipoint,
|
|
33
|
+
Curve: RCNM.curve,
|
|
34
|
+
CompositeCurve: RCNM.composite,
|
|
35
|
+
Surface: RCNM.surface
|
|
36
|
+
};
|
|
37
|
+
export function parseSpatialId(id) {
|
|
38
|
+
const [kind, rrid] = id.split('|');
|
|
39
|
+
return { rrnm: KIND_RCNM[kind] ?? 0, rrid: Number(rrid) };
|
|
40
|
+
}
|
|
41
|
+
export function spatialGeom(ds, ref) {
|
|
42
|
+
switch (ref.rrnm) {
|
|
43
|
+
case RCNM.point: {
|
|
44
|
+
const p = ds.points.get(ref.rrid);
|
|
45
|
+
return p ? { type: 'Point', coord: [p.x, p.y] } : undefined;
|
|
46
|
+
}
|
|
47
|
+
case RCNM.multipoint: {
|
|
48
|
+
const m = ds.multipoints.get(ref.rrid);
|
|
49
|
+
if (!m)
|
|
50
|
+
return undefined;
|
|
51
|
+
const coords = [];
|
|
52
|
+
const z = [];
|
|
53
|
+
for (let i = 0; i < m.pts.length; i += 3) {
|
|
54
|
+
coords.push([m.pts[i], m.pts[i + 1]]);
|
|
55
|
+
z.push(m.pts[i + 2]);
|
|
56
|
+
}
|
|
57
|
+
return { type: 'MultiPoint', coords, z };
|
|
58
|
+
}
|
|
59
|
+
case RCNM.curve:
|
|
60
|
+
case RCNM.composite:
|
|
61
|
+
return { type: 'LineString', lines: [lineCoords(ds, ref)] };
|
|
62
|
+
case RCNM.surface: {
|
|
63
|
+
const s = ds.surfaces.get(ref.rrid);
|
|
64
|
+
if (!s)
|
|
65
|
+
return undefined;
|
|
66
|
+
const outer = s.refs.filter((r) => r.usag === 1).map((r) => lineCoords(ds, r));
|
|
67
|
+
const inner = s.refs.filter((r) => r.usag !== 1).map((r) => lineCoords(ds, r));
|
|
68
|
+
return { type: 'Polygon', rings: [...outer, ...inner] };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
export function featureGeom(ds, spas) {
|
|
74
|
+
if (!spas.length)
|
|
75
|
+
return undefined;
|
|
76
|
+
if (spas.length === 1)
|
|
77
|
+
return spatialGeom(ds, spas[0]);
|
|
78
|
+
const geoms = spas.map((r) => spatialGeom(ds, r)).filter((g) => !!g);
|
|
79
|
+
if (geoms.every((g) => g.type === 'LineString'))
|
|
80
|
+
return {
|
|
81
|
+
type: 'LineString',
|
|
82
|
+
lines: geoms.flatMap((g) => g.lines)
|
|
83
|
+
};
|
|
84
|
+
return geoms[0];
|
|
85
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type ItemKind = 'point' | 'line' | 'fill' | 'pattern' | 'text';
|
|
2
|
+
export interface DrawItem {
|
|
3
|
+
kind: ItemKind;
|
|
4
|
+
viewingGroups: number[];
|
|
5
|
+
priority: number;
|
|
6
|
+
scaleMinimum?: number;
|
|
7
|
+
scaleMaximum?: number;
|
|
8
|
+
/** spatial ids replacing the feature geometry */
|
|
9
|
+
spatialRefs: string[];
|
|
10
|
+
augmentedPoint?: [number, number];
|
|
11
|
+
ref: string;
|
|
12
|
+
color?: string;
|
|
13
|
+
transparency?: number;
|
|
14
|
+
width?: number;
|
|
15
|
+
dash?: number[];
|
|
16
|
+
rotation?: number;
|
|
17
|
+
offset?: [number, number];
|
|
18
|
+
fontSize?: number;
|
|
19
|
+
fontColor?: string;
|
|
20
|
+
italic?: boolean;
|
|
21
|
+
hAlign?: string;
|
|
22
|
+
vAlign?: string;
|
|
23
|
+
text?: string;
|
|
24
|
+
/** a path around the feature's point, for light sectors */
|
|
25
|
+
augmented?: AugmentedPath;
|
|
26
|
+
}
|
|
27
|
+
/** Angles are true bearings in degrees, a negative extent anticlockwise; lengths are metres on the ground when `geographic`, else millimetres on the screen. */
|
|
28
|
+
export type AugmentedSegment = {
|
|
29
|
+
type: 'ray';
|
|
30
|
+
bearing: number;
|
|
31
|
+
length: number;
|
|
32
|
+
} | {
|
|
33
|
+
type: 'arc';
|
|
34
|
+
centre: [number, number];
|
|
35
|
+
radius: number;
|
|
36
|
+
start: number;
|
|
37
|
+
extent: number;
|
|
38
|
+
};
|
|
39
|
+
/** `localCentres`: arc centres are millimetres from the feature's point (x right, y up), else they are not supported and arcs centre on the point. */
|
|
40
|
+
export interface AugmentedPath {
|
|
41
|
+
segments: AugmentedSegment[];
|
|
42
|
+
geographic: boolean;
|
|
43
|
+
localCentres?: boolean;
|
|
44
|
+
}
|
|
45
|
+
export declare function parseInstructions(src: string): DrawItem[];
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// S-100 Part 9a drawing instructions to a flat list of drawing items.
|
|
2
|
+
export function parseInstructions(src) {
|
|
3
|
+
const items = [];
|
|
4
|
+
let vg = [];
|
|
5
|
+
let priority = 0;
|
|
6
|
+
let scaleMinimum;
|
|
7
|
+
let scaleMaximum;
|
|
8
|
+
let spatialRefs = [];
|
|
9
|
+
let augmentedPoint;
|
|
10
|
+
let simple = { width: 0.32, color: 'CHBLK' };
|
|
11
|
+
let dash;
|
|
12
|
+
let interval;
|
|
13
|
+
let augmented;
|
|
14
|
+
let pendingSegments = [];
|
|
15
|
+
let rotation = 0;
|
|
16
|
+
let offset = [0, 0];
|
|
17
|
+
let fontSize = 10;
|
|
18
|
+
let fontColor = 'CHBLK';
|
|
19
|
+
let italic = false;
|
|
20
|
+
let hAlign = 'Start';
|
|
21
|
+
let vAlign = 'Baseline';
|
|
22
|
+
const base = (kind, ref) => ({
|
|
23
|
+
kind,
|
|
24
|
+
viewingGroups: vg,
|
|
25
|
+
priority,
|
|
26
|
+
scaleMinimum,
|
|
27
|
+
scaleMaximum,
|
|
28
|
+
spatialRefs,
|
|
29
|
+
augmentedPoint,
|
|
30
|
+
ref
|
|
31
|
+
});
|
|
32
|
+
for (const part of src.split(';')) {
|
|
33
|
+
const colon = part.indexOf(':');
|
|
34
|
+
const cmd = colon < 0 ? part : part.slice(0, colon);
|
|
35
|
+
const arg = colon < 0 ? '' : part.slice(colon + 1);
|
|
36
|
+
const args = arg.split(',');
|
|
37
|
+
switch (cmd) {
|
|
38
|
+
case 'ViewingGroup':
|
|
39
|
+
vg = args.map(Number).filter((n) => !Number.isNaN(n));
|
|
40
|
+
break;
|
|
41
|
+
case 'DrawingPriority':
|
|
42
|
+
priority = Number(arg);
|
|
43
|
+
break;
|
|
44
|
+
case 'ScaleMinimum':
|
|
45
|
+
scaleMinimum = Number(arg);
|
|
46
|
+
break;
|
|
47
|
+
case 'ScaleMaximum':
|
|
48
|
+
scaleMaximum = Number(arg);
|
|
49
|
+
break;
|
|
50
|
+
case 'SpatialReference':
|
|
51
|
+
spatialRefs = [...spatialRefs, args[0]];
|
|
52
|
+
break;
|
|
53
|
+
case 'AugmentedPoint':
|
|
54
|
+
augmentedPoint = [Number(args[1]), Number(args[2])];
|
|
55
|
+
augmented = undefined;
|
|
56
|
+
break;
|
|
57
|
+
case 'ClearGeometry':
|
|
58
|
+
spatialRefs = [];
|
|
59
|
+
augmentedPoint = undefined;
|
|
60
|
+
augmented = undefined;
|
|
61
|
+
pendingSegments = [];
|
|
62
|
+
break;
|
|
63
|
+
case 'AugmentedRay':
|
|
64
|
+
augmentedPoint = undefined;
|
|
65
|
+
augmented = {
|
|
66
|
+
segments: [{ type: 'ray', bearing: Number(args[1]), length: Number(args[3]) }],
|
|
67
|
+
geographic: args[2] === 'GeographicCRS'
|
|
68
|
+
};
|
|
69
|
+
break;
|
|
70
|
+
case 'ArcByRadius':
|
|
71
|
+
pendingSegments = [
|
|
72
|
+
...pendingSegments,
|
|
73
|
+
{
|
|
74
|
+
type: 'arc',
|
|
75
|
+
centre: [Number(args[0]) || 0, Number(args[1]) || 0],
|
|
76
|
+
radius: Number(args[2]),
|
|
77
|
+
start: args[3] ? Number(args[3]) : 0,
|
|
78
|
+
extent: args[4] ? Number(args[4]) : 360
|
|
79
|
+
}
|
|
80
|
+
];
|
|
81
|
+
break;
|
|
82
|
+
case 'AugmentedPath':
|
|
83
|
+
augmentedPoint = undefined;
|
|
84
|
+
augmented = pendingSegments.length
|
|
85
|
+
? {
|
|
86
|
+
segments: pendingSegments,
|
|
87
|
+
geographic: args[2] === 'GeographicCRS',
|
|
88
|
+
localCentres: args[0] !== 'GeographicCRS'
|
|
89
|
+
}
|
|
90
|
+
: undefined;
|
|
91
|
+
pendingSegments = [];
|
|
92
|
+
break;
|
|
93
|
+
case 'LineStyle':
|
|
94
|
+
if (args[0] === '_simple_') {
|
|
95
|
+
interval = Number(args[1]) || 0;
|
|
96
|
+
simple = {
|
|
97
|
+
width: Number(args[2]) || 0.32,
|
|
98
|
+
color: args[3] || 'CHBLK',
|
|
99
|
+
dash: dash && interval > dash[1] ? [dash[1], interval - dash[1]] : undefined
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
dash = undefined;
|
|
103
|
+
break;
|
|
104
|
+
case 'Dash':
|
|
105
|
+
dash = [Number(args[0]), Number(args[1])];
|
|
106
|
+
break;
|
|
107
|
+
case 'Rotation':
|
|
108
|
+
rotation = Number(args[1]) || 0;
|
|
109
|
+
break;
|
|
110
|
+
case 'LocalOffset':
|
|
111
|
+
offset = [Number(args[0]) || 0, Number(args[1]) || 0];
|
|
112
|
+
break;
|
|
113
|
+
case 'FontSize':
|
|
114
|
+
fontSize = Number(arg);
|
|
115
|
+
break;
|
|
116
|
+
case 'FontColor':
|
|
117
|
+
fontColor = args[0];
|
|
118
|
+
break;
|
|
119
|
+
case 'FontSlant':
|
|
120
|
+
italic = arg === 'Italics';
|
|
121
|
+
break;
|
|
122
|
+
case 'TextAlignHorizontal':
|
|
123
|
+
hAlign = arg;
|
|
124
|
+
break;
|
|
125
|
+
case 'TextAlignVertical':
|
|
126
|
+
vAlign = arg;
|
|
127
|
+
break;
|
|
128
|
+
case 'PointInstruction':
|
|
129
|
+
items.push({ ...base('point', arg), rotation, offset });
|
|
130
|
+
break;
|
|
131
|
+
case 'LineInstruction':
|
|
132
|
+
if (arg === '_simple_') {
|
|
133
|
+
items.push({
|
|
134
|
+
...base('line', arg),
|
|
135
|
+
width: simple.width,
|
|
136
|
+
color: simple.color,
|
|
137
|
+
dash: simple.dash,
|
|
138
|
+
augmented
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
else
|
|
142
|
+
items.push({ ...base('line', arg), augmented });
|
|
143
|
+
break;
|
|
144
|
+
case 'ColorFill':
|
|
145
|
+
items.push({ ...base('fill', args[0]), transparency: Number(args[1]) || 0 });
|
|
146
|
+
break;
|
|
147
|
+
case 'AreaFillReference':
|
|
148
|
+
items.push(base('pattern', arg));
|
|
149
|
+
break;
|
|
150
|
+
case 'TextInstruction':
|
|
151
|
+
items.push({
|
|
152
|
+
...base('text', ''),
|
|
153
|
+
text: arg,
|
|
154
|
+
fontSize,
|
|
155
|
+
fontColor,
|
|
156
|
+
italic,
|
|
157
|
+
hAlign,
|
|
158
|
+
vAlign,
|
|
159
|
+
offset
|
|
160
|
+
});
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return items;
|
|
165
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
type Fmt = {
|
|
2
|
+
kind: 'A';
|
|
3
|
+
width?: number;
|
|
4
|
+
} | {
|
|
5
|
+
kind: 'I';
|
|
6
|
+
width?: number;
|
|
7
|
+
} | {
|
|
8
|
+
kind: 'R';
|
|
9
|
+
width?: number;
|
|
10
|
+
} | {
|
|
11
|
+
kind: 'b';
|
|
12
|
+
type: number;
|
|
13
|
+
bytes: number;
|
|
14
|
+
};
|
|
15
|
+
export interface FieldDef {
|
|
16
|
+
tag: string;
|
|
17
|
+
name: string;
|
|
18
|
+
/** Subfields read once at the start of the field. */
|
|
19
|
+
fixed: string[];
|
|
20
|
+
/** Subfields repeated until the end of the field. */
|
|
21
|
+
repeated: string[];
|
|
22
|
+
formats: Fmt[];
|
|
23
|
+
}
|
|
24
|
+
export type Subfields = Partial<Record<string, string | number>>;
|
|
25
|
+
export interface Field {
|
|
26
|
+
tag: string;
|
|
27
|
+
/** The fixed subfields, or the first row of a purely repeating field. */
|
|
28
|
+
head: Subfields;
|
|
29
|
+
rows: Subfields[];
|
|
30
|
+
}
|
|
31
|
+
export interface Record8211 {
|
|
32
|
+
fields: Field[];
|
|
33
|
+
}
|
|
34
|
+
export declare class Iso8211Reader {
|
|
35
|
+
readonly defs: Map<string, FieldDef>;
|
|
36
|
+
private readonly view;
|
|
37
|
+
private readonly b;
|
|
38
|
+
constructor(b: Uint8Array);
|
|
39
|
+
records(): Generator<Record8211>;
|
|
40
|
+
private decode;
|
|
41
|
+
private value;
|
|
42
|
+
}
|
|
43
|
+
export {};
|
package/dist/iso8211.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// Minimal ISO/IEC 8211 reader for S-100 Part 10a (S-101) files.
|
|
2
|
+
const FT = 0x1e;
|
|
3
|
+
const UT = 0x1f;
|
|
4
|
+
const utf8 = new TextDecoder('utf-8');
|
|
5
|
+
function ascii(b, start, len) {
|
|
6
|
+
let s = '';
|
|
7
|
+
for (let i = 0; i < len; i++)
|
|
8
|
+
s += String.fromCharCode(b[start + i]);
|
|
9
|
+
return s;
|
|
10
|
+
}
|
|
11
|
+
function expandFormats(src) {
|
|
12
|
+
let s = src.trim();
|
|
13
|
+
if (s.startsWith('(') && s.endsWith(')'))
|
|
14
|
+
s = s.slice(1, -1);
|
|
15
|
+
const out = [];
|
|
16
|
+
let i = 0;
|
|
17
|
+
const readNum = () => {
|
|
18
|
+
let n = '';
|
|
19
|
+
while (i < s.length && /\d/.test(s[i]))
|
|
20
|
+
n += s[i++];
|
|
21
|
+
return n ? Number(n) : 1;
|
|
22
|
+
};
|
|
23
|
+
const parseList = (end) => {
|
|
24
|
+
const list = [];
|
|
25
|
+
while (i < s.length && s[i] !== end) {
|
|
26
|
+
if (s[i] === ',') {
|
|
27
|
+
i++;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const rep = /\d/.test(s[i]) ? readNum() : 1;
|
|
31
|
+
let items;
|
|
32
|
+
if (s[i] === '(') {
|
|
33
|
+
i++;
|
|
34
|
+
items = parseList(')');
|
|
35
|
+
i++;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
const c = s[i++];
|
|
39
|
+
if (c === 'b') {
|
|
40
|
+
const type = Number(s[i++]);
|
|
41
|
+
const bytes = Number(s[i++]);
|
|
42
|
+
items = [{ kind: 'b', type, bytes }];
|
|
43
|
+
}
|
|
44
|
+
else if (c === 'A' || c === 'I' || c === 'R') {
|
|
45
|
+
let width;
|
|
46
|
+
if (s[i] === '(') {
|
|
47
|
+
i++;
|
|
48
|
+
let n = '';
|
|
49
|
+
while (s[i] !== ')')
|
|
50
|
+
n += s[i++];
|
|
51
|
+
i++;
|
|
52
|
+
width = Number(n);
|
|
53
|
+
}
|
|
54
|
+
items = [{ kind: c, width }];
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
throw new Error(`Unsupported format ${c} in ${src}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
for (let r = 0; r < rep; r++)
|
|
61
|
+
list.push(...items);
|
|
62
|
+
}
|
|
63
|
+
return list;
|
|
64
|
+
};
|
|
65
|
+
out.push(...parseList('\u0000'));
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
function readLeader(b, off) {
|
|
69
|
+
return {
|
|
70
|
+
recordLength: Number(ascii(b, off, 5)),
|
|
71
|
+
fieldControlLength: Number(ascii(b, off + 10, 2)) || 0,
|
|
72
|
+
baseAddress: Number(ascii(b, off + 12, 5)),
|
|
73
|
+
sizeLen: Number(ascii(b, off + 20, 1)),
|
|
74
|
+
sizePos: Number(ascii(b, off + 21, 1)),
|
|
75
|
+
sizeTag: Number(ascii(b, off + 23, 1))
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function readDirectory(b, off, l) {
|
|
79
|
+
const entries = [];
|
|
80
|
+
let p = off + 24;
|
|
81
|
+
const entrySize = l.sizeTag + l.sizeLen + l.sizePos;
|
|
82
|
+
while (b[p] !== FT) {
|
|
83
|
+
const tag = ascii(b, p, l.sizeTag);
|
|
84
|
+
const len = Number(ascii(b, p + l.sizeTag, l.sizeLen));
|
|
85
|
+
const pos = Number(ascii(b, p + l.sizeTag + l.sizeLen, l.sizePos));
|
|
86
|
+
entries.push({ tag, len, pos });
|
|
87
|
+
p += entrySize;
|
|
88
|
+
}
|
|
89
|
+
return entries;
|
|
90
|
+
}
|
|
91
|
+
export class Iso8211Reader {
|
|
92
|
+
defs = new Map();
|
|
93
|
+
view;
|
|
94
|
+
b;
|
|
95
|
+
constructor(b) {
|
|
96
|
+
this.b = b;
|
|
97
|
+
this.view = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
98
|
+
}
|
|
99
|
+
*records() {
|
|
100
|
+
const b = this.b;
|
|
101
|
+
const ddr = readLeader(b, 0);
|
|
102
|
+
for (const e of readDirectory(b, 0, ddr)) {
|
|
103
|
+
const start = ddr.baseAddress + e.pos;
|
|
104
|
+
const body = b.subarray(start, start + e.len);
|
|
105
|
+
const fc = ddr.fieldControlLength;
|
|
106
|
+
const parts = [];
|
|
107
|
+
let cur = [];
|
|
108
|
+
for (let i = fc; i < body.length; i++) {
|
|
109
|
+
if (body[i] === UT || body[i] === FT) {
|
|
110
|
+
parts.push(utf8.decode(Uint8Array.from(cur)));
|
|
111
|
+
cur = [];
|
|
112
|
+
if (body[i] === FT)
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
else
|
|
116
|
+
cur.push(body[i]);
|
|
117
|
+
}
|
|
118
|
+
const [name = '', arr = '', fmt = ''] = parts;
|
|
119
|
+
const star = arr.indexOf('*');
|
|
120
|
+
const split = (x) => x.replace(/\\+$/, '').split('!').filter(Boolean);
|
|
121
|
+
const fixed = star < 0 ? split(arr) : split(arr.slice(0, star));
|
|
122
|
+
const repeated = star < 0 ? [] : split(arr.slice(star + 1));
|
|
123
|
+
this.defs.set(e.tag, {
|
|
124
|
+
tag: e.tag,
|
|
125
|
+
name,
|
|
126
|
+
fixed,
|
|
127
|
+
repeated,
|
|
128
|
+
formats: fmt ? expandFormats(fmt) : []
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
let off = ddr.recordLength;
|
|
132
|
+
while (off < b.length) {
|
|
133
|
+
const l = readLeader(b, off);
|
|
134
|
+
if (!l.recordLength)
|
|
135
|
+
break;
|
|
136
|
+
const fields = [];
|
|
137
|
+
for (const e of readDirectory(b, off, l)) {
|
|
138
|
+
const def = this.defs.get(e.tag);
|
|
139
|
+
const start = off + l.baseAddress + e.pos;
|
|
140
|
+
if (def)
|
|
141
|
+
fields.push(this.decode(def, start, start + e.len));
|
|
142
|
+
}
|
|
143
|
+
yield { fields };
|
|
144
|
+
off += l.recordLength;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
decode(def, start, end) {
|
|
148
|
+
const { fixed, repeated, formats } = def;
|
|
149
|
+
const limit = end - 1;
|
|
150
|
+
const state = { p: start };
|
|
151
|
+
let fi = 0;
|
|
152
|
+
const head = {};
|
|
153
|
+
for (const label of fixed)
|
|
154
|
+
head[label] = this.value(formats[fi++] ?? formats[formats.length - 1], state, limit);
|
|
155
|
+
const rows = [];
|
|
156
|
+
if (repeated.length) {
|
|
157
|
+
const base = fi;
|
|
158
|
+
while (state.p < limit) {
|
|
159
|
+
const row = {};
|
|
160
|
+
for (let k = 0; k < repeated.length; k++) {
|
|
161
|
+
row[repeated[k]] = this.value(formats[base + k] ?? formats[formats.length - 1], state, limit);
|
|
162
|
+
}
|
|
163
|
+
rows.push(row);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return { tag: def.tag, head: fixed.length ? head : (rows[0] ?? {}), rows };
|
|
167
|
+
}
|
|
168
|
+
value(f, state, limit) {
|
|
169
|
+
const p = state.p;
|
|
170
|
+
if (f.kind === 'b') {
|
|
171
|
+
let v;
|
|
172
|
+
const dv = this.view;
|
|
173
|
+
if (f.type === 1)
|
|
174
|
+
v =
|
|
175
|
+
f.bytes === 1
|
|
176
|
+
? dv.getUint8(p)
|
|
177
|
+
: f.bytes === 2
|
|
178
|
+
? dv.getUint16(p, true)
|
|
179
|
+
: dv.getUint32(p, true);
|
|
180
|
+
else if (f.type === 2)
|
|
181
|
+
v =
|
|
182
|
+
f.bytes === 1
|
|
183
|
+
? dv.getInt8(p)
|
|
184
|
+
: f.bytes === 2
|
|
185
|
+
? dv.getInt16(p, true)
|
|
186
|
+
: dv.getInt32(p, true);
|
|
187
|
+
else if (f.type === 4)
|
|
188
|
+
v = f.bytes === 4 ? dv.getFloat32(p, true) : dv.getFloat64(p, true);
|
|
189
|
+
else
|
|
190
|
+
throw new Error(`b${f.type}${f.bytes}`);
|
|
191
|
+
state.p = p + f.bytes;
|
|
192
|
+
return v;
|
|
193
|
+
}
|
|
194
|
+
let s;
|
|
195
|
+
if (f.width !== undefined) {
|
|
196
|
+
s = utf8.decode(this.b.subarray(p, p + f.width));
|
|
197
|
+
state.p = p + f.width;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
let q = p;
|
|
201
|
+
while (q < limit && this.b[q] !== UT)
|
|
202
|
+
q++;
|
|
203
|
+
s = utf8.decode(this.b.subarray(p, q));
|
|
204
|
+
state.p = q + 1;
|
|
205
|
+
}
|
|
206
|
+
return f.kind === 'A' ? s : Number(s);
|
|
207
|
+
}
|
|
208
|
+
}
|