Drawing Text

Introduction

This demonstration works with arial.ttf, arialbd.ttf and casper.ttf in the project folder. The file casper.ttf is in casper.zip (which includes a SIL Open Font License copyright statement) downloadable from https://www.behance.net/mchereda. We use this file to remind you that you are not restricted to fonts that are typically installed. Open the C:\Windows\Fonts\Arial folder with Windows Explorer and copy Arial Regular (arial.ttf) and Arial Bold (arialbd.ttf) to your project folder.

Alternatively, try using the Arial files directly with these instructions:
Font := SfmlFontCreateFromFile('C:\Windows\Fonts\arial.ttf');
Font := SfmlFontCreateFromFile('C:\Windows\Fonts\arialbd.ttf');
(We use the first of these instructions in our mouse input example).
Captured Output

Captured Output

The Code

program TextDemo;
{$Apptype Gui}
uses
  SysUtils, SfmlGraphics, SfmlSystem, SfmlWindow;
const
  WINDOW_WIDTH = 400;
  WINDOW_HEIGHT = 200;
var
  Window: TSfmlRenderWindow;
  Font: PsfmlFont;
  Text: TsfmlText;
  TextPosX: real;
  OutputImage: PsfmlImage;

begin
  Window := TSfmlRenderWindow.Create(SfmlVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32),
    AnsiString('TextDemo'), [sfTitleBar, sfClose], nil);
  Window.SetVerticalSyncEnabled(True);
  Window.Clear(SfmlBlack);
  Font := SfmlFontCreateFromFile('Casper.ttf');
  // from casper.zip which includes a SIL Open Font License
  // copyright statement downloadable from  https://www.behance.net/mchereda

  if not Assigned(Font) then
    raise Exception.Create('Font error');
  Text := TsfmlText.Create;
  Text.Font := Font;
  Text.&String := 'Right-justified Casper';
  Text.CharacterSize := 24; // in pixels, not points!
  TextPosX := WINDOW_WIDTH - Text.LocalBounds.Width;
  Text.Position := sfmlVector2f(TextPosX, 10);
  // Right justified
  TextPosX := WINDOW_WIDTH - Text.LocalBounds.Width;
  Text.Position := sfmlVector2f(TextPosX, 10);
  Window.Draw(Text);
  Font := nil;
  Font := SfmlFontCreateFromFile('arial.ttf');
  if not Assigned(Font) then
    raise Exception.Create('Font error');
  Text.Font := Font;
  Text.Color := sfmlRed;
  Text.CharacterSize := 16;
  Text.&String := 'regular Arial from arial.ttf';
  Text.Position := sfmlVector2f(5, 30);
  Window.Draw(Text);
  Text.&String := 'regular Arial made bold';
  Text.Position := sfmlVector2f(5, 60);
  Text.Style := 1;
  Window.Draw(Text);
  Font := nil;
  Font := SfmlFontCreateFromFile('arialbd.ttf');
  if not Assigned(Font) then
    raise Exception.Create('Font error');
  Text.Font := Font;
  Text.&String := 'regular Arial bold from arialbd.ttf';
  Text.Position := sfmlVector2f(5, 90);
  Window.Draw(Text);
  Text.Color := sfmlBlue;
  Text.&String := 'rotated regular Arial bold';
  Text.Position := sfmlVector2f(5, 150);
  Text.Rotate(-10);
  Window.Draw(Text);
  OutputImage := Window.Capture;
  SfmlImageSaveToFile(OutputImage, 'TextOutputImage.png');
  Window.Display;
  sfmlSleep(sfmlSeconds(15));
  halt;
end.
    
Programming - a skill for life!

How to use the Simple Fast Media Library in Lazarus