File: logo.R

package info (click to toggle)
r-cran-usethis 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,228 kB
  • sloc: sh: 26; makefile: 17; cpp: 6; ansic: 3
file content (73 lines) | stat: -rw-r--r-- 2,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#' Use a package logo
#'
#' This function helps you use a logo in your package:
#'   * Enforces a specific size
#'   * Stores logo image file at `man/figures/logo.png`
#'   * Produces the markdown text you need in README to include the logo
#'
#' @param img The path to an existing image file
#' @param geometry a [magick::geometry] string specifying size. The default
#'   assumes that you have a hex logo using spec from
#'   <http://hexb.in/sticker.html>.
#' @param retina `TRUE`, the default, scales the image on the README,
#'   assuming that geometry is double the desired size.
#'
#' @examples
#' \dontrun{
#' use_logo("usethis.png")
#' }
#' @export
use_logo <- function(img, geometry = "240x278", retina = TRUE) {
  check_is_package("use_logo()")

  ext <- tolower(path_ext(img))
  logo_path <- proj_path("man", "figures", "logo", ext = ext)
  create_directory(path_dir(logo_path))
  if (!can_overwrite(logo_path)) {
    return(invisible(FALSE))
  }

  if (ext == "svg") {
    logo_path <- path("man", "figures", "logo.svg")
    file_copy(img, proj_path(logo_path), overwrite = TRUE)
    ui_bullets(c("v" = "Copied {.path {pth(img)}} to {.path {logo_path}}."))

    height <- as.integer(sub(".*x", "", geometry))
  } else {
    check_installed("magick")

    img_data <- magick::image_read(img)
    img_data <- magick::image_resize(img_data, geometry)
    magick::image_write(img_data, logo_path)
    ui_bullets(c("v" = "Resized {.path {pth(img)}} to {geometry}."))

    height <- magick::image_info(magick::image_read(logo_path))$height
  }

  pkg <- project_name()
  if (retina) {
    height <- round(height / 2)
  }

  # Have a clickable hyperlink to jump to README if exists.
  readme_path <- find_readme()
  if (is.null(readme_path)) {
    readme_show <- "your README"
  } else {
    readme_show <- cli::format_inline("{.path {pth(readme_path)}}")
  }

  ui_bullets(c("_" = "Add logo to {readme_show} with the following html:"))
  pd_link <- pkgdown_url(pedantic = TRUE)
  if (is.null(pd_link)) {
    ui_code_snippet(
      "# {pkg} <img src=\"{proj_rel_path(logo_path)}\" align=\"right\" height=\"{height}\" alt=\"\" />",
      language = ""
    )
  } else {
    ui_code_snippet(
      "# {pkg} <a href=\"{pd_link}\"><img src=\"{proj_rel_path(logo_path)}\" align=\"right\" height=\"{height}\" alt=\"{pkg} website\" /></a>",
      language = ""
    )
  }
}