Skip to content

Commit

Permalink
Remove text override behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
asinghvi17 committed Jan 15, 2023
1 parent de2ec0c commit dea5b62
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 239 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ MakieTeX allows you to draw and visualize arbitrary TeX documents in Makie! You

It works by compiling a stand-alone <img src="https://upload.wikimedia.org/wikipedia/commons/9/92/LaTeX_logo.svg" alt="LaTeX" height="20" align = "center"></a> document to PDF. For CairoMakie, the PDF is read and rendered directly, and a raster image is rendered in GLMakie.

When loaded, MakieTeX will replace the handling of LaTeXStrings, which Makie natively performs with [`MathTeXEngine.jl`](https://github.com/Kolaru/MathTeXEngine.jl), with the MakieTeX pipeline. This is significantly more time-consuming, so be warned - try not to `MakieTeX` for the axes of plots which you want to interact with! Other things, which don't update as often, are fine.

### Quick start
```julia
using Makie, MakieTeX
Expand All @@ -21,22 +19,15 @@ l1 = Label(
)
ax1 = Axis(
fig[2, 1];
xtickformat = x -> latexstring.("a_{" .* string.(x) .* "}"),
ytickformat = x -> latexstring.(string.(x)),
ylabel = L"\displaystyle \Phi(\vec x) = f(\vec x) + g(V)",
)
heatmap!(ax1, Makie.peaks())
fig
```
<img src="https://user-images.githubusercontent.com/32143268/170724177-d7cf9d16-8feb-4f6e-bb22-68fa8269066c.svg" height=300></img>


Also note that as of Makie 0.17, you cannot _change_ the text rendering mode; this is to say, assigning a LaTeXString or TeXDocument to a `text` plot to which you have passed a String will not work as expected. Consequently, you must provide any LaTeX you want to render _at construction_. This is the reason why we have set the axis attributes within the constructor call.

You need not install anything for MakieTeX to work, since we ship a minimal TeX renderer called [`tectonic`](https://tectonic-typesetting.github.io/en-US/) (based on XeLaTeX). This will download any missing packages when it encounters them the first time. However, it will likely not know about any local packages or TEXMF paths, nor will it be able to render advanced features like TikZ graphs which require LuaTeX. The latexmk/lualatex combination will also likely be faster, and able to use advanced features like calling to other languages with `pythontex` (oh, the heresy!)

MakieTeX also renders TeX in accordance with the theme - specifically, if one changes the text color or textsize, it will be reflected in the rendered TeX. Wrapping the above code with `with_theme(theme_dark()) do ... end` yields:

<img src="https://user-images.githubusercontent.com/32143268/170724393-727fd7f3-277d-46c2-9616-9fcc988e8715.svg" height=300></img>


Expand Down
272 changes: 140 additions & 132 deletions src/text_override.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,135 +149,143 @@ to_array(f::AbstractVector) = f
to_array(f::T) where T <: Makie.VecTypes = T[f]
to_array(f::T) where T = T[f]

function Makie.plot!(t::Makie.Text{<: Tuple{<: CachedTeX}})
teximg!(
t, lift(to_array, t[1]);
space = t.space, position=@lift([$(t.position)]), align = t.align,
rotations = @lift([$(t.rotation)]), visible = t.visible,
scale = 1, render_density = TEXT_RENDER_DENSITY[]
)
end


function Makie.plot!(t::Makie.Text{<: Tuple{<:AbstractVector{<:CachedTeX}}})
teximg!(
t, t[1];
space = t.space, position=t.position, align = t.align,
rotations = lift(to_array, t.rotation), visible = t.visible,
scale = 1, render_density = TEXT_RENDER_DENSITY[]
)
end

function Makie.plot!(t::Makie.Text{<: Tuple{<:TeXDocument}})
plottable_cached_tex = lift(to_array CachedTeX, t[1])

teximg!(
t, plottable_cached_tex;
space = t.space, position=lift(to_array, t.position), align = t.align,
rotations = lift(to_array, t.rotation), visible = t.visible,
scale = 1, render_density = TEXT_RENDER_DENSITY[]
)
end

function Makie.plot!(t::Makie.Text{<: Tuple{<: AbstractVector{<: TeXDocument}}})
plottable_cached_texs = lift(t[1]) do ltexs
ct = CachedTeX.(ltexs)
end

teximg!(
t, plottable_cached_texs;
space = t.space, position = position, align = t.align,
rotations = t.rotation, visible = t.visible,
scale = 1, render_density=TEXT_RENDER_DENSITY[]
)
end

################################################################################
# ☠️ Real Piracy 🦜 #
################################################################################

# Here, we pirate the Makie functions which plot LaTeXString text using
# MathTeXEngine.jl and make them use MakieTeX's handling routines.
# This means that once MakieTeX is loaded, there is no way to go back to
# MathTeXEngine!
# A future solution for this would be to have some global render mode which decides
# which path is taken, but that would likely have to be done in Makie itself.

function Makie.plot!(t::Makie.Text{<: Tuple{<:LaTeXString}})
plottable_cached_tex = lift(t[1], t.font, t.textsize, t.lineheight, t.color) do ltex, font, textsize, lineheight, color
CachedTeX[to_plottable_cachedtex(ltex, font, textsize, lineheight, to_color(color))]
end

teximg!(t, plottable_cached_tex; position=lift(to_array, t.position), scale = 1, render_density = TEXT_RENDER_DENSITY[], align = t.align, rotations = t.rotation, visible = t.visible)
end

function Makie.plot!(t::Makie.Text{<: Tuple{<: AbstractVector{<: LaTeXString}}})
old_ltex = Ref(t[1][])

plottable_cached_texs = Observable{Vector{CachedTeX}}()
onany(t[1], t.font, t.textsize, t.lineheight, t.color) do ltexs, font, textsize, lineheight, color
if !(ltexs == old_ltex)
plottable_cached_texs.val = _plottable_cachedtex_from_array(ltexs, font, textsize, lineheight, to_color(color))
notify(plottable_cached_texs)
old_ltex[] = ltexs
else
return
end

end
t.font[] = t.font[]

teximg!(
t, plottable_cached_texs;
position = t.position, align = t.align, rotations = t.rotation,
visible = t.visible, scale = 1, render_density = TEXT_RENDER_DENSITY[]
)
end


# Define bounding box methods for all extended plot types

function Makie.boundingbox(x::Makie.Text{<:Tuple{<:CachedTeX}})
Makie.boundingbox(
x[1][],
to_ndim(Point3f, x.position[], 0),
x.rotation[],
to_value(get(x.attributes, :scale, 1)),
x.align[]
)
end

function Makie.boundingbox(x::Makie.Text{<:Tuple{<:AbstractArray{<:CachedTeX}}})
Makie.boundingbox(
x[1][],
to_ndim.(Point3f, x.position[], 0),
x.rotation[],
to_value(get(x.attributes, :scale, 1)),
x.align[]
)
end

function Makie.boundingbox(x::Makie.Text{<:Tuple{<:Union{LaTeXString, TeXDocument}}})
Makie.boundingbox(
x.plots[1][1][],
to_ndim(Point3f, x.position[], 0),
x.rotation[],
to_value(get(x.attributes, :scale, 1)),
x.align[]
)
end

function Makie.boundingbox(x::Makie.Text{<:Tuple{<:AbstractArray{<:Union{LaTeXString, TeXDocument}}}})
Makie.boundingbox(
x.plots[1][1][],
to_ndim.(Point3f, x.position[], 0),
x.rotation[],
to_value(get(x.attributes, :scale, 1)),
x.align[]
)
end

# Re-direct and allow our methods to pick these up
function Makie.boundingbox(x::Makie.Text{<:Tuple{<:AbstractArray{<: Tuple{<:T, <:Point}}}}) where T <: AbstractString
return Makie.boundingbox(x.plots[1])
end
### WARNING: deprecated code lies below
# this was rendered invalid by the text refactor, which
# simplified all of the text calls into a central call
# which did not allow dispatch on the type of text!

# Future revisions will have to fit into the framework
# of `_get_glyphcollection_and_linesegments`

# function Makie.plot!(t::Makie.Text{<: Tuple{<: CachedTeX}})
# teximg!(
# t, lift(to_array, t[1]);
# space = t.space, position=@lift([$(t.position)]), align = t.align,
# rotations = @lift([$(t.rotation)]), visible = t.visible,
# scale = 1, render_density = TEXT_RENDER_DENSITY[]
# )
# end


# function Makie.plot!(t::Makie.Text{<: Tuple{<:AbstractVector{<:CachedTeX}}})
# teximg!(
# t, t[1];
# space = t.space, position=t.position, align = t.align,
# rotations = lift(to_array, t.rotation), visible = t.visible,
# scale = 1, render_density = TEXT_RENDER_DENSITY[]
# )
# end

# function Makie.plot!(t::Makie.Text{<: Tuple{<:TeXDocument}})
# plottable_cached_tex = lift(to_array ∘ CachedTeX, t[1])

# teximg!(
# t, plottable_cached_tex;
# space = t.space, position=lift(to_array, t.position), align = t.align,
# rotations = lift(to_array, t.rotation), visible = t.visible,
# scale = 1, render_density = TEXT_RENDER_DENSITY[]
# )
# end

# function Makie.plot!(t::Makie.Text{<: Tuple{<: AbstractVector{<: TeXDocument}}})
# plottable_cached_texs = lift(t[1]) do ltexs
# ct = CachedTeX.(ltexs)
# end

# teximg!(
# t, plottable_cached_texs;
# space = t.space, position = position, align = t.align,
# rotations = t.rotation, visible = t.visible,
# scale = 1, render_density=TEXT_RENDER_DENSITY[]
# )
# end

# ################################################################################
# # ☠️ Real Piracy 🦜 #
# ################################################################################

# # Here, we pirate the Makie functions which plot LaTeXString text using
# # MathTeXEngine.jl and make them use MakieTeX's handling routines.
# # This means that once MakieTeX is loaded, there is no way to go back to
# # MathTeXEngine!
# # A future solution for this would be to have some global render mode which decides
# # which path is taken, but that would likely have to be done in Makie itself.

# function Makie.plot!(t::Makie.Text{<: Tuple{<:LaTeXString}})
# plottable_cached_tex = lift(t[1], t.font, t.textsize, t.lineheight, t.color) do ltex, font, textsize, lineheight, color
# CachedTeX[to_plottable_cachedtex(ltex, font, textsize, lineheight, to_color(color))]
# end

# teximg!(t, plottable_cached_tex; position=lift(to_array, t.position), scale = 1, render_density = TEXT_RENDER_DENSITY[], align = t.align, rotations = t.rotation, visible = t.visible)
# end

# function Makie.plot!(t::Makie.Text{<: Tuple{<: AbstractVector{<: LaTeXString}}})
# old_ltex = Ref(t[1][])

# plottable_cached_texs = Observable{Vector{CachedTeX}}()
# onany(t[1], t.font, t.textsize, t.lineheight, t.color) do ltexs, font, textsize, lineheight, color
# if !(ltexs == old_ltex)
# plottable_cached_texs.val = _plottable_cachedtex_from_array(ltexs, font, textsize, lineheight, to_color(color))
# notify(plottable_cached_texs)
# old_ltex[] = ltexs
# else
# return
# end

# end
# t.font[] = t.font[]

# teximg!(
# t, plottable_cached_texs;
# position = t.position, align = t.align, rotations = t.rotation,
# visible = t.visible, scale = 1, render_density = TEXT_RENDER_DENSITY[]
# )
# end


# # Define bounding box methods for all extended plot types

# function Makie.boundingbox(x::Makie.Text{<:Tuple{<:CachedTeX}})
# Makie.boundingbox(
# x[1][],
# to_ndim(Point3f, x.position[], 0),
# x.rotation[],
# to_value(get(x.attributes, :scale, 1)),
# x.align[]
# )
# end

# function Makie.boundingbox(x::Makie.Text{<:Tuple{<:AbstractArray{<:CachedTeX}}})
# Makie.boundingbox(
# x[1][],
# to_ndim.(Point3f, x.position[], 0),
# x.rotation[],
# to_value(get(x.attributes, :scale, 1)),
# x.align[]
# )
# end

# function Makie.boundingbox(x::Makie.Text{<:Tuple{<:Union{LaTeXString, TeXDocument}}})
# Makie.boundingbox(
# x.plots[1][1][],
# to_ndim(Point3f, x.position[], 0),
# x.rotation[],
# to_value(get(x.attributes, :scale, 1)),
# x.align[]
# )
# end

# function Makie.boundingbox(x::Makie.Text{<:Tuple{<:AbstractArray{<:Union{LaTeXString, TeXDocument}}}})
# Makie.boundingbox(
# x.plots[1][1][],
# to_ndim.(Point3f, x.position[], 0),
# x.rotation[],
# to_value(get(x.attributes, :scale, 1)),
# x.align[]
# )
# end

# # Re-direct and allow our methods to pick these up
# function Makie.boundingbox(x::Makie.Text{<:Tuple{<:AbstractArray{<: Tuple{<:T, <:Point}}}}) where T <: AbstractString
# return Makie.boundingbox(x.plots[1])
# end
Loading

0 comments on commit dea5b62

Please sign in to comment.