]> git.donarmstrong.com Git - lilypond.git/blob - lily/output-def.cc
aba0f5e55e774d4fee63a45eb99142983804d586
[lilypond.git] / lily / output-def.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "context-def.hh"
21 #include "file-path.hh"
22 #include "global-context.hh"
23 #include "international.hh"
24 #include "interval.hh"
25 #include "ly-module.hh"
26 #include "main.hh"
27 #include "output-def.hh"
28 #include "scm-hash.hh"
29 #include "warn.hh"
30
31
32 #include "program-option.hh"
33
34 #include "string-convert.hh"
35
36 using std::string;
37
38 Output_def::Output_def ()
39 {
40   scope_ = SCM_EOL;
41   parent_ = 0;
42
43   smobify_self ();
44
45   scope_ = ly_make_module (false);
46 }
47
48 Output_def::Output_def (Output_def const &s)
49   : Smob<Output_def> ()
50 {
51   scope_ = SCM_EOL;
52   parent_ = 0;
53   smobify_self ();
54
55   input_origin_ = s.input_origin_;
56   scope_ = ly_make_module (false);
57   if (ly_is_module (s.scope_))
58     ly_module_copy (scope_, s.scope_);
59 }
60
61 Output_def::~Output_def ()
62 {
63 }
64
65
66 SCM
67 Output_def::mark_smob () const
68 {
69   /* FIXME: why is this necessary?
70      all paper_ should be protected by themselves. */
71   if (parent_)
72     scm_gc_mark (parent_->self_scm ());
73
74   return scope_;
75 }
76
77 void
78 assign_context_def (Output_def * m, SCM transdef)
79 {
80   Context_def *tp = unsmob<Context_def> (transdef);
81   assert (tp);
82
83   if (tp)
84     {
85       SCM sym = tp->get_context_name ();
86       m->set_variable (sym, transdef);
87     }
88 }
89
90 /* find the translator for NAME. NAME must be a symbol. */
91 SCM
92 find_context_def (Output_def const *m, SCM name)
93 {
94   Context_def *cd = unsmob<Context_def> (m->lookup_variable (name));
95   return cd ? cd->self_scm () : SCM_EOL;
96 }
97
98 int
99 Output_def::print_smob (SCM p, scm_print_state *) const
100 {
101   scm_puts ("#< ", p);
102   scm_puts (class_name (), p);
103   scm_puts (">", p);
104   return 1;
105 }
106
107 Real
108 Output_def::get_dimension (SCM s) const
109 {
110   SCM val = lookup_variable (s);
111   return scm_to_double (val);
112 }
113
114 SCM
115 Output_def::lookup_variable (SCM sym) const
116 {
117   SCM var = ly_module_lookup (scope_, sym);
118   if (SCM_VARIABLEP (var) && !SCM_UNBNDP (SCM_VARIABLE_REF (var)))
119     return SCM_VARIABLE_REF (var);
120
121   if (parent_)
122     return parent_->lookup_variable (sym);
123
124   return SCM_UNDEFINED;
125 }
126
127 SCM
128 Output_def::c_variable (const string &s) const
129 {
130   return lookup_variable (ly_symbol2scm (s.c_str ()));
131 }
132
133 void
134 Output_def::set_variable (SCM sym, SCM val)
135 {
136   scm_module_define (scope_, sym, val);
137 }
138
139 void
140 Output_def::normalize ()
141 {
142   Real paper_width;
143   SCM scm_paper_width = c_variable ("paper-width");
144
145   bool twosided = to_boolean (c_variable ("two-sided"));
146   // We don't distinguish between outer-margin / left-margin and so on
147   // until page-stencil positioning in page.scm
148   Real left_margin, left_margin_default;
149   SCM scm_left_margin_default = (twosided
150                                  ? c_variable ("outer-margin-default-scaled")
151                                  : c_variable ("left-margin-default-scaled"));
152   SCM scm_left_margin = (twosided
153                          ? c_variable ("outer-margin")
154                          : c_variable ("left-margin"));
155
156   Real right_margin, right_margin_default;
157   SCM scm_right_margin_default = (twosided
158                                   ? c_variable ("inner-margin-default-scaled")
159                                   : c_variable ("right-margin-default-scaled"));
160   SCM scm_right_margin = (twosided
161                           ? c_variable ("inner-margin")
162                           : c_variable ("right-margin"));
163
164   if (SCM_UNBNDP (scm_paper_width)
165       || SCM_UNBNDP (scm_left_margin_default)
166       || SCM_UNBNDP (scm_right_margin_default))
167     {
168       programming_error ("called normalize () on paper with missing settings");
169       return;
170     }
171   else
172     {
173       paper_width = scm_to_double (scm_paper_width);
174       left_margin_default = scm_to_double (scm_left_margin_default);
175       right_margin_default = scm_to_double (scm_right_margin_default);
176     }
177
178   Real line_width;
179   Real line_width_default
180     = paper_width - left_margin_default - right_margin_default;
181   SCM scm_line_width = c_variable ("line-width");
182
183   Real binding_offset = 0;
184   if (twosided)
185     binding_offset = robust_scm2double (c_variable ("binding-offset"), 0);
186
187   if (SCM_UNBNDP (scm_line_width))
188     {
189       left_margin = (SCM_UNBNDP (scm_left_margin)
190                      ? left_margin_default
191                      : scm_to_double (scm_left_margin));
192       right_margin = (SCM_UNBNDP (scm_right_margin)
193                       ? right_margin_default
194                       : scm_to_double (scm_right_margin)) + binding_offset;
195       line_width = paper_width - left_margin - right_margin;
196     }
197   else
198     {
199       line_width = scm_to_double (scm_line_width);
200       if (SCM_UNBNDP (scm_left_margin))
201         {
202           // Vertically center systems if only line-width is given
203           if (SCM_UNBNDP (scm_right_margin))
204             {
205               left_margin = (paper_width - line_width) / 2;
206               right_margin = left_margin;
207             }
208           else
209             {
210               right_margin = scm_to_double (scm_right_margin) + binding_offset;
211               left_margin = paper_width - line_width - right_margin;
212             }
213         }
214       else
215         {
216           left_margin = scm_to_double (scm_left_margin);
217           right_margin = (SCM_UNBNDP (scm_right_margin)
218                            ? (paper_width - line_width - left_margin)
219                            : scm_to_double (scm_right_margin)) + binding_offset;
220         }
221     }
222
223   if (to_boolean (c_variable ("check-consistency")))
224     {
225       // Consistency checks. If values don't match, set defaults.
226       if (abs (paper_width - line_width - left_margin - right_margin) > 1e-6)
227         {
228           line_width = line_width_default;
229           left_margin = left_margin_default;
230           right_margin = right_margin_default;
231           warning (_ ("margins do not fit with line-width, setting default values"));
232         }
233       else if ((left_margin < 0) || (right_margin < 0))
234         {
235           line_width = line_width_default;
236           left_margin = left_margin_default;
237           right_margin = right_margin_default;
238           warning (_ ("systems run off the page due to improper paper settings, setting default values"));
239         }
240     }
241
242   set_variable (ly_symbol2scm ("left-margin"), scm_from_double (left_margin));
243   set_variable (ly_symbol2scm ("right-margin"), scm_from_double (right_margin));
244   set_variable (ly_symbol2scm ("line-width"), scm_from_double (line_width));
245 }
246
247 /* FIXME.  This is broken until we have a generic way of
248    putting lists inside the \layout block.  */
249 Interval
250 line_dimensions_int (Output_def *def, int n)
251 {
252   Real lw = def->get_dimension (ly_symbol2scm ("line-width"));
253   Real ind = n
254     ? def->get_dimension (ly_symbol2scm ("short-indent"))
255     : def->get_dimension (ly_symbol2scm ("indent"));
256   return Interval (ind, lw);
257 }