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