| Title: | Read 'SVG' as Polygons |
|---|---|
| Description: | Parse SVG to polygons, bezier curves, lines and associated graphical parameters. This enables the manipulation of the raw SVG data and custom plotting techniques. |
| Authors: | Mike Cheng [aut, cre, cph] |
| Maintainer: | Mike Cheng <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-17 09:32:07 UTC |
| Source: | https://github.com/coolbutuseless/nanosvgr |
This is usually done as part of calling nsvg_read(), but can be
called separately if the points data needs to be calculated at a finer or
coarser level.
nsvg_add_points(nsvg, n = 20)nsvg_add_points(nsvg, n = 20)
nsvg |
'nsvg' object created using |
n |
number of points to use when converting each bezier to a polyline. Default: 20. |
'nsvg' object with modified 'points' column. This is a list-column with each element being a data.frame of computed points along this path
fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) # Use a very low resolution conversion. Only 3 points for each bezier. nsvg <- nsvg_add_points(nsvg, n = 3)fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) # Use a very low resolution conversion. Only 3 points for each bezier. nsvg <- nsvg_add_points(nsvg, n = 3)
A simple demonstration of how we now have access to all the geometry
nsvg_explode(nsvg, scale = 2)nsvg_explode(nsvg, scale = 2)
nsvg |
'nsvg' object created using |
scale |
scale factor for explosion. Default: 2 |
Modified 'nsvg' object with all coordinates scaled by the given factors
fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) |> nsvg_scale(0.3) grid::grid.newpage(); plot(nsvg) nsvg <- nsvg_explode(nsvg, scale = 2) grid::grid.newpage(); plot(nsvg)fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) |> nsvg_scale(0.3) grid::grid.newpage(); plot(nsvg) nsvg <- nsvg_explode(nsvg, scale = 2) grid::grid.newpage(); plot(nsvg)
Some SVGs will have path closedness incorrectly set. This is a helper function to invert the closedness of all paths. Try this if the plot output is empty.
nsvg_invert_closedness(nsvg)nsvg_invert_closedness(nsvg)
nsvg |
'nsvg' object created using |
Modifed 'nsvg' object with the 'closed' status of all paths inverted
fn <- system.file("shuttle.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) |> nsvg_scale(0.3) grid::grid.newpage(); plot(nsvg) nsvg <- nsvg_invert_closedness(nsvg) grid::grid.newpage(); plot(nsvg)fn <- system.file("shuttle.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) |> nsvg_scale(0.3) grid::grid.newpage(); plot(nsvg) nsvg <- nsvg_invert_closedness(nsvg) grid::grid.newpage(); plot(nsvg)
Read an SVG as geometry
nsvg_read(filename, n = 20, units = "px", dpi = 96)nsvg_read(filename, n = 20, units = "px", dpi = 96)
filename |
SVG filename |
n |
number of points to use when converting each bezier to a polyline. Default: 20. Use "NA" to indicate that no conversion should be done. |
units |
units to use. Default 'px'. One of 'px', 'pt', 'pc' 'mm', 'cm', or 'in' |
dpi |
dots per inch. Default: 96 |
An nsvg object which is a data.frame of SVG data.
An SVG is made up of one-or-more shapes.
Each shape contains one-or-more paths. Each path
is made of one-or-more cubic beziers. Each shape has a
set of graphical parameters
shape_idx[int] Shape index. Each SVG is defined as a number of shapes, with each shape having a number of paths.
fill[chr] fill color
stroke[chr] stroke color
alpha[dbl] opacity in range [0, 1]
lwd[dbl] line width
linejoin[chr] Line join style. 'bevel', 'mitre' or 'round'
lineend[chr] Line end style. 'round', 'butt', 'square'
linemitre[dbl] Line mitre limit
linedash[list] Raw list of line dash lengths for each shape
fill_rule[chr] 'evenodd' or 'winding'
fill_type[chr] 'flat', 'linear', 'radial', 'none', 'undef'
gradient[list] Radial or linear gradient information for this shape
stroke_type[chr] 'flat', 'linear', 'radial', 'none', 'undef'
beziers[list] A list of data.frames - one data.frame for each shape containing the coordinates of the bezier control points (Note: there are 4 control points for each cubic bezier).
path_idx[int] Index of path within shape
bez_idx[int] Index of bezier with path
closed[lgl] Is the path closed?
x[dbl] x coordinate of bezier control points
y[dbl] y coordinate of bezier control points
lty[chr] Line type. Either 'solid' or a string of up to 8
characters (from c(1:9, "A":"F")) may be given, giving the length of
line segments which are alternatively drawn and skipped. See
?graphics::par for deails on Line Type Specification
points[list] A list of data.frames - one data.frame for each shape containing the polylines derived from the beziers
path_idx[int] Index of path within shape
bez_idx[int] Index of bezier with path
closed[lgl] Is the path closed?
x[dbl] x coordinate of polylines
y[dbl] y coordinate of polylines
fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) head(nsvg[, 1:8]) grid::grid.newpage() plot(nsvg) # Manually change the fill color for each shape nsvg$fill <- topo.colors(nrow(nsvg)) grid::grid.newpage() plot(nsvg)fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) head(nsvg[, 1:8]) grid::grid.newpage() plot(nsvg) # Manually change the fill color for each shape nsvg$fill <- topo.colors(nrow(nsvg)) grid::grid.newpage() plot(nsvg)
Scale nsvg object
nsvg_scale(nsvg, scalex = 1, scaley = scalex)nsvg_scale(nsvg, scalex = 1, scaley = scalex)
nsvg |
'nsvg' object created using |
scalex, scaley
|
scale factors. Default: 1 |
Modified 'nsvg' object with all coordinates scaled by the given factors
fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) grid::grid.newpage(); plot(nsvg) nsvg <- nsvg_scale(nsvg, 0.3) grid::grid.newpage(); plot(nsvg)fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) grid::grid.newpage(); plot(nsvg) nsvg <- nsvg_scale(nsvg, 0.3) grid::grid.newpage(); plot(nsvg)
Conver nsvg object to grid graphics objects
nsvg_to_grob(nsvg, n = 20, inverty = TRUE)nsvg_to_grob(nsvg, n = 20, inverty = TRUE)
nsvg |
nsvg object |
n |
if beziers not yet flatted to points, will use this value |
inverty |
logical. Invert the y-axis? Default: TRUE |
A grid graphics grob
fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) grob <- nsvg_to_grob(nsvg) grob grid::grid.draw(grob)fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) grob <- nsvg_to_grob(nsvg) grob grid::grid.draw(grob)
Unnest SVG bezier coordinates into a single long data.frame
nsvg_unnest_beziers(nsvg, inverty = TRUE, gpars = FALSE)nsvg_unnest_beziers(nsvg, inverty = TRUE, gpars = FALSE)
nsvg |
'nsvg' object created using |
inverty |
logical. Invert the y-axis? Default: TRUE |
gpars |
logical. Include graphical parameters in result? Default: FALSE |
data.frame with a row for each bezier control point.
fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) bezs <- nsvg_unnest_beziers(nsvg) head(bezs)fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) bezs <- nsvg_unnest_beziers(nsvg) head(bezs)
Unnest SVG polylines into a single long data.frame
nsvg_unnest_points(nsvg, inverty = TRUE, gpars = FALSE)nsvg_unnest_points(nsvg, inverty = TRUE, gpars = FALSE)
nsvg |
'nsvg' object created using |
inverty |
logical. Invert the y-axis? Default: TRUE |
gpars |
logical. Include graphical parameters in result? Default: FALSE |
data.frame with a row for each coordinate for each polyline calculated from the original beziers.
fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) points <- nsvg_unnest_points(nsvg) head(points)fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) points <- nsvg_unnest_points(nsvg) head(points)
Plot SVG object
## S3 method for class 'nsvg' plot(x, ...)## S3 method for class 'nsvg' plot(x, ...)
x |
nsvg object |
... |
ignored |
return nsvg object invisibly
fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) plot(nsvg)fn <- system.file("sailboat.svg", package = 'nanosvgr', mustWork = TRUE) nsvg <- nsvg_read(fn) plot(nsvg)