fix magic font argument and cascading font defaults

This commit is contained in:
Daniel Schwabeneder 2017-11-19 16:59:54 +01:00
parent 6e51a08694
commit e80a73cc1c
2 changed files with 52 additions and 22 deletions

View File

@ -293,6 +293,7 @@ const _plot_defaults = KW(
const _subplot_defaults = KW( const _subplot_defaults = KW(
:title => "", :title => "",
:title_location => :center, # also :left or :right :title_location => :center, # also :left or :right
:fontfamily_subplot => :match,
:titlefontfamily => :match, :titlefontfamily => :match,
:titlefontsize => 14, :titlefontsize => 14,
:titlefonthalign => :hcenter, :titlefonthalign => :hcenter,
@ -764,14 +765,37 @@ function processGridArg!(d::KW, arg, letter)
end end
end end
function processFontArgs!(d::KW, fontname::Symbol, args::Tuple) function processFontArg!(d::KW, fontname::Symbol, arg)
fnt = font(args...) T = typeof(arg)
d[Symbol(fontname, :family)] = fnt.family if T <: Font
d[Symbol(fontname, :size)] = fnt.pointsize d[Symbol(fontname, :family)] = arg.family
d[Symbol(fontname, :halign)] = fnt.halign d[Symbol(fontname, :size)] = arg.pointsize
d[Symbol(fontname, :valign)] = fnt.valign d[Symbol(fontname, :halign)] = arg.halign
d[Symbol(fontname, :rotation)] = fnt.rotation d[Symbol(fontname, :valign)] = arg.valign
d[Symbol(fontname, :color)] = fnt.color d[Symbol(fontname, :rotation)] = arg.rotation
d[Symbol(fontname, :color)] = arg.color
elseif arg == :center
d[Symbol(fontname, :halign)] = :hcenter
d[Symbol(fontname, :valign)] = :vcenter
elseif arg in (:hcenter, :left, :right)
d[Symbol(fontname, :halign)] = arg
elseif arg in (:vcenter, :top, :bottom)
d[Symbol(fontname, :valign)] = arg
elseif T <: Colorant
d[Symbol(fontname, :color)] = arg
elseif T <: Symbol || T <: AbstractString
try
d[Symbol(fontname, :color)] = parse(Colorant, string(arg))
catch
d[Symbol(fontname, :family)] = string(arg)
end
elseif typeof(arg) <: Integer
d[Symbol(fontname, :size)] = arg
elseif typeof(arg) <: Real
d[Symbol(fontname, :rotation)] = convert(Float64, arg)
else
warn("Skipped font arg: $arg ($(typeof(arg)))")
end
end end
_replace_markershape(shape::Symbol) = get(_markerAliases, shape, shape) _replace_markershape(shape::Symbol) = get(_markerAliases, shape, shape)
@ -844,20 +868,26 @@ function preprocessArgs!(d::KW)
# fonts # fonts
for fontname in (:titlefont, :legendfont) for fontname in (:titlefont, :legendfont)
args = pop!(d, fontname, ()) args = pop!(d, fontname, ())
processFontArgs!(d, fontname, args) for arg in wraptuple(args)
processFontArg!(d, fontname, arg)
end
end end
# handle font args common to all axes # handle font args common to all axes
for fontname in (:tickfont, :guidefont) for fontname in (:tickfont, :guidefont)
args = pop!(d, fontname, ()) args = pop!(d, fontname, ())
for letter in (:x, :y, :z) for arg in wraptuple(args)
processFontArgs!(d, Symbol(letter, fontname), args) for letter in (:x, :y, :z)
processFontArg!(d, Symbol(letter, fontname), arg)
end
end end
end end
# handle individual axes font args # handle individual axes font args
for letter in (:x, :y, :z) for letter in (:x, :y, :z)
for fontname in (:tickfont, :guidefont) for fontname in (:tickfont, :guidefont)
args = pop!(d, Symbol(letter, fontname), ()) args = pop!(d, Symbol(letter, fontname), ())
processFontArgs!(d, Symbol(letter, fontname), args) for arg in wraptuple(args)
processFontArg!(d, Symbol(letter, fontname), arg)
end
end end
end end
@ -1134,12 +1164,12 @@ const _match_map = KW(
:top_margin => :margin, :top_margin => :margin,
:right_margin => :margin, :right_margin => :margin,
:bottom_margin => :margin, :bottom_margin => :margin,
:titlefontfamily => :fontfamily, :titlefontfamily => :fontfamily_subplot,
:labelfontfamily => :fontfamily, :legendfontfamily => :fontfamily_subplot,
:tickfontfamily => :fontfamily,
:guidefontfamily => :fontfamily,
:titlefontcolor => :foreground_color_subplot, :titlefontcolor => :foreground_color_subplot,
:labelfontcolor => :foreground_color_subplot, :legendfontcolor => :foreground_color_subplot,
:tickfontcolor => :foreground_color_text,
:guidefontcolor => :foreground_color_guide,
) )
# these can match values from the parent container (axis --> subplot --> plot) # these can match values from the parent container (axis --> subplot --> plot)
@ -1151,8 +1181,9 @@ const _match_map2 = KW(
:foreground_color_grid => :foreground_color_subplot, :foreground_color_grid => :foreground_color_subplot,
:foreground_color_guide => :foreground_color_subplot, :foreground_color_guide => :foreground_color_subplot,
:foreground_color_text => :foreground_color_subplot, :foreground_color_text => :foreground_color_subplot,
:tickfontcolor => :foreground_color_text, :fontfamily_subplot => :fontfamily,
:guidefontcolor => :foreground_color_guide, :tickfontfamily => :fontfamily_subplot,
:guidefontfamily => :fontfamily_subplot,
) )
# properly retrieve from plt.attr, passing `:match` to the correct key # properly retrieve from plt.attr, passing `:match` to the correct key

View File

@ -904,7 +904,6 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
halign = GR.TEXT_HALIGN_CENTER halign = GR.TEXT_HALIGN_CENTER
end end
GR.settextalign(halign, GR.TEXT_VALIGN_TOP) GR.settextalign(halign, GR.TEXT_VALIGN_TOP)
gr_set_textcolor(sp[:foreground_color_title])
gr_text(xpos, viewport_subplot[4], sp[:title]) gr_text(xpos, viewport_subplot[4], sp[:title])
end end
@ -1229,7 +1228,7 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
i = 0 i = 0
if sp[:legendtitle] != nothing if sp[:legendtitle] != nothing
GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_HALF) GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_HALF)
gr_set_textcolor(sp[:foreground_color_legend]) gr_set_textcolor(sp[:legendfontcolor])
GR.settransparency(1) GR.settransparency(1)
gr_text(xpos - 0.03 + 0.5*w, ypos, string(sp[:legendtitle])) gr_text(xpos - 0.03 + 0.5*w, ypos, string(sp[:legendtitle]))
ypos -= dy ypos -= dy
@ -1271,7 +1270,7 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
lab = series[:label] lab = series[:label]
end end
GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF) GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF)
gr_set_textcolor(sp[:foreground_color_legend]) gr_set_textcolor(sp[:legendfontcolor])
gr_text(xpos, ypos, lab) gr_text(xpos, ypos, lab)
ypos -= dy ypos -= dy
end end