-
Notifications
You must be signed in to change notification settings - Fork 0
/
combiners.scrbl
160 lines (133 loc) · 6.59 KB
/
combiners.scrbl
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
#lang scribble/manual
@;; to remove the red links, try
@;; $ scribble +m --htmls --dest htmls --redirect-main http://docs.racket-lang.org main.scrbl && xdg-open htmls/main/index.html
@(require (for-label text-block racket/base racket/list racket/math)
"example.rkt")
@(define the-eval (make-eval 'text-block 'racket/list 'racket/math))
@title{Text block combiners and basic operations}
@defproc[(make-tblock [nrow exact-nonnegative-integer?]
[ncol exact-nonnegative-integer?]
[char char? #\space])
tblock]{
Returns a new tblock with @racketid[nrow] rows and @racketid[ncol] columns
filled with the character @racketid[char].
@display-example[the-eval (displayln (make-tblock 5 10 #\*))]
}
@defproc[(build-tblock [nrow exact-nonnegative-integer?]
[ncol exact-nonnegative-integer?]
[get-char (-> exact-nonnegative-integer?
exact-nonnegative-integer?
char?)])
tblock?]{
Returns a new tblock with @racketid[nrow] rows and @racketid[ncol] columns
and calls @racket[get-char] for each (row, column) to obtain the corresponding character.
@display-example[the-eval
(displayln
(build-tblock 2 26 (λ (row col) (integer->char
(+ 65 col (* row 32))))))]
}
@defproc[(happend [#:align align (or/c 'top 'center 'bottom 'baseline) 'baseline]
[#:pad-char pad-char char? #\space]
[t tblock/any] ...)
tblock?]{
Appends the tblocks @racketid[t] horizontally.}
@display-example[the-eval (displayln (happend "A\nA"
" "
"B\nB\nB\nB"
":\n:"
"C\nC"
#:align 'center))]
@defproc[(vappend [#:align align (one-of/c 'left 'center 'right) 'left]
[#:pad-char char? pad-char #\space]
[#:baseline-of t-bl
(or/c exact-nonnegative-integer?
'first 'second 'last
(list exact-nonnegative-integer?)
tblock?)
(if (empty? ts) 0 'first)]
[t tblock/any] ...)
tblock?]{
Appends the tblocks @racketid[t] vertically.
If @racketid[t-bl] is a number, it is used as the corresponding row of the resulting tblock.
If @racketid[t-bl] is one of the symbols, or @racket[tblock?] (member of @racketid[ts]),
or a number in parenthesis (considered as an index in @racketid[ts]),
then the baseline of the corresponding element in @racketid[ts] is used as the new baseline.
@display-example[the-eval (displayln (vappend "Twinkle, twinkle,"
"Little star,"
"How I wonder what you are!"
#:align 'center))]
}
@defproc[(superimpose [t1 tblock/any]
[t2 block/any]
[rpos (or/c exact-integer?
(-> exact-nonnegative-integer?
exact-nonnegative-integer?
exact-integer?)
'top
'center
'bottom)]
[cpos (or/c exact-integer?
(-> exact-nonnegative-integer?
exact-nonnegative-integer?
exact-integer?)
'left
'center
'right)])
tblock?]{
Returns a new tblock where @racketid[t2] is superimposed over @racketid[t1].
The position @racketid[rpos] can either be
a number relative to the top left of @racketid[t1],
a procedure that takes the widths of both @racket[t1] and @racket[t2]
and returns a number relative to the top left of @racketid[t1],
or a symbolic value---and similarly for @racketid[cpos].
@display-example[the-eval
(displayln (superimpose (make-tblock 8 4 #\.)
"AB\nCD"
'bottom 'right))
(displayln (superimpose (make-tblock 8 4 #\.)
"AB\nCD"
1 'left))
(displayln (superimpose (make-tblock 10 20 #\.)
"AB\nCD"
'center
(λ (w1 w2) (exact-truncate (* 3/4 (- w1 w2))))))]
}
@defthing[frame-style/c contract?]{
A contract for @racket[frame] styles.
Matches @racket[(or/c (one-of/c 'single 'round 'double 'heavy)
(list/c string? string? string?))]
where the list of strings is for defining custom styles.
}
@defproc[(frame
[t tblock/any]
[#:style style frame-style/c 'single]
[#:inset inset exact-nonnegative-integer? 0])
tblock?]{
Adds a frame aronud the tblock @racketid[t].
@display-example[the-eval (displayln (frame "Someone framed me!\nI swear!"))]
A custom style defines all characters of the frame as well as the padding character:
@display-example[the-eval
(displayln (frame (lines->tblock "Someone framed me!\nI swear!"
#:pad-char #\.)
#:inset 2
#:style '("(+)"
"[.]"
"{~}")))]
}
@defthing[line-style/c contract?]{
A contract for @racket[overline] and @racket[underline].
Matches @racket[(or/c 'single 'double 'heavy char?)].}
@defproc*[([(overline [t tblock/any] [#:style style line-style/c 'single]) tblock?]
[(underline [t tblock/any] [#:style style line-style/c 'single]) tblock?])]{
Draws a line above or below the text.
@display-example[
the-eval
(displayln (happend (overline "hello") " " (underline 'there #:style 'double) "!"))]
}
@defproc*[([(hflip [t tblock/any]) tblock?]
[(vflip [t tblock/any]) tblock?])]{
Returns a new tblock like @racketid[t], flipped either horizontally or vertically.
@display-example[the-eval
(displayln (vflip "ab\nAB"))
(displayln (hflip "ab\nAB"))]
}