]> git.donarmstrong.com Git - lilypond.git/blob - scm/page-layout.scm
* scm/page-layout.scm (plain-header): add printpagenumber boolean
[lilypond.git] / scm / page-layout.scm
1 ;;; page-layout.scm -- page breaking and page layout
2 ;;;
3 ;;;;  source file of the GNU LilyPond music typesetter
4 ;;;;
5 ;;;; (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
6 ;;;;          Han-Wen Nienhuys <hanwen@cs.uu.nl>
7
8 (use-modules (oop goops describe)
9              (oop goops))
10
11
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
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))
19
20 (define-method (display (node <optimally-broken-page-node>) port)
21   (map (lambda (x) (display x port))
22        (list
23         "Page " (node-page-number node)
24         " Lines: " (node-lines node)
25         " Penalty " (node-penalty node)
26         "\n")))
27
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29
30 (define TAGLINE
31   (string-append "Engraved by LilyPond (version " (lilypond-version) ")"))
32
33 ;; TODO: take <optimally-broken-page-node> iso. page-number
34 ;; for all of these functions ?
35
36 (define-public (plain-header paper scopes page-number last?)
37   "Standard header for a part: page number --outside--  and instrument--centered."
38
39   (let* ((props (page-properties paper))
40          (pnum
41           (if (ly:output-def-lookup paper 'printpagenumber)
42               (markup #:bold (number->string page-number))
43               ""
44               ))
45          (instr (ly:modules-lookup scopes 'instrument))
46          
47          (line (list "" (if (markup? instr) instr "") pnum)))
48
49     (if (even? page-number)
50         (set! line (reverse line)))
51
52     (if (< 1 page-number)
53         (interpret-markup
54          paper props (make-fill-line-markup line))
55         '())
56     ))
57
58
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."
62   
63   (let*
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))
68        (stencil #f))
69
70     (if last?
71         (set! stencil
72               (ly:stencil-combine-at-edge
73                stencil Y DOWN (interpret-markup paper props tagline)
74                0.0
75                )))
76
77     (if (and (= 1 page-number)
78              (markup? copyright))
79
80         (set! stencil
81               (ly:stencil-combine-at-edge
82                stencil Y DOWN (interpret-markup paper props copyright)
83                0.0
84                )))
85
86     stencil))
87   
88 (define (page-headfoot paper scopes number sym sepsym dir last?)
89   "Create a stencil including separating space."
90   (let*
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)))
94        (head-stencil
95         (if (procedure? header-proc)
96             (header-proc paper scopes number last?)
97             #f)))
98
99     (if (and (number? sep) (ly:stencil? head-stencil))
100         (set! head-stencil
101               (ly:stencil-combine-at-edge
102                stencil Y  dir head-stencil
103                sep 0.0)))
104
105     head-stencil))
106
107 (define-public (default-page-music-height paper scopes number last?)
108   "Printable area for music and titles; matches default-page-make-stencil." 
109   (let*
110       ((h (- (ly:output-def-lookup paper 'vsize)
111              (ly:output-def-lookup paper 'top-margin)
112              (ly:output-def-lookup paper 'bottom-margin)))
113        (head (page-headfoot paper scopes number 'make-header 'head-sep UP last?))
114        (foot (page-headfoot paper scopes number 'make-footer 'foot-sep DOWN last?)))
115     (- h (if (ly:stencil? head)
116              (interval-length (ly:stencil-extent head Y))
117              0)
118        (if (ly:stencil? foot)
119            (interval-length (ly:stencil-extent foot Y))
120            0))
121     ))
122
123
124 (define-public (default-page-make-stencil lines paper scopes number last? )
125   "Construct a stencil representing the page from LINES.  "
126   (let*
127      ((top-margin  (ly:output-def-lookup paper 'top-margin))
128       
129       ;; TODO: naming vsize/hsize not analogous to TeX.
130       
131       (hsize (ly:output-def-lookup paper 'hsize))
132       (left-margin (/ (- hsize
133                          (ly:output-def-lookup paper 'linewidth)) 2))
134       (vsize (ly:output-def-lookup paper 'vsize))
135       (bottom-edge (- vsize
136                       (ly:output-def-lookup paper 'bottom-margin)))
137                      
138       (head (page-headfoot paper scopes number 'make-header 'head-sep UP last?))
139       (foot (page-headfoot paper scopes number 'make-footer 'foot-sep DOWN last?))
140       (line-stencils (map ly:paper-system-stencil lines))
141       (height-proc (ly:output-def-lookup paper 'page-music-height))
142       (music-height (height-proc paper scopes number last?))
143       (ragged (ly:output-def-lookup paper 'raggedbottom))
144       (ragged-last   (ly:output-def-lookup paper 'raggedlastbottom))
145       (ragged-bottom (or (eq? #t ragged)
146                          (and last? (eq? #t ragged-last))))
147
148       (spc-left (-  music-height
149                    (apply + (map (lambda (x)
150                                    (interval-length (ly:stencil-extent x Y)))
151                         line-stencils))))
152       (stretchable-lines (remove ly:paper-system-title? (cdr lines)))
153       (stretch (if (or (null? stretchable-lines)
154                        (> spc-left (/ music-height 2))
155                        ragged-bottom)
156                    0.0
157                    (/ spc-left (length stretchable-lines))))
158
159       (page-stencil (ly:make-stencil '()
160                     (cons left-margin hsize)
161                     (cons (- top-margin) 0)))
162       (was-title #t))
163
164     (set! page-stencil (ly:stencil-combine-at-edge
165           page-stencil Y DOWN head 0. 0.))
166
167     (for-each
168      (lambda (l)
169        (set! page-stencil
170              (ly:stencil-combine-at-edge
171               page-stencil Y DOWN (ly:paper-system-stencil l)
172               (if was-title
173                   0.0
174                   stretch)
175               ))
176
177        (set! was-title (ly:paper-system-title? l)))
178      lines)
179
180     (if (ly:stencil? foot)
181         (set! page-stencil
182               (ly:stencil-add
183                page-stencil
184                (ly:stencil-translate
185                 foot
186                 (cons 0
187                       (+ (- bottom-edge) (- (car (ly:stencil-extent foot Y)))))
188                 ))))
189
190     (ly:stencil-translate page-stencil (cons left-margin 0))
191   ))
192   
193
194
195
196 ;;; optimal page breaking
197
198 ;;; This is not optimal page breaking, this is optimal distribution of
199 ;;; lines over pages; line breaks are a given.
200
201 ; TODO:
202 ;
203 ; - density scoring
204 ; - separate function for word-wrap style breaking?
205 ; - raggedbottom? raggedlastbottom? 
206
207 (define-public (ly:optimal-page-breaks
208                 lines paper-book)
209   "Return pages as a list starting with 1st page. Each page is a list
210 of lines. "
211
212   (define (make-node prev lines page-num penalty)
213     (make <optimally-broken-page-node>
214       #:prev prev
215       #:lines lines
216       #:pageno page-num
217       #:penalty penalty))
218
219   (define MAXPENALTY 1e9)
220   (define bookpaper (ly:paper-book-book-paper paper-book))
221   (define scopes (ly:paper-book-scopes paper-book))
222   (define (line-height line)
223     (ly:paper-system-extent line Y))
224
225   ;; FIXME: may need some tweaking: square, cubic
226   (define (height-penalty available used)
227     ;; FIXME, simplistic
228     (let* ((left (- available used))
229            ;; scale-independent
230            (relative (abs (/ left available))))
231       (if (negative? left)
232
233           ;; too full, penalise more
234           (* 10 (1+ relative) relative)
235           
236           ;; Convexity: two half-empty pages is better than 1 completely
237           ;; empty page
238           (* (1+ relative) relative))))
239
240   (define (page-height page-number last?)
241     (let
242         ((p (ly:output-def-lookup bookpaper 'page-music-height)))
243
244       (if (procedure? p)
245           (p bookpaper scopes page-number last?)
246           10000)))
247
248   
249   (define (cumulative-height lines)
250     (apply + (map line-height lines)))
251
252   (define (get-path node done)
253     "Follow NODE.PREV, and return as an ascending list of pages. DONE
254 is what have collected so far, and has ascending page numbers."
255     (if (is-a? node <optimally-broken-page-node>)
256         (get-path (node-prev node) (cons node done))
257         done))
258
259   (define (combine-penalties user page prev)
260     (+ prev page user))
261
262   (define (walk-paths done-lines best-paths current-lines  last? current-best)
263     "Return the best optimal-page-break-node that contains
264 CURRENT-LINES.  DONE-LINES.reversed ++ CURRENT-LINES is a consecutive
265 ascending range of lines, and BEST-PATHS contains the optimal breaks
266 corresponding to DONE-LINES.
267
268 CURRENT-BEST is the best result sofar, or #f."
269
270     (let* ((this-page-num (if (null? best-paths)
271                               1
272                               (1+ (node-page-number (car best-paths)))))
273            (prev-penalty (if (null? best-paths)
274                              0.0
275                              (node-penalty (car best-paths))))
276            (page-height (page-height this-page-num last?))
277            (space-used (cumulative-height current-lines))
278            (this-page-penalty (height-penalty  page-height space-used))
279            (user-penalty (ly:paper-system-break-penalty (car current-lines)))
280            (total-penalty (combine-penalties
281                            user-penalty this-page-penalty prev-penalty))
282            (better? (or
283                      (not current-best)
284                      (< total-penalty (node-penalty current-best))))
285            (new-best (if better?
286                          (make-node (if (null? best-paths)
287                                         #f
288                                         (car best-paths))
289                                     current-lines
290                                     this-page-num total-penalty)
291                          current-best)))
292
293       (if #f ;; debug
294           (display
295            (list
296             "user pen " user-penalty " prev-penalty "
297             prev-penalty "\n"
298             "better? " better? " total-penalty " total-penalty "\n"
299             "height " page-height " spc used: " space-used "\n"
300             "pen " this-page-penalty " lines: " current-lines "\n")))
301
302       (if (and (pair? done-lines)
303                ;; if this page is too full, adding another line won't help
304                (< this-page-penalty MAXPENALTY))
305           (walk-paths (cdr done-lines) (cdr best-paths)
306                       (cons (car done-lines) current-lines)
307                       last? new-best)
308           new-best)))
309
310   (define (walk-lines done best-paths todo)
311     "Return the best page breaking as a single
312 <optimal-page-break-node> for optimally breaking TODO ++
313 DONE.reversed. BEST-PATHS is a list of break nodes corresponding to
314 DONE."
315     (if (null? todo)
316         (car best-paths)
317         (let* ((this-line (car todo))
318                (last? (null? (cdr todo)))
319                (next (walk-paths done best-paths (list this-line) last? #f)))
320         
321           (walk-lines (cons this-line done)
322                       (cons next best-paths)
323                       (cdr todo)))))
324
325   (define (line-number node)
326     (ly:paper-system-number (car (node-lines node))))
327
328   (let* ((best-break-node (walk-lines '() '() lines))
329          (break-nodes (get-path best-break-node '()))
330          )
331
332     (if (ly:get-option 'verbose)
333         (begin
334           (format (current-error-port) "breaks: ~S\n" (map line-number break-nodes))
335           (force-output (current-error-port))))
336
337     
338     ; create stencils.
339     
340     (map (lambda (node)
341            ((ly:output-def-lookup bookpaper 'page-make-stencil)
342             (node-lines node)
343             bookpaper
344             scopes
345             (node-page-number node)
346             (eq? node best-break-node)))
347          break-nodes)))
348
349