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