From 789e857d98a458132716523b57c62a636e99a7c7 Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 15 Dec 2022 16:22:22 -0300 Subject: [PATCH] [lua] Add path functions to GraphicsContext object --- laf | 2 +- src/app/script/graphics_context.cpp | 85 +++++++++++++++++++++++++++++ src/app/script/graphics_context.h | 14 +++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/laf b/laf index ad742295f..817e0c5b1 160000 --- a/laf +++ b/laf @@ -1 +1 @@ -Subproject commit ad742295fb92edaa9c51c7cf8c68fe1e7d550357 +Subproject commit 817e0c5b1c27f974f0fc298c96bb89f144d5715e diff --git a/src/app/script/graphics_context.cpp b/src/app/script/graphics_context.cpp index 2a8a2a897..5c09c7db8 100644 --- a/src/app/script/graphics_context.cpp +++ b/src/app/script/graphics_context.cpp @@ -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(L, 1); + gc->beginPath(); + lua_pushvalue(L, 1); + return 1; +} + +int GraphicsContext_closePath(lua_State* L) +{ + auto gc = get_obj(L, 1); + gc->closePath(); + lua_pushvalue(L, 1); + return 1; +} + +int GraphicsContext_moveTo(lua_State* L) +{ + auto gc = get_obj(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(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(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(L, 1); + gc->stroke(); + lua_pushvalue(L, 1); + return 1; +} + +int GraphicsContext_fill(lua_State* L) +{ + auto gc = get_obj(L, 1); + gc->fill(); + lua_pushvalue(L, 1); + return 1; +} + int GraphicsContext_get_width(lua_State* L) { auto gc = get_obj(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 } }; diff --git a/src/app/script/graphics_context.h b/src/app/script/graphics_context.h index 7281ae87d..cf280db74 100644 --- a/src/app/script/graphics_context.h +++ b/src/app/script/graphics_context.h @@ -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 m_saved; };