Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Walter Stanish committed Sep 27, 2016
1 parent cba36ed commit 42872b4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<circle>` support (when circles are not drawn as `<ellipse>`)
- 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, `<path>` ([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

Expand Down
54 changes: 38 additions & 16 deletions svglover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,"<svg [^>]+width=\"(%d+)\"")
svg.height = string.match(file_contents,"<svg [^>]+height=\"(%d+)\"")
svg.width = string.match(file_contents,"<svg [^>]+width=\"([0-9.]+)")
svg.height = string.match(file_contents,"<svg [^>]+height=\"([0-9.]+)")
-- - finally, loop over lines, appending to svg.drawcommands
for line in string.gmatch(file_contents, "[^\n]+") do
-- parse it
Expand Down Expand Up @@ -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,'<ellipse ') then
-- ellipse or circle
elseif string.match(line,'<ellipse ') or string.match(line,'<circle ') then
-- SVG example:
-- <ellipse fill="#ffffff" fill-opacity="0.501961" cx="81" cy="16" rx="255" ry="22" />
-- <circle cx="114.279" cy="10.335" r="10"/>
-- lua example:
-- love.graphics.setColor( red, green, blue, alpha )
-- love.graphics.ellipse( mode, x, y, radiusx, radiusy, segments )
Expand All @@ -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

Expand Down Expand Up @@ -267,16 +285,20 @@ function __svglover_lineparse(line)
result = result .. "love.graphics.polygon(\"fill\",{" .. vertices .. "})\n";
return result

-- start or end svg
elseif string.match(line,'</?svg') then
-- start or end svg etc.
elseif string.match(line,'</?svg') or
string.match(line,'<.xml') or
string.match(line,'<!--') or
string.match(line,'</?title') or
string.match(line,'<!DOCTYPE') then
-- ignore

-- end group
elseif string.match(line,'</g>') then
return 'love.graphics.pop()'

-- start group
elseif string.match(line,'<g ') then
elseif string.match(line,'<g[> ]') then
-- SVG example:
-- <g transform="translate(226 107) rotate(307) scale(3 11)">
-- <g transform="scale(4.000000) translate(0.5 0.5)">
Expand Down

0 comments on commit 42872b4

Please sign in to comment.