rsvg: Render SVG into Bitmap

Description Usage Arguments Examples

View source: R/rsvg.R

Description

Render svg image into a high quality bitmap. When both width and height are NULL, the output resolution matches that of the input. When either width or height is specified, the image is scaled proportionally. When both width and height are specified, the image is stretched into the requested size.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13

Arguments

svg

path/url to svg file or raw vector with svg data. Use charToRaw to convert an SVG string into raw data.

width

output width in pixels or NULL for default.

height

output height in pixels or NULL for default

css

path/url to external css file or raw vector with css data. This requires your system has a recent version of librsvg.

file

path to output file or NULL to return content as raw vector

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# create some svg
options(example.ask=FALSE)
tmp <- tempfile()
svglite::svglite(tmp, width = 10, height = 7)
ggplot2::qplot(mpg, wt, data = mtcars, colour = factor(cyl))
dev.off()

# convert directly into a bitmap format
rsvg_pdf(tmp, "out.pdf")
rsvg_png(tmp, "out.png")
rsvg_svg(tmp, "out.svg")
rsvg_ps(tmp, "out.ps")

# render into raw bitmap array
bitmap <- rsvg(tmp, height = 1440)
dim(bitmap) # h*w*c

# read in your package of choice
magick::image_read(bitmap)
webp::write_webp(bitmap, "bitmap.webp", quality = 100)

# cleanup
unlink(c("out.pdf", "out.png", "out.svg", "out.ps", "bitmap.webp"))

Example output

png 
  2 
[1] 1440 2057    4
# A tibble: 1 x 7
  format width height colorspace matte filesize density
  <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
1 PNG     2057   1440 sRGB       TRUE         0 72x72  

rsvg documentation built on May 3, 2021, 9:07 a.m.