freetypelcd多行显示代码分析

来源:互联网 发布:丽晶软件 编辑:程序博客网 时间:2024/06/18 07:16

#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 FT_FREETYPE_H
#include FT_GLYPH_H

int fd_fb;
struct fb_var_screeninfo var; /* Current var */
struct fb_fix_screeninfo fix; /* Current fix */
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;


/* color : 0x00RRGGBB */
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 int *pen_32; 

 unsigned int red, green, blue; 

 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:
  {
   /* 565 */
   red   = (color >> 16) & 0xff;
   green = (color >> 8) & 0xff;
   blue  = (color >> 0) & 0xff;
   color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
   //red取高5位 green取高6位 blue 取高5位
   *pen_16 = color;
   break;
  }
  case 32:
  {
   *pen_32 = color;
   break;
  }
  default:
  {
   printf("can't surport %dbpp\n", var.bits_per_pixel);
   break;
  }
 }
}

 

/* Replace this function with something useful. */

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]);
    }
  }
}

//argc 参数个数 argv 参数
int main(int argc, char **argv)
{
 wchar_t *wstr1 = L"百问网gif";
 wchar_t *wstr2 = L"www.100ask.net";

 FT_Library   library;
 FT_Face    face;
 int error;
    FT_Vector     pen;
 FT_GlyphSlot  slot;
 int i;

 if (argc != 2)
 {
  printf("Usage : %s <font_file>\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;
 }
 //使用ioctl操作驱动程序
 //通过fbmem.c 中的FBIOGET_VSCREENINFO FBIOGET_FSCREENINFO 获取屏幕信息
 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("can't mmap\n");
  return -1;
 }

 /* 清屏: 全部设为黑色 */
 memset(fbmem, 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 - lcd_y = var.yres - 24
  */
 pen.x = 0 * 64;
 pen.y = (var.yres - 24) * 64;

 for (i = 0; i < wcslen(wstr1); i++)
 {
     /* set transformation */
     FT_Set_Transform( face, 0, &pen);

     /* load glyph image into the slot (erase previous one) */
     error = FT_Load_Char( face, wstr1[i], FT_LOAD_RENDER );
  if (error)
  {
   printf("FT_Load_Char error\n");
   return -1;
  }
  
     draw_bitmap( &slot->bitmap,
                  slot->bitmap_left,
                  var.yres - slot->bitmap_top);

  /* increment pen position */
  pen.x += slot->advance.x;
  //pen.y += slot->advance.y;

 }
 return 0; 
}

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信老是屏蔽网站怎么办 华帝售后没人理怎么办 新开的网店没生意怎么办 淘小二介入买家举证不全怎么办 新股申购中签后钱不够怎么办 买的东西收不到怎么办? 拼多多按错收货怎么办 团购招生做到一半不如意怎么办 网购付款后卖家没有货怎么办 淘宝退货商家不同意退款怎么办 淘宝确认收货后卖家拒绝退款怎么办 淘宝卖家不同意退款怎么办 淘宝仅退款卖家不处理怎么办 淘宝申请退款卖家不处理怎么办 快递没收到货要退款怎么办 发票给了不给钱怎么办 付款后不给发票怎么办 供货商不给发票怎么办 刚生过孩子太胖买衣服怎么办 黑色牛仔裤洗的发白怎么办 蘑菇街手机丢了怎么办 黑衣服上全是白毛毛怎么办 支付宝注销了钱怎么办 网上买东西手机号写错了怎么办 二类工资卡过万怎么办 淘宝发货地址写错怎么办 淘宝发货地址写错了怎么办 淘宝不能代付了怎么办 支付宝付款码被盗刷怎么办 地方选举追究不到相关责任人怎么办 天正建筑画个直线找不到怎么办 Wi-Fi模块不支持多播怎么办? 魅族手机屏幕点不动怎么办 uc打开网页很慢怎么办 京东手机号码无法登录怎么办 织梦系统网站没收录怎么办 电脑开机出现一堆乱码怎么办 电脑文件夹出现乱码打不开怎么办 电脑出现f1和f2怎么办 电脑中韩文内容显示乱码怎么办 入驻shopee没身份证怎么办