Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate text align #4651

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/basic_recipes/text.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions test/text.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading