Chinaunix首页 | 论坛 | 博客
  • 博客访问: 312214
  • 博文数量: 68
  • 博客积分: 1501
  • 博客等级: 上尉
  • 技术积分: 1010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-30 09:52
文章分类

全部博文(68)

文章存档

2010年(1)

2009年(67)

我的朋友

分类:

2009-03-09 11:07:00

转自 UNIXCHINA.COM 论坛 tbag-well 大作
 
 
在图象开发中,字体显示是很关键的一个环节,在嵌入式开发中,使用点阵字的占大多数,虽然有少部分使用矢量字体,但是也需要一定的资源,所以,这里简单的介绍一下点阵字的显示。而点阵字又分有很多中,包括12x6的,16x8的和24x24,16x16的点阵字,下面介绍一下hzk16的点阵字的显示。

        HZK16字库是符合GB2312标准的16×16点阵字库,HZK16的GB2312-80支持的汉字有6763个,符号682个。其中一级汉字有3755个,按声序排列,二级汉字有3008个,按偏旁部首排列。在一些应用场合根本用不到这么多汉字字模,所以在应用时就可以只提取部分字体作为己用。

HZK16字库里的16×16汉字一共需要256个点来显示,也就是说需要32个字节才能达到显示一个普通汉字的目的。

一个GB2312汉字是由两个字节编码的,范围为A1A1~FEFE。A1-A9为符号区,B0到F7为汉字区。每一个区有94个字符(注意:这只是编码的许可范围,不一定都有字型对应,比如符号区就有很多编码空白区域)。下面以汉字“我”为例,介绍如何在HZK16文件中找到它对应的32个字节的字模数据。

前面说到一个汉字占两个字节,这两个中前一个字节为该汉字的区号,后一个字节为该字的位号。其中,每个区记录94个汉字,位号为该字在该区中的位置。所以要找到“大”在hzk16库中的位置就必须得到它的区码和位码。

区码:区号(汉字的第一个字节)-0xa0    (因为汉字编码是从0xa0区开始的,所以文件最前面就是从0xa0区开始,要算出相对区码)

位码:位号(汉字的第二个字节)-0xa0

这样就可以得到汉字在HZK16中的绝对偏移位置:

offset=(94*(区码-1)+(位码-1))*32                 

注解:1、区码减1是因为数组是以0为开始而区号位号是以1为开始的

      2、(94*(区号-1)+位号-1)是 汉字在字库中的偏移地址

      3、最后乘以32是 汉字的字模信息(一个汉字要有32个字节),

       
原理介绍到这里已经告一段落,接下来演示一下代码和调试:

CODE:
  1 /***************************************************
  2  *              fonttest.c
  3  *      Author: T-bagwell
  4  *      Compile: gcc fonttest.c -o fonttest
  5  * ************************************************/
  6
  7 #include
  8 #include
  9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15
16 int main()
17 {
18         int i,j,k;
19         unsigned char incode[3];
20         incode[0]=0xb4;
21         incode[1]=0xf3;
22         unsigned char qh,wh;
23         unsigned long offset;
24         printf("The Font Code is %#x,%#x\n",incode[0],incode[1]);
25         qh=incode[0] - 0xa0;
26         wh=incode[1] - 0xa0;
27         offset = (94*(qh-1)+(wh-1))*32;
28
29         FILE    *HZK;
30         char    *mat=(char *)malloc(32);
31         memset(mat,0,32);
32         if((HZK=fopen("/root/HZK16","rb"))==NULL)
33         {
34                 printf("Can't    Open    hzk16\n");
35                 exit(0);
36         }
37         fseek(HZK,offset,SEEK_SET);
38         fread(mat,32,1,HZK);
39
40         for(j=0;j<16;j++)
41         {
42                 for(i=0;i<2;i++)
43                 {
44                         for(k=0;k<8;k++)
45                         {
46                                 if(j==7);
47                                 if(((mat[j*2+i]>>(7-k)) & 0x1) != 0)
48                                 {
49                                         printf("x");  // 填充位
50                                 }
51                                 else
52                                 {
53                                         printf(" ");
54                                 }
55
56                         }
57                         //      printf("\n");
58                 }
59                 printf("\n");
60
61         }
62         fclose(HZK);
63 }

编译完代码以后执行应用程序,会在终端中生成一个图象,如下图:


字体在frambuffer中显示只要将对应的字模输出到frambuffer中即可,显示坐标和区域与现在jpeg图片实现一样
 
 
CODE:
1        /**************************************************
2         *                example5.c
3         *        Author:        T-bagwell
4         *       
5         *        Compile:gcc -Wall example5.c -o example5
6         *************************************************/
7                #include
8                #include
9                #include
10        #include
11        #include
12        #include
13        #include
14        #include
15        #include
16        #include
17        #include
18       
19        int print_screen(short *buf,int width,int height);
20        void draw_hline(short *buffer,int x,int y,int maxy,int maxx);
21        char *fb_addr;
22        unsigned fb_size;
23       
24        int main(int argc, char *argv[])
25        {
26                int screen_fbd=0;
27                struct fb_fix_screeninfo fb_fix;
28                struct fb_var_screeninfo fb_var;
29                char *env=NULL;
30                short *color_white;
31               
32                if(!(env = getenv("FRAMEBUFFER")))
33                {
34                        env = "/dev/fb0";
35                }
36       
37                screen_fbd = open(env, O_RDWR);
38               
39                if(screen_fbd < 0)
40                {
41                        return 0;
42                }
43               
44                if(ioctl(screen_fbd, FBIOGET_FSCREENINFO, &fb_fix) == -1)
45                {
46                        close(screen_fbd);
47                        return 0;
48                }       
49       
50                if(ioctl(screen_fbd, FBIOGET_VSCREENINFO, &fb_var) == -1)
51                {
52                        close(screen_fbd);
53                        return 0;
54                }
55               
56                fb_size = fb_var.yres * fb_fix.line_length;       
57   fb_addr = (char *)mmap(NULL, fb_size, PROT_READ|PROT_WRITE,MAP_SHARED, screen_fbd, 0);
58       color_white=(short *)malloc(sizeof(fb_var.yres * fb_fix.line_length);
59                draw_hline(color_white,0,30,fb_var.yres,fb_var.xres);
60                print_screen(color_white,fb_var.xres,fb_var.yres);
61                if (!fb_addr)
62                        exit(1);
63                free(color_white);       
64                return 0;
65        }
66       
67        /*********************************************************
68         * function:        print_screen
69         *args
70         *        buf:        the address of the buffer
71         *        width:        the width of the framebuffer
72         *         height:        the height of the framebuffer
73         ********************************************************/
74        int print_screen(short *buf,int width,int height)
75        {
76                        short *t_data = (short *)buf;
77                        short *t_fb_addr = (short *)fb_addr;
78                        int bytew= width<<1;
79                        while(--height >= 0)        
80                        {               
81                  memcpy(t_fb_addr,t_data,bytew);                               
82                                t_fb_addr += width;               
83                                t_data += width;       
84                        }
85                return 0;
86        }
87       
88        /*********************************************************
89         * function:        draw_hline       
90         *args
91         *        line_buffer:        the address of the buffer
92         *        x:                the x of the line start point on the framebuffer
93         *         y:                the y of the line start point on the framebuffer
94         *         maxy:                the height of the framebuffer
95         *         maxx:                the width of the framebuffer
96         ********************************************************/
97        void draw_hline(short *line_buffer,int x,int y,int maxy,int maxx)
98        {
99                int i=0,j=0;
100       
101                for(j=0;j102                {
103                        if(j==y)
104                        {
105                                for(i=0;i106                                {
107                                        *line_buffer++=0xf800;
108                                }
109                        }
110                        else
111                        {
112                                for(i=0;i113                                {
114                                        *line_buffer++=0xffff;
115                                }
116                        }
117                }
118                return ;
119        }

上述代码实现了在屏幕中坐标为x坐标为0,y坐标为30处画一条红色的横线,当然,颜色值可以作为参数进行传递,只要对上面的函数进行少量的修改即可。手累了,先写这么点吧。转帖请注明

参考部分:  baidu贴吧(RGB部分的一小部分)
             frambuffer部分在Linuxkernel的Documents部分有详细讲解

阅读(1628) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-01-19 14:37:48

还行,不过Framebuffer 下显示字符,怎么没有代码的??? framebuffer 显示字体?????