Skip to content

Commit

Permalink
v0.0.3: Added zoom mode and compressed SVG loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Walter Stanish committed Oct 2, 2016
1 parent e5ec99b commit 1373bcd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ The latest code can always be found at [Github](https://github.com/globalcitizen

## News

* 2016-10-02: [v1.0.3](https://github.com/globalcitizen/svglover/releases/tag/v1.0.3) released!
- Allow automatic / transparent loading of gzip compressed SVG files
- Add zoom feature

* 2016-10-02: [v1.0.2](https://github.com/globalcitizen/svglover/releases/tag/v1.0.2) released!
- Fix Lua logic error that was stopping fill-related scaling from working properly

Expand Down Expand Up @@ -45,10 +49,10 @@ vector_image = svglover_load('some.svg')
You then specify where you want them displayed using:

```
svglover_display(vector_image,topleft_x,topleft_y,width,height,completely_fill_region,border_color,border_width)
svglover_display(vector_image,topleft_x,topleft_y,width,height,completely_fill_region,border_color,border_width,zoom)
```

... where `completely_fill_region`, `border_color` and `border_width` are optional.
... where `completely_fill_region`, `border_color`, `border_width` and `zoom` are optional.

Finally, you should add the `svglover_draw()` call to the end of your `love.draw()` function.

Expand All @@ -57,7 +61,7 @@ A complete example:
```
function love.load()
vector_image = svglover_load('some.svg')
svglover_display(vector_image,100,100,100,100,true,{255,0,0,255},1)
svglover_display(vector_image,100,100,100,100,true,{255,0,0,255},1,1)
end
function love.draw()
Expand Down
38 changes: 28 additions & 10 deletions svglover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ svglover_onscreen_svgs = {}
function svglover_load(svgfile)
-- validate input
-- file exists?
local fh = io.open(svgfile, "r")
local fh = io.open(svgfile, "rb")
if not fh then
print("FATAL: file does not exist: '" .. svgfile .. "'")
os.exit()
Expand All @@ -31,12 +31,12 @@ function svglover_load(svgfile)

-- process input
-- - first we read the whole file in to a string
local file_contents=''
for line in love.filesystem.lines(svgfile) do
if not (line==nil) then
file_contents = file_contents .. line
end
end
file_contents, _ = love.filesystem.read(svgfile)
-- - decompress if appropriate
magic = love.filesystem.read(svgfile,2)
if hex_dump(magic) == '1f 8b' then
file_contents = love.math.decompress(file_contents,'zlib')
end
-- - remove all newlines
file_contents = string.gsub(file_contents,"\n","")
-- - insert newline after all tags
Expand All @@ -63,7 +63,7 @@ function svglover_load(svgfile)
end

-- place a loaded svg in a given screen region
function svglover_display(svg,x,y,region_width,region_height,leave_no_edges,border_color,border_width)
function svglover_display(svg,x,y,region_width,region_height,leave_no_edges,border_color,border_width,zoom)
-- handle arguments
region_width = region_width or math.min(love.graphics.getWidth-x,svg.width)
region_height = region_height or math.min(love.graphics.getHeight-y,svg.height)
Expand All @@ -72,6 +72,7 @@ function svglover_display(svg,x,y,region_width,region_height,leave_no_edges,bord
end
border_color = border_color or nil
border_width = border_width or 1
zoom = zoom or 1
-- validate arguments
if svg.width == nil or svg.height == nil or svg.drawcommands == nil then
print("FATAL: passed invalid svg object")
Expand All @@ -95,6 +96,9 @@ function svglover_display(svg,x,y,region_width,region_height,leave_no_edges,bord
elseif border_width < 1 or border_width > 10000 then
print("FATAL: passed invalid border_width")
os.exit()
elseif zoom <= 0 or zoom > 10000 then
print("FATAL: passed invalid zoom")
os.exit()
end

-- calculate drawing parameters
Expand All @@ -112,13 +116,16 @@ function svglover_display(svg,x,y,region_width,region_height,leave_no_edges,bord
scale_factor = math.min(scale_factor_x,scale_factor_y)
end

-- apply zoom
scale_factor = scale_factor * zoom

-- - centering offsets
local centering_offset_x = 0
local centering_offset_y = 0
if scale_factor * svg.width > region_width then
centering_offset_x = -math.floor(((scale_factor*svg.width)-region_width)*0.5)
centering_offset_x = -math.floor(((scale_factor*svg.width)-region_width*zoom)*0.5)
elseif scale_factor * svg.height > region_height then
centering_offset_y = -math.floor(((scale_factor*svg.height)-region_height)*0.5)
centering_offset_y = -math.floor(((scale_factor*svg.height)-region_height*zoom)*0.5)
end

-- remember the determined properties
Expand Down Expand Up @@ -372,3 +379,14 @@ function __svglover_dc(orig)
end
return copy
end

-- simple hex dump
function hex_dump (str)
local len = string.len( str )
local hex = ""
for i = 1, len do
local ord = string.byte( str, i )
hex = hex .. string.format( "%02x ", ord )
end
return string.gsub(hex,' $','')
end

0 comments on commit 1373bcd

Please sign in to comment.