Skip to content

Commit 311de90

Browse files
committed
Add a way to clear local added fonts
1 parent 516304a commit 311de90

7 files changed

+41
-0
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ S3method(print,font_feature)
77
export(add_fonts)
88
export(as_font_weight)
99
export(as_font_width)
10+
export(clear_local_fonts)
1011
export(clear_registry)
1112
export(font_fallback)
1213
export(font_feature)

R/cpp11.R

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ add_local_fonts <- function(paths) {
2424
.Call(`_systemfonts_add_local_fonts`, paths)
2525
}
2626

27+
clear_local_fonts_c <- function() {
28+
invisible(.Call(`_systemfonts_clear_local_fonts_c`))
29+
}
30+
2731
match_font_c <- function(family, italic, bold) {
2832
.Call(`_systemfonts_match_font_c`, family, italic, bold)
2933
}

R/register_font.R

+9
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ register_variant <- function(name, family, weight = NULL, width = NULL, features
196196
#'
197197
#' add_fonts(empty_font)
198198
#'
199+
#' clear_local_fonts()
200+
#'
199201
add_fonts <- function(files) {
200202
if (!is.character(files)) {
201203
stop("`files` must be a character vector")
@@ -219,3 +221,10 @@ scan_local_fonts <- function() {
219221
))
220222
add_fonts(files)
221223
}
224+
#' @rdname add_fonts
225+
#' @export
226+
#'
227+
clear_local_fonts <- function() {
228+
message("Run `scan_local_fonts()` in order to re-add the automatically added fonts")
229+
clear_local_fonts_c()
230+
}

man/add_fonts.Rd

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cpp11.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ extern "C" SEXP _systemfonts_add_local_fonts(SEXP paths) {
4848
return cpp11::as_sexp(add_local_fonts(cpp11::as_cpp<cpp11::decay_t<cpp11::strings>>(paths)));
4949
END_CPP11
5050
}
51+
// font_local.h
52+
void clear_local_fonts_c();
53+
extern "C" SEXP _systemfonts_clear_local_fonts_c() {
54+
BEGIN_CPP11
55+
clear_local_fonts_c();
56+
return R_NilValue;
57+
END_CPP11
58+
}
5159
// font_matching.h
5260
cpp11::list match_font_c(cpp11::strings family, cpp11::logicals italic, cpp11::logicals bold);
5361
extern "C" SEXP _systemfonts_match_font_c(SEXP family, SEXP italic, SEXP bold) {
@@ -146,6 +154,7 @@ extern "C" SEXP _systemfonts_get_line_width_c(SEXP string, SEXP path, SEXP index
146154
extern "C" {
147155
static const R_CallMethodDef CallEntries[] = {
148156
{"_systemfonts_add_local_fonts", (DL_FUNC) &_systemfonts_add_local_fonts, 1},
157+
{"_systemfonts_clear_local_fonts_c", (DL_FUNC) &_systemfonts_clear_local_fonts_c, 0},
149158
{"_systemfonts_clear_registry_c", (DL_FUNC) &_systemfonts_clear_registry_c, 0},
150159
{"_systemfonts_dev_string_metrics_c", (DL_FUNC) &_systemfonts_dev_string_metrics_c, 6},
151160
{"_systemfonts_dev_string_widths_c", (DL_FUNC) &_systemfonts_dev_string_widths_c, 6},

src/font_local.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,15 @@ int add_local_fonts(cpp11::strings paths) {
8383

8484
FT_Done_FreeType(library);
8585

86+
FontMap& font_map = get_font_map();
87+
font_map.clear();
88+
8689
return 0;
8790
}
91+
92+
void clear_local_fonts_c() {
93+
ResultSet& font_list = get_local_font_list();
94+
font_list.clear();
95+
FontMap& font_map = get_font_map();
96+
font_map.clear();
97+
}

src/font_local.h

+3
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ FontDescriptor *match_local_fonts(FontDescriptor *desc);
1111

1212
[[cpp11::register]]
1313
int add_local_fonts(cpp11::strings paths);
14+
15+
[[cpp11::register]]
16+
void clear_local_fonts_c();

0 commit comments

Comments
 (0)