[READ-ONLY] Mirror of https://github.com/thoda-dev/svg-lua. svg lua library
0

Configure Feed

Select the types of activity you want to include in your feed.

v 1.5.0 fix several bugs and lower amount of characters and memory usage

Thomas (Mar 11, 2019, 2:03 PM +0100) 5f8dd5ee 463c1ac5

+97 -50
+1 -1
svg-min.lua
··· 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.."</"..f..">"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="<svg width=\""..self.width.."\" height=\""..self.height.."\" fill=\""..self.fill.."\" stroke=\""..self.stroke.."\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">"for n,o in pairs(self.elements)do m=m..o.strElement end;m=m.."</svg>"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 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.."</"..f..">"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="<svg width=\""..self.width.."\" height=\""..self.height.."\" fill=\""..self.fill.."\" stroke=\""..self.stroke.."\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">"for n,o in pairs(self.elements)do m=m..o.strElement end;m=m.."</svg>"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
+87 -40
svg.lua
··· 147 147 -- fill: string => fill color 148 148 -- tranform: string => transformation options (eg: rotation) 149 149 function svg:Circle(r, cx, cy, stroke, strokeWidth, fill, transform) 150 - return self.Element:create("circle", { 150 + local options = { 151 151 r = r or 25, 152 152 cx = cx or 30, 153 - cy = cy or 30, 154 - stroke = stroke or self.stroke, 155 - ["stroke-width"] = strokeWidth or 1, 156 - fill = fill or self.fill, 157 - transform = transform or "" 158 - }) 153 + cy = cy or 30 154 + } 155 + 156 + if not (stroke == nil) and not (stroke == "") then 157 + options.stroke = stroke 158 + end 159 + if not (strokeWidth == nil) and not (strokeWidth == "") then 160 + options["stroke-width"] = strokeWidth 161 + end 162 + if not (fill == nil) and not (fill == "") then 163 + options.fill = fill 164 + end 165 + if not (transform == nil) and not (transform == "") then 166 + options.transform = transform 167 + end 168 + return self.Element:create("circle", options) 159 169 end 160 170 function svg:addCircle(r, cx, cy, stroke, strokeWidth, fill, transform) 161 171 self:add(self:Circle(r, cx, cy, stroke, strokeWidth, fill, transform)) ··· 169 179 -- stroke: string => lines color 170 180 -- strokeWidth: string => lines width 171 181 -- fill: string => fill color 172 - -- tranform: string => transformation options (eg: rotation) 182 + -- transform: string => transformation options (eg: rotation) 173 183 function svg:Ellipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform) 174 - return self.Element:create("ellipse", { 175 - rx = r or 25, 176 - ry = r or 15, 184 + local options = { 185 + rx = rx or 25, 186 + ry = ry or 15, 177 187 cx = cx or 30, 178 - cy = cy or 30, 179 - stroke = stroke or self.stroke, 180 - ["stroke-width"] = strokeWidth or 1, 181 - fill = fill or self.fill, 182 - transform = transform or "" 183 - }) 188 + cy = cy or 30 189 + } 190 + 191 + if not (stroke == nil) and not (stroke == "") then 192 + options.stroke = stroke 193 + end 194 + if not (strokeWidth == nil) and not (strokeWidth == "") then 195 + options["stroke-width"] = strokeWidth 196 + end 197 + if not (fill == nil) and not (fill == "") then 198 + options.fill = fill 199 + end 200 + if not (transform == nil) and not (transform == "") then 201 + options.transform = transform 202 + end 203 + return self.Element:create("ellipse", options) 184 204 end 185 205 function svg:addEllipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform) 186 206 self:add(self:Ellipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform)) ··· 195 215 -- strokeWidth: string => lines width 196 216 -- fill: string => fill color 197 217 function svg:Line(x1, y1, x2, y2, stroke, strokeWidth, fill) 198 - return self.Element:create("line", { 199 - x1 = r or 15, 200 - y1 = r or 15, 201 - x2 = cx or 50, 202 - y2 = cy or 45, 203 - stroke = stroke or self.stroke, 204 - ["stroke-width"] = strokeWidth or 1, 205 - fill = fill or self.fill 206 - }) 218 + local options = { 219 + x1 = x1 or 15, 220 + y1 = y1 or 15, 221 + x2 = x2 or 50, 222 + y2 = y2 or 45 223 + } 224 + if not (stroke == nil) and not (stroke == "") then 225 + options.stroke = stroke 226 + end 227 + if not (strokeWidth == nil) and not (strokeWidth == "") then 228 + options["stroke-width"] = strokeWidth 229 + end 230 + if not (fill == nil) and not (fill == "") then 231 + options.fill = fill 232 + end 233 + return self.Element:create("line", options) 207 234 end 208 235 function svg:addLine(x1, y1, x2, y2, stroke, strokeWidth, fill) 209 236 self:add(self:Line(x1, y1, x2, y2, stroke, strokeWidth, fill)) ··· 216 243 -- strokeWidth: string => lines width 217 244 -- fill: string => fill color 218 245 function svg:Polyline(points, stroke, strokeWidth, fill, transform) 219 - return self.Element:create("polyline", { 220 - points = points or "10,10 50,50 50,25 75,25", 221 - stroke = stroke or self.stroke, 222 - ["stroke-width"] = strokeWidth or 1, 223 - fill = fill or self.fill, 224 - transform = transform or "" 225 - }) 246 + local options = { 247 + points = points or "10,10 50,50 50,25 75,25" 248 + } 249 + 250 + if not (stroke == nil) and not (stroke == "") then 251 + options.stroke = stroke 252 + end 253 + if not (strokeWidth == nil) and not (strokeWidth == "") then 254 + options["stroke-width"] = strokeWidth 255 + end 256 + if not (fill == nil) and not (fill == "") then 257 + options.fill = fill 258 + end 259 + if not (transform == nil) and not (transform == "") then 260 + options.transform = transform 261 + end 262 + return self.Element:create("polyline", options) 226 263 end 227 264 function svg:addPolyline(points, stroke, strokeWidth, fill, transform) 228 265 self:add(self:Polyline(points, stroke, strokeWidth, fill, transform)) ··· 234 271 -- strokeWidth: string => lines width 235 272 -- fill: string => fill color 236 273 function svg:Path(d, stroke, strokeWidth, fill, transform) 237 - return self.Element:create("path", { 238 - d = d or "M50 0 L75 100 L25 100 Z", 239 - stroke = stroke or self.stroke, 240 - ["stroke-width"] = strokeWidth or 1, 241 - fill = fill or self.fill, 242 - transform = transform or "" 243 - }) 274 + local options = { 275 + d = d or "M50 0 L75 100 L25 100 Z" 276 + } 277 + 278 + if not (stroke == nil) and not (stroke == "") then 279 + options.stroke = stroke 280 + end 281 + if not (strokeWidth == nil) and not (strokeWidth == "") then 282 + options["stroke-width"] = strokeWidth 283 + end 284 + if not (fill == nil) and not (fill == "") then 285 + options.fill = fill 286 + end 287 + if not (transform == nil) and not (transform == "") then 288 + options.transform = transform 289 + end 290 + return self.Element:create("path", options) 244 291 end 245 292 function svg:addPath(d, stroke, strokeWidth, fill, transform) 246 293 self:add(self:Path(d, stroke, strokeWidth, fill, transform))
+9 -9
test.lua
··· 1 1 -- INCLUDE THE CURRENT PATH FOR LUA AND REQUIRE THE LIB -- 2 2 -- DO NOT CHANGE THAT PART -- 3 3 package.path = package.path .. ";" .. "..\\?.lua" 4 - require "svg" 4 + require "svg-min" 5 5 -- END OF LIBRARY IMPORT -- 6 6 7 7 -- TEST IS STARTING HERE -- YOU CAN MODIFY IT -- 8 8 9 9 mon_dessin = svg:create() 10 - -- mon_dessin:addText("Hello World !!") 11 - -- mon_dessin:addRect() 12 - -- mon_dessin:addCircle() 13 - -- mon_dessin:addEllipse() 14 - -- mon_dessin:addLine() 15 - -- mon_dessin:addPolyline() 16 - -- mon_dessin:addPath() 17 - print(mon_dessin:innerdraw()) 10 + mon_dessin:addText("Hello World !!") 11 + mon_dessin:addRect() 12 + mon_dessin:addCircle() 13 + mon_dessin:addEllipse() 14 + mon_dessin:addLine() 15 + mon_dessin:addPolyline() 16 + mon_dessin:addPath() 17 + print(mon_dessin:draw())