R/grob_utils.R
7539e693
 # `[.grob` = function(x, i) {
 # 	x2 = x
 # 	for(nm in SUBSETABLE_FIELDS[[ intersect(names(SUBSETABLE_FIELDS), class(x)) ]]) {
 # 		if(inherits(x2[[nm]], "gpar")) {
 # 			# change to the class defined here
 # 			class(x2[[nm]]) = "gpar"
 # 		}
5b47a845
 
7539e693
 # 		if(length(x2[[nm]]) > 1) {
 # 			x2[[nm]] = x2[[nm]][i]
 # 		}
 # 	}
 # 	x2
 # }
5b47a845
 
7539e693
 # `[.gpar` = function(x, i) {
 # 	lapply(x, function(y) {
 # 		if(length(y) > 1) {
 # 			y[i]
 # 		} else {
 # 			y
 # 		}
 # 	})
 # }
5b47a845
 
7539e693
 # SUBSETABLE_FIELDS = list(
 # 	"text" = c("label", "x", "y", "gp"),
 # 	"richtext_grob" = c("gp", "children", "childrenOrder")
 # )
5b47a845
 
7539e693
 # length.text = function(x) {
 # 	length(x$label)
 # }
5b47a845
 
7539e693
 # length.richtext_grob = function(x) {
 # 	length(x$children)
 # }
5b47a845
 
7539e693
 # update_xy = function (gb, x, y, ...) {
 # 	UseMethod("update_xy")
 # }
5b47a845
 
7539e693
 # update_xy.text = function(gb, x, y, ...) {
 # 	n = length(gb$label)
 # 	if(!missing(x)) {
 # 		if(n > 1 & length(x) > 1 && n != length(x)) {
 # 			stop_wrap("Length of `x` should be the same as the length of labels.")
 # 		}
 # 		gb$x = x
 # 	}
 # 	if(!missing(y)) {
 # 		if(n > 1 & length(y) > 1 && n != length(y)) {
 # 			stop_wrap("Length of `y` should be the same as the length of labels.")
 # 		}
 # 		gb$y = y
 # 	}
 # 	gb
 # }
5b47a845
 
7539e693
 # update_xy.richtext_grob = function(gb, x, y, ...) {
 # 	n = length(gb$children)
5b47a845
 
7539e693
 # 	if(!missing(x)) {
 # 		if(n > 1 & length(x) > 1 && n != length(x)) {
 # 			stop_wrap("Length of `x` should be the same as the length of labels.")
 # 		}
 # 		for(i in 1:n) {
 # 			if(length(x) == 1) {
 # 				gb$children[[i]]$vp$x = x
 # 			} else {
 # 				gb$children[[i]]$vp$x = x[i]
 # 			}
 # 		}
 # 	}
 # 	if(!missing(y)) {
 # 		if(n > 1 & length(y) > 1 && n != length(y)) {
 # 			stop_wrap("Length of `y` should be the same as the length of labels.")
 # 		}
 # 		for(i in 1:n) {
 # 			if(length(y) == 1) {
 # 				gb$children[[i]]$vp$y = y
 # 			} else {
 # 				gb$children[[i]]$vp$y = y[i]
 # 			}
 # 		}
 # 	}
 # 	gb
 # }
5b47a845
 
7539e693
 # textGrob = function(label, ...) {
 # 	if(inherits(label, "grob")) {
 # 		return(label)
 # 	} else {
 # 		grid::textGrob(label, ...)
 # 	}
 # }
5b47a845
 
7539e693
 # grid.text = function(label, x = unit(0.5, "npc"), y = unit(0.5, "npc"), ...) {
 # 	if(inherits(label, "grob")) {
 # 		gb = label
 # 		gb = update_xy(gb, x, y)
 # 		grid.draw(gb)
 # 	} else {
 # 		grid::grid.text(label, x, y, ...)
 # 	}
 # }