davidgohel / officer

:cop: officer: office documents from R
https://ardata-fr.github.io/officeverse/
Other
613 stars 107 forks source link

Allow metric units in page_size #579

Closed nc011 closed 3 months ago

nc011 commented 6 months ago

The page_size() function currently only allows measurements in inches. Although the user can easily convert metric into inches, it might instead be useful to allow the user to specify the measurement units (inches, centimetres, millimetres).

For example:

page_size <- function(width = 21 / 2.54, height = 29.7 / 2.54, units="in", orient = "portrait") {

allowed_units <- c("in", "cm", "mm")
if(units %in% allowed_units){
  if(units %in% "cm"){
    width=width/2.54
    height=height/2.54
  } else if(units %in% "mm"){
    width=width/25.4
    height=height/25.4
  }
} else {//throw an error or just default to inches}

  if (orient %in% "portrait") {
    h <- max(c(height, width))
    w <- min(c(height, width))
  } else {
    h <- min(c(height, width))
    w <- max(c(height, width))
  }

  z <- list(width = w, height = h, orient = orient)
  class(z) <- c("page_size")
  z
}

#' @export
to_wml.page_size <- function(x, add_ns = FALSE, ...) {
  out <- sprintf(
    "<w:pgSz w:h=\"%.0f\" w:w=\"%.0f\" w:orient=\"%s\"/>",
    inch_to_tweep(x$height), inch_to_tweep(x$width), x$orient
  )
  out
}
davidgohel commented 3 months ago

thanks, it's implemented now