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