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
This commit is contained in:
Emilio Cobos Álvarez 2024-09-10 18:14:40 +00:00
Родитель 923571bfe3
Коммит e02418a02c
4 изменённых файлов: 51 добавлений и 7 удалений

Просмотреть файл

@ -2602,14 +2602,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();

Просмотреть файл

@ -5053,6 +5053,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,

Просмотреть файл

@ -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>

Просмотреть файл

@ -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>