forked from oplS17projects/FP3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexploration2.rkt
81 lines (71 loc) · 3.25 KB
/
exploration2.rkt
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
#lang racket
(require racket/draw)
;; Abstraction for list of strings as a paragraph
(define (make-paragraph . words) words)
;; BEGIN MAKE-BOOK
;; Closure for a book
(define (make-book title)
;; Constuctor (more or less)
(define book
(new pdf-dc%
[ interactive #f ]
[ use-paper-bbox #f ]
[ as-eps #f ]
[ width 592 ]
[ height 756 ]
[ output title ]))
;; Page management
(define (open-book) (send* book (start-doc "hello") (start-page)))
(define (turn-page) (begin (send* book (end-page) (start-page))
(set! row 0) (set! col 40)))
(define (close-book) (send* book (end-page) (end-doc)))
;; Variables for keeping track of position
(define row 0)
(define col 40)
(define (newpara)
(begin (set! col 40) (set! row (+ row 40))))
(define (update-position w h d a)
(if (< col 500)
(begin (set! col (+ col w 2)) #f)
(begin (set! col 0) (set! row (+ row h 2)) #t)))
;; Writes a paragraph to the book
(define (write-paragraph para)
(if (null? para)
(newpara)
(begin (send book draw-text (car para) col row)
(call-with-values (lambda () (send book get-text-extent (car para))) update-position)
(write-paragraph (cdr para)))))
;; Dispatch function
(lambda (m)
(cond ((eq? m 'open) open-book)
((eq? m 'turn-page) turn-page)
((eq? m 'close) close-book)
((eq? m 'write) write-paragraph)
(else (error "Unknown request")))))
;; END OF MAKE-BOOK
;; Sample session.
;; Creates file "exploration2.pdf in cwd.
(define mypara (make-paragraph "Hello" "my" "name" "is" "Doug."))
(define mypara2 (make-paragraph "This" "is" "my" "Exploration" "2."))
(define mypara3 (make-paragraph
"Lorem" "ipsum" "dolor" "sit" "amet," "consectetur" "adipiscing" "elit." "Sed" "cursus" "ante" "dapibus" "diam."
"Lorem" "ipsum" "dolor" "sit" "amet," "consectetur" "adipiscing" "elit." "Sed" "cursus" "ante" "dapibus" "diam."
"Lorem" "ipsum" "dolor" "sit" "amet," "consectetur" "adipiscing" "elit." "Sed" "cursus" "ante" "dapibus" "diam."
"Lorem" "ipsum" "dolor" "sit" "amet," "consectetur" "adipiscing" "elit." "Sed" "cursus" "ante" "dapibus" "diam."
"Lorem" "ipsum" "dolor" "sit" "amet," "consectetur" "adipiscing" "elit." "Sed" "cursus" "ante" "dapibus" "diam."
"Lorem" "ipsum" "dolor" "sit" "amet," "consectetur" "adipiscing" "elit." "Sed" "cursus" "ante" "dapibus" "diam."
"Lorem" "ipsum" "dolor" "sit" "amet," "consectetur" "adipiscing" "elit." "Sed" "cursus" "ante" "dapibus" "diam."
"Lorem" "ipsum" "dolor" "sit" "amet," "consectetur" "adipiscing" "elit." "Sed" "cursus" "ante" "dapibus" "diam."
"Lorem" "ipsum" "dolor" "sit" "amet," "consectetur" "adipiscing" "elit." "Sed" "cursus" "ante" "dapibus" "diam."
"Lorem" "ipsum" "dolor" "sit" "amet," "consectetur" "adipiscing" "elit." "Sed" "cursus" "ante" "dapibus" "diam."
"Lorem" "ipsum" "dolor" "sit" "amet," "consectetur" "adipiscing" "elit." "Sed" "cursus" "ante" "dapibus" "diam."))
(define myBook (make-book "exploration2.pdf"))
((myBook 'open))
((myBook 'write) mypara)
((myBook 'write) mypara2)
((myBook 'write) mypara3)
((myBook 'turn-page))
((myBook 'write) mypara3)
((myBook 'write) mypara3)
((myBook 'write) mypara3)
((myBook 'close))