+ "If the write-page-layout paper variable is true, dumps page breaks
+ and tweaks."
+
+ (let*
+ ((parser (ly:modules-lookup (list (current-module)) 'parser))
+ (output-name (ly:parser-output-name parser))
+ )
+
+ (if (ly:output-def-lookup layout 'write-page-layout #f)
+ (write-page-breaks pages output-name))))
+
+;;;
+;;; Utilities for computing line distances and positions
+;;;
+(define (line-extent line)
+ "Return the extent of the line (its lowest and highest Y-coordinates)."
+ (paper-system-extent line Y))
+
+(define (line-height line)
+ "Return the system height, that is the length of its vertical extent."
+ (interval-length (line-extent line)))
+
+(define (line-next-space line next-line layout)
+ "Return space to use between `line' and `next-line'.
+ `next-line' can be #f, meaning that `line' is the last line."
+ (let* ((title (paper-system-title? line))
+ (next-title (and next-line (paper-system-title? next-line))))
+ (ly:prob-property
+ line 'next-space
+ (ly:output-def-lookup layout
+ (cond ((and title next-title) 'between-title-space)
+ (title 'after-title-space)
+ (next-title 'before-title-space)
+ (else 'between-system-space))))))
+
+(define (line-next-padding line next-line layout)
+ "Return padding to use between `line' and `next-line'.
+ `next-line' can be #f, meaning that `line' is the last line."
+ (let ((default (ly:output-def-lookup layout 'between-system-padding)))
+ (if (ly:grob? line)
+ (let* ((details (ly:grob-property line 'line-break-system-details))
+ (padding (assq 'next-padding details)))
+ (if padding
+ (cdr padding)
+ default))
+ (ly:prob-property line 'next-padding default))))
+
+
+(define (line-minimum-distance line next-line layout ignore-padding)
+ "Minimum distance between `line' reference position and `next-line'
+ reference position. If next-line is #f, return #f."
+ (and next-line
+ (let ((padding (if ignore-padding
+ 0
+ (line-next-padding line next-line layout))))
+ (if (or (ly:grob? line) (ly:grob? next-line))
+ (max 0 (+ padding
+ (- (interval-start (line-extent line))
+ (interval-end (line-extent next-line)))))
+ (max 0 (+ padding
+ (ly:paper-system-minimum-distance line next-line)))))))
+
+(define (line-ideal-distance line next-line layout ignore-padding)
+ "Ideal distance between `line' reference position and `next-line'
+ reference position. If next-line is #f, return #f."
+ (and next-line
+ (max 0
+ (+ (- (+ (interval-end (paper-system-staff-extents next-line))
+ (if ignore-padding 0 (line-next-padding line next-line layout)))
+ (interval-start (paper-system-staff-extents line)))
+ (line-next-space line next-line layout)))))
+
+(define (first-line-position line layout)
+ "Position of the first line on page"
+ (max (+ (if (ly:prob-property? line 'is-title)
+ ;; do not use page-top-space if first line is a title
+ 0.0
+ (ly:output-def-lookup layout 'page-top-space))
+ (interval-end (paper-system-staff-extents line)))
+ (interval-end (line-extent line))))
+
+(define (line-ideal-relative-position line prev-line layout ignore-padding)
+ "Return ideal position of `line', relative to `prev-line' position.
+ `prev-line' can be #f, meaning that `line' is the first line."
+ (if (not prev-line)
+ ;; first line on page
+ (first-line-position line layout)
+ ;; not the first line on page
+ (max (line-minimum-distance prev-line line layout ignore-padding)
+ (line-ideal-distance prev-line line layout ignore-padding))))
+
+(define (line-minimum-relative-position line prev-line layout ignore-padding)
+ "Return position of `line', relative to `prev-line' position.
+ `prev-line' can be #f, meaning that `line' is the first line."
+ (if (not prev-line)
+ ;; first line on page
+ (first-line-position line layout)
+ ;; not the first line on page
+ (line-minimum-distance prev-line line layout ignore-padding)))
+
+(define (line-position-on-page line prev-line prev-position page relative-positionning-fn)
+ "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 (+ (relative-positionning-fn line prev-line layout #f)
+ (if prev-line prev-position 0.0)))
+ (bottom-position (- position
+ (interval-start (line-extent line)))))
+ position))
+
+(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 (line-extent last-line))))))
+
+(define (page-space-left page relative-positionning-fn)
+ (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-position-on-page
+ line prev-line prev-position page relative-positionning-fn)))
+ (if (null? (cdr lines))
+ (max 0
+ (- (page-printable-height page)
+ (- position
+ (interval-start (line-extent line)))))
+ (bottom-position (cdr lines) line position)))))))
+
+(define (page-maximum-space-left page)
+ (page-space-left page line-minimum-relative-position))
+
+(define (page-ideal-space-left page)
+ (page-space-left page line-ideal-relative-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)
+ (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)))))
+
+
+;;;
+;;; Page breaking function
+;;;
+
+;; Optimal distribution of
+;; lines over pages; line breaks are a given.