* lily/paper-book.cc: remove copyright & tagline. Remove
[lilypond.git] / lily / output-def.cc
blob0363aef752da7142babdebe4af37b8df0f9b6978
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 */
10 #include "context-def.hh"
11 #include "file-path.hh"
12 #include "global-context.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 #include "ly-smobs.icc"
23 Output_def::Output_def ()
25 scope_ = SCM_EOL;
26 parent_ = 0;
27 smobify_self ();
29 scope_ = ly_make_anonymous_module (false);
32 Output_def::~Output_def ()
36 Output_def::Output_def (Output_def const &s)
38 scope_ = SCM_EOL;
39 parent_ = 0;
40 smobify_self ();
42 input_origin_ = s.input_origin_;
43 scope_= ly_make_anonymous_module (false);
44 if (ly_c_module_p (s.scope_))
45 ly_import_module (scope_, s.scope_);
49 IMPLEMENT_SMOBS (Output_def);
50 IMPLEMENT_DEFAULT_EQUAL_P (Output_def);
53 SCM
54 Output_def::mark_smob (SCM m)
56 Output_def * mo = (Output_def*) SCM_CELL_WORD_1 (m);
59 FIXME: why is this necessary?
60 all bookpaper_ should be protected by themselves.
62 if (mo->parent_)
63 scm_gc_mark (mo->parent_->self_scm ());
66 mo->derived_mark ();
67 return mo->scope_;
70 void
71 Output_def::derived_mark () {}
73 void
74 assign_context_def (Output_def * m, SCM transdef)
76 Context_def *tp = unsmob_context_def (transdef);
77 assert (tp);
79 if (tp)
81 SCM sym = tp->get_context_name ();
82 scm_module_define (m->scope_, sym, transdef);
87 find the translator for NAME. NAME must be a symbol.
89 SCM
90 find_context_def (Output_def const *m, SCM name)
92 Context_def *cd = unsmob_context_def (m->lookup_variable (name));
93 return cd ? cd->self_scm () : SCM_EOL;
96 int
97 Output_def::print_smob (SCM s, SCM p, scm_print_state *)
99 Output_def * def = unsmob_output_def (s);
100 scm_puts ("#< ", p);
101 scm_puts (classname (def), p);
103 (void)def;
104 scm_puts (">", p);
105 return 1;
108 Real
109 Output_def::get_dimension (SCM s) const
111 SCM val = lookup_variable (s);
112 return ly_scm2double (val);
117 Output_def::lookup_variable (SCM sym) const
119 SCM var = ly_module_lookup (scope_, sym);
120 if (SCM_VARIABLEP (var) && SCM_VARIABLE_REF(var) != SCM_UNDEFINED)
121 return SCM_VARIABLE_REF (var);
123 if (parent_)
124 return parent_->lookup_variable (sym);
126 return SCM_EOL;
130 Output_def::c_variable (String s) const
132 return lookup_variable (ly_symbol2scm (s.to_str0 ()));
135 void
136 Output_def::set_variable (SCM sym, SCM val)
138 scm_module_define (scope_, sym, val);
141 LY_DEFINE (ly_paper_lookup, "ly:output-def-lookup",
142 2, 0, 0, (SCM pap, SCM sym),
143 "Lookup @var{sym} in @var{pap}. "
144 "Return the value or @code{'()} if undefined.")
146 Output_def *op = unsmob_output_def (pap);
147 SCM_ASSERT_TYPE (op, pap, SCM_ARG1, __FUNCTION__, "Output_def");
148 SCM_ASSERT_TYPE (ly_c_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");
150 return op->lookup_variable (sym);
153 LY_DEFINE (ly_output_def_scope, "ly:output-def-scope",
154 1, 0,0, (SCM def),
155 "Get the variable scope inside @var{def}.")
157 Output_def *op = unsmob_output_def (def);
158 SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
159 return op->scope_;
163 LY_DEFINE (ly_output_def_parent, "ly:output-def-parent",
164 1, 0, 0, (SCM def),
165 "Get the parent output-def of @var{def}.")
167 Output_def *op = unsmob_output_def (def);
168 SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
169 return op->parent_ ? op->parent_->self_scm () : SCM_EOL;
173 LY_DEFINE (ly_output_def_clone, "ly:output-def-clone",
174 1, 0, 0, (SCM def),
175 "Clone @var{def}.")
177 Output_def *op = unsmob_output_def (def);
178 SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
179 SCM s = op->clone ()->self_scm ();
180 scm_gc_unprotect_object (s);
181 return s;
184 LY_DEFINE(ly_output_description, "ly:output-description",
185 1,0,0,
186 (SCM output_def),
187 "Return the description of translators in @var{output-def}.")
189 Output_def *id = unsmob_output_def (output_def);
191 SCM al =ly_module_to_alist (id->scope_);
193 SCM l = SCM_EOL;
194 for (SCM s = al ; ly_c_pair_p (s); s = ly_cdr (s))
196 Context_def * td = unsmob_context_def (ly_cdar (s));
197 SCM key = ly_caar (s);
198 if (td && key == td->get_context_name ())
201 l = scm_cons (scm_cons (key, td->to_alist ()), l);
204 return l;
209 #include "interval.hh"
211 /* FIXME. This is broken until we have a generic way of
212 putting lists inside the \paper block. */
213 Interval
214 line_dimensions_int (Output_def *def, int n)
216 Real lw = def->get_dimension (ly_symbol2scm ("linewidth"));
217 Real ind = n ? 0.0 : def->get_dimension (ly_symbol2scm ("indent"));
219 return Interval (ind, lw);
222 LY_DEFINE (ly_paper_def_p, "ly:paper-def?",
223 1, 0, 0, (SCM def),
224 "Is @var{def} a paper definition?")
226 return ly_bool2scm (unsmob_output_def (def));
231 LY_DEFINE (ly_bookpaper_outputscale, "ly:bookpaper-outputscale",
232 1, 0, 0,
233 (SCM bp),
234 "Get outputscale for BP.")
236 Output_def *b = unsmob_output_def (bp);
237 SCM_ASSERT_TYPE (b, bp, SCM_ARG1, __FUNCTION__, "bookpaper");
238 return scm_make_real (output_scale (b));
243 LY_DEFINE (ly_make_output_def, "ly:make-output-def",
244 0, 0, 0, (),
245 "Make a output def.")
247 Output_def *bp = new Output_def ;
248 return scm_gc_unprotect_object (bp->self_scm ());