Skip to content

Commit 5106b34

Browse files
committed
make woff2 optional
1 parent 925710e commit 5106b34

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

R/web_fonts.R

+15-7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ search_web_fonts <- function(family, n_max = 10, ...) {
3939
#' @param dir Where to download the font to. The default places it in your user
4040
#' local font folder so that the font will be available automatically in new R
4141
#' sessions. Set to `tempdir()` to only keep the font for the session.
42+
#' @param woff2 Should the font be downloaded in the woff2 format (smaller and
43+
#' more optimized)? Defaults to FALSE as the format is not supported on all
44+
#' systems
4245
#'
4346
#' @return A logical invisibly indicating whether a font was found and
4447
#' downloaded or not
@@ -51,8 +54,8 @@ NULL
5154
#' @rdname web-fonts
5255
#' @export
5356
#'
54-
get_from_google_fonts <- function(family, dir = "~/fonts") {
55-
fonts <- get_google_fonts_registry()
57+
get_from_google_fonts <- function(family, dir = "~/fonts", woff2 = FALSE) {
58+
fonts <- get_google_fonts_registry(woff2)
5659
match <- which(tolower(fonts$family) == tolower(family))
5760

5861
if (length(match) == 0) {
@@ -252,10 +255,15 @@ import_from_font_library <- function(family, ...) {
252255
# REGISTRIES -------------------------------------------------------------------
253256

254257
google_fonts_registry <- new.env(parent = emptyenv())
255-
get_google_fonts_registry <- function() {
256-
if (is.null(google_fonts_registry$fonts)) {
257-
fonts <- jsonlite::read_json("https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyBkOYsZREsyZWvbSR_d03SI5XX30cIapYo&sort=popularity&capability=WOFF2")$items
258-
google_fonts_registry$fonts <- do.call(rbind, lapply(fonts, function(x) {
258+
get_google_fonts_registry <- function(woff2 = FALSE) {
259+
loc <- if (woff2) "woff2" else "ttf"
260+
if (is.null(google_fonts_registry[[loc]])) {
261+
url <- "https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyBkOYsZREsyZWvbSR_d03SI5XX30cIapYo&sort=popularity"
262+
if (woff2) {
263+
url <- paste0(url, "&capability=WOFF2")
264+
}
265+
fonts <- jsonlite::read_json(url)$items
266+
google_fonts_registry[[loc]] <- do.call(rbind, lapply(fonts, function(x) {
259267
x <- data.frame(
260268
family = x$family,
261269
variant = unlist(x$variants),
@@ -270,7 +278,7 @@ get_google_fonts_registry <- function() {
270278
x
271279
}))
272280
}
273-
google_fonts_registry$fonts
281+
google_fonts_registry[[loc]]
274282
}
275283

276284
font_squirrel_registry <- new.env(parent = emptyenv())

man/web-fonts.Rd

+5-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)