Skip to content

Commit 4ffc076

Browse files
committed
Take into account the current transform when getting font size for FreeText
Fixes issue #20504. And the text position in Arabic FreeText annotations.
1 parent ee72d8d commit 4ffc076

5 files changed

Lines changed: 83 additions & 2 deletions

File tree

src/core/annotation.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,8 @@ class Annotation {
12861286

12871287
const text = [];
12881288
const buffer = [];
1289+
let firstPositionX = Infinity;
1290+
let firstPositionY = Infinity;
12891291
let firstPosition = null;
12901292
const sink = {
12911293
desiredSize: Math.Infinity,
@@ -1296,7 +1298,8 @@ class Annotation {
12961298
if (item.str === undefined) {
12971299
continue;
12981300
}
1299-
firstPosition ||= item.transform.slice(-2);
1301+
firstPositionX = Math.min(firstPositionX, item.transform[4]);
1302+
firstPositionY = Math.min(firstPositionY, item.transform[5]);
13001303
buffer.push(item.str);
13011304
if (item.hasEOL) {
13021305
text.push(buffer.join("").trimEnd());
@@ -1317,6 +1320,10 @@ class Annotation {
13171320
});
13181321
this.reset();
13191322

1323+
if (firstPositionX !== Infinity) {
1324+
firstPosition = [firstPositionX, firstPositionY];
1325+
}
1326+
13201327
if (buffer.length) {
13211328
text.push(buffer.join("").trimEnd());
13221329
}

src/core/default_appearance.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
LINE_FACTOR,
2727
OPS,
2828
shadow,
29+
Util,
2930
warn,
3031
} from "../shared/util.js";
3132
import { ColorSpaceUtils } from "./colorspace_utils.js";
@@ -144,7 +145,8 @@ class AppearanceStreamEvaluator extends EvaluatorPreprocessor {
144145
result = stack.pop() || result;
145146
break;
146147
case OPS.setTextMatrix:
147-
result.scaleFactor *= Math.hypot(args[0], args[1]);
148+
const tm = Util.transform(this.stateManager.state.ctm, args);
149+
result.scaleFactor *= Math.hypot(tm[0], tm[1]);
148150
break;
149151
case OPS.setFont:
150152
const [fontName, fontSize] = args;

test/integration/freetext_editor_spec.mjs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,77 @@ describe("FreeText Editor", () => {
17991799
});
18001800
});
18011801

1802+
describe("FreeText (open existing generated with Cairo)", () => {
1803+
let pages;
1804+
1805+
beforeEach(async () => {
1806+
pages = await loadAndWait(
1807+
"issue20504.pdf",
1808+
".annotationEditorLayer",
1809+
100
1810+
);
1811+
});
1812+
1813+
afterEach(async () => {
1814+
await closePages(pages);
1815+
});
1816+
1817+
it("must open some existing annotations", async () => {
1818+
await Promise.all(
1819+
pages.map(async ([browserName, page]) => {
1820+
const boxes = [];
1821+
for (const num of [48, 49, 50, 51, 52]) {
1822+
const id = `${num}R`;
1823+
await page.waitForSelector(getAnnotationSelector(id), {
1824+
visible: true,
1825+
});
1826+
const rect = await getRect(page, getAnnotationSelector(id));
1827+
boxes.push(rect);
1828+
}
1829+
1830+
await switchToFreeText(page);
1831+
1832+
// The font sizes extracted from the Cairo appearance streams.
1833+
const expectedFontSizes = [61, 38, 38, 38, 56];
1834+
const PRECISION = 0.3;
1835+
1836+
for (let i = 0; i < boxes.length; i++) {
1837+
const rect = await getRect(page, `#pdfjs_internal_editor_${i}`);
1838+
1839+
// The default used font can be different from a platform to another
1840+
// hence we just check that the dimensions have the some order of
1841+
// magnitude as the annotation rect.
1842+
expect(Math.abs(rect.width / boxes[i].width - 1))
1843+
.withContext(`In ${browserName}, editor ${i} width`)
1844+
.toBeLessThan(PRECISION);
1845+
expect(Math.abs(rect.height / boxes[i].height - 1))
1846+
.withContext(`In ${browserName}, editor ${i} height`)
1847+
.toBeLessThan(PRECISION);
1848+
expect(Math.abs(rect.x / boxes[i].x - 1))
1849+
.withContext(`In ${browserName}, editor ${i} x`)
1850+
.toBeLessThan(PRECISION);
1851+
expect(Math.abs(rect.y / boxes[i].y - 1))
1852+
.withContext(`In ${browserName}, editor ${i} y`)
1853+
.toBeLessThan(PRECISION);
1854+
1855+
// Verify that the font size is correctly extracted from the Cairo
1856+
// appearance stream (font size is encoded in the cm operator).
1857+
const fontSize = await page.evaluate(N => {
1858+
const editorDiv = document.getElementById(
1859+
`pdfjs_internal_editor_${N}-editor`
1860+
);
1861+
const match = editorDiv?.style.fontSize.match(/calc\((\d+)px/);
1862+
return match ? parseInt(match[1]) : 0;
1863+
}, i);
1864+
expect(fontSize)
1865+
.withContext(`In ${browserName}, editor ${i} fontSize`)
1866+
.toEqual(expectedFontSizes[i]);
1867+
}
1868+
})
1869+
);
1870+
});
1871+
});
1872+
18021873
describe("Keyboard shortcuts when the editor layer isn't focused", () => {
18031874
let pages;
18041875

test/pdfs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,4 @@
904904
!three_pages_with_number.pdf
905905
!issue13520.pdf
906906
!22060_A1_01_Plans.pdf
907+
!issue20504.pdf

test/pdfs/issue20504.pdf

31.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)