freetype
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [ft] Rendering rotated text properly


From: Jaco van der Westhuizen
Subject: RE: [ft] Rendering rotated text properly
Date: Wed, 30 Nov 2005 11:35:42 +0200

Hi all,

Nevermind, I found the ft2demo source code and managed to get it going
nicely.

Thanks !

-----Original Message-----
From: address@hidden
[mailto:address@hidden On Behalf Of
Jaco van der Westhuizen
Sent: 30 November 2005 08:46
To: address@hidden
Subject: [ft] Rendering rotated text properly


I'm having difficulty rendering rotated text properly, I basically have
two problems:

A) The character positions of lower case letter eg 'a' aligns to the
upper part of uppercase letters and not at the base line, so it looks
like the a is too high up. I can correct this, but not when the text is
rotated.
B) When I rotate the text, the characters seem to wobble as the string
rotates, eg the placement relative to the previous character changes as
the rotation angle changes.

I have tried various things, even the sample code in the tutorials with
no luck. The current code is based on the way the GD library does it.
(If you don't know GD library, its about drawing shapes and images,
including fonts using FreeType,  directly to an image, making use of its
own drawing functions)


Help with this will be greatly appreciated. Thanks!

Here is the code:
bool    CFTFnt::Draw( HDC dc, int x, int y, const char *Text, int
pix_height, double angledegrees, ALIGN align, bool bbox, SIZE *box ) {
        if ( ( !m_hLib ) || ( !m_Face ) )
                return false;


        double                  radians, sin_a, cos_a;
        int                             nx, ny;
        FT_Vector               ofs, start, kern;
        FT_GlyphSlot    slot;
        FT_Glyph                image;
        FT_BitmapGlyph  bit;
        FT_Matrix               matrix;
        int                             g_index, prev_g_index;

        radians =  3.141592654 * 2 / 360 * (double)(angledegrees);
        start.x = 0;
        start.y = 0;
        if ( box != 0 )
        {
                box->cx = 0;
                box->cy = 0;
        }
        prev_g_index = -1;
        slot = m_Face->glyph;

        // Calculate the alignment offsets
        switch ( align )
        {
                default:
                case CFTFnt::LEFT:
                        ofs.x = x * 64;
                        ofs.y = y * 64;
                        break;
/*
                case CFTFnt::MIDDLE:
                        ofs.x = ( x - string_width / 2 ) * 64;
                        ofs.y = y * 64;
                        break;
                case CFTFnt::RIGHT:
                        ofs.x = (x- string_width) * 64 ;
                        ofs.y = y * 64;
                        break;*/
        }

        /* set up transform (a rotation here) */
        sin_a = sin( radians );
        cos_a = cos( radians );
        matrix.xx = (FT_Fixed)( cos_a * 0x10000L );
        matrix.xy = (FT_Fixed)(-sin_a * 0x10000L );
        matrix.yx = (FT_Fixed)( sin_a * 0x10000L );
        matrix.yy = (FT_Fixed)( cos_a * 0x10000L );

        // Setup the pixel sizes
        FT_Set_Char_Size( m_Face, 0, FT_MAKESIZE( pix_height ), m_DpiX,
m_DpiY );

        // Setup the transformation
        FT_Set_Transform( m_Face, &matrix, 0 );

        // Render the characters
        for ( ; *Text; ++Text )
        {
                // Get the glyph index
                g_index = FT_Get_Char_Index( m_Face, *Text );

                // Get kerning if any
                if ( prev_g_index != -1 )
                {
                        FT_Get_Kerning( m_Face, prev_g_index, g_index,
FT_KERNING_DEFAULT, &kern );
                        start.x += kern.x;
                }
                prev_g_index = g_index;

                // Load the glyph
                if ( FT_Load_Glyph( m_Face, g_index, FT_LOAD_RENDER ) )
                        return false;

                // Get the Glyph image
                if ( FT_Get_Glyph( slot, &image ) )
                        return false;

                // Check if its a bitmap, if not, make one
                if (image->format != ft_glyph_format_bitmap)
                {
                        if ( FT_Glyph_To_Bitmap (&image,
ft_render_mode_normal, 0, 1) )
                        {
                                FT_Done_Glyph( image );
                                return false;
                        }
                }

                // Get the Bitmap Glyph and render it
                bit = (FT_BitmapGlyph)image;

                nx = ofs.x + (start.x * cos_a - start.y * sin_a ) +
bit->left;
                ny = ofs.y - (start.x * sin_a + start.y * cos_a) -
bit->top;

                DrawToDC( dc, nx, ny, &bit->bitmap );

                // Update our character cursor
                start.x += slot->metrics.horiAdvance;
//              start.y += m_Face->glyph->advance.y;%

                // Destroy the generated image
                FT_Done_Glyph( image );
        }

        return true;
}


_______________________________________________
Freetype mailing list
address@hidden http://lists.nongnu.org/mailman/listinfo/freetype




reply via email to

[Prev in Thread] Current Thread [Next in Thread]