😎 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.

267 satır
16KB

  1. #' @param text_color Text Color. Defaults to #000. Modifies the `body` element.
  2. #' @param header_color Header Color. Defaults to #000. Modifies the `h1, h2, h3` elements.
  3. #' @param background_color Slide Background Color. Defaults to #FFF. Modifies the `.remark-slide-content` class.
  4. #' @param link_color Link Color. Defaults to rgb(249, 38, 114). Modifies the `a, a > code` elements.
  5. #' @param text_bold_color Bold Text Color. Defaults to `NULL`. Modifies the `strong` element.
  6. #' @param text_slide_number_color Slide Number Color. Defaults to `inverse_background_color`. Modifies the `.remark-slide-number` class.
  7. #' @param padding Slide Padding in `top right [bottom left]` format. Defaults to 16px 64px 16px 64px. Modifies the `.remark-slide-content` class.
  8. #' @param background_image Background image applied to each *and every* slide. Set `title_slide_background_image = "none"` to remove the background image from the title slide. Defaults to `NULL`. Modifies the `.remark-slide-content` class.
  9. #' @param background_size Background image size, requires `background_image` to be set. If `background_image` is set, `background_size` will default to `cover` so the backround fills the screen. If both `background_image` and `background_position` are set, will default to 100 percent. Defaults to `NULL`. Modifies the `.remark-slide-content` class.
  10. #' @param background_position Background image position, requires `background_image` to be set, and it is recommended to adjust `background_size`. Defaults to `NULL`. Modifies the `.remark-slide-content` class.
  11. #' @param code_highlight_color Code Line Highlight. Defaults to rgba(255,255,0,0.5). Modifies the `.remark-code-line-highlighted` class.
  12. #' @param code_inline_color Inline Code Color. Defaults to #000. Modifies the `.remark-inline-code` class.
  13. #' @param code_inline_background_color Inline Code Background Color. Defaults to `NULL`. Modifies the `.remark-inline-code` class.
  14. #' @param code_inline_font_size Inline Code Text Font Size. Defaults to 1rem. Modifies the `.remark-inline-code` class.
  15. #' @param inverse_background_color Inverse Background Color. Defaults to #272822. Modifies the `.inverse` class.
  16. #' @param inverse_text_color Inverse Text Color. Defaults to #d6d6d6. Modifies the `.inverse` class.
  17. #' @param inverse_text_shadow Enables Shadow on text of inverse slides. Defaults to `FALSE`. Modifies the `.inverse` class.
  18. #' @param inverse_header_color Inverse Header Color. Defaults to #f3f3f3. Modifies the `.inverse h1, .inverse h2, .inverse h3` classes.
  19. #' @param title_slide_text_color Title Slide Text Color. Defaults to `inverse_text_color`. Modifies the `.title-slide` class.
  20. #' @param title_slide_background_color Title Slide Background Color. Defaults to `inverse_background_color`. Modifies the `.title-slide` class.
  21. #' @param title_slide_background_image Title Slide Background Image URL. Defaults to `NULL`. Modifies the `.title-slide` class.
  22. #' @param title_slide_background_size Title Slide Background Image Size, defaults to "cover" if background image is set. Defaults to `NULL`. Modifies the `.title-slide` class.
  23. #' @param title_slide_background_position Title Slide Background Image Position. Defaults to `NULL`. Modifies the `.title-slide` class.
  24. #' @param footnote_color Footnote text color (if `NA`, then it will be the same color as `text_color`). Defaults to `NULL`. Modifies the `.footnote` class.
  25. #' @param footnote_font_size Footnote font size. Defaults to 0.9rem. Modifies the `.footnote` class.
  26. #' @param footnote_position_bottom Footnote location from bottom of screen. Defaults to 60px. Modifies the `.footnote` class.
  27. #' @param left_column_subtle_color Left Column Text (not last). Defaults to #777. Modifies the `.left-column h2, .left-column h3` classes.
  28. #' @param left_column_selected_color Left Column Current Selection. Defaults to #000. Modifies the `.left-column h2:last-of-type, .left-column h3:last-child` classes.
  29. #' @param blockquote_left_border_color Blockquote Left Border Color. Defaults to lightgray. Modifies the `blockquote` element.
  30. #' @param table_border_color Table top/bottom border. Defaults to #666. Modifies the `table: border-top, border-bottom` elements.
  31. #' @param table_row_border_color Table row inner bottom border. Defaults to #ddd. Modifies the `table thead th: border-bottom` elements.
  32. #' @param table_row_even_background_color Table Even Row Background Color. Defaults to #eee. Modifies the `thead, tfoot, tr:nth-child(even)` elements.
  33. #' @param base_font_size Base Font Size for All Slide Elements (must be `px`). Defaults to 20px. Modifies the `html` element.
  34. #' @param text_font_size Slide Body Text Font Size. Defaults to 1rem. Modifies the `.remark-slide-content` class.
  35. #' @param header_h1_font_size h1 Header Text Font Size. Defaults to 2.75rem. Modifies the `.remark-slide-content h1` class.
  36. #' @param header_h2_font_size h2 Header Text Font Size. Defaults to 2.25rem. Modifies the `.remark-slide-content h2` class.
  37. #' @param header_h3_font_size h3 Header Text Font Size. Defaults to 1.75rem. Modifies the `.remark-slide-content h3` class.
  38. #' @param header_background_auto Add background under slide title automatically for h1 header elements. If not enabled, use `class: header_background` to enable. Defaults to `FALSE`.
  39. #' @param header_background_color Background Color for h1 Header with Background. Defaults to `header_color`. Modifies the `.remark-slide-content h1` class.
  40. #' @param header_background_text_color Text Color for h1 Header with Background. Defaults to `background_color`. Modifies the `.remark-slide-content h1` class.
  41. #' @param header_background_padding Padding for h1 Header with Background. Defaults to `NULL`. Modifies the `.remark-slide-content h1` class.
  42. #' @param header_background_content_padding_top Top Padding for Content in Slide with Header with Background. Defaults to 7rem. Modifies the `.remark-slide-content` class.
  43. #' @param header_background_ignore_classes Slide Classes Where Header with Background will not be Applied. Defaults to `c('normal', 'inverse', 'title', 'middle', 'bottom')`. Modifies the `.remark-slide-content` class.
  44. #' @param text_slide_number_font_size Slide Number Text Font Size. Defaults to 0.9rem. Modifies the `.remark-slide-number` class.
  45. #' @param text_font_google Use `google_font()` to specify body font. Defaults to `NULL`. Modifies the `body` element.
  46. #' @param text_font_family Body Text Font Family (xaringan default is `'Droid Serif'`). Defaults to Noto Sans. Modifies the `body` element.
  47. #' @param text_font_weight Body Text Font Weight. Defaults to normal. Modifies the `body` element.
  48. #' @param text_font_url Body Text Font URL(s). Defaults to https://fonts.googleapis.com/css?family=Noto+Sans:400,400i,700,700i&display=swap. Modifies the `@import url()` elements.
  49. #' @param text_font_family_fallback Body Text Font Fallbacks. Defaults to -apple-system, BlinkMacSystemFont, avenir next, avenir, helvetica neue, helvetica, Ubuntu, roboto, noto, segoe ui, arial. Modifies the `body` element.
  50. #' @param text_font_base Body Text Base Font (Total Failure Fallback). Defaults to sans-serif. Modifies the `body` element.
  51. #' @param header_font_google Use `google_font()` to specify header font. Defaults to `NULL`. Modifies the `body` element.
  52. #' @param header_font_family Header Font Family (xaringan default is `'Yanone Kaffeesatz'`). Defaults to Cabin. Modifies the `h1, h2, h3` elements.
  53. #' @param header_font_weight Header Font Weight. Defaults to 600. Modifies the `h1, h2, h3` elements.
  54. #' @param header_font_url Header Font URL. Defaults to https://fonts.googleapis.com/css?family=Cabin:600,600i&display=swap. Modifies the `@import url` elements.
  55. #' @param code_font_google Use `google_font()` to specify code font. Defaults to `NULL`. Modifies the `body` element.
  56. #' @param code_font_family Code Font Family. Defaults to Source Code Pro. Modifies the `.remark-code, .remark-inline-code` classes.
  57. #' @param code_font_size Code Text Font Size. Defaults to 0.9rem. Modifies the `.remark-inline` class.
  58. #' @param code_font_url Code Font URL. Defaults to https://fonts.googleapis.com/css?family=Source+Code+Pro:400,700&display=swap. Modifies the `@import url` elements.
  59. #' @param code_font_family_fallback Code Font Fallback. Defaults to Menlo, Consolas, Monaco, Liberation Mono, Lucida Console. Modifies the `.remark-code, .remark-inline-code` classes.
  60. #' @template theme_params
  61. #' @template style_xaringan
  62. #' @export
  63. style_xaringan <- function(
  64. text_color = "#000",
  65. header_color = "#000",
  66. background_color = "#FFF",
  67. link_color = "rgb(249, 38, 114)",
  68. text_bold_color = NULL,
  69. text_slide_number_color = inverse_background_color,
  70. padding = "16px 64px 16px 64px",
  71. background_image = NULL,
  72. background_size = NULL,
  73. background_position = NULL,
  74. code_highlight_color = "rgba(255,255,0,0.5)",
  75. code_inline_color = "#000",
  76. code_inline_background_color = NULL,
  77. code_inline_font_size = "1rem",
  78. inverse_background_color = "#272822",
  79. inverse_text_color = "#d6d6d6",
  80. inverse_text_shadow = FALSE,
  81. inverse_header_color = "#f3f3f3",
  82. title_slide_text_color = inverse_text_color,
  83. title_slide_background_color = inverse_background_color,
  84. title_slide_background_image = NULL,
  85. title_slide_background_size = NULL,
  86. title_slide_background_position = NULL,
  87. footnote_color = NULL,
  88. footnote_font_size = "0.9rem",
  89. footnote_position_bottom = "60px",
  90. left_column_subtle_color = "#777",
  91. left_column_selected_color = "#000",
  92. blockquote_left_border_color = "lightgray",
  93. table_border_color = "#666",
  94. table_row_border_color = "#ddd",
  95. table_row_even_background_color = "#eee",
  96. base_font_size = "20px",
  97. text_font_size = "1rem",
  98. header_h1_font_size = "2.75rem",
  99. header_h2_font_size = "2.25rem",
  100. header_h3_font_size = "1.75rem",
  101. header_background_auto = FALSE,
  102. header_background_color = header_color,
  103. header_background_text_color = background_color,
  104. header_background_padding = NULL,
  105. header_background_content_padding_top = "7rem",
  106. header_background_ignore_classes = c('normal', 'inverse', 'title', 'middle', 'bottom'),
  107. text_slide_number_font_size = "0.9rem",
  108. text_font_google = NULL,
  109. text_font_family = "Noto Sans",
  110. text_font_weight = "normal",
  111. text_font_url = "https://fonts.googleapis.com/css?family=Noto+Sans:400,400i,700,700i&display=swap",
  112. text_font_family_fallback = "-apple-system, BlinkMacSystemFont, avenir next, avenir, helvetica neue, helvetica, Ubuntu, roboto, noto, segoe ui, arial",
  113. text_font_base = "sans-serif",
  114. header_font_google = NULL,
  115. header_font_family = "Cabin",
  116. header_font_weight = "600",
  117. header_font_url = "https://fonts.googleapis.com/css?family=Cabin:600,600i&display=swap",
  118. code_font_google = NULL,
  119. code_font_family = "Source Code Pro",
  120. code_font_size = "0.9rem",
  121. code_font_url = "https://fonts.googleapis.com/css?family=Source+Code+Pro:400,700&display=swap",
  122. code_font_family_fallback = "Menlo, Consolas, Monaco, Liberation Mono, Lucida Console",
  123. colors = NULL,
  124. extra_css = NULL,
  125. extra_fonts = NULL,
  126. outfile = "xaringan-themer.css"
  127. ) {
  128. # DO NOT EDIT - Generated from inst/scripts/generate_theme_functions.R
  129. # Make sure font names are wrapped in quotes if they have spaces
  130. f_args <- names(formals(sys.function()))
  131. for (var in f_args[grepl("font_family$", f_args)]) {
  132. var_value <- get(var, inherits = FALSE)
  133. if (!is.null(var_value)) {
  134. eval(parse(text = paste0(var, "<-quote_elements_w_spaces(", var, ")")))
  135. }
  136. # set an is_google flag default of FALSE that is possibly overwritten later
  137. eval(parse(text = paste0(sub("font_family$", "font_is_google", var), "<-0")))
  138. }
  139. # Warn if base_font_size isn't absolute
  140. css_abs_units <- c("cm", "mm", "Q", "in", "pc", "pt", "px")
  141. if (!grepl(paste(tolower(css_abs_units), collapse = "|"), tolower(base_font_size))) {
  142. warning(
  143. glue::glue(
  144. "Base font size '{base_font_size}' is not in absolute units. ",
  145. "For best results, specify the `base_font_size` using absolute CSS units: ",
  146. "{paste(css_abs_units, collapse = ', ')}"
  147. ),
  148. call. = FALSE,
  149. immediate. = TRUE
  150. )
  151. }
  152. # Use font_..._google args to overwrite font args
  153. for (var in f_args[grepl("font_google$", f_args)]) {
  154. gf <- eval(parse(text = var))
  155. if (is.null(gf)) next
  156. if (!inherits(gf, "google_font")) {
  157. stop("`", var, "` must be set using `google_font()`.")
  158. }
  159. group <- strsplit(var, "_")[[1]][1]
  160. if (group == "text") {
  161. text_font_family <- quote_elements_w_spaces(gf$family)
  162. text_font_weight <- gf$weights %||% "normal"
  163. text_font_weight <- substr(text_font_weight, 1, regexpr(",", text_font_weight)[1] - 1)
  164. text_font_url <- gf$url
  165. } else {
  166. eval(parse(text = paste0(group, "_font_family <- quote_elements_w_spaces(gf$family)")))
  167. eval(parse(text = paste0(group, "_font_url <- gf$url")))
  168. }
  169. eval(parse(text = paste0(group, "_font_is_google <- 1")))
  170. }
  171. is_default <- function(type, suffix, reference = style_xaringan) {
  172. var <- paste0(type, "_", suffix)
  173. default_value <- formals(reference)[[var]]
  174. if (suffix == "font_family") {
  175. default_value <- quote_elements_w_spaces(default_value)
  176. }
  177. get(var, envir = parent.frame(2), inherits = FALSE) == default_value
  178. }
  179. # the defaults are google fonts
  180. for (var in c("text", "header", "code")) {
  181. suffixes <- c("font_family", "font_weight", "font_url")
  182. if (var == "code") suffixes <- setdiff(suffixes, "font_weight")
  183. var_is_google <- all(vapply(suffixes, is_default, logical(1), type = var))
  184. if (var_is_google) {
  185. eval(parse(text = paste0(var, "_font_is_google <- 1")))
  186. }
  187. }
  188. extra_font_imports <- if (is.null(extra_fonts)) "" else list2fonts(extra_fonts)
  189. extra_font_imports <- paste(extra_font_imports, collapse = "\n")
  190. # convert NA arguments to NULL
  191. for (var in f_args) {
  192. val <- eval(parse(text = var))
  193. if (is.null(val)) next
  194. val <- val[!is.na(val)]
  195. is_na <- length(val) == 0
  196. if (is_na) assign(var, NULL)
  197. }
  198. # prepare variables for template
  199. body_font_family <- paste(c(text_font_family, text_font_family_fallback, text_font_base), collapse = ", ")
  200. background_size_fallback <- if (is.null(background_position)) "cover" else "100%"
  201. background_size <- background_image %??% (background_size %||% background_size_fallback)
  202. title_slide_background_size <- title_slide_background_size %||% (
  203. title_slide_background_image %??% "cover"
  204. )
  205. table_row_even_background_color <- table_row_even_background_color %||% background_color
  206. # stash theme settings in package env
  207. lapply(f_args, function(n) assign(n, get(n), envir = xaringanthemer_env))
  208. for (font_is_google in paste0(c("text", "code", "header"), "_font_is_google")) {
  209. assign(
  210. font_is_google,
  211. get(font_is_google, inherits = FALSE) == 1,
  212. envir = xaringanthemer_env
  213. )
  214. }
  215. xaringanthemer_version <- utils::packageVersion("xaringanthemer")
  216. # prepare header background object
  217. needs_leading_dot <- !grepl("^\\.", header_background_ignore_classes)
  218. header_background_ignore_classes[needs_leading_dot] <- paste0(
  219. ".",
  220. header_background_ignore_classes[needs_leading_dot]
  221. )
  222. header_background_ignore_classes <- purrr::map(
  223. header_background_ignore_classes,
  224. ~ list(class = .)
  225. )
  226. if (is.null(header_background_padding)) {
  227. slide_padding <- css_get_padding(padding)
  228. header_background_padding <- paste(
  229. "2rem", slide_padding$right, "1.5rem", slide_padding$left
  230. )
  231. }
  232. header_background <- list(
  233. auto = header_background_auto,
  234. background_color = header_background_color,
  235. text_color = header_background_text_color,
  236. padding = header_background_padding,
  237. content_padding_top = header_background_content_padding_top,
  238. ignore = header_background_ignore_classes
  239. )
  240. colors <- prepare_colors(colors)
  241. tf <- system.file("resources", "template.css", package = "xaringanthemer")
  242. template <- readLines(tf, warn = FALSE)
  243. template <- paste(template, collapse = "\n")
  244. x <- whisker::whisker.render(template)
  245. if (!is.null(extra_css)) {
  246. x <- c(x, style_extra_css(extra_css, outfile = NULL))
  247. }
  248. if (is.null(outfile)) {
  249. return(x)
  250. }
  251. writeLines(x, con = outfile)
  252. invisible(outfile)
  253. }