From a45e4a391a8ead2a3ae75b48082bee71478ba369 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Thu, 3 Oct 2024 18:21:29 +0200 Subject: [PATCH] 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. --- src/display/text_layer.js | 31 +++++++++++++++++++++++++++++-- src/shared/util.js | 11 ++++++++++- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/display/text_layer.js b/src/display/text_layer.js index 2f72b712ef71f..4bcfde930174c 100644 --- a/src/display/text_layer.js +++ b/src/display/text_layer.js @@ -16,7 +16,13 @@ /** @typedef {import("./display_utils").PageViewport} PageViewport */ /** @typedef {import("./api").TextContent} TextContent */ -import { AbortException, Util, warn } from "../shared/util.js"; +import { + AbortException, + FeatureTest, + shadow, + Util, + warn, +} from "../shared/util.js"; import { setLayerDimensions } from "./display_utils.js"; /** @@ -152,6 +158,24 @@ class TextLayer { } } + static get fontFamilyMap() { + const { isWindows, isFirefox } = FeatureTest.platform; + return shadow( + this, + "fontFamilyMap", + new Map([ + [ + "sans-serif", + `${isWindows && isFirefox ? "Calibri, " : ""}sans-serif`, + ], + [ + "monospace", + `${isWindows && isFirefox ? "Lucida Console, " : ""}monospace`, + ], + ]) + ); + } + /** * Render the textLayer. * @returns {Promise} @@ -300,9 +324,12 @@ class TextLayer { angle += Math.PI / 2; } - const fontFamily = + let fontFamily = (this.#fontInspectorEnabled && style.fontSubstitution) || style.fontFamily; + + // Workaround for bug 1922063. + fontFamily = TextLayer.fontFamilyMap.get(fontFamily) || fontFamily; const fontHeight = Math.hypot(tx[2], tx[3]); const fontAscent = fontHeight * TextLayer.#getAscent(fontFamily, this.#lang); diff --git a/src/shared/util.js b/src/shared/util.js index 19be8031ec8b1..de0abe5796e4a 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -636,9 +636,18 @@ class FeatureTest { ) { return shadow(this, "platform", { isMac: navigator.platform.includes("Mac"), + isWindows: navigator.platform.includes("Win"), + isFirefox: + (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) || + (typeof navigator?.userAgent === "string" && + navigator.userAgent.includes("Firefox")), }); } - return shadow(this, "platform", { isMac: false }); + return shadow(this, "platform", { + isMac: false, + isWindows: false, + isFirefox: false, + }); } static get isCSSRoundSupported() {