Skip to content

Commit ccc4e32

Browse files
committed
fix: fortify font-families identification from css
1 parent b4c3c27 commit ccc4e32

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
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.2.011
4+
Version: 0.9.2.012
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"),

NEWS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
mode when plot is rendered.
99
- improve `opts_toolbar()` documentation and examples to better demonstrate the `hidden`
1010
parameter for customizing which toolbar buttons are displayed.
11-
- add examples with correct management of fonts (using `register_liberationsans()`)
11+
- add examples with correct management of fonts (using `register_liberationsans()`).
12+
- add `check_fonts_registered` and `check_fonts_dependencies` arguments to `girafe()`
13+
to validate that fonts used in plots are properly registered and available in HTML dependencies.
14+
15+
1216

1317
## Issues
1418

R/fonts.R

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ list_fonts <- function(gg) {
9898
list_layers_fonts(gg),
9999
list_theme_fonts(gg)
100100
)
101-
browser()
102101
fonts_families <- sort(unique(fonts_families))
103102
fonts_families %||% character()
104103
}
@@ -150,14 +149,30 @@ list_layers_fonts <- function(gg) {
150149
}
151150

152151
extract_family_names_regex <- function(lines) {
153-
# Pattern pour capturer le contenu entre quotes après font-family:
154-
pattern <- "font-family:\\s*['\"]([^'\"]+)['\"]"
155-
matches <- regmatches(lines, regexpr(pattern, lines, perl = TRUE))
152+
# one single line
153+
css_text <- paste(lines, collapse = "\n")
156154

157-
# Extraire seulement le nom de la police (groupe capturé)
158-
font_names <- gsub("font-family:\\s*['\"]([^'\"]+)['\"]", "\\1", matches, perl = TRUE)
155+
# Pattern for @font-face
156+
fontface_pattern <- "@font-face\\s*\\{[^}]+\\}"
157+
fontface_blocks <- regmatches(css_text, gregexpr(fontface_pattern, css_text, perl = TRUE))[[1]]
159158

160-
return(font_names)
159+
if (length(fontface_blocks) == 0) {
160+
return(character())
161+
}
162+
163+
# Pattern to extract family in @font-face blocks
164+
family_pattern <- "font-family:\\s*['\"]([^'\"]+)['\"]"
165+
166+
font_names <- character()
167+
for (block in fontface_blocks) {
168+
matches <- regmatches(block, regexpr(family_pattern, block, perl = TRUE))
169+
if (length(matches) > 0) {
170+
family_name <- gsub(family_pattern, "\\1", matches, perl = TRUE)
171+
font_names <- c(font_names, family_name)
172+
}
173+
}
174+
175+
return(sort(unique(font_names)))
161176
}
162177

163178
htmldep_css_files <- function(dep) {

0 commit comments

Comments
 (0)