]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* lily/paper-score.cc (process): Do not show progress newline.
[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::output (String outname)
118 {
119   if (!score_lines_.size ())
120     return;
121
122   /* Generate all stencils to trigger font loads.  */
123   pages ();
124   
125   SCM formats = ly_output_formats();
126   for (SCM s = formats; ly_c_pair_p (s); s = ly_cdr (s)) 
127     {
128       String format = ly_scm2string (ly_car (s));
129       Paper_outputter *out = get_paper_outputter (outname + "." + format,
130                                                   format);
131   
132       SCM scopes = SCM_EOL;
133       if (ly_c_module_p (header_))
134         scopes = scm_cons (header_, scopes);
135   
136       String func_nm = format;
137       func_nm = "output-framework-" + func_nm;
138         
139       SCM func = ly_scheme_function (func_nm.to_str0 ());
140       scm_apply_0 (func, scm_list_n (out->self_scm (),
141                                      self_scm (),
142                                      scopes,
143                                      dump_fields (),
144                                      scm_makfrom0str (outname.to_str0 ()),
145                                      SCM_UNDEFINED));
146
147       scm_gc_unprotect_object (out->self_scm ());
148       progress_indication ("\n");
149     }
150 }
151
152 void
153 Paper_book::classic_output (String outname)
154 {
155   /* Generate all stencils to trigger font loads.  */
156   lines ();
157
158   // ugh code dup
159   SCM scopes = SCM_EOL;
160   if (ly_c_module_p (header_))
161     scopes = scm_cons (header_, scopes);
162
163   if (ly_c_module_p (score_lines_[0].header_))
164     scopes = scm_cons (score_lines_[0].header_, scopes);
165   //end ugh
166
167   Array<String> output_formats = split_string (output_format_global, ',');
168
169   for (int i = 0; i < output_formats.size (); i++)
170     {
171       String format = output_formats[i];
172       String func_nm = format;
173       func_nm = "output-classic-framework-" + func_nm;
174       
175       SCM func = ly_scheme_function (func_nm.to_str0 ());
176       Paper_outputter *out = get_paper_outputter (outname + "." + format,
177                                                   format);
178
179       scm_apply_0 (func, scm_list_5 (out->self_scm (), self_scm (), scopes,
180                                      dump_fields (),
181                                      scm_makfrom0str (outname.to_str0 ())));
182
183       scm_gc_unprotect_object (out->self_scm ());
184       progress_indication ("\n");
185     }
186 }
187
188 LY_DEFINE (ly_paper_book_pages, "ly:paper-book-pages",
189           1, 0, 0, (SCM pb),
190           "Return pages in book PB.")
191 {
192   return unsmob_paper_book(pb)->pages ();
193 }
194
195 LY_DEFINE (ly_paper_book_scopes, "ly:paper-book-scopes",
196           1, 0, 0, (SCM book),
197           "Return pages in paper book @var{book}.")
198 {
199   Paper_book *pb = unsmob_paper_book(book);
200   SCM_ASSERT_TYPE(pb, book, SCM_ARG1, __FUNCTION__, "Paper_book");
201   
202   SCM scopes = SCM_EOL;
203   if (ly_c_module_p (pb->header_))
204     scopes = scm_cons (pb->header_, scopes);
205   
206   return scopes;
207 }
208
209 LY_DEFINE (ly_paper_book_lines, "ly:paper-book-lines",
210            1, 0, 0, (SCM pb),
211            "Return lines in book PB.")
212 {
213   return unsmob_paper_book (pb)->lines ();
214 }
215
216 LY_DEFINE (ly_paper_book_book_paper, "ly:paper-book-book-paper",
217           1, 0, 0, (SCM pb),
218           "Return pages in book PB.")
219 {
220   return unsmob_paper_book (pb)->bookpaper_->self_scm ();
221 }
222
223 /* TODO: resurrect more complex user-tweaks for titling?  */
224 Stencil
225 Paper_book::book_title ()
226 {
227   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
228   Stencil title;
229
230   SCM scopes = SCM_EOL;
231   if (ly_c_module_p (header_))
232     scopes = scm_cons (header_, scopes);
233
234  
235   SCM tit = SCM_EOL;
236   if (ly_c_procedure_p (title_func))
237     tit = scm_call_2 (title_func,
238                      bookpaper_->self_scm (),
239                      scopes);
240
241   if (unsmob_stencil (tit))
242     title = *unsmob_stencil (tit);
243
244   if (!title.is_empty ())
245     title.align_to (Y_AXIS, UP);
246   
247   return title;
248 }
249
250 Stencil
251 Paper_book::score_title (int i)
252 {
253   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
254
255   Stencil title;
256
257   // ugh code dup
258   SCM scopes = SCM_EOL;
259   if (ly_c_module_p (header_))
260     scopes = scm_cons (header_, scopes);
261
262   if (ly_c_module_p (score_lines_[i].header_))
263     scopes = scm_cons (score_lines_[i].header_, scopes);
264   //end ugh
265
266   SCM tit = SCM_EOL;
267   if (ly_c_procedure_p (title_func))
268     tit = scm_call_2 (title_func,
269                      bookpaper_->self_scm (),
270                      scopes);
271
272   if (unsmob_stencil (tit))
273     title = *unsmob_stencil (tit);
274
275   if (!title.is_empty ())
276     title.align_to (Y_AXIS, UP);
277   
278   return title;
279 }
280   
281 SCM
282 Paper_book::lines ()
283 {
284   if (lines_ != SCM_BOOL_F)
285     return lines_;
286
287   lines_ = SCM_EOL;
288   Stencil title = book_title ();
289
290   if (!title.is_empty ())
291     {
292       Paper_system *pl = new Paper_system (title, true);
293       lines_ = scm_cons (pl->self_scm (), lines_);
294       scm_gc_unprotect_object (pl->self_scm ());
295     }
296   
297   int score_count = score_lines_.size ();
298   for (int i = 0; i < score_count; i++)
299     {
300       Stencil title = score_title (i);      
301       if (!title.is_empty ())
302         {
303           Paper_system *pl = new Paper_system (title, true);
304           lines_ = scm_cons (pl->self_scm (), lines_);
305           scm_gc_unprotect_object (pl->self_scm ());
306         }
307       
308       if (scm_vector_p (score_lines_[i].lines_) == SCM_BOOL_T)
309         {
310           // guh.         
311           SCM line_list = scm_vector_to_list (score_lines_[i].lines_);
312
313           line_list = scm_reverse (line_list);
314           lines_ = scm_append (scm_list_2 (line_list, lines_));
315         }
316     }
317   
318   lines_ = scm_reverse (lines_);
319   
320   int i = 0;
321   Paper_system *last = 0;
322   for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
323     {
324       Paper_system * p = unsmob_paper_line (ly_car (s));
325       p->number_ = ++i;
326
327       if (last && last->is_title ())
328         // ugh, hardcoded.      
329         p->penalty_ = 10000;
330       last = p;
331     }
332   
333   return lines_;
334 }
335
336 SCM
337 Paper_book::pages ()
338 {
339   if (SCM_BOOL_F != pages_)
340     return pages_;
341
342   pages_ = SCM_EOL;
343   Output_def *paper = bookpaper_;
344   SCM proc = paper->c_variable ("page-breaking");
345   pages_ = scm_apply_0 (proc, scm_list_2 (lines (), self_scm ()));
346   return pages_;
347 }
348
349
350 /****************************************************************/
351
352 Score_lines::Score_lines ()
353 {
354   lines_ = SCM_EOL;
355   header_ = SCM_EOL;
356 }
357
358 void
359 Score_lines::gc_mark ()
360 {
361   scm_gc_mark (lines_);
362   scm_gc_mark (header_);
363 }
364