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-number (node-lines node)))
33 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
35 (define (page-headfoot layout scopes number sym sepsym dir last?)
36 "Create a stencil including separating space."
37 (let* ((header-proc (ly:output-def-lookup layout sym))
38 (sep (ly:output-def-lookup layout sepsym))
39 (stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
41 (if (procedure? header-proc)
42 (header-proc layout scopes number last?)
45 (if (and (number? sep)
46 (ly:stencil? head-stencil)
47 (not (ly:stencil-empty? head-stencil)))
49 (ly:stencil-combine-at-edge
50 stencil Y dir head-stencil
55 (define-public (default-page-music-height layout scopes number last?)
56 "Printable area for music and titles; matches default-page-make-stencil."
57 (let* ((h (- (ly:output-def-lookup layout 'vsize)
58 (ly:output-def-lookup layout 'topmargin)
59 (ly:output-def-lookup layout 'bottommargin)))
60 (head (page-headfoot layout scopes number 'make-header 'headsep UP last?))
61 (foot (page-headfoot layout scopes number 'make-footer 'footsep DOWN last?))
63 (- h (if (ly:stencil? head)
64 (interval-length (ly:stencil-extent head Y))
66 (if (ly:stencil? foot)
67 (interval-length (ly:stencil-extent foot Y))
70 ;; (display (list "\n available" available head foot))
73 (define-public (default-page-make-stencil
74 lines offsets layout scopes number last?)
75 "Construct a stencil representing the page from LINES.
77 Offsets is a list of increasing numbers. They must be negated to
81 (let* ((topmargin (ly:output-def-lookup layout 'topmargin))
83 ;; TODO: naming vsize/hsize not analogous to TeX.
85 (vsize (ly:output-def-lookup layout 'vsize))
86 (hsize (ly:output-def-lookup layout 'hsize))
88 (system-separator-markup (ly:output-def-lookup layout 'systemSeparatorMarkup))
89 (system-separator-stencil (if (markup? system-separator-markup)
90 (interpret-markup layout
91 (layout-extract-page-properties layout)
92 system-separator-markup)
94 (lmargin (ly:output-def-lookup layout 'leftmargin))
95 (leftmargin (if lmargin
98 (ly:output-def-lookup layout 'linewidth)) 2)))
100 (rightmargin (ly:output-def-lookup layout 'rightmargin))
101 (bottom-edge (- vsize
102 (ly:output-def-lookup layout 'bottommargin)))
104 (head (page-headfoot layout scopes number 'make-header 'headsep UP last?))
105 (foot (page-headfoot layout scopes number 'make-footer 'footsep DOWN last?))
107 (head-height (if (ly:stencil? head)
108 (interval-length (ly:stencil-extent head Y))
111 (height-proc (ly:output-def-lookup layout 'page-music-height))
113 (page-stencil (ly:make-stencil '()
114 (cons leftmargin hsize)
115 (cons (- topmargin) 0)))
118 (add-to-page (lambda (stencil y)
120 (ly:stencil-add page-stencil
121 (ly:stencil-translate-axis stencil
122 (- 0 head-height y topmargin) Y)))))
124 (lambda (stencil-position)
125 (let* ((system (car stencil-position))
126 (stencil (ly:paper-system-stencil system))
127 (y (cadr stencil-position))
128 (is-title (ly:paper-system-title?
129 (car stencil-position))))
130 (add-to-page stencil y)
131 (if (and (ly:stencil? system-separator-stencil)
133 (not (ly:paper-system-title? system))
134 (not (ly:paper-system-title? last-system)))
136 system-separator-stencil
138 (car (ly:paper-system-staff-extents last-system)))
140 (cdr (ly:paper-system-staff-extents system))))))
141 (set! last-system system)
146 "leftmargin " leftmargin "rightmargin " rightmargin
149 (set! page-stencil (ly:stencil-combine-at-edge
153 (not (ly:stencil-empty? head)))
155 (ly:make-stencil "" (cons 0 0) (cons 0 0)))
158 (map add-system (zip lines offsets))
159 (if (and (ly:stencil? foot)
160 (not (ly:stencil-empty? foot)))
164 (ly:stencil-translate
168 (- (car (ly:stencil-extent foot Y)))))))))
170 (ly:stencil-translate page-stencil (cons leftmargin 0))))
172 ;;; optimal page breaking
174 ;;; This is not optimal page breaking, this is optimal distribution of
175 ;;; lines over pages; line breaks are a given.
180 ;; - separate function for word-wrap style breaking?
181 ;; - raggedbottom? raggedlastbottom?
183 (define-public (ly:optimal-page-breaks
185 "Return pages as a list starting with 1st page. Each page is a list
189 (define MAXPENALTY 1e9)
190 (define paper (ly:paper-book-paper paper-book))
191 (define scopes (ly:paper-book-scopes paper-book))
193 (define (page-height page-number last?)
194 (let ((p (ly:output-def-lookup paper 'page-music-height)))
197 (p paper scopes page-number last?)
200 (define (get-path node done)
201 "Follow NODE.PREV, and return as an ascending list of pages. DONE
202 is what have collected so far, and has ascending page numbers."
204 (if (is-a? node <optimally-broken-page-node>)
205 (get-path (node-prev node) (cons node done))
208 (define (combine-penalties force user best-paths)
209 (let* ((prev-force (if (null? best-paths)
211 (node-force (car best-paths))))
212 (prev-penalty (if (null? best-paths)
214 (node-penalty (car best-paths))))
215 (inter-system-space (ly:output-def-lookup paper 'betweensystemspace))
216 (force-equalization-factor 0.3)
217 (relative-force (/ force inter-system-space))
218 (abs-relative-force (abs relative-force)))
221 (+ (* abs-relative-force (+ abs-relative-force 1))
223 (* force-equalization-factor (/ (abs (- prev-force force)) inter-system-space))
226 (define (space-systems page-height lines ragged?)
227 (let* ((inter-system-space
228 (ly:output-def-lookup paper 'betweensystemspace))
229 (system-vector (list->vector
231 (if (= (length lines) 1)
236 (append (map ly:paper-system-staff-extents lines)
237 (if (= (length lines) 1)
244 (lambda (sys) (ly:paper-system-extent sys Y)) lines)
245 (if (= (length lines) 1)
248 (no-systems (vector-length real-extents))
249 (topskip (interval-end (vector-ref real-extents 0)))
250 (space-left (- page-height
251 (apply + (map interval-length (vector->list real-extents)))))
253 (space (- page-height
255 (- (interval-start (vector-ref real-extents (1- no-systems))))))
257 (fixed-dist (ly:output-def-lookup paper 'betweensystempadding))
260 (let* ((this-system-ext (vector-ref staff-extents idx))
261 (next-system-ext (vector-ref staff-extents (1+ idx)))
262 (fixed (max 0 (- (+ (interval-end next-system-ext)
264 (interval-start this-system-ext))))
265 (title1? (and (vector-ref system-vector idx)
266 (ly:paper-system-title? (vector-ref system-vector idx))))
268 (vector-ref system-vector (1+ idx))
269 (ly:paper-system-title? (vector-ref system-vector (1+ idx)))))
272 ((and title2? title1?)
273 (ly:output-def-lookup paper 'betweentitlespace))
275 (ly:output-def-lookup paper 'aftertitlespace))
277 (ly:output-def-lookup paper 'beforetitlespace))
278 (else inter-system-space))
280 (hooke (/ 1 (- ideal fixed))))
281 (list ideal hooke))))
283 (springs (map calc-spring (iota (1- no-systems))))
286 (let* ((this-system-ext (vector-ref real-extents idx))
287 (next-system-ext (vector-ref real-extents (1+ idx)))
288 (distance (max (- (+ (interval-end next-system-ext)
290 (interval-start this-system-ext)
292 (entry (list idx (1+ idx) distance)))
294 (rods (map calc-rod (iota (1- no-systems))))
296 ;; we don't set ragged based on amount space left.
297 ;; raggedbottomlast = ##T is much more predictable
298 (result (ly:solve-spring-rod-problem
310 (display (list "\n# systems: " no-systems
311 "\nreal-ext" real-extents "\nstaff-ext" staff-extents
312 "\ninterscore" inter-system-space
313 "\nspace-letf" space-left
314 "\nspring,rod" springs rods
317 "\npage-height" page-height
321 "\npositions" positions "\n"))))
323 (cons force positions)))
325 (define (walk-paths done-lines best-paths current-lines last? current-best)
326 "Return the best optimal-page-break-node that contains
327 CURRENT-LINES. DONE-LINES.reversed ++ CURRENT-LINES is a consecutive
328 ascending range of lines, and BEST-PATHS contains the optimal breaks
329 corresponding to DONE-LINES.
331 CURRENT-BEST is the best result sofar, or #f."
333 (let* ((this-page-num (if (null? best-paths)
334 (ly:output-def-lookup paper 'firstpagenumber)
335 (1+ (node-page-number (car best-paths)))))
337 (ragged-all? (eq? #t (ly:output-def-lookup paper 'raggedbottom)))
338 (ragged-last? (eq? #t (ly:output-def-lookup paper 'raggedlastbottom)))
339 (ragged? (or ragged-all?
342 (page-height (page-height this-page-num last?))
343 (vertical-spacing (space-systems page-height current-lines ragged?))
344 (satisfied-constraints (car vertical-spacing))
345 (force (if satisfied-constraints
346 (if (and last? ragged-last?)
348 satisfied-constraints)
350 (positions (cdr vertical-spacing))
351 (user-nobreak-penalties
353 (apply + (filter negative?
354 (map ly:paper-system-break-before-penalty
355 (cdr current-lines))))))
358 (max (ly:paper-system-break-before-penalty (car current-lines)) 0.0)
359 user-nobreak-penalties))
360 (total-penalty (combine-penalties
367 (< total-penalty (node-penalty current-best))))
368 (new-best (if better?
369 (make <optimally-broken-page-node>
370 #:prev (if (null? best-paths)
373 #:lines current-lines
374 #:pageno this-page-num
376 #:configuration positions
377 #:penalty total-penalty)
383 "\nuser pen " user-penalty
384 "\nsatisfied-constraints" satisfied-constraints
385 "\nlast? " last? "ragged?" ragged?
386 "\nbetter? " better? " total-penalty " total-penalty "\n"
387 "\nconfig " positions
389 "\nlines: " current-lines "\n")))
392 (display (list "\nnew-best is " (node-lines new-best)
394 (if (null? best-paths)
396 (node-lines (car best-paths))))))
398 (if (and (pair? done-lines)
399 ;; if this page is too full, adding another line won't help
400 satisfied-constraints)
401 (walk-paths (cdr done-lines) (cdr best-paths)
402 (cons (car done-lines) current-lines)
406 (define (walk-lines done best-paths todo)
407 "Return the best page breaking as a single
408 <optimal-page-break-node> for optimally breaking TODO ++
409 DONE.reversed. BEST-PATHS is a list of break nodes corresponding to
413 (let* ((this-line (car todo))
414 (last? (null? (cdr todo)))
415 (next (walk-paths done best-paths (list this-line) last? #f)))
417 ;; (display "\n***************")
418 (walk-lines (cons this-line done)
419 (cons next best-paths)
422 (define (line-number node)
423 (ly:paper-system-number (car (node-lines node))))
425 (ly:message (_ "Calculating page breaks..."))
427 (let* ((best-break-node (walk-lines '() '() lines))
428 (break-nodes (get-path best-break-node '()))
429 (last-node (car (last-pair break-nodes))))
431 (define (node->page-stencil node)
432 (if (not (eq? node last-node))
435 ((ly:output-def-lookup paper 'page-make-stencil)
437 (node-configuration node)
440 (node-page-number node)
441 (eq? node best-break-node))))
442 (if (not (eq? node last-node))
444 (ly:progress (number->string
445 (car (last-pair (node-system-numbers node)))))
449 (if #f; (ly:get-option 'verbose)
452 "\nbreaks: " (map line-number break-nodes))
453 "\nsystems " (map node-lines break-nodes)
454 "\npenalties " (map node-penalty break-nodes)
455 "\nconfigs " (map node-configuration break-nodes))))
457 (let ((stencils (map node->page-stencil break-nodes)))