2 music-output-def.cc -- implement Output_def
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "output-def.hh"
11 #include "context-def.hh"
12 #include "file-path.hh"
13 #include "global-context.hh"
14 #include "international.hh"
15 #include "interval.hh"
17 #include "output-def.hh"
18 #include "scm-hash.hh"
21 #include "ly-smobs.icc"
23 #include "program-option.hh"
25 #include "string-convert.hh"
27 Output_def::Output_def ()
34 scope_
= ly_make_anonymous_module (false);
37 Output_def::Output_def (Output_def
const &s
)
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_
);
49 Output_def::~Output_def ()
53 IMPLEMENT_SMOBS (Output_def
);
54 IMPLEMENT_DEFAULT_EQUAL_P (Output_def
);
57 Output_def::mark_smob (SCM m
)
59 Output_def
*mo
= (Output_def
*) SCM_CELL_WORD_1 (m
);
61 /* FIXME: why is this necessary?
62 all paper_ should be protected by themselves. */
64 scm_gc_mark (mo
->parent_
->self_scm ());
70 assign_context_def (Output_def
* m
, SCM transdef
)
72 Context_def
*tp
= unsmob_context_def (transdef
);
77 SCM sym
= tp
->get_context_name ();
78 m
->set_variable (sym
, transdef
);
82 /* find the translator for NAME. NAME must be a symbol. */
84 find_context_def (Output_def
const *m
, SCM name
)
86 Context_def
*cd
= unsmob_context_def (m
->lookup_variable (name
));
87 return cd
? cd
->self_scm () : SCM_EOL
;
91 Output_def::print_smob (SCM s
, SCM p
, scm_print_state
*)
93 Output_def
* def
= unsmob_output_def (s
);
95 scm_puts (def
->class_name (), p
);
101 Output_def::get_dimension (SCM s
) const
103 SCM val
= lookup_variable (s
);
104 return scm_to_double (val
);
108 Output_def::lookup_variable (SCM sym
) const
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
);
115 return parent_
->lookup_variable (sym
);
117 return SCM_UNDEFINED
;
121 Output_def::c_variable (string s
) const
123 return lookup_variable (ly_symbol2scm (s
.c_str ()));
127 Output_def::set_variable (SCM sym
, SCM val
)
129 scm_module_define (scope_
, sym
, val
);
133 Output_def::normalize ()
136 SCM scm_paper_width
= c_variable ("paper-width");
138 bool twosided
= to_boolean (c_variable ("two-sided"));
139 // We don't distinguish between outer-margin / left-margin and so on
140 // until page-stencil positioning in page.scm
141 Real left_margin
, left_margin_default
;
142 SCM scm_left_margin_default
= (twosided
143 ? c_variable ("outer-margin-default-scaled")
144 : c_variable ("left-margin-default-scaled"));
145 SCM scm_left_margin
= (twosided
146 ? c_variable ("outer-margin")
147 : c_variable ("left-margin"));
149 Real right_margin
, right_margin_default
;
150 SCM scm_right_margin_default
= (twosided
151 ? c_variable ("inner-margin-default-scaled")
152 : c_variable ("right-margin-default-scaled"));
153 SCM scm_right_margin
= (twosided
154 ? c_variable ("inner-margin")
155 : c_variable ("right-margin"));
157 if (scm_paper_width
== SCM_UNDEFINED
158 || scm_left_margin_default
== SCM_UNDEFINED
159 || scm_right_margin_default
== SCM_UNDEFINED
)
161 programming_error ("called normalize () on paper with missing settings");
166 paper_width
= scm_to_double (scm_paper_width
);
167 left_margin_default
= scm_to_double (scm_left_margin_default
);
168 right_margin_default
= scm_to_double (scm_right_margin_default
);
172 Real line_width_default
173 = paper_width
- left_margin_default
- right_margin_default
;
174 SCM scm_line_width
= c_variable ("line-width");
176 Real binding_offset
= 0;
178 binding_offset
= robust_scm2double (c_variable ("binding-offset"), 0);
180 if (scm_line_width
== SCM_UNDEFINED
)
182 left_margin
= ((scm_left_margin
== SCM_UNDEFINED
)
183 ? left_margin_default
184 : scm_to_double (scm_left_margin
));
185 right_margin
= ((scm_right_margin
== SCM_UNDEFINED
)
186 ? right_margin_default
187 : scm_to_double (scm_right_margin
)) + binding_offset
;
188 line_width
= paper_width
- left_margin
- right_margin
;
192 line_width
= scm_to_double (scm_line_width
);
193 if (scm_left_margin
== SCM_UNDEFINED
)
195 // Vertically center systems if only line-width is given
196 if (scm_right_margin
== SCM_UNDEFINED
)
198 left_margin
= (paper_width
- line_width
) / 2;
199 right_margin
= left_margin
;
203 right_margin
= scm_to_double (scm_right_margin
) + binding_offset
;
204 left_margin
= paper_width
- line_width
- right_margin
;
209 left_margin
= scm_to_double (scm_left_margin
);
210 right_margin
= ((scm_right_margin
== SCM_UNDEFINED
)
211 ? (paper_width
- line_width
- left_margin
)
212 : scm_to_double (scm_right_margin
)) + binding_offset
;
216 if (to_boolean (c_variable ("check-consistency")))
218 // Consistency checks. If values don't match, set defaults.
219 if (abs (paper_width
- line_width
- left_margin
- right_margin
) > 1e-6)
221 line_width
= line_width_default
;
222 left_margin
= left_margin_default
;
223 right_margin
= right_margin_default
;
224 warning (_ ("margins do not fit with line-width, setting default values"));
226 else if ((left_margin
< 0) || (right_margin
< 0))
228 line_width
= line_width_default
;
229 left_margin
= left_margin_default
;
230 right_margin
= right_margin_default
;
231 warning (_ ("systems run off the page due to improper paper settings, setting default values"));
235 set_variable (ly_symbol2scm ("left-margin"), scm_from_double (left_margin
));
236 set_variable (ly_symbol2scm ("right-margin"), scm_from_double (right_margin
));
237 set_variable (ly_symbol2scm ("line-width"), scm_from_double (line_width
));
240 /* FIXME. This is broken until we have a generic way of
241 putting lists inside the \layout block. */
243 line_dimensions_int (Output_def
*def
, int n
)
245 Real lw
= def
->get_dimension (ly_symbol2scm ("line-width"));
247 ? def
->get_dimension (ly_symbol2scm ("short-indent"))
248 : def
->get_dimension (ly_symbol2scm ("indent"));
249 return Interval (ind
, lw
);