Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: manual image x and y size to support svg #37

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
10 changes: 10 additions & 0 deletions assets/julia_logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 47 additions & 9 deletions src/Picture.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

11 changes: 11 additions & 0 deletions test/testConstructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading