-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrhythm.styl
74 lines (65 loc) · 2.54 KB
/
rhythm.styl
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
/* rhythm(fontsize, baseline, margintop, marginbottom, squash) applies a vertical
* rhythm to a block-level element. Arguments:
*
* fontsize: the font size, relative to the base font size
*
* baseline: the baseline grid unit that we should snap the element's
* line-height to
*
* margintop, marginbottom: the number of baseline units to add above and
* below the element. Note: this is *baseline units*, not multiples of the
* element's new line-height.
*
* squash: a float value specifying how much tolerance the line-height
* should have for squashing. 1 means the line-height will never be smaller
* than 1; <1 means it may be squashed; >1 means it must have extra
*
* allow-half-lines: whether or not to permit rounding to half-lines. Pretty
* important for font-sizes below baseline / 2, actually.
*/
rhythm(fontsize = 1em, baseline = null, margin = 1, squash = 1, allow-half-lines = false) {
font-size: fontsize;
line-height: rhythm-line-height(fontsize, baseline, squash, allow-half-lines);
baseline-size = rhythm-baseline-size(fontsize, baseline);
margin = expand-sides(margin, 2);
if (margin[0] != none) {
margin-top: unit(baseline-size * margin[0], em);
}
if (margin[1] != none) {
margin-bottom: unit(baseline-size * margin[1], em);
}
}
rhythm-ratio(fontsize = 1em, baseline = null, squash = 1, allow-half-lines = false) {
baseline = rhythm-get-baseline(baseline);
warn("Ratio: baseline " + baseline);
ratio = (fontsize / baseline) * squash;
warn("Ratio: fontsize " + fontsize);
warn("Ratio: squash " + squash);
warn("Ratio: ratio " + ratio);
if (allow-half-lines) {
ratio = ceil(ratio * 2) / 2;
} else {
ratio = ceil(ratio);
}
ratio;
}
// get the size of the baseline grid, in units relative to the given font size
rhythm-baseline-size(fontsize = 1em, baseline = null) {
baseline = rhythm-get-baseline(baseline);
warn("rhythm-get-baseline " + baseline);
warn("Baseline / fontsize " + (baseline / fontsize));
(baseline / fontsize);
}
// fit the font into the baseline grid
rhythm-line-height(fontsize = 1em, baseline = null, squash = 1, allow-half-lines = false) {
baseline = rhythm-get-baseline(baseline);
ratio = rhythm-ratio(fontsize, baseline, squash, allow-half-lines);
warn("Ratio " + ratio);
(baseline * ratio / fontsize);
}
// checks for a baseline size defined elsewhere in the CSS before assigning a default of 1.5
rhythm-get-baseline(baseline = null) {
warn("Baseline " + baseline);
warn("Baselinesize " + baselinesize);
baseline ? baseline : (baselinesize is defined ? baselinesize : 1.5);
}