2 paper-book.cc -- implement Paper_book
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
11 #include "ly-module.hh"
13 #include "paper-book.hh"
14 #include "paper-def.hh"
15 #include "paper-outputter.hh"
16 #include "paper-score.hh"
19 // WIP -- simplistic page interface
20 // Do we need this at all? SCM, smob?
30 /* actual height filled with text. */
33 //HMMM all this size stuff to paper/paper-outputter?
39 /* available area for text. */
43 void output (Paper_outputter*, bool);
46 Page::Page (Paper_def *paper)
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"));
61 Page::output (Paper_outputter *out, bool is_last)
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)));
76 h -= header_->extent (Y_AXIS).length () + head_sep_;
78 h -= footer_->extent (Y_AXIS).length () + foot_sep_;
82 /****************************************************************/
84 /* Current global paper book. Gives default_rendering access from
85 input-file-results. */
86 Paper_book *paper_book;
88 Paper_book::Paper_book ()
93 Paper_book::~Paper_book ()
98 Paper_book::output (String outname)
100 /* Generate all stencils to trigger font loads. */
101 Link_array<Page> *pages = get_pages ();
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);
108 int page_count = pages->size ();
109 for (int i = 0; i < page_count; i++)
110 (*pages)[i]->output (out, i + 1 == page_count);
112 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
113 progress_indication ("\n");
117 Paper_book::get_scopes (int i)
119 SCM scopes = SCM_EOL;
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);
128 Paper_book::get_title (int i)
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"));
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)));
144 title = unsmob_stencil (scm_call_2 (i == 0 ? book_title : score_title,
145 papers_[0]->self_scm (),
148 title->align_to (Y_AXIS, UP);
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), ? */
158 Paper_book::get_pages ()
160 Link_array<Page> *pages = new Link_array<Page>;
161 int score_count = scores_.size ();
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++)
168 Stencil *title = get_title (i);
170 book_height += title->extent (Y_AXIS).length ();
172 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
173 for (int j = 0; j < line_count; j++)
175 SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
176 book_height += ly_scm2offset (ly_car (line))[Y_AXIS];
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 ());
186 /* Simplistic page breaking. */
187 Real text_height = page->text_height ();
188 for (int i = 0; i < score_count; i++)
190 Stencil *title = get_title (i);
192 book_height += title->extent (Y_AXIS).length ();
195 h = title->extent (Y_AXIS).length ();
197 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
198 for (int j = 0; j < line_count; j++)
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)
205 page = new Page (paper);
207 if (page->height_ + h <= text_height || page->height_ == 0)
211 Offset dim = Offset (title->extent (X_AXIS).length (),
212 title->extent (Y_AXIS).length ());
214 = ly_snoc (scm_cons (ly_offset2scm (dim),
217 (ly_offset2scm (Offset (0, 0)),
218 title->smobbed_copy ()))),
221 page->lines_ = ly_snoc (line, page->lines_);
233 Paper_book::classic_output (String outname)
235 Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
236 int count = scores_.size ();
238 out->output_metadata (get_scopes (count - 1), papers_.top ());
239 out->output_header (papers_.top ());
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);
246 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
247 progress_indication ("\n");
251 #include "ly-smobs.icc"
253 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
254 IMPLEMENT_SMOBS (Paper_book)
255 IMPLEMENT_TYPE_P (Paper_book, "ly:paper-book?")
258 Paper_book::mark_smob (SCM smob)
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]);
273 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
275 Paper_book *b = (Paper_book*) ly_cdr (smob);
277 scm_puts ("#<", port);
278 scm_puts (classname (b), port);
279 scm_puts (" ", port);
280 //scm_puts (b->, port);
281 scm_puts (">", port);