Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix embedded runs baseline #192

Merged
merged 3 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions src/AvaloniaEdit/Rendering/SingleCharacterElementGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public override bool IsWhitespace(int visualColumn)
}
}

private sealed class TabTextElement : VisualLineElement
internal sealed class TabTextElement : VisualLineElement
{
internal readonly TextLine Text;

Expand Down Expand Up @@ -175,7 +175,7 @@ public override bool IsWhitespace(int visualColumn)
}
}

private sealed class TabGlyphRun : TextEmbeddedObject
internal sealed class TabGlyphRun : TextEmbeddedObject
{
private readonly TabTextElement _element;

Expand All @@ -195,8 +195,9 @@ public TabGlyphRun(TabTextElement element, TextRunProperties properties)

public override Size GetSize(double remainingParagraphWidth)
{
var width = Math.Min(0, _element.Text.WidthIncludingTrailingWhitespace - 1);
return new Size(width, _element.Text.Height);
return new Size(
_element.Text.WidthIncludingTrailingWhitespace,
_element.Text.Height);
}

public override Rect ComputeBoundingBox()
Expand All @@ -206,7 +207,6 @@ public override Rect ComputeBoundingBox()

public override void Draw(DrawingContext drawingContext, Point origin)
{
origin = origin.WithY(origin.Y - _element.Text.Baseline);
_element.Text.Draw(drawingContext, origin);
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/AvaloniaEdit/Text/TextLineRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ public double Baseline
{
return 0.0;
}

double defaultBaseLine = GetDefaultBaseline(TextRun.Properties.FontMetrics);

if (IsEmbedded && TextRun is TextEmbeddedObject embeddedObject)
{
var box = embeddedObject.ComputeBoundingBox();
return box.Y;
return defaultBaseLine - box.Y;
}

return GetDefaultBaseline(TextRun.Properties.FontMetrics);
return defaultBaseLine;
}
}

Expand Down
6 changes: 4 additions & 2 deletions test/AvaloniaEdit.Tests/AvaloniaMocks/MockGlyphTypeface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ public class MockGlyphTypeface : IGlyphTypefaceImpl
{
public const int GlyphAdvance = 8;
public const short DefaultFontSize = 10;
public const int GlyphAscent = 2;
public const int GlyphDescent = 10;

public short DesignEmHeight => DefaultFontSize;
public int Ascent => 2;
public int Descent => 10;
public int Ascent => GlyphAscent;
public int Descent => GlyphDescent;
public int LineGap { get; }
public int UnderlinePosition { get; }
public int UnderlineThickness { get; }
Expand Down
76 changes: 75 additions & 1 deletion test/AvaloniaEdit.Tests/Text/TextLineRunTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia.Media;
using Avalonia;
using Avalonia.Media;
using Avalonia.Platform;

using AvaloniaEdit.AvaloniaMocks;
Expand Down Expand Up @@ -137,6 +138,79 @@ public void TextEmbeddedObject_Line_Run_Should_Have_Fixed_Glyph_Width()
run.GetDistanceFromCharacter(1));
}

[Test]
public void TextEmbeddedObject_Should_Have_Valid_Baseline()
{
using var app = UnitTestApplication.Start(new TestServices().With(
renderInterface: new MockPlatformRenderInterface(),
fontManagerImpl: new MockFontManagerImpl(),
formattedTextImpl: Mock.Of<IFormattedTextImpl>()));

int runWidth = 50;

TextLine textLine = Mock.Of<TextLine>(
t => t.WidthIncludingTrailingWhitespace == runWidth);

SpecialCharacterTextRun f = new SpecialCharacterTextRun(
new FormattedTextElement("BEL", 1) { TextLine = textLine },
CreateDefaultTextProperties());

Mock<TextSource> ts = new Mock<TextSource>();
ts.Setup(s => s.GetTextRun(It.IsAny<int>())).Returns(f);

TextLineRun run = TextLineRun.Create(ts.Object, 0, 0, 1, CreateDefaultParagraphProperties());

Assert.AreEqual(MockGlyphTypeface.GlyphAscent, run.Baseline);
}

[Test]
public void Simple_Run_Should_Have_Valid_Baseline()
{
using var app = UnitTestApplication.Start(new TestServices().With(
renderInterface: new MockPlatformRenderInterface(),
fontManagerImpl: new MockFontManagerImpl(),
formattedTextImpl: Mock.Of<IFormattedTextImpl>()));

SimpleTextSource s = new SimpleTextSource(
"a\ta",
CreateDefaultTextProperties());

var paragraphProperties = CreateDefaultParagraphProperties();

TextLineRun run = TextLineRun.Create(s, 0, 0, 1, paragraphProperties);

Assert.AreEqual(MockGlyphTypeface.GlyphAscent, run.Baseline);
}

[Test]
public void Tab_Glyph_Run_Shuld_Have_Valid_Bounds()
{
using var app = UnitTestApplication.Start(new TestServices().With(
renderInterface: new MockPlatformRenderInterface(),
fontManagerImpl: new MockFontManagerImpl(),
formattedTextImpl: Mock.Of<IFormattedTextImpl>()));

double runWidth = 50;
double runHeight = 20;

Mock<TextLine> textLineMock = new Mock<TextLine>();
textLineMock.Setup(
t => t.WidthIncludingTrailingWhitespace).Returns(runWidth);
textLineMock.Setup(
t => t.Height).Returns(runHeight);

TabTextElement tabTextElement = new TabTextElement(textLineMock.Object);

TabGlyphRun tabRun = new TabGlyphRun(
tabTextElement,
CreateDefaultTextProperties());

Size runSize = tabRun.GetSize(double.PositiveInfinity);

Assert.AreEqual(runWidth, runSize.Width, "Wrong run width");
Assert.AreEqual(runHeight, runSize.Height, "Wrong run height");
}

TextRunProperties CreateDefaultTextProperties()
{
return new TextRunProperties()
Expand Down