Created
December 6, 2024 13:16
-
-
Save jkrumbiegel/c3a8d3edf51c5e1f4d6634d248a022da to your computer and use it in GitHub Desktop.
save cairomakie svg around axis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CairoMakie | |
using GLMakie | |
GLMakie.activate!() | |
f = Figure() | |
for i in 1:3, j in 1:3 | |
Axis(f[i, j], title = "$i $j") | |
end | |
display(f) | |
function outer_bbox(ax; padding) | |
sbb = ax.layoutobservables.suggestedbbox[] | |
prot = ax.layoutobservables.protrusions[] | |
o = sbb.origin .- (prot.left, prot.bottom) .- padding | |
w = sbb.widths .+ (prot.left + prot.right, prot.bottom + prot.top) .+ 2 * padding | |
Rect2f(o, w) | |
end | |
function save_cropped_svg(file, scene, bbox) | |
sw, sh = scene.viewport[].widths | |
ox, oy = bbox.origin | |
w, h = bbox.widths | |
svg = mktempdir() do dir | |
save(joinpath(dir, "output.svg"), scene; backend = CairoMakie) | |
read(joinpath(dir, "output.svg"), String) | |
end | |
svg = replace( | |
svg, | |
r"viewBox=\".*?\"" => "viewBox=\"$ox $(sh - oy - h) $w $h\"", | |
r"width=\".*?\"" => "width=\"$w\"", | |
r"height=\".*?\"" => "height=\"$h\"", | |
count = 3, | |
) | |
open(file, "w") do io | |
write(io, svg) | |
end | |
return | |
end | |
ax = current_axis() | |
save_cropped_svg("test.svg", ax.blockscene, outer_bbox(ax; padding = 10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment