Skip to content

Commit

Permalink
Disable system fonts on Android (issue 18210)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Snuffleupagus committed Jul 21, 2024
1 parent e92a6a1 commit f0f486b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 19 deletions.
10 changes: 0 additions & 10 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1004,16 +1004,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({
Expand Down
67 changes: 61 additions & 6 deletions web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {

// Limit canvas size to 5 mega-pixels on mobile.
// Support: Android, iOS
(function checkCanvasSizeLimitation() {
(function () {
if (isIOS || isAndroid) {
compatibilityParams.maxCanvasPixels = 5242880;
}
})();

// Don't use system fonts on Android (issue 18210).
// Support: Android
(function () {
if (isAndroid) {
compatibilityParams.useSystemFonts = false;
}
})();
}

const OptionKind = {
Expand All @@ -47,6 +55,7 @@ const OptionKind = {
API: 0x04,
WORKER: 0x08,
EVENT_DISPATCH: 0x10,
UNDEF_ALLOWED: 0x20,
PREFERENCE: 0x80,
};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 have \`undefined\` value for "PREFERENCE" kind: ${name}`
);
}
if (
typeof compatibilityParams === "object" &&
compatibilityParams[name] !== undefined
Expand All @@ -480,6 +507,23 @@ 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 have \`undefined\` value for "BROWSER" kind: ${name}`
);
}
if (
typeof compatibilityParams === "object" &&
compatibilityParams[name] !== undefined
) {
throw new Error(
`Should not have compatibility-value for "BROWSER" kind: ${name}`
);
}
if (value === undefined) {
throw new Error(`Invalid value for "BROWSER" kind: ${name}`);
}
}
}
}
Expand Down Expand Up @@ -513,7 +557,13 @@ class AppOptions {
static set(name, value) {
const defaultOption = defaultOptions[name];

if (!defaultOption || typeof value !== typeof defaultOption.value) {
if (
!defaultOption ||
!(
typeof value === typeof defaultOption.value ||
defaultOption.kind & OptionKind.UNDEF_ALLOWED
)
) {
return;
}
userOptions[name] = value;
Expand All @@ -526,7 +576,13 @@ class AppOptions {
const defaultOption = defaultOptions[name],
userOption = options[name];

if (!defaultOption || typeof userOption !== typeof defaultOption.value) {
if (
!defaultOption ||
!(
typeof userOption === typeof defaultOption.value ||
defaultOption.kind & OptionKind.UNDEF_ALLOWED
)
) {
continue;
}
if (prefs) {
Expand Down Expand Up @@ -554,9 +610,8 @@ class AppOptions {

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
// Re-apply a compatibility-value, if it exists, to the user-options.
const val = compatibilityParams[name];
if (val !== undefined) {
userOptions[name] = val;
if (name in compatibilityParams) {
userOptions[name] = compatibilityParams[name];
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions web/viewer-geckoview.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<!--#endif-->

<!--#if !MOZCENTRAL-->
<script>
if (typeof PDFJSDev === "undefined") {
window.isGECKOVIEW = true;
}
</script>

<script type="importmap">
{
"imports": {
Expand Down
3 changes: 0 additions & 3 deletions web/viewer-geckoview.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ function getViewerConfiguration() {
function webViewerLoad() {
const config = getViewerConfiguration();

if (typeof PDFJSDev === "undefined") {
window.isGECKOVIEW = true;
}
PDFViewerApplication.run(config);
}

Expand Down

0 comments on commit f0f486b

Please sign in to comment.