😎 Give your xaringan slides some style
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

125 lines
4.5KB

  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. is_default <- function(type, suffix, reference = style_xaringan) {
  44. var <- paste0(type, "_", suffix)
  45. default_value <- formals(reference)[[var]]
  46. if (suffix == "font_family") {
  47. default_value <- quote_elements_w_spaces(default_value)
  48. }
  49. get(var, envir = parent.frame(2), inherits = FALSE) == default_value
  50. }
  51. # the defaults are google fonts
  52. for (var in c("text", "header", "code")) {
  53. suffixes <- c("font_family", "font_weight", "font_url")
  54. if (var == "code") suffixes <- setdiff(suffixes, "font_weight")
  55. var_is_google <- all(vapply(suffixes, is_default, logical(1), type = var))
  56. if (var_is_google) {
  57. eval(parse(text = paste0(var, "_font_is_google <- 1")))
  58. }
  59. }
  60. extra_font_imports <- if (is.null(extra_fonts)) "" else list2fonts(extra_fonts)
  61. extra_font_imports <- paste(extra_font_imports, collapse = "\n")
  62. # convert NA arguments to NULL
  63. for (var in f_args) {
  64. val <- eval(parse(text = var))
  65. if (is.null(val)) next
  66. val <- val[!is.na(val)]
  67. is_na <- length(val) == 0
  68. if (is_na) assign(var, NULL)
  69. }
  70. # prepare variables for template
  71. body_font_family <- paste(c(text_font_family, text_font_family_fallback, text_font_base), collapse = ", ")
  72. background_size_fallback <- if (is.null(background_position)) "cover" else "100%"
  73. background_size <- background_image %??% (background_size %||% background_size_fallback)
  74. title_slide_background_size <- title_slide_background_size %||% (
  75. title_slide_background_image %??% "cover"
  76. )
  77. table_row_even_background_color <- table_row_even_background_color %||% background_color
  78. lapply(names(formals()), function(n) assign(n, get(n), envir = xaringanthemer_env))
  79. xaringanthemer_version <- utils::packageVersion("xaringanthemer")
  80. # prepare header background object
  81. needs_leading_dot <- !grepl("^\\.", header_background_ignore_classes)
  82. header_background_ignore_classes[needs_leading_dot] <- paste0(
  83. ".",
  84. header_background_ignore_classes[needs_leading_dot]
  85. )
  86. header_background_ignore_classes <- purrr::map(
  87. header_background_ignore_classes,
  88. ~ list(class = .)
  89. )
  90. if (is.null(header_background_padding)) {
  91. slide_padding <- css_get_padding(padding)
  92. header_background_padding <- paste(
  93. "2rem", slide_padding$right, "1.5rem", slide_padding$left
  94. )
  95. }
  96. header_background <- list(
  97. auto = header_background_auto,
  98. background_color = header_background_color,
  99. text_color = header_background_text_color,
  100. padding = header_background_padding,
  101. content_padding_top = header_background_content_padding_top,
  102. ignore = header_background_ignore_classes
  103. )
  104. colors <- prepare_colors(colors)
  105. tf <- system.file("resources", "template.css", package = "xaringanthemer")
  106. template <- readLines(tf, warn = FALSE)
  107. template <- paste(template, collapse = "\n")
  108. x <- whisker::whisker.render(template)
  109. writeLines(x, con = outfile)
  110. if (!is.null(extra_css)) style_extra_css(extra_css, outfile)
  111. outfile