Skip to content

Commit

Permalink
Fix the algorithm to get ruby/romaji position text rectangle.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed May 21, 2022
1 parent e2725fb commit 986c8b6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
30 changes: 10 additions & 20 deletions osu.Framework.Font/Graphics/Sprites/LyricSpriteText_Characters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,31 +369,21 @@ public RectangleF GetCharacterDrawRectangle(int index, bool drawSizeOnly = false

public RectangleF GetRubyTagDrawRectangle(PositionText rubyTag, bool drawSizeOnly = false)
{
int rubyIndex = Rubies.ToList().IndexOf(rubyTag);
if (rubyIndex < 0)
throw new ArgumentOutOfRangeException(nameof(rubyIndex));

int startCharacterIndex = Text.Length + skinIndex(Rubies, rubyIndex);
int count = rubyTag.Text.Length;
var drawRectangle = characters.ToList()
.GetRange(startCharacterIndex, count)
.Select(x => TextBuilderGlyphUtils.GetCharacterRectangle(x, drawSizeOnly))
.Aggregate(RectangleF.Union);
if (!rubyCharactersBacking.TryGetValue(rubyTag, out var glyphs))
throw new ArgumentOutOfRangeException(nameof(rubyTag));

var drawRectangle = glyphs.Select(x => TextBuilderGlyphUtils.GetCharacterRectangle(x, drawSizeOnly))
.Aggregate(RectangleF.Union);
return getComputeCharacterDrawRectangle(drawRectangle);
}

public RectangleF GetRomajiTagDrawRectangle(PositionText romajiTag, bool drawSizeOnly = false)
{
int romajiIndex = Romajies.ToList().IndexOf(romajiTag);
if (romajiIndex < 0)
throw new ArgumentOutOfRangeException(nameof(romajiIndex));

int startCharacterIndex = Text.Length + skinIndex(Rubies, Rubies.Count) + skinIndex(Romajies, romajiIndex);
int count = romajiTag.Text.Length;
var drawRectangle = characters.ToList()
.GetRange(startCharacterIndex, count)
.Select(x => TextBuilderGlyphUtils.GetCharacterRectangle(x, drawSizeOnly))
.Aggregate(RectangleF.Union);
if (!romajiCharactersBacking.TryGetValue(romajiTag, out var glyphs))
throw new ArgumentOutOfRangeException(nameof(romajiTag));

var drawRectangle = glyphs.Select(x => TextBuilderGlyphUtils.GetCharacterRectangle(x, drawSizeOnly))
.Aggregate(RectangleF.Union);
return getComputeCharacterDrawRectangle(drawRectangle);
}

Expand Down
15 changes: 15 additions & 0 deletions osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,20 @@ public static RectangleF GetCharacterRectangle(TextBuilderGlyph character, bool
Bottom = character.Baseline - character.Height - character.YOffset + bottomIncrease,
});
}

public static RectangleF GetCharacterRectangle(PositionTextBuilderGlyph character, bool drawSizeOnly)
{
if (drawSizeOnly)
return character.DrawRectangle;

// todo: should get the real value.
var topReduce = character.Baseline * 0.3f;
var bottomIncrease = character.Baseline * 0.2f;
return character.DrawRectangle.Inflate(new MarginPadding
{
Top = character.YOffset - topReduce,
Bottom = character.Baseline - character.Height - character.YOffset + bottomIncrease,
});
}
}
}

0 comments on commit 986c8b6

Please sign in to comment.