]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* input/regression/score-text.ly: Really add.
[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 "text-item.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 (">", 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
138   func = scm_variable_ref (func);
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   SCM page_properties
242     = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
243                   paper_->self_scm ());
244
245   int score_count = score_systems_.size ();
246   for (int i = 0; i < score_count; i++)
247     {
248       Stencil title = score_title (i);      
249       if (!title.is_empty ())
250         {
251           Paper_system *ps = new Paper_system (title, true);
252           systems_ = scm_cons (ps->self_scm (), systems_);
253           scm_gc_unprotect_object (ps->self_scm ());
254
255           set_system_penalty (ps, score_systems_[i].header_);
256           
257         }
258       
259       if (scm_vector_p (score_systems_[i].systems_) == SCM_BOOL_T)
260         {
261           // guh.         
262           SCM system_list = scm_vector_to_list (score_systems_[i].systems_);
263
264           system_list = scm_reverse (system_list);
265           systems_ = scm_append (scm_list_2 (system_list, systems_));
266         }
267
268       for (SCM s = score_systems_[i].texts_; s != SCM_EOL; s = scm_cdr (s))
269         {
270           SCM t = Text_interface::interpret_markup (paper_->self_scm (),
271                                                     page_properties,
272                                                     scm_car (s));
273           // FIXME: title=true?
274           Paper_system *ps = new Paper_system (*unsmob_stencil (t), true);
275           systems_ = scm_cons (ps->self_scm (), systems_);
276           scm_gc_unprotect_object (ps->self_scm ());
277           // FIXME: figure out penalty.
278           //set_system_penalty (ps, score_systems_[i].header_);
279         }
280     }
281   
282   systems_ = scm_reverse (systems_);
283   
284   int i = 0;
285   Paper_system *last = 0;
286   for (SCM s = systems_; s != SCM_EOL; s = scm_cdr (s))
287     {
288       Paper_system *ps = unsmob_paper_system (scm_car (s));
289       ps->number_ = ++i;
290
291       if (last
292           && last->is_title ()
293           && !ps->break_before_penalty_)
294         ps->break_before_penalty_ = 10000;
295       last = ps;
296     }
297   
298   return systems_;
299 }
300
301 SCM
302 Paper_book::pages ()
303 {
304   if (SCM_BOOL_F != pages_)
305     return pages_;
306
307   pages_ = SCM_EOL;
308   SCM proc = paper_->c_variable ("page-breaking");
309   pages_ = scm_apply_0 (proc, scm_list_2 (systems (), self_scm ()));
310   return pages_;
311 }
312
313
314 /****************************************************************/
315
316 Score_systems::Score_systems ()
317 {
318   systems_ = SCM_EOL;
319   header_ = SCM_EOL;
320   texts_ = SCM_EOL;
321 }
322
323 void
324 Score_systems::gc_mark ()
325 {
326   scm_gc_mark (systems_);
327   scm_gc_mark (header_);
328   scm_gc_mark (texts_);
329 }
330