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