1 ;;;; page-layout.scm -- page breaking and page layout
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004--2005 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 (use-modules (oop goops describe)
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 (define-class <optimally-broken-page-node> ()
15 (prev #:init-value '() #:accessor node-prev #:init-keyword #:prev)
16 (page #:init-value 0 #:accessor node-page-number #:init-keyword #:pageno)
17 (force #:init-value 0 #:accessor node-force #:init-keyword #:force)
18 (penalty #:init-value 0 #:accessor node-penalty #:init-keyword #:penalty)
19 (configuration #:init-value '() #:accessor node-configuration #:init-keyword #:configuration)
20 (lines #:init-value 0 #:accessor node-lines #:init-keyword #:lines))
22 (define-method (display (node <optimally-broken-page-node>) port)
23 (map (lambda (x) (display x port))
25 "Page " (node-page-number node)
26 " Lines: " (node-lines node)
27 " Penalty " (node-penalty node)
30 (define-method (node-system-numbers (node <optimally-broken-page-node>))
31 (map ly:paper-system-property (node-lines node) 'number))
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38 (define (page-headfoot layout scopes number sym sepsym dir last?)
39 "Create a stencil including separating space."
40 (let* ((header-proc (ly:output-def-lookup layout sym))
41 (sep (ly:output-def-lookup layout sepsym))
42 (stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
44 (if (procedure? header-proc)
45 (header-proc layout scopes number last?)
48 (if (and (number? sep)
49 (ly:stencil? head-stencil)
50 (not (ly:stencil-empty? head-stencil)))
52 (ly:stencil-combine-at-edge
53 stencil Y dir head-stencil
58 (define-public (default-page-music-height layout scopes number last?)
59 "Printable area for music and titles; matches default-page-make-stencil."
60 (let* ((h (- (ly:output-def-lookup layout 'vsize)
61 (ly:output-def-lookup layout 'topmargin)
62 (ly:output-def-lookup layout 'bottommargin)))
63 (head (page-headfoot layout scopes number 'make-header 'headsep UP last?))
64 (foot (page-headfoot layout scopes number 'make-footer 'footsep DOWN last?))
66 (- h (if (ly:stencil? head)
67 (interval-length (ly:stencil-extent head Y))
69 (if (ly:stencil? foot)
70 (interval-length (ly:stencil-extent foot Y))
73 ;; (display (list "\n available" available head foot))
76 (define-public (default-page-make-stencil
77 lines offsets layout scopes number last?)
78 "Construct a stencil representing the page from LINES.
80 Offsets is a list of increasing numbers. They must be negated to
84 (let* ((topmargin (ly:output-def-lookup layout 'topmargin))
86 ;; TODO: naming vsize/hsize not analogous to TeX.
88 (vsize (ly:output-def-lookup layout 'vsize))
89 (hsize (ly:output-def-lookup layout 'hsize))
91 (system-xoffset (ly:output-def-lookup layout 'horizontalshift 0.0))
92 (system-separator-markup (ly:output-def-lookup layout 'systemSeparatorMarkup))
93 (system-separator-stencil (if (markup? system-separator-markup)
94 (interpret-markup layout
95 (layout-extract-page-properties layout)
96 system-separator-markup)
98 (lmargin (ly:output-def-lookup layout 'leftmargin))
99 (leftmargin (if lmargin
102 (ly:output-def-lookup layout 'linewidth)) 2)))
104 (rightmargin (ly:output-def-lookup layout 'rightmargin))
105 (bottom-edge (- vsize
106 (ly:output-def-lookup layout 'bottommargin)))
108 (head (page-headfoot layout scopes number 'make-header 'headsep UP last?))
109 (foot (page-headfoot layout scopes number 'make-footer 'footsep DOWN last?))
111 (head-height (if (ly:stencil? head)
112 (interval-length (ly:stencil-extent head Y))
115 (height-proc (ly:output-def-lookup layout 'page-music-height))
117 (page-stencil (ly:make-stencil '()
118 (cons leftmargin hsize)
119 (cons (- topmargin) 0)))
122 (add-to-page (lambda (stencil y)
124 (ly:stencil-add page-stencil
125 (ly:stencil-translate stencil
128 (- 0 head-height y topmargin))
132 (lambda (stencil-position)
133 (let* ((system (car stencil-position))
134 (stencil (ly:paper-system-stencil system))
135 (y (cadr stencil-position))
136 (is-title (paper-system-title?
137 (car stencil-position))))
138 (add-to-page stencil y)
139 (if (and (ly:stencil? system-separator-stencil)
141 (not (paper-system-title? system))
142 (not (paper-system-title? last-system)))
144 system-separator-stencil
146 (car (ly:paper-system-staff-extents last-system)))
148 (cdr (ly:paper-system-staff-extents system))))))
149 (set! last-system system)
154 "leftmargin " leftmargin "rightmargin " rightmargin
157 (set! page-stencil (ly:stencil-combine-at-edge
161 (not (ly:stencil-empty? head)))
163 (ly:make-stencil "" (cons 0 0) (cons 0 0)))
166 (map add-system (zip lines offsets))
167 (if (and (ly:stencil? foot)
168 (not (ly:stencil-empty? foot)))
172 (ly:stencil-translate
176 (- (car (ly:stencil-extent foot Y)))))))))
178 (ly:stencil-translate page-stencil (cons leftmargin 0))))
180 ;;; optimal page breaking
182 ;;; This is not optimal page breaking, this is optimal distribution of
183 ;;; lines over pages; line breaks are a given.
188 ;; - separate function for word-wrap style breaking?
189 ;; - raggedbottom? raggedlastbottom?
191 (define-public (optimal-page-breaks lines paper-book)
192 "Return pages as a list starting with 1st page. Each page is a list
195 (define MAXPENALTY 1e9)
196 (define paper (ly:paper-book-paper paper-book))
197 (define scopes (ly:paper-book-scopes paper-book))
198 (define force-equalization-factor #f)
200 (define (page-height page-number last?)
201 (let ((p (ly:output-def-lookup paper 'page-music-height)))
204 (p paper scopes page-number last?)
207 (define (get-path node done)
208 "Follow NODE.PREV, and return as an ascending list of pages. DONE
209 is what have collected so far, and has ascending page numbers."
211 (if (is-a? node <optimally-broken-page-node>)
212 (get-path (node-prev node) (cons node done))
215 (define (combine-penalties force user best-paths)
216 (let* ((prev-force (if (null? best-paths)
218 (node-force (car best-paths))))
219 (prev-penalty (if (null? best-paths)
221 (node-penalty (car best-paths))))
222 (inter-system-space (ly:output-def-lookup paper 'betweensystemspace))
223 (relative-force (/ force inter-system-space))
224 (abs-relative-force (abs relative-force)))
227 (+ (* abs-relative-force (+ abs-relative-force 1))
229 (* force-equalization-factor (/ (abs (- prev-force force))
233 (define (space-systems page-height lines ragged?)
234 (let* ((global-inter-system-space
235 (ly:output-def-lookup paper 'betweensystemspace))
237 (ly:output-def-lookup paper 'pagetopspace))
238 (global-fixed-dist (ly:output-def-lookup paper 'betweensystempadding))
240 (system-vector (list->vector
242 (if (= (length lines) 1)
247 (append (map ly:paper-system-staff-extents lines)
248 (if (= (length lines) 1)
256 (lambda (sys) (ly:paper-system-extent sys Y)) lines)
257 (if (= (length lines) 1)
261 (system-count (vector-length real-extents))
265 (interval-end (vector-ref staff-extents 0)))
266 (interval-end (vector-ref real-extents 0))
268 (last-system (vector-ref system-vector (1- system-count)))
269 (bottom-space (if (ly:paper-system? last-system)
270 (ly:paper-system-property last-system 'bottom-space 0.0)
272 (space-left (- page-height
274 (apply + (map interval-length
275 (vector->list real-extents)))))
277 (space (- page-height
281 (vector-ref real-extents (1- system-count))))))
286 (upper-system (vector-ref system-vector idx))
287 (between-space (ly:paper-system-property upper-system 'next-space
288 global-inter-system-space))
289 (fixed-dist (ly:paper-system-property upper-system 'next-padding
292 (this-system-ext (vector-ref staff-extents idx))
293 (next-system-ext (vector-ref staff-extents (1+ idx)))
294 (fixed (max 0 (- (+ (interval-end next-system-ext)
296 (interval-start this-system-ext))))
297 (title1? (and (vector-ref system-vector idx)
298 (paper-system-title? (vector-ref system-vector idx)
301 (vector-ref system-vector (1+ idx))
302 (paper-system-title? (vector-ref system-vector (1+ idx)))))
305 ((and title2? title1?)
306 (ly:output-def-lookup paper 'betweentitlespace))
308 (ly:output-def-lookup paper 'aftertitlespace))
310 (ly:output-def-lookup paper 'beforetitlespace))
311 (else between-space))
313 (hooke (/ 1 (- ideal fixed))))
314 (list ideal hooke))))
316 (springs (map calc-spring (iota (1- system-count))))
320 (upper-system (vector-ref system-vector idx))
321 (fixed-dist (ly:paper-system-property upper-system 'next-padding
323 (this-system-ext (vector-ref real-extents idx))
324 (next-system-ext (vector-ref real-extents (1+ idx)))
326 (distance (max (- (+ (interval-end next-system-ext)
328 (interval-start this-system-ext)
330 (entry (list idx (1+ idx) distance)))
332 (rods (map calc-rod (iota (1- system-count))))
334 ;; we don't set ragged based on amount space left.
335 ;; raggedbottomlast = ##T is much more predictable
336 (result (ly:solve-spring-rod-problem
348 (display (list "\n# systems: " system-count
349 "\nreal-ext" real-extents "\nstaff-ext" staff-extents
350 "\ninterscore" inter-system-space
351 "\nspace-letf" space-left
352 "\nspring,rod" springs rods
355 "\npage-height" page-height
359 "\npositions" positions "\n"))))
361 (cons force positions)))
363 (define (walk-paths done-lines best-paths current-lines last? current-best)
364 "Return the best optimal-page-break-node that contains
365 CURRENT-LINES. DONE-LINES.reversed ++ CURRENT-LINES is a consecutive
366 ascending range of lines, and BEST-PATHS contains the optimal breaks
367 corresponding to DONE-LINES.
369 CURRENT-BEST is the best result sofar, or #f."
372 (let* ((this-page-num (if (null? best-paths)
373 (ly:output-def-lookup paper 'firstpagenumber)
374 (1+ (node-page-number (car best-paths)))))
376 (ragged-all? (eq? #t (ly:output-def-lookup paper 'raggedbottom)))
377 (ragged-last? (eq? #t (ly:output-def-lookup paper 'raggedlastbottom)))
378 (ragged? (or ragged-all?
381 (page-height (page-height this-page-num last?))
382 (vertical-spacing (space-systems page-height current-lines ragged?))
383 (satisfied-constraints (car vertical-spacing))
384 (force (if satisfied-constraints
385 (if (and last? ragged-last?)
387 satisfied-constraints)
389 (positions (cdr vertical-spacing))
390 (get-break-penalty (lambda (sys)
391 (ly:paper-system-property sys 'penalty 0.0)))
392 (user-nobreak-penalties
394 (apply + (filter negative?
395 (map get-break-penalty
396 (cdr current-lines))))))
399 (max (get-break-penalty (car current-lines)) 0.0)
400 user-nobreak-penalties))
401 (total-penalty (combine-penalties
408 (< total-penalty (node-penalty current-best))))
409 (new-best (if better?
410 (make <optimally-broken-page-node>
411 #:prev (if (null? best-paths)
414 #:lines current-lines
415 #:pageno this-page-num
417 #:configuration positions
418 #:penalty total-penalty)
421 ;; (display total-penalty) (newline)
425 "\nuser pen " user-penalty
426 "\nsatisfied-constraints" satisfied-constraints
427 "\nlast? " last? "ragged?" ragged?
428 "\nbetter? " better? " total-penalty " total-penalty "\n"
429 "\nconfig " positions
431 "\nlines: " current-lines "\n")))
434 (display (list "\nnew-best is " (node-lines new-best)
436 (if (null? best-paths)
438 (node-lines (car best-paths))))))
440 (if (and (pair? done-lines)
441 ;; if this page is too full, adding another line won't help
442 satisfied-constraints)
443 (walk-paths (cdr done-lines) (cdr best-paths)
444 (cons (car done-lines) current-lines)
448 (define (walk-lines done best-paths todo)
449 "Return the best page breaking as a single
450 <optimal-page-break-node> for optimally breaking TODO ++
451 DONE.reversed. BEST-PATHS is a list of break nodes corresponding to
455 (let* ((this-line (car todo))
456 (last? (null? (cdr todo)))
457 (next (walk-paths done best-paths (list this-line) last? #f)))
459 ;; (display "\n***************")
460 (walk-lines (cons this-line done)
461 (cons next best-paths)
464 (define (line-number node)
465 (ly:paper-system-property (car (node-lines node)) 'number))
467 (ly:message (_ "Calculating page breaks..."))
468 (set! force-equalization-factor
469 (ly:output-def-lookup paper 'verticalequalizationfactor 0.3))
471 (let* ((best-break-node (walk-lines '() '() lines))
472 (break-nodes (get-path best-break-node '()))
473 (last-node (car (last-pair break-nodes))))
475 (define (node->page-stencil node)
476 (if (not (eq? node last-node))
479 ((ly:output-def-lookup paper 'page-make-stencil)
481 (node-configuration node)
484 (node-page-number node)
485 (eq? node best-break-node))))
486 (if (not (eq? node last-node))
488 (ly:progress (number->string
489 (car (last-pair (node-system-numbers node)))))
493 (if #f; (ly:get-option 'verbose)
496 "\nbreaks: " (map line-number break-nodes))
497 "\nsystems " (map node-lines break-nodes)
498 "\npenalties " (map node-penalty break-nodes)
499 "\nconfigs " (map node-configuration break-nodes))))
501 (let ((stencils (map node->page-stencil break-nodes)))