+ #:export (post-process-pages optimal-page-breaks make-page-from-systems
+ page-breaking-wrapper
+ stretchable-line? ; delete me
+ ;; utilities for writing custom page breaking functions
+ line-height line-next-space line-next-padding
+ line-minimum-distance line-ideal-distance
+ first-line-position
+ line-ideal-relative-position line-minimum-relative-position
+ page-maximum-space-to-fill page-maximum-space-left space-systems))
+
+; this is for 2-pass spacing. Delete me.
+(define (stretchable-line? line)
+ "Say whether a system can be stretched."
+ (not (or (ly:prob-property? line 'is-title)
+ (let ((system-extent (paper-system-staff-extents line)))
+ (= (interval-start system-extent)
+ (interval-end system-extent))))))
+
+(define (stretch-and-draw-page paper-book systems page-number ragged
+ is-last-bookpart is-bookpart-last-page)
+ (define (max-stretch sys)
+ (if (ly:grob? sys)
+ (ly:grob-property sys 'max-stretch)
+ 0.0))
+
+ (define (stretchable? sys)
+ (and (ly:grob? sys)
+ (> (max-stretch sys) 0.0)))
+
+ (define (height-estimate sys)
+ (interval-length
+ (if (ly:grob? sys)
+ (ly:grob-property sys 'pure-Y-extent)
+ (paper-system-extent sys Y))))
+
+ (define (print-system sys)
+ (if (ly:grob? sys)
+ (ly:system-print sys)
+ sys))
+
+ (define (set-line-stretch! sorted-lines rest-height space-left)
+ (if (not (null? sorted-lines))
+ (let* ((line (first sorted-lines))
+ (height (height-estimate line))
+ (stretch (min (max-stretch line)
+ (if (positive? rest-height)
+ (/ (* height space-left) rest-height)
+ 0.0))))
+ (if (stretchable? line)
+ (ly:system-stretch line stretch))
+ (set-line-stretch! (cdr sorted-lines)
+ (if (stretchable? line)
+ (- rest-height height)
+ rest-height)
+ (- space-left stretch)))))
+
+ (define (total-padding systems)
+ (let ((layout (ly:paper-book-paper paper-book)))
+ (if (or (null? systems)
+ (null? (cdr systems)))
+ 0.0
+ (+ (line-next-padding (car systems) (cadr systems) layout)
+ (total-padding (cdr systems))))))
+
+ (let* ((page (make-page paper-book
+ 'page-number page-number
+ 'is-last-bookpart is-last-bookpart
+ 'is-bookpart-last-page is-bookpart-last-page))
+ (paper (ly:paper-book-paper paper-book))
+ (height (page-printable-height page))
+ ; there is a certain amount of impreciseness going on here:
+ ; the system heights are estimated, we aren't using skyline distances
+ ; yet, etc. If we overstretch because of underestimation, the result
+ ; is very bad. So we stick in some extra space, just to be sure.
+ (buffer (/ height 10.0))
+ (total-system-height (+ (apply + (map height-estimate systems))
+ (total-padding systems)))
+ (height-left (- height total-system-height buffer)))
+
+ (if (and
+ (not ragged)
+ (> height-left 0))
+ (set-line-stretch! (sort systems
+ (lambda (s1 s2)
+ (< (height-estimate s1)
+ (height-estimate s2))))
+ (apply + (map height-estimate
+ (filter stretchable? systems)))
+ height-left))
+
+ (let ((lines (map print-system systems)))
+ (page-set-property! page 'lines lines)
+ (page-set-property!
+ page 'configuration
+ (if (null? lines)
+ (list)
+ (let* ((paper (ly:paper-book-paper paper-book))
+ (max-space-to-fill (page-maximum-space-to-fill page lines paper))
+ (space-to-fill (if (ly:output-def-lookup
+ paper 'page-limit-inter-system-space #f)
+ (min max-space-to-fill
+ (* (ly:output-def-lookup
+ paper 'page-limit-inter-system-space-factor 1.4)
+ (- max-space-to-fill
+ (or (page-ideal-space-left page) 0))))
+ max-space-to-fill))
+ (spacing (space-systems space-to-fill lines ragged paper #f)))
+ (if (and (> (length lines) 1)
+ (or (not (car spacing)) (inf? (car spacing))))
+ (begin
+ (ly:warning (_ "Can't fit systems on page -- ignoring between-system-padding"))
+ (cdr (space-systems space-to-fill lines ragged paper #t)))
+ (cdr spacing)))))
+ page)))
+
+(define (page-breaking-wrapper paper-book)
+ "Compute line and page breaks by calling the page-breaking paper variable,
+ then performs the post process function using the page-post-process paper
+ variable. Finally, return the pages."
+ (let* ((paper (ly:paper-book-paper paper-book))
+ (pages ((ly:output-def-lookup paper 'page-breaking) paper-book)))
+ ((ly:output-def-lookup paper 'page-post-process) paper pages)
+ pages))