Skip to content

Commit

Permalink
Merge pull request #37 from ASML-Labs/svg-support
Browse files Browse the repository at this point in the history
Feat: manual image x and y size to support svg
  • Loading branch information
matthijscox-asml authored Aug 15, 2023
2 parents c10c448 + e09832a commit 0ebf88b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
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

2 comments on commit 0ebf88b

@matthijscox-asml
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

Option to set the Picture x and y size, to avoid picture loading in the automatic ratio calculation. This enables use of files not supported by FileIO, such as SVG.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/89684

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.2 -m "<description of version>" 0ebf88bc410f5a5d2fb5a8c60a2838f4f012f4ea
git push origin v0.6.2

Please sign in to comment.