https://github.com/r-lib/systemfonts
System Native Font Handling in R
https://github.com/r-lib/systemfonts
fonts rstats
Last synced: 2 months ago
JSON representation
System Native Font Handling in R
- Host: GitHub
- URL: https://github.com/r-lib/systemfonts
- Owner: r-lib
- License: other
- Created: 2019-06-04T08:45:46.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2025-05-13T10:21:22.000Z (2 months ago)
- Last Synced: 2025-05-13T11:24:49.298Z (2 months ago)
- Topics: fonts, rstats
- Language: C++
- Homepage: https://systemfonts.r-lib.org
- Size: 6.79 MB
- Stars: 93
- Watchers: 6
- Forks: 18
- Open Issues: 8
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# systemfonts[](https://cran.r-project.org/package=systemfonts)
[](https://lifecycle.r-lib.org/articles/stages.html)
[](https://github.com/r-lib/systemfonts/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/r-lib/systemfonts)systemfonts is a package that locates installed fonts. It uses the system-native libraries on Mac (CoreText) and Linux (FontConfig), and uses Freetype to parse the fonts in the registry on Windows.
## Installation
systemfonts is available from CRAN using `install.packages('systemfonts')`. It
is however still under development and you can install the development version
using devtools.```{r, eval=FALSE}
# install.packages('pak')
pak::pak('r-lib/systemfonts')
```## Examples
The main use of this package is to locate font files based on family and style:```{r}
library(systemfonts)match_fonts('Avenir', italic = TRUE)
```This function returns the path to the file holding the font, as well as the
0-based index of the font in the file.It is also possible to get a data.frame of all available fonts:
```{r, include=FALSE}
library(tibble)
```
```{r}
system_fonts()
```Further, you can query additional information about fonts and specific glyphs,
if that is of interest using the `font_info()` and `glyph_info()` functions.### Custom fonts
While the package was created to provide transparent access to fonts installed
on the system, it has grown to also provide ways to work with font files not
part of the system installation. This is especially beneficial if you are
running code on a system where you don't have administrator rights and need to
use a custom font.systemfonts provide the `add_fonts()` function which takes a vector of file
paths and add these to the lookup database without installing them. Further,
systemfonts automatically looks in the `./fonts` and `~/fonts` folders and adds
any font files located there during startup. This means that you can distribute
a script along with a fonts directory and have those fonts automatically
available during execution of the script.In addition to the above, systemfonts also provides access to online font
repositories such as [Google Fonts](https://fonts.google.com) and can search and
download from these, automatically adding the downloaded fonts to the lookup
database.All these functionalities are condensed into a single function,
`require_font()`, which allows you to state a font dependency inside a script.
The function will first look for the font on the system, and failing that, will
try to fetch it from an online repository. If that fails it will either throw an
error or remap the font to another of the developers choosing.## C API
While getting this information in R is nice, the intended use is mostly through
compiled code so that graphic devices can easily locate relevant font files etc.In order to use functions from systemfonts in C(++) code your package should
list systemfonts in the `LinkingTo` field in the `DESCRIPTION` file. Once this
is done you can now `#include ` in your code and use the provided
functions. Look into the
[`inst/include/systemfonts.h`](https://github.com/r-lib/systemfonts/blob/master/inst/include/systemfonts.h)
file to familiarise yourself with the C API.## System Defaults
systemfonts will always try to find a font for you, even if none exist with the
given family name or style. How it resolves this is system specific and should
not be relied on, but it can be expected that a valid font file is always
returned no matter the input.A few special aliases exist that behaves predictably but system dependent:
- `""` and `"sans"` return *Helvetica* on Mac, *Arial* on Windows, and the
default sans-serif font on Linux (*DejaVu Sans* on Ubuntu)
- `"serif"` return *Times* on Mac, *Times New Roman* on Windows, and the
default serif font on Linux (*DejaVu Serif* on Ubuntu)
- `"mono"` return *Courier* on Mac, *Courier New* on Windows, and the
default mono font on Linux (*DejaVu Mono* on Ubuntu)
- `"emoji"` return *Apple Color Emoji* on Mac, *Segoe UI Emoji* on Windows, and the
default emoji font on Linux (*Noto Color* on Ubuntu)## Code of Conduct
Please note that the 'systemfonts' project is released with a
[Contributor Code of Conduct](https://github.com/r-lib/systemfonts/blob/master/CODE_OF_CONDUCT.md).
By contributing to this project, you agree to abide by its terms.