]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* lily/text-spanner.cc: add bound-padding.
[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 #include "paper-score.hh"
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   header_ = SCM_EOL;
23   header_0_ = SCM_EOL;
24   pages_ = SCM_BOOL_F;
25   scores_ = SCM_EOL;
26   systems_ = SCM_BOOL_F;
27
28   paper_ = 0;
29   smobify_self ();
30 }
31
32 Paper_book::~Paper_book ()
33 {
34 }
35
36 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
37 IMPLEMENT_SMOBS (Paper_book)
38   IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
39
40   SCM
41 Paper_book::mark_smob (SCM smob)
42 {
43   Paper_book *b = (Paper_book *) SCM_CELL_WORD_1 (smob);
44   if (b->paper_)
45     scm_gc_mark (b->paper_->self_scm ());
46   scm_gc_mark (b->header_);
47   scm_gc_mark (b->header_0_);
48   scm_gc_mark (b->pages_);
49   scm_gc_mark (b->scores_);
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 (">", 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::add_score (SCM s)
78 {
79   scores_ = scm_cons (s, scores_);
80 }
81
82 void
83 Paper_book::output (String outname)
84 {
85   if (scores_ == SCM_EOL)
86     return;
87
88   /* Generate all stencils to trigger font loads.  */
89   pages ();
90
91   SCM scopes = SCM_EOL;
92   if (ly_c_module_p (header_))
93     scopes = scm_cons (header_, scopes);
94
95   String mod_nm = "scm framework-" + output_backend_global;
96
97   SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
98   if (make_print)
99     {
100       SCM func = scm_c_module_lookup (mod, "output-framework");
101
102       func = scm_variable_ref (func);
103       scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()),
104                                      self_scm (),
105                                      scopes,
106                                      dump_fields (),
107                                      SCM_UNDEFINED));
108     }
109
110   if (make_preview)
111     {
112       SCM func = scm_c_module_lookup (mod, "output-preview-framework");
113       func = scm_variable_ref (func);
114       scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()),
115                                      self_scm (),
116                                      scopes,
117                                      dump_fields (),
118                                      SCM_UNDEFINED));
119     }
120 }
121
122 void
123 Paper_book::classic_output (String outname)
124 {
125   /* Generate all stencils to trigger font loads.  */
126   systems ();
127
128   SCM scopes = SCM_EOL;
129   if (ly_c_module_p (header_))
130     scopes = scm_cons (header_, scopes);
131
132   if (ly_c_module_p (header_0_))
133     scopes = scm_cons (header_0_, scopes);
134
135   String format = output_backend_global;
136   String mod_nm = "scm framework-" + format;
137
138   SCM mod = scm_c_resolve_module (mod_nm.to_str0 ());
139   SCM func = scm_c_module_lookup (mod, "output-classic-framework");
140
141   func = scm_variable_ref (func);
142   scm_apply_0 (func, scm_list_n (scm_makfrom0str (outname.to_str0 ()),
143                                  self_scm (),
144                                  scopes,
145                                  dump_fields (),
146                                  SCM_UNDEFINED));
147
148   progress_indication ("\n");
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   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 (SCM header)
179 {
180   SCM title_func = paper_->lookup_variable (ly_symbol2scm ("score-title"));
181
182   Stencil title;
183
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 (header))
189     scopes = scm_cons (header, scopes);
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 void
223 Paper_book::add_score_title (SCM header)
224 {
225   Stencil title = score_title (header);
226   if (title.is_empty ())
227     title = score_title (header_);
228   if (!title.is_empty ())
229     {
230       Paper_system *ps = new Paper_system (title, true);
231       systems_ = scm_cons (ps->self_scm (), systems_);
232       scm_gc_unprotect_object (ps->self_scm ());
233       set_system_penalty (ps, header);
234     }
235 }
236
237 SCM
238 Paper_book::systems ()
239 {
240   if (systems_ != SCM_BOOL_F)
241     return systems_;
242
243   systems_ = SCM_EOL;
244   Stencil title = book_title ();
245
246   if (!title.is_empty ())
247     {
248       Paper_system *ps = new Paper_system (title, true);
249       set_system_penalty (ps, header_);
250
251       systems_ = scm_cons (ps->self_scm (), systems_);
252       scm_gc_unprotect_object (ps->self_scm ());
253     }
254
255   SCM page_properties
256     = scm_call_1 (ly_lily_module_constant ("layout-extract-page-properties"),
257                   paper_->self_scm ());
258
259   SCM header = SCM_EOL;
260   for (SCM s = scm_reverse (scores_); s != SCM_EOL; s = scm_cdr (s))
261     {
262       if (ly_c_module_p (scm_car (s)))
263         {
264           header = scm_car (s);
265           if (header_0_ == SCM_EOL)
266             header_0_ = header;
267         }
268       else if (Paper_score *pscore
269                = dynamic_cast<Paper_score *> (unsmob_music_output (scm_car (s))))
270         {
271           add_score_title (header);
272           
273           header = SCM_EOL;
274
275           
276           SCM system_list = scm_vector_to_list (pscore->get_paper_systems ());
277           system_list = scm_reverse (system_list);
278           systems_ = scm_append (scm_list_2 (system_list, systems_));
279         }
280       else if (scm_is_vector (scm_car (s)))
281         {
282           /*
283             UGH. code dup.  
284            */
285           add_score_title (header);
286           header = SCM_EOL;
287           
288           SCM system_list = scm_vector_to_list (scm_car (s));
289           system_list = scm_reverse (system_list);
290           systems_ = scm_append (scm_list_2 (system_list, systems_));
291         }
292       else if (Text_interface::markup_p (scm_car (s)))
293         {
294           SCM t = Text_interface::interpret_markup (paper_->self_scm (),
295                                                     page_properties,
296                                                     scm_car (s));
297           // FIXME: title=true?
298           Paper_system *ps = new Paper_system (*unsmob_stencil (t), true);
299           systems_ = scm_cons (ps->self_scm (), systems_);
300           scm_gc_unprotect_object (ps->self_scm ());
301           // FIXME: figure out penalty.
302           //set_system_penalty (ps, scores_[i].header_);
303         }
304       else
305         assert (0);
306     }
307
308   systems_ = scm_reverse (systems_);
309
310   int i = 0;
311   Paper_system *last = 0;
312   for (SCM s = systems_; s != SCM_EOL; s = scm_cdr (s))
313     {
314       Paper_system *ps = unsmob_paper_system (scm_car (s));
315       ps->number_ = ++i;
316
317       if (last
318           && last->is_title ()
319           && !ps->break_before_penalty_)
320         ps->break_before_penalty_ = 10000;
321       last = ps;
322     }
323
324   return systems_;
325 }
326
327 SCM
328 Paper_book::pages ()
329 {
330   if (SCM_BOOL_F != pages_)
331     return pages_;
332
333   pages_ = SCM_EOL;
334   SCM proc = paper_->c_variable ("page-breaking");
335   pages_ = scm_apply_0 (proc, scm_list_2 (systems (), self_scm ()));
336   return pages_;
337 }