Skip to content

Commit

Permalink
Improve bar_labels: LaTeXStrings and fontsize (#2501)
Browse files Browse the repository at this point in the history
* Improve `bar_labels`: `LaTeXString`s and `fontsize`


alignment


use `align` keyword again


use `get_xshift` once more


fix

* add test

---------

Co-authored-by: Simon <sdanisch@protonmail.com>
  • Loading branch information
greimel and SimonDanisch authored Jul 22, 2023
1 parent 1734cf8 commit acf64cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 52 deletions.
12 changes: 12 additions & 0 deletions ReferenceTests/src/tests/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/basic_recipes/barplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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[])
Expand Down
74 changes: 24 additions & 50 deletions src/basic_recipes/text.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -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

0 comments on commit acf64cf

Please sign in to comment.