@@ -39,6 +39,9 @@ search_web_fonts <- function(family, n_max = 10, ...) {
39
39
# ' @param dir Where to download the font to. The default places it in your user
40
40
# ' local font folder so that the font will be available automatically in new R
41
41
# ' 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
42
45
# '
43
46
# ' @return A logical invisibly indicating whether a font was found and
44
47
# ' downloaded or not
51
54
# ' @rdname web-fonts
52
55
# ' @export
53
56
# '
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 )
56
59
match <- which(tolower(fonts $ family ) == tolower(family ))
57
60
58
61
if (length(match ) == 0 ) {
@@ -252,10 +255,15 @@ import_from_font_library <- function(family, ...) {
252
255
# REGISTRIES -------------------------------------------------------------------
253
256
254
257
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 ) {
259
267
x <- data.frame (
260
268
family = x $ family ,
261
269
variant = unlist(x $ variants ),
@@ -270,7 +278,7 @@ get_google_fonts_registry <- function() {
270
278
x
271
279
}))
272
280
}
273
- google_fonts_registry $ fonts
281
+ google_fonts_registry [[ loc ]]
274
282
}
275
283
276
284
font_squirrel_registry <- new.env(parent = emptyenv())
0 commit comments