pods::SDL::TTF(3pm)

🌸 NAME 🌸

SDL::TTF β€” True Type Font functions (libfreetype). Yep bestie, this is the pods::SDL::TTF(3pm) page, and we are getting into font magic. πŸ¦‹

🏷️ CATEGORY 🏷️

TTF

✨ CONSTANTS ✨

Okay so the constants are exported by default. If that is not your vibe, dodge it with:

use SDL::TTF ();

and grab them directly like:

SDL::TTF::TTF_HINTING_NORMAL;

Available constants for hinting:

  • TTF_HINTING_NORMAL
  • TTF_HINTING_LIGHT
  • TTF_HINTING_MONO
  • TTF_HINTING_NONE

Available constants for style:

  • TTF_STYLE_NORMAL
  • TTF_STYLE_BOLD
  • TTF_STYLE_ITALIC
  • TTF_STYLE_UNDERLINE
  • TTF_STYLE_STRIKETHROUGH

πŸ’… METHODS πŸ’…

🧚 General methods 🧚

linked_version

my $version = SDL::TTF::linked_version();

This gives you the SDL::Version object that the SDL_ttf lib is using on your system. No prior initialization needs to be done before this function is called, periodt. ✨

Example:

use SDL::TTF;
use SDL::Version;

my $version = SDL::TTF::linked_version();

printf("got version: %d.%d.%d\n", $version->major, $version->minor, $version->patch);

compile_time_version

my $version = SDL::TTF::compile_time_version();

This gives you the SDL::Version object that SDL_ttf was present as at compile time.

init

my $success = SDL::TTF::init();

Initialize the truetype font API. This must be called before using other functions in this library, except SDL::TTF::was_init and SDL::TTF::linked_version. SDL does not have to be initialized before this call.

Returns: 0 on success, -1 on any error.

was_init

my $was_init = SDL::TTF::was_init();

Query the initialization status of the truetype font API. You may, of course, use this before SDL::TTF::init to avoid initializing twice in a row. Or use this to determine if you need to call SDL::TTF::quit.

quit

SDL::TTF::quit();

Shutdown and cleanup the truetype font API. After calling this the SDL::TTF functions should not be used, excepting SDL::TTF::was_init. You may, of course, use SDL::TTF::init to use the functionality again.

πŸŽ€ Management functions πŸŽ€

open_font

my $font = SDL::TTF::open_font($font_file, $point_size);

Load file for use as a font, at the given size. This is actually SDL::TTF::open_font_index(..., ..., $index = 0). This can load TTF, OTF and FON files.

Returns: a SDL::TTF::Font object. undef is returned on errors.

Example:

use SDL::TTF;
use SDL::TTF::Font;

my $font = SDL::TTF::open_font('arial.ttf', 24);

open_font_index

my $font = SDL::TTF::open_font($font_file, $point_size, $face_index);

This is the same as SDL::TTF::open_font, except you can specify the face index of a font file containing multiple faces. This can load TTF and FON files.

open_font_RW

my $font = SDL::TTF::open_font_RW($rwops_object, $free, $point_size);

This is the same as SDL::TTF::open_font, except you can pass an SDL::RWOps-object. If you pass true as $free, the SDL::RWOps-object will be freed by the SDL_ttf library. Don't do this, perl will free this object for you.

Example:

my $font = SDL::TTF::open_font_RW(SDL::RWOps->new_file($font_file, 'r'), 0, 24);

open_font_index_RW

my $font = SDL::TTF::open_font_index_RW($rwops_object, $free, $point_size, $face_index);

This is the same as SDL::TTF::open_font_index, except you can pass an SDL::RWOps-object. If you pass true as $free, the SDL::RWOps-object will be freed by the SDL_ttf library. Don't do this, perl will free this object for you.


πŸ’œ Attributes πŸ’œ

🌈 Global attributes 🌈

byte_swapped_unicode

SDL::TTF::byte_swapped_unicode( $bool );

This function tells SDL_ttf whether UNICODE (2 bytes per character) text is generally byteswapped. A UNICODE_BOM_NATIVE or UNICODE_BOM_SWAPPED character in a string will temporarily override this setting for the remainder of that string, however this setting will be restored for the next one. The default mode is non-swapped, native endianness of the CPU.

πŸ’… Font style πŸ’…

get_font_style

SDL::TTF::get_font_style($font);

Returns: The style as a bitmask composed of the following masks:

  • TTF_STYLE_NORMAL
  • TTF_STYLE_BOLD
  • TTF_STYLE_ITALIC
  • TTF_STYLE_UNDERLINE
  • TTF_STYLE_STRIKETHROUGH (since SDL_ttf 2.0.10)

Example:

my $style = SDL::TTF::get_font_style($font);

print("normal\n")        if $style == TTF_STYLE_NORMAL;
print("bold\n")          if $style  & TTF_STYLE_BOLD;
print("italic\n")        if $style  & TTF_STYLE_ITALIC;
print("underline\n")     if $style  & TTF_STYLE_UNDERLINE;
print("strikethrough\n") if $style  & TTF_STYLE_STRIKETHROUGH;

set_font_style

SDL::TTF::set_font_style($font, $style);

Set the rendering style of the loaded font.

Heads up bestie: TTF_STYLE_UNDERLINE may cause surfaces created by SDL::TTF::render_glyph_* functions to be extended vertically, downward only, to encompass the underline if the original glyph metrics didn't allow for the underline to be drawn below. This does not change the math used to place a glyph using glyph metrics. On the other hand TTF_STYLE_STRIKETHROUGH doesn't extend the glyph, since this would invalidate the metrics used to position the glyph when blitting, because they would likely be extended vertically upward. There is perhaps a workaround, but it would require programs to be smarter about glyph blitting math than they are currently designed for. Still, sometimes the underline or strikethrough may be outside of the generated surface, and thus not visible when blitted to the screen. In this case, you should probably turn off these styles and draw your own strikethroughs and underlines.

get_font_outline

my $outline = SDL::TTF::get_font_outline($font);

Get the current outline width of the font, in pixels. Note: at least SDL_ttf 2.0.10 needed.

set_font_outline

SDL::TTF::set_font_outline($font, $outline);

Set the outline pixel width of the loaded font. Use 0(zero) to turn off outlining. Note: at least SDL_ttf 2.0.10 needed.

πŸŽ›οΈ Font settings πŸŽ›οΈ

get_font_hinting

my $hinting = SDL::TTF::get_font_hinting($font);

Get the current hinting setting of the loaded font. Note: at least SDL_ttf 2.0.10 needed.

Returns the hinting type matching one of the following defined values:

  • TTF_HINTING_NORMAL
  • TTF_HINTING_LIGHT
  • TTF_HINTING_MONO
  • TTF_HINTING_NONE

set_font_hinting

SDL::TTF::set_font_hinting($font, $hinting);

Set the hinting of the loaded font. You should experiment with this setting if you know which font you are using beforehand, especially when using smaller sized fonts. If the user is selecting a font, you may wish to let them select the hinting mode for that font as well. Note: at least SDL_ttf 2.0.10 needed.

Example:

SDL::TTF::set_font_hinting($font, TTF_HINTING_LIGHT);

get_font_kerning

my $kerning_enabled = SDL::TTF::get_font_kerning($font);

Get the current kerning setting of the loaded font.

Returns: 0(zero) if kerning is disabled. A non-zero value is returned when enabled. The default for a newly loaded font is enabled(1).

Note: at least SDL_ttf 2.0.10 needed.

Note: This function returns wrong values: See http://bugzilla.libsdl.org/show_bug.cgi?id=973.

set_font_kerning

SDL::TTF::set_font_kerning($font, $kerning_enabled);

Set whether to use kerning when rendering the loaded font. This has no effect on individual glyphs, but rather when rendering whole strings of characters, at least a word at a time. Perhaps the only time to disable this is when kerning is not working for a specific font, resulting in overlapping glyphs or abnormal spacing within words.

Pass 0 to disable kerning, 1 to enable.

Note: at least SDL_ttf 2.0.10 needed.

πŸ“ Font metrics πŸ“

font_height

my $font_height = SDL::TTF::font_height($font);

Get the maximum pixel height of all glyphs of the loaded font. You may use this height for rendering text as close together vertically as possible, though adding at least one pixel height to it will space it so they can't touch. Remember that SDL_ttf doesn't handle multiline printing, so you are responsible for line spacing, see the SDL::TTF::font_line_skip as well.

font_ascent

my $font_ascent = SDL::TTF::font_ascent($font);

Get the maximum pixel ascent of all glyphs of the loaded font. This can also be interpreted as the distance from the top of the font to the baseline. It could be used when drawing an individual glyph relative to a top point, by combining it with the glyph's maxy metric to resolve the top of the rectangle used when blitting the glyph on the screen.

Example:

my ($minx, $maxx, $miny, $maxy, $advance) = @{ SDL::TTF::glyph_metrics($font, "\0M") };

$rect->y( $top + SDL::TTF::font_ascent($font) - $maxy );

font_descent

my $font_descent = SDL::TTF::font_descent($font);

Get the maximum pixel descent of all glyphs of the loaded font. This can also be interpreted as the distance from the baseline to the bottom of the font. It could be used when drawing an individual glyph relative to a bottom point, by combining it with the glyph's maxy metric to resolve the top of the rectangle used when blitting the glyph on the screen.

Example:

my ($minx, $maxx, $miny, $maxy, $advance) = @{ SDL::TTF::glyph_metrics($font, "\0M") };

$rect->y( $bottom - SDL::TTF::font_descent($font) - $maxy );

font_line_skip

my $font_line_skip = SDL::TTF::font_line_skip($font);

Get the recommended pixel height of a rendered line of text of the loaded font. This is usually larger than the SDL::TTF::font_height of the font.

🧚 Face attributes 🧚

font_faces

my $font_faces = SDL::TTF::font_faces($font);

Get the number of faces (sub-fonts) available in the loaded font. This is a count of the number of specific fonts (based on size and style and other typographical features perhaps) contained in the font itself.

font_face_is_fixed_width

my $font_face_is_fixed_width = SDL::TTF::font_face_is_fixed_width($font);

Test if the current font face of the loaded font is a fixed width font. Fixed width fonts are monospace, meaning every character that exists in the font is the same width, thus you can assume that a rendered string's width is going to be the result of glyph_width * string_length.

Returns: >0 if font is a fixed width font. 0 if not a fixed width font.

font_face_family_name

my $font_face_family_name = SDL::TTF::font_face_family_name($font);

Get the current font face family name from the loaded font. This information is not for every font available.

Example:

my $font = SDL::TTF::open_font('arialuni.ttf', 8);

printf("%s\n", SDL::TTF::font_face_family_name($font)); # will print "Arial Unicode MS"

SDL::TTF::font_face_style_name($font) ✨

my $font_face_style_name = SDL::TTF::font_face_style_name($font);

Grabs the current font face style name right off the loaded font, bestie. Heads up tho β€” this info isn't available for every font, so don't come for it if it's giving nothing.

Example, so you see the vibe:

my $font = SDL::TTF::open_font('arialuni.ttf', 8);

printf("%s\n", SDL::TTF::font_face_style_name($font)); # will print "Regular"

πŸ¦‹ Glyphs πŸ¦‹

SDL::TTF::glyph_is_provided($font, $unicode_char) 🫢

my $glyph_is_provided = SDL::TTF::glyph_is_provided($font, $unicode_char);

Checks whether the loaded font actually has this glyph. Like, does it exist or not, periodt.

Returns: the index of the glyph in font, or 0 for an undefined character code.

Note: you have to pass this unicode character either as UTF16/UCS-2 big endian without BOM, or with BOM as UTF16/UCS-2 big/little endian.
Note: at least SDL_ttf 2.0.10 needed β€” no cap, older versions simply don't have this.

Example:

print("We have this char!\n") if SDL::TTF::glyph_is_provided($font, "\0M");

SDL::TTF::glyph_metrics($font, $unicode_char) πŸ¦‹

my @glyph_metrics = @{ SDL::TTF::glyph_metrics($font, $unicode_char) };

Pulls the desired glyph metrics of that UNICODE char out of the loaded font πŸ’…

See also: The FreeType2 Documentation Tutorial <http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html> β€” go read it, iykyk.

Note: you have to pass this unicode character either as UTF16/UCS-2 big endian without BOM, or with BOM as UTF16/UCS-2 big/little endian.

Example:

my ($minx, $maxx, $miny, $maxy, $advance) = @{ SDL::TTF::glyph_metrics($font, "\0M") };

πŸ“ Text metrics πŸ“

SDL::TTF::size_text($font, $text) πŸŽ€

my ($width, $height) = @{ SDL::TTF::size_text($font, $text) };

Works out what surface size the LATIN1 encoded text would be if rendered with $font. And no, it doesn't actually render anything β€” no actual rendering is done β€” but it does do correct kerning so the width you get is the real deal. The height it hands back is the same one you can get using SDL::TTF::font_height. Slay.

SDL::TTF::size_utf8($font, $text) 🧚

my ($width, $height) = @{ SDL::TTF::size_utf8($font, $text) };

Same situation but for UTF8 encoded text rendered using $font. No actual rendering is done, however correct kerning is done to get the actual width. The height returned in h is the same as you can get using SDL::TTF::font_height.

Lowkey important: the first example uses the same text as in the LATIN1 example, and that's because plain ASCII is UTF8 compatible 🫢

Examples:

($width, $height) = @{ SDL::TTF::size_utf8($font, 'Hello World!') }; # plain text, if your script is in utf8 or ansi-format

# or

($width, $height) = @{ SDL::TTF::size_utf8($font, "\xE4\xBB\x8A\xE6\x97\xA5\xE3\x81\xAF") }; # utf8 hex-data

# or

use Unicode::String;
my $unicode       = utf8($data_from_somewhere);
($width, $height) = @{ SDL::TTF::size_utf8($font, $unicode->utf8) }; # utf8 via Unicode::String

SDL::TTF::size_unicode($font, $text) πŸ’œ

my ($width, $height) = @{ SDL::TTF::size_unicode($font, $text) };

Same energy, but for UNICODE encoded text rendered using $font. No actual rendering is done, however correct kerning is done to get the actual width. The height returned in h is the same as you can get using SDL::TTF::font_height.

And $text has to be one of these three, no exceptions bestie:

  • UTF16BE without BOM β€” "hello" will look like "\0h\0e\0l\0l\0o"
  • UTF16BE with BOM β€” "hello" will look like "\xFE\xFF\0h\0e\0l\0l\0o"
  • UTF16LE with BOM β€” "hello" will look like "\xFF\xFEh\0e\0l\0l\0o\0"

You may use Unicode::String for this. Iconic of it honestly.


🌈 Font Rendering 🌈

⭐ Solid ⭐

SDL::TTF::render_glyph_solid($font, $char, $color)

my $surface = SDL::TTF::render_glyph_solid($font, $char, $color);

Renders that unicode encoded char onto a brand new surface using the Solid mode, and after that you can blit this surface to your display-surface. Main character behaviour.

Note: the unicode char has to be passed exactly like for SDL::TTF::size_unicode.
Note: see space-character bug <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=374062> 😭 β€” you have to upgrade libfreetype2 to at least version 2.3.5.

SDL::TTF::render_text_solid($font, $text, $color)

my $surface = SDL::TTF::render_text_solid($font, $text, $color);

Renders the LATIN1 encoded text onto a new surface, using the Solid mode. After that you can blit this surface to your display-surface.

Note: see space-character bug <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=374062> 😭 β€” you have to upgrade libfreetype2 to at least version 2.3.5.

Example, full and verbatim:

use SDL;
use SDL::Rect;
use SDL::Video;
use SDL::Color;
use SDL::TTF;
use SDL::TTF::Font;

SDL::init(SDL_INIT_VIDEO);
SDL::TTF::init();
my $display = SDL::Video::set_video_mode(640, 480, 32, SDL_SWSURFACE);
my $font    = SDL::TTF::open_font('somefont.ttf', '24');
die 'Coudnt make font '. SDL::get_error if !$font;
my $surface = SDL::TTF::render_text_solid($font, 'Hello!', SDL::Color->new(0xFF,0xFF,0xFF));
SDL::Video::blit_surface($surface, SDL::Rect->new(0, 0, 640, 480), $display, SDL::Rect->new(10, 10, 640, 480));
SDL::Video::update_rect($display, 0, 0, 0, 0);
SDL::delay(5000);

SDL::TTF::render_utf8_solid($font, $text, $color)

my $surface = SDL::TTF::render_utf8_solid($font, $text, $color);

Renders the UTF8 encoded text onto a new surface, using the Solid mode. After that you can blit this surface to your display-surface 🫢

Note: see space-character bug <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=374062> 😭 β€” you have to upgrade libfreetype2 to at least version 2.3.5.

SDL::TTF::render_unicode_solid($font, $text, $color)

my $surface = SDL::TTF::render_unicode_solid($font, $text, $color);

Renders the unicode encoded text onto a new surface, using the Solid mode. After that you can blit this surface to your display-surface.

Note: the unicode test has to be passed exactly like for SDL::TTF::size_unicode.
Note: see space-character bug <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=374062> 😭 β€” you have to upgrade libfreetype2 to at least version 2.3.5.

πŸ“ Shaded πŸ“

SDL::TTF::render_glyph_shaded($font, $char, $color, $background_color)

my $surface = SDL::TTF::render_glyph_shaded($font, $char, $color, $background_color);

Renders the unicode encoded char onto a new surface β€” and this time the surface gets filled with $background_color. After that you can blit this surface to your display-surface πŸ’–

Note: the unicode char has to be passed exactly like for SDL::TTF::size_unicode.

SDL::TTF::render_text_shaded($font, $text, $color, $background_color)

my $surface = SDL::TTF::render_text_shaded($font, $text, $color, $background_color);

Renders the LATIN1 encoded text onto a new surface, with the surface filled with $background_color. After that you can blit this surface to your display-surface ✨

Example (transcribing it exactly as it lives in the docs, bestie):

use SDL;
use SDL::Video;
use SDL::Color;
use SDL::TTF;
use SDL::TTF::Font;

SDL::init(SDL_INIT_VIDEO);

SDL::TTF::init();

my $display = SDL::Video::set_video_mode(640, 480, 32, SDL_SWSURFACE);
my $font    = SDL::TTF::open_font('arial.ttf', '24');
my $white   = SDL::Color->new(0xFF, 0xFF, 0xFF);
my $black   = SDL::Color->new(0x00, 0x00, 0x00);
my $surface = SDL::TTF::render_text_solid($font, 'Hello!', $white, $black);

SDL::Video::blit_surface($surface, SDL::Rect->new(0, 0, 640, 480), $display, SDL::Rect->new(10, 10, 640, 480));
SDL::Video::update_rect($display, 0, 0, 0, 0);

SDL::delay(5000);

SDL::TTF::render_utf8_shaded($font, $text, $color, $background_color)

my $surface = SDL::TTF::render_utf8_shaded($font, $text, $color, $background_color);

Renders the UTF8 encoded text onto a new surface, and the surface is filled with $background_color. After that you can blit this surface to your display-surface 🌸

SDL::TTF::render_unicode_shaded($font, $text, $color, $background_color)

my $surface = SDL::TTF::render_unicode_shaded($font, $text, $color, $background_color);

Renders the unicode encoded text onto a new surface, surface filled with $background_color. After that you can blit this surface to your display-surface πŸ’œ

Note: the unicode text has to be passed exactly like for SDL::TTF::size_unicode.

πŸ‘‘ Blended πŸ‘‘

SDL::TTF::render_glyph_blended($font, $char, $color)

my $surface = SDL::TTF::render_glyph_blended($font, $char, $color);

Renders the unicode encoded char onto a new surface. After that you can blit this surface to your display-surface πŸ‘‘

Note: the unicode char has to be passed exactly like for SDL::TTF::size_unicode.

SDL::TTF::render_text_blended($font, $text, $color)

my $surface = SDL::TTF::render_text_blended($font, $text, $color);

Renders the LATIN1 encoded text onto a new surface. After that you can blit this surface to your display-surface πŸ”₯

Example:

use SDL;
use SDL::Video;
use SDL::Color;
use SDL::TTF;
use SDL::TTF::Font;

SDL::init(SDL_INIT_VIDEO);

SDL::TTF::init();

my $display = SDL::Video::set_video_mode(640, 480, 32, SDL_SWSURFACE);
my $font    = SDL::TTF::open_font('arial.ttf', '24');
my $surface = SDL::TTF::render_text_blended($font, 'Hello!', SDL::Color->new(0xFF,0xFF,0xFF));

SDL::Video::blit_surface($surface, SDL::Rect->new(0, 0, 640, 480), $display, SDL::Rect->new(10, 10, 640, 480));
SDL::Video::update_rect($display, 0, 0, 0, 0);

SDL::delay(5000);

SDL::TTF::render_utf8_blended($font, $text, $color)

my $surface = SDL::TTF::render_utf8_blended($font, $text, $color);

Renders the UTF8 encoded text onto a new surface. After that you can blit this surface to your display-surface ⭐

SDL::TTF::render_unicode_blended($font, $text, $color)

my $surface = SDL::TTF::render_unicode_blended($font, $text, $color);

Renders the unicode encoded text onto a new surface. After that you can blit this surface to your display-surface πŸ’«

Note: the unicode char has to be passed exactly like for SDL::TTF::size_unicode.

🌟 AUTHORS 🌟

See "AUTHORS" in SDL β€” that's where the credit lives, go give them their flowers 🌸

πŸ”— SEE ALSO πŸ”—

SDL::TTF::Font, Unicode::String, SDL::Video, SDL::Surface β€” the whole friend group, fr fr 🫢


And that's the whole page, bestie, you ate it πŸ’…βœ¨ This little number ships as pods::SDL::TTF(3pm), running with perl v5.42.3, dated 2026-08-19 β€” go forth and render something iconic πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from