Re-factor the stringToPDFString
helper function for UTF-16 strings
This patch changes the function to instead utilize the `TextDecoder` for both kinds of UTF-16 BOM strings.
This commit is contained in:
parent
76444888fb
commit
12d8f0b64d
@ -955,38 +955,29 @@ const PDFStringTranslateTable = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
function stringToPDFString(str) {
|
function stringToPDFString(str) {
|
||||||
const length = str.length,
|
if (str[0] >= "\xEF") {
|
||||||
strBuf = [];
|
let encoding;
|
||||||
// UTF-16BE BOM
|
if (str[0] === "\xFE" && str[1] === "\xFF") {
|
||||||
if (str[0] === "\xFE" && str[1] === "\xFF") {
|
encoding = "utf-16be";
|
||||||
for (let i = 2; i < length; i += 2) {
|
} else if (str[0] === "\xFF" && str[1] === "\xFE") {
|
||||||
strBuf.push(
|
encoding = "utf-16le";
|
||||||
String.fromCharCode((str.charCodeAt(i) << 8) | str.charCodeAt(i + 1))
|
} else if (str[0] === "\xEF" && str[1] === "\xBB" && str[2] === "\xBF") {
|
||||||
);
|
encoding = "utf-8";
|
||||||
}
|
}
|
||||||
return strBuf.join("");
|
|
||||||
}
|
if (encoding) {
|
||||||
// UTF-16LE BOM
|
try {
|
||||||
if (str[0] === "\xFF" && str[1] === "\xFE") {
|
const decoder = new TextDecoder(encoding, { fatal: true });
|
||||||
for (let i = 2; i < length; i += 2) {
|
const buffer = stringToBytes(str);
|
||||||
strBuf.push(
|
return decoder.decode(buffer);
|
||||||
String.fromCharCode((str.charCodeAt(i + 1) << 8) | str.charCodeAt(i))
|
} catch (ex) {
|
||||||
);
|
warn(`stringToPDFString: "${ex}".`);
|
||||||
}
|
}
|
||||||
return strBuf.join("");
|
|
||||||
}
|
|
||||||
// UTF-8 BOM
|
|
||||||
if (str[0] === "\xEF" && str[1] === "\xBB" && str[2] === "\xBF") {
|
|
||||||
try {
|
|
||||||
const decoder = new TextDecoder("utf-8", { fatal: true });
|
|
||||||
const buffer = stringToBytes(str);
|
|
||||||
return decoder.decode(buffer);
|
|
||||||
} catch (ex) {
|
|
||||||
warn(`stringToPDFString: "${ex}".`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ISO Latin 1
|
// ISO Latin 1
|
||||||
for (let i = 0; i < length; ++i) {
|
const strBuf = [];
|
||||||
|
for (let i = 0, ii = str.length; i < ii; i++) {
|
||||||
const code = PDFStringTranslateTable[str.charCodeAt(i)];
|
const code = PDFStringTranslateTable[str.charCodeAt(i)];
|
||||||
strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
|
strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user