Skip to content

Commit

Permalink
Changes required for pdf.js update to 4.4.168 version
Browse files Browse the repository at this point in the history
  • Loading branch information
ekaterina-chubrick committed Oct 30, 2024
1 parent 795874e commit bd1202a
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 75 deletions.
16 changes: 8 additions & 8 deletions src/core/bounding_boxes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {

Check failure on line 1 in src/core/bounding_boxes.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Replace `⏎··OPS,·Util,·IDENTITY_MATRIX⏎}·from·'../shared/util.js'` with `·OPS,·Util,·IDENTITY_MATRIX·}·from·"../shared/util.js"`
OPS, Util, IDENTITY_MATRIX

Check failure on line 2 in src/core/bounding_boxes.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Member 'IDENTITY_MATRIX' of the import declaration should be sorted alphabetically
} from '../shared/util';
} from '../shared/util.js';
import {

Check failure on line 4 in src/core/bounding_boxes.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Dependency cycle detected

Check failure on line 4 in src/core/bounding_boxes.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Replace `⏎··StateManager,⏎··TextState⏎` with `·StateManager,·TextState·`
StateManager,
TextState
} from "./evaluator";
import {isDict} from "./primitives";
} from "./evaluator.js";
import {isDict} from "./primitives.js";

Check failure on line 8 in src/core/bounding_boxes.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Replace `isDict` with `·isDict·`

var BoundingBoxesCalculator = (function PartialEvaluatorClosure() {

Check failure on line 10 in src/core/bounding_boxes.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Unexpected var, use let or const instead

Check failure on line 10 in src/core/bounding_boxes.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

Exporting mutable 'var' binding, use 'const' instead
function BoundingBoxesCalculator(ignoreCalculations) {

Check failure on line 11 in src/core/bounding_boxes.js

View workflow job for this annotation

GitHub Actions / Lint (lts/*)

'BoundingBoxesCalculator' is already declared in the upper scope on line 10 column 5
Expand Down Expand Up @@ -59,11 +59,11 @@ var BoundingBoxesCalculator = (function PartialEvaluatorClosure() {
[tx0, ty0] = Util.applyTransform([0, descent + rise], this.textStateManager.state.textMatrix);
//Calculate transformed height and shift to place whole glyph inside of bbox
shift = [tx0 - this.textStateManager.state.textMatrix[4], ty0 - this.textStateManager.state.textMatrix[5]];
height = Util.applyTransform([0, ascent - descent], this.textStateManager.state.textMatrix);
height = Util.applyTransform([0, ascent - descent], this.textStateManager.state.textMatrix);
} else {
[tx0, ty0] = Util.applyTransform([-this.textStateManager.state.fontSize / 2, rise], this.textStateManager.state.textMatrix);
shift = [tx0 - this.textStateManager.state.textMatrix[4], ty0 - this.textStateManager.state.textMatrix[5]];
height = Util.applyTransform([ascent - descent, 0], this.textStateManager.state.textMatrix);
height = Util.applyTransform([ascent - descent, 0], this.textStateManager.state.textMatrix);
}
height[0] -= this.textStateManager.state.textMatrix[4];
height[1] -= this.textStateManager.state.textMatrix[5];
Expand Down Expand Up @@ -713,7 +713,7 @@ var BoundingBoxStack = (function BoundingBoxStack() {
var NoMCIDBoundingBoxStack = (function NoMCIDBoundingBoxStack() {
function NoMCIDBoundingBoxStack() {
this.boundingBoxesStack = new BoundingBoxStack();
this.contentCounter = null;
this.contentCounter = null;
this.content = {};
this.pointer = {};
}
Expand All @@ -732,7 +732,7 @@ var NoMCIDBoundingBoxStack = (function NoMCIDBoundingBoxStack() {

save: function NoMCIDBoundingBoxStack_save(x, y, w, h) {
if (this.pointer.contentItems) {
this.pointer.contentItems.push({
this.pointer.contentItems.push({
contentItem: { x, y, w, h }
});
this.pointer.final = true;
Expand Down Expand Up @@ -761,7 +761,7 @@ var NoMCIDBoundingBoxStack = (function NoMCIDBoundingBoxStack() {
this.content[this.contentCounter.index] = { parent: this.content, contentItems: [] };
this.pointer = this.content[this.contentCounter.index];
} else if (
this.contentCounter.inMarkedContent !== false && isMC ||
this.contentCounter.inMarkedContent !== false && isMC ||
!!this.contentCounter.inMarkedContent === isMC && !Object.keys(this.pointer).length
) {
delete this.content[this.contentCounter.index].parent;
Expand Down
166 changes: 105 additions & 61 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -1760,73 +1760,77 @@ class ExtendedCatalog extends Catalog {
constructor(pdfManager, xref) {
super(pdfManager, xref);

this.pages = this.getPages(this.toplevelPagesDict.get('Kids'));
this.pages = this.getPages(this.toplevelPagesDict.get("Kids"));
this.roleMap = this.getRoleMap(this.structTreeRoot);
}

_convertStructToObject(struct) {
if (Array.isArray(struct)) {
return struct.map(el => this._convertStructToObject(el));
} else if (isDict(struct)) {
let result = {};
} else if (struct instanceof Dict) {
const result = {};
struct.getKeys().forEach(key => {
result[key] = this._convertStructToObject(struct.get(key));
});
return result;
} else if (isName(struct)) {
} else if (struct instanceof Name) {
return struct.name;
} else {
return struct;
}
return struct;
}

get structTreeRoot() {
const structTreeRoot = this._catDict.get('StructTreeRoot');
if (!isDict(structTreeRoot)) {
const structTreeRoot = this._catDict.get("StructTreeRoot");
if ((!structTreeRoot) instanceof Dict) {
return null;
}
return shadow(this, 'structTreeRoot', structTreeRoot);
return shadow(this, "structTreeRoot", structTreeRoot);
}

getTreeElement(el, page, ref) {
//update page for current element
if (isDict(el) && el.has('Pg')) {
let pageRef = el.getRaw('Pg');
let newPage = this.pages.findIndex(el => el.num === pageRef.num && el.gen === pageRef.gen);
// update page for current element
if (el instanceof Dict && el.has("Pg")) {
const pageRef = el.getRaw("Pg");
let newPage = this.pages.findIndex(
pageEl => pageEl.num === pageRef.num && pageEl.gen === pageRef.gen
);
newPage = newPage !== -1 ? newPage : null;
if (newPage !== page) {
page = newPage;
}
}

if (isDict(el) && el.has('K')) {
let name = el.has('S') ? el.get('S').name : null;
let roleName = this.getRoleName(el, name);
if (el instanceof Dict && el.has("K")) {
const name = el.has("S") ? el.get("S").name : null;
const roleName = this.getRoleName(el, name);

return {
name: name ? stringToUTF8String(name) : null,
roleName: roleName ? stringToUTF8String(roleName) : null,
children: this.getTreeElement(el.get('K'), page, el.getRaw('K')),
children: this.getTreeElement(el.get("K"), page, el.getRaw("K")),
pageIndex: page,
ref: ref
ref: ref instanceof Ref ? ref : null,
};
}

if (isDict(el) && el.has('Obj')) {
let obj = el.get('Obj');
if (el instanceof Dict && el.has("Obj")) {
const obj = el.get("Obj");
let type = null;
if (obj.has('Type')) {
type = obj.get('Type').name;
}
if (obj.has('Subtype')) {
type = obj.get('Subtype').name;
}
switch (type){
case 'Link':
case 'Annot':
let rect = obj.get('Rect');
let pageRef = Array.isArray(this.pages) && Number.isInteger(page) && page >= 0 ? this.pages[page] : null;
let pageObj = pageRef ? this.xref.fetch(pageRef) : null;
if (obj.has("Type")) {
type = obj.get("Type").name;
}
if (obj.has("Subtype")) {
type = obj.get("Subtype").name;
}
switch (type) {
case "Link":
case "Annot":
const rect = obj.get("Rect");
const pageRef =
Array.isArray(this.pages) && Number.isInteger(page) && page >= 0
? this.pages[page]
: null;
const pageObj = pageRef ? this.xref.fetch(pageRef) : null;

return {
annotIndex: this.getAnnotIndex(el, pageObj),
Expand All @@ -1841,49 +1845,55 @@ class ExtendedCatalog extends Catalog {
if (Array.isArray(el)) {
return el.map(subel => {
if (Number.isInteger(subel)) {
return {mcid: subel, pageIndex: page};
} else if (!(subel.hasOwnProperty('num') && subel.hasOwnProperty('gen')) && subel.get('Type') !== 'OBJR') {
return { mcid: subel, pageIndex: page };
} else if (
!(subel.hasOwnProperty("num") && subel.hasOwnProperty("gen")) &&
subel.get("Type") !== "OBJR"
) {
return this.getTreeElement(subel, page);
} else if (subel.hasOwnProperty('num') && subel.hasOwnProperty('gen')){
} else if (subel.hasOwnProperty("num") && subel.hasOwnProperty("gen")) {
return this.getTreeElement(this.xref.fetch(subel), page, subel);
}
})
return null;
});
}

if (Number.isInteger(el)) {
return {mcid: el, pageIndex: page};
return { mcid: el, pageIndex: page };
}

if (isDict(el) && el.has('Type') && el.get('Type').name === 'MCR') {
return {mcid: el.get('MCID'), pageIndex: page};
if (el instanceof Dict && el.has("Type") && el.get("Type").name === "MCR") {
return { mcid: el.get("MCID"), pageIndex: page };
}

if (isDict(el) && el.has('S')) {
let name = el.get('S').name;
let roleName = this.getRoleName(el, name);
if (el instanceof Dict && el.has("S")) {
const name = el.get("S").name;
const roleName = this.getRoleName(el, name);

return {
name: name ? stringToUTF8String(name) : null,
roleName: roleName ? stringToUTF8String(roleName) : null,
children: [],
pageIndex: page,
ref: ref
ref: ref instanceof Ref ? ref : null,
};
}

return null;
}

getPages(pages) {
let pagesArray = [];
pages.map(kid => {
if (kid instanceof Ref){
let kidObj = this.xref.fetch(kid);
let kidObjType = kidObj.get('Type').name;
if (kid instanceof Ref) {
const kidObj = this.xref.fetch(kid);
const kidObjType = kidObj.get("Type").name;
switch (kidObjType) {
case 'Page':
case "Page":
pagesArray.push(kid);
break;
case 'Pages':
let array = this.getPages(kidObj.get('Kids'));
case "Pages":
const array = this.getPages(kidObj.get("Kids"));
pagesArray = pagesArray.concat(array);
break;
default:
Expand All @@ -1895,31 +1905,65 @@ class ExtendedCatalog extends Catalog {
}

getRoleMap(tree) {
return isDict(tree) && tree.has('RoleMap') ? tree.get('RoleMap') : new Map();
return tree instanceof Dict && tree.has("RoleMap")
? tree.get("RoleMap")
: new Map();
}

getRoleName(el, name) {
let namespace = isDict(el) && el.has('NS') ? el.get('NS') : null;
let roleNameNS = isDict(namespace) && namespace.has('RoleMapNS') ? namespace.get('RoleMapNS') : null;
let roleNameNSArray = isDict(roleNameNS) && roleNameNS.has(name) ? roleNameNS.get(name) : null;
let roleName_v1 = this.roleMap.get(name) ? this.roleMap.get(name).name : null;
let roleName_v2 = Array.isArray(roleNameNSArray) && roleNameNSArray.length > 0 && roleNameNSArray[0].hasOwnProperty('name') ? roleNameNSArray[0].name : null;
const namespace = el instanceof Dict && el.has("NS") ? el.get("NS") : null;
const roleNameNS =
namespace instanceof Dict && namespace.has("RoleMapNS")
? namespace.get("RoleMapNS")
: null;
const roleNameNSArray =
roleNameNS instanceof Dict && roleNameNS.has(name)
? roleNameNS.get(name)
: null;
const roleName_v1 = this.roleMap.get(name)
? this.roleMap.get(name).name
: null;
const roleName_v2 =
Array.isArray(roleNameNSArray) &&
roleNameNSArray.length > 0 &&
roleNameNSArray[0].hasOwnProperty("name")
? roleNameNSArray[0].name
: null;
return roleName_v1 || roleName_v2 || name;
}

getAnnotIndex(el, pageObj) {
let objRef = isDict(el) && el.has('Obj') ? el.getRaw('Obj') : null;
let annotsArray = isDict(pageObj) && pageObj.has('Annots') ? pageObj.get('Annots') : null;
let annotIndex = Array.isArray(annotsArray) && annotsArray.length > 0 && objRef instanceof Ref ? annotsArray.findIndex(el => el.num === objRef.num && el.gen === objRef.gen) : null;
const objRef =
el instanceof Dict && el.has("Obj") ? el.getRaw("Obj") : null;
const annotsArray =
pageObj instanceof Dict && pageObj.has("Annots")
? pageObj.get("Annots")
: null;
const annotIndex =
Array.isArray(annotsArray) &&
annotsArray.length > 0 &&
objRef instanceof Ref
? annotsArray.findIndex(
annot => annot.num === objRef.num && annot.gen === objRef.gen
)
: null;
return annotIndex;
}

get structureTree() {
let structureTree = null;
if (this.structTreeRoot && isDict(this.structTreeRoot) && this.structTreeRoot.has('K')) {
structureTree = this.getTreeElement(this.structTreeRoot.get('K'), null, this.structTreeRoot.getRaw('K'));
if (
this.structTreeRoot &&
this.structTreeRoot instanceof Dict &&
this.structTreeRoot.has("K")
) {
structureTree = this.getTreeElement(
this.structTreeRoot.get("K"),
null,
this.structTreeRoot.getRaw("K")
);
}
return shadow(this, 'structureTree', structureTree);
return shadow(this, "structureTree", structureTree);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import { ImageResizer } from "./image_resizer.js";
import { MurmurHash3_64 } from "../shared/murmurhash3.js";
import { OperatorList } from "./operator_list.js";
import { PDFImage } from "./image.js";
import { BoundingBoxesCalculator } from "./bounding_boxes";
import { BoundingBoxesCalculator } from "./bounding_boxes.js";

const DefaultPartialEvaluatorOptions = Object.freeze({
maxImageSize: -1,
Expand Down
15 changes: 13 additions & 2 deletions src/core/flate_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,19 @@ class FlateStream extends DecodeStream {
try {
const { readable, writable } = new DecompressionStream("deflate");
const writer = writable.getWriter();
writer.write(bytes);
writer.close();
// SHOULD BE FIXED IN FUTURE PDF.JS VERSIONS - START
await writer.ready;

// We can't await writer.write() because it'll block until the reader
// starts which happens few lines below.
writer
.write(bytes)
.then(async () => {
await writer.ready;
await writer.close();
})
.catch(() => {});
// SHOULD BE FIXED IN FUTURE PDF.JS VERSIONS - END

const chunks = [];
let totalLength = 0;
Expand Down
1 change: 1 addition & 0 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ class WorkerMessageHandler {

function setupDoc(data) {
function onSuccess(doc) {
console.log(doc);
ensureNotTerminated();
handler.send("GetDoc", { pdfInfo: doc });
}
Expand Down
12 changes: 10 additions & 2 deletions src/core/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,16 @@ async function writeStream(stream, buffer, transform) {
try {
const cs = new CompressionStream("deflate");
const writer = cs.writable.getWriter();
writer.write(bytes);
writer.close();
// SHOULD BE FIXED IN FUTURE PDF.JS VERSIONS - START
await writer.ready;
writer
.write(bytes)
.then(async () => {
await writer.ready;
await writer.close();
})
.catch(() => {});
// SHOULD BE FIXED IN FUTURE PDF.JS VERSIONS - END

// Response::text doesn't return the correct data.
const buf = await new Response(cs.readable).arrayBuffer();
Expand Down
1 change: 1 addition & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1958,6 +1958,7 @@ class LoopbackPort {
#deferred = Promise.resolve();

postMessage(obj, transfer) {
console.log(obj);
const event = {
data: structuredClone(obj, transfer ? { transfer } : null),
};
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.16.105",
"version": "4.4.168",
"build": 305,
"commit": "eaaa8b4"
}

0 comments on commit bd1202a

Please sign in to comment.