Make Fira Sans Condensed use more consistent in ggplot

Change-Id: Ifd3aa8544b0ef828dcfc51ad8d5ce15fa5b33cff
diff --git a/R/ggplot-theme.R b/R/ggplot-theme.R
index 67f06c3..fc562e3 100644
--- a/R/ggplot-theme.R
+++ b/R/ggplot-theme.R
@@ -25,60 +25,69 @@
 #' @importFrom ggplot2 theme element_rect element_text element_line element_blank
 #' @export
 theme_ids <- function(base_size = 10,
-                     base_family = idsBaseFontFamily,
-                     style = c("default", "light", "dark"),
-                     bgcolor = NULL) {
-
+                      base_family = idsBaseFontFamily,
+                      style = c("default", "light", "dark", "mono"),
+                      bgcolor = NULL) {
   if (!is.null(bgcolor)) {
     warning("`bgcolor` is deprecated. Use `style` instead.")
     style <- bgcolor
   }
   style <- match.arg(style)
   bgcolor <- switch(style,
-                    default = backgroundColor,
-                    "light" = backgroundColor,
-                    "mono" = backgroundColor,
-                    "dark" = backgroundColorDark)
+    default = backgroundColor,
+    "light" = backgroundColor,
+    "mono" = backgroundColor,
+    "dark" = backgroundColorDark
+  )
 
-  ret <- theme(rect = element_rect(fill = bgcolor, linetype = 0, colour = NA),
-               text = element_text(size = base_size * 0.8, family = base_family),
-               plot.title = element_text(hjust = 0.5),
-               plot.subtitle = element_text(hjust = 0.5, size = base_size * 0.9, colour = mediumContrastColor),
-               title = element_text(hjust = 0.5),
-               axis.text = element_text(size = base_size * 1.0, family = base_family),
-               axis.title = element_text(size = base_size * 1.0, family = base_family),
-               legend.title = element_text(size = base_size * 1.0, family = base_family, colour = mediumContrastColor),
-               legend.text = element_text(size = base_size * 1.0, family = base_family),
-               strip.text =  element_text(size = base_size * 1.0, family = base_family),
-               axis.title.x = element_text(hjust = 0.5, size = base_size * 1.0,
-                                           colour = mediumContrastColor),
-               axis.title.y = element_text(hjust = 0.5, size = base_size * 1.0,
-                                           margin = margin(r = 10),
-                                           colour = mediumContrastColor),
-               panel.grid.major.x = element_line(linetype = "dotted", colour = lowContrastColor),
-               panel.grid.major.y = element_line(colour = lowContrastColor),
-               panel.grid.minor.y = element_blank(),
-               panel.grid.minor.x = element_blank(),
-               panel.border = element_blank(),
-               panel.background = element_blank(),
-               legend.position = "right",
-               legend.key = element_rect(fill = paste0(bgcolor,"00")))
+  base_family <- ids_resolve_font_family(base_family)
+
+  ret <- theme(
+    rect = element_rect(fill = bgcolor, linetype = 0, colour = NA),
+    text = element_text(size = base_size * 0.8, family = base_family),
+    plot.title = element_text(hjust = 0.5),
+    plot.subtitle = element_text(hjust = 0.5, size = base_size * 0.9, colour = mediumContrastColor),
+    title = element_text(hjust = 0.5),
+    axis.text = element_text(size = base_size * 1.0, family = base_family),
+    axis.title = element_text(size = base_size * 1.0, family = base_family),
+    legend.title = element_text(size = base_size * 1.0, family = base_family, colour = mediumContrastColor),
+    legend.text = element_text(size = base_size * 1.0, family = base_family),
+    strip.text = element_text(size = base_size * 1.0, family = base_family),
+    axis.title.x = element_text(
+      hjust = 0.5, size = base_size * 1.0,
+      colour = mediumContrastColor
+    ),
+    axis.title.y = element_text(
+      hjust = 0.5, size = base_size * 1.0,
+      margin = margin(r = 10),
+      colour = mediumContrastColor
+    ),
+    panel.grid.major.x = element_line(linetype = "dotted", colour = lowContrastColor),
+    panel.grid.major.y = element_line(colour = lowContrastColor),
+    panel.grid.minor.y = element_blank(),
+    panel.grid.minor.x = element_blank(),
+    panel.border = element_blank(),
+    panel.background = element_blank(),
+    legend.position = "right",
+    legend.key = element_rect(fill = paste0(bgcolor, "00"))
+  )
 
   if (style == "dark") {
-    ret <- (ret + theme(rect = element_rect(fill = bgcolor),
-                        text = element_text(colour = textColorDark),
-                        plot.title = element_text(colour = highContrastColorDark),
-                        plot.subtitle = element_text(colour = mediumContrastColorDark),
-                        axis.title.x = element_text(colour = textColorDark),
-                        axis.title.y = element_text(colour = textColorDark),
-                        axis.text.x = element_text(color=textColorDark),
-                        axis.text.y = element_text(color=textColorDark),
-                        panel.grid.major.y = element_line(colour = lowContrastColorDark),
-                        legend.title = element_text(colour = textColorDark)))
+    ret <- (ret + theme(
+      rect = element_rect(fill = bgcolor),
+      text = element_text(colour = textColorDark),
+      plot.title = element_text(colour = highContrastColorDark),
+      plot.subtitle = element_text(colour = mediumContrastColorDark),
+      axis.title.x = element_text(colour = textColorDark),
+      axis.title.y = element_text(colour = textColorDark),
+      axis.text.x = element_text(color = textColorDark),
+      axis.text.y = element_text(color = textColorDark),
+      panel.grid.major.y = element_line(colour = lowContrastColorDark),
+      legend.title = element_text(colour = textColorDark)
+    ))
   }
   update_geom_defaults("text", list(size = 3, family = base_family))
   ret
-
 }
 
 
@@ -99,7 +108,7 @@
 #' @rdname scale_ids
 #' @export
 scale_colour_ids <- function(palette = "default", ...) {
-  discrete_scale("colour", "ids", ids_pal(palette=palette, ...))
+  discrete_scale("colour", "ids", ids_pal(palette = palette, ...))
 }
 
 #' @rdname scale_ids
@@ -113,5 +122,5 @@
 #' @importFrom ggplot2 discrete_scale
 #' @export
 scale_fill_ids <- function(palette = "default", ...) {
-  discrete_scale("fill", "ids", ids_pal(palette=palette, ...))
+  discrete_scale("fill", "ids", ids_pal(palette = palette, ...))
 }
diff --git a/R/idsThemeR.R b/R/idsThemeR.R
index bbe510b..d370628 100644
--- a/R/idsThemeR.R
+++ b/R/idsThemeR.R
@@ -1,4 +1,34 @@
-idsBaseFontFamily <- 'Fira Sans Condensed'
+idsBaseFontFamily <- "Fira Sans Condensed"
+
+ids_font_available <- function(font_family) {
+  if (!requireNamespace("systemfonts", quietly = TRUE)) {
+    return(FALSE)
+  }
+  info <- tryCatch(systemfonts::font_info(font_family), error = function(e) NULL)
+  !is.null(info) && nrow(info) > 0
+}
+
+ids_resolve_font_family <- function(preferred = idsBaseFontFamily, fallback = "sans") {
+  candidates <- unique(stats::na.omit(c(preferred, idsBaseFontFamily, fallback)))
+  available <- vapply(candidates, ids_font_available, logical(1))
+  if (any(available)) {
+    return(candidates[which(available)[1]])
+  }
+
+  if (requireNamespace("sysfonts", quietly = TRUE)) {
+    try(sysfonts::font_add_google("Fira Sans Condensed", "Fira Sans Condensed"), silent = TRUE)
+    if (requireNamespace("showtext", quietly = TRUE)) {
+      try(showtext::showtext_auto(enable = TRUE), silent = TRUE)
+    }
+    candidates_after <- unique(c("Fira Sans Condensed", candidates))
+    available_after <- vapply(candidates_after, ids_font_available, logical(1))
+    if (any(available_after)) {
+      return(candidates_after[which(available_after)[1]])
+    }
+  }
+
+  fallback
+}
 
 
 #' Title
@@ -9,27 +39,27 @@
 #' @examples
 idsPaletteIDS <- function() {
   c(
-  '#F18700',
-  "#1E2662",
-  "#6A7702",
-  "#C10A25",
-  "#009EE3",
-  "#FAF3DE",
-  "#A7C2D2",
-  "#E2E8A4",
-  "#CFBCBD",
-  "#BCE3FA",
-  "#5C401C",
-  "#000000",
-  "#354C32",
-  "#5C131C",
-  "#273582",
-  "#FFE3BF",
-  "#8994E1",
-  "#A3B29E",
-  "#DF9599",
-  "#79B8DB"
-)
+    "#F18700",
+    "#1E2662",
+    "#6A7702",
+    "#C10A25",
+    "#009EE3",
+    "#FAF3DE",
+    "#A7C2D2",
+    "#E2E8A4",
+    "#CFBCBD",
+    "#BCE3FA",
+    "#5C401C",
+    "#000000",
+    "#354C32",
+    "#5C131C",
+    "#273582",
+    "#FFE3BF",
+    "#8994E1",
+    "#A3B29E",
+    "#DF9599",
+    "#79B8DB"
+  )
 }
 
 #' Title
@@ -40,27 +70,27 @@
 #' @examples
 idsPaletteD3 <- function() {
   c(
-  '#EB7C31',
-  "#1F77B4",
-  "#2CA02C",
-  "#D62728",
-  "#9467BD",
-  "#8C564B",
-  "#E377C2",
-  "#7F7F7F",
-  "#BCBD22",
-  "#17BECF",
-  "#AEC7E8",
-  "#FFBB78",
-  "#98DF8A",
-  "#FF9896",
-  "#C5B0D5",
-  "#C49C94",
-  "#F7B6D2",
-  "#C7C7C7",
-  "#DBDB8D",
-  "#9EDAE5"
-)
+    "#EB7C31",
+    "#1F77B4",
+    "#2CA02C",
+    "#D62728",
+    "#9467BD",
+    "#8C564B",
+    "#E377C2",
+    "#7F7F7F",
+    "#BCBD22",
+    "#17BECF",
+    "#AEC7E8",
+    "#FFBB78",
+    "#98DF8A",
+    "#FF9896",
+    "#C5B0D5",
+    "#C49C94",
+    "#F7B6D2",
+    "#C7C7C7",
+    "#DBDB8D",
+    "#9EDAE5"
+  )
 }
 
 #' Title
@@ -84,7 +114,7 @@
 #' @examples
 idsPaletteGreyscale <- function() {
   c(
-  "#000000", "#B2B0AD", "#737373", "#D8D7D6", "#B2B0AD", "#8C8984"
+    "#000000", "#B2B0AD", "#737373", "#D8D7D6", "#B2B0AD", "#8C8984"
   )
 }
 
@@ -125,13 +155,13 @@
 #'
 #' \url{https://github.com/d3/d3}
 #'
-ids_pal <- function(palette="default") {
+ids_pal <- function(palette = "default") {
   values <- switch(tolower(palette),
-                   ids = idsPaletteIDS(),
-                   gray = idsPaletteGreyscale(),
-                   grey = idsPaletteGreyscale(),
-                   poly20 = idsPalettePoly20(),
-                   idsPaletteD3()
+    ids = idsPaletteIDS(),
+    gray = idsPaletteGreyscale(),
+    grey = idsPaletteGreyscale(),
+    poly20 = idsPalettePoly20(),
+    idsPaletteD3()
   )
   f <- scales::manual_pal(values)
   attr(f, "max_n") <- length(values)