-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathshowy.typ
268 lines (249 loc) · 8.66 KB
/
showy.typ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* ShowyBox - A package for Typst
* Pablo González Calderón and Showybox Contributors (c) 2023-2024
*
* Main Contributors:
* - Jonas Neugebauer (<https://github.com/jneug>)
*
* showy.typ -- The package's main file containing the
* public and (more) useful functions
*
* This file is under the MIT license. For more
* information see LICENSE on the package's main folder.
*/
/*
* Import functions
*/
#import "lib/func.typ": *
#import "lib/sections.typ": *
#import "lib/shadows.typ": *
#import "lib/id.typ": *
#import "lib/pre-rendering.typ": *
/*
* Function: showybox()
*
* Description: Creates a showybox
*
*/
#let showybox(
frame: (:),
title-style: (:),
boxed-style: (:),
body-style: (:),
footer-style: (:),
sep: (:),
shadow: none,
width: 100%,
breakable: false,
/* align: none, / collides with align-function */
/* spacing, above, and below are by default what's set for all `block`s */
title: "",
footer: "",
..body,
) = {
/*
* Complete and store all the dictionary-like-properties inside a
* single dictionary. This will improve readability and avoids to
* constantly have a default option while accessing
*/
let props = (
frame: (
title-color: frame.at("title-color", default: black),
body-color: frame.at("body-color", default: white),
border-color: frame.at("border-color", default: black),
footer-color: frame.at("footer-color", default: luma(220)),
inset: frame.at("inset", default: (x: 1em, y: .65em)),
radius: frame.at("radius", default: 5pt),
thickness: frame.at("thickness", default: 1pt),
dash: frame.at("dash", default: "solid"),
),
title-style: (
color: title-style.at("color", default: white),
weight: title-style.at("weight", default: "regular"),
align: title-style.at("align", default: start),
sep-thickness: title-style.at("sep-thickness", default: 1pt),
boxed-style: if title-style.at("boxed-style", default: none) != none and type(title-style.at("boxed-style", default: none)) == dictionary {
(
anchor: (
y: title-style.boxed-style.at("anchor", default: (:)).at("y", default: horizon),
x: title-style.boxed-style.at("anchor", default: (:)).at("x", default: start),
),
offset: (
x: title-style.boxed-style.at("offset", default: (:)).at("x", default: 0pt),
y: title-style.boxed-style.at("offset", default: (:)).at("y", default: 0pt),
),
radius: title-style.boxed-style.at("radius", default: 5pt),
)
} else {
none
},
),
body-style: (
color: body-style.at("color", default: black),
align: body-style.at("align", default: start),
),
footer-style: (
color: footer-style.at("color", default: luma(85)),
weight: footer-style.at("weight", default: "regular"),
align: footer-style.at("align", default: start),
sep-thickness: footer-style.at("sep-thickness", default: 1pt),
),
sep: (
thickness: sep.at("thickness", default: 1pt),
dash: sep.at("dash", default: "solid"),
gutter: sep.at("gutter", default: 0.65em),
),
shadow: if shadow != none {
if type(shadow.at("offset", default: 4pt)) != dictionary {
(offset: (
x: shadow.at("offset", default: 4pt),
y: shadow.at("offset", default: 4pt),
), color: shadow.at("color", default: luma(128)))
} else {
(offset: (
x: shadow.at("offset").at("x", default: 4pt),
y: shadow.at("offset").at("y", default: 4pt),
), color: shadow.at("color", default: luma(128)))
}
} else {
none
},
breakable: breakable,
width: width,
title: title,
)
// Add title, body and footer inset (if present)
for section-inset in ("title-inset", "body-inset", "footer-inset") {
let value = frame.at(section-inset, default: none)
if value != none {
props.frame.insert(section-inset, value)
}
}
_showy-id.step()
context {
let id = str(_showy-id.get().first())
/*
* Update title height in state.
*
* NOTE: Although a `place` and `hide` are used in the pre-render
* function, for avoiding nesting components inside unaccesible
* containers, we must call this function inside another `place`.
*/
if title != "" and props.title-style.boxed-style != none {
place(top, showy-pre-render-title(props, id))
}
/*
* Alignment wrapper
*/
let alignprops = (:)
for prop in ("spacing", "above", "below") {
if prop in body.named() {
alignprops.insert(prop, body.named().at(prop))
}
}
let alignwrap(content) = block(
..alignprops,
breakable: breakable,
width: 100%,
if "align" in body.named() and body.named().align != none {
align(body.named().align, content)
} else {
content
},
)
let showyblock = context {
let my-state = state("showybox-" + id, 0pt)
// /*
// * Decide wheter add a page break or not, based on the remaining
// * space in the page. For calculating it, we substract the page
// * margin to the page size. Later, we estimate a minimum size of
// * the showybox, adding the frame thickness, the frame inset,
// * and the size of 1 line of text.
// */
// let abs-margin = if page.margin == auto {
// let small-side = calc.min(page.height, page.width)
// (2.5/21) * small-side // According to docs, this is the 'auto' margin
// } else {
// margin.at("y", default: margin.at("top", default: margin.rest))
// }
// let remaining = 0pt
// let to-use = 0pt
// remaining = page.height - location.position(here()).y - abs-margin
// to-use += text.size.to-absolute()
// if type(props.frame.thickness) == dictionary {
// to-use += props.frame.thickness.at("y", default: props.frame.thickness.at("top", default: props.frame.inset.at("rest", default: .65em))).to-absolute()
// to-use += props.frame.thickness.at("y", default: props.frame.thickness.at("bottom", default: props.frame.inset.at("rest", default: .65em))).to-absolute()
// } else {
// to-use += 2*props.frame.thickness
// }
// if type(props.frame.inset) == dictionary {
// to-use += 4*props.frame.inset.at("y", default: props.frame.inset.at("top", default: props.frame.inset.at("rest", default: .65em))).to-absolute()
// } else {
// to-use += 4*props.frame.inset.to-absolute()
// }
// // Since we cannot add a pagebreak directly, add the to-use space,
// // which would be greater than the remaining space.
// if remaining - to-use <= 0pt {
// v(to-use)
// }
if title != "" and props.title-style.boxed-style != none {
if props.title-style.boxed-style.anchor.y == bottom {
v(my-state.final())
} else if props.title-style.boxed-style.anchor.y == horizon {
v(my-state.final() / 2)
} // Otherwise don't add extra space
// Add the boxed-title shadow before rendering the body
if props.shadow != none {
showy-boxed-title-shadow(props, id)
}
}
block(
width: if props.shadow == none {
width
} else {
100%
},
fill: props.frame.body-color,
radius: props.frame.radius,
inset: 0pt,
spacing: 0pt,
breakable: breakable,
stroke: showy-stroke(props.frame),
)[
/*
* Title of the showybox
*/
#if title != "" and props.title-style.boxed-style == none {
showy-title(props)
} else if title != "" and props.title-style.boxed-style != none {
if props.title-style.boxed-style.anchor.y == top {
v(my-state.final())
} else if props.title-style.boxed-style.anchor.y == horizon {
v(my-state.final() / 2)
}
place(
top + props.title-style.boxed-style.anchor.x,
dx: props.title-style.boxed-style.offset.x,
dy: props.title-style.boxed-style.offset.y + if props.title-style.boxed-style.anchor.y == bottom {
-my-state.final()
} else if props.title-style.boxed-style.anchor.y == horizon {
-my-state.final() / 2
},
block(spacing: 0pt, inset: (x: 1em), showy-title(props)),
)
}
/*
* Body of the showybox
*/
#showy-body(props, ..body)
/*
* Footer of the showybox
*/
#if footer != "" {
showy-footer(props, footer)
}
]
}
alignwrap(showy-shadow(props, showyblock, id))
}
}