Skip to content

Commit

Permalink
Merge pull request #38 from jaakkor2/svg_aspect_ratio
Browse files Browse the repository at this point in the history
Support SVG aspect ratio
  • Loading branch information
matthijscox-asml authored Aug 16, 2023
2 parents 0ebf88b + 0530efb commit 3ef260b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
27 changes: 18 additions & 9 deletions src/Picture.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,25 @@ function copy_picture(p::Picture)
end

function image_aspect_ratio(path::String)
local img
try
img = load(path)
catch e
if e isa ErrorException && contains(e.msg, "No applicable_loaders found")
error("Cannot load image to determine aspect ratio, consider setting `size_x` and `size_y` manually.")
else
rethrow(e)
if endswith(path, ".svg")
doc = readxml(path)
r = root(doc)
m = match(r"(?<height>\d*)", r["height"])
height = isnothing(m) ? 1 : parse(Float64, m[:height])
m = match(r"(?<width>\d*)", r["width"])
width = isnothing(m) ? 1 : parse(Float64, m[:width])
else
local img
try
img = load(path)
catch e
if e isa ErrorException && contains(e.msg, "No applicable_loaders found")
error("Cannot load image to determine aspect ratio, consider setting `size_x` and `size_y` manually.")
else
rethrow(e)
end
end
height, width = size(img)
end
height, width = size(img)
return width / height
end
54 changes: 26 additions & 28 deletions test/testConstructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,34 @@ using Test
end
@testset "Picture" begin
@test_throws ArgumentError pic = Picture("path")
logo_path = joinpath(PPTX.ASSETS_DIR,"julia_logo.png")
pic = Picture(logo_path)
@test pic.offset_x == 0
width = 150
pic = Picture(logo_path, top = 100, left = 120, size=width)
@test pic.offset_x == Int(round(120*PPTX._EMUS_PER_MM))
@test pic.offset_y == Int(round(100*PPTX._EMUS_PER_MM))
@test pic.size_x == Int(round(width*PPTX._EMUS_PER_MM))
ratio = PPTX.image_aspect_ratio(logo_path)
height = Int(round(width / ratio*PPTX._EMUS_PER_MM))
@test pic.size_y == height

# make setting the rid doesn't change the other values
pic2 = PPTX.set_rid(pic, 5)
@test pic2.rid == 5
@test pic2.source == pic.source
@test pic2.offset_x == pic.offset_x
@test pic2.offset_y == pic.offset_y
@test pic2.size_x == pic.size_x
@test pic2.size_y == pic.size_y

contains(PPTX._show_string(pic2, false), "source is \"$(pic.source)\"")
fnames = ["julia_logo.png", "julia_logo.svg"]
for fname in fnames
logo_path = joinpath(PPTX.ASSETS_DIR, fname)
pic = Picture(logo_path)
@test pic.offset_x == 0
width = 150
pic = Picture(logo_path, top = 100, left = 120, size=width)
@test pic.offset_x == Int(round(120*PPTX._EMUS_PER_MM))
@test pic.offset_y == Int(round(100*PPTX._EMUS_PER_MM))
@test pic.size_x == Int(round(width*PPTX._EMUS_PER_MM))
ratio = PPTX.image_aspect_ratio(logo_path)
height = Int(round(width / ratio*PPTX._EMUS_PER_MM))
@test pic.size_y == height

# make setting the rid doesn't change the other values
pic2 = PPTX.set_rid(pic, 5)
@test pic2.rid == 5
@test pic2.source == pic.source
@test pic2.offset_x == pic.offset_x
@test pic2.offset_y == pic.offset_y
@test pic2.size_x == pic.size_x
@test pic2.size_y == pic.size_y

contains(PPTX._show_string(pic2, false), "source is \"$(pic.source)\"")
end
end
@testset "Picture - custom aspect ratio" begin
# SVG is not supported by FileIO.jl
# this means we need to manually set the aspect ratio
logo_path = joinpath(PPTX.ASSETS_DIR,"julia_logo.svg")
msg = "Cannot load image to determine aspect ratio, consider setting `size_x` and `size_y` manually."
@test_throws ErrorException(msg) pic = Picture(logo_path)

pic = Picture(logo_path; size_x=40, size_y=30)
@test pic.size_x == 1440000
@test pic.size_y == 1080000
Expand Down Expand Up @@ -101,4 +99,4 @@ end
push!(p, Slide())
@test sprint(show, p) == "Presentation with 2 slides"
end
end
end

0 comments on commit 3ef260b

Please sign in to comment.