Debian Codegraph @

man/font_face.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/fonts.R
3 \name{font_google}
4 \alias{font_google}
5 \alias{font_link}
6 \alias{font_face}
7 \alias{font_collection}
8 \alias{is_font_collection}
9 \title{Helpers for importing web fonts}
10 \usage{
11 font_google(
12 family,
13 local = TRUE,
14 cache = sass_file_cache(sass_cache_context_dir()),
15 wght = NULL,
16 ital = NULL,
17 display = c("swap", "auto", "block", "fallback", "optional")
18 )
19
20 font_link(family, href)
21
22 font_face(
23 family,
24 src,
25 weight = NULL,
26 style = NULL,
27 display = c("swap", "auto", "block", "fallback", "optional"),
28 stretch = NULL,
29 variant = NULL,
30 unicode_range = NULL
31 )
32
33 font_collection(..., default_flag = TRUE, quote = TRUE)
34
35 is_font_collection(x)
36 }
37 \arguments{
38 \item{family}{A character string with a \emph{single} font family name.}
39
40 \item{local}{Whether or not download and bundle local (woff2) font files.}
41
42 \item{cache}{A \code{\link[=sass_file_cache]{sass_file_cache()}} object (or, more generally, a file
43 caching class with \verb{$get_file()} and \verb{$set_file()} methods). Set this
44 argument to \code{FALSE} or \code{NULL} to disable caching.}
45
46 \item{wght}{One of the following:
47 \itemize{
48 \item \code{NULL}, the default weight for the \code{family}.
49 \item A character string defining an \href{https://developers.google.com/fonts/docs/css2#axis_ranges}{axis range}
50 \item A numeric vector of desired font weight(s).
51 }}
52
53 \item{ital}{One of the following:
54 \itemize{
55 \item \code{NULL}, the default \code{font-style} for the \code{family}.
56 \item \code{0}, meaning \code{font-style: normal}
57 \item \code{1}, meaning \code{font-style: italic}
58 \item \code{c(0, 1)}, meaning both \code{normal} and \code{italic}
59 }}
60
61 \item{display}{A character vector for the \code{font-display} \verb{@font-face}
62 property.}
63
64 \item{href}{A URL resource pointing to the font data.}
65
66 \item{src}{A character vector for the \code{src} \verb{@font-face} property. Beware
67 that is character strings are taken verbatim, so careful quoting and/or URL
68 encoding may be required.}
69
70 \item{weight}{A character (or numeric) vector for the \code{font-weight}
71 \verb{@font-face} property.}
72
73 \item{style}{A character vector for the \code{font-style} \verb{@font-face} property.}
74
75 \item{stretch}{A character vector for the \code{font-stretch} \verb{@font-face}
76 property.}
77
78 \item{variant}{A character vector for the \code{font-variant} \verb{@font-face}
79 property.}
80
81 \item{unicode_range}{A character vector for \code{unicode-range} \verb{@font-face}
82 property.}
83
84 \item{...}{a collection of \code{font_google()}, \code{font_link()}, \code{font_face()}, and/or character vector(s) (i.e., family names to include in the CSS \code{font-family} properly). Family names are automatically quoted as necessary.}
85
86 \item{default_flag}{whether or not to include a \code{!default} when converted to a Sass variable with \code{\link[=as_sass]{as_sass()}}.}
87
88 \item{quote}{whether or not to attempt automatic quoting of family names.}
89
90 \item{x}{test whether \code{x} is a \code{font_collection()}, \code{font_google()}, \code{font_link()}, or \code{font_face()} object.}
91 }
92 \value{
93 a \code{\link[=sass_layer]{sass_layer()}} holding an \code{\link[htmltools:htmlDependency]{htmltools::htmlDependency()}} which points
94 to the font files.
95 }
96 \description{
97 Include font file(s) when defining a Sass variable that represents a CSS
98 \code{font-family} property.
99 }
100 \details{
101 These helpers \strong{must be used the named list approach to variable
102 definitions}, for example:
103
104 \if{html}{\out{<div class="sourceCode">}}\preformatted{list(
105 list("font-variable" = font_google("Pacifico")),
106 list("body\{font-family: $font-variable\}")
107 )
108 }\if{html}{\out{</div>}}
109 }
110 \section{Font fallbacks}{
111
112
113 By default, \code{font_google()} downloads, caches, and serves the relevant font
114 file(s) locally. By locally serving files, there's a guarantee that the font
115 can render in any client browser, even when the client doesn't have internet
116 access. However, when importing font files remotely (i.e., \code{font_google(..., local = FALSE)} or \code{font_link()}), it's a good idea to provide fallback
117 font(s) in case the remote link isn't working (e.g., maybe the end user
118 doesn't have an internet connection). To provide fallback fonts, use
119 \code{\link[=font_collection]{font_collection()}}, for example:
120
121 \if{html}{\out{<div class="sourceCode">}}\preformatted{pacifico <- font_google("Pacifico", local = FALSE)
122 as_sass(list(
123 list("font-variable" = font_collection(pacifico, "system-ui")),
124 list("body\{font-family: $font-variable\}")
125 ))
126 }\if{html}{\out{</div>}}
127 }
128
129 \section{Default flags}{
130
131
132 These font helpers encourage best practice of adding a \code{!default} to Sass
133 variable definitions, but the flag may be removed via \code{font_collection()} if
134 desired.
135
136 \if{html}{\out{<div class="sourceCode">}}\preformatted{as_sass(list("font-variable" = pacifico))
137 #> $font-variable: Pacifico !default;
138 as_sass(list("font-variable" = font_collection(pacifico, default_flag = F)))
139 #> $font-variable: Pacifico;
140 }\if{html}{\out{</div>}}
141 }
142
143 \section{Serving non-Google fonts locally}{
144
145
146 Non-Google fonts may also be served locally with \code{font_face()}, but it
147 requires downloading font file(s) and pointing \code{src} to the right location
148 on disk. If you want \code{src} to be a relative file path (you almost certainly
149 do), then you'll need to mount that resource path using something like
150 \code{\link[shiny:resourcePaths]{shiny::addResourcePath()}} (for a shiny app) or \code{servr::httd()} (for static
151 HTML).
152 }
153
154 \examples{
155
156 library(htmltools)
157
158 my_font <- list("my-font" = font_google("Pacifico"))
159 hello <- tags$body(
160 "Hello",
161 tags$style(
162 sass(
163 list(
164 my_font,
165 list("body {font-family: $my-font}")
166 )
167 )
168 )
169 )
170
171 if (interactive()) {
172 browsable(hello)
173 }
174
175 # Three different yet equivalent ways of importing a remotely-hosted Google Font
176 a <- font_google("Crimson Pro", wght = "200..900", local = FALSE)
177 b <- font_link(
178 "Crimson Pro",
179 href = "https://fonts.googleapis.com/css2?family=Crimson+Pro:[email protected]"
180 )
181 url <- "https://fonts.gstatic.com/s/crimsonpro/v13/q5uDsoa5M_tv7IihmnkabARboYF6CsKj.woff2"
182 c <- font_face(
183 family = "Crimson Pro",
184 style = "normal",
185 weight = "200 900",
186 src = paste0("url(", url, ") format('woff2')")
187 )
188 }
189 \references{
190 \url{https://developers.google.com/fonts/docs/css2}
191
192 \url{https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face}
193
194 \url{https://developer.mozilla.org/en-US/docs/Learn/CSS/Styling_text/Web_fonts}
195 }