-;;;; (c) 2004--2005 Jan Nieuwenhuizen <janneke@gnu.org>
-;;;; Han-Wen Nienhuys <hanwen@cs.uu.nl>
-
-(use-modules (oop goops describe)
- (oop goops))
-
-
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-(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 (lambda (ps) (ly:paper-system-property ps 'number))
- (node-lines node)))
-
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-(define (annotate? layout)
- (eq? #t (ly:output-def-lookup layout 'annotatespacing)))
-
-
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-(define-public (paper-system-staff-extents ps)
- (ly:paper-system-property ps 'refpoint-Y-extent '(0 . 0)))
-
-
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-;; ANNOTATIONS
-;;
-;; annotations are arrows indicating the numerical value of
-;; spacing variables
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-(define (annotate-y-interval layout name extent is-length?)
- ;; do something sensible for 0,0 intervals.
- (set! extent (interval-widen extent 0.001))
- (let*
- ((text-props (cons
- '((font-size . -3)
- (font-family . typewriter))
- (layout-extract-page-properties layout)))
- (annotation (interpret-markup
- layout text-props
- (make-column-markup
- (list
- (make-whiteout-markup (make-simple-markup name))
- (make-whiteout-markup
- (make-simple-markup
- (if is-length?
- (format "~$" (interval-length extent))
- (format "(~$,~$)" (car extent)
- (cdr extent)))))))))
- (arrows
- (ly:stencil-translate-axis
- (dimension-arrows (cons 0 (interval-length extent)))
- (interval-start extent) Y)))
-
- (set! annotation
- (ly:stencil-aligned-to annotation Y CENTER))
-
- (set! annotation (ly:stencil-translate annotation
- (cons 0 (interval-center extent))))
-
- (set! annotation
- (ly:stencil-combine-at-edge arrows X RIGHT annotation 0.5 0))
-
- (set! annotation
- (ly:make-stencil (ly:stencil-expr annotation)
- (ly:stencil-extent annotation X)
- (cons 10000 -10000)))
- annotation))
-
-(define (paper-system-annotate-last system layout)
- (let*
- ((bottomspace (ly:paper-system-property system 'bottom-space))
- (y-extent (paper-system-extent system Y))
- (x-extent (paper-system-extent system X))
- (stencil (ly:paper-system-property system 'stencil))
-
- (arrow (if (number? bottomspace)
- (annotate-y-interval layout
- "bottom-space"
- (cons (- (car y-extent) bottomspace)
- (car y-extent))
- #t)
- #f)))
-
- (if arrow
- (set! stencil
- (ly:stencil-add stencil arrow)))
-
- (set! (ly:paper-system-property system 'stencil)
- stencil)
- ))
-
-(define (paper-system-annotate system layout)
- "Add arrows and texts to indicate which lengths are set."
- (let*
- ((annotations (ly:make-stencil '() (cons 0 2) (cons 0 0)))
- (append-stencil
- (lambda (a b)
- (ly:stencil-combine-at-edge a X RIGHT b 0.5 0)))
-
- (annotate-property
- (lambda (name extent is-length?)
- (set! annotations
- (append-stencil annotations
- (annotate-y-interval layout
- name extent is-length?)))))
-
- (bbox-extent (paper-system-extent system Y))
- (refp-extent (ly:paper-system-property system 'refpoint-Y-extent))
- (next-space (ly:paper-system-property system 'next-space
- (ly:output-def-lookup layout 'betweensystemspace)
- ))
- (next-padding (ly:paper-system-property system 'next-padding
- (ly:output-def-lookup layout 'betweensystempadding)
- ))
-
- )
-
- (if (number-pair? bbox-extent)
- (begin
- (annotate-property "Y-extent"
- bbox-extent #f)
- (annotate-property "next-padding"
- (interval-translate (cons (- next-padding) 0) (car bbox-extent))
- #t)))
-
- ;; titles don't have a refpoint-Y-extent.
- (if (number-pair? refp-extent)
- (begin
- (annotate-property "refpoint-Y-extent"
- refp-extent #f)
-
- (annotate-property "next-space"
- (interval-translate (cons (- next-space) 0) (car refp-extent))
- #t)))
-
-
-
- (set! (ly:paper-system-property system 'stencil)
- (ly:stencil-add
- (ly:paper-system-property system 'stencil)
- (ly:make-stencil
- (ly:stencil-expr annotations)
- (ly:stencil-extent empty-stencil X)
- (ly:stencil-extent empty-stencil Y)
- )))
-
- ))
-
-(define (annotate-page layout stencil)
- (let*
- ((topmargin (ly:output-def-lookup layout 'topmargin))
- (vsize (ly:output-def-lookup layout 'vsize))
- (bottommargin (ly:output-def-lookup layout 'bottommargin))
- (add-stencil (lambda (y)
- (set! stencil
- (ly:stencil-add stencil y))
- )))
-
- (add-stencil
- (ly:stencil-translate-axis
- (annotate-y-interval layout "vsize"
- (cons (- vsize) 0)
- #t)
- 1 X))
-
-
- (add-stencil
- (ly:stencil-translate-axis
- (annotate-y-interval layout "topmargin"
- (cons (- topmargin) 0)
- #t)
- 2 X))
-
- (add-stencil
- (ly:stencil-translate-axis
- (annotate-y-interval layout "bottommargin"
- (cons (- vsize) (- bottommargin vsize))
- #t)
- 2 X))
-
- stencil))
-
-(define (annotate-space-left page-stencil layout bottom-edge)
- (let*
- ((arrow (annotate-y-interval layout
- "space left"
- (cons (- bottom-edge) (car (ly:stencil-extent page-stencil Y)))
- #t)))
-
- (set! arrow (ly:stencil-translate-axis arrow 8 X))
- (ly:stencil-add page-stencil arrow)))
-
-\f
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
-
-(define (page-headfoot layout scopes number
- sym separation-symbol dir last?)
- "Create a stencil including separating space."
-
- (let* ((header-proc (ly:output-def-lookup layout sym))
- (sep (ly:output-def-lookup layout separation-symbol))
- (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)))
-
- (begin
- (set! head-stencil
- (ly:stencil-combine-at-edge
- stencil Y dir head-stencil
- sep 0.0))
-
-
- ;; add arrow markers
- (if (annotate? layout)
- (set! head-stencil
- (ly:stencil-add
- (ly:stencil-translate-axis
- (annotate-y-interval layout
- (symbol->string separation-symbol)
- (cons (min 0 (* dir sep))
- (max 0 (* dir sep)))
- #t)
- (/ (ly:output-def-lookup layout 'linewidth) 2)
- X)
- (if (= dir UP)
- (ly:stencil-translate-axis
- (annotate-y-interval layout
- "topspace"
- (cons
- (- (min 0 (* dir sep))
- (ly:output-def-lookup layout 'pagetopspace))
- (min 0 (* dir sep)))
- #t)
- (+ 7 (interval-center (ly:stencil-extent head-stencil X))) X)
- empty-stencil
- )
- head-stencil
- ))
- )))
-
- 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-xoffset (ly:output-def-lookup layout 'horizontalshift 0.0))
- (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 stencil
- (cons
- system-xoffset
- (- 0 head-height y topmargin))
-
- )))))
- (add-system
- (lambda (stencil-position)
- (let* ((system (car stencil-position))
- (stencil (paper-system-stencil system))
- (y (cadr stencil-position))
- (is-title (paper-system-title?
- (car stencil-position))))
- (add-to-page stencil y)
- (if (and (ly:stencil? system-separator-stencil)
- last-system
- (not (paper-system-title? system))
- (not (paper-system-title? last-system)))
- (add-to-page
- system-separator-stencil
- (average (- last-y
- (car (paper-system-staff-extents last-system)))
- (- y
- (cdr (paper-system-staff-extents system))))))
- (set! last-system system)
- (set! last-y y))))
- )
-
-
- (if (annotate? layout)
- (begin
- (for-each (lambda (sys) (paper-system-annotate sys layout))
- lines)
- (paper-system-annotate-last (car (last-pair lines)) layout)))
-
-
- (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 (annotate? layout)
- (set!
- page-stencil
- (annotate-space-left page-stencil layout
- (- bottom-edge
- (if (ly:stencil? foot)
- (interval-length (ly:stencil-extent foot Y))
- 0)))
- ))
-
-
- (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)))))))))
-
- (set! page-stencil
- (ly:stencil-translate page-stencil (cons leftmargin 0)))
-
- ;; annotation.
- (if (annotate? layout)
- (set! page-stencil (annotate-page layout page-stencil)))
-
-
- page-stencil))
-
-;;; optimal page breaking
-
-;;; This is not optimal page breaking, this is optimal distribution of
-;;; lines over pages; line breaks are a given.
+;;;; (c) 2004--2006 Jan Nieuwenhuizen <janneke@gnu.org>
+;;;; Han-Wen Nienhuys <hanwen@xs4all.nl>
+
+(define-module (scm layout-page-layout)
+ #:use-module (srfi srfi-1)
+ #:use-module (oop goops describe)
+ #:use-module (oop goops)
+ #:use-module (scm paper-system)
+ #:use-module (scm page)
+ #:use-module (scm layout-page-dump)
+ #:use-module (lily)
+ #:export (post-process-pages optimal-page-breaks make-page-from-systems
+ page-breaking-wrapper
+ ;; utilities for writing custom page breaking functions
+ line-height line-next-space line-next-padding
+ line-minimum-distance line-ideal-distance
+ first-line-position
+ line-ideal-relative-position line-minimum-relative-position
+ line-minimum-position-on-page stretchable-line?
+ page-maximum-space-to-fill page-maximum-space-left space-systems))
+
+(define (page-breaking-wrapper paper-book)
+ "Compute line and page breaks by calling the page-breaking paper variable,
+ then performs the post process function using the page-post-process paper
+ variable. Finally, return the pages."
+ (let* ((paper (ly:paper-book-paper paper-book))
+ (pages ((ly:output-def-lookup paper 'page-breaking) paper-book)))
+ ((ly:output-def-lookup paper 'page-post-process) paper pages)
+ pages))
+
+(define (post-process-pages layout pages)
+ "If the write-page-layout paper variable is true, dumps page breaks
+ and tweaks."
+ (if (ly:output-def-lookup layout 'write-page-layout #f)
+ (write-page-breaks pages)))
+
+;;;
+;;; Utilities for computing line distances and positions
+;;;
+(define (line-height line)
+ "Return the system height, that is the length of its vertical extent."
+ (interval-length (paper-system-extent line Y)))
+
+(define (line-next-space line next-line layout)
+ "Return space to use between `line' and `next-line'.
+ `next-line' can be #f, meaning that `line' is the last line."
+ (let* ((title (paper-system-title? line))
+ (next-title (and next-line (paper-system-title? next-line))))
+ (cond ((and title next-title)
+ (ly:output-def-lookup layout 'between-title-space))
+ (title
+ (ly:output-def-lookup layout 'after-title-space))
+ (next-title
+ (ly:output-def-lookup layout 'before-title-space))
+ (else
+ (ly:prob-property
+ line 'next-space
+ (ly:output-def-lookup layout 'between-system-space))))))
+
+(define (line-next-padding line next-line layout)
+ "Return padding to use between `line' and `next-line'.
+ `next-line' can be #f, meaning that `line' is the last line."
+ (ly:prob-property
+ line 'next-padding
+ (ly:output-def-lookup layout 'between-system-padding)))
+
+
+(define (line-minimum-distance line next-line layout ignore-padding)
+ "Minimum distance between `line' reference position and `next-line'
+ reference position. If next-line is #f, return #f."
+ (and next-line
+ (max 0 (- (+ (interval-end (paper-system-extent next-line Y))
+ (if ignore-padding 0 (line-next-padding line next-line layout)))
+ (interval-start (paper-system-extent line Y))))))
+
+(define (line-ideal-distance line next-line layout ignore-padding)
+ "Ideal distance between `line' reference position and `next-line'
+ reference position. If next-line is #f, return #f."
+ (and next-line
+ (+ (max 0 (- (+ (interval-end (paper-system-staff-extents next-line))
+ (if ignore-padding 0 (line-next-padding line next-line layout)))
+ (interval-start (paper-system-staff-extents line))))
+ (line-next-space line next-line layout))))
+
+(define (first-line-position line layout)
+ "Position of the first line on page"
+ (max (+ (ly:output-def-lookup layout 'page-top-space)
+ (interval-end (paper-system-staff-extents line)))
+ (interval-end (paper-system-extent line Y))))
+
+(define (line-ideal-relative-position line prev-line layout ignore-padding)
+ "Return ideal position of `line', relative to `prev-line' position.
+ `prev-line' can be #f, meaning that `line' is the first line."
+ (if (not prev-line)
+ ;; first line on page
+ (first-line-position line layout)
+ ;; not the first line on page
+ (max (line-minimum-distance prev-line line layout ignore-padding)
+ (line-ideal-distance prev-line line layout ignore-padding))))
+
+(define (line-minimum-relative-position line prev-line layout ignore-padding)
+ "Return position of `line', relative to `prev-line' position.
+ `prev-line' can be #f, meaning that `line' is the first line."
+ (if (not prev-line)
+ ;; first line on page
+ (first-line-position line layout)
+ ;; not the first line on page
+ (line-minimum-distance prev-line line layout ignore-padding)))
+
+(define (line-minimum-position-on-page line prev-line prev-position page)
+ "If `line' fits on `page' after `prev-line', which position on page is
+ `prev-position', then return the line's postion on page, otherwise #f.
+ `prev-line' can be #f, meaning that `line' is the first line."
+ (let* ((layout (ly:paper-book-paper (page-property page 'paper-book)))
+ (position (+ (line-minimum-relative-position line prev-line layout #f)
+ (if prev-line prev-position 0.0)))
+ (bottom-position (- position
+ (interval-start (paper-system-extent line Y)))))
+ (and (or (not prev-line)
+ (< bottom-position (page-printable-height page)))
+ position)))
+
+(define (stretchable-line? line)
+ "Say whether a system can be stretched."
+ (not (or (ly:prob-property? line 'is-title)
+ (let ((system-extent (paper-system-staff-extents line)))
+ (= (interval-start system-extent)
+ (interval-end system-extent))))))
+
+(define (page-maximum-space-to-fill page lines paper)
+ "Return the space between the first line top position and the last line
+ bottom position. This constitutes the maximum space to fill on `page'
+ with `lines'."
+ (let ((last-line (car (last-pair lines))))
+ (- (page-printable-height page)
+ (first-line-position (first lines) paper)
+ (ly:prob-property last-line
+ 'bottom-space 0.0)
+ (- (interval-start (paper-system-extent last-line Y))))))
+
+(define (page-maximum-space-left page)
+ (let ((paper (ly:paper-book-paper (page-property page 'paper-book))))
+ (let bottom-position ((lines (page-property page 'lines))
+ (prev-line #f)
+ (prev-position #f))
+ (if (null? lines)
+ (page-printable-height page)
+ (let* ((line (first lines))
+ (position (line-minimum-position-on-page
+ line prev-line prev-position page)))
+ (if (null? (cdr lines))
+ (and position
+ (- (page-printable-height page)
+ (- position
+ (interval-start (paper-system-extent line Y)))))
+ (bottom-position (cdr lines) line position)))))))
+
+;;;
+;;; Utilities for distributing systems on a page
+;;;
+
+(define (space-systems space-to-fill lines ragged paper ignore-padding)
+ "Compute lines positions on page: return force and line positions as a pair.
+ force is #f if lines do not fit on page."
+ (let* ((empty-stencil (ly:make-stencil '() '(0 . 0) '(0 . 0)))
+ (empty-prob (ly:make-prob 'paper-system (list `(stencil . ,empty-stencil))))
+ (cdr-lines (append (cdr lines)
+ (if (<= (length lines) 1)
+ (list empty-prob)
+ '())))
+ (springs (map (lambda (prev-line line)
+ (list (line-ideal-distance prev-line line paper ignore-padding)
+ (/ 1.0 (line-next-space prev-line line paper))))
+ lines
+ cdr-lines))
+ (rods (map (let ((i -1))
+ (lambda (prev-line line)
+ (set! i (1+ i))
+ (list i (1+ i)
+ (line-minimum-distance prev-line line paper ignore-padding))))
+ lines
+ cdr-lines))
+ (space-result
+ (ly:solve-spring-rod-problem springs rods space-to-fill ragged)))
+ (cons (car space-result)
+ (map (let ((topskip (first-line-position (first lines) paper)))
+ (lambda (y)
+ (+ y topskip)))
+ (cdr space-result)))))
+
+(define (make-page-from-systems paper-book lines page-number ragged last)
+ "Return a new page, filled with `lines'."
+ (let* ((page (make-page paper-book
+ 'lines lines
+ 'page-number page-number
+ 'is-last last))
+ (posns (if (null? lines)
+ (list)
+ (let* ((paper (ly:paper-book-paper paper-book))
+ (space-to-fill (page-maximum-space-to-fill
+ page lines paper))
+ (spacing (space-systems space-to-fill lines ragged paper #f)))
+ (if (or (not (car spacing)) (inf? (car spacing)))
+ (cdr (space-systems space-to-fill lines ragged paper #t))
+ (cdr spacing))))))
+ (page-set-property! page 'configuration posns)
+ page))
+
+;;;
+;;; Page breaking function
+;;;
+
+;; Optimal distribution of
+;; lines over pages; line breaks are a given.