]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* lily/paper-book.cc (split_string): new function
[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 "page.hh"
12 #include "paper-book.hh"
13 #include "output-def.hh"
14 #include "paper-outputter.hh"
15 #include "paper-line.hh"
16 #include "paper-score.hh"
17 #include "stencil.hh"
18 #include "warn.hh"
19
20
21 // JUNKME
22 SCM
23 stencil2line (Stencil stil, bool is_title = false)
24 {
25   static SCM z;
26   if (!z)
27     z = scm_permanent_object (ly_offset2scm (Offset (0, 0)));
28   Offset dim = Offset (stil.extent (X_AXIS).length (),
29                        stil.extent (Y_AXIS).length ());
30   Paper_line *pl = new Paper_line (dim, scm_cons (stil.smobbed_copy (),
31                                                   SCM_EOL),
32                                    -10001 * is_title, is_title);
33
34   return scm_gc_unprotect_object (pl->self_scm ());
35 }
36
37
38 Paper_book::Paper_book ()
39 {
40   copyright_ = SCM_EOL;
41   tagline_ = SCM_EOL;
42   header_ = SCM_EOL;
43   
44   bookpaper_ = 0;
45   smobify_self ();
46 }
47
48 Paper_book::~Paper_book ()
49 {
50 }
51
52 #include "ly-smobs.icc"
53
54 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
55 IMPLEMENT_SMOBS (Paper_book)
56 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
57
58 SCM
59 Paper_book::mark_smob (SCM smob)
60 {
61   Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
62   for (int i = 0; i < b->score_lines_.size (); i++)
63     b->score_lines_[i].gc_mark ();
64
65   scm_gc_mark (b->copyright_);
66   if (b->bookpaper_)
67     scm_gc_mark (b->bookpaper_->self_scm ());
68   scm_gc_mark (b->header_);
69   return b->tagline_;
70 }
71
72 int
73 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
74 {
75   Paper_book *b = (Paper_book*) ly_cdr (smob);
76      
77   scm_puts ("#<", port);
78   scm_puts (classname (b), port);
79   scm_puts (" ", port);
80   //scm_puts (b->, port);
81   scm_puts (">", port);
82   return 1;
83 }
84
85 Array<String>
86 split_string (String s, char c)
87 {
88   Array<String> rv; 
89   while (s.length ())
90     {
91       int i = s.index (c);
92       
93       if (i == 0)
94         {
95           s = s.nomid_string (0, 1);
96           continue;
97         }
98       
99       if (i < 0)
100         i = s.length () ;
101
102       rv.push (s.cut_string (0, i));
103       s = s.nomid_string (0, i);
104     }
105
106   return rv;
107 }
108   
109
110
111 /*
112   TODO: there is too much code dup, and the interface is not
113   clear. FIXME.
114  */
115 void
116 Paper_book::output (String outname)
117 {
118   if (!score_lines_.size ())
119     // FIXME: no end-output?
120     return;
121     
122   /* Generate all stencils to trigger font loads.  */
123   SCM pages = this->pages ();
124
125   Array<String> output_formats = split_string (output_format_global, ',');
126
127   for (int i = 0; i < output_formats.size (); i++)
128     {
129       String format = output_formats[i];
130       Paper_outputter *out = get_paper_outputter (outname + "." + output_formats[i], format);
131       int page_count = scm_ilength (pages);
132
133       SCM scopes = SCM_EOL;
134       if (ly_c_module_p (header_))
135         scopes = scm_cons (header_, scopes);
136   
137       out->output_header (bookpaper_, scopes, page_count, false);
138
139       for (SCM s = pages; s != SCM_EOL; s = ly_cdr (s))
140         {
141           Page *p = unsmob_page (ly_car (s));
142           progress_indication ("[" + to_string (p->number_));
143           out->output_page (p, ly_cdr (s) == SCM_EOL);
144           progress_indication ("]");
145         }
146
147       out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
148       progress_indication ("\n");
149     }
150 }
151
152 Stencil
153 Paper_book::title (int i)
154 {
155   SCM user_title = bookpaper_->lookup_variable (ly_symbol2scm ("user-title"));
156   SCM book_title = bookpaper_->lookup_variable (ly_symbol2scm ("book-title"));
157   SCM score_title = bookpaper_->lookup_variable (ly_symbol2scm ("score-title"));
158   SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
159                : ly_symbol2scm ("scoreTitle"));
160
161   Stencil title;
162
163   // ugh code dup
164   SCM scopes = SCM_EOL;
165   if (ly_c_module_p (header_))
166     scopes = scm_cons (header_, scopes);
167
168   if (ly_c_module_p (score_lines_[i].header_))
169     scopes = scm_cons (score_lines_[i].header_, scopes);
170    //end ugh
171   
172   SCM s = ly_modules_lookup (scopes, field, SCM_BOOL_F);
173   if (s != SCM_BOOL_F)
174     title = *unsmob_stencil (scm_call_2 (user_title,
175                                          bookpaper_->self_scm (),
176                                          s));
177   else
178     title = *unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
179                                          bookpaper_->self_scm (),
180                                          scopes));
181   if (!title.is_empty ())
182     title.align_to (Y_AXIS, UP);
183   
184   return title;
185 }
186
187 void
188 Paper_book::classic_output (String outname)
189 {
190   String format = "tex";
191   Paper_outputter *out = get_paper_outputter (outname + "." + format, format);
192
193   Output_def * p = bookpaper_;
194   while (p && p->parent_)
195     p = p->parent_;
196
197   // ugh code dup
198   SCM scopes = SCM_EOL;
199   if (ly_c_module_p (header_))
200     scopes = scm_cons (header_, scopes);
201
202   if (ly_c_module_p (score_lines_[0].header_))
203     scopes = scm_cons (score_lines_[0].header_, scopes);
204   //end ugh
205   
206   out->output_header (p, scopes, 0, true);
207
208   SCM top_lines = score_lines_.top ().lines_;
209   Paper_line *first = unsmob_paper_line (scm_vector_ref (top_lines,
210                                                          scm_int2num (0)));
211   Offset o (0, -0.5 * first->dim ()[Y_AXIS]);
212   int line_count = SCM_VECTOR_LENGTH (top_lines);
213   for (int i = 0; i < line_count; i++)
214     {
215       /* In classic compatibility TeX tracks how large things are, and
216          advances the Y pos for us.  If we advance it too, we get too
217          much space.
218
219          FIXME: vague... why is TeX is different from other ouput
220                 backends, why not fix the TeX backend? -- jcn */
221       if (format == "tex")
222         o = Offset (0, 0);
223
224       out->output_line (scm_vector_ref (top_lines, scm_int2num (i)),
225                         &o, i == line_count - 1);
226     }
227   
228   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
229   progress_indication ("\n");
230 }
231
232 /* calculate book height, #lines, stencils.  */
233 void
234 Paper_book::init ()
235 {
236   int score_count = score_lines_.size ();
237
238   /* Calculate the full book height.  Hmm, can't we cache system
239      heights while making stencils?  */
240   height_ = 0;
241   for (int i = 0; i < score_count; i++)
242     {
243       Stencil title = this->title (i);
244       if (!title.is_empty ())
245         height_ += title.extent (Y_AXIS).length ();
246
247       int line_count = SCM_VECTOR_LENGTH (score_lines_[i].lines_);
248       for (int j = 0; j < line_count; j++)
249         {
250           SCM s = scm_vector_ref ((SCM) score_lines_[i].lines_, scm_int2num (j));
251           height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
252         }
253     }
254
255   Output_def *paper = bookpaper_;
256   
257   SCM scopes = SCM_EOL;
258   if (ly_c_module_p (header_))
259     scopes = scm_cons (header_, scopes);
260
261   SCM make_tagline = paper->c_variable ("make-tagline");
262   tagline_ = scm_call_2 (make_tagline, paper->self_scm (), scopes);
263   Real tag_height = 0;
264   if (Stencil *s = unsmob_stencil (tagline_))
265     tag_height = s->extent (Y_AXIS).length ();
266   height_ += tag_height;
267
268   SCM make_copyright = paper->c_variable ("make-copyright");
269   copyright_ = scm_call_2 (make_copyright, paper->self_scm (), scopes);
270   Real copy_height = 0;
271   if (Stencil *s = unsmob_stencil (copyright_))
272     copy_height = s->extent (Y_AXIS).length ();
273   height_ += copy_height;
274 }
275
276 SCM
277 Paper_book::lines ()
278 {
279   int score_count = score_lines_.size ();
280   SCM lines = SCM_EOL;
281   for (int i = 0; i < score_count; i++)
282     {
283       Stencil title = this->title (i);      
284       if (!title.is_empty ())
285         lines = ly_snoc (stencil2line (title, true), lines);
286       lines = scm_append (scm_list_2 (lines, scm_vector_to_list (score_lines_[i].lines_)));
287     }
288   
289   //debug helper; ughr
290   int i = 0;
291   for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
292     unsmob_paper_line (ly_car (s))->number_ = ++i;
293   return lines;
294 }
295
296 SCM
297 Paper_book::pages ()
298 {
299   init ();
300   Page::page_count_ = 0;
301
302   Output_def *paper = bookpaper_;
303   Page *page = new Page (paper, 1);
304
305   Real text_height = page->text_height ();
306
307   Real copy_height = 0;
308   if (Stencil *s = unsmob_stencil (copyright_))
309     copy_height = s->extent (Y_AXIS).length ();
310
311   Real tag_height = 0;
312   if (Stencil *s = unsmob_stencil (tagline_))
313     tag_height = s->extent (Y_AXIS).length ();
314
315   SCM all = lines ();
316   SCM proc = paper->c_variable ("page-breaking");
317   SCM breaks = scm_apply_0 (proc, scm_list_n (all, scm_make_real (height_),
318                                               scm_make_real (text_height),
319                                               scm_make_real (-copy_height),
320                                               scm_make_real (-tag_height),
321                                               SCM_UNDEFINED));
322
323   /* Copyright on first page.  */
324   if (unsmob_stencil (copyright_))
325     page->copyright_ = copyright_;
326
327   SCM pages = SCM_EOL;
328   int page_count = SCM_VECTOR_LENGTH ((SCM) breaks);
329   int line = 1;
330
331   for (int i = 0; i < page_count; i++)
332     {
333       if (i)
334         page = new Page (paper, i + 1);
335
336       int next = i + 1 < page_count
337         ? ly_scm2int (scm_vector_ref (breaks, scm_int2num (i))) : 0;
338       while ((!next && all != SCM_EOL) || line <= next)
339         {
340           SCM s = ly_car (all);
341           page->lines_ = ly_snoc (s, page->lines_);
342           page->height_ += unsmob_paper_line (s)->dim ()[Y_AXIS];
343           page->line_count_++;
344           all = ly_cdr (all);
345           line++;
346         }
347       pages = scm_cons (page->self_scm (), pages);
348     }
349
350   /* Tagline on last page.  */
351   if (unsmob_stencil (tagline_))
352     page->tagline_ = tagline_;
353
354   return scm_reverse (pages);
355 }
356
357 static SCM
358 c_ragged_page_breaks (SCM lines, Real book_height, Real text_height,
359                       Real first, Real last)
360 {
361   int page_number = 0;
362   int page_count = int (book_height / text_height + 0.5);
363   SCM breaks = SCM_EOL;
364   Real page_height = text_height + first;
365   Real h = 0;
366   int number = 0;
367   for (SCM s = lines; s != SCM_EOL; s = ly_cdr (s))
368     {
369       Paper_line *pl = unsmob_paper_line (ly_car (s));
370       if (!pl->is_title () && h < page_height)
371         number++;
372       h += pl->dim ()[Y_AXIS];
373       if (!pl->is_title () && h > page_height)
374         {
375           breaks = ly_snoc (scm_int2num (number), breaks);
376           page_number++;
377           page_height = text_height + (page_number == page_count) * last;
378           h = 0;
379         }
380       if (ly_cdr (s) == SCM_EOL)
381         breaks = ly_snoc (scm_int2num (pl->number_), breaks);
382     }
383
384   return scm_vector (breaks);
385 }
386
387 LY_DEFINE (ly_ragged_page_breaks, "ly:ragged-page-breaks",
388            5, 0, 0, (SCM lines, SCM book, SCM text, SCM first, SCM last),
389            "Return a vector with line numbers of page breaks.")
390 {
391   SCM_ASSERT_TYPE (scm_pair_p (lines), lines, SCM_ARG1, __FUNCTION__, "list");
392   SCM_ASSERT_TYPE (ly_c_number_p (book), book, SCM_ARG2, __FUNCTION__, "real");
393   SCM_ASSERT_TYPE (ly_c_number_p (text), text, SCM_ARG2, __FUNCTION__, "real");
394   SCM_ASSERT_TYPE (ly_c_number_p (first), first, SCM_ARG2, __FUNCTION__, "real");
395   SCM_ASSERT_TYPE (ly_c_number_p (last), last, SCM_ARG2, __FUNCTION__, "real");
396
397   return c_ragged_page_breaks (lines,
398                                ly_scm2double (book), ly_scm2double (text),
399                                ly_scm2double (first), ly_scm2double (last));
400 }
401
402 /****************************************************************/
403
404 Score_lines::Score_lines ()
405 {
406   lines_ = SCM_EOL;
407   header_ = SCM_EOL;
408 }
409
410 void
411 Score_lines::gc_mark ()
412 {
413   scm_gc_mark (lines_);
414   scm_gc_mark (header_);
415 }
416