]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-column-engraver.cc
Merge branch 'master' of git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond into jneeman2
[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 void
120 Paper_column_engraver::process_music ()
121 {
122   for (vsize i = 0; i < break_events_.size (); i++)
123     {
124       string prefix;
125       SCM name_sym = break_events_[i]->get_property ("class");
126       string name = ly_scm2string (scm_symbol_to_string (name_sym));
127       size_t end = name.rfind ("-event");
128       if (end)
129         prefix = name.substr (0, end);
130       else
131         {
132           programming_error ("Paper_column_engraver doesn't know about this break-event");
133           return;
134         }
135
136       string perm_str = prefix + "-permission";
137       string pen_str = prefix + "-penalty";
138
139       SCM cur_pen = command_column_->get_property (pen_str.c_str ());
140       SCM pen = break_events_[i]->get_property ("break-penalty");
141       SCM perm = break_events_[i]->get_property ("break-permission");
142
143       if (scm_is_number (pen))
144         {
145           Real new_pen = robust_scm2double (cur_pen, 0.0) + scm_to_double (pen);
146           command_column_->set_property (pen_str.c_str (), scm_from_double (new_pen));
147           command_column_->set_property (perm_str.c_str (), ly_symbol2scm ("allow"));
148         }
149       else
150         command_column_->set_property (perm_str.c_str (), perm);
151     }
152
153   bool start_of_measure = (last_moment_.main_part_ != now_mom ().main_part_
154                            && !measure_position (context ()).main_part_);
155
156   /*
157     We can't do this in start_translation_timestep (), since time sig
158     changes won't have happened by then.
159   */
160   if (start_of_measure)
161     {
162       Moment mlen = Moment (measure_length (context ()));
163       Grob *column = unsmob_grob (get_property ("currentCommandColumn"));
164       if (column)
165         column->set_property ("measure-length", mlen.smobbed_copy ());
166       else
167         programming_error ("No command column?");
168     }
169 }
170
171 void
172 Paper_column_engraver::stop_translation_timestep ()
173 {
174   SCM m = now_mom ().smobbed_copy ();
175   command_column_->set_property ("when", m);
176   musical_column_->set_property ("when", m);
177
178   for (vsize i = 0; i < items_.size (); i++)
179     {
180       Item *elem = items_[i];
181       if (!elem->get_parent (X_AXIS)
182           || !unsmob_grob (elem->get_object ("axis-group-parent-X")))
183         {
184           bool br = Item::is_non_musical (elem);
185           Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
186         }
187     }
188   items_.clear ();
189
190   if (to_boolean (get_property ("forbidBreak"))
191      && breaks_) /* don't honour forbidBreak if it occurs on the first moment of a score */
192     {
193       command_column_->set_property ("page-break-permission", SCM_EOL);
194       command_column_->set_property ("line-break-permission", SCM_EOL);
195       for (vsize i = 0; i < break_events_.size (); i++)
196         {
197           SCM perm = break_events_[i]->get_property ("break-permission");
198           if (perm == ly_symbol2scm ("force") || perm == ly_symbol2scm ("allow"))
199             warning (_f ("forced break was overridden by some other event, should you be using bar checks?"));
200         }
201     }
202   else if (Paper_column::is_breakable (command_column_))
203     {
204       breaks_++;
205
206       if (! (breaks_%8))
207         progress_indication ("[" + to_string (breaks_) + "]");
208     }
209
210   context ()->get_score_context ()->unset_property (ly_symbol2scm ("forbidBreak"));
211
212   first_ = false;
213   break_events_.clear ();
214
215
216   SCM mpos = get_property ("measurePosition");
217   SCM barnum = get_property ("internalBarNumber");
218   if (unsmob_moment (mpos)
219       && scm_is_integer (barnum))
220     {
221       SCM where = scm_cons (barnum,
222                             mpos);
223
224       command_column_->set_property ("rhythmic-location", where);
225       musical_column_->set_property ("rhythmic-location", where);
226     }
227 }
228
229 void
230 Paper_column_engraver::start_translation_timestep ()
231 {
232   /*
233     TODO: don't make columns when skipTypesetting is true.
234   */
235   if (!first_)
236     make_columns ();
237 }
238
239 ADD_ACKNOWLEDGER (Paper_column_engraver, item);
240 ADD_ACKNOWLEDGER (Paper_column_engraver, note_spacing);
241 ADD_ACKNOWLEDGER (Paper_column_engraver, staff_spacing);
242
243 ADD_TRANSLATOR (Paper_column_engraver,
244                 /* doc */ "Takes care of generating columns."
245                 "\n\n "
246                 "This engraver decides whether a column is breakable. The default is "
247                 "that a column is always breakable. However, every Bar_engraver "
248                 "that does not have a barline at a certain point will set forbidBreaks "
249                 "in the score context to stop linebreaks.  In practice, this "
250                 "means that you can make a breakpoint by creating a barline (assuming "
251                 "that there are no beams or notes that prevent a breakpoint.) ",
252                 
253                 /* create */
254                 "PaperColumn "
255                 "NonMusicalPaperColumn",
256                 /* read */
257                 "forbidBreak "
258                 ,
259                 /* write */
260                 "forbidBreak "
261                 "currentCommandColumn "
262                 "currentMusicalColumn "
263                 );