]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
*** empty log message ***
[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   lines_ = SCM_BOOL_F;
25   header_ = SCM_EOL;
26   
27   bookpaper_ = 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_lines_.size (); i++)
44     b->score_lines_[i].gc_mark ();
45
46   if (b->bookpaper_)
47     scm_gc_mark (b->bookpaper_->self_scm ());
48   scm_gc_mark (b->header_);
49   scm_gc_mark (b->pages_);
50   return b->lines_;
51 }
52
53 int
54 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
55 {
56   Paper_book *b = (Paper_book*) ly_cdr (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_lines_.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; ly_c_pair_p (s); s = ly_cdr (s)) 
159     {
160       String format = ly_scm2string (ly_car (s));
161       String file_name = outname + "." + format;
162       Paper_outputter *out = get_paper_outputter (file_name, format);
163   
164       SCM scopes = SCM_EOL;
165       if (ly_c_module_p (header_))
166         scopes = scm_cons (header_, scopes);
167   
168       String mod_nm = "scm framework-" + format;
169       
170       SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
171       SCM func = scm_c_module_lookup (mod, "output-framework");
172
173       func = scm_variable_ref (func);
174       scm_apply_0 (func, scm_list_n (out->self_scm (),
175                                      self_scm (),
176                                      scopes,
177                                      dump_fields (),
178                                      scm_makfrom0str (outname.to_str0 ()),
179                                      SCM_UNDEFINED));
180       out->close ();
181       scm_gc_unprotect_object (out->self_scm ());
182
183       post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
184       
185       if (make_preview)
186         {
187           String file_name = outname + ".preview." + format;
188           Paper_outputter *out = get_paper_outputter (file_name, format);
189   
190           SCM func = scm_c_module_lookup (mod, "output-preview-framework");
191           func = scm_variable_ref (func);
192           scm_apply_0 (func, scm_list_n (out->self_scm (),
193                                          self_scm (),
194                                          scopes,
195                                          dump_fields (),
196                                          scm_makfrom0str (outname.to_str0 ()),
197                                          SCM_UNDEFINED));
198
199           out->close ();
200           scm_gc_unprotect_object (out->self_scm ());
201
202           post_processing (mod, scm_makfrom0str (file_name.to_str0 ()));
203         }
204       
205       progress_indication ("\n");
206     }
207 }
208
209 void
210 Paper_book::classic_output (String outname)
211 {
212   /* Generate all stencils to trigger font loads.  */
213   lines ();
214
215   // ugh code dup
216   SCM scopes = SCM_EOL;
217   if (ly_c_module_p (header_))
218     scopes = scm_cons (header_, scopes);
219
220   if (ly_c_module_p (score_lines_[0].header_))
221     scopes = scm_cons (score_lines_[0].header_, scopes);
222   //end ugh
223
224   Array<String> output_formats = split_string (output_format_global, ',');
225
226   for (int i = 0; i < output_formats.size (); i++)
227     {
228       String format = output_formats[i];
229       String mod_nm = "scm framework-" + format;
230       
231       SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
232       SCM func = scm_c_module_lookup (mod, "output-classic-framework");
233
234       func = scm_variable_ref (func);
235       
236       Paper_outputter *out = get_paper_outputter (outname + "." + format,
237                                                   format);
238
239       scm_apply_0 (func, scm_list_n (out->self_scm (), self_scm (), scopes,
240                                      dump_fields (),
241                                      scm_makfrom0str (outname.to_str0 ()),
242                                      SCM_UNDEFINED));
243
244       scm_gc_unprotect_object (out->self_scm ());
245       progress_indication ("\n");
246     }
247 }
248
249 LY_DEFINE (ly_paper_book_pages, "ly:paper-book-pages",
250           1, 0, 0, (SCM pb),
251           "Return pages in book PB.")
252 {
253   return unsmob_paper_book(pb)->pages ();
254 }
255
256 LY_DEFINE (ly_paper_book_scopes, "ly:paper-book-scopes",
257           1, 0, 0, (SCM book),
258           "Return pages in paper book @var{book}.")
259 {
260   Paper_book *pb = unsmob_paper_book(book);
261   SCM_ASSERT_TYPE(pb, book, SCM_ARG1, __FUNCTION__, "Paper_book");
262   
263   SCM scopes = SCM_EOL;
264   if (ly_c_module_p (pb->header_))
265     scopes = scm_cons (pb->header_, scopes);
266   
267   return scopes;
268 }
269
270 LY_DEFINE (ly_paper_book_lines, "ly:paper-book-lines",
271            1, 0, 0, (SCM pb),
272            "Return lines in book PB.")
273 {
274   return unsmob_paper_book (pb)->lines ();
275 }
276
277 LY_DEFINE (ly_paper_book_book_paper, "ly:paper-book-book-paper",
278           1, 0, 0, (SCM pb),
279           "Return pages in book PB.")
280 {
281   return unsmob_paper_book (pb)->bookpaper_->self_scm ();
282 }
283
284 /* TODO: resurrect more complex user-tweaks for titling?  */
285 Stencil
286 Paper_book::book_title ()
287 {
288   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
289   Stencil title;
290
291   SCM scopes = SCM_EOL;
292   if (ly_c_module_p (header_))
293     scopes = scm_cons (header_, scopes);
294
295  
296   SCM tit = SCM_EOL;
297   if (ly_c_procedure_p (title_func))
298     tit = scm_call_2 (title_func,
299                      bookpaper_->self_scm (),
300                      scopes);
301
302   if (unsmob_stencil (tit))
303     title = *unsmob_stencil (tit);
304
305   if (!title.is_empty ())
306     title.align_to (Y_AXIS, UP);
307   
308   return title;
309 }
310
311 Stencil
312 Paper_book::score_title (int i)
313 {
314   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
315
316   Stencil title;
317
318   // ugh code dup
319   SCM scopes = SCM_EOL;
320   if (ly_c_module_p (header_))
321     scopes = scm_cons (header_, scopes);
322
323   if (ly_c_module_p (score_lines_[i].header_))
324     scopes = scm_cons (score_lines_[i].header_, scopes);
325   //end ugh
326
327   SCM tit = SCM_EOL;
328   if (ly_c_procedure_p (title_func))
329     tit = scm_call_2 (title_func,
330                      bookpaper_->self_scm (),
331                      scopes);
332
333   if (unsmob_stencil (tit))
334     title = *unsmob_stencil (tit);
335
336   if (!title.is_empty ())
337     title.align_to (Y_AXIS, UP);
338   
339   return title;
340 }
341   
342 SCM
343 Paper_book::lines ()
344 {
345   if (lines_ != SCM_BOOL_F)
346     return lines_;
347
348   lines_ = SCM_EOL;
349   Stencil title = book_title ();
350
351   if (!title.is_empty ())
352     {
353       Paper_system *pl = new Paper_system (title, true);
354       lines_ = scm_cons (pl->self_scm (), lines_);
355       scm_gc_unprotect_object (pl->self_scm ());
356     }
357   
358   int score_count = score_lines_.size ();
359   for (int i = 0; i < score_count; i++)
360     {
361       Stencil title = score_title (i);      
362       if (!title.is_empty ())
363         {
364           Paper_system *pl = new Paper_system (title, true);
365           lines_ = scm_cons (pl->self_scm (), lines_);
366           scm_gc_unprotect_object (pl->self_scm ());
367         }
368       
369       if (scm_vector_p (score_lines_[i].lines_) == SCM_BOOL_T)
370         {
371           // guh.         
372           SCM line_list = scm_vector_to_list (score_lines_[i].lines_);
373
374           line_list = scm_reverse (line_list);
375           lines_ = scm_append (scm_list_2 (line_list, lines_));
376         }
377     }
378   
379   lines_ = scm_reverse (lines_);
380   
381   int i = 0;
382   Paper_system *last = 0;
383   for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
384     {
385       Paper_system * p = unsmob_paper_line (ly_car (s));
386       p->number_ = ++i;
387
388       if (last && last->is_title ())
389         // ugh, hardcoded.      
390         p->penalty_ = 10000;
391       last = p;
392     }
393   
394   return lines_;
395 }
396
397 SCM
398 Paper_book::pages ()
399 {
400   if (SCM_BOOL_F != pages_)
401     return pages_;
402
403   pages_ = SCM_EOL;
404   SCM proc = bookpaper_->c_variable ("page-breaking");
405   pages_ = scm_apply_0 (proc, scm_list_2 (lines (), self_scm ()));
406   return pages_;
407 }
408
409
410 /****************************************************************/
411
412 Score_lines::Score_lines ()
413 {
414   lines_ = SCM_EOL;
415   header_ = SCM_EOL;
416 }
417
418 void
419 Score_lines::gc_mark ()
420 {
421   scm_gc_mark (lines_);
422   scm_gc_mark (header_);
423 }
424