里飞网

标题: 裸机解析并显示emwin字体代码 [打印本页]

作者: 阿里兄    时间: 2019-11-15 09:15
标题: 裸机解析并显示emwin字体代码
(, 下载次数: 741)
(, 下载次数: 1056)


增加个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. }
复制代码








欢迎光临 里飞网 (http://dz.lfly.xyz/) Powered by Discuz! X3.3