]> git.donarmstrong.com Git - lilypond.git/blob - lily/output-def.cc
* configure.in: Test for and accept lmodern if EC fonts not found.
[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 "output-def.hh"
10
11 #include "context-def.hh"
12 #include "file-path.hh"
13 #include "global-context.hh"
14 #include "interval.hh"
15 #include "ly-module.hh"
16 #include "main.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 paper_ should be protected by themselves. */
57   if (mo->parent_)
58     scm_gc_mark (mo->parent_->self_scm ());
59
60   return mo->scope_;
61 }
62
63 void
64 assign_context_def (Output_def * m, SCM transdef)
65 {
66   Context_def *tp = unsmob_context_def (transdef);
67   assert (tp);
68
69   if (tp)
70     {
71       SCM sym = tp->get_context_name ();
72       m->set_variable (sym, transdef);
73     }  
74 }
75
76 /* find the translator for NAME. NAME must be a symbol. */
77 SCM
78 find_context_def (Output_def const *m, SCM name)
79 {  
80   Context_def *cd = unsmob_context_def (m->lookup_variable (name));
81   return cd ? cd->self_scm () : SCM_EOL;
82 }
83
84 int
85 Output_def::print_smob (SCM s, SCM p, scm_print_state *)
86 {
87   Output_def * def = unsmob_output_def (s);
88   scm_puts ("#< ", p);
89   scm_puts (classname (def), p);
90   
91   (void)def;
92   scm_puts (">", p);
93   return 1;
94 }
95
96 Real
97 Output_def::get_dimension (SCM s) const
98 {
99   SCM val = lookup_variable (s);
100   return scm_to_double (val);
101 }
102
103 SCM
104 Output_def::lookup_variable (SCM sym) const
105 {
106   SCM var = ly_module_lookup (scope_, sym);
107   if (SCM_VARIABLEP (var) && SCM_VARIABLE_REF(var) != SCM_UNDEFINED)
108     return SCM_VARIABLE_REF (var);
109   
110   if (parent_)
111     return parent_->lookup_variable (sym);
112   
113   return SCM_EOL;
114 }
115
116 SCM
117 Output_def::c_variable (String s) const
118 {
119   return lookup_variable (ly_symbol2scm (s.to_str0 ()));
120 }
121
122 void
123 Output_def::set_variable (SCM sym, SCM val)
124 {
125   scm_module_define (scope_, sym, val);
126 }
127
128 LY_DEFINE (ly_layout_lookup, "ly:output-def-lookup",
129            2, 0, 0, (SCM pap, SCM sym),
130            "Lookup @var{sym} in @var{pap}. "
131            "Return the value or @code{'()} if undefined.")
132 {
133   Output_def *op = unsmob_output_def (pap);
134   SCM_ASSERT_TYPE (op, pap, SCM_ARG1, __FUNCTION__, "Output_def");
135   SCM_ASSERT_TYPE (scm_is_symbol (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");
136
137   return op->lookup_variable (sym);
138 }
139
140 LY_DEFINE (ly_output_def_scope, "ly:output-def-scope",
141            1, 0, 0, (SCM def),
142            "Get the variable scope inside @var{def}.")
143 {
144   Output_def *op = unsmob_output_def (def);
145   SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
146   return op->scope_;
147 }
148
149 LY_DEFINE (ly_output_def_parent, "ly:output-def-parent",
150            1, 0, 0, (SCM def),
151            "Get the parent output-def of @var{def}.")
152 {
153   Output_def *op = unsmob_output_def (def);
154   SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
155   return op->parent_ ? op->parent_->self_scm () : SCM_EOL;
156 }
157
158 LY_DEFINE (ly_output_def_clone, "ly:output-def-clone",
159            1, 0, 0, (SCM def),
160            "Clone @var{def}.")
161 {
162   Output_def *op = unsmob_output_def (def);
163   SCM_ASSERT_TYPE (op, def, SCM_ARG1, __FUNCTION__, "Output definition");
164   SCM s = op->clone ()->self_scm ();
165   scm_gc_unprotect_object (s);
166   return s;
167 }
168
169 LY_DEFINE (ly_output_description, "ly:output-description",
170            1, 0, 0, (SCM output_def),
171            "Return the description of translators in @var{output-def}.")
172 {
173   Output_def *id = unsmob_output_def (output_def);
174   
175   SCM al = ly_module2alist (id->scope_);
176
177   SCM ell = SCM_EOL;
178   for (SCM s = al; scm_is_pair (s); s = scm_cdr (s))
179     {
180       Context_def * td = unsmob_context_def (scm_cdar (s));
181       SCM key = scm_caar (s);
182       if (td && key == td->get_context_name ())
183         ell = scm_cons (scm_cons (key, td->to_alist ()),  ell);
184     }
185   return ell;  
186 }
187   
188 /* FIXME.  This is broken until we have a generic way of
189    putting lists inside the \layout block.  */
190 Interval
191 line_dimensions_int (Output_def *def, int n)
192 {
193   Real lw = def->get_dimension (ly_symbol2scm ("linewidth"));
194   Real ind = n ? 0.0 : def->get_dimension (ly_symbol2scm ("indent"));
195   return Interval (ind, lw);
196 }
197
198 LY_DEFINE (ly_layout_def_p, "ly:layout-def?",
199            1, 0, 0, (SCM def),
200            "Is @var{def} a layout definition?")
201 {
202   return ly_bool2scm (unsmob_output_def (def));
203 }
204
205 LY_DEFINE (ly_paper_outputscale, "ly:paper-outputscale",
206           1, 0, 0, (SCM bp),
207           "Get outputscale for BP.")
208 {
209   Output_def *b = unsmob_output_def (bp);
210   SCM_ASSERT_TYPE (b, bp, SCM_ARG1, __FUNCTION__, "paper");
211   return scm_make_real (output_scale (b));
212 }
213
214 LY_DEFINE (ly_make_output_def, "ly:make-output-def",
215            0, 0, 0, (),
216            "Make a output def.")
217 {
218   Output_def *bp = new Output_def ;
219   return scm_gc_unprotect_object (bp->self_scm ());
220 }
221
222