Skip to content

Commit

Permalink
Remove that damn if.
Browse files Browse the repository at this point in the history
Utils should focus on calculating single type of size.
  • Loading branch information
andy840119 committed May 21, 2022
1 parent e3e219f commit b3e0772
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 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
18 changes: 4 additions & 14 deletions osu.Framework.Font/Utils/TextBuilderGlyphUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
});
}
}
}

0 comments on commit b3e0772

Please sign in to comment.