rtf-codec 0.0.0 → 1.1.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/LICENSE +21 -0
- package/README.md +226 -0
- package/dist/base64.cjs +66 -0
- package/dist/base64.d.cts +7 -0
- package/dist/base64.d.ts +7 -0
- package/dist/base64.js +62 -0
- package/dist/bytes.cjs +25 -0
- package/dist/bytes.d.cts +6 -0
- package/dist/bytes.d.ts +6 -0
- package/dist/bytes.js +22 -0
- package/dist/cell-format.cjs +142 -0
- package/dist/cell-format.d.cts +25 -0
- package/dist/cell-format.d.ts +25 -0
- package/dist/cell-format.js +137 -0
- package/dist/codec.cjs +29 -0
- package/dist/codec.d.cts +2344 -0
- package/dist/codec.d.ts +2344 -0
- package/dist/codec.js +26 -0
- package/dist/codepage.cjs +98 -0
- package/dist/codepage.d.cts +10 -0
- package/dist/codepage.d.ts +10 -0
- package/dist/codepage.js +92 -0
- package/dist/constructs.cjs +131 -0
- package/dist/constructs.d.cts +31 -0
- package/dist/constructs.d.ts +31 -0
- package/dist/constructs.js +121 -0
- package/dist/diagnostics-BgG_KAiN.d.cts +52 -0
- package/dist/diagnostics-BgG_KAiN.d.ts +52 -0
- package/dist/diagnostics.cjs +76 -0
- package/dist/diagnostics.d.cts +2 -0
- package/dist/diagnostics.d.ts +2 -0
- package/dist/diagnostics.js +68 -0
- package/dist/group.cjs +40 -0
- package/dist/group.d.cts +11 -0
- package/dist/group.d.ts +11 -0
- package/dist/group.js +38 -0
- package/dist/header.cjs +433 -0
- package/dist/header.d.cts +44 -0
- package/dist/header.d.ts +44 -0
- package/dist/header.js +431 -0
- package/dist/index.cjs +28 -0
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/list-id.cjs +30 -0
- package/dist/list-id.d.cts +16 -0
- package/dist/list-id.d.ts +16 -0
- package/dist/list-id.js +27 -0
- package/dist/options.cjs +7 -0
- package/dist/options.d.cts +18 -0
- package/dist/options.d.ts +18 -0
- package/dist/options.js +5 -0
- package/dist/read.cjs +1211 -0
- package/dist/read.d.cts +16 -0
- package/dist/read.d.ts +16 -0
- package/dist/read.js +1209 -0
- package/dist/tokenize.cjs +155 -0
- package/dist/tokenize.d.cts +25 -0
- package/dist/tokenize.d.ts +25 -0
- package/dist/tokenize.js +154 -0
- package/dist/units.cjs +43 -0
- package/dist/units.d.cts +17 -0
- package/dist/units.d.ts +17 -0
- package/dist/units.js +29 -0
- package/dist/write.cjs +532 -0
- package/dist/write.d.cts +7 -0
- package/dist/write.d.ts +7 -0
- package/dist/write.js +530 -0
- package/package.json +95 -2
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Color, ContentBorder, ContentStrokeStyle } from "document-schema.js";
|
|
2
|
+
//#region src/cell-format.d.ts
|
|
3
|
+
type CellBorderSide = "top" | "left" | "bottom" | "right";
|
|
4
|
+
declare const CELL_BORDER_SIDES: ReadonlyMap<string, CellBorderSide>;
|
|
5
|
+
interface PendingBorder {
|
|
6
|
+
style: ContentStrokeStyle | undefined;
|
|
7
|
+
widthTwips: number | undefined;
|
|
8
|
+
colorIndex: number | undefined;
|
|
9
|
+
none: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface PendingCell {
|
|
12
|
+
borders: Partial<Record<CellBorderSide, PendingBorder>>;
|
|
13
|
+
side: CellBorderSide | undefined;
|
|
14
|
+
backgroundIndex: number | undefined;
|
|
15
|
+
verticalMergeFirst: boolean;
|
|
16
|
+
verticalMergeContinuation: boolean;
|
|
17
|
+
horizontalMergeFirst: boolean;
|
|
18
|
+
horizontalMergeContinuation: boolean;
|
|
19
|
+
}
|
|
20
|
+
declare function newPendingCell(): PendingCell;
|
|
21
|
+
declare function applyCellDefinitionControlWord(name: string, param: number | undefined, cell: PendingCell): boolean;
|
|
22
|
+
declare function resolveBorder(pending: PendingBorder, colorAt: (index: number) => Color | undefined): ContentBorder | undefined;
|
|
23
|
+
declare function borderControlWords(side: CellBorderSide, border: ContentBorder, colorIndex: number | undefined): string;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { CELL_BORDER_SIDES, CellBorderSide, PendingBorder, PendingCell, applyCellDefinitionControlWord, borderControlWords, newPendingCell, resolveBorder };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Color, ContentBorder, ContentStrokeStyle } from "document-schema.js";
|
|
2
|
+
//#region src/cell-format.d.ts
|
|
3
|
+
type CellBorderSide = "top" | "left" | "bottom" | "right";
|
|
4
|
+
declare const CELL_BORDER_SIDES: ReadonlyMap<string, CellBorderSide>;
|
|
5
|
+
interface PendingBorder {
|
|
6
|
+
style: ContentStrokeStyle | undefined;
|
|
7
|
+
widthTwips: number | undefined;
|
|
8
|
+
colorIndex: number | undefined;
|
|
9
|
+
none: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface PendingCell {
|
|
12
|
+
borders: Partial<Record<CellBorderSide, PendingBorder>>;
|
|
13
|
+
side: CellBorderSide | undefined;
|
|
14
|
+
backgroundIndex: number | undefined;
|
|
15
|
+
verticalMergeFirst: boolean;
|
|
16
|
+
verticalMergeContinuation: boolean;
|
|
17
|
+
horizontalMergeFirst: boolean;
|
|
18
|
+
horizontalMergeContinuation: boolean;
|
|
19
|
+
}
|
|
20
|
+
declare function newPendingCell(): PendingCell;
|
|
21
|
+
declare function applyCellDefinitionControlWord(name: string, param: number | undefined, cell: PendingCell): boolean;
|
|
22
|
+
declare function resolveBorder(pending: PendingBorder, colorAt: (index: number) => Color | undefined): ContentBorder | undefined;
|
|
23
|
+
declare function borderControlWords(side: CellBorderSide, border: ContentBorder, colorIndex: number | undefined): string;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { CELL_BORDER_SIDES, CellBorderSide, PendingBorder, PendingCell, applyCellDefinitionControlWord, borderControlWords, newPendingCell, resolveBorder };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { pointsToTwips, twipsToPoints } from "./units.js";
|
|
2
|
+
//#region src/cell-format.ts
|
|
3
|
+
const CELL_BORDER_SIDES = /* @__PURE__ */ new Map([
|
|
4
|
+
["clbrdrt", "top"],
|
|
5
|
+
["clbrdrl", "left"],
|
|
6
|
+
["clbrdrb", "bottom"],
|
|
7
|
+
["clbrdrr", "right"]
|
|
8
|
+
]);
|
|
9
|
+
const BORDER_STYLES = /* @__PURE__ */ new Map([
|
|
10
|
+
["brdrs", "solid"],
|
|
11
|
+
["brdrth", "solid"],
|
|
12
|
+
["brdrsh", "solid"],
|
|
13
|
+
["brdrhair", "solid"],
|
|
14
|
+
["brdrinset", "solid"],
|
|
15
|
+
["brdroutset", "solid"],
|
|
16
|
+
["brdrengrave", "solid"],
|
|
17
|
+
["brdremboss", "solid"],
|
|
18
|
+
["brdrdot", "dotted"],
|
|
19
|
+
["brdrdash", "dashed"],
|
|
20
|
+
["brdrdashsm", "dashed"],
|
|
21
|
+
["brdrdashd", "dashed"],
|
|
22
|
+
["brdrdashdd", "dashed"],
|
|
23
|
+
["brdrdashdot", "dashed"],
|
|
24
|
+
["brdrdashdotdot", "dashed"],
|
|
25
|
+
["brdrdashdotstr", "dashed"],
|
|
26
|
+
["brdrdb", "double"],
|
|
27
|
+
["brdrtriple", "double"],
|
|
28
|
+
["brdrwavydb", "double"],
|
|
29
|
+
["brdrtnthsg", "double"],
|
|
30
|
+
["brdrthtnsg", "double"],
|
|
31
|
+
["brdrtnthtnsg", "double"],
|
|
32
|
+
["brdrtnthmg", "double"],
|
|
33
|
+
["brdrthtnmg", "double"],
|
|
34
|
+
["brdrtnthtnmg", "double"],
|
|
35
|
+
["brdrtnthlg", "double"],
|
|
36
|
+
["brdrthtnlg", "double"],
|
|
37
|
+
["brdrtnthtnlg", "double"],
|
|
38
|
+
["brdrwavy", "solid"]
|
|
39
|
+
]);
|
|
40
|
+
const NO_BORDER_KEYWORDS = /* @__PURE__ */ new Set([
|
|
41
|
+
"brdrnone",
|
|
42
|
+
"brdrnil",
|
|
43
|
+
"brdrtbl"
|
|
44
|
+
]);
|
|
45
|
+
const DEFAULT_BORDER_WIDTH_TWIPS = 15;
|
|
46
|
+
function newPendingCell() {
|
|
47
|
+
return {
|
|
48
|
+
borders: {},
|
|
49
|
+
side: void 0,
|
|
50
|
+
backgroundIndex: void 0,
|
|
51
|
+
verticalMergeFirst: false,
|
|
52
|
+
verticalMergeContinuation: false,
|
|
53
|
+
horizontalMergeFirst: false,
|
|
54
|
+
horizontalMergeContinuation: false
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function applyCellDefinitionControlWord(name, param, cell) {
|
|
58
|
+
const side = CELL_BORDER_SIDES.get(name);
|
|
59
|
+
if (side !== void 0) {
|
|
60
|
+
cell.side = side;
|
|
61
|
+
cell.borders[side] = {
|
|
62
|
+
style: void 0,
|
|
63
|
+
widthTwips: void 0,
|
|
64
|
+
colorIndex: void 0,
|
|
65
|
+
none: false
|
|
66
|
+
};
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
switch (name) {
|
|
70
|
+
case "clvmgf":
|
|
71
|
+
cell.verticalMergeFirst = true;
|
|
72
|
+
return true;
|
|
73
|
+
case "clvmrg":
|
|
74
|
+
cell.verticalMergeContinuation = true;
|
|
75
|
+
return true;
|
|
76
|
+
case "clmgf":
|
|
77
|
+
cell.horizontalMergeFirst = true;
|
|
78
|
+
return true;
|
|
79
|
+
case "clmrg":
|
|
80
|
+
cell.horizontalMergeContinuation = true;
|
|
81
|
+
return true;
|
|
82
|
+
case "clcbpat":
|
|
83
|
+
cell.backgroundIndex = param;
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
const pending = cell.side === void 0 ? void 0 : cell.borders[cell.side];
|
|
87
|
+
if (pending === void 0) return false;
|
|
88
|
+
if (NO_BORDER_KEYWORDS.has(name)) {
|
|
89
|
+
pending.none = true;
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
const style = BORDER_STYLES.get(name);
|
|
93
|
+
if (style !== void 0) {
|
|
94
|
+
pending.style = style;
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
if (name === "brdrw") {
|
|
98
|
+
pending.widthTwips = param;
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
if (name === "brdrcf") {
|
|
102
|
+
pending.colorIndex = param;
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
return name.startsWith("brdr") || name.startsWith("brsp");
|
|
106
|
+
}
|
|
107
|
+
function resolveBorder(pending, colorAt) {
|
|
108
|
+
if (pending.none) return;
|
|
109
|
+
const widthPt = twipsToPoints(pending.widthTwips ?? DEFAULT_BORDER_WIDTH_TWIPS);
|
|
110
|
+
if (widthPt <= 0) return;
|
|
111
|
+
const color = pending.colorIndex === void 0 ? void 0 : colorAt(pending.colorIndex);
|
|
112
|
+
const style = pending.style;
|
|
113
|
+
return {
|
|
114
|
+
color: color ?? {
|
|
115
|
+
r: 0,
|
|
116
|
+
g: 0,
|
|
117
|
+
b: 0
|
|
118
|
+
},
|
|
119
|
+
widthPt,
|
|
120
|
+
...style === void 0 || style === "solid" ? {} : { style }
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function borderControlWords(side, border, colorIndex) {
|
|
124
|
+
const sideWord = [...CELL_BORDER_SIDES].find(([, value]) => value === side)?.[0];
|
|
125
|
+
if (sideWord === void 0) return "";
|
|
126
|
+
const style = BORDER_STYLE_CONTROL_WORDS[border.style ?? "solid"];
|
|
127
|
+
const width = Math.max(1, pointsToTwips(border.widthPt));
|
|
128
|
+
return `\\${sideWord}\\${style}\\brdrw${String(width)}` + (colorIndex === void 0 ? "" : `\\brdrcf${String(colorIndex)}`);
|
|
129
|
+
}
|
|
130
|
+
const BORDER_STYLE_CONTROL_WORDS = {
|
|
131
|
+
solid: "brdrs",
|
|
132
|
+
dashed: "brdrdash",
|
|
133
|
+
dotted: "brdrdot",
|
|
134
|
+
double: "brdrdb"
|
|
135
|
+
};
|
|
136
|
+
//#endregion
|
|
137
|
+
export { CELL_BORDER_SIDES, applyCellDefinitionControlWord, borderControlWords, newPendingCell, resolveBorder };
|
package/dist/codec.cjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_read = require("./read.cjs");
|
|
3
|
+
const require_write = require("./write.cjs");
|
|
4
|
+
let zod = require("zod");
|
|
5
|
+
let document_schema_js = require("document-schema.js");
|
|
6
|
+
//#region src/codec.ts
|
|
7
|
+
const RTF_MAGIC = [
|
|
8
|
+
123,
|
|
9
|
+
92,
|
|
10
|
+
114,
|
|
11
|
+
116,
|
|
12
|
+
102
|
|
13
|
+
];
|
|
14
|
+
function hasRtfMagic(bytes) {
|
|
15
|
+
return RTF_MAGIC.every((byte, index) => bytes[index] === byte);
|
|
16
|
+
}
|
|
17
|
+
const RtfBytesSchema = zod.z.instanceof(Uint8Array).refine(hasRtfMagic, { message: "does not begin with '{\\rtf'" });
|
|
18
|
+
const rtfCodec = zod.z.codec(RtfBytesSchema, document_schema_js.DocumentTreeSchema, {
|
|
19
|
+
decode: (bytes) => require_read.readRtf(bytes).documentPackage,
|
|
20
|
+
encode: (documentPackage) => require_write.writeRtf(documentPackage)
|
|
21
|
+
});
|
|
22
|
+
const rtfContentCodec = zod.z.codec(RtfBytesSchema, document_schema_js.ContentDocumentSchema, {
|
|
23
|
+
decode: (bytes) => require_read.readRtfContent(bytes).document,
|
|
24
|
+
encode: (document) => require_write.writeRtfContent(document)
|
|
25
|
+
});
|
|
26
|
+
//#endregion
|
|
27
|
+
exports.RtfBytesSchema = RtfBytesSchema;
|
|
28
|
+
exports.rtfCodec = rtfCodec;
|
|
29
|
+
exports.rtfContentCodec = rtfContentCodec;
|