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