-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage-text.rkt
171 lines (147 loc) · 4.98 KB
/
image-text.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
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
#lang racket
;;; File:
;;; image-text.rkt
;;; Summary:
;;; A variety of procedures for working with text as an image.
;;; Author:
;;; Samuel A. Rebelsky
;;;
;;; IMPORTANT: If you are going to edit this file (or any of the
;;; imported files), make sure that you've updated the DrRacket editor
;;; to treat `sstruct` like `struct`.
(require racket/generic)
(require racket/include)
(require (prefix-in 2htdp: 2htdp/image))
(require "sstruct.rkt")
(require "cloneable.rkt")
(permit-cloneable)
(permit-done)
(require "type-predicates.rkt")
(require "colors.rkt")
(require "point.rkt")
(require "line.rkt")
(require "image-core.rkt")
(provide (all-defined-out))
; +-------+----------------------------------------------------------
; | Fonts |
; +-------+
(sstruct %font (face family style weight underline?)
#:reflection-name 'font
#:transparent)
(define font? %font?)
(define font-face %font-face)
(define font-family %font-family)
(define font-style %font-style)
(define font-weight %font-weight)
(define font-underline? %font-underline?)
;;; (font face family style weight underline?)
;;; face : (any-of string? false?)
;;; family : (one-of "default" "decorative" "roman" "script"
;;; "swiss" "modern" "symbol" "system")
;;; style : (one-of "normal" "italic")
;;; weight : (one-of "normal" "bold" "light")
;;; underline? : boolean?
;;; Create a font value for use in building text.
(define font
(lambda (face family style weight underline?)
(param-check! font 1 (any-of string? false?) face)
(param-check! font 2
(one-of "default" "decorative" "roman" "script"
"swiss" "modern" "symbol" "system")
family)
(param-check! font 3 (one-of "normal" "italic") style)
(param-check! font 4 (one-of "normal" "bold" "light") weight)
(param-check! font 5 boolean? underline?)
(%font face family style weight underline?)))
;;; (default-font [default]) -> font
;;; default : font?
;;; Get or set the defalt font.
(define default-font
(let ([deffont (font #f "default" "normal" "normal" #f)])
(lambda params
(when (not (null? params))
(let ([default (car params)])
(param-check! default-font 1 font? default)
(set! deffont default)))
deffont)))
; +-------------+----------------------------------------------------
; | Text basics |
; +-------------+
(sstruct %text %shape (string size font)
#:cloneable
#:methods gen:img-make-desc
[(define image-make-desc
(lambda (img)
(format "the text [~a]"
(text-string img))))]
#:methods gen:img-make-pict
[(define image-make-pict
(lambda (img)
(let ([font (text-font img)])
(2htdp:text/font (text-string img)
(text-size img)
(color->2htdp (image-color img))
(font-face font)
(font-family font)
(font-style font)
(font-weight font)
(font-underline? font)))))]
#:methods gen:img-make-stru
[(define image-make-stru
(lambda (img)
(list 'text
(text-string img)
(text-size img)
(image-color img)
(text-font img))))]
#:done)
;;; (text? img) -> boolean?
;;; image : image?
;;; Determines whether `img` is a text image.
(define text? %text?)
;;; (text-string text) -> string?
;;; test : text?
;;; Grab the string associated with a text image.
(define text-string %text-string)
;;; (text-size text) -> nonnegative-exact-integer?
;;; test : text?
;;; Grab the size of the text in a text image (in pixels).
(define text-size %text-size)
;;; (text-color text) -> rgb?
;;; test : text?
;;; Grab the string associated with a text image.
(define text-color %basic-image-color)
;;; (text-font text) -> font?
;;; font : text?
;;; Grab the font associated with a text image.
(define text-font %text-font)
;;; (text string size color [font]) -> text?
;;; string : string?
;;; size : (all-of exact-positive-integer? (less-than 256))
;;; color : color?
;;; font : font?
;;; Create an image of text of the given size and color, in either
;;; the specified font (if given) or the default font (if not given).
(define text
(lambda (string size color [font (default-font)])
(%text #f #f #f #f
color
string
(min 255 (max 1 size ))
font)))
; +----------------------------+-------------------------------------
; | Additional text procedures |
; +----------------------------+
;; (find-text str img) -> (or text? #f)
;;; img : image?
;;; Finds a text block that contains the given string, if there is
;;; one.
(define find-text
(lambda (str img)
(or (and (text? img)
(string-ci=? str (text-string img))
img)
(and (transformed? img)
(find-text str (subimage img)))
(and (compound? img)
(ormap (lambda (i) (find-text str i)) (subimages img))))))