wrap → break

main
Matthew Butterick 6 years ago
parent c18e2c2b19
commit cbe6fd4d5b

@ -15,15 +15,15 @@
[else 0]))
(define+provide/contract (wrap xs
[target-size (current-wrap-distance)]
[debug #f]
#:break-val [break-val 'break]
#:break-before? [break-before? #f]
#:break-after? [break-after? #f]
#:mandatory-break-proc [mandatory-break? (const #f)]
#:optional-break-proc [optional-break? (const #f)]
#:finish-wrap-proc [finish-wrap-proc values])
(define+provide/contract (break xs
[target-size (current-wrap-distance)]
[debug #f]
#:break-val [break-val 'break]
#:break-before? [break-before? #f]
#:break-after? [break-after? #f]
#:mandatory-break-proc [mandatory-break? (const #f)]
#:optional-break-proc [optional-break? (const #f)]
#:finish-wrap-proc [finish-wrap-proc values])
((any/c) (real? any/c
#:break-val any/c
#:break-before? boolean?
@ -31,27 +31,27 @@
#:mandatory-break-proc procedure?
#:optional-break-proc procedure?
#:finish-wrap-proc procedure?) . ->* . (listof any/c))
(wrap-private xs
target-size
debug
break-val
break-before?
break-after?
mandatory-break?
optional-break?
finish-wrap-proc))
(break-private xs
target-size
debug
break-val
break-before?
break-after?
mandatory-break?
optional-break?
finish-wrap-proc))
;; the mandatory breaks are used to divide the wrap territory into smaller chunks
;; that can be cached, parallelized, etc.
(define (wrap-private xs
target-size
debug
break-val
break-before?
break-after?
mandatory-break?
optional-break?
finish-wrap-proc)
(define (break-private xs
target-size
debug
break-val
break-before?
break-after?
mandatory-break?
optional-break?
finish-wrap-proc)
(define break-val-equal? (if (symbol? break-val) eq? equal?))
(define (cleanup-wraplist xs) (dropf-right (append* (reverse xs)) (λ (x) (break-val-equal? break-val x))))
(define wraps
@ -66,23 +66,23 @@
(values (cons (future (λ () (list break-val))) wraps) (cdr xs))]
[else
(define-values (head tail) (splitf-at xs (λ (x) (not (mandatory-break? x)))))
(values (cons (future (λ () (cleanup-wraplist (wrap-optionals head
target-size
debug
break-val
optional-break?
finish-wrap-proc)))) wraps) tail)])))
(values (cons (future (λ () (cleanup-wraplist (break-optionals head
target-size
debug
break-val
optional-break?
finish-wrap-proc)))) wraps) tail)])))
(append (if break-before? (list break-val) empty) (cleanup-wraplist wraps) (if break-after? (list break-val) empty)))
(define (nonprinting-at-start? x) (if (quad? x) (not (printable? x 'start)) #t))
(define (nonprinting-at-end? x) (if (quad? x) (not (printable? x 'end)) #t))
(define (wrap-optionals xs
target-size
debug
break-val
optional-break?
finish-wrap-proc)
(define (break-optionals xs
target-size
debug
break-val
optional-break?
finish-wrap-proc)
(define start-signal (gensym))
(define last-optional-break-k #f)
(define (capture-optional-break-k!)
@ -188,10 +188,10 @@
(define optional-break? (λ (q) (and (quad? q) (memv (car (elems q)) '(#\space #\-)))))
(define (linewrap xs size [debug #f])
(wrap xs size debug
#:break-val 'lb
#:mandatory-break-proc (λ (q) (and (quad? q) (memv (car (elems q)) '(#\newline))))
#:optional-break-proc optional-break?))
(break xs size debug
#:break-val 'lb
#:mandatory-break-proc (λ (q) (and (quad? q) (memv (car (elems q)) '(#\newline))))
#:optional-break-proc optional-break?))
(module+ test
@ -312,11 +312,11 @@
(define (pagewrap xs size [debug #f])
(wrap xs size debug
#:break-val 'pb
#:break-before? #t
#:mandatory-break-proc (λ (x) (and (quad? x) (memv (car (elems x)) '(#\page))))
#:optional-break-proc (λ (x) (eq? x 'lb))))
(break xs size debug
#:break-val 'pb
#:break-before? #t
#:mandatory-break-proc (λ (x) (and (quad? x) (memv (car (elems x)) '(#\page))))
#:optional-break-proc (λ (x) (eq? x 'lb))))
(define pbr (q '(size #f) #\page))
(module+ test
@ -350,11 +350,11 @@
(struct $slug $quad () #:transparent)
(define (slug . xs) ($slug #f xs))
(define (linewrap2 xs size [debug #f])
(wrap xs size debug
#:break-val 'lb
#:mandatory-break-proc (λ (q) (and (quad? q) (memv (car (elems q)) '(#\newline))))
#:optional-break-proc optional-break?
#:finish-wrap-proc (λ (pcs) (list ($slug #f pcs)))))
(break xs size debug
#:break-val 'lb
#:mandatory-break-proc (λ (q) (and (quad? q) (memv (car (elems q)) '(#\newline))))
#:optional-break-proc optional-break?
#:finish-wrap-proc (λ (pcs) (list ($slug #f pcs)))))
(module+ test
(test-case

@ -61,10 +61,11 @@
(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)
|#
(module+ test
(define x ($quad (hasheq 'entrance 0

@ -1,5 +1,5 @@
#lang debug br/quicklang
(require racket/promise racket/list sugar/list sugar/debug "quad.rkt" "atomize.rkt" "wrap.rkt" "qexpr.rkt" "generic.rkt" "position.rkt")
(require racket/promise racket/list sugar/list sugar/debug "quad.rkt" "atomize.rkt" "break.rkt" "qexpr.rkt" "generic.rkt" "position.rkt")
(require pitfall/document)
(provide (rename-out [mb #%module-begin]) (except-out (all-from-out br/quicklang) #%module-begin))
@ -34,13 +34,13 @@
(struct $doc $quad () #:transparent)
(struct $break $quad () #:transparent)
(define page-count 1)
(define (break . xs) ($break (hasheq 'printable? #f 'size '(0 0)) xs))
(define (make-break . xs) ($break (hasheq 'printable? #f 'size '(0 0)) xs))
(define line-height 16)
(define consolidate-into-runs? #t)
(define (line-wrap xs size [debug #f])
(wrap xs size debug
#:break-val (break #\newline)
(break xs size debug
#:break-val (make-break #\newline)
#:optional-break-proc optional-break?
#:finish-wrap-proc (λ (pcs) (list ($line (hasheq 'size (list +inf.0 line-height) 'out 'sw)
;; consolidate chars into a single run (naively)
@ -69,7 +69,7 @@
(as-link doc str "https://beautifulracket.com" 10 10)
(set! page-count (add1 page-count)))) '(#\page)))
(define (page-wrap xs size [debug #f])
(wrap xs size debug
(break xs size debug
#:break-before? #t
#:break-val pb
#:optional-break-proc $break?

Loading…
Cancel
Save