Skip to content

Commit

Permalink
Add SpriteFontPlus sample to create SpriteFonts at runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
Videogamers0 committed Oct 9, 2024
1 parent fc36fc1 commit 26c1604
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
22 changes: 21 additions & 1 deletion MGUI.Samples/Dialogs/Debugging/Debug1.xaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Window xmlns="clr-namespace:MGUI.Core.UI.XAML;assembly=MGUI.Core"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Left="440" Top="20" Width="350" Height="420"
Left="440" Top="20" Width="450" Height="430"
ThemeName="Debug1_Theme"
TitleText="[ToolTip=Test2][Action=Debug1_TestAction][img=SkullAndCrossbones 16x16][/Action][/ToolTip] [b]Window [ToolTip=Test1]Title[/ToolTip] Text[/b]">
<!--<Window.TitleBar>
Expand Down Expand Up @@ -159,6 +159,26 @@
</StackPanel>
</ScrollViewer>
</TabItem>
<TabItem Header="Tab #4">
<ScrollViewer>
<StackPanel Orientation="Vertical">
<TextBlock Text="Testing SpriteFonts generated at runtime by SpriteFontPlus nuget pkg:" />
<Spacer Height="20" />
<TextBlock FontFamily="Century Gothic" FontSize="12" Text="The quick brown fox jumps over the lazy dog!" />
<TextBlock FontFamily="Century Gothic" FontSize="12" Text="The quick brown fox jumps over the lazy dog!" IsBold="True" />
<TextBlock FontFamily="Century Gothic" FontSize="12" Text="The quick brown fox jumps over the lazy dog!" IsItalic="True" />
<TextBlock FontFamily="Century Gothic" FontSize="16" Text="The quick brown fox jumps over the lazy dog!" />
<TextBlock FontFamily="Century Gothic" FontSize="16" Text="The quick brown fox jumps over the lazy dog!" IsBold="True" />
<TextBlock FontFamily="Century Gothic" FontSize="16" Text="The quick brown fox jumps over the lazy dog!" IsItalic="True" />
<TextBlock FontFamily="Century Gothic" FontSize="20" Text="The quick brown fox jumps over the lazy dog!" />
<TextBlock FontFamily="Century Gothic" FontSize="20" Text="The quick brown fox jumps over the lazy dog!" IsBold="True" />
<TextBlock FontFamily="Century Gothic" FontSize="20" Text="The quick brown fox jumps over the lazy dog!" IsItalic="True" />
<!--<Button Height="60">
<TextBlock VerticalAlignment="Center" Background="Red" FontFamily="Century Gothic" FontSize="20" Text="The quick brown fox jumps over the lazy dog!" IsBold="True" />
</Button>-->
</StackPanel>
</ScrollViewer>
</TabItem>
</TabControl>

<!--<UniformGrid Name="UG1" Rows="6" Columns="4" CellSize="32,32" HeaderRowHeight="16" GridLinesVisibility="All" BG="LightBlue"
Expand Down
2 changes: 1 addition & 1 deletion MGUI.Samples/Dialogs/Debugging/Debug1.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Debug1(ContentManager Content, MGDesktop Desktop)
if (Window == null)
return;

Window.Scale = 2f;
//Window.Scale = 2f;

//Window.WindowStyle = WindowStyle.None;

Expand Down
69 changes: 69 additions & 0 deletions MGUI.Samples/Game1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
using MGUI.Shared.Helpers;
using MGUI.Shared.Input.Keyboard;
using MGUI.Shared.Rendering;
using MGUI.Shared.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using SpriteFontPlus;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -40,9 +44,74 @@ protected override void Initialize()
_graphics.PreferredBackBufferHeight = 900;
_graphics.ApplyChanges();

_spriteBatch = new SpriteBatch(GraphicsDevice);

this.MGUIRenderer = new(new GameRenderHost<Game1>(this));
this.Desktop = new(MGUIRenderer);

// Optional: Use the SpriteFontPlus library (nuget pkg: https://www.nuget.org/packages/SpriteFontPlus) to dynamically create SpriteFonts at runtime
// and add them to MGDesktop.FontManager so they can be used by MGTextBlocks/MGTextBoxes etc
try
{
const int FontBitmapWidth = 1024;
const int FontBitmapHeight = 1024;

ReadOnlyCollection<CharacterRange> FontCharacterRanges = new[]
{
CharacterRange.BasicLatin,
CharacterRange.Latin1Supplement,
CharacterRange.LatinExtendedA,
CharacterRange.Cyrillic
}.ToList().AsReadOnly();

string FontsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);

// Open a file stream for the regular, bold, and italic variants of the "Century Gothic" ttf font (you can also use the "Bold Italic" variant if you want)
// Note: If unsure of the exact file name, search "Font settings" in the windows start menu, look for the desired font family, select the desired font face, and look at the font file value
using (FileStream RegularStream = File.OpenRead(Path.Combine(FontsDirectory, "gothic.ttf")))
{
using (FileStream BoldStream = File.OpenRead(Path.Combine(FontsDirectory, "gothicb.ttf")))
{
using (FileStream ItalicStream = File.OpenRead(Path.Combine(FontsDirectory, "gothici.ttf")))
{
Dictionary<CustomFontStyles, FileStream> FileStreamsLookup = new Dictionary<CustomFontStyles, FileStream>()
{
{ CustomFontStyles.Normal, RegularStream },
{ CustomFontStyles.Bold, BoldStream },
{ CustomFontStyles.Italic, ItalicStream }
};

// Use TtfFontBaker to make a SpriteFont for each font size + font style tuple
Dictionary<SpriteFont, FontMetadata> SpriteFonts = new();

// Note: large font sizes may require a lot of memory for the spritefont file.
// Even just this example of 5 sizes and 3 variants per size (15 SpriteFonts) uses over 50MB of memory!
// Also note that SpriteFontPlus uses px heights when generating the sprite font which isn't the same as font pts.
// So for example, using an MGTextBlock with FontSize=12 will be smaller text than 12pt font that you're probably used to
ReadOnlyCollection<int> DesiredSizes = new List<int>() { 12, 14, 16, 20, 24 }.AsReadOnly();
ReadOnlyCollection<CustomFontStyles> DesiredStyles = new List<CustomFontStyles>() {
CustomFontStyles.Normal, CustomFontStyles.Bold, CustomFontStyles.Italic
}.AsReadOnly();

foreach (int FontSize in DesiredSizes)
{
foreach (CustomFontStyles FontStyle in DesiredStyles)
{
FontMetadata Metadata = new FontMetadata(FontSize, FontStyle.HasFlag(CustomFontStyles.Bold), FontStyle.HasFlag(CustomFontStyles.Italic));
SpriteFont SF = TtfFontBaker.Bake(FileStreamsLookup[FontStyle], FontSize, FontBitmapWidth, FontBitmapHeight, FontCharacterRanges).CreateSpriteFont(GraphicsDevice);
SpriteFonts.Add(SF, Metadata);
}
}

Desktop.FontManager.AddFontSet(new FontSet("Century Gothic", SpriteFonts));

// Now that the Century Gothic font set has been added to the FontManager, we can use MGTextBlocks with FontFamily="Century Gothic"
}
}
}
}
catch (Exception ex) { Debug.WriteLine(ex); }

// This is a dialog with toggle buttons to launch other dialogs
Compendium Compendium = new(Content, Desktop);
Compendium.Show();
Expand Down
1 change: 1 addition & 0 deletions MGUI.Samples/MGUI.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
<PackageReference Include="Microsoft.VisualStudio.VsixColorCompiler" Version="17.0.31709.430" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.0.1641" />
<PackageReference Include="SpriteFontPlus" Version="0.9.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MGUI.Core\MGUI.Core.csproj" />
Expand Down

0 comments on commit 26c1604

Please sign in to comment.