]> git.donarmstrong.com Git - lilypond.git/blob - lily/score.cc
* input/regression/score-text.ly: Really add.
[lilypond.git] / lily / score.cc
1 /*
2   score.cc -- implement Score
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "score.hh"
10
11 #include <cstdio>
12
13 #include "book.hh"
14 #include "cpu-timer.hh"
15 #include "global-context.hh"
16 #include "lily-parser.hh"
17 #include "lilypond-key.hh"
18 #include "ly-smobs.icc"
19 #include "main.hh"
20 #include "music-iterator.hh"
21 #include "music.hh"
22 #include "output-def.hh"
23 #include "output-def.hh"
24 #include "paper-book.hh"
25 #include "paper-score.hh"
26 #include "scm-hash.hh"
27 #include "warn.hh"
28
29 Score::Score ()
30   : Input ()
31 {
32   header_ = SCM_EOL;
33   music_ = SCM_EOL;
34   texts_ = SCM_EOL;
35   error_found_ = false;
36   smobify_self ();
37 }
38
39 Score::~Score ()
40 {
41 }
42
43 IMPLEMENT_SMOBS (Score);
44 IMPLEMENT_DEFAULT_EQUAL_P (Score);
45 IMPLEMENT_TYPE_P (Score, "ly:score?");
46
47 SCM
48 Score::mark_smob (SCM s)
49 {
50   Score *sc = (Score*) SCM_CELL_WORD_1 (s);
51
52   scm_gc_mark (sc->header_);
53   scm_gc_mark (sc->texts_);
54   for (int i = sc->defs_.size (); i--;)
55     scm_gc_mark (sc->defs_[i]->self_scm ());
56   return sc->music_;
57 }
58
59 int
60 Score::print_smob (SCM , SCM p, scm_print_state*)
61 {
62   scm_puts ("#<Score>", p);
63
64   return 1;
65 }
66
67 Score::Score (Score const &s)
68   : Input (s)
69 {
70   header_ = SCM_EOL;
71   music_ = SCM_EOL;
72   texts_ = SCM_EOL;
73   error_found_ = s.error_found_;
74   smobify_self ();
75
76   Music *m = unsmob_music (s.music_);
77   music_ = m ? m->clone ()->self_scm () : SCM_EOL;
78   scm_gc_unprotect_object (music_);
79   
80   for (int i = 0, n = s.defs_.size (); i < n; i++)
81     defs_.push (s.defs_[i]->clone ());
82
83   header_ = ly_make_anonymous_module (false);
84   if (ly_c_module_p (s.header_))
85     ly_module_copy (header_, s.header_);
86
87   texts_ = s.texts_;
88 }
89
90
91 void
92 default_rendering (SCM music, SCM outdef,
93                    SCM book_outputdef,
94                    SCM header, SCM texts,
95                    SCM outname,
96                    SCM key)
97 {
98   SCM scaled_def = outdef;
99   SCM scaled_bookdef = book_outputdef;
100   
101   Output_def *bpd = unsmob_output_def (book_outputdef);
102
103   /* ugh.  */
104   if (bpd->c_variable ("is-paper") == SCM_BOOL_T)
105     {
106       Real scale = scm_to_double (bpd->c_variable ("outputscale"));
107       
108       Output_def *def = scale_output_def (unsmob_output_def (outdef), scale);
109       scaled_def = def->self_scm ();
110
111       scaled_bookdef = scale_output_def (bpd, scale)->self_scm ();
112       unsmob_output_def (scaled_def)->parent_
113         = unsmob_output_def (scaled_bookdef);
114       
115       scm_gc_unprotect_object (scaled_bookdef);
116       scm_gc_unprotect_object (scaled_def);
117     }
118   
119   SCM context = ly_run_translator (music, scaled_def, key);
120   if (Global_context *g = dynamic_cast<Global_context*>
121       (unsmob_context (context)))
122     {
123       SCM systems = ly_format_output (context, outname);
124       Music_output *output = g->get_output ();
125       if (systems != SCM_UNDEFINED)
126         {
127           /* ugh, this is strange, Paper_book without a Book object. */
128           Paper_book *paper_book = new Paper_book ();
129           paper_book->header_ = header;
130           paper_book->paper_ = unsmob_output_def (scaled_bookdef);
131           
132           Score_systems sc;
133           sc.systems_ = systems;
134           sc.header_ = header;
135           sc.texts_ = texts;
136
137           paper_book->score_systems_.push (sc);
138           
139           paper_book->classic_output (ly_scm2string (outname));
140           scm_gc_unprotect_object (paper_book->self_scm ());
141         }
142       delete output;
143     }
144
145   scm_remember_upto_here_1 (scaled_def);
146   scm_remember_upto_here_1 (scaled_bookdef);
147 }
148
149 /*
150 Format score, return systems. OUTNAME is still passed to create a midi
151 file.
152
153 LAYOUTBOOK should be scaled already.
154
155 */
156 SCM
157 Score::book_rendering (String outname,
158                        Output_def *layoutbook,
159                        Output_def *default_def,
160                        Object_key *book_key)
161 {
162   if (error_found_)
163     return SCM_EOL;
164    
165   SCM scaled_bookdef = SCM_EOL;
166   Real scale = 1.0;
167
168   if (layoutbook && layoutbook->c_variable ("is-paper") == SCM_BOOL_T)
169     scale = scm_to_double (layoutbook->c_variable ("outputscale"));
170   
171   SCM out = scm_makfrom0str (outname.to_str0 ());
172   SCM systems = SCM_EOL;
173   int outdef_count = defs_.size ();
174
175   Object_key * key = new Lilypond_general_key (book_key, user_key_, 0);
176   SCM scm_key = key->self_scm();
177   scm_gc_unprotect_object (scm_key);
178   
179   for (int i = 0; !i || i < outdef_count; i++)
180     {
181       Output_def *def = outdef_count ? defs_[i] : default_def;
182       SCM scaled = SCM_EOL;
183       if (def->c_variable ("is-layout") == SCM_BOOL_T)
184         {
185           def = scale_output_def (def, scale);
186           def->parent_ = layoutbook;
187           scaled = def->self_scm ();
188           scm_gc_unprotect_object (scaled);
189         }
190
191       /* TODO: fix or junk --no-layout.  */
192       SCM context = ly_run_translator (music_, def->self_scm (), scm_key);
193       if (dynamic_cast<Global_context*> (unsmob_context (context)))
194         {
195           SCM s = ly_format_output (context, out);
196           if (s != SCM_UNDEFINED)
197             systems = s;
198         }
199
200       scm_remember_upto_here_1 (scaled);
201     }
202
203   scm_remember_upto_here_1 (scm_key);
204   scm_remember_upto_here_1 (scaled_bookdef);
205   return systems;
206 }
207
208
209
210
211
212 void
213 Score::set_music (SCM music, SCM parser)
214 {
215   /* URG? */
216   SCM check_funcs = ly_lily_module_constant ("toplevel-music-functions");
217   for (; scm_is_pair (check_funcs); check_funcs = scm_cdr (check_funcs))
218     music = scm_call_2 (scm_car (check_funcs), music, parser);
219
220   if (unsmob_music (music_))
221     {
222       unsmob_music (music)->origin ()->error (_("Already have music in score"));
223       unsmob_music (music_)->origin ()->error (_("This is the previous music"));
224     }
225   Music * m = unsmob_music (music);
226   if (m && to_boolean (m->get_property ("error-found")))
227     {
228       m->origin()->error (_("Error found in this music expression. Ignoring it"));
229       
230       this->error_found_ = this->error_found_ || to_boolean (m->get_property ("error-found"));
231       
232     }
233
234   if (this->error_found_)
235     this->music_ = SCM_EOL; 
236   else
237     this->music_ = music;
238
239 }
240
241 SCM
242 Score::get_music () const
243 {
244   return music_;
245 }