1 ;;;; layout-page-layout.scm -- page breaking and page layout
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2006 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
8 (define-module (scm layout-page-layout)
9 #:use-module (srfi srfi-1)
10 #:use-module (oop goops describe)
11 #:use-module (oop goops)
12 #:use-module (scm paper-system)
13 #:use-module (scm page)
14 #:use-module (scm layout-page-dump)
16 #:export (post-process-pages optimal-page-breaks make-page-from-systems
18 ;; utilities for writing custom page breaking functions
19 line-height line-next-space line-next-padding
20 line-minimum-distance line-ideal-distance
22 line-ideal-relative-position line-minimum-relative-position
23 line-minimum-position-on-page stretchable-line?
24 page-maximum-space-to-fill page-maximum-space-left space-systems))
26 (define (page-breaking-wrapper paper-book)
27 "Compute line and page breaks by calling the page-breaking paper variable,
28 then performs the post process function using the page-post-process paper
29 variable. Finally, return the pages."
30 (let* ((paper (ly:paper-book-paper paper-book))
31 (pages ((ly:output-def-lookup paper 'page-breaking) paper-book)))
32 ((ly:output-def-lookup paper 'page-post-process) paper pages)
35 (define (post-process-pages layout pages)
36 "If the write-page-layout paper variable is true, dumps page breaks
40 ((parser (ly:modules-lookup (list (current-module)) 'parser))
41 (output-name (ly:parser-output-name parser))
44 (if (ly:output-def-lookup layout 'write-page-layout #f)
45 (write-page-breaks pages output-name))))
48 ;;; Utilities for computing line distances and positions
50 (define (line-height line)
51 "Return the system height, that is the length of its vertical extent."
52 (interval-length (paper-system-extent line Y)))
54 (define (line-next-space line next-line layout)
55 "Return space to use between `line' and `next-line'.
56 `next-line' can be #f, meaning that `line' is the last line."
57 (let* ((title (paper-system-title? line))
58 (next-title (and next-line (paper-system-title? next-line))))
59 (cond ((and title next-title)
60 (ly:output-def-lookup layout 'between-title-space))
62 (ly:output-def-lookup layout 'after-title-space))
64 (ly:output-def-lookup layout 'before-title-space))
68 (ly:output-def-lookup layout 'between-system-space))))))
70 (define (line-next-padding line next-line layout)
71 "Return padding to use between `line' and `next-line'.
72 `next-line' can be #f, meaning that `line' is the last line."
75 (ly:output-def-lookup layout 'between-system-padding)))
78 (define (line-minimum-distance line next-line layout ignore-padding)
79 "Minimum distance between `line' reference position and `next-line'
80 reference position. If next-line is #f, return #f."
82 (let ((non-skyline-distance (- (interval-end (paper-system-extent next-line Y))
83 (interval-start (paper-system-extent line Y)))))
84 (max 0 (+ (ly:prob-property next-line 'skyline-distance non-skyline-distance)
85 (if ignore-padding 0 (line-next-padding line next-line layout)))))))
87 (define (line-ideal-distance line next-line layout ignore-padding)
88 "Ideal distance between `line' reference position and `next-line'
89 reference position. If next-line is #f, return #f."
91 (+ (max 0 (- (+ (interval-end (paper-system-staff-extents next-line))
92 (if ignore-padding 0 (line-next-padding line next-line layout)))
93 (interval-start (paper-system-staff-extents line))))
94 (line-next-space line next-line layout))))
96 (define (first-line-position line layout)
97 "Position of the first line on page"
98 (max (+ (if (ly:prob-property? line 'is-title)
99 ;; do not use page-top-space if first line is a title
101 (ly:output-def-lookup layout 'page-top-space))
102 (interval-end (paper-system-staff-extents line)))
103 (interval-end (paper-system-extent line Y))))
105 (define (line-ideal-relative-position line prev-line layout ignore-padding)
106 "Return ideal position of `line', relative to `prev-line' position.
107 `prev-line' can be #f, meaning that `line' is the first line."
109 ;; first line on page
110 (first-line-position line layout)
111 ;; not the first line on page
112 (max (line-minimum-distance prev-line line layout ignore-padding)
113 (line-ideal-distance prev-line line layout ignore-padding))))
115 (define (line-minimum-relative-position line prev-line layout ignore-padding)
116 "Return position of `line', relative to `prev-line' position.
117 `prev-line' can be #f, meaning that `line' is the first line."
119 ;; first line on page
120 (first-line-position line layout)
121 ;; not the first line on page
122 (line-minimum-distance prev-line line layout ignore-padding)))
124 (define (line-minimum-position-on-page line prev-line prev-position page)
125 "If `line' fits on `page' after `prev-line', which position on page is
126 `prev-position', then return the line's postion on page, otherwise #f.
127 `prev-line' can be #f, meaning that `line' is the first line."
128 (let* ((layout (ly:paper-book-paper (page-property page 'paper-book)))
129 (position (+ (line-minimum-relative-position line prev-line layout #f)
130 (if prev-line prev-position 0.0)))
131 (bottom-position (- position
132 (interval-start (paper-system-extent line Y)))))
133 (and (or (not prev-line)
134 (< bottom-position (page-printable-height page)))
137 (define (stretchable-line? line)
138 "Say whether a system can be stretched."
139 (not (or (ly:prob-property? line 'is-title)
140 (let ((system-extent (paper-system-staff-extents line)))
141 (= (interval-start system-extent)
142 (interval-end system-extent))))))
144 (define (page-maximum-space-to-fill page lines paper)
145 "Return the space between the first line top position and the last line
146 bottom position. This constitutes the maximum space to fill on `page'
148 (let ((last-line (car (last-pair lines))))
149 (- (page-printable-height page)
150 (first-line-position (first lines) paper)
151 (ly:prob-property last-line
153 (- (interval-start (paper-system-extent last-line Y))))))
155 (define (page-maximum-space-left page)
156 (let ((paper (ly:paper-book-paper (page-property page 'paper-book))))
157 (let bottom-position ((lines (page-property page 'lines))
161 (page-printable-height page)
162 (let* ((line (first lines))
163 (position (line-minimum-position-on-page
164 line prev-line prev-position page)))
165 (if (null? (cdr lines))
167 (- (page-printable-height page)
169 (interval-start (paper-system-extent line Y)))))
170 (bottom-position (cdr lines) line position)))))))
173 ;;; Utilities for distributing systems on a page
176 (define (space-systems space-to-fill lines ragged paper ignore-padding)
177 "Compute lines positions on page: return force and line positions as a pair.
178 force is #f if lines do not fit on page."
179 (let* ((empty-stencil (ly:make-stencil '() '(0 . 0) '(0 . 0)))
180 (empty-prob (ly:make-prob 'paper-system (list `(stencil . ,empty-stencil))))
181 (cdr-lines (append (cdr lines)
182 (if (<= (length lines) 1)
185 (springs (map (lambda (prev-line line)
186 (list (line-ideal-distance prev-line line paper ignore-padding)
187 (/ 1.0 (line-next-space prev-line line paper))))
190 (rods (map (let ((i -1))
191 (lambda (prev-line line)
194 (line-minimum-distance prev-line line paper ignore-padding))))
198 (ly:solve-spring-rod-problem springs rods space-to-fill ragged)))
199 (cons (car space-result)
200 (map (let ((topskip (first-line-position (first lines) paper)))
203 (cdr space-result)))))
205 (define (make-page-from-systems paper-book lines page-number ragged last)
206 "Return a new page, filled with `lines'."
207 (let* ((page (make-page paper-book
209 'page-number page-number
211 (posns (if (null? lines)
213 (let* ((paper (ly:paper-book-paper paper-book))
214 (space-to-fill (page-maximum-space-to-fill
216 (spacing (space-systems space-to-fill lines ragged paper #f)))
217 (if (and (> (length lines) 1)
218 (or (not (car spacing)) (inf? (car spacing))))
220 (ly:warning (_ "Can't fit systems on page -- ignoring between-system-padding"))
221 (cdr (space-systems space-to-fill lines ragged paper #t)))
223 (page-set-property! page 'configuration posns)
227 ;;; Page breaking function
230 ;; Optimal distribution of
231 ;; lines over pages; line breaks are a given.
236 ;; - separate function for word-wrap style breaking?
237 ;; - ragged-bottom? ragged-last-bottom?
239 (define (get-path node done)
240 "Follow NODE.PREV, and return as an ascending list of pages. DONE
241 is what have collected so far, and has ascending page numbers."
243 (get-path (page-prev node) (cons node done))
246 (define (combine-penalties force user best-paths
247 inter-system-space force-equalization-factor)
248 (let* ((prev-force (if (null? best-paths)
250 (page-force (car best-paths))))
251 (prev-penalty (if (null? best-paths)
253 (page-penalty (car best-paths))))
254 (relative-force (/ force inter-system-space))
255 (abs-relative-force (abs relative-force)))
256 (+ (* abs-relative-force (+ abs-relative-force 1))
258 (* force-equalization-factor (/ (abs (- prev-force force))
262 (define (walk-paths done-lines best-paths current-lines last current-best
263 paper-book page-alist)
264 "Return the best optimal-page-break-node that contains
265 CURRENT-LINES. DONE-LINES.reversed ++ CURRENT-LINES is a consecutive
266 ascending range of lines, and BEST-PATHS contains the optimal breaks
267 corresponding to DONE-LINES.
269 CURRENT-BEST is the best result sofar, or #f."
270 (let* ((paper (ly:paper-book-paper paper-book))
271 (this-page (make-page
274 'page-number (if (null? best-paths)
275 (ly:output-def-lookup paper 'first-page-number)
276 (1+ (page-page-number (first best-paths))))))
277 (ragged-all (eq? #t (ly:output-def-lookup paper 'ragged-bottom)))
278 (ragged-last (eq? #t (ly:output-def-lookup paper 'ragged-last-bottom)))
279 (ragged (or ragged-all (and ragged-last last)))
280 (space-to-fill (page-maximum-space-to-fill this-page current-lines paper))
281 (vertical-spacing (space-systems space-to-fill current-lines ragged paper #f))
282 (satisfied-constraints (car vertical-spacing))
283 (force (if satisfied-constraints
284 (if (and last ragged-last)
286 satisfied-constraints)
288 (positions (cdr vertical-spacing))
289 (get-break-penalty (lambda (sys)
290 (ly:prob-property sys 'penalty 0.0)))
291 (user-nobreak-penalties (- (apply + (filter negative?
292 (map get-break-penalty
293 (cdr current-lines))))))
294 (user-penalty (+ (max (get-break-penalty (car current-lines)) 0.0)
295 user-nobreak-penalties))
296 (total-penalty (combine-penalties
297 force user-penalty best-paths
298 (ly:output-def-lookup paper 'between-system-space)
299 (ly:output-def-lookup paper 'verticalequalizationfactor 0.3)))
300 (new-best (if (or (not current-best)
301 (and satisfied-constraints
302 (< total-penalty (page-penalty current-best))))
305 (page-set-property! this-page
308 (list (cons 'prev (if (null? best-paths)
311 (cons 'lines current-lines)
313 (cons 'configuration positions)
314 (cons 'penalty total-penalty)))
320 "\nuser pen " user-penalty
321 "\nsatisfied-constraints" satisfied-constraints
322 "\nlast? " last "ragged?" ragged
323 "\nis-better " is-better " total-penalty " total-penalty "\n"
324 "\nconfig " positions
326 "\nlines: " current-lines "\n")))
328 (display (list "\nnew-best is " (page-lines new-best)
330 (if (null? best-paths)
332 (page-lines (car best-paths))))))
333 (if (and (pair? done-lines)
334 ;; if this page is too full, adding another line won't help
335 satisfied-constraints)
336 (walk-paths (cdr done-lines) (cdr best-paths)
337 (cons (car done-lines) current-lines)
339 paper-book page-alist)
342 (define (walk-lines done best-paths todo paper-book page-alist)
343 "Return the best page breaking as a single
344 page node for optimally breaking TODO ++
345 DONE.reversed. BEST-PATHS is a list of break nodes corresponding to
349 (let* ((this-line (car todo))
350 (last (null? (cdr todo)))
351 (next (walk-paths done best-paths (list this-line) last #f
352 paper-book page-alist)))
353 (walk-lines (cons this-line done)
354 (cons next best-paths)
359 (define-public (optimal-page-breaks paper-book)
360 "Return pages as a list starting with 1st page. Each page is a 'page Prob."
361 (let* ((paper (ly:paper-book-paper paper-book))
362 (lines (ly:paper-book-systems paper-book))
363 (page-alist (layout->page-init paper))
364 (force-equalization-factor (ly:output-def-lookup
365 paper 'verticalequalizationfactor 0.3)))
366 (ly:message (_ "Calculating page breaks..."))
367 (let* ((best-break-node (walk-lines '() '() lines paper-book page-alist))
368 (break-nodes (get-path best-break-node '())))
369 (page-set-property! (car (last-pair break-nodes)) 'is-last #t)
370 (if #f; (ly:get-option 'verbose)
373 "\nbreaks: " (map (lambda (node)
374 (ly:prob-property (car (page-lines node))
377 "\nsystems " (map page-lines break-nodes)
378 "\npenalties " (map page-penalty break-nodes)
379 "\nconfigs " (map page-configuration break-nodes)))))
380 ;; construct page stencils.
381 (for-each page-stencil break-nodes)