-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont.rkt
51 lines (44 loc) · 1.34 KB
/
font.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
#lang racket/base
(require racket/string
(except-in 2htdp-raven/image text))
(provide the-font
normalize-text-charset)
(define printable-ASCII (string-split
#<<ASCII
!"#$%&'()*+,-./
0123456789;:<=>?
@ABCDEFGHIJKLMNO
PQRSTUVWXYZ[\]^_
`abcdefghijklmno
pqrstuvwxyz{|}~
ASCII
"\n"
))
(define atlas-filename "apricot-atlas.png")
(define (load-font charset filename)
(define image (bitmap/file filename))
(define row-len (string-length (car charset)))
(define rows (length charset))
(define char-width (/ (image-width image) row-len))
(define char-height (/ (image-height image) rows))
(for*/hash ([px row-len]
[py rows])
(values (list-ref (string->list (list-ref charset py)) px)
(crop (* px char-width)
(* py char-height)
char-width
char-height
image))))
(define the-font (load-font printable-ASCII atlas-filename))
(define (normalize-text-charset text)
(list->string
(for/list ([char (in-string text)])
(case char
[(#\“ #\”) #\"]
[(#\‘ #\’) #\']
[(#\—) #\-]
[(#\newline) #\newline]
[else
(unless (hash-has-key? the-font char)
(error "unmapped char in normalization" char))
char]))))