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