]> git.donarmstrong.com Git - lilypond.git/blob - lily/output-def.cc
* lily/paper-score.cc (process): Do not show progress newline.
[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 #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"
20
21 Output_def::Output_def ()
22 {
23   scope_ = SCM_EOL;
24   parent_ = 0;
25   smobify_self ();
26
27   scope_ = ly_make_anonymous_module (false);
28 }
29
30 Output_def::Output_def (Output_def const &s)
31 {
32   scope_ = SCM_EOL;
33   parent_ = 0;
34   smobify_self ();
35
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_);
40 }
41
42 Output_def::~Output_def ()
43 {
44 }
45
46 #include "ly-smobs.icc"
47 IMPLEMENT_SMOBS (Output_def);
48 IMPLEMENT_DEFAULT_EQUAL_P (Output_def);
49
50 SCM
51 Output_def::mark_smob (SCM m)
52 {
53   Output_def *mo = (Output_def*) SCM_CELL_WORD_1 (m);
54
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 ());
59
60   mo->derived_mark ();
61   return mo->scope_;
62 }
63
64 void
65 Output_def::derived_mark ()
66 {
67 }
68
69 void
70 assign_context_def (Output_def * m, SCM transdef)
71 {
72   Context_def *tp = unsmob_context_def (transdef);
73   assert (tp);
74
75   if (tp)
76     {
77       SCM sym = tp->get_context_name ();
78       scm_module_define (m->scope_, sym, transdef);
79     }  
80 }
81
82 /* find the translator for NAME. NAME must be a symbol. */
83 SCM
84 find_context_def (Output_def const *m, SCM name)
85 {  
86   Context_def *cd = unsmob_context_def (m->lookup_variable (name));
87   return cd ? cd->self_scm () : SCM_EOL;
88 }
89
90 int
91 Output_def::print_smob (SCM s, SCM p, scm_print_state *)
92 {
93   Output_def * def = unsmob_output_def (s);
94   scm_puts ("#< ", p);
95   scm_puts (classname (def), p);
96   
97   (void)def;
98   scm_puts (">", p);
99   return 1;
100 }
101
102 Real
103 Output_def::get_dimension (SCM s) const
104 {
105   SCM val = lookup_variable (s);
106   return ly_scm2double (val);
107 }
108
109 SCM
110 Output_def::lookup_variable (SCM sym) const
111 {
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);
115   
116   if (parent_)
117     return parent_->lookup_variable (sym);
118   
119   return SCM_EOL;
120 }
121
122 SCM
123 Output_def::c_variable (String s) const
124 {
125   return lookup_variable (ly_symbol2scm (s.to_str0 ()));
126 }
127
128 void
129 Output_def::set_variable (SCM sym, SCM val)
130 {
131   scm_module_define (scope_, sym, val);
132 }
133
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.")
138 {
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");
142
143   return op->lookup_variable (sym);
144 }
145
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}.")
149 {
150   Output_def *op = unsmob_output_def (def);
151   SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
152   return op->scope_;
153 }
154
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}.")
158 {
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;
162 }
163
164 LY_DEFINE (ly_output_def_clone, "ly:output-def-clone",
165            1, 0, 0, (SCM def),
166            "Clone @var{def}.")
167 {
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;
173 }
174
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}.")
178 {
179   Output_def *id = unsmob_output_def (output_def);
180   
181   SCM al = ly_module2alist (id->scope_);
182
183   SCM ell = SCM_EOL;
184   for (SCM s = al; ly_c_pair_p (s); s = ly_cdr (s))
185     {
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);
190     }
191   return ell;  
192 }
193   
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)
198 {
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);
202 }
203
204 LY_DEFINE (ly_paper_def_p, "ly:paper-def?",
205            1, 0, 0, (SCM def),
206            "Is @var{def} a paper definition?")
207 {
208   return ly_bool2scm (unsmob_output_def (def));
209 }
210
211 LY_DEFINE (ly_bookpaper_outputscale, "ly:bookpaper-outputscale",
212           1, 0, 0, (SCM bp),
213           "Get outputscale for BP.")
214 {
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));
218 }
219
220 LY_DEFINE (ly_make_output_def, "ly:make-output-def",
221            0, 0, 0, (),
222            "Make a output def.")
223 {
224   Output_def *bp = new Output_def ;
225   return scm_gc_unprotect_object (bp->self_scm ());
226 }
227
228