😎 Give your xaringan slides some style
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 line
3.8KB

  1. # Make sure font names are wrapped in quotes if they have spaces
  2. f_args <- names(formals(sys.function()))
  3. for (var in f_args[grepl("font_family$", f_args)]) {
  4. var_value <- get(var, inherits = FALSE)
  5. if (!is.null(var_value)) {
  6. eval(parse(text = paste0(var, "<-quote_elements_w_spaces(", var, ")")))
  7. }
  8. # set an is_google flag default of FALSE that is possibly overwritten later
  9. eval(parse(text = paste0(sub("font_family$", "font_is_google", var), "<-0")))
  10. }
  11. # Warn if base_font_size isn't absolute
  12. css_abs_units <- c("cm", "mm", "Q", "in", "pc", "pt", "px")
  13. if (!grepl(paste(tolower(css_abs_units), collapse = "|"), tolower(base_font_size))) {
  14. warning(
  15. glue::glue(
  16. "Base font size '{base_font_size}' is not in absolute units. ",
  17. "For best results, specify the `base_font_size` using absolute CSS units: ",
  18. "{paste(css_abs_units, collapse = ', ')}"
  19. ),
  20. call. = FALSE,
  21. immediate. = TRUE
  22. )
  23. }
  24. # Use font_..._google args to overwrite font args
  25. for (var in f_args[grepl("font_google$", f_args)]) {
  26. gf <- eval(parse(text = var))
  27. if (is.null(gf)) next
  28. if (!inherits(gf, "google_font")) {
  29. stop("`", var, "` must be set using `google_font()`.")
  30. }
  31. group <- strsplit(var, "_")[[1]][1]
  32. if (group == "text") {
  33. text_font_family <- quote_elements_w_spaces(gf$family)
  34. text_font_weight <- gf$weights %||% "normal"
  35. text_font_weight <- substr(text_font_weight, 1, regexpr(",", text_font_weight)[1] - 1)
  36. text_font_url <- gf$url
  37. } else {
  38. eval(parse(text = paste0(group, "_font_family <- quote_elements_w_spaces(gf$family)")))
  39. eval(parse(text = paste0(group, "_font_url <- gf$url")))
  40. }
  41. eval(parse(text = paste0(group, "_font_is_google <- 1")))
  42. }
  43. extra_font_imports <- if (is.null(extra_fonts)) "" else list2fonts(extra_fonts)
  44. extra_font_imports <- paste(extra_font_imports, collapse = "\n")
  45. # convert NA arguments to NULL
  46. for (var in f_args) {
  47. val <- eval(parse(text = var))
  48. if (is.null(val)) next
  49. val <- val[!is.na(val)]
  50. is_na <- length(val) == 0
  51. if (is_na) assign(var, NULL)
  52. }
  53. # prepare variables for template
  54. body_font_family <- paste(c(text_font_family, text_font_family_fallback, text_font_base), collapse = ", ")
  55. background_size_fallback <- if (is.null(background_position)) "cover" else "100%"
  56. background_size <- background_image %??% (background_size %||% background_size_fallback)
  57. title_slide_background_size <- title_slide_background_size %||% (
  58. title_slide_background_image %??% "cover"
  59. )
  60. table_row_even_background_color <- table_row_even_background_color %||% background_color
  61. lapply(names(formals()), function(n) assign(n, get(n), envir = xaringanthemer_env))
  62. xaringanthemer_version <- utils::packageVersion("xaringanthemer")
  63. # prepare header background object
  64. needs_leading_dot <- !grepl("^\\.", header_background_ignore_classes)
  65. header_background_ignore_classes[needs_leading_dot] <- paste0(
  66. ".",
  67. header_background_ignore_classes[needs_leading_dot]
  68. )
  69. header_background_ignore_classes <- purrr::map(
  70. header_background_ignore_classes,
  71. ~ list(class = .)
  72. )
  73. if (is.null(header_background_padding)) {
  74. slide_padding <- css_get_padding(padding)
  75. header_background_padding <- paste(
  76. "2rem", slide_padding$right, "1.5rem", slide_padding$left
  77. )
  78. }
  79. header_background <- list(
  80. auto = header_background_auto,
  81. background_color = header_background_color,
  82. text_color = header_background_text_color,
  83. padding = header_background_padding,
  84. content_padding_top = header_background_content_padding_top,
  85. ignore = header_background_ignore_classes
  86. )
  87. colors <- prepare_colors(colors)
  88. tf <- system.file("resources", "template.css", package = "xaringanthemer")
  89. template <- readLines(tf, warn = FALSE)
  90. template <- paste(template, collapse = "\n")
  91. x <- whisker::whisker.render(template)
  92. writeLines(x, con = outfile)
  93. if (!is.null(extra_css)) style_extra_css(extra_css, outfile)
  94. outfile