Rand Stats

SDL2-ttf

zef:thundergnat

Actions Status

NAME

SDL2-ttf - FreeType interface to render text in SDL2

SYNOPSIS

use SDL2::Raw;
use SDL2-ttf;

# initialize
TTF_Init();

# set a render color
my $color = TTF_Color(255, 28, 174); # Pink

# select a font file
my $font-file = '/path/to/font/my_Font.ttf';

# open it at a certain point size (May be scaled later by SDL2)
my $font = TTF_OpenFont($font-file, 64);

# render text using the selected font and color as a SDL2 texture
my $raku = SDL_CreateTextureFromSurface(
    SDL_CreateRenderer(), TTF_Render_Blended($font, ' Raku ', $color)
);

# Clean up afterwords
TTF_CloseFont($font);
TTF_Quit();

The $raku variable now contains a rendered texture that may be used in SDL2.

DESCRIPTION

Provides a convenient interface to FreeType to assist in loading and using TrueType fonts as rendering textures in SDL2.

Needs to have the libsdl2-dev and libsdl2-ttf-dev development libraries installed.

On Debian derived distributions:

sudo apt-get install libsdl2-dev libsdl2-ttf-dev

Similar for others.

Documentation for the SDL2_ttf library, which works very similar to, but not exactly the same as these bindings: https://www.libsdl.org/projects/SDL_ttf/docs/index.html

Several routines are called slightly differently, and many are not exposed at due to the different requirements and capabilities of the Raku runtime and the C library.

Try out the ttftest.p6 script in the examples folder to get a feel for how it can be used.

Enumerations

There are a few enumerations provided.

TTF_STYLE - The various font styles that may be available and/or specified:

TTF_STYLE
Namevalue
STYLE_NORMAL0
STYLE_BOLD1
STYLE_ITALIC2
STYLE_UNDERLINE4
STYLE_STRIKETHROUGH8

TTF_HINTING - The various font hints that may be available and/or specified:

TTF_HINTING
Namevalue
HINT_NORMAL0
HINT_LIGHT1
HINT_MONO2
HINT_NONE3

Subroutines:

General

Attributes

Rendering

Rendering modes

TTF_Init()

TTF_WasInit

TTF_Quit

TTF_GetError

TTF_OpenFont($file, $ptsize)

TTF_OpenFontIndex($file, $ptsize, $index)

TTF_CloseFont($font)

TTF_GetFontStyle($font)

TTF_GetFontStyles($font)

TTF_SetFontStyle($font, $style)

TTF_GetFontOutline($font)

TTF_SetFontOutline($font, $outline)

TTF_GetFontHinting($font)

TTF_SetFontHinting($font, $hint)

TTF_GetFontKerning($font)

TTF_SetFontKerning($font, $allowed)

TTF_FontHeight($font)

TTF_FontAscent($font)

TTF_FontDescent($font)

TTF_FontLineSkip($font)

TTF_FontFaces($font)

TTF_FontFaceIsFixedWidth($font)

TTF_FontFaceFamilyName($font)

TTF_FontFaceStyleName($font)

TTF_GlyphIsProvided($font, $character or $ordinal)

TTF_GlyphMetrics($font, $character)

Glyph Metrics explained: (image copyright libsdl.org)

Font Metrics

TTF_GetTextSize($font, $text)

TTF_Color($red, $green, $blue, $alpha = 255)

TTF_Render_Solid($font, $text, $fg)

TTF_Render_Shaded($font, $text, $fg, $bg)

TTF_Render_Blended($font, $text, $fg)

TTF_Render_Blended_Wrapped($font, $text, $fg, $length)

AUTHOR

SDL2_ttf library: Sam Lantinga

Raku bindings: Steve Schulze (thundergnat)

COPYRIGHT AND LICENSE

Copyright 2020 Steve Schulze (thundergnat)

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.