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
8 changes: 5 additions & 3 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
OPS,
RenderingIntentFlag,
shadow,
stringToPDFString,
unreachable,
Util,
warn,
Expand All @@ -53,8 +52,6 @@ import {
numberToString,
RESOURCES_KEYS_OPERATOR_LIST,
RESOURCES_KEYS_TEXT_CONTENT,
stringToAsciiOrUTF16BE,
stringToUTF16String,
} from "./core_utils.js";
import {
createDefaultAppearance,
Expand All @@ -66,6 +63,11 @@ import {
import { DateFormats, TimeFormats } from "../shared/scripting_utils.js";
import { Dict, isName, isRefsEqual, Name, Ref, RefSet } from "./primitives.js";
import { Stream, StringStream } from "./stream.js";
import {
stringToAsciiOrUTF16BE,
stringToPDFString,
stringToUTF16String,
} from "./string_utils.js";
import { BaseStream } from "./base_stream.js";
import { bidi } from "./bidi.js";
import { Catalog } from "./catalog.js";
Expand Down
2 changes: 1 addition & 1 deletion src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
objectSize,
PermissionFlag,
shadow,
stringToPDFString,
stringToUTF8String,
warn,
} from "../shared/util.js";
Expand Down Expand Up @@ -53,6 +52,7 @@ import { clearGlobalCaches } from "./cleanup_helper.js";
import { ColorSpaceUtils } from "./colorspace_utils.js";
import { FileSpec } from "./file_spec.js";
import { MetadataParser } from "./metadata_parser.js";
import { stringToPDFString } from "./string_utils.js";
import { StructTreeRoot } from "./struct_tree.js";

const isRef = v => v instanceof Ref;
Expand Down
45 changes: 1 addition & 44 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import {
BaseException,
makeArr,
objectSize,
stringToPDFString,
Util,
warn,
} from "../shared/util.js";
import { Dict, isName, isRefsEqual, Name, Ref, RefSet } from "./primitives.js";
import { BaseStream } from "./base_stream.js";
import { stringToPDFString } from "./string_utils.js";

const PDF_VERSION_REGEXP = /^[1-9]\.\d$/;
const MAX_INT_32 = 2 ** 31 - 1;
Expand Down Expand Up @@ -684,45 +684,6 @@ function getNewAnnotationsMap(annotationStorage) {
return newAnnotationsByPage.size > 0 ? newAnnotationsByPage : null;
}

// If the string is null or undefined then it is returned as is.
function stringToAsciiOrUTF16BE(str) {
if (str === null || str === undefined) {
return str;
}
return isAscii(str) ? str : stringToUTF16String(str, /* bigEndian = */ true);
}

function isAscii(str) {
if (typeof str !== "string") {
return false;
}
return !str || /^[\x00-\x7F]*$/.test(str);
}

function stringToUTF16HexString(str) {
const buf = [];
for (let i = 0, ii = str.length; i < ii; i++) {
const char = str.charCodeAt(i);
buf.push(Util.hexNums[(char >> 8) & 0xff], Util.hexNums[char & 0xff]);
}
return buf.join("");
}

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(
String.fromCharCode((char >> 8) & 0xff),
String.fromCharCode(char & 0xff)
);
}
return buf.join("");
}

function getModificationDate(date = new Date()) {
if (!(date instanceof Date)) {
date = new Date(date);
Expand Down Expand Up @@ -782,7 +743,6 @@ export {
getRotationMatrix,
getSizeInBytes,
IDENTITY_MATRIX,
isAscii,
isBooleanArray,
isNumberArray,
isWhiteSpace,
Expand All @@ -798,9 +758,6 @@ export {
recoverJsURL,
RESOURCES_KEYS_OPERATOR_LIST,
RESOURCES_KEYS_TEXT_CONTENT,
stringToAsciiOrUTF16BE,
stringToUTF16HexString,
stringToUTF16String,
toRomanNumerals,
validateCSSFont,
validateFontName,
Expand Down
2 changes: 1 addition & 1 deletion src/core/default_appearance.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
escapePDFName,
getRotationMatrix,
numberToString,
stringToUTF16HexString,
} from "./core_utils.js";
import { Dict, Name } from "./primitives.js";
import {
Expand All @@ -33,6 +32,7 @@ import { EvaluatorPreprocessor } from "./evaluator.js";
import { LocalColorSpaceCache } from "./image_utils.js";
import { PDFFunctionFactory } from "./function.js";
import { StringStream } from "./stream.js";
import { stringToUTF16HexString } from "./string_utils.js";

class DefaultAppearanceEvaluator extends EvaluatorPreprocessor {
constructor(str) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
RenderingIntentFlag,
shadow,
stringToBytes,
stringToPDFString,
stringToUTF8String,
unreachable,
Util,
Expand Down Expand Up @@ -76,6 +75,7 @@ import { OperatorList } from "./operator_list.js";
import { PartialEvaluator } from "./evaluator.js";
import { PDFImage } from "./image.js";
import { StreamsSequenceStream } from "./decode_stream.js";
import { stringToPDFString } from "./string_utils.js";
import { StructTreePage } from "./struct_tree.js";
import { XFAFactory } from "./xfa/factory.js";
import { XRef } from "./xref.js";
Expand Down
4 changes: 2 additions & 2 deletions src/core/editor/pdf_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import {
getInheritableProperty,
getModificationDate,
getNewAnnotationsMap,
stringToAsciiOrUTF16BE,
} from "../core_utils.js";
import { Dict, isName, Name, Ref, RefSet, RefSetCache } from "../primitives.js";
import { incrementalUpdate, writeValue } from "../writer.js";
import { NameTree, NumberTree } from "../name_number_tree.js";
import { stringToBytes, stringToPDFString } from "../../shared/util.js";
import { stringToAsciiOrUTF16BE, stringToPDFString } from "../string_utils.js";
import { AnnotationFactory } from "../annotation.js";
import { BaseStream } from "../base_stream.js";
import { StringStream } from "../stream.js";
import { stringToBytes } from "../../shared/util.js";

const MAX_LEAVES_PER_PAGES_NODE = 16;
const MAX_IN_NAME_TREE_NODE = 64;
Expand Down
2 changes: 1 addition & 1 deletion src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
normalizeUnicode,
OPS,
shadow,
stringToPDFString,
TextRenderingMode,
Util,
warn,
Expand Down Expand Up @@ -90,6 +89,7 @@ import { getUnicodeForGlyph } from "./unicode.js";
import { MurmurHash3_64 } from "../shared/murmurhash3.js";
import { PDFImage } from "./image.js";
import { Stream } from "./stream.js";
import { stringToPDFString } from "./string_utils.js";

const DefaultPartialEvaluatorOptions = Object.freeze({
maxImageSize: -1,
Expand Down
3 changes: 2 additions & 1 deletion src/core/file_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* limitations under the License.
*/

import { stringToPDFString, stripPath, warn } from "../shared/util.js";
import { stripPath, warn } from "../shared/util.js";
import { BaseStream } from "./base_stream.js";
import { Dict } from "./primitives.js";
import { stringToPDFString } from "./string_utils.js";

function pickPlatformItem(dict) {
if (dict instanceof Dict) {
Expand Down
121 changes: 121 additions & 0 deletions src/core/string_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* Copyright 2019 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { stringToBytes, Util, warn } from "../shared/util.js";

function isAscii(str) {
return typeof str === "string" && (!str || /^[\x00-\x7F]*$/.test(str));
}

// If the string is null or undefined then it is returned as is.
function stringToAsciiOrUTF16BE(str) {
if (str === null || str === undefined) {
return str;
}
return isAscii(str) ? str : stringToUTF16String(str, /* bigEndian = */ true);
}

function stringToUTF16HexString(str) {
const buf = [];
for (let i = 0, ii = str.length; i < ii; i++) {
const char = str.charCodeAt(i);
buf.push(Util.hexNums[(char >> 8) & 0xff], Util.hexNums[char & 0xff]);
}
return buf.join("");
}

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(
String.fromCharCode((char >> 8) & 0xff),
String.fromCharCode(char & 0xff)
);
}
return buf.join("");
}

const PDFStringTranslateTable = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2d8,
0x2c7, 0x2c6, 0x2d9, 0x2dd, 0x2db, 0x2da, 0x2dc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0x2022, 0x2020, 0x2021, 0x2026, 0x2014, 0x2013, 0x192,
0x2044, 0x2039, 0x203a, 0x2212, 0x2030, 0x201e, 0x201c, 0x201d, 0x2018,
0x2019, 0x201a, 0x2122, 0xfb01, 0xfb02, 0x141, 0x152, 0x160, 0x178, 0x17d,
0x131, 0x142, 0x153, 0x161, 0x17e, 0, 0x20ac,
];

function stringToPDFString(str, keepEscapeSequence = false) {
// See section 7.9.2.2 Text String Type.
// The string can contain some language codes bracketed with 0x1b,
// so we must remove them.
if (str[0] >= "\xEF") {
let encoding;
if (str[0] === "\xFE" && str[1] === "\xFF") {
encoding = "utf-16be";
if (str.length % 2 === 1) {
str = str.slice(0, -1);
}
} else if (str[0] === "\xFF" && str[1] === "\xFE") {
encoding = "utf-16le";
if (str.length % 2 === 1) {
str = str.slice(0, -1);
}
} else if (str[0] === "\xEF" && str[1] === "\xBB" && str[2] === "\xBF") {
encoding = "utf-8";
}

if (encoding) {
try {
const decoder = new TextDecoder(encoding, { fatal: true });
const buffer = stringToBytes(str);
const decoded = decoder.decode(buffer);
if (keepEscapeSequence || !decoded.includes("\x1b")) {
return decoded;
}
return decoded.replaceAll(/\x1b[^\x1b]*(?:\x1b|$)/g, "");
} catch (ex) {
warn(`stringToPDFString: "${ex}".`);
}
}
}
// ISO Latin 1
const strBuf = [];
for (let i = 0, ii = str.length; i < ii; i++) {
const charCode = str.charCodeAt(i);
if (!keepEscapeSequence && charCode === 0x1b) {
// eslint-disable-next-line no-empty
while (++i < ii && str.charCodeAt(i) !== 0x1b) {}
continue;
}
const code = PDFStringTranslateTable[charCode];
strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
}
return strBuf.join("");
}

export {
isAscii,
stringToAsciiOrUTF16BE,
stringToPDFString,
stringToUTF16HexString,
stringToUTF16String,
};
4 changes: 2 additions & 2 deletions src/core/struct_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
import {
AnnotationPrefix,
makeArr,
stringToPDFString,
stringToUTF8String,
warn,
} from "../shared/util.js";
import { Dict, isName, Name, Ref, RefSetCache } from "./primitives.js";
import { lookupNormalRect, stringToAsciiOrUTF16BE } from "./core_utils.js";
import { stringToAsciiOrUTF16BE, stringToPDFString } from "./string_utils.js";
import { BaseStream } from "./base_stream.js";
import { lookupNormalRect } from "./core_utils.js";
import { NumberTree } from "./name_number_tree.js";

const MAX_DEPTH = 40;
Expand Down
2 changes: 1 addition & 1 deletion src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
isNodeJS,
PasswordException,
setVerbosityLevel,
stringToPDFString,
VerbosityLevel,
warn,
} from "../shared/util.js";
Expand All @@ -38,6 +37,7 @@ import { clearGlobalCaches } from "./cleanup_helper.js";
import { incrementalUpdate } from "./writer.js";
import { PDFEditor } from "./editor/pdf_editor.js";
import { PDFWorkerStream } from "./worker_stream.js";
import { stringToPDFString } from "./string_utils.js";
import { StructTreeRoot } from "./struct_tree.js";

class WorkerTask {
Expand Down
Loading
Loading