Skip to content

Commit

Permalink
Update for new Makie
Browse files Browse the repository at this point in the history
  • Loading branch information
asinghvi17 committed Feb 15, 2024
1 parent 8e97853 commit 7e780e3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"

Expand Down
18 changes: 9 additions & 9 deletions src/layoutable.jl
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -55,24 +55,24 @@ 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]

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


Expand Down
34 changes: 19 additions & 15 deletions src/recipe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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[])
Expand All @@ -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

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/text_override.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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]

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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"),
Expand Down Expand Up @@ -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
Expand All @@ -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"),
Expand Down

0 comments on commit 7e780e3

Please sign in to comment.