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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 (define (page-headfoot layout scopes number sym sepsym dir last?)
33 "Create a stencil including separating space."
34 (let* ((header-proc (ly:output-def-lookup layout sym))
35 (sep (ly:output-def-lookup layout sepsym))
36 (stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
38 (if (procedure? header-proc)
39 (header-proc layout scopes number last?)
42 (if (and (number? sep)
43 (ly:stencil? head-stencil)
44 (not (ly:stencil-empty? head-stencil)))
46 (ly:stencil-combine-at-edge
47 stencil Y dir head-stencil
52 (define-public (default-page-music-height layout scopes number last?)
53 "Printable area for music and titles; matches default-page-make-stencil."
54 (let* ((h (- (ly:output-def-lookup layout 'vsize)
55 (ly:output-def-lookup layout 'topmargin)
56 (ly:output-def-lookup layout 'bottommargin)))
57 (head (page-headfoot layout scopes number 'make-header 'headsep UP last?))
58 (foot (page-headfoot layout scopes number 'make-footer 'footsep DOWN last?))
60 (- h (if (ly:stencil? head)
61 (interval-length (ly:stencil-extent head Y))
63 (if (ly:stencil? foot)
64 (interval-length (ly:stencil-extent foot Y))
67 ;; (display (list "\n available" available head foot))
70 (define-public (default-page-make-stencil
71 lines offsets layout scopes number last?)
72 "Construct a stencil representing the page from LINES.
74 Offsets is a list of increasing numbers. They must be negated to
77 (let* ((topmargin (ly:output-def-lookup layout 'topmargin))
79 ;; TODO: naming vsize/hsize not analogous to TeX.
81 (vsize (ly:output-def-lookup layout 'vsize))
82 (hsize (ly:output-def-lookup layout 'hsize))
84 (system-separator-markup (ly:output-def-lookup layout 'systemSeparatorMarkup))
85 (system-separator-stencil (if (markup? system-separator-markup)
86 (interpret-markup layout
87 (page-properties layout)
88 system-separator-markup)
90 (lmargin (ly:output-def-lookup layout 'leftmargin))
91 (leftmargin (if lmargin
94 (ly:output-def-lookup layout 'linewidth)) 2)))
96 (rightmargin (ly:output-def-lookup layout 'rightmargin))
98 (ly:output-def-lookup layout 'bottommargin)))
100 (head (page-headfoot layout scopes number 'make-header 'headsep UP last?))
101 (foot (page-headfoot layout scopes number 'make-footer 'footsep DOWN last?))
103 (head-height (if (ly:stencil? head)
104 (interval-length (ly:stencil-extent head Y))
107 (height-proc (ly:output-def-lookup layout 'page-music-height))
109 (page-stencil (ly:make-stencil '()
110 (cons leftmargin hsize)
111 (cons (- topmargin) 0)))
114 (add-to-page (lambda (stencil y)
116 (ly:stencil-add page-stencil
117 (ly:stencil-translate-axis stencil
118 (- 0 head-height y topmargin) Y)))))
119 (add-system (lambda (stencil-position)
121 ((system (car stencil-position))
122 (stencil (ly:paper-system-stencil system))
123 (y (cadr stencil-position))
124 (is-title (ly:paper-system-title?
125 (car stencil-position))))
127 (add-to-page stencil y)
128 (if (and (ly:stencil? system-separator-stencil)
130 (not (ly:paper-system-title? system))
131 (not (ly:paper-system-title? last-system)))
132 (add-to-page system-separator-stencil
134 (car (ly:paper-system-staff-extents last-system)))
136 (cdr (ly:paper-system-staff-extents system))))))
137 (set! last-system system)
142 "leftmargin" leftmargin "rightmargin" rightmargin)))
144 (set! page-stencil (ly:stencil-combine-at-edge
145 page-stencil Y DOWN head 0. 0.))
147 (map add-system (zip lines offsets))
148 (if (ly:stencil? foot)
152 (ly:stencil-translate
156 (- (car (ly:stencil-extent foot Y)))))))))
158 (ly:stencil-translate page-stencil (cons leftmargin 0))))
160 ;;; optimal page breaking
162 ;;; This is not optimal page breaking, this is optimal distribution of
163 ;;; lines over pages; line breaks are a given.
168 ;; - separate function for word-wrap style breaking?
169 ;; - raggedbottom? raggedlastbottom?
171 (define-public (ly:optimal-page-breaks
173 "Return pages as a list starting with 1st page. Each page is a list
177 (define MAXPENALTY 1e9)
178 (define paper (ly:paper-book-paper paper-book))
179 (define scopes (ly:paper-book-scopes paper-book))
181 (define (page-height page-number last?)
182 (let ((p (ly:output-def-lookup paper 'page-music-height)))
185 (p paper scopes page-number last?)
188 (define (get-path node done)
189 "Follow NODE.PREV, and return as an ascending list of pages. DONE
190 is what have collected so far, and has ascending page numbers."
192 (if (is-a? node <optimally-broken-page-node>)
193 (get-path (node-prev node) (cons node done))
196 (define (combine-penalties force user best-paths)
197 (let* ((prev-force (if (null? best-paths)
199 (node-force (car best-paths))))
200 (prev-penalty (if (null? best-paths)
202 (node-penalty (car best-paths))))
203 (inter-system-space (ly:output-def-lookup paper 'betweensystemspace))
204 (force-equalization-factor 0.3)
205 (relative-force (/ force inter-system-space))
206 (abs-relative-force (abs relative-force)))
209 (+ (* abs-relative-force (+ abs-relative-force 1))
211 (* force-equalization-factor (/ (abs (- prev-force force)) inter-system-space))
214 (define (space-systems page-height lines ragged?)
215 (let* ((inter-system-space
216 (ly:output-def-lookup paper 'betweensystemspace))
217 (system-vector (list->vector
219 (if (= (length lines) 1)
224 (append (map ly:paper-system-staff-extents lines)
225 (if (= (length lines) 1)
232 (lambda (sys) (ly:paper-system-extent sys Y)) lines)
233 (if (= (length lines) 1)
236 (no-systems (vector-length real-extents))
237 (topskip (interval-end (vector-ref real-extents 0)))
238 (space-left (- page-height
239 (apply + (map interval-length (vector->list real-extents)))))
241 (space (- page-height
243 (- (interval-start (vector-ref real-extents (1- no-systems))))))
245 (fixed-dist (ly:output-def-lookup paper 'betweensystempadding))
248 (let* ((this-system-ext (vector-ref staff-extents idx))
249 (next-system-ext (vector-ref staff-extents (1+ idx)))
250 (fixed (max 0 (- (+ (interval-end next-system-ext)
252 (interval-start this-system-ext))))
253 (title1? (and (vector-ref system-vector idx)
254 (ly:paper-system-title? (vector-ref system-vector idx))))
256 (vector-ref system-vector (1+ idx))
257 (ly:paper-system-title? (vector-ref system-vector (1+ idx)))))
260 ((and title2? title1?)
261 (ly:output-def-lookup paper 'betweentitlespace))
263 (ly:output-def-lookup paper 'aftertitlespace))
265 (ly:output-def-lookup paper 'beforetitlespace))
266 (else inter-system-space))
268 (hooke (/ 1 (- ideal fixed))))
269 (list ideal hooke))))
271 (springs (map calc-spring (iota (1- no-systems))))
274 (let* ((this-system-ext (vector-ref real-extents idx))
275 (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- no-systems))))
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: " no-systems
299 "\nreal-ext" real-extents "\nstaff-ext" staff-extents
300 "\ninterscore" inter-system-space
301 "\nspace-letf" 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+ (node-page-number (car best-paths)))))
325 (ragged-all? (eq? #t (ly:output-def-lookup paper 'raggedbottom)))
326 (ragged-last? (eq? #t (ly:output-def-lookup paper 'raggedlastbottom)))
327 (ragged? (or ragged-all?
330 (page-height (page-height this-page-num last?))
331 (vertical-spacing (space-systems page-height current-lines ragged?))
332 (satisfied-constraints (car vertical-spacing))
333 (force (if satisfied-constraints
334 (if (and last? ragged-last?)
336 satisfied-constraints)
338 (positions (cdr vertical-spacing))
339 (user-nobreak-penalties
341 (apply + (filter negative?
342 (map ly:paper-system-break-before-penalty
343 (cdr current-lines))))))
346 (max (ly:paper-system-break-before-penalty (car current-lines)) 0.0)
347 user-nobreak-penalties))
348 (total-penalty (combine-penalties
355 (< total-penalty (node-penalty current-best))))
356 (new-best (if better?
357 (make <optimally-broken-page-node>
358 #:prev (if (null? best-paths)
361 #:lines current-lines
362 #:pageno this-page-num
364 #:configuration positions
365 #:penalty total-penalty)
371 "\nuser pen " user-penalty
372 "\nsatisfied-constraints" satisfied-constraints
373 "\nlast? " last? "ragged?" ragged?
374 "\nbetter? " better? " total-penalty " total-penalty "\n"
375 "\nconfig " positions
377 "\nlines: " current-lines "\n")))
380 (display (list "\nnew-best is " (node-lines new-best)
382 (if (null? best-paths)
384 (node-lines (car best-paths))))))
386 (if (and (pair? done-lines)
387 ;; if this page is too full, adding another line won't help
388 satisfied-constraints)
389 (walk-paths (cdr done-lines) (cdr best-paths)
390 (cons (car done-lines) current-lines)
394 (define (walk-lines done best-paths todo)
395 "Return the best page breaking as a single
396 <optimal-page-break-node> for optimally breaking TODO ++
397 DONE.reversed. BEST-PATHS is a list of break nodes corresponding to
401 (let* ((this-line (car todo))
402 (last? (null? (cdr todo)))
403 (next (walk-paths done best-paths (list this-line) last? #f)))
405 ;; (display "\n***************")
406 (walk-lines (cons this-line done)
407 (cons next best-paths)
410 (define (line-number node)
411 (ly:paper-system-number (car (node-lines node))))
413 (let* ((best-break-node (walk-lines '() '() lines))
414 (break-nodes (get-path best-break-node '())))
416 (if #f; (ly:get-option 'verbose)
419 "\nbreaks: " (map line-number break-nodes))
420 "\nsystems " (map node-lines break-nodes)
421 "\npenalties " (map node-penalty break-nodes)
422 "\nconfigs " (map node-configuration break-nodes))))
426 ((ly:output-def-lookup paper 'page-make-stencil)
428 (node-configuration node)
431 (node-page-number node)
432 (eq? node best-break-node)))