]> git.donarmstrong.com Git - lilypond.git/blob - lily/paper-column-engraver.cc
* lily/spacing-basic.cc (standard_breakable_column_spacing): also
[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   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 void
63 Paper_column_engraver::initialize ()
64 {
65   system_ = dynamic_cast<System *> (unsmob_grob (get_property ("rootSystem")));
66   make_columns ();
67
68   system_->set_bound (LEFT, command_column_);
69   command_column_->set_property ("breakable", SCM_BOOL_T);
70 }
71
72 void
73 Paper_column_engraver::acknowledge_item (Grob_info gi)
74 {
75   items_.push_back (gi.item ());
76 }
77
78 void
79 Paper_column_engraver::acknowledge_staff_spacing (Grob_info gi)
80 {
81   Pointer_group_interface::add_grob (command_column_,
82                                      ly_symbol2scm ("spacing-wishes"),
83                                      gi.grob ());
84 }
85 void
86 Paper_column_engraver::acknowledge_note_spacing (Grob_info gi)
87 {
88   Pointer_group_interface::add_grob (musical_column_,
89                                      ly_symbol2scm ("spacing-wishes"),
90                                      gi.grob ());
91 }
92
93 void
94 Paper_column_engraver::set_columns (Paper_column *new_command,
95                                     Paper_column *new_musical)
96 {
97   command_column_ = new_command;
98   musical_column_ = new_musical;
99   if (new_command)
100     context ()->set_property ("currentCommandColumn", new_command->self_scm ());
101
102   if (new_musical)
103     context ()->set_property ("currentMusicalColumn", new_musical->self_scm ());
104
105   system_->add_column (command_column_);
106   system_->add_column (musical_column_);
107 }
108
109 void
110 Paper_column_engraver::forbid_breaks ()
111 {
112   if (command_column_ && !first_)
113     command_column_->set_property ("breakable", SCM_EOL);
114 }
115
116 bool
117 Paper_column_engraver::try_music (Music *m)
118 {
119   break_event_ = m;
120
121   return true;
122 }
123
124 void
125 Paper_column_engraver::process_music ()
126 {
127   if (break_event_)
128     {
129       SCM pen = command_column_->get_property ("penalty");
130       Real total_penalty = scm_is_number (pen) ? scm_to_double (pen) : 0.0;
131
132       SCM mpen = break_event_->get_property ("penalty");
133       if (scm_is_number (mpen))
134         total_penalty += scm_to_double (mpen);
135
136       command_column_->set_property ("penalty", scm_from_double (total_penalty));
137
138       /* ugh.  arbitrary, hardcoded */
139       if (total_penalty > 10000.0)
140         forbid_breaks ();
141
142       SCM page_pen = command_column_->get_property ("page-penalty");
143       Real total_pp = scm_is_number (page_pen) ? scm_to_double (page_pen) : 0.0;
144       SCM mpage_pen = break_event_->get_property ("page-penalty");
145       if (scm_is_number (mpage_pen))
146         total_pp += scm_to_double (mpage_pen);
147
148       command_column_->set_property ("page-penalty", scm_from_double (total_pp));
149     }
150
151   bool start_of_measure = (last_moment_.main_part_ != now_mom ().main_part_
152                            && !measure_position (context ()).main_part_);
153
154   /*
155     We can't do this in start_translation_timestep(), since time sig
156     changes won't have happened by then.
157   */
158   if (start_of_measure)
159     {
160       Moment mlen = Moment (measure_length (context ()));
161       Grob *column = unsmob_grob (get_property ("currentCommandColumn"));
162       if (column)
163         column->set_property ("measure-length", mlen.smobbed_copy ());
164       else
165         programming_error ("No command column?");
166     }
167 }
168
169 void
170 Paper_column_engraver::stop_translation_timestep ()
171 {
172   for (vsize i = 0; i < items_.size (); i++)
173     {
174       Item *elem = items_[i];
175       if (!elem->get_parent (X_AXIS)
176           || !unsmob_grob (elem->get_object ("axis-group-parent-X")))
177         {
178           bool br = to_boolean (elem->get_property ("breakable"));
179           Axis_group_interface::add_element (br ? command_column_ : musical_column_, elem);
180         }
181     }
182   items_.clear ();
183
184   if (to_boolean (command_column_->get_property ("breakable")))
185     {
186       breaks_++;
187       if (! (breaks_%8))
188         progress_indication ("[" + to_string (breaks_) + "]");
189     }
190
191   first_ = false;
192   break_event_ = 0;
193 }
194
195 void
196 Paper_column_engraver::start_translation_timestep ()
197 {
198   /*
199     TODO: don't make columns when skipTypesetting is true.
200   */
201   if (!first_)
202     make_columns ();
203 }
204
205 ADD_ACKNOWLEDGER (Paper_column_engraver, item);
206 ADD_ACKNOWLEDGER (Paper_column_engraver, note_spacing);
207 ADD_ACKNOWLEDGER (Paper_column_engraver, staff_spacing);
208
209 ADD_TRANSLATOR (Paper_column_engraver,
210                 /* doc */ "Takes care of generating columns."
211                 "\n\n "
212                 "This engraver decides whether a column is breakable. The default is "
213                 "that a column is always breakable. However, when every Bar_engraver "
214                 "that does not have a barline at a certain point will call "
215                 "Score_engraver::forbid_breaks to stop linebreaks.  In practice, this "
216                 "means that you can make a breakpoint by creating a barline (assuming "
217                 "that there are no beams or notes that prevent a breakpoint.) ",
218                 
219                 /* create */
220                 "PaperColumn "
221                 "NonMusicalPaperColumn",
222                 
223                 /* accept */ "break-event",
224                 /* read */ "",
225                 /* write */
226                 "currentCommandColumn "
227                 "currentMusicalColumn");