-
Notifications
You must be signed in to change notification settings - Fork 39
/
README.Rmd
122 lines (85 loc) · 4.6 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r}
#| include: false
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# svglite <a href='https://svglite.r-lib.org'><img src="man/figures/logo.png" align="right" height="131.5"/></a>
<!-- badges: start -->
[![R-CMD-check](https://github.com/r-lib/svglite/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/svglite/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/r-lib/svglite/branch/main/graph/badge.svg)](https://app.codecov.io/gh/r-lib/svglite?branch=main)
[![CRAN Status Badge](http://www.r-pkg.org/badges/version/svglite)](https://cran.r-project.org/package=svglite)
<!-- badges: end -->
svglite is a graphics device that produces clean svg output, suitable for use on the web, or hand editing. Compared to the built-in `svg()`, svglite produces smaller files, and leaves text as is, making it easier to edit the result after creation. It also supports multiple nice features such as embedding of web fonts.
## Installation
svglite is available on CRAN using `install.packages("svglite")`. You can install the development version from github with:
```{r}
#| eval: false
# install.packages("pak")
pak::pak("r-lib/svglite")
```
## Motivation
The grDevices package bundled with R already comes with an SVG device (using the eponymous `svg()` call). The development of svglite is motivated by the following considerations:
### Speed
`svglite()` is considerably faster than `svg()`. If you are rendering SVGs dynamically to serve over the web this can be quite important:
```{r}
#| message: false
library(svglite)
x <- runif(1e3)
y <- runif(1e3)
tmp1 <- tempfile()
tmp2 <- tempfile()
svglite_test <- function() {
svglite(tmp1)
plot(x, y)
dev.off()
}
svg_test <- function() {
svg(tmp2, onefile = TRUE)
plot(x, y)
dev.off()
}
bench::mark(svglite_test(), svg_test(), min_iterations = 250, check = FALSE)
```
### File size
Another point with high relevance when serving SVGs over the web is the size. `svglite()` produces much smaller files
```{r}
# svglite
fs::file_size(tmp1)
# svg
fs::file_size(tmp2)
```
In both cases, compressing to make `.svgz` (gzipped svg) is worthwhile. svglite supports compressed output directly which will be triggered if the provided path has a `".svgz"` (or `".svg.gz"`) extension.
```{r}
tmp3 <- tempfile(fileext = ".svgz")
svglite(tmp3)
plot(x, y)
invisible(dev.off())
# svglite - svgz
fs::file_size(tmp3)
```
### Editability
One of the main reasons for the size difference between the size of the output of `svglite()` and `svg()` is the fact that `svglite()` encodes text as styled `<text>` elements, whereas `svg()` converts the glyphs to polygons and renders these. The latter approach means that the output of `svg()` does not require the font to be present on the system that displays the SVG but makes it more or less impossible to edit the text after the fact. svglite focuses on providing maximal editability of the output, so that you can open up the result in a vector drawing program such as Inkscape or Illustrator and polish the output if you so choose.
### Font support
svglite uses systemfonts for font discovery which means that all installed fonts on your system is available to use. The systemfonts foundation means that fonts registered with `register_font()` or `register_variant()` will also be available. If any of these contains non-standard weights or OpenType features (e.g. ligatures or tabular numerics) this will be correctly encoded in the style block. systemfonts also allows you to embed webfont `@imports` in your file to ensure that the file looks as expected even on systems without the used font installed.
## Building svglite
_This section is only relevant for building svglite from scratch, as opposed to installing from a pre-built package on CRAN._
Building vdiffr requires the system dependency libpng. As vdiffr doesn't have any build-time configuration, your R configuration must point to libpng's `include` and `lib` folders.
For instance on macOS, install libpng with:
```sh
brew install libpng
```
And make sure your `~/.R/Makevars` knows about Homebrew's `include` and `lib` folders where libpng should now be installed. On arm64 hardware, this would be:
```mk
CPPFLAGS += -I/opt/homebrew/include
LDFLAGS += -L/opt/homebrew/lib
```
## Code of Conduct
Please note that the svglite project is released with a [Contributor Code of Conduct](https://svglite.r-lib.org/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.