Skip to content

Commit 03b9a80

Browse files
committed
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](https://github.com/mozilla/pdf.js/blob/ed83d7c5e16798a56c493d56aaa8200dd280bb17/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.
1 parent 98f7af3 commit 03b9a80

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

web/app.js

-10
Original file line numberDiff line numberDiff line change
@@ -1003,16 +1003,6 @@ const PDFViewerApplication = {
10031003
AppOptions.set("docBaseUrl", this.baseUrl);
10041004
}
10051005

1006-
// On Android, there is almost no chance to have the font we want so we
1007-
// don't use the system fonts in this case.
1008-
if (
1009-
typeof PDFJSDev === "undefined"
1010-
? window.isGECKOVIEW
1011-
: PDFJSDev.test("GECKOVIEW")
1012-
) {
1013-
args.useSystemFonts = false;
1014-
}
1015-
10161006
// Set the necessary API parameters, using all the available options.
10171007
const apiParams = AppOptions.getAll(OptionKind.API);
10181008
const loadingTask = getDocument({

web/app_options.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
3939
compatParams.set("maxCanvasPixels", 5242880);
4040
}
4141
})();
42+
43+
// Don't use system fonts on Android (issue 18210).
44+
// Support: Android
45+
(function () {
46+
if (isAndroid) {
47+
compatParams.set("useSystemFonts", false);
48+
}
49+
})();
4250
}
4351

4452
const OptionKind = {
@@ -47,6 +55,7 @@ const OptionKind = {
4755
API: 0x04,
4856
WORKER: 0x08,
4957
EVENT_DISPATCH: 0x10,
58+
UNDEF_ALLOWED: 0x20,
5059
PREFERENCE: 0x80,
5160
};
5261

@@ -377,6 +386,19 @@ const defaultOptions = {
377386
: "../web/standard_fonts/",
378387
kind: OptionKind.API,
379388
},
389+
useSystemFonts: {
390+
// On Android, there is almost no chance to have the font we want so we
391+
// don't use the system fonts in this case (bug 1882613).
392+
/** @type {boolean|undefined} */
393+
value: (
394+
typeof PDFJSDev === "undefined"
395+
? window.isGECKOVIEW
396+
: PDFJSDev.test("GECKOVIEW")
397+
)
398+
? false
399+
: undefined,
400+
kind: OptionKind.API + OptionKind.UNDEF_ALLOWED,
401+
},
380402
verbosity: {
381403
/** @type {number} */
382404
value: 1,
@@ -464,6 +486,11 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
464486
if (kind & OptionKind.BROWSER) {
465487
throw new Error(`Cannot mix "PREFERENCE" and "BROWSER" kind: ${name}`);
466488
}
489+
if (kind & OptionKind.UNDEF_ALLOWED) {
490+
throw new Error(
491+
`Cannot allow \`undefined\` value for "PREFERENCE" kind: ${name}`
492+
);
493+
}
467494
if (typeof compatParams === "object" && compatParams.has(name)) {
468495
throw new Error(
469496
`Should not have compatibility-value for "PREFERENCE" kind: ${name}`
@@ -478,6 +505,11 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
478505
throw new Error(`Invalid value for "PREFERENCE" kind: ${name}`);
479506
}
480507
} else if (kind & OptionKind.BROWSER) {
508+
if (kind & OptionKind.UNDEF_ALLOWED) {
509+
throw new Error(
510+
`Cannot allow \`undefined\` value for "BROWSER" kind: ${name}`
511+
);
512+
}
481513
if (typeof compatParams === "object" && compatParams.has(name)) {
482514
throw new Error(
483515
`Should not have compatibility-value for "BROWSER" kind: ${name}`
@@ -522,7 +554,14 @@ class AppOptions {
522554
static set(name, value) {
523555
const defaultOpt = defaultOptions[name];
524556

525-
if (!defaultOpt || typeof value !== typeof defaultOpt.value) {
557+
if (
558+
!defaultOpt ||
559+
!(
560+
typeof value === typeof defaultOpt.value ||
561+
(defaultOpt.kind & OptionKind.UNDEF_ALLOWED &&
562+
(value === undefined || defaultOpt.value === undefined))
563+
)
564+
) {
526565
return;
527566
}
528567
userOptions.set(name, value);
@@ -535,7 +574,14 @@ class AppOptions {
535574
const defaultOpt = defaultOptions[name],
536575
userOpt = options[name];
537576

538-
if (!defaultOpt || typeof userOpt !== typeof defaultOpt.value) {
577+
if (
578+
!defaultOpt ||
579+
!(
580+
typeof userOpt === typeof defaultOpt.value ||
581+
(defaultOpt.kind & OptionKind.UNDEF_ALLOWED &&
582+
(userOpt === undefined || defaultOpt.value === undefined))
583+
)
584+
) {
539585
continue;
540586
}
541587
if (prefs) {

web/viewer-geckoview.html

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
<!--#endif-->
4343

4444
<!--#if !MOZCENTRAL-->
45+
<script>
46+
if (typeof PDFJSDev === "undefined") {
47+
window.isGECKOVIEW = true;
48+
}
49+
</script>
50+
4551
<script type="importmap">
4652
{
4753
"imports": {

web/viewer-geckoview.js

-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ function getViewerConfiguration() {
6060
function webViewerLoad() {
6161
const config = getViewerConfiguration();
6262

63-
if (typeof PDFJSDev === "undefined") {
64-
window.isGECKOVIEW = true;
65-
}
6663
PDFViewerApplication.run(config);
6764
}
6865

0 commit comments

Comments
 (0)