2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "output-def.hh"
22 #include "context-def.hh"
23 #include "file-path.hh"
24 #include "global-context.hh"
25 #include "international.hh"
26 #include "interval.hh"
28 #include "output-def.hh"
29 #include "scm-hash.hh"
33 #include "program-option.hh"
35 #include "string-convert.hh"
37 Output_def::Output_def ()
44 scope_ = ly_make_module (false);
47 Output_def::Output_def (Output_def const &s)
54 input_origin_ = s.input_origin_;
55 scope_ = ly_make_module (false);
56 if (ly_is_module (s.scope_))
57 ly_module_copy (scope_, s.scope_);
60 Output_def::~Output_def ()
66 Output_def::mark_smob ()
68 /* FIXME: why is this necessary?
69 all paper_ should be protected by themselves. */
71 scm_gc_mark (parent_->self_scm ());
77 assign_context_def (Output_def * m, SCM transdef)
79 Context_def *tp = Context_def::unsmob (transdef);
84 SCM sym = tp->get_context_name ();
85 m->set_variable (sym, transdef);
89 /* find the translator for NAME. NAME must be a symbol. */
91 find_context_def (Output_def const *m, SCM name)
93 Context_def *cd = Context_def::unsmob (m->lookup_variable (name));
94 return cd ? cd->self_scm () : SCM_EOL;
98 Output_def::print_smob (SCM p, scm_print_state *)
101 scm_puts (class_name (), p);
107 Output_def::get_dimension (SCM s) const
109 SCM val = lookup_variable (s);
110 return scm_to_double (val);
114 Output_def::lookup_variable (SCM sym) const
116 SCM var = ly_module_lookup (scope_, sym);
117 if (SCM_VARIABLEP (var) && SCM_VARIABLE_REF (var) != SCM_UNDEFINED)
118 return SCM_VARIABLE_REF (var);
121 return parent_->lookup_variable (sym);
123 return SCM_UNDEFINED;
127 Output_def::c_variable (const string &s) const
129 return lookup_variable (ly_symbol2scm (s.c_str ()));
133 Output_def::set_variable (SCM sym, SCM val)
135 scm_module_define (scope_, sym, val);
139 Output_def::normalize ()
142 SCM scm_paper_width = c_variable ("paper-width");
144 bool twosided = to_boolean (c_variable ("two-sided"));
145 // We don't distinguish between outer-margin / left-margin and so on
146 // until page-stencil positioning in page.scm
147 Real left_margin, left_margin_default;
148 SCM scm_left_margin_default = (twosided
149 ? c_variable ("outer-margin-default-scaled")
150 : c_variable ("left-margin-default-scaled"));
151 SCM scm_left_margin = (twosided
152 ? c_variable ("outer-margin")
153 : c_variable ("left-margin"));
155 Real right_margin, right_margin_default;
156 SCM scm_right_margin_default = (twosided
157 ? c_variable ("inner-margin-default-scaled")
158 : c_variable ("right-margin-default-scaled"));
159 SCM scm_right_margin = (twosided
160 ? c_variable ("inner-margin")
161 : c_variable ("right-margin"));
163 if (scm_paper_width == SCM_UNDEFINED
164 || scm_left_margin_default == SCM_UNDEFINED
165 || scm_right_margin_default == SCM_UNDEFINED)
167 programming_error ("called normalize () on paper with missing settings");
172 paper_width = scm_to_double (scm_paper_width);
173 left_margin_default = scm_to_double (scm_left_margin_default);
174 right_margin_default = scm_to_double (scm_right_margin_default);
178 Real line_width_default
179 = paper_width - left_margin_default - right_margin_default;
180 SCM scm_line_width = c_variable ("line-width");
182 Real binding_offset = 0;
184 binding_offset = robust_scm2double (c_variable ("binding-offset"), 0);
186 if (scm_line_width == SCM_UNDEFINED)
188 left_margin = ((scm_left_margin == SCM_UNDEFINED)
189 ? left_margin_default
190 : scm_to_double (scm_left_margin));
191 right_margin = ((scm_right_margin == SCM_UNDEFINED)
192 ? right_margin_default
193 : scm_to_double (scm_right_margin)) + binding_offset;
194 line_width = paper_width - left_margin - right_margin;
198 line_width = scm_to_double (scm_line_width);
199 if (scm_left_margin == SCM_UNDEFINED)
201 // Vertically center systems if only line-width is given
202 if (scm_right_margin == SCM_UNDEFINED)
204 left_margin = (paper_width - line_width) / 2;
205 right_margin = left_margin;
209 right_margin = scm_to_double (scm_right_margin) + binding_offset;
210 left_margin = paper_width - line_width - right_margin;
215 left_margin = scm_to_double (scm_left_margin);
216 right_margin = ((scm_right_margin == SCM_UNDEFINED)
217 ? (paper_width - line_width - left_margin)
218 : scm_to_double (scm_right_margin)) + binding_offset;
222 if (to_boolean (c_variable ("check-consistency")))
224 // Consistency checks. If values don't match, set defaults.
225 if (abs (paper_width - line_width - left_margin - right_margin) > 1e-6)
227 line_width = line_width_default;
228 left_margin = left_margin_default;
229 right_margin = right_margin_default;
230 warning (_ ("margins do not fit with line-width, setting default values"));
232 else if ((left_margin < 0) || (right_margin < 0))
234 line_width = line_width_default;
235 left_margin = left_margin_default;
236 right_margin = right_margin_default;
237 warning (_ ("systems run off the page due to improper paper settings, setting default values"));
241 set_variable (ly_symbol2scm ("left-margin"), scm_from_double (left_margin));
242 set_variable (ly_symbol2scm ("right-margin"), scm_from_double (right_margin));
243 set_variable (ly_symbol2scm ("line-width"), scm_from_double (line_width));
246 /* FIXME. This is broken until we have a generic way of
247 putting lists inside the \layout block. */
249 line_dimensions_int (Output_def *def, int n)
251 Real lw = def->get_dimension (ly_symbol2scm ("line-width"));
253 ? def->get_dimension (ly_symbol2scm ("short-indent"))
254 : def->get_dimension (ly_symbol2scm ("indent"));
255 return Interval (ind, lw);