From 147db08f90b9f3625e7d024cc60f07945df937fe Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Thu, 2 Mar 2023 12:34:34 +0100 Subject: [PATCH] Introduce GlyphRun.Bounds --- src/Avalonia.Base/Media/GlyphRun.cs | 14 +++++++------- src/Avalonia.Base/Media/GlyphRunDrawing.cs | 2 +- src/Avalonia.Base/Media/TextDecoration.cs | 2 +- .../Media/TextFormatting/ShapedTextRun.cs | 4 ++-- .../Media/TextFormatting/TextEllipsisHelper.cs | 2 +- .../TextLeadingPrefixCharacterEllipsis.cs | 2 +- .../Media/TextFormatting/TextLineImpl.cs | 6 +++--- src/Avalonia.Base/Platform/IGlyphRunImpl.cs | 3 +-- .../Composition/Server/DiagnosticTextRenderer.cs | 8 ++++---- .../Rendering/SceneGraph/GlyphRunNode.cs | 5 ++--- .../HeadlessPlatformRenderInterface.cs | 4 ++-- src/Skia/Avalonia.Skia/DrawingContextImpl.cs | 2 +- src/Skia/Avalonia.Skia/GlyphRunImpl.cs | 4 ++-- .../Avalonia.Direct2D1/Media/GlyphRunImpl.cs | 4 ++-- tests/Avalonia.Benchmarks/NullGlyphRun.cs | 2 +- tests/Avalonia.UnitTests/MockGlyphRun.cs | 4 ++-- 16 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/Avalonia.Base/Media/GlyphRun.cs b/src/Avalonia.Base/Media/GlyphRun.cs index 2966ceee8d..d795cca894 100644 --- a/src/Avalonia.Base/Media/GlyphRun.cs +++ b/src/Avalonia.Base/Media/GlyphRun.cs @@ -151,9 +151,9 @@ namespace Avalonia.Media } /// - /// Gets or sets the conservative bounding box of the . + /// Gets the conservative bounding box of the . /// - public Size Size => PlatformImpl.Item.Size; + public Rect Bounds => PlatformImpl.Item.Bounds; /// /// @@ -252,7 +252,7 @@ namespace Avalonia.Media if (characterIndex > Metrics.LastCluster) { - return Size.Width; + return Bounds.Width; } var glyphIndex = FindGlyphIndex(characterIndex); @@ -287,7 +287,7 @@ namespace Avalonia.Media if (characterIndex <= Metrics.FirstCluster) { - return Size.Width; + return Bounds.Width; } for (var i = glyphIndex + 1; i < _glyphInfos.Count; i++) @@ -295,7 +295,7 @@ namespace Avalonia.Media distance += _glyphInfos[i].GlyphAdvance; } - return Size.Width - distance; + return Bounds.Width - distance; } } @@ -321,7 +321,7 @@ namespace Avalonia.Media } //After - if (distance >= Size.Width) + if (distance >= Bounds.Width) { isInside = false; @@ -354,7 +354,7 @@ namespace Avalonia.Media } else { - currentX = Size.Width; + currentX = Bounds.Width; for (var index = _glyphInfos.Count - 1; index >= 0; index--) { diff --git a/src/Avalonia.Base/Media/GlyphRunDrawing.cs b/src/Avalonia.Base/Media/GlyphRunDrawing.cs index 06d92fd81c..961203e30e 100644 --- a/src/Avalonia.Base/Media/GlyphRunDrawing.cs +++ b/src/Avalonia.Base/Media/GlyphRunDrawing.cs @@ -32,7 +32,7 @@ public override Rect GetBounds() { - return GlyphRun != null ? new Rect(GlyphRun.Size) : default; + return GlyphRun != null ? GlyphRun.Bounds : default; } } } diff --git a/src/Avalonia.Base/Media/TextDecoration.cs b/src/Avalonia.Base/Media/TextDecoration.cs index b74b7df9c5..e89a7d8826 100644 --- a/src/Avalonia.Base/Media/TextDecoration.cs +++ b/src/Avalonia.Base/Media/TextDecoration.cs @@ -223,7 +223,7 @@ namespace Avalonia.Media if (intersections.Count > 0) { var last = baselineOrigin.X; - var finalPos = last + glyphRun.Size.Width; + var finalPos = last + glyphRun.Bounds.Width; var end = last; var points = new List(); diff --git a/src/Avalonia.Base/Media/TextFormatting/ShapedTextRun.cs b/src/Avalonia.Base/Media/TextFormatting/ShapedTextRun.cs index c5dd30b620..2f28c3f954 100644 --- a/src/Avalonia.Base/Media/TextFormatting/ShapedTextRun.cs +++ b/src/Avalonia.Base/Media/TextFormatting/ShapedTextRun.cs @@ -38,14 +38,14 @@ namespace Avalonia.Media.TextFormatting public override double Baseline => -TextMetrics.Ascent; - public override Size Size => GlyphRun.Size; + public override Size Size => GlyphRun.Bounds.Size; public GlyphRun GlyphRun => _glyphRun ??= CreateGlyphRun(); /// public override void Draw(DrawingContext drawingContext, Point origin) { - using (drawingContext.PushPreTransform(Matrix.CreateTranslation(origin))) + using (drawingContext.PushTransform(Matrix.CreateTranslation(origin))) { if (GlyphRun.GlyphInfos.Count == 0) { diff --git a/src/Avalonia.Base/Media/TextFormatting/TextEllipsisHelper.cs b/src/Avalonia.Base/Media/TextFormatting/TextEllipsisHelper.cs index 6422f23dcd..8b6d576c6e 100644 --- a/src/Avalonia.Base/Media/TextFormatting/TextEllipsisHelper.cs +++ b/src/Avalonia.Base/Media/TextFormatting/TextEllipsisHelper.cs @@ -19,7 +19,7 @@ namespace Avalonia.Media.TextFormatting var collapsedLength = 0; var shapedSymbol = TextFormatterImpl.CreateSymbol(properties.Symbol, FlowDirection.LeftToRight); - if (properties.Width < shapedSymbol.GlyphRun.Size.Width) + if (properties.Width < shapedSymbol.GlyphRun.Bounds.Width) { //Not enough space to fit in the symbol return Array.Empty(); diff --git a/src/Avalonia.Base/Media/TextFormatting/TextLeadingPrefixCharacterEllipsis.cs b/src/Avalonia.Base/Media/TextFormatting/TextLeadingPrefixCharacterEllipsis.cs index a21a5d45e9..41d451b9e3 100644 --- a/src/Avalonia.Base/Media/TextFormatting/TextLeadingPrefixCharacterEllipsis.cs +++ b/src/Avalonia.Base/Media/TextFormatting/TextLeadingPrefixCharacterEllipsis.cs @@ -60,7 +60,7 @@ namespace Avalonia.Media.TextFormatting var currentWidth = 0.0; var shapedSymbol = TextFormatterImpl.CreateSymbol(Symbol, FlowDirection.LeftToRight); - if (Width < shapedSymbol.GlyphRun.Size.Width) + if (Width < shapedSymbol.GlyphRun.Bounds.Width) { return Array.Empty(); } diff --git a/src/Avalonia.Base/Media/TextFormatting/TextLineImpl.cs b/src/Avalonia.Base/Media/TextFormatting/TextLineImpl.cs index b3321d4d9f..f426a20b2c 100644 --- a/src/Avalonia.Base/Media/TextFormatting/TextLineImpl.cs +++ b/src/Avalonia.Base/Media/TextFormatting/TextLineImpl.cs @@ -423,7 +423,7 @@ namespace Avalonia.Media.TextFormatting { if (currentGlyphRun != null) { - currentDistance -= currentGlyphRun.Size.Width; + currentDistance -= currentGlyphRun.Bounds.Width; } return currentDistance + distance; @@ -477,7 +477,7 @@ namespace Avalonia.Media.TextFormatting { if (currentGlyphRun.IsLeftToRight || flowDirection == FlowDirection.RightToLeft) { - distance = currentGlyphRun.Size.Width; + distance = currentGlyphRun.Bounds.Width; } return true; @@ -1483,7 +1483,7 @@ namespace Avalonia.Media.TextFormatting trailingWhitespaceLength += glyphRunMetrics.TrailingWhitespaceLength; - var whitespaceWidth = glyphRun.Size.Width - glyphRunMetrics.Width; + var whitespaceWidth = glyphRun.Bounds.Width - glyphRunMetrics.Width; width -= whitespaceWidth; } diff --git a/src/Avalonia.Base/Platform/IGlyphRunImpl.cs b/src/Avalonia.Base/Platform/IGlyphRunImpl.cs index 46b065b04e..fccea27c43 100644 --- a/src/Avalonia.Base/Platform/IGlyphRunImpl.cs +++ b/src/Avalonia.Base/Platform/IGlyphRunImpl.cs @@ -10,11 +10,10 @@ namespace Avalonia.Platform [Unstable] public interface IGlyphRunImpl : IDisposable { - /// /// Gets the conservative bounding box of the glyph run./>. /// - Size Size { get; } + Rect Bounds { get; } /// /// Gets the baseline origin of the glyph run./>. diff --git a/src/Avalonia.Base/Rendering/Composition/Server/DiagnosticTextRenderer.cs b/src/Avalonia.Base/Rendering/Composition/Server/DiagnosticTextRenderer.cs index b01fb46aa3..04e40e8744 100644 --- a/src/Avalonia.Base/Rendering/Composition/Server/DiagnosticTextRenderer.cs +++ b/src/Avalonia.Base/Rendering/Composition/Server/DiagnosticTextRenderer.cs @@ -20,7 +20,7 @@ namespace Avalonia.Rendering.Composition.Server for (var c = FirstChar; c <= LastChar; c++) { - var height = _runs[c - FirstChar].Size.Height; + var height = _runs[c - FirstChar].Bounds.Height; if (height > maxHeight) { maxHeight = height; @@ -51,8 +51,8 @@ namespace Avalonia.Rendering.Composition.Server { var effectiveChar = c is >= FirstChar and <= LastChar ? c : ' '; var run = _runs[effectiveChar - FirstChar]; - width += run.Size.Width; - height = Math.Max(height, run.Size.Height); + width += run.Bounds.Width; + height = Math.Max(height, run.Bounds.Height); } return new Size(width, height); @@ -69,7 +69,7 @@ namespace Avalonia.Rendering.Composition.Server var run = _runs[effectiveChar - FirstChar]; context.Transform = originalTransform * Matrix.CreateTranslation(offset, 0.0); context.DrawGlyphRun(foreground, run.PlatformImpl); - offset += run.Size.Width; + offset += run.Bounds.Width; } context.Transform = originalTransform; diff --git a/src/Avalonia.Base/Rendering/SceneGraph/GlyphRunNode.cs b/src/Avalonia.Base/Rendering/SceneGraph/GlyphRunNode.cs index 4b09bc9280..381c63f430 100644 --- a/src/Avalonia.Base/Rendering/SceneGraph/GlyphRunNode.cs +++ b/src/Avalonia.Base/Rendering/SceneGraph/GlyphRunNode.cs @@ -16,12 +16,11 @@ namespace Avalonia.Rendering.SceneGraph /// The transform. /// The foreground brush. /// The glyph run to draw. - /// Auxiliary data required to draw the brush. public GlyphRunNode( Matrix transform, IImmutableBrush foreground, IRef glyphRun) - : base(new Rect(glyphRun.Item.BaselineOrigin, glyphRun.Item.Size), transform, foreground) + : base(glyphRun.Item.Bounds, transform, foreground) { GlyphRun = glyphRun.Clone(); } @@ -54,7 +53,7 @@ namespace Avalonia.Rendering.SceneGraph } /// - public override bool HitTest(Point p) => new Rect(GlyphRun.Item.Size).ContainsExclusive(p); + public override bool HitTest(Point p) => GlyphRun.Item.Bounds.ContainsExclusive(p); public override void Dispose() { diff --git a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs index 31aaebcdc7..f8100d3832 100644 --- a/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs +++ b/src/Avalonia.Headless/HeadlessPlatformRenderInterface.cs @@ -118,7 +118,7 @@ namespace Avalonia.Headless public IGeometryImpl BuildGlyphRunGeometry(GlyphRun glyphRun) { - return new HeadlessGeometryStub(new Rect(glyphRun.Size)); + return new HeadlessGeometryStub(glyphRun.Bounds); } public IGlyphRunImpl CreateGlyphRun( @@ -132,7 +132,7 @@ namespace Avalonia.Headless class HeadlessGlyphRunStub : IGlyphRunImpl { - public Size Size => new Size(8, 12); + public Rect Bounds => new Rect(new Size(8, 12)); public Point BaselineOrigin => new Point(0, 8); diff --git a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs index af0231579c..e3e2f664c3 100644 --- a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs +++ b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs @@ -516,7 +516,7 @@ namespace Avalonia.Skia return; } - using (var paintWrapper = CreatePaint(_fillPaint, foreground, glyphRun.Item.Size)) + using (var paintWrapper = CreatePaint(_fillPaint, foreground, glyphRun.Item.Bounds.Size)) { var glyphRunImpl = (GlyphRunImpl)glyphRun.Item; diff --git a/src/Skia/Avalonia.Skia/GlyphRunImpl.cs b/src/Skia/Avalonia.Skia/GlyphRunImpl.cs index 079eea7bef..0521e238f3 100644 --- a/src/Skia/Avalonia.Skia/GlyphRunImpl.cs +++ b/src/Skia/Avalonia.Skia/GlyphRunImpl.cs @@ -11,7 +11,7 @@ namespace Avalonia.Skia { TextBlob = textBlob ?? throw new ArgumentNullException(nameof(textBlob)); - Size = size; + Bounds = new Rect(new Point(baselineOrigin.X, 0), size); BaselineOrigin = baselineOrigin; } @@ -21,7 +21,7 @@ namespace Avalonia.Skia /// public SKTextBlob TextBlob { get; } - public Size Size { get; } + public Rect Bounds { get; } public Point BaselineOrigin { get; } diff --git a/src/Windows/Avalonia.Direct2D1/Media/GlyphRunImpl.cs b/src/Windows/Avalonia.Direct2D1/Media/GlyphRunImpl.cs index 446db47d92..2e7a4b67f6 100644 --- a/src/Windows/Avalonia.Direct2D1/Media/GlyphRunImpl.cs +++ b/src/Windows/Avalonia.Direct2D1/Media/GlyphRunImpl.cs @@ -9,12 +9,12 @@ namespace Avalonia.Direct2D1.Media { public GlyphRunImpl(GlyphRun glyphRun, Size size, Point baselineOrigin) { - Size = size; + Bounds = new Rect(new Point(baselineOrigin.X, 0), size); BaselineOrigin = baselineOrigin; GlyphRun = glyphRun; } - public Size Size { get; } + public Rect Bounds{ get; } public Point BaselineOrigin { get; } diff --git a/tests/Avalonia.Benchmarks/NullGlyphRun.cs b/tests/Avalonia.Benchmarks/NullGlyphRun.cs index c4707c78c8..5b584f302d 100644 --- a/tests/Avalonia.Benchmarks/NullGlyphRun.cs +++ b/tests/Avalonia.Benchmarks/NullGlyphRun.cs @@ -5,7 +5,7 @@ namespace Avalonia.Benchmarks { internal class NullGlyphRun : IGlyphRunImpl { - public Size Size => default; + public Rect Bounds => default; public Point BaselineOrigin => default; diff --git a/tests/Avalonia.UnitTests/MockGlyphRun.cs b/tests/Avalonia.UnitTests/MockGlyphRun.cs index 0319803a5e..4561d3b3f2 100644 --- a/tests/Avalonia.UnitTests/MockGlyphRun.cs +++ b/tests/Avalonia.UnitTests/MockGlyphRun.cs @@ -16,10 +16,10 @@ namespace Avalonia.UnitTests width += glyphInfos[i].GlyphAdvance; } - Size = new Size(width, 10); + Bounds = new Rect(new Size(width, 10)); } - public Size Size { get; } + public Rect Bounds { get; } public Point BaselineOrigin => new Point(0, 8);