- Notifications
You must be signed in to change notification settings - Fork1
a python3 module for rasterising font glyphs into a texture atlas and kerning data and a C loader
License
golightlyb/bakefont3
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
bakefont3 is a python3 module for rasterising font glyphs into a RGBA textureatlas and exporting metrics and kerning information, and a C library forloading that information.
An included example program shows how Bakefont3 can be used for fast &high-quality OpenGL text rendering.
Pack glyphs efficiently into a texture atlas
(using example-generate.py)
Make use of kerning information
(based on example-gl.c)
Use any beautiful truetype font you want
(realtime rendering using example-gl.c)
Support any language and locale with full unicode support
(realtime rendering using example-gl.c; depends on font support)
(only partial support for vertical writing - see limitations if you can help)
- Unicode support
- customisable character ranges
- exports font metrics and kerning data as binary optimised for quick lookup
- efficient packing to reduce physical texture size
- can use all four colour channels to share space with other textures (canexport RGBA, RGB, greyscale)
- suitable for efficient real-time rendering using OpenGL shaders
- supports square and rectangle texture atlases of any size
- metrics accurate up to 1/64th of a pixel (e.g. for supersampling)
- small
.c
loader - no heavy dependencies in client software - pixel-perfect results for even the smallest text
- fonts, sizes and styles must be known and generated in advance (slow!)
- for large text, vector graphics are better
- not ideal for rotated text (decent results with supersampling?)
- does not include a layout engine - just exports the metrics that one would use(but see example simple layout in
example-gl.c
) - does not support RGBA glyphs - e.g. full colour emojis (yet)
Bakefont3 does not export vertical kerning information for vertical fonts.This can easily be fixed - please get in touch if you can help. This isn'ta technical barrier: I just don't know what the correct result looks likeand I don't have any fonts to test with.
A sample script,example-generate.py
, is provided. You may like to edit itto select the fonts, sizes and characters to generate.
$ python3 example-generate.py
This file generates the following files:
test.bf3test-rgba.pngtest-r.pngtest-g.pngtest-b.pngtest-a.png
test-rgba.png
is a texture atlas containing rasterised glyphs in each colourchannel. The other images show the individual channels, but you don't need them.
test.bf3
contains information about the fonts used, how to find charactersin the texture atlas, metrics and kerning information for laying out texton a screen.
A sample program,example.c
is provided. You may like to edit it tochange the font data it tries to load.
bakefont3.h
andbakefont3.c
are an example interface for loading andquerying bakefont3 data.
$ # Compile$ gcc -std=c99 example.c bakefont3.h bakefont3.c -lm -o example.bin# # Run$ ./example.bin example/test.bf3 example/test-rgba.png
A sample program,example-gl.c
is provided. You may like to edit it tochange the font data it tries to load.
bakefont3.h
andbakefont3.c
are an example interface for loading andquerying bakefont3 data.
$ # Compile$ gcc -std=c99 example-gl.c bakefont3.c lib/png.c lib/gl3w.c lib/utf8.c -lm -lglfw -lGL -lpng -ldl -Wall -Wextra -o example-gl.bin# # Run$ ./example-gl.bin example/test.bf3 example/test-rgba.png
- Python3 and modules Pillow, numpy, freetype-py
Example:
$ sudo apt-get install libfreetype6$ sudo pip3 install Pillow numpy freetype-py
- Roboto and Roboto Mono fonts (fromfonts.google.com)
- Arial font from Microsoft (search
msttcorefonts
)
- Maths: link with
-lm
- a C compiler (e.g. gcc, clang)
- You will also need libpng and libglfw
Example:
$ sudo apt-get install libglfw3 libglfw3-dev libpng12-0 libpng12-dev
When talking about font faces and glyphs, what do terms like ascent, descent,kerning, etc. mean?
Font sizes are given in pixels.
Typographic dpi is always 72. At 72ppi, 1pt == 1px.Convert as necessary for different devices.
What is the "26.6 fixed float" format?
This encodes aReal number as asigned 32 bit integer. Divide by 64.0 to get the Real value. Or, multiplya Real value by 64.0 and cast to a signed 32 bit integer to encode it.
One of the reasons these are used is because unlike floats, they compareprecisely - e.g. a font size of 13.5pt can be identified exactly.
(Freetype also has a "2.14 fixed float" format but you don't need to knowanything about this).
Why use all four colour channels to bake the texture atlas?
If you are using the texture atlas on its own, you gain nothing by doing thisand may as well just generate the greyscale version (bakefont3 can do that, too).
However, you sometimes want to combine several textures - like the glyphs, GUIimages, game graphics, etc. into one big RGBA texture as an optimisation toavoid texture switching on the GPU.
For example, you might extend your text rendering code to draw full-colouricons and emojis aligned with your text in the same batch.
This does depend: copyright law in the U.S protects the font, but not thetypeface. In Europe, the typeface is usually protected.
The safest and most ethical way to handle this is to think of Bakefont3 assomething that creates a derived work. Copyrighted data goes in, copyrighteddata comes out.
That means that the output of a Bakefont3 program should inherit the samecopyright and license as the input. In the case of multiple inputs by differentauthors, the output shares all these copyrights and licenses. The licensesmust not conflict.
Most open source fonts share the same one or two permissive licenses(fonts.google.com is a great place for this), so thisis usually quite simple in practice.
It's also worth knowing that many open source font licenses say that you can'tuse the same name for a derived work. Bakefont3 by design makes it easy to useyour own generic names for fonts (it's also fine to say something like"My Font based on Original Font").
Bakefont3 is free and open source software.
Did you publish software that uses Bakefont3? Did it save you time? Did itadd value to your software? If so, please considersupporting the author.
bakefont3Copyright © 2015 - 2017 Ben Golightly <golightly.ben@googlemail.com>Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.