Merge pull request #13375 from Snuffleupagus/refactor-getContentStream

Improve the `Page.content` and `Page.getContentStream` methods
This commit is contained in:
Tim van der Meij 2021-05-14 22:16:39 +02:00 committed by GitHub
commit c9892be47c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 34 deletions

View File

@ -54,6 +54,7 @@ import {
} from "./core_utils.js";
import { NullStream, Stream } from "./stream.js";
import { AnnotationFactory } from "./annotation.js";
import { BaseStream } from "./base_stream.js";
import { calculateMD5 } from "./crypto.js";
import { Catalog } from "./catalog.js";
import { Linearization } from "./parser.js";
@ -136,7 +137,7 @@ class Page {
}
get content() {
return this.pageDict.get("Contents");
return this.pageDict.getArray("Contents");
}
get resources() {
@ -229,25 +230,20 @@ class Page {
return shadow(this, "rotate", rotate);
}
/**
* @returns {Promise<BaseStream>}
*/
getContentStream() {
const content = this.content;
let stream;
if (Array.isArray(content)) {
// Fetching the individual streams from the array.
const xref = this.xref;
const streams = [];
for (const subStream of content) {
streams.push(xref.fetchIfRef(subStream));
return this.pdfManager.ensure(this, "content").then(content => {
if (content instanceof BaseStream) {
return content;
}
if (Array.isArray(content)) {
return new StreamsSequenceStream(content);
}
stream = new StreamsSequenceStream(streams);
} else if (isStream(content)) {
stream = content;
} else {
// Replace non-existent page content with empty content.
stream = new NullStream();
}
return stream;
return new NullStream();
});
}
get xfaData() {
@ -313,10 +309,7 @@ class Page {
renderInteractiveForms,
annotationStorage,
}) {
const contentStreamPromise = this.pdfManager.ensure(
this,
"getContentStream"
);
const contentStreamPromise = this.getContentStream();
const resourcesPromise = this.loadResources([
"ColorSpace",
"ExtGState",
@ -420,10 +413,7 @@ class Page {
sink,
combineTextItems,
}) {
const contentStreamPromise = this.pdfManager.ensure(
this,
"getContentStream"
);
const contentStreamPromise = this.getContentStream();
const resourcesPromise = this.loadResources([
"ExtGState",
"Font",

View File

@ -118,16 +118,24 @@ class Dict {
// Same as get(), but dereferences all elements if the result is an Array.
getArray(key1, key2, key3) {
let value = this.get(key1, key2, key3);
if (!Array.isArray(value) || !this.xref) {
return value;
}
value = value.slice(); // Ensure that we don't modify the Dict data.
for (let i = 0, ii = value.length; i < ii; i++) {
if (!(value[i] instanceof Ref)) {
continue;
let value = this._map[key1];
if (value === undefined && key2 !== undefined) {
value = this._map[key2];
if (value === undefined && key3 !== undefined) {
value = this._map[key3];
}
}
if (value instanceof Ref && this.xref) {
value = this.xref.fetch(value, this.suppressEncryption);
}
if (Array.isArray(value)) {
value = value.slice(); // Ensure that we don't modify the Dict data.
for (let i = 0, ii = value.length; i < ii; i++) {
if (value[i] instanceof Ref && this.xref) {
value[i] = this.xref.fetch(value[i], this.suppressEncryption);
}
}
value[i] = this.xref.fetch(value[i], this.suppressEncryption);
}
return value;
}