1 ;;; page-layout.scm -- page breaking and page layout
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2004 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 (penalty #:init-value 0 #:accessor node-penalty #:init-keyword #:penalty)
18 (lines #:init-value 0 #:accessor node-lines #:init-keyword #:lines))
20 (define-method (display (node <optimally-broken-page-node>) port)
21 (map (lambda (x) (display x port))
23 "Page " (node-page-number node)
24 " Lines: " (node-lines node)
25 " Penalty " (node-penalty node)
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
31 (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))
33 ;; TODO: take <optimally-broken-page-node> iso. page-number
34 ;; for all of these functions ?
36 (define-public (plain-header paper scopes page-number last?)
37 "Standard header for a part: page number --outside-- and instrument--centered."
39 (let* ((props (page-properties paper))
41 (if (ly:output-def-lookup paper 'printpagenumber)
42 (markup #:bold (number->string page-number))
45 (instr (ly:modules-lookup scopes 'instrument))
47 (line (list "" (if (markup? instr) instr "") pnum)))
49 (if (even? page-number)
50 (set! line (reverse line)))
54 paper props (make-fill-line-markup line))
59 ;; TODO: add publisher ID on non-first page.
60 (define-public (plain-footer paper scopes page-number last?)
61 "Standard footer. Empty, save for first (copyright) and last (tagline) page."
64 ((props (page-properties paper))
65 (copyright (ly:modules-lookup scopes 'copyright))
66 (tagline-var (ly:modules-lookup scopes 'tagline))
67 (tagline (if (markup? tagline-var) tagline-var TAGLINE))
72 (ly:stencil-combine-at-edge
73 stencil Y DOWN (interpret-markup paper props tagline)
77 (if (and (= 1 page-number)
81 (ly:stencil-combine-at-edge
82 stencil Y DOWN (interpret-markup paper props copyright)
88 (define (page-headfoot paper scopes number sym sepsym dir last?)
89 "Create a stencil including separating space."
91 ((header-proc (ly:output-def-lookup paper sym))
92 (sep (ly:output-def-lookup paper sepsym))
93 (stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
95 (if (procedure? header-proc)
96 (header-proc paper scopes number last?)
99 (if (and (number? sep) (ly:stencil? head-stencil))
101 (ly:stencil-combine-at-edge
102 stencil Y dir head-stencil
107 (define-public (default-page-music-height paper scopes number last?)
108 "Printable area for music and titles; matches default-page-make-stencil."
110 ((h (- (ly:output-def-lookup paper 'vsize)
111 (ly:output-def-lookup paper 'topmargin)
112 (ly:output-def-lookup paper 'bottommargin)))
113 (head (page-headfoot paper scopes number 'make-header 'headsep UP last?))
114 (foot (page-headfoot paper scopes number 'make-footer 'footsep DOWN last?)))
115 (- h (if (ly:stencil? head)
116 (interval-length (ly:stencil-extent head Y))
118 (if (ly:stencil? foot)
119 (interval-length (ly:stencil-extent foot Y))
124 (define-public (default-page-make-stencil lines paper scopes number last? )
125 "Construct a stencil representing the page from LINES. "
127 ((topmargin (ly:output-def-lookup paper 'topmargin))
129 ;; TODO: naming vsize/hsize not analogous to TeX.
132 (vsize (ly:output-def-lookup paper 'vsize))
133 (hsize (ly:output-def-lookup paper 'hsize))
135 (lmargin (ly:output-def-lookup paper 'leftmargin))
136 (leftmargin (if lmargin
139 (ly:output-def-lookup paper 'linewidth)) 2)))
141 (rightmargin (ly:output-def-lookup paper 'rightmargin))
142 (bottom-edge (- vsize
143 (ly:output-def-lookup paper 'bottommargin)))
145 (head (page-headfoot paper scopes number 'make-header 'headsep UP last?))
146 (foot (page-headfoot paper scopes number 'make-footer 'footsep DOWN last?))
147 (line-stencils (map ly:paper-system-stencil lines))
148 (height-proc (ly:output-def-lookup paper 'page-music-height))
149 (music-height (height-proc paper scopes number last?))
150 (ragged (ly:output-def-lookup paper 'raggedbottom))
151 (ragged-last (ly:output-def-lookup paper 'raggedlastbottom))
152 (ragged-bottom (or (eq? #t ragged)
153 (and last? (eq? #t ragged-last))))
155 (spc-left (- music-height
156 (apply + (map (lambda (x)
157 (interval-length (ly:stencil-extent x Y)))
159 (stretchable-lines (remove ly:paper-system-title? (cdr lines)))
160 (stretch (if (or (null? stretchable-lines)
161 (> spc-left (/ music-height 2))
164 (/ spc-left (length stretchable-lines))))
166 (page-stencil (ly:make-stencil '()
167 (cons leftmargin hsize)
168 (cons (- topmargin) 0)))
171 (set! page-stencil (ly:stencil-combine-at-edge
172 page-stencil Y DOWN head 0. 0.))
177 (ly:stencil-combine-at-edge
178 page-stencil Y DOWN (ly:paper-system-stencil l)
184 (set! was-title (ly:paper-system-title? l)))
187 (if (ly:stencil? foot)
191 (ly:stencil-translate
194 (+ (- bottom-edge) (- (car (ly:stencil-extent foot Y)))))
197 (ly:stencil-translate page-stencil (cons leftmargin 0))
203 ;;; optimal page breaking
205 ;;; This is not optimal page breaking, this is optimal distribution of
206 ;;; lines over pages; line breaks are a given.
211 ; - separate function for word-wrap style breaking?
212 ; - raggedbottom? raggedlastbottom?
214 (define-public (ly:optimal-page-breaks
216 "Return pages as a list starting with 1st page. Each page is a list
219 (define (make-node prev lines page-num penalty)
220 (make <optimally-broken-page-node>
226 (define MAXPENALTY 1e9)
227 (define bookpaper (ly:paper-book-book-paper paper-book))
228 (define scopes (ly:paper-book-scopes paper-book))
229 (define (line-height line)
230 (ly:paper-system-extent line Y))
232 ;; FIXME: may need some tweaking: square, cubic
233 (define (height-penalty available used)
235 (let* ((left (- available used))
237 (relative (abs (/ left available))))
240 ;; too full, penalise more
241 (* 10 (1+ relative) relative)
243 ;; Convexity: two half-empty pages is better than 1 completely
245 (* (1+ relative) relative))))
247 (define (page-height page-number last?)
249 ((p (ly:output-def-lookup bookpaper 'page-music-height)))
252 (p bookpaper scopes page-number last?)
256 (define (cumulative-height lines)
257 (apply + (map line-height lines)))
259 (define (get-path node done)
260 "Follow NODE.PREV, and return as an ascending list of pages. DONE
261 is what have collected so far, and has ascending page numbers."
262 (if (is-a? node <optimally-broken-page-node>)
263 (get-path (node-prev node) (cons node done))
266 (define (combine-penalties user page prev)
269 (define (walk-paths done-lines best-paths current-lines last? current-best)
270 "Return the best optimal-page-break-node that contains
271 CURRENT-LINES. DONE-LINES.reversed ++ CURRENT-LINES is a consecutive
272 ascending range of lines, and BEST-PATHS contains the optimal breaks
273 corresponding to DONE-LINES.
275 CURRENT-BEST is the best result sofar, or #f."
277 (let* ((this-page-num (if (null? best-paths)
279 (1+ (node-page-number (car best-paths)))))
280 (prev-penalty (if (null? best-paths)
282 (node-penalty (car best-paths))))
283 (page-height (page-height this-page-num last?))
284 (space-used (cumulative-height current-lines))
285 (this-page-penalty (height-penalty page-height space-used))
286 (user-penalty (ly:paper-system-break-penalty (car current-lines)))
287 (total-penalty (combine-penalties
288 user-penalty this-page-penalty prev-penalty))
291 (< total-penalty (node-penalty current-best))))
292 (new-best (if better?
293 (make-node (if (null? best-paths)
297 this-page-num total-penalty)
303 "user pen " user-penalty " prev-penalty "
305 "better? " better? " total-penalty " total-penalty "\n"
306 "height " page-height " spc used: " space-used "\n"
307 "pen " this-page-penalty " lines: " current-lines "\n")))
309 (if (and (pair? done-lines)
310 ;; if this page is too full, adding another line won't help
311 (< this-page-penalty MAXPENALTY))
312 (walk-paths (cdr done-lines) (cdr best-paths)
313 (cons (car done-lines) current-lines)
317 (define (walk-lines done best-paths todo)
318 "Return the best page breaking as a single
319 <optimal-page-break-node> for optimally breaking TODO ++
320 DONE.reversed. BEST-PATHS is a list of break nodes corresponding to
324 (let* ((this-line (car todo))
325 (last? (null? (cdr todo)))
326 (next (walk-paths done best-paths (list this-line) last? #f)))
328 (walk-lines (cons this-line done)
329 (cons next best-paths)
332 (define (line-number node)
333 (ly:paper-system-number (car (node-lines node))))
335 (let* ((best-break-node (walk-lines '() '() lines))
336 (break-nodes (get-path best-break-node '()))
339 (if (ly:get-option 'verbose)
341 (format (current-error-port) "breaks: ~S\n" (map line-number break-nodes))
342 (force-output (current-error-port))))
348 ((ly:output-def-lookup bookpaper 'page-make-stencil)
352 (node-page-number node)
353 (eq? node best-break-node)))