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