]> git.donarmstrong.com Git - lilypond.git/blob - lily/music-output-def.cc
new file, move from
[lilypond.git] / lily / music-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 #include "book-paper-def.hh"
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   scope_= ly_make_anonymous_module (false);
43   if (is_module (s.scope_))
44     ly_import_module (scope_, s.scope_);
45 }
46
47
48 IMPLEMENT_SMOBS (Output_def);
49 IMPLEMENT_DEFAULT_EQUAL_P (Output_def);
50
51
52 SCM
53 Output_def::mark_smob (SCM m)
54 {
55   Output_def * mo = (Output_def*) SCM_CELL_WORD_1 (m);
56
57   /*
58     FIXME: why is this necessary?
59     all bookpaper_ should be protected by themselves.
60   */
61   if (mo->parent_)
62     scm_gc_mark (mo->parent_->self_scm ());
63   
64
65   mo->derived_mark ();
66   return mo->scope_;
67 }
68
69 void
70 Output_def::derived_mark () {}
71
72 void
73 assign_context_def (Output_def * m, SCM transdef)
74 {
75   Context_def *tp = unsmob_context_def (transdef);
76   assert (tp);
77
78   if (tp)
79     {
80       SCM sym = tp->get_context_name ();
81       scm_module_define (m->scope_, sym, transdef);
82     }  
83 }
84
85 /*
86   find the translator for NAME. NAME must be a symbol.
87 */
88 SCM
89 find_context_def (Output_def const *m, SCM name)
90 {  
91   Context_def *cd = unsmob_context_def (m->lookup_variable (name));
92   return cd ? cd->self_scm () : SCM_EOL;
93 }
94
95 int
96 Output_def::print_smob (SCM s, SCM p, scm_print_state *)
97 {
98   Output_def * def = unsmob_output_def (s);
99   scm_puts ("#< ", p);
100   scm_puts (classname (def), p);
101   
102   (void)def;
103   scm_puts (">", p);
104   return 1;
105 }
106
107 Real
108 Output_def::get_dimension (SCM s) const
109 {
110   SCM val = lookup_variable (s);
111   return ly_scm2double (val);
112 }
113
114
115 SCM
116 Output_def::lookup_variable (SCM sym) const
117 {
118   SCM var = ly_module_lookup (scope_, sym);
119   if (SCM_VARIABLEP (var))
120     return SCM_VARIABLE_REF (var);
121   
122   if (parent_)
123     return parent_->lookup_variable (sym);
124   
125   return SCM_EOL;
126 }
127
128 SCM
129 Output_def::c_variable (String s) const
130 {
131   return lookup_variable (ly_symbol2scm (s.to_str0 ()));
132 }
133
134 void
135 Output_def::set_variable (SCM sym, SCM val)
136 {
137   scm_module_define (scope_, sym, val);
138 }
139
140 LY_DEFINE (ly_paper_lookup, "ly:output-def-lookup",
141            2, 0, 0, (SCM pap, SCM sym),
142            "Lookup @var{sym} in @var{pap}. "
143            "Return the value or @code{'()} if undefined.")
144 {
145   Output_def *op = unsmob_output_def (pap);
146   SCM_ASSERT_TYPE (op, pap, SCM_ARG1, __FUNCTION__, "Output_def");
147   SCM_ASSERT_TYPE (ly_c_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");
148
149   return op->lookup_variable (sym);
150 }
151
152 LY_DEFINE (ly_output_def_scope, "ly:output-def-scope",
153            1, 0,0, (SCM def),
154            "Get the variable scope inside @var{def}.")
155 {
156   Output_def *op = unsmob_output_def (def);
157   SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
158   return op->scope_;
159 }
160
161
162 LY_DEFINE (ly_output_def_parent, "ly:output-def-parent",
163            1, 0, 0, (SCM def),
164            "Get the parent output-def of @var{def}.")
165 {
166   Output_def *op = unsmob_output_def (def);
167   SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
168   return op->parent_ ? op->parent_->self_scm () : SCM_EOL;
169 }
170
171
172 LY_DEFINE (ly_output_def_clone, "ly:output-def-clone",
173            1, 0, 0, (SCM def),
174            "Clone @var{def}.")
175 {
176   Output_def *op = unsmob_output_def (def);
177   SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
178   SCM s = op->clone ()->self_scm ();
179   scm_gc_unprotect_object (s);
180   return s;
181 }
182
183 LY_DEFINE(ly_output_description, "ly:output-description",
184           1,0,0,
185           (SCM output_def),
186           "Return the description of translators in @var{output-def}.")
187 {
188   Output_def *id = unsmob_output_def (output_def);
189   
190   SCM al =ly_module_to_alist (id->scope_);
191
192   SCM l = SCM_EOL;
193   for (SCM s = al ; ly_c_pair_p (s); s = ly_cdr (s))
194     {
195       Context_def * td = unsmob_context_def (ly_cdar (s));
196       SCM key = ly_caar (s);
197       if (td && key == td->get_context_name ())
198         {
199           
200           l = scm_cons (scm_cons (key, td->to_alist ()),  l);
201         }
202     }
203   return l;  
204 }
205   
206
207
208 #include "interval.hh"
209
210 /* FIXME.  This is broken until we have a generic way of
211    putting lists inside the \paper block.  */
212 Interval
213 line_dimensions_int (Output_def *def, int n)
214 {
215   Real lw =  def->get_dimension (ly_symbol2scm ("linewidth"));
216   Real ind = n ? 0.0 : def->get_dimension (ly_symbol2scm ("indent"));
217
218   return Interval (ind, lw);
219 }
220
221 LY_DEFINE (ly_paper_def_p, "ly:paper-def?",
222            1, 0, 0, (SCM def),
223            "Is @var{def} a paper definition?")
224 {
225   return ly_bool2scm (unsmob_output_def (def));
226 }