Skip to content

Commit 749d520

Browse files
committed
Fixes for Fedora
1 parent bddc6ab commit 749d520

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ importFrom(httr,GET)
6060
importFrom(httr,content)
6161
importFrom(jsonlite,fromJSON)
6262
importFrom(methods,is)
63+
importFrom(methods,slotNames)
6364
importFrom(pbapply,pblapply)
6465
importFrom(png,readPNG)
6566
importFrom(rsvg,rsvg_png)

R/add_phylopic_base.r

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
#' @importFrom graphics par grconvertX grconvertY
5050
#' @importFrom grid grid.raster
5151
#' @importFrom grImport2 grid.picture
52-
#' @importFrom methods is
52+
#' @importFrom methods is slotNames
5353
#' @export
5454
#' @examples
5555
#' # single image
@@ -168,7 +168,16 @@ add_phylopic_base <- function(img = NULL, name = NULL, uuid = NULL,
168168

169169
# grobify and plot
170170
if (is(img, "Picture")) { # svg
171-
grid.picture(img, x = x, y = y, height = ysize, expansion = 0)
171+
if ("summary" %in% slotNames(img) &&
172+
all(c("xscale", "yscale") %in% slotNames(img@summary)) &&
173+
is.numeric(img@summary@xscale) && length(img@summary@xscale) == 2 &&
174+
all(is.finite(img@summary@xscale)) && diff(img@summary@xscale) != 0 &&
175+
is.numeric(img@summary@yscale) && length(img@summary@yscale) == 2 &&
176+
all(is.finite(img@summary@yscale)) && diff(img@summary@yscale) != 0) {
177+
grid.picture(img, x = x, y = y, height = ysize, expansion = 0)
178+
} else {
179+
return(NULL)
180+
}
172181
} else { # png
173182
grid.raster(img, x = x, y = y, height = ysize)
174183
}

R/geom_phylopic.R

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ GeomPhylopic <- ggproto("GeomPhylopic", Geom,
184184
)
185185

186186
#' @importFrom grImport2 pictureGrob
187-
#' @importFrom grid rasterGrob gList gTree
187+
#' @importFrom grid rasterGrob gList gTree nullGrob
188+
#' @importFrom methods slotNames
188189
phylopicGrob <- function(img, x, y, height, color, alpha,
189190
horizontal, vertical, angle,
190191
remove_background) {
@@ -198,12 +199,21 @@ phylopicGrob <- function(img, x, y, height, color, alpha,
198199

199200
# grobify
200201
if (is(img, "Picture")) { # svg
201-
# modified from
202-
# https://github.com/k-hench/hypoimg/blob/master/R/hypoimg_recolor_svg.R
203-
img_grob <- pictureGrob(img, x = x, y = y, height = height,
204-
default.units = "native", expansion = 0)
205-
img_grob <- gList(img_grob)
206-
img_grob <- gTree(children = img_grob)
202+
if ("summary" %in% slotNames(img) &&
203+
all(c("xscale", "yscale") %in% slotNames(img@summary)) &&
204+
is.numeric(img@summary@xscale) && length(img@summary@xscale) == 2 &&
205+
all(is.finite(img@summary@xscale)) && diff(img@summary@xscale) != 0 &&
206+
is.numeric(img@summary@yscale) && length(img@summary@yscale) == 2 &&
207+
all(is.finite(img@summary@yscale)) && diff(img@summary@yscale) != 0) {
208+
# modified from
209+
# https://github.com/k-hench/hypoimg/blob/master/R/hypoimg_recolor_svg.R
210+
img_grob <- pictureGrob(img, x = x, y = y, height = height,
211+
default.units = "native", expansion = 0)
212+
img_grob <- gList(img_grob)
213+
img_grob <- gTree(children = img_grob)
214+
} else {
215+
img_grob <- nullGrob()
216+
}
207217
} else { # png
208218
img_grob <- rasterGrob(img, x = x, y = y, height = height,
209219
default.units = "native")

R/phylopic_utils.R

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,28 +231,31 @@ rgb_to_rgba <- function(img) {
231231
recolor_phylopic.Picture <- function(img, alpha = 1, color = NULL,
232232
remove_background = TRUE) {
233233
img <- recolor_content(img, alpha, color, remove_background)
234+
if (length(img@content) == 0) stop("Invalid 'Picture' object")
234235
return(img)
235236
}
236237

238+
#' @importFrom methods slotNames
237239
recolor_content <- function(x, alpha, color, remove_background) {
238-
if (is(x@content[[1]], "PicturePath")) {
239-
tmp <- lapply(x@content, function(path) {
240+
tmp <- lapply(x@content, function(element) {
241+
if (is(element, "PicturePath")) {
240242
# a bit of a hack until PhyloPic fixes these white backgrounds
241-
if (remove_background && path@gp$fill %in% c("#FFFFFFFF", "#FFFFFF")) {
242-
NULL
243+
if (remove_background && "gp" %in% slotNames(element) &&
244+
"fill" %in% names(element@gp) &&
245+
element@gp$fill %in% c("#FFFFFFFF", "#FFFFFF")) {
246+
return(NULL)
243247
} else {
244-
path@gp$alpha <- alpha
248+
element@gp$alpha <- alpha
245249
if (!is.null(color)) {
246-
path@gp$fill <- color
250+
element@gp$fill <- color
247251
}
248-
path
252+
return(element)
249253
}
250-
})
251-
x@content <- Filter(function(path) !is.null(path), tmp)
252-
return(x)
253-
} else { # need to go another level down
254-
x@content <- lapply(x@content, recolor_content, alpha = alpha,
255-
color = color, remove_background = remove_background)
256-
return(x)
257-
}
254+
} else if (is(element, "PictureGroup")) {
255+
# need to go another level down
256+
recolor_content(element, alpha, color, remove_background)
257+
}
258+
})
259+
x@content <- Filter(function(element) !is.null(element), tmp)
260+
return(x)
258261
}

0 commit comments

Comments
 (0)