-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from davidanthoff/mime-save
Add mime save for eps, pdf, png and svg
- Loading branch information
Showing
9 changed files
with
1,052 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function FileIO.save(file::File{format"PNG"}, data) | ||
if mimewritable("image/png", data) | ||
open(file.filename, "w") do s | ||
show(IOContext(s, :full_fidelity=>true), "image/png", data) | ||
end | ||
else | ||
throw(ArgumentError("Argument does not support conversion to png.")) | ||
end | ||
end | ||
|
||
function FileIO.save(file::File{format"SVG"}, data) | ||
if mimewritable("image/svg+xml", data) | ||
open(file.filename, "w") do s | ||
show(IOContext(s, :full_fidelity=>true), "image/svg+xml", data) | ||
end | ||
else | ||
throw(ArgumentError("Argument does not support conversion to svg.")) | ||
end | ||
end | ||
|
||
function FileIO.save(file::File{format"PDF"}, data) | ||
if mimewritable("application/pdf", data) | ||
open(file.filename, "w") do s | ||
show(IOContext(s, :full_fidelity=>true), "application/pdf", data) | ||
end | ||
else | ||
throw(ArgumentError("Argument does not support conversion to pdf.")) | ||
end | ||
end | ||
|
||
function FileIO.save(file::File{format"EPS"}, data) | ||
if mimewritable("application/eps", data) | ||
open(file.filename, "w") do s | ||
show(IOContext(s, :full_fidelity=>true), "application/eps", data) | ||
end | ||
else | ||
throw(ArgumentError("Argument does not support conversion to eps.")) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
using FileIO | ||
using Base.Test | ||
|
||
immutable MimeSaveTestType | ||
end | ||
|
||
@testset "FileIO" begin | ||
include("query.jl") | ||
include("loadsave.jl") | ||
include("error_handling.jl") | ||
include("test_mimesave.jl") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using FileIO | ||
using Base.Test | ||
|
||
@testset "Mime save" begin | ||
|
||
function Base.show(io::IO, m::MIME"image/svg+xml", data::MimeSaveTestType) | ||
content = read(joinpath(@__DIR__, "files", "mimesavetest.svg")) | ||
write(io, content) | ||
end | ||
|
||
function Base.show(io::IO, m::MIME"application/pdf", data::MimeSaveTestType) | ||
content = read(joinpath(@__DIR__, "files", "mimesavetest.pdf")) | ||
write(io, content) | ||
end | ||
|
||
function Base.show(io::IO, m::MIME"application/eps", data::MimeSaveTestType) | ||
content = read(joinpath(@__DIR__, "files", "mimesavetest.eps")) | ||
write(io, content) | ||
end | ||
|
||
function Base.show(io::IO, m::MIME"image/png", data::MimeSaveTestType) | ||
content = read(joinpath(@__DIR__, "files", "mimesavetest.png")) | ||
write(io, content) | ||
end | ||
|
||
data = MimeSaveTestType() | ||
|
||
output_filename = tempname() | ||
|
||
for filetype in [".svg", ".pdf", ".eps", ".png"] | ||
|
||
try | ||
save(output_filename * filetype, data) | ||
|
||
content_original = read(joinpath(@__DIR__, "files", "mimesavetest$filetype")) | ||
content_new = read(output_filename * filetype) | ||
|
||
@test content_new == content_original | ||
finally | ||
rm(output_filename * filetype) | ||
end | ||
|
||
end | ||
|
||
end |