Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Avalonia.Controls/Presenters/TextPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ protected override Size MeasureOverride(Size availableSize)
Typeface = new Typeface(FontFamily, FontSize, FontStyle, FontWeight),
TextAlignment = TextAlignment,
Constraint = availableSize,
}.Measure();
}.Measure().Size;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Controls/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ protected override Size MeasureOverride(Size availableSize)
FormattedText.Constraint = Size.Infinity;
}

return FormattedText.Measure();
return FormattedText.Measure().Size;
}

return new Size();
Expand Down
9 changes: 3 additions & 6 deletions src/Avalonia.Visuals/Media/FormattedText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,10 @@ public IEnumerable<Rect> HitTestTextRange(int index, int length)
}

/// <summary>
/// Gets the size of the text, taking <see cref="Constraint"/> into account.
/// Gets the bounds of the text, taking <see cref="Constraint"/> into account.
/// </summary>
/// <returns>The bounds box of the text.</returns>
public Size Measure()
{
return PlatformImpl.Size;
}
/// <returns>The bounds of the text.</returns>
public Rect Measure() => PlatformImpl.Bounds;

private void Set<T>(ref T field, T value)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Avalonia.Visuals/Platform/IFormattedTextImpl.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System;
using System.Collections.Generic;
using Avalonia.Media;

Expand All @@ -17,9 +18,9 @@ public interface IFormattedTextImpl
Size Constraint { get; }

/// <summary>
/// The measured size of the text.
/// The measured bounds of the text.
/// </summary>
Size Size { get; }
Rect Bounds{ get; }

/// <summary>
/// Gets the text.
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Visuals/Rendering/SceneGraph/TextNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public TextNode(
Point origin,
IFormattedTextImpl text,
IDictionary<IVisual, Scene> childScenes = null)
: base(new Rect(origin, text.Size), transform, null)
: base(text.Bounds, transform, null)
{
Transform = transform;
Foreground = foreground?.ToImmutable();
Expand Down
2 changes: 1 addition & 1 deletion src/Skia/Avalonia.Skia/DrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void FillRectangle(IBrush brush, Rect rect, float cornerRadius = 0)
/// <inheritdoc />
public void DrawText(IBrush foreground, Point origin, IFormattedTextImpl text)
{
using (var paint = CreatePaint(foreground, text.Size))
using (var paint = CreatePaint(foreground, text.Bounds.Size))
{
var textImpl = (FormattedTextImpl) text;
textImpl.Draw(this, Canvas, origin.ToSKPoint(), paint, _canTextUseLcdRendering);
Expand Down
26 changes: 20 additions & 6 deletions src/Skia/Avalonia.Skia/FormattedTextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public FormattedTextImpl(

public Size Constraint => _constraint;

public Size Size => _size;
public Rect Bounds => _bounds;

public IEnumerable<FormattedTextLine> GetLines()
{
Expand Down Expand Up @@ -135,7 +135,7 @@ public TextHitTestResult HitTestPoint(Point point)
};
}

bool end = point.X > _size.Width || point.Y > _lines.Sum(l => l.Height);
bool end = point.X > _bounds.Width || point.Y > _lines.Sum(l => l.Height);

return new TextHitTestResult()
{
Expand Down Expand Up @@ -323,7 +323,7 @@ internal void Draw(DrawingContextImpl context,
private Size _constraint = new Size(double.PositiveInfinity, double.PositiveInfinity);
private float _lineHeight = 0;
private float _lineOffset = 0;
private Size _size;
private Rect _bounds;
private List<AvaloniaFormattedTextLine> _skiaLines;

private static void ApplyWrapperTo(ref SKPaint current, DrawingContextImpl.PaintWrapper wrapper,
Expand Down Expand Up @@ -639,12 +639,26 @@ private void Rebuild()
if (_skiaLines.Count == 0)
{
_lines.Add(new FormattedTextLine(0, _lineHeight));
_size = new Size(0, _lineHeight);
_bounds = new Rect(0, 0, 0, _lineHeight);
}
else
{
var lastLine = _skiaLines[_skiaLines.Count - 1];
_size = new Size(maxX, lastLine.Top + lastLine.Height);
_bounds = new Rect(0, 0, maxX, lastLine.Top + lastLine.Height);

switch (_paint.TextAlign)
{
case SKTextAlign.Center:
_bounds = new Rect(Constraint).CenterRect(_bounds);
break;
case SKTextAlign.Right:
_bounds = new Rect(
Constraint.Width - _bounds.Width,
0,
_bounds.Width,
_bounds.Height);
break;
}
}
}

Expand All @@ -660,7 +674,7 @@ private float TransformX(float originX, float lineWidth, SKTextAlign align)
{
double width = Constraint.Width > 0 && !double.IsPositiveInfinity(Constraint.Width) ?
Constraint.Width :
_size.Width;
_bounds.Width;

switch (align)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Windows/Avalonia.Direct2D1/Media/DrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void DrawText(IBrush foreground, Point origin, IFormattedTextImpl text)
{
var impl = (FormattedTextImpl)text;

using (var brush = CreateBrush(foreground, impl.Size))
using (var brush = CreateBrush(foreground, impl.Bounds.Size))
using (var renderer = new AvaloniaTextRenderer(this, _deviceContext, brush.PlatformBrush))
{
if (brush.PlatformBrush != null)
Expand Down
12 changes: 8 additions & 4 deletions src/Windows/Avalonia.Direct2D1/Media/FormattedTextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public FormattedTextImpl(
}
}

Size = Measure();
Bounds = Measure();
}

public Size Constraint => new Size(TextLayout.MaxWidth, TextLayout.MaxHeight);

public Size Size { get; }
public Rect Bounds { get; }

public string Text { get; }

Expand Down Expand Up @@ -104,7 +104,7 @@ private void ApplySpan(FormattedTextStyleSpan span)
}
}

private Size Measure()
private Rect Measure()
{
var metrics = TextLayout.Metrics;

Expand All @@ -115,7 +115,11 @@ private Size Measure()
width = metrics.Width;
}

return new Size(width, TextLayout.Metrics.Height);
return new Rect(
TextLayout.Metrics.Left,
TextLayout.Metrics.Top,
width,
TextLayout.Metrics.Height);
}
}
}
2 changes: 1 addition & 1 deletion tests/Avalonia.Layout.UnitTests/FullLayoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public FormattedTextMock(string text)

public string Text { get; }

public Size Size => new Size();
public Rect Bounds => Rect.Empty;

public void Dispose()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Avalonia.RenderTests/Media/FormattedTextImplTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private IFormattedTextImpl Create(string text, double fontSize, TextWrapping wra
public void Should_Measure_String_Correctly(string input, double fontSize, double expWidth, double expHeight)
{
var fmt = Create(input, fontSize);
var size = fmt.Size;
var size = fmt.Bounds.Size;

Assert.Equal(expWidth, size.Width, 2);
Assert.Equal(expHeight, size.Height, 2);
Expand Down Expand Up @@ -265,4 +265,4 @@ public void Should_HitTestRange_Correctly(string input,
}
}
}
}
}