Read an svg file (or text) into a grid::grobTree object or data.frame

read_svg(
  svg_file,
  xoffset = 0,
  yoffset = 0,
  npoints = 30,
  scale = 1,
  default.units = "snpc",
  stroke_scale = 1,
  font_scale = 1,
  style_default = list(),
  user_css = NULL,
  obj_type = c("grob", "data.frame", "list", "debug"),
  false_colour = NULL
)

Arguments

svg_file

either a filename, or a single character string containing all the text of an SVG. Filenames may either be '.svg' or '.svgz' (gzip compressed SVG)

xoffset, yoffset

Extra offsets to element coordinates applied in the grob coordinate system (not the SVG coordinate system). Default: (0, 0)

npoints

number of segmentation points per section of bezier, arc, circle or ellipse. Default 30 Increase this number if the curves look too jaggy for your use case.

scale

Scale factor to apply to all coordinates. Default: 1.

default.units

the grid units to use throughout. The default ('snpc') is a pretty safe bet that will give you an auto-resizing vector object. Uou could also set it to an absolute unit (like 'mm') and then play with the scale argument to get a fixed size grid object.

stroke_scale

Default: 1. Multiplication factor for width of strokes. The value to use here is heavily dependent upon what size output you are rendering to.

font_scale

extra scaling applied to font parameters. Default: 1. The value to use here is heavily dependent upon the output size you are rendering to.

style_default

a named list of CSS properties which should override the defaults. default: list(). E.g. set style_default = list(fill = 'red') to set the default fill style for all elements to 'red'. This style will still be overridden by inline styles, css styles, or presentation attributes. It is a useful way of setting the 'color' property which is often used in SVG icon sets (which make heavy use of the 'currentColor' property)

user_css

single string containing CSS text e.g. "circle { fill: red !important; }". Note: Normal cascading style rules apply i.e. more specific rules override those with lower specificity, and inline style specifications have the highest specificty. You may need to use !important to override styles consistently.

obj_type

What kind of R object to return - choices c('grob', 'data.frame', 'list', 'debug'). Default: 'grob'.

The 'list' and 'data.frame' options are for advanced/debugging use, but some users may find them useful if trying to extract coordinate information etc.

The data.frame option could be used to recreate much of the SVG but it is missing key information such as clipping paths, and gradients (as these are pretty difficult to capture nicely in a data.frame).

The debug option returns all the possible information. Currently this returned object is undocumented. Use at your peril!

The list option returns a list containing the following elements for each parsed SVG element

  • svg - the SVG string for this element

  • tag - the SVG tag e.g. "path", "circle"

  • svg_df - data.frame of coordinates in SVG coordinate system

  • transform - the transform matrix for this element

  • grid_df - the transformed coordinates in R/grid coordinate space

  • style - caclculated style for this element. Named list of sttyle attributes

  • gp - the gpar() equivalent of the style

  • grob - the final generated grob for this element

false_colour

Use false colouring on all elements, by selecting random colours from palettes in grDevices Default: NULL means to use actual colours. Possible values: 'rainbow', 'hcl', 'heat', 'terrain', 'topo', 'cm'

Value

Return type determined by obj_type argument