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