UnicodePlots: rework png output (#4171)

This commit is contained in:
t-bltg 2022-04-05 14:56:47 +02:00 committed by GitHub
parent e691a42b90
commit 02351a45b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 27 deletions

View File

@ -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

View File

@ -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])

View File

@ -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