morgan pushed to branch base-browser-128.5.0esr-14.0-1 at The Tor Project / Applications / Tor Browser
Commits: 665e7cdd by Emilio Cobos Álvarez at 2024-12-09T16:54:02+00:00 Bug 1909625 - Ignore CSS zoom and text zoom for canvas. r=gfx-reviewers,lsalzman
The spec doesn't mention anything about applying them, and other browsers don't, so let's just be consistent...
Differential Revision: https://phabricator.services.mozilla.com/D221709 - - - - - d7a304c0 by Emilio Cobos Álvarez at 2024-12-09T16:54:02+00:00 Bug 1918454 - Prevent divide by zero when inverting effective zoom. r=firefox-style-system-reviewers,layout-reviewers,boris
See comment.
Differential Revision: https://phabricator.services.mozilla.com/D222090 - - - - -
9 changed files:
- dom/canvas/CanvasRenderingContext2D.cpp - servo/components/style/properties/cascade.rs - servo/components/style/properties/properties.mako.rs - servo/components/style/values/computed/box.rs - servo/components/style/values/specified/length.rs - servo/ports/geckolib/glue.rs - + testing/web-platform/tests/css/css-viewport/zoom/canvas-ref.html - + testing/web-platform/tests/css/css-viewport/zoom/canvas.html - + testing/web-platform/tests/css/css-viewport/zoom/textarea-very-small-zoom-crash.html
Changes:
===================================== dom/canvas/CanvasRenderingContext2D.cpp ===================================== @@ -6,6 +6,7 @@ #include "CanvasRenderingContext2D.h"
#include "mozilla/gfx/Helpers.h" +#include "nsCSSValue.h" #include "nsXULElement.h"
#include "nsMathUtils.h" @@ -2585,14 +2586,8 @@ static already_AddRefed<StyleLockedDeclarationBlock> CreateDeclarationForServo( return nullptr; }
- // From canvas spec, force to set line-height property to 'normal' font - // property. if (aProperty == eCSSProperty_font) { - const nsCString normalString = "normal"_ns; - Servo_DeclarationBlock_SetPropertyById( - servoDeclarations, eCSSProperty_line_height, &normalString, false, - env.mUrlExtraData, StyleParsingMode::DEFAULT, env.mCompatMode, - env.mLoader, env.mRuleType, {}); + Servo_DeclarationBlock_SanitizeForCanvas(servoDeclarations); }
return servoDeclarations.forget(); @@ -2657,12 +2652,9 @@ static already_AddRefed<const ComputedStyle> GetFontStyleForServo( // The font-size component must be converted to CSS px for reserialization, // so we update the declarations with the value from the computed style. if (!sc->StyleFont()->mFont.family.is_system_font) { - nsAutoCString computedFontSize; - sc->GetComputedPropertyValue(eCSSProperty_font_size, computedFontSize); - Servo_DeclarationBlock_SetPropertyById( - declarations, eCSSProperty_font_size, &computedFontSize, false, nullptr, - StyleParsingMode::DEFAULT, eCompatibility_FullStandards, nullptr, - StyleCssRuleType::Style, {}); + float px = sc->StyleFont()->mFont.size.ToCSSPixels(); + Servo_DeclarationBlock_SetLengthValue(declarations, eCSSProperty_font_size, + px, eCSSUnit_Pixel); }
// The font getter is required to be reserialized based on what we
===================================== servo/components/style/properties/cascade.rs ===================================== @@ -1247,7 +1247,7 @@ impl<'b> Cascade<'b> { ); debug_assert!( !text_scale.text_zoom_enabled(), - "We only ever disable text zoom (in svg:text), never enable it" + "We only ever disable text zoom never enable it" ); let device = builder.device; builder.mutate_font().unzoom_fonts(device); @@ -1257,9 +1257,8 @@ impl<'b> Cascade<'b> { debug_assert!(self.seen.contains(LonghandId::Zoom)); // NOTE(emilio): Intentionally not using the effective zoom here, since all the inherited // zooms are already applied. - let zoom = builder.get_box().clone_zoom(); let old_size = builder.get_font().clone_font_size(); - let new_size = old_size.zoom(zoom); + let new_size = old_size.zoom(builder.resolved_specified_zoom()); if old_size == new_size { return; }
===================================== servo/components/style/properties/properties.mako.rs ===================================== @@ -2711,6 +2711,19 @@ impl<'a> StyleBuilder<'a> { self.get_box().clone_zoom() }
+ /// The zoom we need to apply for this element, without including ancestor effective zooms. + pub fn resolved_specified_zoom(&self) -> computed::Zoom { + let zoom = self.specified_zoom(); + if zoom.is_document() { + // If our inherited effective zoom has derived to zero, there's not much we can do. + // This value is not exposed to content anyways (it's used for scrollbars and to avoid + // zoom affecting canvas). + self.inherited_effective_zoom().inverted().unwrap_or(computed::Zoom::ONE) + } else { + zoom + } + } + /// Inherited zoom. pub fn inherited_effective_zoom(&self) -> computed::Zoom { self.inherited_style.effective_zoom @@ -2747,7 +2760,7 @@ impl<'a> StyleBuilder<'a> { let lh = device.calc_line_height(&font, writing_mode, None); if line_height_base == LineHeightBase::InheritedStyle { // Apply our own zoom if our style source is the parent style. - computed::NonNegativeLength::new(self.get_box().clone_zoom().zoom(lh.px())) + computed::NonNegativeLength::new(self.resolved_specified_zoom().zoom(lh.px())) } else { lh }
===================================== servo/components/style/values/computed/box.rs ===================================== @@ -357,6 +357,21 @@ impl Zoom { self == Self::ONE }
+ /// Returns whether we're the `document` keyword. + #[inline] + pub fn is_document(self) -> bool { + self == Self::DOCUMENT + } + + /// Returns the inverse of our value. + #[inline] + pub fn inverted(&self) -> Option<Self> { + if self.0.value == 0 { + return None; + } + Some(Self(Self::ONE.0 / self.0)) + } + /// Returns the value as a float. #[inline] pub fn value(&self) -> f32 {
===================================== servo/components/style/values/specified/length.rs ===================================== @@ -103,7 +103,7 @@ impl FontBaseSize { Self::InheritedStyle => { // If we're using the size from our inherited style, we still need to apply our // own zoom. - let zoom = style.get_box().clone_zoom(); + let zoom = style.resolved_specified_zoom(); style.get_parent_font().clone_font_size().zoom(zoom) }, }
===================================== servo/ports/geckolib/glue.rs ===================================== @@ -4972,6 +4972,21 @@ fn set_property( ) }
+#[no_mangle] +pub unsafe extern "C" fn Servo_DeclarationBlock_SanitizeForCanvas( + declarations: &LockedDeclarationBlock, +) { + use style::properties::PropertyDeclaration; + use style::values::specified::{LineHeight, XTextScale, Zoom}; + // From canvas spec, force to set line-height property to 'normal' font property. + // Also, for compat, disable text scaling and CSS zoom. + write_locked_arc(declarations, |decls: &mut PropertyDeclarationBlock| { + decls.push(PropertyDeclaration::LineHeight(LineHeight::Normal), Importance::Normal); + decls.push(PropertyDeclaration::Zoom(Zoom::Document), Importance::Normal); + decls.push(PropertyDeclaration::XTextScale(XTextScale::None), Importance::Normal); + }); +} + #[no_mangle] pub unsafe extern "C" fn Servo_DeclarationBlock_SetProperty( declarations: &LockedDeclarationBlock,
===================================== testing/web-platform/tests/css/css-viewport/zoom/canvas-ref.html ===================================== @@ -0,0 +1,15 @@ +<!DOCTYPE html> +<style> +canvas { + width: 600px; + height: 200px; +} +</style> +<script> +document.addEventListener("DOMContentLoaded", () => { + const ctx = document.getElementById("canvas").getContext("2d"); + ctx.font = "48px serif"; + ctx.fillText(ctx.font, 10, 50); +}); +</script> +<canvas id="canvas" width="300" height="100"></canvas>
===================================== testing/web-platform/tests/css/css-viewport/zoom/canvas.html ===================================== @@ -0,0 +1,20 @@ +<!DOCTYPE html> +<link rel="help" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1909625"> +<link rel="help" href="https://html.spec.whatwg.org/#dom-context-2d-font"> +<link rel="author" href="mailto:emilio@crisal.io" title="Emilio Cobos Álvarez"> +<link rel="author" href="https://mozilla.org" title="Mozilla"> +<link rel="match" href="canvas-ref.html"> +<title>zoom is ignored for canvas</title> +<style> +canvas { + zoom: 2; +} +</style> +<script> +document.addEventListener("DOMContentLoaded", () => { + const ctx = document.getElementById("canvas").getContext("2d"); + ctx.font = "48px serif"; + ctx.fillText(ctx.font, 10, 50); +}); +</script> +<canvas id="canvas" width="300" height="100"></canvas>
===================================== testing/web-platform/tests/css/css-viewport/zoom/textarea-very-small-zoom-crash.html ===================================== @@ -0,0 +1,6 @@ +<style> +*:nth-of-type(1) { + zoom: 5%; +} +</style> +<textarea>
View it on GitLab: https://gitlab.torproject.org/tpo/applications/tor-browser/-/compare/f230a2e...
tor-commits@lists.torproject.org