diff --git a/src/core/default_appearance.js b/src/core/default_appearance.js index 6b36572d6..888cb33e1 100644 --- a/src/core/default_appearance.js +++ b/src/core/default_appearance.js @@ -154,7 +154,7 @@ class AppearanceStreamEvaluator extends EvaluatorPreprocessor { result.fontName = fontName.name; } if (typeof fontSize === "number" && fontSize > 0) { - result.fontSize = fontSize * result.scaleFactor; + result.fontSize = fontSize; } break; case OPS.setFillColorSpace: @@ -184,6 +184,10 @@ class AppearanceStreamEvaluator extends EvaluatorPreprocessor { case OPS.showSpacedText: case OPS.nextLineShowText: case OPS.nextLineSetSpacingShowText: + // The font (Tf) and the text matrix (Tm) can be set in any order, + // so the scale factor is applied here, when text is actually shown + // and both are known to be in effect. + result.fontSize *= result.scaleFactor; breakLoop = true; break; } diff --git a/test/integration/freetext_editor_spec.mjs b/test/integration/freetext_editor_spec.mjs index 35f10c542..28240b5ac 100644 --- a/test/integration/freetext_editor_spec.mjs +++ b/test/integration/freetext_editor_spec.mjs @@ -1876,6 +1876,49 @@ describe("FreeText Editor", () => { }); }); + describe("FreeText (open existing generated with Skia)", () => { + let pages; + + beforeEach(async () => { + pages = await loadAndWait( + "issue20504_skia.pdf", + ".annotationEditorLayer", + 100 + ); + }); + + afterEach(async () => { + await closePages(pages); + }); + + it("must extract the font size when Tf comes before Tm", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + const id = "24R"; + await page.waitForSelector(getAnnotationSelector(id), { + visible: true, + }); + + await switchToFreeText(page); + + // The Skia appearance stream sets the font (Tf) before the text + // matrix (Tm), and the text matrix is scaled by 0.5 through a cm + // operator: 20 * 0.5 = 10. + const fontSize = await page.evaluate(() => { + const editorDiv = document.getElementById( + `pdfjs_internal_editor_0-editor` + ); + const match = editorDiv?.style.fontSize.match(/calc\((\d+)px/); + return match ? parseInt(match[1], 10) : 0; + }); + expect(fontSize) + .withContext(`In ${browserName}, editor 0 fontSize`) + .toEqual(10); + }) + ); + }); + }); + describe("Keyboard shortcuts when the editor layer isn't focused", () => { let pages; diff --git a/test/pdfs/.gitignore b/test/pdfs/.gitignore index a7b875122..54d04c8cf 100644 --- a/test/pdfs/.gitignore +++ b/test/pdfs/.gitignore @@ -941,3 +941,4 @@ !multimedia_annotations.pdf !issue17333.pdf !issue20504.pdf +!issue20504_skia.pdf diff --git a/test/pdfs/issue20504_skia.pdf b/test/pdfs/issue20504_skia.pdf new file mode 100644 index 000000000..ab5dd609c Binary files /dev/null and b/test/pdfs/issue20504_skia.pdf differ