shithub: slug

Download patch

ref: bfa7ac2ac80fcaf49f8ab3ec052f0822386c182c
parent: 96cd321a31e76bd368ec6a6c73509701b4a14f64
author: phil9 <[email protected]>
date: Fri Dec 2 08:50:23 EST 2022

implement pushStyle() and popStyle()

	save/restore all style related attributes (fill, stroke, ...)

--- a/api.c
+++ b/api.c
@@ -1,6 +1,7 @@
 #include "a.h"
 
 typedef struct Tmatrix Tmatrix;
+typedef struct Style Style;
 
 struct Tmatrix
 {
@@ -8,6 +9,16 @@
 	double angle;
 };
 
+struct Style
+{
+	int		nostroke;
+	int		strokecap;
+	Image	*stroke;
+	int		strokewidth;
+	int		nofill;
+	Image	*fill;
+};
+
 enum
 {
 	Crgb,
@@ -32,6 +43,8 @@
 
 Tmatrix tstack[255] = {0};
 int ntstack;
+Style sstack[255] = {0};
+int nsstack;
 
 void
 initstate(lua_State *L)
@@ -464,6 +477,36 @@
 	return 0;
 }
 
+int
+cpushstyle(lua_State *L)
+{
+	if(nsstack == nelem(sstack) - 1)
+		return luaL_error(L, "stack overflow");
+	sstack[nsstack].nostroke = nostroke;
+	sstack[nsstack].strokecap = strokecap;
+	sstack[nsstack].stroke = stroke;
+	sstack[nsstack].strokewidth = strokewidth;
+	sstack[nsstack].nofill = nofill;
+	sstack[nsstack].fill = fill;
+	nsstack++;
+	return 0;
+}
+
+int
+cpopstyle(lua_State *L)
+{
+	if(nsstack == 0)
+		return luaL_error(L, "stack underflow");
+	nsstack--;
+	nostroke = sstack[nsstack].nostroke;
+	strokecap = sstack[nsstack].strokecap;
+	stroke = sstack[nsstack].stroke;
+	strokewidth = sstack[nsstack].strokewidth;
+	nofill = sstack[nsstack].nofill;
+	fill = sstack[nsstack].fill;
+	return 0;
+}
+
 void
 registerfunc(lua_State *L, const char *name, int(*f)(lua_State*))
 {
@@ -505,5 +548,7 @@
 	registerfunc(L, "radians", cradians);
 	registerfunc(L, "pushMatrix", cpushmatrix);
 	registerfunc(L, "popMatrix", cpopmatrix);
+	registerfunc(L, "pushStyle", cpushstyle);
+	registerfunc(L, "popStyle", cpopstyle);
 }