顯示多行文字 兩行文字左邊對齊 簡單使用兩個迴圈顯示兩行字體 根據上一行字體的寬度來進行下一行左邊的計算 #include #include #include #include #include #include #include #include #include #include #includ... ...
顯示多行文字
兩行文字左邊對齊
簡單使用兩個迴圈顯示兩行字體
根據上一行字體的寬度來進行下一行左邊的計算
#include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <linux/fb.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <math.h> #include <wchar.h> #include <ft2build.h> #include <stdlib.h> #include FT_FREETYPE_H #include FT_GLYPH_H #include <linux/font.h> int fd_fb; struct fb_var_screeninfo var; /* Current var */ struct fb_fix_screeninfo fix; /* Current fix */ int screen_size; //空間大小 unsigned char *fb_mem; //framebaffer空間地址 unsigned int line_width; unsigned int pixel_width; void lcd_put_pixel(int x, int y, unsigned int color) { unsigned char *pen_8 = fb_mem + y * line_width + x * pixel_width; //當前像素對應記憶體位置 unsigned short *pen_16; unsigned int *pen_32; unsigned int red, blue, green; pen_16 = (unsigned short *)pen_8; pen_32 = (unsigned int *)pen_8; switch(var.bits_per_pixel) { case 8: { *pen_8 = color; //對應調色板顏色 break; } case 16: { /* 5*6*5 */ red = (color >> 16) & 0xff; green = (color >> 8) & 0xff; blue = (color >> 0) & 0xff; color = ((red >> 3 ) << 11) | ((green >> 2) << 5) | ( blue >> 3); /* 顏色數據為高位 */ *pen_16 = color; break; } case 32: { *pen_32 = color; break; } } } void 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; printf("x = %d, y = %d\n", x, y); 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 >= var.xres || j >= var.yres ) continue; // image[j][i] |= bitmap->buffer[q * bitmap->width + p]; lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]); } } } int main(int argc, char **argv) { wchar_t * str1 = L"陳志朋gif"; wchar_t * str2 = L"hello the world"; FT_BBox bbox; FT_Glyph glyph; FT_Library library; FT_Face face; FT_Vector pen; /* untransformed origin */ FT_GlyphSlot slot; FT_Matrix matrix; /* transformation matrix */ int error; double angle; int i; int line_box_ymin = 10000; int line_box_ymax = 0; if(argc != 2) { printf("Usage : %s <font_file> <angle>\n",argv[0]); return -1; } fd_fb = open("/dev/fb0",O_RDWR); if(fd_fb < 0) { printf("can't open /dev/fb0 \n"); return -1; } if(ioctl(fd_fb, FBIOGET_VSCREENINFO, &var)) { printf("can't get var \n"); return -1; } if(ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix)) { printf("can't get fix \n"); return -1; } screen_size = var.xres * var.yres * var.bits_per_pixel / 8; //單位位元組 line_width = var.xres * var.bits_per_pixel / 8; pixel_width = var.bits_per_pixel / 8; fb_mem = (unsigned char *)mmap(NULL, screen_size, \ PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0); if(fb_mem == (unsigned char *) -1) { printf("can't mmap \n"); return -1; } memset(fb_mem, 0, screen_size); /* 顯示矢量文字 */ error = FT_Init_FreeType( &library ); /* initialize library */ /* error handling omitted */ error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */ /* error handling omitted */ slot = face->glyph; FT_Set_Pixel_Sizes(face, 24, 0); /*確定坐標 *lcd_x = 0 *lcd_y = 24 *笛卡爾坐標 *x = lcd_x = 0 *y = var.yres - 24 */ pen.x = ( 0 ) * 64; pen.y = ( var.yres - 24 ) * 64; for(i = 0; i < wcslen(str1); i++) { /* set transformation */ FT_Set_Transform( face,0, &pen ); /* load glyph image into the slot (erase previous one) */ error = FT_Load_Char( face, str1[i], FT_LOAD_RENDER ); if ( error ) { printf("FT_Load_Char error\n"); return -1; } error = FT_Get_Glyph( face->glyph, &glyph ); if (error) { printf("FT_Get_Glyph error!\n"); return -1; } FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox ); if(line_box_ymin > bbox.yMin) line_box_ymin = bbox.yMin; if(line_box_ymax < bbox.yMax) line_box_ymax = bbox.yMax; /* now, draw to our target surface (convert position) */ draw_bitmap( &slot->bitmap, slot->bitmap_left, var.yres - slot->bitmap_top ); pen.x += slot->advance.x; } /*確定坐標 *lcd_x = 0 *lcd_y = 24 *笛卡爾坐標 *x = lcd_x = 0 *y = var.yres - 24 */ pen.x = ( 0 ) * 64; pen.y = ( var.yres - (line_box_ymax - line_box_ymin + 24) ) * 64; for(i = 0; i < wcslen(str2); i++) { /* set transformation */ FT_Set_Transform( face,0, &pen ); /* load glyph image into the slot (erase previous one) */ error = FT_Load_Char( face, str2[i], FT_LOAD_RENDER ); if ( error ) { printf("FT_Load_Char error\n"); return -1; } error = FT_Get_Glyph( face->glyph, &glyph ); if (error) { printf("FT_Get_Glyph error!\n"); return -1; } FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox ); if(line_box_ymin > bbox.yMin) line_box_ymin = bbox.yMin; if(line_box_ymax < bbox.yMax) line_box_ymax = bbox.yMax; /* now, draw to our target surface (convert position) */ draw_bitmap( &slot->bitmap, slot->bitmap_left, var.yres - slot->bitmap_top ); pen.x += slot->advance.x; } }
使用freetpye中的函數實現一行文字居中
4.6 高級文本渲染:變換 + 居中 + 字距調整
現在我們將修改我們的代碼,以便可以容易地變換已渲染的字元串,例如旋轉它。我們將以實行少許小
改進開始:
4.6.1 打包然後平移字形
我們先把與一個字形圖像相關的信息打包到一個結構體,而不是並行的數組。因此我們定義下麵的結構
體類型:
typedef struct TGlyph_
{
FT_UInt index; /* 字形索引 */
FT_Vector pos; /* 基線上面的字形原點 */
FT_Glyph image; /* 字形圖像 */
} TGlyph, *PGlyph;
我們在裝載每一個字形圖像過程中,在把它裝載它在基線所在位置後便直接平移它。我們將看到,這有若幹
好處。我們的字形序列裝載其因而變成:
FT_GlyphSlot slot = face->glyph; /* 一個小捷徑 */
FT_UInt glyph_index;
FT_Bool use_kerning;
FT_UInt previous;
int pen_x, pen_y, n;
TGlyph glyphs[MAX_GLYPHS]; /* 字形表 */
PGlyph glyph; /* 表中的當前字形*/
FT_UInt num_glyphs;
... 初始化庫 ...
... 創建 face 對象 ...
... 設置字元尺寸 ...
pen_x = 0; /* 以 (0,0) 開始 */
pen_y = 0;
num_glyphs = 0;
use_kerning = FT_HAS_KERNING( face );
previous = 0;
glyph = glyphs;
for ( n = 0; n < num_chars; n++ )
{
glyph->index = FT_Get_Char_Index( face, text[n] );
if ( use_kerning && previous && glyph->index )
{
FT_Vector delta;
FT_Get_Kerning( face, previous, glyph->index,
FT_KERNING_MODE_DEFAULT, &delta );
pen_x += delta.x >> 6;
}
/* 保存當前筆位置 */
glyph->pos.x = pen_x;
glyph->pos.y = pen_y;
error = FT_Load_Glyph(face,glyph_index,FT_LOAD_DEFAULT);
if ( error ) continue;
error = FT_Get_Glyph( face->glyph, &glyph->image );
if ( error ) continue;
/* 現在平移字形圖像 */
FT_Glyph_Transform( glyph->image, 0, &glyph->pos );
pen_x += slot->advance.x >> 6;
previous = glyph->index;
/* 增加字形的數量 */
glyph++;
}
/* 計算已裝載的字形的數量 */
num_glyphs = glyph - glyphs;
註意,這個時候平移字形有若幹好處。第一是當我們計算字元串的邊界框時不需要平移字形 bbox。代碼將會
變成這樣:
void compute_string_bbox( FT_BBox *abbox )
{
FT_BBox bbox;
bbox.xMin = bbox.yMin = 32000;
bbox.xMax = bbox.yMax = -32000;
for ( n = 0; n < num_glyphs; n++ )
{
FT_BBox glyph_bbox;
FT_Glyph_Get_CBox( glyphs[n], &glyph_bbox );
if (glyph_bbox.xMin < bbox.xMin)
bbox.xMin = glyph_bbox.xMin;
if (glyph_bbox.yMin < bbox.yMin)
bbox.yMin = glyph_bbox.yMin;
if (glyph_bbox.xMax > bbox.xMax)
bbox.xMax = glyph_bbox.xMax;
if (glyph_bbox.yMax > bbox.yMax)
bbox.yMax = glyph_bbox.yMax;
}
if ( bbox.xMin > bbox.xMax )
{
bbox.xMin = 0;
bbox.yMin = 0;
bbox.xMax = 0;
bbox.yMax = 0;
}
*abbox = bbox;
}
更詳細描述: compute_string_bbox 函數現在可以計算一個已轉換的字形字元串的邊界框。例如,我們可以做
如下的事情:
FT_BBox bbox;
FT_Matrix matrix;
FT_Vector delta;
... 裝載字形序列 ...
... 設置 "matrix" 和 "delta" ...
/* 變換字形 */
for ( n = 0; n < num_glyphs; n++ )
FT_Glyph_Transform( glyphs[n].image, &matrix, &delta );
/* 計算已變換字形的邊界框 */
compute_string_bbox( &bbox );
#include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <linux/fb.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <math.h> #include <wchar.h> #include <ft2build.h> #include <stdlib.h> #include FT_FREETYPE_H #include FT_GLYPH_H #include <linux/font.h> #define MAX_GLYPHS 100 typedef struct TGlyph_ { FT_UInt index; /* ???? */ FT_Vector pos; /* ????????? */ FT_Glyph image; /* ???? */ } TGlyph, *PGlyph; int fd_fb; struct fb_var_screeninfo var; /* Current var */ struct fb_fix_screeninfo fix; /* Current fix */ int screen_size; //空間大小 unsigned char *fb_mem; //framebaffer空間地址 unsigned int line_width; unsigned int pixel_width; void lcd_put_pixel(int x, int y, unsigned int color) { unsigned char *pen_8 = fb_mem + y * line_width + x * pixel_width; //當前像素對應記憶體位置 unsigned short *pen_16; unsigned int *pen_32; unsigned int red, blue, green; pen_16 = (unsigned short *)pen_8; pen_32 = (unsigned int *)pen_8; switch(var.bits_per_pixel) { case 8: { *pen_8 = color; //對應調色板顏色 break; } case 16: { /* 5*6*5 */ red = (color >> 16) & 0xff; green = (color >> 8) & 0xff; blue = (color >> 0) & 0xff; color = ((red >> 3 ) << 11) | ((green >> 2) << 5) | ( blue >> 3); /* 顏色數據為高位 */ *pen_16 = color; break; } case 32: { *pen_32 = color; break; } } } void 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; printf("x = %d, y = %d\n", x, y); 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 >= var.xres || j >= var.yres ) continue; // image[j][i] |= bitmap->buffer[q * bitmap->width + p]; lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]); } } } int Get_Glphs_Frm_Wstr(FT_Face face, wchar_t * wstr, TGlyph glyphs[]) { PGlyph glyph = glyphs; FT_GlyphSlot slot = face->glyph; int n; int pen_x =0; int pen_y = 0; int error; for(n = 0; n < wcslen(wstr); n++ ) { glyph->index = FT_Get_Char_Index( face, wstr[n] ); /* 保存當前筆位置 */ glyph->pos.x = pen_x; glyph->pos.y = pen_y; /* load 是把glyph 載入到face->glyph */ error = FT_Load_Glyph(face, glyph->index, FT_LOAD_DEFAULT); if ( error ) continue; error = FT_Get_Glyph( face->glyph, &glyph->image ); if ( error ) continue; /* 現在平移字形圖像 */ /* 使得glyph->image裡面有位置信息 */ FT_Glyph_Transform( glyph->image, 0, &glyph->pos ); pen_x += slot->advance.x; /* 1 / 64 piont */ /* 增加字形的數量 */ glyph++; } /* 計算已裝載的字形的數量 */ return (glyph - glyphs); } void compute_string_bbox( TGlyph glyphs[], FT_UInt num_glyphs, FT_BBox *abbox ) { FT_BBox bbox; int n; bbox.xMin = bbox.yMin = 32000; bbox.xMax = bbox.yMax = -32000; for ( n = 0; n < num_glyphs; n++ ) { FT_BBox glyph_bbox; FT_Glyph_Get_CBox( glyphs[n].image, FT_GLYPH_BBOX_TRUNCATE, &glyph_bbox ); if (glyph_bbox.xMin < bbox.xMin) bbox.xMin = glyph_bbox.xMin; if (glyph_bbox.yMin < bbox.yMin) bbox.yMin = glyph_bbox.yMin; if (glyph_bbox.xMax > bbox.xMax) bbox.xMax = glyph_bbox.xMax; if (glyph_bbox.yMax > bbox.yMax) bbox.yMax = glyph_bbox.yMax; } *abbox = bbox; } void Draw_Glyphs(TGlyph glyphs[], FT_UInt num_glyphs, FT_Vector pen) { int n; int error; for ( n = 0; n < num_glyphs; n++ ) { FT_Glyph_Transform( glyphs[n].image, 0, &pen ); error = FT_Glyph_To_Bitmap(&glyphs[n].image, FT_RENDER_MODE_NORMAL, 0, 1 ); /* 沒有附加的平移*//* 銷毀 "image" 指向的副本 */ if ( !error ) { FT_BitmapGlyph bit = (FT_BitmapGlyph)glyphs[n].image; draw_bitmap( &bit->bitmap, bit->left, var.yres - bit->top); FT_Done_Glyph( glyphs[n].image ); } } } int main(int argc, char **argv) { wchar_t * str1 = L"陳志朋gif"; wchar_t * str2 = L"hello the world"; FT_BBox bbox; FT_Glyph glyph; FT_Library library; FT_Face face; FT_Vector pen; /* untransformed origin */ FT_GlyphSlot slot; FT_Matrix matrix; /* transformation matrix */ TGlyph glyphs[MAX_GLYPHS]; /* ??? */ FT_UInt num_glyphs; int error; double angle; int i; int line_box_ymin = 10000; int line_box_ymax = 0; int line_box_width; int line_box_hight; if(argc != 2) { printf("Usage : %s <font_file> <angle>\n",argv[0]); return -1; } fd_fb = open("/dev/fb0",O_RDWR); if(fd_fb < 0) { printf("can't open /dev/fb0 \n"); return -1; } if(ioctl(fd_fb, FBIOGET_VSCREENINFO, &var)) { printf("can't get var \n"); return -1; } if(ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix)) { printf("can't get fix \n"); return -1; } screen_size = var.xres * var.yres * var.bits_per_pixel / 8; //單位位元組 line_width = var.xres * var.bits_per_pixel / 8; pixel_width = var.bits_per_pixel / 8; fb_mem = (unsigned char *)mmap(NULL, screen_size, \ PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0); if(fb_mem == (unsigned char *) -1) { printf("can't mmap \n"); return -1; } memset(fb_mem, 0, screen_size); /* 顯示矢量文字 */ error = FT_Init_FreeType( &library ); /* initialize library */ /* error handling omitted */ error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */ /* error handling omitted */ slot = face->glyph; FT_Set_Pixel_Sizes(face, 24, 0); num_glyphs = Get_Glphs_Frm_Wstr(face, str1, glyphs); compute_string_bbox(glyphs, num_glyphs, &bbox); line_box_width = bbox.xMax - bbox.xMin; line_box_hight = bbox.yMax - bbox.yMin; pen.x = (var.xres - line_box_width) / 2 * 64; pen.y = (var.yres - line_box_hight) / 2 * 64; Draw_Glyphs(glyphs, num_glyphs, pen); num_glyphs = Get_Glphs_Frm_Wstr(face, str2, glyphs); compute_string_bbox(glyphs, num_glyphs, &bbox); line_box_width = bbox.xMax - bbox.xMin; line_box_hight = bbox.yMax - bbox.yMin; pen.x = (var.xres - line_box_width) / 2 * 64; pen.y = pen.y - 24 * 64; Draw_Glyphs(glyphs, num_glyphs, pen); }
中心雙行輸出
4.6.2 渲染一個已變換的字形序列
無論如何,如果我們想重用字形來以不同的角度或變換方式繪製字元串,直接變換序列中的字形都不是
一個好主意。更好的方法是在字形被渲染前執行放射變換,如下麵的代碼所示:
FT_Vector start;
FT_Matrix transform;
/* 獲取原始字形序列的 bbox */
compute_string_bbox( &string_bbox );
/* 計算整數象素表示的字元串尺度 */
string_width = (string_bbox.xMax - string_bbox.xMin) / 64;
string_height = (string_bbox.yMax - string_bbox.yMin) / 64;
/* 設置 26.6 笛卡兒空間表示的筆起始位置 */
start.x = ( ( my_target_width - string_width ) / 2 ) * 64;
start.y = ( ( my_target_height - string_height ) / 2 ) * 64;
/* 設置變換(旋轉) */
matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );
for ( n = 0; n < num_glyphs; n++ )
{
FT_Glyph image;
FT_Vector pen;
FT_BBox bbox;
/* 創建原始字形的副本 */
error = FT_Glyph_Copy( glyphs[n].image, &image );
if ( error ) continue;
/* 變換副本(這將平移它到正確的位置) */
FT_Glyph_Transform( image, &matrix, &start );
/* 檢查邊界框;如果已變換的字形圖像不在*/
/* 我們的目標錶面中,我們可以避免渲染它 */
FT_Glyph_Get_CBox( image, ft_glyph_bbox_pixels, &bbox );
if ( bbox.xMax <= 0 || bbox.xMin >= my_target_width ||
bbox.yMax <= 0 || bbox.yMin >= my_target_height )
continue;
/* 把字形圖像轉換為點陣圖(銷毀字形的副本!) */
error = FT_Glyph_To_Bitmap(
&image,
FT_RENDER_MODE_NORMAL,
0, /* 沒有附加的平移*/
1 ); /* 銷毀 "image" 指向的副本 */
if ( !error )
{
FT_BitmapGlyph bit = (FT_BitmapGlyph)image;
my_draw_bitmap( bitmap->bitmap,
bitmap->left,
my_target_height - bitmap->top );
FT_Done_Glyph( image );
}
}
這份代碼相對於原始版本有少許改變:
z 我們沒改變原始的字形圖像,而是變換該字形圖像的拷貝。
z 我們執行“剪取”操作以處理渲染和繪製的字形不在我們的目標錶面(surface)的情況。
z 當調用 FT_Glyhp_To_Bitmap 時,我們總是銷毀字形圖像的拷貝,這是為了銷毀已變換的圖像。註意,即
使當這個函數返回錯誤碼,該圖像依然會被銷毀(這就是為什麼 FT_Done_Glyph 只在複合語句中被調用
的原因)。
z 平移字形序列到起始筆位置集成到 FT_Glyph_Transform 函數,而不是 FT_Glyph_To_Bitmap 函數。
可以多次調用這個函數以渲染字元串到不同角度的,或者甚至改變計算 start 的方法以移動它到另外的地
方。無論如何,要註意通常的實現會使用一個字形緩衝以減少記憶體消耗。據個例子,讓我們假定我們的字元