1 #include "lua/internal.hpp"
2 #include "fonts/wrapper.hpp"
3 #include "core/framebuffer.hpp"
4 #include "library/framebuffer.hpp"
5 #include "library/customfont.hpp"
6 #include "library/utf8.hpp"
15 lua_customfont(lua_state
& L
, const std::string
& filename
);
16 ~lua_customfont() throw();
17 int draw(lua_state
& L
, const std::string
& fname
);
18 const custom_font
& get_font() { return font
; }
28 DECLARE_LUACLASS(lua_customfont
, "CUSTOMFONT");
32 struct render_object_text_cf
: public render_object
34 render_object_text_cf(int32_t _x
, int32_t _y
, const std::string
& _text
, premultiplied_color _fg
,
35 premultiplied_color _bg
, premultiplied_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
<X
>& scr
) throw()
45 const custom_font
& fdata
= font
->get_font();
46 std::u32string _text
= to_u32string(text
);
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 font_glyph_data
& glyph
= fdata
.lookup_glyph(k
);
65 drawx
= (drawx
+ 64) >> 6 << 6;
68 drawy
+= fdata
.get_rowadvance();
70 glyph
.render(scr
, drawx
, drawy
, fg
, bg
, hl
);
75 bool kill_request(void* obj
) throw()
77 return kill_request_ifeq(font
.object(), obj
);
79 void operator()(struct framebuffer
<true>& scr
) throw() { op(scr
); }
80 void operator()(struct framebuffer
<false>& scr
) throw() { op(scr
); }
81 void clone(render_queue
& q
) const throw(std::bad_alloc
) { q
.clone_helper(this); }
86 premultiplied_color fg
;
87 premultiplied_color bg
;
88 premultiplied_color hl
;
89 lua_obj_pin
<lua_customfont
> font
;
92 lua_customfont::lua_customfont(lua_state
& L
, const std::string
& filename
)
95 objclass
<lua_customfont
>().bind_multi(L
, {
96 {"__call", &lua_customfont::draw
},
100 lua_customfont::~lua_customfont() throw()
102 render_kill_request(this);
105 int lua_customfont::draw(lua_state
& L
, const std::string
& fname
)
109 int64_t fgc
= 0xFFFFFFU
;
112 int32_t _x
= L
.get_numeric_argument
<int32_t>(2, fname
.c_str());
113 int32_t _y
= L
.get_numeric_argument
<int32_t>(3, fname
.c_str());
114 L
.get_numeric_argument
<int64_t>(5, fgc
, fname
.c_str());
115 L
.get_numeric_argument
<int64_t>(6, bgc
, fname
.c_str());
116 L
.get_numeric_argument
<int64_t>(7, hlc
, fname
.c_str());
117 std::string text
= L
.get_string(4, fname
.c_str());
118 auto f
= lua_class
<lua_customfont
>::pin(L
, 1, fname
.c_str());
119 premultiplied_color
fg(fgc
);
120 premultiplied_color
bg(bgc
);
121 premultiplied_color
hl(hlc
);
122 lua_render_ctx
->queue
->create_add
<render_object_text_cf
>(_x
, _y
, text
, fg
, bg
, hl
, f
);
126 function_ptr_luafun
gui_text_cf(lua_func_misc
, "gui.loadfont", [](lua_state
& L
, const std::string
& fname
)
128 std::string filename
= L
.get_string(1, fname
.c_str());
129 lua_class
<lua_customfont
>::create(L
, filename
);