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

Systemfonts and friends #128

Merged
merged 86 commits into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
ee90666
set up env and font registry
matthewstern Apr 3, 2021
972f828
tweak
matthewstern Apr 3, 2021
12e15cb
revert back to `globals`
matthewstern Apr 3, 2021
7060aa5
implement envir getter and setter
matthewstern Apr 3, 2021
874bff3
documentation
matthewstern Apr 3, 2021
0f7bb58
left some notes
matthewstern Apr 3, 2021
1de5255
more fiddling
matthewstern Apr 3, 2021
02c1021
working now, check passes
matthewstern Apr 3, 2021
21d0bec
working get and set
matthewstern Apr 3, 2021
af920ef
mas
matthewstern Apr 3, 2021
b906067
documentation
matthewstern Apr 3, 2021
680c7ca
final changes? I hope
matthewstern Apr 5, 2021
9ca3cd9
set global tweak
matthewstern Apr 5, 2021
44fa37b
Update DESCRIPTION
matthewstern Apr 5, 2021
d8d99d7
Move helper functions into utilities.R
nmpeterson Apr 5, 2021
9169794
Minor edits
nmpeterson Apr 5, 2021
f87e295
Moved geoms_that_change definition into cmapplot_globals.R
nmpeterson Apr 5, 2021
6289616
Fixed typo
nmpeterson Apr 5, 2021
eed0806
Added note about font size units
nmpeterson Apr 5, 2021
3f345bf
Rebuilt documentation
nmpeterson Apr 5, 2021
cc4ca16
Merge branch 'version1.2' into systemfonts
matthewstern Apr 5, 2021
c4dcaa1
Corrected font size note
nmpeterson Apr 5, 2021
b721dee
Merge branch 'systemfonts' of https://github.com/CMAP-REPOS/cmapplot …
nmpeterson Apr 5, 2021
44fbb83
Create test pkgdown action
nmpeterson Apr 5, 2021
1b200fc
Rebuilt NAMESPACE
nmpeterson Apr 5, 2021
9db07e9
Reference list tweaks
nmpeterson Apr 5, 2021
11db673
Print all located font files in log
nmpeterson Apr 5, 2021
4da37df
Don't force all non-numeric types to character
nmpeterson Apr 5, 2021
cc40816
Attempt to register fonts
nmpeterson Apr 5, 2021
c3d3aa6
Corrected syntax issue
nmpeterson Apr 5, 2021
715fd28
Corrected another syntax issue
nmpeterson Apr 5, 2021
5a46945
Unload/reload cmapplot before deploy
nmpeterson Apr 5, 2021
130c5b2
Try devtools::reload() instead
nmpeterson Apr 5, 2021
fdcf783
One last attempt for the day...
nmpeterson Apr 5, 2021
32cb822
Just kidding, this is the real last attempt today
nmpeterson Apr 5, 2021
9ec7364
Check for Whitney in systemfonts::registry_fonts() too
nmpeterson Apr 6, 2021
2cfec6f
Manually register fonts
nmpeterson Apr 6, 2021
608be33
Print registry_fonts() family/path to log
nmpeterson Apr 6, 2021
c022a2b
a test
matthewstern Apr 6, 2021
11dc0ff
test 2
matthewstern Apr 6, 2021
f69c275
typo
matthewstern Apr 7, 2021
881a496
no italics?
matthewstern Apr 7, 2021
ac2932d
Register fonts from cloned repo instead of ~/Library/Fonts
nmpeterson Apr 7, 2021
f4e8d1b
print font registry in vignette
matthewstern Apr 10, 2021
418944f
print contents of VM font folder
matthewstern Apr 11, 2021
4b9e647
clean up pkgdown GHA
matthewstern Apr 11, 2021
da5ab92
typo
matthewstern Apr 11, 2021
3b08605
tweak font dir
matthewstern Apr 11, 2021
524f73a
another typo
matthewstern Apr 11, 2021
a8ba100
mac font registration attempt 1
matthewstern Apr 11, 2021
ad921ba
typo
matthewstern Apr 11, 2021
a20eb26
new branch
matthewstern Apr 12, 2021
1b519ac
test
matthewstern Apr 16, 2021
013787b
typo
matthewstern Apr 16, 2021
51b8f24
getting things working again
matthewstern Apr 19, 2021
b62472b
clean up vignette testing
matthewstern Apr 19, 2021
6c3e4ac
update pkgdown GHAs
matthewstern Apr 19, 2021
8274a64
Merge branch 'version1.2' into systemfonts
matthewstern Apr 19, 2021
90803ab
adjust location of preferred font names
matthewstern Apr 19, 2021
5ff89ba
Minor tweaks
nmpeterson Apr 21, 2021
bc4adb2
custom registration
matthewstern Apr 23, 2021
233773a
test message
matthewstern Apr 23, 2021
5ba8cde
new check
matthewstern Apr 23, 2021
cface8d
new check
matthewstern Apr 23, 2021
c76adff
new check
matthewstern Apr 23, 2021
ba44649
more checkssss
matthewstern Apr 23, 2021
5546ff2
new checks
matthewstern Apr 23, 2021
ba1590a
I think I found it!
matthewstern Apr 23, 2021
64e2ce8
lets try this
matthewstern Apr 23, 2021
0fe7e7f
famous last words
matthewstern Apr 23, 2021
c698e0f
whoops
matthewstern Apr 23, 2021
c56726b
check() works
matthewstern Apr 25, 2021
50a921d
require agg for rstudio graphics
matthewstern Apr 25, 2021
9daa57c
typo in grep
matthewstern Apr 25, 2021
53c8627
documentation updates
matthewstern Apr 25, 2021
8ce6f93
clearer messages
matthewstern Apr 25, 2021
6e6ea4e
simplify `try()` and clarify comments
matthewstern May 1, 2021
661667a
typo
matthewstern May 1, 2021
840b1d5
small tweaks
matthewstern May 2, 2021
c2c7b49
attempt at dynamic GHA workflow
May 2, 2021
e74c216
tweaks to GHA
May 2, 2021
8204056
Remove pkgtown test workflow, update main workflow
May 2, 2021
699f2da
Modified text formatting example
nmpeterson May 3, 2021
a1caf11
Modified text formatting example
nmpeterson May 3, 2021
612c01e
Handle edge case
dlcomeaux May 4, 2021
51d467e
add path length requirement
matthewstern May 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ Imports:
lubridate,
magrittr,
purrr,
ragg,
rlang,
scales,
stringr,
sysfonts
systemfonts
Suggests:
knitr,
rmarkdown,
Expand Down
6 changes: 4 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ export(cmap_fill_highlight)
export(cmap_fill_race)
export(cmap_gradients)
export(cmap_palettes)
export(cmapplot_globals)
export(finalize_plot)
export(geom_recessions)
export(geom_text_lastonly)
export(get_cmapplot_global)
export(get_cmapplot_globals)
export(gg_lwd_convert)
export(integer_breaks)
export(set_cmapplot_global)
export(theme_cmap)
export(unapply_cmap_default_aes)
export(update_recessions)
Expand All @@ -39,6 +41,7 @@ import(grid)
import(gridtext)
import(rlang)
import(scales)
import(systemfonts)
importFrom(generics,intersect)
importFrom(ggpubr,get_legend)
importFrom(glue,glue)
Expand All @@ -50,6 +53,5 @@ importFrom(purrr,map)
importFrom(purrr,walk2)
importFrom(stringr,str_replace)
importFrom(stringr,str_trunc)
importFrom(sysfonts,font_files)
importFrom(utils,modifyList)
importFrom(utils,read.csv)
228 changes: 46 additions & 182 deletions R/cmapplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,209 +13,72 @@
#' @docType package
#' @import ggplot2 dplyr grid scales grDevices graphics rlang gridtext
#' @importFrom glue glue glue_collapse
#' @importFrom sysfonts font_files
#' @keywords internal
"_PACKAGE"

#'cmapplot global variables
#'
#'A list of predefined variables for use by the cmapplot package and its users.
#'It includes commonly used colors, font and font size specifications, and a
#'list of constants which aid in drawing cmap-themed plots.
#'
#'@section Plot Constants: The only portion of these global variables of
#' interest to the user is \code{cmapplot_globals$consts}, a list of default
#' constants that set certain plot aesthetics. Units of all plot constants are
#' "bigpts": 1/72 of an inch. Most plot constants are invoked (and can be
#' overridden) in \code{\link{finalize_plot}}: these are marked below with an
#' \strong{F}. Some are used/can be overridden in \code{\link{theme_cmap}}:
#' these are marked with \strong{T}.
#'
#' \itemize{ \item \code{lwd_strongline}: This stronger-width line is drawn
#' vertically or horizontally with the \code{hline, vline} args of
#' \code{theme_cmap()}. \strong{(T)} \item \code{lwd_gridline}: This
#' thinner-width line is drawn vertically or horizontally with the
#' \code{gridlines, axislines} args of \code{theme_cmap()}. \strong{(T)} \item
#' \code{lwd_plotline}: The width of any lines drawn by geoms in the plot (e.g.
#' \code{geom_line}) but not explicitly sized by the geom's aesthetic.
#' Implemented by \code{finalize_plot} or by \code{apply_cmap_default_aes} but
#' not overridable in either context. (Modify by setting the size explicitly in
#' the geom, but see \code{gg_lwd_convert} first.) \item \code{lwd_topline}:
#' The width of the line above the plot. \strong{(F)} \item
#' \code{length_ticks}: The length of the axis ticks (if shown). \strong{(T)}
#' \item \code{margin_topline_t}: The margin between the top edge of the image
#' and the top line. \strong{(F)} \item \code{margin_title_t}: The margin
#' between the top line and the title. \strong{(F)} \item
#' \code{margin_title_b}: The margin between the title and the caption when
#' both are drawn in the sidebar. \strong{(F)} \item \code{margin_caption_b}:
#' The margin between the bottom of the caption and the bottom edge of the
#' image. \strong{(F)} \item \code{margin_legend_t}: The margin between the top
#' line and the plot box (i.e., the top of the legend). \strong{(F)} \item
#' \code{margin_legend_i}: The margin between legends (this only applies in
#' plots with two or more legends and does not affect legend spacing on plots
#' with single legends that have multiple rows). \strong{(T, F)} \item
#' \code{margin_legend_b}: The margin between the bottom of the legend and the
#' rest of the plot. \strong{(T, F)} \item \code{margin_plot_b}: The margin
#' between the bottom of the plot and the bottom edge of the image (or top of
#' caption). \strong{(F)} \item \code{margin_sidebar_l}: The margin between the
#' left edge of the image and the title and caption, when the sidebar exists.
#' Deducted from \code{title_width}. \strong{(F)} \item \code{margin_plot_l}:
#' The margin between the left edge of the plot and the sodebar. \strong{(F)}
#' \item \code{margin_plot_r}: The margin between the right edge of the plot
#' and the edge of the image. \strong{(F)} \item \code{margin_panel_r}: Padding
#' between the plot and its right-hand drawing extent. Override this based on
#' space needed for x axis labels. \strong{(T)} \item \code{leading_title}:
#' Text leading for Title text. \strong{(F)} \item \code{leading_caption}: Text
#' leading for Caption text. \strong{(F)} }
#'
#'@export
cmapplot_globals <- list(

## Colors
colors = list(
blackish = "#222222"
),

## Font sizes
fsize = list(
S = 11,
M = 14,
L = 17
),

## Base typefaces -- modified later by .onLoad()
font = list(
strong = list(family = "Arial", face = "bold"),
regular = list(family = "Arial", face = "plain"),
light = list(family = "Arial", face = "plain")
),
use_whitney = FALSE,

## Establish plotting constants in bigpts (1/72 of inch)
consts = list(
lwd_gridline = 0.3,
lwd_strongline = 1,
lwd_plotline = 3,
lwd_topline = 2,
length_ticks = 7,
margin_topline_t = 5,
margin_title_t = 5,
margin_title_b = 5,
margin_caption_b = 5,
margin_legend_t = 5,
margin_legend_i = 8,
margin_legend_b = 10,
margin_plot_b = 5,
margin_sidebar_l = 2,
margin_plot_l = 10,
margin_plot_r = 10,
margin_panel_r = 10,
leading_title = 1,
leading_caption = 1
),

# list of geoms whose aesthetics will be customized
geoms_that_change = c(
"Label",
"Line",
"Text",
"TextLast",
"PointLast",
"RecessionsText"
),

# empty location for loading in preferred aesthetics during `.onLoad`
default_aes_cmap = NULL,

# empty location for caching existing aesthetics during `.onLoad`
default_aes_cached = NULL

)


## Update fonts based on system -- *must* be done with .onLoad()
#' @import systemfonts
.onLoad <- function(...) {

# Check for Whitney
all_fonts <- sysfonts::font_files()
whitney_fonts <- all_fonts[all_fonts$family %in% c("Whitney Medium", "Whitney Book", "Whitney Semibold") & all_fonts$face=="Regular", ]
cmapplot_globals$use_whitney <<- length(whitney_fonts$family) >= 3

# Font handling for Windows users
if (.Platform$OS.type == "windows") {

# Use Whitney if available
if (cmapplot_globals$use_whitney) {
# Add fonts to R
grDevices::windowsFonts(
`Whitney Medium` = grDevices::windowsFont("Whitney Medium"),
`Whitney Book` = grDevices::windowsFont("Whitney Book"),
`Whitney Semibold` = grDevices::windowsFont("Whitney Semibold")
)

# Update font variables
cmapplot_globals$font <<- list(
strong = list(family = "Whitney Semibold", face = "plain"),
regular = list(family = "Whitney Medium", face = "plain"),
light = list(family = "Whitney Book", face = "plain")
)

# Otherwise, use Calibri
} else {
packageStartupMessage(
"WARNING: Whitney is not installed on this PC, so CMAP theme will default to Calibri"
)
# Add fonts to R
grDevices::windowsFonts(
`Calibri` = grDevices::windowsFont("Calibri"),
`Calibri Light` = grDevices::windowsFont("Calibri Light")
)

# Update font variables
cmapplot_globals$font <<- list(
strong = list(family = "Calibri", face = "bold"),
regular = list(family = "Calibri", face = "plain"),
light = list(family = "Calibri Light", face = "plain")
)
}
family <- name <- path <- NULL

# check for Whitney
all_fonts <- systemfonts::system_fonts()
whitney_core <- all_fonts$name[all_fonts$name %in% c("Whitney-Medium", "Whitney-Book", "Whitney-Semibold")]
assign("use_whitney", length(whitney_core) >= 3, envir = cmapplot_globals)

if(get("use_whitney", envir = cmapplot_globals)){
# Register all Whitney fonts (note: this registers italic fonts both as
# variants of core fonts and as standalone fonts, so there is some
# duplication.)
whitney_fonts <- select(filter(all_fonts, family == "Whitney"), name, path)
purrr::walk2(whitney_fonts$name, whitney_fonts$path, systemfonts::register_font)

# Update font variables
assign("font",
list(
strong = list(family = "Whitney-Semibold", face = "plain"),
regular = list(family = "Whitney-Medium", face = "plain"),
light = list(family = "Whitney-Book", face = "plain")),
envir = cmapplot_globals)
}

# Font handling for macOS/Linux/Unix
} else {
# Load CMAP preferred default.aes (can't be done until fonts are specified)
assign("default_aes_cmap",
init_cmap_default_aes(),
envir = cmapplot_globals)

# Use Whitney if available
if (cmapplot_globals$use_whitney) {
# Add fonts to R
grDevices::X11Fonts(
`Whitney Medium` = grDevices::X11Font("-*-whitney-medium-%s-*-*-%d-*-*-*-*-*-*-*"),
`Whitney Book` = grDevices::X11Font("-*-whitney-book-%s-*-*-%d-*-*-*-*-*-*-*"),
`Whitney Semibold` = grDevices::X11Font("-*-whitney-semibold-%s-*-*-%d-*-*-*-*-*-*-*")
)
# Cache existing default.aes
assign("default_aes_cached",
fetch_current_default_aes(),
envir = cmapplot_globals)
}

# Update font variables
cmapplot_globals$font <<- list(
strong = list(family = "Whitney Semibold", face = "plain"),
regular = list(family = "Whitney Medium", face = "plain"),
light = list(family = "Whitney Book", face = "plain")
)

# Otherwise, stick to Arial (set prior to .onLoad())
} else {
packageStartupMessage(
"WARNING: Whitney is not installed on this system, so CMAP theme will default to Arial"
)
}
.onAttach <- function(...){
if(!get("use_whitney", envir = cmapplot_globals)){
packageStartupMessage(
"WARNING: Whitney is not installed on this machine, so CMAP theme will use your default sans-Serif font"
)
}
}

# Load CMAP preferred default.aes (can't be done until fonts are specified)
cmapplot_globals$default_aes_cmap <<- init_cmap_default_aes()

# Cache existing default.aes
cmapplot_globals$default_aes_cached <<- fetch_current_default_aes()
}


# Font spec visualization helper function ---------------------------------

#' Font visualization test
#'
#' This internal function uses base R graphics to display the five text variants
#' that should show up on a cmap themed graphic - and what fonts the package is
#' planning to use to display them.
#'
#' @noRd
display_cmap_fonts <- function() {
graphics::plot(c(0,2), c(0,6), type="n", xlab="", ylab="")

Expand Down Expand Up @@ -308,3 +171,4 @@ gg_lwd_convert <- function(value, unit = "bigpts") {
value_out / .lwd
)
}

Loading