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, each with it's own advantages and disadvantages

  • Default Font
  • The Default Font (provided by CL_Font ) uses the current ClanLib default font provider. Currently this is CL_Font_Bitmap

  • Native Font
  • The Native Font (provided by CL_Font_Native ) uses the operating system font, stored in a pixel buffer ( CL_PixelBuffer ). On Windows, this is the standard window font. On Linux, this is a FreeType Font )

  • Freetype Font
  • The Freetype Font (provided by CL_Font_FreeType ) uses a truetype font, stored in a pixel buffer ( CL_PixelBuffer ).

  • Texture Font
  • The texture font (provided by CL_Font_Texture ) uses a font based on a pixel buffer (for example, a png file). This font imports the pixel buffer, and stores the result a texture group CL_TextureGroup. The Texture Font is faster than the Native Font and the Freetype Font. You can also import a Native font, which can be used to provide missing font glpyhs.

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

    Constructing fonts

    All fonts use a common font description class CL_FontDescription that is passed to the constructor of the desired font

  • Native Font
  • 	CL_FontDescription desc;
    	font_desc.set_typeface_name("tahoma");
    	CL_Font_Native native_font(gc, font_desc);
    	native_font.draw_text(gc, 100, 100, "Hello World");
    
  • Freetype Font
  •   CL_FontDescription desc;
      font_desc.set_typeface_name("myfont.ttf");
      CL_Font_Freetype freetype_font(font_desc);
      freetype_font.draw_text(gc, 100, 100, "Hello World");
    
  • Texture Font
  •   // To use a native font as CL_Textures's
    
      CL_FontDescription desc;
      font_desc.set_typeface_name("tahoma");
      CL_Font_Texture texture_font(gc, font_desc);
      texture_font.draw_text(gc, 100, 100, "Hello World");
    
      // To use a font from a png
    
      CL_FontDescription desc;
      font_desc.set_typeface_name("tahoma");
      CL_Font_Texture texture_font(gc, &pixel_buffer_font, font_positions);
      texture_font.load_font(gc, font_desc);	// Use tahoma for unknown glyphs
      texture_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");
    

    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_FontDescription font_desc;
    	font_desc.set_typeface_name("GUI Font Name");
    	font_desc.set_height(-21);
    	font_desc.set_weight(700);
    	CL_Font_Texture font_texture(gc, font_desc);
    	font_desc.set_typeface_name("Custom Font Name (usually the same)");
    	gui->set_named_font(font_texture, font_desc);