library(ggforma)
library(grid)
library(systemfonts)

options <- list(sans = c("Arial", "Liberation Sans", "DejaVu Sans"), serif = c("Georgia", 
"Liberation Serif", "DejaVu Serif"))
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, ...))
}

styles <- function() {
  sheet("Reading a count", "One paragraph can carry a change of voice, a correction and units.")
  label <- paste0("The <span style='font-family:", fonts["serif"],
    ";font-size:19pt;color:#0072B2'><b>observed count</b></span> was ",
    "<s>18</s> <b>21</b>. The density was 7 m<sup>−2</sup>.<br>",
    "<u>The estimate uses all observations from both sampling periods.</u>")
  note(label, .067, .66, size = 14, width = unit(133, "mm"), vjust = 1,
       padding = unit(4, "mm"), fill = "#F3F7F8", border = "#ADBDC4",
       radius = unit(3, "mm"), lineheight = 1.28)
}

styles()
