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