Skip to content

Commit

Permalink
Merge pull request #913 from agaertner/feature/runtime-fonts
Browse files Browse the repository at this point in the history
Loading BitmapFonts at runtime
  • Loading branch information
dlamkins authored Apr 3, 2024
2 parents e609b90 + 5ad5ab4 commit 8ee73af
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 268 deletions.
1 change: 1 addition & 0 deletions Blish HUD/Blish HUD.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
<PackageReference Include="Newtonsoft.Json" Version="[13.0.1]" />
<PackageReference Include="NLog" Version="4.6.7" PrivateAssets="all" />
<PackageReference Include="SemanticVersioning" Version="1.2.2" PrivateAssets="all" />
<PackageReference Include="SpriteFontPlus.MonoGame" Version="0.7.0.22" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.14.1" PrivateAssets="all" />
<PackageReference Include="System.Resources.Extensions" Version="6.0.0" />
<PackageReference Include="protobuf-net" Version="3.0.101" PrivateAssets="all" />
Expand Down
167 changes: 0 additions & 167 deletions Blish HUD/Controls/Book.cs

This file was deleted.

78 changes: 0 additions & 78 deletions Blish HUD/Controls/Page.cs

This file was deleted.

9 changes: 3 additions & 6 deletions Blish HUD/Controls/_Types/CachedStringRender.cs
Original file line number Diff line number Diff line change
@@ -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;

namespace Blish_HUD.Controls {
public class CachedStringRender : IDisposable {
Expand Down
5 changes: 2 additions & 3 deletions Blish HUD/Controls/_Types/FormattedLabelPart.cs
Original file line number Diff line number Diff line change
@@ -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;

namespace Blish_HUD.Controls {
internal class FormattedLabelPart : IDisposable {
Expand Down
43 changes: 43 additions & 0 deletions Blish HUD/GameServices/Content/BitmapFontEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended.BitmapFonts;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Blish_HUD.Content {
/// <summary>
/// Extends <see cref="BitmapFont"/> to allow disposing of its glyph lookup texture.
/// </summary>
public class BitmapFontEx : BitmapFont, IDisposable {

private readonly Texture2D _texture;

/// <summary>
/// Creates a <see cref="BitmapFontEx"/> with the provided identifier name, glyph regions, line height, and texture to draw letters from.
/// </summary>
/// <param name="name">Name to identify the font with.</param>
/// <param name="regions">Regions of the glyphs on the <c>texture</c>.</param>
/// <param name="lineHeight">Line height of the font.</param>
/// <param name="texture">Lookup texture to draw letters from.</param>
public BitmapFontEx(string name, IEnumerable<BitmapFontRegion> regions, int lineHeight, Texture2D texture) : base(name, regions, lineHeight) {
_texture = texture ?? throw new ArgumentNullException(nameof(texture));
}

/// <summary>
/// Creates a <see cref="BitmapFontEx"/> with the provided identifier name, glyph regions and line height.
/// </summary>
/// <param name="name">Name to identify the font with.</param>
/// <param name="regions">Regions of the glyphs on the <c>texture</c>.</param>
/// <param name="lineHeight">Line height of the font.</param>
public BitmapFontEx(string name, IReadOnlyList<BitmapFontRegion> regions, int lineHeight) : base(name, regions, lineHeight) {
_texture = regions?.FirstOrDefault()?.TextureRegion?.Texture ?? throw new ArgumentException($"Parameter '{nameof(regions)}' was null or empty.");
}

/// <summary>
/// Disposes the lookup texture of this <see cref="BitmapFontEx"/> to free memory. Renders this <see cref="BitmapFontEx"/> unusable.
/// </summary>
public void Dispose() {
_texture?.Dispose();
}
}
}
32 changes: 26 additions & 6 deletions Blish HUD/GameServices/ContentService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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 SpriteFontPlus;
using System;
using System.Collections.Concurrent;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;

namespace Blish_HUD {

Expand Down Expand Up @@ -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);

Expand Down
Loading

0 comments on commit 8ee73af

Please sign in to comment.