SDL Font

This unit contains two classes for text drawing - TSDLRasterFont and TSDLTTFFont and one class for text wrapping - TWrapManager. TSDLRasterFont and TSDLTTFFont inherits from TSDLFont so they have some same properties and methods.

Declared types and variables

TSDLTextLayout = (tlTop, tlCenter, tlBottom);
Used for vertical text alignment.
TSDLAlignment = (taLeft, taCenter, taRight);
Used for horizontal text alignment.
TSDLFontStyles = (fsBold,fsItalic,fsUnderline);
Used for TSDLFontStyle.
TSDLFontStyle = set of TSDLFontStyles;
Used for style in TSDLTTFFont.
GlobalFont: TSDLFont;
Most controls demands that you assign them font right after their creation. In order to avoid public variables like this one in every unit where you create controls I declared one here, so every unit that uses font can assign this font to controls it creates. The first font you create will be assigned to this variable. GlobalFont is assigned to TSDLComponent on its creation.

TSDLFont

So, the same properties for TSDLRasterFont and TSDLTTFFont are:

property Name : string read FName write FName;
Name of font.
property FileName: string read FFileName write FFileName;
Name of file from witch the font was loaded.
property Alignment: TSDLAlignment read FAlignment write FAlignment;
Horizontal alignment of text.
property Layout: TSDLTextLayout read FLayout write FLayout;
Vertical alignment of text.
property Color: Cardinal read FColor write SetColor;
Color of text.

And the same methods are:

procedure TextOut(Target: PSDL_Surface;X,Y: Integer;const Text: string); virtual; abstract;
Prints Text to the specified Surface at X,Y coordinates.
procedure TextRect(Target : PSDL_Surface;const Rect: TSDL_Rect;const Text: string);
Prints Text to the specified Surface. Text is clipped inside the Rect and it is align in it depending from Alignment and Layout properties.
procedure TextRectA(Target : PSDL_Surface;Rect: TSDL_Rect;X,Y: Integer;const Text: string);
Prints Text to the specified Surface at X,Y coordinates. It is clipped inside the Rect.
function TextExtent(const Text: string): TSDL_Rect; virtual; abstract;
Returns Text width and height in Result.w and Result.h.
function TextWidth(const Text: string): Integer; virtual; abstract;
Returns width of Text.
function TextHeight(const Text: string): Integer; virtual; abstract;
Returns Height of Text.
procedure LoadFromFile(const AFileName: string);
Loads a font from the specified file.
procedure LoadFromStream(Stream: TStream); virtual; abstract;
Loads a font from the specified stream.
procedure SaveToFile(const FileName: string);
Saves a font to the specified FileName.
procedure SaveToStream(Stream: TStream); virtual; abstract;
Saves a font to the specified stream. Only font data is saved, but it contains FileName so it can load font image or ttf file.
procedure SetTempAlign(AAlignment: TSDLAlignment; ALayout: TSDLTextLayout);
Saves old Alignment and Layout and then sets the new ones.
procedure RestoreAlignment;
Restores saved Alignment and Layout.

TSDLRasterFont

Use this to work with bitmap fonts in SGF - SDL Graphic Font format. Image of letters can be in any supported format, but at the end of that file special data will be added. This allows all image viewing and editing programs to be able to open that image. When you edit and save that image additional data will be lost so you have to extract it first and add it back later. For this you have a special tool - SG File Operator. You also have tools to build these fonts from ttf fonts or other bitmap font formats. See Tools. It works the same with and without OpenGL. It has three new properties:

property CharSet: string read FCharset write FCharset;
Contains the characters in order they apear in the image.
property ReplaceColor: Boolean read FReplaceColor write FReplaceColor;
If True seting new color only replaces the old one and this is faster. If False it will call SDLScreen.ModifyHueTo.
property TabSpaces: Byte read FTabSpaces write FTabSpaces;
Represents number of spaces to draw on Tab char. Default is four.

And one new method:

procedure Load(const FontName, ImageFile: string);
Loads a font from ImageFile and names it as FontName.

TSDLTTFFont

Use this to work with TTF fonts. It works the only without OpenGL. It has four new properties:

property Size: Byte read FSize write SetSize;
Size of Font.
property Style: TSDLFontStyle read FStyle write SetStyle;
Can contain none or more values of fsBold,fsItalic,fsUnderline.
Shaded: Boolean;
If this is True letters will have soft edges but nontransparent backgound.
BackColor: Cardinal;
Color of background when Shaded is True.

And one new method:

procedure Load(const FontName, AFileName: string;PointSize: Integer);
Loads a font from AFileName and names it as FontName. It also sets its size to PointSize.

TWrapManager

Use this to work with wrapped text. It wrappes text using max width text can occupy or it can simply manage text with line breaks without additional wrapping.

property TrimLines: Boolean read FTrimLines write FTrimLines;
If True it will trim spaces on the begining and the end of every line. This does not change the original text in WrapingText property. Default is True.
property WrapToWidth: Boolean read FWrapToWidth write SetWrapToWidth;
If True it does additional wrapping of every line so that text fits in specified width.
property WrapWidth: Word read FWrapWidth write SetWrapWidth;
Maximal width to which the text should be wrapped.
property WrapingText: string read FWrapingText write SetWrapingText;
Text to wrap. This is never changed by wrapping manager.
property WrapedLines: TStringList read FWrapedLines;
Wrapped lines.
property LinesCount: Integer read GetLinesCount;
Number of wrapped lines.
property OnChange: TNotifyEvent read FOnChange write FOnChange;
Occurs when WrappingText of WrapWidth change.

Methods are:

constructor Create(AFont: TSDLFont);
Creates a new wrap manager and connects AFont with it.
procedure TextOut(Target : PSDL_Surface;X,Y: Integer);
Prints wrapped text on surface Target at X,Y coordinates using AFont's current Alignment.
procedure TextRect(Target : PSDL_Surface;const Rect: TSDL_Rect);
Prints wrapped text on surface Target inside the specified rect using AFont's current Alignment and Layout.
procedure TextRectA(Target : PSDL_Surface;const Rect: TSDL_Rect;X,Y: Integer);
Prints wrapped text on surface Target inside the specified rect at the specified coordinates.