From a6b9c141583a8b8fa7eba6d5845775cbee45e987 Mon Sep 17 00:00:00 2001 From: SuperSonic16 <25761885+thesupersonic16@users.noreply.github.com> Date: Wed, 24 Apr 2024 10:04:44 +1000 Subject: [PATCH] Added FNTFile --- DALTools/DALLib/DALLib.csproj | 1 + DALTools/DALLib/File/FNTFile.cs | 60 ++++++++++++++++++++++++++ DALTools/DALLib/File/FontFile.cs | 8 ++-- DALTools/FontEditor/MainWindow.xaml.cs | 36 ++++++++++++++-- 4 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 DALTools/DALLib/File/FNTFile.cs diff --git a/DALTools/DALLib/DALLib.csproj b/DALTools/DALLib/DALLib.csproj index 83a57b5..14ca9e3 100644 --- a/DALTools/DALLib/DALLib.csproj +++ b/DALTools/DALLib/DALLib.csproj @@ -59,6 +59,7 @@ + diff --git a/DALTools/DALLib/File/FNTFile.cs b/DALTools/DALLib/File/FNTFile.cs new file mode 100644 index 0000000..1b86698 --- /dev/null +++ b/DALTools/DALLib/File/FNTFile.cs @@ -0,0 +1,60 @@ +using DALLib.Exceptions; +using DALLib.IO; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Runtime.Remoting.Messaging; +using System.Text; +using System.Threading.Tasks; + +namespace DALLib.File +{ + public class FNTFile : FileBase + { + + public FontFile FontCode { get; set; } + public TEXFile FontTexture { get; set; } + + public override void Load(ExtendedBinaryReader reader, bool keepOpen = false) + { + string tableSig = reader.ReadDALSignature("Table"); + if (tableSig != "Table" && tableSig.Length <= 4) + throw new SignatureMismatchException("Table", tableSig); + + int textureOffset = reader.ReadInt32(); + + FontCode = new FontFile(); + FontTexture = new TEXFile(); + + FontCode.WidthScale = -1; + FontCode.HeightScale = -1; + FontCode.MonospaceOnly = true; + FontCode.Load(reader, true); + + // Load texture + reader.JumpTo(textureOffset); + FontTexture.Load(reader, true); + + // Set scale size + FontCode.WidthScale = (float)FontCode.CharacterHeight / FontTexture.SheetWidth; + FontCode.HeightScale = (float)FontCode.CharacterHeight / FontTexture.SheetHeight; + } + + public override void Save(ExtendedBinaryWriter writer) + { + writer.WriteDALSignature("Table", false); + + writer.AddOffset("texture"); + + // Code + FontCode.WidthScale = -1; + FontCode.HeightScale = -1; + FontCode.Save(writer); + + // Texture + writer.FillInOffset("texture"); + FontTexture.Save(writer); + } + } +} diff --git a/DALTools/DALLib/File/FontFile.cs b/DALTools/DALLib/File/FontFile.cs index 8226224..01e8740 100644 --- a/DALTools/DALLib/File/FontFile.cs +++ b/DALTools/DALLib/File/FontFile.cs @@ -38,9 +38,11 @@ public override void Load(ExtendedBinaryReader reader, bool keepOpen = false) // The amount of characters defined int characterCount = reader.ReadInt32(); // Unknown - WidthScale = reader.ReadSingle(); - HeightScale = reader.ReadSingle(); - + if (WidthScale == 0 || HeightScale == 0) + { + WidthScale = reader.ReadSingle(); + HeightScale = reader.ReadSingle(); + } for (int i = 0; i < characterCount; ++i) { var fontEntry = new FontEntry diff --git a/DALTools/FontEditor/MainWindow.xaml.cs b/DALTools/FontEditor/MainWindow.xaml.cs index 2de627a..2229904 100644 --- a/DALTools/FontEditor/MainWindow.xaml.cs +++ b/DALTools/FontEditor/MainWindow.xaml.cs @@ -59,7 +59,7 @@ public void UpdateBorderSize(int id) var character = FontCodeFile.Characters[id]; double x = (character.XScale + character.Kerning / UI_FontImage.Source.Width) * UI_FontImage.ActualWidth; double y = character.YScale * UI_FontImage.ActualHeight; - double w = FontCodeFile.WidthScale + character.Width * (UI_FontImage.ActualWidth / UI_FontImage.Source.Width); + double w = (FontCodeFile.WidthScale + character.Width) * (UI_FontImage.ActualWidth / UI_FontImage.Source.Width); double h = FontCodeFile.CharacterHeight * (UI_FontImage.ActualHeight / UI_FontImage.Source.Height); Borders[id].Margin = new Thickness(x, y, 0, 0); Borders[id].Width = w; @@ -185,20 +185,48 @@ public void LoadFontPCK(string path) UI_SaveButton.IsEnabled = true; } + public void LoadFontFNT(string path) + { + FilePath = path; + + FNTFile file = new FNTFile(); + file.Load(path); + + FontCodeFile = file.FontCode; + FontImageTexFile = file.FontTexture; + + UI_FontImage.Source = ImageTools.ConvertToSource(FontImageTexFile.CreateBitmap()); + + LastIndex = -1; + HoverIndex = -1; + + // Reload + ReloadUI(); + + UI_SaveButton.IsEnabled = true; + } + public void LoadFile(string path) { if (path == null) return; // Check if its a PCK - if (path.ToLowerInvariant().Contains(".pck")) + if (path.ToLowerInvariant().EndsWith(".pck")) { LoadFontPCK(path); return; } + // Check if its a FNT + if (path.ToLowerInvariant().EndsWith(".fnt")) + { + LoadFontFNT(path); + return; + } + // Check if file is valid and make sure FilePath is the .code - if (path.ToLowerInvariant().Contains("_data.tex") || path.ToLowerInvariant().Contains(".code")) + if (path.ToLowerInvariant().EndsWith("_data.tex") || path.ToLowerInvariant().EndsWith(".code")) { FilePath = path.Replace("_data.tex", ".code"); } @@ -368,7 +396,7 @@ private void UI_KTextBox_OnTextChanged(object sender, TextChangedEventArgs e) private void Button_Click(object sender, RoutedEventArgs e) { var ofd = new OpenFileDialog(); - ofd.Filter = "All Supported Files|*.code;*.pck|Font Code|*.code|PCK Archive|*.pck"; + ofd.Filter = "All Supported Files|*.code;*.pck;*.fnt|Font Code|*.code|PCK Archive|*.pck|Font Bundle|*.fnt"; if (ofd.ShowDialog() == true) { LoadFile(ofd.FileName);