]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-column-engraver.cc
* lily/global-context-scheme.cc: Separated ly:run-translator into
[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 "axis-group-interface.hh"
18 #include "warn.hh"
19
20 #include "translator.icc"
21
22 Paper_column_engraver::Paper_column_engraver ()
23 {
24   last_moment_.main_part_ = Rational (-1,1); 
25   command_column_ = 0;
26   musical_column_ = 0;
27   breaks_ = 0;
28   system_ = 0;
29   first_ = true;
30   last_breakable_column_ = 0;
31   last_breakable_moment_ = Moment (-1);
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       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 void
88 Paper_column_engraver::acknowledge_note_spacing (Grob_info gi)
89 {
90   Pointer_group_interface::add_grob (musical_column_,
91                                      ly_symbol2scm ("spacing-wishes"),
92                                      gi.grob ());
93 }
94
95 void
96 Paper_column_engraver::set_columns (Paper_column *new_command,
97                                     Paper_column *new_musical)
98 {
99   command_column_ = new_command;
100   musical_column_ = new_musical;
101   if (new_command)
102     context ()->set_property ("currentCommandColumn", new_command->self_scm ());
103
104   if (new_musical)
105     context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
106
107   system_->add_column (command_column_);
108   system_->add_column (musical_column_);
109 }
110
111 bool
112 Paper_column_engraver::try_music (Music *m)
113 {
114   break_events_.push_back (m);
115
116   return true;
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 = break_events_[i]->get_property ("name");
126       if (name == ly_symbol2scm ("LineBreakEvent"))
127         prefix = "line-break";
128       else if (name == ly_symbol2scm ("PageBreakEvent"))
129         prefix = "page-break";
130       else if (name == ly_symbol2scm ("PageTurnEvent"))
131         prefix = "page-turn";
132       else
133         {
134           programming_error ("Paper_column_engraver doesn't know about this break-event");
135           return;
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       last_breakable_column_ = command_column_;
197       last_breakable_moment_ = now_mom ();
198       if (! (breaks_%8))
199         progress_indication ("[" + to_string (breaks_) + "]");
200     }
201
202   SCM page_br = get_property ("allowPageTurn");
203   if (scm_is_pair (page_br) && last_breakable_moment_ >= Rational (0))
204     {
205       SCM pen = scm_cdr (page_br);
206       Moment *m = unsmob_moment (scm_car (page_br));
207       if (m && scm_is_number (pen) && *m <= last_breakable_moment_)
208         {
209           last_breakable_column_->set_property ("page-turn-permission", ly_symbol2scm ("allow"));
210           last_breakable_column_->set_property ("page-turn-penalty", pen);
211         }
212     }
213
214   context ()->get_score_context ()->unset_property (ly_symbol2scm ("forbidBreak"));
215   context ()->get_score_context ()->unset_property (ly_symbol2scm ("allowPageTurn"));
216
217   first_ = false;
218   break_events_.clear ();
219 }
220
221 void
222 Paper_column_engraver::start_translation_timestep ()
223 {
224   /*
225     TODO: don't make columns when skipTypesetting is true.
226   */
227   if (!first_)
228     make_columns ();
229 }
230
231 ADD_ACKNOWLEDGER (Paper_column_engraver, item);
232 ADD_ACKNOWLEDGER (Paper_column_engraver, note_spacing);
233 ADD_ACKNOWLEDGER (Paper_column_engraver, staff_spacing);
234
235 ADD_TRANSLATOR (Paper_column_engraver,
236                 /* doc */ "Takes care of generating columns."
237                 "\n\n "
238                 "This engraver decides whether a column is breakable. The default is "
239                 "that a column is always breakable. However, every Bar_engraver "
240                 "that does not have a barline at a certain point will set forbidBreaks "
241                 "in the score context to stop linebreaks.  In practice, this "
242                 "means that you can make a breakpoint by creating a barline (assuming "
243                 "that there are no beams or notes that prevent a breakpoint.) ",
244                 
245                 /* create */
246                 "PaperColumn "
247                 "NonMusicalPaperColumn",
248                 
249                 /* accept */ "break-event",
250                 /* read */
251                 "forbidBreak "
252                 "allowPageTurn",
253                 /* write */
254                 "forbidBreak "
255                 "allowPageTurn "
256                 "currentCommandColumn "
257                 "currentMusicalColumn");