1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

clean up GenericFont changes

This commit is contained in:
Ellpeck 2020-06-20 12:12:34 +02:00
parent 73f2e1c565
commit 64b9246aa5
5 changed files with 18 additions and 17 deletions

View file

@ -33,7 +33,7 @@ namespace MLEM.Extended.Font {
} }
/// <inheritdoc /> /// <inheritdoc />
protected override Vector2 CalcCharSize(char c) { protected override Vector2 MeasureChar(char c) {
var region = this.Font.GetCharacterRegion(c); var region = this.Font.GetCharacterRegion(c);
return region != null ? new Vector2(region.XAdvance, region.Height) : Vector2.Zero; return region != null ? new Vector2(region.XAdvance, region.Height) : Vector2.Zero;
} }

View file

@ -46,7 +46,7 @@ namespace MLEM.Ui.Style {
public override GenericFont Italic => this; public override GenericFont Italic => this;
public override float LineHeight => 1; public override float LineHeight => 1;
protected override Vector2 CalcCharSize(char c) { protected override Vector2 MeasureChar(char c) {
return Vector2.Zero; return Vector2.Zero;
} }

View file

@ -49,7 +49,7 @@ namespace MLEM.Font {
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth); public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);
///<inheritdoc cref="SpriteFont.MeasureString(string)"/> ///<inheritdoc cref="SpriteFont.MeasureString(string)"/>
protected abstract Vector2 CalcCharSize(char c); protected abstract Vector2 MeasureChar(char c);
/// <summary> /// <summary>
/// Draws a string with the given text alignment. /// Draws a string with the given text alignment.
@ -85,23 +85,23 @@ namespace MLEM.Font {
this.DrawString(batch, text, position, color, rotation, origin, scale, effects, layerDepth); this.DrawString(batch, text, position, color, rotation, origin, scale, effects, layerDepth);
} }
///<inheritdoc cref="SpriteFont.MeasureString(string)"/>
public Vector2 MeasureChar(char c) {
return c == OneEmSpace ? new Vector2(this.LineHeight) : this.CalcCharSize(c);
}
///<inheritdoc cref="SpriteFont.MeasureString(string)"/> ///<inheritdoc cref="SpriteFont.MeasureString(string)"/>
public Vector2 MeasureString(string text) { public Vector2 MeasureString(string text) {
var size = Vector2.Zero; var size = Vector2.Zero;
var xOffset = 0F; var xOffset = 0F;
foreach (var c in text) { foreach (var c in text) {
if (c == '\n') { switch (c) {
case '\n':
xOffset = 0; xOffset = 0;
size.Y += this.LineHeight; size.Y += this.LineHeight;
continue; break;
} case OneEmSpace:
xOffset += this.LineHeight;
break;
default:
xOffset += this.MeasureChar(c).X; xOffset += this.MeasureChar(c).X;
break;
}
// increase x size if this line is the longest // increase x size if this line is the longest
if (xOffset > size.X) if (xOffset > size.X)
size.X = xOffset; size.X = xOffset;

View file

@ -31,7 +31,7 @@ namespace MLEM.Font {
} }
/// <inheritdoc /> /// <inheritdoc />
protected override Vector2 CalcCharSize(char c) { protected override Vector2 MeasureChar(char c) {
return this.Font.MeasureString(c.ToString()); return this.Font.MeasureString(c.ToString());
} }

View file

@ -124,8 +124,9 @@ namespace MLEM.Formatting {
if (i == 0) if (i == 0)
token.DrawSelf(time, batch, pos + innerOffset, font, color, scale, depth); token.DrawSelf(time, batch, pos + innerOffset, font, color, scale, depth);
token.DrawCharacter(time, batch, c, c.ToString(), i, pos + innerOffset, drawFont, drawColor, scale, depth); var cString = c.ToString();
innerOffset.X += font.MeasureChar(c).X * scale; token.DrawCharacter(time, batch, c, cString, i, pos + innerOffset, drawFont, drawColor, scale, depth);
innerOffset.X += font.MeasureString(cString).X * scale;
} }
} }
} }