You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/quad/quad/quad.rkt

49 lines
2.1 KiB
Racket

6 years ago
#lang debug racket/base
(require racket/match racket/function "generic.rkt")
6 years ago
(provide (all-defined-out))
(module+ test (require rackunit))
(struct $quad (attrs elems) #:transparent #:mutable
6 years ago
#:methods gen:quad
[(define (elems q) ($quad-elems q))
(define (attrs q) ($quad-attrs q))
(define (start q) (hash-ref (attrs q) 'start 'nw))
(define (end q) (hash-ref (attrs q) 'end 'ne))
(define (inner q) (hash-ref (attrs q) 'inner (λ () (start q))))
(define (size q [condition #f]) (hash-ref (attrs q) 'size 1))
(define (offset q [condition #f]) (hash-ref (attrs q) 'offset 0))
(define (origin q) (hash-ref (attrs q) 'origin 0))
(define (set-origin! q val) (set-$quad-attrs! q (hash-set (attrs q) 'origin val)))
6 years ago
(define (draw q [surface #f] [origin #f]) ((hash-ref (attrs q) 'draw (λ () (λ () (println "<no draw routine>"))))))])
6 years ago
6 years ago
(define (quad-attrs? x) (and (hash? x) (hash-eq? x)))
(define (quad-elem? x) (or (char? x) (string? x) ($quad? x)))
(define (quad-elems? xs) (and (pair? xs) (andmap quad-elem? xs)))
(define (quad #:type [type $quad] . xs)
6 years ago
(match xs
[(list #f xs ...) (apply quad #:type type (hasheq) xs)]
[(list (? quad-attrs? attrs) (? quad-elem? elems) ...) (type attrs elems)]
[(list (? quad-elem? elems) ...) (apply quad #:type type #f elems)]
6 years ago
[else (error 'bad-quad-input)]))
6 years ago
(define q quad)
6 years ago
(define (quads? xs) (andmap quad? xs))
6 years ago
(define (atomic-quad? x) (and (quad? x) (match (elems x)
[(list (? char?)) #t]
[else #f])))
6 years ago
(define (atomic-quads? xs) (andmap atomic-quad? xs))
6 years ago
(module+ test
(check-true (atomic-quad? ($quad '#hasheq() '(#\H))))
(check-true (atomic-quads? (list ($quad '#hasheq() '(#\H))))))
(struct $break $quad () #:transparent)
(define (break . xs) (apply quad #:type $break xs))
(define b break)
6 years ago
6 years ago
(module+ test
(define x ($quad (hasheq 'entrance 0
'exit 10+10i
'inner 5+5i
'size 8+8i
'draw (λ () (println "foo"))) '(#\H #\e #\l #\o)))
(draw x))