Merge pull request #2612 from daschw/savefig

fix savefig extensions
This commit is contained in:
Daniel Schwabeneder 2020-04-23 00:01:58 +02:00 committed by GitHub
commit 08c0f6d9a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 91 deletions

View File

@ -1,7 +1,7 @@
name = "Plots" name = "Plots"
uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
author = ["Tom Breloff (@tbreloff)"] author = ["Tom Breloff (@tbreloff)"]
version = "1.0.13" version = "1.0.14"
[deps] [deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

View File

@ -1,5 +1,4 @@
defaultOutputFormat(plt::Plot) = "png" defaultOutputFormat(plt::Plot) = "png"
function png(plt::Plot, fn::AbstractString) function png(plt::Plot, fn::AbstractString)
@ -18,7 +17,6 @@ function svg(plt::Plot, fn::AbstractString)
end end
svg(fn::AbstractString) = svg(current(), fn) svg(fn::AbstractString) = svg(current(), fn)
function pdf(plt::Plot, fn::AbstractString) function pdf(plt::Plot, fn::AbstractString)
fn = addExtension(fn, "pdf") fn = addExtension(fn, "pdf")
io = open(fn, "w") io = open(fn, "w")
@ -27,7 +25,6 @@ function pdf(plt::Plot, fn::AbstractString)
end end
pdf(fn::AbstractString) = pdf(current(), fn) pdf(fn::AbstractString) = pdf(current(), fn)
function ps(plt::Plot, fn::AbstractString) function ps(plt::Plot, fn::AbstractString)
fn = addExtension(fn, "ps") fn = addExtension(fn, "ps")
io = open(fn, "w") io = open(fn, "w")
@ -76,8 +73,8 @@ function txt(plt::Plot, fn::AbstractString)
end end
txt(fn::AbstractString) = txt(current(), fn) txt(fn::AbstractString) = txt(current(), fn)
# ----------------------------------------------------------------
# ----------------------------------------------------------------
const _savemap = Dict( const _savemap = Dict(
"png" => png, "png" => png,
@ -92,24 +89,15 @@ const _savemap = Dict(
"txt" => txt, "txt" => txt,
) )
function getExtension(fn::AbstractString) const _extension_map = Dict("tikz" => "tex")
pieces = split(fn, ".")
length(pieces) > 1 || error("Can't extract file extension: ", fn)
ext = pieces[end]
haskey(_savemap, ext) || error("Invalid file extension: ", fn)
ext
end
function addExtension(fn::AbstractString, ext::AbstractString) function addExtension(fn::AbstractString, ext::AbstractString)
try oldfn, oldext = splitext(fn)
oldext = getExtension(fn) oldext = chop(oldext, head = 1, tail = 0)
if string(_savemap[oldext]) == ext if get(_extension_map, oldext, oldext) == ext
return fn return fn
else else
return "$fn.$ext" return string(fn, ".", ext)
end
catch
return "$fn.$ext"
end end
end end
@ -122,26 +110,27 @@ file types, some also support svg, ps, eps, html and tex.
""" """
function savefig(plt::Plot, fn::AbstractString) function savefig(plt::Plot, fn::AbstractString)
fn = abspath(expanduser(fn)) fn = abspath(expanduser(fn))
# get the extension # get the extension
local ext fn, ext = splitext(fn)
try ext = chop(ext, head = 1, tail = 0)
ext = getExtension(fn) if isempty(ext)
catch
# if we couldn't extract the extension, add the default
ext = defaultOutputFormat(plt) ext = defaultOutputFormat(plt)
fn = addExtension(fn, ext)
end end
# save it # save it
func = get(_savemap, ext) do if haskey(_savemap, ext)
error("Unsupported extension $ext with filename ", fn) func = _savemap[ext]
return func(plt, fn)
else
error("Invalid file extension: ", fn)
end end
func(plt, fn)
end end
savefig(fn::AbstractString) = savefig(current(), fn) savefig(fn::AbstractString) = savefig(current(), fn)
# --------------------------------------------------------- # ---------------------------------------------------------
""" """
gui([plot]) gui([plot])
@ -169,12 +158,8 @@ end
# --------------------------------------------------------- # ---------------------------------------------------------
const _best_html_output_type = KW( const _best_html_output_type =
:pyplot => :png, KW(:pyplot => :png, :unicodeplots => :txt, :plotlyjs => :html, :plotly => :html)
:unicodeplots => :txt,
:plotlyjs => :html,
:plotly => :html
)
# a backup for html... passes to svg or png depending on the html_output_format arg # a backup for html... passes to svg or png depending on the html_output_format arg
function _show(io::IO, ::MIME"text/html", plt::Plot) function _show(io::IO, ::MIME"text/html", plt::Plot)
@ -184,7 +169,12 @@ function _show(io::IO, ::MIME"text/html", plt::Plot)
end end
if output_type == :png if output_type == :png
# @info("writing png to html output") # @info("writing png to html output")
print(io, "<img src=\"data:image/png;base64,", base64encode(show, MIME("image/png"), plt), "\" />") print(
io,
"<img src=\"data:image/png;base64,",
base64encode(show, MIME("image/png"), plt),
"\" />",
)
elseif output_type == :svg elseif output_type == :svg
# @info("writing svg to html output") # @info("writing svg to html output")
show(io, MIME("image/svg+xml"), plt) show(io, MIME("image/svg+xml"), plt)
@ -205,9 +195,18 @@ function _display(plt::Plot)
end end
# for writing to io streams... first prepare, then callback # for writing to io streams... first prepare, then callback
for mime in ("text/plain", "text/html", "image/png", "image/eps", "image/svg+xml", for mime in (
"application/eps", "application/pdf", "application/postscript", "text/plain",
"application/x-tex", "application/vnd.plotly.v1+json") "text/html",
"image/png",
"image/eps",
"image/svg+xml",
"application/eps",
"application/pdf",
"application/postscript",
"application/x-tex",
"application/vnd.plotly.v1+json",
)
@eval function Base.show(io::IO, m::MIME{Symbol($mime)}, plt::Plot) @eval function Base.show(io::IO, m::MIME{Symbol($mime)}, plt::Plot)
if haskey(io, :juno_plotsize) if haskey(io, :juno_plotsize)
showjuno(io, m, plt) showjuno(io, m, plt)
@ -219,7 +218,8 @@ for mime in ("text/plain", "text/html", "image/png", "image/eps", "image/svg+xml
end end
end end
Base.show(io::IO, m::MIME"application/prs.juno.plotpane+html", plt::Plot) = showjuno(io, MIME("text/html"), plt) Base.show(io::IO, m::MIME"application/prs.juno.plotpane+html", plt::Plot) =
showjuno(io, MIME("text/html"), plt)
# default text/plain for all backends # default text/plain for all backends
_show(io::IO, ::MIME{Symbol("text/plain")}, plt::Plot) = show(io, plt) _show(io::IO, ::MIME{Symbol("text/plain")}, plt::Plot) = show(io, plt)