shithub: slug

Download patch

ref: 1122377ec3b6f86704e467a65ab22fdae2e45902
parent: f0e667390f5f9e9e1cf366e8db032f1f9718b9d2
author: phil9 <telephil9@gmail.com>
date: Thu Dec 1 01:24:29 EST 2022

implement strokeCap()

	strokeCap() takes a single parameter which is either SQUARE or ROUND.
	This defines the line ending style of line shapes (line, triangle, quad)

--- a/a.h
+++ b/a.h
@@ -13,13 +13,14 @@
 
 void	drawcanvas(void);
 void	initstate(lua_State*);
-void	registerfuncs(lua_State*);
+void	registerapi(lua_State*);
 
 Image*	color(int, int, int);
 Image*	grayscale(int);
 
-extern int drawing;
-extern Image* canvas;
-extern int width;
-extern int height;
-extern Point origin;
+extern int		drawing;
+extern Image	*canvas;
+extern int		width;
+extern int		height;
+extern Point	origin;
+
--- a/api.c
+++ b/api.c
@@ -1,14 +1,15 @@
 #include "a.h"
 
-Image *canvas;
-int width;
-int height;
-int nostroke;
-Image *stroke;
-int strokewidth;
-int nofill;
-Image *fill;
-Point origin;
+Image	*canvas;
+int		width;
+int		height;
+int		nostroke;
+int		strokecap;
+Image	*stroke;
+int		strokewidth;
+int		nofill;
+Image	*fill;
+Point	origin;
 
 void
 initstate(lua_State *L)
@@ -20,6 +21,7 @@
 	r = Rect(0, 0, width, height);
 	canvas = allocimage(display, r, screen->chan, 0, DWhite);
 	nostroke = 0;
+	strokecap = Endsquare;
 	stroke = display->black;
 	strokewidth = 1;
 	nofill = 0;
@@ -95,6 +97,17 @@
 }
 
 int
+cstrokecap(lua_State *L)
+{
+	int n;
+
+	n = luaL_checkinteger(L, 1);
+	if(n == Endsquare || n == Enddisc)
+		strokecap = n;
+	return 0;
+}
+
+int
 cstroke(lua_State *L)
 {
 	Image *i;
@@ -166,7 +179,7 @@
 	p1 = addpt(origin, Pt(x1, y1));
 	p2 = addpt(origin, Pt(x2, y2));
 	if(!nostroke)
-		line(canvas, p1, p2, 0, 0, strokewidth, stroke, ZP);
+		line(canvas, p1, p2, strokecap, strokecap, strokewidth, stroke, ZP);
 	return 0;
 }
 
@@ -285,7 +298,7 @@
 	if(!nofill)
 		fillpoly(canvas, p, 3, 0, fill, ZP);
 	if(!nostroke)
-		poly(canvas, p, 4, 0, 0, strokewidth, stroke, ZP);
+		poly(canvas, p, 4, strokecap, strokecap, strokewidth, stroke, ZP);
 	return 0;
 }
 
@@ -311,7 +324,7 @@
 	if(!nofill)
 		fillpoly(canvas, p, 4, 0, fill, ZP);
 	if(!nostroke)
-		poly(canvas, p, 5, 0, 0, strokewidth, stroke, ZP);
+		poly(canvas, p, 5, strokecap, strokecap, strokewidth, stroke, ZP);
 	return 0;
 
 }
@@ -335,11 +348,15 @@
 }
 
 void
-registerfuncs(lua_State *L)
+registerapi(lua_State *L)
 {
+	lset(L, "SQUARE", Endsquare);
+	lset(L, "ROUND", Enddisc);
+
 	registerfunc(L, "size", csize);
 	registerfunc(L, "background", cbackground);
 	registerfunc(L, "noStroke", cnostroke);
+	registerfunc(L, "strokeCap", cstrokecap);
 	registerfunc(L, "stroke", cstroke);
 	registerfunc(L, "strokeWidth", cstrokewidth);
 	registerfunc(L, "noFill", cnofill);
--- a/slug.c
+++ b/slug.c
@@ -117,7 +117,7 @@
 	alts[1].c = mc->resizec;
 	alts[2].c = kc->c;
 	L = linit(argc, argv);
-	registerfuncs(L);
+	registerapi(L);
 	initstate(L);
 	resize(L, width, height);
 	drawing = 0;