From 77b59510373b80cb0c990fa34b4291eac053a07a Mon Sep 17 00:00:00 2001 From: Daniel Schwabeneder Date: Wed, 22 Apr 2020 19:00:20 +0200 Subject: [PATCH 1/5] fix savefig for .tex extension --- src/output.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/output.jl b/src/output.jl index 003deec4..96f65d3d 100644 --- a/src/output.jl +++ b/src/output.jl @@ -88,6 +88,7 @@ const _savemap = Dict( "tex" => tex, "json" => json, "html" => html, + "tex" => tex, "tikz" => tex, "txt" => txt, ) From 4e877a206e67132e420c496247f578dcc38b9dee Mon Sep 17 00:00:00 2001 From: Daniel Schwabeneder Date: Wed, 22 Apr 2020 19:28:12 +0200 Subject: [PATCH 2/5] fix double extensions in savefig --- src/output.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/output.jl b/src/output.jl index 96f65d3d..e99ce535 100644 --- a/src/output.jl +++ b/src/output.jl @@ -93,6 +93,8 @@ const _savemap = Dict( "txt" => txt, ) +const _extension_map = Dict("tikz" => "tex") + function getExtension(fn::AbstractString) pieces = split(fn, ".") length(pieces) > 1 || error("Can't extract file extension: ", fn) @@ -104,7 +106,7 @@ end function addExtension(fn::AbstractString, ext::AbstractString) try oldext = getExtension(fn) - if string(_savemap[oldext]) == ext + if get(_extesion_map, oldext, oldext) == ext return fn else return "$fn.$ext" From 8b5fc954436e87c08b3fdc57b548f9292a47c20a Mon Sep 17 00:00:00 2001 From: Daniel Schwabeneder Date: Wed, 22 Apr 2020 19:30:02 +0200 Subject: [PATCH 3/5] bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index fbef8bca..84266b53 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Plots" uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" author = ["Tom Breloff (@tbreloff)"] -version = "1.0.13" +version = "1.0.14" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" From df6fa6fd68498dee937ff34d9ab4fa75d9f7c910 Mon Sep 17 00:00:00 2001 From: Daniel Schwabeneder Date: Wed, 22 Apr 2020 23:37:31 +0200 Subject: [PATCH 4/5] fix double extensions in savefig --- src/output.jl | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/output.jl b/src/output.jl index e99ce535..6ac58ebf 100644 --- a/src/output.jl +++ b/src/output.jl @@ -88,7 +88,6 @@ const _savemap = Dict( "tex" => tex, "json" => json, "html" => html, - "tex" => tex, "tikz" => tex, "txt" => txt, ) @@ -104,15 +103,12 @@ function getExtension(fn::AbstractString) end function addExtension(fn::AbstractString, ext::AbstractString) - try - oldext = getExtension(fn) - if get(_extesion_map, oldext, oldext) == ext - return fn - else - return "$fn.$ext" - end - catch - return "$fn.$ext" + oldfn, oldext = splitext(fn) + oldext = chop(oldext, head = 1, tail = 0) + if get(_extension_map, oldext, oldext) == ext + return fn + else + return string(fn, ".", ext) end end @@ -125,21 +121,21 @@ file types, some also support svg, ps, eps, html and tex. """ function savefig(plt::Plot, fn::AbstractString) fn = abspath(expanduser(fn)) + # get the extension - local ext - try - ext = getExtension(fn) - catch - # if we couldn't extract the extension, add the default + fn, ext = splitext(fn) + ext = chop(ext, head = 1, tail = 0) + if isempty(ext) ext = defaultOutputFormat(plt) - fn = addExtension(fn, ext) end # save it - func = get(_savemap, ext) do - error("Unsupported extension $ext with filename ", fn) + if haskey(_savemap, ext) + func = _savemap[ext] + return func(plt, fn) + else + error("Invalid file extension: ", fn) end - func(plt, fn) end savefig(fn::AbstractString) = savefig(current(), fn) From fafb0d9510bb99c4e8543239455624b0d726eb0e Mon Sep 17 00:00:00 2001 From: Daniel Schwabeneder Date: Wed, 22 Apr 2020 23:43:38 +0200 Subject: [PATCH 5/5] format code to tab length of 4 --- src/output.jl | 179 +++++++++++++++++++++++++------------------------- 1 file changed, 90 insertions(+), 89 deletions(-) diff --git a/src/output.jl b/src/output.jl index 6ac58ebf..1501c214 100644 --- a/src/output.jl +++ b/src/output.jl @@ -1,54 +1,51 @@ - defaultOutputFormat(plt::Plot) = "png" function png(plt::Plot, fn::AbstractString) - fn = addExtension(fn, "png") - io = open(fn, "w") - show(io, MIME("image/png"), plt) - close(io) + fn = addExtension(fn, "png") + io = open(fn, "w") + show(io, MIME("image/png"), plt) + close(io) end png(fn::AbstractString) = png(current(), fn) function svg(plt::Plot, fn::AbstractString) - fn = addExtension(fn, "svg") - io = open(fn, "w") - show(io, MIME("image/svg+xml"), plt) - close(io) + fn = addExtension(fn, "svg") + io = open(fn, "w") + show(io, MIME("image/svg+xml"), plt) + close(io) end svg(fn::AbstractString) = svg(current(), fn) - function pdf(plt::Plot, fn::AbstractString) - fn = addExtension(fn, "pdf") - io = open(fn, "w") - show(io, MIME("application/pdf"), plt) - close(io) + fn = addExtension(fn, "pdf") + io = open(fn, "w") + show(io, MIME("application/pdf"), plt) + close(io) end pdf(fn::AbstractString) = pdf(current(), fn) - function ps(plt::Plot, fn::AbstractString) - fn = addExtension(fn, "ps") - io = open(fn, "w") - show(io, MIME("application/postscript"), plt) - close(io) + fn = addExtension(fn, "ps") + io = open(fn, "w") + show(io, MIME("application/postscript"), plt) + close(io) end ps(fn::AbstractString) = ps(current(), fn) function eps(plt::Plot, fn::AbstractString) - fn = addExtension(fn, "eps") - io = open(fn, "w") - show(io, MIME("image/eps"), plt) - close(io) + fn = addExtension(fn, "eps") + io = open(fn, "w") + show(io, MIME("image/eps"), plt) + close(io) end eps(fn::AbstractString) = eps(current(), fn) function tex(plt::Plot, fn::AbstractString) - fn = addExtension(fn, "tex") - io = open(fn, "w") - show(io, MIME("application/x-tex"), plt) - close(io) + fn = addExtension(fn, "tex") + io = open(fn, "w") + show(io, MIME("application/x-tex"), plt) + close(io) end tex(fn::AbstractString) = tex(current(), fn) @@ -69,47 +66,39 @@ end html(fn::AbstractString) = html(current(), fn) function txt(plt::Plot, fn::AbstractString) - fn = addExtension(fn, "txt") - io = open(fn, "w") - show(io, MIME("text/plain"), plt) - close(io) + fn = addExtension(fn, "txt") + io = open(fn, "w") + show(io, MIME("text/plain"), plt) + close(io) end txt(fn::AbstractString) = txt(current(), fn) -# ---------------------------------------------------------------- +# ---------------------------------------------------------------- const _savemap = Dict( "png" => png, "svg" => svg, "pdf" => pdf, - "ps" => ps, + "ps" => ps, "eps" => eps, "tex" => tex, "json" => json, "html" => html, "tikz" => tex, "txt" => txt, - ) +) const _extension_map = Dict("tikz" => "tex") -function getExtension(fn::AbstractString) - 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) - oldfn, oldext = splitext(fn) - oldext = chop(oldext, head = 1, tail = 0) - if get(_extension_map, oldext, oldext) == ext - return fn - else - return string(fn, ".", ext) - end + oldfn, oldext = splitext(fn) + oldext = chop(oldext, head = 1, tail = 0) + if get(_extension_map, oldext, oldext) == ext + return fn + else + return string(fn, ".", ext) + end end """ @@ -120,27 +109,28 @@ type is inferred from the file extension. All backends support png and pdf file types, some also support svg, ps, eps, html and tex. """ function savefig(plt::Plot, fn::AbstractString) - fn = abspath(expanduser(fn)) + fn = abspath(expanduser(fn)) - # get the extension - fn, ext = splitext(fn) - ext = chop(ext, head = 1, tail = 0) - if isempty(ext) - ext = defaultOutputFormat(plt) - end + # get the extension + fn, ext = splitext(fn) + ext = chop(ext, head = 1, tail = 0) + if isempty(ext) + ext = defaultOutputFormat(plt) + end - # save it - if haskey(_savemap, ext) - func = _savemap[ext] - return func(plt, fn) - else - error("Invalid file extension: ", fn) - end + # save it + if haskey(_savemap, ext) + func = _savemap[ext] + return func(plt, fn) + else + error("Invalid file extension: ", fn) + end end savefig(fn::AbstractString) = savefig(current(), fn) # --------------------------------------------------------- + """ gui([plot]) @@ -163,17 +153,13 @@ end _do_plot_show(plt, showval::Bool) = showval && gui(plt) function _do_plot_show(plt, showval::Symbol) showval == :gui && gui(plt) - showval in (:inline,:ijulia) && inline(plt) + showval in (:inline, :ijulia) && inline(plt) end # --------------------------------------------------------- -const _best_html_output_type = KW( - :pyplot => :png, - :unicodeplots => :txt, - :plotlyjs => :html, - :plotly => :html -) +const _best_html_output_type = + KW(:pyplot => :png, :unicodeplots => :txt, :plotlyjs => :html, :plotly => :html) # 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) @@ -183,7 +169,12 @@ function _show(io::IO, ::MIME"text/html", plt::Plot) end if output_type == :png # @info("writing png to html output") - print(io, "") + print( + io, + "", + ) elseif output_type == :svg # @info("writing svg to html output") show(io, MIME("image/svg+xml"), plt) @@ -195,7 +186,7 @@ function _show(io::IO, ::MIME"text/html", plt::Plot) end # delegate showable to _show instead -function Base.showable(m::M, plt::P) where {M<:MIME, P<:Plot} +function Base.showable(m::M, plt::P) where {M <: MIME, P <: Plot} return hasmethod(_show, Tuple{IO, M, P}) end @@ -204,21 +195,31 @@ function _display(plt::Plot) end # for writing to io streams... first prepare, then callback -for mime in ("text/plain", "text/html", "image/png", "image/eps", "image/svg+xml", - "application/eps", "application/pdf", "application/postscript", - "application/x-tex", "application/vnd.plotly.v1+json") +for mime in ( + "text/plain", + "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) if haskey(io, :juno_plotsize) - showjuno(io, m, plt) + showjuno(io, m, plt) else - prepare_output(plt) - _show(io, m, plt) + prepare_output(plt) + _show(io, m, plt) end return nothing 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 _show(io::IO, ::MIME{Symbol("text/plain")}, plt::Plot) = show(io, plt) @@ -257,25 +258,25 @@ function showjuno(io::IO, m, plt) scale = minimum(jsize[i] / sz[i] for i in 1:2) plt[:size] = [s * scale for s in sz] - plt[:dpi] = jratio*Plots.DPI + plt[:dpi] = jratio * Plots.DPI plt[:thickness_scaling] *= scale prepare_output(plt) try - _showjuno(io, m, plt) + _showjuno(io, m, plt) finally - plt[:size] = sz - plt[:dpi] = dpi - plt[:thickness_scaling] = thickness_scaling + plt[:size] = sz + plt[:dpi] = dpi + plt[:thickness_scaling] = thickness_scaling end end function _showjuno(io::IO, m::MIME"image/svg+xml", plt) - if Symbol(plt.attr[:html_output_format]) ≠ :svg - throw(MethodError(show, (typeof(m), typeof(plt)))) - else - _show(io, m, plt) - end + if Symbol(plt.attr[:html_output_format]) ≠ :svg + throw(MethodError(show, (typeof(m), typeof(plt)))) + else + _show(io, m, plt) + end end Base.showable(::MIME"application/prs.juno.plotpane+html", plt::Plot) = false