From 42fe91c8bb56c43b7d6df3d8bc4ad3705fa8e702 Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Sun, 29 Aug 2021 17:08:55 -0400 Subject: [PATCH 1/5] gr tick rotations --- src/backends/gr.jl | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index e9de8630..e279e74f 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -672,16 +672,38 @@ end function gr_set_tickfont(sp, letter) axis = sp[Symbol(letter, :axis)] - # invalidate alignment changes for small rotations (|θ| < 45°) - trigger(rot) = abs(sind(rot)) < abs(cosd(rot)) ? 0 : sign(rot) - - rot = axis[:rotation] + rot = axis[:rotation] % 360 if letter === :x || (RecipesPipeline.is3d(sp) && letter === :y) - halign = (:left, :hcenter, :right)[trigger(rot) + 2] - valign = (axis[:mirror] ? :bottom : :top, :vcenter)[trigger(abs(rot)) + 1] + valign = if -90 < rot < 90 || rot < -270 || rot > 270 + axis[:mirror] ? :bottom : :top + elseif abs(rot) % 90 == 0 # -270, -90, 90, 270 + :vcenter + else + axis[:mirror] ? :top : :bottom + end + halign = if abs(rot) % 180 == 0 # -180, 0, 180 + :hcenter + elseif 0 < rot < 180 || -360 < rot < -180 + axis[:mirror] ? :left : :right + else # 180 < rot < 360 || -180 < rot < 0 + axis[:mirror] ? :right : :left + end else - halign = (axis[:mirror] ? :left : :right, :hcenter)[trigger(abs(rot)) + 1] - valign = (:top, :vcenter, :bottom)[trigger(rot) + 2] + valign = if rot % 180 == 0 + :vcenter + elseif 0 < rot < 180 || -360 < rot < -180 + axis[:mirror] ? :top : :bottom + else # rot < 0 + axis[:mirror] ? :bottom : :top + end + + halign = if -90 < rot < 90 || rot < -270 || rot > 270 + axis[:mirror] ? :left : :right + elseif abs(rot) == 90 || abs(rot) == 270 + :hcenter + else # 90 < rot < 270; -270 < rot < -90 + axis[:mirror] ? :right : :left + end end gr_set_font( tickfont(axis), From 41f7de672a50eda8bfa116830c82e70f64d9bab8 Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Sun, 29 Aug 2021 17:33:10 -0400 Subject: [PATCH 2/5] consistency --- src/backends/gr.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index e279e74f..3a18f86f 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -681,6 +681,7 @@ function gr_set_tickfont(sp, letter) else axis[:mirror] ? :top : :bottom end + halign = if abs(rot) % 180 == 0 # -180, 0, 180 :hcenter elseif 0 < rot < 180 || -360 < rot < -180 From 896743ed1c090f00548d5cbfaef82b915228e412 Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Fri, 17 Sep 2021 17:09:01 -0400 Subject: [PATCH 3/5] start of angle offset work --- src/backends/gr.jl | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 3a18f86f..fe0e958a 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -1576,21 +1576,27 @@ function gr_draw_ticks(sp, axis, segments, func = gr_polyline) end function gr_label_ticks(sp, letter, ticks) - axis = sp[Symbol(letter, :axis)] + println(ticks) + println(gr_text_size.(ticks |> last)) + grs = gr_text_size.(ticks |> last) + axis = sp[get_attr_symbol(letter, :axis)] + println(text_box_width.(map(first, grs), map(last, grs), axis[:rotation])) + diff_width = ((text_box_width.(map(first, grs), map(last, grs), axis[:rotation]) .- map(first, grs)) ./ 2) .* cos(axis[:rotation]) + println(diff_width) isy = letter === :y oletter = isy ? :x : :y oaxis = sp[Symbol(oletter, :axis)] oamin, oamax = axis_limits(sp, oletter) gr_set_tickfont(sp, letter) out_factor = ifelse(axis[:tick_direction] === :out, 1.5, 1) - x_offset = isy ? -1.5e-2 * out_factor : 0 + x_offset = (isy ? -1.5e-2 * out_factor : 0) y_offset = isy ? 0 : -8e-3 * out_factor ov = sp[:framestyle] == :origin ? 0 : xor(oaxis[:flip], axis[:mirror]) ? oamax : oamin sgn = axis[:mirror] ? -1 : 1 - for (cv, dv) in zip(ticks...) + for (cv, dv, diff_width) in zip(ticks..., diff_width) x, y = GR.wctondc(reverse_if((cv, ov), isy)...) - gr_text(x + sgn * x_offset, y + sgn * y_offset, dv) + gr_text(x + sgn * (x_offset + diff_width), y + sgn * y_offset, dv) end end From 14765ef2e96155d7809a83e732c48ae39dfc4451 Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Fri, 17 Sep 2021 22:03:22 -0400 Subject: [PATCH 4/5] Working! --- src/backends/gr.jl | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index fe0e958a..95ef0b38 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -1576,27 +1576,33 @@ function gr_draw_ticks(sp, axis, segments, func = gr_polyline) end function gr_label_ticks(sp, letter, ticks) - println(ticks) - println(gr_text_size.(ticks |> last)) - grs = gr_text_size.(ticks |> last) - axis = sp[get_attr_symbol(letter, :axis)] - println(text_box_width.(map(first, grs), map(last, grs), axis[:rotation])) - diff_width = ((text_box_width.(map(first, grs), map(last, grs), axis[:rotation]) .- map(first, grs)) ./ 2) .* cos(axis[:rotation]) - println(diff_width) + axis = sp[Symbol(letter, :axis)] isy = letter === :y oletter = isy ? :x : :y oaxis = sp[Symbol(oletter, :axis)] oamin, oamax = axis_limits(sp, oletter) gr_set_tickfont(sp, letter) out_factor = ifelse(axis[:tick_direction] === :out, 1.5, 1) - x_offset = (isy ? -1.5e-2 * out_factor : 0) + x_offset = isy ? -1.5e-2 * out_factor : 0 y_offset = isy ? 0 : -8e-3 * out_factor + rot = axis[:rotation] ov = sp[:framestyle] == :origin ? 0 : xor(oaxis[:flip], axis[:mirror]) ? oamax : oamin sgn = axis[:mirror] ? -1 : 1 - for (cv, dv, diff_width) in zip(ticks..., diff_width) + for (cv, dv) in zip(ticks...) x, y = GR.wctondc(reverse_if((cv, ov), isy)...) - gr_text(x + sgn * (x_offset + diff_width), y + sgn * y_offset, dv) + y_rot_offset = y_offset + x_rot_offset = x_offset + if rot % 90 != 0 + if isy + sgn2 = -180 < rot < 0 ? -1 : 1 + y_rot_offset -= sgn2 * last(gr_text_size(dv)) / 2 * cosd(rot) + else + sgn2 = rot < -90 || rot > 90 ? -1 : 1 + x_rot_offset -= sgn2 * first(gr_text_size(dv)) / 2 * sind(rot) + end + end + gr_text(x + sgn * x_rot_offset, y + sgn * y_rot_offset, dv) end end From b74d1ff276a143f8f1dc7028c8d302d49014f17c Mon Sep 17 00:00:00 2001 From: t-bltg Date: Sat, 18 Sep 2021 13:20:26 +0200 Subject: [PATCH 5/5] simplify logic --- src/backends/gr.jl | 67 +++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 95ef0b38..e5105378 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -672,45 +672,44 @@ end function gr_set_tickfont(sp, letter) axis = sp[Symbol(letter, :axis)] - rot = axis[:rotation] % 360 - if letter === :x || (RecipesPipeline.is3d(sp) && letter === :y) - valign = if -90 < rot < 90 || rot < -270 || rot > 270 - axis[:mirror] ? :bottom : :top - elseif abs(rot) % 90 == 0 # -270, -90, 90, 270 - :vcenter - else - axis[:mirror] ? :top : :bottom - end - - halign = if abs(rot) % 180 == 0 # -180, 0, 180 - :hcenter - elseif 0 < rot < 180 || -360 < rot < -180 - axis[:mirror] ? :left : :right - else # 180 < rot < 360 || -180 < rot < 0 - axis[:mirror] ? :right : :left - end - else - valign = if rot % 180 == 0 - :vcenter - elseif 0 < rot < 180 || -360 < rot < -180 - axis[:mirror] ? :top : :bottom - else # rot < 0 - axis[:mirror] ? :bottom : :top - end + mirror = ( + vcenter = :vcenter, + hcenter = :hcenter, + left = :right, + right = :left, + bottom = :top, + top = :bottom, + ) - halign = if -90 < rot < 90 || rot < -270 || rot > 270 - axis[:mirror] ? :left : :right - elseif abs(rot) == 90 || abs(rot) == 270 - :hcenter - else # 90 < rot < 270; -270 < rot < -90 - axis[:mirror] ? :right : :left - end + hv_align(rot) = begin + halign = if 0 < rot < 180 || -360 < rot < -180 + :right + elseif 180 < rot < 360 || -180 < rot < 0 + :left + else + :hcenter + end + valign = if -90 < rot < 90 || rot < -270 || rot > 270 + :top + elseif 90 < rot < 270 || -270 < rot < -90 + :bottom + else + :vcenter + end + halign, valign end + + if letter === :x || (RecipesPipeline.is3d(sp) && letter === :y) + halign, valign = hv_align(axis[:rotation] % 360) + else + halign, valign = hv_align((axis[:rotation] + 90) % 360) # y := x + 90° + end + gr_set_font( tickfont(axis), sp, - halign = halign, - valign = valign, + halign = axis[:mirror] ? mirror[halign] : halign, + valign = axis[:mirror] ? mirror[valign] : valign, rotation = axis[:rotation], color = axis[:tickfontcolor], )