]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
Web-ja: update introduction
[lilypond.git] / lily / paper-book.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2015 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 #include "ly-module.hh"
34 #include "lily-imports.hh"
35
36
37 Paper_book::Paper_book ()
38 {
39   header_ = SCM_EOL;
40   header_0_ = SCM_EOL;
41   pages_ = SCM_BOOL_F;
42   scores_ = SCM_EOL;
43   bookparts_ = SCM_EOL;
44   performances_ = SCM_EOL;
45   systems_ = SCM_BOOL_F;
46
47   paper_ = 0;
48   parent_ = 0;
49   smobify_self ();
50 }
51
52 Paper_book::~Paper_book ()
53 {
54 }
55
56 const char * const Paper_book::type_p_name_ = "ly:paper-book?";
57
58 SCM
59 Paper_book::mark_smob () const
60 {
61   if (paper_)
62     scm_gc_mark (paper_->self_scm ());
63   if (parent_)
64     scm_gc_mark (parent_->self_scm ());
65   scm_gc_mark (header_);
66   scm_gc_mark (header_0_);
67   scm_gc_mark (pages_);
68   scm_gc_mark (performances_);
69   scm_gc_mark (scores_);
70   scm_gc_mark (bookparts_);
71   return systems_;
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 long
113 Paper_book::output_aux (SCM output_channel,
114                         bool is_last,
115                         long *first_page_number,
116                         long *first_performance_number)
117 {
118   long page_nb = 0;
119   if (scm_is_pair (performances_))
120     {
121       Lily::write_performances_midis (performances (),
122                                       output_channel,
123                                       scm_from_long (*first_performance_number));
124       *first_performance_number += scm_ilength (performances_);
125     }
126
127   if (scm_is_pair (bookparts_))
128     {
129       for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
130         if (Paper_book *pbookpart = unsmob<Paper_book> (scm_car (p)))
131           {
132             bool is_last_part = (is_last && !scm_is_pair (scm_cdr (p)));
133             page_nb += pbookpart->output_aux (output_channel,
134                                               is_last_part,
135                                               first_page_number,
136                                               first_performance_number);
137           }
138     }
139   else
140     {
141       if (scm_is_null (scores_))
142         return 0;
143       paper_->set_variable (ly_symbol2scm ("first-page-number"),
144                             scm_from_long (*first_page_number));
145       paper_->set_variable (ly_symbol2scm ("is-last-bookpart"),
146                             ly_bool2scm (is_last));
147       /* Generate all stencils to trigger font loads.  */
148       page_nb = scm_ilength (pages ());
149       *first_page_number += page_nb;
150     }
151   return page_nb;
152 }
153
154 void
155 Paper_book::output (SCM output_channel)
156 {
157   long first_page_number
158     = robust_scm2int (paper_->c_variable ("first-page-number"), 1);
159   long first_performance_number = 0;
160
161   /* FIXME: We need a line-width for ps output (framework-ps.scm:92).
162      If we don't have any, we take the paper-width unless we know
163      better which line-width to choose (e.g. if there are \bookparts
164      with different line-widths) and why we need it at all.
165   */
166
167   if (SCM_UNBNDP (paper_->c_variable ("line-width")))
168     paper_->set_variable (ly_symbol2scm ("line-width"),
169                           paper_->c_variable ("paper-width"));
170
171   if (!output_aux (output_channel,
172                    true,
173                    &first_page_number,
174                    &first_performance_number))
175     return;
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,
188                                         ly_symbol2scm ("output-framework"));
189
190       if (scm_is_true (framework))
191         {
192           SCM func = scm_variable_ref (framework);
193           scm_call_4 (func,
194                       output_channel,
195                       self_scm (),
196                       scopes,
197                       dump_fields ());
198         }
199       else
200         warning (_f ("program option -dprint-pages not supported by backend `%s'",
201                      get_output_backend_name ()));
202     }
203
204   if (get_program_option ("preview"))
205     {
206       SCM framework
207         = ly_module_lookup (mod, ly_symbol2scm ("output-preview-framework"));
208
209       if (scm_is_true (framework))
210         {
211           SCM func = scm_variable_ref (framework);
212           scm_call_4 (func,
213                       output_channel,
214                       self_scm (),
215                       scopes,
216                       dump_fields ());
217         }
218       else
219         warning (_f ("program option -dpreview not supported by backend `%s'",
220                      get_output_backend_name ()));
221     }
222 }
223
224 void
225 Paper_book::classic_output_aux (SCM output,
226                                 long *first_performance_number)
227 {
228   if (scm_is_pair (performances_))
229     {
230       Lily::write_performances_midis (performances (),
231                                       output,
232                                       scm_from_long (*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   long 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_call_4 (func,
261               output,
262               self_scm (),
263               scopes,
264               dump_fields ());
265   progress_indication ("\n");
266 }
267
268 /* TODO: resurrect more complex user-tweaks for titling?  */
269 Stencil
270 Paper_book::book_title ()
271 {
272   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
273   Stencil title;
274
275   SCM scopes = SCM_EOL;
276   if (ly_is_module (header_))
277     scopes = scm_cons (header_, scopes);
278
279   SCM tit = SCM_EOL;
280   if (ly_is_procedure (title_func))
281     tit = scm_call_2 (title_func,
282                       paper_->self_scm (),
283                       scopes);
284
285   if (unsmob<Stencil> (tit))
286     title = *unsmob<Stencil> (tit);
287
288   if (!title.is_empty ())
289     title.align_to (Y_AXIS, UP);
290
291   return title;
292 }
293
294 Stencil
295 Paper_book::score_title (SCM header)
296 {
297   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
298
299   Stencil title;
300
301   SCM scopes = SCM_EOL;
302   if (ly_is_module (header_))
303     scopes = scm_cons (header_, scopes);
304
305   if (ly_is_module (header))
306     scopes = scm_cons (header, scopes);
307
308   SCM tit = SCM_EOL;
309   if (ly_is_procedure (title_func))
310     tit = scm_call_2 (title_func,
311                       paper_->self_scm (),
312                       scopes);
313
314   if (unsmob<Stencil> (tit))
315     title = *unsmob<Stencil> (tit);
316
317   if (!title.is_empty ())
318     title.align_to (Y_AXIS, UP);
319
320   return title;
321 }
322
323 void
324 set_page_permission (SCM sys, SCM symbol, SCM permission)
325 {
326   if (Paper_score *ps = unsmob<Paper_score> (sys))
327     {
328       vector<Grob *> cols = ps->get_columns ();
329       if (cols.size ())
330         {
331           Paper_column *col = dynamic_cast<Paper_column *> (cols.back ());
332           col->set_property (symbol, permission);
333           col->find_prebroken_piece (LEFT)->set_property (symbol, permission);
334         }
335     }
336   else if (Prob *pb = unsmob<Prob> (sys))
337     pb->set_property (symbol, permission);
338 }
339
340 /* read the breakbefore property of a score block and set up the preceding
341    system-spec to honour it. That is, SYS should be the system spec that
342    immediately precedes the score (from which HEADER is taken)
343    in the get_system_specs () list */
344 void
345 set_system_penalty (SCM sys, SCM header)
346 {
347   if (ly_is_module (header))
348     {
349       SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
350       if (SCM_VARIABLEP (force)
351           && scm_is_bool (SCM_VARIABLE_REF (force)))
352         {
353           if (to_boolean (SCM_VARIABLE_REF (force)))
354             {
355               set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
356                                    ly_symbol2scm ("force"));
357               set_page_permission (sys, ly_symbol2scm ("line-break-permission"),
358                                    ly_symbol2scm ("force"));
359             }
360           else
361             set_page_permission (sys, ly_symbol2scm ("page-break-permission"),
362                                  SCM_EOL);
363         }
364     }
365 }
366
367 void
368 set_labels (SCM sys, SCM labels)
369 {
370   if (Paper_score *ps = unsmob<Paper_score> (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
380             = dynamic_cast<Paper_column *> (col->find_prebroken_piece (RIGHT));
381           col_right->set_property ("labels",
382                                    scm_append_x (scm_list_2 (col_right->get_property ("labels"),
383                                                              labels)));
384         }
385     }
386   else if (Prob *pb = unsmob<Prob> (sys))
387     pb->set_property ("labels",
388                       scm_append_x (scm_list_2 (pb->get_property ("labels"),
389                                                 labels)));
390 }
391
392 SCM
393 Paper_book::get_score_title (SCM header)
394 {
395   Stencil title = score_title (header);
396   if (title.is_empty ())
397     title = score_title (header_);
398   if (!title.is_empty ())
399     {
400       /*
401         TODO: this should come from the \layout {} block, which should
402         override settings from \paper {}
403       */
404       SCM props
405         = paper_->lookup_variable (ly_symbol2scm ("score-title-properties"));
406       Prob *ps = make_paper_system (props);
407       paper_system_set_stencil (ps, title);
408
409       return ps->self_scm ();
410     }
411
412   return SCM_BOOL_F;
413 }
414
415 SCM
416 Paper_book::get_system_specs ()
417 {
418   SCM system_specs = SCM_EOL;
419
420   Stencil title = book_title ();
421   if (!title.is_empty ())
422     {
423       SCM props
424         = paper_->lookup_variable (ly_symbol2scm ("book-title-properties"));
425       Prob *ps = make_paper_system (props);
426       paper_system_set_stencil (ps, title);
427
428       system_specs = scm_cons (ps->self_scm (), system_specs);
429       ps->unprotect ();
430     }
431
432   SCM page_properties
433     = Lily::layout_extract_page_properties (paper_->self_scm ());
434
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 (scm_is_null (header_0_))
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 = Lily::interpret_markup_list (paper_->self_scm (),
496                                                    page_properties,
497                                                    scm_car (s));
498           Prob *first = 0;
499           Prob *last = 0;
500           for (SCM list = texts; scm_is_pair (list); list = scm_cdr (list))
501             {
502               SCM t = scm_car (list);
503               // TODO: init props
504               Prob *ps = make_paper_system (SCM_EOL);
505               ps->set_property ("page-break-permission",
506                                 ly_symbol2scm ("allow"));
507               ps->set_property ("page-turn-permission",
508                                 ly_symbol2scm ("allow"));
509               ps->set_property ("last-markup-line", SCM_BOOL_F);
510               ps->set_property ("first-markup-line", SCM_BOOL_F);
511
512               paper_system_set_stencil (ps, *unsmob<Stencil> (t));
513
514               SCM footnotes = get_footnotes (unsmob<Stencil> (t)->expr ());
515               ps->set_property ("footnotes", footnotes);
516               ps->set_property ("is-title", SCM_BOOL_T);
517               if (scm_is_eq (list, texts))
518                 first = ps;
519               else
520                 {
521                   // last line so far, in a multi-line paragraph
522                   last = ps;
523                   //Place closely to previous line, no stretching.
524                   ps->set_property ("tight-spacing", SCM_BOOL_T);
525                 }
526               system_specs = scm_cons (ps->self_scm (), system_specs);
527               ps->unprotect ();
528
529               if (scm_is_pair (labels))
530                 {
531                   set_labels (scm_car (system_specs), labels);
532                   labels = SCM_EOL;
533                 }
534               // FIXME: figure out penalty.
535               //set_system_penalty (ps, scores_[i].header_);
536             }
537           /* Set properties to avoid widowed/orphaned lines.
538              Single-line markup_lists are excluded, but in future
539              we may want to add the case of a very short, single line. */
540           if (first && last)
541             {
542               last->set_property ("last-markup-line", SCM_BOOL_T);
543               first->set_property ("first-markup-line", SCM_BOOL_T);
544             }
545         }
546       else
547         assert (0);
548     }
549
550   system_specs = scm_reverse_x (system_specs, SCM_EOL);
551   return system_specs;
552 }
553
554 SCM
555 Paper_book::systems ()
556 {
557   if (scm_is_true (systems_))
558     return systems_;
559
560   systems_ = SCM_EOL;
561   if (scm_is_pair (bookparts_))
562     {
563       SCM system_list = SCM_EOL;
564       for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
565         if (Paper_book *pbookpart = unsmob<Paper_book> (scm_car (p)))
566           system_list = scm_cons (pbookpart->systems (), system_list);
567       systems_ = scm_append (scm_reverse_x (system_list, SCM_EOL));
568     }
569   else
570     {
571       SCM specs = get_system_specs ();
572       for (SCM s = specs; scm_is_pair (s); s = scm_cdr (s))
573         {
574           if (Paper_score * pscore
575               = unsmob<Paper_score> (scm_car (s)))
576             {
577               SCM system_list
578                 = scm_vector_to_list (pscore->get_paper_systems ());
579
580               systems_ = scm_reverse_x (system_list, systems_);
581             }
582           else
583             {
584               systems_ = scm_cons (scm_car (s), systems_);
585             }
586         }
587       systems_ = scm_reverse_x (systems_, SCM_EOL);
588
589       /* backwards compatibility for the old page breaker */
590       int i = 0;
591       Prob *last = 0;
592       for (SCM s = systems_; scm_is_pair (s); s = scm_cdr (s))
593         {
594           Prob *ps = unsmob<Prob> (scm_car (s));
595           ps->set_property ("number", scm_from_int (++i));
596
597           if (last
598               && to_boolean (last->get_property ("is-title"))
599               && !scm_is_number (ps->get_property ("penalty")))
600             ps->set_property ("penalty", scm_from_int (10000));
601           last = ps;
602
603           if (scm_is_pair (scm_cdr (s)))
604             {
605               SCM perm = ps->get_property ("page-break-permission");
606               Prob *next = unsmob<Prob> (scm_cadr (s));
607               if (scm_is_null (perm))
608                 next->set_property ("penalty", scm_from_int (10001));
609               else if (scm_is_eq (perm, ly_symbol2scm ("force")))
610                 next->set_property ("penalty", scm_from_int (-10001));
611             }
612         }
613     }
614
615   return systems_;
616 }
617
618 SCM
619 Paper_book::pages ()
620 {
621   if (scm_is_true (pages_))
622     return pages_;
623
624   pages_ = SCM_EOL;
625   if (scm_is_pair (bookparts_))
626     {
627       for (SCM p = bookparts_; scm_is_pair (p); p = scm_cdr (p))
628         if (Paper_book *pbookpart = unsmob<Paper_book> (scm_car (p)))
629           pages_ = scm_cons (pbookpart->pages (), pages_);
630       pages_ = scm_append (scm_reverse_x (pages_, SCM_EOL));
631     }
632   else if (scm_is_pair (scores_))
633     {
634       SCM page_breaking = paper_->c_variable ("page-breaking");
635       pages_ = scm_call_1 (page_breaking, self_scm ());
636
637       // Create all the page stencils.
638       SCM page_module = scm_c_resolve_module ("scm page");
639       SCM page_stencil = scm_c_module_lookup (page_module, "page-stencil");
640       page_stencil = scm_variable_ref (page_stencil);
641       for (SCM pages = pages_; scm_is_pair (pages); pages = scm_cdr (pages))
642         scm_call_1 (page_stencil, scm_car (pages));
643
644       // Perform any user-supplied post-processing.
645       SCM post_process = paper_->c_variable ("page-post-process");
646       if (ly_is_procedure (post_process))
647         scm_call_2 (post_process, paper_->self_scm (), pages_);
648
649       /* set systems_ from the pages */
650       if (scm_is_false (systems_))
651         {
652           systems_ = SCM_EOL;
653           for (SCM p = pages_; scm_is_pair (p); p = scm_cdr (p))
654             {
655               Prob *page = unsmob<Prob> (scm_car (p));
656               SCM systems = page->get_property ("lines");
657               systems_ = scm_cons (systems, systems_);
658             }
659           systems_ = scm_append (scm_reverse_x (systems_, SCM_EOL));
660         }
661     }
662   return pages_;
663 }
664
665 SCM
666 Paper_book::performances () const
667 {
668   return scm_reverse (performances_);
669 }