Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions apps/server/src/provider/documentTextWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ function* notebook(source: string): Generator<string, void> {

// Per-byte decoding only supports the single-byte \fcharset values.
const FONT_CODE_PAGES = new Map([
[0, "windows-1252"],
[77, "macintosh"],
[161, "windows-1253"],
[162, "windows-1254"],
Expand All @@ -475,6 +476,18 @@ const FONT_CODE_PAGES = new Map([
[238, "windows-1250"],
]);

// Per-byte decoding only supports single-byte code pages.
const singleByteCodePage = (value: number) =>
value === 0
? "windows-1252"
: value === 866
? "ibm866"
: value === 10000
? "macintosh"
: value === 874 || (value >= 1250 && value <= 1258)
? `windows-${value}`
: undefined;

function* rtf(source: string): Generator<string, void> {
if (!source.startsWith("{\\rtf")) throw new Error("Invalid RTF document");
let state = {
Expand All @@ -484,10 +497,12 @@ function* rtf(source: string): Generator<string, void> {
uc: 1,
font: 0,
fontTable: false,
fontFile: false,
decoder: new TextDecoder("windows-1252"),
};
const stack: Array<typeof state> = [];
// A font's \fcharset, not the document code page, encodes the bytes written in that font.
// A font's \cpg, else its \fcharset, not the document code page, encodes the bytes written in
// that font. A \cpg inside \fontfile encodes the font's file name instead.
const fonts = new Map<number, typeof state.decoder>();
let defaultFont = 0;
const decoder = () => fonts.get(state.font) ?? state.decoder;
Expand Down Expand Up @@ -561,13 +576,17 @@ function* rtf(source: string): Generator<string, void> {
)
state.skip = true;
if (control === "fonttbl") state.fontTable = true;
else if (control === "fontfile") state.fontFile = true;
else if (control === "deff") state.font = defaultFont = value;
else if (control === "f") {
state.font = value;
if (state.fontTable) fonts.delete(value);
} else if (control === "fcharset" && state.fontTable) {
const codePage = FONT_CODE_PAGES.get(value);
if (codePage) fonts.set(state.font, new TextDecoder(codePage));
} else if (control === "cpg" && state.fontTable && !state.fontFile) {
const codePage = singleByteCodePage(value);
if (codePage) fonts.set(state.font, new TextDecoder(codePage));
}
if (control === "deleted") state.deleted = value !== 0;
else if (control === "v") state.hiddenText = value !== 0;
Expand All @@ -579,15 +598,7 @@ function* rtf(source: string): Generator<string, void> {
if (control === "mac" || (control === "ansicpg" && value === 10000))
state.decoder = new TextDecoder("macintosh");
else if (control === "ansicpg") {
// Per-byte decoding only supports single-byte code pages.
const label =
value === 0
? "windows-1252"
: value === 866
? "ibm866"
: value === 874 || (value >= 1250 && value <= 1258)
? `windows-${value}`
: undefined;
const label = singleByteCodePage(value);
if (!label) throw new Error("Unsupported RTF code page");
state.decoder = new TextDecoder(label, { fatal: true });
} else if (control === "uc") {
Expand Down
20 changes: 20 additions & 0 deletions apps/server/src/provider/userInputAttachments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,26 @@ describe("question answer paths", () => {
\f0 \'e3\'e1\'c7\'cd\'d9\'c9\f1 : caf\'e9, \f0\plain\'c7a}`,
"ملاحظة: café, Ça",
],
[
"ANSI font under another code page",
String.raw`{\rtf1\ansi\ansicpg1251{\fonttbl\f0\froman\fcharset0 Times-Roman;}\f0 \'805 caf\'e9}`,
"€5 café",
],
[
"font code page",
String.raw`{\rtf1\ansi\ansicpg1251{\fonttbl{\f0\fnil\fcharset0\cpg1251 Arial;}{\f1\fnil\fcharset204 Arial{\*\fontfile\cpg1252 arial.ttf};}}\f0 \'cf\'f0\'e8\f1\'e2\'e5\'f2}`,
"Привет",
],
[
"ANSI font in a Macintosh document",
String.raw`{\rtf1\mac\ansicpg10000{\fonttbl\f0\fnil\fcharset0 LucidaGrande;}\f0 pridr\'9eana}`,
"pridržana",
],
[
"Macintosh font code page",
String.raw`{\rtf1\mac\ansicpg10000{\fonttbl\f0\fnil\fcharset0\cpg10000 Geneva;}\f0 Caf\'8e}`,
"Café",
],
[
"font character set outside the font table",
String.raw`{\rtf1\ansi{\fonttbl{\f0\fcharset178 A;}{\f0\fcharset0 B;}{\f1\fcharset0 C;}{\f2\fcharset178 D;}{\f2 E;}}{\stylesheet{\s0\f1\fcharset178 N;}}\f0 \'e3\f1\'e3\f2\'e3}`,
Expand Down
Loading