里飞网

 找回密码
 立即注册
查看: 7069|回复: 0
打印 上一主题 下一主题

裸机解析并显示emwin字体代码

[复制链接]

45

主题

290

帖子

2492

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2492
跳转到指定楼层
楼主
发表于 2019-11-15 09:15:29 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

裸机解析并显示emwin字体.rar (3.11 KB, 下载次数: 1051)


增加个utf-8编码支持
  1. //返回解码utf8的字节数
  2. uint32_t getByteNumOfDecodeUtf8(uint8_t byte){
  3.         //byte应该是utf8的最高1字节,如果指向了utf8编码后面低字节部分则返回0
  4.         if((byte & 0xc0)==0x80) return 0;  //1000 0000
  5.         if((byte & 0xf8)==0xf0) return 4;  //1111 0000
  6.         if((byte & 0xf0)==0xf0) return 3;  //1110 0000
  7.         if((byte & 0xe0)==0xc0) return 2;  //1100 0000
  8.         return 1;          //ASCII码
  9. }
  10. //解码以bytePtr为起始地址的utf8序列,其最大长度为length,若不是utf8序列就返回-1
  11. int decodeUtf8(const uint8_t* bytePtr, uint32_t length){
  12.         //若是1字节的ascii码: 0xxxxxxx
  13.         if(*bytePtr <= 0x7f) return *bytePtr;
  14.         
  15.         int value;
  16.         uint32_t remainingBytes;

  17.         //先读取高1字节
  18.         //根据高1字节的高n位判断相应字节数的utf8编码
  19.         if((*bytePtr & 0xe0)==0xc0){
  20.                 //若是2字节的utf8
  21.                 value = *bytePtr & 0x1f; //记录后面的5位有效位
  22.                 remainingBytes = 1;
  23.         }        
  24.         else if((*bytePtr & 0xf0)==0xe0){
  25.                 //若是3字节的utf8
  26.                 value = *bytePtr & 0x0f; //记录后面的4位有效位
  27.                 remainingBytes = 2;
  28.         }
  29.         else if((*bytePtr & 0xf8)==0xf0){
  30.                 //若是4字节的utf8
  31.                 value = *bytePtr & 0x07;
  32.                 remainingBytes = 3;
  33.         }
  34.         else {return -1;} //非法编码
  35.         
  36.         //如果utf8被斩断了就不再读下去了
  37.         if(remainingBytes > length - 1){return -1;}

  38.         //再读取低字节中的数据
  39.         while(remainingBytes > 0){
  40.                 bytePtr++;
  41.                 remainingBytes--;
  42.                 //高两位必须是10
  43.                 if((*bytePtr & 0xc0) != 0x80){return -1;}
  44.                 //从次高字节往低字节读
  45.                 value = value << 6 | (*bytePtr & 0x3f); //value左移6为写入6位有效位
  46.         }
  47.         return value;   //返回解码得到的value值
  48. }
  49. //显示字符串,支持中英文显示
  50. void gui_disp_string(uint16_t x, uint16_t y, const uint8_t *str)
  51. {
  52.     while(*str)
  53.     {   
  54.         //GBK格式
  55.         // if(*str < 0xA0)
  56.         // {
  57.             // gui_disp_char(&x, &y, *str++);
  58.         // }
  59.         // else
  60.         // {
  61.             // uint16_t ch = (uint16_t)*str++ << 8;
  62.             // gui_disp_char(&x, &y, (ch | (uint16_t)*str++));
  63.         // }
  64.         //UTF-8格式
  65.         int data = decodeUtf8(str, strlen(str));
  66.         if(data==-1){
  67.             return;
  68.         }
  69.         else{
  70.             gui_disp_char(&x, &y, data);
  71.             int pos = getByteNumOfDecodeUtf8(*str);
  72.             if(pos==0){
  73.                 return;
  74.             }
  75.             str += pos;
  76.         }
  77.     }
  78. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|里飞网  

GMT+8, 2024-4-24 09:34 , Processed in 0.094682 second(s), 6 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表