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

683 lines
22KB

  1. #' A Plot Theme for ggplot2 by xaringanthemer
  2. #'
  3. #' Creates [ggplot2] themes to match the xaringanthemer theme used in the
  4. #' [xaringan] slides that seamlessly matches the "normal" slide colors and
  5. #' styles.
  6. #'
  7. #' @param text_color Color for text and foreground, inherits from `text_color`
  8. #' @param background_color Color for background, inherits from
  9. #' `background_color`
  10. #' @param accent_color Color for titles and accents, inherits from
  11. #' `header_color`
  12. #' @param accent_secondary_color Color for secondary accents, inherits from
  13. #' `text_bold_color`
  14. #' @inheritDotParams theme_xaringan_base
  15. #'
  16. #' @examples
  17. #' if (requireNamespace("ggplot2", quietly = TRUE)) {
  18. #' # Set xaringanthemer theme but save to tempfile
  19. #' style_duo_accent(outfile = tempfile())
  20. #'
  21. #' library(ggplot2)
  22. #' ggplot(iris) +
  23. #' aes(Petal.Length, Petal.Width) +
  24. #' geom_point() +
  25. #' theme_xaringan()
  26. #' }
  27. #'
  28. #' @return A ggplot2 theme
  29. #' @family xaringanthemer ggplot2 themes
  30. #' @export
  31. theme_xaringan <- function(
  32. text_color = NULL,
  33. background_color = NULL,
  34. accent_color = NULL,
  35. accent_secondary_color = NULL,
  36. ...
  37. ) {
  38. requires_xaringanthemer_env()
  39. requires_package(fn = "xaringan_theme")
  40. background_color <- background_color %||% xaringanthemer_env$background_color
  41. text_color <- text_color %||% xaringanthemer_env$text_color
  42. accent_color <- accent_color %||% xaringanthemer_env$header_color
  43. accent_secondary_color <- accent_secondary_color %||% xaringanthemer_env$text_bold_color %||% accent_color
  44. theme_xaringan_base(text_color, background_color,
  45. accent_color = accent_color,
  46. accent_secondary_color = accent_secondary_color,
  47. ...)
  48. }
  49. #' An Inverse Plot Theme for ggplot2 by xaringanthemer
  50. #'
  51. #' A [ggplot2] xaringanthemer plot theme to seamlessly match the "inverse"
  52. #' [xaringan] slide colors and styles as styled by [xaringanthemer].
  53. #'
  54. #' @param text_color Color for text and foreground, inherits from `text_color`
  55. #' @param background_color Color for background, inherits from
  56. #' `background_color`
  57. #' @param accent_color Color for titles and accents, inherits from
  58. #' `header_color`
  59. #' @param accent_secondary_color Color for secondary accents, inherits from
  60. #' `text_bold_color`
  61. #' @inheritDotParams theme_xaringan_base
  62. #'
  63. #' @examples
  64. #' if (requireNamespace("ggplot2", quietly = TRUE)) {
  65. #' # Set xaringanthemer theme but save to tempfile
  66. #' style_duo_accent(outfile = tempfile())
  67. #'
  68. #' library(ggplot2)
  69. #' ggplot(iris) +
  70. #' aes(Petal.Length, Petal.Width) +
  71. #' geom_point() +
  72. #' theme_xaringan()
  73. #' }
  74. #'
  75. #' @return A ggplot2 theme
  76. #' @family xaringanthemer ggplot2 themes
  77. #' @export
  78. theme_xaringan_inverse <- function(
  79. text_color = NULL,
  80. background_color = NULL,
  81. accent_color = NULL,
  82. accent_secondary_color = NULL,
  83. ...
  84. ) {
  85. requires_xaringanthemer_env()
  86. requires_package(fn = "xaringan_theme")
  87. background_color <- background_color %||% xaringanthemer_env$inverse_background_color
  88. text_color <- text_color %||% xaringanthemer_env$inverse_text_color
  89. accent_color <- accent_color %||% xaringanthemer_env$inverse_header_color
  90. accent_secondary_color <- accent_secondary_color %||% accent_color
  91. theme_xaringan_base(text_color, background_color,
  92. accent_color = accent_color,
  93. accent_secondary_color = accent_secondary_color,
  94. ...)
  95. }
  96. #' The ggplot2 xaringanthemer base plot theme
  97. #'
  98. #' Provides a base plot theme for [ggplot2] to match the [xaringan] slide theme
  99. #' created by [xaringanthemer]. The theme is designed to create a general plot
  100. #' style from two colors, a `background_color` and a `text_color` (or foreground
  101. #' color). Also accepts an `accent_color` and an `accent_secondary_color` that are
  102. #' [xaringanthemer] is not required for the base theme. Use
  103. #' [theme_xaringan()] or [theme_xaringan_inverse()] in xaringan slides styled by
  104. #' xaringanthemer for a plot theme that matches the slide style.
  105. #'
  106. #' @param text_color Color for text and foreground
  107. #' @param background_color Color for background
  108. #' @param accent_color Color for titles and accents, inherits from
  109. #' `header_color` or `text_color`. Used for the `title` base setting in
  110. #' [ggplot2::theme()], and additionally for setting the `color` or `fill` of
  111. #' [ggplot2] geom defaults.
  112. #' @param accent_secondary_color Color for secondary accents, inherits from
  113. #' `text_bold_color` or `accent_color`. Used only when setting [ggplot2] geom
  114. #' defaults.
  115. #' @param set_ggplot_defaults Should defaults be set for [ggplot2] _geoms_?
  116. #' Defaults to TRUE. To restore ggplot's defaults, or the previously set geom
  117. #' defaults, see [theme_xaringan_restore_defaults()].
  118. #' @param text_font Font to use for text elements, passed to
  119. #' [sysfonts::font_add_google()], if available and `text_font_use_google` is
  120. #' `TRUE`. Inherits from `text_font_family`.
  121. #' @param text_font_use_google Is `text_font` available on [Google
  122. #' Fonts](https://fonts.google.com)?
  123. #' @param text_font_size Base text font size, inherits from `text_font_size`, or
  124. #' defaults to 11.
  125. #' @param title_font Font to use for title elements, passed to
  126. #' [sysfonts::font_add_google()], if available and `title_font_use_google` is
  127. #' `TRUE`. Inherits from `title_font_family`.
  128. #' @param title_font_use_google Is `title_font` available on [Google
  129. #' Fonts](https://fonts.google.com)?
  130. #' @param title_font_size Base text font size, inherits from `title_font_size`,
  131. #' or defaults to 14.
  132. #' @param ... Ignored
  133. #'
  134. #' @examples
  135. #' if (requireNamespace("ggplot2", quietly = TRUE)) {
  136. #' library(ggplot2)
  137. #' ggplot(iris) +
  138. #' aes(Petal.Length, Petal.Width) +
  139. #' geom_point() +
  140. #' theme_xaringan_base(
  141. #' text_color = "#e1e5f2",
  142. #' background_color = "#021c25",
  143. #' accent_color = "#1f7a8c",
  144. #' set_ggplot_defaults = TRUE) +
  145. #' labs(title = "Basic Iris Plot",
  146. #' subtitle = "+ theme_xaringan_base()",
  147. #' caption = "{xaringanthemer}")
  148. #'
  149. #' ggplot(iris) +
  150. #' aes(Petal.Length, Petal.Width) +
  151. #' geom_point() +
  152. #' theme_xaringan_base(
  153. #' text_color = "#021c25",
  154. #' background_color = "#e1e5f2",
  155. #' accent_color = "#1f7a8c",
  156. #' set_ggplot_defaults = TRUE) +
  157. #' labs(title = "Basic Iris Plot",
  158. #' subtitle = "+ theme_xaringan_base()",
  159. #' caption = "{xaringanthemer}")
  160. #' }
  161. #'
  162. #' @return A ggplot2 theme
  163. #' @family xaringanthemer ggplot2 themes
  164. #' @export
  165. theme_xaringan_base <- function(
  166. text_color,
  167. background_color,
  168. ...,
  169. set_ggplot_defaults = TRUE,
  170. accent_color = NULL,
  171. accent_secondary_color = NULL,
  172. text_font = NULL,
  173. text_font_use_google = NULL,
  174. text_font_size = NULL,
  175. title_font = NULL,
  176. title_font_use_google = NULL,
  177. title_font_size = NULL
  178. ) {
  179. blend <- color_blender(text_color, background_color)
  180. text_font_size <- text_font_size %||% web_to_point(xaringanthemer_env$text_font_size, scale = 1.25) %||% 11
  181. title_font_size <- title_font_size %||% web_to_point(xaringanthemer_env$header_h3_font_size, scale = 0.8) %||% 14
  182. text_font <- if (!is.null(text_font)) {
  183. register_font(text_font, identical(text_font_use_google, TRUE))
  184. } else get_theme_font("text")
  185. title_font <- if (!is.null(title_font)) {
  186. register_font(title_font, identical(title_font_use_google, TRUE))
  187. } else get_theme_font("header")
  188. text_font %||% "sans"
  189. title_font %||% "sans"
  190. if (set_ggplot_defaults) {
  191. accent_color <- accent_color %||% xaringanthemer_env$header_color %||% text_color
  192. accent_secondary_color <- accent_secondary_color %||% xaringanthemer_env$text_bold_color %||% accent_color
  193. theme_xaringan_set_defaults(text_color, background_color, accent_color, accent_secondary_color)
  194. }
  195. ggplot2::theme(
  196. line = ggplot2::element_line(color = blend(0.2)),
  197. rect = ggplot2::element_rect(fill = background_color),
  198. text = ggplot2::element_text(
  199. color = blend(0.1),
  200. family = text_font,
  201. size = text_font_size),
  202. title = ggplot2::element_text(
  203. color = accent_color,
  204. family = title_font,
  205. size = title_font_size),
  206. plot.background = ggplot2::element_rect(
  207. fill = background_color,
  208. color = background_color),
  209. panel.background = ggplot2::element_rect(
  210. fill = background_color,
  211. color = background_color),
  212. panel.grid.major = ggplot2::element_line(
  213. color = blend(0.8),
  214. inherit.blank = TRUE),
  215. panel.grid.minor = ggplot2::element_line(
  216. color = blend(0.9),
  217. inherit.blank = TRUE),
  218. axis.title = ggplot2::element_text(size = title_font_size * 0.8),
  219. axis.ticks = ggplot2::element_line(color = blend(0.8)),
  220. axis.text = ggplot2::element_text(color = blend(0.4)),
  221. legend.key = element_rect(fill="transparent", colour=NA),
  222. plot.caption = ggplot2::element_text(
  223. size = text_font_size * 0.8,
  224. color = blend(0.3))
  225. )
  226. }
  227. #' Set and Restore ggplot2 geom Defaults
  228. #'
  229. #' Set [ggplot2] _geom_ defaults to match [theme_xaringan()] with
  230. #' `theme_xaringan_set_defaults()` and restore the standard or previously-set
  231. #' defaults with `theme_xaringan_restore_defaults()`. By default,
  232. #' `theme_xaringan_set_defaults()` is run with [theme_xaringan()] or
  233. #' [theme_xaringan_inverse()].
  234. #'
  235. #' @family xaringanthemer ggplot2 themes
  236. #' @inheritParams theme_xaringan
  237. #' @return Invisibly returns a list of the current ggplot2 geom defaults
  238. #' @export
  239. theme_xaringan_set_defaults <- function(
  240. text_color = NULL,
  241. background_color = NULL,
  242. accent_color = text_color,
  243. accent_secondary_color = accent_color,
  244. text_family = NULL
  245. ) {
  246. requires_package("ggplot2")
  247. blend <- color_blender(text_color, background_color)
  248. xaringan_theme_defaults <- list(
  249. "line" = list(color = text_color),
  250. "vline" = list(color = accent_secondary_color),
  251. "hline" = list(color = accent_secondary_color),
  252. "abline" = list(color = accent_secondary_color),
  253. "segment" = list(color = text_color),
  254. "bar" = list(fill = accent_color),
  255. "col" = list(fill = accent_color),
  256. "boxplot" = list(color = text_color),
  257. "contour" = list(color = text_color),
  258. "density" = list(color = text_color,
  259. fill = text_color,
  260. alpha = 0.1),
  261. "dotplot" = list(color = accent_color),
  262. "errorbarh" = list(color = text_color),
  263. "crossbar" = list(color = text_color),
  264. "errorbar" = list(color = text_color),
  265. "linerange" = list(color = text_color),
  266. "pointrange" = list(color = text_color),
  267. "map" = list(color = text_color),
  268. "path" = list(color = text_color),
  269. "line" = list(color = text_color),
  270. "step" = list(color = text_color),
  271. "point" = list(color = accent_color),
  272. "polygon" = list(color = accent_color,
  273. fill = accent_color),
  274. "quantile" = list(color = text_color),
  275. "rug" = list(color = blend(0.5)),
  276. "segment" = list(color = text_color),
  277. "smooth" = list(fill = blend(0.75),
  278. color = accent_secondary_color),
  279. "spoke" = list(color = text_color),
  280. "label" = list(color = text_color,
  281. family = text_family %||% get_theme_font("text")),
  282. "text" = list(color = text_color,
  283. family = text_family %||% get_theme_font("text")),
  284. "rect" = list(fill = text_color),
  285. "tile" = list(fill = text_color),
  286. "violin" = list(fill = text_color),
  287. "sf" = list(color = text_color)
  288. )
  289. geom_names <- setNames(nm = names(xaringan_theme_defaults))
  290. previous_defaults <- lapply(
  291. geom_names,
  292. function(geom) safely_set_geom(geom, xaringan_theme_defaults[[geom]])
  293. )
  294. if (is.null(xaringanthemer_env$old_ggplot_defaults)) {
  295. xaringanthemer_env$old_ggplot_defaults <- previous_defaults
  296. }
  297. invisible(previous_defaults)
  298. }
  299. #' @describeIn theme_xaringan_set_defaults Restore previous or standard [ggplot2] _geom_ defaults.
  300. #' @inheritParams theme_xaringan
  301. #' @return Invisibly returns a list of the current ggplot2 geom defaults
  302. #' @export
  303. xaringan_theme_restore_defaults <- function() {
  304. requires_package("ggplot2")
  305. requires_xaringanthemer_env()
  306. if (is.null(xaringanthemer_env$old_ggplot_defaults)) return(invisible())
  307. old_default <- xaringanthemer_env$old_ggplot_defaults
  308. old_default_not_std <- vapply(old_default, function(x) length(x) > 0, logical(1))
  309. old_default <- old_default[old_default_not_std]
  310. restore_default <- utils::modifyList(xaringanthemer_env$std_ggplot_defaults, old_default)
  311. geom_names <- setNames(nm = names(restore_default))
  312. previous_defaults <- lapply(
  313. geom_names,
  314. function(geom) safely_set_geom(geom, restore_default[[geom]])
  315. )
  316. invisible(previous_defaults)
  317. }
  318. safely_set_geom <- function(geom, new) {
  319. tryCatch({
  320. ggplot2::update_geom_defaults(geom, new)
  321. },
  322. error = function(e) invisible(),
  323. warning = function(w) invisible())
  324. }
  325. # Color Scales ------------------------------------------------------------
  326. #' Xaringan Themer ggplot2 Scales
  327. #'
  328. #' Color and fill single-color scales for discrete and continuous values,
  329. #' created using the primary accent color of the xaringanthemer styles.
  330. #'
  331. #' @param ... Arguments passed on to the appropriate scale function, one of
  332. #' [colorspace::scale_color_discrete_sequential],
  333. #' [colorspace::scale_color_continuous_sequential],
  334. #' [colorspace::scale_fill_discrete_sequential], or
  335. #' [colorspace::scale_fill_continuous_sequential].
  336. #' @param color A color value, in hex, to override the default color. Otherwise,
  337. #' the primary color of the resulting scale is chosen from the xaringanthemer
  338. #' slide styles.
  339. #' @param inverse If `color` is not supplied and `inverse = TRUE`, a primary
  340. #' color is chosen to work well with the inverse slide styles, namely the
  341. #' value of `inverse_header_color`
  342. #' @param direction Direction of the discrete scale. Use values less than 0 to
  343. #' reverse the direction, e.g. `direction = -1`.
  344. #' @inheritParams colorspace::scale_color_continuous_sequential
  345. #' @param ... Additional arguments passed to [ggplot2::continuous_scale()] or
  346. #' [ggplot2:discrete_scale()].
  347. #' @param aes_type The type of aesthetic to which the scale is being applied.
  348. #' One of "color", "colour", or "fill".
  349. #' @name scale_xaringan
  350. NULL
  351. #' @rdname scale_xaringan
  352. #' @export
  353. scale_xaringan_discrete <- function(
  354. aes_type = c("color", "colour", "fill"),
  355. ...,
  356. color = NULL,
  357. direction = 1,
  358. inverse = FALSE
  359. ) {
  360. aes_type <- match.arg(aes_type)
  361. color <- hex2HCL(get_theme_accent_color(color, inverse))
  362. pal <- function(n) {
  363. colors <- colorspace::sequential_hcl(
  364. n = n,
  365. c1 = color[1, "C"], l1 = color[1, "L"], h1 = color[1, "H"],
  366. rev = direction >= 1
  367. )
  368. }
  369. ggplot2::discrete_scale(aes_type, "manual", pal, ...)
  370. }
  371. #' @rdname scale_xaringan
  372. #' @export
  373. scale_xaringan_fill_discrete <- function(
  374. ..., color = NULL, direction = 1, inverse = FALSE
  375. ) {
  376. scale_xaringan_discrete(
  377. "fill", ..., color = color, direction = direction, inverse = inverse
  378. )
  379. }
  380. #' @rdname scale_xaringan
  381. #' @export
  382. scale_xaringan_color_discrete <- function(
  383. ..., color = NULL, direction = 1, inverse = FALSE
  384. ) {
  385. scale_xaringan_discrete(
  386. "color", ..., color = color, direction = direction, inverse = inverse
  387. )
  388. }
  389. #' @rdname scale_xaringan
  390. #' @export
  391. scale_xaringan_colour_discrete <- scale_xaringan_color_discrete
  392. #' @rdname scale_xaringan
  393. #' @export
  394. scale_xaringan_continuous <- function(
  395. aes_type = c("color", "colour", "fill"),
  396. ..., color = NULL, begin = 0, end = 1, inverse = FALSE
  397. ) {
  398. aes_type <- match.arg(aes_type)
  399. color <- hex2HCL(get_theme_accent_color(color, inverse))
  400. colors <- colorspace::sequential_hcl(
  401. n = 12, c1 = color[1, "C"], l1 = color[1, "L"], h1 = color[1, "H"],
  402. rev = TRUE
  403. )
  404. rescaler <- function(x, ...) {
  405. scales::rescale(x, to = c(begin, end), from = range(x, na.rm = TRUE))
  406. }
  407. ggplot2::continuous_scale(
  408. aes_type, "continuous_sequential",
  409. palette = scales::gradient_n_pal(colors, values = NULL),
  410. rescaler = rescaler,
  411. oob = scales::censor,
  412. ...
  413. )
  414. }
  415. #' @rdname scale_xaringan
  416. #' @export
  417. scale_xaringan_fill_continuous <- function(
  418. ..., color = NULL, begin = 0, end = 1, inverse = FALSE
  419. ) {
  420. scale_xaringan_continuous(
  421. "fill", ..., color = color, begin = begin, end = end, inverse = inverse
  422. )
  423. }
  424. #' @rdname scale_xaringan
  425. #' @export
  426. scale_xaringan_color_continuous <- function(
  427. ..., color = NULL, begin = 0, end = 1, inverse = FALSE
  428. ) {
  429. scale_xaringan_continuous(
  430. "color", ..., color = color, begin = begin, end = end, inverse = inverse
  431. )
  432. }
  433. #' @rdname scale_xaringan
  434. #' @export
  435. scale_xaringan_colour_continuous <- scale_xaringan_color_continuous
  436. get_theme_accent_color <- function(color = NULL, inverse = FALSE) {
  437. color <-
  438. if (!inverse) {
  439. color %||%
  440. xaringanthemer_env[["header_color"]] %||%
  441. xaringanthemer_env[["text_color"]]
  442. } else {
  443. color %||% xaringanthemer_env[["inverse_header_color"]]
  444. }
  445. if (is.null(color)) {
  446. stop(
  447. call. = FALSE,
  448. "No color provided and no default available. ",
  449. "Have you forgotten to use a style function to set the xaringan theme?"
  450. )
  451. }
  452. color
  453. }
  454. blend_colors <- function(x, y, alpha = 0.5) {
  455. x <- colorspace::hex2RGB(x)
  456. y <- colorspace::hex2RGB(y)
  457. z <- colorspace::mixcolor(alpha, x, y)
  458. colorspace::hex(z)
  459. }
  460. color_blender <- function(x, y) function(alpha = 0.5) blend_colors(x, y, alpha)
  461. hex2HCL <- function(x) {
  462. colorspace::coords(as(colorspace::hex2RGB(x), "polarLUV"))
  463. }
  464. # Fonts -------------------------------------------------------------------
  465. get_theme_font <- function(element = c("text", "header", "code")) {
  466. element <- match.arg(element)
  467. element_family <- paste0(element, "_font_family")
  468. element_google <- paste0(element, "_font_google")
  469. element_url <- paste0(element, "_font_url")
  470. family <- xaringanthemer_env[[element_family]]
  471. is_google_font <- !is.null(xaringanthemer_env[[element_google]]) ||
  472. grepl("fonts.google", xaringanthemer_env[[element_url]], fixed = TRUE)
  473. register_font(family, google = is_google_font, fn = sys.calls()[[max(1, length(sys.calls()) - 1)]])
  474. }
  475. register_font <- function(
  476. family,
  477. google = TRUE,
  478. fn = sys.calls()[[max(1, length(sys.calls()) - 1)]][[1]],
  479. ...
  480. ) {
  481. if (is.null(family)) return(NULL)
  482. family <- gsub("['\"]", "", family)
  483. if (!identical(xaringanthemer_env$showtext_auto, TRUE)) {
  484. if (requires_package(pkg = "showtext", fn, required = FALSE)) {
  485. showtext::showtext_auto()
  486. } else return(family)
  487. xaringanthemer_env$showtext_auto <- TRUE
  488. }
  489. if (!requires_package(pkg = "sysfonts", fn, required = FALSE)) {
  490. return(family)
  491. } else if (!family %in% sysfonts::font_families()) {
  492. is_default_font <- family %in% c(
  493. "Roboto", "Source Code Pro", "Yanone Kaffeesatz"
  494. )
  495. if (identical(google, TRUE) || is_default_font) {
  496. tryCatch(
  497. sysfonts::font_add_google(family, ...),
  498. error = function(e) warning(e$message),
  499. warning = function(w) warning(w$message)
  500. )
  501. } else {
  502. warning(paste(
  503. "Please manually register fonts not served by Google Fonts.",
  504. "See `sysfonts::font_add()` for more information."))
  505. }
  506. }
  507. family
  508. }
  509. requires_package <- function(pkg = "ggplot2", fn = "", required = TRUE) {
  510. raise <- if (required) stop else warning
  511. if (!requireNamespace(pkg, quietly = TRUE)) {
  512. msg <- paste0(
  513. "`", pkg, "` is ",
  514. if (required) "required " else "suggested ",
  515. if (fn != "") paste0("by ", fn, "() ")[1],
  516. "but is not installed."
  517. )
  518. raise(msg, call. = FALSE)
  519. invisible(FALSE)
  520. }
  521. invisible(TRUE)
  522. }
  523. requires_xaringanthemer_env <- function() {
  524. if (!exists("xaringanthemer_env") || is.null(xaringanthemer_env$header_color)) {
  525. stop("Please call a xaringanthemer theme function first.")
  526. }
  527. }
  528. #' Get the Value of xaringanthemer Style Setting
  529. #'
  530. #' A helper function to retrieve the value of style settings as set by a
  531. #' xaringanthemer style function, for use in plotting and other circumstances.
  532. #'
  533. #' @section Style Settings:
  534. #' Style settings used by xaringanthemer include:
  535. #'
  536. #' - `background_color`
  537. #' - `background_image`
  538. #' - `background_position`
  539. #' - `background_size`
  540. #' - `blockquote_left_border_color`
  541. #' - `code_font_family`
  542. #' - `code_font_family_fallback`
  543. #' - `code_font_google`
  544. #' - `code_font_size`
  545. #' - `code_font_url`
  546. #' - `code_highlight_color`
  547. #' - `code_inline_background_color`
  548. #' - `code_inline_color`
  549. #' - `code_inline_font_size`
  550. #' - `extra_css`
  551. #' - `extra_fonts`
  552. #' - `footnote_color`
  553. #' - `footnote_font_size`
  554. #' - `footnote_position_bottom`
  555. #' - `header_background_auto`
  556. #' - `header_background_color`
  557. #' - `header_background_content_padding_top`
  558. #' - `header_background_ignore_classes`
  559. #' - `header_background_padding`
  560. #' - `header_background_text_color`
  561. #' - `header_color`
  562. #' - `header_font_family`
  563. #' - `header_font_google`
  564. #' - `header_font_url`
  565. #' - `header_font_weight`
  566. #' - `header_h1_font_size`
  567. #' - `header_h2_font_size`
  568. #' - `header_h3_font_size`
  569. #' - `inverse_background_color`
  570. #' - `inverse_header_color`
  571. #' - `inverse_text_color`
  572. #' - `inverse_text_shadow`
  573. #' - `left_column_selected_color`
  574. #' - `left_column_subtle_color`
  575. #' - `link_color`
  576. #' - `padding`
  577. #' - `table_border_color`
  578. #' - `table_row_border_color`
  579. #' - `table_row_even_background_color`
  580. #' - `text_bold_color`
  581. #' - `text_color`
  582. #' - `text_font_base`
  583. #' - `text_font_family`
  584. #' - `text_font_family_fallback`
  585. #' - `text_font_google`
  586. #' - `text_font_size`
  587. #' - `text_font_url`
  588. #' - `text_font_weight`
  589. #' - `text_slide_number_color`
  590. #' - `text_slide_number_font_size`
  591. #' - `title_slide_background_color`
  592. #' - `title_slide_background_image`
  593. #' - `title_slide_background_position`
  594. #' - `title_slide_background_size`
  595. #' - `title_slide_text_color`
  596. #'
  597. #' @param setting A xaringanthemer style setting
  598. #' @export
  599. theme_xaringan_get_value <- function(setting) {
  600. requires_xaringanthemer_env()
  601. if (length(setting) > 1) {
  602. xaringanthemer_env[setting]
  603. } else {
  604. xaringanthemer_env[[setting]]
  605. }
  606. }
  607. web_to_point <- function(x, px_per_em = 16, scale = 1) {
  608. if (is.null(x)) return(NULL)
  609. if (grepl("pt$", x)) {
  610. return(as.numeric(sub("pt$", "", x)))
  611. } else if (grepl("px$", x)) {
  612. x <- as.numeric(sub("px$", "", x))
  613. return(x * 0.75)
  614. } else if (grepl("em$", x)) {
  615. x <- as.numeric(sub("em$", "", x))
  616. return(x * px_per_em * 0.75)
  617. } else {
  618. return()
  619. }
  620. }