diff --git a/Project.toml b/Project.toml index 4297266..bb2cb89 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "MakieTeX" uuid = "6d554a22-29e7-47bd-aee5-0c5f06619414" authors = ["Anshul Singhvi"] -version = "0.3.1" +version = "0.3.2" [deps] Cairo = "159f3aea-2a34-519c-b102-8c37f9878175" @@ -18,11 +18,11 @@ tectonic_jll = "d7dd28d6-a5e6-559c-9131-7eb760cdacc5" [compat] Cairo = "1.0.5" -CairoMakie = "0.10" +CairoMakie = "0.11" Colors = "0.9, 0.10, 0.11, 0.12" DocStringExtensions = "0.8, 0.9" LaTeXStrings = "1" -Makie = "0.18, 0.19" +Makie = "0.20" Poppler_jll = "21.9" julia = "1" diff --git a/src/layoutable.jl b/src/layoutable.jl index 58efa05..e54179a 100644 --- a/src/layoutable.jl +++ b/src/layoutable.jl @@ -1,8 +1,8 @@ -import Makie.MakieLayout: inherit +import Makie: inherit # This code has basically been adapted from the Label code in the main repo. -Makie.MakieLayout.@Block LTeX begin +Makie.@Block LTeX begin @attributes begin "The LaTeX code to be compiled and drawn. Can be a String, a TeXDocument or a CachedTeX." tex = "\\LaTeX" @@ -35,7 +35,7 @@ end LTeX(x, tex; kwargs...) = LTeX(x; tex = tex, kwargs...) -function Makie.MakieLayout.initialize_block!(l::LTeX) +function Makie.initialize_block!(l::LTeX) topscene = l.blockscene layoutobservables = l.layoutobservables @@ -55,16 +55,16 @@ function Makie.MakieLayout.initialize_block!(l::LTeX) textbb = Ref(BBox(0, 1, 0, 1)) onany(l.tex, l.scale, l.rotation, l.padding) do tex, scale, rotation, padding - textbb[] = Makie.rotatedrect(Makie.MakieLayout.Rect2f(0,0,(t[1][][1].dims .* scale)...), rotation) - autowidth = Makie.MakieLayout.width(textbb[]) + padding[1] + padding[2] - autoheight = Makie.MakieLayout.height(textbb[]) + padding[3] + padding[4] + textbb[] = Makie.rotatedrect(Makie.Rect2f(0,0,(t[1][][1].dims .* scale)...), rotation) + autowidth = Makie.width(textbb[]) + padding[1] + padding[2] + autoheight = Makie.height(textbb[]) + padding[3] + padding[4] layoutobservables.autosize[] = (autowidth, autoheight) end onany(layoutobservables.computedbbox, l.padding) do bbox, padding - tw = Makie.MakieLayout.width(textbb[]) - th = Makie.MakieLayout.height(textbb[]) + tw = Makie.width(textbb[]) + th = Makie.height(textbb[]) box = bbox.origin[1] boy = bbox.origin[2] @@ -72,7 +72,7 @@ function Makie.MakieLayout.initialize_block!(l::LTeX) tx = box + padding[1] + 0.5 * tw ty = boy + padding[3] + 0.5 * th - textpos[] = Makie.MakieLayout.Point3f[(tx, ty, 0)] + textpos[] = Makie.Point3f[(tx, ty, 0)] end diff --git a/src/recipe.jl b/src/recipe.jl index 0bc26ca..d653e69 100644 --- a/src/recipe.jl +++ b/src/recipe.jl @@ -63,7 +63,7 @@ function Makie.plot!(plot::T) where T <: TeXImg # We always want to draw this at a 1:1 ratio, so increasing scale or # changing dpi should rerender plottable_images = lift(plot[1], plot.render_density, plot.scale) do cachedtex, render_density, scale - to_array(firstpage2img((cachedtex); render_density = render_density * scale)) + to_array(firstpage2img.((cachedtex); render_density = render_density * scale)) end scatter_images = Observable(plottable_images[]) @@ -77,7 +77,7 @@ function Makie.plot!(plot::T) where T <: TeXImg onany(plottable_images, plot.position, plot.rotations, plot.align, plot.scale) do images, pos, rotations, align, scale if length(images) != length(pos) # skip this update and let the next one propagate - @debug "Length of images ($(length(images))) != length of positions ($(length(pos))). Skipping this update." + @debug "TexImg: Length of images ($(length(images))) != length of positions ($(length(pos))). Skipping this update." return end @@ -94,7 +94,7 @@ function Makie.plot!(plot::T) where T <: TeXImg notify(scatter_rotations) end - plot.position[] = plot.position[] + notify(plot.position) # trigger the first update scatter!( plot, @@ -112,7 +112,6 @@ end # CairoMakie direct drawing method function draw_tex(scene::Scene, screen::CairoMakie.Screen, cachedtex::CachedTeX, position::VecTypes, scale::VecTypes, rotation::Real, align::Tuple{Symbol, Symbol}) # establish some initial values - x0, y0 = 0.0, 0.0 w, h = cachedtex.dims ctx = screen.context # First we center the position with respect to the center of the image, @@ -124,21 +123,22 @@ function draw_tex(scene::Scene, screen::CairoMakie.Screen, cachedtex::CachedTeX, # Then, we find the appropriate "marker offset" w.r.t. alignment. # This is separate because of Cairo's reversed y-axis. halign, valign = align - pos = Point2f(0) - pos = if halign == :left - pos .- (-scale[1] / 2, 0) + offset_pos = Point2f(0) + # First, we handle the horizontal alignment + offset_pos = if halign == :left + offset_pos .- (-scale[1] / 2, 0) elseif halign == :center - pos .- (0, 0) + offset_pos .- (0, 0) elseif halign == :right - pos .- (scale[1] / 2, 0) + offset_pos .- (scale[1] / 2, 0) end - - pos = if valign == :top - pos .+ (0, scale[2]/2) + # and then the vertical alignment. + offset_pos = if valign == :top + offset_pos .+ (0, scale[2]/2) elseif valign == :center - pos .+ (0, 0) + offset_pos .+ (0, 0) elseif valign == :bottom - pos .- (0, scale[2]/2) + offset_pos .- (0, scale[2]/2) end # Calculate, with respect to the rotation, where the rotated center of the image @@ -161,7 +161,7 @@ function draw_tex(scene::Scene, screen::CairoMakie.Screen, cachedtex::CachedTeX, #compensate for that with previously calculated values Cairo.translate(ctx, cx, cy) # apply "marker offset" to implement/simulate alignment - Cairo.translate(ctx, pos[1], pos[2]) + Cairo.translate(ctx, offset_pos[1], offset_pos[2]) # scale the marker appropriately Cairo.scale( ctx, @@ -198,6 +198,10 @@ function draw_tex(scene::Scene, screen::CairoMakie.Screen, cachedtex::CachedTeX, Cairo.restore(ctx) end +# Override `is_cairomakie_atomic_plot` to allow `TeXImg` to remain a unit, +# instead of auto-decomposing into its component scatter plot. +CairoMakie.is_cairomakie_atomic_plot(plot::TeXImg) = true + function CairoMakie.draw_plot(scene::Scene, screen::CairoMakie.Screen, img::T) where T <: MakieTeX.TeXImg broadcast_foreach(img[1][], img.position[], img.scale[], CairoMakie.remove_billboard(img.rotations[]), img.align[]) do cachedtex, position, scale, rotation, align diff --git a/src/text_override.jl b/src/text_override.jl index 2de1c9d..e18f43a 100644 --- a/src/text_override.jl +++ b/src/text_override.jl @@ -149,6 +149,8 @@ to_array(f::AbstractVector) = f to_array(f::T) where T <: Makie.VecTypes = T[f] to_array(f::T) where T = T[f] +# We use Makie's spec-API to redirect text calls with our input to + ### WARNING: deprecated code lies below # this was rendered invalid by the text refactor, which # simplified all of the text calls into a central call diff --git a/test/runtests.jl b/test/runtests.jl index 2f65798..32fcd68 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -20,7 +20,7 @@ function render_texample(url) fig = Figure() - lt = Label(fig[1, 1], CachedTeX(TeXDocument(read(Downloads.download(url), String)))) + lt = LTeX(fig[1, 1], CachedTeX(TeXDocument(read(Downloads.download(url), String)))) @test true @@ -38,7 +38,7 @@ end @testset "MakieTeX.jl" begin can_access_texample = try - Downloads.download("https://texample.net/media/tikz/examples/TEX/rotated_triangle.tex") + Downloads.download("https://texample.net/media/tikz/examples/TEX/rotated-triangle.tex") true catch e false @@ -102,7 +102,7 @@ end # mkpath(joinpath(example_path, "aligns")) - # f = Figure(resolution = (200, 200)) + # f = Figure(size = (200, 200)) # lt = Label(f[1, 1], LaTeXString("Hello from Makie\\TeX{}!")) # teximg = lt.blockscene.plots[1] @@ -119,8 +119,8 @@ end @testset "Layouting" begin @testset "Logo" begin - fig = Figure(figure_padding = 1, resolution = (1, 1)) - @test_nowarn Label(fig[1, 1], LaTeXString("Makie\\TeX.jl")) + fig = Figure(figure_padding = 1, size = (1, 1)) + @test_nowarn LTeX(fig[1, 1], LaTeXString("Makie\\TeX.jl")) @test_nowarn resize_to_layout!(fig) save_test("logo", fig) @@ -132,7 +132,7 @@ end @testset "Corrupting Axis" begin - fig = Figure(fontsize = 12, resolution = (300, 300)) + fig = Figure(fontsize = 12, size = (300, 300)) # Create a GridLayout for the axis and labels gl = fig[1, 1] = GridLayout() # Create the Axis within this layout, leave space for the title and labels @@ -155,7 +155,7 @@ end end # @testset "Integrating with Axis" begin - # fig = Figure(fontsize = 12, resolution = (300, 300)) + # fig = Figure(fontsize = 12, size = (300, 300)) # ax = Axis( # fig[1,1]; # xlabel = LaTeXString("time (\$t\$) in arbitrary units"), @@ -192,7 +192,7 @@ end """) fig = Figure() - @test_nowarn lab = Label(fig[1, 1], td) + @test_nowarn lab = LTeX(fig[1, 1], td) @test_nowarn save_test("link", fig) end @@ -207,7 +207,7 @@ end # @testset "Theming" begin # @test_nowarn begin # fig = with_theme(theme_dark()) do - # fig = Figure(fontsize = 12, resolution = (300, 300)) + # fig = Figure(fontsize = 12, size = (300, 300)) # ax = Axis( # fig[1,1]; # xlabel = LaTeXString("time (\$t\$) in arbitrary units"),