]> git.donarmstrong.com Git - lilypond.git/blob - lily/output-def.cc
Introduce new handling for paper margin settings.
[lilypond.git] / lily / output-def.cc
1 /*
2   music-output-def.cc -- implement Output_def
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "output-def.hh"
10
11 #include "context-def.hh"
12 #include "file-path.hh"
13 #include "global-context.hh"
14 #include "international.hh"
15 #include "interval.hh"
16 #include "main.hh"
17 #include "output-def.hh"
18 #include "scm-hash.hh"
19 #include "warn.hh"
20
21 #include "ly-smobs.icc"
22
23 #include "program-option.hh"
24
25 #include "string-convert.hh"
26
27 Output_def::Output_def ()
28 {
29   scope_ = SCM_EOL;
30   parent_ = 0;
31
32   smobify_self ();
33
34   scope_ = ly_make_anonymous_module (false);
35 }
36
37 Output_def::Output_def (Output_def const &s)
38 {
39   scope_ = SCM_EOL;
40   parent_ = 0;
41   smobify_self ();
42
43   input_origin_ = s.input_origin_;
44   scope_ = ly_make_anonymous_module (false);
45   if (ly_is_module (s.scope_))
46     ly_module_copy (scope_, s.scope_);
47 }
48
49 Output_def::~Output_def ()
50 {
51 }
52
53 IMPLEMENT_SMOBS (Output_def);
54 IMPLEMENT_DEFAULT_EQUAL_P (Output_def);
55
56 SCM
57 Output_def::mark_smob (SCM m)
58 {
59   Output_def *mo = (Output_def*) SCM_CELL_WORD_1 (m);
60
61   /* FIXME: why is this necessary?
62      all paper_ should be protected by themselves. */
63   if (mo->parent_)
64     scm_gc_mark (mo->parent_->self_scm ());
65
66   return mo->scope_;
67 }
68
69 void
70 assign_context_def (Output_def * m, SCM transdef)
71 {
72   Context_def *tp = unsmob_context_def (transdef);
73   assert (tp);
74
75   if (tp)
76     {
77       SCM sym = tp->get_context_name ();
78       m->set_variable (sym, transdef);
79     }
80 }
81
82 /* find the translator for NAME. NAME must be a symbol. */
83 SCM
84 find_context_def (Output_def const *m, SCM name)
85 {
86   Context_def *cd = unsmob_context_def (m->lookup_variable (name));
87   return cd ? cd->self_scm () : SCM_EOL;
88 }
89
90 int
91 Output_def::print_smob (SCM s, SCM p, scm_print_state *)
92 {
93   Output_def * def = unsmob_output_def (s);
94   scm_puts ("#< ", p);
95   scm_puts (def->class_name (), p);
96   scm_puts (">", p);
97   return 1;
98 }
99
100 Real
101 Output_def::get_dimension (SCM s) const
102 {
103   SCM val = lookup_variable (s);
104   return scm_to_double (val);
105 }
106
107 SCM
108 Output_def::lookup_variable (SCM sym) const
109 {
110   SCM var = ly_module_lookup (scope_, sym);
111   if (SCM_VARIABLEP (var) && SCM_VARIABLE_REF (var) != SCM_UNDEFINED)
112     return SCM_VARIABLE_REF (var);
113
114   if (parent_)
115     return parent_->lookup_variable (sym);
116
117   return SCM_UNDEFINED;
118 }
119
120 SCM
121 Output_def::c_variable (string s) const
122 {
123   return lookup_variable (ly_symbol2scm (s.c_str ()));
124 }
125
126 void
127 Output_def::set_variable (SCM sym, SCM val)
128 {
129   scm_module_define (scope_, sym, val);
130 }
131
132 void
133 Output_def::normalize ()
134 {
135   Real paper_width;
136   SCM scm_paper_width = c_variable ("paper-width");
137
138   Real left_margin, left_margin_default;
139   SCM scm_left_margin_default = c_variable ("left-margin-default");
140   SCM scm_left_margin = c_variable ("left-margin");
141
142   Real right_margin, right_margin_default;
143   SCM scm_right_margin_default = c_variable ("right-margin-default");
144   SCM scm_right_margin = c_variable ("right-margin");
145
146   if (scm_paper_width == SCM_UNDEFINED
147       || scm_left_margin_default == SCM_UNDEFINED
148       || scm_right_margin_default == SCM_UNDEFINED)
149     {
150       programming_error ("called normalize() on paper with missing settings");
151       return;
152     }
153   else
154     {
155       paper_width = scm_to_double (scm_paper_width);
156       left_margin_default = scm_to_double (scm_left_margin_default);
157       right_margin_default = scm_to_double (scm_right_margin_default);
158     }
159
160   Real line_width;
161   Real line_width_default = paper_width - left_margin_default - right_margin_default;
162   SCM scm_line_width = c_variable ("line-width");
163
164   if (scm_line_width == SCM_UNDEFINED)
165     {
166       left_margin = ((scm_left_margin == SCM_UNDEFINED) ? left_margin_default : scm_to_double(scm_left_margin));
167       right_margin = ((scm_right_margin == SCM_UNDEFINED) ? right_margin_default : scm_to_double(scm_right_margin));
168       line_width = paper_width - left_margin - right_margin;
169     }
170   else
171     {
172       line_width = scm_to_double (scm_line_width);
173       if (scm_left_margin == SCM_UNDEFINED)
174         {
175           if (scm_right_margin == SCM_UNDEFINED) // Vertically center systems if only line-width is given
176             {
177               left_margin = (paper_width - line_width) / 2;
178               right_margin = left_margin;
179             }
180           else
181             {
182               right_margin = scm_to_double (scm_right_margin);
183               left_margin = paper_width - line_width - right_margin;
184             }
185         }
186       else
187         {
188           left_margin = scm_to_double (scm_left_margin);
189           right_margin = ((scm_right_margin == SCM_UNDEFINED)
190                            ? (paper_width - line_width - left_margin)
191                            : scm_to_double (scm_right_margin));
192         }
193     }
194
195   if (to_boolean (c_variable ("check-consistency")))
196     {
197       // Consistency checks. If values don't match, set defaults.
198       if (abs(paper_width - line_width - left_margin - right_margin) > 1e-6)
199         {
200           line_width = line_width_default;
201           left_margin = left_margin_default;
202           right_margin = right_margin_default;
203           warning (_ ("margins do not fit with line-width, setting default values"));
204         }
205       else if ((left_margin < 0) || (right_margin < 0))
206         {
207           line_width = line_width_default;
208           left_margin = left_margin_default;
209           right_margin = right_margin_default;
210           warning (_ ("systems run off the page due to improper paper settings, setting default values"));
211         }
212     }
213
214   set_variable (ly_symbol2scm ("left-margin"), scm_from_double (left_margin));
215   set_variable (ly_symbol2scm ("right-margin"), scm_from_double (right_margin));
216   set_variable (ly_symbol2scm ("line-width"), scm_from_double (line_width));
217 }
218
219 /* FIXME.  This is broken until we have a generic way of
220    putting lists inside the \layout block.  */
221 Interval
222 line_dimensions_int (Output_def *def, int n)
223 {
224   Real lw = def->get_dimension (ly_symbol2scm ("line-width"));
225   Real ind = n
226     ? def->get_dimension (ly_symbol2scm ("short-indent"))
227     : def->get_dimension (ly_symbol2scm ("indent"));
228   return Interval (ind, lw);
229 }
230
231