rtf-codec 4.0.8 → 4.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/dist/codepage.cjs CHANGED
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_diagnostics = require("./diagnostics.cjs");
3
+ const require_codepage_dbcs = require("./codepage-dbcs.cjs");
3
4
  //#region src/codepage.ts
4
5
  const DEFAULT_CODEPAGE = 1252;
5
6
  const UTF8_CODEPAGE = 65001;
@@ -71,11 +72,13 @@ function codepageForFontCharset(charset) {
71
72
  return FCHARSET_CODEPAGES.get(charset);
72
73
  }
73
74
  function isSupportedCodepage(codepage) {
74
- return codepage === 65001 || SINGLE_BYTE_PAGES.has(codepage);
75
+ return codepage === 65001 || SINGLE_BYTE_PAGES.has(codepage) || require_codepage_dbcs.DBCS_LEAD_BYTE_TABLES.has(codepage);
75
76
  }
76
77
  function decodeCodepageBytes(input, codepage, sink) {
77
78
  if (input.length === 0) return "";
78
79
  if (codepage === 65001) return new TextDecoder("utf-8").decode(input);
80
+ const leadTable = require_codepage_dbcs.DBCS_LEAD_BYTE_TABLES.get(codepage);
81
+ if (leadTable !== void 0) return decodeDbcsBytes(input, codepage, leadTable);
79
82
  const table = SINGLE_BYTE_PAGES.get(codepage);
80
83
  if (table === void 0) {
81
84
  sink({
@@ -89,6 +92,30 @@ function decodeCodepageBytes(input, codepage, sink) {
89
92
  for (const byte of input) out += byte < 128 ? String.fromCharCode(byte) : table[byte - 128] ?? "�";
90
93
  return out;
91
94
  }
95
+ function decodeDbcsBytes(input, codepage, leadTable) {
96
+ const singleByteExtras = require_codepage_dbcs.DBCS_SINGLE_BYTE_EXTRAS.get(codepage);
97
+ let out = "";
98
+ let i = 0;
99
+ while (i < input.length) {
100
+ const byte = input[i];
101
+ if (byte === void 0) break;
102
+ if (byte < 128) {
103
+ out += String.fromCharCode(byte);
104
+ i += 1;
105
+ continue;
106
+ }
107
+ const trailTable = leadTable.get(byte);
108
+ if (trailTable !== void 0) {
109
+ const trail = input[i + 1];
110
+ out += trail === void 0 ? "�" : trailTable[trail] ?? "�";
111
+ i += trail === void 0 ? 1 : 2;
112
+ continue;
113
+ }
114
+ out += singleByteExtras?.[byte - 128] ?? "�";
115
+ i += 1;
116
+ }
117
+ return out;
118
+ }
92
119
  //#endregion
93
120
  exports.DEFAULT_CODEPAGE = DEFAULT_CODEPAGE;
94
121
  exports.DOCUMENT_CHARSET_CODEPAGES = DOCUMENT_CHARSET_CODEPAGES;
package/dist/codepage.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { RtfDiagnosticCodes } from "./diagnostics.js";
2
+ import { DBCS_LEAD_BYTE_TABLES, DBCS_SINGLE_BYTE_EXTRAS } from "./codepage-dbcs.js";
2
3
  //#region src/codepage.ts
3
4
  const DEFAULT_CODEPAGE = 1252;
4
5
  const UTF8_CODEPAGE = 65001;
@@ -70,11 +71,13 @@ function codepageForFontCharset(charset) {
70
71
  return FCHARSET_CODEPAGES.get(charset);
71
72
  }
72
73
  function isSupportedCodepage(codepage) {
73
- return codepage === 65001 || SINGLE_BYTE_PAGES.has(codepage);
74
+ return codepage === 65001 || SINGLE_BYTE_PAGES.has(codepage) || DBCS_LEAD_BYTE_TABLES.has(codepage);
74
75
  }
75
76
  function decodeCodepageBytes(input, codepage, sink) {
76
77
  if (input.length === 0) return "";
77
78
  if (codepage === 65001) return new TextDecoder("utf-8").decode(input);
79
+ const leadTable = DBCS_LEAD_BYTE_TABLES.get(codepage);
80
+ if (leadTable !== void 0) return decodeDbcsBytes(input, codepage, leadTable);
78
81
  const table = SINGLE_BYTE_PAGES.get(codepage);
79
82
  if (table === void 0) {
80
83
  sink({
@@ -88,5 +91,29 @@ function decodeCodepageBytes(input, codepage, sink) {
88
91
  for (const byte of input) out += byte < 128 ? String.fromCharCode(byte) : table[byte - 128] ?? "�";
89
92
  return out;
90
93
  }
94
+ function decodeDbcsBytes(input, codepage, leadTable) {
95
+ const singleByteExtras = DBCS_SINGLE_BYTE_EXTRAS.get(codepage);
96
+ let out = "";
97
+ let i = 0;
98
+ while (i < input.length) {
99
+ const byte = input[i];
100
+ if (byte === void 0) break;
101
+ if (byte < 128) {
102
+ out += String.fromCharCode(byte);
103
+ i += 1;
104
+ continue;
105
+ }
106
+ const trailTable = leadTable.get(byte);
107
+ if (trailTable !== void 0) {
108
+ const trail = input[i + 1];
109
+ out += trail === void 0 ? "�" : trailTable[trail] ?? "�";
110
+ i += trail === void 0 ? 1 : 2;
111
+ continue;
112
+ }
113
+ out += singleByteExtras?.[byte - 128] ?? "�";
114
+ i += 1;
115
+ }
116
+ return out;
117
+ }
91
118
  //#endregion
92
119
  export { DEFAULT_CODEPAGE, DOCUMENT_CHARSET_CODEPAGES, UTF8_CODEPAGE, codepageForFontCharset, decodeCodepageBytes, isSupportedCodepage };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rtf-codec",
3
- "version": "4.0.8",
3
+ "version": "4.1.0",
4
4
  "description": "Hand-written Rich Text Format (RTF 1.9.1) reader and writer against the shared document-schema.js content pivot.",
5
5
  "type": "module",
6
6
  "repository": {
@@ -80,7 +80,7 @@
80
80
  "license": "MIT",
81
81
  "packageManager": "pnpm@11.6.0",
82
82
  "dependencies": {
83
- "archive-codec": "1.8.0",
83
+ "archive-codec": "1.9.0",
84
84
  "document-schema.js": "7.3.1",
85
85
  "zod": "4.4.3"
86
86
  },