Get rid of DECLARE_LUACLASS
[lsnes.git] / src / lua / gui-text-cf.cpp
blob61a0e75fa7546a48b5edfe6e75b0a776ff759fc0
1 #include "lua/internal.hpp"
2 #include "fonts/wrapper.hpp"
3 #include "core/framebuffer.hpp"
4 #include "library/framebuffer.hpp"
5 #include "library/framebuffer-font2.hpp"
6 #include "library/utf8.hpp"
7 #include <algorithm>
10 namespace
12 struct lua_customfont
14 public:
15 lua_customfont(lua_state& L, const std::string& filename);
16 lua_customfont(lua_state& L);
17 ~lua_customfont() throw();
18 int draw(lua_state& L, const std::string& fname);
19 const framebuffer::font2& get_font() { return font; }
20 std::string print()
22 return orig_filename;
24 private:
25 void init(lua_state& L);
26 std::string orig_filename;
27 framebuffer::font2 font;
30 lua_class<lua_customfont> class_customfont("CUSTOMFONT");
32 struct render_object_text_cf : public framebuffer::object
34 render_object_text_cf(int32_t _x, int32_t _y, const std::string& _text, framebuffer::color _fg,
35 framebuffer::color _bg, framebuffer::color _hl, lua_obj_pin<lua_customfont> _font) throw()
36 : x(_x), y(_y), text(_text), fg(_fg), bg(_bg), hl(_hl), font(_font) {}
37 ~render_object_text_cf() throw()
40 template<bool X> void op(struct framebuffer::fb<X>& scr) throw()
42 fg.set_palette(scr);
43 bg.set_palette(scr);
44 hl.set_palette(scr);
45 const framebuffer::font2& fdata = font->get_font();
46 std::u32string _text = to_u32string(text);
47 int32_t orig_x = x;
48 int32_t drawx = x;
49 int32_t drawy = y;
50 if(hl) {
51 //Adjust for halo.
52 drawx++;
53 orig_x++;
54 drawy++;
56 for(size_t i = 0; i < _text.size();) {
57 uint32_t cp = _text[i];
58 std::u32string k = fdata.best_ligature_match(_text, i);
59 const framebuffer::font2::glyph& glyph = fdata.lookup_glyph(k);
60 if(k.length())
61 i += k.length();
62 else
63 i++;
64 if(cp == 9) {
65 drawx = (drawx + 64) >> 6 << 6;
66 } else if(cp == 10) {
67 drawx = orig_x;
68 drawy += fdata.get_rowadvance();
69 } else {
70 glyph.render(scr, drawx, drawy, fg, bg, hl);
71 drawx += glyph.width;
75 bool kill_request(void* obj) throw()
77 return kill_request_ifeq(font.object(), obj);
79 void operator()(struct framebuffer::fb<true>& scr) throw() { op(scr); }
80 void operator()(struct framebuffer::fb<false>& scr) throw() { op(scr); }
81 void clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
82 private:
83 int32_t x;
84 int32_t y;
85 std::string text;
86 framebuffer::color fg;
87 framebuffer::color bg;
88 framebuffer::color hl;
89 lua_obj_pin<lua_customfont> font;
92 void lua_customfont::init(lua_state& L)
94 objclass<lua_customfont>().bind_multi(L, {
95 {"__call", &lua_customfont::draw},
96 });
99 lua_customfont::lua_customfont(lua_state& L, const std::string& filename)
100 : font(filename)
102 orig_filename = filename;
103 init(L);
106 lua_customfont::lua_customfont(lua_state& L)
107 : font(main_font)
109 orig_filename = "<builtin>";
110 init(L);
113 lua_customfont::~lua_customfont() throw()
115 render_kill_request(this);
118 int lua_customfont::draw(lua_state& L, const std::string& fname)
120 if(!lua_render_ctx)
121 return 0;
122 int64_t fgc = 0xFFFFFFU;
123 int64_t bgc = -1;
124 int64_t hlc = -1;
125 int32_t _x = L.get_numeric_argument<int32_t>(2, fname.c_str());
126 int32_t _y = L.get_numeric_argument<int32_t>(3, fname.c_str());
127 L.get_numeric_argument<int64_t>(5, fgc, fname.c_str());
128 L.get_numeric_argument<int64_t>(6, bgc, fname.c_str());
129 L.get_numeric_argument<int64_t>(7, hlc, fname.c_str());
130 std::string text = L.get_string(4, fname.c_str());
131 auto f = lua_class<lua_customfont>::pin(L, 1, fname.c_str());
132 framebuffer::color fg(fgc);
133 framebuffer::color bg(bgc);
134 framebuffer::color hl(hlc);
135 lua_render_ctx->queue->create_add<render_object_text_cf>(_x, _y, text, fg, bg, hl, f);
136 return 0;
139 function_ptr_luafun gui_text_cf(lua_func_misc, "gui.loadfont", [](lua_state& L, const std::string& fname)
140 -> int {
141 if(L.type(1) == LUA_TNONE || L.type(1) == LUA_TNIL) {
142 lua_class<lua_customfont>::create(L);
143 return 1;
145 std::string filename = L.get_string(1, fname.c_str());
146 lua_class<lua_customfont>::create(L, filename);
147 return 1;