Generating and modifying JK2 fonts
Posted: 01 Mar 2016, 21:51
Creating new font for your mod
Get this modified version of Font Builder: https://github.com/aufau/fontbuilder/tree/jedi-knight
Compile (on Linux type `qmake && make`) and launch FontBuilder from bin/ directory.
Go to Characters tab and load "jk2-characters.txt" from main fontbuilder directory.
Go to Layout tab and set it like this:
Go to Output tab, select png image format and "Jedi Knight - fontdat" description format (select "description checkbox"):
Load your chosen font file, set size, find hinting setting you like and click "Write font".
You will get two files with .png and .fontdat extensions. Put them in fonts/ directory of your mod. You can register and use them either in cgame/ui mod, or in .menu files. For examples refer to jk2 sdk cgame/ui source code and main.menu from original assets.
If you wish to support jk2mv's high resolution fonts (coming soon), you will need to generate few more fonts in larger sizes which will together constitute a font face. If you use scales ranging from 1.0 to 1.5, I recommend following steps (just multiply your font size by them and round towards 0):
Example:
Assume you generate a font of size 19, called myfont.png and myfont.fontdat. Next step is to generate the same font with size [1.5 * 19] = 28 and call it myfont_sharp1.png, myfont_sharp1.fontdat. Then goes size 2.0 * 19 = 38, call it myfont_sharp2.png and myfont_sharp2.fontdat etc. Refer to assetsmv.pk3 for an examplary font face.
That's all, not that hard. If for some reason you need to replace existing font like I had to… you're screwed :D Fear not, I will lead you through the struggle laying ahead of you in next section.
Replacing existing font, modifying font data
These two tasks go hand in hand because in order to replace existing font you will surely need to modify font data of newly generated replacement.
First question: what kind of data does .fontdat file contain? A little C program I wrote, which you can find in fontbuilder main directory, will print .fontdat file in human-readable format. You can compile it using following command: `gcc -std=c99 jk2fontdat.c -o jk2fontdat` or with any other C compiler. Run it with .fontdat file as an argument. Example output:
Unfortunately when replacing existing font (or generating high resolution versions) you will need to modify some of these values, so your replacement has exactly the same positioning and metrics.
For this task I wrote yet another tool, that is .fontdat editor built into jk2mvmp client. You can download patched jk2mv sources from here: https://github.com/aufau/jk2mv/tree/font-editor and compile them as usual.
Prepare your mod, launch the patched jk2mvmp and find a place where you can see your font on in game. Type /font_select <name> to edit fonts/<name>.fontdat. In order to modify font data use /font_transform command, you'll be able to see it's effects instantly. Here is help text it prints, I hope you can figure out the rest yourself by trial and error:
For more details on these variables' meaning, refer to comments in jk2fontdat.c source code.
When you're done, save changes you've made using font_save command (it won't modify .pk3 archive, but rather write .fontdat file in base/fonts directory).
I highly doubt anyone will ever again need or want to go this far, so let me end this tutorial with this optimistic consideration.
Get this modified version of Font Builder: https://github.com/aufau/fontbuilder/tree/jedi-knight
Compile (on Linux type `qmake && make`) and launch FontBuilder from bin/ directory.
Go to Characters tab and load "jk2-characters.txt" from main fontbuilder directory.
Go to Layout tab and set it like this:
Go to Output tab, select png image format and "Jedi Knight - fontdat" description format (select "description checkbox"):
Load your chosen font file, set size, find hinting setting you like and click "Write font".
You will get two files with .png and .fontdat extensions. Put them in fonts/ directory of your mod. You can register and use them either in cgame/ui mod, or in .menu files. For examples refer to jk2 sdk cgame/ui source code and main.menu from original assets.
If you wish to support jk2mv's high resolution fonts (coming soon), you will need to generate few more fonts in larger sizes which will together constitute a font face. If you use scales ranging from 1.0 to 1.5, I recommend following steps (just multiply your font size by them and round towards 0):
Code: Select all
1.0 1.5 2.0 3.0 4.0
Assume you generate a font of size 19, called myfont.png and myfont.fontdat. Next step is to generate the same font with size [1.5 * 19] = 28 and call it myfont_sharp1.png, myfont_sharp1.fontdat. Then goes size 2.0 * 19 = 38, call it myfont_sharp2.png and myfont_sharp2.fontdat etc. Refer to assetsmv.pk3 for an examplary font face.
That's all, not that hard. If for some reason you need to replace existing font like I had to… you're screwed :D Fear not, I will lead you through the struggle laying ahead of you in next section.
Replacing existing font, modifying font data
These two tasks go hand in hand because in order to replace existing font you will surely need to modify font data of newly generated replacement.
First question: what kind of data does .fontdat file contain? A little C program I wrote, which you can find in fontbuilder main directory, will print .fontdat file in human-readable format. You can compile it using following command: `gcc -std=c99 jk2fontdat.c -o jk2fontdat` or with any other C compiler. Run it with .fontdat file as an argument. Example output:
Code: Select all
File: ergoec.fontdat
Global parameters:
mPointSize: 20 mHeight: 22 mAscender: 17 mDescender: 5 mKoreanHack: 0
Glyph parameters:
0x00 Height: 0 Ascent: 0 Descent: 0 Offset: 0 Width: 0 Advance: 0
0x01 Height: 0 Ascent: 0 Descent: 0 Offset: 0 Width: 0 Advance: 0
(…)
0x1f Height: 0 Ascent: 0 Descent: 0 Offset: 0 Width: 0 Advance: 0
0x20 ' ' Height: 1 Ascent: 0 Descent: 1 Offset: 0 Width: 1 Advance: 6
0x21 '!' Height: 14 Ascent: 13 Descent: 1 Offset: 1 Width: 4 Advance: 5
0x22 '"' Height: 4 Ascent: 13 Descent: -9 Offset: 0 Width: 7 Advance: 6
0x23 '#' Height: 15 Ascent: 14 Descent: 1 Offset: -1 Width: 13 Advance: 10
0x24 '$' Height: 18 Ascent: 15 Descent: 3 Offset: 1 Width: 9 Advance: 10
0x25 '%' Height: 15 Ascent: 14 Descent: 1 Offset: 0 Width: 15 Advance: 14
0x26 '&' Height: 14 Ascent: 13 Descent: 1 Offset: 0 Width: 13 Advance: 12
0x27 ''' Height: 4 Ascent: 13 Descent: -9 Offset: 0 Width: 5 Advance: 3
0x28 '(' Height: 17 Ascent: 13 Descent: 4 Offset: 1 Width: 5 Advance: 6
(…)
For this task I wrote yet another tool, that is .fontdat editor built into jk2mvmp client. You can download patched jk2mv sources from here: https://github.com/aufau/jk2mv/tree/font-editor and compile them as usual.
Prepare your mod, launch the patched jk2mvmp and find a place where you can see your font on in game. Type /font_select <name> to edit fonts/<name>.fontdat. In order to modify font data use /font_transform command, you'll be able to see it's effects instantly. Here is help text it prints, I hope you can figure out the rest yourself by trial and error:
Code: Select all
Usage: font_transform [x [y [advance [line-height [baseline ...]
Example: `font_transform 0 -1' lowers font by 1 point.
When you're done, save changes you've made using font_save command (it won't modify .pk3 archive, but rather write .fontdat file in base/fonts directory).
I highly doubt anyone will ever again need or want to go this far, so let me end this tutorial with this optimistic consideration.