freetype
[Top][All Lists]
Advanced

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

[ft] letters leaning when rendring on windows


From: puzzleroot
Subject: [ft] letters leaning when rendring on windows
Date: Sun, 10 Aug 2008 14:53:56 -0700 (PDT)

Can someone help me please? I am trying to use freetype2 to render a simple string using visual C++ 8 but I am facing a strange problem that I could not find a way to solve it. Some letters appears correctly and some appears leaning and cursive. And I changed the font size, I get different results. CODE:
void InitFT2(HWND hwnd, char *Font)
{
    FT_Library  library;   /* handle to library     */
    FT_Face     face;      /* handle to face object */
    int error;
    FT_GlyphSlot  slot;
    FT_Vector    pen    = {0,0};
    FT_UInt        glyph_index;
    FT_Bitmap    *BitMapGlyph;
    FT_Vector    delta;
    double        angle = ( 0.0 / 360 ) * 3.14159 * 2;
    FT_Glyph    glyph = NULL;
    FT_BBox        bbox;

    //initialize the library 
    error = FT_Init_FreeType( &library );
    if ( error )
        MessageBox(NULL, "Error Initializing Freetype2 library", "FT_Init_FreeType() error", MB_OK|MB_ICONEXCLAMATION);

    //open a font file by creating a new face object 
    error = FT_New_Face( library, Font, 0, &face );
    if ( error == FT_Err_Unknown_File_Format )
        MessageBox(NULL, "the font file could be opened and read,\n but it appears that its font format is unsupported", "FT_New_Face() error", MB_OK|MB_ICONEXCLAMATION);
    else if ( error )
        MessageBox(NULL, "another error code means that the font file could not be opened or read,\n or simply that it is broken", "FT_New_Face() error", MB_OK|MB_ICONEXCLAMATION);

    if (error = FT_Set_Char_Size(face, (64*64), 0, 96, 0))
        MessageBox(NULL, "Can not set charterer size", "FT_Set_Char_Size() error", MB_OK|MB_ICONEXCLAMATION);

    slot    = face->glyph;

    WCHAR *ttext = L"ABCDEFGHI";
    int len = wcslen(ttext);
    pen.x = 0 * 64;
    pen.y = 0 * 64;
    for (int a =0; a<len; a++)
    {
        //FT_Set_Transform( face, 0, &pen );

        FT_Load_Char(face, ttext[a], FT_LOAD_RENDER);

        FontRedner(hwnd, &slot->bitmap, pen.x+slot->bitmap_left , slot->bitmap_top );
        FT_Done_Glyph( glyph );
        pen.x += slot->advance.x >> 6;
        //pen.y += slot->advance.y >> 6;
    }
    
    FT_Done_Face    ( face );
    FT_Done_FreeType( library );
}

void FontRedner(HWND hwnd, FT_Bitmap *ftBitmap, int x, int y)
{
    UINT BufferSize = ftBitmap->width * ftBitmap->rows;
    HDC srcDC = GetDC(hwnd);
    HDC desDC = CreateCompatibleDC(srcDC);
    char *ppvBITs;        // Direct access to bits array in memory created with CreateDIBSection()
    ZeroMemory(&ppvBITs,sizeof(ftBitmap->buffer));
    
    // Construct Bitmap info
    BITMAPINFO *BitMapInfo = (BITMAPINFO*) malloc(sizeof(BITMAPINFOHEADER)+ 256 * sizeof(RGBQUAD));
    BITMAPINFOHEADER *BitMapInfoHeader    = (BITMAPINFOHEADER *) BitMapInfo;
    BitMapInfoHeader->biSize            = sizeof(BITMAPINFOHEADER);
    BitMapInfoHeader->biWidth            = ftBitmap->width;
    BitMapInfoHeader->biHeight            = (ftBitmap->rows)*-1;    // * -1 to reverse order
    BitMapInfoHeader->biPlanes            = 1;
    BitMapInfoHeader->biBitCount        = 8;
    BitMapInfoHeader->biSizeImage        = NULL;
    BitMapInfoHeader->biCompression        = BI_RGB;

    RGBQUAD *paleta=(LPRGBQUAD)(((BYTE *)(BitMapInfo))+ sizeof(BITMAPINFOHEADER));
    for (int i = 0; i < 256; i++)
    {
        paleta->rgbBlue = paleta->rgbGreen = paleta->rgbRed = (BYTE) i;
        paleta->rgbReserved = 0;
        paleta++;
    }

    HBITMAP hNewBitMap = CreateDIBSection(desDC,BitMapInfo,DIB_RGB_COLORS, (void **)&ppvBITs,NULL, NULL);
    
    // copy ft_bitmap bits to NewBitMap pointer
    memcpy(ppvBITs, ftBitmap->buffer, BufferSize );
    
    HBITMAP tempBitMapHandle = (HBITMAP)SelectObject(desDC,hNewBitMap);

    BitBlt(srcDC, x, 0, ftBitmap->width, ftBitmap->rows, desDC, 0, 0, SRCCOPY);
    DeleteObject(SelectObject(desDC, tempBitMapHandle));
    DeleteDC(desDC);
    ReleaseDC(hwnd, srcDC);

}
test_32p.jpg test_64p.jpg

View this message in context: letters leaning when rendring on windows
Sent from the Freetype - User mailing list archive at Nabble.com.

reply via email to

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