Jelajahi Sumber

update: eink库也改造成ufont模式. 现在只剩下字库生成和关联了

Wendal Chen 3 tahun lalu
induk
melakukan
08709eab2d

+ 54 - 247
components/eink/luat_lib_eink.c

@@ -28,8 +28,12 @@
 #include "u8g2_luat_fonts.h"
 #include "luat_zbuff.h"
 
-int8_t u8g2_font_decode_get_signed_bits(u8g2_font_decode_t *f, uint8_t cnt);
-uint8_t u8g2_font_decode_get_unsigned_bits(u8g2_font_decode_t *f, uint8_t cnt);
+#include "luat_ufont.h"
+extern const lv_font_t luat_font_sarasa_bold_16_lvgl;
+static lv_font_t* font_current;
+
+static size_t epd_w = 0;
+static size_t epd_h = 0;
 
 #define COLORED      0
 #define UNCOLORED    1
@@ -47,18 +51,32 @@ typedef struct eink_ctx
 
 static uint32_t ctx_index = 0;
 static eink_ctx_t *ctxs[2]; // 暂时只支持2种颜色, 有需要的话后续继续加
-static u8g2_t luat_eink_u8g2;
-
+// static u8g2_t luat_eink_u8g2;
 
-// static uint32_t eink_str_color;
 
-// static EPD epd;
-// static Paint paint = {0};
-// static unsigned char* frame_buffer = NULL;
+static void (eink_ui_draw_point)(void* userdata, int x, int y, uint32_t color) {
+    Paint_DrawPixel(&ctxs[ctx_index]->paint, x, y, color);
+}
+static void (eink_ui_draw_line)(void* userdata, int x1, int y1, int x2, int y2, uint32_t color) {
+    Paint_DrawLine(&ctxs[ctx_index]->paint, x1, y1, x2, y2, color);
+}
+static void (eink_ui_draw_block)(void* userdata, int x, int y, int w, int h, size_t bitw, void* data) {
+    LLOGD("lcd_ui_draw_block NOT support yet");
+}
+static void (eink_ui_draw_fill)(void* userdata, int x, int y, int w, int h, uint32_t color) {
+    Paint_DrawFilledRectangle(&ctxs[ctx_index]->paint, x, y, x+w, y+h, color);
+}
+static void (eink_ui_draw_flush)(void* userdata) {
+    // TODO 
+};
 
-// #ifdef econf
-// #undef econf
-// #endif
+const ui_draw_opts_t eink_ui_opts = {
+    .draw_point = eink_ui_draw_point,
+    .draw_line  = eink_ui_draw_line,
+    .draw_block = eink_ui_draw_block,
+    .draw_fill  = eink_ui_draw_fill,
+    .draw_flush = eink_ui_draw_flush
+};
 
 eink_conf_t econf = {0};
 
@@ -134,9 +152,6 @@ static int l_eink_setup(lua_State *L) {
       LLOGD("spi setup fail, eink init fail");
       return 0;
     }
-
-    size_t epd_w = 0;
-    size_t epd_h = 0;
     size_t colors = 0;
     if(status == 0)
     {
@@ -172,11 +187,7 @@ static int l_eink_setup(lua_State *L) {
         }
 
     }
-    u8g2_SetFont(&(luat_eink_u8g2), u8g2_font_opposansm8);
-    u8g2_SetFontMode(&(luat_eink_u8g2), 0);
-    u8g2_SetFontDirection(&(luat_eink_u8g2), 0);
-    //paint.inited = 1;
-    //LLOGD("epd init complete");
+    font_current = &luat_font_sarasa_bold_16_lvgl;
     lua_pushboolean(L, 1);
     return 1;
 }
@@ -253,60 +264,6 @@ static int l_eink_getWin(lua_State *L)
     return 3;
 }
 
-static uint8_t utf8_state;
-static uint16_t encoding;
-static uint16_t utf8_next(uint8_t b)
-{
-  if ( b == 0 )  /* '\n' terminates the string to support the string list procedures */
-    return 0x0ffff; /* end of string detected, pending UTF8 is discarded */
-  if ( utf8_state == 0 )
-  {
-    if ( b >= 0xfc )  /* 6 byte sequence */
-    {
-      utf8_state = 5;
-      b &= 1;
-    }
-    else if ( b >= 0xf8 )
-    {
-      utf8_state = 4;
-      b &= 3;
-    }
-    else if ( b >= 0xf0 )
-    {
-      utf8_state = 3;
-      b &= 7;
-    }
-    else if ( b >= 0xe0 )
-    {
-      utf8_state = 2;
-      b &= 15;
-    }
-    else if ( b >= 0xc0 )
-    {
-      utf8_state = 1;
-      b &= 0x01f;
-    }
-    else
-    {
-      /* do nothing, just use the value as encoding */
-      return b;
-    }
-    encoding = b;
-    return 0x0fffe;
-  }
-  else
-  {
-    utf8_state--;
-    /* The case b < 0x080 (an illegal UTF8 encoding) is not checked here. */
-    encoding<<=6;
-    b &= 0x03f;
-    encoding |= b;
-    if ( utf8_state != 0 )
-      return 0x0fffe; /* nothing to do yet */
-  }
-  return encoding;
-}
-
 static void drawFastHLine(Paint* conf,int16_t x, int16_t y, int16_t len, uint16_t color){
     Paint_DrawHorizontalLine(conf,x, y, len,color);
 }
@@ -314,140 +271,6 @@ static void drawFastVLine(Paint* conf,int16_t x, int16_t y, int16_t len, uint16_
     Paint_DrawVerticalLine(conf,x, y, len,color);
 }
 
-static void u8g2_draw_hv_line(u8g2_t *u8g2, int16_t x, int16_t y, int16_t len, uint8_t dir, uint16_t color){
-  switch(dir)
-  {
-    case 0:
-      drawFastHLine(&ctxs[ctx_index]->paint,x,y,len,color);
-      break;
-    case 1:
-        drawFastVLine(&ctxs[ctx_index]->paint,x,y,len,color);
-      break;
-    case 2:
-        drawFastHLine(&ctxs[ctx_index]->paint,x-len+1,y,len,color);
-      break;
-    case 3:
-        drawFastVLine(&ctxs[ctx_index]->paint,x,y-len+1,len,color);
-      break;
-  }
-}
-
-static void u8g2_font_decode_len(u8g2_t *u8g2, uint8_t len, uint8_t is_foreground){
-  uint8_t cnt;  /* total number of remaining pixels, which have to be drawn */
-  uint8_t rem;  /* remaining pixel to the right edge of the glyph */
-  uint8_t current;  /* number of pixels, which need to be drawn for the draw procedure */
-    /* current is either equal to cnt or equal to rem */
-  /* local coordinates of the glyph */
-  uint8_t lx,ly;
-  /* target position on the screen */
-  int16_t x, y;
-  u8g2_font_decode_t *decode = &(u8g2->font_decode);
-  cnt = len;
-  /* get the local position */
-  lx = decode->x;
-  ly = decode->y;
-  for(;;){
-    /* calculate the number of pixel to the right edge of the glyph */
-    rem = decode->glyph_width;
-    rem -= lx;
-    /* calculate how many pixel to draw. This is either to the right edge */
-    /* or lesser, if not enough pixel are left */
-    current = rem;
-    if ( cnt < rem )
-      current = cnt;
-    /* now draw the line, but apply the rotation around the glyph target position */
-    //u8g2_font_decode_draw_pixel(u8g2, lx,ly,current, is_foreground);
-    /* get target position */
-    x = decode->target_x;
-    y = decode->target_y;
-    /* apply rotation */
-    x = u8g2_add_vector_x(x, lx, ly, decode->dir);
-    y = u8g2_add_vector_y(y, lx, ly, decode->dir);
-    /* draw foreground and background (if required) */
-    if ( current > 0 )		/* avoid drawing zero length lines, issue #4 */
-    {
-      if ( is_foreground )
-      {
-	    u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, ctxs[ctx_index]->str_color);
-      }
-      else if ( decode->is_transparent == 0 )
-      {
-	// u8g2_draw_hv_line(u8g2, x, y, current, decode->dir, decode->bg_color);
-      }
-    }
-    /* check, whether the end of the run length code has been reached */
-    if ( cnt < rem )
-      break;
-    cnt -= rem;
-    lx = 0;
-    ly++;
-  }
-  lx += cnt;
-  decode->x = lx;
-  decode->y = ly;
-}
-static void u8g2_font_setup_decode(u8g2_t *u8g2, const uint8_t *glyph_data)
-{
-  u8g2_font_decode_t *decode = &(u8g2->font_decode);
-  decode->decode_ptr = glyph_data;
-  decode->decode_bit_pos = 0;
-
-  /* 8 Nov 2015, this is already done in the glyph data search procedure */
-  /*
-  decode->decode_ptr += 1;
-  decode->decode_ptr += 1;
-  */
-
-  decode->glyph_width = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_char_width);
-  decode->glyph_height = u8g2_font_decode_get_unsigned_bits(decode,u8g2->font_info.bits_per_char_height);
-
-}
-static int8_t u8g2_font_decode_glyph(u8g2_t *u8g2, const uint8_t *glyph_data){
-  uint8_t a, b;
-  int8_t x, y;
-  int8_t d;
-  int8_t h;
-  u8g2_font_decode_t *decode = &(u8g2->font_decode);
-  u8g2_font_setup_decode(u8g2, glyph_data);
-  h = u8g2->font_decode.glyph_height;
-  x = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_x);
-  y = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_char_y);
-  d = u8g2_font_decode_get_signed_bits(decode, u8g2->font_info.bits_per_delta_x);
-
-  if ( decode->glyph_width > 0 )
-  {
-    decode->target_x = u8g2_add_vector_x(decode->target_x, x, -(h+y), decode->dir);
-    decode->target_y = u8g2_add_vector_y(decode->target_y, x, -(h+y), decode->dir);
-    //u8g2_add_vector(&(decode->target_x), &(decode->target_y), x, -(h+y), decode->dir);
-    /* reset local x/y position */
-    decode->x = 0;
-    decode->y = 0;
-    /* decode glyph */
-    for(;;){
-      a = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_0);
-      b = u8g2_font_decode_get_unsigned_bits(decode, u8g2->font_info.bits_per_1);
-      do{
-        u8g2_font_decode_len(u8g2, a, 0);
-        u8g2_font_decode_len(u8g2, b, 1);
-      } while( u8g2_font_decode_get_unsigned_bits(decode, 1) != 0 );
-      if ( decode->y >= h )
-        break;
-    }
-  }
-  return d;
-}
-const uint8_t *u8g2_font_get_glyph_data(u8g2_t *u8g2, uint16_t encoding);
-static int16_t u8g2_font_draw_glyph(u8g2_t *u8g2, int16_t x, int16_t y, uint16_t encoding){
-  int16_t dx = 0;
-  u8g2->font_decode.target_x = x;
-  u8g2->font_decode.target_y = y;
-  const uint8_t *glyph_data = u8g2_font_get_glyph_data(u8g2, encoding);
-  if ( glyph_data != NULL ){
-    dx = u8g2_font_decode_glyph(u8g2, glyph_data);
-  }
-  return dx;
-}
-
 /**
 设置字体
 @api eink.setFont(font)
@@ -469,7 +292,7 @@ static int l_eink_set_font(lua_State *L) {
         LLOGE("only font pointer is allow");
         return 0;
     }
-    u8g2_SetFont(&(luat_eink_u8g2), ptr);
+    font_current = (lv_font_t*)ptr;
     lua_pushboolean(L, 1);
     return 1;
 }
@@ -495,45 +318,29 @@ eink.print(10, 20, "LuatOS")
 */
 static int l_eink_print(lua_State *L)
 {
-    size_t len;
-    int x           = luaL_checkinteger(L, 1);
-    int y           = luaL_checkinteger(L, 2);
-    const char *str = luaL_checklstring(L, 3, &len);
-
-    
-    if (check_init() == 0) {
-      return 0;
-    }
-    ctxs[ctx_index]->str_color = luaL_optinteger(L, 4, 0);
-
-    uint16_t e;
-    int16_t delta, sum;
-    utf8_state = 0;
-    sum = 0;
-    for(;;){
-        e = utf8_next((uint8_t)*str);
-        if ( e == 0x0ffff )
-        break;
-        str++;
-        if ( e != 0x0fffe ){
-        delta = u8g2_font_draw_glyph(&(luat_eink_u8g2), x, y, e);
-        switch(luat_eink_u8g2.font_decode.dir){
-            case 0:
-            x += delta;
-            break;
-            case 1:
-            y += delta;
-            break;
-            case 2:
-            x -= delta;
-            break;
-            case 3:
-            y -= delta;
-            break;
-        }
-        sum += delta;
-        }
-    }
+    int x, y;
+    size_t sz;
+    const uint8_t* str;
+    x = luaL_checkinteger(L, 1);
+    y = luaL_checkinteger(L, 2);
+    str = (const uint8_t*)luaL_checklstring(L, 3, &sz);
+    if (sz == 0)
+        return 0;
+    ui_draw_str_ctx_t ctx = {
+        .font = font_current,
+        .bg_color = 0,
+        .front_color = 1, // Colored? 会不会写反了
+        .draw_mode = 0,
+        .opts = eink_ui_opts,
+        .ui_h = epd_h,
+        .ui_w = epd_w,
+        .x = x,
+        .y = y,
+        .utf8_letters = str,
+        .userdata = NULL,
+    };
+    int ret = luat_ufont_drawUTF8(&ctx);
+    lua_pushinteger(L, ret);
     return 0;
 }
 

+ 0 - 7
components/lcd/luat_lib_lcd.c

@@ -20,9 +20,6 @@
 
 #include "qrcodegen.h"
 
-int8_t u8g2_font_decode_get_signed_bits(u8g2_font_decode_t *f, uint8_t cnt);
-uint8_t u8g2_font_decode_get_unsigned_bits(u8g2_font_decode_t *f, uint8_t cnt);
-
 extern luat_color_t BACK_COLOR , FORE_COLOR ;
 
 #include "luat_ufont.h"
@@ -263,9 +260,6 @@ static int l_lcd_init(lua_State* L) {
         }
         // 初始化OK, 配置额外的参数
         default_conf = conf;
-        // u8g2_SetFont(&(conf->luat_lcd_u8g2), u8g2_font_opposansm8);
-        // u8g2_SetFontMode(&(conf->luat_lcd_u8g2), 0);
-        // u8g2_SetFontDirection(&(conf->luat_lcd_u8g2), 0);
         font_current = &luat_font_sarasa_bold_16_lvgl;
         lua_pushboolean(L, 1);
         return 1;
@@ -680,7 +674,6 @@ static int l_lcd_set_font(lua_State *L) {
       LLOGE("only font pointer is allow");
       return 0;
     }
-    // u8g2_SetFont(&(default_conf->luat_lcd_u8g2), ptr);
     font_current = (lv_font_t*)ptr;
     lua_pushboolean(L, 1);
     return 1;

+ 1 - 1
components/u8g2/luat_lib_u8g2.c

@@ -336,7 +336,7 @@ static int l_u8g2_DrawUTF8(lua_State *L) {
     };
     int ret = luat_ufont_drawUTF8(&ctx);
     lua_pushinteger(L, ret);
-    return 1;
+    return 0;
 }
 
 /*

+ 4 - 0
components/ufont/luat_ufont.h

@@ -1,3 +1,6 @@
+#ifndef LUAT_UFONT_H_
+#define LUAT_UFONT_H_
+
 #include "luat_base.h"
 
 // LVGL fonts
@@ -88,3 +91,4 @@ bool luat_fonts_lvgl_get_glyph_dsc(const struct _lv_font_struct *, lv_font_glyph
 /** Get a glyph's bitmap from a font*/
 const uint8_t * luat_fonts_lvgl_get_glyph_bitmap(const struct _lv_font_struct *, uint32_t);
 
+#endif