Skip to content

Commit

Permalink
Merge pull request #205 from andy840119/adjust-full-sizing-calculation
Browse files Browse the repository at this point in the history
Adjust full sizing calculation
  • Loading branch information
andy840119 authored May 21, 2022
2 parents 95d1e97 + b3e0772 commit 2ee0662
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions osu.Framework.Font/Text/PositionTextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
38 changes: 14 additions & 24 deletions osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,26 @@

namespace osu.Framework.Utils
{
public class TextBuilderGlyphUtils
public static class TextBuilderGlyphUtils
{
public static RectangleF GetCharacterRectangle(TextBuilderGlyph character, bool drawSizeOnly)
{
if (drawSizeOnly)
return character.DrawRectangle;
private static float getCharacterTopOffset(ICharacterGlyph character)
=> character.Baseline * 0.3f;

// todo: should get the real value.
var topReduce = character.Baseline * 0.3f;
var bottomIncrease = character.Baseline * 0.2f;
return character.DrawRectangle.Inflate(new MarginPadding
private static float getCharacterBottomOffset(ICharacterGlyph character)
=> character.Baseline * 0.03f;

public static RectangleF GetCharacterSizeRectangle(TextBuilderGlyph character)
=> 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),
});
}

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
public static RectangleF GetCharacterSizeRectangle(PositionTextBuilderGlyph character)
=> 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),
});
}
}
}

0 comments on commit 2ee0662

Please sign in to comment.