]> git.donarmstrong.com Git - lilypond.git/blobdiff - scm/layout-page-layout.scm
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / scm / layout-page-layout.scm
index 1dcf9033e579deed4317404921e924efecb02b3b..8a7628af857519d7c1dc0ec0bb8c718db029ab27 100644 (file)
             (scm page)
             )
 
+
 (define (write-page-breaks pages) 
   "Dump page breaks"
 
-  
-  (define tweaks '())
+  (define tweaks (make-hash-table 23))
+
+  (define (record what property-pairs)
+    (let*
+       ((key (ly:output-def-lookup (ly:grob-layout what)
+                                   'tweak-key
+                                   "tweaks"
+                                   ))
+        (when (ly:grob-property what 'when))
+        )
 
-  (define (record when property-pairs)
-    (set! tweaks
-         (acons when property-pairs
-                tweaks)))
+      (if (not (hash-ref tweaks key))
+         (hash-set! tweaks key '()))
+
+      (hash-set! tweaks key
+                (acons when property-pairs
+                       (hash-ref tweaks key)))
+      
+      ))
+
+  (define (graceless-moment mom)
+    (ly:make-moment
+     (ly:moment-main-numerator mom)
+     (ly:moment-main-denominator mom)
+     0 0))
 
   (define (moment->skip mom)
-    (format "s1*~a/~a"
-           (ly:moment-main-numerator mom)
-           (ly:moment-main-denominator mom)))
-  
+    (let*
+       ((main (if (> (ly:moment-main-numerator mom) 0)
+                  (format "\\skip 1*~a/~a"
+                          (ly:moment-main-numerator mom)
+                          (ly:moment-main-denominator mom))
+                  ""))
+        (grace (if (< (ly:moment-grace-numerator mom) 0)
+                   (format "\\grace { \\skip 1*~a/~a }"
+                           (- (ly:moment-grace-numerator mom))
+                           (ly:moment-grace-denominator mom))
+                   "")))
+
+      (format "~a~a" main grace)))
+    
   (define (dump-tweaks out-port tweak-list last-moment)
     (if (not (null? tweak-list))
        (let*
             (diff (ly:moment-sub now last-moment))
             (these-tweaks (cdar tweak-list))
             (skip (moment->skip diff))
-
-            (base (format "\\overrideProperty
-       #\"Score.NonMusicalPaperColumn\"
-       #'line-break-system-details
-        #'~a" these-tweaks))
+            (line-break-str (if (assoc-get 'line-break these-tweaks #f)
+                                "\\break\n"
+                                ""))
+            (page-break-str (if (assoc-get 'page-break these-tweaks #f)
+                                "\\pageBreak\n"
+                                ""))
+            (space-tweaks (format "\\spacingTweaks #'~a\n"
+                                  (with-output-to-string
+                                    (lambda ()
+                                      (pretty-print
+                                  
+                                       (assoc-get 'spacing-parameters these-tweaks '()))))
+                                    ))
+            (base (format "~a~a~a"
+                          line-break-str
+                          page-break-str
+                          space-tweaks))
             )
 
-         (format out-port "\\skip ~a\n~a\n" skip base)
-         (dump-tweaks out-port (cdr tweak-list) now)
-       )
+         (format out-port "~a\n~a\n" skip base)
+         (dump-tweaks out-port (cdr tweak-list) (graceless-moment now))
+       )))
+
+  (define (dump-all-tweaks)
+    (let*
+     ((paper (ly:paper-book-paper (page-property  (car pages) 'paper-book)))
+      (parser (ly:output-def-parser paper))
+      (name  (format "~a-page-layout.ly"
+                    (ly:parser-output-name parser)))
+      (out-port (open-output-file name)))
+      
+     (ly:progress "Writing page layout to ~a" name)
+     (hash-for-each
+      (lambda (key val)
+       (format out-port "~a = {" key)
+       (dump-tweaks out-port (reverse val) (ly:make-moment 0 1))
+        (display "}" out-port))
+       tweaks)
+     (close-port out-port)
+     ))
 
-       ))
-  
   (define (handle-page page)
+    (define index 0)
+    (define music-system-heights
+      (map-in-order (lambda (sys)
+                     (* -1 (car (paper-system-extent sys Y))))
+                   (remove (lambda (sys)
+                             (ly:prob-property? sys 'is-title))
+                           (page-lines page))))
     (define (handle-system sys)
       (let*
-         ((props '((line-break . #t))))
+         ((props `((line-break . #t)
+                   (spacing-parameters
+                    . ((system-Y-extent . ,(paper-system-extent sys Y))
+                       (system-refpoint-Y-extent . ,(paper-system-staff-extents sys))
+                       (system-index . ,index)
+                       (music-system-heights . ,music-system-heights)
+                       (page-system-count . ,(length (page-lines page)))
+                       (page-printable-height . ,(page-printable-height page)) 
+                       (page-space-left . ,(page-property page 'space-left))))
+                   )))
 
        (if (equal? (car (page-lines page)) sys)
            (set! props (cons '(page-break . #t)
                              props)))
-
        (if (not (ly:prob-property? sys 'is-title))
-           (record (ly:grob-property (ly:spanner-bound (ly:prob-property sys 'system-grob) LEFT) 'when)
-                 props))
+           (record  (ly:spanner-bound (ly:prob-property sys 'system-grob) LEFT)
+                    props))
+
+       (set! index (1+ index))
        ))
     (for-each handle-system (page-lines page)))
-  
-  
-  (for-each handle-page pages)
-
-  (let*
-      ((out-port (open-output-file "breaks.ly")))
-
-    (display "{" out-port)
-    (dump-tweaks out-port (reverse tweaks) (ly:make-moment 0 1))
-    (display "}" out-port)
-  ))
-
 
+  (for-each handle-page pages)
+  (dump-all-tweaks))
 
 (define (post-process-pages layout pages)
-  (if (ly:get-option 'write-page-layout)
+  (if (ly:output-def-lookup layout 'write-page-layout #f)
       (write-page-breaks pages)))
 
-
-
-;;; optimal page breaking
-
-;;; This is not optimal page breaking, this is optimal distribution of
-;;; lines over pages; line breaks are a given.
+;; Optimal distribution of
+;; lines over pages; line breaks are a given.
 
 ;; TODO:
 ;;
 ;; - density scoring
 ;; - separate function for word-wrap style breaking?
-;; - raggedbottom? raggedlastbottom?
+;; - ragged-bottom? ragged-last-bottom?
 
 (define-public (optimal-page-breaks lines paper-book)
   "Return pages as a list starting with 1st page. Each page is a 'page Prob."
   (define scopes (ly:paper-book-scopes paper-book))
   (define force-equalization-factor #f)
   (define (get-path node done)
+    
     "Follow NODE.PREV, and return as an ascending list of pages. DONE
 is what have collected so far, and has ascending page numbers."
 
@@ -117,7 +178,7 @@ is what have collected so far, and has ascending page numbers."
           (prev-penalty (if (null? best-paths)
                             0.0
                             (page-penalty (car best-paths))))
-        (inter-system-space (ly:output-def-lookup paper 'betweensystemspace))
+        (inter-system-space (ly:output-def-lookup paper 'between-system-space))
         (relative-force (/ force inter-system-space))
         (abs-relative-force (abs relative-force)))
 
@@ -129,10 +190,10 @@ is what have collected so far, and has ascending page numbers."
 
   (define (space-systems page-height lines ragged?)
     (let* ((global-inter-system-space
-           (ly:output-def-lookup paper 'betweensystemspace))
+           (ly:output-def-lookup paper 'between-system-space))
           (top-space
-           (ly:output-def-lookup paper 'pagetopspace))
-          (global-fixed-dist (ly:output-def-lookup paper 'betweensystempadding))
+           (ly:output-def-lookup paper 'page-top-space))
+          (global-fixed-dist (ly:output-def-lookup paper 'between-system-padding))
           
           (system-vector (list->vector
                           (append lines
@@ -200,11 +261,11 @@ is what have collected so far, and has ascending page numbers."
                     (ideal (+
                             (cond
                              ((and title2? title1?)
-                              (ly:output-def-lookup paper 'betweentitlespace))
+                              (ly:output-def-lookup paper 'between-title-space))
                              (title1?
-                              (ly:output-def-lookup paper 'aftertitlespace))
+                              (ly:output-def-lookup paper 'after-title-space))
                              (title2?
-                              (ly:output-def-lookup paper 'beforetitlespace))
+                              (ly:output-def-lookup paper 'before-title-space))
                              (else between-space))
                             fixed))
                     (hooke (/ 1 (- ideal fixed))))
@@ -229,7 +290,7 @@ is what have collected so far, and has ascending page numbers."
           (rods (map calc-rod (iota (1- system-count))))
 
           ;; we don't set ragged based on amount space left.
-          ;; raggedbottomlast = ##T is much more predictable
+          ;; ragged-bottomlast = ##T is much more predictable
           (result (ly:solve-spring-rod-problem
                    springs rods space
                    ragged?))
@@ -257,7 +318,7 @@ is what have collected so far, and has ascending page numbers."
 
       (cons force positions)))
 
-  (define (walk-paths done-lines best-paths current-lines  last? current-best)
+  (define (walk-paths done-lines best-paths current-lines last? current-best)
     "Return the best optimal-page-break-node that contains
 CURRENT-LINES.  DONE-LINES.reversed ++ CURRENT-LINES is a consecutive
 ascending range of lines, and BEST-PATHS contains the optimal breaks
@@ -265,9 +326,8 @@ corresponding to DONE-LINES.
 
 CURRENT-BEST is the best result sofar, or #f."
 
-
     (let* ((this-page-num (if (null? best-paths)
-                              (ly:output-def-lookup paper 'firstpagenumber)
+                              (ly:output-def-lookup paper 'first-page-number)
                               (1+ (page-page-number (car best-paths)))))
 
           (this-page (make-page
@@ -276,13 +336,14 @@ CURRENT-BEST is the best result sofar, or #f."
                       'is-last last?
                       'page-number this-page-num))
 
-          (ragged-all? (eq? #t (ly:output-def-lookup paper 'raggedbottom)))
-          (ragged-last? (eq? #t (ly:output-def-lookup paper 'raggedlastbottom)))
+          (ragged-all? (eq? #t (ly:output-def-lookup paper 'ragged-bottom)))
+          (ragged-last? (eq? #t (ly:output-def-lookup paper 'ragged-last-bottom)))
           (ragged? (or ragged-all?
                        (and ragged-last?
                             last?)))
            (height (page-printable-height this-page))
           (vertical-spacing (space-systems height current-lines ragged?))
+          
           (satisfied-constraints (car vertical-spacing))
            (force (if satisfied-constraints
                      (if (and last? ragged-last?)
@@ -306,10 +367,12 @@ CURRENT-BEST is the best result sofar, or #f."
                            force user-penalty
                           best-paths))
 
-           (better? (or
-                     (not current-best)
-                     (< total-penalty (page-penalty current-best))))
-           (new-best (if better?
+           (is-better (or
+                      (not current-best)
+                      (and
+                       satisfied-constraints
+                       (< total-penalty (page-penalty current-best)))))
+           (new-best (if is-better
                         (begin
                           (map
                            (lambda (x)
@@ -334,7 +397,7 @@ CURRENT-BEST is the best result sofar, or #f."
             "\nuser pen " user-penalty
            "\nsatisfied-constraints" satisfied-constraints
            "\nlast? " last? "ragged?" ragged?
-            "\nbetter? " better? " total-penalty " total-penalty "\n"
+            "\nis-better " is-better " total-penalty " total-penalty "\n"
            "\nconfig " positions
             "\nforce " force
            "\nlines: " current-lines "\n")))
@@ -379,8 +442,7 @@ DONE."
        (ly:output-def-lookup paper 'verticalequalizationfactor 0.3))
   
   (let* ((best-break-node (walk-lines '() '() lines))
-        (break-nodes (get-path best-break-node '()))
-        )
+        (break-nodes (get-path best-break-node '())))
 
     (page-set-property! (car (last-pair break-nodes)) 'is-last #t)
     (if #f; (ly:get-option 'verbose)