Page 1 of 1

Font Tool v0.1 and font formats description

Posted: Sun Jul 09, 2017 10:11 pm
by reapher
Hi, I haven't been doing too much in regards to THPS but as a curiosity I recently made a tool which can open fonts from THPS4, THUG, and THUG2, do some basic editing, and save fonts to those formats. I don't know if there is already a tool but I made this from my own research. Some of the features currently don't work like saving characters to TGA file and importing TGA files to characters, but you can currently load and view characters from a THUG2 font and save it for THPS4/THUG(same format) and vice-versa. You can also change the corresponding Ascii/keycodes to characters and the baseline which they sit on with the tool. Some features don't work yet like adding new characters and importing TGA files for characters. There are also some situations in which the tool may crash when opening, saving, and reopening fonts from different THPS versions in one session or trying to open a THUG2 font when THUG or THPS4 is the selected version or vice-versa. Right now it's an alpha tool.

The program is written in C# and requires .NET or mono runtimes to work. I found it's much easier to make complex GUIs using C# rather than C/C++ and this tool is heavily GUI based. You should also run it as Administrator or else it might not be able to open files from your Program Files directory.

Here's a demonstration of changing the trick text font in THPS4

Default THPS4 font:
thps4_default.jpg (136.73 KiB) Viewed 1819 times

THUG2 trick font in THPS4:
thug2_font_in_thps4.jpg (133.99 KiB) Viewed 1819 times

Changing the timer font in THPS4

Default THPS4 font:
thps4_default.jpg (116.54 KiB) Viewed 1818 times

THUG timer font in THPS4:
thug_font_in_thps4.jpg (109.85 KiB) Viewed 1818 times

(7.66 KiB) Downloaded 102 times

Here's the source code files and project file. I used SharpDevelop IDE:
(116.72 KiB) Downloaded 81 times

Some pics of the tool:
thps4.jpg (42.31 KiB) Viewed 1819 times
thug.jpg (42.76 KiB) Viewed 1819 times
thug2.jpg (45.54 KiB) Viewed 1819 times

The way to convert fonts from one game to another is to, for example, select THPS4 in the drop down box, load a THPS4 font file, then select THUG in the drop down box and save the font to the THUG font directory

Re: Font Tool v0.1 and font formats description

Posted: Sun Jul 09, 2017 10:28 pm
by reapher
The font files for THPS4 and THUG1 are the same format. The files for THUG2 are slightly different.

[Font Header Section]

There is a section at the beginning of the file I will call the font header.

-All of them start with 4 bytes that equals the total size of the file.
-In THUG2 there are 4 more bytes after the file length which are usually 0x00000001. This is not present in THPS4. I don't know what this value means.
-Next is 4 bytes that tell the number of characters in the file.
-Next is 4 bytes which tells the default height of the font characters.
-Next is 4 bytes which tells the maximum number of pixels any character is over the font baseline.

The THPS4/THUG font header has 16 bytes and THUG2 has 20 bytes total.

[Basic Character Descriptions]

In the next section are described the baselines and ASCII codes for each character. For each character there are:

-2 bytes which tell the baseline of the character
-2 bytes which tell the ASCII code of each character
-In THUG2 there are 2 more bytes for each character that are usually 0x0000. Maybe it supports 4 byte ASCII codes.

[Texture Section]

The next section contains a bitmap and color lookup table. The color lookup table always contains 256 32bit colors (1024 bytes) and the bitmap contains 1 byte values telling which color from the lookup table is used for each pixel. The bitmap is a single image containing all the characters separated by 1 pixel black lines. There is some info preceding the bitmap, that is the texture header and it contains:

-4 bytes telling the size of the remaining data in the file, including these 4 bytes
-2 bytes telling the width of the bitmap
-2 bytes telling the height of the bitmap
-2 bytes telling the depth of the bitmap(ex. 8 means 8 bits, or one byte per pixel)
-6 bytes of garbage data that is not used

The texture header is followed by the bitmap data. It's length is the texture width * texture height * texture depth (depth 8 means 1 byte)

The bitmap is then followed by 1024 bytes describing the color lookup table. Each color is in RGBA format and is 4 bytes. If a pixel of the bitmap has byte 0x1F it means that it is represented by color 31(1F in decimal) of the color lookup table.

[Extra Character Data]

The next section has data describing the locations of each character in the bitmap. These are in the same order as the Basic Character Data before the Texture section. Each character has extra data for:

-2 bytes telling the X position in the bitmap
-2 bytes telling the Y position in the bitmap
-2 bytes telling the character width in the bitmap
-2 bytes telling the character height in the bitmap

Re: Font Tool v0.1 and font formats description

Posted: Mon Jul 10, 2017 1:48 pm
by reapher
I also made another tool before this in C that converts THUG/THPS4 fonts to a directory of TGA files for each character and a text file that lists the characters. It's a lot more complicated to import images into the font because the positions of each character in the bitmap has to be recalculated and I haven't written code for that yet. I'll get TGA exporting into the first font tool soon since that's pretty simple but it may be a while before I make it so that the individual images can be changed. Anyway here's this other program, it's a console application so just drag and drop fnt files from THPS4/THUG onto it and it will output the individual characters as TGA images.
(5.22 KiB) Downloaded 79 times

Source code/project file (I use Codeblocks editor, and sorry the code isn't very clear, optimized or commented)
(38.4 KiB) Downloaded 77 times

Re: Font Tool v0.1 and font formats description

Posted: Sat Jul 15, 2017 10:14 am
by Demo

Re: Font Tool v0.1 and font formats description

Posted: Sun Jul 16, 2017 2:29 pm
by reapher

Just found out this also works for THPS3 fonts when the tool is in THPS4 mode