···11-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
11+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
···147147-- fill: string => fill color
148148-- tranform: string => transformation options (eg: rotation)
149149function svg:Circle(r, cx, cy, stroke, strokeWidth, fill, transform)
150150- return self.Element:create("circle", {
150150+ local options = {
151151 r = r or 25,
152152 cx = cx or 30,
153153- cy = cy or 30,
154154- stroke = stroke or self.stroke,
155155- ["stroke-width"] = strokeWidth or 1,
156156- fill = fill or self.fill,
157157- transform = transform or ""
158158- })
153153+ cy = cy or 30
154154+ }
155155+156156+ if not (stroke == nil) and not (stroke == "") then
157157+ options.stroke = stroke
158158+ end
159159+ if not (strokeWidth == nil) and not (strokeWidth == "") then
160160+ options["stroke-width"] = strokeWidth
161161+ end
162162+ if not (fill == nil) and not (fill == "") then
163163+ options.fill = fill
164164+ end
165165+ if not (transform == nil) and not (transform == "") then
166166+ options.transform = transform
167167+ end
168168+ return self.Element:create("circle", options)
159169end
160170function svg:addCircle(r, cx, cy, stroke, strokeWidth, fill, transform)
161171 self:add(self:Circle(r, cx, cy, stroke, strokeWidth, fill, transform))
···169179-- stroke: string => lines color
170180-- strokeWidth: string => lines width
171181-- fill: string => fill color
172172--- tranform: string => transformation options (eg: rotation)
182182+-- transform: string => transformation options (eg: rotation)
173183function svg:Ellipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform)
174174- return self.Element:create("ellipse", {
175175- rx = r or 25,
176176- ry = r or 15,
184184+ local options = {
185185+ rx = rx or 25,
186186+ ry = ry or 15,
177187 cx = cx or 30,
178178- cy = cy or 30,
179179- stroke = stroke or self.stroke,
180180- ["stroke-width"] = strokeWidth or 1,
181181- fill = fill or self.fill,
182182- transform = transform or ""
183183- })
188188+ cy = cy or 30
189189+ }
190190+191191+ if not (stroke == nil) and not (stroke == "") then
192192+ options.stroke = stroke
193193+ end
194194+ if not (strokeWidth == nil) and not (strokeWidth == "") then
195195+ options["stroke-width"] = strokeWidth
196196+ end
197197+ if not (fill == nil) and not (fill == "") then
198198+ options.fill = fill
199199+ end
200200+ if not (transform == nil) and not (transform == "") then
201201+ options.transform = transform
202202+ end
203203+ return self.Element:create("ellipse", options)
184204end
185205function svg:addEllipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform)
186206 self:add(self:Ellipse(cx, cy, rx, ry, stroke, strokeWidth, fill, transform))
···195215-- strokeWidth: string => lines width
196216-- fill: string => fill color
197217function svg:Line(x1, y1, x2, y2, stroke, strokeWidth, fill)
198198- return self.Element:create("line", {
199199- x1 = r or 15,
200200- y1 = r or 15,
201201- x2 = cx or 50,
202202- y2 = cy or 45,
203203- stroke = stroke or self.stroke,
204204- ["stroke-width"] = strokeWidth or 1,
205205- fill = fill or self.fill
206206- })
218218+ local options = {
219219+ x1 = x1 or 15,
220220+ y1 = y1 or 15,
221221+ x2 = x2 or 50,
222222+ y2 = y2 or 45
223223+ }
224224+ if not (stroke == nil) and not (stroke == "") then
225225+ options.stroke = stroke
226226+ end
227227+ if not (strokeWidth == nil) and not (strokeWidth == "") then
228228+ options["stroke-width"] = strokeWidth
229229+ end
230230+ if not (fill == nil) and not (fill == "") then
231231+ options.fill = fill
232232+ end
233233+ return self.Element:create("line", options)
207234end
208235function svg:addLine(x1, y1, x2, y2, stroke, strokeWidth, fill)
209236 self:add(self:Line(x1, y1, x2, y2, stroke, strokeWidth, fill))
···216243-- strokeWidth: string => lines width
217244-- fill: string => fill color
218245function svg:Polyline(points, stroke, strokeWidth, fill, transform)
219219- return self.Element:create("polyline", {
220220- points = points or "10,10 50,50 50,25 75,25",
221221- stroke = stroke or self.stroke,
222222- ["stroke-width"] = strokeWidth or 1,
223223- fill = fill or self.fill,
224224- transform = transform or ""
225225- })
246246+ local options = {
247247+ points = points or "10,10 50,50 50,25 75,25"
248248+ }
249249+250250+ if not (stroke == nil) and not (stroke == "") then
251251+ options.stroke = stroke
252252+ end
253253+ if not (strokeWidth == nil) and not (strokeWidth == "") then
254254+ options["stroke-width"] = strokeWidth
255255+ end
256256+ if not (fill == nil) and not (fill == "") then
257257+ options.fill = fill
258258+ end
259259+ if not (transform == nil) and not (transform == "") then
260260+ options.transform = transform
261261+ end
262262+ return self.Element:create("polyline", options)
226263end
227264function svg:addPolyline(points, stroke, strokeWidth, fill, transform)
228265 self:add(self:Polyline(points, stroke, strokeWidth, fill, transform))
···234271-- strokeWidth: string => lines width
235272-- fill: string => fill color
236273function svg:Path(d, stroke, strokeWidth, fill, transform)
237237- return self.Element:create("path", {
238238- d = d or "M50 0 L75 100 L25 100 Z",
239239- stroke = stroke or self.stroke,
240240- ["stroke-width"] = strokeWidth or 1,
241241- fill = fill or self.fill,
242242- transform = transform or ""
243243- })
274274+ local options = {
275275+ d = d or "M50 0 L75 100 L25 100 Z"
276276+ }
277277+278278+ if not (stroke == nil) and not (stroke == "") then
279279+ options.stroke = stroke
280280+ end
281281+ if not (strokeWidth == nil) and not (strokeWidth == "") then
282282+ options["stroke-width"] = strokeWidth
283283+ end
284284+ if not (fill == nil) and not (fill == "") then
285285+ options.fill = fill
286286+ end
287287+ if not (transform == nil) and not (transform == "") then
288288+ options.transform = transform
289289+ end
290290+ return self.Element:create("path", options)
244291end
245292function svg:addPath(d, stroke, strokeWidth, fill, transform)
246293 self:add(self:Path(d, stroke, strokeWidth, fill, transform))
+9-9
test.lua
···11-- INCLUDE THE CURRENT PATH FOR LUA AND REQUIRE THE LIB --
22-- DO NOT CHANGE THAT PART --
33package.path = package.path .. ";" .. "..\\?.lua"
44-require "svg"
44+require "svg-min"
55-- END OF LIBRARY IMPORT --
6677-- TEST IS STARTING HERE -- YOU CAN MODIFY IT --
8899mon_dessin = svg:create()
1010--- mon_dessin:addText("Hello World !!")
1111--- mon_dessin:addRect()
1212--- mon_dessin:addCircle()
1313--- mon_dessin:addEllipse()
1414--- mon_dessin:addLine()
1515--- mon_dessin:addPolyline()
1616--- mon_dessin:addPath()
1717-print(mon_dessin:innerdraw())1010+mon_dessin:addText("Hello World !!")
1111+mon_dessin:addRect()
1212+mon_dessin:addCircle()
1313+mon_dessin:addEllipse()
1414+mon_dessin:addLine()
1515+mon_dessin:addPolyline()
1616+mon_dessin:addPath()
1717+print(mon_dessin:draw())