Просмотр исходного кода

add: fonts库支持加载文件字体,并添加额外的宏,为自定义字体库做准备

https://gitee.com/openLuat/LuatOS/issues/I53RCQ?from=project-issue
Wendal Chen 3 лет назад
Родитель
Сommit
d4a71edcca

+ 92 - 5
components/luatfonts/luat_lib_fonts.c

@@ -1,4 +1,17 @@
+/*
+@module  fonts
+@summary 字体库
+@version 1.0
+@date    2022.07.11
+*/
+
+
 #include "luat_base.h"
+#include "luat_malloc.h"
+#include "luat_fs.h"
+
+#define LUAT_LOG_TAG "fonts"
+#include "luat_log.h"
 
 #include "u8g2.h"
 #include "u8g2_luat_fonts.h"
@@ -9,7 +22,6 @@ typedef struct u8g2_font
     const uint8_t* font;
 }u8g2_font_t;
 
-
 static u8g2_font_t u8g2_fonts[] = {
 #ifdef USE_U8G2_OPPOSANSM_ENGLISH
     {.name="unifont_t_symbols", .font=u8g2_font_unifont_t_symbols},
@@ -49,10 +61,26 @@ static u8g2_font_t u8g2_fonts[] = {
 #ifdef USE_U8G2_OPPOSANSM32_CHINESE
     {.name="opposansm32_chinese", .font=u8g2_font_opposansm32_chinese},
 #endif
+
+#ifdef LUAT_FONTS_CUSTOM_U8G2
+    LUAT_FONTS_CUSTOM_U8G2
+#endif
     {.name="", .font=NULL},
 };
 
-
+/*
+获取u8g2字体
+@api fonts.u8g2_get(name)
+@string 字体名称, 例如opposansm8_chinese unifont_t_symbols
+@return userdata 若字体存放,返回字体指针, 否则返回nil
+@usage
+oppo_8 = fonts.u8g2_get("opposansm8_chinese")
+if oppo_8 then
+    u8g2.SetFont(oppo_8)
+else
+    log.warn("fonts", "no such font opposansm8_chinese")
+end
+*/
 static int l_fonts_u8g2_get(lua_State *L) {
     const char* name = luaL_checkstring(L,  1);
     u8g2_font_t *font = u8g2_fonts;
@@ -66,16 +94,75 @@ static int l_fonts_u8g2_get(lua_State *L) {
     return 0;
 }
 
+/*
+从文件加载u8g2字体
+@api fonts.u8g2_load(path)
+@string 字体路径, 例如 /luadb/abc.bin
+@return userdata 若字体存放,返回字体指针, 否则返回nil
+@usage
+-- 提醒: 若文件位于/luadb下, 不需要占用内存
+-- 若文件处于其他路径, 例如tf/sd卡, spi flash, 会自动加载到内存, 消耗lua vm的内存空间
+-- 加载后请适当引用, 不必反复加载同一个字体文件
+oppo12 = fonts.u8g2_load("/luadb/oppo12.bin")
+if oppo12 then
+    u8g2.SetFont(oppo12)
+else
+    log.warn("fonts", "no such font file oppo12.bin")
+end
+*/
 static int l_fonts_u8g2_load(lua_State *L) {
-    // TODO 从文件加载
-    return 0;
+    char* ptr = NULL;
+    // 从文件加载
+    const char* path = luaL_checkstring(L, 1);
+    size_t flen = luat_fs_fsize(path);
+    if (flen < 16) {
+        LLOGE("not a good font file %s", path);
+        return 0;
+    }
+    FILE* fd = luat_fs_fopen(path, "rb");
+    if (fd == NULL) {
+        LLOGE("no such file %s", path);
+        return 0;
+    }
+#ifdef LUAT_USE_FS_VFS
+  //LLOGD("try mmap");
+  ptr = (char*)luat_vfs_mmap(fd);
+  if (ptr != NULL) {
+    LLOGD("load by mmap %s %p", path, ptr);
+    lua_pushlightuserdata(L, ptr);
+    luat_fs_fclose(fd);
+    return 1;
+  }
+#endif
+    ptr = lua_newuserdata(L, flen);
+    if (ptr == NULL) {
+        luat_fs_fclose(fd);
+        LLOGE("no engouh memory for font %s", path);
+        return 0;
+    }
+    char buff[256];
+    int len = 0;
+    int count = 0;
+    while (count < flen)
+    {
+        len = luat_fs_fread(buff, 256, 1, fd);
+        if (len < 0)
+            break;
+        if (len > 0) {
+            memcpy(ptr + count, buff, len);
+            count += len;
+        }
+    }
+    luat_fs_fclose(fd);
+    lua_pushlightuserdata(L, ptr);
+    return 1;
 }
 
 #include "rotable2.h"
 static const rotable_Reg_t reg_fonts[] =
 {
     { "u8g2_get" ,       ROREG_FUNC(l_fonts_u8g2_get)},
-    //{ "u8g2_load" ,      ROREG_FUNC(l_fonts_u8g2_load)},
+    { "u8g2_load" ,      ROREG_FUNC(l_fonts_u8g2_load)},
 	{ NULL,              ROREG_INT(0)},
 };
 

+ 0 - 0
components/u8g2/luat_u8g2_fonts_custom.h


+ 3 - 0
components/u8g2/u8g2_luat_fonts.h

@@ -37,6 +37,9 @@ extern const uint8_t u8g2_font_opposansm22_chinese[] U8G2_FONT_SECTION("u8g2_fon
 extern const uint8_t u8g2_font_opposansm24_chinese[] U8G2_FONT_SECTION("u8g2_font_opposansm24_chinese");
 extern const uint8_t u8g2_font_opposansm32_chinese[] U8G2_FONT_SECTION("u8g2_font_opposansm32_chinese");
 
+// include custom fonts
+#include "luat_u8g2_fonts_custom.h"
+
 /* end font list */