]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-book.cc
* lily/font-select.cc (properties_to_font_size_family): Fix
[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 "main.hh"
12 #include "paper-book.hh"
13 #include "paper-score.hh"
14 #include "paper-def.hh"
15 #include "stencil.hh"
16 #include "paper-outputter.hh"
17
18 // -- this simply adds one Page per \score
19 // #define ONE_SCORE_PER_PAGE
20  
21 // WIP -- simplistic page interface
22 // Do we need this at all?  SCM, smob?
23 class Page
24 {
25 public:
26   Paper_def *paper_;
27
28   SCM lines_;
29   Stencil *header_;
30   Stencil *footer_;
31
32   /* actual height filled with text.  */
33   Real height_;
34   
35   //HMMM all this size stuff to paper/paper-outputter?
36   Real hsize_;
37   Real vsize_;
38   Real foot_sep_;
39   Real head_sep_;
40
41   /* available area for text.  */
42   Real text_height ();
43
44   Page (Paper_def*);
45   void output (Paper_outputter*, bool);
46 };
47
48 Page::Page (Paper_def *paper)
49 {
50   paper_ = paper;
51   height_ = 0;
52   header_ = 0;
53   footer_ = 0;
54   lines_ = SCM_EOL;
55   
56   hsize_ = paper->get_realvar (ly_symbol2scm ("hsize"));
57   vsize_ = paper->get_realvar (ly_symbol2scm ("vsize"));
58   head_sep_ = 0; //paper->get_realvar (ly_symbol2scm ("head-sep"));
59   foot_sep_ = 0; //paper->get_realvar (ly_symbol2scm ("foot-sep"));
60 }
61
62 void
63 Page::output (Paper_outputter *out, bool is_last)
64 {
65 #if ONE_SCORE_PER_PAGE
66   int line_count = SCM_VECTOR_LENGTH ((SCM) lines_);
67   for (int i = 0; i < line_count; i++)
68     out->output_line (scm_vector_ref (lines_, scm_int2num (i)),
69                       is_last && i == line_count - 1);
70 #else
71   // TODO: header/footer etc
72   out->output_scheme (scm_list_1 (ly_symbol2scm ("start-page")));
73   for (SCM s = lines_; gh_pair_p (s); s = ly_cdr (s))
74     out->output_line (ly_car (s), is_last && gh_pair_p (ly_cdr (s)));
75   out->output_scheme (scm_list_2 (ly_symbol2scm ("stop-page"),
76                                   gh_bool2scm (is_last)));
77 #endif
78 }
79
80 Real
81 Page::text_height ()
82 {
83   Real h = vsize_;
84   if (header_)
85     h -= header_->extent (Y_AXIS).length () + head_sep_;
86   if (footer_)
87     h -= footer_->extent (Y_AXIS).length () + foot_sep_;
88   return h;
89 }
90
91
92 Paper_book *paper_book;
93
94 Paper_book::Paper_book ()
95 {
96 }
97
98 void
99 Paper_book::output ()
100 {
101   Paper_outputter *out = paper_scores_[0]->outputter_;
102   
103   Paper_def *paper = paper_scores_[0]->paper_;
104   out->output_header (paper);
105   
106   if (paper_scores_[0]->book_title_)
107     out->output_expr (paper_scores_[0]->book_title_->get_expr (),
108                       Offset (0, 0));
109
110   Link_array<Page> *pages = get_pages ();
111   int page_count = pages->size ();
112   for (int i = 0; i < page_count; i++)
113     (*pages)[i]->output (out, i + 1 == page_count);
114
115   out->output_scheme (scm_list_1 (ly_symbol2scm ("end-output")));
116   progress_indication ("\n");
117 }
118
119 /*
120   WIP
121
122   FIXME: titling is broken.
123   
124   TODO:
125      * ->SCM?
126      * decent page breaking algorithm
127      * header / footer (generate per Page, with page#)
128      * override: # pages, or pageBreakLines= #'(3 3 4), ?
129      * what about between-system-breaking, can we junk that?
130      
131 */
132 Link_array<Page>*
133 Paper_book::get_pages ()
134 {
135   Link_array<Page> *pages = new Link_array<Page>;
136   Paper_def *paper = paper_scores_[0]->paper_;
137   int score_count = paper_scores_.size ();
138
139   /* Calculate the full book height.  Hmm, can't we cache system
140      heights while making stencils?  */
141   Real book_height = 0;
142   for (int i = 0; i < score_count; i++)
143     {
144       Paper_score *pscore = paper_scores_[i];
145       Stencil *title = (i == 0 ? pscore->book_title_ : pscore->score_title_);
146       if (title)
147         book_height += title->extent (Y_AXIS).length ();
148
149       int line_count = SCM_VECTOR_LENGTH ((SCM) pscore->lines_);
150       for (int j = 0; j < line_count; j++)
151         {
152           SCM line = scm_vector_ref (pscore->lines_, scm_int2num (j));
153           book_height += ly_scm2offset (ly_car (line))[Y_AXIS];
154         }
155     }
156
157   Page *page = new Page (paper);
158   fprintf (stderr, "book_height: %f\n", book_height);
159   fprintf (stderr, "vsize: %f\n", page->vsize_);
160   fprintf (stderr, "pages: %f\n", book_height / page->text_height ());
161
162 #if ONE_SCORE_PER_PAGE
163   for (int i = 0; i < score_count; i++)
164     {
165       page->lines_ = paper_scores_[i]->lines_;
166       pages->push (page);
167     }
168 #else
169   /* Simplistic page breaking.  */
170   Real text_height = page->text_height ();
171   for (int i = 0; i < score_count; i++)
172     {
173       Paper_score *pscore = paper_scores_[i];
174       Stencil *title = (i == 0 ? pscore->book_title_ : pscore->score_title_);
175       Real h = 0;
176       if (title)
177         h = title->extent (Y_AXIS).length ();
178
179       int line_count = SCM_VECTOR_LENGTH ((SCM) pscore->lines_);
180       for (int j = 0; j < line_count; j++)
181         {
182           SCM line = scm_vector_ref (pscore->lines_, scm_int2num (j));
183           h += ly_scm2offset (ly_car (line))[Y_AXIS];
184           if (page->height_ + h > text_height)
185             {
186               pages->push (page);
187               page = new Page (paper);
188             }
189           if (page->height_ + h <= text_height || page->height_ == 0)
190             {
191               if (j == 0 && title)
192                 page->lines_
193                   = ly_snoc (scm_cons (ly_offset2scm (Offset (0, 0)),
194                                        scm_list_1
195                                        (scm_cons
196                                         (ly_offset2scm (Offset (0, 0)),
197                                          title->smobbed_copy ()))),
198                              page->lines_);
199               page->lines_ = ly_snoc (line, page->lines_);
200               page->height_ += h;
201               h = 0;
202             }
203         }
204     }
205 #endif
206   
207   pages->push (page);
208   return pages;
209 }