repr icon indicating copy to clipboard operation
repr copied to clipboard

Added functionality to set the font family for the Cairo device

Open casparvl opened this issue 4 years ago • 0 comments

This PR partly solves https://github.com/IRkernel/IRkernel/issues/604 .

As suggested in that issue, fonts for axes labels sometimes appear to have poor resolution. To resolve this, it is suggested in that issue to use the Cairo device, by setting options(bitmapType='Cairo') in the startup argv for the kernel (i.e. in the kernel.json).

This does indeed make sure that the Cairo device is used from that point onward. However, it will still use the default font. In my case that was a Helvetica, a (low-resolution) X11 font, and the resulting axes labels would still show the poor resolution.

The solution is to use a vector-type font, such as sans. The problem is that for png plot types with the Cairo device, the repr.plot.family is ignored.

This PR makes sure that the font family specified in repr.plot.family is indeed set to the Cairo devices.

I already suggested the change a long time ago but never found the time to make the change, test it, and PR it... In that thread, I proposed this approach to @flying-sheep who also considered it a good approach.

For me, with this PR, I can now do

options(repr.plot.width = 10, repr.plot.height = 8, repr.plot.res=96, repr.plot.family='FreeMono')

ggplot(iris) + geom_point(aes(Sepal.Width, Petal.Width)) + cowplot::theme_cowplot(
    font_size=25)

and get: image While with

options(repr.plot.family='LiberationMono')
ggplot(iris) + geom_point(aes(Sepal.Width, Petal.Width)) + cowplot::theme_cowplot(
    font_size=25)

I get image

These samples clearly show that the repr.plot.family is now respected.

casparvl avatar Sep 20 '21 12:09 casparvl