+ (line-minimum-distance prev-line line layout ignore-padding)))
+
+(define (line-minimum-position-on-page line prev-line prev-position page)
+ "If `line' fits on `page' after `prev-line', which position on page is
+ `prev-position', then return the line's postion on page, otherwise #f.
+ `prev-line' can be #f, meaning that `line' is the first line."
+ (let* ((layout (ly:paper-book-paper (page-property page 'paper-book)))
+ (position (+ (line-minimum-relative-position line prev-line layout #f)
+ (if prev-line prev-position 0.0)))
+ (bottom-position (- position
+ (interval-start (paper-system-extent line Y)))))
+ (and (or (not prev-line)
+ (< bottom-position (page-printable-height page)))
+ position)))
+
+(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 (page-maximum-space-to-fill page lines paper)
+ "Return the space between the first line top position and the last line
+ bottom position. This constitutes the maximum space to fill on `page'
+ with `lines'."
+ (let ((last-line (car (last-pair lines))))
+ (- (page-printable-height page)
+ (first-line-position (first lines) paper)
+ (ly:prob-property last-line
+ 'bottom-space 0.0)
+ (- (interval-start (paper-system-extent last-line Y))))))
+
+(define (page-maximum-space-left page)
+ (let ((paper (ly:paper-book-paper (page-property page 'paper-book))))
+ (let bottom-position ((lines (page-property page 'lines))
+ (prev-line #f)
+ (prev-position #f))
+ (if (null? lines)
+ (page-printable-height page)
+ (let* ((line (first lines))
+ (position (line-minimum-position-on-page
+ line prev-line prev-position page)))
+ (if (null? (cdr lines))
+ (and position
+ (- (page-printable-height page)
+ (- position
+ (interval-start (paper-system-extent line Y)))))
+ (bottom-position (cdr lines) line position)))))))
+
+;;;
+;;; Utilities for distributing systems on a page
+;;;
+
+(define (space-systems space-to-fill lines ragged paper ignore-padding)
+ "Compute lines positions on page: return force and line positions as a pair.
+ force is #f if lines do not fit on page."
+ (let* ((empty-stencil (ly:make-stencil '() '(0 . 0) '(0 . 0)))
+ (empty-prob (ly:make-prob 'paper-system (list `(stencil . ,empty-stencil))))
+ (cdr-lines (append (cdr lines)
+ (if (<= (length lines) 1)
+ (list empty-prob)
+ '())))
+ (springs (map (lambda (prev-line line)
+ (list (line-ideal-distance prev-line line paper ignore-padding)
+ (/ 1.0 (line-next-space prev-line line paper))))
+ lines
+ cdr-lines))
+ (rods (map (let ((i -1))
+ (lambda (prev-line line)
+ (set! i (1+ i))
+ (list i (1+ i)
+ (line-minimum-distance prev-line line paper ignore-padding))))
+ lines
+ cdr-lines))
+ (space-result
+ (ly:solve-spring-rod-problem springs rods space-to-fill ragged)))
+ (cons (car space-result)
+ (map (let ((topskip (first-line-position (first lines) paper)))
+ (lambda (y)
+ (+ y topskip)))
+ (cdr space-result)))))
+
+(define (make-page-from-systems paper-book lines page-number ragged last)
+ "Return a new page, filled with `lines'."
+ (let* ((page (make-page paper-book
+ 'lines lines
+ 'page-number page-number
+ 'is-last last))
+ (posns (if (null? lines)
+ (list)
+ (let* ((paper (ly:paper-book-paper paper-book))
+ (space-to-fill (page-maximum-space-to-fill
+ page lines paper))
+ (spacing (space-systems space-to-fill lines ragged paper #f)))
+ (if (or (not (car spacing)) (inf? (car spacing)))
+ (cdr (space-systems space-to-fill lines ragged paper #t))
+ (cdr spacing))))))
+ (page-set-property! page 'configuration posns)
+ page))