Skip to content

Commit 7de8c7c

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 216d3a9 commit 7de8c7c

File tree

4 files changed

+56
-19
lines changed

4 files changed

+56
-19
lines changed

web/app.js

-10
Original file line numberDiff line numberDiff line change
@@ -1008,16 +1008,6 @@ const PDFViewerApplication = {
10081008
AppOptions.set("docBaseUrl", this.baseUrl);
10091009
}
10101010

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

web/app_options.js

+50-6
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,19 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
3434

3535
// Limit canvas size to 5 mega-pixels on mobile.
3636
// Support: Android, iOS
37-
(function checkCanvasSizeLimitation() {
37+
(function () {
3838
if (isIOS || isAndroid) {
3939
compatibilityParams.maxCanvasPixels = 5242880;
4040
}
4141
})();
42+
43+
// Don't use system fonts on Android (issue 18210).
44+
// Support: Android
45+
(function () {
46+
if (isAndroid) {
47+
compatibilityParams.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 have \`undefined\` value for "PREFERENCE" kind: ${name}`
492+
);
493+
}
467494
if (
468495
typeof compatibilityParams === "object" &&
469496
compatibilityParams[name] !== undefined
@@ -480,6 +507,12 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
480507
) {
481508
throw new Error(`Invalid value for "PREFERENCE" kind: ${name}`);
482509
}
510+
} else if (kind & OptionKind.BROWSER) {
511+
if (kind & OptionKind.UNDEF_ALLOWED) {
512+
throw new Error(
513+
`Cannot have \`undefined\` value for "BROWSER" kind: ${name}`
514+
);
515+
}
483516
}
484517
}
485518
}
@@ -513,7 +546,13 @@ class AppOptions {
513546
static set(name, value) {
514547
const defaultOption = defaultOptions[name];
515548

516-
if (!defaultOption || typeof value !== typeof defaultOption.value) {
549+
if (
550+
!defaultOption ||
551+
!(
552+
typeof value === typeof defaultOption.value ||
553+
defaultOption.kind & OptionKind.UNDEF_ALLOWED
554+
)
555+
) {
517556
return;
518557
}
519558
userOptions[name] = value;
@@ -526,7 +565,13 @@ class AppOptions {
526565
const defaultOption = defaultOptions[name],
527566
userOption = options[name];
528567

529-
if (!defaultOption || typeof userOption !== typeof defaultOption.value) {
568+
if (
569+
!defaultOption ||
570+
!(
571+
typeof userOption === typeof defaultOption.value ||
572+
defaultOption.kind & OptionKind.UNDEF_ALLOWED
573+
)
574+
) {
530575
continue;
531576
}
532577
if (prefs) {
@@ -554,9 +599,8 @@ class AppOptions {
554599

555600
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
556601
// Re-apply a compatibility-value, if it exists, to the user-options.
557-
const val = compatibilityParams[name];
558-
if (val !== undefined) {
559-
userOptions[name] = val;
602+
if (name in compatibilityParams) {
603+
userOptions[name] = compatibilityParams[name];
560604
}
561605
}
562606
}

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)