This provides an API for working with TTF/OTF font files, in the excellant low-level graphics library pntr.
I made an example that uses pntr-font in SDL, that can be compiled for native or the web, but you can use it with any rendering library (see these rad examples.)
Currently, I am downloading schrift and adding it to the build, but eventually I should probably inline it in pntr-font.h
, for easier use.
This adds a few functions for working with ttf or otf files:
// convert a schrift font to pntr font
pntr_font* sft_to_pntr(SFT_Font* fontIn);
// load a font from a TTF/OTF file
pntr_font* pntr_load_font(const char* fileName);
// load a font from a TTF/OTF memory-buffer
pntr_font* pntr_load_font_from_memory(const unsigned char* fileData, unsigned int dataSize);
pntr has some utils you can use with all fonts, including pntr-font:
// generate an image object form some text + font
pntr_image* pntr_gen_image_text(pntr_font* font, const char* text);
// draw some text on an image with a loaded font
void pntr_draw_text(pntr_image* dst, pntr_font* font, const char* text, int posX, int posY);
// unload any font
void pntr_unload_font(pntr_font* font);
// measure text rendered with a font
int pntr_measure_text(pntr_font* font, const char* text);
pntr_vector pntr_measure_text_ex(pntr_font* font, const char* text);
// load the default ttyfont
pntr_font* pntr_load_default_font();
pntr already has support for bm & tty fonts. This library is meant to work well with those, and it should all be fairly seamless, for the user-perspective.
These are variable-width, from a 1-char high image, using color lines in between them. I made a little tool to create them from any font you have installed on your system.
// load a bmfont from a file
pntr_font* pntr_load_bmfont(const char* fileName, const char* characters);
// load a bmfont from an existing image object
pntr_font* pntr_load_bmfont_from_image(pntr_image* image, const char* characters);
// load a bmfont from memory-buffer
pntr_font* pntr_load_bmfont_from_memory(const unsigned char* fileData, unsigned int dataSize, const char* characters);
These are fixed-width fonts, in a grid, from an image.
// load a ttyfont from a file
pntr_font* pntr_load_ttyfont(const char* fileName, int glyphWidth, int glyphHeight, const char* characters);
// load a ttyfont from an existing image object
pntr_font* pntr_load_ttyfont_from_image(pntr_image* image, int glyphWidth, int glyphHeight, const char* characters);
// load a ttyfont from memory-buffer
pntr_font* pntr_load_ttyfont_from_memory(const unsigned char* fileData, unsigned int dataSize, int glyphWidth, int glyphHeight, const char* characters);
You can build the example for native, like this:
cd example
cmake -B build .
make -C build
./build/pntr-font-demo
You can build demo for web like this, after you have run cmake -B build .
:
# might need to do this for lingering old native builds
rm -rf example/build
cd example
emcmake cmake -B build .
emmake make -C build
Note: This is currently not working due to
error: static declaration of 'reallocarray' follows non-static declaration
error with schrift