summaryrefslogtreecommitdiff
path: root/src/tangara/lua/lua_font.cpp
blob: e5913492a41176422362bd6d17ee07505dbb490d (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
/*
 * Copyright 2024 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "lua_font.hpp"

#include <cstdint>
#include <string>

#include "lauxlib.h"
#include "lua.h"
#include "lvgl.h"

#include "events/event_queue.hpp"
#include "lua/bridge.hpp"
#include "lua/lua_registry.hpp"
#include "lua/lua_thread.hpp"

namespace lua {

[[maybe_unused]] static constexpr char kTag[] = "lua_font";

/* Reads the given file completely into PSRAM. */
static auto readFont(std::string path) -> std::span<uint8_t> {
  // This following is a bit C-brained. Sorry.
  FILE* f = fopen(path.c_str(), "r");
  if (!f) {
    return {};
  }

  uint8_t* data = NULL;
  long len = 0;

  if (fseek(f, 0, SEEK_END)) {
    goto fail;
  }

  len = ftell(f);
  if (len <= 0) {
    goto fail;
  }

  if (fseek(f, 0, SEEK_SET)) {
    len = 0;
    goto fail;
  }

  data = reinterpret_cast<uint8_t*>(heap_caps_malloc(len, MALLOC_CAP_SPIRAM));
  if (!data) {
    len = 0;
    goto fail;
  }

  if (fread(data, 1, len, f) < len) {
    heap_caps_free(data);
    len = 0;
  }

fail:
  fclose(f);

  return {data, static_cast<size_t>(len)};
}

static auto parseFont(std::span<uint8_t> data) -> lv_font_t* {
  if (data.empty()) {
    return nullptr;
  }

  lv_font_t* font = lv_binfont_create_from_buffer(data.data(), data.size());
  heap_caps_free(data.data());

  return font;
}

auto loadFont(lua_State* L, const char* path, int cb_ref) -> void {
  // Most Lua file paths start with "//" in order to deal with LVGL's Windows-y
  // approach to paths. Try to handle such paths correctly so that paths in Lua
  // code look a bit more consistent.
  std::string path_str = path;
  if (path_str.starts_with("//")) {
    path++;
    path_str = path;
  }

  // Do the file read from the current thread, since the path might be for a
  // file in flash, and we can't read from flash in a background task.
  auto font_data = readFont(path_str);

  Bridge* bridge = Bridge::Get(L);
  bridge->services().bg_worker().Dispatch<void>([=]() {
    // Do the parsing now that we're in the background.
    lv_font_t* font = parseFont(font_data);

    // Hop back to the UI task to invoke the Lua callback.
    events::Ui().RunOnTask([=] {
      // Retrieve the callback by ref, and release the ref.
      lua_rawgeti(L, LUA_REGISTRYINDEX, cb_ref);
      luaL_unref(L, LUA_REGISTRYINDEX, cb_ref);

      // We always invoke the callback, but we don't always have a result.
      if (font) {
        lua_pushlightuserdata(L, (void*)font);
      } else {
        lua_pushnil(L);
      }

      CallProtected(L, 1, 0);
    });
  });
}

}  // namespace lua