Skip to content

Commit bb0fd5e

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 4af193b commit bb0fd5e

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

src/core/annotation.js

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

12791279
const text = [];
12801280
const buffer = [];
1281+
let firstPositionX = Infinity;
1282+
let firstPositionY = Infinity;
12811283
let firstPosition = null;
12821284
const sink = {
12831285
desiredSize: Math.Infinity,
@@ -1288,7 +1290,8 @@ class Annotation {
12881290
if (item.str === undefined) {
12891291
continue;
12901292
}
1291-
firstPosition ||= item.transform.slice(-2);
1293+
firstPositionX = Math.min(firstPositionX, item.transform[4]);
1294+
firstPositionY = Math.min(firstPositionY, item.transform[5]);
12921295
buffer.push(item.str);
12931296
if (item.hasEOL) {
12941297
text.push(buffer.join("").trimEnd());
@@ -1309,6 +1312,10 @@ class Annotation {
13091312
});
13101313
this.reset();
13111314

1315+
if (firstPositionX !== Infinity) {
1316+
firstPosition = [firstPositionX, firstPositionY];
1317+
}
1318+
13121319
if (buffer.length) {
13131320
text.push(buffer.join("").trimEnd());
13141321
}

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";
@@ -145,7 +146,8 @@ class AppearanceStreamEvaluator extends EvaluatorPreprocessor {
145146
result = stack.pop() || result;
146147
break;
147148
case OPS.setTextMatrix:
148-
result.scaleFactor *= Math.hypot(args[0], args[1]);
149+
const tm = Util.transform(this.stateManager.state.ctm, args);
150+
result.scaleFactor *= Math.hypot(tm[0], tm[1]);
149151
break;
150152
case OPS.setFont:
151153
const [fontName, fontSize] = args;

test/integration/freetext_editor_spec.mjs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,60 @@ 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+
for (let i = 0; i < boxes.length; i++) {
1833+
const rect = await getRect(
1834+
page,
1835+
`#pdfjs_internal_editor_${i}-editor`
1836+
);
1837+
1838+
expect(Math.abs(rect.width - boxes[i].width) <= 20)
1839+
.withContext(`In ${browserName}`)
1840+
.toEqual(true);
1841+
expect(Math.abs(rect.height - boxes[i].height) <= 20)
1842+
.withContext(`In ${browserName}`)
1843+
.toEqual(true);
1844+
expect(Math.abs(rect.x - boxes[i].x) <= 20)
1845+
.withContext(`In ${browserName}`)
1846+
.toEqual(true);
1847+
expect(Math.abs(rect.y - boxes[i].y) <= 20)
1848+
.withContext(`In ${browserName}`)
1849+
.toEqual(true);
1850+
}
1851+
})
1852+
);
1853+
});
1854+
});
1855+
18021856
describe("Keyboard shortcuts when the editor layer isn't focused", () => {
18031857
let pages;
18041858

test/pdfs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,4 @@
765765
!two_paragraphs.pdf
766766
!paragraph_and_link.pdf
767767
!issue20225.pdf
768+
!issue20504.pdf

test/pdfs/issue20504.pdf

31.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)