DirectFB 之 字体显示
通过本文,可以简单地了解directfb字体内部运行机制。
简介
SetFont函数,是每次写字体前必须调用的一个函数,否则directfb程序将会报错。这个函数是将某种字体与某个surface相关联。GetAscender与GetHeight调用该函数可以有效的确定字体的ascend和fontheight,这两个函数函数有利于我们的排版。
GetStringBreak ( IDirectFBFont * thiz, const char * text, int bytes, int max_width, int * ret_width, int * ret_str_length, const char ** ret_next_line );这个函数同样对我们很有用了,它可以确定某一行有多少个字符,该行是否写满,如果没有写满的话, ret_next_line返回下一行第一个字符指针。
ret_str_length 返回字母个数,或者汉字的个数,你不能直接在DrawString()函数中直接使用这个值,否则有可能出错,解决的办法如下:假如你能确定你字符的编码方式的话,假如是ascii编码,你可以直接使用该值,如果是Unicode编码,你需要做*2操作,最直接的办法是 假如ret_next_line不为空的话,(ret_next_line-text)*sizeof(char)这个值直接可以用了,注意,一定是不为空时才能这样。
通过这三个函数,你可以轻松的实现界面的字体设置,排版,换行功能。
DFBResult DrawString ( IDirectFBSurface * thiz, const char * text, int bytes, int x, int y, DFBSurfaceTextFlags flags );在指定位置根据指定的颜色,按照指定的对其方式flags画出某一个字符串text
示例
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <directfb.h> static IDirectFB *dfb = NULL; static IDirectFBSurface *primary = NULL; static int screen_width = 0; static int screen_height = 0; static IDirectFBFont *font_decker = NULL; static IDirectFBFont *font_cuprum = NULL; static IDirectFBFont *font_monaco = NULL; static char *text1 = "DirectFB rulez! - decker.ttf"; static char *text2 = "DirectFB rulez! - cuprum.otf"; static char *text3 = "DirectFB rulez! - monaco.ttf"; #define DFBCHECK(x...) { \ DFBResult err = x; \ if(err != DFB_OK) { \ fprintf(stderr, "%s <%d>:\n\t", __FILE__, __LINE__); \ DirectFBErrorFatal(#x, err); \ } \ } int main(int argc, char **argv) { DFBFontDescription font_dsc; DFBSurfaceDescription dsc; DirectFBInit(&argc, &argv); DirectFBCreate(&dfb); dfb->SetCooperativeLevel(dfb, DFSCL_FULLSCREEN); dsc.flags = DSDESC_CAPS; dsc.caps = DSCAPS_PRIMARY | DSCAPS_FLIPPING; dfb->CreateSurface(dfb, &dsc, &primary); primary->GetSize(primary, &screen_width, &screen_height); font_dsc.flags = DFDESC_HEIGHT; font_dsc.height = 48; DFBCHECK(dfb->CreateFont(dfb, "./data/decker.ttf", &font_dsc, &font_decker)); DFBCHECK(dfb->CreateFont(dfb, "./data/Cuprum.otf", &font_dsc, &font_cuprum)); DFBCHECK(dfb->CreateFont(dfb, "./data/Monaco.ttf", &font_dsc, &font_monaco)); primary->SetColor(primary, 0x0, 0x0, 0x0, 0xff); primary->FillRectangle(primary, 0, 0, screen_width, screen_height); primary->SetColor(primary, 0x80, 0x0, 0x20, 0xff); primary->SetFont(primary, font_decker); primary->DrawString(primary, text1, -1, 30, 100, DSTF_LEFT); primary->SetFont(primary, font_cuprum); primary->DrawString(primary, text2, -1, 30, 200, DSTF_LEFT); primary->SetFont(primary, font_monaco); primary->DrawString(primary, text3, -1, 30, 300, DSTF_LEFT); primary->Flip(primary, NULL, 0); sleep(10); font_decker->Release(font_decker); font_cuprum->Release(font_cuprum); font_monaco->Release(font_monaco); primary->Release(primary); dfb->Release(dfb); return 0; }
posted on 2016-05-24 21:13 YoungerChina 阅读(1170) 评论(0) 编辑 收藏 举报