summaryrefslogtreecommitdiff
path: root/lib/luavgl/src/font.c
blob: 87e0bbef451816216fb55e08e707fa954113e2e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "luavgl.h"
#include "private.h"

static char *to_lower(char *str)
{
  for (char *s = str; *s; ++s)
    *s = *s >= 'A' && *s <= 'Z' ? *s | 0x60 : *s;
  return str;
}

static char *luavgl_strchr(const char *s, char c)
{
  while (*s) {
    if (c == *s) {
      return (char *)s;
    }
    s++;
  }
  return NULL;
}

/**
 * Dynamic font family fallback is not supported.
 * The fallback only happen when font creation fails and continue to try next
 * one. Fallback logic in lvgl is supposed to be system wide.
 *
 * lvgl.Font("MiSansW medium, montserrat", 24, "normal")
 */
static int luavgl_font_create(lua_State *L)
{

  if (!lua_isstring(L, 1)) {
    return luaL_argerror(L, 1, "expect string");
  }
  const char *name = lua_tostring(L, 1);
  const lv_font_t *font = NULL;

  luavgl_ctx_t *ctx = luavgl_context(L);
  if (ctx->make_font) {
    font = ctx->make_font(name);
  }

  if (font) {
    lua_pushlightuserdata(L, (void *)font);
    return 1;
  }

  return luaL_error(L, "cannot create font");
}