Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposition de script pour installer les polices en local #1

Open
2 tasks done
statnmap opened this issue Nov 5, 2020 · 1 comment
Open
2 tasks done

Proposition de script pour installer les polices en local #1

statnmap opened this issue Nov 5, 2020 · 1 comment

Comments

@statnmap
Copy link

statnmap commented Nov 5, 2020

  • Testé sur Ubuntu/Linux.
  • Testé en partie sur Windows

Ce serait pratique d'avoir cela sous forme de fonction pour installer les polices avec le CI notamment, mais aussi bien sûr en local.

Pour le CI ça marche, mais il faut exécuter deux fois le code selon l'endroit où on été installées les polices.

# Install package with fonts

install.packages("extrafont")
# install.packages("extrafontdb")
remotes::install_github("spyrales/gouvdown.fonts", upgrade = FALSE)
install.packages("systemfonts")
# remotes::install_github("r-lib/systemfonts")
if (.Platform == "windows") {
  install.packages("installr")
  installr::install.Rtools()
}
# Get fonts directories
gouvdown_font_path <- list.files(system.file("resources/fonts", package = "gouvdown.fonts"),
                                 full.names = TRUE)
# Install only if needed
all_fonts <- systemfonts::system_fonts()
all_good <- all(any(grepl("marianne|Marianne", all_fonts$path)) & any(grepl("spectral|Spectral", all_fonts$path)))
message("is all good? ", all_good)

# User font directory
if (.Platform == "windows") {
  user_font <- normalizePath(file.path(dirname(Sys.getenv("HOME")), "AppData/Local/Microsoft/Windows/Fonts"))
} else {
  user_font <- normalizePath("~/.fonts")
}
if (!dir.exists(user_font)) {
  dir.create(user_font)
  message("home font dir created. \nYou may need to reinstall {systemfonts} so that it detects the new directory:\n",
          " remotes::install_github('r-lib/systemfonts')\n",
          "And restart your R session")
  # remotes::install_github("r-lib/systemfonts")
}

# Default paths where to save fonts on computer
font_paths <- extrafont:::ttf_find_default_path()
if (.Platform == "windows") {
  # {extrafont} does not know local font path
  font_paths <- c(font_paths, user_font)
}
message("font paths:", paste(font_paths, collapse = ", "))

# Import all fonts - Too long - avoid
# extrafont::font_import(prompt = FALSE)

# if (!all(gouvdown::check_fonts_in_r())) {
if (!all_good) {

  # Try to copy in. Some write access rights may be necessary.
  for (path in font_paths) {
    # path <- font_paths[2]
    try(copy <- file.copy(gouvdown_font_path, to = path, recursive = TRUE))
    message(path, ": ", paste(copy, collapse = " "))
    if (isTRUE(any(copy))) {
      installed_path <- path
      # Break loop if copy is successful
      break
    }
  }
  if (!exists("installed_path")) {
    warning("Fonts were not installed")
  } else {
    message("Fonts installed in: ", installed_path)
  }

  # Register fonts for R
  # extrafont::font_import(paths = installed_path, prompt = FALSE)
}

# Check system installation
all_fonts <- systemfonts::system_fonts()
if (length(grep("marianne", tolower(all_fonts$path))) > 0) {
  message("marianne font is installed on your system")
} else {
  warning("marianne font is not installed on your system")
}
if (length(grep("spectral", tolower(all_fonts$path))) > 0) {
  message("spectral font is installed on your system")
} else {
  warning("spectral font is not installed on your system")
}

# Install fonts on R for figures
all_fonts <- systemfonts::system_fonts()
print(sort(unique(all_fonts$family)))

gouvdown_font_installed <- unique(dirname(all_fonts[grep("marianne|spectral", tolower(all_fonts$family)),]$path))
if (length(gouvdown_font_installed) != 0) {
  extrafont::font_import(gouvdown_font_installed, prompt = FALSE)
} else {
  warning("Fonts not found")
}

all_fonts_r <- extrafont::fonts()
if (length(grep("marianne", tolower(all_fonts_r))) > 0) {
  message("marianne font is installed in R")
} else {
  warning("marianne font is not installed in R")
}
if (length(grep("spectral", tolower(all_fonts_r))) > 0) {
  message("spectral font is installed in R")
} else {
  warning("spectral font is not installed in R")
}

J'ai fait des tests avec match_font() parce que de mon côté ça ne renvoie pas toujours le bon résultat alors que la police est bien installée. Et comme c'est ce qui est utilisé dans {gouvdown}, je m'interroge. Du coup, j'ai penché pour utiliser les sorties de system_fonts() qui elles sont correctes.

@RLesur
Copy link
Member

RLesur commented Nov 6, 2020

Oh, merci ! c'est vraiment pas drôle à faire.
Jusqu'ici, je m'étais contenté d'écrire ça pour linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants