1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-18 11:24:31 +02:00

added GenericFont compatibility for FontStashSharp

This commit is contained in:
Ell 2021-02-27 16:58:36 +01:00
parent 6900da9858
commit dda827b985
6 changed files with 91 additions and 4 deletions

View file

@ -0,0 +1,74 @@
using System.Text;
using FontStashSharp;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Font;
namespace MLEM.Extended.Font {
/// <inheritdoc/>
public class GenericStashFont : GenericFont {
/// <summary>
/// The <see cref="SpriteFontBase"/> that is being wrapped by this generic font
/// </summary>
public readonly SpriteFontBase Font;
/// <inheritdoc />
public override GenericFont Bold { get; }
/// <inheritdoc />
public override GenericFont Italic { get; }
/// <inheritdoc />
public override float LineHeight { get; }
/// <summary>
/// Creates a new generic font using <see cref="SpriteFontBase"/>.
/// Optionally, a bold and italic version of the font can be supplied.
/// </summary>
/// <param name="font">The font to wrap</param>
/// <param name="bold">A bold version of the font</param>
/// <param name="italic">An italic version of the font</param>
public GenericStashFont(SpriteFontBase font, SpriteFontBase bold = null, SpriteFontBase italic = null) {
this.Font = font;
// SpriteFontBase provides no line height, so we measure the height of a new line
this.LineHeight = font.MeasureString("\n").Y;
this.Bold = bold != null ? new GenericStashFont(bold) : this;
this.Italic = italic != null ? new GenericStashFont(italic) : this;
}
/// <inheritdoc />
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
this.Font.DrawText(batch, text, position, color);
}
/// <inheritdoc />
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
this.Font.DrawText(batch, text, position, color, new Vector2(scale), rotation, origin, layerDepth);
}
/// <inheritdoc />
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
this.Font.DrawText(batch, text, position, color, scale, rotation, origin, layerDepth);
}
/// <inheritdoc />
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
this.Font.DrawText(batch, text, position, color);
}
/// <inheritdoc />
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
this.Font.DrawText(batch, text, position, color, new Vector2(scale), rotation, origin, layerDepth);
}
/// <inheritdoc />
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
this.Font.DrawText(batch, text, position, color, scale, rotation, origin, layerDepth);
}
/// <inheritdoc />
protected override Vector2 MeasureChar(char c) {
return this.Font.MeasureString(c.ToCachedString());
}
}
}

View file

@ -6,7 +6,7 @@
<PropertyGroup>
<Authors>Ellpeck</Authors>
<Description>MLEM Library for Extending MonoGame extension that ties in with MonoGame.Extended</Description>
<Description>MLEM Library for Extending MonoGame extension that ties in with MonoGame.Extended and other MonoGame libraries</Description>
<PackageTags>monogame ellpeck mlem utility extensions monogame.extended extended</PackageTags>
<PackageProjectUrl>https://mlem.ellpeck.de/</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ellpeck/MLEM</RepositoryUrl>
@ -21,6 +21,9 @@
<PackageReference Include="MonoGame.Extended.Tiled" Version="3.8.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FontStashSharp.MonoGame" Version="0.9.2">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="MonoGame.Framework.Portable" Version="3.7.1.189">
<PrivateAssets>all</PrivateAssets>
</PackageReference>

View file

@ -14,6 +14,9 @@
#---------------------------------- Content ---------------------------------#
#begin Fonts/Cadman_Roman.otf
/copy:Fonts/Cadman_Roman.otf
#begin Fonts/Regular.fnt
/importer:BitmapFontImporter
/processor:BitmapFontProcessor

Binary file not shown.

View file

@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using FontStashSharp;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
@ -10,6 +11,7 @@ using MLEM.Data.Content;
using MLEM.Extended.Font;
using MLEM.Extended.Tiled;
using MLEM.Extensions;
using MLEM.Font;
using MLEM.Formatting;
using MLEM.Formatting.Codes;
using MLEM.Input;
@ -68,8 +70,11 @@ namespace Sandbox {
textureData[textureData.FromIndex(textureData.ToIndex(25, 9))] = Color.Yellow;
}
var system = new FontSystem(this.GraphicsDevice, 1024, 1024);
system.AddFont(File.ReadAllBytes("Content/Fonts/Cadman_Roman.otf"));
//var font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
var font = new GenericBitmapFont(LoadContent<BitmapFont>("Fonts/Regular"));
//var font = new GenericBitmapFont(LoadContent<BitmapFont>("Fonts/Regular"));
var font = new GenericStashFont(system.GetFont(32));
this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) {
Font = font,
TextScale = 0.1F,
@ -124,8 +129,9 @@ namespace Sandbox {
var res = this.Content.LoadJson<Test>("Test");
Console.WriteLine("The res is " + res);
/*this.OnDraw += (game, time) => {
this.OnDraw += (game, time) => {
this.SpriteBatch.Begin();
system.GetFont(32).DrawText(this.SpriteBatch, "Left Aligned\nover multiple lines", new Vector2(640, 0), Color.White);
font.DrawString(this.SpriteBatch, "Left Aligned\nover multiple lines", new Vector2(640, 0), TextAlign.Left, Color.White);
font.DrawString(this.SpriteBatch, "Center Aligned\nover multiple lines", new Vector2(640, 100), TextAlign.Center, Color.White);
font.DrawString(this.SpriteBatch, "Right Aligned\nover multiple lines", new Vector2(640, 200), TextAlign.Right, Color.White);
@ -138,7 +144,7 @@ namespace Sandbox {
font.DrawString(this.SpriteBatch, font.TruncateString("This is a very long string", 200, 1, true), new Vector2(200, 500), Color.White);
font.DrawString(this.SpriteBatch, font.TruncateString("This is a very long string", 200, 1, true, "..."), new Vector2(200, 550), Color.White);
this.SpriteBatch.End();
};*/
};
var sc = 4;
var formatter = new TextFormatter();

View file

@ -18,6 +18,7 @@
<PackageReference Include="MonoGame.Extended.Content.Pipeline" Version="3.7.0" />
<PackageReference Include="MonoGame.Extended.Tiled" Version="3.7.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="FontStashSharp.MonoGame" Version="0.9.2" />
</ItemGroup>
<ItemGroup>