diff --git a/svg.lua b/svg.lua new file mode 100644 index 0000000..6a00720 --- /dev/null +++ b/svg.lua @@ -0,0 +1,81 @@ +-- INIT SVG Object +svg = {} +svg.__index = svg; + +-- function to create a SVG drawing +-- width: number => width of the drawing +-- height: number => height of the drawing +-- stroke: string => lines color +-- fill: string => fill color +function svg:create(width, heigth, stroke, fill) + local s = {} + setmetatable(s,svg) + s.width = width or 100 + s.height = height or 100 + s.stroke = stroke or "#000000" + s.fill = fill or "transparent" + s.elements = {} + + + -- INIT svg.Element Object + s.Element = {} + s.Element.__index = svg.Element + + -- function to create svg.Element + -- name: string => name of the element (standard svg element name) + -- options: table => a table containing all the parameters of the element + function s.Element:create(name, options) + local e = {} + setmetatable(e,s.Element) + e.name = name + e.strElement = "<" .. name .. " " + for key, value in pairs(options) do + e[key] = value + e.strElement = e.strElement .. key .. "=\"" .. value .. "\" " + end + e.strElement = e.strElement .. "/>" + return e; + end + + return s +end + +-- function to add an element to the svg drawing +-- element : svg.Element => the element to add to the drawing +function svg:add(element) + table.insert(self.elements, element) +end + +-- function to get the string formatted for the full svg drawing +function svg:draw() + local svgStr = "" + for k,v in pairs(self.elements) do + svgStr = svgStr .. v.strElement + end + svgStr = svgStr .. "" + return svgStr +end + +-- INIT Rect element Class +-- x: number => Position du rectangle sur l'axe horizontal par rapport au coin supérieur gauche +-- y: number => Position du rectangle sur l'axe vertical par rapport au coin supérieur gauche +-- width: number => Largeur du rectangle +-- height: number => Hauteur du rectangle. +-- stroke: string => lines color +-- strokeWidth: string => lines width +-- fill: string => fill color +-- rx: number => Rayon x des coins du rectangle +-- ry: number => Rayon y des coins du rectangle +function svg:Rect(x, y, width, height, stroke, strokeWidth, fill, rx, ry) + return self.Element:create("rect", { + x = x or 10, + y = y or 10, + width = width or 10, + height = height or 10, + stroke = stroke or svg.stroke, + fill = fill or svg.fill, + ["stroke-width"] = strokeWidth or 1, + rx = rx or 0, + ry = ry or 0 + }) +end \ No newline at end of file