An open API service indexing awesome lists of open source software.

https://github.com/yixuan/fontr

Extracting Glyphs From Font Files
https://github.com/yixuan/fontr

Last synced: 21 days ago
JSON representation

Extracting Glyphs From Font Files

Awesome Lists containing this project

README

        

---
output:
html_document:
keep_md: yes
self_contained: no
---
# fontr

**fontr** helps you to extract character glyphs from a specific font, either as
bitmaps or outline polygons. This is exactly what
[showtext](https://github.com/yixuan/showtext) does internally, and **fontr**
simply makes it work in the R level.

## Quick Examples

The code below generates a matrix representing the bitmap image of the character
"C", given the specification of the font.

```{r fig.width=5, fig.height=5}
library(fontr)
ch_C = glyph_bitmap("C", family = "sans", face = "regular", pixel_size = 20)
ch_C
plot(ch_C)
```

The glyph can also be extracted as outline curves that are similar to the SVG
path elements.

```{r}
ch_H = glyph_outline("H", family = "sans", face = "regular")
ch_H
```

The meaning of the characters in the `type` column can be found in sections
8.3.2 to 8.3.7 of the
[SVG specification document](http://www.w3.org/TR/SVG/paths.html).

This form is an exact representation of the glyph outlines that are contained
in the font file. However, it is usually more convenient to use polygons to
approximate those curves, as the following example shows:

```{r fig.width=7, fig.height=7}
ch_n = glyph_polygon("n", family = "serif", face = "italic", nseg = 10)
head(ch_n, 15)
plot(ch_n)
```

The returned data frame contains the x and y coordinates of the vertices of
the polygons.

## Loading Fonts

The method to load fonts is the same as the **showtext** package. See the
**Loading Fonts** section of the [README file](https://github.com/yixuan/showtext)
of **showtext**.

```{r}
library(sysfonts)
font.add.google("Lobster", "lobster")
plot(glyph_bitmap("Welcome", family = "lobster", rot = 30))
```