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_;
83 Paper_book *paper_book;
85 Paper_book::Paper_book ()
91 Paper_book::output (String outname)
93 /* Generate all stencils to trigger font loads. */
94 Link_array<Page> *pages = get_pages ();
96 Paper_def *paper = papers_[0];
97 Paper_outputter *out = paper->get_paper_outputter (outname);
98 out->output_metadata (get_scopes (0), paper);
99 out->output_header (paper);
101 int page_count = pages->size ();
102 for (int i = 0; i < page_count; i++)
103 (*pages)[i]->output (out, i + 1 == page_count);
105 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
106 progress_indication ("\n");
110 Paper_book::get_scopes (int i)
112 SCM scopes = SCM_EOL;
114 scopes = scm_cons (headers_[i], scopes);
115 if (global_headers_[i] && global_headers_[i] != headers_[i])
116 scopes = scm_cons (global_headers_[i], scopes);
121 Paper_book::get_title (int i)
123 SCM make_title = scm_primitive_eval (ly_symbol2scm ("make-title"));
124 SCM field = (i == 0 ? ly_symbol2scm ("bookTitle")
125 : ly_symbol2scm ("scoreTitle"));
127 SCM s = ly_modules_lookup (get_scopes (i), field);
128 if (s != SCM_UNDEFINED && scm_variable_bound_p (s) == SCM_BOOL_T)
130 Stencil *title = unsmob_stencil (gh_call2 (make_title,
131 papers_[0]->self_scm (),
132 scm_variable_ref (s)));
134 title->align_to (Y_AXIS, UP);
144 FIXME: titling is broken.
148 * decent page breaking algorithm
149 * header / footer (generate per Page, with page#)
150 * override: # pages, or pageBreakLines= #'(3 3 4), ?
151 * what about between-system-breaking, can we junk that?
155 Paper_book::get_pages ()
157 Link_array<Page> *pages = new Link_array<Page>;
158 int score_count = scores_.size ();
160 /* Calculate the full book height. Hmm, can't we cache system
161 heights while making stencils? */
162 Real book_height = 0;
163 for (int i = 0; i < score_count; i++)
165 Stencil *title = get_title (i);
167 book_height += title->extent (Y_AXIS).length ();
169 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
170 for (int j = 0; j < line_count; j++)
172 SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
173 book_height += ly_scm2offset (ly_car (line))[Y_AXIS];
177 Paper_def *paper = papers_[0];
178 Page *page = new Page (paper);
179 fprintf (stderr, "book_height: %f\n", book_height);
180 fprintf (stderr, "vsize: %f\n", page->vsize_);
181 fprintf (stderr, "pages: %f\n", book_height / page->text_height ());
183 /* Simplistic page breaking. */
184 Real text_height = page->text_height ();
185 for (int i = 0; i < score_count; i++)
187 Stencil *title = get_title (i);
189 book_height += title->extent (Y_AXIS).length ();
192 h = title->extent (Y_AXIS).length ();
194 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_[i]);
195 for (int j = 0; j < line_count; j++)
197 SCM line = scm_vector_ref ((SCM) scores_[i], scm_int2num (j));
198 h += ly_scm2offset (ly_car (line))[Y_AXIS];
199 if (page->height_ + h > text_height)
202 page = new Page (paper);
204 if (page->height_ + h <= text_height || page->height_ == 0)
208 Offset dim = Offset (title->extent (X_AXIS).length (),
209 title->extent (Y_AXIS).length ());
211 = ly_snoc (scm_cons (ly_offset2scm (dim),
214 (ly_offset2scm (Offset (0, 0)),
215 title->smobbed_copy ()))),
218 page->lines_ = ly_snoc (line, page->lines_);
230 Paper_book::classic_output (String outname)
232 Paper_outputter *out = papers_.top ()->get_paper_outputter (outname);
233 int count = scores_.size ();
235 out->output_metadata (get_scopes (count - 1), papers_.top ());
236 out->output_header (papers_.top ());
238 int line_count = SCM_VECTOR_LENGTH ((SCM) scores_.top ());
239 for (int i = 0; i < line_count; i++)
240 out->output_line (scm_vector_ref ((SCM) scores_.top (), scm_int2num (i)),
241 i == line_count - 1);
243 out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
244 progress_indication ("\n");
248 #include "ly-smobs.icc"
250 IMPLEMENT_DEFAULT_EQUAL_P (Paper_book);
251 IMPLEMENT_SIMPLE_SMOBS (Paper_book)
252 IMPLEMENT_TYPE_P (Paper_book, "ly:paper_book?")
255 Paper_book::mark_smob (SCM smob)
257 Paper_book *b = (Paper_book*) SCM_CELL_WORD_1 (smob);
260 scm_gc_mark (b->scores_);
261 scm_gc_mark (b->global_headers_);
262 scm_gc_mark (b->headers_);
263 scm_gc_mark (b->papers_);
270 Paper_book::print_smob (SCM smob, SCM port, scm_print_state*)
272 Paper_book *b = (Paper_book*) ly_cdr (smob);
274 scm_puts ("#<", port);
275 scm_puts (classname (b), port);
276 scm_puts (" ", port);
277 //scm_puts (b->, port);
278 scm_puts (">", port);
283 Paper_book::smobbed_copy () const
285 Paper_book *b = new Paper_book (*this);
286 return b->smobbed_self ();