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