Truly independent web browser
LibWeb: Paint text decoration lines on whole device pixels
sideshowbarkerf7cbd5357f90
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/text_decoration.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/text_decoration.rsindex 18ba94c58fe..c005c56b760 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/text_decoration.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/text_decoration.rs@@ -17,7 +17,7 @@ use crate::painting::display_list::recorder::{PaintStyleOrColor, StrokePathParamuse crate::painting::force_dark::ForceDarkRole;use crate::painting::record::PaintRecorder;use libgfx_rust::path::Path;-use libgfx_rust::{CapStyle, Color, FloatPoint, IntPoint, JoinStyle, LineStyle, ShouldAntiAlias};+use libgfx_rust::{CapStyle, Color, FloatPoint, IntPoint, IntRect, JoinStyle, LineStyle, ShouldAntiAlias};fn decoration_color(style: crate::css::computed_value_views::ComputedValuesView<'_>) -> u32 {let text = style.inherited_text();@@ -311,8 +311,8 @@ fn compute_skip_ink_segments<O: Observer>(fragment_index: u32,span_start_x: i32,span_end_x: i32,- line_y: i32,- line_thickness: i32,+ band_top: i32,+ band_thickness: i32,font_size: f32,) -> Vec<DecorationSegment> {let side = recorder.source.committed_side_data(block);@@ -330,10 +330,9 @@ fn compute_skip_ink_segments<O: Observer>(let blob_origin_x = fragment_absolute_rect.x.to_float() * scale as f32;let blob_origin_y = (fragment_absolute_rect.y.to_float() + fragment.baseline.to_float()) * scale as f32;- // Convert the underline's y-band from device pixels to blob-local coordinates.- let half_thickness = line_thickness as f32 / 2.0;- let y_top = line_y as f32 - half_thickness - blob_origin_y;- let y_bottom = line_y as f32 + half_thickness - blob_origin_y;+ // Convert the band of pixel rows the line paints from device pixels to blob-local coordinates.+ let y_top = band_top as f32 - blob_origin_y;+ let y_bottom = (band_top + band_thickness) as f32 - blob_origin_y;let intervals = run.glyph_intercepts(scale as f32, y_top, y_bottom);if intervals.is_empty() {@@ -459,13 +458,17 @@ fn build_triangle_wave_path(from: IntPoint, to: IntPoint, amplitude: f32) -> Patpub(crate) enum DecorationStrokeGeometry {Single(LineStyle),- Double { first_y: i32, second_y: i32 },+ Double { first_top: i32, second_top: i32 },Wavy { center_y: i32, amplitude: i32 },}pub(crate) struct DecorationLineGeometry {pub start: IntPoint,pub end: IntPoint,+ // NB: The line's edges go on device pixel boundaries, so that a line one device pixel thick covers one row of+ // pixels rather than half of each of two rows: Its top edge is rounded to a device pixel, and a solid line+ // is filled as a rectangle from there, while a dotted or dashed one is stroked along the true center.+ pub top: i32,pub thickness: i32,pub color: Color,pub should_skip_ink: bool,@@ -598,6 +601,12 @@ pub(crate) fn decoration_line_geometry(center_y: line_center_y,} = css_line;let device_line_thickness = converter.rounded_device_pixels(line_thickness);+ let top = converter+ .rounded_device_point(fragment_box.location().translated(+ CssPixels::from_raw(0),+ line_center_y - line_thickness.div_as_fraction(CssPixels::from_integer(2)),+ ))+ .y;let line_start_point = converter.rounded_device_point(fragment_box.location()@@ -625,18 +634,13 @@ pub(crate) fn decoration_line_geometry(text_decoration_style::DOTTED => DecorationStrokeGeometry::Single(LineStyle::Dotted),text_decoration_style::DOUBLE => {// Two parallel lines with a 1px gap, expanding away from the text.- let step = device_line_thickness + 1;- let mut first_y = line_y;- let mut second_y = line_y;- match line {- text_decoration_line::UNDERLINE => second_y += step,- text_decoration_line::OVERLINE => second_y -= step,- _ => {- first_y -= step / 2;- second_y = first_y + step;- }- }- DecorationStrokeGeometry::Double { first_y, second_y }+ let step = device_line_thickness.max(1) + 1;+ let (first_top, second_top) = match line {+ text_decoration_line::UNDERLINE => (top, top + step),+ text_decoration_line::OVERLINE => (top - step, top),+ _ => (top - step / 2, top - step / 2 + step),+ };+ DecorationStrokeGeometry::Double { first_top, second_top }}_ => {// Wavy: the wave oscillates amplitude/2 above and below its center.@@ -656,6 +660,7 @@ pub(crate) fn decoration_line_geometry(lines.push(DecorationLineGeometry {start: line_start_point,end: line_end_point,+ top,thickness: device_line_thickness,color: line_color,should_skip_ink,@@ -676,6 +681,8 @@ pub(crate) fn paint_decoration_lines<O: Observer>(let line_start_point = geometry.start;let line_end_point = geometry.end;let device_line_thickness = geometry.thickness;+ let top_y = geometry.top;+ let thickness = device_line_thickness.max(1);let line_color = geometry.color;let should_skip_ink = geometry.should_skip_ink;let segments = if !should_skip_ink {@@ -690,36 +697,81 @@ pub(crate) fn paint_decoration_lines<O: Observer>(fragment_index,line_start_point.x,line_end_point.x,- line_start_point.y,- device_line_thickness,+ top_y,+ thickness,set.text_font_pixel_size,)};- let line_y = line_start_point.y;- let draw_line_for_segment =- |recorder: &mut PaintRecorder<'_, O>, segment: DecorationSegment, y: i32, style: LineStyle| {- recorder.recorder.draw_line(- IntPoint { x: segment.start_x, y },- IntPoint { x: segment.end_x, y },- line_color,- device_line_thickness,- style,- Color::TRANSPARENT,- ForceDarkRole::Foreground,- );- };+ let fill_line = |recorder: &mut PaintRecorder<'_, O>, segment: DecorationSegment, top: i32| {+ recorder.recorder.fill_rect(+ IntRect {+ x: segment.start_x,+ y: top,+ width: segment.end_x - segment.start_x,+ height: thickness,+ },+ line_color,+ ForceDarkRole::Foreground,+ );+ };+ let stroke_line = |recorder: &mut PaintRecorder<'_, O>,+ segment: DecorationSegment,+ dash_array: Vec<f32>,+ cap_style: CapStyle| {+ let center_y = top_y as f32 + thickness as f32 / 2.0;+ let mut path = Path::new();+ path.move_to(FloatPoint {+ x: segment.start_x as f32,+ y: center_y,+ });+ path.line_to(FloatPoint {+ x: segment.end_x as f32,+ y: center_y,+ });+ recorder.recorder.stroke_path(StrokePathParams {+ force_dark_role: ForceDarkRole::Foreground,+ cap_style,+ join_style: JoinStyle::Miter,+ miter_limit: 4.0,+ dash_array,+ dash_offset: 0.0,+ path: &path,+ opacity: 1.0,+ paint_style_or_color: PaintStyleOrColor::Color(line_color),+ thickness: thickness as f32,+ should_anti_alias: ShouldAntiAlias::Yes,+ });+ };match geometry.stroke {- DecorationStrokeGeometry::Single(style) => {+ DecorationStrokeGeometry::Single(LineStyle::Dashed) => {+ let dash = (thickness * 2) as f32;+ for segment in &segments {+ stroke_line(recorder, *segment, vec![dash, dash], CapStyle::Butt);+ }+ }+ DecorationStrokeGeometry::Single(LineStyle::Dotted) => {+ // Dots one line thickness across, one thickness apart. A dot narrower than 3 device pixels is square,+ // since antialiasing would wash out a round one that small.+ let size = thickness as f32;+ for segment in &segments {+ if thickness < 3 {+ stroke_line(recorder, *segment, vec![size, size], CapStyle::Butt);+ } else {+ stroke_line(recorder, *segment, vec![0.0, size * 2.0], CapStyle::Round);+ }+ }+ }+ DecorationStrokeGeometry::Single(_) => {for segment in &segments {- draw_line_for_segment(recorder, *segment, line_y, style);+ fill_line(recorder, *segment, top_y);}}- DecorationStrokeGeometry::Double { first_y, second_y } => {+ DecorationStrokeGeometry::Double { first_top, second_top } => {for segment in &segments {- draw_line_for_segment(recorder, *segment, first_y, LineStyle::Solid);- draw_line_for_segment(recorder, *segment, second_y, LineStyle::Solid);+ fill_line(recorder, *segment, first_top);+ fill_line(recorder, *segment, second_top);}}DecorationStrokeGeometry::Wavy {diff --git a/Tests/LibWeb/Screenshot/expected/text-decorations.png b/Tests/LibWeb/Screenshot/expected/text-decorations.pngindex 71403fc4603..af615de02e3 100644Binary files a/Tests/LibWeb/Screenshot/expected/text-decorations.png and b/Tests/LibWeb/Screenshot/expected/text-decorations.png differdiff --git a/Tests/LibWeb/Text/expected/css/text-underline-position.txt b/Tests/LibWeb/Text/expected/css/text-underline-position.txtindex 64f5ce77d10..19ccfac3b87 100644--- a/Tests/LibWeb/Text/expected/css/text-underline-position.txt+++ b/Tests/LibWeb/Text/expected/css/text-underline-position.txt@@ -7,8 +7,8 @@ AccumulatedVisualContext Tree:DisplayList:DrawGlyphRun@s1/e0 rect=[8,8 28x16] translation=[8,21] color=rgb(0, 0, 0)-DrawLine@s1/e0 from=[8,24] to=[35,24] color=rgb(0, 0, 0) thickness=2 style=Solid+FillRect@s1/e0 rect=[8,23 27x2] color=rgb(0, 0, 0)DrawGlyphRun@s1/e0 rect=[35,8 9x16] translation=[35.15625,21] color=rgb(0, 0, 0)DrawGlyphRun@s1/e0 rect=[43,8 28x16] translation=[43.15625,21] color=rgb(0, 0, 0)-DrawLine@s1/e0 from=[43,27] to=[71,27] color=rgb(0, 0, 0) thickness=2 style=Solid+FillRect@s1/e0 rect=[43,26 28x2] color=rgb(0, 0, 0)diff --git a/Tests/LibWeb/Text/expected/display_list/button-with-text-decoration.txt b/Tests/LibWeb/Text/expected/display_list/button-with-text-decoration.txtindex 3f6873aa7e0..e493d95cb1b 100644--- a/Tests/LibWeb/Text/expected/display_list/button-with-text-decoration.txt+++ b/Tests/LibWeb/Text/expected/display_list/button-with-text-decoration.txt@@ -9,5 +9,5 @@ DisplayList:FillRectWithRoundedCorners@s1/e0 rect=[8,8 98x20] color=rgb(239, 239, 239)FillRoundedRectRing@s1/e0 rect=[8,8 98x20] corner_radii=[4x4,4x4,4x4,4x4] widths=[1,1,1,1] color=rgb(118, 118, 118)DrawGlyphRun@s1/e0 rect=[17,11 80x14] translation=[17,22] color=rgb(0, 0, 0)-DrawLine@s1/e0 from=[17,25] to=[97,25] color=rgb(0, 0, 0) thickness=1 style=Solid+FillRect@s1/e0 rect=[17,24 80x1] color=rgb(0, 0, 0)diff --git a/Tests/LibWeb/Text/expected/display_list/multiline-inline-paint-invalidation.txt b/Tests/LibWeb/Text/expected/display_list/multiline-inline-paint-invalidation.txtindex cb4a1afb318..1b35a32b644 100644--- a/Tests/LibWeb/Text/expected/display_list/multiline-inline-paint-invalidation.txt+++ b/Tests/LibWeb/Text/expected/display_list/multiline-inline-paint-invalidation.txt@@ -8,13 +8,13 @@ AccumulatedVisualContext Tree:DisplayList:DrawGlyphRun@s1/e0 rect=[8,8 70x16] translation=[8,21] color=rgb(0, 0, 255)-DrawLine@s1/e0 from=[8,24] to=[78,24] color=rgb(0, 0, 255) thickness=2 style=Solid+FillRect@s1/e0 rect=[8,23 70x2] color=rgb(0, 0, 255)DrawGlyphRun@s1/e0 rect=[8,24 48x16] translation=[8,37] color=rgb(0, 0, 255)-DrawLine@s1/e0 from=[8,40] to=[55,40] color=rgb(0, 0, 255) thickness=2 style=Solid+FillRect@s1/e0 rect=[8,39 47x2] color=rgb(0, 0, 255)DrawGlyphRun@s1/e0 rect=[8,40 60x16] translation=[8,53] color=rgb(0, 0, 255)-DrawLine@s1/e0 from=[8,56] to=[68,56] color=rgb(0, 0, 255) thickness=2 style=Solid+FillRect@s1/e0 rect=[8,55 60x2] color=rgb(0, 0, 255)DrawGlyphRun@s1/e0 rect=[8,56 37x16] translation=[8,69] color=rgb(0, 0, 255)-DrawLine@s1/e0 from=[8,72] to=[44,72] color=rgb(0, 0, 255) thickness=2 style=Solid+FillRect@s1/e0 rect=[8,71 36x2] color=rgb(0, 0, 255)After:AccumulatedVisualContext Tree:@@ -26,11 +26,11 @@ AccumulatedVisualContext Tree:DisplayList:DrawGlyphRun@s1/e0 rect=[8,8 70x16] translation=[8,21] color=rgb(255, 0, 0)-DrawLine@s1/e0 from=[8,24] to=[78,24] color=rgb(255, 0, 0) thickness=2 style=Solid+FillRect@s1/e0 rect=[8,23 70x2] color=rgb(255, 0, 0)DrawGlyphRun@s1/e0 rect=[8,24 48x16] translation=[8,37] color=rgb(255, 0, 0)-DrawLine@s1/e0 from=[8,40] to=[55,40] color=rgb(255, 0, 0) thickness=2 style=Solid+FillRect@s1/e0 rect=[8,39 47x2] color=rgb(255, 0, 0)DrawGlyphRun@s1/e0 rect=[8,40 60x16] translation=[8,53] color=rgb(255, 0, 0)-DrawLine@s1/e0 from=[8,56] to=[68,56] color=rgb(255, 0, 0) thickness=2 style=Solid+FillRect@s1/e0 rect=[8,55 60x2] color=rgb(255, 0, 0)DrawGlyphRun@s1/e0 rect=[8,56 37x16] translation=[8,69] color=rgb(255, 0, 0)-DrawLine@s1/e0 from=[8,72] to=[44,72] color=rgb(255, 0, 0) thickness=2 style=Solid+FillRect@s1/e0 rect=[8,71 36x2] color=rgb(255, 0, 0)diff --git a/Tests/LibWeb/Text/expected/display_list/selection-highlight-painting-order.txt b/Tests/LibWeb/Text/expected/display_list/selection-highlight-painting-order.txtindex d8f7d920c41..a52bb3b8755 100644--- a/Tests/LibWeb/Text/expected/display_list/selection-highlight-painting-order.txt+++ b/Tests/LibWeb/Text/expected/display_list/selection-highlight-painting-order.txt@@ -10,12 +10,12 @@ PaintTextShadow@s1/e0 shadows_rect=[2,22 56x20] rect=[0,20 56x20] translation=[0PaintTextShadow@s1/e0 shadows_rect=[2,22 56x20] rect=[0,20 56x20] translation=[0,36] orientation=Horizontal layers=[offset=[2,2] blur_radius=0 color=rgb(4, 5, 6)]PaintTextShadow@s1/e0 shadows_rect=[2,22 56x20] rect=[0,20 56x20] translation=[0,36] orientation=Horizontal layers=[offset=[2,2] blur_radius=0 color=rgb(4, 5, 6)]DrawGlyphRun@s1/e0 rect=[0,20 56x20] translation=[0,36] color=rgb(1, 2, 3) inline_clips=[clip=[0,20 12x18]]-DrawLine@s1/e0 from=[0,39] to=[12,39] color=rgb(7, 8, 9) thickness=2 style=Solid+FillRect@s1/e0 rect=[0,38 12x2] color=rgb(7, 8, 9)DrawGlyphRun@s1/e0 rect=[0,20 56x20] translation=[0,36] color=rgb(1, 2, 3) inline_clips=[clip=[44,20 11x18]]-DrawLine@s1/e0 from=[44,39] to=[55,39] color=rgb(7, 8, 9) thickness=2 style=Solid+FillRect@s1/e0 rect=[44,38 11x2] color=rgb(7, 8, 9)FillRect@s1/e0 rect=[12,20 32x20] color=rgb(13, 14, 15)PaintTextShadow@s1/e0 shadows_rect=[4,24 56x20] rect=[0,20 56x20] translation=[0,36] orientation=Horizontal layers=[offset=[4,4] blur_radius=0 color=rgb(16, 17, 18)]DrawGlyphRun@s1/e0 rect=[0,20 56x20] translation=[0,36] color=rgb(10, 11, 12) inline_clips=[clip=[12,20 32x18]]-DrawLine@s1/e0 from=[12,39] to=[44,39] color=rgb(10, 11, 12) thickness=2 style=Solid-DrawLine@s1/e0 from=[12,29] to=[44,29] color=rgb(19, 20, 21) thickness=2 style=Solid+FillRect@s1/e0 rect=[12,38 32x2] color=rgb(10, 11, 12)+FillRect@s1/e0 rect=[12,28 32x2] color=rgb(19, 20, 21)diff --git a/Tests/LibWeb/Text/expected/display_list/text-decoration-thin-lines-cover-whole-pixels.txt b/Tests/LibWeb/Text/expected/display_list/text-decoration-thin-lines-cover-whole-pixels.txtnew file mode 100644index 00000000000..48bf53707c2--- /dev/null+++ b/Tests/LibWeb/Text/expected/display_list/text-decoration-thin-lines-cover-whole-pixels.txt@@ -0,0 +1,16 @@+AccumulatedVisualContext Tree:+ spatial:+ [s0] transform=[1,0,0,1,0,0] origin=(0,0) (Viewport<#document>)+ [s1] scroll (Viewport<#document>)+ effects:+ [e0 in s0] effects=[]++DisplayList:+DrawGlyphRun@s1/e0 rect=[8,14 45x14] translation=[8,25] color=rgb(0, 0, 0)+FillRect@s1/e0 rect=[8,27 44x1] color=rgb(0, 0, 0)+FillRect@s1/e0 rect=[8,29 44x1] color=rgb(0, 0, 0)+DrawGlyphRun@s1/e0 rect=[8,42 43x14] translation=[8,53] color=rgb(0, 0, 0)+StrokePath@s1/e0 path_bounding_rect=[7.5,55 43x1] thickness=1 color=rgb(0, 0, 0) dash_array_size=2 dash_offset=0.00+DrawGlyphRun@s1/e0 rect=[8,70 46x14] translation=[8,81] color=rgb(0, 0, 0)+StrokePath@s1/e0 path_bounding_rect=[7.5,83 47x1] thickness=1 color=rgb(0, 0, 0) dash_array_size=2 dash_offset=0.00+diff --git a/Tests/LibWeb/Text/expected/display_list/text-decoration-underline-partial-selection.txt b/Tests/LibWeb/Text/expected/display_list/text-decoration-underline-partial-selection.txtindex 69ac0baba60..47067e103fe 100644--- a/Tests/LibWeb/Text/expected/display_list/text-decoration-underline-partial-selection.txt+++ b/Tests/LibWeb/Text/expected/display_list/text-decoration-underline-partial-selection.txt@@ -7,12 +7,12 @@ AccumulatedVisualContext Tree:DisplayList:DrawGlyphRun@s1/e0 rect=[8,-17 169x100] translation=[8,63] color=rgb(0, 0, 0) inline_clips=[clip=[9,-15 60x91]]-DrawLine@s1/e0 from=[8,70] to=[45,70] color=rgb(0, 0, 0) thickness=10 style=Solid-DrawLine@s1/e0 from=[57,70] to=[67,70] color=rgb(0, 0, 0) thickness=10 style=Solid+FillRect@s1/e0 rect=[8,65 37x10] color=rgb(0, 0, 0)+FillRect@s1/e0 rect=[57,65 10x10] color=rgb(0, 0, 0)DrawGlyphRun@s1/e0 rect=[8,-17 169x100] translation=[8,63] color=rgb(0, 0, 0) inline_clips=[clip=[116,-15 61x91]]-DrawLine@s1/e0 from=[116,70] to=[153,70] color=rgb(0, 0, 0) thickness=10 style=Solid-DrawLine@s1/e0 from=[165,70] to=[177,70] color=rgb(0, 0, 0) thickness=10 style=Solid+FillRect@s1/e0 rect=[116,65 37x10] color=rgb(0, 0, 0)+FillRect@s1/e0 rect=[165,65 12x10] color=rgb(0, 0, 0)FillRect@s1/e0 rect=[69,-17 47x100] color=rgba(101, 2, 0, 0.8)DrawGlyphRun@s1/e0 rect=[8,-17 169x100] translation=[8,63] color=rgb(0, 0, 0) inline_clips=[clip=[69,-15 47x91]]-DrawLine@s1/e0 from=[109,70] to=[116,70] color=rgb(0, 0, 0) thickness=10 style=Solid+FillRect@s1/e0 rect=[109,65 7x10] color=rgb(0, 0, 0)diff --git a/Tests/LibWeb/Text/input/display_list/text-decoration-thin-lines-cover-whole-pixels.html b/Tests/LibWeb/Text/input/display_list/text-decoration-thin-lines-cover-whole-pixels.htmlnew file mode 100644index 00000000000..2551738904e--- /dev/null+++ b/Tests/LibWeb/Text/input/display_list/text-decoration-thin-lines-cover-whole-pixels.html@@ -0,0 +1,18 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ body {+ font: 14px SerenitySans;+ }+ span {+ text-decoration-thickness: 1px;+ }+</style>+<p><span style="text-decoration: underline double">double</span></p>+<p><span style="text-decoration: underline dotted">dotted</span></p>+<p><span style="text-decoration: underline dashed">dashed</span></p>+<script>+ test(() => {+ println(internals.dumpDisplayList());+ });+</script>