Skip to content

Commit 9710918

Browse files
committed
change: start using font_set
1 parent d85f32c commit 9710918

File tree

11 files changed

+85
-108
lines changed

11 files changed

+85
-108
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: ggiraph
33
Title: Make 'ggplot2' Graphics Interactive
4-
Version: 0.9.5.006
4+
Version: 0.9.5.007
55
Authors@R: c(
66
person("David", "Gohel", , "david.gohel@ardata.fr", role = c("aut", "cre")),
77
person("Panagiotis", "Skintzos", , "sigmapi@posteo.net", role = "aut"),
@@ -25,7 +25,7 @@ BugReports: https://github.com/davidgohel/ggiraph/issues
2525
Imports:
2626
cli,
2727
dplyr,
28-
gdtools (>= 0.4.4),
28+
gdtools (>= 0.5.0),
2929
ggplot2 (>= 4.0.0),
3030
grid,
3131
htmltools,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ importFrom(dplyr,lag)
280280
importFrom(dplyr,mutate)
281281
importFrom(dplyr,ungroup)
282282
importFrom(gdtools,font_family_exists)
283+
importFrom(gdtools,font_set_liberation)
283284
importFrom(gdtools,sys_fonts)
284285
importFrom(grDevices,dev.cur)
285286
importFrom(grDevices,dev.list)

R/default.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ css_default_selection <- girafe_css_bicolor(
88
)
99

1010
default_girafe_settings <- list(
11-
fonts = default_fontname(),
11+
fonts = validated_fonts(),
1212
opts_sizing = opts_sizing(width = 1),
1313
opts_tooltip = opts_tooltip(
1414
css = "padding:5px;background:black;color:white;border-radius:2px;text-align:left;",

R/dsvg.R

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,13 @@
1919
#' @param desc A longer description for accessibility purposes (aria-description/aria-describedby).
2020
#' @param canvas_id svg id within HTML page.
2121
#' @param fonts Named list of font names to be aliased with
22-
#' fonts installed on your system. If unspecified, the R default
23-
#' families "sans", "serif", "mono" and "symbol"
24-
#' are aliased to the family returned by [match_family()].
25-
#'
26-
#' If fonts are available, the default mapping will use these values:
27-
#'
28-
#' | R family | Font on Windows | Font on Unix | Font on Mac OS |
29-
#' |----------|--------------------|--------------|----------------|
30-
#' | `sans` | Arial | DejaVu Sans | Helvetica |
31-
#' | `serif` | Times New Roman | DejaVu serif | Times |
32-
#' | `mono` | Courier | DejaVu mono | Courier |
33-
#' | `symbol` | Symbol | DejaVu Sans | Symbol |
22+
#' fonts installed on your system. If unspecified, the defaults
23+
#' from [gdtools::font_set_liberation()] are used for the R families
24+
#' "sans", "serif", "mono" and "symbol".
3425
#'
3526
#' As an example, using `fonts = list(sans = "Roboto")` would make the
36-
#' default font "Roboto" as many ggplot theme are using `theme_minimal(base_family="")` or
37-
#' `theme_minimal(base_family="sans")`.
38-
#'
39-
#' You can also use theme_minimal(base_family="Roboto").
27+
#' default font "Roboto" as many ggplot themes use
28+
#' `theme_minimal(base_family = "sans")`.
4029
#' @seealso [Devices]
4130
#' @examples
4231
#' fileout <- tempfile(fileext = ".svg")

R/fonts.R

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,35 @@
11
r_font_families <- c("sans", "serif", "mono", "symbol")
2-
default_fonts <- list(
3-
windows = c(
4-
sans = "Arial",
5-
serif = "Times New Roman",
6-
mono = "Courier New",
7-
symbol = "Symbol"
8-
),
9-
osx = c(
10-
sans = "Helvetica",
11-
serif = "Times",
12-
mono = "Courier",
13-
symbol = "Symbol"
14-
),
15-
unix = c(
16-
sans = "DejaVu Sans",
17-
serif = "DejaVu serif",
18-
mono = "DejaVu mono",
19-
symbol = "DejaVu Sans"
20-
)
21-
)
22-
23-
#' @importFrom gdtools font_family_exists
24-
default_fontname <- function() {
25-
os <- get_os()
26-
if (!os %in% c("windows", "osx")) {
27-
os <- "unix"
28-
}
29-
def_fonts <- default_fonts[[os]]
30-
def_fonts <- def_fonts[unlist(lapply(def_fonts, font_family_exists))]
31-
missing_fonts <- setdiff(r_font_families, names(def_fonts))
32-
def_fonts[missing_fonts] <- lapply(def_fonts[missing_fonts], match_family)
33-
def_fonts
34-
}
352

363
#' @export
374
#' @title List of validated default fonts
385
#' @description Validates and possibly modifies the fonts to be used as default
39-
#' value in a graphic according to the fonts available on the machine. It process
40-
#' elements named "sans", "serif", "mono" and "symbol".
41-
#' @param fonts Named list of font names to be aliased with
42-
#' fonts installed on your system. If unspecified, the R default
43-
#' families "sans", "serif", "mono" and "symbol"
44-
#' are aliased to the family returned by [match_family()].
6+
#' value in a graphic according to the fonts available on the machine. It
7+
#' processes elements named "sans", "serif", "mono" and "symbol".
458
#'
46-
#' If fonts are available, the default mapping will use these values:
9+
#' Default font resolution is delegated to [gdtools::font_set_liberation()],
10+
#' which uses Liberation fonts (bundled by 'fontquiver', SIL Open Font
11+
#' License) for reproducible offline output.
4712
#'
48-
#' | R family | Font on Windows | Font on Unix | Font on Mac OS |
49-
#' |----------|--------------------|--------------|----------------|
50-
#' | `sans` | Arial | DejaVu Sans | Helvetica |
51-
#' | `serif` | Times New Roman | DejaVu serif | Times |
52-
#' | `mono` | Courier | DejaVu mono | Courier |
53-
#' | `symbol` | Symbol | DejaVu Sans | Symbol |
13+
#' @param fonts Named list of font names to be aliased with
14+
#' fonts installed on your system. If unspecified, the defaults
15+
#' from [gdtools::font_set_liberation()] are used.
5416
#' @return a named list of validated font family names
5517
#' @seealso [girafe()], [dsvg()]
5618
#' @family functions for font management
19+
#' @importFrom gdtools font_family_exists font_set_liberation
5720
#' @examples
5821
#' validated_fonts()
5922
validated_fonts <- function(fonts = list()) {
23+
auto <- font_set_liberation()
24+
defaults <- c(
25+
sans = auto$sans,
26+
serif = auto$serif,
27+
mono = auto$mono,
28+
symbol = auto$symbol
29+
)
6030
fonts <- fonts[unlist(lapply(fonts, font_family_exists))]
6131
missing_fonts <- setdiff(r_font_families, names(fonts))
62-
fonts[missing_fonts] <- default_fontname()[missing_fonts]
32+
fonts[missing_fonts] <- defaults[missing_fonts]
6333
fonts
6434
}
6535

R/girafe.R

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@
3535
#' @param pointsize the default pointsize of plotted text in pixels, default to 12.
3636
#' @param options a list of options for girafe rendering, see
3737
#' [opts_tooltip()], [opts_hover()], [opts_selection()], ...
38+
#' @param font_set A [gdtools::font_set()] object controlling font
39+
#' aliases and HTML dependencies. The default is
40+
#' [gdtools::font_set_liberation()], which uses Liberation fonts
41+
#' (bundled by 'fontquiver' under the SIL Open Font License).
42+
#' This makes output reproducible offline without requiring
43+
#' any system font.
44+
#'
45+
#' For system-aware font selection use [gdtools::font_set_auto()],
46+
#' or build a custom configuration with [gdtools::font_set()].
47+
#'
48+
#' `font_set$dsvg_fonts` is passed as `fonts` argument to [dsvg()] and
49+
#' `font_set$dependencies` are appended to `dependencies`.
3850
#' @param dependencies Additional widget HTML dependencies, see [htmlwidgets::createWidget()].
3951
#' @param check_fonts_registered whether to check if fonts families found in
4052
#' the ggplot are registered with 'systemfonts'.
@@ -97,7 +109,7 @@
97109
#' function `girafe`. Its value is beeing used to define a relative
98110
#' width of the graphic within its HTML container. Its height is automatically
99111
#' adjusted regarding to the argument `width` and the aspect ratio.
100-
#' @seealso [girafe_options()], [validated_fonts()], [dsvg()]
112+
#' @seealso [girafe_options()], [dsvg()], [gdtools::font_set()]
101113
#' @export
102114
girafe <- function(
103115
ggobj = NULL,
@@ -106,6 +118,7 @@ girafe <- function(
106118
width_svg = NULL,
107119
height_svg = NULL,
108120
options = list(),
121+
font_set = NULL,
109122
dependencies = NULL,
110123
check_fonts_registered = FALSE,
111124
check_fonts_dependencies = FALSE,
@@ -136,6 +149,16 @@ girafe <- function(
136149
args$bg <- "#ffffff01"
137150
}
138151

152+
# font_set integration -----
153+
if (is.null(font_set)) {
154+
font_set <- font_set_liberation()
155+
}
156+
if (!inherits(font_set, "font_set")) {
157+
cli::cli_abort("{.arg font_set} must be a {.cls font_set} object.")
158+
}
159+
args$fonts <- font_set$dsvg_fonts
160+
dependencies <- c(dependencies, font_set$dependencies)
161+
139162
if (!is.null(ggobj)) {
140163
# check ggobj -----
141164
if (!inherits(ggobj, "ggplot")) {

examples/girafe.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ library(ggplot2)
22
library(ggiraph)
33
library(gdtools)
44

5-
register_liberationsans()
5+
fonts <- font_set(sans = font_liberation("sans"))
66

77
dataset <- mtcars
88
dataset$carname <- row.names(mtcars)
@@ -18,13 +18,11 @@ gg_point <- ggplot(
1818
)
1919
) +
2020
geom_point_interactive(hover_nearest = TRUE, size = 11 / .pt) +
21-
theme_minimal(base_family = "Liberation Sans", base_size = 11)
21+
theme_minimal(base_family = fonts$sans, base_size = 11)
2222

2323
x <- girafe(
2424
ggobj = gg_point,
25-
dependencies = list(
26-
liberationsansHtmlDependency()
27-
)
25+
font_set = fonts
2826
)
2927

3028
x

inst/tinytest/test-dsvg_text.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ source("setup.R")
8282

8383
# test with font categories (sans, serif, mono, symbol) ----
8484
{
85-
fonts <- ggiraph:::default_fontname()
85+
fonts <- ggiraph::validated_fonts()
8686
for(name in names(fonts)) {
8787
if (gdtools::font_family_exists(fonts[[name]])) {
8888
doc <- dsvg_doc(

man/dsvg.Rd

Lines changed: 5 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/girafe.Rd

Lines changed: 18 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)