diff --git a/Project.toml b/Project.toml index 3578f4b6..f95faaa7 100644 --- a/Project.toml +++ b/Project.toml @@ -40,6 +40,8 @@ OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" SymEngine = "123dc426-2d89-5057-bbad-38513e3affd8" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" +VisualRegressionTests = "34922c18-7c2a-561c-bac1-01e79b2c4c92" +Gtk = "4c0ca9eb-093a-5379-98c5-f87ac0bbbf44" [targets] -test = ["DataFrames", "OffsetArrays", "SymEngine", "SparseArrays", "Test"] +test = ["DataFrames", "OffsetArrays", "SymEngine", "SparseArrays", "VisualRegressionTests", "Gtk", "Test"] diff --git a/src/utils.jl b/src/utils.jl index a5d558fd..e37dc799 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -145,6 +145,7 @@ function render(s::LaTeXString, mime::MIME"image/png"; callshow=true, open=true, dpi=DEFAULT_DPI[], + transparent=true, kw... ) ext = "png" @@ -154,11 +155,13 @@ function render(s::LaTeXString, mime::MIME"image/png"; # prefer tex -> pdf -> png instead if convert === :gs aux_mime = MIME("application/pdf") - cmd = `gs -sDEVICE=pngalpha -dTextAlphaBits=4 -r$dpi -o $name.$ext $aux_name.pdf` + device = transparent ? "pngalpha" : "png16m" + cmd = `gs -sDEVICE=$device -dTextAlphaBits=4 -r$dpi -o $name.$ext $aux_name.pdf` elseif convert === :dvipng aux_mime = MIME("application/x-dvi") + background = transparent ? "Transparent" : "white" deb = debug ? [] : ["-q"] - cmd = `dvipng $(deb...) -bg Transparent -D $dpi -T tight $aux_name.dvi -o $name.$ext` + cmd = `dvipng $(deb...) -bg $background -D $dpi -T tight $aux_name.dvi -o $name.$ext` else error("$convert program not understood") end diff --git a/test/runtests.jl b/test/runtests.jl index 26a58c64..f9eefc72 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -5,8 +5,14 @@ using Latexify using LaTeXStrings using Test +is_ci() = parse(Bool, get(ENV, "CI", "false")) + # Run tests +if ~is_ci() || Sys.islinux() + # Tests that for some reason don't run on Mac or Windows CI go here + @testset "visual regression tests" begin include("visualregression_tests.jl") end +end @testset "macro test" begin include("macros.jl") end @testset "recipe test" begin include("recipe_test.jl") end @testset "latexify tests" begin include("latexify_test.jl") end diff --git a/test/visualreferences/ket.png b/test/visualreferences/ket.png new file mode 100644 index 00000000..ecd61b7d Binary files /dev/null and b/test/visualreferences/ket.png differ diff --git a/test/visualreferences/simpleequation.png b/test/visualreferences/simpleequation.png new file mode 100644 index 00000000..47accb3c Binary files /dev/null and b/test/visualreferences/simpleequation.png differ diff --git a/test/visualreferences/table.png b/test/visualreferences/table.png new file mode 100644 index 00000000..c5200e4d Binary files /dev/null and b/test/visualreferences/table.png differ diff --git a/test/visualregression_tests.jl b/test/visualregression_tests.jl new file mode 100644 index 00000000..52303ef1 --- /dev/null +++ b/test/visualregression_tests.jl @@ -0,0 +1,20 @@ +using Latexify, VisualRegressionTests +if ~is_ci() + using Gtk +end + +function latexify_VRT(filename, args...; kwargs...) + testfun(fname) = render(latexify(args...; kwargs...), MIME"image/png"(); name=replace(fname, r".png$"=>""), transparent=false) + @visualtest testfun joinpath("visualreferences", filename) ~is_ci() 0.01 +end + +latexify_VRT("simpleequation.png", :(x^2-2y_a*exp(3)∈[1,2,3]); cdot=false) +latexify_VRT("table.png", hcat([1,2,3], [4,5,6], [7;8;9]); env=:table, head=["a" "b" "c"]) + +struct Ket{T} + x::T +end +@latexrecipe function f(x::Ket) + return Expr(:latexifymerge, "\\left|", x.x, "\\right>") +end +latexify_VRT("ket.png", :($(Ket(:a)) + $(Ket(:b))))