diff --git a/Project.toml b/Project.toml index b77aa77..f110e57 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PPTX" uuid = "14a86994-10a4-4a7d-b9ad-ef6f3b1fac6a" authors = ["Xander de Vries", "Matthijs Cox"] -version = "0.6.1" +version = "0.6.2" [deps] DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" diff --git a/assets/julia_logo.svg b/assets/julia_logo.svg new file mode 100644 index 0000000..c608baf --- /dev/null +++ b/assets/julia_logo.svg @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/src/Picture.jl b/src/Picture.jl index c9a6318..f955bdc 100644 --- a/src/Picture.jl +++ b/src/Picture.jl @@ -23,6 +23,20 @@ Picture size_x is 1440000 EMUs size_y is 1475072 EMUs +``` + +Optionally, you can set the `size_x` and `size_y` manually for filetypes not supported by FileIO, such as SVG. +```julia +julia> using PPTX + +julia> img = Picture(joinpath(PPTX.ASSETS_DIR, "julia_logo.svg"); size_x=40, size_y=30) +Picture + source is "./julia_logo.svg" + offset_x is 0 EMUs + offset_y is 0 EMUs + size_x is 1440000 EMUs + size_y is 1080000 EMUs + ``` """ struct Picture <: AbstractShape @@ -34,21 +48,37 @@ struct Picture <: AbstractShape rid::Int end -function Picture(source::String; top::Real=0, left::Real=0, offset_x::Real=left, offset_y::Real=top, size::Real=40, rid::Int=0) - ratio = image_aspect_ratio(source) - size_x = Int(round(size * _EMUS_PER_MM)) - size_y = Int(round(size_x / ratio)) +function Picture( + source::String; + top::Real=0, + left::Real=0, + offset_x::Real=left, + offset_y::Real=top, + size::Real=40, + size_x::Real=size, + size_y::Union{Nothing, Real}=nothing, + rid::Int=0, +) + scaled_size_x = Int(round(size_x * _EMUS_PER_MM)) + if isnothing(size_y) + ratio = image_aspect_ratio(source) + scaled_size_y = Int(round(scaled_size_x / ratio)) + else + scaled_size_y = Int(round(size_y * _EMUS_PER_MM)) + end return Picture( source, Int(round(offset_x * _EMUS_PER_MM)), Int(round(offset_y * _EMUS_PER_MM)), - size_x, - size_y, + scaled_size_x, + scaled_size_y, rid, ) end -set_rid(s::Picture, i::Int) = Picture(s.source, s.offset_x, s.offset_y, s.size_x, s.size_y, i) +function set_rid(s::Picture, i::Int) + return Picture(s.source, s.offset_x, s.offset_y, s.size_x, s.size_y, i) +end rid(s::Picture) = s.rid has_rid(s::Picture) = true @@ -116,8 +146,16 @@ function copy_picture(p::Picture) end function image_aspect_ratio(path::String) - img = load(path) + 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) return width / height end - diff --git a/test/testConstructors.jl b/test/testConstructors.jl index cfb0bbd..81110ac 100644 --- a/test/testConstructors.jl +++ b/test/testConstructors.jl @@ -43,6 +43,17 @@ using Test contains(PPTX._show_string(pic2, false), "source is \"$(pic.source)\"") 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 + end @testset "empty" begin p = Presentation() ps = slides(p)