svglite produces SVG files containing plain text but fonts are still important for plot generation and rendering. Fonts are used during SVG generation to figure out the metrics of graphical elements. The font name is then recorded in the
font-family property of text anchors so that SVG renderers know what fonts to use. svglite does try to ensure a consistent figure rendering even when fonts are not available at the time of rendering (by supplying the textLength SVG text attribute). However, the text may look slightly distorted when a fallback font is used. This means that for optimal display, the font must be available on both the computer used to create the svg, and the computer used to render the svg. The defaults are fonts that are available on almost all systems: there may be small differences between them, but they are unlikely to cause problems in most causes.
|R family||Font on Windows||Font on Unix|
||Times New Roman||Times|
||Standard Symbols L||Symbol|
One downside to these default fonts is that they do not have good coverage of characters for non-latin alphabets. This can be fixed by using the arguments
user_fonts which provide control over which fonts to use during SVG generation and rendering.
system_fonts takes a named list of font families as argument. The names typically correspond to standard R faces but they can also alias non-standard families (though this is less useful):
<- list( fonts sans = "Helvetica", mono = "Consolas", `Times New Roman` = "DejaVu Serif" ) <- svgstring(system_fonts = fonts) ss plot(1:10) text(0.8, 0.8, "Some text", family = "mono") text(0.2, 0.2, "Other text", family = "Times New Roman") dev.off() ss()
If you need support for non-latin characters, choose fonts with good Unicode coverage. “Arial Unicode MS” is a sans serif font with good coverage that is available on macOS and Windows systems (on the latter, only if MS Office is installed). Note that this font does not support kerning and has no bold or italic faces.
svglite("Rplots.svg", system_fonts = list(sans = "Arial Unicode MS")) plot.new() text(0.5, 0.5, "正規分布") dev.off()
The Noto fontset provided by Google as well as the Han Sans family by Adobe have excellent coverage but may not be available at the time of rendering. This can be a concern if you distribute the SVG files on the Internet.
In addition to system fonts, you can also provide fonts that are not necessarily installed on the system (i.e., fonts that live in user space). The main reason to do this is to generate reproducible SVG files as different platforms can have different versions of a font and thus produce different text metrics. The
user_fonts arguments takes either paths to font files, fonts from the
fontquiver package, or a list that specifies the alias. Whereas
system_fonts gets a named list of families as argument,
user_fonts takes a named tree of lists of families (
symbol) and faces (
# Using ttf files from fontquiver here, but it could be any ttf <- fontquiver::font("Liberation", "Sans", "Regular")$ttf some_file <- fontquiver::font("Liberation", "Sans", "Italic")$ttf other_file <- fontquiver::font("Liberation", "serif", "Italic")$ttf serif_file # The outer named list contains families while the inner named list # contains faces: <- list( fonts sans = list( plain = some_file, italic = other_file ),serif = list(plain = serif_file) ) <- svglite("plot.svg", user_fonts = fonts) ss plot.new() text(0.5, 0.5, "Sans Plain text") text(0.2, 0.2, "Sans Italic text", font = 3) text(0.8, 0.8, "Serif text", family = "serif") dev.off()
You can also control which font gets written in the
font-family fields of SVGs by supplying a list containing
<- list(alias = "Foobar Font", file = other_file) file_with_alias <- list(sans = list(plain = file_with_alias)) fonts <- svgstring(user_fonts = fonts) ss plot(1:10) text(0.5, 0.5, "Sans text") dev.off() ss()
fontquiver fonts are particularly useful for creating reproducible SVG files. The
vdiffr package uses svglite with fontquiver fonts to create visual unit tests reliably across platforms. The Liberation fontset is appropriate for this usage because it features all 12 combinations of standard R families and faces. In addition fontquiver provides Symbola for the symbol font. The function
fontquiver::font_families() produces a list with the appropriate structure and can be directly supplied to svglite:
<- fontquiver::font_families("Liberation") fonts $symbol$symbol <- fontquiver::font_symbol("Symbola") fontsstr(fonts, 2) svglite("reproducible.svg", user_fonts = fonts) plot(1:10) dev.off()
The systemfonts package is used to match font family names to fonts installed on the system. systemfonts will always return a valid font, but if the requested font is badly misspelled or missing, a default will be returned. To test if the expected font is matched you can use the
font_info() functions from systemfonts:
::match_font("Helvetica") systemfonts#> $path #>  "/System/Library/Fonts/Helvetica.ttc" #> #> $index #>  0 #> #> $features #> NULL ::font_info("Helvetica", bold = TRUE) systemfonts#> path index family style italic bold #> 1 /System/Library/Fonts/Helvetica.ttc 1 Helvetica Bold FALSE TRUE #> monospace weight width kerning color scalable vertical n_glyphs n_sizes #> 1 FALSE bold normal FALSE FALSE TRUE FALSE 2252 0 #> n_charmaps bbox max_ascend max_descend #> 1 10 -12.218750, 16.468750, -5.765625, 11.546875 9.234375 -2.765625 #> max_advance_width max_advance_height lineheight underline_pos underline_size #> 1 18 12 12 -1.203125 0.59375