Skip to content

Commit

Permalink
[Editor] Add the ability to create/update the structure tree when sav…
Browse files Browse the repository at this point in the history
…ing a pdf containing newly added annotations (bug 1845087)

When there is no tree, the tags for the new annotions are just put under the root element.
When there is a tree, we insert the new tags at the right place in using the value
of structTreeParentId (added in PR mozilla#16916).
  • Loading branch information
calixteman committed Sep 16, 2023
1 parent 7f8de83 commit a8573d4
Show file tree
Hide file tree
Showing 8 changed files with 613 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,7 @@ class MarkupAnnotation extends Annotation {
}

static async createNewAnnotation(xref, annotation, dependencies, params) {
const annotationRef = annotation.ref || xref.getNewTemporaryRef();
const annotationRef = (annotation.ref ||= xref.getNewTemporaryRef());
const ap = await this.createNewAppearanceStream(annotation, xref, params);
const buffer = [];
let annotationDict;
Expand All @@ -1652,6 +1652,9 @@ class MarkupAnnotation extends Annotation {
} else {
annotationDict = this.createNewDict(annotation, xref, {});
}
if (Number.isInteger(annotation.parentTreeId)) {
annotationDict.set("StructParent", annotation.parentTreeId);
}

buffer.length = 0;
await writeObject(annotationRef, annotationDict, buffer, xref);
Expand Down
10 changes: 8 additions & 2 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class Catalog {
this.systemFontCache = new Map();
}

cloneDict() {
return this._catDict.clone();
}

get version() {
const version = this._catDict.get("Version");
if (version instanceof Name) {
Expand Down Expand Up @@ -245,11 +249,13 @@ class Catalog {
* @private
*/
_readStructTreeRoot() {
const obj = this._catDict.get("StructTreeRoot");
const rawObj = this._catDict.getRaw("StructTreeRoot");
const obj = this.xref.fetchIfRef(rawObj);
if (!(obj instanceof Dict)) {
return null;
}
const root = new StructTreeRoot(obj);

const root = new StructTreeRoot(obj, rawObj);
root.init();
return root;
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/pdf_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class BasePdfManager {
return this._docBaseUrl;
}

get catalog() {
return this.pdfDocument.catalog;
}

ensureDoc(prop, args) {
return this.ensure(this.pdfDocument, prop, args);
}
Expand Down
8 changes: 8 additions & 0 deletions src/core/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ class Dict {

return mergedDict.size > 0 ? mergedDict : Dict.empty;
}

clone() {
const dict = new Dict(this.xref);
for (const key of this.getKeys()) {
dict.set(key, this.getRaw(key));
}
return dict;
}
}

class Ref {
Expand Down
Loading

0 comments on commit a8573d4

Please sign in to comment.