Skip to content

Commit 1aed680

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 e92a6a1 commit 1aed680

File tree

4 files changed

+67
-19
lines changed

4 files changed

+67
-19
lines changed

web/app.js

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

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

web/app_options.js

+61-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 allow \`undefined\` value for "PREFERENCE" kind: ${name}`
492+
);
493+
}
467494
if (
468495
typeof compatibilityParams === "object" &&
469496
compatibilityParams[name] !== undefined
@@ -480,6 +507,23 @@ 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 allow \`undefined\` value for "BROWSER" kind: ${name}`
514+
);
515+
}
516+
if (
517+
typeof compatibilityParams === "object" &&
518+
compatibilityParams[name] !== undefined
519+
) {
520+
throw new Error(
521+
`Should not have compatibility-value for "BROWSER" kind: ${name}`
522+
);
523+
}
524+
if (value === undefined) {
525+
throw new Error(`Invalid value for "BROWSER" kind: ${name}`);
526+
}
483527
}
484528
}
485529
}
@@ -513,7 +557,13 @@ class AppOptions {
513557
static set(name, value) {
514558
const defaultOption = defaultOptions[name];
515559

516-
if (!defaultOption || typeof value !== typeof defaultOption.value) {
560+
if (
561+
!defaultOption ||
562+
!(
563+
typeof value === typeof defaultOption.value ||
564+
defaultOption.kind & OptionKind.UNDEF_ALLOWED
565+
)
566+
) {
517567
return;
518568
}
519569
userOptions[name] = value;
@@ -526,7 +576,13 @@ class AppOptions {
526576
const defaultOption = defaultOptions[name],
527577
userOption = options[name];
528578

529-
if (!defaultOption || typeof userOption !== typeof defaultOption.value) {
579+
if (
580+
!defaultOption ||
581+
!(
582+
typeof userOption === typeof defaultOption.value ||
583+
defaultOption.kind & OptionKind.UNDEF_ALLOWED
584+
)
585+
) {
530586
continue;
531587
}
532588
if (prefs) {
@@ -554,9 +610,8 @@ class AppOptions {
554610

555611
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
556612
// 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;
613+
if (name in compatibilityParams) {
614+
userOptions[name] = compatibilityParams[name];
560615
}
561616
}
562617
}

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)