[lua] Add fillText() and measureText() functions to GraphicsContext

This commit is contained in:
David Capello 2022-12-15 10:01:13 -03:00
parent 0aaa9fdddf
commit b42a8b7ecf
3 changed files with 55 additions and 8 deletions

View File

@ -43,6 +43,7 @@ void Canvas::callPaint()
// Draw only on resize (onPaint we draw the cached m_surface) // Draw only on resize (onPaint we draw the cached m_surface)
GraphicsContext gc(m_surface); GraphicsContext gc(m_surface);
gc.font(AddRef(font()));
Paint(gc); Paint(gc);
} }

View File

@ -16,12 +16,25 @@
#include "app/script/engine.h" #include "app/script/engine.h"
#include "app/script/luacpp.h" #include "app/script/luacpp.h"
#include "app/util/conversion_to_surface.h" #include "app/util/conversion_to_surface.h"
#include "os/draw_text.h"
#ifdef ENABLE_UI #ifdef ENABLE_UI
namespace app { namespace app {
namespace script { namespace script {
void GraphicsContext::fillText(const std::string& text, int x, int y)
{
os::draw_text(m_surface.get(), m_font.get(),
text, m_paint.color(), 0, x, y, nullptr);
}
gfx::Size GraphicsContext::measureText(const std::string& text) const
{
return os::draw_text(nullptr, m_font.get(), text,
0, 0, 0, 0, nullptr).size();
}
void GraphicsContext::drawImage(const doc::Image* img, int x, int y) void GraphicsContext::drawImage(const doc::Image* img, int x, int y)
{ {
convert_image_to_surface( convert_image_to_surface(
@ -72,6 +85,27 @@ int GraphicsContext_fillRect(lua_State* L)
return 0; return 0;
} }
int GraphicsContext_fillText(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
if (const char* text = lua_tostring(L, 2)) {
int x = lua_tointeger(L, 3);
int y = lua_tointeger(L, 4);
gc->fillText(text, x, y);
}
return 0;
}
int GraphicsContext_measureText(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
if (const char* text = lua_tostring(L, 2)) {
push_obj(L, gc->measureText(text));
return 1;
}
return 0;
}
int GraphicsContext_drawImage(lua_State* L) int GraphicsContext_drawImage(lua_State* L)
{ {
auto gc = get_obj<GraphicsContext>(L, 1); auto gc = get_obj<GraphicsContext>(L, 1);
@ -148,6 +182,8 @@ const luaL_Reg GraphicsContext_methods[] = {
{ "restore", GraphicsContext_restore }, { "restore", GraphicsContext_restore },
{ "strokeRect", GraphicsContext_strokeRect }, { "strokeRect", GraphicsContext_strokeRect },
{ "fillRect", GraphicsContext_fillRect }, { "fillRect", GraphicsContext_fillRect },
{ "fillText", GraphicsContext_fillText },
{ "measureText", GraphicsContext_measureText },
{ "drawImage", GraphicsContext_drawImage }, { "drawImage", GraphicsContext_drawImage },
{ nullptr, nullptr } { nullptr, nullptr }
}; };

View File

@ -10,6 +10,7 @@
#ifdef ENABLE_UI #ifdef ENABLE_UI
#include "os/font.h"
#include "os/paint.h" #include "os/paint.h"
#include "os/surface.h" #include "os/surface.h"
@ -27,8 +28,13 @@ public:
GraphicsContext(const os::SurfaceRef& surface) : m_surface(surface) { } GraphicsContext(const os::SurfaceRef& surface) : m_surface(surface) { }
GraphicsContext(GraphicsContext&& gc) { GraphicsContext(GraphicsContext&& gc) {
std::swap(m_surface, gc.m_surface); std::swap(m_surface, gc.m_surface);
std::swap(m_paint, gc.m_paint);
std::swap(m_font, gc.m_font);
} }
os::FontRef font() const { return m_font; }
void font(const os::FontRef& font) { m_font = font; }
int width() const { return m_surface->width(); } int width() const { return m_surface->width(); }
int height() const { return m_surface->height(); } int height() const { return m_surface->height(); }
@ -45,6 +51,15 @@ public:
} }
} }
bool antialias() const { return m_paint.antialias(); }
void antialias(bool value) { m_paint.antialias(value); }
gfx::Color color() const { return m_paint.color(); }
void color(gfx::Color color) { m_paint.color(color); }
float strokeWidth() const { return m_paint.strokeWidth(); }
void strokeWidth(float value) { m_paint.strokeWidth(value); }
void strokeRect(const gfx::Rect& rc) { void strokeRect(const gfx::Rect& rc) {
m_paint.style(os::Paint::Stroke); m_paint.style(os::Paint::Stroke);
m_surface->drawRect(rc, m_paint); m_surface->drawRect(rc, m_paint);
@ -55,20 +70,15 @@ public:
m_surface->drawRect(rc, m_paint); m_surface->drawRect(rc, m_paint);
} }
bool antialias() const { return m_paint.antialias(); } void fillText(const std::string& text, int x, int y);
void antialias(bool value) { m_paint.antialias(value); } gfx::Size measureText(const std::string& text) const;
gfx::Color color() const { return m_paint.color(); }
void color(gfx::Color color) { m_paint.color(color); }
float strokeWidth() const { return m_paint.strokeWidth(); }
void strokeWidth(float value) { m_paint.strokeWidth(value); }
void drawImage(const doc::Image* img, int x, int y); void drawImage(const doc::Image* img, int x, int y);
private: private:
os::SurfaceRef m_surface = nullptr; os::SurfaceRef m_surface = nullptr;
os::Paint m_paint; os::Paint m_paint;
os::FontRef m_font;
std::stack<os::Paint> m_saved; std::stack<os::Paint> m_saved;
}; };