To draw some text you need an instance of the TCastleFont class. To make it easy, one global instance of this class is already created for you: UIFont (part of CastleControls unit). So you can simply draw text like this:
UIFont.Print(10, 10, Yellow, 'Some text to print');
You should place such drawing code inside a render method, for example inside the OnRender event of TCastleWindowCustom or OnRender event of TCastleControlCustom or inside the overridden TUIControl.Render implementation. See the tutorial about 2D drawing for a general info about 2D rendering.
Instead of directly drawing the text, you can also use TCastleLabel that manages the drawing for you. You can customize it's font using CustomFont, FontSize and SmallFont properties. Many other UI controls (see for example unit CastleControls) descend from TUIControlFont. and thus may have their font customized, for example TCastleButton.
TCastleFont class has a lot of methods and properties.
TCastleFont is actually an abstract class representing some font that can be drawn. To create a new font, you create an instance of a non-abstract class, most often the TTextureFont class — it draws font glyphs from a texture, and can be loaded from a TTF font. There are other possible font implementations, for example TSimpleTextureFont allows to use a font drawn on an image (so you can make colorful letters, with fancy custom outline and such).
for a simple example of creating fonts.
In the basic version, you simply use
to load a font from a TTF file (or any other font format supported by the
FreeType2 library). So you construct a font like this:
MyNewFont := TTextureFont.Create(ApplicationData('MyFontFile.ttf'), 20, true);
Remember to install the FreeType2 library for this to work. On Windows,
place appropriate FreeType2 DLL alongside the exe, you can get the DLL
of the engine.
Note that you can also assign the new font as the global
so it will be by default used by all standard UI controls:
UIFont := TTextureFont.Create(ApplicationData('MyFontFile.ttf'), 20, true);
Instead of loading the font data from a TTF file, you can also provide
a TTextureFontData instance to the TTextureFont constructor. This allows to create the font data at runtime
or to use the font data embedded in a Pascal source code.
You can use the
texturefont2pascal program (compile it from
to convert a TTF file into a Pascal unit:
texturefont2pascal --size 20 MyFontFile.ttf
In response, it will create a unit called
CastleTextureFont_MyFontFile_20 with a public function:
function TextureFont_MyFontFile_20: TTextureFontData;
You can use this unit in your program, and create a font instance like this:
MyNewFont := TTextureFont.Create(TextureFont_MyFontFile_20);
The advantages of embedding a font inside a Pascal unit are:
The disadvantages are of course that you cannot easily edit the TTF anymore,
you will need to rerun the
and recompile your program to see the new font.
All font routines (printing, measuring) expect the international characters to be encoded using UTF-8. To draw the international characters (anything beyond basic English ASCII set) you also need to create a font with these characters.
When constructing TTextureFont,
you need to use the overloaded constructor with parameter
ACharacters (TUnicodeCharList). Provide there a list
of the characters (including all the possible international characters)
that you want to display. Like this:
uses ..., CasteFonts, CastleStringUtils, CastleUnicode; function CreateMyFont: TCastleFont; var Characters: TUnicodeCharList; begin Characters := TUnicodeCharList.Create; try Characters.Add(SimpleAsciiCharacters); Characters.Add('string containing all my weird chars, in UTF-8:)'); Result := TTextureFont.Create(ApplicationData('MyFontFile.ttf'), 20, true, Characters); finally FreeAndNil(Characters) end; end;
If you use
texturefont2pascal utility to embed fonts in
Pascal sources then just use it's parameter
to provide the additional (beyond simple ASCII) chars. Like this:
texturefont2pascal --size 20 MyFontFile.ttf --sample-text 'string containing all my weird chars, in UTF-8:)'
Note that you need to make sure to provide the sample characters encoded
Make sure your source code is in UTF-8 (edit it using an UTF-8
capable editor, consider adding an UTF-8 BOM,
see FPC source codepage option). If you use
texturefont2pascal then make
sure that your command-line, and/or your script interpreter, correcly
handle UTF-8 (on Linux, this should be a breeze, since everything works with UTF-8
out of the box; on modern Windows it should also work).