1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Disable system fonts on Android (issue 18210)

To avoid introducing any inline "hacks" in the viewer-code this meant adding `useSystemFonts` to the AppOptions, which thus required some new functionality since the default value should be `undefined` given how the option is handled in the API; note [this code](ed83d7c5e1/src/display/api.js (L298-L301)).

Finally, also moves the definition of the development-mode `window.isGECKOVIEW` property to the HTML file such that it's guaranteed to be set regardless of how and when it's accessed.
This commit is contained in:
Jonas Jenwald 2024-07-21 10:31:38 +02:00
parent 45ce3a057f
commit 80d6bf6319
4 changed files with 54 additions and 15 deletions

View file

@ -1003,16 +1003,6 @@ const PDFViewerApplication = {
AppOptions.set("docBaseUrl", this.baseUrl);
}
// On Android, there is almost no chance to have the font we want so we
// don't use the system fonts in this case.
if (
typeof PDFJSDev === "undefined"
? window.isGECKOVIEW
: PDFJSDev.test("GECKOVIEW")
) {
args.useSystemFonts = false;
}
// Set the necessary API parameters, using all the available options.
const apiParams = AppOptions.getAll(OptionKind.API);
const loadingTask = getDocument({

View file

@ -39,6 +39,14 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
compatParams.set("maxCanvasPixels", 5242880);
}
})();
// Don't use system fonts on Android (issue 18210).
// Support: Android
(function () {
if (isAndroid) {
compatParams.set("useSystemFonts", false);
}
})();
}
const OptionKind = {
@ -47,6 +55,7 @@ const OptionKind = {
API: 0x04,
WORKER: 0x08,
EVENT_DISPATCH: 0x10,
UNDEF_ALLOWED: 0x20,
PREFERENCE: 0x80,
};
@ -377,6 +386,19 @@ const defaultOptions = {
: "../web/standard_fonts/",
kind: OptionKind.API,
},
useSystemFonts: {
// On Android, there is almost no chance to have the font we want so we
// don't use the system fonts in this case (bug 1882613).
/** @type {boolean|undefined} */
value: (
typeof PDFJSDev === "undefined"
? window.isGECKOVIEW
: PDFJSDev.test("GECKOVIEW")
)
? false
: undefined,
kind: OptionKind.API + OptionKind.UNDEF_ALLOWED,
},
verbosity: {
/** @type {number} */
value: 1,
@ -464,6 +486,11 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
if (kind & OptionKind.BROWSER) {
throw new Error(`Cannot mix "PREFERENCE" and "BROWSER" kind: ${name}`);
}
if (kind & OptionKind.UNDEF_ALLOWED) {
throw new Error(
`Cannot allow \`undefined\` value for "PREFERENCE" kind: ${name}`
);
}
if (typeof compatParams === "object" && compatParams.has(name)) {
throw new Error(
`Should not have compatibility-value for "PREFERENCE" kind: ${name}`
@ -478,6 +505,11 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
throw new Error(`Invalid value for "PREFERENCE" kind: ${name}`);
}
} else if (kind & OptionKind.BROWSER) {
if (kind & OptionKind.UNDEF_ALLOWED) {
throw new Error(
`Cannot allow \`undefined\` value for "BROWSER" kind: ${name}`
);
}
if (typeof compatParams === "object" && compatParams.has(name)) {
throw new Error(
`Should not have compatibility-value for "BROWSER" kind: ${name}`
@ -522,7 +554,14 @@ class AppOptions {
static set(name, value) {
const defaultOpt = defaultOptions[name];
if (!defaultOpt || typeof value !== typeof defaultOpt.value) {
if (
!defaultOpt ||
!(
typeof value === typeof defaultOpt.value ||
(defaultOpt.kind & OptionKind.UNDEF_ALLOWED &&
(value === undefined || defaultOpt.value === undefined))
)
) {
return;
}
userOptions.set(name, value);
@ -535,7 +574,14 @@ class AppOptions {
const defaultOpt = defaultOptions[name],
userOpt = options[name];
if (!defaultOpt || typeof userOpt !== typeof defaultOpt.value) {
if (
!defaultOpt ||
!(
typeof userOpt === typeof defaultOpt.value ||
(defaultOpt.kind & OptionKind.UNDEF_ALLOWED &&
(userOpt === undefined || defaultOpt.value === undefined))
)
) {
continue;
}
if (prefs) {

View file

@ -42,6 +42,12 @@ See https://github.com/adobe-type-tools/cmap-resources
<!--#endif-->
<!--#if !MOZCENTRAL-->
<script>
if (typeof PDFJSDev === "undefined") {
window.isGECKOVIEW = true;
}
</script>
<script type="importmap">
{
"imports": {

View file

@ -60,9 +60,6 @@ function getViewerConfiguration() {
function webViewerLoad() {
const config = getViewerConfiguration();
if (typeof PDFJSDev === "undefined") {
window.isGECKOVIEW = true;
}
PDFViewerApplication.run(config);
}