Skip to content

Commit 1bd526d

Browse files
authored
Better document default_device_type() (#800)
* Switch to Cairo as the preferred fallback device * Try to confirm a hypothesis * Rework `default_device_type()` to be very clear about per OS behavior * Remove hypothesis
1 parent 681d969 commit 1bd526d

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

crates/ark/src/modules/positron/graphics.R

+60-2
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,72 @@ default_resolution_in_pixels_per_inch <- function() {
409409
}
410410
}
411411

412+
#' Determines the default device `type` for png, jpeg, and tiff
413+
#'
414+
#' Only applicable when ragg is not in use
412415
default_device_type <- function() {
416+
switch(
417+
system_os(),
418+
macos = default_device_type_macos(),
419+
windows = default_device_type_windows(),
420+
linux = default_device_type_linux(),
421+
# Treat `other` as linux
422+
other = default_device_type_linux()
423+
)
424+
}
425+
426+
#' On MacOS, we prefer Quartz
427+
#'
428+
#' At one point we considered preferring Cairo, but `capabilities("cairo")`
429+
#' isn't a reliable signal of whether or not you have Cairo support, because
430+
#' surprisingly you also need xquartz installed as well, i.e. with `brew install
431+
#' --cask xquartz`. Confusingly you don't need that to use the `"quartz"` type.
432+
#' https://github.com/posit-dev/positron/issues/913
433+
#' https://github.com/posit-dev/positron/issues/2919
434+
#'
435+
#' From https://cran.r-project.org/doc/manuals/r-release/R-admin.html#Installing-R-under-macOS-1:
436+
#' "Various parts of the build require XQuartz to be installed...This is also
437+
#' needed for some builds of the cairographics-based devices...such as
438+
#' png(type = "cairo") and svg()..."
439+
#'
440+
#' To avoid this issue for Mac users, we don't even consider Cairo in our
441+
#' fallback path. It seems unlikely we'd ever get past `"quartz"` as the
442+
#' fallback anyways, it's probably installed on all Macs.
443+
default_device_type_macos <- function() {
413444
if (has_aqua()) {
414445
"quartz"
415-
} else if (has_cairo()) {
446+
} else if (has_x11()) {
447+
"Xlib"
448+
} else {
449+
stop_no_plotting_capabilities()
450+
}
451+
}
452+
453+
#' On Windows, we prefer Cairo
454+
#'
455+
#' According to Thomas, this is much preferred over the default on Windows,
456+
#' which uses the Windows GDI. We don't even offer that as a fallback.
457+
default_device_type_windows <- function() {
458+
if (has_cairo()) {
459+
"cairo"
460+
} else {
461+
stop_no_plotting_capabilities()
462+
}
463+
}
464+
465+
#' On Linux, we prefer Cairo
466+
#'
467+
#' This is the default there, and we have no reason to move away from it.
468+
default_device_type_linux <- function() {
469+
if (has_cairo()) {
416470
"cairo"
417471
} else if (has_x11()) {
418472
"Xlib"
419473
} else {
420-
stop("This version of R wasn't built with plotting capabilities")
474+
stop_no_plotting_capabilities()
421475
}
422476
}
477+
478+
stop_no_plotting_capabilities <- function() {
479+
stop("This version of R wasn't built with plotting capabilities")
480+
}

crates/ark/src/modules/positron/system.R

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
is_macos <- function() {
2-
Sys.info()[["sysname"]] == "Darwin"
1+
#' One of:
2+
#' - "macos"
3+
#' - "windows"
4+
#' - "linux"
5+
#' - "other"
6+
system_os <- function() {
7+
switch(
8+
tolower(Sys.info()[["sysname"]]),
9+
darwin = "macos",
10+
windows = "windows",
11+
linux = "linux",
12+
# For example, possibly `sunos`, but very unlikely
13+
"other"
14+
)
315
}
416

517
has_aqua <- function() {
6-
is_macos() && capabilities("aqua")
18+
capabilities("aqua")
719
}
20+
# Here be dragons! On MacOS, this will return `TRUE`, but you won't be able to
21+
# use `png(type = "cairo")` or `svg()` unless xquartz is also installed too,
22+
# i.e. with `brew install --cask xquartz`. So we can't use this as a reliable
23+
# indicator of Cairo graphics support on MacOS.
824
has_cairo <- function() {
925
capabilities("cairo")
1026
}

0 commit comments

Comments
 (0)