Skip to content

Commit

Permalink
Add bootstrap icons support
Browse files Browse the repository at this point in the history
  • Loading branch information
rsh52 committed Nov 14, 2024
1 parent db9c044 commit eec6407
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 23 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ export(GeomSwimArrow)
export(GeomSwimLane)
export(GeomSwimMarker)
export(aes)
export(bootstrap)
export(fontawesome)
export(geom_swim_arrow)
export(geom_swim_lane)
export(geom_swim_marker)
export(ggsave)
export(grid.draw)
export(scale_marker_discrete)
export(search_bootstrap)
export(search_fontawesome)
export(theme_ggswim)
export(theme_ggswim_dark)
Expand Down
71 changes: 63 additions & 8 deletions R/fontawesome.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ search_fontawesome <- function(str = "", type = "solid", approximate = FALSE) {
sort()
}

#' @title Search for Bootstrap aliases to include in ggswim
#' @description
#' Check strings against the available aliases for Bootstrap icons.
#' @inheritParams search_fontawesome
#' @returns Matching aliases from the available Bootstrap data
#' @examples
#' search_bootstrap("bs-car-front")
#'
#' @export
search_bootstrap <- function(str = "", approximate = FALSE) {
df <- "bootstrap-icons"

if (approximate) {
hits <- agrep(str, Bootstrap[[df]][["aliases"]])
} else {
hits <- grep(str, Bootstrap[[df]][["aliases"]])
}
out <- unlist(Bootstrap[[df]][["aliases"]][hits])

# Sort values to better identify instances of multiple hits
out |>
sort()
}

#' @title Retrieve FontAwesome unicode
#' @description
#' When assigning FontAwesome icons as glyphs, [fontawesome()] should be used to
Expand All @@ -41,7 +65,7 @@ search_fontawesome <- function(str = "", type = "solid", approximate = FALSE) {
#' @returns Unicode text
#' @export
#' @examples
#' fontawesome('fa-car')
#' fontawesome("fa-car")
fontawesome <- function(aliases, type = "solid") {
df <- case_when(type == "regular" ~ "fa-regular-400",
type == "brands" ~ "fa-brands-400",
Expand Down Expand Up @@ -69,10 +93,48 @@ fontawesome <- function(aliases, type = "solid") {
return(out)
}

#' @title Retrieve Bootstrap unicode
#' @description
#' When assigning Bootstrap icons as glyphs, [bootstrap()] should be used to
#' convert the alias string to the appropriate Unicode format.
#'
#' All `aliases` should be prepended with "bs".
#'
#' @inheritParams fontawesome
#' @returns Unicode text
#' @export
#' @examples
#' boostrap("bs-car-front")
bootstrap <- function(aliases) {
df <- "bootstrap-icons"

matched_rows <- which(Bootstrap[[df]]$aliases %in% aliases)

result <- if (length(matched_rows) > 0) {
matched_rows
} else {
NA
}

if (all(is.na(result))) {
out <- NA
} else {
out <- Bootstrap[[df]][result, 1][["fa"]]
}

result <- is.na(out)

if (any(result)) {
message("Invalid: ", paste(aliases[result], collapse = ", "))
}
return(out)
}

#' @noRd
#' @keywords internal
.load_fonts <- function(verbose = TRUE) {
custom_names <- c(
"bootstrap-icons" = "Bootstrap",
"fa-brands-400" = "FontAwesome-Brands",
"fa-regular-400" = "FontAwesome-Regular",
"fa-solid-900" = "FontAwesome-Solid"
Expand All @@ -91,13 +153,6 @@ fontawesome <- function(aliases, type = "solid") {
font_names, font_paths,
function(name, path) {
features <- list("kern" = 1, "zero" = 0)
if (str_detect(name, "^Inter-")) {
features <- c(features, "numbers" = "tabular")
} else if (str_detect(name, "^Piazzolla-")) {
features <- c(features, "numbers" = "proportional")
} else if (str_detect(name, "^AtkinsonHyperlegible-")) {
features <- c(features, "numbers" = "proportional")
}
feature_spec <- do.call(font_feature, features)
register_font(name = name, plain = path, features = feature_spec)
}
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
31 changes: 21 additions & 10 deletions data-raw/fontawesome.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# This file kept as a reference for unpacking font files (TTFs) and converting
# them into usable styles
# This file used internally to convert TTF files from FontAwesome into usable
# sysdata

# Force reticulate to look for virtual env. Python executable
reticulate::use_virtualenv("./.venv", required = TRUE)
# Source and create RDA files
reticulate::source_python("data-raw/fonttools.py")

load("inst/fonts/FontAwesome/fa-solid-900.rda")
file.remove("inst/fonts/FontAwesome/fa-solid-900.rda")
load("inst/fonts/FontAwesome/fa-regular-400.rda")
file.remove("inst/fonts/FontAwesome/fa-regular-400.rda")
load("inst/fonts/FontAwesome/fa-brands-400.rda")
file.remove("inst/fonts/FontAwesome/fa-brands-400.rda")

paths <- c("inst/fonts/FontAwesome", "inst/fonts/Bootstrap")
rda_files <- list.files(paths, pattern = "\\.rda$", full.names = TRUE)
purrr::walk(rda_files, ~load(.x, envir = .GlobalEnv))
purrr::walk(rda_files, ~file.remove(.x))

# Create the FontAwesome rds for sysdata.rda
FontAwesome <- list("fa-solid-900" = `fa-solid-900`,
"fa-regular-400" = `fa-regular-400`,
"fa-brands-400" = `fa-brands-400`)

# Create Bootstrap rds for sysdata.rda
Bootstrap <- list("bootstrap-icons" = `bootstrap-icons`)

# Prepend "fa-" to each alias when multiple aliases are present
FontAwesome <- lapply(FontAwesome, function(df) {
df$aliases <- sapply(strsplit(df$aliases, ","), function(alias_vector) {
Expand All @@ -30,4 +30,15 @@ FontAwesome <- lapply(FontAwesome, function(df) {
tibble::tibble(df)
})

usethis::use_data(FontAwesome, internal = TRUE, overwrite = TRUE)
# Prepend "bs-" to each alias when multiple aliases are present
Bootstrap <- lapply(Bootstrap, function(df) {
df$aliases <- sapply(strsplit(df$aliases, ","), function(alias_vector) {
# Trim whitespace and prepend "fa-" to each alias
modified_aliases <- paste0("bs-", trimws(alias_vector))
# Recombine the aliases into a single string
paste(modified_aliases, collapse = ", ")
})
tibble::tibble(df)
})

usethis::use_data(FontAwesome, Bootstrap, internal = TRUE, overwrite = TRUE)
12 changes: 8 additions & 4 deletions data-raw/fonttools.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# This file used to unpack TTF files using the Python fontTools library
# Tailor the end commands to add additional files for conversion

from fontTools import ttLib
import pandas as pd
import pyreadr
import os as os


def create_fontawesome_rda(sub_folder, file_name):
def create_font_rda(sub_folder, file_name):
# Path to FontAwesome .TTF file
ttf_path = "inst/fonts/" + sub_folder + "/" + file_name + ".ttf"

Expand Down Expand Up @@ -62,6 +65,7 @@ def get_best_cmap(cmap_table):
pyreadr.write_rdata(out_path, df, df_name)


create_fontawesome_rda("FontAwesome", "fa-solid-900")
create_fontawesome_rda("FontAwesome", "fa-regular-400")
create_fontawesome_rda("FontAwesome", "fa-brands-400")
create_font_rda("FontAwesome", "fa-solid-900")
create_font_rda("FontAwesome", "fa-regular-400")
create_font_rda("FontAwesome", "fa-brands-400")
create_font_rda("Bootstrap", "bootstrap-icons")
21 changes: 21 additions & 0 deletions inst/font-licenses/Bootstrap/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2011-2024 The Bootstrap Authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
File renamed without changes.
Binary file added inst/fonts/Bootstrap/bootstrap-icons.ttf
Binary file not shown.
23 changes: 23 additions & 0 deletions man/bootstrap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/fontawesome.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/search_bootstrap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eec6407

Please sign in to comment.