]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-column-engraver.cc
* scm/page.scm (make-page): make it friendlier to call (esp. from C++)
[lilypond.git] / lily / paper-column-engraver.cc
1 /*
2   paper-column-engraver.cc -- implement Paper_column_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "paper-column-engraver.hh"
10 #include "system.hh"
11 #include "international.hh"
12 #include "axis-group-interface.hh"
13 #include "context.hh"
14 #include "item.hh"
15 #include "note-spacing.hh"
16 #include "paper-column.hh"
17 #include "pointer-group-interface.hh"
18 #include "staff-spacing.hh"
19 #include "system.hh"
20 #include "warn.hh"
21
22 #include "translator.icc"
23
24 Paper_column_engraver::Paper_column_engraver ()
25 {
26   last_moment_.main_part_ = Rational (-1,1); 
27   command_column_ = 0;
28   musical_column_ = 0;
29   breaks_ = 0;
30   system_ = 0;
31   last_special_barline_column_ = 0;
32   last_breakable_column_ = 0;
33   first_ = true;
34 }
35
36 void
37 Paper_column_engraver::finalize ()
38 {
39   if ((breaks_ % 8))
40     progress_indication ("[" + to_string (breaks_) + "]");
41
42   if (command_column_)
43     {
44       command_column_->set_property ("line-break-permission", ly_symbol2scm ("allow"));
45       system_->set_bound (RIGHT, command_column_);
46     }
47 }
48
49 void
50 Paper_column_engraver::make_columns ()
51 {
52   /*
53     ugh.
54   */
55   Paper_column *p1 = make_paper_column ("NonMusicalPaperColumn");
56   Paper_column *p2 = make_paper_column ("PaperColumn");
57   /* 
58      The columns are timestamped with now_mom () in
59      stop_translation_timestep. Cannot happen now, because the
60      first column is sometimes created before now_mom is initialised.
61   */
62
63   set_columns (p1, p2);
64 }
65
66 void
67 Paper_column_engraver::initialize ()
68 {
69   system_ = dynamic_cast<System *> (unsmob_grob (get_property ("rootSystem")));
70   make_columns ();
71
72   system_->set_bound (LEFT, command_column_);
73   command_column_->set_property ("line-break-permission", ly_symbol2scm ("allow"));
74 }
75
76 void
77 Paper_column_engraver::acknowledge_item (Grob_info gi)
78 {
79   items_.push_back (gi.item ());
80 }
81
82 void
83 Paper_column_engraver::acknowledge_staff_spacing (Grob_info gi)
84 {
85   Pointer_group_interface::add_grob (command_column_,
86                                      ly_symbol2scm ("spacing-wishes"),
87                                      gi.grob ());
88 }
89
90 void
91 Paper_column_engraver::acknowledge_note_spacing (Grob_info gi)
92 {
93   Pointer_group_interface::add_grob (musical_column_,
94                                      ly_symbol2scm ("spacing-wishes"),
95                                      gi.grob ());
96 }
97
98 void
99 Paper_column_engraver::set_columns (Paper_column *new_command,
100                                     Paper_column *new_musical)
101 {
102   command_column_ = new_command;
103   musical_column_ = new_musical;
104   if (new_command)
105     context ()->set_property ("currentCommandColumn", new_command->self_scm ());
106
107   if (new_musical)
108     context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
109
110   system_->add_column (command_column_);
111   system_->add_column (musical_column_);
112 }
113
114 IMPLEMENT_TRANSLATOR_LISTENER (Paper_column_engraver, break);
115 void
116 Paper_column_engraver::listen_break (Stream_event *ev)
117 {
118   break_events_.push_back (ev);
119 }
120
121 void
122 Paper_column_engraver::process_music ()
123 {
124   for (vsize i = 0; i < break_events_.size (); i++)
125     {
126       string prefix;
127       SCM name_sym = break_events_[i]->get_property ("class");
128       string name = ly_scm2string (scm_symbol_to_string (name_sym));
129       size_t end = name.rfind ("-event");
130       if (end)
131         prefix = name.substr (0, end);
132       else
133         {
134           programming_error ("Paper_column_engraver doesn't know about this break-event");
135           return;
136         }
137
138       string perm_str = prefix + "-permission";
139       string pen_str = prefix + "-penalty";
140
141       SCM cur_pen = command_column_->get_property (pen_str.c_str ());
142       SCM pen = break_events_[i]->get_property ("break-penalty");
143       SCM perm = break_events_[i]->get_property ("break-permission");
144
145       if (scm_is_number (pen))
146         {
147           Real new_pen = robust_scm2double (cur_pen, 0.0) + scm_to_double (pen);
148           command_column_->set_property (pen_str.c_str (), scm_from_double (new_pen));
149           command_column_->set_property (perm_str.c_str (), ly_symbol2scm ("allow"));
150         }
151       else
152         command_column_->set_property (perm_str.c_str (), perm);
153     }
154
155   bool start_of_measure = (last_moment_.main_part_ != now_mom ().main_part_
156                            && !measure_position (context ()).main_part_);
157
158   /*
159     We can't do this in start_translation_timestep(), since time sig
160     changes won't have happened by then.
161   */
162   if (start_of_measure)
163     {
164       Moment mlen = Moment (measure_length (context ()));
165       Grob *column = unsmob_grob (get_property ("currentCommandColumn"));
166       if (column)
167         column->set_property ("measure-length", mlen.smobbed_copy ());
168       else
169         programming_error ("No command column?");
170     }
171 }
172
173 /* return either
174    - the last column with a special (ie. not "|" or "") barline
175    - the last column
176    after the given moment
177 */
178 Paper_column*
179 Paper_column_engraver::find_turnable_column (Moment after_this)
180 {
181   if (last_special_barline_column_)
182     {
183       Moment m = *unsmob_moment (last_special_barline_column_->get_property ("when"));
184       if (m >= after_this)
185         return last_special_barline_column_;
186     }
187   if (last_breakable_column_)
188     {
189       Moment m = *unsmob_moment (last_breakable_column_->get_property ("when"));
190       if (m >= after_this)
191         return last_breakable_column_;
192     }
193   return 0;
194 }
195
196 void
197 Paper_column_engraver::revoke_page_turns (Moment after_this, Real new_penalty)
198 {
199   if (!page_turnable_columns_.size ())
200     return;
201
202   for (vsize i = page_turnable_columns_.size () - 1; i--;)
203     {
204       Paper_column *col = page_turnable_columns_[i];
205       Moment mom = *unsmob_moment (col->get_property ("when"));
206       if (mom >= after_this)
207         {
208           if (isinf (new_penalty))
209             {
210               col->del_property ( ly_symbol2scm ("page-turn-permission"));
211               page_turnable_columns_.erase (page_turnable_columns_.begin () + i);
212             }
213           else
214             {
215               Real prev_pen = robust_scm2double (col->get_property ("page-turn-penalty"), 0);
216               if (new_penalty > prev_pen)
217                 col->set_property ("page-turn-penalty", scm_from_double (new_penalty));
218             }
219         }
220     }
221 }
222
223 void
224 Paper_column_engraver::stop_translation_timestep ()
225 {
226   SCM m = now_mom ().smobbed_copy ();
227   command_column_->set_property ("when", m);
228   musical_column_->set_property ("when", m);
229
230   for (vsize i = 0; i < items_.size (); i++)
231     {
232       Item *elem = items_[i];
233       if (!elem->get_parent (X_AXIS)
234           || !unsmob_grob (elem->get_object ("axis-group-parent-X")))
235         {
236           bool br = Item::is_non_musical (elem);
237           Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
238         }
239     }
240   items_.clear ();
241
242   if (to_boolean (get_property ("forbidBreak")))
243     command_column_->set_property ("line-break-permission", SCM_EOL);
244   else if (Paper_column::is_breakable (command_column_))
245     {
246       breaks_++;
247       last_breakable_column_ = command_column_;
248
249       SCM which_bar = get_property ("whichBar");
250       if (scm_is_string (which_bar))
251         {
252           string bar = ly_scm2string (which_bar);
253           if (bar != "" && bar != "|")
254             last_special_barline_column_ = command_column_;
255         }
256
257       if (! (breaks_%8))
258         progress_indication ("[" + to_string (breaks_) + "]");
259     }
260
261   SCM page_br = get_property ("allowPageTurn");
262   if (scm_is_pair (page_br) && last_breakable_column_)
263     {
264       SCM pen = scm_cdr (page_br);
265       Moment *m = unsmob_moment (scm_car (page_br));
266       if (m)
267         {
268           Paper_column *turn = find_turnable_column (*m);
269           if (turn)
270             {
271               turn->set_property ("page-turn-permission", ly_symbol2scm ("allow"));
272               turn->set_property ("page-turn-penalty", pen);
273               page_turnable_columns_.push_back (turn);
274             }
275         }
276     }
277
278   /* The page-turn-engraver is allowed to change its mind and revoke previously-allowed
279      page turns (for example if there is a volta repeat where a turn is inconvenient) */
280   SCM revokes = get_property ("revokePageTurns");
281   if (scm_is_pair (revokes))
282     {
283       Moment *start = unsmob_moment (scm_car (revokes));
284       Real pen = robust_scm2double (scm_cdr (revokes), infinity_f);
285       if (start)
286         revoke_page_turns (*start, pen);
287     }
288
289   context ()->get_score_context ()->unset_property (ly_symbol2scm ("forbidBreak"));
290   context ()->get_score_context ()->unset_property (ly_symbol2scm ("allowPageTurn"));
291   context ()->get_score_context ()->unset_property (ly_symbol2scm ("revokePageTurns"));
292
293   first_ = false;
294   break_events_.clear ();
295 }
296
297 void
298 Paper_column_engraver::start_translation_timestep ()
299 {
300   /*
301     TODO: don't make columns when skipTypesetting is true.
302   */
303   if (!first_)
304     make_columns ();
305 }
306
307 ADD_ACKNOWLEDGER (Paper_column_engraver, item);
308 ADD_ACKNOWLEDGER (Paper_column_engraver, note_spacing);
309 ADD_ACKNOWLEDGER (Paper_column_engraver, staff_spacing);
310
311 ADD_TRANSLATOR (Paper_column_engraver,
312                 /* doc */ "Takes care of generating columns."
313                 "\n\n "
314                 "This engraver decides whether a column is breakable. The default is "
315                 "that a column is always breakable. However, every Bar_engraver "
316                 "that does not have a barline at a certain point will set forbidBreaks "
317                 "in the score context to stop linebreaks.  In practice, this "
318                 "means that you can make a breakpoint by creating a barline (assuming "
319                 "that there are no beams or notes that prevent a breakpoint.) ",
320                 
321                 /* create */
322                 "PaperColumn "
323                 "NonMusicalPaperColumn",
324                 
325                 /* accept */ "break-event",
326                 /* read */
327                 "forbidBreak "
328                 "allowPageTurn "
329                 "revokePageTurns "
330                 ,
331                 /* write */
332                 "forbidBreak "
333                 "allowPageTurn "
334                 "revokePageTurns "
335                 "currentCommandColumn "
336                 "currentMusicalColumn "
337                 );