#include #include FT_FREETYPE_H #include FT_COLOR_H #include "log.h" #include #include #include #include #include #include #include #include #include #include #include #include #define WIDTH 640 #define HEIGHT 480 FT_Library ft; FT_Face face; unsigned char image[WIDTH][HEIGHT]; void show_image( char *fb ); void show_image( char *fb ) { int i, j; for ( i = 0; i < WIDTH; i++ ) { for ( j = 0; j < HEIGHT; j++ ) fb[i+2432*j] = image[i][j]; } } static void draw_bitmap(char *fb, 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++ ) { if ( i < 0 || j < 0 || i >= WIDTH || j >= HEIGHT ) continue; image[i][j] |= bitmap->buffer[q * bitmap->width + p]; } } } int TypewriterSetForeground(uint32_t color) { FT_Color fc; fc.alpha = color & 0xFF000000 >> 24; fc.red = color & 0xFF0000 >> 16; fc.green = color & 0xFF00 >> 8; fc.blue = color & 0xFF; FT_Palette_Set_Foreground_Color(face, fc); } int TypewriterInit(void) { if (FT_Init_FreeType(&ft)) { LOG_E("Typewriter init failed"); return -1; } if (FT_New_Face(ft, "/root/projects/GPU/fonts/arial.ttf", 0, &face)) { LOG_E("Typewriter failed to load font"); return -1; } } int TypewriterRenderText(char *fb, char *text) { int n = 0; int target_height = 600; int pen_x = 300; int pen_y = 200; assert(text!=NULL); FT_ULong character = 'x'; FT_UInt glyph_index; FT_Vector pen; FT_UInt gi = FT_Get_Char_Index (face, character); /* char width, height, horz_resolution, vert_resolution, 50pt at 100dpi */ if(FT_Set_Char_Size(face, 100 * 64, 0, 100, 100 )) { LOG_E("Set char size failed"); return -1; } for (n = 0; n < strlen(text); n++) { if (text[n] == 'g') { FT_UInt gi = FT_Get_Char_Index (face, text[n]); FT_Load_Glyph (face, gi, FT_LOAD_DEFAULT); face->glyph->metrics.horiBearingY = face->glyph->metrics.horiBearingY + 5; } FT_Load_Char( face, text[n], FT_LOAD_RENDER ); draw_bitmap(fb, &face->glyph->bitmap, pen_x + face->glyph->bitmap_left, pen_y + face->glyph->bitmap_top ); /* increment pen position */ pen_x += face->glyph->advance.x >> 6; printf("Char %c is printed \n", text[n]); } show_image(fb); }