Sign in Join
Wiki copytemporary

BGRABitmap tutorial Font rendering: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11Basic text functions are available in [[TBGRABitmap class|TBGRABitmap]] objects. You can find explanations about this in [[BGRABitmap tutorial 12|tutorial 12]].
22
33Here are more advanced features.
44
55### FontRenderer property
66
77TBGRABitmap 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).
88
99You 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.
1010
1111#### Adding effects
1212
1313To improve speed and add effects, you will need TBGRATextEffectFontRenderer class, which is in BGRATextFX unit. For this, simply write:
1414
1515``` pascal
1616Uses BGRABitmap, BGRATextFX;
1717
1818var Bitmap: TBGRABitmap;
1919 Renderer: TBGRATextEffectFontRenderer;
2020
2121begin
2222 ...
2323 Renderer := TBGRATextEffectFontRenderer.Create;
2424 Bitmap.FontRenderer := Renderer;
2525```
2626
2727Note that you could directly assign the object without using an intermediary variable Renderer, but it will be useful to choose applied effects.
2828
2929- For a shadow, use the following properties: ShadowVisible, ShadowColor, ShadowOffset and ShadowRadius. A shadow is visible if ShadowVisible is True and ShadowColor is not transparent.
3030- For an outline, use OutlineVisible, OuterOutlineOnly, OutlineColor or OutlineTexture and OutlineWidth. An outline is visible if OutlineVisible is True and a texture, otherwise a non transparent color is defined. Moreover OutlineWidth must be non zero.
3131
3232For example, you can define an OnPaint event by double-clicking in the object inspector of a window and write:
3333
3434``` pascal
3535Uses BGRABitmap, BGRABitmapTypes, BGRATextFX;
3636
3737procedure TForm1.FormPaint(Sender: TObject);
3838var
3939 bmp: TBGRABitmap;
4040 renderer: TBGRATextEffectFontRenderer;
4141begin
4242 bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToRGB(clBtnFace));
4343 renderer := TBGRATextEffectFontRenderer.Create;
4444 bmp.FontRenderer := renderer;
4545 renderer.ShadowVisible := True;
4646 renderer.OutlineVisible := True;
4747 renderer.OutlineColor := BGRABlack;
4848 renderer.OuterOutlineOnly := True;
4949 bmp.FontFullHeight := 50;
5050 bmp.TextOut(5,5,'Hello world',CSSRed);
5151 bmp.Draw(Canvas,0,0);
5252 bmp.Free;
5353end;
5454```
5555
56-<figure>
57-<img src="bgratexteffectfontrendering.png" title="bgratexteffectfontrendering.png" />
58-<figcaption>bgratexteffectfontrendering.png</figcaption>
59-</figure>
56+![](/wiki-images/6/68/Bgratexteffectfontrendering.png)
6057
6158Note that antialiasing is not activated. To draw with antialiasing:
6259
6360``` pascal
6461 bmp.FontQuality:= fqFineAntialiasing;
6562```
6663
67-<figure>
68-<img src="bgratexteffectfontrendering_aa.png" title="bgratexteffectfontrendering_aa.png" />
69-<figcaption>bgratexteffectfontrendering_aa.png</figcaption>
70-</figure>
64+![](/wiki-images/9/90/Bgratexteffectfontrendering_aa.png)
7165
7266##### Using phong shading
7367
7468To have a phong shading, you will need shader. To create it:
7569
7670``` pascal
7771uses BGRAGradients;
7872var
7973 shader: TPhongShading;
8074 renderer: TBGRATextEffectFontRenderer;
8175begin
8276 shader := TPhongShading.Create;
8377 renderer := TBGRATextEffectFontRenderer.Create(shader, True); //the second parameter indicates that the shader is owned by the font renderer
8478```
8579
8680The full example, using shading, becomes:
8781
8882``` pascal
8983uses BGRABitmap, BGRABitmapTypes, BGRATextFX, BGRAGradients;
9084
9185procedure TForm1.FormPaint(Sender: TObject);
9286var bmp: TBGRABitmap;
9387 shader: TPhongShading;
9488 renderer: TBGRATextEffectFontRenderer;
9589begin
9690 bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToRGB(clBtnFace));
9791 shader := TPhongShading.Create;
9892 renderer := TBGRATextEffectFontRenderer.Create(shader,True);
9993 bmp.FontRenderer := renderer;
10094 renderer.ShadowVisible := True;
10195 bmp.FontFullHeight := 50;
10296 bmp.FontQuality:= fqFineAntialiasing;
10397 bmp.TextOut(5,5,'Hello world',CSSRed);
10498```
10599
106-<figure>
107-<img src="bgratexteffectfontrendering_shader.png" title="bgratexteffectfontrendering_shader.png" />
108-<figcaption>bgratexteffectfontrendering_shader.png</figcaption>
109-</figure>
100+![](/wiki-images/1/1e/Bgratexteffectfontrendering_shader.png)
110101
111102#### Using fonts with LazFreeType integrated in Lazarus
112103
113104Since 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. Note that in the current implementation, it is necessary to provide Arial font even if you are not using it.
114105
115106In order to define fonts, write this when creating your application:
116107
117108``` pascal
118109uses LazFreeTypeFontCollection;
119110
120111var FFontCollection: TFreeTypeFontCollection;
121112
122113procedure TForm1.FormCreate(Sender: TObject);
123114begin
124115 FFontCollection := TFreeTypeFontCollection.Create;
125116 FFontCollection.AddFile('Arial.ttf'); //supposing the file is next to the application
126117 SetDefaultFreeTypeFontCollection(FFontCollection);
127118end;
128119
129120procedure TForm1.FormDestroy(Sender: TObject);
130121begin
131122 SetDefaultFreeTypeFontCollection(nil);
132123 FreeAndNil(FFontCollection);
133124end;
134125```
135126
136127Now, to use those fonts, defined the FontRenderer property:
137128
138129``` pascal
139130uses BGRABitmap, BGRABitmapTypes, BGRAFreeType;
140131
141132procedure TForm1.FormPaint(Sender: TObject);
142133var
143134 bmp: TBGRABitmap;
144135 renderer: TBGRAFreeTypeFontRenderer;
145136begin
146137 bmp := TBGRABitmap.Create(ClientWidth,ClientHeight,ColorToRGB(clBtnFace));
147138 renderer := TBGRAFreeTypeFontRenderer.Create;
148139 bmp.FontRenderer := renderer;
149140 bmp.FontName := 'Arial';
150141 bmp.FontFullHeight:= 50;
151142 bmp.FontQuality := fqFineClearTypeRGB;
152143 bmp.TextOut(10,10,'Hello world',BGRABlack);
153144 bmp.Draw(Canvas,0,0);
154145 bmp.Free;
155146end;
156147```
157148
158-<figure>
159-<img src="bgrafreetyperenderer.png" title="bgrafreetyperenderer.png" />
160-<figcaption>bgrafreetyperenderer.png</figcaption>
161-</figure>
149+![](/wiki-images/d/d8/Bgrafreetyperenderer.png)
162150
163151You can add a shadow with Shadow properties:
164152
165153``` pascal
166154 renderer.ShadowVisible := True;
167155```
168156
169-<figure>
170-<img src="bgrafreetyperenderer_shadow.png" title="bgrafreetyperenderer_shadow.png" />
171-<figcaption>bgrafreetyperenderer_shadow.png</figcaption>
172-</figure>
157+![](/wiki-images/a/a6/Bgrafreetyperenderer_shadow.png)
173158
174159To add an outline:
175160
176161``` pascal
177162 renderer.OutlineVisible := True;
178163 renderer.OutlineColor := CSSBlack;
179164 renderer.OuterOutlineOnly := True;
180165```
181166
182-<figure>
183-<img src="bgrafreetyperenderer_outline.png" title="bgrafreetyperenderer_outline.png" />
184-<figcaption>bgrafreetyperenderer_outline.png</figcaption>
185-</figure>
167+![](/wiki-images/0/0f/Bgrafreetyperenderer_outline.png)
186168
187169##### Phong shading
188170
189171To add phong shading, you will need a shader:
190172
191173``` pascal
192174uses BGRAGradients;
193175var
194176 shader: TPhongShading;
195177 renderer: TBGRAFreeTypeFontRenderer;
196178begin
197179 shader := TPhongShading.Create;
198180 renderer := TBGRAFreeTypeFontRenderer.Create(shader, True); //shader owned by the font renderer
199181```
200182
201-<figure>
202-<img src="bgrafreetyperenderer_shader.png" title="bgrafreetyperenderer_shader.png" />
203-<figcaption>bgrafreetyperenderer_shader.png</figcaption>
204-</figure>
183+![](/wiki-images/0/0d/Bgrafreetyperenderer_shader.png)
205184
206185---
207186*From the Free Pascal wiki, page "BGRABitmap tutorial Font rendering", as it stood on 2020-04-11 after a change by Circular, out of the wiki's dump of 2024-05-26. 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](/wiki-contact).*
208187Category: BGRABitmap
209188Category: Tutorials

Back to history