From 87cd032a469490e9bb4d90f7dd0fb84ea8cf8bf1 Mon Sep 17 00:00:00 2001 From: Julius Krumbiegel Date: Fri, 6 Dec 2024 14:27:23 +0100 Subject: [PATCH] Validate text align --- src/basic_recipes/text.jl | 25 +++++++++++++++++++++++-- test/text.jl | 8 ++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/basic_recipes/text.jl b/src/basic_recipes/text.jl index a033fadeaef..fd57624e817 100644 --- a/src/basic_recipes/text.jl +++ b/src/basic_recipes/text.jl @@ -13,6 +13,24 @@ convert_arguments(::Type{<:Text}, string::AbstractString) = (string,) # Fallback to PointBased convert_arguments(::Type{<:Text}, args...) = convert_arguments(PointBased(), args...) +function validate_text_align(al) + if al isa Tuple{Any,Any} + if !(al[1] isa Real || al[1] in (:left, :right, :center)) + error("Horizontal text align must be a Real or :left, :right, :center. Got $(repr(al[1]))") + end + if !(al[2] isa Real || al[2] in (:top, :bottom, :center, :baseline)) + error("Vertical text align must be a Real or :top, :bottom, :center, :baseline. Got $(repr(al[2]))") + end + else + error("Text align must be a two-element tuple, got $(repr(al))") + end + return +end + +function validate_text_align(als::AbstractVector) + foreach(validate_text_align, als) + return +end function plot!(plot::Text) positions = plot[1] @@ -47,8 +65,11 @@ function plot!(plot::Text) lwidths = Float32[] lcolors = RGBAf[] lindices = Int[] - function push_args(args...) - gc, ls, lw, lc, lindex = _get_glyphcollection_and_linesegments(args...) + + validate_text_align(al) + + function push_args(str, id, ts, f, fs, al, rot, jus, lh, col, scol, swi, www, offs) + gc, ls, lw, lc, lindex = _get_glyphcollection_and_linesegments(str, id, ts, f, fs, al, rot, jus, lh, col, scol, swi, www, offs) push!(gcs, gc) append!(lsegs, ls) append!(lwidths, lw) diff --git a/test/text.jl b/test/text.jl index 02bca1c97b1..08b5f36c37c 100644 --- a/test/text.jl +++ b/test/text.jl @@ -123,3 +123,11 @@ end @test_throws err Label(Figure()[1, 1], "hi", textsize = 30) # @test_throws err text(1, 2, text = "hi", textsize = 30) end + +@testset "align validation" begin + @test_throws ErrorException("Text align must be a two-element tuple, got :center") text(1, 2, align = :center) + @test_throws ErrorException("Vertical text align must be a Real or :top, :bottom, :center, :baseline. Got :centr") text(1, 2, align = (1, :centr)) + @test_throws ErrorException("Horizontal text align must be a Real or :left, :right, :center. Got :centr") text(1, 2, align = (:centr, 1)) + @test_throws ErrorException("Text align must be a two-element tuple, got :center") text(1:2, 3:4, text = ["A", "B"], align = [:center, :center]) + @test_throws ErrorException("Vertical text align must be a Real or :top, :bottom, :center, :baseline. Got :centr") text(1:2, 3:4, text = ["A", "B"], align = [(:center, :centr), (:center, :center)]) +end