lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Re: feature-request / doc-actualization (right-margin)


From: Michael Käppler
Subject: [PATCH] Re: feature-request / doc-actualization (right-margin)
Date: Tue, 11 Aug 2009 18:15:01 +0200
User-agent: Thunderbird 2.0.0.12 (X11/20071114)

Hi all,
here is the first draft of a patch concerning the margin settings for review. (I would like to upload it on Rietveld but I don't know how)
The goals are:

- make default margin settings accessible from *.ly stuff (now in paper-defaults-init.ly)
- set line-width automatically depending on margin settings
- calculate missing values from given values (left-margin, right-margin, line-width)

Cheers,
Michael
>From 9778370158e39bfe9e0a0015e234adcf1cab7e3e Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Michael=20K=C3=A4ppler?= <address@hidden>
Date: Tue, 11 Aug 2009 18:01:01 +0200
Subject: [PATCH] Implement new handling for margin and line-width settings.

---
 lily/include/output-def.hh |    3 +-
 lily/output-def-scheme.cc  |   13 +++++++
 lily/output-def.cc         |   76 ++++++++++++++++++++++++++++++++++++++++++++
 ly/paper-defaults-init.ly  |    4 ++
 scm/framework-ps.scm       |    7 ++--
 scm/page.scm               |   15 +++-----
 scm/paper.scm              |   14 ++------
 scm/titling.scm            |    6 ++--
 8 files changed, 112 insertions(+), 26 deletions(-)

diff --git a/lily/include/output-def.hh b/lily/include/output-def.hh
index b683cc6..1d2b056 100644
--- a/lily/include/output-def.hh
+++ b/lily/include/output-def.hh
@@ -59,12 +59,13 @@ public:
   SCM lookup_variable (SCM sym) const;
   void set_variable (SCM sym, SCM val);
   Real get_dimension (SCM symbol) const;
+  void normalize ();
 };
 SCM get_font_table (Output_def *def);
 void assign_context_def (Output_def *m, SCM transdef);
 SCM find_context_def (Output_def const *m, SCM name);
 
-Interval line_dimensions_int (Output_def*def, int);
+Interval line_dimensions_int (Output_def *def, int);
  
 
 Font_metric *select_encoded_font (Output_def *layout, SCM chain);
diff --git a/lily/output-def-scheme.cc b/lily/output-def-scheme.cc
index 73a8048..7616e05 100644
--- a/lily/output-def-scheme.cc
+++ b/lily/output-def-scheme.cc
@@ -142,6 +142,19 @@ LY_DEFINE (ly_paper_get_number, "ly:paper-get-number",
   return scm_from_double (layout->get_dimension (sym));
 }
 
+LY_DEFINE (ly_paper_params_normalize, "ly:paper-params-normalize",
+           1, 0, 0, (SCM def),
+           "Checks whether @code{left-margin}, @code{right-margin} and 
@code{line-width)" 
+           "are set in output definition @var{def}, add missing values when 
needed and return the completed output-def." 
+           "Additionately, check if those values are consistent, otherwise 
print an error.")
+{
+  LY_ASSERT_SMOB (Output_def, def, 1);
+  Output_def *paper = unsmob_output_def (def);
+  Output_def *newpaper = paper->clone (); 
+  newpaper->normalize ();
+  return newpaper->unprotect ();
+}
+
 LY_DEFINE (ly_paper_fonts, "ly:paper-fonts",
           1, 0, 0, (SCM def),
           "Return a list containing the fonts from output definition"
diff --git a/lily/output-def.cc b/lily/output-def.cc
index f9fbda9..91d3763 100644
--- a/lily/output-def.cc
+++ b/lily/output-def.cc
@@ -11,6 +11,7 @@
 #include "context-def.hh"
 #include "file-path.hh"
 #include "global-context.hh"
+#include "international.hh"
 #include "interval.hh"
 #include "main.hh"
 #include "output-def.hh"
@@ -128,12 +129,87 @@ Output_def::set_variable (SCM sym, SCM val)
   scm_module_define (scope_, sym, val);
 }
 
+void
+Output_def::normalize ()
+{
+  Real paper_width = scm_to_double (lookup_variable (ly_symbol2scm 
("paper-width")));
+
+  Real left_margin;
+  Real left_margin_default = scm_to_double (lookup_variable (ly_symbol2scm 
("left-margin-default")));
+  SCM lmargin = lookup_variable (ly_symbol2scm ("left-margin"));
+
+  Real right_margin;
+  Real right_margin_default = scm_to_double (lookup_variable (ly_symbol2scm 
("right-margin-default")));
+  SCM rmargin = lookup_variable (ly_symbol2scm ("right-margin"));
+
+  Real line_width;
+  SCM lwidth = lookup_variable (ly_symbol2scm ("line-width"));
+
+  // Otherwise we would break such things like \score markups which inherit 
margins from global paper block
+  if ((lookup_variable (ly_symbol2scm ("is-layout")) == SCM_BOOL_T) && (lwidth 
!= SCM_UNDEFINED)) return;
+
+  if (lwidth != SCM_UNDEFINED)
+    {
+      line_width = scm_to_double (lwidth);
+      if (lmargin != SCM_UNDEFINED)
+        { 
+          left_margin = scm_to_double (lmargin);
+          right_margin = ((rmargin != SCM_UNDEFINED) ? scm_to_double(rmargin) 
: (paper_width - line_width - left_margin));
+        }
+      else if (rmargin != SCM_UNDEFINED) 
+        {
+          right_margin = scm_to_double (rmargin);
+          left_margin = paper_width - line_width - right_margin; 
+        }
+        else // Vertically center systems if only line-width is given. TODO: 
Tweakable
+          {
+            left_margin = (paper_width - line_width) / 2;
+            right_margin = left_margin;
+          }
+    }
+  else
+    {
+      left_margin = ((lmargin != SCM_UNDEFINED) ? scm_to_double(lmargin) : 
left_margin_default);
+      right_margin = ((rmargin != SCM_UNDEFINED) ? scm_to_double(rmargin) : 
right_margin_default);
+      line_width = paper_width - left_margin - right_margin;
+    }
   
+  bool consistency = true;
+ 
+ // Consistency checks. FIXME: Print warnings just once
+
+  if (paper_width != (line_width + left_margin + right_margin)) 
+    {
+      warning (_ ("margins don't fit with line-width, setting default 
values"));
+      consistency = false;
+    }
+  if ((left_margin < 0) || (right_margin < 0)) // TODO: Could somebody want 
this? 
+    {
+      warning (_ ("systems run off the page due to improper paper settings, 
setting default values"));
+      consistency = false;
+    }
+  
+  if (consistency)
+    {
+      set_variable (ly_symbol2scm ("line-width"), scm_from_double(line_width));
+      set_variable (ly_symbol2scm ("left-margin"), 
scm_from_double(left_margin));
+      set_variable (ly_symbol2scm ("right-margin"), 
scm_from_double(right_margin));
+    }
+  else // If values don't match, set default values
+    { 
+      set_variable (ly_symbol2scm ("left-margin"), 
scm_from_double(left_margin_default));
+      set_variable (ly_symbol2scm ("right-margin"), 
scm_from_double(right_margin_default));
+      set_variable (ly_symbol2scm ("line-width"), 
+                              scm_from_double(paper_width - 
left_margin_default - right_margin_default));     
+    }
+}
+ 
 /* FIXME.  This is broken until we have a generic way of
    putting lists inside the \layout block.  */
 Interval
 line_dimensions_int (Output_def *def, int n)
 {
+  def->normalize ();
   Real lw = def->get_dimension (ly_symbol2scm ("line-width"));
   Real ind = n
     ? def->get_dimension (ly_symbol2scm ("short-indent"))
diff --git a/ly/paper-defaults-init.ly b/ly/paper-defaults-init.ly
index 9bffbff..2e6a003 100644
--- a/ly/paper-defaults-init.ly
+++ b/ly/paper-defaults-init.ly
@@ -101,6 +101,10 @@
 
     top-margin = 5 \mm
     bottom-margin = 6 \mm
+
+    left-margin-default = 10 \mm
+    right-margin-default = 10 \mm   
+
     head-separation = 4 \mm
     foot-separation = 4 \mm
 
diff --git a/scm/framework-ps.scm b/scm/framework-ps.scm
index becf51d..6ee744b 100644
--- a/scm/framework-ps.scm
+++ b/scm/framework-ps.scm
@@ -81,9 +81,10 @@
      (else "")))
 
   (define (output-entry ps-key ly-key)
-    (string-append
-     "/" ps-key " "
-     (value->string (ly:output-def-lookup layout ly-key)) " def\n"))
+    (let ((layout-new (ly:paper-params-normalize layout)))
+       (string-append
+        "/" ps-key " "
+        (value->string (ly:output-def-lookup layout-new ly-key)) " def\n")))
 
   (string-append
    "/lily-output-units "
diff --git a/scm/page.scm b/scm/page.scm
index ca4e0d8..048a47b 100644
--- a/scm/page.scm
+++ b/scm/page.scm
@@ -182,16 +182,13 @@
 (define (layout->page-init layout)
   "Alist of settings for page layout"
   (let*
-      ((paper-height (ly:output-def-lookup layout 'paper-height))
-       (paper-width (ly:output-def-lookup layout 'paper-width))
-       (lmargin (ly:output-def-lookup layout 'left-margin #f))
-       (left-margin (if lmargin
-                      lmargin
-                      (/ (- paper-width
-                            (ly:output-def-lookup layout 'line-width)) 2)))
+      ((layout-new (ly:paper-params-normalize layout))
+       (paper-height (ly:output-def-lookup layout-new 'paper-height))
+       (paper-width (ly:output-def-lookup layout-new 'paper-width))
+       (left-margin (ly:output-def-lookup layout-new 'left-margin))
        (bottom-edge (- paper-height
-                      (ly:output-def-lookup layout 'bottom-margin)) )
-       (top-margin (ly:output-def-lookup layout 'top-margin))
+                      (ly:output-def-lookup layout-new 'bottom-margin)) )
+       (top-margin (ly:output-def-lookup layout-new 'top-margin))
        )
     
     `((paper-height . ,paper-height)
diff --git a/scm/paper.scm b/scm/paper.scm
index 48f4a46..6b6900c 100644
--- a/scm/paper.scm
+++ b/scm/paper.scm
@@ -211,19 +211,13 @@ size. SZ is in points"
 
 (define (set-paper-dimensions m w h)
   "M is a module (i.e. layout->scope_ )"
-  (let* ((mm (eval 'mm m)))
+  (begin
+    ;; page layout - what to do with (printer specific!) margin settings?
     (module-define! m 'paper-width w)
     (module-define! m 'paper-height h)
-    (module-define! m 'line-width (- w
-                                    (ly:modules-lookup (list m) 'left-margin 
(* 10 mm))
-                                    (ly:modules-lookup (list m) 'right-margin 
(* 10 mm))))
-
     (module-define! m 'indent (/ w 14))
-    (module-define! m 'short-indent 0)
-
-    ;; page layout - what to do with (printer specific!) margin settings?
-
-    ))
+    (module-define! m 'short-indent 0)))
+    
 
 (define (internal-set-paper-size module name landscape?)
   (define (swap x)
diff --git a/scm/titling.scm b/scm/titling.scm
index 66f45fc..7d28ce0 100644
--- a/scm/titling.scm
+++ b/scm/titling.scm
@@ -6,9 +6,9 @@
 ;;;;          Han-Wen Nienhuys <address@hidden>
 
 (define-public (layout-extract-page-properties layout)
-  (list (append `((line-width . ,(ly:paper-get-number
-                                layout 'line-width)))
-               (ly:output-def-lookup layout 'text-font-defaults))))
+  (let ((layout-new (ly:paper-params-normalize layout)))
+    (list (append `((line-width . ,(ly:paper-get-number layout-new 
'line-width)))
+                   (ly:output-def-lookup layout-new 'text-font-defaults)))))
 
 ;;;;;;;;;;;;;;;;;;
 
-- 
1.5.6


reply via email to

[Prev in Thread] Current Thread [Next in Thread]