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