Skip to content

Commit

Permalink
Remove TypstImg and LTypst
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobjpeters committed Jun 8, 2024
1 parent 57245bb commit 5aea23c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 93 deletions.
3 changes: 1 addition & 2 deletions src/MakieTeX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ export PDFDocument, CachedPDF
export SVGDocument, CachedSVG
export dvi2svg, latex2dvi, rsvg2recordsurf, svg2rsvg
export teximg, teximg!, TeXImg
export typstimg, typstimg!, TypstImg
export LTeX, LTypst
export LTeX

export LaTeXStrings, LaTeXString, latexstring, @L_str
export Typstry, TypstString, @typst_str
Expand Down
58 changes: 8 additions & 50 deletions src/layoutable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,65 +33,23 @@ Makie.@Block LTeX begin
end
end

Makie.@Block LTypst begin
@attributes begin
"The Typst code to be compiled and drawn. Can be a String, a TypstDocument or a CachedTypst."
typst = "\\Typst"
"The density of pixels rendered (1 means 1 px == 1 pt)"
render_density::Int = 1
"Controls if the graphic is visible."
visible::Bool = true
"A scaling factor to resize the graphic."
scale::Float32 = 1.0
"The horizontal alignment of the graphic in its suggested boundingbox"
halign = :center
"The vertical alignment of the graphic in its suggested boundingbox"
valign = :center
"The counterclockwise rotation of the graphic in radians."
rotation::Float32 = 0f0
"The extra space added to the sides of the graphic boundingbox."
padding = (0f0, 0f0, 0f0, 0f0)
"The height setting of the graphic."
height = Auto()
"The width setting of the graphic."
width = Auto()
"Controls if the parent layout can adjust to this element's width"
tellwidth::Bool = true
"Controls if the parent layout can adjust to this element's height"
tellheight::Bool = true
"The align mode of the graphic in its parent GridLayout."
alignmode = Inside()
end
end

LTeX(x, tex; kwargs...) = LTeX(x; tex = tex, kwargs...)
LTypst(x, typst; kwargs...) = LTypst(x; typst = typst, kwargs...)

code(lt::LTeX) = lt.tex
code(lt::LTypst) = lt.typst

img!(::Type{LTeX}, args...; kwargs...) = teximg!(args...; kwargs...)
img!(::Type{LTypst}, args...; kwargs...) = typstimg!(args...; kwargs...)

__to_cached(T, x) = T(x)
__to_cached(T, x::AbstractDocument) = Cached(x)

_to_cached(::Type{LTeX}, x) = __to_cached(CachedTEX, x)
_to_cached(::Type{LTypst}, x) = __to_cached(CachedTypst, x)
_to_cachedtex(x) = CachedTEX(x)
_to_cachedtex(x::AbstractDocument) = Cached(x)

function Makie.initialize_block!(l::T) where T <: Union{LTeX, LTypst}
function Makie.initialize_block!(l::LTeX)

_code = code(l)
topscene = l.blockscene
layoutobservables = l.layoutobservables

textpos = Observable([Point3f(0, 0, 0)])
scale = Observable([Vec2f(1.0, 1.0)])

cached = lift(collect tuple (x -> _to_cached(T, x)), _code)
cached_tex = lift(collect tuple _to_cachedtex, l.tex)

t = img!(T,
topscene, cached; position = textpos, visible = l.visible,
t = teximg!(
topscene, cached_tex; position = textpos, visible = l.visible,
scale = scale, align = (:bottom, :left),
rotations = l.rotation,
markerspace = :pixel,
Expand All @@ -100,7 +58,7 @@ function Makie.initialize_block!(l::T) where T <: Union{LTeX, LTypst}

textbb = Ref(BBox(0, 1, 0, 1))

onany(_code, l.scale, l.rotation, l.padding) do code, scale, rotation, padding
onany(l.tex, l.scale, l.rotation, l.padding) do tex, scale, rotation, padding
textbb[] = 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]
Expand All @@ -124,7 +82,7 @@ function Makie.initialize_block!(l::T) where T <: Union{LTeX, LTypst}


# trigger first update, otherwise bounds are wrong somehow
notify(_code)
notify(l.tex)
# trigger bbox
layoutobservables.suggestedbbox[] = layoutobservables.suggestedbbox[]

Expand Down
47 changes: 6 additions & 41 deletions src/recipe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,6 @@ $(Makie.ATTRIBUTES)
)
end

"""
typstimg(typst; position, ...)
typstimg!(ax_or_scene, tex; position, ...)
This recipe plots rendered `Typst` to your Figure or Scene.
There are three types of input you can provide:
- Any `String`, which is rendered to Typst cognizant of the figure's overall theme,
- A [`TypstDocument`](@ref) object, which is rendered to Typst directly, and can be customized by the user,
- A [`CachedTypst`](@ref) object, which is a pre-rendered Typst document.
`typst` may be a single one of these objects, or an array of them.
## Attributes
$(Makie.ATTRIBUTES)
"""
@recipe(TypstImg, typst) do scene
merge(
default_theme(scene),
Attributes(
render_density = 2,
align = (:center, :center),
scale = 1.0,
position = [Point2{Float32}(0)],
rotation = [0f0],
space = :data,
markerspace = :pixel
)
)
end

# First, handle the case of one or more abstract strings passed in!
# These are themable.

Expand Down Expand Up @@ -118,21 +87,18 @@ function offset_from_align(align::Tuple{Symbol, Symbol}, wh)::Vec2f
return Vec2f(x, y)
end

__bc_if_array(::Type{TeXImg}, x) = _bc_if_array(CachedTeX, x)
__bc_if_array(::Type{TypstImg}, x) = _bc_if_array(CachedTypst, x)

_bc_if_array(f, x) = f(x)
_bc_if_array(f, x::AbstractArray) = f.(x)

# scatter: marker size, rotations to determine everything
function Makie.plot!(plot::T) where T <: Union{TeXImg, TypstImg}
function Makie.plot!(plot::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 ct, render_density, scale
if ct isa AbstractString || ct isa AbstractArray{<: AbstractString}
to_array(__bc_if_array(T, ct))
plottable_images = lift(plot[1], plot.render_density, plot.scale) do cachedtex, render_density, scale
if cachedtex isa AbstractString || cachedtex isa AbstractArray{<: AbstractString}
to_array(_bc_if_array(CachedTEX, cachedtex))
else
to_array(_bc_if_array(Cached, ct))
to_array(_bc_if_array(Cached, cachedtex))
end
end

Expand All @@ -147,7 +113,7 @@ function Makie.plot!(plot::T) where T <: Union{TeXImg, TypstImg}
onany(plot, plottable_images, plot.position, plot.rotation, plot.align, plot.scale) do images, pos, rotations, align, scale
if length(images) != length(pos) && !(pos isa Makie.VecTypes)
# skip this update and let the next one propagate
@debug "$T: 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 Down Expand Up @@ -177,4 +143,3 @@ function Makie.plot!(plot::T) where T <: Union{TeXImg, TypstImg}
markerspace = plot.markerspace,
)
end

0 comments on commit 5aea23c

Please sign in to comment.