diff --git a/svg-min.lua b/svg-min.lua index 608fb89..f7916eb 100644 --- a/svg-min.lua +++ b/svg-min.lua @@ -1 +1 @@ -svg={}svg.__index=svg;function svg:create(a,b,c,d)local e={}setmetatable(e,svg)e.width=a or 100;e.height=b or 100;e.stroke=c or"#000000"e.fill=d or"transparent"e.elements={}e.Element={}e.Element.__index=svg.Element;function e.Element:create(f,g,h)local i={}setmetatable(i,e.Element)i.name=f;i.strElement="<"..f.." "for j,k in pairs(g)do i[j]=k;i.strElement=i.strElement..j.."=\""..k.."\" "end;if h~=nil then i.strElement=i.strElement..">"..h..""else i.strElement=i.strElement.."/>"end;return i end;return e end;function svg:add(l)table.insert(self.elements,l)end;function svg:draw()local m=""for n,o in pairs(self.elements)do m=m..o.strElement end;m=m..""return m end;function svg:innerdraw()local m=""for n,o in pairs(self.elements)do m=m..o.strElement end;return m end;function svg:Text(p,q,s,t,u,v)return self.Element:create("text",{x=q or 10,y=s or 50,["text-anchor"]=t or"start",style=u or"font-family: Verdana; font-size: 10; stroke: "..self.stroke.."; fill: "..self.fill..";",transform=v or""},p)end;function svg:addText(p,q,s,u,v)self:add(self:Text(p,q,s,u,v))end;function svg:Rect(q,s,a,b,c,w,d,x,y,v)return self.Element:create("rect",{x=q or 10,y=s or 10,width=a or 10,height=b or 10,stroke=c or self.stroke,fill=d or self.fill,["stroke-width"]=w or 1,rx=x or 0,ry=y or 0,transform=v or""})end;function svg:addRect(q,s,a,b,c,w,d,x,y,v)self:add(self:Rect(q,s,a,b,c,w,d,x,y,v))end;function svg:Circle(r,cx,cy,c,w,d,v)return self.Element:create("circle",{r=r or 25,cx=cx or 30,cy=cy or 30,stroke=c or self.stroke,["stroke-width"]=w or 1,fill=d or self.fill,transform=v or""})end;function svg:addCircle(r,cx,cy,c,w,d,v)self:add(self:Circle(r,cx,cy,c,w,d,v))end;function svg:Ellipse(cx,cy,x,y,c,w,d,v)return self.Element:create("ellipse",{rx=r or 25,ry=r or 15,cx=cx or 30,cy=cy or 30,stroke=c or self.stroke,["stroke-width"]=w or 1,fill=d or self.fill,transform=v or""})end;function svg:addEllipse(cx,cy,x,y,c,w,d,v)self:add(self:Ellipse(cx,cy,x,y,c,w,d,v))end;function svg:Line(z,A,B,C,c,w,d)return self.Element:create("line",{x1=r or 15,y1=r or 15,x2=cx or 50,y2=cy or 45,stroke=c or self.stroke,["stroke-width"]=w or 1,fill=d or self.fill})end;function svg:addLine(z,A,B,C,c,w,d)self:add(self:Line(z,A,B,C,c,w,d))end;function svg:Polyline(D,c,w,d,v)return self.Element:create("polyline",{points=D or"10,10 50,50 50,25 75,25",stroke=c or self.stroke,["stroke-width"]=w or 1,fill=d or self.fill,transform=v or""})end;function svg:addPolyline(D,c,w,d,v)self:add(self:Polyline(D,c,w,d,v))end;function svg:Path(E,c,w,d,v)return self.Element:create("path",{d=E or"M50 0 L75 100 L25 100 Z",stroke=c or self.stroke,["stroke-width"]=w or 1,fill=d or self.fill,transform=v or""})end;function svg:addPath(E,c,w,d,v)self:add(self:Path(E,c,w,d,v))end +svg={}svg.__index=svg;function svg:create(a,b,c,d)local e={}setmetatable(e,svg)e.width=a or 100;e.height=b or 100;e.stroke=c or"#000000"e.fill=d or"transparent"e.elements={}e.Element={}e.Element.__index=svg.Element;function e.Element:create(f,g,h)local i={}setmetatable(i,e.Element)i.name=f;i.strElement="<"..f.." "for j,k in pairs(g)do i[j]=k;i.strElement=i.strElement..j.."=\""..k.."\" "end;if h~=nil then i.strElement=i.strElement..">"..h..""else i.strElement=i.strElement.."/>"end;return i end;return e end;function svg:add(l)table.insert(self.elements,l)end;function svg:draw()local m=""for n,o in pairs(self.elements)do m=m..o.strElement end;m=m..""return m end;function svg:innerdraw()local m=""for n,o in pairs(self.elements)do m=m..o.strElement end;return m end;function svg:Text(p,q,r,s,t,u)local g={x=q or 0,y=r or 0}if not(s==nil)and not(s=="")then g["text-anchor"]=s end;if not(t==nil)and not(t=="")then g.style=t end;if not(u==nil)and not(u=="")then g.transform=u end;return self.Element:create("text",g,p)end;function svg:addText(p,q,r,s,t,u)self:add(self:Text(p,q,r,s,t,u))end;function svg:Rect(q,r,a,b,c,v,d,w,x,u)local g={x=q or 0,y=r or 0,width=a or 20,height=b or 10}if not(c==nil)and not(c=="")then g.stroke=c end;if not(d==nil)and not(d=="")then g.fill=d end;if not(v==nil)and not(v=="")then g["stroke-width"]=v end;if not(w==nil)and not(w=="")then g.rx=w end;if not(x==nil)and not(x=="")then g.ry=x end;if not(u==nil)and not(u=="")then g.transform=u end;return self.Element:create("rect",g)end;function svg:addRect(q,r,a,b,c,v,d,w,x,u)self:add(self:Rect(q,r,a,b,c,v,d,w,x,u))end;function svg:Circle(y,z,A,c,v,d,u)local g={r=y or 25,cx=z or 30,cy=A or 30}if not(c==nil)and not(c=="")then g.stroke=c end;if not(v==nil)and not(v=="")then g["stroke-width"]=v end;if not(d==nil)and not(d=="")then g.fill=d end;if not(u==nil)and not(u=="")then g.transform=u end;return self.Element:create("circle",g)end;function svg:addCircle(y,z,A,c,v,d,u)self:add(self:Circle(y,z,A,c,v,d,u))end;function svg:Ellipse(z,A,w,x,c,v,d,u)local g={rx=w or 25,ry=x or 15,cx=z or 30,cy=A or 30}if not(c==nil)and not(c=="")then g.stroke=c end;if not(v==nil)and not(v=="")then g["stroke-width"]=v end;if not(d==nil)and not(d=="")then g.fill=d end;if not(u==nil)and not(u=="")then g.transform=u end;return self.Element:create("ellipse",g)end;function svg:addEllipse(z,A,w,x,c,v,d,u)self:add(self:Ellipse(z,A,w,x,c,v,d,u))end;function svg:Line(B,C,D,E,c,v,d)local g={x1=B or 15,y1=C or 15,x2=D or 50,y2=E or 45}if not(c==nil)and not(c=="")then g.stroke=c end;if not(v==nil)and not(v=="")then g["stroke-width"]=v end;if not(d==nil)and not(d=="")then g.fill=d end;return self.Element:create("line",g)end;function svg:addLine(B,C,D,E,c,v,d)self:add(self:Line(B,C,D,E,c,v,d))end;function svg:Polyline(F,c,v,d,u)local g={points=F or"10,10 50,50 50,25 75,25"}if not(c==nil)and not(c=="")then g.stroke=c end;if not(v==nil)and not(v=="")then g["stroke-width"]=v end;if not(d==nil)and not(d=="")then g.fill=d end;if not(u==nil)and not(u=="")then g.transform=u end;return self.Element:create("polyline",g)end;function svg:addPolyline(F,c,v,d,u)self:add(self:Polyline(F,c,v,d,u))end;function svg:Path(G,c,v,d,u)local g={d=G or"M50 0 L75 100 L25 100 Z"}if not(c==nil)and not(c=="")then g.stroke=c end;if not(v==nil)and not(v=="")then g["stroke-width"]=v end;if not(d==nil)and not(d=="")then g.fill=d end;if not(u==nil)and not(u=="")then g.transform=u end;return self.Element:create("path",g)end;function svg:addPath(G,c,v,d,u)self:add(self:Path(G,c,v,d,u))end diff --git a/svg.lua b/svg.lua index d1358a2..044c1d1 100644 --- a/svg.lua +++ b/svg.lua @@ -147,15 +147,25 @@ end -- fill: string => fill color -- tranform: string => transformation options (eg: rotation) function svg:Circle(r, cx, cy, stroke, strokeWidth, fill, transform) - return self.Element:create("circle", { + local options = { r = r or 25, cx = cx or 30, - cy = cy or 30, - stroke = stroke or self.stroke, - ["stroke-width"] = strokeWidth or 1, - fill = fill or self.fill, - transform = transform or "" - }) + cy = cy or 30 + } + + if not (stroke == nil) and not (stroke == "") then + options.stroke = stroke + end + if not (strokeWidth == nil) and not (strokeWidth == "") then + options["stroke-width"] = strokeWidth + end + if not (fill == nil) and not (fill == "") then + options.fill = fill + end + if not (transform == nil) and not (transform == "") then + options.transform = transform + end + return self.Element:create("circle", options) end function svg:addCircle(r, cx, cy, stroke, strokeWidth, fill, transform) self:add(self:Circle(r, cx, cy, stroke, strokeWidth, fill, transform)) @@ -169,18 +179,28 @@ end -- stroke: string => lines color -- strokeWidth: string => lines width -- fill: string => fill color --- tranform: string => transformation options (eg: rotation) +-- transform: string => transformation options (eg: rotation) function svg:Ellipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform) - return self.Element:create("ellipse", { - rx = r or 25, - ry = r or 15, + local options = { + rx = rx or 25, + ry = ry or 15, cx = cx or 30, - cy = cy or 30, - stroke = stroke or self.stroke, - ["stroke-width"] = strokeWidth or 1, - fill = fill or self.fill, - transform = transform or "" - }) + cy = cy or 30 + } + + if not (stroke == nil) and not (stroke == "") then + options.stroke = stroke + end + if not (strokeWidth == nil) and not (strokeWidth == "") then + options["stroke-width"] = strokeWidth + end + if not (fill == nil) and not (fill == "") then + options.fill = fill + end + if not (transform == nil) and not (transform == "") then + options.transform = transform + end + return self.Element:create("ellipse", options) end function svg:addEllipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform) self:add(self:Ellipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform)) @@ -195,15 +215,22 @@ end -- strokeWidth: string => lines width -- fill: string => fill color function svg:Line(x1, y1, x2, y2, stroke, strokeWidth, fill) - return self.Element:create("line", { - x1 = r or 15, - y1 = r or 15, - x2 = cx or 50, - y2 = cy or 45, - stroke = stroke or self.stroke, - ["stroke-width"] = strokeWidth or 1, - fill = fill or self.fill - }) + local options = { + x1 = x1 or 15, + y1 = y1 or 15, + x2 = x2 or 50, + y2 = y2 or 45 + } + if not (stroke == nil) and not (stroke == "") then + options.stroke = stroke + end + if not (strokeWidth == nil) and not (strokeWidth == "") then + options["stroke-width"] = strokeWidth + end + if not (fill == nil) and not (fill == "") then + options.fill = fill + end + return self.Element:create("line", options) end function svg:addLine(x1, y1, x2, y2, stroke, strokeWidth, fill) self:add(self:Line(x1, y1, x2, y2, stroke, strokeWidth, fill)) @@ -216,13 +243,23 @@ end -- strokeWidth: string => lines width -- fill: string => fill color function svg:Polyline(points, stroke, strokeWidth, fill, transform) - return self.Element:create("polyline", { - points = points or "10,10 50,50 50,25 75,25", - stroke = stroke or self.stroke, - ["stroke-width"] = strokeWidth or 1, - fill = fill or self.fill, - transform = transform or "" - }) + local options = { + points = points or "10,10 50,50 50,25 75,25" + } + + if not (stroke == nil) and not (stroke == "") then + options.stroke = stroke + end + if not (strokeWidth == nil) and not (strokeWidth == "") then + options["stroke-width"] = strokeWidth + end + if not (fill == nil) and not (fill == "") then + options.fill = fill + end + if not (transform == nil) and not (transform == "") then + options.transform = transform + end + return self.Element:create("polyline", options) end function svg:addPolyline(points, stroke, strokeWidth, fill, transform) self:add(self:Polyline(points, stroke, strokeWidth, fill, transform)) @@ -234,13 +271,23 @@ end -- strokeWidth: string => lines width -- fill: string => fill color function svg:Path(d, stroke, strokeWidth, fill, transform) - return self.Element:create("path", { - d = d or "M50 0 L75 100 L25 100 Z", - stroke = stroke or self.stroke, - ["stroke-width"] = strokeWidth or 1, - fill = fill or self.fill, - transform = transform or "" - }) + local options = { + d = d or "M50 0 L75 100 L25 100 Z" + } + + if not (stroke == nil) and not (stroke == "") then + options.stroke = stroke + end + if not (strokeWidth == nil) and not (strokeWidth == "") then + options["stroke-width"] = strokeWidth + end + if not (fill == nil) and not (fill == "") then + options.fill = fill + end + if not (transform == nil) and not (transform == "") then + options.transform = transform + end + return self.Element:create("path", options) end function svg:addPath(d, stroke, strokeWidth, fill, transform) self:add(self:Path(d, stroke, strokeWidth, fill, transform)) diff --git a/test.lua b/test.lua index 6133938..2c8f401 100644 --- a/test.lua +++ b/test.lua @@ -1,17 +1,17 @@ -- INCLUDE THE CURRENT PATH FOR LUA AND REQUIRE THE LIB -- -- DO NOT CHANGE THAT PART -- package.path = package.path .. ";" .. "..\\?.lua" -require "svg" +require "svg-min" -- END OF LIBRARY IMPORT -- -- TEST IS STARTING HERE -- YOU CAN MODIFY IT -- mon_dessin = svg:create() --- mon_dessin:addText("Hello World !!") --- mon_dessin:addRect() --- mon_dessin:addCircle() --- mon_dessin:addEllipse() --- mon_dessin:addLine() --- mon_dessin:addPolyline() --- mon_dessin:addPath() -print(mon_dessin:innerdraw()) \ No newline at end of file +mon_dessin:addText("Hello World !!") +mon_dessin:addRect() +mon_dessin:addCircle() +mon_dessin:addEllipse() +mon_dessin:addLine() +mon_dessin:addPolyline() +mon_dessin:addPath() +print(mon_dessin:draw()) \ No newline at end of file