]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-column-engraver.cc
* lily/ various: Introduce stream events of types Prepare,
[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 "item.hh"
12 #include "paper-column.hh"
13 #include "staff-spacing.hh"
14 #include "note-spacing.hh"
15 #include "pointer-group-interface.hh"
16 #include "context.hh"
17 #include "score-context.hh"
18 #include "axis-group-interface.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   last_breakable_column_ = 0;
32   last_breakable_moment_ = Moment (-1);
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       command_column_->set_property ("line-break-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 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 bool
113 Paper_column_engraver::try_music (Music *m)
114 {
115   break_events_.push_back (m);
116
117   return true;
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 = break_events_[i]->get_property ("name");
127       if (name == ly_symbol2scm ("LineBreakEvent"))
128         prefix = "line-break";
129       else if (name == ly_symbol2scm ("PageBreakEvent"))
130         prefix = "page-break";
131       else if (name == ly_symbol2scm ("PageTurnEvent"))
132         prefix = "page-turn";
133       else
134         {
135           programming_error ("Paper_column_engraver doesn't know about this break-event");
136           return;
137         }
138       string perm_str = prefix + "-permission";
139       string pen_str = prefix + "-penalty";
140
141       SCM cur_pen = command_column_->get_property (pen_str.c_str ());
142       SCM pen = break_events_[i]->get_property ("break-penalty");
143       SCM perm = break_events_[i]->get_property ("break-permission");
144
145       if (scm_is_number (pen))
146         {
147           Real new_pen = robust_scm2double (cur_pen, 0.0) + scm_to_double (pen);
148           command_column_->set_property (pen_str.c_str (), scm_from_double (new_pen));
149           command_column_->set_property (perm_str.c_str (), ly_symbol2scm ("allow"));
150         }
151       else
152         command_column_->set_property (perm_str.c_str (), perm);
153     }
154
155   bool start_of_measure = (last_moment_.main_part_ != now_mom ().main_part_
156                            && !measure_position (context ()).main_part_);
157
158   /*
159     We can't do this in start_translation_timestep(), since time sig
160     changes won't have happened by then.
161   */
162   if (start_of_measure)
163     {
164       Moment mlen = Moment (measure_length (context ()));
165       Grob *column = unsmob_grob (get_property ("currentCommandColumn"));
166       if (column)
167         column->set_property ("measure-length", mlen.smobbed_copy ());
168       else
169         programming_error ("No command column?");
170     }
171 }
172
173 void
174 Paper_column_engraver::stop_translation_timestep ()
175 {
176   SCM m = now_mom ().smobbed_copy ();
177   command_column_->set_property ("when", m);
178   musical_column_->set_property ("when", m);
179
180   for (vsize i = 0; i < items_.size (); i++)
181     {
182       Item *elem = items_[i];
183       if (!elem->get_parent (X_AXIS)
184           || !unsmob_grob (elem->get_object ("axis-group-parent-X")))
185         {
186           bool br = Item::is_non_musical (elem);
187           Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
188         }
189     }
190   items_.clear ();
191
192   if (to_boolean (get_property ("forbidBreak")))
193     command_column_->set_property ("line-break-permission", SCM_EOL);
194   else if (Paper_column::is_breakable (command_column_))
195     {
196       breaks_++;
197       last_breakable_column_ = command_column_;
198       last_breakable_moment_ = now_mom ();
199       if (! (breaks_%8))
200         progress_indication ("[" + to_string (breaks_) + "]");
201     }
202
203   SCM page_br = get_property ("allowPageTurn");
204   if (scm_is_pair (page_br) && last_breakable_moment_ >= Rational (0))
205     {
206       SCM pen = scm_cdr (page_br);
207       Moment *m = unsmob_moment (scm_car (page_br));
208       if (m && scm_is_number (pen) && *m <= last_breakable_moment_)
209         {
210           last_breakable_column_->set_property ("page-turn-permission", ly_symbol2scm ("allow"));
211           last_breakable_column_->set_property ("page-turn-penalty", pen);
212         }
213     }
214
215   context ()->get_score_context ()->unset_property (ly_symbol2scm ("forbidBreak"));
216   context ()->get_score_context ()->unset_property (ly_symbol2scm ("allowPageTurn"));
217
218   first_ = false;
219   break_events_.clear ();
220 }
221
222 void
223 Paper_column_engraver::start_translation_timestep ()
224 {
225   /*
226     TODO: don't make columns when skipTypesetting is true.
227   */
228   if (!first_)
229     make_columns ();
230 }
231
232 ADD_ACKNOWLEDGER (Paper_column_engraver, item);
233 ADD_ACKNOWLEDGER (Paper_column_engraver, note_spacing);
234 ADD_ACKNOWLEDGER (Paper_column_engraver, staff_spacing);
235
236 ADD_TRANSLATOR (Paper_column_engraver,
237                 /* doc */ "Takes care of generating columns."
238                 "\n\n "
239                 "This engraver decides whether a column is breakable. The default is "
240                 "that a column is always breakable. However, every Bar_engraver "
241                 "that does not have a barline at a certain point will set forbidBreaks "
242                 "in the score context to stop linebreaks.  In practice, this "
243                 "means that you can make a breakpoint by creating a barline (assuming "
244                 "that there are no beams or notes that prevent a breakpoint.) ",
245                 
246                 /* create */
247                 "PaperColumn "
248                 "NonMusicalPaperColumn",
249                 
250                 /* accept */ "break-event",
251                 /* read */
252                 "forbidBreak "
253                 "allowPageTurn",
254                 /* write */
255                 "forbidBreak "
256                 "allowPageTurn "
257                 "currentCommandColumn "
258                 "currentMusicalColumn");