Skip to content

Commit fb13bd4

Browse files
committed
[style] use hyperlink color if found in theme
1 parent c9895c6 commit fb13bd4

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

R/class-workbook.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -3120,7 +3120,9 @@ wbWorkbook <- R6::R6Class(
31203120
font_name = "Aptos Narrow",
31213121
...
31223122
) {
3123-
standardize(...)
3123+
arguments <- c("font_size", "font_color", "font_name",
3124+
"font_type", "font_panose")
3125+
standardize(..., arguments = arguments)
31243126
if (font_size < 0) stop("Invalid font_size")
31253127
if (!is_wbColour(font_color)) font_color <- wb_color(font_color)
31263128

@@ -3132,7 +3134,7 @@ wbWorkbook <- R6::R6Class(
31323134
if (!exists("font_type")) font_type <- "Regular"
31333135

31343136
sel <- panose$family == font_name & panose$type == font_type
3135-
if (!any(sel)) {
3137+
if (!any(sel) && !exists("font_panose")) {
31363138
panose_hex <- NULL
31373139
} else if (exists("font_panose")) {
31383140
# the input provides a panose value

R/write.R

+15-1
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,24 @@ write_data2 <- function(
467467
dim_sel <- get_data_class_dims("hyperlink")
468468
# message("hyperlink: ", dim_sel)
469469

470+
# get hyperlink color from template
471+
if (is.null(wb$theme)) {
472+
has_hlink <- 11
473+
} else {
474+
clrs <- xml_node(wb$theme, "a:theme", "a:themeElements", "a:clrScheme")
475+
has_hlink <- which(xml_node_name(clrs, "a:clrScheme") == "a:hlink")
476+
}
477+
478+
if (has_hlink) {
479+
hyperlink_col <- wb_color(theme = has_hlink - 1L)
480+
} else {
481+
hyperlink_col <- wb_color(hex = "FF0000FF")
482+
}
483+
470484
wb$add_font(
471485
sheet = sheetno,
472486
dims = dim_sel,
473-
color = wb_color(hex = "FF0000FF"),
487+
color = hyperlink_col,
474488
name = wb_get_base_font(wb)$name$val,
475489
size = wb_get_base_font(wb)$size$val,
476490
u = "single"

tests/testthat/test-base_font.R

+38
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,42 @@ test_that("wb_set_base_font() actually alters the base font", {
7272
fS <- xml_node(wb$theme, "a:theme", "a:themeElements", "a:fontScheme")
7373
expect_equal(character(), fS)
7474

75+
# custom panose values are possible
76+
wb <- wb_workbook(theme = thm)$
77+
set_base_font(font_name = "Monaco", font_panose = "xxxxxxxxxxxxxx")
78+
fS <- xml_node(wb$theme, "a:theme", "a:themeElements", "a:fontScheme")
79+
80+
exp <- "<a:latin typeface=\"Monaco\" panose=\"xxxxxxxxxxxxxx\"/>"
81+
got <- xml_node(fS, "a:fontScheme", "a:majorFont", "a:latin")
82+
expect_equal(exp, got)
83+
got <- xml_node(fS, "a:fontScheme", "a:minorFont", "a:latin")
84+
expect_equal(exp, got)
85+
86+
# different font types are possible for panose, not sure how useful this is
87+
wb <- wb_workbook()$
88+
set_base_font(font_name = "Arial", font_type = "Italic")
89+
fS <- xml_node(wb$theme, "a:theme", "a:themeElements", "a:fontScheme")
90+
91+
exp <- "<a:latin typeface=\"Arial\" panose=\"020B0604020202090204\"/>"
92+
got <- xml_node(fS, "a:fontScheme", "a:majorFont", "a:latin")
93+
expect_equal(exp, got)
94+
got <- xml_node(fS, "a:fontScheme", "a:minorFont", "a:latin")
95+
expect_equal(exp, got)
96+
97+
})
98+
99+
test_that("hyperlink font size works", {
100+
101+
wb <- wb_workbook()$
102+
set_base_font(font_size = 13, font_name = "Monaco")$
103+
add_worksheet()$
104+
add_formula(x = create_hyperlink(text = "foo", file = "bar"))
105+
106+
exp <- c(
107+
"<font><color theme=\"1\"/><family val=\"2\"/><name val=\"Monaco\"/><scheme val=\"minor\"/><sz val=\"13\"/></font>",
108+
"<font><color theme=\"10\"/><name val=\"Monaco\"/><sz val=\"13\"/><u val=\"single\"/></font>"
109+
)
110+
got <- wb$styles_mgr$styles$fonts
111+
expect_equal(exp, got)
112+
75113
})

0 commit comments

Comments
 (0)