]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
Merge branch 'master' into nested-bookparts
[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--2007 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-column.hh"
15 #include "paper-score.hh"
16 #include "paper-system.hh"
17 #include "text-interface.hh"
18 #include "warn.hh"
19 #include "program-option.hh"
20 #include "page-marker.hh"
21
22 #include "ly-smobs.icc"
23
24 Paper_book::Paper_book ()
25 {
26   header_ = SCM_EOL;
27   header_0_ = SCM_EOL;
28   pages_ = SCM_BOOL_F;
29   scores_ = SCM_EOL;
30   bookparts_ = SCM_EOL;
31   performances_ = SCM_EOL;
32   systems_ = SCM_BOOL_F;
33
34   paper_ = 0;
35   parent_ = 0;
36   smobify_self ();
37 }
38
39 Paper_book::~Paper_book ()
40 {
41 }
42
43 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
44 IMPLEMENT_SMOBS (Paper_book);
45 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?");
46
47 SCM
48 Paper_book::mark_smob (SCM smob)
49 {
50   Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
51   if (b->paper_)
52     scm_gc_mark (b->paper_->self_scm ());
53   if (b->parent_)
54     scm_gc_mark (b->parent_->self_scm ());
55   scm_gc_mark (b->header_);
56   scm_gc_mark (b->header_0_);
57   scm_gc_mark (b->pages_);
58   scm_gc_mark (b->performances_);
59   scm_gc_mark (b->scores_);
60   scm_gc_mark (b->bookparts_);
61   return b->systems_;
62 }
63
64 int
65 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
66 {
67   Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
68   (void)b;
69   scm_puts ("#<Paper_book>", port);
70   return 1;
71 }
72
73 Output_def *
74 Paper_book::top_paper ()
75 {
76   Output_def *paper = paper_;
77   while (paper->parent_)
78     paper = paper->parent_;
79   return paper;
80 }
81
82 SCM
83 dump_fields ()
84 {
85   SCM fields = SCM_EOL;
86   for (vsize i = dump_header_fieldnames_global.size (); i--;)
87     fields
88       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].c_str ()),
89                   fields);
90   return fields;
91 }
92
93 void
94 Paper_book::add_score (SCM s)
95 {
96   scores_ = scm_cons (s, scores_);
97 }
98
99 void
100 Paper_book::add_bookpart (SCM p)
101 {
102   bookparts_ = scm_cons (p, bookparts_);
103 }
104
105 void
106 Paper_book::add_performance (SCM s)
107 {
108   performances_ = scm_cons (s, performances_);
109 }
110
111 void
112 Paper_book::output_aux (SCM output_channel,
113                         bool is_first,
114                         bool is_last,
115                         int *first_page_number,
116                         int *first_performance_number)
117 {
118   if (scm_is_pair (performances_))
119     {
120       SCM proc = ly_lily_module_constant ("write-performances-midis");
121  
122       scm_call_3 (proc,
123                   performances (),
124                   output_channel,
125                   scm_long2num (*first_performance_number));
126       *first_performance_number += scm_ilength (performances_);
127     }
128
129   if (scm_is_pair (bookparts_))
130     {
131       bool is_first_part = is_first;
132       for (SCM p = scm_reverse (bookparts_); scm_is_pair (p); p = scm_cdr (p))
133         if (Paper_book *pbookpart = unsmob_paper_book (scm_car (p)))
134           {
135             bool is_last_part = (is_last && !scm_is_pair (scm_cdr (p)));
136             pbookpart->output_aux (output_channel,
137                                    is_first_part,
138                                    is_last_part,
139                                    first_page_number,
140                                    first_performance_number);
141             is_first_part = false;
142           }
143     }
144   else
145     {
146       if (scores_ == SCM_EOL)
147         return;
148       paper_->set_variable (ly_symbol2scm ("part-first-page-number"),
149                             scm_long2num (*first_page_number));
150       paper_->set_variable (ly_symbol2scm ("part-is-first"),
151                             ly_bool2scm (is_first));
152       paper_->set_variable (ly_symbol2scm ("part-is-last"),
153                             ly_bool2scm (is_last));
154       /* Generate all stencils to trigger font loads.  */
155       *first_page_number += scm_ilength (pages ());
156     }
157 }
158
159 void
160 Paper_book::output (SCM output_channel)
161 {
162   int first_page_number = robust_scm2int (paper_->c_variable ("first-page-number"), 1);
163   int first_performance_number = 0;
164   output_aux (output_channel,
165               true,
166               true,
167               &first_page_number,
168               &first_performance_number);
169
170   SCM scopes = SCM_EOL;
171   if (ly_is_module (header_))
172     scopes = scm_cons (header_, scopes);
173
174   string mod_nm = "scm framework-" + get_output_backend_name ();
175
176   SCM mod = scm_c_resolve_module (mod_nm.c_str ());
177
178   if (get_program_option ("print-pages"))
179     {
180       SCM func = scm_c_module_lookup (mod, "output-framework");
181
182       func = scm_variable_ref (func);
183       scm_apply_0 (func, scm_list_n (output_channel,
184                                      self_scm (),
185                                      scopes,
186                                      dump_fields (),
187                                      SCM_UNDEFINED));
188     }
189
190   if (get_program_option ("preview"))
191     {
192       SCM func = scm_c_module_lookup (mod, "output-preview-framework");
193       func = scm_variable_ref (func);
194       scm_apply_0 (func, scm_list_n (output_channel,
195                                      self_scm (),
196                                      scopes,
197                                      dump_fields (),
198                                      SCM_UNDEFINED));
199     }
200 }
201
202 void
203 Paper_book::classic_output_aux (SCM output,
204                                 int *first_performance_number)
205 {
206   if (scm_is_pair (performances_))
207     {
208       SCM proc = ly_lily_module_constant ("write-performances-midis");
209       scm_call_3 (proc,
210                   performances (),
211                   output,
212                   scm_long2num (*first_performance_number));
213       *first_performance_number += scm_ilength (performances_);
214     }
215   
216   /* Generate all stencils to trigger font loads.  */
217   systems ();
218 }
219
220 void
221 Paper_book::classic_output (SCM output)
222 {
223   int first_performance_number = 0;
224   classic_output_aux (output, &first_performance_number);
225
226   SCM scopes = SCM_EOL;
227   if (ly_is_module (header_))
228     scopes = scm_cons (header_, scopes);
229
230   if (ly_is_module (header_0_))
231     scopes = scm_cons (header_0_, scopes);
232
233   string format = get_output_backend_name ();
234   string mod_nm = "scm framework-" + format;
235
236   SCM mod = scm_c_resolve_module (mod_nm.c_str ());
237   SCM func = scm_c_module_lookup (mod, "output-classic-framework");
238
239   func = scm_variable_ref (func);
240   scm_apply_0 (func, scm_list_n (output,
241                                  self_scm (),
242                                  scopes,
243                                  dump_fields (),
244                                  SCM_UNDEFINED));
245
246   progress_indication ("\n");
247 }
248
249 /* TODO: resurrect more complex user-tweaks for titling?  */
250 Stencil
251 Paper_book::book_title ()
252 {
253   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
254   Stencil title;
255
256   SCM scopes = SCM_EOL;
257   if (ly_is_module (header_))
258     scopes = scm_cons (header_, scopes);
259
260   SCM tit = SCM_EOL;
261   if (ly_is_procedure (title_func))
262     tit = scm_call_2 (title_func,
263                       paper_->self_scm (),
264                       scopes);
265
266   if (unsmob_stencil (tit))
267     title = *unsmob_stencil (tit);
268
269   if (!title.is_empty ())
270     title.align_to (Y_AXIS, UP);
271
272   return title;
273 }
274
275 Stencil
276 Paper_book::score_title (SCM header)
277 {
278   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
279
280   Stencil title;
281
282   SCM scopes = SCM_EOL;
283   if (ly_is_module (header_))
284     scopes = scm_cons (header_, scopes);
285
286   if (ly_is_module (header))
287     scopes = scm_cons (header, scopes);
288
289   SCM tit = SCM_EOL;
290   if (ly_is_procedure (title_func))
291     tit = scm_call_2 (title_func,
292                       paper_->self_scm (),
293                       scopes);
294
295   if (unsmob_stencil (tit))
296     title = *unsmob_stencil (tit);
297
298   if (!title.is_empty ())
299     title.align_to (Y_AXIS, UP);
300
301   return title;
302 }
303
304 void
305 set_page_permission (SCM sys, SCM symbol, SCM permission)
306 {
307   if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
308     {
309       vector<Grob*> cols = ps->get_columns ();
310       if (cols.size ())
311         {
312           Paper_column *col = dynamic_cast<Paper_column*> (cols.back ());
313           col->set_property (symbol, permission);
314           col->find_prebroken_piece (LEFT)->set_property (symbol, permission);
315         }
316     }
317   else if (Prob *pb = unsmob_prob (sys))
318     pb->set_property (symbol, permission);
319 }
320
321 /* read the breakbefore property of a score block and set up the preceding
322    system-spec to honour it. That is, SYS should be the system spec that
323    immediately precedes the score (from which HEADER is taken)
324    in the get_system_specs () list */
325 void
326 set_system_penalty (SCM sys, SCM header)
327 {
328   if (ly_is_module (header))
329     {
330       SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
331       if (SCM_VARIABLEP (force)
332           && scm_is_bool (SCM_VARIABLE_REF (force)))
333         {
334           if (to_boolean (SCM_VARIABLE_REF (force)))
335             {
336               set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
337                                    ly_symbol2scm ("force"));
338               set_page_permission (sys, ly_symbol2scm ("line-break-permission"),
339                                    ly_symbol2scm ("force"));
340             }
341           else
342             set_page_permission (sys, ly_symbol2scm ("page-break-permission"), SCM_EOL);
343         }
344     }
345 }
346
347 void
348 set_labels (SCM sys, SCM labels)
349 {
350   if (Paper_score *ps = dynamic_cast<Paper_score*> (unsmob_music_output (sys)))
351     {
352       vector<Grob*> cols = ps->get_columns ();
353       if (cols.size ())
354         {
355           Paper_column *col = dynamic_cast<Paper_column*> (cols[0]);
356           col->set_property ("labels", 
357                              scm_append_x (scm_list_2 (col->get_property ("labels"),
358                                                        labels)));
359           Paper_column *col_right = dynamic_cast<Paper_column*> (col->find_prebroken_piece (RIGHT));
360           col_right->set_property ("labels", 
361                                    scm_append_x (scm_list_2 (col_right->get_property ("labels"),
362                                                              labels)));
363         }
364     }
365   else if (Prob *pb = unsmob_prob (sys))
366     pb->set_property ("labels", 
367                       scm_append_x (scm_list_2 (pb->get_property ("labels"),
368                                                 labels)));
369 }
370
371 SCM
372 Paper_book::get_score_title (SCM header)
373 {
374   Stencil title = score_title (header);
375   if (title.is_empty ())
376     title = score_title (header_);
377   if (!title.is_empty ())
378     {
379       /*
380         TODO: this should come from the \layout {} block, which should
381         override settings from \paper {}
382       */
383       SCM props = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
384       Prob *ps = make_paper_system (props);
385       paper_system_set_stencil (ps, title);
386
387       return ps->self_scm ();
388     }
389
390   return SCM_BOOL_F;
391 }
392
393
394 SCM
395 Paper_book::get_system_specs ()
396 {
397   SCM system_specs = SCM_EOL;
398   
399   Stencil title = book_title ();
400   if (!title.is_empty ())
401     {
402       SCM props = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
403       Prob *ps = make_paper_system (props);
404       paper_system_set_stencil (ps, title);
405       
406       system_specs = scm_cons (ps->self_scm (), system_specs);
407       ps->unprotect ();
408     }
409
410   SCM page_properties
411     = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
412                   paper_->self_scm ());
413
414   SCM interpret_markup_list = ly_lily_module_constant ("interpret-markup-list");
415   SCM header = SCM_EOL;
416   SCM labels = SCM_EOL;
417   for (SCM s = scm_reverse (scores_); scm_is_pair (s); s = scm_cdr (s))
418     {
419       if (ly_is_module (scm_car (s)))
420         {
421           header = scm_car (s);
422           if (header_0_ == SCM_EOL)
423             header_0_ = header;
424         }
425       else if (Page_marker *page_marker = unsmob_page_marker (scm_car (s)))
426         {
427           /* page markers are used to set page breaking/turning permission,
428              or to place bookmarking labels */ 
429           if (scm_is_symbol (page_marker->permission_symbol ()))
430             {
431               /* set previous element page break or turn permission */
432               if (scm_is_pair (system_specs))
433                 set_page_permission (scm_car (system_specs),
434                                      page_marker->permission_symbol (),
435                                      page_marker->permission_value ());
436             }
437           if (scm_is_symbol (page_marker->label ()))
438             {
439               /* The next element label is to be set */
440               labels = scm_cons (page_marker->label (), labels);
441             }
442         }
443       else if (Music_output *mop = unsmob_music_output (scm_car (s)))
444         {
445           if (Paper_score *pscore = dynamic_cast<Paper_score *> (mop))
446             {
447               SCM title = get_score_title (header);
448
449               if (scm_is_pair (system_specs))
450                 set_system_penalty (scm_car (system_specs), header);
451
452               if (unsmob_prob (title))
453                 {
454                   system_specs = scm_cons (title, system_specs);
455                   unsmob_prob (title)->unprotect ();
456                 }
457
458               header = SCM_EOL;
459               system_specs = scm_cons (pscore->self_scm (), system_specs);
460               if (scm_is_pair (labels))
461                 {
462                   set_labels (scm_car (system_specs), labels);
463                   labels = SCM_EOL;
464                 }
465             }
466           else
467             {
468               /*
469                 Ignore MIDI
470               */
471             }
472         }
473       else if (Text_interface::is_markup_list (scm_car (s)))
474         {
475           SCM texts = scm_call_3 (interpret_markup_list,
476                                   paper_->self_scm (),
477                                   page_properties,
478                                   scm_car (s));
479           for (SCM list = texts ; scm_is_pair (list) ; list = scm_cdr (list))
480             {
481               SCM t = scm_car (list);
482               // TODO: init props
483               Prob *ps = make_paper_system (SCM_EOL);
484               ps->set_property ("page-break-permission", ly_symbol2scm ("allow"));
485               ps->set_property ("page-turn-permission", ly_symbol2scm ("allow"));
486               
487               paper_system_set_stencil (ps, *unsmob_stencil (t));
488               ps->set_property ("is-title", SCM_BOOL_T); 
489               if (scm_is_pair (scm_cdr (list)))
490                 {
491                   /* If an other markup is following, set this markup 
492                    * next padding and next space to 0, so that baseline-skip 
493                    * only should be taken into account for lines vertical
494                    * spacing. */
495                   ps->set_property ("next-padding", scm_double2num (0.0));
496                   ps->set_property ("next-space", scm_double2num (0.0));
497                 }
498               system_specs = scm_cons (ps->self_scm (), system_specs);
499               ps->unprotect ();
500               
501               if (scm_is_pair (labels))
502                 {
503                   set_labels (scm_car (system_specs), labels);
504                   labels = SCM_EOL;
505                 }
506               // FIXME: figure out penalty.
507               //set_system_penalty (ps, scores_[i].header_);
508             }
509         }
510       else
511         assert (0);
512     }
513
514   system_specs = scm_reverse_x (system_specs, SCM_EOL);
515   return system_specs;
516 }
517
518 SCM
519 Paper_book::systems ()
520 {
521   if (systems_ != SCM_BOOL_F)
522     return systems_;
523
524   systems_ = SCM_EOL;
525   if (scm_is_pair (bookparts_))
526     {
527       for (SCM p = scm_reverse (bookparts_); scm_is_pair (p); p = scm_cdr (p))
528         if (Paper_book *pbookpart = unsmob_paper_book (scm_car (p)))
529           systems_ = scm_append_x (scm_list_2 (systems_, pbookpart->systems ()));
530     }
531   else
532     {
533       SCM specs = get_system_specs ();
534       for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
535         {
536           if (Paper_score *pscore = dynamic_cast<Paper_score*> (unsmob_music_output (scm_car (s))))
537             {
538               SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
539               system_list = scm_reverse (system_list);
540               systems_ = scm_append (scm_list_2 (system_list, systems_));
541             }
542           else
543             {
544               systems_ = scm_cons (scm_car (s), systems_);
545             }
546         }
547       systems_ = scm_reverse (systems_);
548
549       /* backwards compatibility for the old page breaker */
550       int i = 0;
551       Prob *last = 0;
552       for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
553         {
554           Prob *ps = unsmob_prob (scm_car (s));
555           ps->set_property ("number", scm_from_int (++i));
556           
557           if (last
558               && to_boolean (last->get_property ("is-title"))
559               && !scm_is_number (ps->get_property ("penalty")))
560             ps->set_property ("penalty", scm_from_int (10000));
561           last = ps;
562           
563           if (scm_is_pair (scm_cdr (s)))
564             {
565               SCM perm = ps->get_property ("page-break-permission");
566               Prob *next = unsmob_prob (scm_cadr (s));
567               if (perm == SCM_EOL)
568                 next->set_property ("penalty", scm_from_int (10001));
569               else if (perm == ly_symbol2scm ("force"))
570                 next->set_property ("penalty", scm_from_int (-10001));
571             }
572         }
573     }
574
575   return systems_;
576 }
577
578 SCM
579 Paper_book::pages ()
580 {
581   if (SCM_BOOL_F != pages_)
582     return pages_;
583
584   pages_ = SCM_EOL;
585   if (scm_is_pair (bookparts_))
586     {
587       for (SCM p = scm_reverse (bookparts_); scm_is_pair (p); p = scm_cdr (p))
588         if (Paper_book *pbookpart = unsmob_paper_book (scm_car (p)))
589           pages_ = scm_append_x (scm_list_2 (pages_, pbookpart->pages ()));
590     }
591   else
592     {
593       SCM proc = paper_->c_variable ("page-breaking-wrapper");
594       pages_ = scm_apply_0 (proc, scm_list_1 (self_scm ()));
595
596       /* set systems_ from the pages */
597       if (systems_ == SCM_BOOL_F)
598         {
599           systems_ = SCM_EOL;
600           for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
601             {
602               Prob *page = unsmob_prob (scm_car (p));
603               SCM systems = page->get_property ("lines");
604               systems_ = scm_append (scm_list_2 (systems_, systems));
605             }
606         }
607     }
608   return pages_;
609 }
610
611 SCM
612 Paper_book::performances () const
613 {
614   return scm_reverse (performances_);
615 }