main
Matthew Butterick 6 years ago
parent 59ffecf689
commit 1d09780840

@ -19,9 +19,15 @@
(define (break xs (define (break xs
[target-size (current-wrap-distance)] [target-size (current-wrap-distance)]
[debug #f] [debug #f]
#:hard-break-proc [hard-break? (λ xs #f)] #:hard-break-proc [hard-break? (λ (x) #f)]
#:soft-break-proc [soft-break? (λ xs #f)] #:soft-break-proc [soft-break? (λ (x) #f)]
#:finish-wrap-proc [finish-wrap-proc list]) #:finish-wrap-proc [finish-wrap-proc list])
#;((listof quad?)
(real?
any/c
#:hard-break-proc (any/c . -> . any/c)
#:soft-break-proc (any/c . -> . any/c)
#:finish-wrap-proc ((listof any/c) . -> . (listof any/c))) . ->* . (listof any/c))
(break-hards xs (break-hards xs
target-size target-size
debug debug
@ -37,12 +43,10 @@
hard-break? hard-break?
soft-break? soft-break?
finish-wrap-proc) finish-wrap-proc)
(define (cleanup-wraplist xs) (define (cleanup-wraplist xs) (append* (reverse xs)))
(append* (reverse xs)))
(define wraps
(let loop ([wraps null][xs xs]) (let loop ([wraps null][xs xs])
(match xs (match xs
[(? null?) wraps] [(? null?) (cleanup-wraplist wraps)]
[(cons (? hard-break?) rest) [(cons (? hard-break?) rest)
(debug-report x 'hard-break) (debug-report x 'hard-break)
(loop wraps rest)] (loop wraps rest)]
@ -52,11 +56,13 @@
debug debug
soft-break? soft-break?
finish-wrap-proc)) wraps) tail)]))) finish-wrap-proc)) wraps) tail)])))
(cleanup-wraplist wraps))
(define (nonprinting-at-start? x) (if (quad? x) (not (printable? x 'start)) #t)) (define (nonprinting-at-start? x)
(define (nonprinting-at-end? x) (if (quad? x) (not (printable? x 'end)) #t)) (not (printable? x 'start)))
(define (nonprinting-in-middle-soft-break? x) (and (quad? x) (not (printable? x)) (soft-break? x))) (define (nonprinting-at-end? x)
(not (printable? x 'end)))
(define (nonprinting-in-middle-soft-break? x)
(and (not (printable? x)) (soft-break? x)))
(define (wrap-append partial wrap) (define (wrap-append partial wrap)
(match/values (match/values
@ -64,77 +70,74 @@
[((== 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 (break-softs qs (define (break-softs qs
target-size target-size
debug debug
soft-break? soft-break?
finish-wrap-proc) finish-wrap-proc)
(for/fold ([wraps null] ; list of (list of quads) (let loop ([wraps null] ; list of (list of quads)
[next-wrap-head null] ; list of quads ending in previous `soft-break?` [next-wrap-head null] ; list of quads ending in previous `soft-break?`
[next-wrap-tail null] ; list of unbreakable quads [next-wrap-tail null] ; list of unbreakable quads
[current-dist #false] ; #false (to indicate start) or integer [current-dist #false] ; #false (to indicate start) or integer
[qs qs] ; list of quads [qs qs]) ; list of quads
#:result (let () (match qs
(define last-wrap (wrap-append #false (wrap-append next-wrap-tail next-wrap-head))) [(== empty) (define last-wrap (wrap-append #false (wrap-append next-wrap-tail next-wrap-head)))
(define finished-wraps
(for/list ([wrap (in-list (cons last-wrap wraps))]) (for/list ([wrap (in-list (cons last-wrap wraps))])
(match wrap
[(list (? nonprinting-at-end?)) wrap] ; matches break signals
;; pieces will have been accumulated in reverse order ;; pieces will have been accumulated in reverse order
;; thus beginning of list represents the end of the wrap ;; thus beginning of list represents the end of the wrap
(match wrap
[(list (and (? soft-break?) (? nonprinting-at-end?)) ... rest ...) [(list (and (? soft-break?) (? nonprinting-at-end?)) ... rest ...)
(finish-wrap-proc (reverse rest))]))) (finish-wrap-proc (reverse rest))]))]
finished-wraps)) [(cons q other-qs)
([i (in-naturals)]
#:break (empty? qs))
(match-define (cons q other-qs) qs)
(debug-report q 'next-q) (debug-report q 'next-q)
(debug-report (quad-elems q) 'next-q-elems) (debug-report (quad-elems q) 'next-q-elems)
(define at-start? (not current-dist)) (define at-start? (not current-dist))
(define dist (if (and (quad? q) (printable? q)) (distance q) 0)) (define dist (if (and (quad? q) (printable? q)) (distance q) 0))
(define would-overflow? (and current-dist (> (+ dist current-dist) target-size))) (define would-overflow? (and current-dist (> (+ dist current-dist) target-size)))
(cond (cond
[(and at-start? (soft-break? q) (nonprinting-at-start? q)) [at-start?
(cond
[(and (soft-break? q) (nonprinting-at-start? q))
(debug-report q 'skipping-soft-break-at-beginning) (debug-report q 'skipping-soft-break-at-beginning)
(values wraps (loop wraps
next-wrap-head next-wrap-head
next-wrap-tail next-wrap-tail
current-dist current-dist
other-qs)] other-qs)]
[at-start? [else (debug-report 'hard-quad-at-start)
(debug-report 'hard-quad-at-start) (loop wraps
(values wraps
next-wrap-head next-wrap-head
(list q) (list q)
(distance q) (distance q)
other-qs)] other-qs)])]
[(and would-overflow? (soft-break? q) (nonprinting-at-end? q)) [would-overflow?
(cond
[(and (soft-break? q) (nonprinting-at-end? q))
(debug-report 'would-overflow-soft-nonprinting) (debug-report 'would-overflow-soft-nonprinting)
;; a break is inevitable but we want to wait to finish the wrap until we see a hard quad ;; a break is inevitable but we want to wait to finish the wrap until we see a hard quad
;; but we can move the current-partial into the current-wrap ;; but we can move the current-partial into the current-wrap
(values wraps (loop wraps
(wrap-append (cons q next-wrap-tail) next-wrap-head) (wrap-append (cons q next-wrap-tail) next-wrap-head)
null null
(+ dist current-dist) (+ dist current-dist)
other-qs)] other-qs)]
[(and would-overflow? (empty? next-wrap-head)) [(empty? next-wrap-head)
(debug-report 'would-overflow-hard-without-captured-break) (debug-report 'would-overflow-hard-without-captured-break)
(values (cons next-wrap-tail wraps) (loop (cons next-wrap-tail wraps)
null null
null null
#false #false
qs)] qs)]
[would-overflow? ; finish the wrap & reset the line without consuming a quad [else ; finish the wrap & reset the line without consuming a quad
(values (cons next-wrap-head wraps) (loop (cons next-wrap-head wraps)
null null
next-wrap-tail next-wrap-tail
(apply + (map distance next-wrap-tail)) (apply + (map distance next-wrap-tail))
qs)] qs)])]
[(soft-break? q) ; printing soft break, like a hyphen [(soft-break? q) ; printing soft break, like a hyphen
(debug-report 'would-not-overflow-soft) (debug-report 'would-not-overflow-soft)
;; a soft break that fits, so move it on top of the next-wrap-head with the next-wrap-tail ;; a soft break that fits, so move it on top of the next-wrap-head with the next-wrap-tail
(values wraps (loop wraps
(wrap-append (cons q next-wrap-tail) next-wrap-head) (wrap-append (cons q next-wrap-tail) next-wrap-head)
null null
(+ dist current-dist) (+ dist current-dist)
@ -142,11 +145,11 @@
[else [else
(debug-report 'would-not-overflow) (debug-report 'would-not-overflow)
;; add to partial ;; add to partial
(values wraps (loop wraps
next-wrap-head next-wrap-head
(cons q next-wrap-tail) (cons q next-wrap-tail)
(+ dist current-dist) (+ dist current-dist)
other-qs)]))) other-qs)])])))
(define q-zero (q #:size (pt 0 0))) (define q-zero (q #:size (pt 0 0)))

Loading…
Cancel
Save