Skip to content

Commit

Permalink
Merge pull request #39 from jaakkor2/emf_wmf_aspect_ratios
Browse files Browse the repository at this point in the history
Aspect ratio for WMF and EMF files
  • Loading branch information
matthijscox-asml authored Aug 17, 2023
2 parents 3ef260b + 4e78936 commit d1c0e33
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
Binary file added assets/julia_dots.wmf
Binary file not shown.
Binary file added assets/julia_logo.emf
Binary file not shown.
49 changes: 37 additions & 12 deletions src/Picture.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,50 @@ function copy_picture(p::Picture)
end

function image_aspect_ratio(path::String)
if endswith(path, ".svg")
if endswith(lowercase(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
return width / height
end
if endswith(lowercase(path), ".wmf")
# https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-WMF/%5bMS-WMF%5d.pdf
f = read(path)
# 2.3.2.3 META_PLACEABLE Record
key = reinterpret(UInt32, f[1:4])[1]
iswmf = key == 0x9ac6cdd7
# 2.2.2.18 Rect Object
left, top, right, bottom = reinterpret(Int16, f[7:14])
width = right - left
height = bottom - top
iswmf && return width / height
end
if endswith(lowercase(path), ".emf")
# https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-EMF/%5bMS-EMF%5d.pdf
f = read(path)
# 2.3.4.2 EMR_HEADER Record Types
type = reinterpret(UInt32, f[1:4])[1]
isemf = type == 0x00000001
# 2.2.9 Header Object
left, top, right, bottom = reinterpret(Int32, f[9:24]) # MS-WMF 2.2.2.19
width = right - left
height = bottom - top
isemf && return width / height
end

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
height, width = size(img)
end
height, width = size(img)
return width / height
end
2 changes: 1 addition & 1 deletion test/testConstructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using Test
end
@testset "Picture" begin
@test_throws ArgumentError pic = Picture("path")
fnames = ["julia_logo.png", "julia_logo.svg"]
fnames = ["julia_logo.png", "julia_logo.svg", "julia_logo.emf", "julia_dots.wmf"]
for fname in fnames
logo_path = joinpath(PPTX.ASSETS_DIR, fname)
pic = Picture(logo_path)
Expand Down

0 comments on commit d1c0e33

Please sign in to comment.