freetype
[Top][All Lists]
Advanced

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

[ft] Help finding glyphs in TTF files


From: Michael Franklin
Subject: [ft] Help finding glyphs in TTF files
Date: Sat, 30 Mar 2013 05:34:57 -0700 (PDT)


Hello,

First of all, let me say thanks for FreeType.  I was thrilled when text first 
appeared (clean and crisp) on my embedded system for the fist time even if it 
wasn't the text I hoped for.  And I'm hoping you can help me with that.

I'm creating an embedded system using a Cortex-M4 processor and the Sourcery 
Codebench Lite (GCC 4.7.2) toolchain.

I'm trying to do a simple glyph to bitmap rendering as demonstrated in the 
FreeType Tutorial 1.  I have successfully displayed glyphs from Verdana.ttf 
(stolen from my Windows 8 computer) and FreeMono.ttf (stolen from my Mint Linux 
computer).  However, with both of these fonts, I can't get the correct glyphs 
unless I subtract 29 from the character code as shown below.

error = FT_Load_Char(face, text[i] - 29, FT_LOAD_RENDER);   //this works
error = FT_Load_Char(face, text[i], FT_LOAD_RENDER);   //this doesn't work

Verdana.ttf reports 1 charmap (platform: 0, encoding: 1) and Mono.ttf reports 0 
charmaps, which also seems strange.

I also tried a few other fonts (Arial.ttf from my Windows 8 computer and a few 
others), but they all return various errors after calling FT_Load_Char.  
Sometimes FT_Err_Invalid_Opcode and sometimes FT_Err_ENDF_In_Exec_Stream 
depending on the file.

At first I suspected my disk I/O routines, but these have all been working well 
in my libpng port, so I'm fairly confident they are working well.  Also,  
FT_New_Face doesn't give me any errors.

Here's my source code.  Please let me know if you have any suggestions.

Thanks.

DrawText(const Point& p, const uint8_t size, const File& file, const Color& 
color, const char* text, ...)
{
    FT_Library library;

    FT_Error error = FT_Init_FreeType(&library);
    if (error)
    {
        printf("FT_Init_FreeType failed\r]n");
        return;
    }

    printf("FT_Init_FreeType finished\r\n");

    FT_Face face;
    error = FT_New_Face( library, "/arialbi.ttf", 0, &face ); 
    if ( error == FT_Err_Unknown_File_Format ) 
    { 
        printf("FT_New_Face failed\r\n");
        //... the font file could be opened and read, but it appears 
        //... that its font format is unsupported 
    } 
    else if ( error ) 
    { 
        printf("FT_New_Face failed 2: %d\r\n", error);
        //... another error code means that the font file could not 
        //... be opened or read, or simply that it is broken... 
    }
    else
    {
        printf("FT_New_Face finished\r\n");

        printf("%d charmaps exist\r\n", face->num_charmaps);
        for (int n = 0; n < face->num_charmaps; n++ ) 
        { 
            FT_CharMap charmap = face->charmaps[n]; 
            printf("charmap: %d, %d\r\n", charmap->platform_id, 
charmap->encoding_id);
        }

        error = FT_Set_Char_Size( 
            face,      // handle to face object  
            0,         // char_width in 1/64th of points  
            size << 6, // char_height in 1/64th of points  
            72,        // horizontal device resolution in dots per inch
            72);       // vertical device resolution 

        if (error)
        {
            printf("FT_Set_Char_Size failed\r\n");
        }
        else
        {
            printf("FT_Set_Char_Size finished\r\n");

            int i = 0;
            int16_t x = 200;
            int16_t y = 200;
            while (text[i] != '\0')
            {
                error = FT_Load_Char(face, text[i] - 29, FT_LOAD_RENDER);
                if (error)
                {
                    printf("Missing Glyph for char %c: %d\r\n", text[i], error);
                }
                else
                {
                    printf("FT_Load_Char[%c] succeeded\r\n", text[i]);
                    FT_Draw_Bitmap(&face->glyph->bitmap, 
face->glyph->bitmap_left + x, y - face->glyph->bitmap_top);

                    //Move cursor to position for next character
                    x += (face->glyph->advance.x >> 6);  //int 1/64th units, so 
shift by 6
                    y += (face->glyph->advance.y >> 6);

                    printf("%d,%d\r\n", x, y);
                }

                i++;
            }
        }

        error = FT_Done_Face(face);
        if (error)
        {
            printf("FT_Done_Face failed\r\n");
        }
    }

    error = FT_Done_FreeType(library);
    if (error)
    {
        printf("FT_Done_FreeType failed\r\n");
    }
}

void FT_Draw_Bitmap( FT_Bitmap*  bitmap, FT_Int x, FT_Int y)
{
    FT_Int  i, j, p, q;
    FT_Int  x_max = x + bitmap->width;
    FT_Int  y_max = y + bitmap->rows;

    for ( i = x, p = 0; i < x_max; i++, p++ )
    {
        for ( j = y, q = 0; j < y_max; j++, q++ )
        {
            uint8_t alpha = bitmap->buffer[q * bitmap->width + p];
            SetPixel({i, j}, Color(alpha, alpha, alpha));
        }
    }
}                                                                               
                            



reply via email to

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