显示文字这里我用了freetype库。
以左上角显示两行文字:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "show_font.h"
#include
#include FT_FREETYPE_H
#include
unsigned char *hzkmem;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;
struct fb_var_screeninfo var;
void lcd_put_pixel( int x, int y, unsigned int color )
{
unsigned char *pen_8 = fbmem +y*line_width + x*pixel_width;
unsigned short *pen_16;
unsigned short *pen_32;
unsigned char red,green,blue;
pen\_16 = (unsigned short \*)pen\_8;
pen\_32 = (unsigned short \*)pen\_8;
switch( pixel\_width\*8 )
{
case 8:
\*pen\_8 = color;
break;
case 16:
/\* 565 \*/
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;
default:
printf("can't support %ddpp\\n",pixel\_width\*8 );
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;
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 )
{
int hzk_fd;
int fd_fb;
struct fb_fix_screeninfo fix;
int screen\_size;
FT\_Library library;
FT\_Error error;
FT\_Face face;
FT\_Matrix matrix; /\* transformation matrix \*/
FT\_Vector pen; /\* untransformed origin \*/
FT\_BBox bbox;
FT\_Glyph glyph;
int i;
double angle;
wchar\_t \*chinese\_char = L"周zhou";
wchar\_t \*chinese\_char2 = L"嵌入式Linux";
int line\_box\_ymin=800;
int line\_box\_ymax=0;
struct stat hzk\_stat;
fd\_fb = open("/dev/fb0", O\_RDWR );
if( fd\_fb<0 )
{
perror("oepn failed");
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;
}
line\_width = var.xres \* var.bits\_per\_pixel / 8;
pixel\_width = var.bits\_per\_pixel / 8;
screen\_size = var.xres \* var.yres \* var.bits\_per\_pixel / 8;
fbmem = (unsigned char \*)mmap( NULL, screen\_size, PROT\_READ | PROT\_WRITE, MAP\_SHARED,fd\_fb,0 );
if( fbmem == (unsigned char \*)-1 )
{
printf("mmap is failed\\n");
return -1;
}
memset( fbmem, 0, screen\_size );
if( argc != 2 )
{
printf("Usage: %s <font\_file>\\n", argv\[0\]);
return -1;
}
error = FT\_Init\_FreeType( &library ); /\* initialize library \*/
/\* error handling omitted \*/
error = FT\_New\_Face( library, argv\[1\], 0, &face ); /\* create face object \*/
/\* use 50pt at 100dpi \*/
error = FT\_Set\_Pixel\_Sizes( face, 30, 0 ); /\* set character size \*/
angle = ( 0.0 / 360 ) \* 3.14159 \* 2; /\* use 25 degrees \*/
/\* set up matrix \*/
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 );
/\* the pen position in 26.6 cartesian space coordinates; \*/
/\* start at (0,24) relative to the upper left corner \*/
pen.x = (0) \* 64;
pen.y = ( var.yres - 24 ) \* 64;
for( i=0; i<wcslen(chinese\_char); i++ )
{
/\* set transformation \*/
FT\_Set\_Transform( face, &matrix, &pen );
/\* load glyph image into the slot (erase previous one) \*/
error = FT\_Load\_Char( face, chinese\_char\[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;
draw\_bitmap( &face->glyph->bitmap,
face->glyph->bitmap\_left,
var.yres - face->glyph->bitmap\_top );
/\* increment pen position \*/
pen.x += face->glyph->advance.x;
pen.y += face->glyph->advance.y;
}
/\* the pen position in 26.6 cartesian space coordinates; \*/
/\* start at (0,24) relative to the upper left corner \*/
pen.x = (0) \* 64;
pen.y = ( var.yres-( line\_box\_ymax-line\_box\_ymin+24) ) \* 64;
for( i=0; i<wcslen(chinese\_char2); i++ )
{
/\* set transformation \*/
FT\_Set\_Transform( face, &matrix, &pen );
/\* load glyph image into the slot (erase previous one) \*/
error = FT\_Load\_Char( face, chinese\_char2\[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;
draw\_bitmap( &face->glyph->bitmap,
face->glyph->bitmap\_left,
var.yres - face->glyph->bitmap\_top );
/\* increment pen position \*/
pen.x += face->glyph->advance.x;
pen.y += face->glyph->advance.y;
}
return 0;
}
在屏幕的中间左右上下对称显示:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "show_font.h"
#include
#include FT_FREETYPE_H
#include
typedef struct TGlyph_
{
FT_UInt index; /* glyph index */
FT_Vector pos; /* glyph origin on the baseline */
FT_Glyph image; /* glyph image */
} TGlyph, *PGlyph;
#define MAX_GLYPHS 100
unsigned char *hzkmem;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;
struct fb_var_screeninfo var;
void lcd_put_pixel( int x, int y, unsigned int color )
{
unsigned char *pen_8 = fbmem +y*line_width + x*pixel_width;
unsigned short *pen_16;
unsigned short *pen_32;
unsigned char red,green,blue;
pen\_16 = (unsigned short \*)pen\_8;
pen\_32 = (unsigned short \*)pen\_8;
switch( pixel\_width\*8 )
{
case 8:
\*pen\_8 = color;
break;
case 16:
/\* 565 \*/
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;
default:
printf("can't support %ddpp\\n",pixel\_width\*8 );
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;
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_Glyphs_From_Wstr( FT_Face face, wchar_t* wstr, TGlyph glyphs[] )
{
int n;
PGlyph glyph = glyphs;
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\] );
/\* store current pen position \*/
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;
/\* translate the glyph image now \*/
FT\_Glyph\_Transform( glyph->image, 0, &glyph->pos );
pen\_x += face->glyph->advance.x ;
/\* increment number of glyphs \*/
glyph++;
}
/\* count number of glyphs loaded \*/
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 );
/\* convert glyph image to bitmap (destroy the glyph copy!) \*/
error = FT\_Glyph\_To\_Bitmap( &glyphs\[n\].image, FT\_RENDER\_MODE\_NORMAL,
0, /\* no additional translation \*/
1 ); /\* destroy copy in "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 )
{
int hzk_fd;
int fd_fb;
struct fb_fix_screeninfo fix;
int screen\_size;
FT\_Library library;
FT\_Error error;
FT\_Face face;
FT\_Matrix matrix; /\* transformation matrix \*/
FT\_Vector pen; /\* untransformed origin \*/
FT\_BBox bbox;
TGlyph glyphs\[MAX\_GLYPHS\]; /\* glyphs table \*/
PGlyph glyph; /\* current glyph in table \*/
FT\_UInt num\_glyphs;
int i;
double angle;
wchar\_t \*chinese\_char = L"周zhou";
wchar\_t \*chinese\_char2 = L"嵌入式Linux";
int line\_box\_ymin=800;
int line\_box\_ymax=0;
int line\_box\_width;
int line\_box\_hight;
struct stat hzk\_stat;
fd\_fb = open("/dev/fb0", O\_RDWR );
if( fd\_fb<0 )
{
perror("oepn failed");
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;
}
line\_width = var.xres \* var.bits\_per\_pixel / 8;
pixel\_width = var.bits\_per\_pixel / 8;
screen\_size = var.xres \* var.yres \* var.bits\_per\_pixel / 8;
fbmem = (unsigned char \*)mmap( NULL, screen\_size, PROT\_READ | PROT\_WRITE, MAP\_SHARED,fd\_fb,0 );
if( fbmem == (unsigned char \*)-1 )
{
printf("mmap is failed\\n");
return -1;
}
memset( fbmem, 0, screen\_size );
if( argc != 2 )
{
printf("Usage: %s <font\_file>\\n", argv\[0\]);
return -1;
}
error = FT\_Init\_FreeType( &library ); /\* initialize library \*/
/\* error handling omitted \*/
error = FT\_New\_Face( library, argv\[1\], 0, &face ); /\* create face object \*/
/\* use 50pt at 100dpi \*/
error = FT\_Set\_Pixel\_Sizes( face, 24, 0 ); /\* set character size \*/
num\_glyphs = Get\_Glyphs\_From\_Wstr( face, chinese\_char, 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\_Glyphs\_From\_Wstr( face, chinese\_char2, 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 );
return 0;
}
sd
手机扫一扫
移动阅读更方便
你可能感兴趣的文章