2 ;; page.scm -- implement Page stuff.
4 ;; source file of the GNU LilyPond music typesetter
6 ;; (c) 2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 (define-module (scm page)
34 (define (annotate? layout)
35 (eq? #t (ly:output-def-lookup layout 'annotate-spacing)))
38 (define page-module (current-module))
40 (define (make-page paper-book . args)
42 ((p (apply ly:make-prob (append
43 (list 'page (layout->page-init (ly:paper-book-paper paper-book))
44 'paper-book paper-book)
47 (page-set-property! p 'head-stencil (page-header p))
48 (page-set-property! p 'foot-stencil (page-footer p))
52 (define page-property ly:prob-property)
53 (define page-set-property! ly:prob-set-property!)
54 (define (page-property? page sym)
55 (eq? #t (page-property page sym)))
56 (define (page? x) (ly:prob-type? x 'page))
64 (string->symbol (format "page-~a" j))
66 (page-property pg j))))
68 '(page-number prev lines force penalty lines))
70 (define (page-system-numbers page)
71 (map (lambda (ps) (ly:prob-property ps 'number))
74 (define (page-translate-systems page)
82 (if (not (number? (ly:prob-property sys 'Y-offset)))
83 (ly:prob-set-property! sys 'Y-offset off))))
85 (zip (page-property page 'lines)
86 (page-property page 'configuration))))
88 (define (annotate-page layout stencil)
89 (let ((top-margin (ly:output-def-lookup layout 'top-margin))
90 (paper-height (ly:output-def-lookup layout 'paper-height))
91 (bottom-margin (ly:output-def-lookup layout 'bottom-margin))
92 (add-stencil (lambda (y)
94 (ly:stencil-add stencil
95 (ly:stencil-translate-axis y 6 X))))))
97 (ly:stencil-translate-axis
98 (annotate-y-interval layout "paper-height"
99 (cons (- paper-height) 0)
103 (ly:stencil-translate-axis
104 (annotate-y-interval layout "top-margin"
105 (cons (- top-margin) 0)
109 (ly:stencil-translate-axis
110 (annotate-y-interval layout "bottom-margin"
111 (cons (- paper-height) (- bottom-margin paper-height))
116 (define (annotate-space-left page)
118 ((paper-book (page-property page 'paper-book))
119 (layout (ly:paper-book-paper paper-book))
120 (arrow (annotate-y-interval layout
123 (page-property page 'bottom-edge)
124 (let ((foot (page-property page 'foot-stencil)))
125 (if (and (ly:stencil? foot)
126 (not (ly:stencil-empty? foot)))
127 (car (ly:stencil-extent foot Y))
129 (page-property page 'bottom-system-edge))
132 (set! arrow (ly:stencil-translate-axis arrow 8 X))
139 (define (page-headfoot layout scopes number sym separation-symbol dir
140 is-last-bookpart is-bookpart-last-page)
142 "Create a stencil including separating space."
144 (let* ((header-proc (ly:output-def-lookup layout sym))
145 (sep (ly:output-def-lookup layout separation-symbol))
146 (stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
148 (if (procedure? header-proc)
149 (header-proc layout scopes number is-last-bookpart is-bookpart-last-page)
152 (if (and (number? sep)
153 (ly:stencil? head-stencil)
154 (not (ly:stencil-empty? head-stencil)))
158 (ly:stencil-combine-at-edge
159 stencil Y dir head-stencil
164 (if (or (annotate? layout)
165 (ly:output-def-lookup layout 'annotate-headers #f))
168 (ly:stencil-translate-axis
169 (annotate-y-interval layout
170 (symbol->string separation-symbol)
171 (cons (min 0 (* dir sep))
174 (/ (ly:output-def-lookup layout 'line-width) 2)
177 (ly:stencil-translate-axis
178 (annotate-y-interval layout
181 (- (min 0 (* dir sep))
182 (ly:output-def-lookup layout 'page-top-space))
185 (+ 7 (interval-center (ly:stencil-extent head-stencil X))) X)
194 (define (page-header-or-footer page dir)
196 ((paper-book (page-property page 'paper-book))
197 (layout (ly:paper-book-paper paper-book))
198 (scopes (ly:paper-book-scopes paper-book))
199 (number (page-page-number page))
200 (is-last-bookpart (page-property page 'is-last-bookpart))
201 (is-bookpart-last-page (page-property page 'is-bookpart-last-page)))
203 (page-headfoot layout scopes number
210 dir is-last-bookpart is-bookpart-last-page)))
212 (define (page-header page)
213 (page-header-or-footer page UP))
215 (define (page-footer page)
216 (page-header-or-footer page DOWN))
218 (define (layout->page-init layout)
219 "Alist of settings for page layout"
221 ((paper-height (ly:output-def-lookup layout 'paper-height))
222 (paper-width (ly:output-def-lookup layout 'paper-width))
223 (lmargin (ly:output-def-lookup layout 'left-margin #f))
224 (left-margin (if lmargin
227 (ly:output-def-lookup layout 'line-width)) 2)))
228 (bottom-edge (- paper-height
229 (ly:output-def-lookup layout 'bottom-margin)) )
230 (top-margin (ly:output-def-lookup layout 'top-margin))
233 `((paper-height . ,paper-height)
234 (paper-width . ,paper-width)
235 (left-margin . ,left-margin)
236 (top-margin . ,top-margin)
237 (bottom-edge . ,bottom-edge)
240 (define (make-page-stencil page)
241 "Construct a stencil representing the page from PAGE."
244 (page-translate-systems page)
246 ((paper-book (page-property page 'paper-book))
247 (prop (lambda (sym) (page-property page sym)))
248 (layout (ly:paper-book-paper paper-book))
249 (scopes (ly:paper-book-scopes paper-book))
250 (lines (page-lines page))
251 (number (page-page-number page))
253 ;; TODO: naming paper-height/paper-width not analogous to TeX.
255 (system-xoffset (ly:output-def-lookup layout 'horizontal-shift 0.0))
256 (system-separator-markup (ly:output-def-lookup layout 'system-separator-markup))
257 (system-separator-stencil (if (markup? system-separator-markup)
258 (interpret-markup layout
259 (layout-extract-page-properties layout)
260 system-separator-markup)
263 (head-height (if (ly:stencil? (prop 'head-stencil))
264 (interval-length (ly:stencil-extent (prop 'head-stencil) Y))
267 (page-stencil (ly:make-stencil '()))
271 (add-to-page (lambda (stencil x y)
273 (ly:stencil-add page-stencil
274 (ly:stencil-translate stencil
277 (- 0 head-height y (prop 'top-margin)))
282 (let* ((stencil (paper-system-stencil system))
283 (y (ly:prob-property system 'Y-offset))
284 (is-title (paper-system-title?
287 (ly:prob-property system 'X-offset 0.0)
289 (if (and (ly:stencil? system-separator-stencil)
291 (not (paper-system-title? system))
292 (not (paper-system-title? last-system)))
294 system-separator-stencil
297 (car (paper-system-staff-extents last-system)))
299 (cdr (paper-system-staff-extents system))))))
300 (set! last-system system)
302 (head (prop 'head-stencil))
303 (foot (prop 'foot-stencil))
307 (or (annotate? layout)
308 (ly:output-def-lookup layout 'annotate-systems #f))
312 (for-each (lambda (sys next-sys)
313 (paper-system-annotate sys next-sys layout))
315 (append (cdr lines) (list #f)))
316 (paper-system-annotate-last (car (last-pair lines)) layout)))
320 (not (ly:stencil-empty? head)))
322 (set! page-stencil (ly:stencil-add page-stencil
323 (ly:stencil-translate-axis head
324 (- 0 head-height (prop 'top-margin)) Y))))
326 (map add-system lines)
329 (ly:prob-set-property! page 'bottom-system-edge
330 (car (ly:stencil-extent page-stencil Y)))
331 (ly:prob-set-property! page 'space-left
332 (+ (prop 'bottom-edge)
333 (prop 'bottom-system-edge)
334 (if (and (ly:stencil? foot)
335 (not (ly:stencil-empty? foot)))
336 (car (ly:stencil-extent foot Y))
339 (if (annotate? layout)
341 (ly:stencil-add page-stencil
342 (annotate-space-left page))))
344 (if (and (ly:stencil? foot)
345 (not (ly:stencil-empty? foot)))
349 (ly:stencil-translate
352 (+ (- (prop 'bottom-edge))
353 (- (car (ly:stencil-extent foot Y)))))))))
356 (ly:stencil-translate page-stencil (cons (prop 'left-margin) 0)))
359 (if (or (annotate? layout)
360 (ly:output-def-lookup layout 'annotate-page #f))
361 (set! page-stencil (annotate-page layout page-stencil)))
367 (define-public (page-stencil page)
368 (if (not (ly:stencil? (page-property page 'stencil)))
370 ;; todo: make tweakable.
371 ;; via property + callbacks.
373 (page-set-property! page 'stencil (make-page-stencil page)))
374 (page-property page 'stencil))
376 (define (calc-printable-height page)
377 "Printable area for music and titles; matches default-page-make-stencil."
379 ((paper-book (page-property page 'paper-book))
380 (layout (ly:paper-book-paper paper-book))
381 (h (- (ly:output-def-lookup layout 'paper-height)
382 (ly:output-def-lookup layout 'top-margin)
383 (ly:output-def-lookup layout 'bottom-margin)))
385 (head (page-property page 'head-stencil))
386 (foot (page-property page 'foot-stencil))
388 (- h (if (ly:stencil? head)
389 (interval-length (ly:stencil-extent head Y))
391 (if (ly:stencil? foot)
392 (interval-length (ly:stencil-extent foot Y))
395 ;; (display (list "\n available" available head foot))
398 (define (page-printable-height page)
399 (if (not (number? (page-property page 'printable-height)))
400 (page-set-property! page 'printable-height (calc-printable-height page)))
402 (page-property page 'printable-height))