Back to Home Page

Embedded Font Generator


Embedded Font Generator is a free generator of a cross-platform ANSI C code that renders a text using OpenGL. Generated code includes everything you need to render a custom text, also it is easy to compile and link with your application. To produce this code EFGen uses bitmap files in tga format that are generated by free and easy to use Bitmap Font Generator developed by Andreas Jönsson (http://www.angelcode.com).
EFGen and the source code it generates are free and released under zlib license.

Download the latest version

Features:

Limitation:


How to use it:

  1. Create a bitmap file using Bitmap Font Generator.
    Bitmap Font Generator should generate 2 files: *.tga and *.fnt
    *.tga is a bitmap file while *.fnt is a font descriptor

    Required settings in Bitmap Font Generator (to set the supported format of the output files):

    Following screenshots show the proper settings in Bitmap Font Generator:

      

  2. Run the EFGen with the name of previously created font descriptor file (*.fnt) as a parameter
    e.g.
    efgen terminal.fnt

    Before running EFGen make sure that both files: *.tga and *.fnt are in the same directory
    EFGen should generate 2 files:
    efont.c
    efont.h
    which are the C implementation of selected font (efont is a short from embedded font)
    Please note that this embedded font includes only those glyphs selected while creating a bitmap in Bitmap Font Generator. This can optimize the code when you plan on using e.g. only digits or only upper-case letters but at the same time efont_print will skip a character if one is not embedded. Therefore make sure to select all possible characters that might be used in the final application.

  3. In your application add the header:
    #include “efont.h”

  4. At the end of OpenGL initialization section add this function to setup the embedded font:
    void efont_init(GLint window_width, GLint window_height)
    where:
    window_width, window_height - width and height of the main window (in pixels)

  5. Render custom text with this function:
    void efont_print(const char * s, GLint x, GLint y)
    where:
    s - text to render
    x, y - destination coordinates (in window coordinate system) of the bottom left corner of the first character of the text
    e.g.
    efont_print(“Hello world”, 100, 100) ;
    Please note that you can only use efont_print when the current matrix mode is set to GL_MODELVIEW

  6. At the point where your application closes free the resources adding this line:
    efont_destroy() ;

    "Hello World" sample using GLUT:

    #include <stdlib.h>
    #include <GL/glut.h>
    #include “efont.h”

    void keyboard(unsigned char key, int x, int y) ;
    void display(void) ;

    int main(int argc, char** argv)
    {
     glutInit(&argc, argv) ;
     glutInitWindowSize(300,100) ;
     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) ;
     glutCreateWindow("Embedded Font Sample") ;
     glutKeyboardFunc(&keyboard) ;
     glutDisplayFunc(&display) ;
     efont_init( glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) ) ;
     glutMainLoop() ;
     return EXIT_SUCCESS ;
    }

    void keyboard(unsigned char key, int x, int y)
    {
     switch (key)
     {
      case '\x1B':
       efont_destroy() ;
       exit(EXIT_SUCCESS) ;
       break ;
     }
    }

    void display()
    {
     glClear(GL_COLOR_BUFFER_BIT) ;
     efont_print("Hello world", 10, glutGet(GLUT_WINDOW_HEIGHT)/2) ;
     glFlush() ;
    }

    Running sample:

  7. Add efont.c to your project's build list
    You may change efont.c to efont.cpp if you are using C++.
    Also this file name can be permanently changed by editing this line:
    #define C_FILE_NAME "efont.c"
    located in efgen.h

  8. Compile your application
  9. GCC works well but other C/C++ compilers should work too.
    efont.c can be compiled to the object file separately with this simple line:
    gcc -c efont.c
    You can then link efont.o with the rest of your application


Contact info: lukas <at> newtechvisions <dot> com