]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* Documentation/user/changing-defaults.itely (Creating titles):
[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 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "paper-book.hh"
10
11 #include "ly-module.hh"
12 #include "main.hh"
13 #include "output-def.hh"
14 #include "paper-outputter.hh"
15 #include "paper-score.hh"
16 #include "paper-system.hh"
17 #include "warn.hh"
18
19 #include "ly-smobs.icc"
20
21 Paper_book::Paper_book ()
22 {
23   pages_ = SCM_BOOL_F;
24   systems_ = SCM_BOOL_F;
25   header_ = SCM_EOL;
26   
27   paper_ = 0;
28   smobify_self ();
29 }
30
31 Paper_book::~Paper_book ()
32 {
33 }
34
35 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
36 IMPLEMENT_SMOBS (Paper_book)
37 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
38
39 SCM
40 Paper_book::mark_smob (SCM smob)
41 {
42   Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
43   for (int i = 0; i < b->score_systems_.size (); i++)
44     b->score_systems_[i].gc_mark ();
45
46   if (b->paper_)
47     scm_gc_mark (b->paper_->self_scm ());
48   scm_gc_mark (b->header_);
49   scm_gc_mark (b->pages_);
50   return b->systems_;
51 }
52
53 int
54 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
55 {
56   Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
57      
58   scm_puts ("#<", port);
59   scm_puts (classname (b), port);
60   scm_puts (" ", port);
61   //scm_puts (b->, port);
62   scm_puts (">", port);
63   return 1;
64 }
65
66 Array<String>
67 split_string (String s, char c)
68 {
69   Array<String> rv; 
70   while (s.length ())
71     {
72       int i = s.index (c);
73       
74       if (i == 0)
75         {
76           s = s.nomid_string (0, 1);
77           continue;
78         }
79       
80       if (i < 0)
81         i = s.length () ;
82
83       rv.push (s.cut_string (0, i));
84       s = s.nomid_string (0, i);
85     }
86
87   return rv;
88 }
89
90 SCM
91 dump_fields ()
92 {
93   SCM fields = SCM_EOL;
94   for (int i = dump_header_fieldnames_global.size (); i--; )
95     fields
96       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
97                   fields);
98   return fields;
99 }
100
101 LY_DEFINE (ly_output_formats, "ly:output-formats",
102            0, 0, 0, (),
103            "Formats passed to --format as a list of strings, "
104            "used for the output.")
105 {
106   Array<String> output_formats = split_string (output_format_global, ',');
107
108   SCM lst = SCM_EOL;
109   int output_formats_count = output_formats.size ();
110   for (int i = 0; i < output_formats_count; i ++)
111     lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst);
112   
113   return lst; 
114 }
115
116 void
117 Paper_book::post_processing (SCM module,
118                              SCM file_name)
119 {
120   struct
121   {
122     bool do_it_;
123     char const *func_name_;
124   } settings[] = {
125     {make_tex, "convert-to-tex"},
126     {make_dvi, "convert-to-dvi"},
127     {make_ps, "convert-to-ps"},
128     {make_pdf, "convert-to-pdf"},
129     {make_png, "convert-to-png"},
130     {0, 0},
131   };
132
133   for (int i = 0; settings[i].func_name_; i++)
134     {
135       if (settings[i].do_it_)
136         {
137           SCM func = scm_c_module_lookup (module, settings[i].func_name_);
138           if (scm_variable_p (func) == SCM_BOOL_T)
139             {
140               func = scm_variable_ref (func);
141               if (ly_c_procedure_p (func))
142                 scm_call_2 (func, self_scm (), file_name);
143             }
144         }
145     }
146 }
147
148 void
149 Paper_book::output (String outname)
150 {
151   if (!score_systems_.size ())
152     return;
153
154   /* Generate all stencils to trigger font loads.  */
155   pages ();
156   
157   SCM formats = ly_output_formats ();
158   for (SCM s = formats; scm_is_pair (s); s = scm_cdr (s)) 
159     {
160       String format = ly_scm2string (scm_car (s));
161       String file_name = outname;
162       
163       if (file_name != "-")
164         file_name += "." + format;
165       
166       Paper_outputter *out = get_paper_outputter (file_name, format);
167   
168       SCM scopes = SCM_EOL;
169       if (ly_c_module_p (header_))
170         scopes = scm_cons (header_, scopes);
171   
172       String mod_nm = "scm framework-" + format;
173
174       SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
175       if (make_pages)
176         {
177           SCM func = scm_c_module_lookup (mod, "output-framework");
178
179           func = scm_variable_ref (func);
180           scm_apply_0 (func, scm_list_n (out->self_scm (),
181                                          self_scm (),
182                                          scopes,
183                                          dump_fields (),
184                                          scm_makfrom0str (outname.to_str0 ()),
185                                          SCM_UNDEFINED));
186           out->close ();
187           scm_gc_unprotect_object (out->self_scm ());
188           post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
189         }
190       
191       if (make_preview)
192         {
193           String file_name = outname + ".preview." + format;
194           Paper_outputter *out = get_paper_outputter (file_name, format);
195   
196           SCM func = scm_c_module_lookup (mod, "output-preview-framework");
197           func = scm_variable_ref (func);
198           scm_apply_0 (func, scm_list_n (out->self_scm (),
199                                          self_scm (),
200                                          scopes,
201                                          dump_fields (),
202                                          scm_makfrom0str (outname.to_str0 ()),
203                                          SCM_UNDEFINED));
204
205           out->close ();
206           scm_gc_unprotect_object (out->self_scm ());
207
208           post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
209         }
210     }
211   progress_indication ("\n");
212 }
213
214 void
215 Paper_book::classic_output (String outname)
216 {
217   /* Generate all stencils to trigger font loads.  */
218   systems ();
219
220   // ugh code dup
221   SCM scopes = SCM_EOL;
222   if (ly_c_module_p (header_))
223     scopes = scm_cons (header_, scopes);
224
225   if (ly_c_module_p (score_systems_[0].header_))
226     scopes = scm_cons (score_systems_[0].header_, scopes);
227   //end ugh
228
229   Array<String> output_formats = split_string (output_format_global, ',');
230
231   for (int i = 0; i < output_formats.size (); i++)
232     {
233       String format = output_formats[i];
234       String mod_nm = "scm framework-" + format;
235       
236       SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
237       SCM func = scm_c_module_lookup (mod, "output-classic-framework");
238
239       func = scm_variable_ref (func);
240       
241       Paper_outputter *out = get_paper_outputter (outname + "." + format,
242                                                   format);
243
244       scm_apply_0 (func, scm_list_n (out->self_scm (), self_scm (), scopes,
245                                      dump_fields (),
246                                      scm_makfrom0str (outname.to_str0 ()),
247                                      SCM_UNDEFINED));
248
249       scm_gc_unprotect_object (out->self_scm ());
250       progress_indication ("\n");
251     }
252 }
253
254 LY_DEFINE (ly_paper_book_pages, "ly:paper-book-pages",
255           1, 0, 0, (SCM pb),
256           "Return pages in book PB.")
257 {
258   return unsmob_paper_book(pb)->pages ();
259 }
260
261 LY_DEFINE (ly_paper_book_scopes, "ly:paper-book-scopes",
262           1, 0, 0, (SCM book),
263           "Return pages in layout book @var{book}.")
264 {
265   Paper_book *pb = unsmob_paper_book(book);
266   SCM_ASSERT_TYPE(pb, book, SCM_ARG1, __FUNCTION__, "Paper_book");
267   
268   SCM scopes = SCM_EOL;
269   if (ly_c_module_p (pb->header_))
270     scopes = scm_cons (pb->header_, scopes);
271   
272   return scopes;
273 }
274
275 LY_DEFINE (ly_paper_book_systems, "ly:paper-book-systems",
276            1, 0, 0, (SCM pb),
277            "Return systems in book PB.")
278 {
279   return unsmob_paper_book (pb)->systems ();
280 }
281
282 LY_DEFINE (ly_paper_book_paper, "ly:paper-book-paper",
283           1, 0, 0, (SCM pb),
284           "Return pages in book PB.")
285 {
286   return unsmob_paper_book (pb)->paper_->self_scm ();
287 }
288
289 /* TODO: resurrect more complex user-tweaks for titling?  */
290 Stencil
291 Paper_book::book_title ()
292 {
293   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
294   Stencil title;
295
296   SCM scopes = SCM_EOL;
297   if (ly_c_module_p (header_))
298     scopes = scm_cons (header_, scopes);
299
300  
301   SCM tit = SCM_EOL;
302   if (ly_c_procedure_p (title_func))
303     tit = scm_call_2 (title_func,
304                      paper_->self_scm (),
305                      scopes);
306
307   if (unsmob_stencil (tit))
308     title = *unsmob_stencil (tit);
309
310   if (!title.is_empty ())
311     title.align_to (Y_AXIS, UP);
312   
313   return title;
314 }
315
316 Stencil
317 Paper_book::score_title (int i)
318 {
319   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
320
321   Stencil title;
322
323   // ugh code dup
324   SCM scopes = SCM_EOL;
325   if (ly_c_module_p (header_))
326     scopes = scm_cons (header_, scopes);
327
328   if (ly_c_module_p (score_systems_[i].header_))
329     scopes = scm_cons (score_systems_[i].header_, scopes);
330   //end ugh
331
332   SCM tit = SCM_EOL;
333   if (ly_c_procedure_p (title_func))
334     tit = scm_call_2 (title_func,
335                      paper_->self_scm (),
336                      scopes);
337
338   if (unsmob_stencil (tit))
339     title = *unsmob_stencil (tit);
340
341   if (!title.is_empty ())
342     title.align_to (Y_AXIS, UP);
343   
344   return title;
345 }
346
347 void
348 set_system_penalty (Paper_system * ps, SCM header)
349 {
350   if (ly_c_module_p (header))
351     {
352       SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
353       if (SCM_VARIABLEP(force)
354           && scm_is_bool (SCM_VARIABLE_REF(force)))
355         {
356           ps->break_before_penalty_ = to_boolean (SCM_VARIABLE_REF(force))
357             ? -10000
358             : 10000;
359         }
360     }
361 }
362             
363 SCM
364 Paper_book::systems ()
365 {
366   if (systems_ != SCM_BOOL_F)
367     return systems_;
368
369   systems_ = SCM_EOL;
370   Stencil title = book_title ();
371
372   if (!title.is_empty ())
373     {
374       Paper_system *ps = new Paper_system (title, true);
375       set_system_penalty (ps, header_);
376       
377       systems_ = scm_cons (ps->self_scm (), systems_);
378       scm_gc_unprotect_object (ps->self_scm ());
379     }
380   
381   int score_count = score_systems_.size ();
382   for (int i = 0; i < score_count; i++)
383     {
384       Stencil title = score_title (i);      
385       if (!title.is_empty ())
386         {
387           Paper_system *ps = new Paper_system (title, true);
388           systems_ = scm_cons (ps->self_scm (), systems_);
389           scm_gc_unprotect_object (ps->self_scm ());
390
391           set_system_penalty (ps, score_systems_[i].header_);
392           
393         }
394       
395       if (scm_vector_p (score_systems_[i].systems_) == SCM_BOOL_T)
396         {
397           // guh.         
398           SCM system_list = scm_vector_to_list (score_systems_[i].systems_);
399
400           system_list = scm_reverse (system_list);
401           systems_ = scm_append (scm_list_2 (system_list, systems_));
402         }
403     }
404   
405   systems_ = scm_reverse (systems_);
406   
407   int i = 0;
408   Paper_system *last = 0;
409   for (SCM s = systems_; s != SCM_EOL; s = scm_cdr (s))
410     {
411       Paper_system *ps = unsmob_paper_system (scm_car (s));
412       ps->number_ = ++i;
413
414       if (last
415           && last->is_title ()
416           && !ps->break_before_penalty_)
417         ps->break_before_penalty_ = 10000;
418       last = ps;
419     }
420   
421   return systems_;
422 }
423
424 SCM
425 Paper_book::pages ()
426 {
427   if (SCM_BOOL_F != pages_)
428     return pages_;
429
430   pages_ = SCM_EOL;
431   SCM proc = paper_->c_variable ("page-breaking");
432   pages_ = scm_apply_0 (proc, scm_list_2 (systems (), self_scm ()));
433   return pages_;
434 }
435
436
437 /****************************************************************/
438
439 Score_systems::Score_systems ()
440 {
441   systems_ = SCM_EOL;
442   header_ = SCM_EOL;
443 }
444
445 void
446 Score_systems::gc_mark ()
447 {
448   scm_gc_mark (systems_);
449   scm_gc_mark (header_);
450 }
451