sciapp / gr

GR framework: a graphics library for visualisation applications
Other
328 stars 55 forks source link

Get bbox box of objects #154

Open isentropic opened 2 years ago

isentropic commented 2 years ago

Is there an easy way to obtain bboxes of text objects and latex rendered text objects? Asking to for Plots jl. If there would be some communication with gr backend it would be great. Now, creating layouts is a little difficult as it is hard to determining appropriate padding for text

jheinen commented 2 years ago

The bounding box of text objects can be obtained using the GR.inqtext function. There are several examples in the GR examples directory (mixed_text.jl, user_fonts.jl, math_align.jl, math_bbox.jl, circumscribe_text.jl), e.g.:

using GR
using LaTeXStrings

s = "Using inline math \$\\frac{2hc^2}{\\lambda^5} \\frac{1}{e^{\\frac{hc}{\\lambda k_B T}} - 1}\$ in GR text\nmixed with LaTeXStrings " * L"- \frac{{\hbar ^2}}{{2m}}\frac{{\partial ^2 \psi (x,t)}}{{\partial x^2 }} + U(x)\psi (x,t) = i\hbar \frac{{\partial \psi (x,t)}}{{\partial t}}" * "\n– with line breaks\nand UTF-8 characters (ħπ),\nand rendered using GR's text attributes"

selntran(0)

settextfontprec(232, 3)
settextalign(2, 3)
setcharheight(0.02)

for ϕ in LinRange(0, 2π, 360)
    clearws()
    setcharup(sin(ϕ), cos(ϕ))
    text(0.5, 0.5, s)
    tbx, tby = inqtext(0.5, 0.5, s)
    fillarea(tbx, tby)
    updatews()
end
Screenshot 2022-05-31 at 09 06 23

From Plots.jl, you can access this function with Plots.GR.inqtext(). But as the text attributes might be changed within Plots, it would probably be better to provide a dedicated Plots function to provide this functionality.

t-bltg commented 2 years ago

It would be really great to be able to toggle bounding boxes in Plots for developing / debugging.