From e3e219f00cbd151e06119c0f964b12aed69bbe36 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 21 May 2022 16:04:39 +0800 Subject: [PATCH 1/2] Should give it al better full-size calculation. --- .../Utils/TextBuilderGlyphUtils.cs | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs b/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs index 0e8fe9f..b07a342 100644 --- a/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs +++ b/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs @@ -7,20 +7,23 @@ namespace osu.Framework.Utils { - public class TextBuilderGlyphUtils + public static class TextBuilderGlyphUtils { + private static float getCharacterTopOffset(ICharacterGlyph character) + => character.Baseline * 0.3f; + + private static float getCharacterBottomOffset(ICharacterGlyph character) + => character.Baseline * 0.03f; + public static RectangleF GetCharacterRectangle(TextBuilderGlyph 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, + Top = character.YOffset - getCharacterTopOffset(character), + Bottom = character.Baseline - character.Height - character.YOffset + getCharacterBottomOffset(character), }); } @@ -29,13 +32,10 @@ public static RectangleF GetCharacterRectangle(PositionTextBuilderGlyph characte 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, + Top = character.YOffset - getCharacterTopOffset(character), + Bottom = character.Baseline - character.Height - character.YOffset + getCharacterBottomOffset(character), }); } } From b3e0772c147cfc0598c562a5350e9d340ff29ecd Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 21 May 2022 16:37:15 +0800 Subject: [PATCH 2/2] Remove that damn if. Utils should focus on calculating single type of size. --- .../Sprites/LyricSpriteText_Characters.cs | 6 +++--- .../Text/PositionTextFormatter.cs | 6 +++--- .../Utils/TextBuilderGlyphUtils.cs | 18 ++++-------------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/osu.Framework.Font/Graphics/Sprites/LyricSpriteText_Characters.cs b/osu.Framework.Font/Graphics/Sprites/LyricSpriteText_Characters.cs index d0f9240..cd35669 100644 --- a/osu.Framework.Font/Graphics/Sprites/LyricSpriteText_Characters.cs +++ b/osu.Framework.Font/Graphics/Sprites/LyricSpriteText_Characters.cs @@ -377,7 +377,7 @@ public RectangleF GetCharacterDrawRectangle(int index, bool drawSizeOnly = false throw new ArgumentOutOfRangeException(nameof(index)); var character = characters[charIndex]; - var drawRectangle = TextBuilderGlyphUtils.GetCharacterRectangle(character, drawSizeOnly); + var drawRectangle = drawSizeOnly ? character.DrawRectangle : TextBuilderGlyphUtils.GetCharacterSizeRectangle(character); return getComputeCharacterDrawRectangle(drawRectangle); } @@ -386,7 +386,7 @@ public RectangleF GetRubyTagDrawRectangle(PositionText rubyTag, bool drawSizeOnl if (!rubyCharactersBacking.TryGetValue(rubyTag, out var glyphs)) throw new ArgumentOutOfRangeException(nameof(rubyTag)); - var drawRectangle = glyphs.Select(x => TextBuilderGlyphUtils.GetCharacterRectangle(x, drawSizeOnly)) + var drawRectangle = glyphs.Select(x => drawSizeOnly ? x.DrawRectangle : TextBuilderGlyphUtils.GetCharacterSizeRectangle(x)) .Aggregate(RectangleF.Union); return getComputeCharacterDrawRectangle(drawRectangle); } @@ -396,7 +396,7 @@ public RectangleF GetRomajiTagDrawRectangle(PositionText romajiTag, bool drawSiz if (!romajiCharactersBacking.TryGetValue(romajiTag, out var glyphs)) throw new ArgumentOutOfRangeException(nameof(romajiTag)); - var drawRectangle = glyphs.Select(x => TextBuilderGlyphUtils.GetCharacterRectangle(x, drawSizeOnly)) + var drawRectangle = glyphs.Select(x => drawSizeOnly ? x.DrawRectangle : TextBuilderGlyphUtils.GetCharacterSizeRectangle(x)) .Aggregate(RectangleF.Union); return getComputeCharacterDrawRectangle(drawRectangle); } diff --git a/osu.Framework.Font/Text/PositionTextFormatter.cs b/osu.Framework.Font/Text/PositionTextFormatter.cs index 74c5e51..a10d159 100644 --- a/osu.Framework.Font/Text/PositionTextFormatter.cs +++ b/osu.Framework.Font/Text/PositionTextFormatter.cs @@ -70,15 +70,15 @@ private static Vector2 getPositionTextStartPosition(RectangleF mainTextRect, flo } private static Vector2 getPositionTextSize(TextBuilderGlyph[] glyphs) - => glyphs.Select(x => TextBuilderGlyphUtils.GetCharacterRectangle(x, false)) + => glyphs.Select(TextBuilderGlyphUtils.GetCharacterSizeRectangle) .Aggregate(RectangleF.Union).Size; private RectangleF getMainCharacterRectangleF(int startCharIndex, int endCharIndex) { var starCharacter = characterList[startCharIndex]; var endCharacter = characterList[endCharIndex - 1]; - var startCharacterRectangle = TextBuilderGlyphUtils.GetCharacterRectangle(starCharacter, false); - var endCharacterRectangle = TextBuilderGlyphUtils.GetCharacterRectangle(endCharacter, false); + var startCharacterRectangle = TextBuilderGlyphUtils.GetCharacterSizeRectangle(starCharacter); + var endCharacterRectangle = TextBuilderGlyphUtils.GetCharacterSizeRectangle(endCharacter); var position = startCharacterRectangle.TopLeft; diff --git a/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs b/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs index b07a342..cfcba9e 100644 --- a/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs +++ b/osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs @@ -15,28 +15,18 @@ private static float getCharacterTopOffset(ICharacterGlyph character) private static float getCharacterBottomOffset(ICharacterGlyph character) => character.Baseline * 0.03f; - public static RectangleF GetCharacterRectangle(TextBuilderGlyph character, bool drawSizeOnly) - { - if (drawSizeOnly) - return character.DrawRectangle; - - return character.DrawRectangle.Inflate(new MarginPadding + public static RectangleF GetCharacterSizeRectangle(TextBuilderGlyph character) + => character.DrawRectangle.Inflate(new MarginPadding { Top = character.YOffset - getCharacterTopOffset(character), Bottom = character.Baseline - character.Height - character.YOffset + getCharacterBottomOffset(character), }); - } - - public static RectangleF GetCharacterRectangle(PositionTextBuilderGlyph character, bool drawSizeOnly) - { - if (drawSizeOnly) - return character.DrawRectangle; - return character.DrawRectangle.Inflate(new MarginPadding + public static RectangleF GetCharacterSizeRectangle(PositionTextBuilderGlyph character) + => character.DrawRectangle.Inflate(new MarginPadding { Top = character.YOffset - getCharacterTopOffset(character), Bottom = character.Baseline - character.Height - character.YOffset + getCharacterBottomOffset(character), }); - } } }