diff --git a/ReferenceTests/src/tests/primitives.jl b/ReferenceTests/src/tests/primitives.jl index ee037943133..150aab8a35d 100644 --- a/ReferenceTests/src/tests/primitives.jl +++ b/ReferenceTests/src/tests/primitives.jl @@ -445,3 +445,15 @@ end scene end + +@reference_test "barplot with TeX-ed labels" begin + fig = Figure(resolution = (800, 800)) + lab1 = L"\int f(x) dx" + lab2 = lab1 + # lab2 = L"\frac{a}{b} - \sqrt{b}" # this will not work until #2667 is fixed + + barplot(fig[1,1], [1, 2], [0.5, 0.2], bar_labels = [lab1, lab2], flip_labels_at = 0.3, direction=:x) + barplot(fig[1,2], [1, 2], [0.5, 0.2], bar_labels = [lab1, lab2], flip_labels_at = 0.3) + + fig +end \ No newline at end of file diff --git a/src/basic_recipes/barplot.jl b/src/basic_recipes/barplot.jl index 164630cdb48..e86d10960de 100644 --- a/src/basic_recipes/barplot.jl +++ b/src/basic_recipes/barplot.jl @@ -78,7 +78,7 @@ $(ATTRIBUTES) color_over_bar = automatic, label_offset = 5, label_font = theme(scene, :font), - label_size = 20, + label_size = theme(scene, :fontsize), label_formatter = bar_label_formatter, transparency = false ) @@ -220,7 +220,7 @@ end function Makie.plot!(p::BarPlot) - labels = Observable(Tuple{String, Point2f}[]) + labels = Observable(Tuple{Union{String,LaTeXStrings.LaTeXString}, Point2f}[]) label_aligns = Observable(Vec2f[]) label_offsets = Observable(Vec2f[]) label_colors = Observable(RGBAf[]) diff --git a/src/basic_recipes/text.jl b/src/basic_recipes/text.jl index 8581cd4fca1..aee85ac7d5e 100644 --- a/src/basic_recipes/text.jl +++ b/src/basic_recipes/text.jl @@ -231,21 +231,8 @@ function texelems_and_glyph_collection(str::LaTeXString, fontscale_px, halign, v end end - xshift = if halign === :center - width(bb) ./ 2 - elseif halign === :left - minimum(bb)[1] - elseif halign === :right - maximum(bb)[1] - end - - yshift = if valign === :center - maximum(bb)[2] - (height(bb) / 2) - elseif valign === :top - maximum(bb)[2] - else - minimum(bb)[2] - end + xshift = get_xshift(minimum(bb)[1], maximum(bb)[1], halign) + yshift = get_yshift(minimum(bb)[2], maximum(bb)[2], valign, default=0f0) shift = Vec3f(xshift, yshift, 0) positions = basepositions .- Ref(shift) @@ -395,25 +382,8 @@ function apply_alignment_and_justification!(lines, ju, al) top_y = max_y_ascender(lines[1]) bottom_y = min_y_descender(lines[end]) - al_offset_x = if al[1] === :center - max_x / 2 - elseif al[1] === :left - 0f0 - elseif al[1] === :right - max_x - else - 0f0 - end - - al_offset_y = if al[2] === :center - 0.5 * (top_y + bottom_y) - elseif al[2] === :bottom - bottom_y - elseif al[2] === :top - top_y - else - 0f0 - end + al_offset_x = get_xshift(0f0, max_x, al[1]; default=0f0) + al_offset_y = get_yshift(bottom_y, top_y, al[2]; default=0f0) fju = float_justification(ju, al) @@ -431,23 +401,9 @@ end function float_justification(ju, al)::Float32 halign = al[1] float_justification = if ju === automatic - if halign === :left || halign == 0 - 0.0f0 - elseif halign === :right || halign == 1 - 1.0f0 - elseif halign === :center || halign == 0.5 - 0.5f0 - else - 0.5f0 - end - elseif ju === :left - 0.0f0 - elseif ju === :right - 1.0f0 - elseif ju === :center - 0.5f0 + get_xshift(0f0, 1f0, halign) else - Float32(ju) + get_xshift(0f0, 1f0, ju; default=ju) # errors if wrong symbol is used end end @@ -546,3 +502,21 @@ function new_glyphstate(gs::GlyphState, rt::RichText, val::Val{:sub}, fonts) end iswhitespace(r::RichText) = iswhitespace(String(r)) + +function get_xshift(lb, ub, align; default=0.5f0) + if align isa Symbol + align = align === :left ? 0.0f0 : + align === :center ? 0.5f0 : + align === :right ? 1.0f0 : default + end + lb * (1-align) + ub * align |> Float32 +end + +function get_yshift(lb, ub, align; default=0.5f0) + if align isa Symbol + align = align === :bottom ? 0.0f0 : + align === :center ? 0.5f0 : + align === :top ? 1.0f0 : default + end + lb * (1-align) + ub * align |> Float32 +end