MatthewBJane / ThemePark

Fun ggplot themes for popular culture
http://matthewbjane.com/ThemePark/
MIT License
196 stars 13 forks source link

Add `theme_dune` + functionality for local font files #41

Closed lcpilling closed 5 months ago

lcpilling commented 5 months ago

This PR comes with two changes! I wanted to create a DUNE theme but the font is not available on Google Fonts. However people have made amazing fonts (e.g., https://www.dafont.com/dune-rise.font) and the {sysfonts} package has a function to import local font files. I have therefore added the possibility for a theme to specify that the font should be loaded locally. The creator needs to put the font file in /inst/fonts/ and it is simplest if the file has the name that the family will use.

For Dune, I downloaded the Dune_Rise.ttf font file and put it in /inst/fonts/

The utils function now has an extra option "local" where the theme can specify the filename without the suffix:

initialize_font <- function(name, family = name, local=NULL, ...) {
  if (missing(name)) stop('`name` must be specified.')
  curr_families <- sysfonts::font_families()
  if (!family %in% curr_families) {
    if (is.null(local))  {  ## if not local, use the google fonts as before
      sysfonts::font_add_google(name = name, family = family, ...)
    }  else  {  ## if local, use the local file that comes with the package
      sysfonts::font_add(local, paste0(path.package("ThemePark"), "/fonts/", local, ".ttf"))
    }
  } else {
    invisible(curr_families)
  }

So for theme_dune the font part looks like:

  # CUSTOM FONT: add a custom font from google fonts
  font_family = ifelse(dune_font,"Dune_Rise","sans") # use this line if you have a custom font
  if (dune_font) {
    ThemePark:::initialize_font(name = "Dune_Rise", family = "Dune_Rise", local = "Dune_Rise")
  }

The long and short of it is that this new DUNE font has the films colour palette, and uses a local font.

Examples!

image

image

image

christopherkenny commented 5 months ago

I like this, especially for when a font has a nice open license (like the Dune Rise font).

Do you mind adding a test for this theme? (Run usethis::use_test() with the R file open, then copy from one of the other test theme files?)

lcpilling commented 5 months ago

I have added the test.

I also changed it so that the "local" option passed is the actual font filename in case it differs from what the uploader wants the "family"/"name" to be

christopherkenny commented 5 months ago

Thanks for adding the test, this looks like a great addition to me.

MatthewBJane commented 5 months ago

This looks awesome!