| Title: | Read SVG as Grobs and Data Frames |
|---|---|
| Description: | Read SVG to grid graphics objects (i.e. grobs), and data.frames. |
| Authors: | mikefc |
| Maintainer: | mikefc <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.2 |
| Built: | 2026-05-08 08:15:29 UTC |
| Source: | https://github.com/coolbutuseless/svgparser |
Apply the given transform to x,y coordds
apply_transform(transform, coords_df)apply_transform(transform, coords_df)
transform |
matrix transform to apply |
coords_df |
data.frame containing x,y coords |
data.frame with updated coordinates
<path> arc to a data.frame of coordinatesReference https://svgwg.org/svg2-draft/implnote.html#ArcConversionEndpointToCenter
arc_to_df(params, npoints)arc_to_df(params, npoints)
params |
named list of arc parameters from the path segment i.e. xo, y0, x, y, arcflag, sweepflag, rx, ry, rot (degrees) |
npoints |
number of points along arc |
ToDo: actual rotation of arc coords by 'phi'
data.frame of coodinates of arc
Convert cubic and quadratic beziers to data.frame of coordinates along curve
bezier3_to_df(x, y, N = 5) bezier2_to_df(x, y, N = 5)bezier3_to_df(x, y, N = 5) bezier2_to_df(x, y, N = 5)
x, y
|
coords of control points |
N |
Number of output points |
data.frame of coords
Create a data.frame for a rectangle with rounded corners
create_rect_df(x, y, width, height, rx, ry, npoints)create_rect_df(x, y, width, height, rx, ry, npoints)
x, y, width, height, rx, ry
|
rect parameters |
npoints |
number of points along each of the rounded corners |
data.frame of (x, y) coordinates
Create a named list to store computed styles indexed by the xpath of each element
css_calc_all_styles(svg, user_css = NULL)css_calc_all_styles(svg, user_css = NULL)
svg |
root node of svg |
user_css |
user supplied CSS (as a single text string) |
list of style lookup by xpath
Convert a string value from CSS into a numeric value
css_value_as_numeric(x, percentage_as_fraction = TRUE, ...)css_value_as_numeric(x, percentage_as_fraction = TRUE, ...)
x |
Character string of a CSS value e.g. "12", "12px", "3em", "47%" |
percentage_as_fraction |
Should percentages like "12 as a fraction (e.g. 0.12)? Default: TRUE. If FALSE then returns the percentage as a numeric value (e.g. 12) |
... |
other arguments passed to |
a numeric value as best we can with limited knowledge
20x Faster than 'data.frame()', but with *ZERO* sanity checking and no support for row.names
data_frame(...)data_frame(...)
... |
named arguments |
data.frame
cx, cy, rx, ry are attribute parameters of the <ellipse> tag.
npoints is defined by the ser.
ellipse_to_df(cx, cy, rx, ry, npoints)ellipse_to_df(cx, cy, rx, ry, npoints)
cx, cy, rx, ry
|
ellipse parameters |
npoints |
number of points around ellipse |
data.frame of x,y coordinates around the ellipse
This function is called when a <use> tag references another element
or a a colour tag references a gradient element.
get_element_by_id(svg, id)get_element_by_id(svg, id)
svg |
xml document containing svg |
id |
id, #id, url(#id), url('#id') |
SVG element with the given id, otherwise NA
This converts a gpar object to a 1-row data.frame representation. This is used when accumulating a data.frame representation to return to the user.
gpar_to_df(gp)gpar_to_df(gp)
gp |
gpar object with a single specification. no vectors allowed |
Linear and Radial gradient objects are replaced with their first colour.
Although this could be something fancier e.g. list object, or some text
representation of the gradient parameters. Maybe just deparse1()?
data.frame with single row.
Break a string into labelled tokens based upon a set of patterns
lex(text, regexes, verbose = FALSE)lex(text, regexes, verbose = FALSE)
text |
a single character string |
regexes |
a named vector of regex strings. Each string represents
a regex to match a token, and the name of the string is the
label for the token. Each regex can contain an explicit
captured group using the standard |
verbose |
print more information about the matching process. default: FALSE |
a named character vector with the names representing the token type with the value being the element extracted by the corresponding regular expression.
## Not run: lex("hello there 123.45", regexes=c(number=re$number, word="(\\w+)", whitespace="(\\s+)")) ## End(Not run)## Not run: lex("hello there 123.45", regexes=c(number=re$number, word="(\\w+)", whitespace="(\\s+)")) ## End(Not run)
supertinyicons icon packLoad an SVG icon from the supertinyicons icon pack
load_supertinyicon( name, obj_type = c("grob", "data.frame", "list", "debug", "svg"), ... )load_supertinyicon( name, obj_type = c("grob", "data.frame", "list", "debug", "svg"), ... )
name |
name of icon. e.g. |
obj_type |
What kind of R object to return - choices
|
... |
other arguments passed to |
Parse tokens representing a single transform instruction into a matrix
parse_command_to_matrix(tokens)parse_command_to_matrix(tokens)
tokens |
character vector of c(command, number, number, ...) |
matrix representing this transform
Parse xml representing some SVG
parse_svg_group(elem, state)parse_svg_group(elem, state)
elem |
as SVG grouping element e.g. <svg> <g> <clipPath> |
state |
environment containing state information for this SVG |
Parse path into a list of parameters and meta-information
parse_svg_path_d(svg_path)parse_svg_path_d(svg_path)
svg_path |
SVG path string (single character string) |
list of lists of information - 1 list per element in the path. each element is itself a list of parameters and meta-information about the path element e.g. index of the element in the path
Parse an SVG tag of given type
parse_svg_radialGradient(elem, state) parse_svg_linearGradient(elem, state) parse_svg_switch(elem, state) parse_svg_image(elem, state) parse_svg_use(elem, state) parse_svg_path(elem, state) parse_svg_polyline(elem, state) parse_svg_polygon(elem, state) parse_svg_line(elem, state) parse_svg_rect(elem, state) parse_svg_circle(elem, state) parse_svg_ellipse(elem, state) parse_svg_text(elem, state)parse_svg_radialGradient(elem, state) parse_svg_linearGradient(elem, state) parse_svg_switch(elem, state) parse_svg_image(elem, state) parse_svg_use(elem, state) parse_svg_path(elem, state) parse_svg_polyline(elem, state) parse_svg_polygon(elem, state) parse_svg_line(elem, state) parse_svg_rect(elem, state) parse_svg_circle(elem, state) parse_svg_ellipse(elem, state) parse_svg_text(elem, state)
elem |
SVG element which is of known type |
state |
environment containing state information for this SVG |
grob
Note there can be multiple transform instructions in a transform string
parse_transform_string_to_matrices(transform_string)parse_transform_string_to_matrices(transform_string)
transform_string |
e.g. "rotate(10) translate(3, 3)" |
list of matrices
Note there can be multiple transform instructions in a transform string
parse_transform_string_to_matrix(transform_string)parse_transform_string_to_matrix(transform_string)
transform_string |
e.g. "rotate(10) translate(3, 3)" |
combined transform marix
Prepare complete list of control point coordinates for all beziers
path_list_expand_beziers(path_list, state)path_list_expand_beziers(path_list, state)
path_list |
path as returned by parse_svg_path_d |
state |
named list of current state. Default list(x=0, y=0) |
path_list with all bezier elements now having 'x' and 'y'
vectors with coords of the control points.
Convert path relative coordinates to absolute coordinates
path_list_to_abs_path_list(path_list, state = list(x = 0, y = 0))path_list_to_abs_path_list(path_list, state = list(x = 0, y = 0))
path_list |
path as returned by |
state |
named list of current state. Default |
path_list with only absolute elements
Convert a path list to a data.frame
path_list_to_df(path_list, state = list(x = 0, y = 0))path_list_to_df(path_list, state = list(x = 0, y = 0))
path_list |
path as returned by parse_svg_path_d |
state |
named list of current state. Default list(x=0, y=0) |
data.frame of x,y coordinates
This function expands all data.frames to have the same columns, whereas base R just complains a throws an error
rbind_dfs(dfs)rbind_dfs(dfs)
dfs |
list of data.frames |
single data.frame
readLines() in combination with unz() fails o read the last
line of a file if it does not end in a CR/LF. I haven't found a workaround
besides a manual read loop like below.
read_file(conn)read_file(conn)
conn |
connection. For svgparser this is always an |
This function reads all character in a file regardless of line endings or file endings.
single string representing file contents
grid::grobTree object or data.frame
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 )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 )
svg_file |
either a filename, or a single character string containing all the text of
an SVG. Filenames may either be |
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 ( |
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: |
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 |
obj_type |
What kind of R object to return - choices
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 The The
|
false_colour |
Use false colouring on all elements, by selecting random
colours from palettes in |
Return type determined by obj_type argument
As long as 'word' & 'words' are not empty, this function should always return a valid member of 'words'.
shotgun_match(word, words)shotgun_match(word, words)
word |
user word |
words |
list of all words |
gpar objectConvert a named list of CSS style information into a gpar object. E.g.
list(stroke = 'black') is converted to gpar(col = 'black').
style_to_gpar(style, state)style_to_gpar(style, state)
style |
named list of style properties for an element. E.g.
|
state |
state information. See inline documentation for |
This function handles stroke and fill colours (and separate alphas for each), fonts, linear and radial gradients, line parameters (width, join and end types etc).
Not yet done:
Conversion of line type information from CSS to gpar
gpar object
Currently this only supports setting the clipping path on the viewport.
style_to_viewport(style, transform, state)style_to_viewport(style, transform, state)
style |
named list of style properties |
transform |
transform matrix of element being clipped |
state |
state information |
Masks would also be set here, but I don't have any good examples to debug with, so leaving this for now.
viewport object
supertinyicons setNames of icons in supertinyicons set
supertinyicon_namessupertinyicon_names
An object of class character of length 297.
Handles: False colouring, hex colours, oct colours, radial gradients.
svg_colour_to_r_colour(col, state)svg_colour_to_r_colour(col, state)
col |
svg colour |
state |
environment containing state information for this SVG |
More complex CSS colour specifications like rgb(10, 20, 30, 0.5)
are handled by cssparser::css_colour_to_hex()
valid R colour
Trim the end off a word.
trim(x, len)trim(x, len)
x |
string |
len |
number of characters to trim |
Update a transform matrix with an element
update_transform(transform, elem)update_transform(transform, elem)
transform |
matrix |
elem |
svg element |
new transform matrix
Update a transform matrix with the new SVG transform string
update_transform_with_string(transform, transform_string)update_transform_with_string(transform, transform_string)
transform |
matrix |
transform_string |
the transform string which is a presentation attribute on an element |
new transform matrix