From 42872b4031094207cfc42e4190b33f9b67dd878f Mon Sep 17 00:00:00 2001 From: Walter Stanish Date: Tue, 27 Sep 2016 17:43:58 +0800 Subject: [PATCH] v1.0.1 --- README.md | 15 +++++++++++++-- svglover.lua | 54 ++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 8dbdfaf..4268e34 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,22 @@ Note that because it is specifically written for LÖVE, it is probably not usefu The latest code can always be found at [Github](https://github.com/globalcitizen/svglover). +## News + + +* 2016-09-27: [v1.0.1](https://github.com/globalcitizen/svglover/releases/tag/v1.0.1) released! + - Add `` support (when circles are not drawn as ``) + - Slightly hardier parsing + - Improved documentation + +* 2016-09-27: [v1.0.0](https://github.com/globalcitizen/svglover/releases/tag/v1.0.0) released! + - Basic functionality + ## Status -The library is currently functional only for polygons, elipses (and circles), and rectangles. All forms of translation, rotation and scaling are supported. Groups are supported. Transparency is supported. Here is an example screenshot. +The library is currently functional only for polygons, elipses (and circles), and rectangles. All forms of translation, rotation and scaling are supported. Groups are supported. Transparency is supported. It does not support gradient fills of any kind, `` ([see issue #1](https://github.com/globalcitizen/svglover/issues/1)) or view boxes. Here is an example screenshot. -![Polygon example](https://raw.githubusercontent.com/globalcitizen/svglover/master/screenshot-polygon.jpg) +![Polygon, rectangle, circle, ellipse example](https://raw.githubusercontent.com/globalcitizen/svglover/master/screenshot-polygon.jpg) ## Usage diff --git a/svglover.lua b/svglover.lua index 87791f1..83006d5 100644 --- a/svglover.lua +++ b/svglover.lua @@ -37,14 +37,16 @@ function svglover_load(svgfile) file_contents = file_contents .. line end end + -- - remove all newlines + file_contents = string.gsub(file_contents,"\n","") -- - insert newline after all tags file_contents = string.gsub(file_contents,">",">\n") -- - flush blank lines file_contents = string.gsub(file_contents,"\n+","\n") -- remove multiple newlines file_contents = string.gsub(file_contents,"\n$","") -- remove trailing newline -- - extract height and width - svg.width = string.match(file_contents,"]+width=\"(%d+)\"") - svg.height = string.match(file_contents,"]+height=\"(%d+)\"") + svg.width = string.match(file_contents,"]+width=\"([0-9.]+)") + svg.height = string.match(file_contents,"]+height=\"([0-9.]+)") -- - finally, loop over lines, appending to svg.drawcommands for line in string.gmatch(file_contents, "[^\n]+") do -- parse it @@ -202,10 +204,11 @@ function __svglover_lineparse(line) result = result .. "love.graphics.rectangle(\"fill\"," .. x_offset .. "," .. y_offset .. "," .. width .. "," .. height .. ")\n" return result - -- ellipse (eg. circle) - elseif string.match(line,' + -- -- lua example: -- love.graphics.setColor( red, green, blue, alpha ) -- love.graphics.ellipse( mode, x, y, radiusx, radiusy, segments ) @@ -217,24 +220,39 @@ function __svglover_lineparse(line) -- cy (center_y) center_y = string.match(line," cy=\"([^\"]+)\"") - -- rx (radius_x) - radius_x = string.match(line," rx=\"([^\"]+)\"") + -- r (radius, for a circle) + radius = string.match(line," r=\"([^\"]+)\"") - -- ry (radius_y) - radius_y = string.match(line," ry=\"([^\"]+)\"") + if radius ~= nil then + radius_x = radius + radius_y = radius + else + -- rx (radius_x, for an ellipse) + radius_x = string.match(line," rx=\"([^\"]+)\"") + + -- ry (radius_y, for an ellipse) + radius_y = string.match(line," ry=\"([^\"]+)\"") + end -- fill (red/green/blue) red, green, blue = string.match(line,"fill=\"#(..)(..)(..)\"") - red = tonumber(red,16) - green = tonumber(green,16) - blue = tonumber(blue,16) + if red ~= nil then + red = tonumber(red,16) + green = tonumber(green,16) + blue = tonumber(blue,16) + end -- fill-opacity (alpha) alpha = string.match(line,"opacity=\"(.-)\"") - alpha = math.floor(255*tonumber(alpha,10)) + if alpha ~= nil then + alpha = math.floor(255*tonumber(alpha,10)) + end -- output - local result = "love.graphics.setColor(" .. red .. "," .. green .. "," .. blue .. "," .. alpha .. ")\n"; + local result = '' + if red ~= nil then + result = result .. "love.graphics.setColor(" .. red .. "," .. green .. "," .. blue .. "," .. alpha .. ")\n"; + end result = result .. "love.graphics.ellipse(\"fill\"," .. center_x .. "," .. center_y .. "," .. radius_x .. "," .. radius_y .. ",50)\n"; return result @@ -267,8 +285,12 @@ function __svglover_lineparse(line) result = result .. "love.graphics.polygon(\"fill\",{" .. vertices .. "})\n"; return result - -- start or end svg - elseif string.match(line,' ]') then -- SVG example: -- --