From 02351a45b4316efbfca501c19495cd7e1e404810 Mon Sep 17 00:00:00 2001 From: t-bltg Date: Tue, 5 Apr 2022 14:56:47 +0200 Subject: [PATCH] `UnicodePlots`: rework `png` output (#4171) --- .github/workflows/docs.yml | 16 ++++++---------- src/backends/gaston.jl | 5 ++--- src/backends/unicodeplots.jl | 28 ++++++++++++++-------------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index ffca8f0e..93d16b74 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -9,7 +9,7 @@ on: jobs: Build_docs: - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 with: @@ -29,17 +29,14 @@ jobs: - name: Install dependencies run: | sudo apt-get update -y - sudo apt-get install -y qt5-default \ + sudo apt-get install -y \ + texlive-{latex-{base,extra},binaries,pictures,luatex} \ ttf-mscorefonts-installer \ poppler-utils \ - pdf2svg \ - texlive-latex-base \ - texlive-binaries \ - texlive-pictures \ - texlive-latex-extra \ - texlive-luatex \ ghostscript-x \ - libgconf2-4 \ + libgconf-2-4 \ + qt5-default \ + pdf2svg \ gnuplot - name: Install fonts run: | @@ -49,7 +46,6 @@ jobs: url="$repo/releases/download/$ver/JuliaMono-ttf.tar.gz" echo "downloading & extract url=$url" wget -q "$url" -O - | tar -xz -C ~/.fonts - ls ~/.fonts sudo fc-cache -vr fc-list | grep 'JuliaMono' - name: Build documentation diff --git a/src/backends/gaston.jl b/src/backends/gaston.jl index e9d6e30c..a883c00c 100644 --- a/src/backends/gaston.jl +++ b/src/backends/gaston.jl @@ -244,10 +244,9 @@ function gaston_add_series(plt::Plot{GastonBackend}, series::Series) if (lx = length(x)) == 2 && lx != nc x = collect(range(x[1], x[2], length = nc)) end - elseif st == :heatmap - length(x) == size(z, 2) + 1 && (x = @view x[1:(end - 1)]) - length(y) == size(z, 1) + 1 && (y = @view y[1:(end - 1)]) end + length(x) == size(z, 2) + 1 && (x = (x[1:(end - 1)] + x[2:end]) / 2) + length(y) == size(z, 1) + 1 && (y = (y[1:(end - 1)] + y[2:end]) / 2) end if st == :mesh3d x, y, z = mesh3d_triangles(x, y, z, series[:connections]) diff --git a/src/backends/unicodeplots.jl b/src/backends/unicodeplots.jl index 4ce0fa60..7afa3156 100644 --- a/src/backends/unicodeplots.jl +++ b/src/backends/unicodeplots.jl @@ -1,8 +1,5 @@ # https://github.com/JuliaPlots/UnicodePlots.jl -should_warn_on_unsupported(::UnicodePlotsBackend) = false - -# ------------------------------------------------------------------------------------------ const _canvas_map = ( braille = UnicodePlots.BrailleCanvas, density = UnicodePlots.DensityCanvas, @@ -13,11 +10,10 @@ const _canvas_map = ( dot = UnicodePlots.DotCanvas, ) -# do all the magic here... build it all at once, -# since we need to know about all the series at the very beginning -function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend}) - plt.o = UnicodePlots.Plot[] +should_warn_on_unsupported(::UnicodePlotsBackend) = false +function _before_layout_calcs(plt::Plot{UnicodePlotsBackend}) + plt.o = UnicodePlots.Plot[] up_width = UnicodePlots.DEFAULT_WIDTH[] up_height = UnicodePlots.DEFAULT_HEIGHT[] @@ -254,8 +250,7 @@ end # ------------------------------------------------------------------------------------------ -function png(plt::Plot{UnicodePlotsBackend}, fn::AbstractString) - unicodeplots_rebuild(plt) +function _show(io::IO, ::MIME"image/png", plt::Plot{UnicodePlotsBackend}) nr, nc = size(plt.layout) s1 = zeros(Int, nr, nc) s2 = zeros(Int, nr, nc) @@ -292,18 +287,20 @@ function png(plt::Plot{UnicodePlotsBackend}, fn::AbstractString) end n1 += m1[r] end - UnicodePlots.FileIO.save(fn, img) + stream = UnicodePlots.FileIO.Stream{UnicodePlots.FileIO.format"PNG"}(io) + UnicodePlots.FileIO.save(stream, img) end nothing end -# ------------------------------------------------------------------------------------------ Base.show(plt::Plot{UnicodePlotsBackend}) = show(stdout, plt) -Base.show(io::IO, plt::Plot{UnicodePlotsBackend}) = _show(io, MIME("text/plain"), plt) +function Base.show(io::IO, plt::Plot{UnicodePlotsBackend}) + prepare_output(plt) + _show(io, MIME("text/plain"), plt) +end # NOTE: _show(...) must be kept for Base.showable (src/output.jl) function _show(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend}) - unicodeplots_rebuild(plt) nr, nc = size(plt.layout) if nr == 1 && nc == 1 # fast path n = length(plt.o) @@ -367,4 +364,7 @@ function _show(io::IO, ::MIME"text/plain", plt::Plot{UnicodePlotsBackend}) end # we only support MIME"text/plain", hence display(...) falls back to plain-text on stdout -_display(plt::Plot{UnicodePlotsBackend}) = (show(stdout, plt); println(stdout)) +function _display(plt::Plot{UnicodePlotsBackend}) + show(stdout, plt) + println(stdout) +end