R/page_template.R

Defines functions pad_any page_info get_page_footer get_footnotes get_title_header get_page_by get_titles get_page_header page_template_text

# The page template is everything except the content: page header/footer,
# titles, footnotes, etc.


# Page Template Functions ----------------------------------------------

#' Create a page template with header, titles, footnotes, and footer
#' @param rs The report spec
#' @return The page template object
#' @noRd
page_template_text <- function(rs) {
  
  pt <- structure(list(), class = c("page_template_text", "list"))
  
  pt$page_header <- get_page_header(rs)
  pt$title_hdr <- get_title_header(rs$title_hdr, rs$line_size, rs$line_size, 
                                   rs$uchar, rs$char_width)
  pt$titles <- get_titles(rs$titles, rs$line_size, rs$line_size, 
                          rs$uchar, rs$char_width)
  pt$footnotes <- c()
  if (!is.null(rs$footnotes)) {
    if (!is.null(rs$footnotes[[1]])) {
      if (rs$footnotes[[1]]$valign == "bottom")
        pt$footnotes <- get_footnotes(rs$footnotes, rs$line_size, rs$line_size, 
                                      rs$uchar, rs$char_width)
    }
  
  }
  pt$page_footer <- get_page_footer(rs)
  # Page by not here.  Messes up line counts.
  
  return(pt)
}

#' Get page header text strings suitable for printing
#' @param rs The report spec
#' @return A vector of strings
#' @noRd
get_page_header <- function(rs) {
  
  if (is.null(rs$line_size)) {
    stop("line_size cannot be null.") 
    
  }
  
  phdrr <- rs$page_header_right
  phdrl <- rs$page_header_left
  phdr <- rs$page_header_left
  if(length(phdrl) < length(phdrr))
    phdr <- phdrr
  
  ret <- c()
  
  if (!is.null(phdr)) {
    for (i in seq_along(phdr)) {
      
      hl <- ""
      hr <- ""
      
      if (length(phdrl) >= i)
        hl <- phdrl[[i]]
      
      if (length(phdrr) >= i)
        hr <- phdrr[[i]]
      
      gp <- rs$line_size - (get_nchar(hl) + get_nchar(hr))
      
      #print("header")
      if (gp >= 0) {
        
        lw <- rs$line_size - get_nchar(hr)
        ln <- paste0(pad_right(hl, lw), hr) 
      }
      
      else {
        # stop(paste0("Page header exceeds available width\n", 
        #             "Header Left: ", hl, "\n", 
        #             "Header Right: ", hr, "\n",
        #             "Header length: ", nchar(hl) + nchar(hr), "\n",
        #             "Line length: ", rs$line_size, "\n"))
        # ln <- ""
        lw <- round(rs$line_size/2)
        rw <- round(rs$line_size/2) - 1
        
        hl_split <- stri_wrap(unlist(
          strsplit(hl, split = "\n", fixed = TRUE)),
          width = lw, normalize = FALSE)
        
        hr_split <- stri_wrap(unlist(
          strsplit(hr, split = "\n", fixed = TRUE)),
          width = rw, normalize = FALSE)
        
        max_line <- max(length(hl_split), length(hr_split))
        
        ln <- ""
        for (k in 1:max_line) {
          
          if (k <= length(hl_split)) {
            hl_s <- hl_split[k]
          } else {
            hl_s <- ""
          }
          
          if (k <= length(hr_split)) {
            hr_s <- hr_split[k]
          } else {
            hr_s <- ""
          }
          
          cat <- ""
          if (k > 1) {
            cat <- "\n"
          }
          
          ln <- paste0(ln, cat, pad_right(hl_s, lw + 1), hr_s)
        }
        
      }
      
      ret[i] <- ln
    }
    
    if (length(ret) > 0 & rs$page_header_blank_row == "below") 
      ret[[length(ret) + 1]] <- "" 
    
  }
  
  return(ret)
}

#' Get title text strings suitable for printing
#' @import stringi
#' @param titles A list of title objects
#' @param width The width to set the title strings to
#' @return A vector of strings
#' @noRd
get_titles <- function(titles, content_width, page_width, uchar, char_width) {
  
  if (is.null(content_width)) {
    stop("width cannot be null.") 
    
  }
  
  ret <- c()
  

  
  if (!is.null(titles)) { 
    
    for (ttl in titles) {
      
      if (!any(class(ttl) == "title_spec")) {
        stop("titles parameter value is not a title spec.")
      }
      
      cols <- ttl$columns
      
      if (ttl$width == "page")
        width <- page_width
      else if (ttl$width == "content")
        width <- content_width
      else if (is.numeric(ttl$width))
        width <- ceiling(ttl$width / char_width)
      
      # Split the strings and distribute to columns
      col_width <- round(width / cols)
      
      string_list <- vector(mode = "list", length = cols)
      
      for (i in 1:length(ttl$titles)) {
        
        s <- ttl$titles[i]
        
        s_split <- stri_wrap(unlist(
          strsplit(s, split = "\n", fixed = TRUE)),
          width = col_width, normalize = FALSE)
        
        col_num <- i %% cols
        col_num <- ifelse(col_num == 0, cols, col_num)
        
        string_list[[col_num]] <- append(string_list[[col_num]], s_split)
      }
      
      string_matrix <- stri_list2matrix(string_list, byrow = FALSE, fill = "")

      # Deal with blank lines and border          
      if (any(ttl$blank_row %in% c("above", "both")) & length(ttl$titles) > 0)
        ret[length(ret) + 1] <- ""
      
      if (any(ttl$borders %in% c("top", "all")) & length(ttl$titles) > 0)
        ret[length(ret) + 1] <-  paste0(rep(uchar, width), 
                                               collapse = "")
      
      algn <- ttl$align
      if (algn == "centre"){
        algn <- "center"
      }
      
      # Loop through all the split strings
      for (row in 1:nrow(string_matrix)) {
        ln <- ""
        for (col in 1:ncol(string_matrix)) {
          
          # Deal with column alignments
          if (cols == 1) {
            calgn <- algn
          } else if (cols == 2) {
            if (col == 1) {
              calgn <- "left"
            } else {
              calgn <- "right"
            }
          } else if (cols == 3) {
            if (col == 1) {
              calgn <- "left"
            } else if (col == 2) {
              calgn <- "center"
            } else if (col == 3) {
              calgn <- "right"
            }
          }
          
          string_value <- string_matrix[row, col]

          if (calgn == "left") {
            ln <- paste0(ln, pad_right(string_value, col_width))
          } else if (calgn == "right") {
            ln <- paste0(ln, pad_left(string_value, col_width))
          } else if (calgn == "center") {
            ln <- paste0(ln, pad_both(string_value, col_width))
          }
          
        }
        
        ret[length(ret) + 1] <- ln
      }
      
      # i <- 1
      # while (i <= length(ttl$titles)) {
      #   
      #   ln <- ""
      #   
      #   for (j in seq_len(cols)) {
      # 
      #     # Not all cells have titles
      #     if (i > length(ttl$titles))
      #       t <- ""
      #     else 
      #       t <- ttl$titles[[i]]
      #     
      #     # Deal with column alignments
      #     if (cols == 1) {
      #       calgn <- algn 
      #     } else if (cols == 2) {
      #       if (j == 1)
      #         calgn <- "left"
      #       else 
      #         calgn <- "right"
      #     } else if (cols == 3) {
      #       if (j == 1)
      #         calgn <- "left"
      #       else if (j == 2)
      #         calgn <- "center"
      #       else if (j == 3) 
      #         calgn <- "right"
      #     }
      #     
      #     gp <- cll - nchar(t)
      #     
      #     #print("titles")
      #     if (gp > 0) {
      #       
      #       if (calgn == "left")
      #         ln <- paste0(ln, pad_right(t, cll))
      #       else if (calgn == "right")
      #         ln <- paste0(ln, pad_left(t, cll))
      #       else if (calgn == "center")
      #         ln <- paste0(ln, pad_both(t, cll))
      #       
      #     } else {
      #       # warning(paste0("Title exceeds available width.",
      #       #             "\nTitle: ", t,
      #       #             "\nTitle width: ", nchar(t),
      #       #             "\nLine length: ", cll))
      #       # 
      #       # tgp <- cll - 3
      #       # 
      #       # if (tgp >= 0) {
      #       #   if (ttl$align == "left") {
      #       #     ln <- paste0(substr(pad_right(t, cll), 1, tgp), "...")
      #       #   } else if (ttl$align == "right") {
      #       #     ln <- paste0("...", substr(pad_left(t, cll), 1, tgp))
      #       #   } else if (ttl$align == "center" | ttl$align == "centre") {
      #       #     ln <- paste0(substr(pad_both(t, cll), 1, tgp), "...")
      #       #   }
      #       #   
      #       # } else ln <- ""
      #       
      #       # Split string
      #       title_split <- stri_wrap(unlist(
      #         strsplit(t, split = "\n", fixed = TRUE)),
      #         width = cll, normalize = FALSE)
      #       
      #       cat <- ""
      #       for (k in 1:length(title_split)) {
      #         if (k > 1) {
      #           cat <- "\n"
      #         }
      #         
      #         st <- title_split[k]
      #         
      #         if (calgn == "left") {
      #           ln <- paste0(ln, cat, pad_right(st, cll))
      #         } else if (calgn == "right") {
      #           ln <- paste0(ln, cat, pad_left(st, cll))
      #         } else if (calgn == "center") {
      #           ln <- paste0(ln, cat, pad_both(st, cll))
      #         }
      #       }
      #       
      #     }
      #     
      #     i <- i + 1
      #   
      #   }
      #   
      #   ret[length(ret) + 1] <- ln
      # }
      
      if (any(ttl$borders %in% c("bottom", "all")) & length(ttl$titles) > 0)
        ret[length(ret) + 1] <- paste0(rep(uchar, width),  collapse = "")
      
      if (any(ttl$blank_row %in% c("below", "both")) & length(ttl$titles) > 0)
        ret[length(ret) + 1] <- ""
    }
    
  }
  
  
  return(ret)
}


#' Get page by text strings suitable for printing
#' @import stringi
#' @param titles Page by object
#' @param width The width to set the page by strings to
#' @return A vector of strings
#' @import stringi
#' @noRd
get_page_by <- function(pgby, width, value, pgby_cnt = NULL) {
  
  if (is.null(width)) {
    stop("width cannot be null.") 
    
  }
  
  # Set up fake values if there is no value yet.
  # Needed for estimation of header line counts
  if (is.null(value)) {
    value <- get_pgby_value(value, pgby_cnt)
  }
  
  ll <- width
  ret <- c()
  
  if (!is.null(pgby)) { 
    
    if (!any(class(pgby) == "page_by"))
      stop("pgby parameter value is not a page_by.")
    
    if (pgby$blank_row %in% c("above", "both"))
      ret[length(ret) + 1] <- ""
  
    # Don't display "NA" when value is NA to differentiate real character "NA"
    value <- ifelse(is.na(value),"",value)
    pb <- paste0(pgby$label, value)
    
    lns <- unlist(stri_split_fixed(pb, "\n"))
    
    for (pbln in lns) {
    
      gp <- ll - get_nchar(pbln) 
      
  
      if (gp > 0) {
        
        if (pgby$align == "left")
          ln <- pad_right(pbln, ll)
        else if (pgby$align == "right")
          ln <- pad_left(pbln, ll)
        else if (pgby$align == "center" | pgby$align == "centre")
          ln <- pad_both(pbln, ll)
        
        ret[length(ret) + 1] <- ln
      } else {
        # stop("Page by exceeds available width.")
        s_split <- stri_wrap(unlist(
          strsplit(pbln, split = "\n", fixed = TRUE)),
          width = ll, normalize = FALSE)
        
        for (s in s_split) {
          if (pgby$align == "left") {
            ln <- pad_right(s, ll)
          } else if (pgby$align == "right") {
            ln <- pad_left(s, ll)
          } else if (pgby$align == "center" | pgby$align == "centre") {
            ln <- pad_both(s, ll)
          }
          ret[length(ret) + 1] <- ln
        }
      }
    }
    
    
    if (pgby$blank_row %in% c("below", "both"))
      ret[length(ret) + 1] <- ""
    
    
  }
  
  
  return(ret)
}

#' Get title header text strings suitable for printing
#' @import stringi
#' @param title_hdr A title_hdr object
#' @param width The width to set the title header to
#' @return A vector of strings
#' @noRd
get_title_header <- function(title_hdr, content_width, page_width, 
                             uchar = "-", char_width) {
  
  if (is.null(content_width)) {
    stop("width cannot be null.") 
    
  }
  
  ret <- c()
  
  if (!is.null(title_hdr)) { 
    
    for (ttl_hdr in title_hdr) {
      
      if (!any(class(ttl_hdr) == "title_hdr"))
        stop("title header parameter value is not a title header.")
      
      if (ttl_hdr$width == "page")
        width <- page_width
      else if (ttl_hdr$width == "content")
        width <- content_width
      else if (is.numeric(ttl_hdr$width))
        width <- ceiling(ttl_hdr$width / char_width)
      
      ll <- width 
      
      if (ttl_hdr$blank_row %in% c("above", "both") & length(ttl_hdr$titles) > 0)
        ret[length(ret) + 1] <- ""
      
      if (any(ttl_hdr$borders %in% c("top", "all")) & length(ttl_hdr$titles) > 0) {
        #ret[length(ret) + 1] <-  paste0(paste0(rep(uchar, ll), collapse = ""), " ")
        ret[length(ret) + 1] <-  paste0(rep(uchar, ll), collapse = "")
      }
      
      maxlen <- length(ttl_hdr$titles)
      if (length(ttl_hdr$right) > maxlen)
        maxlen <- length(ttl_hdr$right)
      
      hdr <- ttl_hdr$right
      
      lw <- round(width/2)
      rw <- round(width/2)
      
      if (length(hdr) == 1){
        if (hdr == "") {
          lw <- width
          rw <- 0
        }
      }
      
      if (is.null(ttl_hdr$titles)){
        lw <- 0
        rw <- width
        ttl_hdr_titles <- ""
      } else {
        ttl_hdr_titles <- ttl_hdr$titles
      }
      
      # Split the strings and distribute to columns
      string_list <- vector(mode = "list", length = 2)
      
      string_list[[1]] <- stri_wrap(unlist(
        strsplit(ttl_hdr_titles, split = "\n", fixed = TRUE)),
        width = lw, normalize = FALSE)
      
      string_list[[2]] <- stri_wrap(unlist(
        strsplit(hdr, split = "\n", fixed = TRUE)),
        width = lw, normalize = FALSE)
      
      string_matrix <- stri_list2matrix(string_list, byrow = FALSE, fill = "")
      
      for (row in 1:nrow(string_matrix)) {
        ln <- ""
        for (col in 1:ncol(string_matrix)) {
          
          # Deal with column alignments
          if (col == 1) {
            calgn <- "left"
          } else if (col == 2) {
            calgn <- "right"
          }
          
          string_value <- string_matrix[row, col]
          
          if (calgn == "left") {
            ln <- paste0(ln, pad_right(string_value, lw))
          } else if (calgn == "right") {
            ln <- paste0(ln, pad_left(string_value, rw))
          } 
          
        }
        
        ret[length(ret) + 1] <- ln
      }
      
      # for (i in seq_len(maxlen)) {
      #   
      #   if (i <= length(ttl_hdr$titles))
      #     t <- ttl_hdr$titles[i]
      #   else 
      #     t <- ""
      #   
      #   if (i <= length(hdr))
      #     h <- hdr[i]
      #   else 
      #     h <- ""
      #   
      #   # gp <- ll - nchar(t) - nchar(h)
      #   # 
      #   # #print("titles")
      #   # if (gp >= 0) {
      #   #   
      #   # 
      #   #     #ln <- paste0(pad_right(t, ll - nchar(h)), h, " ")
      #   #     ln <- paste0(pad_right(t, ll - nchar(h)), h)
      #   # 
      #   #   
      #   # } else {
      #   #   warning(paste0("Title header exceeds available width.\n",
      #   #               "Title: ", t, "\n",
      #   #               "Header: ", h, "\n",
      #   #               "Title length: ", nchar(t), "\n",
      #   #               "Header length: ", nchar(h), "\n",
      #   #               "Line length: ", ll, "\n"))
      #   # 
      #   #   tgp <- ll - 3
      #   #   if (tgp >= 0) {
      #   # 
      #   #     ln <- paste0(substr(paste0(pad_right(t, ll - nchar(h)), h, " "),
      #   #                         1, tgp), "...")
      #   # 
      #   #   } else ln <- ""
      #   # }
      #   
      #   ret[length(ret) + 1] <- ln
      # }
      
      if (any(ttl_hdr$borders %in% c("bottom", "all")) & 
          length(ttl_hdr$titles) > 0) {
        #ret[length(ret) + 1] <-  paste0(paste0(rep(uchar, ll), collapse = ""), " ")
        ret[length(ret) + 1] <-  paste0(rep(uchar, ll), collapse = "")
        
      }
      
      if (ttl_hdr$blank_row %in% c("below", "both") & 
          length(ttl_hdr$titles) > 0)
        ret[length(ret) + 1] <- ""
  
    
    }
  }
  
  
  return(ret)
}



#' Get footnote text strings suitable for printing
#' @param rs The report spec
#' @return A vector of strings
#' @noRd
get_footnotes <- function(footnotes, content_width, page_width, 
                          uchar = "-", char_width) {
  
  if (is.null(char_width)) {
    stop("width cannot be null.") 
    
  }
  
  ret <- c()
  
  if (!is.null(footnotes)) {
    for (ftn in footnotes) {
      
      if (!any(class(ftn) == "footnote_spec"))
        stop("footnotes parameter value is not a footnote spec.")
      
      cols <- ftn$columns
      
      if (ftn$width == "page")
        width <- page_width
      else if (ftn$width == "content")
        width <- content_width
      else if (is.numeric(ftn$width))
        width <- ceiling(ftn$width / char_width)
      
      # Split the strings and distribute to columns
      col_width <- round(width / cols)
      
      string_list <- vector(mode = "list", length = cols)
      
      for (i in 1:length(ftn$footnotes)) {
        
        s <- ftn$footnotes[i]
        
        s_split <- stri_wrap(unlist(
          strsplit(s, split = "\n", fixed = TRUE)),
          width = col_width, normalize = FALSE)
        
        col_num <- i %% cols
        col_num <- ifelse(col_num == 0, cols, col_num)
        
        string_list[[col_num]] <- append(string_list[[col_num]], s_split)
      }
      
      string_matrix <- stri_list2matrix(string_list, byrow = FALSE, fill = "")
      
      # Deal with blank lines and border
      if (ftn$blank_row %in% c("above", "both") & length(ftn$footnotes) > 0)
        ret[length(ret) + 1] <- ""
      
      if (any(ftn$borders %in% c("top", "all")) & length(ftn$footnotes) > 0) {
        # ret[length(ret) + 1] <- paste0(paste0(rep(uchar, ll), 
        #                                       collapse = ""), " ")
        
        ret[length(ret) + 1] <- paste0(rep(uchar, width), collapse = "")
      }
      
      algn <- ftn$align
      if (algn == "centre")
        algn <- "center"
      
      # Loop through all the split strings
      for (row in 1:nrow(string_matrix)) {
        ln <- ""
        for (col in 1:ncol(string_matrix)) {
          
          # Deal with column alignments
          if (cols == 1) {
            calgn <- algn
          } else if (cols == 2) {
            if (col == 1) {
              calgn <- "left"
            } else {
              calgn <- "right"
            }
          } else if (cols == 3) {
            if (col == 1) {
              calgn <- "left"
            } else if (col == 2) {
              calgn <- "center"
            } else if (col == 3) {
              calgn <- "right"
            }
          }
          
          string_value <- string_matrix[row, col]
          
          if (calgn == "left") {
            ln <- paste0(ln, pad_right(string_value, col_width))
          } else if (calgn == "right") {
            ln <- paste0(ln, pad_left(string_value, col_width))
          } else if (calgn == "center") {
            ln <- paste0(ln, pad_both(string_value, col_width))
          }
          
        }
        
        ret[length(ret) + 1] <- ln
      }
      
      # i <- 1
      # while (i <= length(ftn$footnotes)) {
      #   
      #   ln <- ""
      # 
      #   for (j in seq_len(cols)) {
      #     
      #     # Not all cells have footnotes
      #     if (i > length(ftn$footnotes))
      #       f <- ""
      #     else 
      #       f <- ftn$footnotes[[i]]
      #     
      #     # Deal with column alignments
      #     if (cols == 1) {
      #       calgn <- algn 
      #     } else if (cols == 2) {
      #       if (j == 1)
      #         calgn <- "left"
      #       else 
      #         calgn <- "right"
      #     } else if (cols == 3) {
      #       if (j == 1)
      #         calgn <- "left"
      #       else if (j == 2)
      #         calgn <- "center"
      #       else if (j == 3) 
      #         calgn <- "right"
      #     }
      #     
      #     gp <- cll - nchar(f)
      #     
      #     #print("footnotes")
      #     if (gp > 0) {
      #       
      #       # if (ftn$align == "left")
      #       #   ln <- pad_right(paste0(f, " "), ll + 1)
      #       # else if (ftn$align == "right")
      #       #   ln <- pad_left(paste0(f, " "), ll + 1)
      #       # else if (ftn$align == "center" | ftn$align == "centre")
      #       #   ln <- pad_both(paste0(f, " "), ll + 1)
      #       
      #       # if (ftn$align == "left")
      #       #   ln <- pad_right(f, ll)
      #       # else if (ftn$align == "right")
      #       #   ln <- pad_left(f, ll)
      #       # else if (ftn$align == "center" | ftn$align == "centre")
      #       #   ln <- pad_both(f, ll)
      #       
      #       if (calgn == "left")
      #         ln <- paste0(ln, pad_right(f, cll))
      #       else if (calgn == "right")
      #         ln <- paste0(ln, pad_left(f, cll))
      #       else if (calgn == "center")
      #         ln <- paste0(ln, pad_both(f, cll))
      #       
      #     } else {
      #       # warning(paste0("Footnote exceeds available width.",
      #       #             "\nFootnote: ", f,
      #       #             "\nFootnote length: ", nchar(f),
      #       #             "\nLine Length: ", ll))
      #       # 
      #       # tln <- cll - 3
      #       # 
      #       # if (tln >= 0) {
      #       # 
      #       #   if (ftn$align == "left") {
      #       #     ln <- paste0(substr(pad_right(f , cll), 1, tln), "...")
      #       #   } else if (ftn$align == "right") {
      #       #     ln <- paste0("...", substr(pad_left(f, cll), 1, tln))
      #       #   } else if (ftn$align == "center" | ftn$align == "centre") {
      #       #     ln <- paste0(substr(pad_both(f, cll), 1, tln), "...")
      #       #   }
      #       # } else ln <- ""
      #       
      #       # Split string
      #       footnote_split <- stri_wrap(unlist(
      #         strsplit(f, split = "\n", fixed = TRUE)),
      #         width = cll, normalize = FALSE)
      # 
      #       sf_cat <- ""
      #       for (k in 1:length(footnote_split)) {
      #         if (k > 1) {
      #           sf_cat <- "\n"
      #         }
      # 
      #         sf <- footnote_split[k]
      # 
      #         if (calgn == "left") {
      #           ln <- paste0(ln, sf_cat, pad_right(sf, cll))
      #         } else if (calgn == "right") {
      #           ln <- paste0(ln, sf_cat, pad_left(sf, cll))
      #         } else if (calgn == "center") {
      #           ln <- paste0(ln, sf_cat, pad_both(sf, cll))
      #         }
      #       }
      #     }
      #     
      #     i <- i + 1
      #   }
      #   
      #   ret[length(ret) + 1] <- ln
      # }
      
      if (any(ftn$borders %in% c("bottom", "all")) & length(ftn$footnotes) > 0) {
        # ret[length(ret) + 1] <- paste0(paste0(rep(uchar, ll), 
        #                                       collapse = ""), " ")
        ret[length(ret) + 1] <- paste0(rep(uchar, width), collapse = "")
      }
      
      if (ftn$blank_row %in% c("below", "both") & length(ftn$footnotes) > 0)
        ret[length(ret) + 1] <- ""
    }
  }
  
  return(ret)
}

#' Get page footer text strings suitable for printing
#' @param rs The report spec
#' @return A vector of strings
#' @noRd
get_page_footer <- function(rs) {
  
  if (is.null(rs$line_size)) {
    stop("line_size cannot be null.") 
    
  }
  
  pftrr <- rs$page_footer_right
  pftrl <- rs$page_footer_left
  pftrc <- rs$page_footer_center
  
  mx <- max(c(length(pftrr), length(pftrl), length(pftrc)))
  
  if (length(pftrr) < mx)
    pftrr <- c(rep("", mx - length(pftrr)), pftrr)
  
  if (length(pftrl) < mx)
    pftrl <- c(rep("", mx - length(pftrl)), pftrl)
  
  if (length(pftrc) < mx)
    pftrc <- c(rep("", mx - length(pftrc)), pftrc)
  
  # Put blank space above page footer by default
  ret <- NULL
  
  if (mx != 0) {
    
    if (rs$page_footer_blank_row == "above") 
      ret <- c("")
    else 
      ret <- c()
    
    for (i in 1:mx) {
      
      fl <- ""
      fc <- ""
      fr <- ""
      
      if (length(pftrl) >= i)
        fl <- as.character(pftrl[[i]])
      
      if (length(pftrr) >= i)
        fr <- as.character(pftrr[[i]])
      
      if (length(pftrc) >= i)
        fc <- as.character(pftrc[[i]])
      
      l_sz <- if (is.null(fl)) 0 else get_nchar(fl)
      r_sz <- if (is.null(fr)) 0 else get_nchar(fr)
      c_sz <- if (is.null(fc)) 0 else get_nchar(fc)
      
      gp <- rs$line_size - (l_sz + r_sz + c_sz)
      
      #print("footer")
      if (gp >= 0) {
        if (l_sz > r_sz)
          fr <- pad_left(fr, l_sz)
        else
          fl <- pad_right(fl, r_sz)
        
        lw <- rs$line_size - get_nchar(fr) - get_nchar(fl)
        ln <- paste0(fl, pad_both(fc, lw), fr)
      }
      else {
        
        # stop(paste0("Page footer exceeds available width\n", 
        #             "Footer Left: ", fl, "\n", 
        #             "Footer Center: ", fc, "\n",
        #             "Footer Right: ", fr, "\n",
        #             "Footer length: ", nchar(fl) + nchar(fc) + nchar(fr), "\n",
        #             "Line length: ", rs$line_size, "\n"))
        # ln <- ""
        
        lw <- round(rs$line_size/3)
        rw <- round(rs$line_size/3)
        cw <- round(rs$line_size/3)
        
        fl_split <- stri_wrap(unlist(
          strsplit(fl, split = "\n", fixed = TRUE)),
          width = lw, normalize = FALSE)
        
        fr_split <- stri_wrap(unlist(
          strsplit(fr, split = "\n", fixed = TRUE)),
          width = rw, normalize = FALSE)
        
        fc_split <- stri_wrap(unlist(
          strsplit(fc, split = "\n", fixed = TRUE)),
          width = cw, normalize = FALSE)
        
        max_line <- max(length(fl_split), length(fr_split), length(fc_split))
        
        ln <- ""
        for (k in 1:max_line) {
          
          if (k <= length(fl_split)) {
            fl_s <- fl_split[k]
          } else {
            fl_s <- ""
          }
          
          if (k <= length(fr_split)) {
            fr_s <- fr_split[k]
          } else {
            fr_s <- ""
          }
          
          if (k <= length(fc_split)) {
            fc_s <- fc_split[k]
          } else {
            fc_s <- ""
          }
          
          cat <- ""
          if (k > 1) {
            cat <- "\n"
          }
          
          ln <- paste0(ln, cat, fl_s, pad_both(fc_s, cw), fr_s)
        }
      }
      
      ret[length(ret) + 1] <- ln
    }
    

  }
  
  
  return(ret)
}



# Page Info ---------------------------------------------------------------

#' @description A internal class to collect data about a report page.  This 
#' object is created in create_table_pages_text and passed around to 
#' different functions so they can make decisions based on the information
#' contained in this object.
#' @noRd
page_info <- function(data, keys, font_name, col_width, col_align,
                      label, label_align, page_by = NULL, table_align = NULL) {
  
  ret <- structure(list(), class = c("page_info", "list"))
  
  ret$data <- data
  ret$keys <- keys
  ret$font_name <- font_name
  ret$col_width <- col_width
  ret$col_align <- col_align
  ret$label <- label
  ret$label_align <- label_align
  ret$total_pages <- 0
  ret$page_number <- 0
  ret$page_by <- page_by
  ret$table_align <- table_align
  
  return(ret)
  
}




# Utilities ---------------------------------------------------------------

#' @noRd
pad_right <- Vectorize(function(s, w) {
  
  l <- w - get_nchar(s)
  
  if (l < 0)
    ret <- s
  else
    ret <- paste0(s, paste0(rep_len(" ", length.out = w - get_nchar(s)), collapse = ""))

  return(ret)
}, USE.NAMES = FALSE)

#' @noRd
pad_left <- Vectorize(function(s, w) {
  
  l <- w - get_nchar(s)
  
  if (l < 0)
    ret <- s
  else 
    ret <- paste0(paste0(rep_len(" ", length.out =l), collapse = ""), s)
  
  
  return(ret)
  
}, USE.NAMES = FALSE)

#' @noRd
pad_both <- Vectorize(function(s, w) {
  
  l <- w - get_nchar(s)
  
  if (l < 0)
    ret <- s
  else {
  
    lp <- floor(l / 2)
    rp <- ceiling(l / 2)
    
    ret <- paste0(paste0(rep_len(" ", length.out = lp), collapse = ""), 
                s, paste0(rep_len(" ", length.out = rp), collapse = ""))
  
  }
  
  return(ret)
  
  
}, USE.NAMES = FALSE)

pad_any <- function(s, w, j) {
  
  if (j == "left")
    ret <- pad_right(s, w)
  else if (j == "right")
    ret <- pad_left(s, w)
  else if (j %in% c("center", "centre"))
    ret <- pad_both(s, w)
  else 
    ret <- pad_right(s, w)
  
  return(ret)
  
}

Try the reporter package in your browser

Any scripts or data that you put into this service are public.

reporter documentation built on Oct. 18, 2026, 1:07 a.m.