Truly independent web browser
LibWeb: Measure canvas text against the em square
sideshowbarkere748fce15660
diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rsindex 109d3461872..4e8158c40ed 100644--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/text.rs@@ -107,17 +107,35 @@ pub(crate) fn path_of_runs(runs: &[ShapedRun]) -> Path {Path::from_glyph_runs(&glyph_runs)}+/// How far the top of the font's em square is above the alphabetic baseline, and its bottom below it. The em square is+/// as tall as the font size, and it's divided at the baseline in the same proportion as the font's ascent and descent.+pub(crate) fn em_square_ascent_and_descent(font: &FontHandle) -> (f32, f32) {+ let facts = font.facts();+ let font_height = facts.ascent + facts.descent;+ let ascent = if font_height > 0.0 {+ facts.pixel_size * facts.ascent / font_height+ } else {+ facts.pixel_size / 2.0+ };+ (ascent, facts.pixel_size - ascent)+}+/// The distance from the alphabetic baseline down to the baseline that textBaseline names, in the metrics of the/// first font of the cascade, https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-textbaselinepub(crate) fn baseline_offset(text_baseline: CanvasTextBaseline, first_font: &FontHandle) -> f32 {let ascent = first_font.facts().ascent;let descent = first_font.facts().descent;+ let (em_ascent, em_descent) = em_square_ascent_and_descent(first_font);match text_baseline {- CanvasTextBaseline::Top => ascent,+ // "The top of the em square"+ CanvasTextBaseline::Top => em_ascent,CanvasTextBaseline::Hanging => ascent * 0.8,- CanvasTextBaseline::Middle => (ascent - descent) / 2.0,+ // "The middle of the em square"+ CanvasTextBaseline::Middle => (em_ascent - em_descent) / 2.0,CanvasTextBaseline::Alphabetic => 0.0,- CanvasTextBaseline::Ideographic | CanvasTextBaseline::Bottom => -descent,+ CanvasTextBaseline::Ideographic => -descent,+ // "The bottom of the em square"+ CanvasTextBaseline::Bottom => -em_descent,}}diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rsindex 070b6121dca..54ae499ca4f 100644--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_text.rs@@ -22,7 +22,8 @@ use crate::canvas_2d::context::{CanvasKind, ContextState, RenderingContext2D, pause crate::canvas_2d::float;use crate::canvas_2d::host::LentFont;use crate::canvas_2d::text::{- baseline_offset, horizontal_alignment_offset, path_of_runs, replace_ascii_whitespace, shape_text, text_transform,+ baseline_offset, em_square_ascent_and_descent, horizontal_alignment_offset, path_of_runs, replace_ascii_whitespace,+ shape_text, text_transform,};use crate::html::canvas_rendering_context2d::CanvasTextBaseline;@@ -223,6 +224,7 @@ impl<K: CanvasKind> RenderingContext2D<K> {let hanging_baseline = ascent * 0.8;let text_baseline: CanvasTextBaseline = self.drawing_state().text_baseline;let baseline_offset = baseline_offset(text_baseline, &first_font);+ let (em_ascent, em_descent) = em_square_ascent_and_descent(&first_font);// NB: The bounding rectangle of the given text is that of the ink of its glyphs, which is relative to the// alphabetic baseline at the start of the inline box. Text without ink has that of the inline box.@@ -270,12 +272,12 @@ impl<K: CanvasKind> RenderingContext2D<K> {// the given baseline is below the top of that em square (so this value will usually be positive). Zero if// the given baseline is the top of that em square; half the font size if the given baseline is the middle// of that em square.- em_height_ascent: f64::from(ascent - baseline_offset),+ em_height_ascent: f64::from(em_ascent - baseline_offset),// emHeightDescent attribute: The distance from the horizontal line indicated by the textBaseline attribute// to the lowest bottom of the em squares in the inline box, in CSS pixels; positive numbers indicating// that the given baseline is above the bottom of that em square. (Zero if the given baseline is the bottom// of that em square.)- em_height_descent: f64::from(descent + baseline_offset),+ em_height_descent: f64::from(em_descent + baseline_offset),// hangingBaseline attribute: The distance from the horizontal line indicated by the textBaseline attribute// to the hanging baseline of the inline box, in CSS pixels; positive numbers indicating that the given// baseline is below the hanging baseline. (Zero if the given baseline is the hanging baseline.)diff --git a/Tests/LibWeb/Text/expected/canvas/measureText-em-square.txt b/Tests/LibWeb/Text/expected/canvas/measureText-em-square.txtnew file mode 100644index 00000000000..668f91c731c--- /dev/null+++ b/Tests/LibWeb/Text/expected/canvas/measureText-em-square.txt@@ -0,0 +1,9 @@+font ascent and descent span more than 80px: true+em square is 80px tall+em square divided in the font's proportion: true+top: emHeightAscent 0, emHeightDescent 80+top: alphabetic baseline at the expected offset: true+middle: emHeightAscent 40, emHeightDescent 40+middle: alphabetic baseline at the expected offset: true+bottom: emHeightAscent 80, emHeightDescent 0+bottom: alphabetic baseline at the expected offset: truediff --git a/Tests/LibWeb/Text/input/canvas/measureText-em-square.html b/Tests/LibWeb/Text/input/canvas/measureText-em-square.htmlnew file mode 100644index 00000000000..55e0a5541b3--- /dev/null+++ b/Tests/LibWeb/Text/input/canvas/measureText-em-square.html@@ -0,0 +1,30 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // The em square is as tall as the font size, divided at the alphabetic baseline in the proportion of the font's+ // ascent and descent, and "top", "middle", and "bottom" are its top, middle, and bottom. Lato's ascent and descent+ // span more than its font size, so they'd give different results.+ asyncTest(async done => {+ const lato = new FontFace("Lato", "url(../../../Assets/Lato-Bold.ttf)");+ document.fonts.add(await lato.load());++ const context = document.createElement("canvas").getContext("2d");+ context.font = "80px Lato";+ context.textBaseline = "alphabetic";+ const alphabetic = context.measureText("Hello");+ const fontHeight = alphabetic.fontBoundingBoxAscent + alphabetic.fontBoundingBoxDescent;+ println(`font ascent and descent span more than 80px: ${fontHeight > 80}`);+ println(`em square is ${alphabetic.emHeightAscent + alphabetic.emHeightDescent}px tall`);+ const expectedEmAscent = (80 * alphabetic.fontBoundingBoxAscent) / fontHeight;+ println(`em square divided in the font's proportion: ${Math.abs(alphabetic.emHeightAscent - expectedEmAscent) < 0.01}`);++ for (const baseline of ["top", "middle", "bottom"]) {+ context.textBaseline = baseline;+ const metrics = context.measureText("Hello");+ println(`${baseline}: emHeightAscent ${metrics.emHeightAscent}, emHeightDescent ${metrics.emHeightDescent}`);+ const offset = alphabetic.emHeightAscent - metrics.emHeightAscent;+ println(`${baseline}: alphabetic baseline at the expected offset: ${Math.abs(-metrics.alphabeticBaseline - offset) < 0.01}`);+ }+ done();+ });+</script>