SDL2-FFI

 view release on metacpan or  search on metacpan

lib/SDL2/TTF.pm  view on Meta::CPAN

package SDL2::TTF 0.01 {
    use strict;
    use SDL2::Utils;
    use experimental 'signatures';
    use base 'Exporter::Tiny';
    use SDL2::Utils qw[attach define load_lib];
    use SDL2::FFI;
    #
    our %EXPORT_TAGS;
    #
    sub _ver() {
        CORE::state $version //= TTF_Linked_Version();
        $version;
    }
    #
    load_lib('SDL2_ttf');
    #
    define ttf => [
        [ SDL_TTF_MAJOR_VERSION => sub () { SDL2::TTF::_ver()->major } ],
        [ SDL_TTF_MINOR_VERSION => sub () { SDL2::TTF::_ver()->minor } ],
        [ SDL_TTF_PATCHLEVEL    => sub () { SDL2::TTF::_ver()->patch } ],
        [   SDL_TTF_VERSION => sub ( $version = SDL2::Version->new() ) {
                my $ver = TTF_Linked_Version();
                $version->major( $ver->major );
                $version->minor( $ver->minor );
                $version->patch( $ver->patch );
            }
        ],
        [   SDL_TTF_COMPILEDVERSION => sub () {
                SDL2::FFI::SDL_VERSIONNUM( SDL_TTF_MAJOR_VERSION(), SDL_TTF_MINOR_VERSION(),
                    SDL_TTF_PATCHLEVEL() );
            }
        ],
        [   SDL_TTF_VERSION_ATLEAST => sub ( $X, $Y, $Z ) {
                ( SDL_TTF_COMPILEDVERSION() >= SDL_VERSIONNUM( $X, $Y, $Z ) )
            }
        ]
    ];
    attach ttf => { TTF_Linked_Version => [ [], 'SDL_Version' ] };
    define ttf => [ [ UNICODE_BOM_NATIVE => 0xFEFF ], [ UNICODE_BOM_SWAPPED => 0xFFFE ] ];

    package SDL2::TTF::Font {
        use SDL2::Utils;
        our $TYPE = has();
    };
    attach ttf => {
        TTF_Init    => [ [], 'int' ],
        TTF_WasInit => [ [], 'int' ],
        TTF_Quit    => [ [] ],
        #
        TTF_OpenFont        => [ [ 'string', 'int' ],                   'SDL_TTF_Font' ],
        TTF_OpenFontRW      => [ [ 'SDL_RWops', 'int', 'int' ],         'SDL_TTF_Font' ],
        TTF_OpenFontIndex   => [ [ 'string', 'int', 'long' ],           'SDL_TTF_Font' ],
        TTF_OpenFontIndexRW => [ [ 'SDL_RWops', 'int', 'int', 'long' ], 'SDL_TTF_Font' ],
        TTF_CloseFont       => [ ['SDL_TTF_Font'] ],
        #
        TTF_ByteSwappedUNICODE   => [ ['int'] ],
        TTF_GetFontStyle         => [ ['SDL_TTF_Font'], 'int' ],
        TTF_SetFontStyle         => [ [ 'SDL_TTF_Font', 'int' ] ],
        TTF_GetFontOutline       => [ ['SDL_TTF_Font'], 'int' ],
        TTF_SetFontOutline       => [ [ 'SDL_TTF_Font', 'int' ] ],
        TTF_GetFontHinting       => [ ['SDL_TTF_Font'], 'int' ],
        TTF_SetFontHinting       => [ [ 'SDL_TTF_Font', 'int' ] ],
        TTF_GetFontKerning       => [ ['SDL_TTF_Font'], 'int' ],
        TTF_SetFontKerning       => [ [ 'SDL_TTF_Font', 'int' ] ],
        TTF_FontHeight           => [ ['SDL_TTF_Font'], 'int' ],
        TTF_FontAscent           => [ ['SDL_TTF_Font'], 'int' ],
        TTF_FontDescent          => [ ['SDL_TTF_Font'], 'int' ],
        TTF_FontLineSkip         => [ ['SDL_TTF_Font'], 'int' ],
        TTF_FontFaces            => [ ['SDL_TTF_Font'], 'long' ],
        TTF_FontFaceIsFixedWidth => [ ['SDL_TTF_Font'], 'int' ],
        TTF_FontFaceFamilyName   => [ ['SDL_TTF_Font'], 'string' ],
        TTF_FontFaceStyleName    => [ ['SDL_TTF_Font'], 'string' ],
        TTF_GlyphIsProvided      => [
            [ 'SDL_TTF_Font', 'uint16' ],
            'int' => sub ( $inner, $font, $ch ) {
                $inner->( $font, ord $ch );
            }
        ],
        TTF_GlyphMetrics => [
            [ 'SDL_TTF_Font', 'uint16', 'int*', 'int*', 'int*', 'int*', 'int*' ],
            'int' => sub ( $inner, $font, $ch, $minx, $maxx, $miny, $maxy, $advance ) {
                $inner->( $font, ord $ch, $minx, $maxx, $miny, $maxy, $advance );
            }
        ],
        TTF_SizeText    => [ [ 'SDL_TTF_Font', 'string', 'int*', 'int*' ], 'int' ],
        TTF_SizeUNICODE => [ [ 'SDL_TTF_Font', 'string', 'int*', 'int*' ], 'int' ],
        #
        TTF_RenderText_Solid    => [ [ 'SDL_TTF_Font', 'string', 'SDL_Color' ], 'SDL_Surface' ],
        TTF_RenderUTF8_Solid    => [ [ 'SDL_TTF_Font', 'string', 'SDL_Color' ], 'SDL_Surface' ],
        TTF_RenderUNICODE_Solid => [ [ 'SDL_TTF_Font', 'string', 'SDL_Color' ], 'SDL_Surface' ],
        TTF_RenderGlyph_Solid   => [
            [ 'SDL_TTF_Font', 'uint16', 'SDL_Color' ],
            'SDL_Surface' => sub ( $inner, $font, $ch, $fg ) {
                $inner->( $font, ord $ch, $fg );
            }
        ],
        TTF_RenderText_Shaded =>
            [ [ 'SDL_TTF_Font', 'string', 'SDL_Color', 'SDL_Color' ], 'SDL_Surface' ],
        TTF_RenderUTF8_Shaded =>

lib/SDL2/TTF.pm  view on Meta::CPAN

=item C<ptsize> - point size (based on 72 DPI) to load font as; this basically translates to pixel height

=item C<index> - choose a font face from a file containing multiple font faces

=back

Returns a new L<SDL2::TTF::Font> structure on success.

=head2 C<TTF_OpenFontIndexRW( ... )>

Load C<src>, face C<index>, for use as a font, at C<ptsize> size.

    # load font.ttf at size 16 into font
    my $font = TTF_OpenFontIndexRW( SDL_RWFromFile( 'font.ttf', 'rb' ), 1, 16, 0 );
    if ( !$font ) {
        printf( "TTF_OpenFontIndexRW: %s\n", TTF_GetError() );

        # handle error
    }

Expected parameters include:

=over

=item C<src> - the source L<SDL2::RWops>

=item C<freesrc> - a non-zero value means it will automatically close and free the C<src> for you after it finishes using the C<src>, even if a noncritical error occurred

=item C<ptsize> - point size (based on 72 DPI) to load font as; this basically translates to pixel height

=item C<index> - choose a font face from a file containing multiple font faces

=back

Returns a new L<SDL2::TTF::Font> structure on success.

=head2 C<TTF_CloseFont( ... )>

Free the memory used by C<font>, and free C<font> itself as well. Do not use
C<font> after this without loading a new font to it.

    # free the font
    TTF_CloseFont( $font );
    undef $font; # to be safe...

Expected parameters include:

=over

=item C<font> - pointer to the L<TTF_Font> to free

=back

=head1 Attribute Functions

These functions deal with L<SDL2::TTF::Font> and global attributes.

=head2 C<TTF_ByteSwappedUNICODE( ... )>

This function tells C<SDL_ttf> whether UNICODE (Uint16 per character) text is
generally byteswapped. A B<UNICODE_BOM_NATIVE> or B<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.

    # Turn on byte swapping for UNICODE text
    TTF_ByteSwappedUNICODE( 1 );

Expected parameters include:

=over

=item C<swapped>

=over

=item - if non-zero then UNICODE data is byte swapped relative to the CPU's native endianness

=item - if zero, then do not swap UNICODE data, use the CPU's native endianness

=back

=back

=head2 C<TTF_GetFontStyle( ... )>

Get the rendering style of the loaded C<font>.

    # get the loaded font's style
    my $font  = TTF_OpenFontIndex( 'your.ttf', 16, 0 );
    my $style = TTF_GetFontStyle($font);
    print 'The font style is: ' . ( $style == TTF_STYLE_NORMAL ? 'normal' :
            $style & TTF_STYLE_BOLD          ? 'bold' :
            $style & TTF_STYLE_ITALIC        ? 'italic' :
            $style & TTF_STYLE_UNDERLINE     ? 'underline' :
            $style & TTF_STYLE_STRIKETHROUGH ? 'strikethrough' :
            'unknown' );

Expected parameters include:

=over

=item C<font> - the loaded font to get the style of

=back

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

=over

=item C<TTF_STYLE_BOLD>

=item C<TTF_STYLE_ITALIC>

=item C<TTF_STYLE_UNDERLINE>

=item C<TTF_STYLE_STRIKETHROUGH>

=back

If no style is set then C<TTF_STYLE_NORMAL> is returned.

lib/SDL2/TTF.pm  view on Meta::CPAN


=item * Passing a C<undef> font into this function will cause a segfault

=item * Passing a C<undef> text into this function will result in undefined behavior

=back

Returns a new L<SDL2::Surface> on success.

=head2 C<TTF_RenderGlyph_Blended( ... )>

Render the glyph for the UNICODE C<ch> using C<font> with C<fg> color onto a
new surface, using the B<Blended> mode. The caller (you!) is responsible for
freeing any returned surface.

    # Render and cache all printable ASCII characters in blended black
    my @glyph_cache;
    for my $ord ( 0 .. 127 ) {
        push @glyph_cache, TTF_RenderGlyph_Blended( $font, chr $ord, $color );
    }

Expected parameters include:

=over

=item C<font> - font to render the text with

=item C<ch> - the glyph to render

=item C<fg> - the color to render the glyph in; pixels are blended between transparent and this color to draw the antialiased glyph

=back

Note: Passing a C<undef> font into this function will cause a segfault.

Returns a new L<SDL2::Surface> on success. C<undef> is returned on errors, such
as when the glyph is not available in the font.

Combined with a cache of the glyph metrics (minx, miny, and advance), you might
make a fast text rendering routine that prints directly to the screen, but with
inaccurate kerning.

=head2 Defined Values

These may be imported by name or with the C<:all> tag.

=over

=item C<TTF_MAJOR_VERSION>

SDL_ttf library major number at compilation time.

=item C<TTF_MINOR_VERSION>

SDL_ttf library minor number at compilation time.

=item C<TTF_PATCHLEVEL>

SDL_ttf library patch level at compilation time.

=item C<UNICODE_BOM_NATIVE>

This allows you to switch byte-order of UNICODE text data to native order,
meaning the mode of your CPU. This is meant to be used in a UNICODE string that
you are using with the SDL_ttf API.

=item C<UNICODE_BOM_SWAPPED>

This allows you to switch byte-order of UNICODE text data to swapped order,
meaning the reversed mode of your CPU. So if your CPU is LSB, then the data
will be interpreted as MSB. This is meant to be used in a UNICODE string that
you are using with the SDL_ttf API.

=item C<TTF_STYLE_NORMAL>

Used to indicate regular, normal, plain rendering style.

=item C<TTF_STYLE_BOLD>

Used to indicate bold rendering style. This is used in a bitmask along with
other styles.

=item C<TTF_STYLE_ITALIC>

Used to indicate italicized rendering style. This is used in a bitmask along
with other styles.

=item C<TTF_STYLE_UNDERLINE>

Used to indicate underlined rendering style. This is used in a bitmask along
with other styles.

=item C<TTF_STYLE_STRIKETHROUGH>

Used to indicate strikethrough rendering style. This is used in a bitmask along
with other styles.

=item C<TTF_HINTING_NORMAL>

This corresponds to the default hinting algorithm, optimized for standard
gray-level rendering

=item C<TTF_HINTING_LIGHT>

A lighter hinting algorithm for non-monochrome modes. Many generated glyphs are
more fuzzy but better resemble its original shape. A bit like rendering on Mac
OS X.

=item C<TTF_HINTING_MONO>

Strong hinting algorithm that should only be used for monochrome output. The
result is probably unpleasant if the glyph is rendered in non-monochrome modes.

=item C<TTF_HINTING_NONE>

No hinting is used so the font may become very blurry or messy at smaller
sizes.

=item C<TTF_HINTING_LIGHT_SUBPIXEL>

A grayscale subpixel hinting algorithm.

=back

=head1 LICENSE

Copyright (C) Sanko Robinson.



( run in 1.795 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )