section repeaters

main
Matthew Butterick 4 years ago
parent e723a5a6cd
commit 7ddcf00c78

@ -179,6 +179,8 @@ Naming guidelines
footer-display footer-display
footer-text footer-text
page-repeat
fn-ref fn-ref
fn-text fn-text
fn-text-start)) fn-text-start))

@ -153,22 +153,32 @@
(module+ test (module+ test
(check-equal? (quad-ref (convert-break-quad (qexpr->quad '(q ((break "page") (foo "bar"))))) 'foo) "bar")) (check-equal? (quad-ref (convert-break-quad (qexpr->quad '(q ((break "page") (foo "bar"))))) 'foo) "bar"))
(define-quad draw-quad quad)
(define q:draw (q #:type draw-quad
#:from 'bo
#:to 'bi
#:id 'str
#:printable q:string-printable?
#:draw q:string-draw
#:draw-end q:string-draw-end))
(define (convert-draw-quad q) (define (convert-draw-quad q)
(quad-update! q (struct-copy draw-quad q:draw
[draw (λ (q doc) [attrs #:parent quad (quad-attrs q)]
(save doc) [draw #:parent quad (λ (q doc)
(match (quad-ref q :draw) (save doc)
["line" (match (quad-ref q :draw)
(move-to doc (quad-ref q :x1) (quad-ref q :y1)) ["line"
(line-to doc (quad-ref q :x2) (quad-ref q :y2)) (move-to doc (quad-ref q :x1) (quad-ref q :y1))
(stroke doc "black")] (line-to doc (quad-ref q :x2) (quad-ref q :y2))
["text" (move-to doc 0 0) (stroke doc "black")]
(q:string-draw q doc ["text" (move-to doc 0 0)
#:origin (pt (quad-ref q :x 0) (quad-ref q :y 0)) (q:string-draw q doc
#:text (quad-ref q :text))] #:origin (pt (quad-ref q :x 0) (quad-ref q :y 0))
[_ (void)]) #:text (quad-ref q :text))]
(restore doc))] [_ (void)])
[size (pt 0 0)])) (restore doc))]
[size #:parent quad (pt 0 0)]))
(define (convert-image-quad q) (define (convert-image-quad q)
(define path-string (quad-ref q :image-file)) (define path-string (quad-ref q :image-file))

@ -171,7 +171,7 @@
[qs (time-log hyphenate (apply append (map handle-hyphenate qs)))] [qs (time-log hyphenate (apply append (map handle-hyphenate qs)))]
[qs (map generic->typed-quad qs)] [qs (map generic->typed-quad qs)]
[qs (drop-leading-breaks qs)] [qs (drop-leading-breaks qs)]
[qs (extract-defined-quads qs)] #;[qs (extract-defined-quads qs)]
[qs (insert-first-line-indents qs)]) [qs (insert-first-line-indents qs)])
qs)) qs))
@ -263,10 +263,19 @@
[size (pt line-wrap-size printable-height)])) [size (pt line-wrap-size printable-height)]))
(time-log page-wrap (page-wrap column-qs printable-width page-quad-prototype))) (time-log page-wrap (page-wrap column-qs printable-width page-quad-prototype)))
(define (make-sections qs) (define (apply-doc-repeaters secs repeaters)
secs)
(define (make-sections all-qs)
(define-values (doc-repeaters nonrepeating-qs)
(partition (λ (q) (member (quad-ref q :page-repeat) '("all" "left" "right" "first" "rest"))) all-qs))
(for/fold ([sections-acc null] (for/fold ([sections-acc null]
#:result (reverse sections-acc)) #:result (apply-doc-repeaters (reverse sections-acc) doc-repeaters))
([qs (in-list (filter-split qs section-break-quad?))]) ([all-section-qs (in-list (filter-split nonrepeating-qs section-break-quad?))])
(define-values (section-repeaters qs)
(partition (λ (q) (member (quad-ref q :page-repeat) '("section" "section all" "section left" "section right" "section first" "section rest"))) all-section-qs))
;; section properties ;; section properties
(match-define (list page-width page-height) (parse-page-size (and (pair? qs) (car qs)))) (match-define (list page-width page-height) (parse-page-size (and (pair? qs) (car qs))))
(match-define (list left-margin top-margin right-margin bottom-margin) (match-define (list left-margin top-margin right-margin bottom-margin)
@ -292,13 +301,36 @@
(when insert-blank-page? (when insert-blank-page?
(section-pages-used (add1 (section-pages-used)))) (section-pages-used (add1 (section-pages-used))))
(define section-pages (make-pages column-qs (define section-pages-without-repeaters (make-pages column-qs
left-margin left-margin
top-margin top-margin
gutter-margin gutter-margin
line-wrap-size line-wrap-size
printable-width printable-width
printable-height)) printable-height))
;; put in quads that repeat within the section
(define section-pages
(for/list ([page (in-list section-pages-without-repeaters)]
[page-num (in-naturals 1)]
[page-side (in-cycle ((if (eq? section-starting-side 'left) values reverse) '(left right)))])
;; the first page of the section is 1,
;; so all the odd pages are the same side as the starting side
;; and even pages are the opposite side
(define section-repeaters-for-this-page
(for/list ([repeater (in-list section-repeaters)]
#:when (let* ([val (quad-ref repeater :page-repeat)]
[sym (string->symbol (string-trim val #px"section\\s"))])
(memq sym (list*
(if (= page-num 1) 'first 'rest)
page-side
'(section all)))))
repeater))
(cond
[(null? section-repeaters-for-this-page) page]
[else
(quad-copy page
[elems (append section-repeaters-for-this-page (quad-elems page))])])))
(begin0 (begin0
(cond (cond

Loading…
Cancel
Save