Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tex_annotation!() #43

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/MakieTeX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export LTeX
export LaTeXStrings, LaTeXString, latexstring, @L_str
export Typstry, TypstString, @typst_str

export tex_annotation!

"Try to write to `engine` and see what happens"
function try_tex_engine(engine::Cmd)
try
Expand Down
145 changes: 145 additions & 0 deletions src/recipe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,148 @@ function Makie.plot!(plot::TeXImg)
markerspace = plot.markerspace,
)
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,
# regardless of its alignment. This ensures that rotation takes place
# in the correct "axis" (2d).
position = position .+ (-scale[1]/2, scale[2]/2)


# 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)
elseif halign == :center
pos .- (0, 0)
elseif halign == :right
pos .- (scale[1] / 2, 0)
end

pos = if valign == :top
pos .+ (0, scale[2]/2)
elseif valign == :center
pos .+ (0, 0)
elseif valign == :bottom
pos .- (0, scale[2]/2)
end

# Calculate, with respect to the rotation, where the rotated center of the image
# should be.
# (Rotated center - Normal center)
cx = 0.5scale[1] * cos(rotation) - 0.5scale[2] * sin(rotation) - 0.5scale[1]
cy = 0.5scale[1] * sin(rotation) + 0.5scale[2] * cos(rotation) - 0.5scale[2]

# Begin the drawing and translation process
Cairo.save(ctx)
# translate to normal position
Cairo.translate(
ctx,
position[1],
position[2] - scale[2]
)
# rotate context by required rotation
Cairo.rotate(ctx, -rotation)
# cairo rotates around position as an axis,
#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])
# scale the marker appropriately
Cairo.scale(
ctx,
scale[1] / w,
scale[2] / h
)
# the rendering pipeline
# first is the "safe" Poppler pipeline, with better results in PDF
# and PNG, especially when rotated.
if !(RENDER_EXTRASAFE[])
# retrieve a new Poppler document pointer
document = update_pointer!(cachedtex)
# retrieve the first page
page = ccall(
(:poppler_document_get_page, Poppler_jll.libpoppler_glib),
Ptr{Cvoid},
(Ptr{Cvoid}, Cint),
document, 0 # page 0 is first page
)
# Render the page to the surface
ccall(
(:poppler_page_render, Poppler_jll.libpoppler_glib),
Cvoid,
(Ptr{Cvoid}, Ptr{Cvoid}),
page, ctx.ptr
)
else # "extra-safe" Cairo pipeline, also somewhat faster.
# render the cached CairoSurface to the screen.
# bad with PNG output though.
Cairo.set_source(ctx, cachedtex.surf, 0, 0)
Cairo.paint(ctx)
end
# restore context and end
Cairo.restore(ctx)
end

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

w, h = cachedtex.dims

pos = CairoMakie.project_position(
scene, img.space[],
Makie.apply_transform(scene.transformation.transform_func[], position),
img.model[]
)

_w = scale * w; _h = scale * h
scale_factor = CairoMakie.project_scale(scene, img.space[], Vec2{Float32}(_w, _h), img.model[])

draw_tex(scene, screen, cachedtex, pos, scale_factor, rotation, align)

end

end

"""
tex_annotation!(axis::Axis, lstring, x, y; mainfont = nothing, mathfont = nothing, scale_factor=1)

Add TeX annotation to an existing Makie Axis. Under the hood, it does a few things:

1. via `mathspec` LaTeX package, we set the `mainfont` and `mathfont`
2. Render it using `tectonic_jll` and convert to an image matrix.
3. call `Makie.scatter!` and using the image as `marker`, scale the image by `scale_factor` while preserving aspec ratio.

!!! note
You can use `\textcolor` from `xcolor` inside the latex string.
"""
function tex_annotation!(axis::Axis, lstring, x, y; mainfont = nothing, mathfont = nothing, scale_factor=1)
texdoc = TeXDocument(
String(lstring), true;
requires = "\\RequirePackage{luatex85}",
preamble = """
\\usepackage{amsmath, xcolor}
\\usepackage{mathspec}
\\pagestyle{empty}
$(isnothing(mainfont) ? "" :
"\\setmainfont{$mainfont}[Scale=MatchLowercase, Ligatures=TeX]"
)
$(isnothing(mathfont) ? "" :
"\\setmathfont(Digits,Latin)[Scale=MatchLowercase]{$mathfont}"
)
Comment on lines +167 to +172
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoa cool! I didn't know you could do that.

Do \bf, \it, \sc etc work correctly with this?

Copy link
Author

@Moelf Moelf Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably, we can probably have a comprehensive test of making 2x2x2 different math on the same canvase as a sanity check

two caveats:

  • I can't find the default font's name
  • I'm yet to fully understand how the exactly interaction between multiple set*font work

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default font in Makie is "TeX Gyre Heros Makie", it's in the assets folder of Makie (I think).
I guess we should set the default math font to also be whichever math font relates to TeX Gyre Heros? This is what I was trying to do earlier, in building up a set of known font families for which we have good math fonts.

""",
class = "standalone",
classoptions = "preview, tightpage, 12pt"
)
tex = CachedTeX(texdoc)
marker = MakieTeX.rotl90(MakieTeX.recordsurf2img(tex, 4))
scatter!(axis, x, y; marker, markersize=tex.dims .* scale_factor)
asinghvi17 marked this conversation as resolved.
Show resolved Hide resolved
end
Loading