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 stretchable-line? ; delete me
19 ;; utilities for writing custom page breaking functions
20 line-height line-next-space line-next-padding
21 line-minimum-distance line-ideal-distance
23 line-ideal-relative-position line-minimum-relative-position
24 line-minimum-position-on-page
25 page-maximum-space-to-fill page-maximum-space-left space-systems))
27 ; this is for 2-pass spacing. Delete me.
28 (define (stretchable-line? line)
29 "Say whether a system can be stretched."
30 (not (or (ly:prob-property? line 'is-title)
31 (let ((system-extent (paper-system-staff-extents line)))
32 (= (interval-start system-extent)
33 (interval-end system-extent))))))
35 (define (stretch-and-draw-page paper-book systems page-number ragged last)
36 (define (max-stretch sys)
38 (ly:grob-property sys 'max-stretch)
41 (define (stretchable? sys)
43 (> (max-stretch sys) 0.0)))
45 (define (height-estimate sys)
48 (ly:grob-property sys 'pure-Y-extent)
49 (paper-system-extent sys Y))))
51 (define (print-system sys)
56 (define (set-line-stretch! sorted-lines rest-height space-left)
57 (if (not (null? sorted-lines))
58 (let* ((line (first sorted-lines))
59 (height (height-estimate line))
60 (stretch (min (max-stretch line)
61 (if (positive? rest-height)
62 (/ (* height space-left) rest-height)
64 (if (stretchable? line)
65 (ly:system-stretch line stretch))
66 (set-line-stretch! (cdr sorted-lines)
67 (if (stretchable? line)
68 (- rest-height height)
70 (- space-left stretch)))))
72 (define (total-padding systems)
73 (let ((layout (ly:paper-book-paper paper-book)))
74 (if (or (null? systems)
75 (null? (cdr systems)))
77 (+ (line-next-padding (car systems) (cadr systems) layout)
78 (total-padding (cdr systems))))))
80 (let* ((page (make-page paper-book
81 'page-number page-number
83 (paper (ly:paper-book-paper paper-book))
84 (height (page-printable-height page))
85 ; there is a certain amount of impreciseness going on here:
86 ; the system heights are estimated, we aren't using skyline distances
87 ; yet, etc. If we overstretch because of underestimation, the result
88 ; is very bad. So we stick in some extra space, just to be sure.
89 (buffer (/ height 10.0))
90 (total-system-height (+ (apply + (map height-estimate systems))
91 (total-padding systems)))
92 (height-left (- height total-system-height buffer)))
97 (set-line-stretch! (sort systems
99 (< (height-estimate s1)
100 (height-estimate s2))))
101 (apply + (map height-estimate
102 (filter stretchable? systems)))
105 (let* ((lines (map print-system systems))
106 (posns (if (null? lines)
108 (let* ((paper (ly:paper-book-paper paper-book))
109 (space-to-fill (page-maximum-space-to-fill
111 (spacing (space-systems space-to-fill lines ragged paper #f)))
112 (if (and (> (length lines) 1)
113 (or (not (car spacing)) (inf? (car spacing))))
115 (ly:warning (_ "Can't fit systems on page -- ignoring between-system-padding"))
116 (cdr (space-systems space-to-fill lines ragged paper #t)))
118 (page-set-property! page 'lines lines)
119 (page-set-property! page 'configuration posns)
122 (define (page-breaking-wrapper paper-book)
123 "Compute line and page breaks by calling the page-breaking paper variable,
124 then performs the post process function using the page-post-process paper
125 variable. Finally, return the pages."
126 (let* ((paper (ly:paper-book-paper paper-book))
127 (pages ((ly:output-def-lookup paper 'page-breaking) paper-book)))
128 ((ly:output-def-lookup paper 'page-post-process) paper pages)
131 (define (post-process-pages layout pages)
132 "If the write-page-layout paper variable is true, dumps page breaks
136 ((parser (ly:modules-lookup (list (current-module)) 'parser))
137 (output-name (ly:parser-output-name parser))
140 (if (ly:output-def-lookup layout 'write-page-layout #f)
141 (write-page-breaks pages output-name))))
144 ;;; Utilities for computing line distances and positions
146 (define (line-extent line)
147 "Return the extent of the line (its lowest and highest Y-coordinates)."
148 (paper-system-extent line Y))
150 (define (line-height line)
151 "Return the system height, that is the length of its vertical extent."
152 (interval-length (line-extent line)))
154 (define (line-next-space line next-line layout)
155 "Return space to use between `line' and `next-line'.
156 `next-line' can be #f, meaning that `line' is the last line."
157 (let* ((title (paper-system-title? line))
158 (next-title (and next-line (paper-system-title? next-line))))
161 (ly:output-def-lookup layout
162 (cond ((and title next-title) 'between-title-space)
163 (title 'after-title-space)
164 (next-title 'before-title-space)
165 (else 'between-system-space))))))
167 (define (line-next-padding line next-line layout)
168 "Return padding to use between `line' and `next-line'.
169 `next-line' can be #f, meaning that `line' is the last line."
170 (let ((default (ly:output-def-lookup layout 'between-system-padding)))
172 (let* ((details (ly:grob-property line 'line-break-system-details))
173 (padding (assq 'next-padding details)))
177 (ly:prob-property line 'next-padding default))))
180 (define (line-minimum-distance line next-line layout ignore-padding)
181 "Minimum distance between `line' reference position and `next-line'
182 reference position. If next-line is #f, return #f."
184 (let ((padding (if ignore-padding
186 (line-next-padding line next-line layout))))
187 (if (or (ly:grob? line) (ly:grob? next-line))
189 (- (interval-start (line-extent line))
190 (interval-end (line-extent next-line)))))
192 (ly:paper-system-minimum-distance line next-line)))))))
194 (define (line-ideal-distance line next-line layout ignore-padding)
195 "Ideal distance between `line' reference position and `next-line'
196 reference position. If next-line is #f, return #f."
198 (max (+ (max 0 (- (+ (interval-end (paper-system-staff-extents next-line))
199 (if ignore-padding 0 (line-next-padding line next-line layout)))
200 (interval-start (paper-system-staff-extents line))))
201 (line-next-space line next-line layout))
202 (line-minimum-distance line next-line layout ignore-padding))))
204 (define (first-line-position line layout)
205 "Position of the first line on page"
206 (max (+ (if (ly:prob-property? line 'is-title)
207 ;; do not use page-top-space if first line is a title
209 (ly:output-def-lookup layout 'page-top-space))
210 (interval-end (paper-system-staff-extents line)))
211 (interval-end (line-extent line))))
213 (define (line-ideal-relative-position line prev-line layout ignore-padding)
214 "Return ideal position of `line', relative to `prev-line' position.
215 `prev-line' can be #f, meaning that `line' is the first line."
217 ;; first line on page
218 (first-line-position line layout)
219 ;; not the first line on page
220 (max (line-minimum-distance prev-line line layout ignore-padding)
221 (line-ideal-distance prev-line line layout ignore-padding))))
223 (define (line-minimum-relative-position line prev-line layout ignore-padding)
224 "Return position of `line', relative to `prev-line' position.
225 `prev-line' can be #f, meaning that `line' is the first line."
227 ;; first line on page
228 (first-line-position line layout)
229 ;; not the first line on page
230 (line-minimum-distance prev-line line layout ignore-padding)))
232 (define (line-minimum-position-on-page line prev-line prev-position page)
233 "If `line' fits on `page' after `prev-line', which position on page is
234 `prev-position', then return the line's postion on page, otherwise #f.
235 `prev-line' can be #f, meaning that `line' is the first line."
236 (let* ((layout (ly:paper-book-paper (page-property page 'paper-book)))
237 (position (+ (line-minimum-relative-position line prev-line layout #f)
238 (if prev-line prev-position 0.0)))
239 (bottom-position (- position
240 (interval-start (line-extent line)))))
241 (and (or (not prev-line)
242 (< bottom-position (page-printable-height page)))
245 (define (page-maximum-space-to-fill page lines paper)
246 "Return the space between the first line top position and the last line
247 bottom position. This constitutes the maximum space to fill on `page'
249 (let ((last-line (car (last-pair lines))))
250 (- (page-printable-height page)
251 (first-line-position (first lines) paper)
252 (ly:prob-property last-line
254 (- (interval-start (line-extent last-line))))))
256 (define (page-maximum-space-left page)
257 (let ((paper (ly:paper-book-paper (page-property page 'paper-book))))
258 (let bottom-position ((lines (page-property page 'lines))
262 (page-printable-height page)
263 (let* ((line (first lines))
264 (position (line-minimum-position-on-page
265 line prev-line prev-position page)))
266 (if (null? (cdr lines))
268 (- (page-printable-height page)
270 (interval-start (line-extent line)))))
271 (bottom-position (cdr lines) line position)))))))
274 ;;; Utilities for distributing systems on a page
277 (define (space-systems space-to-fill lines ragged paper ignore-padding)
278 "Compute lines positions on page: return force and line positions as a pair.
279 force is #f if lines do not fit on page."
280 (let* ((empty-stencil (ly:make-stencil '() '(0 . 0) '(0 . 0)))
281 (empty-prob (ly:make-prob 'paper-system (list `(stencil . ,empty-stencil))))
282 (cdr-lines (append (cdr lines)
283 (if (<= (length lines) 1)
286 (springs (map (lambda (prev-line line)
287 (list (line-ideal-distance prev-line line paper ignore-padding)
288 (line-next-space prev-line line paper)))
291 (rods (map (let ((i -1))
292 (lambda (prev-line line)
295 (line-minimum-distance prev-line line paper ignore-padding))))
299 (ly:solve-spring-rod-problem springs rods space-to-fill ragged)))
300 (cons (car space-result)
301 (map (let ((topskip (first-line-position (first lines) paper)))
304 (cdr space-result)))))
308 ;;; Page breaking function
311 ;; Optimal distribution of
312 ;; lines over pages; line breaks are a given.
317 ;; - separate function for word-wrap style breaking?
318 ;; - ragged-bottom? ragged-last-bottom?
320 (define (get-path node done)
321 "Follow NODE.PREV, and return as an ascending list of pages. DONE
322 is what have collected so far, and has ascending page numbers."
324 (get-path (page-prev node) (cons node done))
327 (define (combine-penalties force user best-paths
328 inter-system-space force-equalization-factor)
329 (let* ((prev-force (if (null? best-paths)
331 (page-force (car best-paths))))
332 (prev-penalty (if (null? best-paths)
334 (page-penalty (car best-paths))))
335 (relative-force (/ force inter-system-space))
336 (abs-relative-force (abs relative-force)))
337 (+ (* abs-relative-force (+ abs-relative-force 1))
339 (* force-equalization-factor (/ (abs (- prev-force force))
343 (define (walk-paths done-lines best-paths current-lines last current-best
344 paper-book page-alist)
345 "Return the best optimal-page-break-node that contains
346 CURRENT-LINES. DONE-LINES.reversed ++ CURRENT-LINES is a consecutive
347 ascending range of lines, and BEST-PATHS contains the optimal breaks
348 corresponding to DONE-LINES.
350 CURRENT-BEST is the best result sofar, or #f."
351 (let* ((paper (ly:paper-book-paper paper-book))
352 (this-page (make-page
355 'page-number (if (null? best-paths)
356 (ly:output-def-lookup paper 'first-page-number)
357 (1+ (page-page-number (first best-paths))))))
358 (ragged-all (eq? #t (ly:output-def-lookup paper 'ragged-bottom)))
359 (ragged-last (eq? #t (ly:output-def-lookup paper 'ragged-last-bottom)))
360 (ragged (or ragged-all (and ragged-last last)))
361 (space-to-fill (page-maximum-space-to-fill this-page current-lines paper))
362 (vertical-spacing (space-systems space-to-fill current-lines ragged paper #f))
363 (satisfied-constraints (car vertical-spacing))
364 (force (if satisfied-constraints
365 (if (and last ragged-last)
367 satisfied-constraints)
369 (positions (cdr vertical-spacing))
370 (get-break-penalty (lambda (sys)
371 (ly:prob-property sys 'penalty 0.0)))
372 (user-nobreak-penalties (- (apply + (filter negative?
373 (map get-break-penalty
374 (cdr current-lines))))))
375 (user-penalty (+ (max (get-break-penalty (car current-lines)) 0.0)
376 user-nobreak-penalties))
377 (total-penalty (combine-penalties
378 force user-penalty best-paths
379 (ly:output-def-lookup paper 'between-system-space)
380 (ly:output-def-lookup paper 'verticalequalizationfactor 0.3)))
381 (new-best (if (or (not current-best)
382 (and satisfied-constraints
383 (< total-penalty (page-penalty current-best))))
386 (page-set-property! this-page
389 (list (cons 'prev (if (null? best-paths)
392 (cons 'lines current-lines)
394 (cons 'configuration positions)
395 (cons 'penalty total-penalty)))
401 "\nuser pen " user-penalty
402 "\nsatisfied-constraints" satisfied-constraints
403 "\nlast? " last "ragged?" ragged
404 "\nis-better " is-better " total-penalty " total-penalty "\n"
405 "\nconfig " positions
407 "\nlines: " current-lines "\n")))
409 (display (list "\nnew-best is " (page-lines new-best)
411 (if (null? best-paths)
413 (page-lines (car best-paths))))))
414 (if (and (pair? done-lines)
415 ;; if this page is too full, adding another line won't help
416 satisfied-constraints)
417 (walk-paths (cdr done-lines) (cdr best-paths)
418 (cons (car done-lines) current-lines)
420 paper-book page-alist)
423 (define (walk-lines done best-paths todo paper-book page-alist)
424 "Return the best page breaking as a single
425 page node for optimally breaking TODO ++
426 DONE.reversed. BEST-PATHS is a list of break nodes corresponding to
430 (let* ((this-line (car todo))
431 (last (null? (cdr todo)))
432 (next (walk-paths done best-paths (list this-line) last #f
433 paper-book page-alist)))
434 (walk-lines (cons this-line done)
435 (cons next best-paths)
440 (define-public (optimal-page-breaks paper-book)
441 "Return pages as a list starting with 1st page. Each page is a 'page Prob."
442 (let* ((paper (ly:paper-book-paper paper-book))
443 (lines (ly:paper-book-systems paper-book))
444 (page-alist (layout->page-init paper))
445 (force-equalization-factor (ly:output-def-lookup
446 paper 'verticalequalizationfactor 0.3)))
447 (ly:message (_ "Calculating page breaks..."))
448 (let* ((best-break-node (walk-lines '() '() lines paper-book page-alist))
449 (break-nodes (get-path best-break-node '())))
450 (page-set-property! (car (last-pair break-nodes)) 'is-last #t)
451 (if #f; (ly:get-option 'verbose)
454 "\nbreaks: " (map (lambda (node)
455 (ly:prob-property (car (page-lines node))
458 "\nsystems " (map page-lines break-nodes)
459 "\npenalties " (map page-penalty break-nodes)
460 "\nconfigs " (map page-configuration break-nodes)))))
461 ;; construct page stencils.
462 (for-each page-stencil break-nodes)