-
-
Notifications
You must be signed in to change notification settings - Fork 72
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 character bounds returned by TryMeasureCharacterBounds #398
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -777,7 +777,7 @@ | |
} | ||
} | ||
|
||
public static TheoryData<char, FontRectangle> OpenSans_Data | ||
Check warning on line 780 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, macos-latest, net6.0, 6.0.x, -x64, false)
Check warning on line 780 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)
|
||
= new() | ||
{ | ||
{ '!', new(0, 0, 2, 8) }, | ||
|
@@ -1062,11 +1062,112 @@ | |
Assert.Equal(graphemeCount - 1, lastBound.GraphemeIndex); | ||
} | ||
|
||
[Fact] | ||
public void DoesMeasureCharacterBoundsExtendForAdvanceMultipliers() | ||
{ | ||
FontFamily family = new FontCollection().Add(TestFonts.OpenSansFile); | ||
family.TryGetMetrics(FontStyle.Regular, out FontMetrics metrics); | ||
|
||
TextOptions options = new(family.CreateFont(metrics.UnitsPerEm)) | ||
{ | ||
TabWidth = 8 | ||
}; | ||
|
||
const string text = "H\tH"; | ||
|
||
IReadOnlyList<FontRectangle> glyphsToRender = CaptureGlyphBoundBuilder.GenerateGlyphsBoxes(text, options); | ||
TextMeasurer.TryMeasureCharacterBounds(text, options, out ReadOnlySpan<GlyphBounds> bounds); | ||
|
||
IReadOnlyList<GlyphLayout> glyphLayouts = TextLayout.GenerateLayout(text, options); | ||
|
||
Assert.Equal(glyphsToRender.Count, bounds.Length); | ||
Assert.Equal(glyphsToRender.Count, glyphsToRender.Count); | ||
|
||
for (int glyphIndex = 0; glyphIndex < glyphsToRender.Count; glyphIndex++) | ||
{ | ||
FontRectangle renderGlyph = glyphsToRender[glyphIndex]; | ||
FontRectangle measureGlyph = bounds[glyphIndex].Bounds; | ||
GlyphLayout glyphLayout = glyphLayouts[glyphIndex]; | ||
|
||
if (glyphLayout.IsWhiteSpace()) | ||
{ | ||
Assert.Equal(renderGlyph.X, measureGlyph.X); | ||
Assert.Equal(renderGlyph.Y, measureGlyph.Y); | ||
Assert.Equal(glyphLayout.AdvanceX * options.Dpi, measureGlyph.Width); | ||
Assert.Equal(renderGlyph.Height, measureGlyph.Height); | ||
} | ||
else | ||
{ | ||
Assert.Equal(renderGlyph, measureGlyph); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public void IsMeasureCharacterBoundsSameAsRenderBounds() | ||
{ | ||
FontFamily family = new FontCollection().Add(TestFonts.OpenSansFile); | ||
family.TryGetMetrics(FontStyle.Regular, out FontMetrics metrics); | ||
|
||
TextOptions options = new(family.CreateFont(metrics.UnitsPerEm)) | ||
{ | ||
}; | ||
|
||
const string text = "Hello WorLLd"; | ||
|
||
IReadOnlyList<FontRectangle> glyphsToRender = CaptureGlyphBoundBuilder.GenerateGlyphsBoxes(text, options); | ||
TextMeasurer.TryMeasureCharacterBounds(text, options, out ReadOnlySpan<GlyphBounds> bounds); | ||
|
||
Assert.Equal(glyphsToRender.Count, bounds.Length); | ||
|
||
for (int glyphIndex = 0; glyphIndex < glyphsToRender.Count; glyphIndex++) | ||
{ | ||
FontRectangle renderGlyph = glyphsToRender[glyphIndex]; | ||
FontRectangle measureGlyph = bounds[glyphIndex].Bounds; | ||
|
||
Assert.Equal(renderGlyph.X, measureGlyph.X); | ||
Assert.Equal(renderGlyph.Y, measureGlyph.Y); | ||
Assert.Equal(renderGlyph.Width, measureGlyph.Width); | ||
Assert.Equal(renderGlyph.Height, measureGlyph.Height); | ||
|
||
Assert.Equal(renderGlyph, measureGlyph); | ||
} | ||
} | ||
|
||
private class CaptureGlyphBoundBuilder : IGlyphRenderer | ||
{ | ||
public static List<FontRectangle> GenerateGlyphsBoxes(string text, TextOptions options) | ||
{ | ||
CaptureGlyphBoundBuilder glyphBuilder = new(); | ||
TextRenderer renderer = new(glyphBuilder); | ||
renderer.RenderText(text, options); | ||
return glyphBuilder.GlyphBounds; | ||
} | ||
public readonly List<FontRectangle> GlyphBounds = new(); | ||
Check warning on line 1146 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, macos-latest, net6.0, 6.0.x, -x64, false)
Check warning on line 1146 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)
|
||
public CaptureGlyphBoundBuilder() { } | ||
Check warning on line 1147 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, macos-latest, net6.0, 6.0.x, -x64, false)
Check warning on line 1147 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)
|
||
bool IGlyphRenderer.BeginGlyph(in FontRectangle bounds, in GlyphRendererParameters parameters) | ||
Check warning on line 1148 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, macos-latest, net6.0, 6.0.x, -x64, false)
Check warning on line 1148 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)
|
||
{ | ||
this.GlyphBounds.Add(bounds); | ||
return true; | ||
} | ||
public void BeginFigure() { } | ||
Check warning on line 1153 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, macos-latest, net6.0, 6.0.x, -x64, false)
Check warning on line 1153 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)
|
||
public void MoveTo(Vector2 point) { } | ||
Check warning on line 1154 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, macos-latest, net6.0, 6.0.x, -x64, false)
Check warning on line 1154 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)
|
||
public void QuadraticBezierTo(Vector2 secondControlPoint, Vector2 point) { } | ||
public void CubicBezierTo(Vector2 secondControlPoint, Vector2 thirdControlPoint, Vector2 point) { } | ||
public void LineTo(Vector2 point) { } | ||
public void EndFigure() { } | ||
public void EndGlyph() { } | ||
public void EndText() { } | ||
void IGlyphRenderer.BeginText(in FontRectangle bounds) { } | ||
public TextDecorations EnabledDecorations() => TextDecorations.None; | ||
public void SetDecoration(TextDecorations textDecorations, Vector2 start, Vector2 end, float thickness) { } | ||
} | ||
|
||
private static readonly Font OpenSansTTF = new FontCollection().Add(TestFonts.OpenSansFile).CreateFont(10); | ||
private static readonly Font OpenSansWoff = new FontCollection().Add(TestFonts.OpenSansFile).CreateFont(10); | ||
|
||
#if OS_WINDOWS | ||
public static TheoryData<char, FontRectangle> SegoeUi_Data | ||
Check warning on line 1170 in tests/SixLabors.Fonts.Tests/TextLayoutTests.cs GitHub Actions / Build (false, windows-latest, net6.0, 6.0.x, -x64, true)
|
||
= new() | ||
{ | ||
{ '!', new(0, 0, 2, 8) }, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I'd actually updated this. Obviously not!