ClanLib SDK

Fonts

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.

  • Default Font
  • The Default Font (provided by CL_Font ) uses CL_Font_System

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

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

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

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

    Constructing fonts

  • Freetype Font
  •   CL_FontDescription 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");
    
  • System Font
  •   CL_FontDescription desc;
      font_desc.set_typeface_name("tahoma");
      CL_Font_System system_font(gc, font_desc);
      system_font.draw_text(gc, 100, 100, "Hello World");
    
  • Vector Font
  •   CL_FontDescription desc;
      font_desc.set_typeface_name("myfont.ttf");
      CL_Font_Vector vector_font(font_desc);
      vector_font.draw_text(gc, 100, 100, "Hello World");
    
  • Sprite Font
  • These are constructed from a resource file, inside "<font name="ClanFont">"

    Inside <font> should contain <bitmap> that contains the following attributes:

    <glyphs> - The sprite description containing the glyphs. See Sprites Resources Overview

    <letters> - The glyphs contained in <glyphs>

    <monospace> - If "true", then all letters have equal width

    <spacelen> - The width of unknown glyphs. If unset, this defaults to the average of the font glyphs. If monospace is used, then this always equals the widest glyph

    The font metrics can be set using: (value = floating point value, bool = true or false)

    <height="value">

    <ascent="value">

    <descent="value">

    <internal_leading="value">

    <external_leading="value">

    <average_character_width="value">

    <max_character_width="value">

    <weight="value">

    <overhang="value">

    <digitized_aspect_x="value">

    <digitized_aspect_y="value">

    <italic="bool">

    <underlined="bool">

    <struck_out="bool">

    <fixed_pitch="bool">

      CL_Font_Sprite sprite_font(gc, "ClanFont", &app_resources);
      sprite_font.draw_text(gc, 100, 100, "Hello World");
    
    

    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

    Font Metrics Image