luat_gtfont.c 7.9 KB

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