| 1 |
{ |
| 2 |
Copyright 2016-2021 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Demo of using fonts (TCastleFont and TCastleBitmapFont) to draw text over an image (TCastleImage). |
| 17 |
Running this program will generate file font_draw_over_image_output.png |
| 18 |
in the current directory where the text is drawn. |
| 19 |
|
| 20 |
Of course, you could use the resulting TCastleImage instance in more ways. |
| 21 |
E.g. load it to TDrawableImage and display it. This would be an alternative way |
| 22 |
to display text: instead of drawing it on screen, you can draw it on an image, |
| 23 |
and then display the image. |
| 24 |
|
| 25 |
See also text_tests demo, that draws text directly on screen, |
| 26 |
and instead of the "PrintStrings" calls it uses TCastleLabel components. } |
| 27 |
|
| 28 |
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif} |
| 29 |
|
| 30 |
program font_draw_over_image; |
| 31 |
|
| 32 |
uses SysUtils, Classes, |
| 33 |
CastleControls, CastleFonts, CastleImages, CastleColors, CastleVectors, |
| 34 |
CastleFilesUtils, CastleLog, CastleOpenDocument, CastleRectangles; |
| 35 |
|
| 36 |
const |
| 37 |
DefaultLineSpacing = 2; |
| 38 |
DefaultAlignment = hpMiddle; |
| 39 |
var |
| 40 |
Image: TCastleImage; |
| 41 |
MyFont: TCastleFont; |
| 42 |
MyBitmapFont: TCastleBitmapFont; |
| 43 |
begin |
| 44 |
{ image where the rendering will be done } |
| 45 |
|
| 46 |
// Test: destination as TRGBAlphaImage |
| 47 |
Image := TRGBAlphaImage.Create(1000, 1000); |
| 48 |
// Alternative test: destination as TRGBImage |
| 49 |
// Image := TRGBImage.Create(1000, 1000); |
| 50 |
// Alternative test: destination as TGrayscaleAlphaImage |
| 51 |
// Image := TGrayscaleAlphaImage.Create(1000, 1000); |
| 52 |
// Alternative test: destination as TGrayscaleImage |
| 53 |
// Image := TGrayscaleImage.Create(1000, 1000); |
| 54 |
try |
| 55 |
Image.Clear(Vector4Byte(100, 100, 0, 255)); |
| 56 |
|
| 57 |
MyBitmapFont := TCastleBitmapFont.Create(nil); |
| 58 |
try |
| 59 |
MyBitmapFont.Load(LoadImage('castle-data:/sonic_asalga_0.png'), 8, 12, 2, 2); |
| 60 |
MyBitmapFont.TargetImage := Image; |
| 61 |
MyBitmapFont.PrintStrings(Image.Width div 2, 100, White, |
| 62 |
['A simple test of a font from an image.', |
| 63 |
'Do cats eat bats?', |
| 64 |
'and sometimes, Do bats eat cats?', |
| 65 |
'1 + 2 + 3 = 6'], |
| 66 |
false, DefaultLineSpacing, DefaultAlignment); |
| 67 |
finally FreeAndNil(MyBitmapFont) end; |
| 68 |
|
| 69 |
MyBitmapFont := TCastleBitmapFont.Create(nil); |
| 70 |
try |
| 71 |
MyBitmapFont.Load(LoadImage('castle-data:/null_terminator_0.png'), 8, 12, 1, 1); |
| 72 |
MyBitmapFont.TargetImage := Image; |
| 73 |
MyBitmapFont.PrintStrings(Image.Width div 2, 200, Red, |
| 74 |
['Yet another label.', |
| 75 |
'With different font. Just because we can :)'], |
| 76 |
false, DefaultLineSpacing, DefaultAlignment); |
| 77 |
finally FreeAndNil(MyBitmapFont) end; |
| 78 |
|
| 79 |
MyFont := TCastleFont.Create(nil); |
| 80 |
try |
| 81 |
MyFont.Load('castle-data:/DejaVuSans.ttf', 15, true); |
| 82 |
MyFont.TargetImage := Image; |
| 83 |
MyFont.PrintStrings(Image.Width div 2, 300, White, |
| 84 |
['DejaVuSans font', |
| 85 |
'with anti-aliasing.'], |
| 86 |
false, DefaultLineSpacing, DefaultAlignment); |
| 87 |
finally FreeAndNil(MyFont) end; |
| 88 |
|
| 89 |
MyFont := TCastleFont.Create(nil); |
| 90 |
try |
| 91 |
MyFont.Load('castle-data:/DejaVuSans.ttf', 15, false); |
| 92 |
MyFont.TargetImage := Image; |
| 93 |
MyFont.PrintStrings(Image.Width div 2, 400, White, |
| 94 |
['DejaVuSans font', |
| 95 |
'without anti-aliasing.'], |
| 96 |
false, DefaultLineSpacing, DefaultAlignment); |
| 97 |
finally FreeAndNil(MyFont) end; |
| 98 |
|
| 99 |
MyFont := TCastleFont.Create(nil); |
| 100 |
try |
| 101 |
MyFont.Load('castle-data:/DejaVuSans.ttf', 30, true); |
| 102 |
MyFont.TargetImage := Image; |
| 103 |
MyFont.PrintStrings(Image.Width div 2, 500, Vector4(0.5, 0.5, 1, 1), |
| 104 |
['DejaVuSans font with anti-aliasing', |
| 105 |
'and larger size and <font color="#ff0000">HTML colors</font>.'], |
| 106 |
true, DefaultLineSpacing, DefaultAlignment); |
| 107 |
finally FreeAndNil(MyFont) end; |
| 108 |
|
| 109 |
MyFont := TCastleFont.Create(nil); |
| 110 |
try |
| 111 |
MyFont.Load('castle-data:/DejaVuSans.ttf', 30, true); |
| 112 |
MyFont.TargetImage := Image; |
| 113 |
MyFont.Outline := 2; |
| 114 |
MyFont.OutlineHighQuality := true; |
| 115 |
MyFont.OutlineColor := Red; |
| 116 |
MyFont.PrintStrings(Image.Width div 2, 600, White, |
| 117 |
['DejaVuSans font with anti-aliasing', |
| 118 |
'and larger size and outline.'], |
| 119 |
false, DefaultLineSpacing, DefaultAlignment); |
| 120 |
finally FreeAndNil(MyFont) end; |
| 121 |
|
| 122 |
MyFont := TCastleFont.Create(nil); |
| 123 |
try |
| 124 |
MyFont.Load('castle-data:/PARPG.ttf', 15, true); |
| 125 |
MyFont.TargetImage := Image; |
| 126 |
MyFont.PrintStrings(Image.Width div 2, 700, White, |
| 127 |
['Stylish "old typewriter" font', |
| 128 |
'with anti-aliasing.'], |
| 129 |
false, DefaultLineSpacing, DefaultAlignment); |
| 130 |
finally FreeAndNil(MyFont) end; |
| 131 |
|
| 132 |
MyFont := TCastleFont.Create(nil); |
| 133 |
try |
| 134 |
MyFont.Load('castle-data:/PARPG.ttf', 15, false); |
| 135 |
MyFont.TargetImage := Image; |
| 136 |
MyFont.PrintStrings(Image.Width div 2, 800, White, |
| 137 |
['Stylish "old typewriter" font', |
| 138 |
'without anti-aliasing.'], |
| 139 |
false, DefaultLineSpacing, DefaultAlignment); |
| 140 |
finally FreeAndNil(MyFont) end; |
| 141 |
|
| 142 |
MyFont := TCastleFont.Create(nil); |
| 143 |
try |
| 144 |
MyFont.Load('castle-data:/PARPG.ttf', 30, true); |
| 145 |
MyFont.TargetImage := Image; |
| 146 |
MyFont.PrintStrings(Image.Width div 2, 900, White, |
| 147 |
['Stylish "old typewriter" font', |
| 148 |
'with anti-aliasing', |
| 149 |
'and larger size.'], |
| 150 |
false, DefaultLineSpacing, DefaultAlignment); |
| 151 |
finally FreeAndNil(MyFont) end; |
| 152 |
|
| 153 |
SaveImage(Image, 'font_draw_over_image_output.png'); |
| 154 |
finally FreeAndNil(Image) end; |
| 155 |
end. |