fontr

fontr helps you to extract character glyphs from a specific font, either as bitmaps or outline polygons. This is exactly what 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.

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.

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.

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:

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 of showtext.

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


yixuan/fontr documentation built on May 4, 2019, 5:29 p.m.