View on GitHub


Read font files and render glyphs using FreeType2

[Raku PDF Project] / [Font-FreeType Module] / Font::FreeType :: BitMap

class Font::FreeType::BitMap

Bitmaps from rendered glyphs


use Font::FreeType;

my Font::FreeType $freetype .= new;
my $face = $freetype.face('Vera.ttf');
$face.set-char-size(24, 24, 100, 100);

for $face.glyph-images('Hi') {
    print .outline.svg
        if .is-outline;

    # Render into an array of strings, one byte per pixel.
    my $bitmap = .bitmap;
    my $top = $;
    my $left = $bitmap.left;

    # print a string representation
    print $bitmap.Str;


This class represents the bitmap image of a rendered glyph.



The rendering pixel mode. One of:


The calculated color depth in bits. For example FT_PIXEL_MODE_GRAY has a color depth of 8.


The width of each row, in bytes


The number of rows in the image


Used to calculate the padding at the end of each row.


Returns a numeric shaped array of dimensions $.width and $height. Each item represents one pixel of the image, starting from the top left. A value of 0 indicates background (outside the glyph outline), and 255 represents a point inside the outline.

If anti-aliasing is used then shades of grey between 0 and 255 may occur. Anti-aliasing is performed by default, but can be turned off by passing the FT_RENDER_MODE_MONO option.


Renders the bitmap and constructs it into a PGM (portable grey-map) image file, which it returns as a Buf, suitable for output to a binary file.

The PGM image returned is in the ‘binary’ format, with one byte per pixel. It is not an efficient format, but can be read by many image manipulation programs. For a detailed description of the format see

The example program uses this method.


Returns an ASCII display representation of the rendered glyph.


produces a new bitmap, re-rendered as eight bit FT_PIXEL_MODE_GRAY.


Geoff Richards

David Warring (Raku Port)

Copyright 2004, Geoff Richards.

Ported from Perl to Raku by David Warring Copyright 2017.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.