Skip to content

Commit

Permalink
Update AddNewLine to insert post-scaling glyph height on empty line
Browse files Browse the repository at this point in the history
This one required a bit more work as CSS usually takes the "glyph scale"
of the font specified in the `font-family` as far as my manual testing went.

I think it should be fine to create a `GetFont` method returning
`IGlyphStore`s for that purpose, as it's also required in visual testing
to visualise the baseline position in a sprite text.
  • Loading branch information
frenzibyte committed Apr 23, 2022
1 parent 4748535 commit f59391e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
20 changes: 20 additions & 0 deletions osu.Framework/IO/Stores/FontStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,26 @@ public override void RemoveStore(IResourceStore<TextureUpload> store)
base.RemoveStore(store);
}

/// <summary>
/// Searches for a <see cref="IGlyphStore"/> with the specified name.
/// </summary>
/// <param name="name">The font name.</param>
public IGlyphStore GetFont(string name)
{
var found = glyphStores.Find(g => g.FontName == name);

if (found == null)
{
foreach (var store in nestedFontStores)
{
if ((found = store.GetFont(name)) != null)
break;
}
}

return found;
}

public new Texture Get(string name)
{
var found = base.Get(name, WrapMode.None, WrapMode.None);
Expand Down
7 changes: 7 additions & 0 deletions osu.Framework/Text/ITexturedGlyphLookupStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System.Threading.Tasks;
using osu.Framework.IO.Stores;

namespace osu.Framework.Text
{
Expand All @@ -22,5 +23,11 @@ public interface ITexturedGlyphLookupStore
/// <param name="character">The character to retrieve.</param>
/// <returns>The character glyph.</returns>
Task<ITexturedCharacterGlyph> GetAsync(string fontName, char character);

/// <summary>
/// Searches for a <see cref="IGlyphStore"/> with the specified name.
/// </summary>
/// <param name="name">The font name.</param>
IGlyphStore GetFont(string name);
}
}
16 changes: 15 additions & 1 deletion osu.Framework/Text/TextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using osu.Framework.Caching;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
Expand Down Expand Up @@ -36,6 +37,12 @@ public class TextBuilder : IHasLineBaseHeight
private readonly Vector2 spacing;
private readonly float maxWidth;

/// <summary>
/// The <see cref="IGlyphStore"/> of the font specified in the <see cref="FontUsage"/>.
/// </summary>
[CanBeNull]
private readonly IGlyphStore fontGlyphStore;

private Vector2 currentPos;
private float currentLineHeight;
private float? currentLineBase;
Expand Down Expand Up @@ -81,6 +88,8 @@ public TextBuilder(ITexturedGlyphLookupStore store, FontUsage font, float maxWid
this.spacing = spacing;
this.maxWidth = maxWidth;

fontGlyphStore = store.GetFont(font.FontName);

Characters = characterList ?? new List<TextBuilderGlyph>();
this.neverFixedWidthCharacters = neverFixedWidthCharacters ?? Array.Empty<char>();
this.fallbackCharacter = fallbackCharacter;
Expand Down Expand Up @@ -196,13 +205,18 @@ public bool AddCharacter(char character)
/// Adds a new line to this <see cref="TextBuilder"/>.
/// </summary>
/// <remarks>
/// A height equal to that of the font size will be assumed if the current line is empty, regardless of <see cref="useFontSizeAsHeight"/>.
/// A height equal to that of the font size will be assumed if the current line is empty, regardless of <see cref="useFullGlyphHeight"/>.
/// </remarks>
public void AddNewLine()
{
if (currentNewLine)
{
currentLineHeight = font.Size;

if (font.CssScaling && fontGlyphStore?.Metrics is FontMetrics metrics)
currentLineHeight *= metrics.GlyphScale;
}

// Reset + vertically offset the current position
currentPos.X = startOffset.X;
currentPos.Y += currentLineHeight + spacing.Y;
Expand Down

0 comments on commit f59391e

Please sign in to comment.