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