summaryrefslogtreecommitdiff
path: root/lib/luavgl/src/font.c
blob: f4fe366453bb5977c7b13bd74675afd41c70c476 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "luavgl.h"
#include "private.h"

#define FONT_DEFAULT_SIZE 12

#define _ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))

#define NAMED_WEIGHT_MAX_CHARS 16

/**
 * Follow css style, specify the name by name family, name size,
 * name weight. Font weight can be numeric value or 'bold'. Alls strings
 * are converted to lower-case before matching with local supported name.
 *
 * weight   named weight
 * 100      thin
 * 200      extra light
 * 300      light
 * 400      normal (*default)
 * 500      medium
 * 600      semi bold
 * 700      bold
 * 800      extra bold
 * 900      ultra bold
 *
 * Only normal weight is supported to builtin name.
 *
 * Font returned to lua is a userdata, it's used by object style, or style
 * directly. If name is no longer ref'ed by any object or style, it's
 * gc'ed.
 */

/**
 * lvgl.Font("montserrat, unscii", 8, bold)
 * lvgl.Font("montserrat", 8)
 * lvgl.Font("montserrat")
 *
 * return nil if failed
 */

typedef enum {
  FONT_WEIGHT_THIN = 100,
  FONT_WEIGHT_EXTRA_LIGHT = 200,
  FONT_WEIGHT_LIGHT = 300,
  FONT_WEIGHT_NORMAL = 400,
  FONT_WEIGHT_MEDIUM = 500,
  FONT_WEIGHT_SEMI_BOLD = 600,
  FONT_WEIGHT_BOLD = 700,
  FONT_WEIGHT_EXTRA_BOLD = 800,
  FONT_WEIGHT_ULTRA_BOLD = 900,
} font_weight_t;

static const struct {
  char *name;
  int value;
} g_named_weight[] = {
    {"thin",        100},
    {"extra light", 200},
    {"light",       300},
    {"normal",      400},
    {"medium",      500},
    {"semi bold",   600},
    {"bold",        700},
    {"extra bold",  800},
    {"ultra bold",  900},
};

static const struct {
  int size;
  const lv_font_t *font;
} g_builtin_montserrat[] = {
#if LV_FONT_MONTSERRAT_8
    {8,  &lv_font_montserrat_8 },
#endif

#if LV_FONT_MONTSERRAT_10
    {10, &lv_font_montserrat_10},
#endif

#if LV_FONT_MONTSERRAT_12
    {12, &lv_font_montserrat_12},
#endif

#if LV_FONT_MONTSERRAT_14
    {14, &lv_font_montserrat_14},
#endif

#if LV_FONT_MONTSERRAT_16
    {16, &lv_font_montserrat_16},
#endif

#if LV_FONT_MONTSERRAT_18
    {18, &lv_font_montserrat_18},
#endif

#if LV_FONT_MONTSERRAT_20
    {20, &lv_font_montserrat_20},
#endif

#if LV_FONT_MONTSERRAT_22
    {22, &lv_font_montserrat_22},
#endif

#if LV_FONT_MONTSERRAT_24
    {24, &lv_font_montserrat_24},
#endif

#if LV_FONT_MONTSERRAT_26
    {26, &lv_font_montserrat_26},
#endif

#if LV_FONT_MONTSERRAT_28
    {28, &lv_font_montserrat_28},
#endif

#if LV_FONT_MONTSERRAT_30
    {30, &lv_font_montserrat_30},
#endif

#if LV_FONT_MONTSERRAT_32
    {32, &lv_font_montserrat_32},
#endif

#if LV_FONT_MONTSERRAT_34
    {34, &lv_font_montserrat_34},
#endif

#if LV_FONT_MONTSERRAT_36
    {36, &lv_font_montserrat_36},
#endif

#if LV_FONT_MONTSERRAT_38
    {38, &lv_font_montserrat_38},
#endif

#if LV_FONT_MONTSERRAT_40
    {40, &lv_font_montserrat_40},
#endif

#if LV_FONT_MONTSERRAT_42
    {42, &lv_font_montserrat_42},
#endif

#if LV_FONT_MONTSERRAT_44
    {44, &lv_font_montserrat_44},
#endif

#if LV_FONT_MONTSERRAT_46
    {46, &lv_font_montserrat_46},
#endif

#if LV_FONT_MONTSERRAT_48
    {48, &lv_font_montserrat_48},
#endif
};

static int luavgl_get_named_weight(const char *name)
{
  if (name == NULL) {
    return FONT_DEFAULT_SIZE;
  }

  for (int i = 0; i < _ARRAY_LEN(g_named_weight); i++) {
    if (lv_strcmp(name, g_named_weight[i].name) == 0) {
      return g_named_weight[i].value;
    }
  }

  return FONT_DEFAULT_SIZE;
}

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

static const lv_font_t *_luavgl_font_create(lua_State *L, const char *name,
                                            int size, int weight)
{
  /* check builtin font firstly. */
  if (lv_strcmp(name, "montserrat") == 0) {
    if (FONT_WEIGHT_NORMAL != weight)
      return NULL;

    for (int i = 0; i < _ARRAY_LEN(g_builtin_montserrat); i++) {
      if (size == g_builtin_montserrat[i].size) {
        return g_builtin_montserrat[i].font;
      }
    }
  } else if (lv_strcmp(name, "unscii") == 0) {
    if (FONT_WEIGHT_NORMAL != weight)
      return NULL;
#if LV_FONT_UNSCII_8
    if (size == 8)
      return &lv_font_unscii_8;
#endif

#if LV_FONT_UNSCII_16
    if (size == 16)
      return &lv_font_unscii_16;
#endif
  }
#if LV_FONT_DEJAVU_16_PERSIAN_HEBREW
  else if (lv_strcmp(name, "dejavu_persian_hebrew") == 0) {
    if (size == 16)
      return &lv_font_dejavu_16_persian_hebrew;
  }
#endif

#if LV_FONT_SIMSUN_16_CJK
  else if (lv_strcmp(name, "dejavu_persian_hebrew") == 0) {
    if (size == 16)
      return &lv_font_simsun_16_cjk;
  }
#endif

  /* not built-in font, check extension  */
  luavgl_ctx_t *ctx = luavgl_context(L);
  if (ctx->make_font) {
    return ctx->make_font(name, size, weight);
  }

  return NULL;
}

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)
{
  int weight;
  int size;
  char *str, *name;
  const lv_font_t *font = NULL;

  if (!lua_isstring(L, 1)) {
    return luaL_argerror(L, 1, "expect string");
  }

  /* size is optional, default to FONT_DEFAULT_SIZE */
  size = lua_tointeger(L, 2);
  if (size == 0) {
    size = FONT_DEFAULT_SIZE;
  }

  if (!lua_isnoneornil(L, 3)) {
    if (lua_isinteger(L, 3)) {
      weight = lua_tointeger(L, 3);
    } else {
      char *luastr = (char *)lua_tostring(L, 3);
      int len = lv_strlen(luastr);
      if (len > 128) {
        /* not likely to happen */
        return luaL_argerror(L, 3, "too long");
      }
      char s[NAMED_WEIGHT_MAX_CHARS];
      if (len + 1 > NAMED_WEIGHT_MAX_CHARS) {
        return luaL_argerror(L, 3, "too long");
      }

      lv_strcpy(s, luastr);
      weight = luavgl_get_named_weight(to_lower(s));
    }
  } else {
    weight = FONT_WEIGHT_NORMAL;
  }

  str = lv_strdup(lua_tostring(L, 1));
  if (str == NULL) {
    return luaL_error(L, "no memory");
  }

  name = to_lower(str);
  while (*name) {
    if (*name == ' ') {
      name++;
      continue;
    }

    char *end = luavgl_strchr(name, ',');
    if (end != NULL) {
      *end = '\0';
    } else {
      end = name + lv_strlen(name);
    }

    char *trim = end - 1;
    while (*trim == ' ') {
      *trim-- = '\0'; /* trailing space. */
    }

    font = _luavgl_font_create(L, name, size, weight);
    if (font) {
      break;
    }

    name = end + 1; /* next */
  }

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

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