Skip to content

Commit a45e4a3

Browse files
committed
Use Calibri and Lucida Console, when it's possible, in place of sans-serif and monospaced (bug 1922063)
A recent change in Firefox induced too much difference between the text widths computed in using a Canvas and the ones computed by the text layout engine when rendering the text layer. Consequently, the text selection can be bad on Windows with some fonts like Arial or Consolas. This patch is a workaround to try to use in first place some fonts which don't have the problem.
1 parent 5a25c47 commit a45e4a3

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/display/text_layer.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
/** @typedef {import("./display_utils").PageViewport} PageViewport */
1717
/** @typedef {import("./api").TextContent} TextContent */
1818

19-
import { AbortException, Util, warn } from "../shared/util.js";
19+
import {
20+
AbortException,
21+
FeatureTest,
22+
shadow,
23+
Util,
24+
warn,
25+
} from "../shared/util.js";
2026
import { setLayerDimensions } from "./display_utils.js";
2127

2228
/**
@@ -152,6 +158,24 @@ class TextLayer {
152158
}
153159
}
154160

161+
static get fontFamilyMap() {
162+
const { isWindows, isFirefox } = FeatureTest.platform;
163+
return shadow(
164+
this,
165+
"fontFamilyMap",
166+
new Map([
167+
[
168+
"sans-serif",
169+
`${isWindows && isFirefox ? "Calibri, " : ""}sans-serif`,
170+
],
171+
[
172+
"monospace",
173+
`${isWindows && isFirefox ? "Lucida Console, " : ""}monospace`,
174+
],
175+
])
176+
);
177+
}
178+
155179
/**
156180
* Render the textLayer.
157181
* @returns {Promise}
@@ -300,9 +324,12 @@ class TextLayer {
300324
angle += Math.PI / 2;
301325
}
302326

303-
const fontFamily =
327+
let fontFamily =
304328
(this.#fontInspectorEnabled && style.fontSubstitution) ||
305329
style.fontFamily;
330+
331+
// Workaround for bug 1922063.
332+
fontFamily = TextLayer.fontFamilyMap.get(fontFamily) || fontFamily;
306333
const fontHeight = Math.hypot(tx[2], tx[3]);
307334
const fontAscent =
308335
fontHeight * TextLayer.#getAscent(fontFamily, this.#lang);

src/shared/util.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,9 +636,18 @@ class FeatureTest {
636636
) {
637637
return shadow(this, "platform", {
638638
isMac: navigator.platform.includes("Mac"),
639+
isWindows: navigator.platform.includes("Win"),
640+
isFirefox:
641+
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
642+
(typeof navigator?.userAgent === "string" &&
643+
navigator.userAgent.includes("Firefox")),
639644
});
640645
}
641-
return shadow(this, "platform", { isMac: false });
646+
return shadow(this, "platform", {
647+
isMac: false,
648+
isWindows: false,
649+
isFirefox: false,
650+
});
642651
}
643652

644653
static get isCSSRoundSupported() {

0 commit comments

Comments
 (0)