main
Matthew Butterick 6 years ago
parent 2c6def624b
commit 527682d624

@ -12,7 +12,7 @@
(any/c . -> . real?) (any/c . -> . real?)
(hash-ref! distance-cache (cond (hash-ref! distance-cache (cond
[(quad? q) [(quad? q)
(hash-ref (attrs q) 'id q)] (hash-ref (attrs q) 'id q)]
[(symbol? q) q]) [(symbol? q) q])
(λ () (λ ()
(cond (cond
@ -64,6 +64,8 @@
finish-wrap-proc) finish-wrap-proc)
(define break-val=? (if (symbol? break-val) eq? equal?)) (define break-val=? (if (symbol? break-val) eq? equal?))
(define (cleanup-wraplist xs) (define (cleanup-wraplist xs)
;; combine the segments into a flat list, and drop any trailing breaks
;; (on the idea that breaks should separate things, and there's nothing left to separate)
(dropf-right (append* (reverse xs)) (λ (x) (break-val=? break-val x)))) (dropf-right (append* (reverse xs)) (λ (x) (break-val=? break-val x))))
(define wraps (define wraps
(for/fold ([wraps null] (for/fold ([wraps null]
@ -96,111 +98,90 @@
[((== empty) _) wrap] [((== empty) _) wrap]
[(partial (list (? nonprinting-in-middle-soft-break?) ... rest ...)) (append (or partial null) rest)])) [(partial (list (? nonprinting-in-middle-soft-break?) ... rest ...)) (append (or partial null) rest)]))
(define (finish-wraps wraps wrap-proc soft-break?)
(for/list ([wrap (in-list wraps)])
(match wrap
[(list (? nonprinting-at-end?)) wrap] ; matches break signals
;; pieces will have been accumulated in reverse order
;; thus beginning of list represents the end of the wrap
[(list (? (conjoin soft-break? nonprinting-at-end?)) ... rest ...)
(wrap-proc (reverse rest))])))
(define (break-softs qs (define (break-softs qs
target-size target-size
debug debug
break-val break-val
soft-break? soft-break?
finish-wrap-proc) finish-wrap-proc)
(define start-signal (gensym)) (for/fold ([wraps null] ; list of (list of quads)
;; qs = list of quads [current-wrap null] ; list of quads ending in previous `soft-break?`
;; current-dist = integer [current-partial null] ; list of unbreakable quads
;; current-wrap = list of quads ending in previous `soft-break?` [current-dist #false] ; #false (to indicate start) or integer
;; current-partial = list of unbreakable quads [qs qs] ; list of quads
;; wraps = list of (list of quads) #:result (let ()
(let loop ([wraps null][current-wrap null][current-partial null][current-dist start-signal][qs qs]) (define last-wrap (append-to-wrap #false (append-to-wrap current-partial current-wrap)))
(match qs (finish-wraps (cons last-wrap wraps) finish-wrap-proc soft-break?)))
[(== empty) ([i (in-naturals)]
(when debug (report 'all-quads-wrapped)) #:break (empty? qs))
;; combine the segments into a flat list, and drop any trailing breaks (match-define (cons q other-qs) qs)
;; (on the idea that breaks should separate things, and there's nothing left to separate) (debug-report q 'next-q)
;; wraps alternate with breaks (define at-start? (not current-dist))
(debug-report wraps) (define dist (if (and (quad? q) (printable? q)) (distance q) 0))
;; use false as signal to indicate the end (define would-overflow? (and current-dist (> (+ dist current-dist) target-size)))
(define last-wrap (append-to-wrap #false (append-to-wrap current-partial current-wrap))) (cond
(for/list ([wrap (in-list (cons last-wrap wraps))]) [(and at-start? (soft-break? q) (nonprinting-at-start? q))
(match wrap (debug-report q 'skipping-soft-break-at-beginning)
[(list (? nonprinting-at-end?)) wrap] ; matches break signals (values wraps
;; pieces will have been accumulated in reverse order current-wrap
;; thus beginning of list represents the end of the wrap current-partial
[(list (? (conjoin soft-break? nonprinting-at-end?)) ... rest ...) current-dist
(debug-report (finish-wrap-proc (reverse rest))) other-qs)]
(finish-wrap-proc (reverse rest))]))] [at-start?
[(cons q other-qs) (debug-report 'hard-quad-at-start)
(debug-report q 'next-q) (values wraps
(define at-start? (eq? current-dist start-signal)) current-wrap
(cond (list q)
[at-start? (distance q)
(cond other-qs)]
[(and (soft-break? q) (nonprinting-at-start? q)) [(and would-overflow? (soft-break? q) (nonprinting-at-end? q))
(debug-report q 'skipping-soft-break-at-beginning) (debug-report 'would-overflow-soft-nonprinting)
;; skip it ;; a break is inevitable but we want to wait to finish the wrap until we see a hard quad
(loop wraps ;; but we can move the current-partial into the current-wrap
current-wrap (values wraps
current-partial (append-to-wrap (cons q current-partial) current-wrap)
current-dist null
other-qs)] (+ dist current-dist)
[else ; printing quad other-qs)]
(debug-report 'hard-quad-at-start) [(and would-overflow? (empty? current-wrap))
(loop wraps (debug-report 'would-overflow-hard-without-captured-break)
current-wrap (values (list* (list break-val) current-partial wraps)
(cons q current-partial) null
(distance q) null
other-qs)])] #false
[else qs)]
(define dist (if (and (quad? q) (printable? q)) (distance q) 0)) [would-overflow? ; finish the wrap & reset the line without consuming a quad
(debug-report current-dist) (values (list* (list break-val) current-wrap wraps)
(debug-report dist) null
(define would-overflow? (> (+ dist current-dist) target-size)) current-partial
(cond (apply + (map distance current-partial))
[would-overflow? qs)]
(cond [(soft-break? q) ; printing soft break, like a hyphen
[(and (soft-break? q) (nonprinting-at-end? q)) (debug-report 'would-not-overflow-soft)
(debug-report 'would-overflow-soft-nonprinting) ;; a soft break that fits, so move it on top of the current-wrap with the current-partial
;; a break is inevitable but we want to wait to finish the wrap until we see a hard quad (values wraps
;; but we can move the current-partial into the current-wrap (append-to-wrap (cons q current-partial) current-wrap)
(loop wraps null
(append-to-wrap (cons q current-partial) current-wrap) (+ dist current-dist)
null other-qs)]
(+ dist current-dist) [else
other-qs)] (debug-report 'would-not-overflow)
[else ;; add to partial
(debug-report 'would-overflow-hard) (values wraps
(debug-report (empty? current-wrap)) current-wrap
;; finish the wrap & reset the line without consuming a quad (cons q current-partial)
(if (empty? current-wrap) ; means we have not captured a soft break (+ dist current-dist)
(loop (list* (list break-val) current-partial wraps) other-qs)])))
current-wrap ; which is empty
null
start-signal
qs)
(loop (list* (list break-val) current-wrap wraps)
null
current-partial
(apply + (map distance current-partial))
qs))])]
[else
(cond
[(soft-break? q) ; printing soft break, like a hyphen
(debug-report 'would-not-overflow-soft)
;; a soft break that fits, so move it on top of the current-wrap with the current-partial
(cond
[else
(loop wraps
(append-to-wrap (cons q current-partial) current-wrap)
null
(+ dist current-dist)
other-qs)])]
[else
(debug-report 'would-not-overflow)
;; add to partial
(loop wraps
current-wrap
(cons q current-partial)
(+ dist current-dist)
other-qs)])])
])])))
(define x (q (list 'size (pt 1 1)) #\x)) (define x (q (list 'size (pt 1 1)) #\x))
@ -293,37 +274,37 @@
)) ))
(module+ test (module+ test
(test-case (test-case
"zero width nonbreakers" "zero width nonbreakers"
(check-equal? (linewrap (list sp zwx) 2) (list zwx)) (check-equal? (linewrap (list sp zwx) 2) (list zwx))
(check-equal? (linewrap (list zwx sp) 2) (list zwx)) (check-equal? (linewrap (list zwx sp) 2) (list zwx))
(check-equal? (linewrap (list sp zwx sp) 2) (list zwx)) (check-equal? (linewrap (list sp zwx sp) 2) (list zwx))
(check-equal? (linewrap (list sp sp zwx sp sp) 2) (list zwx)) (check-equal? (linewrap (list sp sp zwx sp sp) 2) (list zwx))
(check-equal? (linewrap (list sp sp zwx sp sp zwx sp) 2) (list zwx sp sp zwx)))) (check-equal? (linewrap (list sp sp zwx sp sp zwx sp) 2) (list zwx sp sp zwx))))
(module+ test (module+ test
(test-case (test-case
"hard breaks" "hard breaks"
(check-equal? (linewrap (list br) 2) (list)) ;; only insert a break if it's between things (check-equal? (linewrap (list br) 2) (list)) ;; only insert a break if it's between things
(check-equal? (linewrap (list a br b) 2) (list a 'lb b)) (check-equal? (linewrap (list a br b) 2) (list a 'lb b))
(check-equal? (linewrap (list a b br) 2) (list a b)) (check-equal? (linewrap (list a b br) 2) (list a b))
(check-equal? (linewrap (list a b br br) 2) (list a b)) (check-equal? (linewrap (list a b br br) 2) (list a b))
(check-equal? (linewrap (list x br x x) 3) (list x 'lb x x)) (check-equal? (linewrap (list x br x x) 3) (list x 'lb x x))
(check-equal? (linewrap (list x x br x) 3) (list x x 'lb x)) (check-equal? (linewrap (list x x br x) 3) (list x x 'lb x))
(check-equal? (linewrap (list x x x x) 3) (list x x x 'lb x)) (check-equal? (linewrap (list x x x x) 3) (list x x x 'lb x))
(check-equal? (linewrap (list x x x sp x x) 2) (list x x 'lb x 'lb x x)) (check-equal? (linewrap (list x x x sp x x) 2) (list x x 'lb x 'lb x x))
(check-equal? (linewrap (list x x x sp x x) 3) (list x x x 'lb x x)))) (check-equal? (linewrap (list x x x sp x x) 3) (list x x x 'lb x x))))
(module+ test (module+ test
(test-case (test-case
"hard breaks and spurious spaces" "hard breaks and spurious spaces"
(check-equal? (linewrap (list a sp sp sp br b) 2) (list a 'lb b)) (check-equal? (linewrap (list a sp sp sp br b) 2) (list a 'lb b))
(check-equal? (linewrap (list x sp br sp sp x x sp) 3) (list x 'lb x x)) (check-equal? (linewrap (list x sp br sp sp x x sp) 3) (list x 'lb x x))
(check-equal? (linewrap (list sp sp x x sp sp br sp sp sp x) 3) (list x x 'lb x)) (check-equal? (linewrap (list sp sp x x sp sp br sp sp sp x) 3) (list x x 'lb x))
(check-equal? (linewrap (list a sp b sp sp br sp c) 3) (list a sp b 'lb c)) (check-equal? (linewrap (list a sp b sp sp br sp c) 3) (list a sp b 'lb c))
(check-equal? (linewrap (list x x x x) 3) (list x x x 'lb x)) (check-equal? (linewrap (list x x x x) 3) (list x x x 'lb x))
(check-equal? (linewrap (list x x x sp x x) 2) (list x x 'lb x 'lb x x)) (check-equal? (linewrap (list x x x sp x x) 2) (list x x 'lb x 'lb x x))
(check-equal? (linewrap (list x x x sp x x) 3) (list x x x 'lb x x)))) (check-equal? (linewrap (list x x x sp x x) 3) (list x x x 'lb x x))))
(define (visual-wrap str int [debug #f]) (define (visual-wrap str int [debug #f])
(apply string (for/list ([b (in-list (linewrap (for/list ([atom (atomize str)]) (apply string (for/list ([b (in-list (linewrap (for/list ([atom (atomize str)])
@ -334,24 +315,24 @@
[else #\|])))) [else #\|]))))
(module+ test (module+ test
(test-case (test-case
"visual breaks" "visual breaks"
(check-equal? (visual-wrap "My dog has fleas" 1) "M|y|d|o|g|h|a|s|f|l|e|a|s") (check-equal? (visual-wrap "My dog has fleas" 1) "M|y|d|o|g|h|a|s|f|l|e|a|s")
(check-equal? (visual-wrap "My dog has fleas" 2) "My|do|g|ha|s|fl|ea|s") (check-equal? (visual-wrap "My dog has fleas" 2) "My|do|g|ha|s|fl|ea|s")
(check-equal? (visual-wrap "My dog has fleas" 3) "My|dog|has|fle|as") (check-equal? (visual-wrap "My dog has fleas" 3) "My|dog|has|fle|as")
(check-equal? (visual-wrap "My dog has fleas" 4) "My|dog|has|flea|s") (check-equal? (visual-wrap "My dog has fleas" 4) "My|dog|has|flea|s")
(check-equal? (visual-wrap "My dog has fleas" 5) "My|dog|has|fleas") (check-equal? (visual-wrap "My dog has fleas" 5) "My|dog|has|fleas")
(check-equal? (visual-wrap "My dog has fleas" 6) "My dog|has|fleas") (check-equal? (visual-wrap "My dog has fleas" 6) "My dog|has|fleas")
(check-equal? (visual-wrap "My dog has fleas" 7) "My dog|has|fleas") (check-equal? (visual-wrap "My dog has fleas" 7) "My dog|has|fleas")
(check-equal? (visual-wrap "My dog has fleas" 8) "My dog|has|fleas") (check-equal? (visual-wrap "My dog has fleas" 8) "My dog|has|fleas")
(check-equal? (visual-wrap "My dog has fleas" 9) "My dog|has fleas") (check-equal? (visual-wrap "My dog has fleas" 9) "My dog|has fleas")
(check-equal? (visual-wrap "My dog has fleas" 10) "My dog has|fleas") (check-equal? (visual-wrap "My dog has fleas" 10) "My dog has|fleas")
(check-equal? (visual-wrap "My dog has fleas" 11) "My dog has|fleas") (check-equal? (visual-wrap "My dog has fleas" 11) "My dog has|fleas")
(check-equal? (visual-wrap "My dog has fleas" 12) "My dog has|fleas") (check-equal? (visual-wrap "My dog has fleas" 12) "My dog has|fleas")
(check-equal? (visual-wrap "My dog has fleas" 13) "My dog has|fleas") (check-equal? (visual-wrap "My dog has fleas" 13) "My dog has|fleas")
(check-equal? (visual-wrap "My dog has fleas" 14) "My dog has|fleas") (check-equal? (visual-wrap "My dog has fleas" 14) "My dog has|fleas")
(check-equal? (visual-wrap "My dog has fleas" 15) "My dog has|fleas") (check-equal? (visual-wrap "My dog has fleas" 15) "My dog has|fleas")
(check-equal? (visual-wrap "My dog has fleas" 16) "My dog has fleas"))) (check-equal? (visual-wrap "My dog has fleas" 16) "My dog has fleas")))
(define (pagewrap xs size [debug #f]) (define (pagewrap xs size [debug #f])
@ -363,34 +344,34 @@
(define pbr (q '(size #f) #\page)) (define pbr (q '(size #f) #\page))
(module+ test (module+ test
(test-case (test-case
"soft page breaks" "soft page breaks"
(check-equal? (pagewrap null 2) '(pb)) (check-equal? (pagewrap null 2) '(pb))
(check-equal? (pagewrap (list x) 2) (list 'pb x)) (check-equal? (pagewrap (list x) 2) (list 'pb x))
(check-equal? (pagewrap (list x x) 2) (list 'pb x x)) (check-equal? (pagewrap (list x x) 2) (list 'pb x x))
(check-equal? (pagewrap (list x x x) 1) (list 'pb x 'pb x 'pb x)) (check-equal? (pagewrap (list x x x) 1) (list 'pb x 'pb x 'pb x))
(check-equal? (pagewrap (list x x x) 2) (list 'pb x x 'pb x)) (check-equal? (pagewrap (list x x x) 2) (list 'pb x x 'pb x))
(check-equal? (pagewrap (list x x x) 3) (list 'pb x x x)) (check-equal? (pagewrap (list x x x) 3) (list 'pb x x x))
(check-equal? (pagewrap (list x x x) 4) (list 'pb x x x)) (check-equal? (pagewrap (list x x x) 4) (list 'pb x x x))
(check-equal? (pagewrap (list x 'lb x x) 2) (list 'pb x 'pb x x)))) (check-equal? (pagewrap (list x 'lb x x) 2) (list 'pb x 'pb x x))))
(module+ test (module+ test
(test-case (test-case
"hard page breaks" "hard page breaks"
(check-equal? (pagewrap (list x pbr x x) 2) (list 'pb x 'pb x x)) (check-equal? (pagewrap (list x pbr x x) 2) (list 'pb x 'pb x x))
(check-equal? (pagewrap (list x pbr x x) 1) (list 'pb x 'pb x 'pb x)) (check-equal? (pagewrap (list x pbr x x) 1) (list 'pb x 'pb x 'pb x))
(check-equal? (pagewrap (list x pbr pbr x x) 1) (list 'pb x 'pb 'pb x 'pb x)) (check-equal? (pagewrap (list x pbr pbr x x) 1) (list 'pb x 'pb 'pb x 'pb x))
(check-equal? (pagewrap (list x pbr pbr x x) 2) (list 'pb x 'pb 'pb x x)) (check-equal? (pagewrap (list x pbr pbr x x) 2) (list 'pb x 'pb 'pb x x))
(check-equal? (pagewrap (list 'lb x 'lb 'lb pbr 'lb x x 'lb) 2) (list 'pb x 'pb x x)))) (check-equal? (pagewrap (list 'lb x 'lb 'lb pbr 'lb x x 'lb) 2) (list 'pb x 'pb x x))))
(module+ test (module+ test
(test-case (test-case
"composed line breaks and page breaks" "composed line breaks and page breaks"
(check-equal? (pagewrap (linewrap null 1) 2) '(pb) ) (check-equal? (pagewrap (linewrap null 1) 2) '(pb) )
(check-equal? (pagewrap (linewrap (list x) 1) 2) (list 'pb x)) (check-equal? (pagewrap (linewrap (list x) 1) 2) (list 'pb x))
(check-equal? (pagewrap (linewrap (list x x x) 1) 2) (list 'pb x 'lb x 'pb x)) (check-equal? (pagewrap (linewrap (list x x x) 1) 2) (list 'pb x 'lb x 'pb x))
(check-equal? (pagewrap (linewrap (list x x x) 2) 2) (list 'pb x x 'pb x)) (check-equal? (pagewrap (linewrap (list x x x) 2) 2) (list 'pb x x 'pb x))
(check-equal? (pagewrap (linewrap (list x x x) 2) 1) (list 'pb x 'pb x 'pb x)))) (check-equal? (pagewrap (linewrap (list x x x) 2) 1) (list 'pb x 'pb x 'pb x))))
(struct $slug $quad () #:transparent) (struct $slug $quad () #:transparent)
(define (slug . xs) ($slug #f xs)) (define (slug . xs) ($slug #f xs))
@ -402,12 +383,12 @@
#:finish-wrap-proc (λ (pcs) (list ($slug #f pcs))))) #:finish-wrap-proc (λ (pcs) (list ($slug #f pcs)))))
(module+ test (module+ test
(test-case (test-case
"hard breaks and spurious spaces with slugs" "hard breaks and spurious spaces with slugs"
(check-equal? (linewrap2 (list a sp sp sp br b) 2) (list (slug a) 'lb (slug b))) (check-equal? (linewrap2 (list a sp sp sp br b) 2) (list (slug a) 'lb (slug b)))
(check-equal? (linewrap2 (list x sp br sp sp x x sp) 3) (list (slug x) 'lb (slug x x))) (check-equal? (linewrap2 (list x sp br sp sp x x sp) 3) (list (slug x) 'lb (slug x x)))
(check-equal? (linewrap2 (list sp sp x x sp sp br sp sp sp x) 3) (list (slug x x) 'lb (slug x))) (check-equal? (linewrap2 (list sp sp x x sp sp br sp sp sp x) 3) (list (slug x x) 'lb (slug x)))
(check-equal? (linewrap2 (list a sp b sp sp br sp c) 3) (list (slug a sp b) 'lb (slug c))) (check-equal? (linewrap2 (list a sp b sp sp br sp c) 3) (list (slug a sp b) 'lb (slug c)))
(check-equal? (linewrap2 (list x x x x) 3) (list (slug x x x) 'lb (slug x))) (check-equal? (linewrap2 (list x x x x) 3) (list (slug x x x) 'lb (slug x)))
(check-equal? (linewrap2 (list x x x sp x x) 2) (list (slug x x) 'lb (slug x) 'lb (slug x x))) (check-equal? (linewrap2 (list x x x sp x x) 2) (list (slug x x) 'lb (slug x) 'lb (slug x x)))
(check-equal? (linewrap2 (list x x x sp x x) 3) (list (slug x x x) 'lb (slug x x))))) (check-equal? (linewrap2 (list x x x sp x x) 3) (list (slug x x x) 'lb (slug x x)))))

Loading…
Cancel
Save