Skip to content

Commit

Permalink
Merge pull request #13320 from Snuffleupagus/BaseStream-getString
Browse files Browse the repository at this point in the history
Add a new `BaseStream.getString(...)` method to replace manual `bytesToString(BaseStream.getBytes(...))` calls
  • Loading branch information
timvandermeij committed May 2, 2021
2 parents 92b9bdc + 90b5fcb commit 404c2b0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 33 deletions.
6 changes: 5 additions & 1 deletion src/core/base_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

import { shadow, unreachable } from "../shared/util.js";
import { bytesToString, shadow, unreachable } from "../shared/util.js";

class BaseStream {
constructor() {
Expand Down Expand Up @@ -79,6 +79,10 @@ class BaseStream {
unreachable("Abstract method `getByteRange` called");
}

getString(length) {
return bytesToString(this.getBytes(length, /* forceClamped = */ false));
}

skip(n) {
this.pos += n || 1;
}
Expand Down
39 changes: 19 additions & 20 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,6 @@
* limitations under the License.
*/

import {
bytesToString,
createPromiseCapability,
createValidAbsoluteUrl,
DocumentActionEventType,
FormatError,
info,
isBool,
isNum,
isString,
objectSize,
PermissionFlag,
shadow,
stringToPDFString,
stringToUTF8String,
warn,
} from "../shared/util.js";
import {
clearPrimitiveCaches,
Dict,
Expand All @@ -46,6 +29,22 @@ import {
MissingDataException,
toRomanNumerals,
} from "./core_utils.js";
import {
createPromiseCapability,
createValidAbsoluteUrl,
DocumentActionEventType,
FormatError,
info,
isBool,
isNum,
isString,
objectSize,
PermissionFlag,
shadow,
stringToPDFString,
stringToUTF8String,
warn,
} from "../shared/util.js";
import { NameTree, NumberTree } from "./name_number_tree.js";
import { ColorSpace } from "./colorspace.js";
import { FileSpec } from "./file_spec.js";
Expand Down Expand Up @@ -136,7 +135,7 @@ class Catalog {
// charsets, let's just hope that the author of the PDF was reasonable
// enough to stick with the XML default charset, which is UTF-8.
try {
const data = stringToUTF8String(bytesToString(stream.getBytes()));
const data = stringToUTF8String(stream.getString());
if (data) {
metadata = new MetadataParser(data).serializable;
}
Expand Down Expand Up @@ -906,7 +905,7 @@ class Catalog {

let js = jsDict.get("JS");
if (isStream(js)) {
js = bytesToString(js.getBytes());
js = js.getString();
} else if (typeof js !== "string") {
return;
}
Expand Down Expand Up @@ -1344,7 +1343,7 @@ class Catalog {
let js;

if (isStream(jsAction)) {
js = bytesToString(jsAction.getBytes());
js = jsAction.getString();
} else if (isString(jsAction)) {
js = jsAction;
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import {
assert,
BaseException,
bytesToString,
objectSize,
stringToPDFString,
warn,
Expand Down Expand Up @@ -269,7 +268,7 @@ function _collectJS(entry, xref, list, parents) {
const js = entry.get("JS");
let code;
if (isStream(js)) {
code = bytesToString(js.getBytes());
code = js.getString();
} else {
code = js;
}
Expand Down
5 changes: 2 additions & 3 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import {
assert,
bytesToString,
FormatError,
info,
InvalidPDFException,
Expand Down Expand Up @@ -811,7 +810,7 @@ class PDFDocument {
};
if (isStream(xfa) && !xfa.isEmpty) {
try {
entries["xdp:xdp"] = stringToUTF8String(bytesToString(xfa.getBytes()));
entries["xdp:xdp"] = stringToUTF8String(xfa.getString());
return entries;
} catch (_) {
warn("XFA - Invalid utf-8 string.");
Expand Down Expand Up @@ -841,7 +840,7 @@ class PDFDocument {
continue;
}
try {
entries[name] = stringToUTF8String(bytesToString(data.getBytes()));
entries[name] = stringToUTF8String(data.getString());
} catch (_) {
warn("XFA - Invalid utf-8 string.");
return null;
Expand Down
8 changes: 4 additions & 4 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ var Font = (function FontClosure() {
}

function readTableEntry(file) {
var tag = bytesToString(file.getBytes(4));
var tag = file.getString(4);

var checksum = file.getInt32() >>> 0;
var offset = file.getInt32() >>> 0;
Expand Down Expand Up @@ -1516,7 +1516,7 @@ var Font = (function FontClosure() {

function readOpenTypeHeader(ttf) {
return {
version: bytesToString(ttf.getBytes(4)),
version: ttf.getString(4),
numTables: ttf.getUint16(),
searchRange: ttf.getUint16(),
entrySelector: ttf.getUint16(),
Expand All @@ -1525,7 +1525,7 @@ var Font = (function FontClosure() {
}

function readTrueTypeCollectionHeader(ttc) {
const ttcTag = bytesToString(ttc.getBytes(4));
const ttcTag = ttc.getString(4);
assert(ttcTag === "ttcf", "Must be a TrueType Collection font.");

const majorVersion = ttc.getUint16();
Expand Down Expand Up @@ -2344,7 +2344,7 @@ var Font = (function FontClosure() {
}
names[1][nameIndex] = str;
} else {
names[0][nameIndex] = bytesToString(font.getBytes(record.length));
names[0][nameIndex] = font.getString(record.length);
}
}
return names;
Expand Down
4 changes: 2 additions & 2 deletions src/core/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function writeDict(dict, buffer, transform) {
function writeStream(stream, buffer, transform) {
writeDict(stream.dict, buffer, transform);
buffer.push(" stream\n");
let string = bytesToString(stream.getBytes());
let string = stream.getString();
if (transform !== null) {
string = transform.encryptString(string);
}
Expand Down Expand Up @@ -129,7 +129,7 @@ function updateXFA(datasetsRef, newRefs, xref) {
return;
}
const datasets = xref.fetchIfRef(datasetsRef);
const str = bytesToString(datasets.getBytes());
const str = datasets.getString();
const xml = new SimpleXMLParser({ hasAttributes: true }).parseFromString(str);

for (const { xfa } of newRefs) {
Expand Down
2 changes: 1 addition & 1 deletion src/display/font_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ class FontFaceObject {
if (!this.data || this.disableFontFace) {
return null;
}
const data = bytesToString(new Uint8Array(this.data));
const data = bytesToString(this.data);
// Add the @font-face rule to the document.
const url = `url(data:${this.mimetype};base64,${btoa(data)});`;
let rule;
Expand Down

0 comments on commit 404c2b0

Please sign in to comment.