mirror of
https://github.com/aseprite/aseprite.git
synced 2025-02-23 09:41:04 +00:00
[lua] Add fillText() and measureText() functions to GraphicsContext
This commit is contained in:
parent
0aaa9fdddf
commit
b42a8b7ecf
@ -43,6 +43,7 @@ void Canvas::callPaint()
|
||||
|
||||
// Draw only on resize (onPaint we draw the cached m_surface)
|
||||
GraphicsContext gc(m_surface);
|
||||
gc.font(AddRef(font()));
|
||||
Paint(gc);
|
||||
}
|
||||
|
||||
|
@ -16,12 +16,25 @@
|
||||
#include "app/script/engine.h"
|
||||
#include "app/script/luacpp.h"
|
||||
#include "app/util/conversion_to_surface.h"
|
||||
#include "os/draw_text.h"
|
||||
|
||||
#ifdef ENABLE_UI
|
||||
|
||||
namespace app {
|
||||
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)
|
||||
{
|
||||
convert_image_to_surface(
|
||||
@ -72,6 +85,27 @@ int GraphicsContext_fillRect(lua_State* L)
|
||||
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)
|
||||
{
|
||||
auto gc = get_obj<GraphicsContext>(L, 1);
|
||||
@ -148,6 +182,8 @@ const luaL_Reg GraphicsContext_methods[] = {
|
||||
{ "restore", GraphicsContext_restore },
|
||||
{ "strokeRect", GraphicsContext_strokeRect },
|
||||
{ "fillRect", GraphicsContext_fillRect },
|
||||
{ "fillText", GraphicsContext_fillText },
|
||||
{ "measureText", GraphicsContext_measureText },
|
||||
{ "drawImage", GraphicsContext_drawImage },
|
||||
{ nullptr, nullptr }
|
||||
};
|
||||
|
@ -10,6 +10,7 @@
|
||||
|
||||
#ifdef ENABLE_UI
|
||||
|
||||
#include "os/font.h"
|
||||
#include "os/paint.h"
|
||||
#include "os/surface.h"
|
||||
|
||||
@ -27,8 +28,13 @@ public:
|
||||
GraphicsContext(const os::SurfaceRef& surface) : m_surface(surface) { }
|
||||
GraphicsContext(GraphicsContext&& gc) {
|
||||
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 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) {
|
||||
m_paint.style(os::Paint::Stroke);
|
||||
m_surface->drawRect(rc, m_paint);
|
||||
@ -55,20 +70,15 @@ public:
|
||||
m_surface->drawRect(rc, m_paint);
|
||||
}
|
||||
|
||||
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 fillText(const std::string& text, int x, int y);
|
||||
gfx::Size measureText(const std::string& text) const;
|
||||
|
||||
void drawImage(const doc::Image* img, int x, int y);
|
||||
|
||||
private:
|
||||
os::SurfaceRef m_surface = nullptr;
|
||||
os::Paint m_paint;
|
||||
os::FontRef m_font;
|
||||
std::stack<os::Paint> m_saved;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user