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