]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
(default-page-make-stencil): don't stretch
[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 "ly-module.hh"
10 #include "main.hh"
11 #include "output-def.hh"
12 #include "paper-book.hh"
13 #include "paper-outputter.hh"
14 #include "paper-score.hh"
15 #include "paper-system.hh"
16 #include "stencil.hh"
17 #include "warn.hh"
18
19 #include "ly-smobs.icc"
20
21 Paper_book::Paper_book ()
22 {
23   pages_ = SCM_BOOL_F;
24   lines_ = SCM_BOOL_F;
25   header_ = SCM_EOL;
26   
27   bookpaper_ = 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_lines_.size (); i++)
44     b->score_lines_[i].gc_mark ();
45
46   if (b->bookpaper_)
47     scm_gc_mark (b->bookpaper_->self_scm ());
48   scm_gc_mark (b->header_);
49   scm_gc_mark (b->pages_);
50   return b->lines_;
51 }
52
53 int
54 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
55 {
56   Paper_book *b = (Paper_book*) ly_cdr (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 Array<String>
67 split_string (String s, char c)
68 {
69   Array<String> rv; 
70   while (s.length ())
71     {
72       int i = s.index (c);
73       
74       if (i == 0)
75         {
76           s = s.nomid_string (0, 1);
77           continue;
78         }
79       
80       if (i < 0)
81         i = s.length () ;
82
83       rv.push (s.cut_string (0, i));
84       s = s.nomid_string (0, i);
85     }
86
87   return rv;
88 }
89
90 SCM
91 dump_fields ()
92 {
93   SCM fields = SCM_EOL;
94   for (int i = dump_header_fieldnames_global.size (); i--; )
95     fields
96       = scm_cons (ly_symbol2scm (dump_header_fieldnames_global[i].to_str0 ()),
97                   fields);
98   return fields;
99 }
100
101 LY_DEFINE (ly_output_formats, "ly:output-formats",
102            0, 0, 0, (),
103            "Formats passed to --format as a list of strings, "
104            "used for the output.")
105 {
106   Array<String> output_formats = split_string (output_format_global, ',');
107
108   SCM lst = SCM_EOL;
109   for (int i = 0; i < output_formats.size (); i ++)
110     lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst);
111   
112   return lst; 
113 }
114
115 void
116 Paper_book::output (String outname)
117 {
118   if (!score_lines_.size ())
119     return;
120
121   
122   /* Generate all stencils to trigger font loads.  */
123   pages ();
124
125   
126   SCM formats = ly_output_formats();
127   for (SCM s = formats; ly_c_pair_p (s); s = ly_cdr (s)) 
128     {
129       String format = ly_scm2string (ly_car (s));
130       
131       Paper_outputter *out = get_paper_outputter (outname + "." + format, format);
132   
133       SCM scopes = SCM_EOL;
134       if (ly_c_module_p (header_))
135         scopes = scm_cons (header_, scopes);
136   
137       String func_nm = format;
138       func_nm = "output-framework-" + func_nm;
139         
140       SCM func = ly_scheme_function (func_nm.to_str0 ());
141       scm_apply_0 (func, scm_list_n (out->self_scm (),
142                                      self_scm (),
143                                      scopes,
144                                      dump_fields (),
145                                      scm_makfrom0str (outname.to_str0 ()),
146                                      SCM_UNDEFINED
147                                      )) ;
148
149       scm_gc_unprotect_object (out->self_scm ());
150     }
151 }
152
153
154 void
155 Paper_book::classic_output (String outname)
156 {
157
158   /* Generate all stencils to trigger font loads.  */
159   lines ();
160
161
162   // ugh code dup
163   SCM scopes = SCM_EOL;
164   if (ly_c_module_p (header_))
165     scopes = scm_cons (header_, scopes);
166
167   if (ly_c_module_p (score_lines_[0].header_))
168     scopes = scm_cons (score_lines_[0].header_, scopes);
169   //end ugh
170
171
172   Array<String> output_formats = split_string (output_format_global, ',');
173
174   for (int i = 0; i < output_formats.size (); i++)
175     {
176       String format = output_formats[i];
177       String func_nm = format;
178       func_nm = "output-classic-framework-" + func_nm;
179       
180       SCM func = ly_scheme_function (func_nm.to_str0 ());
181
182       Paper_outputter *out = get_paper_outputter (outname + "." + format, format);
183
184       scm_apply_0 (func, scm_list_n (out->self_scm (),
185                                      self_scm (),
186                                      scopes,
187                                      dump_fields (),
188                                      scm_makfrom0str (outname.to_str0 ()),
189                                      SCM_UNDEFINED
190                                      )) ;
191
192       scm_gc_unprotect_object (out->self_scm ());
193     }
194   
195   progress_indication ("\n");
196 }
197
198
199
200
201 LY_DEFINE(ly_paper_book_pages, "ly:paper-book-pages",
202           1,0,0,
203           (SCM pb),
204           "Return pages in book PB.")
205 {
206   return unsmob_paper_book(pb)->pages ();
207 }
208
209
210 LY_DEFINE(ly_paper_book_scopes, "ly:paper-book-scopes",
211           1,0,0,
212           (SCM book),
213           "Return pages in paper book @var{book}.")
214 {
215   Paper_book * pb =  unsmob_paper_book(book);
216   SCM_ASSERT_TYPE(pb, book, SCM_ARG1, __FUNCTION__, "Paper_book");
217   
218   SCM scopes = SCM_EOL;
219   if (ly_c_module_p (pb->header_))
220     scopes = scm_cons (pb->header_, scopes);
221   
222   return scopes;
223 }
224
225
226 LY_DEFINE(ly_paper_book_lines, "ly:paper-book-lines",
227           1,0,0,
228           (SCM pb),
229           "Return lines in book PB.")
230 {
231   return unsmob_paper_book (pb)->lines ();
232 }
233
234
235 LY_DEFINE(ly_paper_book_book_paper, "ly:paper-book-book-paper",
236           1,0,0,
237           (SCM pb),
238           "Return pages in book PB.")
239 {
240   return unsmob_paper_book(pb)->bookpaper_->self_scm ();
241 }
242
243 /*
244
245 TODO: resurrect more complex user-tweaks for titling? 
246
247 */
248 Stencil
249 Paper_book::book_title ()
250 {
251   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
252   Stencil title;
253
254   SCM scopes = SCM_EOL;
255   if (ly_c_module_p (header_))
256     scopes = scm_cons (header_, scopes);
257
258  
259   SCM tit = SCM_EOL;
260   if (ly_c_procedure_p (title_func))
261     tit = scm_call_2 (title_func,
262                      bookpaper_->self_scm (),
263                      scopes);
264
265   if (unsmob_stencil (tit))
266     title = *unsmob_stencil (tit);
267
268   if (!title.is_empty ())
269     title.align_to (Y_AXIS, UP);
270   
271   return title;
272 }
273
274   
275
276 Stencil
277 Paper_book::score_title (int i)
278 {
279   SCM title_func = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
280
281   Stencil title;
282
283   // ugh code dup
284   SCM scopes = SCM_EOL;
285   if (ly_c_module_p (header_))
286     scopes = scm_cons (header_, scopes);
287
288   if (ly_c_module_p (score_lines_[i].header_))
289     scopes = scm_cons (score_lines_[i].header_, scopes);
290   //end ugh
291
292   SCM tit = SCM_EOL;
293   if (ly_c_procedure_p (title_func))
294     tit = scm_call_2 (title_func,
295                      bookpaper_->self_scm (),
296                      scopes);
297
298   if (unsmob_stencil (tit))
299     title = *unsmob_stencil (tit);
300
301
302   if (!title.is_empty ())
303     title.align_to (Y_AXIS, UP);
304   
305   return title;
306 }
307   
308
309 SCM
310 Paper_book::lines ()
311 {
312   if (SCM_BOOL_F != lines_)
313     return lines_;
314
315   lines_ = SCM_EOL;
316   Stencil title = book_title ();
317
318   if (!title.is_empty ())
319     {
320       Paper_system *pl = new Paper_system (title, true);
321       
322       lines_ = scm_cons (pl->self_scm (), lines_);
323       scm_gc_unprotect_object (pl->self_scm ());
324     }
325   
326   int score_count = score_lines_.size ();
327   for (int i = 0; i < score_count; i++)
328     {
329       Stencil title = score_title (i);      
330       if (!title.is_empty ())
331         {
332           Paper_system *pl = new Paper_system (title, true);
333           lines_ = scm_cons (pl->self_scm (), lines_);
334           scm_gc_unprotect_object (pl->self_scm ());
335         }
336       
337       if (scm_vector_p (score_lines_[i].lines_) == SCM_BOOL_T)
338         {
339           SCM line_list = scm_vector_to_list (score_lines_[i].lines_); // guh.
340
341           line_list = scm_reverse (line_list);
342           lines_ = scm_append (scm_list_2 (line_list, lines_));
343         }
344     }
345   
346   lines_ = scm_reverse (lines_);
347   
348   int i = 0;
349   Paper_system * last = 0;
350   for (SCM s = lines_; s != SCM_EOL; s = ly_cdr (s))
351     {
352       Paper_system * p = unsmob_paper_line (ly_car (s));
353       p->number_ = ++i;
354
355       if (last && last->is_title ())
356         {
357           p->penalty_ = 10000;  // ugh, hardcoded.
358         }
359       last = p;
360     }
361
362   
363   return lines_;
364 }
365
366
367 SCM
368 Paper_book::pages ()
369 {
370   if (SCM_BOOL_F != pages_)
371     return pages_;
372
373   pages_ = SCM_EOL;
374   
375   Output_def *paper = bookpaper_;
376
377   SCM proc = paper->c_variable ("page-breaking");
378   pages_ = scm_apply_0 (proc, scm_list_n (lines (),
379                                           self_scm (),
380                                           SCM_UNDEFINED));
381
382   return pages_;
383 }
384
385
386
387
388 /****************************************************************/
389
390 Score_lines::Score_lines ()
391 {
392   lines_ = SCM_EOL;
393   header_ = SCM_EOL;
394 }
395
396 void
397 Score_lines::gc_mark ()
398 {
399   scm_gc_mark (lines_);
400   scm_gc_mark (header_);
401 }
402