1##################################################################
2## spacings
3
4spacing_equal <- function(sp = unit(0.3, "lines")) {
5  if (!is.unit(sp)) sp <- unit(sp, "lines")
6  function(d, condvars = NULL) lapply(d, function(x) if(x > 1) rep(sp, x - 1) else NA)
7}
8class(spacing_equal) <- "grapcon_generator"
9
10spacing_dimequal <- function(sp) {
11  if (!is.unit(sp)) sp <- unit(sp, "lines")
12  function(d, condvars = NULL)
13    lapply(seq_along(d), function(i) if(d[i] > 1) rep(sp[i], d[i] - 1) else NA)
14}
15class(spacing_dimequal) <- "grapcon_generator"
16
17spacing_increase <- function(start = unit(0.3, "lines"), rate = 1.5) {
18  if (!is.unit(start)) start <- unit(start, "lines")
19  function(d, condvars = NULL) {
20    sp <- start * rev(cumprod(c(1, rep.int(rate, length(d) - 1))))
21    spacing_dimequal(sp)(d = d, condvars = condvars)
22  }
23}
24class(spacing_increase) <- "grapcon_generator"
25
26spacing_highlighting <- function(start = unit(0.2, "lines"), rate = 1.5) {
27 if (!is.unit(start)) start <- unit(start, "lines")
28  function(d, condvars = NULL)
29    c(spacing_increase(start, rate)(d, condvars)[-length(d)],
30      list(unit(rep(0, d[length(d)]), "lines")))
31}
32class(spacing_highlighting) <- "grapcon_generator"
33
34spacing_conditional <- function(sp = unit(0.3, "lines"),
35                                start = unit(2, "lines"), rate = 1.8) {
36  condfun <- spacing_increase(start, rate)
37  equalfun <- spacing_equal(sp)
38  equalfun2 <- spacing_equal(start)
39  function(d, condvars) {
40    if (length(d) < 3)
41      return(spacing_equal(sp)(d, condvars))
42    condvars <- seq(condvars)
43    ret <- vector("list", length(d))
44    ret[condvars] <- if (length(condvars) < 3)
45      equalfun2(d[condvars])
46    else
47      condfun(d[condvars])
48    ret[-condvars] <- equalfun(d[-condvars])
49    ret
50  }
51}
52class(spacing_conditional) <- "grapcon_generator"
53