-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathsprite.R
More file actions
76 lines (71 loc) · 2.71 KB
/
Copy pathsprite.R
File metadata and controls
76 lines (71 loc) · 2.71 KB
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
idExists <- function(id, sprite) {
sprite_sym <- xml2::xml_text(xml2::xml_find_all(
sprite, "/*[name()='svg']/*[name()='symbol']/@id"))
if(id %in% sprite_sym) return(TRUE) else return(FALSE)
}
#' Add svg to a sprite map
#'
#' @param newsvg \code{xml_document} object with svg that will be added
#' @param sprite \code{xml_document} object with the sprite map
#' @param id sets the id of the new svg inside the sprite map, if is
#' NULL it looks for the id inside the \code{newsvg} object.
#'
#' @return invisible copy of new sprite map
#'
#' @details the sprite map object is modified in place
#'
#' @examples
#' library(xml2)
#' sprite <- xml_new_root("svg", "version" = "1.1", "xmlns" = "http://www.w3.org/2000/svg")
#' newsvg <- read_xml("<svg version='1.1' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 68'>
#' <path d='M55.096 10.462c1.338-1.374.14-3.699-1.322-3.136-1.482.572-1'/>
#' </svg>")
#' addsvg_sprite(newsvg, sprite, "new-icon")
#' @export
addsvg_sprite <- function(newsvg, sprite, id = NULL) {
if(!requireNamespace("xml2", quietly = TRUE)) {
stop("Package \"xml2\" needed for this function to work.
Please install it", call. = FALSE)
}
if(is.null(id)) {
id <- xml2::xml_attr(newsvg, "id")
if(is.na(id)) stop("id is NULL and newsvg does not have id")
}
stopifnot(!idExists(id, sprite))
viewBox <- xml2::xml_attr(newsvg, "viewBox")
root_newsvg <- xml2::xml_new_root("symbol", "id" = id,
"viewBox" = viewBox)
xml2::xml_add_child(root_newsvg, xml2::xml_child(newsvg))
xml2::xml_add_child(sprite,
xml2::xml_find_first(root_newsvg, "/*[name()='symbol']"))
invisible(sprite)
}
#' Remove svg from a sprite map
#'
#' @param id symbol id of the svg to be removed
#' @param sprite \code{xml_document} object with the sprite map
#'
#' @return invisible copy of new sprite map
#'
#' @details sprite map is modifyed on place
#'
#' @examples
#' library(xml2)
#' sprite <- xml_new_root("svg", "version" = "1.1", "xmlns" = "http://www.w3.org/2000/svg")
#' newsvg <- read_xml("<svg version='1.1' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 68'>
#' <path d='M55.096 10.462c1.338-1.374.14-3.699-1.322-3.136-1.482.572-1'/>
#' </svg>")
#' addsvg_sprite(newsvg, sprite, "new-icon")
#' rmsvg_sprite("new-icon", sprite)
#' @export
rmsvg_sprite <- function(id, sprite) {
if(!requireNamespace("xml2", quietly = TRUE)) {
stop("Package \"xml2\" needed for this function to work.
Please install it", call. = FALSE)
}
stopifnot(idExists(id, sprite))
sym2remove <- xml2::xml_find_first(sprite,
paste0("/*[name()='svg']/*[name()='symbol'][@id='",id,"']"))
xml2::xml_remove(sym2remove)
invisible(sprite)
}