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