1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Merge pull request #15701 from Snuffleupagus/move-string-helpers

Move some string helper functions to the worker-thread
This commit is contained in:
Tim van der Meij 2022-11-19 11:20:07 +01:00 committed by GitHub
commit d6908ee145
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 102 additions and 75 deletions

View file

@ -23,26 +23,25 @@ import {
AnnotationType,
assert,
BASELINE_FACTOR,
escapeString,
FeatureTest,
getModificationDate,
IDENTITY_MATRIX,
isAscii,
LINE_DESCENT_FACTOR,
LINE_FACTOR,
OPS,
RenderingIntentFlag,
shadow,
stringToPDFString,
stringToUTF16BEString,
unreachable,
Util,
warn,
} from "../shared/util.js";
import {
collectActions,
escapeString,
getInheritableProperty,
getRotationMatrix,
isAscii,
numberToString,
stringToUTF16String,
} from "./core_utils.js";
@ -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) {

View file

@ -313,6 +313,19 @@ function escapePDFName(str) {
return buffer.join("");
}
// Replace "(", ")", "\n", "\r" and "\" by "\(", "\)", "\\n", "\\r" and "\\"
// in order to write it in a PDF file.
function escapeString(str) {
return str.replace(/([()\\\n\r])/g, match => {
if (match === "\n") {
return "\\n";
} else if (match === "\r") {
return "\\r";
}
return `\\${match}`;
});
}
function _collectJS(entry, xref, list, parents) {
if (!entry) {
return;
@ -572,6 +585,10 @@ function getNewAnnotationsMap(annotationStorage) {
return newAnnotationsByPage.size > 0 ? newAnnotationsByPage : null;
}
function isAscii(str) {
return /^[\x00-\x7F]*$/.test(str);
}
function stringToUTF16HexString(str) {
const buf = [];
for (let i = 0, ii = str.length; i < ii; i++) {
@ -584,8 +601,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(
@ -614,11 +634,13 @@ export {
DocStats,
encodeToXmlString,
escapePDFName,
escapeString,
getArrayLookupTableFactory,
getInheritableProperty,
getLookupTableFactory,
getNewAnnotationsMap,
getRotationMatrix,
isAscii,
isWhiteSpace,
log2,
MissingDataException,

View file

@ -13,9 +13,14 @@
* limitations under the License.
*/
import { bytesToString, escapeString, warn } from "../shared/util.js";
import { bytesToString, warn } from "../shared/util.js";
import { Dict, Name, Ref } from "./primitives.js";
import { escapePDFName, numberToString, parseXFAPath } from "./core_utils.js";
import {
escapePDFName,
escapeString,
numberToString,
parseXFAPath,
} from "./core_utils.js";
import { SimpleDOMNode, SimpleXMLParser } from "./xml_parser.js";
import { BaseStream } from "./base_stream.js";
import { calculateMD5 } from "./crypto.js";

View file

@ -1037,36 +1037,6 @@ function stringToPDFString(str) {
return strBuf.join("");
}
function escapeString(str) {
// replace "(", ")", "\n", "\r" and "\"
// by "\(", "\)", "\\n", "\\r" and "\\"
// in order to write it in a PDF file.
return str.replace(/([()\\\n\r])/g, match => {
if (match === "\n") {
return "\\n";
} else if (match === "\r") {
return "\\r";
}
return `\\${match}`;
});
}
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));
}
@ -1167,7 +1137,6 @@ export {
createPromiseCapability,
createValidAbsoluteUrl,
DocumentActionEventType,
escapeString,
FeatureTest,
FONT_IDENTITY_MATRIX,
FontType,
@ -1180,7 +1149,6 @@ export {
InvalidPDFException,
isArrayBuffer,
isArrayEqual,
isAscii,
LINE_DESCENT_FACTOR,
LINE_FACTOR,
MissingPDFException,
@ -1198,7 +1166,6 @@ export {
string32,
stringToBytes,
stringToPDFString,
stringToUTF16BEString,
stringToUTF8String,
TextRenderingMode,
UnexpectedResponseException,