1 ;;;; 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@cs.uu.nl>
8 (use-modules (oop goops describe)
15 (define (write-page-breaks pages)
18 (define tweaks (make-hash-table 23))
20 (define (record what property-pairs)
22 ((key (ly:output-def-lookup (ly:grob-layout what)
26 (when (ly:grob-property what 'when))
29 (if (not (hash-ref tweaks key))
30 (hash-set! tweaks key '()))
33 (acons when property-pairs
34 (hash-ref tweaks key)))
38 (define (graceless-moment mom)
40 (ly:moment-main-numerator mom)
41 (ly:moment-main-denominator mom)
44 (define (moment->skip mom)
46 ((main (if (> (ly:moment-main-numerator mom) 0)
47 (format "\\skip 1*~a/~a"
48 (ly:moment-main-numerator mom)
49 (ly:moment-main-denominator mom))
51 (grace (if (< (ly:moment-grace-numerator mom) 0)
52 (format "\\grace { \\skip 1*~a/~a }"
53 (- (ly:moment-grace-numerator mom))
54 (ly:moment-grace-denominator mom))
57 (format "~a~a" main grace)))
59 (define (dump-tweaks out-port tweak-list last-moment)
60 (if (not (null? tweak-list))
62 ((now (caar tweak-list))
63 (diff (ly:moment-sub now last-moment))
64 (these-tweaks (cdar tweak-list))
65 (skip (moment->skip diff))
66 (line-break-str (if (assoc-get 'line-break these-tweaks #f)
69 (page-break-str (if (assoc-get 'page-break these-tweaks #f)
72 (space-tweaks (format "\\spacingTweaks #'~a\n"
73 (with-output-to-string
77 (assoc-get 'spacing-parameters these-tweaks '()))))
79 (base (format "~a~a~a"
85 (format out-port "~a\n~a\n" skip base)
86 (dump-tweaks out-port (cdr tweak-list) (graceless-moment now))
89 (define (dump-all-tweaks)
91 ((paper (ly:paper-book-paper (page-property (car pages) 'paper-book)))
92 (parser (ly:output-def-parser paper))
93 (name (format "~a-page-layout.ly"
94 (ly:parser-output-name parser)))
95 (out-port (open-output-file name)))
97 (ly:progress "Writing page layout to ~a" name)
100 (format out-port "~a = {" key)
101 (dump-tweaks out-port (reverse val) (ly:make-moment 0 1))
102 (display "}" out-port))
106 (define (handle-page page)
108 (define (handle-system sys)
110 ((props `((line-break . #t)
112 . ((system-Y-extent . ,(paper-system-extent sys Y))
113 (system-refpoint-Y-extent . ,(paper-system-staff-extents sys))
114 (system-index . ,index)
115 (page-system-count . ,(length (page-lines page)))
116 (page-printable-height . ,(page-printable-height page))
117 (page-space-left . ,(page-property page 'space-left))))
120 (if (equal? (car (page-lines page)) sys)
121 (set! props (cons '(page-break . #t)
123 (if (not (ly:prob-property? sys 'is-title))
124 (record (ly:spanner-bound (ly:prob-property sys 'system-grob) LEFT)
127 (set! index (1+ index))
129 (for-each handle-system (page-lines page)))
131 (for-each handle-page pages)
134 (define (post-process-pages layout pages)
135 (if (ly:output-def-lookup layout 'write-page-layout #f)
136 (write-page-breaks pages)))
138 ;; Optimal distribution of
139 ;; lines over pages; line breaks are a given.
144 ;; - separate function for word-wrap style breaking?
145 ;; - raggedbottom? raggedlastbottom?
147 (define-public (optimal-page-breaks lines paper-book)
148 "Return pages as a list starting with 1st page. Each page is a 'page Prob."
150 (define MAXPENALTY 1e9)
151 (define paper (ly:paper-book-paper paper-book))
154 (define page-alist (layout->page-init (ly:paper-book-paper paper-book)))
155 (define scopes (ly:paper-book-scopes paper-book))
156 (define force-equalization-factor #f)
157 (define (get-path node done)
159 "Follow NODE.PREV, and return as an ascending list of pages. DONE
160 is what have collected so far, and has ascending page numbers."
163 (get-path (page-prev node) (cons node done))
166 (define (combine-penalties force user best-paths)
167 (let* ((prev-force (if (null? best-paths)
169 (page-force (car best-paths))))
170 (prev-penalty (if (null? best-paths)
172 (page-penalty (car best-paths))))
173 (inter-system-space (ly:output-def-lookup paper 'betweensystemspace))
174 (relative-force (/ force inter-system-space))
175 (abs-relative-force (abs relative-force)))
177 (+ (* abs-relative-force (+ abs-relative-force 1))
179 (* force-equalization-factor (/ (abs (- prev-force force))
183 (define (space-systems page-height lines ragged?)
184 (let* ((global-inter-system-space
185 (ly:output-def-lookup paper 'betweensystemspace))
187 (ly:output-def-lookup paper 'pagetopspace))
188 (global-fixed-dist (ly:output-def-lookup paper 'betweensystempadding))
190 (system-vector (list->vector
192 (if (= (length lines) 1)
197 (append (map paper-system-staff-extents lines)
198 (if (= (length lines) 1)
206 (lambda (sys) (paper-system-extent sys Y)) lines)
207 (if (= (length lines) 1)
211 (system-count (vector-length real-extents))
215 (interval-end (vector-ref staff-extents 0)))
216 (interval-end (vector-ref real-extents 0))
218 (last-system (vector-ref system-vector (1- system-count)))
219 (bottom-space (if (ly:prob? last-system)
220 (ly:prob-property last-system 'bottom-space 0.0)
222 (space-left (- page-height
224 (apply + (map interval-length
225 (vector->list real-extents)))))
227 (space (- page-height
231 (vector-ref real-extents (1- system-count))))))
236 (upper-system (vector-ref system-vector idx))
237 (between-space (ly:prob-property upper-system 'next-space
238 global-inter-system-space))
239 (fixed-dist (ly:prob-property upper-system 'next-padding
242 (this-system-ext (vector-ref staff-extents idx))
243 (next-system-ext (vector-ref staff-extents (1+ idx)))
244 (fixed (max 0 (- (+ (interval-end next-system-ext)
246 (interval-start this-system-ext))))
247 (title1? (and (vector-ref system-vector idx)
248 (paper-system-title? (vector-ref system-vector idx)
251 (vector-ref system-vector (1+ idx))
252 (paper-system-title? (vector-ref system-vector (1+ idx)))))
255 ((and title2? title1?)
256 (ly:output-def-lookup paper 'betweentitlespace))
258 (ly:output-def-lookup paper 'aftertitlespace))
260 (ly:output-def-lookup paper 'beforetitlespace))
261 (else between-space))
263 (hooke (/ 1 (- ideal fixed))))
264 (list ideal hooke))))
266 (springs (map calc-spring (iota (1- system-count))))
270 (upper-system (vector-ref system-vector idx))
271 (fixed-dist (ly:prob-property upper-system 'next-padding
273 (this-system-ext (vector-ref real-extents idx))
274 (next-system-ext (vector-ref real-extents (1+ idx)))
276 (distance (max (- (+ (interval-end next-system-ext)
278 (interval-start this-system-ext)
280 (entry (list idx (1+ idx) distance)))
282 (rods (map calc-rod (iota (1- system-count))))
284 ;; we don't set ragged based on amount space left.
285 ;; raggedbottomlast = ##T is much more predictable
286 (result (ly:solve-spring-rod-problem
298 (display (list "\n# systems: " system-count
299 "\nreal-ext" real-extents "\nstaff-ext" staff-extents
300 "\ninterscore" global-inter-system-space
301 "\nspace-left" space-left
302 "\nspring,rod" springs rods
305 "\npage-height" page-height
309 "\npositions" positions "\n"))))
311 (cons force positions)))
313 (define (walk-paths done-lines best-paths current-lines last? current-best)
314 "Return the best optimal-page-break-node that contains
315 CURRENT-LINES. DONE-LINES.reversed ++ CURRENT-LINES is a consecutive
316 ascending range of lines, and BEST-PATHS contains the optimal breaks
317 corresponding to DONE-LINES.
319 CURRENT-BEST is the best result sofar, or #f."
321 (let* ((this-page-num (if (null? best-paths)
322 (ly:output-def-lookup paper 'firstpagenumber)
323 (1+ (page-page-number (car best-paths)))))
325 (this-page (make-page
327 'paper-book paper-book
329 'page-number this-page-num))
331 (ragged-all? (eq? #t (ly:output-def-lookup paper 'raggedbottom)))
332 (ragged-last? (eq? #t (ly:output-def-lookup paper 'raggedlastbottom)))
333 (ragged? (or ragged-all?
336 (height (page-printable-height this-page))
337 (vertical-spacing (space-systems height current-lines ragged?))
338 (satisfied-constraints (car vertical-spacing))
339 (force (if satisfied-constraints
340 (if (and last? ragged-last?)
342 satisfied-constraints)
344 (positions (cdr vertical-spacing))
345 (get-break-penalty (lambda (sys)
346 (ly:prob-property sys 'penalty 0.0)))
347 (user-nobreak-penalties
349 (apply + (filter negative?
350 (map get-break-penalty
351 (cdr current-lines))))))
354 (max (get-break-penalty (car current-lines)) 0.0)
355 user-nobreak-penalties))
357 (total-penalty (combine-penalties
363 (< total-penalty (page-penalty current-best))))
364 (new-best (if better?
368 (page-set-property! this-page
372 (cons 'prev (if (null? best-paths)
375 (cons 'lines current-lines)
377 (cons 'configuration positions)
378 (cons 'penalty total-penalty)))
382 ;; (display total-penalty) (newline)
386 "\nuser pen " user-penalty
387 "\nsatisfied-constraints" satisfied-constraints
388 "\nlast? " last? "ragged?" ragged?
389 "\nbetter? " better? " total-penalty " total-penalty "\n"
390 "\nconfig " positions
392 "\nlines: " current-lines "\n")))
395 (display (list "\nnew-best is " (page-lines new-best)
397 (if (null? best-paths)
399 (page-lines (car best-paths))))))
401 (if (and (pair? done-lines)
402 ;; if this page is too full, adding another line won't help
403 satisfied-constraints)
404 (walk-paths (cdr done-lines) (cdr best-paths)
405 (cons (car done-lines) current-lines)
409 (define (walk-lines done best-paths todo)
410 "Return the best page breaking as a single
411 page node for optimally breaking TODO ++
412 DONE.reversed. BEST-PATHS is a list of break nodes corresponding to
417 (let* ((this-line (car todo))
418 (last? (null? (cdr todo)))
419 (next (walk-paths done best-paths (list this-line) last? #f)))
421 ;; (display "\n***************")
422 (walk-lines (cons this-line done)
423 (cons next best-paths)
426 (define (line-number node)
427 (ly:prob-property (car (page-lines node)) 'number))
429 (ly:message (_ "Calculating page breaks..."))
430 (set! force-equalization-factor
431 (ly:output-def-lookup paper 'verticalequalizationfactor 0.3))
433 (let* ((best-break-node (walk-lines '() '() lines))
434 (break-nodes (get-path best-break-node '())))
436 (page-set-property! (car (last-pair break-nodes)) 'is-last #t)
437 (if #f; (ly:get-option 'verbose)
440 "\nbreaks: " (map line-number break-nodes))
441 "\nsystems " (map page-lines break-nodes)
442 "\npenalties " (map page-penalty break-nodes)
443 "\nconfigs " (map page-configuration break-nodes))))
445 ;; construct page stencils.
446 (for-each page-stencil break-nodes)
447 (post-process-pages paper break-nodes)