-;;;
-;;; Utilities for computing line distances and positions
-;;;
-(define (line-height line)
- "Return the system height, that is the length of its vertical extent."
- (interval-length (paper-system-extent line Y)))
-
-(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))))
- (cond ((and title next-title)
- (ly:output-def-lookup layout 'between-title-space))
- (title
- (ly:output-def-lookup layout 'after-title-space))
- (next-title
- (ly:output-def-lookup layout 'before-title-space))
- (else
- (ly:prob-property
- line 'next-space
- (ly:output-def-lookup layout '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."
- (ly:prob-property
- line 'next-padding
- (ly:output-def-lookup layout 'between-system-padding)))
-
-
-(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
- (max 0 (- (+ (interval-end (paper-system-extent next-line Y))
- (if ignore-padding 0 (line-next-padding line next-line layout)))
- (interval-start (paper-system-extent line Y))))))
-
-(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 (+ (ly:output-def-lookup layout 'page-top-space)
- (interval-end (paper-system-staff-extents line)))
- (interval-end (paper-system-extent line Y))))
-
-(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-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))
-
-;;;
-;;; Page breaking function
-;;;
-