2013-02-01 21:52:27 +02:00
|
|
|
#include "lua/internal.hpp"
|
|
|
|
#include "fonts/wrapper.hpp"
|
2013-02-28 10:36:57 +02:00
|
|
|
#include "core/framebuffer.hpp"
|
2013-02-01 21:52:27 +02:00
|
|
|
#include "library/framebuffer.hpp"
|
2013-12-19 07:32:17 +02:00
|
|
|
#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:
|
2013-12-20 02:02:12 +02:00
|
|
|
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();
|
2013-12-20 02:02:12 +02:00
|
|
|
int draw(lua::state& L, const std::string& fname);
|
2013-12-19 07:32:17 +02:00
|
|
|
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:
|
2013-12-20 02:02:12 +02:00
|
|
|
void init(lua::state& L);
|
2013-12-11 21:12:34 +02:00
|
|
|
std::string orig_filename;
|
2013-12-19 07:32:17 +02:00
|
|
|
framebuffer::font2 font;
|
2013-02-01 21:52:27 +02:00
|
|
|
};
|
|
|
|
|
2013-12-20 02:02:12 +02:00
|
|
|
lua::_class<lua_customfont> class_customfont("CUSTOMFONT");
|
2013-02-01 21:52:27 +02:00
|
|
|
|
2013-12-19 06:57:22 +02:00
|
|
|
struct render_object_text_cf : public framebuffer::object
|
2013-02-01 21:52:27 +02:00
|
|
|
{
|
2013-12-19 06:57:22 +02:00
|
|
|
render_object_text_cf(int32_t _x, int32_t _y, const std::string& _text, framebuffer::color _fg,
|
2013-12-20 02:02:12 +02:00
|
|
|
framebuffer::color _bg, framebuffer::color _hl, lua::objpin<lua_customfont> _font) throw()
|
2013-03-29 00:43:14 +02:00
|
|
|
: x(_x), y(_y), text(_text), fg(_fg), bg(_bg), hl(_hl), font(_font) {}
|
2013-02-28 10:36:57 +02:00
|
|
|
~render_object_text_cf() throw()
|
|
|
|
{
|
|
|
|
}
|
2013-12-19 06:57:22 +02:00
|
|
|
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);
|
2013-03-29 00:43:14 +02:00
|
|
|
hl.set_palette(scr);
|
2013-12-19 07:32:17 +02:00
|
|
|
const framebuffer::font2& fdata = font->get_font();
|
2013-12-20 12:39:24 +02:00
|
|
|
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;
|
2013-03-29 00:43:14 +02:00
|
|
|
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];
|
2013-03-25 00:29:48 +02:00
|
|
|
std::u32string k = fdata.best_ligature_match(_text, i);
|
2013-12-19 07:32:17 +02:00
|
|
|
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 {
|
2013-03-29 00:43:14 +02:00
|
|
|
glyph.render(scr, drawx, drawy, fg, bg, hl);
|
2013-02-01 21:52:27 +02:00
|
|
|
drawx += glyph.width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-28 10:36:57 +02:00
|
|
|
bool kill_request(void* obj) throw()
|
|
|
|
{
|
2013-08-21 19:24:00 +03:00
|
|
|
return kill_request_ifeq(font.object(), obj);
|
2013-02-28 10:36:57 +02:00
|
|
|
}
|
2013-12-19 06:57:22 +02:00
|
|
|
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;
|
2013-12-19 06:57:22 +02:00
|
|
|
framebuffer::color fg;
|
|
|
|
framebuffer::color bg;
|
|
|
|
framebuffer::color hl;
|
2013-12-20 02:02:12 +02:00
|
|
|
lua::objpin<lua_customfont> font;
|
2013-02-01 21:52:27 +02:00
|
|
|
};
|
|
|
|
|
2013-12-20 02:02:12 +02:00
|
|
|
void lua_customfont::init(lua::state& L)
|
2013-02-01 21:52:27 +02:00
|
|
|
{
|
2013-12-20 02:02:12 +02:00
|
|
|
lua::objclass<lua_customfont>().bind_multi(L, {
|
2013-08-22 00:11:34 +03:00
|
|
|
{"__call", &lua_customfont::draw},
|
|
|
|
});
|
2013-02-01 21:52:27 +02:00
|
|
|
}
|
2013-07-06 23:21:11 +03:00
|
|
|
|
2013-12-20 02:02:12 +02: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);
|
|
|
|
}
|
|
|
|
|
2013-12-20 02:02:12 +02:00
|
|
|
lua_customfont::lua_customfont(lua::state& L)
|
2013-12-11 21:12:34 +02:00
|
|
|
: font(main_font)
|
|
|
|
{
|
|
|
|
orig_filename = "<builtin>";
|
|
|
|
init(L);
|
|
|
|
}
|
|
|
|
|
2013-02-28 10:36:57 +02:00
|
|
|
lua_customfont::~lua_customfont() throw()
|
|
|
|
{
|
|
|
|
render_kill_request(this);
|
|
|
|
}
|
2013-07-06 23:21:11 +03:00
|
|
|
|
2013-12-20 02:02:12 +02: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;
|
2013-03-29 00:43:14 +02:00
|
|
|
int64_t hlc = -1;
|
2013-02-01 22:26:22 +02:00
|
|
|
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());
|
2013-03-29 00:43:14 +02:00
|
|
|
L.get_numeric_argument<int64_t>(7, hlc, fname.c_str());
|
2013-02-01 22:26:22 +02:00
|
|
|
std::string text = L.get_string(4, fname.c_str());
|
2013-12-20 02:02:12 +02:00
|
|
|
auto f = lua::_class<lua_customfont>::pin(L, 1, fname.c_str());
|
2013-12-19 06:57:22 +02:00
|
|
|
framebuffer::color fg(fgc);
|
|
|
|
framebuffer::color bg(bgc);
|
|
|
|
framebuffer::color hl(hlc);
|
2013-03-29 00:43:14 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-20 02:02:12 +02:00
|
|
|
lua::fnptr gui_text_cf(lua_func_misc, "gui.loadfont", [](lua::state& L, const std::string& fname)
|
2013-08-07 17:31:09 +03:00
|
|
|
-> int {
|
2013-12-11 21:12:34 +02:00
|
|
|
if(L.type(1) == LUA_TNONE || L.type(1) == LUA_TNIL) {
|
2013-12-20 02:02:12 +02:00
|
|
|
lua::_class<lua_customfont>::create(L);
|
2013-12-11 21:12:34 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2013-02-01 22:26:22 +02:00
|
|
|
std::string filename = L.get_string(1, fname.c_str());
|
2013-12-20 02:02:12 +02:00
|
|
|
lua::_class<lua_customfont>::create(L, filename);
|
2013-09-27 14:22:52 +03:00
|
|
|
return 1;
|
2013-02-01 21:52:27 +02:00
|
|
|
});
|
|
|
|
}
|