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
- Host: GitHub
- URL: https://github.com/yixuan/fontr
- Owner: yixuan
- Created: 2015-09-27T14:16:17.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-09-27T15:25:13.000Z (over 9 years ago)
- Last Synced: 2025-04-04T22:46:55.751Z (26 days ago)
- Language: C++
- Size: 199 KB
- Stars: 24
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
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))
```