]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-column-engraver.cc
* lily/include/translator.icc: new file.
[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 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "paper-column-engraver.hh"
11 #include "system.hh"
12 #include "item.hh"
13 #include "paper-column.hh"
14 #include "staff-spacing.hh"
15 #include "note-spacing.hh"
16 #include "pointer-group-interface.hh"
17 #include "context.hh"
18 #include "axis-group-interface.hh"
19 #include "warn.hh"
20
21
22
23 Paper_column_engraver::Paper_column_engraver ()
24 {
25   command_column_ = 0;
26   musical_column_ = 0;
27   breaks_ = 0;
28   break_event_ = 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       command_column_->set_property ("breakable", SCM_BOOL_T);
42       system_->set_bound (RIGHT, command_column_);
43     }  
44 }
45
46 void
47 Paper_column_engraver::make_columns ()
48 {
49   /*
50     ugh.
51   */
52   Paper_column *p1 = make_paper_column ("NonMusicalPaperColumn");
53   Paper_column *p2 = make_paper_column ("PaperColumn");
54
55   SCM m = now_mom().smobbed_copy();
56   p1->set_property ("when", m);
57   p2->set_property ("when", m);
58       
59   set_columns (p1, p2);
60 }
61
62
63 void
64 Paper_column_engraver::initialize ()
65 {
66   system_ = dynamic_cast<System*> (unsmob_grob (get_property ("rootSystem")));
67   make_columns ();
68
69   
70   system_->set_bound (LEFT, command_column_);
71   command_column_->set_property ("breakable", SCM_BOOL_T);
72 }
73
74 void
75 Paper_column_engraver::acknowledge_grob (Grob_info gi)
76 {
77   Item *item = dynamic_cast<Item *> (gi.grob ());
78   if (!item)
79     {
80       programming_error ("Spanner found in Paper_column_engraver::acknowledge_grob()"); 
81       return;
82     }
83   
84   items_.push (item);
85   
86   if (Staff_spacing::has_interface (item))
87     {
88       Pointer_group_interface::add_grob (command_column_,
89                                          ly_symbol2scm ("spacing-wishes"),
90                                          gi.grob ());
91     }
92   if (Note_spacing::has_interface (item))
93     {
94       Pointer_group_interface::add_grob (musical_column_,
95                                          ly_symbol2scm ("spacing-wishes"),
96                                          gi.grob ());
97     }
98 }
99
100 void
101 Paper_column_engraver::set_columns (Paper_column *new_command,
102                                     Paper_column *new_musical)
103 {
104   command_column_ = new_command;
105   musical_column_ = new_musical;
106   if (new_command)
107     {
108       context ()->set_property ("currentCommandColumn", new_command->self_scm ());
109     }
110
111   if (new_musical)
112     {
113       context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
114     }
115
116   system_->add_column (command_column_);
117   system_->add_column (musical_column_);
118 }
119
120
121 void
122 Paper_column_engraver::forbid_breaks ()
123 {
124   if (command_column_ && !first_)
125     command_column_->set_property ("breakable", SCM_EOL);
126 }
127
128
129 bool
130 Paper_column_engraver::try_music (Music *m)
131 {
132   break_event_ = m;
133
134   return true;
135 }
136
137 void
138 Paper_column_engraver::process_music ()
139 {
140   if (break_event_)
141     {
142       SCM pen = command_column_->get_property ("penalty");
143       Real total_penalty = scm_is_number (pen) ? scm_to_double (pen) : 0.0;
144
145       SCM mpen = break_event_->get_property ("penalty");
146       if (scm_is_number (mpen))
147         total_penalty += scm_to_double (mpen);
148
149       command_column_->set_property ("penalty", scm_make_real (total_penalty));
150
151       /* ugh.  arbitrary, hardcoded */
152       if (total_penalty > 10000.0)
153         forbid_breaks ();
154
155       SCM page_pen = command_column_->get_property ("page-penalty");
156       Real total_pp = scm_is_number (page_pen) ? scm_to_double (page_pen) : 0.0;
157       SCM mpage_pen = break_event_->get_property ("page-penalty");
158       if (scm_is_number (mpage_pen))
159         total_pp += scm_to_double (mpage_pen);
160
161       command_column_->set_property ("page-penalty", scm_make_real (total_pp));
162     }
163
164
165   bool start_of_measure = (last_moment_.main_part_ != now_mom ().main_part_
166                            && !measure_position (context ()).main_part_);
167
168   /*
169     We can't do this in start_translation_timestep(), since time sig
170     changes won't have happened by then.
171   */
172   if (start_of_measure)
173     {
174       Moment mlen = Moment (measure_length (context ()));
175       Grob * column = unsmob_grob (get_property ("currentCommandColumn"));
176       if (column)
177         column->set_property ("measure-length", mlen.smobbed_copy ());
178       else
179         programming_error("No command column?");
180     }
181 }
182
183 void
184 Paper_column_engraver::stop_translation_timestep ()
185 {
186   for (int i = 0; i < items_.size (); i++)
187     {
188       Item *elem = items_[i];
189       if (!elem->get_parent (X_AXIS)
190           || !unsmob_grob (elem->get_object ("axis-group-parent-X")))
191         {
192           bool br = to_boolean (elem->get_property ("breakable"));
193           Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
194         }
195     }
196   items_.clear ();
197   
198   if (to_boolean (command_column_->get_property ("breakable")))
199     {
200       breaks_++;
201       if (! (breaks_%8))
202         progress_indication ("[" + to_string (breaks_) + "]");
203     }
204
205   first_ = false;
206 }
207
208 void
209 Paper_column_engraver::start_translation_timestep ()
210 {
211   /*
212     TODO: don't make columns when skipTypesetting is true.
213   */
214   if (!first_)
215     make_columns ();
216 }
217
218
219
220 #include "translator.icc"
221
222 ADD_TRANSLATOR (Paper_column_engraver,
223                 /* descr */ "Takes care of generating columns."
224                 "\n\n "
225                 "This engraver decides whether a column is breakable. The default is "
226                 "that a column is always breakable. However, when every Bar_engraver "
227                 "that does not have a barline at a certain point will call "
228                 "Score_engraver::forbid_breaks to stop linebreaks.  In practice, this "
229                 "means that you can make a breakpoint by creating a barline (assuming "
230                 "that there are no beams or notes that prevent a breakpoint.) ",
231                 /* creats*/ "PaperColumn NonMusicalPaperColumn",
232                 /* accepts */ "break-event",
233                 /* acks  */ "item-interface",
234                 /* reads */ "",
235                 /* write */ "currentCommandColumn currentMusicalColumn");