Skip to content

Commit

Permalink
Merge pull request #201 from andy840119/refactor-karaoke-sprite-text-…
Browse files Browse the repository at this point in the history
…partial-class

Refactor karaoke sprite text partial class
  • Loading branch information
andy840119 authored May 20, 2022
2 parents 1d0b7cc + 6a7b2ac commit 511c996
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ public TestSceneLyricSpriteTextCharacterPosition()
[TestCase(6, TextIndex.IndexState.End, true)]
[TestCase(-1, TextIndex.IndexState.End, false)]
[TestCase(7, TextIndex.IndexState.Start, false)]
public void TestGetTextIndexPosition(int index, TextIndex.IndexState state, bool valid)
public void TestGetTextIndexXPosition(int index, TextIndex.IndexState state, bool valid)
{
prepareTestCase(() =>
{
var position = lyricSpriteText.GetTextIndexPosition(new TextIndex(index, state));
var position = lyricSpriteText.GetTextIndexXPosition(new TextIndex(index, state));
return new RectangleF(position, 20, 1, 64);
}, valid);
}
Expand All @@ -106,9 +106,9 @@ public void TestGetTextIndexPosition(int index, TextIndex.IndexState state, bool
[TestCase(6, true)]
[TestCase(-1, false)]
[TestCase(7, false)]
public void TestGetCharacterRectangle(int index, bool valid)
public void TestGetCharacterDrawRectangle(int index, bool valid)
{
prepareTestCase(() => lyricSpriteText.GetCharacterRectangle(index), valid);
prepareTestCase(() => lyricSpriteText.GetCharacterDrawRectangle(index), valid);
}

[TestCase("[0,1]:か", true)]
Expand All @@ -119,9 +119,9 @@ public void TestGetCharacterRectangle(int index, bool valid)
[TestCase("[-1,1]:か", false)]
[TestCase("[0,2]:か", false)]
[TestCase("[0,1]:?", false)]
public void TestGetRubyTagPosition(string rubyTag, bool valid)
public void TestGetRubyTagDrawRectangle(string rubyTag, bool valid)
{
prepareTestCase(() => lyricSpriteText.GetRubyTagPosition(TestCaseTagHelper.ParsePositionText(rubyTag)), valid);
prepareTestCase(() => lyricSpriteText.GetRubyTagDrawRectangle(TestCaseTagHelper.ParsePositionText(rubyTag)), valid);
}

[TestCase("[0,1]:ka", true)]
Expand All @@ -132,9 +132,9 @@ public void TestGetRubyTagPosition(string rubyTag, bool valid)
[TestCase("[-1,1]:ka", false)]
[TestCase("[0,2]:ka", false)]
[TestCase("[0,1]:?", false)]
public void TestGetRomajiTagPosition(string rubyTag, bool valid)
public void TestGetRomajiTagDrawRectangle(string rubyTag, bool valid)
{
prepareTestCase(() => lyricSpriteText.GetRomajiTagPosition(TestCaseTagHelper.ParsePositionText(rubyTag)), valid);
prepareTestCase(() => lyricSpriteText.GetRomajiTagDrawRectangle(TestCaseTagHelper.ParsePositionText(rubyTag)), valid);
}

private void prepareTestCase(Func<RectangleF> func, bool valid)
Expand Down
12 changes: 6 additions & 6 deletions osu.Framework.Font/Graphics/Sprites/KaraokeSpriteText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void load(ShaderManager shaderManager)
RoundedTextureShader = shaderManager.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED);
}

#region frame buffer
#region Frame buffer

public DrawColourInfo? FrameBufferDrawColour => base.DrawColourInfo;

Expand Down Expand Up @@ -168,7 +168,7 @@ public IReadOnlyList<IShader> RightLyricTextShaders

#endregion

#region text
#region Text

public string Text
{
Expand Down Expand Up @@ -208,7 +208,7 @@ public IReadOnlyList<PositionText> Romajies

#endregion

#region font
#region Font

public FontUsage Font
{
Expand Down Expand Up @@ -248,7 +248,7 @@ public FontUsage RomajiFont

#endregion

#region style
#region Style

public ColourInfo LeftTextColour
{
Expand Down Expand Up @@ -298,7 +298,7 @@ public LyricTextAlignment RomajiAlignment

#endregion

#region text spacing
#region Text spacing

public Vector2 Spacing
{
Expand Down Expand Up @@ -338,7 +338,7 @@ public Vector2 RomajiSpacing

#endregion

#region margin/padding
#region Margin/padding

public int RubyMargin
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ IEnumerable<KeyValuePair<double, TextIndex>> getInterpolatedTimeTagBetweenTwoTim

private float getTextIndexPosition(TextIndex index)
{
var leftTextIndexPosition = leftLyricText.GetTextIndexPosition(index);
var rightTextIndexPosition = rightLyricText.GetTextIndexPosition(index);
var leftTextIndexPosition = leftLyricText.GetTextIndexXPosition(index);
var rightTextIndexPosition = rightLyricText.GetTextIndexXPosition(index);
return index.State == TextIndex.IndexState.Start
? Math.Min(leftTextIndexPosition, rightTextIndexPosition)
: Math.Max(leftTextIndexPosition, rightTextIndexPosition);
Expand Down
103 changes: 7 additions & 96 deletions osu.Framework.Font/Graphics/Sprites/LyricSpriteText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.IO.Stores;
using osu.Framework.Layout;
using osu.Framework.Text;
using osu.Framework.Utils;
using osuTK;
using osuTK.Graphics;
Expand Down Expand Up @@ -60,7 +58,7 @@ private void load(ShaderManager shaderManager)
}
}

#region frame buffer
#region Frame buffer

public DrawColourInfo? FrameBufferDrawColour => new DrawColourInfo(Color4.White);

Expand Down Expand Up @@ -126,7 +124,7 @@ public IReadOnlyList<IShader> Shaders

#endregion

#region text
#region Text

private string text = string.Empty;

Expand Down Expand Up @@ -203,7 +201,7 @@ public IReadOnlyList<PositionText> Romajies

#endregion

#region font
#region Font

private FontUsage font = FontUsage.Default;

Expand Down Expand Up @@ -255,7 +253,7 @@ public FontUsage RomajiFont

#endregion

#region style
#region Style

private bool allowMultiline = true;

Expand Down Expand Up @@ -382,7 +380,7 @@ public string EllipsisString

#endregion

#region size
#region Size

private bool requiresAutoSizedWidth => explicitWidth == null && (RelativeSizeAxes & Axes.X) == 0;

Expand Down Expand Up @@ -479,7 +477,7 @@ public override Vector2 Size

#endregion

#region text spacing
#region Text spacing

private Vector2 spacing;

Expand Down Expand Up @@ -540,7 +538,7 @@ public Vector2 RomajiSpacing

#endregion

#region margin/padding
#region Margin/padding

private MarginPadding padding;

Expand Down Expand Up @@ -661,95 +659,8 @@ private void invalidate(bool characters = false, bool textBuilder = false)

#endregion

/// <summary>
/// The characters that should be excluded from fixed-width application. Defaults to (".", ",", ":", " ") if null.
/// </summary>
protected virtual char[] FixedWidthExcludeCharacters => null;

/// <summary>
/// The character to use to calculate the fixed width width. Defaults to 'm'.
/// </summary>
protected virtual char FixedWidthReferenceCharacter => 'm';

/// <summary>
/// The character to fallback to use if a character glyph lookup failed.
/// </summary>
protected virtual char FallbackCharacter => '?';

private readonly LayoutValue<TextBuilder> textBuilderCache = new LayoutValue<TextBuilder>(Invalidation.DrawSize, InvalidationSource.Parent);

/// <summary>
/// Invalidates the current <see cref="TextBuilder"/>, causing a new one to be created next time it's required via <see cref="CreateTextBuilder"/>.
/// </summary>
protected void InvalidateTextBuilder() => textBuilderCache.Invalidate();

/// <summary>
/// Creates a <see cref="TextBuilder"/> to generate the character layout for this <see cref="LyricSpriteText"/>.
/// </summary>
/// <param name="store">The <see cref="ITexturedGlyphLookupStore"/> where characters should be retrieved from.</param>
/// <returns>The <see cref="TextBuilder"/>.</returns>
protected virtual TextBuilder CreateTextBuilder(ITexturedGlyphLookupStore store)
{
var excludeCharacters = FixedWidthExcludeCharacters ?? default_never_fixed_width_characters;

var rubyHeight = ReserveRubyHeight || Rubies.Any() ? RubyFont.Size : 0;
var romajiHeight = ReserveRomajiHeight || Romajies.Any() ? RomajiFont.Size : 0;
var startOffset = new Vector2(Padding.Left, Padding.Top + rubyHeight);
var spacing = Spacing + new Vector2(0, rubyHeight + romajiHeight);

float builderMaxWidth = requiresAutoSizedWidth
? MaxWidth
: ApplyRelativeAxes(RelativeSizeAxes, new Vector2(Math.Min(MaxWidth, base.Width), base.Height), FillMode).X - Padding.Right;

if (AllowMultiline)
{
return new MultilineTextBuilder(store, Font, builderMaxWidth, UseFullGlyphHeight, startOffset, spacing, charactersBacking,
excludeCharacters, FallbackCharacter, FixedWidthReferenceCharacter);
}

if (Truncate)
{
return new TruncatingTextBuilder(store, Font, builderMaxWidth, ellipsisString, UseFullGlyphHeight, startOffset, spacing, charactersBacking,
excludeCharacters, FallbackCharacter, FixedWidthReferenceCharacter);
}

return new TextBuilder(store, Font, builderMaxWidth, UseFullGlyphHeight, startOffset, spacing, charactersBacking,
excludeCharacters, FallbackCharacter, FixedWidthReferenceCharacter);
}

protected virtual PositionTextBuilder CreateRubyTextBuilder(ITexturedGlyphLookupStore store)
{
const int builder_max_width = int.MaxValue;
return new PositionTextBuilder(store, RubyFont, builder_max_width, UseFullGlyphHeight,
new Vector2(0, -rubyMargin), rubySpacing, charactersBacking, FixedWidthExcludeCharacters, FallbackCharacter, FixedWidthReferenceCharacter, RelativePosition.Top, rubyAlignment);
}

protected virtual PositionTextBuilder CreateRomajiTextBuilder(ITexturedGlyphLookupStore store)
{
const int builder_max_width = int.MaxValue;
return new PositionTextBuilder(store, RomajiFont, builder_max_width, UseFullGlyphHeight,
new Vector2(0, romajiMargin), romajiSpacing, charactersBacking, FixedWidthExcludeCharacters, FallbackCharacter, FixedWidthReferenceCharacter, RelativePosition.Bottom, romajiAlignment);
}

private TextBuilder getTextBuilder()
{
if (!textBuilderCache.IsValid)
textBuilderCache.Value = CreateTextBuilder(store);

return textBuilderCache.Value;
}

public override string ToString() => $@"""{displayedText}"" " + base.ToString();

public float LineBaseHeight
{
get
{
computeCharacters();
return textBuilderCache.Value.LineBaseHeight;
}
}

public IEnumerable<string> FilterTerms => displayedText.Yield();
}
}
Loading

0 comments on commit 511c996

Please sign in to comment.