]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
65f4bdd966a0290961f9aa413b3580d7bab717dd
[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 <stdio.h>
10
11 #include "ly-module.hh"
12 #include "main.hh"
13 #include "paper-book.hh"
14 #include "paper-def.hh"
15 #include "paper-outputter.hh"
16 #include "paper-score.hh"
17 #include "stencil.hh"
18
19 // WIP -- simplistic page interface
20 // Do we need this at all?  SCM, smob?
21 class Page
22 {
23 public:
24   Paper_def *paper_;
25
26   SCM lines_;
27   Stencil *header_;
28   Stencil *footer_;
29
30   /* actual height filled with text.  */
31   Real height_;
32   
33   //HMMM all this size stuff to paper/paper-outputter?
34   Real hsize_;
35   Real vsize_;
36   Real foot_sep_;
37   Real head_sep_;
38
39   /* available area for text.  */
40   Real text_height ();
41
42   Page (Paper_def*);
43   void output (Paper_outputter*, bool);
44 };
45
46 Page::Page (Paper_def *paper)
47 {
48   paper_ = paper;
49   height_ = 0;
50   header_ = 0;
51   footer_ = 0;
52   lines_ = SCM_EOL;
53   
54   hsize_ = paper->get_realvar (ly_symbol2scm ("hsize"));
55   vsize_ = paper->get_realvar (ly_symbol2scm ("vsize"));
56   head_sep_ = 0; //paper->get_realvar (ly_symbol2scm ("head-sep"));
57   foot_sep_ = 0; //paper->get_realvar (ly_symbol2scm ("foot-sep"));
58 }
59
60 void
61 Page::output (Paper_outputter *out, bool is_last)
62 {
63   // TODO: header/footer etc
64   out->output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
65   for (SCM s = lines_; gh_pair_p (s); s = ly_cdr (s))
66     out->output_line (ly_car (s), is_last && gh_pair_p (ly_cdr (s)));
67   out->output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
68                                   gh_bool2scm (is_last)));
69 }
70
71 Real
72 Page::text_height ()
73 {
74   Real h = vsize_;
75   if (header_)
76     h -= header_->extent (Y_AXIS).length () + head_sep_;
77   if (footer_)
78     h -= footer_->extent (Y_AXIS).length () + foot_sep_;
79   return h;
80 }
81
82 /****************************************************************/
83
84 /* Current global paper book.  Gives default_rendering access from
85    input-file-results.  */
86 Paper_book *paper_book;
87
88 Paper_book::Paper_book ()
89 {
90   smobify_self ();
91 }
92
93 Paper_book::~Paper_book ()
94 {
95 }
96
97 void
98 Paper_book::output (String outname)
99 {
100   /* Generate all stencils to trigger font loads.  */
101   Link_array<Page> *pages = get_pages ();
102
103   Paper_def *paper = papers_[0];
104   Paper_outputter *out = paper->get_paper_outputter (outname);
105   out->output_metadata (get_scopes (0), paper);
106   out->output_header (paper);
107
108   int page_count = pages->size ();
109   for (int i = 0; i < page_count; i++)
110     (*pages)[i]->output (out, i + 1 == page_count);
111
112   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
113   progress_indication ("\n");
114 }
115
116 SCM
117 Paper_book::get_scopes (int i)
118 {
119   SCM scopes = SCM_EOL;
120   if (headers_[i])
121     scopes = scm_cons (headers_[i], scopes);
122   if (global_headers_[i] && global_headers_[i] != headers_[i])
123     scopes = scm_cons (global_headers_[i], scopes);
124   return scopes;
125 }
126
127 Stencil*
128 Paper_book::get_title (int i)
129 {
130   SCM user_title = scm_primitive_eval (ly_symbol2scm ("user-title"));
131   SCM book_title = scm_primitive_eval (ly_symbol2scm ("book-title"));
132   SCM score_title = scm_primitive_eval (ly_symbol2scm ("score-title"));
133   SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
134                : ly_symbol2scm ("scoreTitle"));
135
136   Stencil *title = 0;
137   SCM scopes = get_scopes (i);
138   SCM s = ly_modules_lookup (scopes, field);
139   if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
140     title = unsmob_stencil (scm_call_2 (user_title,
141                                         papers_[0]->self_scm (),
142                                         scm_variable_ref (s)));
143   else
144     title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
145                                         papers_[0]->self_scm (),
146                                         scopes));
147   if (title)
148     title->align_to (Y_AXIS, UP);
149   
150   return title;
151 }
152
153 /* TODO:
154    - decent page breaking algorithm; fill-up page
155    - header / footer (generate per Page, with page#, tagline/copyright)
156    - override: # pages, or pageBreakLines= #'(3 3 4), ?  */
157 Link_array<Page>*
158 Paper_book::get_pages ()
159 {
160   Link_array<Page> *pages = new Link_array<Page>;
161   int score_count = scores_.size ();
162
163   /* Calculate the full book height.  Hmm, can't we cache system
164      heights while making stencils?  */
165   Real book_height = 0;
166   for (int i = 0; i < score_count; i++)
167     {
168       Stencil *title = get_title (i);
169       if (title)
170         book_height += title->extent (Y_AXIS).length ();
171
172       int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
173       for (int j = 0; j < line_count; j++)
174         {
175           SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
176           book_height += ly_scm2offset (ly_car (line))[Y_AXIS];
177         }
178     }
179
180   Paper_def *paper = papers_[0];
181   Page *page = new Page (paper);
182   fprintf (stderr, "book_height: %f\n", book_height);
183   fprintf (stderr, "vsize: %f\n", page->vsize_);
184   fprintf (stderr, "pages: %f\n", book_height / page->text_height ());
185
186   /* Simplistic page breaking.  */
187   Real text_height = page->text_height ();
188   for (int i = 0; i < score_count; i++)
189     {
190       Stencil *title = get_title (i);
191       if (title)
192         book_height += title->extent (Y_AXIS).length ();
193       Real h = 0;
194       if (title)
195         h = title->extent (Y_AXIS).length ();
196
197       int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
198       for (int j = 0; j < line_count; j++)
199         {
200           SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
201           h += ly_scm2offset (ly_car (line))[Y_AXIS];
202           if (page->height_ + h > text_height)
203             {
204               pages->push (page);
205               page = new Page (paper);
206             }
207           if (page->height_ + h <= text_height || page->height_ == 0)
208             {
209               if (j == 0 && title)
210                 {
211                   Offset dim = Offset (title->extent (X_AXIS).length (),
212                                        title->extent (Y_AXIS).length ());
213                   page->lines_
214                     = ly_snoc (scm_cons (ly_offset2scm (dim),
215                                          scm_list_1
216                                          (scm_cons
217                                           (ly_offset2scm (Offset (0, 0)),
218                                            title->smobbed_copy ()))),
219                              page->lines_);
220                 }
221               page->lines_ = ly_snoc (line, page->lines_);
222               page->height_ += h;
223               h = 0;
224             }
225         }
226     }
227   
228   pages->push (page);
229   return pages;
230 }
231
232 void
233 Paper_book::classic_output (String outname)
234 {
235   Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
236   int count = scores_.size ();
237   
238   out->output_metadata (get_scopes (count - 1), papers_.top ());
239   out->output_header (papers_.top ());
240
241   int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
242   for (int i = 0; i < line_count; i++)
243     out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
244                       i == line_count - 1);
245   
246   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
247   progress_indication ("\n");
248 }
249
250
251 #include "ly-smobs.icc"
252
253 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
254 IMPLEMENT_SMOBS (Paper_book)
255 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
256
257 SCM
258 Paper_book::mark_smob (SCM smob)
259 {
260   Paper_book *pb = (Paper_book*) SCM_CELL_WORD_1 (smob);
261   for (int i = 0; i < pb->headers_.size (); i++)
262     scm_gc_mark (pb->headers_[i]);
263   for (int i = 0; i < pb->global_headers_.size (); i++)
264     scm_gc_mark (pb->global_headers_[i]);
265   for (int i = 0; i < pb->papers_.size (); i++)
266     scm_gc_mark (pb->papers_[i]->self_scm ());
267   for (int i = 0; i < pb->scores_.size (); i++)
268     scm_gc_mark (pb->scores_[i]);
269   return SCM_EOL;
270 }
271
272 int
273 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
274 {
275   Paper_book *b = (Paper_book*) ly_cdr (smob);
276      
277   scm_puts ("#<", port);
278   scm_puts (classname (b), port);
279   scm_puts (" ", port);
280   //scm_puts (b->, port);
281   scm_puts (">", port);
282   return 1;
283 }
284