]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-column-engraver.cc
Issue 4997/5: Use Preinit in Engraver_group
[lilypond.git] / lily / paper-column-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "paper-column-engraver.hh"
21 #include "international.hh"
22 #include "accidental-placement.hh"
23 #include "accidental-interface.hh"
24 #include "arpeggio.hh"
25 #include "axis-group-interface.hh"
26 #include "context.hh"
27 #include "note-spacing.hh"
28 #include "paper-column.hh"
29 #include "pointer-group-interface.hh"
30 #include "separation-item.hh"
31 #include "staff-spacing.hh"
32 #include "system.hh"
33 #include "warn.hh"
34
35 #include "translator.icc"
36
37 Paper_column_engraver::Paper_column_engraver ()
38 {
39   last_moment_.main_part_ = Rational (-1, 1);
40   command_column_ = 0;
41   musical_column_ = 0;
42   breaks_ = 0;
43   system_ = 0;
44   first_ = true;
45   made_columns_ = false;
46 }
47
48 void
49 Paper_column_engraver::finalize ()
50 {
51   if (! (breaks_ % 8))
52     progress_indication ("[" + ::to_string (breaks_) + "]");
53
54   if (!made_columns_)
55     {
56       make_columns ();
57       SCM m = now_mom ().smobbed_copy ();
58       command_column_->set_property ("when", m);
59       musical_column_->set_property ("when", m);
60     }
61
62   if (command_column_)
63     {
64       // At the end of the score, allow page breaks and turns by default, but...
65       command_column_->set_property ("page-break-permission", ly_symbol2scm ("allow"));
66       command_column_->set_property ("page-turn-permission", ly_symbol2scm ("allow"));
67
68       // ...allow the user to override them.
69       handle_manual_breaks (true);
70
71       // On the other hand, line breaks are always allowed at the end of a score,
72       // even if they try to stop us.
73       if (!scm_is_symbol (command_column_->get_property ("line-break-permission")))
74         command_column_->set_property ("line-break-permission", ly_symbol2scm ("allow"));
75
76       system_->set_bound (RIGHT, command_column_);
77     }
78 }
79
80 void
81 Paper_column_engraver::make_columns ()
82 {
83   /*
84     ugh.
85   */
86   Paper_column *p1 = make_paper_column ("NonMusicalPaperColumn");
87   Paper_column *p2 = make_paper_column ("PaperColumn");
88   /*
89      The columns are timestamped with now_mom () in
90      stop_translation_timestep. Cannot happen now, because the
91      first column is sometimes created before now_mom is initialised.
92   */
93
94   set_columns (p1, p2);
95 }
96
97 void
98 Paper_column_engraver::initialize ()
99 {
100   system_ = unsmob<System> (get_property ("rootSystem"));
101   make_columns ();
102
103   system_->set_bound (LEFT, command_column_);
104   command_column_->set_property ("line-break-permission", ly_symbol2scm ("allow"));
105 }
106
107 void
108 Paper_column_engraver::acknowledge_item (Grob_info gi)
109 {
110   items_.push_back (gi.item ());
111 }
112
113 void
114 Paper_column_engraver::acknowledge_staff_spacing (Grob_info gi)
115 {
116   Pointer_group_interface::add_grob (command_column_,
117                                      ly_symbol2scm ("spacing-wishes"),
118                                      gi.grob ());
119 }
120
121 void
122 Paper_column_engraver::acknowledge_note_spacing (Grob_info gi)
123 {
124   Pointer_group_interface::add_grob (musical_column_,
125                                      ly_symbol2scm ("spacing-wishes"),
126                                      gi.grob ());
127 }
128
129 void
130 Paper_column_engraver::set_columns (Paper_column *new_command,
131                                     Paper_column *new_musical)
132 {
133   command_column_ = new_command;
134   musical_column_ = new_musical;
135   if (new_command)
136     context ()->set_property ("currentCommandColumn", new_command->self_scm ());
137
138   if (new_musical)
139     context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
140
141   system_->add_column (command_column_);
142   system_->add_column (musical_column_);
143 }
144
145 void
146 Paper_column_engraver::listen_break (Stream_event *ev)
147 {
148   break_events_.push_back (ev);
149 }
150
151 void
152 Paper_column_engraver::listen_label (Stream_event *ev)
153 {
154   label_events_.push_back (ev);
155 }
156
157 void
158 Paper_column_engraver::handle_manual_breaks (bool only_do_permissions)
159 {
160   for (vsize i = 0; i < break_events_.size (); i++)
161     {
162       string prefix;
163       SCM name_sym = scm_car (break_events_[i]->get_property ("class"));
164       string name = ly_symbol2string (name_sym);
165       size_t end = name.rfind ("-event");
166       if (end)
167         prefix = name.substr (0, end);
168       else
169         {
170           programming_error ("Paper_column_engraver doesn't know about this break-event");
171           return;
172         }
173
174       string perm_str = prefix + "-permission";
175       string pen_str = prefix + "-penalty";
176
177       SCM cur_pen = command_column_->get_property (pen_str.c_str ());
178       SCM pen = break_events_[i]->get_property ("break-penalty");
179       SCM perm = break_events_[i]->get_property ("break-permission");
180
181       if (!only_do_permissions && scm_is_number (pen))
182         {
183           Real new_pen = robust_scm2double (cur_pen, 0.0) + scm_to_double (pen);
184           command_column_->set_property (pen_str.c_str (), scm_from_double (new_pen));
185           command_column_->set_property (perm_str.c_str (), ly_symbol2scm ("allow"));
186         }
187       else
188         command_column_->set_property (perm_str.c_str (), perm);
189     }
190 }
191
192 void
193 Paper_column_engraver::process_music ()
194 {
195   handle_manual_breaks (false);
196
197   for (vsize i = 0; i < label_events_.size (); i++)
198     {
199       SCM label = label_events_[i]->get_property ("page-label");
200       SCM labels = command_column_->get_property ("labels");
201       command_column_->set_property ("labels", scm_cons (label, labels));
202     }
203
204   bool start_of_measure = (last_moment_.main_part_ != now_mom ().main_part_
205                            && !measure_position (context ()).main_part_);
206
207   /*
208     We can't do this in start_translation_timestep (), since time sig
209     changes won't have happened by then.
210   */
211   if (start_of_measure)
212     {
213       Moment mlen = Moment (measure_length (context ()));
214       Grob *column = unsmob<Grob> (get_property ("currentCommandColumn"));
215       if (column)
216         column->set_property ("measure-length", mlen.smobbed_copy ());
217       else
218         programming_error ("No command column?");
219     }
220 }
221
222 void
223 Paper_column_engraver::stop_translation_timestep ()
224 {
225   if (to_boolean (get_property ("skipTypesetting")))
226     return;
227
228   SCM m = now_mom ().smobbed_copy ();
229   command_column_->set_property ("when", m);
230   musical_column_->set_property ("when", m);
231
232   SCM mpos = get_property ("measurePosition");
233   SCM barnum = get_property ("internalBarNumber");
234   if (unsmob<Moment> (mpos)
235       && scm_is_integer (barnum))
236     {
237       SCM where = scm_cons (barnum,
238                             mpos);
239
240       command_column_->set_property ("rhythmic-location", where);
241       musical_column_->set_property ("rhythmic-location", where);
242     }
243
244   for (vsize i = 0; i < items_.size (); i++)
245     {
246       Item *elem = items_[i];
247       Grob *col = Item::is_non_musical (elem) ? command_column_ : musical_column_;
248
249       if (!elem->get_parent (X_AXIS))
250         elem->set_parent (col, X_AXIS);
251       if (!unsmob<Grob> (elem->get_object ("axis-group-parent-X")))
252         elem->set_object ("axis-group-parent-X", col->self_scm ());
253
254       if (has_interface<Accidental_placement> (elem)
255           || has_interface<Arpeggio> (elem))
256         Separation_item::add_conditional_item (col, elem);
257       else if (!has_interface<Accidental_interface> (elem))
258         Separation_item::add_item (col, elem);
259     }
260   items_.clear ();
261
262   if (to_boolean (get_property ("forbidBreak"))
263       && breaks_) /* don't honour forbidBreak if it occurs on the first moment of a score */
264     {
265       command_column_->set_property ("page-turn-permission", SCM_EOL);
266       command_column_->set_property ("page-break-permission", SCM_EOL);
267       command_column_->set_property ("line-break-permission", SCM_EOL);
268       for (vsize i = 0; i < break_events_.size (); i++)
269         {
270           SCM perm = break_events_[i]->get_property ("break-permission");
271           if (scm_is_eq (perm, ly_symbol2scm ("force"))
272               || scm_is_eq (perm, ly_symbol2scm ("allow")))
273             warning (_ ("forced break was overridden by some other event, "
274                         "should you be using bar checks?"));
275         }
276     }
277   else if (Paper_column::is_breakable (command_column_))
278     {
279       breaks_++;
280
281       if (! (breaks_ % 8))
282         progress_indication ("[" + ::to_string (breaks_) + "]");
283     }
284
285   context ()->get_score_context ()->unset_property (ly_symbol2scm ("forbidBreak"));
286
287   first_ = false;
288   label_events_.clear ();
289 }
290
291 void
292 Paper_column_engraver::start_translation_timestep ()
293 {
294   break_events_.clear ();
295   if (!first_ && !to_boolean (get_property ("skipTypesetting")))
296     {
297       make_columns ();
298       made_columns_ = true;
299     }
300 }
301
302
303 void
304 Paper_column_engraver::boot ()
305 {
306   ADD_LISTENER (Paper_column_engraver, break);
307   ADD_LISTENER (Paper_column_engraver, label);
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
313 ADD_TRANSLATOR (Paper_column_engraver,
314                 /* doc */
315                 "Take care of generating columns.\n"
316                 "\n"
317                 "This engraver decides whether a column is breakable.  The"
318                 " default is that a column is always breakable.  However,"
319                 " every @code{Bar_engraver} that does not have a barline at a"
320                 " certain point will set @code{forbidBreaks} in the score"
321                 " context to stop line breaks.  In practice, this means that"
322                 " you can make a break point by creating a bar line (assuming"
323                 " that there are no beams or notes that prevent a break"
324                 " point).",
325
326                 /* create */
327                 "PaperColumn "
328                 "NonMusicalPaperColumn ",
329
330                 /* read */
331                 "forbidBreak ",
332
333                 /* write */
334                 "forbidBreak "
335                 "currentCommandColumn "
336                 "currentMusicalColumn "
337                );