]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* lily/ledger-line-spanner.cc (print): swap linear_combination
[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 "ly-module.hh"
10 #include "main.hh"
11 #include "output-def.hh"
12 #include "paper-book.hh"
13 #include "paper-outputter.hh"
14 #include "paper-score.hh"
15 #include "paper-system.hh"
16 #include "stencil.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
215 void
216 Paper_book::classic_output (String outname)
217 {
218   /* Generate all stencils to trigger font loads.  */
219   systems ();
220
221   // ugh code dup
222   SCM scopes = SCM_EOL;
223   if (ly_c_module_p (header_))
224     scopes = scm_cons (header_, scopes);
225
226   if (ly_c_module_p (score_systems_[0].header_))
227     scopes = scm_cons (score_systems_[0].header_, scopes);
228   //end ugh
229
230   Array<String> output_formats = split_string (output_format_global, ',');
231
232   for (int i = 0; i < output_formats.size (); i++)
233     {
234       String format = output_formats[i];
235       String mod_nm = "scm framework-" + format;
236       
237       SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
238       SCM func = scm_c_module_lookup (mod, "output-classic-framework");
239
240       func = scm_variable_ref (func);
241       
242       Paper_outputter *out = get_paper_outputter (outname + "." + format,
243                                                   format);
244
245       scm_apply_0 (func, scm_list_n (out->self_scm (), self_scm (), scopes,
246                                      dump_fields (),
247                                      scm_makfrom0str (outname.to_str0 ()),
248                                      SCM_UNDEFINED));
249
250       scm_gc_unprotect_object (out->self_scm ());
251       progress_indication ("\n");
252     }
253 }
254
255 LY_DEFINE (ly_paper_book_pages, "ly:paper-book-pages",
256           1, 0, 0, (SCM pb),
257           "Return pages in book PB.")
258 {
259   return unsmob_paper_book(pb)->pages ();
260 }
261
262 LY_DEFINE (ly_paper_book_scopes, "ly:paper-book-scopes",
263           1, 0, 0, (SCM book),
264           "Return pages in layout book @var{book}.")
265 {
266   Paper_book *pb = unsmob_paper_book(book);
267   SCM_ASSERT_TYPE(pb, book, SCM_ARG1, __FUNCTION__, "Paper_book");
268   
269   SCM scopes = SCM_EOL;
270   if (ly_c_module_p (pb->header_))
271     scopes = scm_cons (pb->header_, scopes);
272   
273   return scopes;
274 }
275
276 LY_DEFINE (ly_paper_book_systems, "ly:paper-book-systems",
277            1, 0, 0, (SCM pb),
278            "Return systems in book PB.")
279 {
280   return unsmob_paper_book (pb)->systems ();
281 }
282
283 LY_DEFINE (ly_paper_book_paper, "ly:paper-book-paper",
284           1, 0, 0, (SCM pb),
285           "Return pages in book PB.")
286 {
287   return unsmob_paper_book (pb)->paper_->self_scm ();
288 }
289
290 /* TODO: resurrect more complex user-tweaks for titling?  */
291 Stencil
292 Paper_book::book_title ()
293 {
294   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
295   Stencil title;
296
297   SCM scopes = SCM_EOL;
298   if (ly_c_module_p (header_))
299     scopes = scm_cons (header_, scopes);
300
301  
302   SCM tit = SCM_EOL;
303   if (ly_c_procedure_p (title_func))
304     tit = scm_call_2 (title_func,
305                      paper_->self_scm (),
306                      scopes);
307
308   if (unsmob_stencil (tit))
309     title = *unsmob_stencil (tit);
310
311   if (!title.is_empty ())
312     title.align_to (Y_AXIS, UP);
313   
314   return title;
315 }
316
317 Stencil
318 Paper_book::score_title (int i)
319 {
320   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
321
322   Stencil title;
323
324   // ugh code dup
325   SCM scopes = SCM_EOL;
326   if (ly_c_module_p (header_))
327     scopes = scm_cons (header_, scopes);
328
329   if (ly_c_module_p (score_systems_[i].header_))
330     scopes = scm_cons (score_systems_[i].header_, scopes);
331   //end ugh
332
333   SCM tit = SCM_EOL;
334   if (ly_c_procedure_p (title_func))
335     tit = scm_call_2 (title_func,
336                      paper_->self_scm (),
337                      scopes);
338
339   if (unsmob_stencil (tit))
340     title = *unsmob_stencil (tit);
341
342   if (!title.is_empty ())
343     title.align_to (Y_AXIS, UP);
344   
345   return title;
346 }
347   
348 SCM
349 Paper_book::systems ()
350 {
351   if (systems_ != SCM_BOOL_F)
352     return systems_;
353
354   systems_ = SCM_EOL;
355   Stencil title = book_title ();
356
357   if (!title.is_empty ())
358     {
359       Paper_system *ps = new Paper_system (title, true);
360       systems_ = scm_cons (ps->self_scm (), systems_);
361       scm_gc_unprotect_object (ps->self_scm ());
362     }
363   
364   int score_count = score_systems_.size ();
365   for (int i = 0; i < score_count; i++)
366     {
367       Stencil title = score_title (i);      
368       if (!title.is_empty ())
369         {
370           Paper_system *ps = new Paper_system (title, true);
371           systems_ = scm_cons (ps->self_scm (), systems_);
372           scm_gc_unprotect_object (ps->self_scm ());
373         }
374       
375       if (scm_vector_p (score_systems_[i].systems_) == SCM_BOOL_T)
376         {
377           // guh.         
378           SCM system_list = scm_vector_to_list (score_systems_[i].systems_);
379
380           system_list = scm_reverse (system_list);
381           systems_ = scm_append (scm_list_2 (system_list, systems_));
382         }
383     }
384   
385   systems_ = scm_reverse (systems_);
386   
387   int i = 0;
388   Paper_system *last = 0;
389   for (SCM s = systems_; s != SCM_EOL; s = scm_cdr (s))
390     {
391       Paper_system *ps = unsmob_paper_system (scm_car (s));
392       ps->number_ = ++i;
393
394       if (last && last->is_title ())
395         // ugh, hardcoded.      
396         ps->penalty_ = 10000;
397       last = ps;
398     }
399   
400   return systems_;
401 }
402
403 SCM
404 Paper_book::pages ()
405 {
406   if (SCM_BOOL_F != pages_)
407     return pages_;
408
409   pages_ = SCM_EOL;
410   SCM proc = paper_->c_variable ("page-breaking");
411   pages_ = scm_apply_0 (proc, scm_list_2 (systems (), self_scm ()));
412   return pages_;
413 }
414
415
416 /****************************************************************/
417
418 Score_systems::Score_systems ()
419 {
420   systems_ = SCM_EOL;
421   header_ = SCM_EOL;
422 }
423
424 void
425 Score_systems::gc_mark ()
426 {
427   scm_gc_mark (systems_);
428   scm_gc_mark (header_);
429 }
430