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