Sign in Join
Wiki copytemporary

BGRABitmap tutorial 12: what changed

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

OldNewLine
11This tutorial shows how to use the text functions. For advanced features, see [[BGRABitmap tutorial Font rendering|font renderers]].
22
33### Create a new project
44
55Create a new project and add a reference to [[BGRABitmap]], the same way as in [[BGRABitmap tutorial|the first tutorial]].
66
77### Simple text
88
99You can draw a simple text like this:
1010
1111``` pascal
1212procedure TForm1.FormPaint(Sender: TObject);
1313var
1414 image: TBGRABitmap;
1515 c: TBGRAPixel;
1616begin
1717 image := TBGRABitmap.Create(ClientWidth,ClientHeight, ColorToBGRA(ColorToRGB(clBtnFace)) );
1818 c := ColorToBGRA(ColorToRGB(clBtnText)); //retrieve default text color
1919
2020 image.FontHeight := 30;
2121 image.FontAntialias := true;
2222 image.FontStyle := [fsBold];
2323 image.TextOut (5, 5, 'Hello world', c);
2424 image.SetPixel (5, 5, c);
2525
2626 image.free;
2727end;
2828```
2929
3030Here the font size is set to 30 pixels, with antialiasing. Using font antialiasing is slower but more beautiful.
3131
3232The upper-left corner of the text is at (5,5). This origin is shown with a SetPixel.
3333
34-<figure>
35-<img src="BGRATutorial12a.png" title="BGRATutorial12a.png" />
36-<figcaption>BGRATutorial12a.png</figcaption>
37-</figure>
34+![](/wiki-images/4/40/BGRATutorial12a.png)
3835
3936#### Using alignment
4037
4138Just replace the TextOut and SetPixel lines by:
4239
4340``` pascal
4441 image.TextOut(ClientWidth-5,5,'Hello world',c,taRightJustify);
4542 image.SetPixel(ClientWidth-5,5,c);
4643```
4744
4845Now the origin is on the right side of the form, and the text is aligned to the right.
4946
50-<figure>
51-<img src="BGRATutorial12b.png" title="BGRATutorial12b.png" />
52-<figcaption>BGRATutorial12b.png</figcaption>
53-</figure>
47+![](/wiki-images/6/61/BGRATutorial12b.png)
5448
5549### Rotated text
5650
5751It is also simple to draw rotated text. To do this use TextOutAngle or set FontOrientation property:
5852
5953``` pascal
6054 image.TextOutAngle(30,5,-450,'Hello world',c, taLeftJustify);
6155 image.SetPixel(30,5,c);
6256```
6357
6458The angle is in tenth of degrees and positive value means counter clock wise.
6559
66-<figure>
67-<img src="BGRATutorial12c.png" title="BGRATutorial12c.png" />
68-<figcaption>BGRATutorial12c.png</figcaption>
69-</figure>
60+![](/wiki-images/b/b5/BGRATutorial12c.png)
7061
7162Notice where the text origin is (the added pixel).
7263
7364### Word wrapped text
7465
7566There is an easy to use version of TextRect:
7667
7768``` pascal
7869 image.TextRect(rect(5,5,ClientWidth-5,ClientHeight-5),'This is a text that should be word wrapped',taCenter,tlCenter,c);
7970 image.Rectangle(rect(5,5,ClientWidth-5,ClientHeight-5),c,dmSet);
8071```
8172
8273Parameters are:
8374
8475- the bounding rectangle
8576- the text
8677- horizontal alignment
8778- vertical alignment
8879- color
8980
90-<figure>
91-<img src="BGRATutorial12d.png" title="BGRATutorial12d.png" />
92-<figcaption>BGRATutorial12d.png</figcaption>
93-</figure>
81+![](/wiki-images/c/ce/BGRATutorial12d.png)
9482
9583### Text with shadow
9684
9785You can make a text shadow with a blur effect:
9886
9987``` pascal
10088var
10189 image,txt: TBGRABitmap;
10290 grad: TBGRAGradientScanner;
10391 c: TBGRAPixel;
10492begin
10593 image := TBGRABitmap.Create(ClientWidth,ClientHeight, ColorToBGRA(ColorToRGB(clBtnFace)) );
10694 c := ColorToBGRA(ColorToRGB(clBtnText));
10795
10896 txt := TextShadow(ClientWidth,ClientHeight,'Hello world',30,c,BGRABlack,5,5,5);
10997 image.PutImage(0,0,txt,dmDrawWithTransparency);
11098 txt.Free;
11199
112100 image.Draw(Canvas,0,0,True);
113101 image.free;
114102end;
115103```
116104
117105The procedure TextShadow creates a bitmap that contains the text with a shadow. The parameters are:
118106
119107- The size of the bitmap
120108- The text
121109- Font height
122110- Font color
123111- Shadow color
124112- Shadow offset and blur size
125113
126114Do not forget to free the bitmap after using it.
127115
128-<figure>
129-<img src="BGRATutorial12e.png" title="BGRATutorial12e.png" />
130-<figcaption>BGRATutorial12e.png</figcaption>
131-</figure>
116+![](/wiki-images/b/b7/BGRATutorial12e.png)
132117
133118### Text with gradient
134119
135120Like other drawing functions, you can supply a gradient or a texture to fill the text with. Here is an example:
136121
137122``` pascal
138123uses BGRAGradientScanner;
139124
140125var
141126 image: TBGRABitmap;
142127 grad: TBGRAGradientScanner;
143128begin
144129 image := TBGRABitmap.Create(ClientWidth,ClientHeight, ColorToBGRA(ColorToRGB(clBtnFace)) );
145130
146131 grad := TBGRAGradientScanner.Create(BGRA(255,255,0),BGRA(255,0,0),gtLinear,PointF(0,0),PointF(0,35),True,True);
147132 image.FontHeight := 30;
148133 image.FontAntialias := true;
149134 image.FontStyle := [fsBold];
150135 image.TextOut(6,6,'Hello world',BGRABlack); //draw a black border
151136 image.TextOut(5,5,'Hello world',grad); //draw gradient text
152137 grad.free;
153138
154139 image.Draw(Canvas,0,0,True);
155140 image.free;
156141end;
157142```
158143
159144First a horizontal sine gradient is created, with color yellow and red. It is then used as a texture.
160145
161-<figure>
162-<img src="BGRATutorial12f.png" title="BGRATutorial12f.png" />
163-<figcaption>BGRATutorial12f.png</figcaption>
164-</figure>
146+![](/wiki-images/7/7e/BGRATutorial12f.png)
165147
166148[[BGRABitmap tutorial 11|Previous tutorial (combining transformations)]] [[BGRABitmap tutorial 13|Next tutorial (coordinate system)]]
167149
168150---
169151*From the Free Pascal wiki, page "BGRABitmap tutorial 12", as it stood on 2023-01-22 after a change by Crore, 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).*
170152Category: BGRABitmap
171153Category: Graphics

Back to history