library(ggforma)
library(grid)
library(systemfonts)

options <- list(sans = c("Arial", "Liberation Sans", "DejaVu Sans"), lo = c("Leelawadee UI", 
"Noto Sans Lao", "Lao Sangam MN"), th = c("Leelawadee UI", "Noto Sans Thai", 
"Thonburi"))
installed <- system_fonts()$family
fonts <- vapply(options, function(x) {
  found <- intersect(x, installed)
  if (!length(found)) stop('Choose a suitable installed local font for ', paste(x, collapse = ', '))
  found[1L]
}, character(1))

sheet <- function(title, subtitle) {
  grid.newpage()
  grid.draw(markup_grob(title, format = "plain", x = .055, y = .925, hjust = 0,
                        size = 22, family = fonts["sans"], face = "bold", colour = "#172C3C"))
  grid.draw(markup_grob(subtitle, format = "plain", x = .055, y = .810, hjust = 0,
                        size = 10, family = fonts["sans"], colour = "#465865"))
  grid.lines(x = c(.055, .945), y = c(.745, .745), gp = gpar(col = "#CED7DA", lwd = .8))
}

note <- function(text, x, y, size = 17, family = fonts["sans"], ...) {
  grid.draw(markup_grob(text, x = x, y = y, size = size, family = family,
                        colour = "#172C3C", hjust = 0, ...))
}

key <- function(text, x, y) note(text, x, y, size = 9, face = "bold", format = "plain")

wrap <- function() {
  sheet("Line breaks without spaces", "Each paragraph has a 61 mm outer width and 3 mm padding on every side.")
  samples <- c(lo = "ປຽບທຽບຈຳນວນການເຂົ້າຊົມໃນແຕ່ລະເດືອນ",
               th = "เปรียบเทียบจำนวนการเข้าชมในแต่ละเดือน")
  for (i in seq_along(samples)) {
    lang <- names(samples)[i]
    x <- c(.065, .535)[i]
    key(c("LAO", "THAI")[i], x, .645)
    note(paste0("<u>", samples[i], "</u>"), x, .555, family = fonts[lang], size = 16,
         lang = lang, vjust = 1, width = unit(61, "mm"), padding = unit(3, "mm"),
         fill = "#F3F7F8", border = "#ADBDC4", radius = unit(2, "mm"))
  }
}

wrap()
