Limit the Math.sumPrecise polyfill to non-MOZCENTRAL builds

After https://bugzilla.mozilla.org/show_bug.cgi?id=1985121 this functionality is now guaranteed to be available in Firefox.
Unfortunately general browser support is still somewhat lacking; see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sumPrecise#browser_compatibility

Also, while unrelated, use the `MathClamp` helper in the `applyOpacity` function.
This commit is contained in:
Jonas Jenwald 2026-02-01 18:20:29 +01:00
parent d25f13d1fd
commit 76dabeddb3
2 changed files with 7 additions and 4 deletions

View File

@ -799,7 +799,7 @@ class CSSConstants {
}
function applyOpacity(r, g, b, opacity) {
opacity = Math.min(Math.max(opacity ?? 1, 0), 1);
opacity = MathClamp(opacity ?? 1, 0, 1);
const white = 255 * (1 - opacity);
r = Math.round(r * opacity + white);
g = Math.round(g * opacity + white);

View File

@ -1235,9 +1235,12 @@ function MathClamp(v, min, max) {
return Math.min(Math.max(v, min), max);
}
// TODO: Remove this once the `javascript.options.experimental.math_sumprecise`
// preference is removed from Firefox.
if (typeof Math.sumPrecise !== "function") {
// TODO: Remove this once `Math.sumPrecise` is generally available.
if (
(typeof PDFJSDev === "undefined" ||
PDFJSDev.test("SKIP_BABEL && !MOZCENTRAL")) &&
typeof Math.sumPrecise !== "function"
) {
// Note that this isn't a "proper" polyfill, but since we're only using it to
// replace `Array.prototype.reduce()` invocations it should be fine.
Math.sumPrecise = function (numbers) {