MainDocs:Fonts

From ClanLib Game SDK

A Font is a collection of images that can be used to represent text on a screen.

There are various font classes that you can use.

The Default Font (provided by CL_Font ) uses CL_Font_System


The Freetype Font (provided by CL_Font_FreeType ) uses a truetype font, stored in a texture group ( CL_TextureGroup ).


The system font (provided by CL_Font_System ) uses the operating system font, stored in a texture group ( CL_TextureGroup ).


The vector font (provided by CL_Font_Vector ) uses a truetype font, stored as triangle primitives (drawn using CL_PrimitivesArray )


The sprite font (provided by CL_Font_Sprite ) uses a font stored as a CL_Sprite.


Constructing fonts

 
  CL_FontDescription font_desc;
  font_desc.set_typeface_name("myfont.ttf");
  CL_Font_Freetype freetype_font(gc, font_desc);
  freetype_font.draw_text(gc, 100, 100, "Hello World");

Via resources...

 
<font name="ClanFont">
    <freetype file="myfont.ttf" height="14" average_width="0" anti_alias="true" subpixel="true"/>
</font>
 
  CL_FontDescription font_desc;
  font_desc.set_typeface_name("tahoma");
  CL_Font_System system_font(gc, font_desc);
  system_font.draw_text(gc, 100, 100, "Hello World");
 
  CL_FontDescription font_desc;
  font_desc.set_typeface_name("myfont.ttf");
  CL_Font_Vector vector_font(font_desc);
  vector_font.draw_text(gc, 100, 100, "Hello World");
 
  CL_Font_Sprite sprite_font(gc, "ClanFont", &app_resources);
  sprite_font.draw_text(gc, 100, 100, "Hello World");

These are constructed from a resource file, using <font name="ClanFont">. Inside the <font> tag you need a <bitmap> tag that contains the following attributes:

ClanGUI and Fonts

If you want ClanGUI to use a custom font, instead of a native font, you will first need to register the font as follows

 
CL_Font_Sprite sprite_font(gc, "ClanFont", &app_resources);
CL_FontDescription font_desc;
font_desc.set_typeface_name("GUI Font Name");
font_desc.set_height(32);
font_desc.set_weight(400);
gui->register_font(sprite_font, font_desc);

Font Metrics

All ClanLib fonts have a description detailing how the font glphs should be positioned. This is called CL_FontMetrics