This vignette tries to compare the quality of the output of ragg, with that of cairo and, if the system supports it, Xlib (Xlib is unix only). As quality is, to a certain extend, subjective, the vignette will be short on conclusions and filled with examples.

The present version of this vignette has been compiled on a system without the X11 device. The benchmarkings will thus omit this device, though the text will still refer to it.

Scope

There are mainly two areas of high importance when discussing graphic device quality: Shape rendering (fill and stroke) and text rendering. Both of these are highly dependent on the quality of anti-aliasing if any. Apart from that we will also look into alpha blending and raster interpolation.

Shape rendering

The examples here are relevant for rendering of all different types of shapes, be it lines or polygons. Lines obviously don’t have fill, so only the stroke rendering will have relevance here. We chose to use a circle for this as it provides a nice sampling of uneven edges.

circle_quality <- function(device, name, file, ...) {
  device(file, width = 600, height = 300, ...)
  grid.circle(
    x = c(0.25, 0.75), 
    r = 0.4, 
    gp = gpar(col = c('black', NA), fill = c(NA, 'black'), lwd = 2)
  )
  grid.text(y = 0.1, label = name, gp = gpar(cex = 2))
  invisible(dev.off())
}

Observations

ragg is the only device that provides anti-aliasing of fill, which results in obvious quality differences. The reason for not doing so in cairo is presumably to avoid artefacts when shapes are touching each other where anti-aliasing can result in a thin edge between the two shapes instead of a contiguous colour. This is a real issue, but I personally don’t agree that it should be allowed to degrade the overall quality of the device, hence the reason for not following this approach in ragg.

Xlib (if that is available on your system), provides completely non anti-aliased output and so does cairo with antialias = 'none'. It is surprising that Xlib appears to have a better stroke rendering than cairo without anti-aliasing. Further, the difference between 'gray' and 'subpixel' antialiasing is not visible to the naked eye, nor with a 2x zoom.

Text

Text is a difficult thing to handle for a graphic device, both in terms of finding system fonts, and in terms of rendering. Often the rendering is offloaded to another library (e.g. freetype), which will provide a bitmap representation to blend into the device. This approach is often good for horizontal or vertical text, but struggle with other rotations. Here we will test text rendering at different sizes and at a 27° counter-clockwise rotation. We will use the the system provided serif font as it provides a more complex task than a sans-serif one.

text_quality <- function(device, name, file, rotation = 0, ...) {
  text <- 'The quick blue R jumped over the lazy snake'
  vp <- viewport(angle = rotation)
  device(file, width = 600, height = 300, ...)
  pushViewport(vp)
  grid.text(text, x = 0.1, y = 0.2, just = 'left', gp = gpar(fontfamily = 'serif', cex = 0.5))
  grid.text(text, x = 0.1, y = 0.4, just = 'left', gp = gpar(fontfamily = 'serif', cex = 1))
  grid.text(text, x = 0.1, y = 0.6, just = 'left', gp = gpar(fontfamily = 'serif', cex = 1.5))
  grid.text(text, x = 0.1, y = 0.8, just = 'left', gp = gpar(fontfamily = 'serif', cex = 2))
  popViewport()
  grid.text(x = 0.9, y = 0.1, label = name, just = 'right', gp = gpar(cex = 2))
  invisible(dev.off())
}

Observations

Font handling is hard… Setting the font to 'serif' means different things to different devices and being more specific results in some devices not being able to find the font. ragg exclusively uses the system fonts so whatever your OS defines as the base serif type it will pick up. Cairo goes through the internal R database to pick a slightly different font. Xlib… well I can’t comment on what it is doing, but it appears to pick something completely different from the X11 system. Regarding quality, I don’t think I have to be mean and mention Xlib, so let’s look at ragg and cairo. Ignoring for a fact that they have used two different fonts, we can see some differences and some interesting stuff. First, cairo don’t care about the anti-alias setting when it renders fonts. My guess is that it will always ask for an 8bit pixelmap from the font engine (probably freetype) and use that. This ensures high quality fonts no matter the settings. The text appears quite heavy, which (unless this is a feature of the font) indicates that cairo does not gamma-correct the pixelmap before blending it into the image. Correct gamma correction of font is quite important, so if that is the case it is quite sad. Another thing we notice is that cairo uses the pixelmaps even for rotated text. This result in jagged baseline and uneven kerning when plotting rotated text. ragg only uses pixelmaps when plotting axis-aligned text. For rotated text it will convert the glyphs to polygons and render them using the built-in rasterizer ensuring an even baseline and kerning. This means plotting rotated text is slightly less performant, at the cost of looking good — I can live with that trade-off.

Alpha blending

How transparent colours are combined is not necessarily equal among devices. The biggest divide is on whether to use premultiplied colours or not. With premultiplied colours the red, green, and blue intensity is wieghted by the alpha directly, instead of alpha simply being an additional value. Using premultiplied colours is the only way to get correct alpha blending. As the Xlib device doesn’t support transparent colours it will be exempt from this comparison even on systems that have it. Further, as alpha blending is not related to anti-aliasing, we will simply compare ragg against a single cairo setup.

blend_quality <- function(device, name, file, ...) {
  device(file, width = 600, height = 300, ...)
  grid.rect(x = 0.35, y = 0.4, width = 0.5, height = 0.5, gp = gpar(fill = '#FF0000'))
  grid.rect(x = 0.65, y = 0.6, width = 0.5, height = 0.5, gp = gpar(fill = '#00FF001A'))
  grid.text(x = 0.9, y = 0.1, label = name, just = 'right', gp = gpar(cex = 2))
  invisible(dev.off())
}

Observations

Nothing much to say — both devices handles alpha blending correctly (see here to understand what this test was about)

Raster

The main way raster plotting can get influence by the device is in how the image gets interpolated during scaling, which will be briefly compared here.

raster_quality <- function(device, name, file, ...) {
  reds <- matrix(hcl(0, 80, seq(50, 80, 10)),
                        nrow = 4, ncol = 5)
  device(file, width = 600, height = 300, ...)
  grid.raster(reds, vp = viewport(0.25, 0.25, 0.5, 0.5, angle = 27))
  grid.raster(reds, interpolate = FALSE, 
              vp = viewport(0.75, 0.75, 0.5, 0.5, angle = 27))
  grid.text(x = 0.9, y = 0.1, label = name, just = 'right', gp = gpar(cex = 2))
  invisible(dev.off())
}

Observations

As with blending there is nothing much to see here. All devices perform equally and correctly.

Conclusion

When it comes to raster quality there is only really the two contenders are obviously ragg and anti-aliased cairo, as lack of anti-aliasing has clear detrimental effect on output quality. As there appear to be no real difference in quality between cairo’s two anti-aliasing modes the question basically boils down to cairo vs ragg. While for the most part the two rendering systems provide comparable output, there are two areas where ragg takes the lead, quality-wise: rendering of fill, and rendering of rotated text. If these areas are of interest to you then ragg will be the obvious choice.

Session info

sessioninfo::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.1 (2017-01-27)
#>  os       Ubuntu 16.04.6 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_US.UTF-8                 
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       UTC                         
#>  date     2019-08-23                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version    date       lib source        
#>  assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)
#>  backports     1.1.4      2019-04-10 [1] CRAN (R 3.6.0)
#>  cli           1.1.0      2019-03-19 [1] CRAN (R 3.6.0)
#>  commonmark    1.7        2018-12-01 [1] CRAN (R 3.6.0)
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 3.6.0)
#>  digest        0.6.20     2019-07-04 [1] CRAN (R 3.6.0)
#>  evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)
#>  fs            1.3.1      2019-05-06 [1] CRAN (R 3.6.0)
#>  htmltools     0.3.6      2017-04-28 [1] CRAN (R 3.6.0)
#>  knitr         1.24       2019-08-08 [1] CRAN (R 3.6.1)
#>  magick      * 2.1        2019-07-25 [1] CRAN (R 3.6.1)
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)
#>  MASS          7.3-51.4   2019-03-31 [4] CRAN (R 3.6.1)
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 3.6.0)
#>  pkgdown       1.3.0      2018-12-07 [1] CRAN (R 3.6.0)
#>  R6            2.4.0      2019-02-14 [1] CRAN (R 3.6.0)
#>  ragg        * 0.1.2.9000 2019-08-23 [1] local         
#>  Rcpp          1.0.2      2019-07-25 [3] CRAN (R 3.6.1)
#>  rlang         0.4.0      2019-06-25 [1] CRAN (R 3.6.1)
#>  rmarkdown     1.15       2019-08-21 [1] CRAN (R 3.6.1)
#>  roxygen2      6.1.1      2018-11-07 [1] CRAN (R 3.6.0)
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.6.0)
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.1)
#>  stringi       1.4.3      2019-03-12 [1] CRAN (R 3.6.0)
#>  stringr       1.4.0      2019-02-10 [1] CRAN (R 3.6.0)
#>  systemfonts   0.1.1      2019-07-01 [1] CRAN (R 3.6.0)
#>  withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)
#>  xfun          0.8        2019-06-25 [1] CRAN (R 3.6.0)
#>  xml2          1.2.0      2018-01-24 [1] CRAN (R 3.6.0)
#>  yaml          2.2.0      2018-07-25 [1] CRAN (R 3.6.0)
#> 
#> [1] /home/travis/R/Library
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /home/travis/R-bin/lib/R/library