Skip to content

Commit

Permalink
Added FNTFile
Browse files Browse the repository at this point in the history
  • Loading branch information
thesupersonic16 committed Apr 24, 2024
1 parent dbf23ab commit a6b9c14
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
1 change: 1 addition & 0 deletions DALTools/DALLib/DALLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="Exceptions\InvalidFileFormatException.cs" />
<Compile Include="Exceptions\InvalidTextureFormatException.cs" />
<Compile Include="Exceptions\STSCDisassembleException.cs" />
<Compile Include="File\FNTFile.cs" />
<Compile Include="File\FontFile.cs" />
<Compile Include="File\MAFile.cs" />
<Compile Include="File\STSC2File.cs" />
Expand Down
60 changes: 60 additions & 0 deletions DALTools/DALLib/File/FNTFile.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
8 changes: 5 additions & 3 deletions DALTools/DALLib/File/FontFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 32 additions & 4 deletions DALTools/FontEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a6b9c14

Please sign in to comment.