[lua] Add path functions to GraphicsContext object

This commit is contained in:
David Capello 2022-12-15 16:22:22 -03:00
parent 8b390d56fd
commit 789e857d98
3 changed files with 100 additions and 1 deletions

2
laf

@ -1 +1 @@
Subproject commit ad742295fb92edaa9c51c7cf8c68fe1e7d550357
Subproject commit 817e0c5b1c27f974f0fc298c96bb89f144d5715e

View File

@ -46,6 +46,18 @@ void GraphicsContext::drawImage(const doc::Image* img, int x, int y)
img->width(), img->height());
}
void GraphicsContext::stroke()
{
m_paint.style(os::Paint::Stroke);
m_surface->drawPath(m_path, m_paint);
}
void GraphicsContext::fill()
{
m_paint.style(os::Paint::Fill);
m_surface->drawPath(m_path, m_paint);
}
namespace {
int GraphicsContext_gc(lua_State* L)
@ -117,6 +129,72 @@ int GraphicsContext_drawImage(lua_State* L)
return 0;
}
int GraphicsContext_beginPath(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
gc->beginPath();
lua_pushvalue(L, 1);
return 1;
}
int GraphicsContext_closePath(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
gc->closePath();
lua_pushvalue(L, 1);
return 1;
}
int GraphicsContext_moveTo(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
float x = lua_tonumber(L, 2);
float y = lua_tonumber(L, 3);
gc->moveTo(x, y);
lua_pushvalue(L, 1);
return 1;
}
int GraphicsContext_lineTo(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
float x = lua_tonumber(L, 2);
float y = lua_tonumber(L, 3);
gc->lineTo(x, y);
lua_pushvalue(L, 1);
return 1;
}
int GraphicsContext_cubicTo(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
float cp1x = lua_tonumber(L, 2);
float cp1y = lua_tonumber(L, 3);
float cp2x = lua_tonumber(L, 4);
float cp2y = lua_tonumber(L, 5);
float x = lua_tonumber(L, 6);
float y = lua_tonumber(L, 7);
gc->cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
lua_pushvalue(L, 1);
return 1;
}
int GraphicsContext_stroke(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
gc->stroke();
lua_pushvalue(L, 1);
return 1;
}
int GraphicsContext_fill(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
gc->fill();
lua_pushvalue(L, 1);
return 1;
}
int GraphicsContext_get_width(lua_State* L)
{
auto gc = get_obj<GraphicsContext>(L, 1);
@ -185,6 +263,13 @@ const luaL_Reg GraphicsContext_methods[] = {
{ "fillText", GraphicsContext_fillText },
{ "measureText", GraphicsContext_measureText },
{ "drawImage", GraphicsContext_drawImage },
{ "beginPath", GraphicsContext_beginPath },
{ "closePath", GraphicsContext_closePath },
{ "moveTo", GraphicsContext_moveTo },
{ "lineTo", GraphicsContext_lineTo },
{ "cubicTo", GraphicsContext_cubicTo },
{ "stroke", GraphicsContext_stroke },
{ "fill", GraphicsContext_fill },
{ nullptr, nullptr }
};

View File

@ -10,6 +10,7 @@
#ifdef ENABLE_UI
#include "gfx/path.h"
#include "os/font.h"
#include "os/paint.h"
#include "os/surface.h"
@ -30,6 +31,7 @@ public:
std::swap(m_surface, gc.m_surface);
std::swap(m_paint, gc.m_paint);
std::swap(m_font, gc.m_font);
std::swap(m_path, gc.m_path);
}
os::FontRef font() const { return m_font; }
@ -75,10 +77,22 @@ public:
void drawImage(const doc::Image* img, int x, int y);
// Path
void beginPath() { m_path.reset(); }
void closePath() { m_path.close(); }
void moveTo(float x, float y) { m_path.moveTo(x, y); }
void lineTo(float x, float y) { m_path.lineTo(x, y); }
void cubicTo(float cp1x, float cp1y, float cp2x, float cp2y, float x, float y) {
m_path.cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
}
void stroke();
void fill();
private:
os::SurfaceRef m_surface = nullptr;
os::Paint m_paint;
os::FontRef m_font;
gfx::Path m_path;
std::stack<os::Paint> m_saved;
};