luat_gtfont.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "luat_base.h"
  2. #include "luat_spi.h"
  3. #include "luat_lcd.h"
  4. #include "epdpaint.h"
  5. #include "luat_mem.h"
  6. #include "luat_gtfont.h"
  7. #define LUAT_GT_DEBUG 0
  8. #define LUAT_LOG_TAG "gt"
  9. #include "luat_log.h"
  10. #ifdef LUAT_USE_LCD
  11. extern luat_color_t BACK_COLOR, FORE_COLOR;
  12. #else
  13. static luat_color_t BACK_COLOR = LCD_WHITE, FORE_COLOR = LCD_BLACK;
  14. #endif
  15. luat_spi_device_t* gt_spi_dev = NULL;
  16. unsigned char r_dat_bat(unsigned long address,unsigned long DataLen,unsigned char *pBuff) {
  17. if (gt_spi_dev == NULL)
  18. return 0;
  19. char send_buf[4] = {
  20. 0x03,
  21. (uint8_t)((address)>>16),
  22. (uint8_t)((address)>>8),
  23. (uint8_t)(address)
  24. };
  25. luat_spi_lock(gt_spi_dev->bus_id);
  26. luat_spi_device_transfer(gt_spi_dev, send_buf, 4, (char *)pBuff, DataLen);
  27. luat_spi_unlock(gt_spi_dev->bus_id);
  28. #if LUAT_GT_DEBUG
  29. LLOGD("r_dat_bat addr %08X len %d pBuff %X", address, DataLen,*pBuff);
  30. for(int i = 0; i < DataLen;i++){
  31. LLOGD("pBuff[%d]:0x%02X",i,pBuff[i]);
  32. }
  33. #endif
  34. return pBuff[0];
  35. }
  36. unsigned char CheckID(unsigned char CMD, unsigned long address,unsigned long byte_long,unsigned char *p_arr) {
  37. #if LUAT_GT_DEBUG
  38. LLOGD("CheckID CMD %02X addr %08X len %d p_arr %X", CMD, address, byte_long,*p_arr);
  39. #endif
  40. if (gt_spi_dev == NULL)
  41. return 0;
  42. char send_buf[4] = {
  43. CMD,
  44. (uint8_t)((address)>>16),
  45. (uint8_t)((address)>>8),
  46. (uint8_t)(address)
  47. };
  48. luat_spi_lock(gt_spi_dev->bus_id);
  49. luat_spi_device_transfer(gt_spi_dev, send_buf, 4, (char *)p_arr, byte_long);
  50. luat_spi_unlock(gt_spi_dev->bus_id);
  51. // return p_arr[0];
  52. return 1;
  53. }
  54. unsigned char gt_read_data(unsigned char* sendbuf , unsigned char sendlen , unsigned char* receivebuf, unsigned int receivelen)
  55. {
  56. if (gt_spi_dev == NULL)
  57. return 0;
  58. luat_spi_lock(gt_spi_dev->bus_id);
  59. luat_spi_device_transfer(gt_spi_dev, (const char *)sendbuf, sendlen,(char *)receivebuf, receivelen);
  60. luat_spi_unlock(gt_spi_dev->bus_id);
  61. #if LUAT_GT_DEBUG
  62. LLOGD("gt_read_data sendlen:%d receivelen:%d",sendlen,receivelen);
  63. for(int i = 0; i < sendlen;i++){
  64. LLOGD("sendbuf[%d]:0x%02X",i,sendbuf[i]);
  65. }
  66. for(int i = 0; i < receivelen;i++){
  67. LLOGD("receivebuf[%d]:0x%02X",i,receivebuf[i]);
  68. }
  69. #endif
  70. return 1;
  71. }
  72. uint32_t gt_unicode2gb18030(uint32_t unicode){
  73. if (unicode<0x80){
  74. return unicode;
  75. }
  76. // #if LUAT_GT_DEBUG
  77. // uint32_t gb18030 = U2G(unicode);
  78. // LLOGD("U2G unicode:0x%X gb18030:0x%X",unicode,gb18030);
  79. // #endif
  80. return U2G(unicode);
  81. }
  82. //横置横排显示
  83. unsigned int gtfont_draw_w(unsigned char *pBits,unsigned int x,unsigned int y,unsigned int size,unsigned int widt,unsigned int high,int(*point)(void*,uint16_t, uint16_t, uint32_t),void* userdata,int mode){
  84. unsigned int i=0,j=0,k=0,n=0,dw=0;
  85. unsigned char temp;
  86. int w = ((widt+7)>> 3);
  87. for( i = 0;i < high; i++){
  88. for( j = 0;j < w;j++){
  89. temp = pBits[n++];
  90. for(k = 0;k < 8;k++){
  91. if (widt < size){
  92. if (j==(w-1) && k==widt%8){
  93. break;
  94. }
  95. }
  96. if(((temp << k)& 0x80) == 0 ){//背景色
  97. // /* 显示一个像素点 */
  98. // if (mode == 0)point((luat_lcd_conf_t *)userdata, x+k+(j*8), y+i, lcd_str_bg_color);
  99. // else if (mode == 1)point((Paint *)userdata, x+k+(j*8), y+i, 0xFFFF);
  100. }else{
  101. /* 显示一个像素点 */
  102. if (dw<k+(j*8)) dw = k+(j*8);
  103. if (mode == 0)point((luat_lcd_conf_t *)userdata, x+k+(j*8), y+i, FORE_COLOR);
  104. else if (mode == 1)point((Paint *)userdata, x+k+(j*8), y+i, 0x0000);
  105. else if (mode == 2)point((u8g2_t *)userdata, x+k+(j*8), y+i, 0x0000);
  106. }
  107. }
  108. }
  109. if (widt < size){
  110. n += (size-widt)>>3;
  111. }
  112. }
  113. return ++dw;
  114. }
  115. /*----------------------------------------------------------------------------------------
  116. * 灰度数据显示函数 1阶灰度/2阶灰度/4阶灰度
  117. * 参数 :
  118. * data灰度数据; x,y=显示起始坐标 ; w 宽度, h 高度,grade 灰度阶级[1阶/2阶/4阶]
  119. *------------------------------------------------------------------------------------------*/
  120. unsigned int gtfont_draw_gray_hz (unsigned char *data,unsigned short x,unsigned short y,
  121. unsigned short w ,unsigned short h,unsigned char grade,
  122. int(*point)(void*,uint16_t, uint16_t, uint32_t),void* userdata,int mode){
  123. unsigned int temp=0,gray,x_temp=x,dw=0;
  124. unsigned int i=0,j=0,t;
  125. unsigned char c,*p;
  126. unsigned long color4bit,color2bit,color;
  127. t=(w+7)/8*grade;//
  128. p=data;
  129. if(grade==2){
  130. for(i=0;i<t*h;i++){
  131. c=*p++;
  132. for(j=0;j<4;j++){
  133. color2bit=(c>>6);//获取像素点的2bit颜色值
  134. if (color2bit!=0){
  135. if (FORE_COLOR == LCD_BLACK){
  136. color2bit=(3-color2bit)*255/3;//白底黑字
  137. gray=color2bit/8;
  138. color=(0x001f&gray)<<11; //r-5
  139. color=color|(((0x003f)&(gray*2))<<5); //g-6
  140. color=color|(0x001f&gray); //b-5
  141. temp=color;
  142. }else{
  143. temp=FORE_COLOR;
  144. }
  145. if(x<(x_temp+w)){
  146. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,temp);
  147. else if (mode == 1)point((Paint *)userdata, x,y,temp);
  148. // LLOGD("x_temp:%d,x:%d,dw:%d,temp:0x%x",x_temp,x,dw,temp);
  149. if (dw < x){
  150. dw = x;
  151. }
  152. }
  153. }
  154. c<<=2;
  155. x++;
  156. if(x>=x_temp+(w+7)/8*8) {x=x_temp; y++;}
  157. }
  158. }
  159. }else if(grade==4){
  160. for(i=0;i<t*h;i++){
  161. c=*p++;
  162. for(j=0;j<2;j++){
  163. color4bit=(c>>4);
  164. if (color4bit!=0){
  165. uint8_t disp = 0;
  166. if (FORE_COLOR == LCD_BLACK){
  167. color4bit= (15-color4bit)*255/15;//白底黑字
  168. gray=color4bit/8;
  169. color=((0x001f&gray))<<11; //r-5
  170. color=color|(((0x003f&(gray*2)))<<5); //g-6
  171. color=color|((0x001f&gray)); //b-5
  172. temp=color;
  173. disp = 1;
  174. }else if(color4bit>=7){
  175. temp=FORE_COLOR;
  176. disp = 1;
  177. }
  178. if(x<(x_temp+w)){
  179. if (disp){
  180. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,temp);
  181. else if (mode == 1)point((Paint *)userdata, x,y,temp);
  182. }
  183. // LLOGD("x_temp:%d,x:%d,dw:%d,temp:0x%x",x_temp,x,dw,temp);
  184. if (dw < x){
  185. // LLOGD("x_temp:%d,x:%d,dw:%d",x_temp,x,dw);
  186. dw = x;
  187. }
  188. }
  189. }
  190. c<<=4;
  191. x++;
  192. if(x>=x_temp+(w+7)/8*8) {x=x_temp; y++;}
  193. }
  194. }
  195. }else{ //1bits
  196. for(i=0;i<t*h;i++){
  197. c=*p++;
  198. for(j=0;j<8;j++){
  199. if(c&0x80) {
  200. if(x<(x_temp+w)){
  201. if (mode == 0)point((luat_lcd_conf_t *)userdata,x,y,FORE_COLOR);
  202. else if (mode == 1)point((Paint *)userdata, x,y,FORE_COLOR);
  203. if (dw < x){
  204. dw = x;
  205. }
  206. }
  207. }
  208. c<<=1;
  209. x++;
  210. if(x>=x_temp+(w+7)/8*8) {x=x_temp; y++;}
  211. }
  212. }
  213. }
  214. if (dw>x_temp){
  215. return dw-x_temp+1;
  216. }else{
  217. return w/2+1;
  218. }
  219. }
  220. unsigned int gtfont_get_width(unsigned char *p,unsigned int zfwidth,unsigned int zfhigh ){
  221. unsigned char *q;
  222. unsigned int i,j,tem,tem1,witdh1=0,witdh2=0;
  223. q=p;
  224. for (i=0;i<zfwidth/16;i++){
  225. tem=0;
  226. tem1=0;
  227. for (j=0;j<zfhigh;j++){
  228. tem=(*(q+j*(zfwidth/8)+i*2)|(*(q+1+j*(zfwidth/8)+i*2))<<8);
  229. tem1=tem1|tem;
  230. }
  231. witdh1=0;
  232. for (j=0;j<16;j++){
  233. if (((tem1<<j)&0x8000)==0x8000){
  234. witdh1=j;
  235. }
  236. }
  237. witdh2+=witdh1;
  238. }
  239. return witdh2;
  240. }
  241. #ifndef LUAT_COMPILER_NOWEAK
  242. LUAT_WEAK int GT_Font_Init(void) {
  243. return 1;
  244. }
  245. #endif