]> 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--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "paper-book.hh"
10
11 #include "main.hh"
12 #include "output-def.hh"
13 #include "paper-score.hh"
14 #include "paper-system.hh"
15 #include "warn.hh"
16
17 #include "ly-smobs.icc"
18
19 Paper_book::Paper_book ()
20 {
21   pages_ = SCM_BOOL_F;
22   systems_ = SCM_BOOL_F;
23   header_ = SCM_EOL;
24   
25   paper_ = 0;
26   smobify_self ();
27 }
28
29 Paper_book::~Paper_book ()
30 {
31 }
32
33 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
34 IMPLEMENT_SMOBS (Paper_book)
35 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
36
37 SCM
38 Paper_book::mark_smob (SCM smob)
39 {
40   Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
41   for (int i = 0; i < b->score_systems_.size (); i++)
42     b->score_systems_[i].gc_mark ();
43
44   if (b->paper_)
45     scm_gc_mark (b->paper_->self_scm ());
46   scm_gc_mark (b->header_);
47   scm_gc_mark (b->pages_);
48   return b->systems_;
49 }
50
51 int
52 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
53 {
54   Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
55      
56   scm_puts ("#<", port);
57   scm_puts (classname (b), port);
58   scm_puts (" ", port);
59   scm_puts (">", port);
60   return 1;
61 }
62
63 SCM
64 dump_fields ()
65 {
66   SCM fields = SCM_EOL;
67   for (int i = dump_header_fieldnames_global.size (); i--; )
68     fields
69       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
70                   fields);
71   return fields;
72 }
73
74 void
75 Paper_book::output (String outname)
76 {
77   if (!score_systems_.size ())
78     return;
79
80   /* Generate all stencils to trigger font loads.  */
81   pages ();
82   
83   SCM scopes = SCM_EOL;
84   if (ly_c_module_p (header_))
85     scopes = scm_cons (header_, scopes);
86   
87   String mod_nm = "scm framework-" + output_backend_global;
88
89   SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
90   if (make_pages)
91     {
92       SCM func = scm_c_module_lookup (mod, "output-framework");
93
94       func = scm_variable_ref (func);
95       scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()),
96                                      self_scm (),
97                                      scopes,
98                                      dump_fields (),
99                                      SCM_UNDEFINED));
100     }
101       
102   if (make_preview)
103     {
104       SCM func = scm_c_module_lookup (mod, "output-preview-framework");
105       func = scm_variable_ref (func);
106       scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()),
107                                      self_scm (),
108                                      scopes,
109                                      dump_fields (),
110                                      SCM_UNDEFINED));
111     }
112   progress_indication ("\n");
113 }
114
115 void
116 Paper_book::classic_output (String outname)
117 {
118   /* Generate all stencils to trigger font loads.  */
119   systems ();
120
121   // ugh code dup
122   SCM scopes = SCM_EOL;
123   if (ly_c_module_p (header_))
124     scopes = scm_cons (header_, scopes);
125
126   if (ly_c_module_p (score_systems_[0].header_))
127     scopes = scm_cons (score_systems_[0].header_, scopes);
128   //end ugh
129
130   String format = output_backend_global;
131   String mod_nm = "scm framework-" + format;
132       
133   SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
134   SCM func = scm_c_module_lookup (mod, "output-classic-framework");
135  
136
137   func = scm_variable_ref (func);
138   scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()),
139                                  self_scm (),
140                                  scopes,
141                                  dump_fields (),
142                                  SCM_UNDEFINED));
143
144   progress_indication ("\n");
145 }
146
147
148 /* TODO: resurrect more complex user-tweaks for titling?  */
149 Stencil
150 Paper_book::book_title ()
151 {
152   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("book-title"));
153   Stencil title;
154
155   SCM scopes = SCM_EOL;
156   if (ly_c_module_p (header_))
157     scopes = scm_cons (header_, scopes);
158
159  
160   SCM tit = SCM_EOL;
161   if (ly_c_procedure_p (title_func))
162     tit = scm_call_2 (title_func,
163                      paper_->self_scm (),
164                      scopes);
165
166   if (unsmob_stencil (tit))
167     title = *unsmob_stencil (tit);
168
169   if (!title.is_empty ())
170     title.align_to (Y_AXIS, UP);
171   
172   return title;
173 }
174
175 Stencil
176 Paper_book::score_title (int i)
177 {
178   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
179
180   Stencil title;
181
182   // ugh code dup
183   SCM scopes = SCM_EOL;
184   if (ly_c_module_p (header_))
185     scopes = scm_cons (header_, scopes);
186
187   if (ly_c_module_p (score_systems_[i].header_))
188     scopes = scm_cons (score_systems_[i].header_, scopes);
189   //end ugh
190
191   SCM tit = SCM_EOL;
192   if (ly_c_procedure_p (title_func))
193     tit = scm_call_2 (title_func,
194                      paper_->self_scm (),
195                      scopes);
196
197   if (unsmob_stencil (tit))
198     title = *unsmob_stencil (tit);
199
200   if (!title.is_empty ())
201     title.align_to (Y_AXIS, UP);
202   
203   return title;
204 }
205
206 void
207 set_system_penalty (Paper_system * ps, SCM header)
208 {
209   if (ly_c_module_p (header))
210     {
211       SCM force = ly_module_lookup (header, ly_symbol2scm ("breakbefore"));
212       if (SCM_VARIABLEP(force)
213           && scm_is_bool (SCM_VARIABLE_REF(force)))
214         {
215           ps->break_before_penalty_ = to_boolean (SCM_VARIABLE_REF(force))
216             ? -10000
217             : 10000;
218         }
219     }
220 }
221             
222 SCM
223 Paper_book::systems ()
224 {
225   if (systems_ != SCM_BOOL_F)
226     return systems_;
227
228   systems_ = SCM_EOL;
229   Stencil title = book_title ();
230
231   if (!title.is_empty ())
232     {
233       Paper_system *ps = new Paper_system (title, true);
234       set_system_penalty (ps, header_);
235       
236       systems_ = scm_cons (ps->self_scm (), systems_);
237       scm_gc_unprotect_object (ps->self_scm ());
238     }
239   
240   int score_count = score_systems_.size ();
241   for (int i = 0; i < score_count; i++)
242     {
243       Stencil title = score_title (i);      
244       if (!title.is_empty ())
245         {
246           Paper_system *ps = new Paper_system (title, true);
247           systems_ = scm_cons (ps->self_scm (), systems_);
248           scm_gc_unprotect_object (ps->self_scm ());
249
250           set_system_penalty (ps, score_systems_[i].header_);
251           
252         }
253       
254       if (scm_vector_p (score_systems_[i].systems_) == SCM_BOOL_T)
255         {
256           // guh.         
257           SCM system_list = scm_vector_to_list (score_systems_[i].systems_);
258
259           system_list = scm_reverse (system_list);
260           systems_ = scm_append (scm_list_2 (system_list, systems_));
261         }
262     }
263   
264   systems_ = scm_reverse (systems_);
265   
266   int i = 0;
267   Paper_system *last = 0;
268   for (SCM s = systems_; s != SCM_EOL; s = scm_cdr (s))
269     {
270       Paper_system *ps = unsmob_paper_system (scm_car (s));
271       ps->number_ = ++i;
272
273       if (last
274           && last->is_title ()
275           && !ps->break_before_penalty_)
276         ps->break_before_penalty_ = 10000;
277       last = ps;
278     }
279   
280   return systems_;
281 }
282
283 SCM
284 Paper_book::pages ()
285 {
286   if (SCM_BOOL_F != pages_)
287     return pages_;
288
289   pages_ = SCM_EOL;
290   SCM proc = paper_->c_variable ("page-breaking");
291   pages_ = scm_apply_0 (proc, scm_list_2 (systems (), self_scm ()));
292   return pages_;
293 }
294
295
296 /****************************************************************/
297
298 Score_systems::Score_systems ()
299 {
300   systems_ = SCM_EOL;
301   header_ = SCM_EOL;
302 }
303
304 void
305 Score_systems::gc_mark ()
306 {
307   scm_gc_mark (systems_);
308   scm_gc_mark (header_);
309 }
310