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