From 74bbfd8509c34c82d7f73f9bd1e85f9e1801414a Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Wed, 11 Jan 2023 15:35:07 +0100 Subject: [PATCH] Support intercepts for all location but TextDecorationLocation.Strikethrough --- src/Avalonia.Base/Media/TextDecoration.cs | 19 +++++++++---------- tests/Avalonia.Benchmarks/NullGlyphRun.cs | 8 +++++++- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Avalonia.Base/Media/TextDecoration.cs b/src/Avalonia.Base/Media/TextDecoration.cs index 0ed73e7786..d6b2841214 100644 --- a/src/Avalonia.Base/Media/TextDecoration.cs +++ b/src/Avalonia.Base/Media/TextDecoration.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using Avalonia.Collections; using Avalonia.Collections.Pooled; using Avalonia.Media.TextFormatting; @@ -200,26 +201,24 @@ namespace Avalonia.Media break; } - var strokeOffset = 0.0; - switch (StrokeOffsetUnit) { case TextDecorationUnit.FontRenderingEmSize: - strokeOffset = StrokeOffset * textMetrics.FontRenderingEmSize; + origin += new Point(0, StrokeOffset * textMetrics.FontRenderingEmSize); break; case TextDecorationUnit.Pixel: - strokeOffset = StrokeOffset; + origin += new Point(0, StrokeOffset); break; } - origin += new Point(0, strokeOffset); - var pen = new Pen(Stroke ?? defaultBrush, thickness, new DashStyle(StrokeDashArray, StrokeDashOffset), StrokeLineCap); - if (Location == TextDecorationLocation.Underline && MathUtilities.IsZero(strokeOffset)) + if (Location != TextDecorationLocation.Strikethrough) { - var intersections = glyphRun.GlyphRunImpl.GetIntersections((float)(thickness * 0.5d + strokeOffset), (float)(thickness * 1.5d + strokeOffset)); + var offsetY = glyphRun.BaselineOrigin.Y - origin.Y; + + var intersections = glyphRun.GlyphRunImpl.GetIntersections((float)(thickness * 0.5d - offsetY), (float)(thickness * 1.5d - offsetY)); if (intersections != null && intersections.Count > 0) { @@ -257,7 +256,7 @@ namespace Avalonia.Media } return; - } + } } drawingContext.DrawLine(pen, origin, origin + new Point(glyphRun.Metrics.Width, 0)); diff --git a/tests/Avalonia.Benchmarks/NullGlyphRun.cs b/tests/Avalonia.Benchmarks/NullGlyphRun.cs index 5ba0822649..1c2c7c0d7d 100644 --- a/tests/Avalonia.Benchmarks/NullGlyphRun.cs +++ b/tests/Avalonia.Benchmarks/NullGlyphRun.cs @@ -1,4 +1,5 @@ -using Avalonia.Platform; +using System.Collections.Generic; +using Avalonia.Platform; namespace Avalonia.Benchmarks { @@ -7,5 +8,10 @@ namespace Avalonia.Benchmarks public void Dispose() { } + + public IReadOnlyList GetIntersections(float lowerBound, float upperBound) + { + return null; + } } }