freetype
[Top][All Lists]
Advanced

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

[Fwd: Re: [Freetype] FreeType2 and C++ cannot render glyph]


From: Frank W. Miller
Subject: [Fwd: Re: [Freetype] FreeType2 and C++ cannot render glyph]
Date: Mon, 24 Mar 2003 21:17:35 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.3) Gecko/20030312


--- Begin Message --- Subject: Re: [Freetype] FreeType2 and C++ cannot render glyph Date: Mon, 24 Mar 2003 21:13:55 -0500 User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.3) Gecko/20030312 I'm sure there's nothing wrong with FreeType2. Its something I'm doing wrong. I actually copied the FreeType calls that I had in the C based code into the methods for the C++ code. I've included the C++ code (2 files)



Pedriana, Paul wrote:

Is your new app really the same as your old app? What if you take all
your previous .c files and rename to .cpp? My guess is that there is no
fault in Freetype, especially as I use it from C++ with no problems.

Paul



-----Original Message-----
From: Frank W. Miller [mailto:address@hidden Sent: Monday, March 24, 2003 5:50 PM
To: address@hidden
Subject: [Freetype] FreeType2 and C++ cannot render glyph

Greetings,

I've been using FreeType2 2.1.2 and 2.1.3 with an X11 program I wrote using C quite successfully. When I try to recode the application in C++, the FreeType2 stuff works until I call FT_Render_Glyph(). This
call returns error code 19, which is a generic
"cannot render this glyph format". I'm using an Arial TrueType font with the DPI set to 96 and the points set to 12. Any assistance would be greatly appreciated!

FM


#include <config.h>
#include <display.h>
#include <event.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <stdio.h>
#include <text.h>

#define POINTS          12
#define DPI             96

FT_Library library;
FT_Face face;

static char *font_name = "../font/arial.ttf";

class display disp;
class event ev;

static char *system_text = SYSTEM;
static class text sys;

int
main()
{
        int result;

        printf("%s %d.%d  %s %s\n",
               SYSTEM, VERSION, RELEASE, COPYRIGHT, AUTHOR);

        /* Initialize FreeType2 library */
        result = FT_Init_FreeType(&library);
        if (result != 0) {
#if _DEBUG
                printf("main: initialize library failed\n");
#endif
                exit(-1);
        }
        result = FT_New_Face(library, font_name, 0, &face);
        if (result != 0) {
#if _DEBUG
                if (result == FT_Err_Unknown_File_Format)
                        printf("main: bad font file format\n");
                else
                        printf("main: load font failed\n");
#endif
                exit(-1);
        }
#if _DEBUG
        printf("main: num_glyphs %u\n", (unsigned int) face->num_glyphs);
        printf("main: units_per_EM %d\n", face->units_per_EM);
#endif
        result = FT_Set_Char_Size(face, 0, POINTS * 64, DPI, DPI);
        if (result != 0) {
#if _DEBUG
                printf("main: set char size failed\n");
#endif
                exit(-1);
        }
        disp.init();
        ev.init();

        sys.init();
        sys.set_origin(16, 16);
        sys.set_width(128);
        sys.set_height(32);
        sys.set_text(system_text);

        for (;;) {
                ev.next();

                switch (ev.type) {
                case EVENT_EXPOSE:
                        disp.clear();
                        sys.draw();
                        break;

                default:
                        if (ev.type == EVENT_BUTTON)
                                printf("main: button pressed\n");
                        else if (ev.type == EVENT_KEYPRESS)
                                printf("main: key pressed\n");
                        else
                                printf("main: unrecognized event (%d)\n",
                                       ev.type);
                }
        }
}
#include <display.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <stdlib.h>
#include <string.h>
#include <text.h>

#define MAX_GLYPHS      32

extern FT_Face face;

void
text::init()
{
        widget::init(WT_TEXT, NULL, NULL);
        _s = NULL;
}

char *
text::get_text()
{
        return _s;
}

void
text::set_text(char *s)
{
        _s = s;
}

static void
draw_bitmap(int x, int y, FT_Bitmap bitmap)
{
        int i, j, k, rowcnt;
        unsigned char byte;

        for (i = 0; i < bitmap.rows; i++) {
                rowcnt = 0;
                for (j = 0; j < bitmap.pitch; j++) {
                        byte = *(bitmap.buffer + (i * bitmap.pitch) + j);
                        for (k = 0; k < 8; k++) {
                                if (byte & 0x80)
                                        disp.draw_pixel(x + rowcnt, y + i);
                                byte <<= 1;

                                rowcnt++;
                                if (rowcnt == bitmap.width)
                                        break;
                        }
                        if (rowcnt == bitmap.width)
                                break;
                }
        }
}

void
text::draw()
{
        int x, y, width, height;
        int descent, glyph_index, i, len, max_bearing_y = 0, max_descent = 0;
        int pen_x, pen_y, prev = 0, use_kerning, text_width;
        int result;

        if (_s == NULL)
                return;

        get_origin(&x, &y);
        width = get_width();
        height = get_height();
        disp.set_foreground_color();

#if _DEBUG
        printf("text::draw: x %d y %d width %d height %d _s [%s]\n",
               x, y, width, height, _s);
#endif
        use_kerning = FT_HAS_KERNING(face);

        for (pen_x = x, len = strlen(_s), i = 0; i < len; i++) {
                glyph_index = FT_Get_Char_Index(face, _s[i]);

                if (use_kerning && prev && glyph_index) {
                        FT_Vector delta;

                        FT_Get_Kerning(face, prev, glyph_index, 0, &delta);
                        pen_x += delta.x >> 6;
                }
                result = FT_Load_Glyph(face, glyph_index, 0);
                if (result != 0) {
#if _DEBUG
                        printf("text::draw: load glyph failed (%d)", result);
#endif
                        continue;
                }
                if (face->glyph->metrics.horiBearingY > max_bearing_y)
                        max_bearing_y = face->glyph->metrics.horiBearingY;

                descent = face->glyph->metrics.height -
                          face->glyph->metrics.horiBearingY;
                if (descent > max_descent)
                        max_descent = descent;

                pen_x += face->glyph->metrics.horiBearingX >> 6;
                pen_x += face->glyph->advance.x >> 6;
                prev = glyph_index;
        }
        max_bearing_y >>= 6;
        max_descent >>= 6;
        text_width = pen_x - x;
#if _DEBUG
        printf("text::draw: width %d max_bearing_y %d max_descent %d\n",
               text_width, max_bearing_y, max_descent);
#endif
        pen_x = x + (width >> 1) - (text_width >> 1);
        y = y + (height >> 1) - (max_bearing_y >> 1);
        y += max_bearing_y;
        for (len = strlen(_s), i = 0; i < len; i++) {
                glyph_index = FT_Get_Char_Index(face, _s[i]);

                if (use_kerning && prev && glyph_index) {
                        FT_Vector delta;

                        FT_Get_Kerning(face, prev, glyph_index, 0, &delta);
                        pen_x += delta.x >> 6;
                }
                result = FT_Load_Glyph(face, glyph_index, 0);
                if (result != 0) {
#if _DEBUG
                        printf("text::draw: load glyph failed (%d)", result);
#endif
                        continue;
                }
                if (face->glyph->format != ft_glyph_format_bitmap) {
                        result = FT_Render_Glyph(face->glyph,
                                                 ft_render_mode_mono);
                        if (result != 0) {
#if _DEBUG
                                printf("text::draw: ");
                                printf("render glyph failed (%d)\n", result);
#endif
                                continue;
                        }
                }
                pen_x += face->glyph->metrics.horiBearingX >> 6;
                pen_y = y - (face->glyph->metrics.horiBearingY >> 6);
                draw_bitmap(pen_x, pen_y, face->glyph->bitmap);

                pen_x += face->glyph->advance.x >> 6;
                prev = glyph_index;
        }
}

--- End Message ---

reply via email to

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