Support keyword arguments for font attributes

This commit is contained in:
Samuel S. Watson 2019-01-16 13:04:31 -05:00
parent f0a3ca4314
commit 888ed9f0ae

View File

@ -258,24 +258,22 @@ end
"""
font(args...)
Create a Font from an unordered list of features.
Create a Font from a list of features. Values may be specified either as
arguments (which are distinguished by type/value) or as keyword arguments.
# Arguments
- `family`: AbstractString. "serif" or "sans-serif" or "monospace"
- `pointsize`: Integer. Size of font in points
- `halign`: Symbol. Horizontal alignment (:hcenter, :left, or :right)
- `valign`: Symbol. Vertical aligment (:vcenter, :top, or :bottom)
- `rotation`: Real. Angle of rotation for text in degrees (use a non-integer type)
- `color`: Colorant or Symbol
# Examples
```julia-repl
julia> text("sans-serif",8,:hcenter,45.0,:blue)
julia> font(8)
julia> font(family="serif",halign=:center,rotation=45.0)
```
"""
function font(args...)
function font(args...;kw...)
# defaults
family = "sans-serif"
@ -319,6 +317,32 @@ function font(args...)
end
end
for symbol in keys(kw)
if symbol == :family
family = kw[:family]
elseif symbol == :pointsize
pointsize = kw[:pointsize]
elseif symbol == :halign
halign = kw[:halign]
if halign == :center
halign = :hcenter
end
@assert halign in (:hcenter, :left, :right)
elseif symbol == :valign
valign = kw[:valign]
if valign == :center
valign = :vcenter
end
@assert valign in (:vcenter, :top, :bottom)
elseif symbol == :rotation
rotation = kw[:rotation]
elseif symbol == :color
color = parse(Colorant, kw[:color])
else
@warn("Unused font kwarg: $symbol")
end
end
Font(family, pointsize, halign, valign, rotation, color)
end
@ -362,15 +386,16 @@ end
PlotText(str) = PlotText(string(str), font())
"""
text(string, args...)
text(string, args...; kw...)
Create a PlotText object wrapping a string with font info, for plot annotations
Create a PlotText object wrapping a string with font info, for plot annotations.
`args` and `kw` are passed to `font`.
"""
text(t::PlotText) = t
text(t::PlotText, font::Font) = PlotText(t.str, font)
text(str::AbstractString, f::Font) = PlotText(str, f)
function text(str, args...)
PlotText(string(str), font(args...))
function text(str, args...;kw...)
PlotText(string(str), font(args...;kw...))
end
Base.length(t::PlotText) = length(t.str)