From 986c8b6aa06219d4ad85a7b39f42d4e6a1c90f88 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 21 May 2022 11:55:59 +0800 Subject: [PATCH] Fix the algorithm to get ruby/romaji position text rectangle. --- .../Sprites/LyricSpriteText_Characters.cs | 30 +++++++------------ .../Utils/TextBuilderGlyphUtils.cs | 15 ++++++++++ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/osu.Framework.Font/Graphics/Sprites/LyricSpriteText_Characters.cs b/osu.Framework.Font/Graphics/Sprites/LyricSpriteText_Characters.cs index a633d60..611fe0b 100644 --- a/osu.Framework.Font/Graphics/Sprites/LyricSpriteText_Characters.cs +++ b/osu.Framework.Font/Graphics/Sprites/LyricSpriteText_Characters.cs @@ -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); } diff --git a/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs b/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs index 055eca0..0e8fe9f 100644 --- a/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs +++ b/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs @@ -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, + }); + } } }