]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-column-engraver.cc
* lily/include/translator.icc (ADD_ACKNOWLEDGER): new macro.
[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 #include "translator.icc"
22
23
24 Paper_column_engraver::Paper_column_engraver ()
25 {
26   command_column_ = 0;
27   musical_column_ = 0;
28   breaks_ = 0;
29   break_event_ = 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 ("breakable", SCM_BOOL_T);
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   SCM m = now_mom().smobbed_copy();
57   p1->set_property ("when", m);
58   p2->set_property ("when", m);
59       
60   set_columns (p1, p2);
61 }
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   
71   system_->set_bound (LEFT, command_column_);
72   command_column_->set_property ("breakable", SCM_BOOL_T);
73 }
74
75 void
76 Paper_column_engraver::acknowledge_item (Grob_info gi)
77 {
78   items_.push (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     {
104       context ()->set_property ("currentCommandColumn", new_command->self_scm ());
105     }
106
107   if (new_musical)
108     {
109       context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
110     }
111
112   system_->add_column (command_column_);
113   system_->add_column (musical_column_);
114 }
115
116
117 void
118 Paper_column_engraver::forbid_breaks ()
119 {
120   if (command_column_ && !first_)
121     command_column_->set_property ("breakable", SCM_EOL);
122 }
123
124
125 bool
126 Paper_column_engraver::try_music (Music *m)
127 {
128   break_event_ = m;
129
130   return true;
131 }
132
133 void
134 Paper_column_engraver::process_music ()
135 {
136   if (break_event_)
137     {
138       SCM pen = command_column_->get_property ("penalty");
139       Real total_penalty = scm_is_number (pen) ? scm_to_double (pen) : 0.0;
140
141       SCM mpen = break_event_->get_property ("penalty");
142       if (scm_is_number (mpen))
143         total_penalty += scm_to_double (mpen);
144
145       command_column_->set_property ("penalty", scm_make_real (total_penalty));
146
147       /* ugh.  arbitrary, hardcoded */
148       if (total_penalty > 10000.0)
149         forbid_breaks ();
150
151       SCM page_pen = command_column_->get_property ("page-penalty");
152       Real total_pp = scm_is_number (page_pen) ? scm_to_double (page_pen) : 0.0;
153       SCM mpage_pen = break_event_->get_property ("page-penalty");
154       if (scm_is_number (mpage_pen))
155         total_pp += scm_to_double (mpage_pen);
156
157       command_column_->set_property ("page-penalty", scm_make_real (total_pp));
158     }
159
160
161   bool start_of_measure = (last_moment_.main_part_ != now_mom ().main_part_
162                            && !measure_position (context ()).main_part_);
163
164   /*
165     We can't do this in start_translation_timestep(), since time sig
166     changes won't have happened by then.
167   */
168   if (start_of_measure)
169     {
170       Moment mlen = Moment (measure_length (context ()));
171       Grob * column = unsmob_grob (get_property ("currentCommandColumn"));
172       if (column)
173         column->set_property ("measure-length", mlen.smobbed_copy ());
174       else
175         programming_error("No command column?");
176     }
177 }
178
179 void
180 Paper_column_engraver::stop_translation_timestep ()
181 {
182   for (int i = 0; i < items_.size (); i++)
183     {
184       Item *elem = items_[i];
185       if (!elem->get_parent (X_AXIS)
186           || !unsmob_grob (elem->get_object ("axis-group-parent-X")))
187         {
188           bool br = to_boolean (elem->get_property ("breakable"));
189           Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
190         }
191     }
192   items_.clear ();
193   
194   if (to_boolean (command_column_->get_property ("breakable")))
195     {
196       breaks_++;
197       if (! (breaks_%8))
198         progress_indication ("[" + to_string (breaks_) + "]");
199     }
200
201   first_ = false;
202   break_event_ = 0;
203 }
204
205 void
206 Paper_column_engraver::start_translation_timestep ()
207 {
208   /*
209     TODO: don't make columns when skipTypesetting is true.
210   */
211   if (!first_)
212     make_columns ();
213 }
214
215 ADD_ACKNOWLEDGER(Paper_column_engraver,item);
216 ADD_ACKNOWLEDGER(Paper_column_engraver,note_spacing);
217 ADD_ACKNOWLEDGER(Paper_column_engraver,staff_spacing);
218
219
220 ADD_TRANSLATOR (Paper_column_engraver,
221                 /* descr */ "Takes care of generating columns."
222                 "\n\n "
223                 "This engraver decides whether a column is breakable. The default is "
224                 "that a column is always breakable. However, when every Bar_engraver "
225                 "that does not have a barline at a certain point will call "
226                 "Score_engraver::forbid_breaks to stop linebreaks.  In practice, this "
227                 "means that you can make a breakpoint by creating a barline (assuming "
228                 "that there are no beams or notes that prevent a breakpoint.) ",
229                 /* creats*/ "PaperColumn NonMusicalPaperColumn",
230                 /* accepts */ "break-event",
231                 /* acks  */ "",
232                 /* reads */ "",
233                 /* write */ "currentCommandColumn currentMusicalColumn");