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