* scm/beam.scm (check-slope-callbacks): check sign of slope.
[lilypond.git] / lily / output-def.cc
blob10dbc0f783309be2e45c7c97eb5b097f4feb81c2
1 /*
2 music-output-def.cc -- implement Output_def
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include "context-def.hh"
10 #include "file-path.hh"
11 #include "global-context.hh"
12 #include "interval.hh"
13 #include "lily-guile.hh"
14 #include "ly-module.hh"
15 #include "main.hh"
16 #include "output-def.hh"
17 #include "output-def.hh"
18 #include "scm-hash.hh"
19 #include "warn.hh"
21 Output_def::Output_def ()
23 scope_ = SCM_EOL;
24 parent_ = 0;
25 smobify_self ();
27 scope_ = ly_make_anonymous_module (false);
30 Output_def::Output_def (Output_def const &s)
32 scope_ = SCM_EOL;
33 parent_ = 0;
34 smobify_self ();
36 input_origin_ = s.input_origin_;
37 scope_= ly_make_anonymous_module (false);
38 if (ly_c_module_p (s.scope_))
39 ly_import_module (scope_, s.scope_);
42 Output_def::~Output_def ()
46 #include "ly-smobs.icc"
47 IMPLEMENT_SMOBS (Output_def);
48 IMPLEMENT_DEFAULT_EQUAL_P (Output_def);
50 SCM
51 Output_def::mark_smob (SCM m)
53 Output_def *mo = (Output_def*) SCM_CELL_WORD_1 (m);
55 /* FIXME: why is this necessary?
56 all bookpaper_ should be protected by themselves. */
57 if (mo->parent_)
58 scm_gc_mark (mo->parent_->self_scm ());
60 mo->derived_mark ();
61 return mo->scope_;
64 void
65 Output_def::derived_mark ()
69 void
70 assign_context_def (Output_def * m, SCM transdef)
72 Context_def *tp = unsmob_context_def (transdef);
73 assert (tp);
75 if (tp)
77 SCM sym = tp->get_context_name ();
78 scm_module_define (m->scope_, sym, transdef);
82 /* find the translator for NAME. NAME must be a symbol. */
83 SCM
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;
90 int
91 Output_def::print_smob (SCM s, SCM p, scm_print_state *)
93 Output_def * def = unsmob_output_def (s);
94 scm_puts ("#< ", p);
95 scm_puts (classname (def), p);
97 (void)def;
98 scm_puts (">", p);
99 return 1;
102 Real
103 Output_def::get_dimension (SCM s) const
105 SCM val = lookup_variable (s);
106 return ly_scm2double (val);
110 Output_def::lookup_variable (SCM sym) const
112 SCM var = ly_module_lookup (scope_, sym);
113 if (SCM_VARIABLEP (var) && SCM_VARIABLE_REF(var) != SCM_UNDEFINED)
114 return SCM_VARIABLE_REF (var);
116 if (parent_)
117 return parent_->lookup_variable (sym);
119 return SCM_EOL;
123 Output_def::c_variable (String s) const
125 return lookup_variable (ly_symbol2scm (s.to_str0 ()));
128 void
129 Output_def::set_variable (SCM sym, SCM val)
131 scm_module_define (scope_, sym, val);
134 LY_DEFINE (ly_paper_lookup, "ly:output-def-lookup",
135 2, 0, 0, (SCM pap, SCM sym),
136 "Lookup @var{sym} in @var{pap}. "
137 "Return the value or @code{'()} if undefined.")
139 Output_def *op = unsmob_output_def (pap);
140 SCM_ASSERT_TYPE (op, pap, SCM_ARG1, __FUNCTION__, "Output_def");
141 SCM_ASSERT_TYPE (ly_c_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");
143 return op->lookup_variable (sym);
146 LY_DEFINE (ly_output_def_scope, "ly:output-def-scope",
147 1, 0, 0, (SCM def),
148 "Get the variable scope inside @var{def}.")
150 Output_def *op = unsmob_output_def (def);
151 SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
152 return op->scope_;
155 LY_DEFINE (ly_output_def_parent, "ly:output-def-parent",
156 1, 0, 0, (SCM def),
157 "Get the parent output-def of @var{def}.")
159 Output_def *op = unsmob_output_def (def);
160 SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
161 return op->parent_ ? op->parent_->self_scm () : SCM_EOL;
164 LY_DEFINE (ly_output_def_clone, "ly:output-def-clone",
165 1, 0, 0, (SCM def),
166 "Clone @var{def}.")
168 Output_def *op = unsmob_output_def (def);
169 SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
170 SCM s = op->clone ()->self_scm ();
171 scm_gc_unprotect_object (s);
172 return s;
175 LY_DEFINE (ly_output_description, "ly:output-description",
176 1, 0, 0, (SCM output_def),
177 "Return the description of translators in @var{output-def}.")
179 Output_def *id = unsmob_output_def (output_def);
181 SCM al = ly_module2alist (id->scope_);
183 SCM ell = SCM_EOL;
184 for (SCM s = al; ly_c_pair_p (s); s = ly_cdr (s))
186 Context_def * td = unsmob_context_def (ly_cdar (s));
187 SCM key = ly_caar (s);
188 if (td && key == td->get_context_name ())
189 ell = scm_cons (scm_cons (key, td->to_alist ()), ell);
191 return ell;
194 /* FIXME. This is broken until we have a generic way of
195 putting lists inside the \paper block. */
196 Interval
197 line_dimensions_int (Output_def *def, int n)
199 Real lw = def->get_dimension (ly_symbol2scm ("linewidth"));
200 Real ind = n ? 0.0 : def->get_dimension (ly_symbol2scm ("indent"));
201 return Interval (ind, lw);
204 LY_DEFINE (ly_paper_def_p, "ly:paper-def?",
205 1, 0, 0, (SCM def),
206 "Is @var{def} a paper definition?")
208 return ly_bool2scm (unsmob_output_def (def));
211 LY_DEFINE (ly_bookpaper_outputscale, "ly:bookpaper-outputscale",
212 1, 0, 0, (SCM bp),
213 "Get outputscale for BP.")
215 Output_def *b = unsmob_output_def (bp);
216 SCM_ASSERT_TYPE (b, bp, SCM_ARG1, __FUNCTION__, "bookpaper");
217 return scm_make_real (output_scale (b));
220 LY_DEFINE (ly_make_output_def, "ly:make-output-def",
221 0, 0, 0, (),
222 "Make a output def.")
224 Output_def *bp = new Output_def ;
225 return scm_gc_unprotect_object (bp->self_scm ());