mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
Combine the stringToUTF16String
and stringToUTF16BEString
helper functions
Given that these functions are virtually identical, with the latter only adding a BOM, we can combine the two. Furthermore, since both functions were only used on the worker-thread, there's no reason to duplicate this functionality in both of the `pdf.js` and `pdf.worker.js` files.
This commit is contained in:
parent
c7d6ab2f71
commit
2eaa708e3a
5 changed files with 41 additions and 31 deletions
|
@ -34,7 +34,6 @@ import {
|
|||
RenderingIntentFlag,
|
||||
shadow,
|
||||
stringToPDFString,
|
||||
stringToUTF16BEString,
|
||||
unreachable,
|
||||
Util,
|
||||
warn,
|
||||
|
@ -1879,7 +1878,11 @@ class WidgetAnnotation extends Annotation {
|
|||
value,
|
||||
};
|
||||
|
||||
const encoder = val => (isAscii(val) ? val : stringToUTF16BEString(val));
|
||||
const encoder = val => {
|
||||
return isAscii(val)
|
||||
? val
|
||||
: stringToUTF16String(val, /* bigEndian = */ true);
|
||||
};
|
||||
dict.set("V", Array.isArray(value) ? value.map(encoder) : encoder(value));
|
||||
|
||||
const maybeMK = this._getMKDict(rotation);
|
||||
|
@ -3546,14 +3549,19 @@ class FreeTextAnnotation extends MarkupAnnotation {
|
|||
freetext.set("DA", da);
|
||||
freetext.set(
|
||||
"Contents",
|
||||
isAscii(value) ? value : stringToUTF16BEString(value)
|
||||
isAscii(value)
|
||||
? value
|
||||
: stringToUTF16String(value, /* bigEndian = */ true)
|
||||
);
|
||||
freetext.set("F", 4);
|
||||
freetext.set("Border", [0, 0, 0]);
|
||||
freetext.set("Rotate", rotation);
|
||||
|
||||
if (user) {
|
||||
freetext.set("T", isAscii(user) ? user : stringToUTF16BEString(user));
|
||||
freetext.set(
|
||||
"T",
|
||||
isAscii(user) ? user : stringToUTF16String(user, /* bigEndian = */ true)
|
||||
);
|
||||
}
|
||||
|
||||
if (apRef || ap) {
|
||||
|
|
|
@ -584,8 +584,11 @@ function stringToUTF16HexString(str) {
|
|||
return buf.join("");
|
||||
}
|
||||
|
||||
function stringToUTF16String(str) {
|
||||
function stringToUTF16String(str, bigEndian = false) {
|
||||
const buf = [];
|
||||
if (bigEndian) {
|
||||
buf.push("\xFE\xFF");
|
||||
}
|
||||
for (let i = 0, ii = str.length; i < ii; i++) {
|
||||
const char = str.charCodeAt(i);
|
||||
buf.push(
|
||||
|
|
|
@ -1055,18 +1055,6 @@ function isAscii(str) {
|
|||
return /^[\x00-\x7F]*$/.test(str);
|
||||
}
|
||||
|
||||
function stringToUTF16BEString(str) {
|
||||
const buf = ["\xFE\xFF"];
|
||||
for (let i = 0, ii = str.length; i < ii; i++) {
|
||||
const char = str.charCodeAt(i);
|
||||
buf.push(
|
||||
String.fromCharCode((char >> 8) & 0xff),
|
||||
String.fromCharCode(char & 0xff)
|
||||
);
|
||||
}
|
||||
return buf.join("");
|
||||
}
|
||||
|
||||
function stringToUTF8String(str) {
|
||||
return decodeURIComponent(escape(str));
|
||||
}
|
||||
|
@ -1198,7 +1186,6 @@ export {
|
|||
string32,
|
||||
stringToBytes,
|
||||
stringToPDFString,
|
||||
stringToUTF16BEString,
|
||||
stringToUTF8String,
|
||||
TextRenderingMode,
|
||||
UnexpectedResponseException,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue