Sign in Join
Wiki copytemporary

Revision 3 of 3, by WikiArchive on 11-10-2026. The page as it is now.

BGRABitmap tutorial Font rendering

Basic text functions are available in TBGRABitmap objects. You can find explanations about this in tutorial 12.

Here are more advanced features.

FontRenderer property

TBGRABitmap images has a FontRenderer property. By default, the renderer uses LCL drawing functions, eventually by drawing it bigger and applying antialiasing. This is done un TLCLFontRenderer class of BGRAText unit. However, this drawing does not allow to draw shadows or apply an outline to the text. Moreover, it is rather slow when drawing text in a rectangle (TextRect function).

You can replace the renderer by creating an object derived from TBGRACustomFontRenderer and by applying it to FontRenderer property. Note that an instance can be used for one image only, which became the owner. So you don't have to free the renderer yourself.

Adding effects

To improve speed and add effects, you will need TBGRATextEffectFontRenderer class, which is in BGRATextFX unit. For this, simply write:

pascal
Uses BGRABitmap, BGRATextFX;
    
    var Bitmap: TBGRABitmap;
        Renderer: TBGRATextEffectFontRenderer;
    
    begin
      ...
      Renderer := TBGRATextEffectFontRenderer.Create;
      Bitmap.FontRenderer := Renderer;

Note that you could directly assign the object without using an intermediary variable Renderer, but it will be useful to choose applied effects.

For example, you can define an OnPaint event by double-clicking in the object inspector of a window and write:

pascal
Uses BGRABitmap, BGRABitmapTypes, BGRATextFX;
    
    procedure TForm1.FormPaint(Sender: TObject);
    var 
      bmp: TBGRABitmap;
      renderer: TBGRATextEffectFontRenderer;
    begin
      bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToRGB(clBtnFace));
      renderer := TBGRATextEffectFontRenderer.Create;
      bmp.FontRenderer := renderer;
      renderer.ShadowVisible := True;
      renderer.OutlineVisible := True;
      renderer.OutlineColor := BGRABlack;
      renderer.OuterOutlineOnly := True;
      bmp.FontFullHeight := 50;
      bmp.TextOut(5,5,'Hello world',CSSRed);
      bmp.Draw(Canvas,0,0);
      bmp.Free;
    end;

bgratexteffectfontrendering.png

Note that antialiasing is not activated. To draw with antialiasing:

pascal
bmp.FontQuality:= fqFineAntialiasing;

bgratexteffectfontrendering aa.png

Using phong shading

To have a phong shading, you will need shader. To create it:

pascal
uses BGRAGradients;
    var
      shader: TPhongShading;
      renderer: TBGRATextEffectFontRenderer;
    begin
      shader := TPhongShading.Create;
      renderer := TBGRATextEffectFontRenderer.Create(shader, True); //the second parameter indicates that the shader is owned by the font renderer

The full example, using shading, becomes:

pascal
uses BGRABitmap, BGRABitmapTypes, BGRATextFX, BGRAGradients; 
    
    procedure TForm1.FormPaint(Sender: TObject);
    var bmp: TBGRABitmap;
      shader: TPhongShading;
      renderer: TBGRATextEffectFontRenderer;
    begin
      bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToRGB(clBtnFace));
      shader := TPhongShading.Create;
      renderer := TBGRATextEffectFontRenderer.Create(shader,True);
      bmp.FontRenderer := renderer;
      renderer.ShadowVisible := True;
      bmp.FontFullHeight := 50;
      bmp.FontQuality:= fqFineAntialiasing;
      bmp.TextOut(5,5,'Hello world',CSSRed);

bgratexteffectfontrendering shader.png

Using fonts with LazFreeType integrated in Lazarus

Since version 1.0, Lazarus has a FreeType font engine. Note that you must supply yourself files or streams that contain the fonts you want to draw. System directories and installed fonts are ignored, unless of course you explicitely add those files.

If not specified, the default font will default to one of the following families: Helvetica, Helvetica Neue, Arial, Nimbus Sans L, Microsoft Sans Serif, FreeSans, Liberation Sans, DejaVu Sans Condensed, Tahoma.

In order to define fonts, write this when creating your application:

pascal
uses LazFreeTypeFontCollection;
    
    var FFontCollection: TFreeTypeFontCollection;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      FFontCollection := TFreeTypeFontCollection.Create;
      FFontCollection.AddFile('Arial.ttf');   //supposing the file is next to the application
      SetDefaultFreeTypeFontCollection(FFontCollection);
    end;
    
    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      SetDefaultFreeTypeFontCollection(nil);
      FreeAndNil(FFontCollection);
    end;

Now, to use those fonts, defined the FontRenderer property:

pascal
uses BGRABitmap, BGRABitmapTypes, BGRAFreeType;
    
    procedure TForm1.FormPaint(Sender: TObject);
    var
      bmp: TBGRABitmap;
      renderer: TBGRAFreeTypeFontRenderer;
    begin
      bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToRGB(clBtnFace));
      renderer := TBGRAFreeTypeFontRenderer.Create;
      bmp.FontRenderer := renderer;
      bmp.FontName := 'Arial';
      bmp.FontFullHeight:= 50;
      bmp.FontQuality := fqFineClearTypeRGB;
      bmp.TextOut(10,10,'Hello world',BGRABlack);
      bmp.Draw(Canvas,0,0);
      bmp.Free;
    end;

bgrafreetyperenderer.png

You can add a shadow with Shadow properties:

pascal
renderer.ShadowVisible := True;

bgrafreetyperenderer shadow.png

To add an outline:

pascal
renderer.OutlineVisible := True;
      renderer.OutlineColor := CSSBlack;
      renderer.OuterOutlineOnly := True;

bgrafreetyperenderer outline.png

Phong shading

To add phong shading, you will need a shader:

pascal
uses BGRAGradients;
    var
      shader: TPhongShading;
      renderer: TBGRAFreeTypeFontRenderer;
    begin
      shader := TPhongShading.Create;
      renderer := TBGRAFreeTypeFontRenderer.Create(shader, True); //shader owned by the font renderer

bgrafreetyperenderer shader.png


From the Free Pascal wiki, page "BGRABitmap tutorial Font rendering", as the Internet Archive kept it on 2025-01-17, last changed on 2024-10-22. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.