]> git.donarmstrong.com Git - lilypond.git/blob - lily/output-def.cc
(output_def): push scope of parent_ Output_def
[lilypond.git] / lily / output-def.cc
1 /*
2   music-output-def.cc -- implement Output_def
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
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"
20
21 #include "ly-smobs.icc"
22
23 Output_def::Output_def ()
24 {
25   scope_ = SCM_EOL;
26   parent_ = 0;
27   smobify_self ();
28
29   scope_ = ly_make_anonymous_module (false);
30 }
31
32 Output_def::~Output_def ()
33 {
34 }
35
36 Output_def::Output_def (Output_def const &s)
37 {
38   scope_ = SCM_EOL;
39   parent_ = 0;
40   smobify_self ();
41
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_);
46 }
47
48
49 IMPLEMENT_SMOBS (Output_def);
50 IMPLEMENT_DEFAULT_EQUAL_P (Output_def);
51
52
53 SCM
54 Output_def::mark_smob (SCM m)
55 {
56   Output_def * mo = (Output_def*) SCM_CELL_WORD_1 (m);
57
58   /*
59     FIXME: why is this necessary?
60     all bookpaper_ should be protected by themselves.
61   */
62   if (mo->parent_)
63     scm_gc_mark (mo->parent_->self_scm ());
64   
65
66   mo->derived_mark ();
67   return mo->scope_;
68 }
69
70 void
71 Output_def::derived_mark () {}
72
73 void
74 assign_context_def (Output_def * m, SCM transdef)
75 {
76   Context_def *tp = unsmob_context_def (transdef);
77   assert (tp);
78
79   if (tp)
80     {
81       SCM sym = tp->get_context_name ();
82       scm_module_define (m->scope_, sym, transdef);
83     }  
84 }
85
86 /*
87   find the translator for NAME. NAME must be a symbol.
88 */
89 SCM
90 find_context_def (Output_def const *m, SCM name)
91 {  
92   Context_def *cd = unsmob_context_def (m->lookup_variable (name));
93   return cd ? cd->self_scm () : SCM_EOL;
94 }
95
96 int
97 Output_def::print_smob (SCM s, SCM p, scm_print_state *)
98 {
99   Output_def * def = unsmob_output_def (s);
100   scm_puts ("#< ", p);
101   scm_puts (classname (def), p);
102   
103   (void)def;
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 ly_scm2double (val);
113 }
114
115
116 SCM
117 Output_def::lookup_variable (SCM sym) const
118 {
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);
122   
123   if (parent_)
124     return parent_->lookup_variable (sym);
125   
126   return SCM_EOL;
127 }
128
129 SCM
130 Output_def::c_variable (String s) const
131 {
132   return lookup_variable (ly_symbol2scm (s.to_str0 ()));
133 }
134
135 void
136 Output_def::set_variable (SCM sym, SCM val)
137 {
138   scm_module_define (scope_, sym, val);
139 }
140
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.")
145 {
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");
149
150   return op->lookup_variable (sym);
151 }
152
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}.")
156 {
157   Output_def *op = unsmob_output_def (def);
158   SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
159   return op->scope_;
160 }
161
162
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}.")
166 {
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;
170 }
171
172
173 LY_DEFINE (ly_output_def_clone, "ly:output-def-clone",
174            1, 0, 0, (SCM def),
175            "Clone @var{def}.")
176 {
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;
182 }
183
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}.")
188 {
189   Output_def *id = unsmob_output_def (output_def);
190   
191   SCM al =ly_module_to_alist (id->scope_);
192
193   SCM l = SCM_EOL;
194   for (SCM s = al ; ly_c_pair_p (s); s = ly_cdr (s))
195     {
196       Context_def * td = unsmob_context_def (ly_cdar (s));
197       SCM key = ly_caar (s);
198       if (td && key == td->get_context_name ())
199         {
200           
201           l = scm_cons (scm_cons (key, td->to_alist ()),  l);
202         }
203     }
204   return l;  
205 }
206   
207
208
209 #include "interval.hh"
210
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)
215 {
216   Real lw =  def->get_dimension (ly_symbol2scm ("linewidth"));
217   Real ind = n ? 0.0 : def->get_dimension (ly_symbol2scm ("indent"));
218
219   return Interval (ind, lw);
220 }
221
222 LY_DEFINE (ly_paper_def_p, "ly:paper-def?",
223            1, 0, 0, (SCM def),
224            "Is @var{def} a paper definition?")
225 {
226   return ly_bool2scm (unsmob_output_def (def));
227 }
228
229
230
231 LY_DEFINE (ly_bookpaper_outputscale, "ly:bookpaper-outputscale",
232           1, 0, 0,
233           (SCM bp),
234           "Get outputscale for BP.")
235 {
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));
239 }
240
241
242
243 LY_DEFINE (ly_make_output_def, "ly:make-output-def",
244            0, 0, 0, (),
245            "Make a output def.")
246 {
247   Output_def *bp = new Output_def ;
248   return scm_gc_unprotect_object (bp->self_scm ());
249 }
250