Creates a BMFont compatible bitmap font of Signed-Distance Fields from a font file
npm install @mutefish0/msdf-bmfont-xml

!npm
Forked from soimy/msdf-bmfont-xml. This modified version prioritizes using the msdfgen in your PATH over the prebuilt binary included in the project.
Converts a .ttf font file into multichannel signed distance fields, then outputs packed spritesheets and an xml(.fnt} / txt(.fnt) or json representation of an AngelCode BMFont file.
Signed distance fields are a method of reproducing vector shapes from a texture representation, popularized in this paper by Valve.
This tool uses Chlumsky/msdfgen to generate multichannel signed distance fields to preserve corners. The distance fields are created from vector fonts, then rendered into texture pages. A BMFont object is provided for character layout. (See: BMFont format)
!Preview image
``bash`
npm install & npm run render
`bash`
npm install @mutefish0/msdf-bmfont-xml -g
Then you just need to call msdf-bmfont from console to generate font file.msdf-bmfont --help
Type in for more detail usage.
`bash
Usage: msdf-bmfont [options]
Creates a BMFont compatible bitmap font of signed distance fields from a font file
Options:
-V, --version output the version number
-f, --output-type
-o, --filename
font filename always set to font-face name
-s, --font-size
-i, --charset-file
-m, --texture-size
-p, --texture-padding
-b, --border
-r, --distance-range
-t, --field-type
-d, --round-decimal
-v, --vector generate svg vector file for debuging
-u, --reuse [file.cfg] save/create config file for reusing settings (default: false)
--smart-size shrink atlas to the smallest possible square
--pot atlas size shall be power of 2
--square atlas size shall be square
--rot allow 90-degree rotation while packing
--rtl use RTL(Arabic/Persian) charactors fix
-h, --help output usage information
`
Generate a multi-channel signed distance field font atlas with ASCII charset, font size 42, spread 3, maximum texture size 512x256, padding 1, and save out config file:
`bash`
msdf-bmfont --reuse -o path/to/atlas.png -m 512,256 -s 42 -r 3 -p 1 -t msdf path/to/font.ttf
We will get three file: atlas.0.png atlas.0.cfg & font.fnt and this is the generated atlas in the minimum pot size (256x256):
Then we want to use the old setting except a different font and use monochrome signed distance field atlas, and output an extra .svg version of atlas:
`bash`
msdf-bmfont -v -u path/to/atlas.0.cfg -t sdf -p 0 -r 8 path/to/anotherfont.ttf
This time we get a modified atlas.0.png with new bitmap font appended:
Not satisfied with the style? Remember we got a svg atlas!
!svg
How about fire up some graphic editor and add some neat effect and lay on the output atlas?
`bash`
npm install msdf-bmfont-xml
Writing the distance fields and font data to disk:
`js
const generateBMFont = require('msdf-bmfont-xml');
const fs = require('fs');
generateBMFont('Some-Font.ttf', (error, textures, font) => {
if (error) throw error;
textures.forEach((texture, index) => {
fs.writeFile(texture.filename, texture.texture, (err) => {
if (err) throw err;
});
});
fs.writeFile(font.filename, font.data, (err) => {
if (err) throw err;
});
});
`
Generating a single channel signed distance field with a custom character set:
`js
const generateBMFont = require('msdf-bmfont');
const opt = {
charset: 'ABC.ez_as-123!',
fieldType: 'sdf'
};
generateBMFont('Some-Font.ttf', opt, (error, textures, font) => {
...
});
`
#### generateBMFont(fontPath | fontBuffer, [opt], callback)
Renders a bitmap font from the font specified by fontPath or fontBuffer, with optional opt settings, triggering callback on complete.
Options:
- outputType (String)xml
- type of output font file. Defaults to xml
- a BMFont standard .fnt file which is wildly supported.json
- a JSON file compatible with Hierofilename
- (String)charset
- filename of both font file and font atlas. If omited, font face name is used. Required if font is provided as a Buffer.
- (String|Array)fontSize
- the characters to include in the bitmap font. Defaults to all ASCII printable characters.
- (Number)42
- the font size at which to generate the distance field. Defaults to textureSize
- (Array[2])[512, 512]
- the dimensions of an output texture sheet, normally power-of-2 for GPU usage. Both dimensions default to texturePadding
- (Number)2
- pixels between each glyph in the texture. Defaults to border
- (Number)0
- space between glyphs textures & edge. Defaults to fieldType
- (String)msdf
- what kind of distance field to generate. Defaults to . Must be one of:msdf
- Multi-channel signed distance fieldsdf
- Monochrome signed distance fieldpsdf
- monochrome signed pseudo-distance fielddistanceRange
- (Number)3
- the width of the range around the shape between the minimum and maximum representable signed distance in pixels, defaults to roundDecimal
- (Number)xml
- rounded digits of the output font metics. For output, roundDecimal: 0 recommended.vector
- (Boolean)false
- output a SVG Vector file for debugging. Defauts to smart-size
- (Boolean)false
- shrink atlas to the smallest possible square. Default: pot
- (Boolean)false
- output atlas size shall be power of 2. Default: square
- (Boolean)false
- output atlas size shall be square. Default: rot
- (Boolean)false
- allow 90-degree rotation while packing. Default: rtl
- (Boolean)false
- use RTL(Arabic/Persian) charators fix. Default:
The callback is called with the arguments (error, textures, font)
- error on success will be null/undefinedtextures
- an array of js objects of texture spritesheet.textures[index].filename
- Spritesheet filenametextures[index].texture
- Image Buffers, containing the PNG data of one texture sheetfont
- an object containing the BMFont data, to be used to render the fontfont.filename
- font filenamefont.data
- stringified xml\json data to be written to disk
Since opt is optional, you can specify callback` as the second argument.
MIT, see LICENSE.md for details.