change some names

main
Matthew Butterick 6 years ago
parent fa76b65de7
commit eeaddc0b77

@ -54,8 +54,8 @@
(match-define (list str) (quad-elems q))
(match-define (list x y) (quad-origin q))
(text doc str x y #:bg (hash-ref (quad-attrs q) 'bg #f)
#:link (hash-ref (quad-attrs q) 'link #f))
(draw-debug q doc "#99f" "#ccf"))))
#:link (hash-ref (quad-attrs q) 'link #f)))
#:draw-end (λ (q doc) (draw-debug q doc "#99f" "#ccf"))))
(define-runtime-path charter "fonts/charter.ttf")
(define-runtime-path charter-bold "fonts/charter-bold.ttf")
@ -109,18 +109,14 @@
#:inner 'sw ; puts baseline at lower right corner of line box
#:out 'sw
#:printable #true
#:draw (λ (q doc)
(draw-debug q doc)
(default-draw q doc))))
#:draw-start draw-debug))
(struct line-spacer quad () #:transparent)
(define q:line-spacer (q #:type line-spacer
#:size (pt 380 (* line-height 0.6))
#:out 'sw
#:printable (λ (q sig)
(not (memq sig '(start end))))
#:draw (λ (q doc)
(draw-debug q doc)
(default-draw q doc))))
#:draw-start draw-debug))
(define softies (map string '(#\space #\- #\u00AD)))
(define (soft-break-for-line? q)
@ -176,8 +172,8 @@
(define page-offset (pt side-margin top-margin))
(require racket/date)
(define q:page (q #:offset page-offset
#:pre-draw (λ (q doc) (add-page doc))
#:post-draw (λ (q doc)
#:draw-start (λ (q doc) (add-page doc))
#:draw-end (λ (q doc)
(font-size doc 10)
(text doc (format "~a · ~a at ~a" (hash-ref (quad-attrs q) 'page-number)
(hash-ref (quad-attrs q) 'doc-title)
@ -185,8 +181,8 @@
side-margin
(- (pdf-height doc) bottom-margin)))))
(define q:doc (q #:pre-draw (λ (q doc) (start-doc doc))
#:post-draw (λ (q doc) (end-doc doc))))
(define q:doc (q #:draw-start (λ (q doc) (start-doc doc))
#:draw-end (λ (q doc) (end-doc doc))))
(define (make-blockquote pcs)
(q #:attrs (hasheq 'type "bq")
@ -196,7 +192,7 @@
#:size (delay (pt (pt-x (size (car pcs)))
(for/sum ([pc (in-list pcs)])
(pt-y (size pc)))))
#:pre-draw (λ (q doc)
#:draw-start (λ (q doc)
(save doc)
(match-define (list left top) (quad-origin q))
(match-define (list right bottom) (size q))

@ -65,7 +65,7 @@
#:printable #true))
(define $page (q #:attrs (hasheq 'type "page")
#:offset '(36 36)
#:pre-draw (λ (q doc)
#:draw-start (λ (q doc)
(add-page doc)
(font-size doc 10)
(define str (string-append "page " (number->string page-count)))
@ -75,8 +75,8 @@
(text doc str 10 10 (hasheq 'link "https://practicaltypography.com"))
(restore doc)
(set! page-count (add1 page-count)))))
(define $doc (q #:pre-draw (λ (q doc) (start-doc doc))
#:post-draw (λ (q doc) (end-doc doc))))
(define $doc (q #:draw-start (λ (q doc) (start-doc doc))
#:draw-end (λ (q doc) (end-doc doc))))
(struct $break quad ())
(define page-count 1)
(define (make-break . xs) (q #:type $break

@ -14,21 +14,23 @@
[(? procedure? proc) (proc q signal)]
[val val]))
(define (draw q [surface #f])
((quad-draw q) q surface))
(define (draw q [surface (current-output-port)])
((quad-draw-start q) q surface)
((quad-draw q) q surface)
((quad-draw-end q) q surface))
(define (hashes-equal? h1 h2)
(and (= (length (hash-keys h1)) (length (hash-keys h2)))
(for/and ([(k v) (in-hash h1)])
(and (hash-has-key? h2 k) (equal? (hash-ref h2 k) v)))))
(and (hash-has-key? h2 k) (equal? (hash-ref h2 k) v)))))
(define (quad=? q1 q2 recur?)
(and
;; exclude attrs from initial comparison
(for/and ([getter (in-list (list quad-elems quad-size quad-in quad-out quad-inner
quad-offset quad-origin quad-printable
quad-pre-draw quad-post-draw quad-draw))])
(equal? (getter q1) (getter q2)))
quad-draw-start quad-draw-end quad-draw))])
(equal? (getter q1) (getter q2)))
;; and compare them key-by-key
(hashes-equal? (quad-attrs q1) (quad-attrs q2))))
@ -41,9 +43,9 @@
offset
origin
printable
pre-draw
post-draw
draw)
draw-start
draw
draw-end)
#:property prop:custom-write
(λ (v p w?) (display
(format "<quad ~a~a>"
@ -58,9 +60,7 @@
(define (default-printable q [sig #f]) #f)
(define (default-draw q surface)
((quad-pre-draw q) q surface)
(for-each (λ (qi) ((quad-draw qi) qi surface)) (quad-elems q))
((quad-post-draw q) q surface))
(for-each (λ (qi) (draw qi surface)) (quad-elems q)))
;; why 'nw and 'ne as defaults for in and out points:
;; if size is '(0 0), 'nw and 'ne are the same point,
@ -80,9 +80,9 @@
#:offset [offset '(0 0)]
#:origin [origin '(0 0)]
#:printable [printable default-printable]
#:pre-draw [pre-draw void]
#:post-draw [post-draw void]
#:draw-start [draw-start void]
#:draw [draw default-draw]
#:draw-end [draw-end void]
. args)
(match args
[(list (== #false) elems ...) (make-quad #:elems elems)]
@ -98,13 +98,14 @@
offset
origin
printable
pre-draw
post-draw
draw)]))
draw-start
draw
draw-end)]))
(define q make-quad)
(module+ test
(require racket/port)
(define q1 (q #f #\H #\e #\l #\o))
(define q2 (q #f #\H #\e #\l #\o))
(define q3 (q #f #\H #\e #\l))
@ -112,5 +113,5 @@
(check-true (equal? q1 q2))
(check-false (equal? q1 q3))
(define q4 (struct-copy quad q1
[draw (λ (q surface) "foo")]))
(check-equal? (draw q4) "foo"))
[draw (λ (q surface) (display "foo" surface))]))
(check-equal? (with-output-to-string (λ () (draw q4))) "foo"))

Loading…
Cancel
Save