1#' Use a package logo
2#'
3#' This function helps you use a logo in your package:
4#'   * Enforces a specific size
5#'   * Stores logo image file at `man/figures/logo.png`
6#'   * Produces the markdown text you need in README to include the logo
7#'
8#' @param img The path to an existing image file
9#' @param geometry a [magick::geometry] string specifying size. The default
10#'   assumes that you have a hex logo using spec from
11#'   <http://hexb.in/sticker.html>.
12#' @param retina `TRUE`, the default, scales the image on the README,
13#'   assuming that geometry is double the desired size.
14#'
15#' @examples
16#' \dontrun{
17#' use_logo("usethis.png")
18#' }
19#' @export
20use_logo <- function(img, geometry = "240x278", retina = TRUE) {
21  check_is_package("use_logo()")
22
23  logo_path <- proj_path("man", "figures", "logo", ext = path_ext(img))
24  create_directory(path_dir(logo_path))
25  if (!can_overwrite(logo_path)) {
26    return(invisible(FALSE))
27  }
28
29  if (path_ext(img) == "svg") {
30    logo_path <- path("man", "figures", "logo.svg")
31    file_copy(img, proj_path(logo_path))
32    ui_done("Copied {ui_path(img)} to {ui_path(logo_path)}")
33
34    height <- as.integer(sub(".*x", "", geometry))
35  } else {
36    check_installed("magick")
37
38    img_data <- magick::image_read(img)
39    img_data <- magick::image_resize(img_data, geometry)
40    magick::image_write(img_data, logo_path)
41    ui_done("Resized {ui_path(img)} to {geometry}")
42
43    height <- magick::image_info(magick::image_read(logo_path))$height
44  }
45
46  pkg <- project_name()
47  if (retina) {
48    height <- round(height / 2)
49  }
50
51  ui_todo("Add logo to your README with the following html:")
52  pd_link <- pkgdown_url(pedantic = TRUE)
53  if (is.null(pd_link)) {
54    ui_code_block("# {pkg} <img src={ui_path(logo_path)} align=\"right\" height=\"{height}\" />")
55  } else {
56    ui_code_block("# {pkg} <a href={ui_value(pd_link)}><img src={ui_path(logo_path)} align=\"right\" height=\"{height}\" /></a>")
57  }
58}
59
60has_logo <- function() {
61  file_exists(proj_path("man", "figures", "logo.png")) ||
62    file_exists(proj_path("man", "figures", "logo.svg"))
63}
64