lsnes/src/lua/gui-text-cf.cpp

150 lines
4 KiB
C++
Raw Normal View History

2013-02-01 21:52:27 +02:00
#include "lua/internal.hpp"
#include "fonts/wrapper.hpp"
#include "core/framebuffer.hpp"
2013-02-01 21:52:27 +02:00
#include "library/framebuffer.hpp"
#include "library/framebuffer-font2.hpp"
2013-02-01 21:52:27 +02:00
#include "library/utf8.hpp"
2013-03-13 01:02:10 +02:00
#include <algorithm>
2013-02-01 21:52:27 +02:00
namespace
{
struct lua_customfont
{
public:
lua_customfont(lua::state& L, const std::string& filename);
lua_customfont(lua::state& L);
2013-02-01 21:52:27 +02:00
~lua_customfont() throw();
int draw(lua::state& L, const std::string& fname);
const framebuffer::font2& get_font() { return font; }
2013-09-27 10:36:19 +03:00
std::string print()
{
2013-12-11 21:12:34 +02:00
return orig_filename;
2013-09-27 10:36:19 +03:00
}
2013-02-01 21:52:27 +02:00
private:
void init(lua::state& L);
2013-12-11 21:12:34 +02:00
std::string orig_filename;
framebuffer::font2 font;
2013-02-01 21:52:27 +02:00
};
lua::_class<lua_customfont> class_customfont("CUSTOMFONT");
2013-02-01 21:52:27 +02:00
struct render_object_text_cf : public framebuffer::object
2013-02-01 21:52:27 +02:00
{
render_object_text_cf(int32_t _x, int32_t _y, const std::string& _text, framebuffer::color _fg,
framebuffer::color _bg, framebuffer::color _hl, lua::objpin<lua_customfont> _font) throw()
: x(_x), y(_y), text(_text), fg(_fg), bg(_bg), hl(_hl), font(_font) {}
~render_object_text_cf() throw()
{
}
template<bool X> void op(struct framebuffer::fb<X>& scr) throw()
2013-02-01 21:52:27 +02:00
{
fg.set_palette(scr);
bg.set_palette(scr);
hl.set_palette(scr);
const framebuffer::font2& fdata = font->get_font();
std::u32string _text = utf8::to32(text);
2013-02-01 21:52:27 +02:00
int32_t orig_x = x;
int32_t drawx = x;
int32_t drawy = y;
if(hl) {
//Adjust for halo.
drawx++;
orig_x++;
drawy++;
}
2013-02-01 21:52:27 +02:00
for(size_t i = 0; i < _text.size();) {
uint32_t cp = _text[i];
std::u32string k = fdata.best_ligature_match(_text, i);
const framebuffer::font2::glyph& glyph = fdata.lookup_glyph(k);
2013-02-01 21:52:27 +02:00
if(k.length())
i += k.length();
else
i++;
if(cp == 9) {
drawx = (drawx + 64) >> 6 << 6;
} else if(cp == 10) {
drawx = orig_x;
drawy += fdata.get_rowadvance();
} else {
glyph.render(scr, drawx, drawy, fg, bg, hl);
2013-02-01 21:52:27 +02:00
drawx += glyph.width;
}
}
}
bool kill_request(void* obj) throw()
{
2013-08-21 19:24:00 +03:00
return kill_request_ifeq(font.object(), obj);
}
void operator()(struct framebuffer::fb<true>& scr) throw() { op(scr); }
void operator()(struct framebuffer::fb<false>& scr) throw() { op(scr); }
void clone(framebuffer::queue& q) const throw(std::bad_alloc) { q.clone_helper(this); }
2013-02-01 21:52:27 +02:00
private:
int32_t x;
int32_t y;
2013-02-10 15:00:12 +02:00
std::string text;
framebuffer::color fg;
framebuffer::color bg;
framebuffer::color hl;
lua::objpin<lua_customfont> font;
2013-02-01 21:52:27 +02:00
};
void lua_customfont::init(lua::state& L)
2013-02-01 21:52:27 +02:00
{
lua::objclass<lua_customfont>().bind_multi(L, {
{"__call", &lua_customfont::draw},
});
2013-02-01 21:52:27 +02:00
}
2013-07-06 23:21:11 +03:00
lua_customfont::lua_customfont(lua::state& L, const std::string& filename)
2013-12-11 21:12:34 +02:00
: font(filename)
{
orig_filename = filename;
init(L);
}
lua_customfont::lua_customfont(lua::state& L)
2013-12-11 21:12:34 +02:00
: font(main_font)
{
orig_filename = "<builtin>";
init(L);
}
lua_customfont::~lua_customfont() throw()
{
render_kill_request(this);
}
2013-07-06 23:21:11 +03:00
int lua_customfont::draw(lua::state& L, const std::string& fname)
2013-02-01 21:52:27 +02:00
{
if(!lua_render_ctx)
return 0;
int64_t fgc = 0xFFFFFFU;
int64_t bgc = -1;
int64_t hlc = -1;
int32_t _x = L.get_numeric_argument<int32_t>(2, fname.c_str());
int32_t _y = L.get_numeric_argument<int32_t>(3, fname.c_str());
L.get_numeric_argument<int64_t>(5, fgc, fname.c_str());
L.get_numeric_argument<int64_t>(6, bgc, fname.c_str());
L.get_numeric_argument<int64_t>(7, hlc, fname.c_str());
std::string text = L.get_string(4, fname.c_str());
auto f = lua::_class<lua_customfont>::pin(L, 1, fname.c_str());
framebuffer::color fg(fgc);
framebuffer::color bg(bgc);
framebuffer::color hl(hlc);
lua_render_ctx->queue->create_add<render_object_text_cf>(_x, _y, text, fg, bg, hl, f);
2013-02-01 21:52:27 +02:00
return 0;
}
lua::fnptr gui_text_cf(lua_func_misc, "gui.loadfont", [](lua::state& L, const std::string& fname)
-> int {
2013-12-11 21:12:34 +02:00
if(L.type(1) == LUA_TNONE || L.type(1) == LUA_TNIL) {
lua::_class<lua_customfont>::create(L);
2013-12-11 21:12:34 +02:00
return 1;
}
std::string filename = L.get_string(1, fname.c_str());
lua::_class<lua_customfont>::create(L, filename);
return 1;
2013-02-01 21:52:27 +02:00
});
}