]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
(get_system_specs): separate generation of
[lilypond.git] / lily / paper-book.cc
1 /*
2   paper-book.cc -- implement Paper_book
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2006 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "paper-book.hh"
10
11 #include "main.hh"
12 #include "output-def.hh"
13 #include "paper-score.hh"
14 #include "paper-system.hh"
15 #include "text-interface.hh"
16 #include "warn.hh"
17
18 #include "ly-smobs.icc"
19
20 Paper_book::Paper_book ()
21 {
22   header_ = SCM_EOL;
23   header_0_ = SCM_EOL;
24   pages_ = SCM_BOOL_F;
25   scores_ = SCM_EOL;
26   performances_ = SCM_EOL;
27   systems_ = SCM_BOOL_F;
28
29   paper_ = 0;
30   smobify_self ();
31 }
32
33 Paper_book::~Paper_book ()
34 {
35 }
36
37 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
38 IMPLEMENT_SMOBS (Paper_book);
39 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?");
40
41 SCM
42 Paper_book::mark_smob (SCM smob)
43 {
44   Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
45   if (b->paper_)
46     scm_gc_mark (b->paper_->self_scm ());
47   scm_gc_mark (b->header_);
48   scm_gc_mark (b->header_0_);
49   scm_gc_mark (b->pages_);
50   scm_gc_mark (b->performances_);
51   scm_gc_mark (b->scores_);
52   return b->systems_;
53 }
54
55 int
56 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
57 {
58   Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
59   (void)b;
60   scm_puts ("#<Paper_book>", port);
61   return 1;
62 }
63
64 SCM
65 dump_fields ()
66 {
67   SCM fields = SCM_EOL;
68   for (vsize i = dump_header_fieldnames_global.size (); i--;)
69     fields
70       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
71                   fields);
72   return fields;
73 }
74
75 void
76 Paper_book::add_score (SCM s)
77 {
78   scores_ = scm_cons (s, scores_);
79 }
80
81 void
82 Paper_book::add_performance (SCM s)
83 {
84   performances_ = scm_cons (s, performances_);
85 }
86
87 void
88 Paper_book::output (SCM output_channel)
89 {
90   if (scm_is_pair (performances_))
91     {
92       SCM proc = ly_lily_module_constant ("paper-book-write-midis");
93
94       scm_call_2 (proc, self_scm (), output_channel);
95     }
96
97   if (scores_ == SCM_EOL)
98     return;
99
100   /* Generate all stencils to trigger font loads.  */
101   pages ();
102
103   SCM scopes = SCM_EOL;
104   if (ly_is_module (header_))
105     scopes = scm_cons (header_, scopes);
106
107   string mod_nm = "scm framework-" + output_backend_global;
108
109   SCM mod = scm_c_resolve_module (mod_nm.c_str ());
110   if (make_print)
111     {
112       SCM func = scm_c_module_lookup (mod, "output-framework");
113
114       func = scm_variable_ref (func);
115       scm_apply_0 (func, scm_list_n (output_channel,
116                                      self_scm (),
117                                      scopes,
118                                      dump_fields (),
119                                      SCM_UNDEFINED));
120     }
121
122   if (make_preview)
123     {
124       SCM func = scm_c_module_lookup (mod, "output-preview-framework");
125       func = scm_variable_ref (func);
126       scm_apply_0 (func, scm_list_n (output_channel,
127                                      self_scm (),
128                                      scopes,
129                                      dump_fields (),
130                                      SCM_UNDEFINED));
131     }
132 }
133
134 void
135 Paper_book::classic_output (SCM output)
136 {
137   /* Generate all stencils to trigger font loads.  */
138   systems ();
139
140   SCM scopes = SCM_EOL;
141   if (ly_is_module (header_))
142     scopes = scm_cons (header_, scopes);
143
144   if (ly_is_module (header_0_))
145     scopes = scm_cons (header_0_, scopes);
146
147   string format = output_backend_global;
148   string mod_nm = "scm framework-" + format;
149
150   SCM mod = scm_c_resolve_module (mod_nm.c_str ());
151   SCM func = scm_c_module_lookup (mod, "output-classic-framework");
152
153   func = scm_variable_ref (func);
154   scm_apply_0 (func, scm_list_n (output,
155                                  self_scm (),
156                                  scopes,
157                                  dump_fields (),
158                                  SCM_UNDEFINED));
159
160   progress_indication ("\n");
161 }
162
163 /* TODO: resurrect more complex user-tweaks for titling?  */
164 Stencil
165 Paper_book::book_title ()
166 {
167   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
168   Stencil title;
169
170   SCM scopes = SCM_EOL;
171   if (ly_is_module (header_))
172     scopes = scm_cons (header_, scopes);
173
174   SCM tit = SCM_EOL;
175   if (ly_is_procedure (title_func))
176     tit = scm_call_2 (title_func,
177                       paper_->self_scm (),
178                       scopes);
179
180   if (unsmob_stencil (tit))
181     title = *unsmob_stencil (tit);
182
183   if (!title.is_empty ())
184     title.align_to (Y_AXIS, UP);
185
186   return title;
187 }
188
189 Stencil
190 Paper_book::score_title (SCM header)
191 {
192   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
193
194   Stencil title;
195
196   SCM scopes = SCM_EOL;
197   if (ly_is_module (header_))
198     scopes = scm_cons (header_, scopes);
199
200   if (ly_is_module (header))
201     scopes = scm_cons (header, scopes);
202
203   SCM tit = SCM_EOL;
204   if (ly_is_procedure (title_func))
205     tit = scm_call_2 (title_func,
206                       paper_->self_scm (),
207                       scopes);
208
209   if (unsmob_stencil (tit))
210     title = *unsmob_stencil (tit);
211
212   if (!title.is_empty ())
213     title.align_to (Y_AXIS, UP);
214
215   return title;
216 }
217
218 void
219 set_system_penalty (Prob *ps, SCM header)
220 {
221   if (ly_is_module (header))
222     {
223       SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
224       if (SCM_VARIABLEP (force)
225           && scm_is_bool (SCM_VARIABLE_REF (force)))
226         {
227           ps->set_property ("penalty",
228                             scm_from_int(to_boolean (SCM_VARIABLE_REF (force))
229                                          ? -10000
230                                          : 10000));
231         }
232     }
233 }
234
235
236 SCM
237 Paper_book::get_score_title (SCM header)
238 {
239   Stencil title = score_title (header);
240   if (title.is_empty ())
241     title = score_title (header_);
242   if (!title.is_empty ())
243     {
244       /*
245         TODO: this should come from the \layout {} block, which should
246         override settings from \paper {}
247       */
248       SCM props = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
249       Prob *ps = make_paper_system (props);
250       paper_system_set_stencil (ps, title);
251       set_system_penalty (ps, header);
252
253       return ps->self_scm();
254     }
255
256   return SCM_BOOL_F;
257 }
258
259 SCM
260 Paper_book::get_system_specs ()
261 {
262   SCM system_specs = SCM_EOL;
263   
264   Stencil title = book_title ();
265   if (!title.is_empty ())
266     {
267       SCM props = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
268       Prob *ps = make_paper_system (props);
269       paper_system_set_stencil (ps, title);
270       set_system_penalty (ps, header_);
271
272       system_specs = scm_cons (ps->self_scm (), system_specs);
273       ps->unprotect ();
274     }
275
276   SCM page_properties
277     = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
278                   paper_->self_scm ());
279
280   SCM header = SCM_EOL;
281   for (SCM s = scm_reverse (scores_); s != SCM_EOL; s = scm_cdr (s))
282     {
283       if (ly_is_module (scm_car (s)))
284         {
285           header = scm_car (s);
286           if (header_0_ == SCM_EOL)
287             header_0_ = header;
288         }
289       else if (Music_output *mop = unsmob_music_output (scm_car (s)))
290         {
291           if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
292             {
293               SCM title = get_score_title (header);
294               if (unsmob_prob (title))
295                 {
296                   system_specs = scm_cons (title, system_specs);
297                   unsmob_prob (title)->unprotect ();
298                 }
299
300               header = SCM_EOL;
301               system_specs = scm_cons (pscore->self_scm (), system_specs);
302             }
303           else
304             {
305               /*
306                 Ignore MIDI
307               */
308             }
309         }
310       else if (Text_interface::is_markup (scm_car (s)))
311         {
312           SCM t = Text_interface::interpret_markup (paper_->self_scm (),
313                                                     page_properties,
314                                                     scm_car (s));
315           
316           // TODO: init props
317           Prob *ps = make_paper_system (SCM_EOL);
318           paper_system_set_stencil (ps, *unsmob_stencil (t));
319           ps->set_property ("is-title", SCM_BOOL_T); 
320           system_specs = scm_cons (ps->self_scm (), system_specs);
321           ps->unprotect ();
322           
323           // FIXME: figure out penalty.
324           //set_system_penalty (ps, scores_[i].header_);
325         }
326       else
327         assert (0);
328     }
329
330   system_specs = scm_reverse_x (system_specs, SCM_EOL);
331   return system_specs;
332 }
333
334 SCM
335 Paper_book::systems ()
336 {
337   if (systems_ != SCM_BOOL_F)
338     return systems_;
339
340   systems_ = SCM_EOL;
341   SCM specs = get_system_specs ();
342   for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
343     {
344       if (Paper_score *pscore = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
345         {
346           SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
347           system_list = scm_reverse (system_list);
348           systems_ = scm_append (scm_list_2 (system_list, systems_));
349         }
350       else
351         {
352           systems_ = scm_cons (scm_car (s), systems_);
353         }
354     }
355   
356   systems_ = scm_reverse (systems_);
357
358   int i = 0;
359   Prob *last = 0;
360   for (SCM s = systems_; s != SCM_EOL; s = scm_cdr (s))
361     {
362       Prob *ps = unsmob_prob (scm_car (s));
363       ps->set_property ("number", scm_from_int (++i));
364
365       if (last
366           && to_boolean (last->get_property ("is-title"))
367           && !scm_is_number (ps->get_property ("penalty")))
368         ps->set_property ("penalty", scm_from_int (10000));
369       last = ps;
370     }
371
372   return systems_;
373 }
374
375 SCM
376 Paper_book::pages ()
377 {
378   if (SCM_BOOL_F != pages_)
379     return pages_;
380
381   pages_ = SCM_EOL;
382   SCM proc = paper_->c_variable ("page-breaking");
383   pages_ = scm_apply_0 (proc, scm_list_2 (systems (), self_scm ()));
384   return pages_;
385 }
386
387 SCM
388 Paper_book::performances () const
389 {
390   return scm_reverse (performances_);
391 }