-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
Fix character bounds returned by TryMeasureCharacterBounds
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1062,6 +1062,107 @@ public void DoesMeasureCharacterLayoutIncludeStringIndex() | |
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, net7.0, 7.0.x, true, -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)
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, net7.0, 7.0.x, true, -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)
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, net7.0, 7.0.x, true, -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)
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, net7.0, 7.0.x, true, -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)
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, net7.0, 7.0.x, true, -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)
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); | ||
|
||
|