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 (lambda (ps) (ly:paper-system-property ps 'number))
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 (define (annotate? layout)
37 (eq? #t (ly:output-def-lookup layout 'annotatespacing)))
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
42 (define-public (paper-system-staff-extents ps)
43 (ly:paper-system-property ps 'refpoint-Y-extent '(0 . 0)))
46 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
49 ;; annotations are arrows indicating the numerical value of
51 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
53 (define (annotate-y-interval layout name extent is-length?)
54 ;; do something sensible for 0,0 intervals.
55 (set! extent (interval-widen extent 0.001))
59 (font-family . typewriter))
60 (layout-extract-page-properties layout)))
61 (annotation (interpret-markup
65 (make-whiteout-markup (make-simple-markup name))
69 (format "~$" (interval-length extent))
70 (format "(~$,~$)" (car extent)
73 (ly:stencil-translate-axis
74 (dimension-arrows (cons 0 (interval-length extent)))
75 (interval-start extent) Y)))
78 (ly:stencil-aligned-to annotation Y CENTER))
80 (set! annotation (ly:stencil-translate annotation
81 (cons 0 (interval-center extent))))
83 (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5 0)
86 (define (paper-system-annotate-last system layout)
88 ((bottomspace (ly:paper-system-property system 'bottom-space))
89 (y-extent (paper-system-extent system Y))
90 (x-extent (paper-system-extent system X))
91 (stencil (ly:paper-system-property system 'stencil))
93 (arrow (if (number? bottomspace)
94 (annotate-y-interval layout
96 (cons (- (car y-extent) bottomspace)
103 (ly:stencil-add stencil arrow)))
105 (set! (ly:paper-system-property system 'stencil)
109 (define (paper-system-annotate system layout)
110 "Add arrows and texts to indicate which lengths are set."
112 ((annotations (ly:make-stencil '() (cons 0 2) (cons 0 0)))
115 (ly:stencil-combine-at-edge a X RIGHT b 0.5 0)))
118 (lambda (name extent is-length?)
120 (append-stencil annotations
121 (annotate-y-interval layout
122 name extent is-length?)))))
124 (bbox-extent (paper-system-extent system Y))
125 (refp-extent (ly:paper-system-property system 'refpoint-Y-extent))
126 (next-space (ly:paper-system-property system 'next-space
127 (ly:output-def-lookup layout 'betweensystemspace)
129 (next-padding (ly:paper-system-property system 'next-padding
130 (ly:output-def-lookup layout 'betweensystempadding)
135 (if (number-pair? bbox-extent)
137 (annotate-property "Y-extent"
139 (annotate-property "next-padding"
140 (interval-translate (cons (- next-padding) 0) (car bbox-extent))
143 ;; titles don't have a refpoint-Y-extent.
144 (if (number-pair? refp-extent)
146 (annotate-property "refpoint-Y-extent"
149 (annotate-property "next-space"
150 (interval-translate (cons (- next-space) 0) (car refp-extent))
155 (set! (ly:paper-system-property system 'stencil)
157 (ly:paper-system-property system 'stencil)
159 (ly:stencil-expr annotations)
160 (ly:stencil-extent empty-stencil X)
161 (ly:stencil-extent empty-stencil Y)
166 (define (annotate-page layout stencil)
168 ((topmargin (ly:output-def-lookup layout 'topmargin))
169 (vsize (ly:output-def-lookup layout 'vsize))
170 (bottommargin (ly:output-def-lookup layout 'bottommargin))
171 (add-stencil (lambda (y)
173 (ly:stencil-add stencil y))
177 (ly:stencil-translate-axis
178 (annotate-y-interval layout "vsize"
184 (ly:stencil-translate-axis
185 (annotate-y-interval layout "topmargin"
186 (cons (- topmargin) 0)
191 (ly:stencil-translate-axis
192 (annotate-y-interval layout "bottommargin"
193 (cons (- vsize) (- bottommargin vsize))
199 (define (annotate-space-left page-stencil layout bottom-edge)
201 ((arrow (annotate-y-interval layout
203 (cons (- bottom-edge) (car (ly:stencil-extent page-stencil Y)))
206 (set! arrow (ly:stencil-translate-axis arrow 8 X))
207 (ly:stencil-add page-stencil arrow)))
210 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
213 (define (page-headfoot layout scopes number
214 sym separation-symbol dir last?)
215 "Create a stencil including separating space."
217 (let* ((header-proc (ly:output-def-lookup layout sym))
218 (sep (ly:output-def-lookup layout separation-symbol))
219 (stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
221 (if (procedure? header-proc)
222 (header-proc layout scopes number last?)
226 (if (and (number? sep)
227 (ly:stencil? head-stencil)
228 (not (ly:stencil-empty? head-stencil)))
232 (ly:stencil-combine-at-edge
233 stencil Y dir head-stencil
238 (if (annotate? layout)
241 (ly:stencil-translate-axis
242 (annotate-y-interval layout
243 (symbol->string separation-symbol)
244 (cons (min 0 (* dir sep))
247 (/ (ly:output-def-lookup layout 'linewidth) 2)
255 (define-public (default-page-music-height layout scopes number last?)
256 "Printable area for music and titles; matches default-page-make-stencil."
257 (let* ((h (- (ly:output-def-lookup layout 'vsize)
258 (ly:output-def-lookup layout 'topmargin)
259 (ly:output-def-lookup layout 'bottommargin)))
261 (head (page-headfoot layout scopes number 'make-header 'headsep UP last?))
262 (foot (page-headfoot layout scopes number 'make-footer 'footsep DOWN last?))
264 (- h (if (ly:stencil? head)
265 (interval-length (ly:stencil-extent head Y))
267 (if (ly:stencil? foot)
268 (interval-length (ly:stencil-extent foot Y))
271 ;; (display (list "\n available" available head foot))
274 (define-public (default-page-make-stencil
275 lines offsets layout scopes number last?)
276 "Construct a stencil representing the page from LINES.
278 Offsets is a list of increasing numbers. They must be negated to
282 (let* ((topmargin (ly:output-def-lookup layout 'topmargin))
284 ;; TODO: naming vsize/hsize not analogous to TeX.
286 (vsize (ly:output-def-lookup layout 'vsize))
287 (hsize (ly:output-def-lookup layout 'hsize))
289 (system-xoffset (ly:output-def-lookup layout 'horizontalshift 0.0))
290 (system-separator-markup (ly:output-def-lookup layout 'systemSeparatorMarkup))
291 (system-separator-stencil (if (markup? system-separator-markup)
292 (interpret-markup layout
293 (layout-extract-page-properties layout)
294 system-separator-markup)
296 (lmargin (ly:output-def-lookup layout 'leftmargin))
297 (leftmargin (if lmargin
300 (ly:output-def-lookup layout 'linewidth)) 2)))
302 (rightmargin (ly:output-def-lookup layout 'rightmargin))
303 (bottom-edge (- vsize
304 (ly:output-def-lookup layout 'bottommargin)))
306 (head (page-headfoot layout scopes number 'make-header 'headsep UP last?))
307 (foot (page-headfoot layout scopes number 'make-footer 'footsep DOWN last?))
309 (head-height (if (ly:stencil? head)
310 (interval-length (ly:stencil-extent head Y))
313 (height-proc (ly:output-def-lookup layout 'page-music-height))
315 (page-stencil (ly:make-stencil '()
316 (cons leftmargin hsize)
317 (cons (- topmargin) 0)))
320 (add-to-page (lambda (stencil y)
322 (ly:stencil-add page-stencil
323 (ly:stencil-translate stencil
326 (- 0 head-height y topmargin))
330 (lambda (stencil-position)
331 (let* ((system (car stencil-position))
332 (stencil (paper-system-stencil system))
333 (y (cadr stencil-position))
334 (is-title (paper-system-title?
335 (car stencil-position))))
336 (add-to-page stencil y)
337 (if (and (ly:stencil? system-separator-stencil)
339 (not (paper-system-title? system))
340 (not (paper-system-title? last-system)))
342 system-separator-stencil
344 (car (paper-system-staff-extents last-system)))
346 (cdr (paper-system-staff-extents system))))))
347 (set! last-system system)
352 (if (annotate? layout)
354 (for-each (lambda (sys) (paper-system-annotate sys layout))
356 (paper-system-annotate-last (car (last-pair lines)) layout)))
361 "leftmargin " leftmargin "rightmargin " rightmargin
364 (set! page-stencil (ly:stencil-combine-at-edge
368 (not (ly:stencil-empty? head)))
370 (ly:make-stencil "" (cons 0 0) (cons 0 0)))
373 (map add-system (zip lines offsets))
375 (if (annotate? layout)
378 (annotate-space-left page-stencil layout
380 (if (ly:stencil? foot)
381 (interval-length (ly:stencil-extent foot Y))
386 (if (and (ly:stencil? foot)
387 (not (ly:stencil-empty? foot)))
391 (ly:stencil-translate
395 (- (car (ly:stencil-extent foot Y)))))))))
398 (ly:stencil-translate page-stencil (cons leftmargin 0)))
401 (if (annotate? layout)
402 (set! page-stencil (annotate-page layout page-stencil)))
407 ;;; optimal page breaking
409 ;;; This is not optimal page breaking, this is optimal distribution of
410 ;;; lines over pages; line breaks are a given.
415 ;; - separate function for word-wrap style breaking?
416 ;; - raggedbottom? raggedlastbottom?
418 (define-public (optimal-page-breaks lines paper-book)
419 "Return pages as a list starting with 1st page. Each page is a list
422 (define MAXPENALTY 1e9)
423 (define paper (ly:paper-book-paper paper-book))
424 (define scopes (ly:paper-book-scopes paper-book))
425 (define force-equalization-factor #f)
427 (define (page-height page-number last?)
428 (let ((p (ly:output-def-lookup paper 'page-music-height)))
431 (p paper scopes page-number last?)
434 (define (get-path node done)
435 "Follow NODE.PREV, and return as an ascending list of pages. DONE
436 is what have collected so far, and has ascending page numbers."
438 (if (is-a? node <optimally-broken-page-node>)
439 (get-path (node-prev node) (cons node done))
442 (define (combine-penalties force user best-paths)
443 (let* ((prev-force (if (null? best-paths)
445 (node-force (car best-paths))))
446 (prev-penalty (if (null? best-paths)
448 (node-penalty (car best-paths))))
449 (inter-system-space (ly:output-def-lookup paper 'betweensystemspace))
450 (relative-force (/ force inter-system-space))
451 (abs-relative-force (abs relative-force)))
454 (+ (* abs-relative-force (+ abs-relative-force 1))
456 (* force-equalization-factor (/ (abs (- prev-force force))
460 (define (space-systems page-height lines ragged?)
461 (let* ((global-inter-system-space
462 (ly:output-def-lookup paper 'betweensystemspace))
464 (ly:output-def-lookup paper 'pagetopspace))
465 (global-fixed-dist (ly:output-def-lookup paper 'betweensystempadding))
467 (system-vector (list->vector
469 (if (= (length lines) 1)
474 (append (map paper-system-staff-extents lines)
475 (if (= (length lines) 1)
483 (lambda (sys) (paper-system-extent sys Y)) lines)
484 (if (= (length lines) 1)
488 (system-count (vector-length real-extents))
492 (interval-end (vector-ref staff-extents 0)))
493 (interval-end (vector-ref real-extents 0))
495 (last-system (vector-ref system-vector (1- system-count)))
496 (bottom-space (if (ly:paper-system? last-system)
497 (ly:paper-system-property last-system 'bottom-space 0.0)
499 (space-left (- page-height
501 (apply + (map interval-length
502 (vector->list real-extents)))))
504 (space (- page-height
508 (vector-ref real-extents (1- system-count))))))
513 (upper-system (vector-ref system-vector idx))
514 (between-space (ly:paper-system-property upper-system 'next-space
515 global-inter-system-space))
516 (fixed-dist (ly:paper-system-property upper-system 'next-padding
519 (this-system-ext (vector-ref staff-extents idx))
520 (next-system-ext (vector-ref staff-extents (1+ idx)))
521 (fixed (max 0 (- (+ (interval-end next-system-ext)
523 (interval-start this-system-ext))))
524 (title1? (and (vector-ref system-vector idx)
525 (paper-system-title? (vector-ref system-vector idx)
528 (vector-ref system-vector (1+ idx))
529 (paper-system-title? (vector-ref system-vector (1+ idx)))))
532 ((and title2? title1?)
533 (ly:output-def-lookup paper 'betweentitlespace))
535 (ly:output-def-lookup paper 'aftertitlespace))
537 (ly:output-def-lookup paper 'beforetitlespace))
538 (else between-space))
540 (hooke (/ 1 (- ideal fixed))))
541 (list ideal hooke))))
543 (springs (map calc-spring (iota (1- system-count))))
547 (upper-system (vector-ref system-vector idx))
548 (fixed-dist (ly:paper-system-property upper-system 'next-padding
550 (this-system-ext (vector-ref real-extents idx))
551 (next-system-ext (vector-ref real-extents (1+ idx)))
553 (distance (max (- (+ (interval-end next-system-ext)
555 (interval-start this-system-ext)
557 (entry (list idx (1+ idx) distance)))
559 (rods (map calc-rod (iota (1- system-count))))
561 ;; we don't set ragged based on amount space left.
562 ;; raggedbottomlast = ##T is much more predictable
563 (result (ly:solve-spring-rod-problem
575 (display (list "\n# systems: " system-count
576 "\nreal-ext" real-extents "\nstaff-ext" staff-extents
577 "\ninterscore" global-inter-system-space
578 "\nspace-left" space-left
579 "\nspring,rod" springs rods
582 "\npage-height" page-height
586 "\npositions" positions "\n"))))
588 (cons force positions)))
590 (define (walk-paths done-lines best-paths current-lines last? current-best)
591 "Return the best optimal-page-break-node that contains
592 CURRENT-LINES. DONE-LINES.reversed ++ CURRENT-LINES is a consecutive
593 ascending range of lines, and BEST-PATHS contains the optimal breaks
594 corresponding to DONE-LINES.
596 CURRENT-BEST is the best result sofar, or #f."
599 (let* ((this-page-num (if (null? best-paths)
600 (ly:output-def-lookup paper 'firstpagenumber)
601 (1+ (node-page-number (car best-paths)))))
603 (ragged-all? (eq? #t (ly:output-def-lookup paper 'raggedbottom)))
604 (ragged-last? (eq? #t (ly:output-def-lookup paper 'raggedlastbottom)))
605 (ragged? (or ragged-all?
608 (page-height (page-height this-page-num last?))
609 (vertical-spacing (space-systems page-height current-lines ragged?))
610 (satisfied-constraints (car vertical-spacing))
611 (force (if satisfied-constraints
612 (if (and last? ragged-last?)
614 satisfied-constraints)
616 (positions (cdr vertical-spacing))
617 (get-break-penalty (lambda (sys)
618 (ly:paper-system-property sys 'penalty 0.0)))
619 (user-nobreak-penalties
621 (apply + (filter negative?
622 (map get-break-penalty
623 (cdr current-lines))))))
626 (max (get-break-penalty (car current-lines)) 0.0)
627 user-nobreak-penalties))
629 (total-penalty (combine-penalties
635 (< total-penalty (node-penalty current-best))))
636 (new-best (if better?
637 (make <optimally-broken-page-node>
638 #:prev (if (null? best-paths)
641 #:lines current-lines
642 #:pageno this-page-num
644 #:configuration positions
645 #:penalty total-penalty)
648 ;; (display total-penalty) (newline)
652 "\nuser pen " user-penalty
653 "\nsatisfied-constraints" satisfied-constraints
654 "\nlast? " last? "ragged?" ragged?
655 "\nbetter? " better? " total-penalty " total-penalty "\n"
656 "\nconfig " positions
658 "\nlines: " current-lines "\n")))
661 (display (list "\nnew-best is " (node-lines new-best)
663 (if (null? best-paths)
665 (node-lines (car best-paths))))))
667 (if (and (pair? done-lines)
668 ;; if this page is too full, adding another line won't help
669 satisfied-constraints)
670 (walk-paths (cdr done-lines) (cdr best-paths)
671 (cons (car done-lines) current-lines)
675 (define (walk-lines done best-paths todo)
676 "Return the best page breaking as a single
677 <optimal-page-break-node> for optimally breaking TODO ++
678 DONE.reversed. BEST-PATHS is a list of break nodes corresponding to
683 (let* ((this-line (car todo))
684 (last? (null? (cdr todo)))
685 (next (walk-paths done best-paths (list this-line) last? #f)))
687 ;; (display "\n***************")
688 (walk-lines (cons this-line done)
689 (cons next best-paths)
692 (define (line-number node)
693 (ly:paper-system-property (car (node-lines node)) 'number))
695 (ly:message (_ "Calculating page breaks..."))
696 (set! force-equalization-factor
697 (ly:output-def-lookup paper 'verticalequalizationfactor 0.3))
699 (let* ((best-break-node (walk-lines '() '() lines))
700 (break-nodes (get-path best-break-node '()))
701 (last-node (car (last-pair break-nodes))))
703 (define (node->page-stencil node)
704 (if (not (eq? node last-node))
707 ((ly:output-def-lookup paper 'page-make-stencil)
709 (node-configuration node)
712 (node-page-number node)
713 (eq? node best-break-node))))
714 (if (not (eq? node last-node))
716 (ly:progress (number->string
717 (car (last-pair (node-system-numbers node)))))
721 (if #f; (ly:get-option 'verbose)
724 "\nbreaks: " (map line-number break-nodes))
725 "\nsystems " (map node-lines break-nodes)
726 "\npenalties " (map node-penalty break-nodes)
727 "\nconfigs " (map node-configuration break-nodes))))
729 (let ((stencils (map node->page-stencil break-nodes)))