From 6cee59e9b24092b14cbe87413a2757d37361051c Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Sun, 3 Sep 2023 23:15:46 +0200 Subject: [PATCH 01/17] feat: drop in FontStashSharp behind Extended.BitmapFont --- Blish HUD/Blish HUD.csproj | 1 + .../Modules/Managers/ContentsManager.cs | 53 ++++++++++++++++--- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/Blish HUD/Blish HUD.csproj b/Blish HUD/Blish HUD.csproj index 5b078912f..cf42607d2 100644 --- a/Blish HUD/Blish HUD.csproj +++ b/Blish HUD/Blish HUD.csproj @@ -238,6 +238,7 @@ + diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index e0f9f4f5d..7b0e1819f 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -1,9 +1,14 @@ -using System; -using System.IO; -using Blish_HUD.Content; +using Blish_HUD.Content; +using FontStashSharp; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended.BitmapFonts; +using MonoGame.Extended.TextureAtlases; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; namespace Blish_HUD.Modules.Managers { public class ContentsManager : IDisposable { @@ -93,11 +98,47 @@ public SoundEffect GetSound(string soundPath) { } /// - /// [NOT IMPLEMENTED] Loads a from a file. + /// Loads a from a file. /// /// The path to the font file. - public BitmapFont GetBitmapFont(string fontPath) { - throw new NotImplementedException(); + /// Size of the font. + public BitmapFont GetFont(string fontPath, ContentService.FontSize size) { + using var gdx = GameService.Graphics.LendGraphicsDevice(); + var fontSystem = new FontSystem(); + fontSystem.AddFont(_reader.GetFileStream(fontPath)); + var dynamicFont = fontSystem.GetFont((int)size); + var texture = dynamicFont.FontSystem.ExistingTexture; + + var regions = new List(); + + var xAdvance = 0; + + // TODO: Adjust the characterset + string allCharacters = new string(Enumerable.Range(32, 126).Select(x => (char)x).ToArray()); + + var glyphs = dynamicFont.GetGlyphs(allCharacters, + Vector2.Zero, + Vector2.Zero); + + foreach (var glyph in glyphs) { + var glyphTextureRegion = new TextureRegion2D(texture, + glyph.Bounds.Left, + glyph.Bounds.Top, + glyph.Bounds.Width, + glyph.Bounds.Height); + + var region = new BitmapFontRegion(glyphTextureRegion, + glyph.Codepoint, + glyph.Bounds.Left, + glyph.Bounds.Top, + xAdvance); + + regions.Add(region); + + xAdvance += glyph.XAdvance; + } + + return new BitmapFont(Path.GetFileNameWithoutExtension(fontPath), regions, dynamicFont.LineHeight); } /// From 38aab1bedeca71c6ccc5b90bf0fbb6c26fdea137 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Sun, 3 Sep 2023 23:29:10 +0200 Subject: [PATCH 02/17] fix: dispose fontsystem after bitmapfont conversion --- Blish HUD/GameServices/Modules/Managers/ContentsManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index 7b0e1819f..8b60f365c 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -104,7 +104,7 @@ public SoundEffect GetSound(string soundPath) { /// Size of the font. public BitmapFont GetFont(string fontPath, ContentService.FontSize size) { using var gdx = GameService.Graphics.LendGraphicsDevice(); - var fontSystem = new FontSystem(); + using var fontSystem = new FontSystem(); fontSystem.AddFont(_reader.GetFileStream(fontPath)); var dynamicFont = fontSystem.GetFont((int)size); var texture = dynamicFont.FontSystem.ExistingTexture; From ccfc9b04dee01ea068ec0433ae8dfe8e7e917ea1 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Mon, 4 Sep 2023 20:42:22 +0200 Subject: [PATCH 03/17] fix: use spriteFontPlus as it exposes all glyphs --- Blish HUD/Blish HUD.csproj | 2 +- .../Modules/Managers/ContentsManager.cs | 60 +++++++++---------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/Blish HUD/Blish HUD.csproj b/Blish HUD/Blish HUD.csproj index cf42607d2..52a13de2d 100644 --- a/Blish HUD/Blish HUD.csproj +++ b/Blish HUD/Blish HUD.csproj @@ -238,7 +238,6 @@ - @@ -254,6 +253,7 @@ + diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index 8b60f365c..a6d786107 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -1,14 +1,12 @@ using Blish_HUD.Content; -using FontStashSharp; -using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended.BitmapFonts; using MonoGame.Extended.TextureAtlases; +using SpriteFontPlus; using System; using System.Collections.Generic; using System.IO; -using System.Linq; namespace Blish_HUD.Modules.Managers { public class ContentsManager : IDisposable { @@ -98,47 +96,49 @@ public SoundEffect GetSound(string soundPath) { } /// - /// Loads a from a file. + /// Loads a from a TrueTypeFont (*.ttf) file. /// - /// The path to the font file. + /// The path to the TTF font file. /// Size of the font. public BitmapFont GetFont(string fontPath, ContentService.FontSize size) { - using var gdx = GameService.Graphics.LendGraphicsDevice(); - using var fontSystem = new FontSystem(); - fontSystem.AddFont(_reader.GetFileStream(fontPath)); - var dynamicFont = fontSystem.GetFont((int)size); - var texture = dynamicFont.FontSystem.ExistingTexture; - var regions = new List(); + using var fontStream = _reader.GetFileStream(fontPath); + var buffer = new byte[fontStream.Length]; + fontStream.Read(buffer, 0, buffer.Length); + + var bakeResult = TtfFontBaker.Bake(buffer, (int)size, 1024, 1024, new[] { + CharacterRange.BasicLatin, + CharacterRange.Latin1Supplement, + CharacterRange.LatinExtendedA, + CharacterRange.Cyrillic + }); + + using var gdx = GameService.Graphics.LendGraphicsDevice(); + var font = bakeResult.CreateSpriteFont(gdx); - var xAdvance = 0; + var texture = font.Texture; - // TODO: Adjust the characterset - string allCharacters = new string(Enumerable.Range(32, 126).Select(x => (char)x).ToArray()); + var regions = new List(); - var glyphs = dynamicFont.GetGlyphs(allCharacters, - Vector2.Zero, - Vector2.Zero); + var glyphs = font.GetGlyphs(); - foreach (var glyph in glyphs) { + foreach (var glyph in glyphs.Values) { var glyphTextureRegion = new TextureRegion2D(texture, - glyph.Bounds.Left, - glyph.Bounds.Top, - glyph.Bounds.Width, - glyph.Bounds.Height); + glyph.BoundsInTexture.Left, + glyph.BoundsInTexture.Top, + glyph.BoundsInTexture.Width, + glyph.BoundsInTexture.Height); var region = new BitmapFontRegion(glyphTextureRegion, - glyph.Codepoint, - glyph.Bounds.Left, - glyph.Bounds.Top, - xAdvance); + glyph.Character, + glyph.Cropping.Left, + glyph.Cropping.Top, + (int)glyph.WidthIncludingBearings); regions.Add(region); - - xAdvance += glyph.XAdvance; } - - return new BitmapFont(Path.GetFileNameWithoutExtension(fontPath), regions, dynamicFont.LineHeight); + + return new BitmapFont(Path.GetFileNameWithoutExtension(fontPath), regions, font.LineSpacing); } /// From b24ff2bc4175fd6bdfbb8052380bcb202871c3b8 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Mon, 4 Sep 2023 23:13:34 +0200 Subject: [PATCH 04/17] refactor: to extension --- .../Modules/Managers/ContentsManager.cs | 32 +-------------- Blish HUD/_Extensions/SpriteFontExtensions.cs | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 30 deletions(-) create mode 100644 Blish HUD/_Extensions/SpriteFontExtensions.cs diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index a6d786107..c31538b3e 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -2,10 +2,8 @@ using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended.BitmapFonts; -using MonoGame.Extended.TextureAtlases; using SpriteFontPlus; using System; -using System.Collections.Generic; using System.IO; namespace Blish_HUD.Modules.Managers { @@ -101,44 +99,18 @@ public SoundEffect GetSound(string soundPath) { /// The path to the TTF font file. /// Size of the font. public BitmapFont GetFont(string fontPath, ContentService.FontSize size) { - using var fontStream = _reader.GetFileStream(fontPath); var buffer = new byte[fontStream.Length]; + fontStream.Position = 0; fontStream.Read(buffer, 0, buffer.Length); - var bakeResult = TtfFontBaker.Bake(buffer, (int)size, 1024, 1024, new[] { CharacterRange.BasicLatin, CharacterRange.Latin1Supplement, CharacterRange.LatinExtendedA, CharacterRange.Cyrillic }); - using var gdx = GameService.Graphics.LendGraphicsDevice(); - var font = bakeResult.CreateSpriteFont(gdx); - - var texture = font.Texture; - - var regions = new List(); - - var glyphs = font.GetGlyphs(); - - foreach (var glyph in glyphs.Values) { - var glyphTextureRegion = new TextureRegion2D(texture, - glyph.BoundsInTexture.Left, - glyph.BoundsInTexture.Top, - glyph.BoundsInTexture.Width, - glyph.BoundsInTexture.Height); - - var region = new BitmapFontRegion(glyphTextureRegion, - glyph.Character, - glyph.Cropping.Left, - glyph.Cropping.Top, - (int)glyph.WidthIncludingBearings); - - regions.Add(region); - } - - return new BitmapFont(Path.GetFileNameWithoutExtension(fontPath), regions, font.LineSpacing); + return bakeResult.CreateSpriteFont(gdx).ToBitmapFont(); } /// diff --git a/Blish HUD/_Extensions/SpriteFontExtensions.cs b/Blish HUD/_Extensions/SpriteFontExtensions.cs new file mode 100644 index 000000000..92cc13245 --- /dev/null +++ b/Blish HUD/_Extensions/SpriteFontExtensions.cs @@ -0,0 +1,41 @@ +using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.BitmapFonts; +using MonoGame.Extended.TextureAtlases; +using System; +using System.Collections.Generic; + +namespace Blish_HUD { + internal static class SpriteFontExtensions { + + /// + /// Converts a to a . + /// + /// The to convert. + /// A as result of the conversion. + public static BitmapFont ToBitmapFont(this SpriteFont font) { + + var regions = new List(); + + var glyphs = font.GetGlyphs(); + + foreach (var glyph in glyphs.Values) { + var glyphTextureRegion = new TextureRegion2D(font.Texture, + glyph.BoundsInTexture.Left, + glyph.BoundsInTexture.Top, + glyph.BoundsInTexture.Width, + glyph.BoundsInTexture.Height); + + var region = new BitmapFontRegion(glyphTextureRegion, + glyph.Character, + glyph.Cropping.Left, + glyph.Cropping.Top, + (int)glyph.WidthIncludingBearings); + + regions.Add(region); + } + + return new BitmapFont(Guid.NewGuid().ToString("n"), regions, font.LineSpacing); + } + + } +} From a91eb39981e905ae376b7bfe2b349c5de542ad0f Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Mon, 4 Sep 2023 23:31:55 +0200 Subject: [PATCH 05/17] refactor: maintain code style --- .../Modules/Managers/ContentsManager.cs | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index c31538b3e..81b1c03f8 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -99,18 +99,20 @@ public SoundEffect GetSound(string soundPath) { /// The path to the TTF font file. /// Size of the font. public BitmapFont GetFont(string fontPath, ContentService.FontSize size) { - using var fontStream = _reader.GetFileStream(fontPath); - var buffer = new byte[fontStream.Length]; - fontStream.Position = 0; - fontStream.Read(buffer, 0, buffer.Length); - var bakeResult = TtfFontBaker.Bake(buffer, (int)size, 1024, 1024, new[] { - CharacterRange.BasicLatin, - CharacterRange.Latin1Supplement, - CharacterRange.LatinExtendedA, - CharacterRange.Cyrillic - }); - using var gdx = GameService.Graphics.LendGraphicsDevice(); - return bakeResult.CreateSpriteFont(gdx).ToBitmapFont(); + long fontDataLength = _reader.GetFileBytes(fontPath, out byte[] fontData); + + if (fontDataLength > 0) { + using var ctx = GameService.Graphics.LendGraphicsDeviceContext(); + var bakeResult = TtfFontBaker.Bake(fontData, (int)size, 1024, 1024, new[] { + CharacterRange.BasicLatin, + CharacterRange.Latin1Supplement, + CharacterRange.LatinExtendedA, + CharacterRange.Cyrillic + }); + return bakeResult.CreateSpriteFont(ctx.GraphicsDevice).ToBitmapFont(); + } + + return null; } /// From 1ec1012370d5a80463ab2e56fd2431b4bf33e6e8 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Tue, 5 Sep 2023 13:35:46 +0200 Subject: [PATCH 06/17] fix: remove cyrillic characters and add optional line height param --- Blish HUD/GameServices/Modules/Managers/ContentsManager.cs | 6 +++--- Blish HUD/_Extensions/SpriteFontExtensions.cs | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index 81b1c03f8..d4f343f9e 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -98,7 +98,8 @@ public SoundEffect GetSound(string soundPath) { /// /// The path to the TTF font file. /// Size of the font. - public BitmapFont GetFont(string fontPath, ContentService.FontSize size) { + /// Line height for the . By default, will be used. + public BitmapFont GetBitmapFont(string fontPath, ContentService.FontSize size, int lineHeight = 0) { long fontDataLength = _reader.GetFileBytes(fontPath, out byte[] fontData); if (fontDataLength > 0) { @@ -106,8 +107,7 @@ public BitmapFont GetFont(string fontPath, ContentService.FontSize size) { var bakeResult = TtfFontBaker.Bake(fontData, (int)size, 1024, 1024, new[] { CharacterRange.BasicLatin, CharacterRange.Latin1Supplement, - CharacterRange.LatinExtendedA, - CharacterRange.Cyrillic + CharacterRange.LatinExtendedA }); return bakeResult.CreateSpriteFont(ctx.GraphicsDevice).ToBitmapFont(); } diff --git a/Blish HUD/_Extensions/SpriteFontExtensions.cs b/Blish HUD/_Extensions/SpriteFontExtensions.cs index 92cc13245..b14f1ee1d 100644 --- a/Blish HUD/_Extensions/SpriteFontExtensions.cs +++ b/Blish HUD/_Extensions/SpriteFontExtensions.cs @@ -11,8 +11,9 @@ internal static class SpriteFontExtensions { /// Converts a to a . /// /// The to convert. + /// Line height for the . By default, will be used. /// A as result of the conversion. - public static BitmapFont ToBitmapFont(this SpriteFont font) { + public static BitmapFont ToBitmapFont(this SpriteFont font, int lineHeight = 0) { var regions = new List(); @@ -34,7 +35,7 @@ public static BitmapFont ToBitmapFont(this SpriteFont font) { regions.Add(region); } - return new BitmapFont(Guid.NewGuid().ToString("n"), regions, font.LineSpacing); + return new BitmapFont($"{typeof(BitmapFont)}_{Guid.NewGuid():n}", regions, lineHeight > 0 ? lineHeight : font.LineSpacing); } } From 7bc9fd64be951b37a1749608313111f620361f5b Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Tue, 5 Sep 2023 13:50:52 +0200 Subject: [PATCH 07/17] skip: add overload for fontSize param as int --- .../Modules/Managers/ContentsManager.cs | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index d4f343f9e..d8d4e4367 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -97,14 +97,32 @@ public SoundEffect GetSound(string soundPath) { /// Loads a from a TrueTypeFont (*.ttf) file. /// /// The path to the TTF font file. - /// Size of the font. + /// Size of the font. /// Line height for the . By default, will be used. - public BitmapFont GetBitmapFont(string fontPath, ContentService.FontSize size, int lineHeight = 0) { + public BitmapFont GetBitmapFont(string fontPath, ContentService.FontSize fontSize, int lineHeight = 0) { + return GetBitmapFont(fontPath, (int)fontSize, lineHeight); + } + + /// + /// Loads a from a TrueTypeFont (*.ttf) file. + /// + /// The path to the TTF font file. + /// Size of the font. + /// Line height for the . By default, will be used. + public BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = 0) { + if (fontSize <= 0) { + throw new ArgumentException("Font size must be greater than 0.", nameof(fontSize)); + } + + if (lineHeight <= 0) { + throw new ArgumentException("Line height must be greater than 0.", nameof(lineHeight)); + } + long fontDataLength = _reader.GetFileBytes(fontPath, out byte[] fontData); if (fontDataLength > 0) { using var ctx = GameService.Graphics.LendGraphicsDeviceContext(); - var bakeResult = TtfFontBaker.Bake(fontData, (int)size, 1024, 1024, new[] { + var bakeResult = TtfFontBaker.Bake(fontData, (int)fontSize, 1024, 1024, new[] { CharacterRange.BasicLatin, CharacterRange.Latin1Supplement, CharacterRange.LatinExtendedA From 2900e48d863d9438d11d83cbfa331b3cb4595fba Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Tue, 5 Sep 2023 13:51:35 +0200 Subject: [PATCH 08/17] fix: line height exception conditional --- Blish HUD/GameServices/Modules/Managers/ContentsManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index d8d4e4367..f7d01c818 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -114,7 +114,7 @@ public BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = throw new ArgumentException("Font size must be greater than 0.", nameof(fontSize)); } - if (lineHeight <= 0) { + if (lineHeight < 0) { throw new ArgumentException("Line height must be greater than 0.", nameof(lineHeight)); } From 31bf6e5a84678a8c475128ee1e22a9d33f416b21 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Tue, 5 Sep 2023 14:53:46 +0200 Subject: [PATCH 09/17] fix: hook up custom lineHeight param --- Blish HUD/GameServices/Modules/Managers/ContentsManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index f7d01c818..10fa9b456 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -122,12 +122,12 @@ public BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = if (fontDataLength > 0) { using var ctx = GameService.Graphics.LendGraphicsDeviceContext(); - var bakeResult = TtfFontBaker.Bake(fontData, (int)fontSize, 1024, 1024, new[] { + var bakeResult = TtfFontBaker.Bake(fontData, fontSize, 1024, 1024, new[] { CharacterRange.BasicLatin, CharacterRange.Latin1Supplement, CharacterRange.LatinExtendedA }); - return bakeResult.CreateSpriteFont(ctx.GraphicsDevice).ToBitmapFont(); + return bakeResult.CreateSpriteFont(ctx.GraphicsDevice).ToBitmapFont(lineHeight); } return null; From 65f4053d4869d163b4acbb37b3c2d121100164b5 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Tue, 5 Sep 2023 19:44:19 +0200 Subject: [PATCH 10/17] fix: remove FontSize overload because misleading --- .../Modules/Managers/ContentsManager.cs | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index 10fa9b456..c5f7f846a 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -94,30 +94,15 @@ public SoundEffect GetSound(string soundPath) { } /// - /// Loads a from a TrueTypeFont (*.ttf) file. + /// Loads a from a TrueTypeFont (*.ttf) file. /// /// The path to the TTF font file. /// Size of the font. - /// Line height for the . By default, will be used. - public BitmapFont GetBitmapFont(string fontPath, ContentService.FontSize fontSize, int lineHeight = 0) { - return GetBitmapFont(fontPath, (int)fontSize, lineHeight); - } - - /// - /// Loads a from a TrueTypeFont (*.ttf) file. - /// - /// The path to the TTF font file. - /// Size of the font. - /// Line height for the . By default, will be used. - public BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = 0) { + public SpriteFont GetSpriteFont(string fontPath, int fontSize) { if (fontSize <= 0) { throw new ArgumentException("Font size must be greater than 0.", nameof(fontSize)); } - if (lineHeight < 0) { - throw new ArgumentException("Line height must be greater than 0.", nameof(lineHeight)); - } - long fontDataLength = _reader.GetFileBytes(fontPath, out byte[] fontData); if (fontDataLength > 0) { @@ -127,12 +112,25 @@ public BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = CharacterRange.Latin1Supplement, CharacterRange.LatinExtendedA }); - return bakeResult.CreateSpriteFont(ctx.GraphicsDevice).ToBitmapFont(lineHeight); + return bakeResult.CreateSpriteFont(ctx.GraphicsDevice); } return null; } + /// + /// Loads a from a TrueTypeFont (*.ttf) file. + /// + /// The path to the TTF font file. + /// Size of the font. + /// Sets the line height. By default, will be used. + public BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = 0) { + if (lineHeight < 0) { + throw new ArgumentException("Line height cannot be negative.", nameof(lineHeight)); + } + return GetSpriteFont(fontPath, fontSize)?.ToBitmapFont(lineHeight); + } + /// /// [NOT IMPLEMENTED] Loads a from a file. /// From c8cb22fd7dafeb031515bfbd49fd98aa8f8cd873 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Sat, 16 Sep 2023 22:50:29 +0200 Subject: [PATCH 11/17] fix: add optional textureSize param --- Blish HUD/GameServices/Modules/Managers/ContentsManager.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index c5f7f846a..40269da00 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -98,7 +98,8 @@ public SoundEffect GetSound(string soundPath) { /// /// The path to the TTF font file. /// Size of the font. - public SpriteFont GetSpriteFont(string fontPath, int fontSize) { + /// Size of the .
A greater fontSize results in bigger glyphs which may require more texture space. + public SpriteFont GetSpriteFont(string fontPath, int fontSize, int textureSize = 1392) { if (fontSize <= 0) { throw new ArgumentException("Font size must be greater than 0.", nameof(fontSize)); } @@ -107,7 +108,7 @@ public SpriteFont GetSpriteFont(string fontPath, int fontSize) { if (fontDataLength > 0) { using var ctx = GameService.Graphics.LendGraphicsDeviceContext(); - var bakeResult = TtfFontBaker.Bake(fontData, fontSize, 1024, 1024, new[] { + var bakeResult = TtfFontBaker.Bake(fontData, fontSize, textureSize, textureSize, new[] { CharacterRange.BasicLatin, CharacterRange.Latin1Supplement, CharacterRange.LatinExtendedA From b14c97d52f67727ffe01db1709ccee04876b9000 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Sat, 16 Sep 2023 23:23:54 +0200 Subject: [PATCH 12/17] fix: pass textureSize in GetBitmapFont --- .../GameServices/Modules/Managers/ContentsManager.cs | 12 +++++++----- Blish HUD/_Extensions/SpriteFontExtensions.cs | 3 +++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index 40269da00..e623f3999 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -104,6 +104,10 @@ public SpriteFont GetSpriteFont(string fontPath, int fontSize, int textureSize = throw new ArgumentException("Font size must be greater than 0.", nameof(fontSize)); } + if (textureSize <= 0) { + throw new ArgumentException("Texture size must be greater than 0.", nameof(textureSize)); + } + long fontDataLength = _reader.GetFileBytes(fontPath, out byte[] fontData); if (fontDataLength > 0) { @@ -125,11 +129,9 @@ public SpriteFont GetSpriteFont(string fontPath, int fontSize, int textureSize = /// The path to the TTF font file. /// Size of the font. /// Sets the line height. By default, will be used. - public BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = 0) { - if (lineHeight < 0) { - throw new ArgumentException("Line height cannot be negative.", nameof(lineHeight)); - } - return GetSpriteFont(fontPath, fontSize)?.ToBitmapFont(lineHeight); + /// Size of the .
A greater fontSize results in bigger glyphs which may require more texture space. + public BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = 0, int textureSize = 1392) { + return GetSpriteFont(fontPath, fontSize, textureSize)?.ToBitmapFont(lineHeight); } /// diff --git a/Blish HUD/_Extensions/SpriteFontExtensions.cs b/Blish HUD/_Extensions/SpriteFontExtensions.cs index b14f1ee1d..e08acde1f 100644 --- a/Blish HUD/_Extensions/SpriteFontExtensions.cs +++ b/Blish HUD/_Extensions/SpriteFontExtensions.cs @@ -14,6 +14,9 @@ internal static class SpriteFontExtensions { /// Line height for the . By default, will be used. /// A as result of the conversion. public static BitmapFont ToBitmapFont(this SpriteFont font, int lineHeight = 0) { + if (lineHeight < 0) { + throw new ArgumentException("Line height cannot be negative.", nameof(lineHeight)); + } var regions = new List(); From f395c48316fa4e3754ec7792ff6e59fab7cd6a11 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Sat, 16 Sep 2023 23:44:16 +0200 Subject: [PATCH 13/17] fix: make public Avoid duplicate font baking and SpriteFont loading when mixing native with non-native draw calls. --- Blish HUD/_Extensions/SpriteFontExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Blish HUD/_Extensions/SpriteFontExtensions.cs b/Blish HUD/_Extensions/SpriteFontExtensions.cs index e08acde1f..029be0826 100644 --- a/Blish HUD/_Extensions/SpriteFontExtensions.cs +++ b/Blish HUD/_Extensions/SpriteFontExtensions.cs @@ -5,7 +5,7 @@ using System.Collections.Generic; namespace Blish_HUD { - internal static class SpriteFontExtensions { + public static class SpriteFontExtensions { /// /// Converts a to a . From c9114acaa0e2d29ef261e3db0af0bf7ca68933e1 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Tue, 19 Sep 2023 15:57:24 +0200 Subject: [PATCH 14/17] fix: extend BitmapFont with IDisposable to unload module fonts --- Blish HUD/GameServices/Content/BitmapFont.cs | 42 +++++++++++++++++++ .../Modules/Managers/ContentsManager.cs | 4 +- Blish HUD/_Extensions/SpriteFontExtensions.cs | 10 ++--- 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 Blish HUD/GameServices/Content/BitmapFont.cs diff --git a/Blish HUD/GameServices/Content/BitmapFont.cs b/Blish HUD/GameServices/Content/BitmapFont.cs new file mode 100644 index 000000000..b0a46fe6f --- /dev/null +++ b/Blish HUD/GameServices/Content/BitmapFont.cs @@ -0,0 +1,42 @@ +using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.BitmapFonts; +using System; +using System.Collections.Generic; + +namespace Blish_HUD.Content { + /// + /// Extends to allow disposing of its glyph lookup texture. + /// + public class BitmapFont : MonoGame.Extended.BitmapFonts.BitmapFont, IDisposable { + + private readonly Texture2D _texture; + + /// + /// Creates a with the provided identifier name, glyph regions, line height, and texture to draw letters from. + /// + /// Name to identify the font with. + /// Regions of the glyphs on the texture. + /// Line height of the font. + /// Lookup texture to draw letters from. + public BitmapFont(string name, IEnumerable regions, int lineHeight, Texture2D texture) : base(name, regions, lineHeight) { + _texture = texture; + } + + /// + /// Creates a with the provided identifier name, glyph regions and line height. + /// + /// Name to identify the font with. + /// Regions of the glyphs on the texture. + /// Line height of the font. + public BitmapFont(string name, IReadOnlyList regions, int lineHeight) : base(name, regions, lineHeight) { + _texture = regions[0].TextureRegion.Texture; + } + + /// + /// Disposes the lookup texture of this to free memory. Renders this unusable. + /// + public void Dispose() { + _texture?.Dispose(); + } + } +} diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index e623f3999..d4a0880fc 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -124,13 +124,13 @@ public SpriteFont GetSpriteFont(string fontPath, int fontSize, int textureSize = } /// - /// Loads a from a TrueTypeFont (*.ttf) file. + /// Loads a from a TrueTypeFont (*.ttf) file. /// /// The path to the TTF font file. /// Size of the font. /// Sets the line height. By default, will be used. /// Size of the .
A greater fontSize results in bigger glyphs which may require more texture space. - public BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = 0, int textureSize = 1392) { + public Content.BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = 0, int textureSize = 1392) { return GetSpriteFont(fontPath, fontSize, textureSize)?.ToBitmapFont(lineHeight); } diff --git a/Blish HUD/_Extensions/SpriteFontExtensions.cs b/Blish HUD/_Extensions/SpriteFontExtensions.cs index 029be0826..4492bddf1 100644 --- a/Blish HUD/_Extensions/SpriteFontExtensions.cs +++ b/Blish HUD/_Extensions/SpriteFontExtensions.cs @@ -8,12 +8,12 @@ namespace Blish_HUD { public static class SpriteFontExtensions { /// - /// Converts a to a . + /// Converts a to a . /// /// The to convert. - /// Line height for the . By default, will be used. - /// A as result of the conversion. - public static BitmapFont ToBitmapFont(this SpriteFont font, int lineHeight = 0) { + /// Line height for the . By default, will be used. + /// A as result of the conversion. + public static Content.BitmapFont ToBitmapFont(this SpriteFont font, int lineHeight = 0) { if (lineHeight < 0) { throw new ArgumentException("Line height cannot be negative.", nameof(lineHeight)); } @@ -38,7 +38,7 @@ public static BitmapFont ToBitmapFont(this SpriteFont font, int lineHeight = 0) regions.Add(region); } - return new BitmapFont($"{typeof(BitmapFont)}_{Guid.NewGuid():n}", regions, lineHeight > 0 ? lineHeight : font.LineSpacing); + return new Content.BitmapFont($"{typeof(Content.BitmapFont)}_{Guid.NewGuid():n}", regions, lineHeight > 0 ? lineHeight : font.LineSpacing, font.Texture); } } From 054a9c9354e76c8bb9de059e0e276eb33edfc6c9 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Fri, 22 Sep 2023 18:27:28 +0200 Subject: [PATCH 15/17] fix: ambiguity between Extended.BitmapFont and Content.BitmapFont --- Blish HUD/Controls/Book.cs | 167 ------------------ Blish HUD/Controls/Page.cs | 78 -------- .../Controls/_Types/CachedStringRender.cs | 11 +- .../Controls/_Types/FormattedLabelPart.cs | 7 +- Blish HUD/GameServices/ContentService.cs | 14 +- 5 files changed, 14 insertions(+), 263 deletions(-) delete mode 100644 Blish HUD/Controls/Book.cs delete mode 100644 Blish HUD/Controls/Page.cs diff --git a/Blish HUD/Controls/Book.cs b/Blish HUD/Controls/Book.cs deleted file mode 100644 index d1570bd31..000000000 --- a/Blish HUD/Controls/Book.cs +++ /dev/null @@ -1,167 +0,0 @@ -using Blish_HUD.Input; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using MonoGame.Extended.BitmapFonts; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Blish_HUD.Content; - -namespace Blish_HUD.Controls -{ - public class Book : Container - { - private readonly BitmapFont TitleFont = GameService.Content.GetFont(ContentService.FontFace.Menomonia, ContentService.FontSize.Size32, ContentService.FontStyle.Regular); - private readonly AsyncTexture2D TurnPageSprite; - - private static int RIGHT_PADDING = 150; - private static int TOP_PADDING = 100; - private static int SHEET_OFFSET_Y = 20; - - private Rectangle _leftButtonBounds; - private Rectangle _rightButtonBounds; - private Rectangle _titleBounds; - - private bool MouseOverTurnPageLeft; - private bool MouseOverTurnPageRight; - - private List Pages = new List(); - - private string _title = "No Title"; - /// - /// Sets the title of this book. - /// - public string Title - { - get => _title; - set - { - if (value.Equals(_title)) return; - SetProperty(ref _title, value, true); - } - } - /// - /// The currently open page of this book. - /// - public Page CurrentPage { get; private set; } - /// - /// Creates a panel that should act as Parent for Page controls to create a book UI. - /// - public Book() - { - TurnPageSprite = TurnPageSprite ?? AsyncTexture2D.FromAssetId(1909317); - } - protected override void OnResized(ResizedEventArgs e) - { - ContentRegion = new Rectangle(0, 0, e.CurrentSize.X, e.CurrentSize.Y); - - _leftButtonBounds = new Rectangle(25, (ContentRegion.Height - TurnPageSprite.Bounds.Height) / 2 + SHEET_OFFSET_Y, TurnPageSprite.Bounds.Width, TurnPageSprite.Bounds.Height); - _rightButtonBounds = new Rectangle(ContentRegion.Width - TurnPageSprite.Bounds.Width - 25, (ContentRegion.Height - TurnPageSprite.Bounds.Height) / 2 + SHEET_OFFSET_Y, TurnPageSprite.Bounds.Width, TurnPageSprite.Bounds.Height); - - var titleSize = (Point)TitleFont.MeasureString(_title); - _titleBounds = new Rectangle((ContentRegion.Width - titleSize.X) / 2, ContentRegion.Top + (TOP_PADDING - titleSize.Y) / 2, titleSize.X, titleSize.Y); - - if (Pages != null && Pages.Count > 0) { - foreach (Page page in this.Pages) - { - if (page == null) continue; - page.Size = PointExtensions.ResizeKeepAspect(page.Size, ContentRegion.Width - RIGHT_PADDING, ContentRegion.Height - TOP_PADDING, true); - page.Location = new Point((ContentRegion.Width - page.Size.X) / 2, (ContentRegion.Height - page.Size.Y) / 2 + SHEET_OFFSET_Y); - } - } - - base.OnResized(e); - } - protected override void OnHidden(EventArgs e) - { - //TODO: Add gw2 book sound: close book. - base.OnHidden(e); - } - protected override void OnShown(EventArgs e) - { - //TODO: Add gw2 book sound: open book. - base.OnShown(e); - } - protected override void OnChildAdded(ChildChangedEventArgs e) - { - if (e.ChangedChild is Page && !Pages.Any(x => x.Equals((Page)e.ChangedChild))) - { - Page page = (Page)e.ChangedChild; - page.Size = PointExtensions.ResizeKeepAspect(page.Size, ContentRegion.Width - RIGHT_PADDING, ContentRegion.Height - TOP_PADDING, true); - page.Location = new Point((ContentRegion.Width - page.Size.X) / 2, (ContentRegion.Height - page.Size.Y) / 2 + SHEET_OFFSET_Y); - page.PageNumber = Pages.Count + 1; - Pages.Add(page); - - if (Pages.Count == 1) CurrentPage = page; - if (page != CurrentPage) page.Hide(); - } - - base.OnChildAdded(e); - } - protected override void OnMouseMoved(MouseEventArgs e) - { - var relPos = this.RelativeMousePosition; - - this.MouseOverTurnPageLeft = _leftButtonBounds.Contains(relPos); - this.MouseOverTurnPageRight = _rightButtonBounds.Contains(relPos); - - base.OnMouseMoved(e); - } - protected override void OnLeftMouseButtonPressed(MouseEventArgs e) - { - if (this.MouseOverTurnPageLeft) - { - TurnPage(Pages.IndexOf(CurrentPage) - 1); - } - else if (this.MouseOverTurnPageRight) - { - TurnPage(Pages.IndexOf(CurrentPage) + 1); - } - - base.OnLeftMouseButtonPressed(e); - } - private void TurnPage(int index) - { - if (index < Pages.Count && index >= 0) - { - // TODO: Add gw2's book sounds: turn page - CurrentPage = Pages[index]; - - foreach (Page other in Pages) - { - other.Visible = other == CurrentPage; - } - } - } - public override void PaintBeforeChildren(SpriteBatch spriteBatch, Rectangle bounds) - { - base.PaintBeforeChildren(spriteBatch, bounds); - - // TODO: Title background texture from the original. - - // Draw title - spriteBatch.DrawStringOnCtrl(this, _title, TitleFont, _titleBounds, Color.White, false, HorizontalAlignment.Left, VerticalAlignment.Top); - - // Draw turn page buttons - if (!MouseOverTurnPageLeft) - { - spriteBatch.DrawOnCtrl(this, TurnPageSprite, _leftButtonBounds, TurnPageSprite.Bounds, new Color(155, 155, 155, 155), 0, Vector2.Zero, SpriteEffects.FlipHorizontally); - } - else - { - spriteBatch.DrawOnCtrl(this, TurnPageSprite, _leftButtonBounds, TurnPageSprite.Bounds, Color.White, 0, Vector2.Zero, SpriteEffects.FlipHorizontally); - } - - if (!MouseOverTurnPageRight) - { - spriteBatch.DrawOnCtrl(this, TurnPageSprite, _rightButtonBounds, TurnPageSprite.Bounds, new Color(155, 155, 155, 155)); - } - else - { - spriteBatch.DrawOnCtrl(this, TurnPageSprite, _rightButtonBounds, TurnPageSprite.Bounds, Color.White); - } - } - } -} diff --git a/Blish HUD/Controls/Page.cs b/Blish HUD/Controls/Page.cs deleted file mode 100644 index 2bf53f327..000000000 --- a/Blish HUD/Controls/Page.cs +++ /dev/null @@ -1,78 +0,0 @@ -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using MonoGame.Extended.BitmapFonts; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using static Blish_HUD.ContentService; -namespace Blish_HUD.Controls -{ - public class Page : Control - { - private const int SHEET_BORDER = 40; - private const int FIX_WORDCLIPPING_WIDTH = 30; - - private readonly Texture2D SheetSprite; - - private static BitmapFont PageNumberFont = GameService.Content.GetFont(ContentService.FontFace.Menomonia, ContentService.FontSize.Size18, ContentService.FontStyle.Regular); - - private int _pageNumber = 1; - public int PageNumber { - get => _pageNumber; - set - { - if (value == _pageNumber) return; - SetProperty(ref _pageNumber, value, true); - } - } - private string _text = ""; - /// - /// The text to display on the sheet. - /// - public string Text { - get => _text; - set { - if (value.Equals(_text)) return; - SetProperty(ref _text, value, true); - } - } - private BitmapFont _textFont = GameService.Content.GetFont(ContentService.FontFace.Menomonia, ContentService.FontSize.Size24, ContentService.FontStyle.Regular); - /// - /// The font that is for the text. - /// - public BitmapFont TextFont - { - get => _textFont; - set - { - if (value.Equals(_textFont)) return; - SetProperty(ref _textFont, value, true); - } - } - /// - /// Creates a control similar to the Tyrian' sheet of paper found on the book UI. - /// - /// Scale size to keep the sheet's aspect ratio. - public Page(int scale = 1) - { - Size = new Point(420 * scale, 560 * scale); - SheetSprite = SheetSprite ?? Content.GetTexture("1909316"); - } - protected override void Paint(SpriteBatch spriteBatch, Rectangle bounds) - { - spriteBatch.DrawOnCtrl(this, SheetSprite, bounds, SheetSprite.Bounds, Color.White, 0f, Vector2.Zero, SpriteEffects.None); - - if (_text.Length > 0) - { - Rectangle contentArea = new Rectangle(new Point(SHEET_BORDER, SHEET_BORDER), new Point(this.Size.X - (SHEET_BORDER * 2) - FIX_WORDCLIPPING_WIDTH, this.Size.Y - (SHEET_BORDER * 2))); - spriteBatch.DrawStringOnCtrl(this, _text, _textFont, contentArea, Color.Black, true, HorizontalAlignment.Left, VerticalAlignment.Top); - string pageNumber = _pageNumber + ""; - Point pageNumberSize = (Point)PageNumberFont.MeasureString(pageNumber); - Point pageNumberCenter = new Point((this.Size.X - pageNumberSize.X) / 2, this.Size.Y - pageNumberSize.Y - (SHEET_BORDER / 2)); - spriteBatch.DrawStringOnCtrl(this, pageNumber, PageNumberFont, new Rectangle(pageNumberCenter, pageNumberSize), Color.Black, false, HorizontalAlignment.Left, VerticalAlignment.Top); - } - } - } -} diff --git a/Blish HUD/Controls/_Types/CachedStringRender.cs b/Blish HUD/Controls/_Types/CachedStringRender.cs index 54983b865..53d262a1c 100644 --- a/Blish HUD/Controls/_Types/CachedStringRender.cs +++ b/Blish HUD/Controls/_Types/CachedStringRender.cs @@ -1,12 +1,9 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.ServiceModel; -using Blish_HUD; -using Blish_HUD.Content; +using Blish_HUD.Content; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using MonoGame.Extended.BitmapFonts; +using System; +using System.Collections.Concurrent; +using BitmapFont = MonoGame.Extended.BitmapFonts.BitmapFont; namespace Blish_HUD.Controls { public class CachedStringRender : IDisposable { diff --git a/Blish HUD/Controls/_Types/FormattedLabelPart.cs b/Blish HUD/Controls/_Types/FormattedLabelPart.cs index 8264f1ec6..069751609 100644 --- a/Blish HUD/Controls/_Types/FormattedLabelPart.cs +++ b/Blish HUD/Controls/_Types/FormattedLabelPart.cs @@ -1,8 +1,7 @@ -using System; -using Blish_HUD.Content; +using Blish_HUD.Content; using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using MonoGame.Extended.BitmapFonts; +using System; +using BitmapFont = MonoGame.Extended.BitmapFonts.BitmapFont; namespace Blish_HUD.Controls { internal class FormattedLabelPart : IDisposable { diff --git a/Blish HUD/GameServices/ContentService.cs b/Blish HUD/GameServices/ContentService.cs index 8fd5e0069..04ecd9be1 100644 --- a/Blish HUD/GameServices/ContentService.cs +++ b/Blish HUD/GameServices/ContentService.cs @@ -1,14 +1,14 @@ -using System; -using System.Collections.Concurrent; -using System.IO; -using System.IO.Compression; -using System.Text.RegularExpressions; -using Blish_HUD.Content; +using Blish_HUD.Content; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; -using MonoGame.Extended.BitmapFonts; +using System; +using System.Collections.Concurrent; +using System.IO; +using System.IO.Compression; +using System.Text.RegularExpressions; +using BitmapFont = MonoGame.Extended.BitmapFonts.BitmapFont; namespace Blish_HUD { From 39570ed53accf1fd25b56f5bd34e4dd1470e01b9 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Mon, 16 Oct 2023 18:48:02 +0200 Subject: [PATCH 16/17] fix: support gw2 known character ranges --- Blish HUD/GameServices/ContentService.cs | 20 +++++++++++++++++++ .../Modules/Managers/ContentsManager.cs | 7 +------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Blish HUD/GameServices/ContentService.cs b/Blish HUD/GameServices/ContentService.cs index 04ecd9be1..50687c91b 100644 --- a/Blish HUD/GameServices/ContentService.cs +++ b/Blish HUD/GameServices/ContentService.cs @@ -8,6 +8,7 @@ using System.IO; using System.IO.Compression; using System.Text.RegularExpressions; +using SpriteFontPlus; using BitmapFont = MonoGame.Extended.BitmapFonts.BitmapFont; namespace Blish_HUD { @@ -59,6 +60,25 @@ public static void Load() { private IDataReader _audioDataReader; + internal static CharacterRange GeneralPunctuation = new CharacterRange('\u2000', '\u206F'); + internal static CharacterRange Arrows = new CharacterRange('\u2190', '\u21FF'); + internal static CharacterRange MathematicalOperators = new CharacterRange('\u2200', '\u22FF'); + internal static CharacterRange BoxDrawing = new CharacterRange('\u2500', '\u2570'); + internal static CharacterRange GeometricShapes = new CharacterRange('\u25A0', '\u25FF'); + internal static CharacterRange MiscellaneousSymbols = new CharacterRange('\u2600', '\u26FF'); + + internal static readonly CharacterRange[] Gw2CharacterRange = { + CharacterRange.BasicLatin, + CharacterRange.Latin1Supplement, + CharacterRange.LatinExtendedA, + GeneralPunctuation, + Arrows, + MathematicalOperators, + BoxDrawing, + GeometricShapes, + MiscellaneousSymbols + }; + private BitmapFont _defaultFont12; public BitmapFont DefaultFont12 => _defaultFont12 ??= GetFont(FontFace.Menomonia, FontSize.Size12, FontStyle.Regular); diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index d4a0880fc..8cc266a05 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -1,7 +1,6 @@ using Blish_HUD.Content; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Graphics; -using MonoGame.Extended.BitmapFonts; using SpriteFontPlus; using System; using System.IO; @@ -112,11 +111,7 @@ public SpriteFont GetSpriteFont(string fontPath, int fontSize, int textureSize = if (fontDataLength > 0) { using var ctx = GameService.Graphics.LendGraphicsDeviceContext(); - var bakeResult = TtfFontBaker.Bake(fontData, fontSize, textureSize, textureSize, new[] { - CharacterRange.BasicLatin, - CharacterRange.Latin1Supplement, - CharacterRange.LatinExtendedA - }); + var bakeResult = TtfFontBaker.Bake(fontData, fontSize, textureSize, textureSize, ContentService.Gw2CharacterRange); return bakeResult.CreateSpriteFont(ctx.GraphicsDevice); } From 5ad5ab482818f0e2f8141e00400296a67201e007 Mon Sep 17 00:00:00 2001 From: Andreas G <13819164+agaertner@users.noreply.github.com> Date: Wed, 3 Apr 2024 23:42:29 +0200 Subject: [PATCH 17/17] refactor: rename BitmapFont -> BitmapFontEx; revert usings --- .../Controls/_Types/CachedStringRender.cs | 2 +- .../Controls/_Types/FormattedLabelPart.cs | 2 +- .../{BitmapFont.cs => BitmapFontEx.cs} | 19 ++++++++++--------- Blish HUD/GameServices/ContentService.cs | 4 ++-- .../Modules/Managers/ContentsManager.cs | 4 ++-- Blish HUD/_Extensions/SpriteFontExtensions.cs | 13 +++++++------ 6 files changed, 23 insertions(+), 21 deletions(-) rename Blish HUD/GameServices/Content/{BitmapFont.cs => BitmapFontEx.cs} (51%) diff --git a/Blish HUD/Controls/_Types/CachedStringRender.cs b/Blish HUD/Controls/_Types/CachedStringRender.cs index 53d262a1c..4a0d6abec 100644 --- a/Blish HUD/Controls/_Types/CachedStringRender.cs +++ b/Blish HUD/Controls/_Types/CachedStringRender.cs @@ -1,9 +1,9 @@ using Blish_HUD.Content; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.BitmapFonts; using System; using System.Collections.Concurrent; -using BitmapFont = MonoGame.Extended.BitmapFonts.BitmapFont; namespace Blish_HUD.Controls { public class CachedStringRender : IDisposable { diff --git a/Blish HUD/Controls/_Types/FormattedLabelPart.cs b/Blish HUD/Controls/_Types/FormattedLabelPart.cs index 069751609..80103f3ee 100644 --- a/Blish HUD/Controls/_Types/FormattedLabelPart.cs +++ b/Blish HUD/Controls/_Types/FormattedLabelPart.cs @@ -1,7 +1,7 @@ using Blish_HUD.Content; using Microsoft.Xna.Framework; +using MonoGame.Extended.BitmapFonts; using System; -using BitmapFont = MonoGame.Extended.BitmapFonts.BitmapFont; namespace Blish_HUD.Controls { internal class FormattedLabelPart : IDisposable { diff --git a/Blish HUD/GameServices/Content/BitmapFont.cs b/Blish HUD/GameServices/Content/BitmapFontEx.cs similarity index 51% rename from Blish HUD/GameServices/Content/BitmapFont.cs rename to Blish HUD/GameServices/Content/BitmapFontEx.cs index b0a46fe6f..b55724434 100644 --- a/Blish HUD/GameServices/Content/BitmapFont.cs +++ b/Blish HUD/GameServices/Content/BitmapFontEx.cs @@ -2,38 +2,39 @@ using MonoGame.Extended.BitmapFonts; using System; using System.Collections.Generic; +using System.Linq; namespace Blish_HUD.Content { /// - /// Extends to allow disposing of its glyph lookup texture. + /// Extends to allow disposing of its glyph lookup texture. /// - public class BitmapFont : MonoGame.Extended.BitmapFonts.BitmapFont, IDisposable { + public class BitmapFontEx : BitmapFont, IDisposable { private readonly Texture2D _texture; /// - /// Creates a with the provided identifier name, glyph regions, line height, and texture to draw letters from. + /// Creates a with the provided identifier name, glyph regions, line height, and texture to draw letters from. /// /// Name to identify the font with. /// Regions of the glyphs on the texture. /// Line height of the font. /// Lookup texture to draw letters from. - public BitmapFont(string name, IEnumerable regions, int lineHeight, Texture2D texture) : base(name, regions, lineHeight) { - _texture = texture; + public BitmapFontEx(string name, IEnumerable regions, int lineHeight, Texture2D texture) : base(name, regions, lineHeight) { + _texture = texture ?? throw new ArgumentNullException(nameof(texture)); } /// - /// Creates a with the provided identifier name, glyph regions and line height. + /// Creates a with the provided identifier name, glyph regions and line height. /// /// Name to identify the font with. /// Regions of the glyphs on the texture. /// Line height of the font. - public BitmapFont(string name, IReadOnlyList regions, int lineHeight) : base(name, regions, lineHeight) { - _texture = regions[0].TextureRegion.Texture; + public BitmapFontEx(string name, IReadOnlyList regions, int lineHeight) : base(name, regions, lineHeight) { + _texture = regions?.FirstOrDefault()?.TextureRegion?.Texture ?? throw new ArgumentException($"Parameter '{nameof(regions)}' was null or empty."); } /// - /// Disposes the lookup texture of this to free memory. Renders this unusable. + /// Disposes the lookup texture of this to free memory. Renders this unusable. /// public void Dispose() { _texture?.Dispose(); diff --git a/Blish HUD/GameServices/ContentService.cs b/Blish HUD/GameServices/ContentService.cs index 50687c91b..d4c2b015f 100644 --- a/Blish HUD/GameServices/ContentService.cs +++ b/Blish HUD/GameServices/ContentService.cs @@ -3,13 +3,13 @@ using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended.BitmapFonts; +using SpriteFontPlus; using System; using System.Collections.Concurrent; using System.IO; using System.IO.Compression; using System.Text.RegularExpressions; -using SpriteFontPlus; -using BitmapFont = MonoGame.Extended.BitmapFonts.BitmapFont; namespace Blish_HUD { diff --git a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs index 8cc266a05..6e146d22e 100644 --- a/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs +++ b/Blish HUD/GameServices/Modules/Managers/ContentsManager.cs @@ -119,13 +119,13 @@ public SpriteFont GetSpriteFont(string fontPath, int fontSize, int textureSize = } /// - /// Loads a from a TrueTypeFont (*.ttf) file. + /// Loads a from a TrueTypeFont (*.ttf) file. /// /// The path to the TTF font file. /// Size of the font. /// Sets the line height. By default, will be used. /// Size of the .
A greater fontSize results in bigger glyphs which may require more texture space. - public Content.BitmapFont GetBitmapFont(string fontPath, int fontSize, int lineHeight = 0, int textureSize = 1392) { + public BitmapFontEx GetBitmapFont(string fontPath, int fontSize, int lineHeight = 0, int textureSize = 1392) { return GetSpriteFont(fontPath, fontSize, textureSize)?.ToBitmapFont(lineHeight); } diff --git a/Blish HUD/_Extensions/SpriteFontExtensions.cs b/Blish HUD/_Extensions/SpriteFontExtensions.cs index 4492bddf1..ae40e6436 100644 --- a/Blish HUD/_Extensions/SpriteFontExtensions.cs +++ b/Blish HUD/_Extensions/SpriteFontExtensions.cs @@ -1,4 +1,5 @@ -using Microsoft.Xna.Framework.Graphics; +using Blish_HUD.Content; +using Microsoft.Xna.Framework.Graphics; using MonoGame.Extended.BitmapFonts; using MonoGame.Extended.TextureAtlases; using System; @@ -8,12 +9,12 @@ namespace Blish_HUD { public static class SpriteFontExtensions { /// - /// Converts a to a . + /// Converts a to a . /// /// The to convert. - /// Line height for the . By default, will be used. - /// A as result of the conversion. - public static Content.BitmapFont ToBitmapFont(this SpriteFont font, int lineHeight = 0) { + /// Line height for the . By default, will be used. + /// A as result of the conversion. + public static BitmapFontEx ToBitmapFont(this SpriteFont font, int lineHeight = 0) { if (lineHeight < 0) { throw new ArgumentException("Line height cannot be negative.", nameof(lineHeight)); } @@ -38,7 +39,7 @@ public static Content.BitmapFont ToBitmapFont(this SpriteFont font, int lineHeig regions.Add(region); } - return new Content.BitmapFont($"{typeof(Content.BitmapFont)}_{Guid.NewGuid():n}", regions, lineHeight > 0 ? lineHeight : font.LineSpacing, font.Texture); + return new BitmapFontEx($"{typeof(BitmapFontEx)}_{Guid.NewGuid():n}", regions, lineHeight > 0 ? lineHeight : font.LineSpacing, font.Texture); } }