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