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

View File

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