Skip to content

Commit 3599a59

Browse files
committed
better vectorisation of styles
1 parent 9897623 commit 3599a59

File tree

5 files changed

+186
-140
lines changed

5 files changed

+186
-140
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ S3method(makeContent,marquee_precalculated)
1919
S3method(makeContent,svg_grob)
2020
S3method(makeContext,marquee_grob)
2121
S3method(makeContext,marquee_precalculated_grob)
22+
S3method(modify_style,marquee_style)
23+
S3method(modify_style,marquee_style_set)
2224
S3method(print,marquee_em)
2325
S3method(print,marquee_relative)
2426
S3method(print,marquee_rem)

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# marquee (development version)
22

3+
* Better vectorisation of style_set and classic_style
4+
35
# marquee 1.1.1
46

57
* Fixed a bug in `element_marquee()` that resulted in wrong width calculation

R/classic_style.R

Lines changed: 136 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -31,105 +31,141 @@ classic_style <- function(
3131
...,
3232
ltr = TRUE
3333
) {
34-
base <- base_style(family = body_font, size = base_size, ...)
35-
style_set(
36-
base = base,
37-
body = style(margin = skip_inherit(trbl(0))),
38-
ul = style(
39-
padding = if (ltr) trbl(0, 0, 0, em(2)) else trbl(0, em(2), 0, 0),
40-
background = NA,
41-
border = NA
42-
),
43-
ol = style(
44-
padding = if (ltr) trbl(0, 0, 0, em(2)) else trbl(0, em(2), 0, 0),
45-
background = NA,
46-
border = NA
47-
),
48-
li = style(padding = trbl(0), background = NA, border = NA),
49-
hr = style(
50-
padding = trbl(0, 0, rem(1 / 8)),
51-
border = "#eeeeee",
52-
border_size = trbl(0, 0, rem(1 / 16))
53-
),
54-
h1 = style(
55-
family = header_font,
56-
size = relative(2.25),
57-
weight = "bold",
58-
lineheight = 1.2,
59-
margin = trbl(em(1), NULL, NULL, NULL),
60-
padding = trbl(0, 0, em(0.3)),
61-
border = "#eeeeee",
62-
border_size = trbl(0, 0, rem(1 / 16))
63-
),
64-
h2 = style(
65-
family = header_font,
66-
size = relative(1.75),
67-
weight = "bold",
68-
lineheight = 1.225,
69-
margin = trbl(em(1), NULL, NULL, NULL),
70-
padding = trbl(0, 0, em(0.3)),
71-
border = "#eeeeee",
72-
border_size = trbl(0, 0, rem(1 / 16))
73-
),
74-
h3 = style(
75-
family = header_font,
76-
size = relative(1.5),
77-
weight = "bold",
78-
lineheight = 1.43,
79-
margin = trbl(em(1), NULL, NULL, NULL)
80-
),
81-
h4 = style(
82-
family = header_font,
83-
size = relative(1.25),
84-
weight = "bold",
85-
lineheight = 1.4,
86-
margin = trbl(em(1), NULL, NULL, NULL)
87-
),
88-
h5 = style(
89-
family = header_font,
90-
weight = "bold",
91-
lineheight = 1.4,
92-
margin = trbl(em(1), NULL, NULL, NULL)
93-
),
94-
h6 = style(
95-
family = header_font,
96-
weight = "bold",
97-
lineheight = 1.4,
98-
margin = trbl(em(1), NULL, NULL, NULL),
99-
color = "#777777"
100-
),
101-
cb = style(
102-
family = code_font,
103-
size = relative(0.85),
104-
lineheight = 1.45,
105-
padding = trbl(rem(1)),
106-
background = "#f7f7f7",
107-
border_radius = rem(3 / 16)
108-
),
109-
p = style(padding = trbl(0), background = NA, border = NA),
110-
qb = style(
111-
color = "#777777",
112-
padding = if (ltr) trbl(em(0.2), 0, em(0.2), em(1)) else
113-
trbl(em(0.2), em(1), em(0.2), 0),
114-
border = "#dddddd",
115-
border_size = if (ltr) trbl(0, 0, 0, rem(0.25)) else
116-
trbl(0, rem(0.25), 0, 0)
117-
),
118-
em = style(italic = TRUE),
119-
str = style(weight = "bold"),
120-
a = style(color = "#4078c0"),
121-
code = style(
122-
family = code_font,
123-
size = relative(0.85),
124-
background = "#0000000A",
125-
padding = trbl(em(0.2), em(0.1)),
126-
border_radius = rem(3 / 16)
127-
),
128-
u = style(underline = TRUE),
129-
del = style(strikethrough = TRUE),
130-
img = style(align = "center", border = NA),
131-
sub = style(size = em(0.5), baseline = em(-0.2)),
132-
sup = style(size = em(0.5), baseline = em(1)),
133-
out = style(color = "#ffffff", outline = "#000000")
34+
settings <- list2(
35+
base_size = base_size,
36+
body_font = body_font,
37+
header_font = header_font,
38+
code_font = code_font,
39+
...,
40+
ltr = ltr
13441
)
42+
settings <- lapply(settings, function(x) {
43+
if (is_list(x) && !is_bare_list(x)) {
44+
list(x)
45+
} else {
46+
x
47+
}
48+
})
49+
settings <- vctrs::vec_recycle_common(!!!settings)
50+
styles <- lapply(seq_along(settings$base_size), function(i) {
51+
set <- lapply(settings, function(x) x[[i]])
52+
base_size <- set$base_size
53+
set$base_size <- NULL
54+
body_font <- set$body_font
55+
set$body_font <- NULL
56+
header_font <- set$header_font
57+
set$header_font <- NULL
58+
code_font <- set$code_font
59+
set$code_font <- NULL
60+
ltr <- set$ltr
61+
set$ltr <- NULL
62+
base <- inject(base_style(family = body_font, size = base_size, !!!set))
63+
style_set(
64+
base = base,
65+
body = style(margin = skip_inherit(trbl(0))),
66+
ul = style(
67+
padding = if (ltr) trbl(0, 0, 0, em(2)) else trbl(0, em(2), 0, 0),
68+
background = NA,
69+
border = NA
70+
),
71+
ol = style(
72+
padding = if (ltr) trbl(0, 0, 0, em(2)) else trbl(0, em(2), 0, 0),
73+
background = NA,
74+
border = NA
75+
),
76+
li = style(padding = trbl(0), background = NA, border = NA),
77+
hr = style(
78+
padding = trbl(0, 0, rem(1 / 8)),
79+
border = "#eeeeee",
80+
border_size = trbl(0, 0, rem(1 / 16))
81+
),
82+
h1 = style(
83+
family = header_font,
84+
size = relative(2.25),
85+
weight = "bold",
86+
lineheight = 1.2,
87+
margin = trbl(em(1), NULL, NULL, NULL),
88+
padding = trbl(0, 0, em(0.3)),
89+
border = "#eeeeee",
90+
border_size = trbl(0, 0, rem(1 / 16))
91+
),
92+
h2 = style(
93+
family = header_font,
94+
size = relative(1.75),
95+
weight = "bold",
96+
lineheight = 1.225,
97+
margin = trbl(em(1), NULL, NULL, NULL),
98+
padding = trbl(0, 0, em(0.3)),
99+
border = "#eeeeee",
100+
border_size = trbl(0, 0, rem(1 / 16))
101+
),
102+
h3 = style(
103+
family = header_font,
104+
size = relative(1.5),
105+
weight = "bold",
106+
lineheight = 1.43,
107+
margin = trbl(em(1), NULL, NULL, NULL)
108+
),
109+
h4 = style(
110+
family = header_font,
111+
size = relative(1.25),
112+
weight = "bold",
113+
lineheight = 1.4,
114+
margin = trbl(em(1), NULL, NULL, NULL)
115+
),
116+
h5 = style(
117+
family = header_font,
118+
weight = "bold",
119+
lineheight = 1.4,
120+
margin = trbl(em(1), NULL, NULL, NULL)
121+
),
122+
h6 = style(
123+
family = header_font,
124+
weight = "bold",
125+
lineheight = 1.4,
126+
margin = trbl(em(1), NULL, NULL, NULL),
127+
color = "#777777"
128+
),
129+
cb = style(
130+
family = code_font,
131+
size = relative(0.85),
132+
lineheight = 1.45,
133+
padding = trbl(rem(1)),
134+
background = "#f7f7f7",
135+
border_radius = rem(3 / 16)
136+
),
137+
p = style(padding = trbl(0), background = NA, border = NA),
138+
qb = style(
139+
color = "#777777",
140+
padding = if (ltr) {
141+
trbl(em(0.2), 0, em(0.2), em(1))
142+
} else {
143+
trbl(em(0.2), em(1), em(0.2), 0)
144+
},
145+
border = "#dddddd",
146+
border_size = if (ltr) {
147+
trbl(0, 0, 0, rem(0.25))
148+
} else {
149+
trbl(0, rem(0.25), 0, 0)
150+
}
151+
),
152+
em = style(italic = TRUE),
153+
str = style(weight = "bold"),
154+
a = style(color = "#4078c0"),
155+
code = style(
156+
family = code_font,
157+
size = relative(0.85),
158+
background = "#0000000A",
159+
padding = trbl(em(0.2), em(0.1)),
160+
border_radius = rem(3 / 16)
161+
),
162+
u = style(underline = TRUE),
163+
del = style(strikethrough = TRUE),
164+
img = style(align = "center", border = NA),
165+
sub = style(size = em(0.5), baseline = em(-0.2)),
166+
sup = style(size = em(0.5), baseline = em(1)),
167+
out = style(color = "#ffffff", outline = "#000000")
168+
)
169+
})
170+
vctrs::vec_c(!!!styles)
135171
}

R/geom_marquee.R

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,13 @@ make_marquee_geom <- function() {
175175
)
176176
}
177177

178-
styles <- modify_style(
178+
styles <- combine_styles(
179179
styles,
180-
"base",
181180
family = data$family,
182181
size = size,
183182
lineheight = data$lineheight,
184-
color = colour
185-
)
186-
styles <- modify_style(
187-
styles,
188-
"body",
189-
background = skip_inherit(data$fill)
183+
color = colour,
184+
background = data$fill
190185
)
191186

192187
data <- coord$transform(data, panel_params)

R/style_set.R

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,26 @@ format.marquee_style_set <- function(x, ...) {
8181
#' @rdname style_set
8282
#' @export
8383
modify_style <- function(x, tag, ...) {
84-
opts <- list2(...)
84+
UseMethod("modify_style")
85+
}
86+
#' @export
87+
modify_style.marquee_style_set <- function(x, tag, ...) {
88+
tag <- tolower(tag)
89+
check_character(tag)
90+
91+
opts <- lapply(list2(...), function(x) {
92+
if (is_list(x) && !is_bare_list(x)) {
93+
list(x)
94+
} else {
95+
x
96+
}
97+
})
98+
opts <- vctrs::vec_recycle_common(x = x, tag = tag, !!!opts)
99+
x <- opts$x
100+
tag <- opts$tag
101+
opts$x <- NULL
102+
opts$tag <- NULL
103+
85104
args <- names(opts)
86105
expand <- args %in% c("margin", "padding", "border_size")
87106
if (any(expand)) {
@@ -95,37 +114,6 @@ modify_style <- function(x, tag, ...) {
95114
)
96115
}
97116

98-
if (is_style(x)) {
99-
new_style <- style(...)
100-
cls <- class(x)
101-
class(x) <- NULL
102-
x[args] <- new_style[args]
103-
class(x) <- cls
104-
return(x)
105-
}
106-
107-
tag <- tolower(tag)
108-
if (!is_style_set(x)) {
109-
stop_input_type(x, "a style set object")
110-
}
111-
check_character(tag)
112-
tag <- vctrs::vec_recycle(tag, length(x))
113-
114-
for (i in seq_along(opts)) {
115-
opt <- opts[[i]]
116-
if (
117-
is.null(opt) ||
118-
is_style(opt) ||
119-
is_modifier(opt) ||
120-
is_trbl(opt) ||
121-
inherits(opt, "font_feature") ||
122-
inherits(opt, "GridPattern")
123-
) {
124-
opt <- list(opt)
125-
}
126-
opts[[i]] <- vctrs::vec_recycle(opt, length(x), x_arg = names(opts)[i])
127-
}
128-
129117
for (i in seq_along(x)) {
130118
if (is_style(opts[[1]][[i]])) {
131119
if (
@@ -158,6 +146,29 @@ modify_style <- function(x, tag, ...) {
158146
x
159147
}
160148

149+
#' @export
150+
modify_style.marquee_style <- function(x, tag, ...) {
151+
opts <- list2(...)
152+
args <- names(opts)
153+
expand <- args %in% c("margin", "padding", "border_size")
154+
if (any(expand)) {
155+
args <- c(
156+
args[!expand],
157+
paste0(
158+
rep(args[expand], each = 4),
159+
"_",
160+
c("top", "right", "bottom", "left")
161+
)
162+
)
163+
}
164+
new_style <- style(...)
165+
cls <- class(x)
166+
class(x) <- NULL
167+
x[args] <- new_style[args]
168+
class(x) <- cls
169+
x
170+
}
171+
161172
#' @rdname style_set
162173
#' @export
163174
remove_style <- function(x, tag) {

0 commit comments

Comments
 (0)