+(define-class <optimally-broken-page-node> ()
+ (prev #:init-value '() #:accessor node-prev #:init-keyword #:prev)
+ (page #:init-value 0 #:accessor node-page-number #:init-keyword #:pageno)
+ (force #:init-value 0 #:accessor node-force #:init-keyword #:force)
+ (penalty #:init-value 0 #:accessor node-penalty #:init-keyword #:penalty)
+ (configuration #:init-value '() #:accessor node-configuration #:init-keyword #:configuration)
+ (lines #:init-value 0 #:accessor node-lines #:init-keyword #:lines))
+
+(define-method (display (node <optimally-broken-page-node>) port)
+ (map (lambda (x) (display x port))
+ (list
+ "Page " (node-page-number node)
+ " Lines: " (node-lines node)
+ " Penalty " (node-penalty node)
+ "\n")))
+
+(define-method (node-system-numbers (node <optimally-broken-page-node>))
+ (map ly:paper-system-number (node-lines node)))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(define (page-headfoot layout scopes number sym sepsym dir last?)
+ "Create a stencil including separating space."
+ (let* ((header-proc (ly:output-def-lookup layout sym))
+ (sep (ly:output-def-lookup layout sepsym))
+ (stencil (ly:make-stencil "" '(0 . 0) '(0 . 0)))
+ (head-stencil
+ (if (procedure? header-proc)
+ (header-proc layout scopes number last?)
+ #f)))
+
+ (if (and (number? sep)
+ (ly:stencil? head-stencil)
+ (not (ly:stencil-empty? head-stencil)))
+ (set! head-stencil
+ (ly:stencil-combine-at-edge
+ stencil Y dir head-stencil
+ sep 0.0)))
+
+ head-stencil))
+
+(define-public (default-page-music-height layout scopes number last?)
+ "Printable area for music and titles; matches default-page-make-stencil."
+ (let* ((h (- (ly:output-def-lookup layout 'vsize)
+ (ly:output-def-lookup layout 'topmargin)
+ (ly:output-def-lookup layout 'bottommargin)))
+ (head (page-headfoot layout scopes number 'make-header 'headsep UP last?))
+ (foot (page-headfoot layout scopes number 'make-footer 'footsep DOWN last?))
+ (available
+ (- h (if (ly:stencil? head)
+ (interval-length (ly:stencil-extent head Y))
+ 0)
+ (if (ly:stencil? foot)
+ (interval-length (ly:stencil-extent foot Y))
+ 0))))
+
+ ;; (display (list "\n available" available head foot))
+ available))
+
+(define-public (default-page-make-stencil
+ lines offsets layout scopes number last?)
+ "Construct a stencil representing the page from LINES.
+
+ Offsets is a list of increasing numbers. They must be negated to
+create offsets.
+ "
+
+ (let* ((topmargin (ly:output-def-lookup layout 'topmargin))
+
+ ;; TODO: naming vsize/hsize not analogous to TeX.
+
+ (vsize (ly:output-def-lookup layout 'vsize))
+ (hsize (ly:output-def-lookup layout 'hsize))
+
+ (system-separator-markup (ly:output-def-lookup layout 'systemSeparatorMarkup))
+ (system-separator-stencil (if (markup? system-separator-markup)
+ (interpret-markup layout
+ (layout-extract-page-properties layout)
+ system-separator-markup)
+ #f))
+ (lmargin (ly:output-def-lookup layout 'leftmargin))
+ (leftmargin (if lmargin
+ lmargin
+ (/ (- hsize
+ (ly:output-def-lookup layout 'linewidth)) 2)))
+
+ (rightmargin (ly:output-def-lookup layout 'rightmargin))
+ (bottom-edge (- vsize
+ (ly:output-def-lookup layout 'bottommargin)))
+
+ (head (page-headfoot layout scopes number 'make-header 'headsep UP last?))
+ (foot (page-headfoot layout scopes number 'make-footer 'footsep DOWN last?))
+
+ (head-height (if (ly:stencil? head)
+ (interval-length (ly:stencil-extent head Y))
+ 0.0))
+
+ (height-proc (ly:output-def-lookup layout 'page-music-height))
+
+ (page-stencil (ly:make-stencil '()
+ (cons leftmargin hsize)
+ (cons (- topmargin) 0)))
+ (last-system #f)
+ (last-y 0.0)
+ (add-to-page (lambda (stencil y)
+ (set! page-stencil
+ (ly:stencil-add page-stencil
+ (ly:stencil-translate-axis stencil
+ (- 0 head-height y topmargin) Y)))))
+ (add-system
+ (lambda (stencil-position)
+ (let* ((system (car stencil-position))
+ (stencil (ly:paper-system-stencil system))
+ (y (cadr stencil-position))
+ (is-title (ly:paper-system-title?
+ (car stencil-position))))
+ (add-to-page stencil y)
+ (if (and (ly:stencil? system-separator-stencil)
+ last-system
+ (not (ly:paper-system-title? system))
+ (not (ly:paper-system-title? last-system)))
+ (add-to-page
+ system-separator-stencil
+ (average (- last-y
+ (car (ly:paper-system-staff-extents last-system)))
+ (- y
+ (cdr (ly:paper-system-staff-extents system))))))
+ (set! last-system system)
+ (set! last-y y)))))
+
+ (if #f
+ (display (list
+ "leftmargin " leftmargin "rightmargin " rightmargin
+ )))
+
+ (set! page-stencil (ly:stencil-combine-at-edge
+ page-stencil Y DOWN
+ (if (and
+ (ly:stencil? head)
+ (not (ly:stencil-empty? head)))
+ head
+ (ly:make-stencil "" (cons 0 0) (cons 0 0)))
+ 0. 0.))
+
+ (map add-system (zip lines offsets))
+ (if (and (ly:stencil? foot)
+ (not (ly:stencil-empty? foot)))
+ (set! page-stencil
+ (ly:stencil-add
+ page-stencil
+ (ly:stencil-translate
+ foot
+ (cons 0
+ (+ (- bottom-edge)
+ (- (car (ly:stencil-extent foot Y)))))))))
+
+ (ly:stencil-translate page-stencil (cons leftmargin 0))))