😎 Give your xaringan slides some style
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

850 linhas
26KB

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