docxkeun 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +55 -0
- package/README.md +139 -16
- package/dist/api/editor.d.ts +172 -5
- package/dist/api/editor.js +814 -24
- package/dist/api/headers.d.ts +51 -0
- package/dist/api/headers.js +133 -0
- package/dist/api/index.d.ts +5 -1
- package/dist/api/index.js +4 -1
- package/dist/api/lists.d.ts +37 -0
- package/dist/api/lists.js +132 -0
- package/dist/api/parts.js +1 -0
- package/dist/api/positions.js +1 -0
- package/dist/api/revisions.d.ts +61 -0
- package/dist/api/revisions.js +390 -0
- package/dist/api/selection.d.ts +57 -0
- package/dist/api/selection.js +91 -0
- package/dist/charts/color.d.ts +45 -0
- package/dist/charts/color.js +322 -0
- package/dist/charts/format.d.ts +12 -0
- package/dist/charts/format.js +459 -0
- package/dist/charts/index.d.ts +11 -0
- package/dist/charts/index.js +8 -0
- package/dist/charts/model.d.ts +225 -0
- package/dist/charts/model.js +758 -0
- package/dist/charts/outline.d.ts +17 -0
- package/dist/charts/outline.js +214 -0
- package/dist/charts/render.d.ts +20 -0
- package/dist/charts/render.js +1772 -0
- package/dist/drawing/arrows.d.ts +37 -0
- package/dist/drawing/arrows.js +99 -0
- package/dist/drawing/formula.d.ts +40 -0
- package/dist/drawing/formula.js +159 -0
- package/dist/drawing/geometry.d.ts +61 -0
- package/dist/drawing/geometry.js +286 -0
- package/dist/drawing/index.d.ts +12 -0
- package/dist/drawing/index.js +8 -0
- package/dist/drawing/presets.d.ts +72 -0
- package/dist/drawing/presets.js +2874 -0
- package/dist/fonts/shaping/apply.d.ts +94 -0
- package/dist/fonts/shaping/apply.js +349 -0
- package/dist/fonts/shaping/arabic.d.ts +28 -0
- package/dist/fonts/shaping/arabic.js +235 -0
- package/dist/fonts/shaping/buffer.d.ts +53 -0
- package/dist/fonts/shaping/buffer.js +95 -0
- package/dist/fonts/shaping/gdef.d.ts +30 -0
- package/dist/fonts/shaping/gdef.js +66 -0
- package/dist/fonts/shaping/gpos.d.ts +86 -0
- package/dist/fonts/shaping/gpos.js +428 -0
- package/dist/fonts/shaping/gsub.d.ts +65 -0
- package/dist/fonts/shaping/gsub.js +313 -0
- package/dist/fonts/shaping/index.d.ts +32 -0
- package/dist/fonts/shaping/index.js +402 -0
- package/dist/fonts/shaping/indic.d.ts +6 -0
- package/dist/fonts/shaping/indic.js +805 -0
- package/dist/fonts/shaping/layout-common.d.ts +124 -0
- package/dist/fonts/shaping/layout-common.js +349 -0
- package/dist/fonts/shaping/plan.d.ts +87 -0
- package/dist/fonts/shaping/plan.js +152 -0
- package/dist/fonts/shaping/scripts.d.ts +26 -0
- package/dist/fonts/shaping/scripts.js +160 -0
- package/dist/fonts/shaping/shaper.d.ts +23 -0
- package/dist/fonts/shaping/shaper.js +5 -0
- package/dist/fonts/shaping/unicode.d.ts +34 -0
- package/dist/fonts/shaping/unicode.js +138 -0
- package/dist/images/bmp.d.ts +9 -0
- package/dist/images/bmp.js +234 -0
- package/dist/images/gif.d.ts +9 -0
- package/dist/images/gif.js +215 -0
- package/dist/images/index.d.ts +18 -0
- package/dist/images/index.js +63 -0
- package/dist/images/png-encode.d.ts +4 -0
- package/dist/images/png-encode.js +73 -0
- package/dist/images/size.d.ts +10 -0
- package/dist/images/size.js +83 -0
- package/dist/images/tiff.d.ts +14 -0
- package/dist/images/tiff.js +407 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +5 -0
- package/dist/layout/bidi.d.ts +18 -0
- package/dist/layout/bidi.js +71 -0
- package/dist/layout/blocks.d.ts +20 -3
- package/dist/layout/blocks.js +88 -58
- package/dist/layout/cache.d.ts +31 -0
- package/dist/layout/cache.js +38 -0
- package/dist/layout/drawing.d.ts +30 -0
- package/dist/layout/drawing.js +120 -0
- package/dist/layout/engine.d.ts +3 -0
- package/dist/layout/engine.js +218 -11
- package/dist/layout/index.d.ts +1 -0
- package/dist/layout/index.js +1 -0
- package/dist/layout/lines.d.ts +63 -5
- package/dist/layout/lines.js +245 -20
- package/dist/layout/paint.d.ts +25 -3
- package/dist/layout/paint.js +26 -10
- package/dist/layout/shapes.d.ts +7 -0
- package/dist/layout/shapes.js +338 -0
- package/dist/layout/style.d.ts +4 -0
- package/dist/layout/style.js +2 -0
- package/dist/layout/types.d.ts +89 -2
- package/dist/math/boxes.d.ts +88 -0
- package/dist/math/boxes.js +166 -0
- package/dist/math/index.d.ts +29 -0
- package/dist/math/index.js +44 -0
- package/dist/math/layout.d.ts +13 -0
- package/dist/math/layout.js +771 -0
- package/dist/math/linear.d.ts +3 -0
- package/dist/math/linear.js +152 -0
- package/dist/math/omml.d.ts +15 -0
- package/dist/math/omml.js +57 -0
- package/dist/math/paths.d.ts +61 -0
- package/dist/math/paths.js +450 -0
- package/dist/math/symbols.d.ts +49 -0
- package/dist/math/symbols.js +269 -0
- package/dist/math/text.d.ts +53 -0
- package/dist/math/text.js +316 -0
- package/dist/metafile/canvas.d.ts +80 -0
- package/dist/metafile/canvas.js +515 -0
- package/dist/metafile/dib.d.ts +27 -0
- package/dist/metafile/dib.js +300 -0
- package/dist/metafile/emf.d.ts +31 -0
- package/dist/metafile/emf.js +591 -0
- package/dist/metafile/geometry.d.ts +49 -0
- package/dist/metafile/geometry.js +85 -0
- package/dist/metafile/index.d.ts +27 -0
- package/dist/metafile/index.js +34 -0
- package/dist/metafile/reader.d.ts +28 -0
- package/dist/metafile/reader.js +59 -0
- package/dist/metafile/state.d.ts +118 -0
- package/dist/metafile/state.js +172 -0
- package/dist/metafile/text.d.ts +17 -0
- package/dist/metafile/text.js +130 -0
- package/dist/metafile/wmf.d.ts +33 -0
- package/dist/metafile/wmf.js +395 -0
- package/dist/model/content.d.ts +92 -7
- package/dist/model/content.js +251 -17
- package/dist/model/document.d.ts +34 -1
- package/dist/model/document.js +80 -1
- package/dist/model/fields.d.ts +4 -0
- package/dist/model/fields.js +10 -0
- package/dist/model/numbering.d.ts +2 -0
- package/dist/model/numbering.js +22 -0
- package/dist/model/props.d.ts +2 -0
- package/dist/model/props.js +3 -0
- package/dist/opc/index.d.ts +1 -1
- package/dist/opc/package.d.ts +34 -0
- package/dist/opc/package.js +58 -4
- package/dist/pdf/export.js +97 -6
- package/dist/pdf/images.d.ts +7 -0
- package/dist/pdf/images.js +39 -2
- package/dist/render/activity.d.ts +138 -0
- package/dist/render/activity.js +794 -0
- package/dist/render/clipboard.d.ts +29 -0
- package/dist/render/clipboard.js +401 -0
- package/dist/render/comments.d.ts +166 -0
- package/dist/render/comments.js +779 -0
- package/dist/render/context-menu.d.ts +124 -0
- package/dist/render/context-menu.js +525 -0
- package/dist/render/editing.d.ts +146 -5
- package/dist/render/editing.js +678 -53
- package/dist/render/index.d.ts +12 -0
- package/dist/render/index.js +11 -0
- package/dist/render/navigation-panel.d.ts +76 -0
- package/dist/render/navigation-panel.js +431 -0
- package/dist/render/selection.js +5 -0
- package/dist/render/svg.d.ts +4 -6
- package/dist/render/svg.js +69 -7
- package/dist/render/toolbar.d.ts +83 -0
- package/dist/render/toolbar.js +505 -0
- package/dist/render/ui/comment-layout.d.ts +39 -0
- package/dist/render/ui/comment-layout.js +79 -0
- package/dist/render/ui/icons.d.ts +6 -0
- package/dist/render/ui/icons.js +38 -0
- package/dist/render/ui/menu.d.ts +38 -0
- package/dist/render/ui/menu.js +274 -0
- package/dist/render/ui/nav-model.d.ts +11 -0
- package/dist/render/ui/nav-model.js +34 -0
- package/dist/render/ui/overflow.d.ts +5 -0
- package/dist/render/ui/overflow.js +18 -0
- package/dist/render/ui/popover.d.ts +18 -0
- package/dist/render/ui/popover.js +109 -0
- package/dist/render/ui/theme.d.ts +2 -0
- package/dist/render/ui/theme.js +29 -0
- package/dist/render/undo.d.ts +76 -0
- package/dist/render/undo.js +144 -0
- package/dist/render/viewer.d.ts +100 -7
- package/dist/render/viewer.js +302 -92
- package/dist/security/policy.d.ts +3 -0
- package/dist/security/policy.js +6 -2
- package/dist/security/protection.d.ts +1 -0
- package/dist/security/protection.js +1 -1
- package/dist/text/bidi-data.d.ts +53 -0
- package/dist/text/bidi-data.js +318 -0
- package/dist/text/bidi.d.ts +37 -0
- package/dist/text/bidi.js +508 -0
- package/dist/xml/ns.d.ts +5 -0
- package/dist/xml/ns.js +7 -0
- package/package.json +9 -2
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Geometry for the metafile players: affine matrices laid out like a GDI XFORM, and the
|
|
3
|
+
* conversion of GDI ellipses and arcs into cubic Bézier segments (the display list has no arcs).
|
|
4
|
+
*/
|
|
5
|
+
export const IDENTITY = { a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 };
|
|
6
|
+
/** The transform that applies `first`, then `second` (GDI's row-vector product first × second). */
|
|
7
|
+
export function compose(first, second) {
|
|
8
|
+
return {
|
|
9
|
+
a: first.a * second.a + first.b * second.c,
|
|
10
|
+
b: first.a * second.b + first.b * second.d,
|
|
11
|
+
c: first.c * second.a + first.d * second.c,
|
|
12
|
+
d: first.c * second.b + first.d * second.d,
|
|
13
|
+
e: first.e * second.a + first.f * second.c + second.e,
|
|
14
|
+
f: first.e * second.b + first.f * second.d + second.f,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export function apply(m, x, y) {
|
|
18
|
+
return { x: m.a * x + m.c * y + m.e, y: m.b * x + m.d * y + m.f };
|
|
19
|
+
}
|
|
20
|
+
export function determinant(m) {
|
|
21
|
+
return m.a * m.d - m.b * m.c;
|
|
22
|
+
}
|
|
23
|
+
export function isIdentity(m) {
|
|
24
|
+
return m.a === 1 && m.b === 0 && m.c === 0 && m.d === 1 && m.e === 0 && m.f === 0;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Cubic segments for the elliptical arc centred on (cx, cy) with radii rx, ry, starting at
|
|
28
|
+
* parameter t0 and sweeping by `sweep` radians. The point at t is (cx + rx·cos t, cy + ry·sin t),
|
|
29
|
+
* so with y pointing down a positive sweep runs clockwise on screen. Segments span at most 90°,
|
|
30
|
+
* where the standard 4/3·tan(θ/4) control distance stays visually exact.
|
|
31
|
+
*/
|
|
32
|
+
export function arcCubics(cx, cy, rx, ry, t0, sweep) {
|
|
33
|
+
const n = Math.max(1, Math.ceil(Math.abs(sweep) / (Math.PI / 2) - 1e-9));
|
|
34
|
+
const step = sweep / n;
|
|
35
|
+
const k = (4 / 3) * Math.tan(step / 4);
|
|
36
|
+
const out = [];
|
|
37
|
+
for (let i = 0; i < n; i++) {
|
|
38
|
+
const ta = t0 + i * step;
|
|
39
|
+
const tb = ta + step;
|
|
40
|
+
const ca = Math.cos(ta);
|
|
41
|
+
const sa = Math.sin(ta);
|
|
42
|
+
const cb = Math.cos(tb);
|
|
43
|
+
const sb = Math.sin(tb);
|
|
44
|
+
const p0 = { x: cx + rx * ca, y: cy + ry * sa };
|
|
45
|
+
const p3 = { x: cx + rx * cb, y: cy + ry * sb };
|
|
46
|
+
out.push([
|
|
47
|
+
{ x: p0.x - k * rx * sa, y: p0.y + k * ry * ca },
|
|
48
|
+
{ x: p3.x + k * rx * sb, y: p3.y - k * ry * cb },
|
|
49
|
+
p3,
|
|
50
|
+
]);
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The arc GDI's Arc/Chord/Pie/ArcTo draw: on the ellipse inscribed in the box, from where the ray
|
|
56
|
+
* from the centre through (xs, ys) meets it to where the ray through (xe, ye) does. `clockwise`
|
|
57
|
+
* is in the y-down sense of the coordinates given. Equal rays mean a full ellipse, as in GDI.
|
|
58
|
+
*/
|
|
59
|
+
export function gdiArc(left, top, right, bottom, xs, ys, xe, ye, clockwise) {
|
|
60
|
+
const cx = (left + right) / 2;
|
|
61
|
+
const cy = (top + bottom) / 2;
|
|
62
|
+
const rx = Math.abs(right - left) / 2;
|
|
63
|
+
const ry = Math.abs(bottom - top) / 2;
|
|
64
|
+
// Scaling the ray direction onto the unit circle gives the ellipse parameter where the ray meets it.
|
|
65
|
+
const ts = Math.atan2((ys - cy) / (ry || 1), (xs - cx) / (rx || 1));
|
|
66
|
+
const te = Math.atan2((ye - cy) / (ry || 1), (xe - cx) / (rx || 1));
|
|
67
|
+
let sweep = clockwise ? te - ts : ts - te;
|
|
68
|
+
while (sweep <= 1e-9)
|
|
69
|
+
sweep += 2 * Math.PI;
|
|
70
|
+
while (sweep > 2 * Math.PI + 1e-9)
|
|
71
|
+
sweep -= 2 * Math.PI;
|
|
72
|
+
const signed = clockwise ? sweep : -sweep;
|
|
73
|
+
const cubics = arcCubics(cx, cy, rx, ry, ts, signed);
|
|
74
|
+
const start = { x: cx + rx * Math.cos(ts), y: cy + ry * Math.sin(ts) };
|
|
75
|
+
const end = cubics.length ? cubics[cubics.length - 1][2] : start;
|
|
76
|
+
return { start, end, cubics, centre: { x: cx, y: cy } };
|
|
77
|
+
}
|
|
78
|
+
/** Cubic segments of a full ellipse in a box, starting at its rightmost point. */
|
|
79
|
+
export function ellipseCubics(left, top, right, bottom) {
|
|
80
|
+
const cx = (left + right) / 2;
|
|
81
|
+
const cy = (top + bottom) / 2;
|
|
82
|
+
const rx = Math.abs(right - left) / 2;
|
|
83
|
+
const ry = Math.abs(bottom - top) / 2;
|
|
84
|
+
return { start: { x: cx + rx, y: cy }, cubics: arcCubics(cx, cy, rx, ry, 0, 2 * Math.PI) };
|
|
85
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Windows metafiles (EMF and WMF), the vector pictures old Word documents embed for equations,
|
|
3
|
+
* Visio drawings and pasted charts: format detection, natural size, and rendering into the
|
|
4
|
+
* layout display list so the SVG viewer and the PDF exporter draw them like any other shape.
|
|
5
|
+
*/
|
|
6
|
+
import type { DisplayItem, FontProvider } from '../layout/types.js';
|
|
7
|
+
export type MetafileKind = 'emf' | 'wmf';
|
|
8
|
+
export declare function metafileKind(bytes: Uint8Array): MetafileKind | undefined;
|
|
9
|
+
/**
|
|
10
|
+
* Natural size in points. EMF: the header's frame rectangle (0.01 mm). WMF: the placeable
|
|
11
|
+
* header's bounding box at its units per inch, else the window extent at 1440 units per inch.
|
|
12
|
+
*/
|
|
13
|
+
export declare function metafileSize(bytes: Uint8Array): {
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
} | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Draw a metafile scaled to fill `frame` (page points, y down). Returns no items for data that is
|
|
19
|
+
* not a metafile (or an EMF+-only picture, whose drawing lives entirely in EMF+ records);
|
|
20
|
+
* damaged records are skipped rather than failing the whole picture.
|
|
21
|
+
*/
|
|
22
|
+
export declare function renderMetafile(bytes: Uint8Array, frame: {
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
w: number;
|
|
26
|
+
h: number;
|
|
27
|
+
}, fonts: FontProvider): DisplayItem[];
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { emfSize, isEmf, renderEmf } from './emf.js';
|
|
2
|
+
import { readWmfHeader, renderWmf, wmfSize } from './wmf.js';
|
|
3
|
+
export function metafileKind(bytes) {
|
|
4
|
+
if (isEmf(bytes))
|
|
5
|
+
return 'emf';
|
|
6
|
+
if (readWmfHeader(bytes))
|
|
7
|
+
return 'wmf';
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Natural size in points. EMF: the header's frame rectangle (0.01 mm). WMF: the placeable
|
|
12
|
+
* header's bounding box at its units per inch, else the window extent at 1440 units per inch.
|
|
13
|
+
*/
|
|
14
|
+
export function metafileSize(bytes) {
|
|
15
|
+
const kind = metafileKind(bytes);
|
|
16
|
+
if (kind === 'emf')
|
|
17
|
+
return emfSize(bytes);
|
|
18
|
+
if (kind === 'wmf')
|
|
19
|
+
return wmfSize(bytes);
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Draw a metafile scaled to fill `frame` (page points, y down). Returns no items for data that is
|
|
24
|
+
* not a metafile (or an EMF+-only picture, whose drawing lives entirely in EMF+ records);
|
|
25
|
+
* damaged records are skipped rather than failing the whole picture.
|
|
26
|
+
*/
|
|
27
|
+
export function renderMetafile(bytes, frame, fonts) {
|
|
28
|
+
const kind = metafileKind(bytes);
|
|
29
|
+
if (kind === 'emf')
|
|
30
|
+
return renderEmf(bytes, frame, fonts);
|
|
31
|
+
if (kind === 'wmf')
|
|
32
|
+
return renderWmf(bytes, frame, fonts);
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Little-endian field access for one metafile record. Reads outside the record return 0 (and
|
|
3
|
+
* byte slices come back short) instead of throwing, because metafiles in the wild are often
|
|
4
|
+
* truncated or carry wrong counts, and a damaged record should not lose the rest of the picture.
|
|
5
|
+
*/
|
|
6
|
+
export declare class RecordReader {
|
|
7
|
+
readonly bytes: Uint8Array;
|
|
8
|
+
readonly start: number;
|
|
9
|
+
readonly end: number;
|
|
10
|
+
private readonly view;
|
|
11
|
+
/**
|
|
12
|
+
* `start` and `end` are absolute offsets in `bytes`; field offsets are relative to `start`.
|
|
13
|
+
* Pass `view` (over the same bytes) to share one DataView across a file's many records.
|
|
14
|
+
*/
|
|
15
|
+
constructor(bytes: Uint8Array, start: number, end: number, view?: DataView<ArrayBufferLike>);
|
|
16
|
+
get size(): number;
|
|
17
|
+
private ok;
|
|
18
|
+
u8(offset: number): number;
|
|
19
|
+
u16(offset: number): number;
|
|
20
|
+
i16(offset: number): number;
|
|
21
|
+
u32(offset: number): number;
|
|
22
|
+
i32(offset: number): number;
|
|
23
|
+
f32(offset: number): number;
|
|
24
|
+
/** Up to `length` bytes from `offset`, clipped to the record. */
|
|
25
|
+
slice(offset: number, length: number): Uint8Array;
|
|
26
|
+
/** How many items of `itemSize` bytes fit between `offset` and the end of the record (caps bogus counts). */
|
|
27
|
+
fit(offset: number, count: number, itemSize: number): number;
|
|
28
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Little-endian field access for one metafile record. Reads outside the record return 0 (and
|
|
3
|
+
* byte slices come back short) instead of throwing, because metafiles in the wild are often
|
|
4
|
+
* truncated or carry wrong counts, and a damaged record should not lose the rest of the picture.
|
|
5
|
+
*/
|
|
6
|
+
export class RecordReader {
|
|
7
|
+
bytes;
|
|
8
|
+
start;
|
|
9
|
+
end;
|
|
10
|
+
view;
|
|
11
|
+
/**
|
|
12
|
+
* `start` and `end` are absolute offsets in `bytes`; field offsets are relative to `start`.
|
|
13
|
+
* Pass `view` (over the same bytes) to share one DataView across a file's many records.
|
|
14
|
+
*/
|
|
15
|
+
constructor(bytes, start, end, view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)) {
|
|
16
|
+
this.bytes = bytes;
|
|
17
|
+
this.start = start;
|
|
18
|
+
this.end = end;
|
|
19
|
+
this.view = view;
|
|
20
|
+
}
|
|
21
|
+
get size() {
|
|
22
|
+
return this.end - this.start;
|
|
23
|
+
}
|
|
24
|
+
ok(offset, length) {
|
|
25
|
+
return offset >= 0 && this.start + offset + length <= this.end;
|
|
26
|
+
}
|
|
27
|
+
u8(offset) {
|
|
28
|
+
return this.ok(offset, 1) ? this.view.getUint8(this.start + offset) : 0;
|
|
29
|
+
}
|
|
30
|
+
u16(offset) {
|
|
31
|
+
return this.ok(offset, 2) ? this.view.getUint16(this.start + offset, true) : 0;
|
|
32
|
+
}
|
|
33
|
+
i16(offset) {
|
|
34
|
+
return this.ok(offset, 2) ? this.view.getInt16(this.start + offset, true) : 0;
|
|
35
|
+
}
|
|
36
|
+
u32(offset) {
|
|
37
|
+
return this.ok(offset, 4) ? this.view.getUint32(this.start + offset, true) : 0;
|
|
38
|
+
}
|
|
39
|
+
i32(offset) {
|
|
40
|
+
return this.ok(offset, 4) ? this.view.getInt32(this.start + offset, true) : 0;
|
|
41
|
+
}
|
|
42
|
+
f32(offset) {
|
|
43
|
+
if (!this.ok(offset, 4))
|
|
44
|
+
return 0;
|
|
45
|
+
const v = this.view.getFloat32(this.start + offset, true);
|
|
46
|
+
return Number.isFinite(v) ? v : 0;
|
|
47
|
+
}
|
|
48
|
+
/** Up to `length` bytes from `offset`, clipped to the record. */
|
|
49
|
+
slice(offset, length) {
|
|
50
|
+
const from = Math.max(this.start, Math.min(this.end, this.start + offset));
|
|
51
|
+
const to = Math.max(from, Math.min(this.end, from + Math.max(0, length)));
|
|
52
|
+
return this.bytes.subarray(from, to);
|
|
53
|
+
}
|
|
54
|
+
/** How many items of `itemSize` bytes fit between `offset` and the end of the record (caps bogus counts). */
|
|
55
|
+
fit(offset, count, itemSize) {
|
|
56
|
+
const room = Math.floor((this.size - offset) / itemSize);
|
|
57
|
+
return Math.max(0, Math.min(count, room));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { Matrix, Pt } from './geometry.js';
|
|
2
|
+
export declare const MM_TEXT = 1;
|
|
3
|
+
export declare const MM_LOMETRIC = 2;
|
|
4
|
+
export declare const MM_HIMETRIC = 3;
|
|
5
|
+
export declare const MM_LOENGLISH = 4;
|
|
6
|
+
export declare const MM_HIENGLISH = 5;
|
|
7
|
+
export declare const MM_TWIPS = 6;
|
|
8
|
+
export declare const MM_ISOTROPIC = 7;
|
|
9
|
+
export declare const MM_ANISOTROPIC = 8;
|
|
10
|
+
export declare const PS_SOLID = 0;
|
|
11
|
+
export declare const PS_DASH = 1;
|
|
12
|
+
export declare const PS_DOT = 2;
|
|
13
|
+
export declare const PS_DASHDOT = 3;
|
|
14
|
+
export declare const PS_DASHDOTDOT = 4;
|
|
15
|
+
export declare const PS_NULL = 5;
|
|
16
|
+
export declare const PS_INSIDEFRAME = 6;
|
|
17
|
+
export declare const PS_USERSTYLE = 7;
|
|
18
|
+
export declare const PS_ALTERNATE = 8;
|
|
19
|
+
export declare const PS_GEOMETRIC = 65536;
|
|
20
|
+
export declare const BS_SOLID = 0;
|
|
21
|
+
export declare const BS_NULL = 1;
|
|
22
|
+
export declare const BS_HATCHED = 2;
|
|
23
|
+
export declare const BS_PATTERN = 3;
|
|
24
|
+
export declare const BS_DIBPATTERN = 5;
|
|
25
|
+
export declare const BS_DIBPATTERNPT = 6;
|
|
26
|
+
export declare const TRANSPARENT = 1;
|
|
27
|
+
export declare const OPAQUE = 2;
|
|
28
|
+
export declare const ALTERNATE = 1;
|
|
29
|
+
export declare const WINDING = 2;
|
|
30
|
+
export declare const AD_COUNTERCLOCKWISE = 1;
|
|
31
|
+
export declare const AD_CLOCKWISE = 2;
|
|
32
|
+
export declare const TA_UPDATECP = 1;
|
|
33
|
+
export declare const TA_RIGHT = 2;
|
|
34
|
+
export declare const TA_CENTER = 6;
|
|
35
|
+
export declare const TA_BOTTOM = 8;
|
|
36
|
+
export declare const TA_BASELINE = 24;
|
|
37
|
+
export declare const ETO_OPAQUE = 2;
|
|
38
|
+
export declare const ETO_CLIPPED = 4;
|
|
39
|
+
export declare const ETO_GLYPH_INDEX = 16;
|
|
40
|
+
export declare const ETO_NO_RECT = 256;
|
|
41
|
+
export declare const ETO_SMALL_CHARS = 512;
|
|
42
|
+
export declare const ETO_PDY = 8192;
|
|
43
|
+
/** Raster operations the players understand; anything else that needs the destination is skipped. */
|
|
44
|
+
export declare const SRCCOPY = 13369376;
|
|
45
|
+
export declare const PATCOPY = 15728673;
|
|
46
|
+
export declare const BLACKNESS = 66;
|
|
47
|
+
export declare const WHITENESS = 16711778;
|
|
48
|
+
export interface Pen {
|
|
49
|
+
kind: 'pen';
|
|
50
|
+
/** PS_* line style (the low nibble of the pen style). */
|
|
51
|
+
style: number;
|
|
52
|
+
/** Width in logical units; 0 means one device pixel. */
|
|
53
|
+
width: number;
|
|
54
|
+
/** Cosmetic pens are one device pixel wide whatever the transform. */
|
|
55
|
+
cosmetic: boolean;
|
|
56
|
+
color: string;
|
|
57
|
+
cap: 'round' | 'square' | 'butt';
|
|
58
|
+
join: 'round' | 'bevel' | 'miter';
|
|
59
|
+
/** PS_USERSTYLE dash lengths (logical units for geometric pens, pixels for cosmetic ones). */
|
|
60
|
+
dashes?: number[];
|
|
61
|
+
}
|
|
62
|
+
export interface Brush {
|
|
63
|
+
kind: 'brush';
|
|
64
|
+
style: 'solid' | 'null' | 'hatched' | 'pattern';
|
|
65
|
+
color: string;
|
|
66
|
+
}
|
|
67
|
+
export interface Font {
|
|
68
|
+
kind: 'font';
|
|
69
|
+
/** Logical units: negative is the em height, positive the cell height, 0 the default size. */
|
|
70
|
+
height: number;
|
|
71
|
+
weight: number;
|
|
72
|
+
italic: boolean;
|
|
73
|
+
underline: boolean;
|
|
74
|
+
strikeOut: boolean;
|
|
75
|
+
charset: number;
|
|
76
|
+
faceName: string;
|
|
77
|
+
}
|
|
78
|
+
/** Palettes, regions and colour spaces: they occupy object slots but draw nothing here. */
|
|
79
|
+
export interface OtherObject {
|
|
80
|
+
kind: 'other';
|
|
81
|
+
}
|
|
82
|
+
export type GdiObject = Pen | Brush | Font | OtherObject;
|
|
83
|
+
export interface DCState {
|
|
84
|
+
mapMode: number;
|
|
85
|
+
windowOrg: Pt;
|
|
86
|
+
windowExt: Pt;
|
|
87
|
+
viewportOrg: Pt;
|
|
88
|
+
viewportExt: Pt;
|
|
89
|
+
/** World → page transform (EMF only; identity for WMF). */
|
|
90
|
+
world: Matrix;
|
|
91
|
+
/** Set once a world transform is used: GDI then is in GM_ADVANCED, which changes how arcs turn. */
|
|
92
|
+
advanced: boolean;
|
|
93
|
+
pen: Pen;
|
|
94
|
+
brush: Brush;
|
|
95
|
+
font: Font;
|
|
96
|
+
textColor: string;
|
|
97
|
+
bkColor: string;
|
|
98
|
+
bkMode: number;
|
|
99
|
+
textAlign: number;
|
|
100
|
+
polyFillMode: number;
|
|
101
|
+
arcDirection: number;
|
|
102
|
+
/** Current position in logical units. */
|
|
103
|
+
position: Pt;
|
|
104
|
+
/** Extra space after each character (SetTextCharacterExtra), logical units. */
|
|
105
|
+
charExtra: number;
|
|
106
|
+
}
|
|
107
|
+
/** Pen style bits (PS_ENDCAP_*, PS_JOIN_*, PS_GEOMETRIC) to a pen. */
|
|
108
|
+
export declare function makePen(styleBits: number, width: number, color: string, dashes?: number[]): Pen;
|
|
109
|
+
export declare function makeBrush(style: number, color: string): Brush;
|
|
110
|
+
/** GDI stock objects, addressed in metafiles with the high bit set (EMF) or not at all (WMF). */
|
|
111
|
+
export declare function stockObject(index: number): GdiObject | undefined;
|
|
112
|
+
export declare function defaultState(): DCState;
|
|
113
|
+
/** Make a selected object current in the state; other kinds are ignored. */
|
|
114
|
+
export declare function selectInto(state: DCState, obj: GdiObject | undefined): void;
|
|
115
|
+
/** A COLORREF (R, G, B, flags) as RRGGBB. Palette-index colours keep their bytes as an approximation. */
|
|
116
|
+
export declare function colorRef(r: number, g: number, b: number): string;
|
|
117
|
+
/** Mix two RRGGBB colours: `t` of `a` and the rest of `b`. */
|
|
118
|
+
export declare function mixColors(a: string, b: string, t: number): string;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Device-context state shared by the EMF and WMF players: the GDI objects a picture selects
|
|
3
|
+
* (pens, brushes, fonts), the attributes SaveDC/RestoreDC preserve, GDI's constants and its
|
|
4
|
+
* stock objects. Points in the state are replaced, never mutated, so a saved copy stays intact.
|
|
5
|
+
*/
|
|
6
|
+
import { IDENTITY } from './geometry.js';
|
|
7
|
+
export const MM_TEXT = 1;
|
|
8
|
+
export const MM_LOMETRIC = 2;
|
|
9
|
+
export const MM_HIMETRIC = 3;
|
|
10
|
+
export const MM_LOENGLISH = 4;
|
|
11
|
+
export const MM_HIENGLISH = 5;
|
|
12
|
+
export const MM_TWIPS = 6;
|
|
13
|
+
export const MM_ISOTROPIC = 7;
|
|
14
|
+
export const MM_ANISOTROPIC = 8;
|
|
15
|
+
export const PS_SOLID = 0;
|
|
16
|
+
export const PS_DASH = 1;
|
|
17
|
+
export const PS_DOT = 2;
|
|
18
|
+
export const PS_DASHDOT = 3;
|
|
19
|
+
export const PS_DASHDOTDOT = 4;
|
|
20
|
+
export const PS_NULL = 5;
|
|
21
|
+
export const PS_INSIDEFRAME = 6;
|
|
22
|
+
export const PS_USERSTYLE = 7;
|
|
23
|
+
export const PS_ALTERNATE = 8;
|
|
24
|
+
export const PS_GEOMETRIC = 0x10000;
|
|
25
|
+
export const BS_SOLID = 0;
|
|
26
|
+
export const BS_NULL = 1;
|
|
27
|
+
export const BS_HATCHED = 2;
|
|
28
|
+
export const BS_PATTERN = 3;
|
|
29
|
+
export const BS_DIBPATTERN = 5;
|
|
30
|
+
export const BS_DIBPATTERNPT = 6;
|
|
31
|
+
export const TRANSPARENT = 1;
|
|
32
|
+
export const OPAQUE = 2;
|
|
33
|
+
export const ALTERNATE = 1;
|
|
34
|
+
export const WINDING = 2;
|
|
35
|
+
export const AD_COUNTERCLOCKWISE = 1;
|
|
36
|
+
export const AD_CLOCKWISE = 2;
|
|
37
|
+
export const TA_UPDATECP = 1;
|
|
38
|
+
export const TA_RIGHT = 2;
|
|
39
|
+
export const TA_CENTER = 6;
|
|
40
|
+
export const TA_BOTTOM = 8;
|
|
41
|
+
export const TA_BASELINE = 24;
|
|
42
|
+
export const ETO_OPAQUE = 0x2;
|
|
43
|
+
export const ETO_CLIPPED = 0x4;
|
|
44
|
+
export const ETO_GLYPH_INDEX = 0x10;
|
|
45
|
+
export const ETO_NO_RECT = 0x100;
|
|
46
|
+
export const ETO_SMALL_CHARS = 0x200;
|
|
47
|
+
export const ETO_PDY = 0x2000;
|
|
48
|
+
/** Raster operations the players understand; anything else that needs the destination is skipped. */
|
|
49
|
+
export const SRCCOPY = 0x00cc0020;
|
|
50
|
+
export const PATCOPY = 0x00f00021;
|
|
51
|
+
export const BLACKNESS = 0x00000042;
|
|
52
|
+
export const WHITENESS = 0x00ff0062;
|
|
53
|
+
/** Pen style bits (PS_ENDCAP_*, PS_JOIN_*, PS_GEOMETRIC) to a pen. */
|
|
54
|
+
export function makePen(styleBits, width, color, dashes) {
|
|
55
|
+
const capBits = styleBits & 0xf00;
|
|
56
|
+
const joinBits = styleBits & 0xf000;
|
|
57
|
+
const style = styleBits & 0xf;
|
|
58
|
+
const pen = {
|
|
59
|
+
kind: 'pen',
|
|
60
|
+
style,
|
|
61
|
+
width: Math.abs(width),
|
|
62
|
+
// ExtCreatePen marks geometric pens; a LOGPEN's width is always in logical units.
|
|
63
|
+
cosmetic: false,
|
|
64
|
+
color,
|
|
65
|
+
cap: capBits === 0x100 ? 'square' : capBits === 0x200 ? 'butt' : 'round',
|
|
66
|
+
join: joinBits === 0x1000 ? 'bevel' : joinBits === 0x2000 ? 'miter' : 'round',
|
|
67
|
+
};
|
|
68
|
+
if (dashes && dashes.length)
|
|
69
|
+
pen.dashes = dashes;
|
|
70
|
+
return pen;
|
|
71
|
+
}
|
|
72
|
+
export function makeBrush(style, color) {
|
|
73
|
+
switch (style) {
|
|
74
|
+
case BS_SOLID:
|
|
75
|
+
return { kind: 'brush', style: 'solid', color };
|
|
76
|
+
case BS_HATCHED:
|
|
77
|
+
return { kind: 'brush', style: 'hatched', color };
|
|
78
|
+
case BS_PATTERN:
|
|
79
|
+
case BS_DIBPATTERN:
|
|
80
|
+
case BS_DIBPATTERNPT:
|
|
81
|
+
// Without the bitmap at hand a pattern reads best as mid grey.
|
|
82
|
+
return { kind: 'brush', style: 'pattern', color: '808080' };
|
|
83
|
+
default:
|
|
84
|
+
return { kind: 'brush', style: 'null', color };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const SYSTEM_FONT = { kind: 'font', height: 0, weight: 400, italic: false, underline: false, strikeOut: false, charset: 0, faceName: 'Arial' };
|
|
88
|
+
/** GDI stock objects, addressed in metafiles with the high bit set (EMF) or not at all (WMF). */
|
|
89
|
+
export function stockObject(index) {
|
|
90
|
+
const brush = (color) => ({ kind: 'brush', style: 'solid', color });
|
|
91
|
+
const pen = (color) => ({ kind: 'pen', style: PS_SOLID, width: 0, cosmetic: true, color, cap: 'round', join: 'round' });
|
|
92
|
+
switch (index) {
|
|
93
|
+
case 0:
|
|
94
|
+
return brush('FFFFFF');
|
|
95
|
+
case 1:
|
|
96
|
+
return brush('C0C0C0');
|
|
97
|
+
case 2:
|
|
98
|
+
return brush('808080');
|
|
99
|
+
case 3:
|
|
100
|
+
return brush('404040');
|
|
101
|
+
case 4:
|
|
102
|
+
return brush('000000');
|
|
103
|
+
case 5:
|
|
104
|
+
return { kind: 'brush', style: 'null', color: '000000' };
|
|
105
|
+
case 6:
|
|
106
|
+
return pen('FFFFFF');
|
|
107
|
+
case 7:
|
|
108
|
+
return pen('000000');
|
|
109
|
+
case 8:
|
|
110
|
+
return { ...pen('000000'), style: PS_NULL };
|
|
111
|
+
case 10:
|
|
112
|
+
case 11:
|
|
113
|
+
case 16:
|
|
114
|
+
return { ...SYSTEM_FONT, faceName: 'Courier New' };
|
|
115
|
+
case 12:
|
|
116
|
+
case 13:
|
|
117
|
+
case 14:
|
|
118
|
+
case 17:
|
|
119
|
+
return SYSTEM_FONT;
|
|
120
|
+
case 18:
|
|
121
|
+
return brush('FFFFFF');
|
|
122
|
+
case 19:
|
|
123
|
+
return pen('000000');
|
|
124
|
+
default:
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
export function defaultState() {
|
|
129
|
+
return {
|
|
130
|
+
mapMode: MM_TEXT,
|
|
131
|
+
windowOrg: { x: 0, y: 0 },
|
|
132
|
+
windowExt: { x: 1, y: 1 },
|
|
133
|
+
viewportOrg: { x: 0, y: 0 },
|
|
134
|
+
viewportExt: { x: 1, y: 1 },
|
|
135
|
+
world: IDENTITY,
|
|
136
|
+
advanced: false,
|
|
137
|
+
pen: stockObject(7),
|
|
138
|
+
brush: stockObject(0),
|
|
139
|
+
font: SYSTEM_FONT,
|
|
140
|
+
textColor: '000000',
|
|
141
|
+
bkColor: 'FFFFFF',
|
|
142
|
+
bkMode: OPAQUE,
|
|
143
|
+
textAlign: 0,
|
|
144
|
+
polyFillMode: ALTERNATE,
|
|
145
|
+
arcDirection: AD_COUNTERCLOCKWISE,
|
|
146
|
+
position: { x: 0, y: 0 },
|
|
147
|
+
charExtra: 0,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/** Make a selected object current in the state; other kinds are ignored. */
|
|
151
|
+
export function selectInto(state, obj) {
|
|
152
|
+
if (!obj)
|
|
153
|
+
return;
|
|
154
|
+
if (obj.kind === 'pen')
|
|
155
|
+
state.pen = obj;
|
|
156
|
+
else if (obj.kind === 'brush')
|
|
157
|
+
state.brush = obj;
|
|
158
|
+
else if (obj.kind === 'font')
|
|
159
|
+
state.font = obj;
|
|
160
|
+
}
|
|
161
|
+
/** A COLORREF (R, G, B, flags) as RRGGBB. Palette-index colours keep their bytes as an approximation. */
|
|
162
|
+
export function colorRef(r, g, b) {
|
|
163
|
+
return hex2(r) + hex2(g) + hex2(b);
|
|
164
|
+
}
|
|
165
|
+
function hex2(v) {
|
|
166
|
+
return (Math.max(0, Math.min(255, Math.round(v))) | 0).toString(16).padStart(2, '0').toUpperCase();
|
|
167
|
+
}
|
|
168
|
+
/** Mix two RRGGBB colours: `t` of `a` and the rest of `b`. */
|
|
169
|
+
export function mixColors(a, b, t) {
|
|
170
|
+
const ch = (s, i) => Number.parseInt(s.slice(i, i + 2), 16) || 0;
|
|
171
|
+
return colorRef(ch(a, 0) * t + ch(b, 0) * (1 - t), ch(a, 2) * t + ch(b, 2) * (1 - t), ch(a, 4) * t + ch(b, 4) * (1 - t));
|
|
172
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text decoding for metafile text records: 8-bit strings by the font's character set, UTF-16
|
|
3
|
+
* strings with surrogate pairs, and the Symbol font's private encoding (old Equation Editor
|
|
4
|
+
* pictures write Greek letters and maths operators as Symbol-font bytes) mapped to Unicode.
|
|
5
|
+
* Each decoder keeps the per-character advances (Dx) aligned with the characters it returns.
|
|
6
|
+
*/
|
|
7
|
+
export interface DecodedText {
|
|
8
|
+
chars: string[];
|
|
9
|
+
/** One advance per character (logical units), when the record had them. */
|
|
10
|
+
dx: number[] | undefined;
|
|
11
|
+
}
|
|
12
|
+
/** Decode an 8-bit string in the given charset; `faceName` picks the Symbol mapping. */
|
|
13
|
+
export declare function decodeAnsi(bytes: Uint8Array, charset: number, faceName: string, dx: number[] | undefined): DecodedText;
|
|
14
|
+
/** Decode UTF-16 code units, joining surrogate pairs (whose two Dx entries add up). */
|
|
15
|
+
export declare function decodeWide(units: number[], faceName: string, dx: number[] | undefined): DecodedText;
|
|
16
|
+
export declare function isSymbolFace(faceName: string): boolean;
|
|
17
|
+
export declare function symbolChar(code: number): string;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text decoding for metafile text records: 8-bit strings by the font's character set, UTF-16
|
|
3
|
+
* strings with surrogate pairs, and the Symbol font's private encoding (old Equation Editor
|
|
4
|
+
* pictures write Greek letters and maths operators as Symbol-font bytes) mapped to Unicode.
|
|
5
|
+
* Each decoder keeps the per-character advances (Dx) aligned with the characters it returns.
|
|
6
|
+
*/
|
|
7
|
+
/** Windows charset (LOGFONT lfCharSet) → WHATWG encoding label for TextDecoder. */
|
|
8
|
+
const CHARSET_ENCODINGS = {
|
|
9
|
+
0: 'windows-1252',
|
|
10
|
+
1: 'windows-1252',
|
|
11
|
+
128: 'shift_jis',
|
|
12
|
+
129: 'euc-kr',
|
|
13
|
+
134: 'gbk',
|
|
14
|
+
136: 'big5',
|
|
15
|
+
161: 'windows-1253',
|
|
16
|
+
162: 'windows-1254',
|
|
17
|
+
163: 'windows-1258',
|
|
18
|
+
177: 'windows-1255',
|
|
19
|
+
178: 'windows-1256',
|
|
20
|
+
186: 'windows-1257',
|
|
21
|
+
204: 'windows-1251',
|
|
22
|
+
222: 'windows-874',
|
|
23
|
+
238: 'windows-1250',
|
|
24
|
+
};
|
|
25
|
+
const SYMBOL_CHARSET = 2;
|
|
26
|
+
/** Lead bytes of the double-byte charsets: those characters take two bytes (and two Dx entries). */
|
|
27
|
+
function isLeadByte(charset, b) {
|
|
28
|
+
if (charset === 128)
|
|
29
|
+
return (b >= 0x81 && b <= 0x9f) || (b >= 0xe0 && b <= 0xfc);
|
|
30
|
+
if (charset === 129 || charset === 134 || charset === 136)
|
|
31
|
+
return b >= 0x81 && b <= 0xfe;
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const decoders = new Map();
|
|
35
|
+
function decoderFor(label) {
|
|
36
|
+
let d = decoders.get(label);
|
|
37
|
+
if (d === undefined) {
|
|
38
|
+
try {
|
|
39
|
+
d = new TextDecoder(label);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// Runtimes without full ICU lack the legacy encodings; fall back to Latin-1 there.
|
|
43
|
+
d = null;
|
|
44
|
+
}
|
|
45
|
+
decoders.set(label, d);
|
|
46
|
+
}
|
|
47
|
+
return d;
|
|
48
|
+
}
|
|
49
|
+
function latin1(bytes) {
|
|
50
|
+
let s = '';
|
|
51
|
+
for (const b of bytes)
|
|
52
|
+
s += String.fromCharCode(b);
|
|
53
|
+
return s;
|
|
54
|
+
}
|
|
55
|
+
/** Decode an 8-bit string in the given charset; `faceName` picks the Symbol mapping. */
|
|
56
|
+
export function decodeAnsi(bytes, charset, faceName, dx) {
|
|
57
|
+
const chars = [];
|
|
58
|
+
const out = [];
|
|
59
|
+
const symbol = isSymbolFace(faceName);
|
|
60
|
+
if (symbol || charset === SYMBOL_CHARSET) {
|
|
61
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
62
|
+
const b = bytes[i];
|
|
63
|
+
chars.push(symbol ? symbolChar(b) : String.fromCharCode(b));
|
|
64
|
+
out.push(dx?.[i] ?? 0);
|
|
65
|
+
}
|
|
66
|
+
return { chars, dx: dx ? out : undefined };
|
|
67
|
+
}
|
|
68
|
+
const decoder = decoderFor(CHARSET_ENCODINGS[charset] ?? 'windows-1252');
|
|
69
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
70
|
+
const n = isLeadByte(charset, bytes[i]) && i + 1 < bytes.length ? 2 : 1;
|
|
71
|
+
const unit = bytes.subarray(i, i + n);
|
|
72
|
+
chars.push(decoder ? decoder.decode(unit) : latin1(unit));
|
|
73
|
+
out.push((dx?.[i] ?? 0) + (n === 2 ? (dx?.[i + 1] ?? 0) : 0));
|
|
74
|
+
i += n - 1;
|
|
75
|
+
}
|
|
76
|
+
return { chars, dx: dx ? out : undefined };
|
|
77
|
+
}
|
|
78
|
+
/** Decode UTF-16 code units, joining surrogate pairs (whose two Dx entries add up). */
|
|
79
|
+
export function decodeWide(units, faceName, dx) {
|
|
80
|
+
const chars = [];
|
|
81
|
+
const out = [];
|
|
82
|
+
const symbol = isSymbolFace(faceName);
|
|
83
|
+
for (let i = 0; i < units.length; i++) {
|
|
84
|
+
const u = units[i];
|
|
85
|
+
const next = units[i + 1];
|
|
86
|
+
if (u >= 0xd800 && u <= 0xdbff && next !== undefined && next >= 0xdc00 && next <= 0xdfff) {
|
|
87
|
+
chars.push(String.fromCharCode(u, next));
|
|
88
|
+
out.push((dx?.[i] ?? 0) + (dx?.[i + 1] ?? 0));
|
|
89
|
+
i++;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
// Symbol fonts are addressed through U+F020–U+F0FF (or plain bytes) in Unicode text.
|
|
93
|
+
if (symbol && ((u >= 0xf020 && u <= 0xf0ff) || (u >= 0x20 && u <= 0xff)))
|
|
94
|
+
chars.push(symbolChar(u & 0xff));
|
|
95
|
+
else
|
|
96
|
+
chars.push(String.fromCharCode(u));
|
|
97
|
+
out.push(dx?.[i] ?? 0);
|
|
98
|
+
}
|
|
99
|
+
return { chars, dx: dx ? out : undefined };
|
|
100
|
+
}
|
|
101
|
+
export function isSymbolFace(faceName) {
|
|
102
|
+
return faceName.trim().toLowerCase() === 'symbol';
|
|
103
|
+
}
|
|
104
|
+
/** Symbol-font byte → Unicode, for the codes that differ from ASCII/Latin-1. */
|
|
105
|
+
const SYMBOL = {
|
|
106
|
+
0x22: 0x2200, 0x24: 0x2203, 0x27: 0x220b, 0x2a: 0x2217, 0x2d: 0x2212, 0x40: 0x2245,
|
|
107
|
+
0x41: 0x0391, 0x42: 0x0392, 0x43: 0x03a7, 0x44: 0x0394, 0x45: 0x0395, 0x46: 0x03a6, 0x47: 0x0393, 0x48: 0x0397,
|
|
108
|
+
0x49: 0x0399, 0x4a: 0x03d1, 0x4b: 0x039a, 0x4c: 0x039b, 0x4d: 0x039c, 0x4e: 0x039d, 0x4f: 0x039f, 0x50: 0x03a0,
|
|
109
|
+
0x51: 0x0398, 0x52: 0x03a1, 0x53: 0x03a3, 0x54: 0x03a4, 0x55: 0x03a5, 0x56: 0x03c2, 0x57: 0x03a9, 0x58: 0x039e,
|
|
110
|
+
0x59: 0x03a8, 0x5a: 0x0396, 0x5c: 0x2234, 0x5e: 0x22a5, 0x60: 0x203e,
|
|
111
|
+
0x61: 0x03b1, 0x62: 0x03b2, 0x63: 0x03c7, 0x64: 0x03b4, 0x65: 0x03b5, 0x66: 0x03c6, 0x67: 0x03b3, 0x68: 0x03b7,
|
|
112
|
+
0x69: 0x03b9, 0x6a: 0x03d5, 0x6b: 0x03ba, 0x6c: 0x03bb, 0x6d: 0x03bc, 0x6e: 0x03bd, 0x6f: 0x03bf, 0x70: 0x03c0,
|
|
113
|
+
0x71: 0x03b8, 0x72: 0x03c1, 0x73: 0x03c3, 0x74: 0x03c4, 0x75: 0x03c5, 0x76: 0x03d6, 0x77: 0x03c9, 0x78: 0x03be,
|
|
114
|
+
0x79: 0x03c8, 0x7a: 0x03b6, 0x7e: 0x223c,
|
|
115
|
+
0xa0: 0x20ac, 0xa1: 0x03d2, 0xa2: 0x2032, 0xa3: 0x2264, 0xa4: 0x2044, 0xa5: 0x221e, 0xa6: 0x0192, 0xa7: 0x2663,
|
|
116
|
+
0xa8: 0x2666, 0xa9: 0x2665, 0xaa: 0x2660, 0xab: 0x2194, 0xac: 0x2190, 0xad: 0x2191, 0xae: 0x2192, 0xaf: 0x2193,
|
|
117
|
+
0xb2: 0x2033, 0xb3: 0x2265, 0xb4: 0x00d7, 0xb5: 0x221d, 0xb6: 0x2202, 0xb7: 0x2022, 0xb8: 0x00f7, 0xb9: 0x2260,
|
|
118
|
+
0xba: 0x2261, 0xbb: 0x2248, 0xbc: 0x2026, 0xbd: 0x23d0, 0xbe: 0x23af, 0xbf: 0x21b5,
|
|
119
|
+
0xc0: 0x2135, 0xc1: 0x2111, 0xc2: 0x211c, 0xc3: 0x2118, 0xc4: 0x2297, 0xc5: 0x2295, 0xc6: 0x2205, 0xc7: 0x2229,
|
|
120
|
+
0xc8: 0x222a, 0xc9: 0x2283, 0xca: 0x2287, 0xcb: 0x2284, 0xcc: 0x2282, 0xcd: 0x2286, 0xce: 0x2208, 0xcf: 0x2209,
|
|
121
|
+
0xd0: 0x2220, 0xd1: 0x2207, 0xd2: 0x00ae, 0xd3: 0x00a9, 0xd4: 0x2122, 0xd5: 0x220f, 0xd6: 0x221a, 0xd7: 0x22c5,
|
|
122
|
+
0xd8: 0x00ac, 0xd9: 0x2227, 0xda: 0x2228, 0xdb: 0x21d4, 0xdc: 0x21d0, 0xdd: 0x21d1, 0xde: 0x21d2, 0xdf: 0x21d3,
|
|
123
|
+
0xe0: 0x25ca, 0xe1: 0x27e8, 0xe2: 0x00ae, 0xe3: 0x00a9, 0xe4: 0x2122, 0xe5: 0x2211, 0xe6: 0x239b, 0xe7: 0x239c,
|
|
124
|
+
0xe8: 0x239d, 0xe9: 0x23a1, 0xea: 0x23a2, 0xeb: 0x23a3, 0xec: 0x23a7, 0xed: 0x23a8, 0xee: 0x23a9, 0xef: 0x23aa,
|
|
125
|
+
0xf1: 0x27e9, 0xf2: 0x222b, 0xf3: 0x2320, 0xf4: 0x23ae, 0xf5: 0x2321, 0xf6: 0x239e, 0xf7: 0x239f, 0xf8: 0x23a0,
|
|
126
|
+
0xf9: 0x23a4, 0xfa: 0x23a5, 0xfb: 0x23a6, 0xfc: 0x23ab, 0xfd: 0x23ac, 0xfe: 0x23ad,
|
|
127
|
+
};
|
|
128
|
+
export function symbolChar(code) {
|
|
129
|
+
return String.fromCodePoint(SYMBOL[code] ?? code);
|
|
130
|
+
}
|