Skip to content

Commit

Permalink
Workaround for Ghostscript_jll/ImageMagick_jll issues on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
Krastanov committed Mar 7, 2021
1 parent 6d0ea8a commit abbd182
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/Quantikz.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ end
circuit2string(circuit, qubits; kw...) = table2string(circuit2table(circuit, qubits; kw...))
circuit2string(circuit; kw...) = table2string(circuit2table(circuit; kw...))

function string2image(string; scale=5)
function string2image(string; scale=5, kw...)
dir = mktempdir()
cp(quantikzfile, joinpath(dir,quantikzname))
template = """
Expand All @@ -262,20 +262,33 @@ function string2image(string; scale=5)
\\end{adjustbox}
\\end{document}
"""
# Workaround for imagemagick failing to find gs on Windows (see https://github.com/JuliaIO/ImageMagick.jl/issues/198)
savefile = get(Dict(kw), :_workaround_savefile, nothing)
olddir = pwd()
cd(dir) do
f = open("input.tex", "w")
print(f,template)
close(f)
@trysuppress tectonic() do bin
run(`$bin input.tex`)
end
gs() do bin
return load("input.pdf")
# Workaround for imagemagick failing to find gs on Windows (see https://github.com/JuliaIO/ImageMagick.jl/issues/198)
#gs() do bin
# return load("input.pdf")
#end
if isnothing(savefile)
@trysuppress gs() do bin
run(`$bin -dNOPAUSE -sDEVICE=pngalpha -dSAFER -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 -dTextAlphaBits=4 -dGraphicsAlphaBits=1 -sOutputFile=input.png input.pdf -dBATCH`)
end
return load("input.png")
else
cp("input.pdf", joinpath(olddir,savefile), force=true)
return
end
end
end

circuit2image(circuit, qubits; scale=5, kw...) = string2image(table2string(circuit2table(circuit, qubits; kw...)); scale=scale)
circuit2image(circuit, qubits; scale=5, kw...) = string2image(table2string(circuit2table(circuit, qubits; kw...)); scale=scale, kw...)
circuit2image(circuit; scale=5, kw...) = circuit2image(circuit, circuitwidth(circuit); scale=scale, kw...)

function displaycircuit(circuit, qubits; scale=1, kw...)
Expand All @@ -286,6 +299,11 @@ end
displaycircuit(circuit; scale=1, kw...) = displaycircuit(circuit, circuitwidth(circuit); scale=scale, kw...)

function savecircuit(circuit,qubits,filename; scale=5, kw...) # TODO remove duplicated code
# Workaround for imagemagick failing to find gs on Windows (see https://github.com/JuliaIO/ImageMagick.jl/issues/198)
if endswith(filename, "pdf") || endswith(filename, "PDF")
circuit2image(circuit, qubits; scale=scale, _workaround_savefile=filename)
return
end
image = circuit2image(circuit, qubits; scale=scale)
save(filename,image)
end
Expand Down

0 comments on commit abbd182

Please sign in to comment.