Font modding

本页面所适用的版本可能已经过时,最后更新于1.5
Katty von Keksburg讨论 | 贡献2022年12月5日 (一) 15:30的版本 (文本替换 - 替换“[[Category:”为“[[分类:”)


Fonts are applied to textual elements in the interface. All fonts are found and should be placed in the /Hearts of Iron IV/gfx/fonts/ folder.

Creating a New Font

To create a new font you need to download a program called BMFont, which is used to generate the font page and descriptor file from a font.

Once you have BMFont installed, open it and import a font. To do this, click on the Options menu button and then the Font Settings button. Within the Font Settings menu, add your font file.

This will populate the font canvas with all the symbols supported by your chosen font. All your symbols should be highlighted for them to be included in the font file, for this reason you may choose to not include some symbols by not highlighting them, be it through the side bar or manually selecting them. This is not necessary unless you need your font to be a specific size.

Font Settings

Having imported your font, it is time to setup how you want the font to look. To do this, open the Font Settings menu again. Below are the fields you need to change:

  • Charset: set this to OEM - ANSI.
  • Size: this is the font size for your font, set this to your desired size.
  • Match char height: tick this so each character has the same height.
  • Font smoothing: tick this if your font may look overly sharp in game.
  • Outline thickness: if you want your font to have an outline, set the desired thickness here.

Export Options

Having setup your font, it is now time to setup how you want the font exported. To do this, open the Export Options menu. Below are the fields you may want to change:

  • Padding: controls the padding between each character in the font image. Only needed if you intend to manually edit the font file and don't want the characters too close together.
  • Spacing: controls the minimum space between character in the the font image. Set this to 1-1, or higher if you experience bleeding between characters.
  • Width: controls the width of the font image. Make this higher if all the characters don't fit on one image.
  • Height: controls the height of the font image. Make this higher if all the characters don't fit on one image.
  • Channels: controls how the characters are composited. Set them all to glyph unless you have specified an outline, then set the alpha channel to outline, with the rest as glyph.
  • Font descriptor: needs to be set to text.
  • Textures: should be set to .tga or .dds

Saving the Font

Having setup the font as you would like it, check that the characters all fit on one page by clicking on the Visualize button in the Options menu. If they don't you need to increase the size of the font image width and height.

Save the font.

Using a New Font

Having saved your new font, you should now possess two files, a font image file (i.e. my_font.tga) and a font descriptor (i.e. my_font.fnt). Open your font descriptor file, as you need to edit it for compatibility with Hearts of Iron IV.

Once opened, you need to change the following lines:

  • size: make the value a positive.
  • unicode: delete the key and value.
  • outline: delete the key and value if your font doesn't use an outline.
  • pages=1: delete the key and value.
  • packed=0: delete the key and value.
  • alphaChnl=0: delete the key and value.
  • redChnl=0: delete the key and value.
  • greenChnl=0: delete the key and value.
  • blueChnl=0: delete the key and value.
  • page id=0: delete the key and value.
  • file="x.tga": delete the key and value.
  • chars count=216: delete the key and value.
  • chnl=15: delete the key and value for every character.

Once you have done this, save your font descriptor file. It is now ready to be used in Hearts of Iron IV.

To make your new font available you need to add a .gfx definition for it in your mod files. Here is an example:

bitmapfonts = { 
    bitmapfont = {
        name = "monofonto_tooltip"
        path = "gfx/fonts/monofonto_tooltip"   
        color = 0xffffffff
    }
}

Note you can change the localization color symbols effects by including the textcolors scope within your font:

bitmapfonts = { 
    bitmapfont = {
        name = "monofonto_tooltip"
        path = "gfx/fonts/monofonto_tooltip"   
        color = 0xffffffff
        textcolors = {
           G = { 86 172 91 }
           R = { 222 86 70 }
           Y = { 238 201 35 }
           H = { 238 203 35 }
           T = { 255 255 255 }
       }
    }
}

If you want to change the map font, you need to override the tahoma_60 font definition with your own font.

Kerning

Many fonts will not be exported with kerning information included in the font descriptor file. This can lead to character overlaps ingame which can be unsightly. To fix this, you need to add kerning information to your font descriptor file.

To do this, open your font descriptor file and add a new line following this format for each kerning pair:

kerning first=<symbol position> second=<symbol position> amount=<pixel width>

A symbol's position within a font can be seen in BMFont by looking in the lower right at the status bar when hovering over a symbol in the font canvas.

The pixel amount is the space between the first and second symbol.

You may have realized that manually creating each kerning pair is not very feasible. It is best to use a programming language (such as Python) to generate the kerning lines for you. Below is an example script you can use in Python 3.x or above:

    file = open( "kerning.txt", "wt" )

    # Add the symbol positions of the blank symbol slots here.
    exclude = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 , 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26,
                27, 28, 29, 30, 31, 127, 129, 141, 143, 144, 156, 173, 181]

    for x in range( 1, 255 ):
        for y in range(1, 255):
            if x not in exclude:
                if y not in exclude:
                    file.write( "kerning first={0}  second={1}  amount=1\n".format( x, y ) )

    file.close()