Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Editor] Avoid conflicts between new persistent refs and the ones created when saving (bug 1865341) #17374

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/core/font_substitutions.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ function getFontSubstitution(
baseFontName,
standardFontName
) {
if (baseFontName.startsWith("InvalidPDFjsFont_")) {
return null;
}

// It's possible to have a font name with spaces, commas or dashes, hence we
// just replace them by a dash.
baseFontName = normalizeFontName(baseFontName);
Expand Down
20 changes: 20 additions & 0 deletions src/core/xref.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class XRef {
this._pendingRefs = new RefSet();
this._newPersistentRefNum = null;
this._newTemporaryRefNum = null;
this._persistentRefsCache = null;
}

getNewPersistentRef(obj) {
Expand All @@ -63,13 +64,32 @@ class XRef {
// stream.
if (this._newTemporaryRefNum === null) {
this._newTemporaryRefNum = this.entries.length || 1;
if (this._newPersistentRefNum) {
this._persistentRefsCache = new Map();
for (
let i = this._newTemporaryRefNum;
i < this._newPersistentRefNum;
i++
) {
// We *temporarily* clear the cache, see `resetNewTemporaryRef` below,
// to avoid any conflict with the refs created during saving.
this._persistentRefsCache.set(i, this._cacheMap.get(i));
this._cacheMap.delete(i);
}
}
}
return Ref.get(this._newTemporaryRefNum++, 0);
}

resetNewTemporaryRef() {
// Called once saving is finished.
this._newTemporaryRefNum = null;
if (this._persistentRefsCache) {
for (const [num, obj] of this._persistentRefsCache) {
this._cacheMap.set(num, obj);
}
}
this._persistentRefsCache = null;
}

setStartXRef(startXRef) {
Expand Down
6 changes: 6 additions & 0 deletions test/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,12 @@ class Driver {
if (!task.annotationStorage) {
throw new Error("Missing `annotationStorage` entry.");
}
if (task.loadAnnotations) {
for (let num = 1; num <= doc.numPages; num++) {
const page = await doc.getPage(num);
await page.getAnnotations({ intent: "display" });
}
}
doc.annotationStorage.setAll(task.annotationStorage);

const data = await doc.saveDocument();
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,4 @@
!issue17069.pdf
!issue17215.pdf
!bug1863910.pdf
!bug1865341.pdf
Binary file added test/pdfs/bug1865341.pdf
Binary file not shown.
21 changes: 21 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8381,5 +8381,26 @@
"link": true,
"type": "eq",
"annotations": true
},
{
"id": "bug1865341",
"file": "pdfs/bug1865341.pdf",
"md5": "e241b6d1dc493df73e77e3eeabc71721",
"rounds": 1,
"type": "eq",
"save": true,
"print": true,
"loadAnnotations": true,
"annotationStorage": {
"pdfjs_internal_editor_0": {
"annotationType": 3,
"color": [255, 0, 0],
"fontSize": 10,
"value": "Załącznik",
"pageIndex": 0,
"rect": [ 238, 629, 287, 649],
"rotation": 0
}
}
}
]