]> git.donarmstrong.com Git - lilypond.git/blob - lily/separating-line-group-engraver.cc
* lily/include/grob-info.hh (class Grob_info): make data member
[lilypond.git] / lily / separating-line-group-engraver.cc
1 /*
2   '  separating-line-group-engraver.cc -- implement Separating_line_group_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "separating-group-spanner.hh"
10 #include "separation-item.hh"
11 #include "paper-column.hh"
12 #include "output-def.hh"
13 #include "engraver.hh"
14 #include "axis-group-interface.hh"
15 #include "note-spacing.hh"
16 #include "accidental-placement.hh"
17 #include "context.hh"
18
19 struct Spacings
20 {
21   Item *staff_spacing_;
22   Link_array<Item> note_spacings_;
23
24   Spacings ()
25   {
26     staff_spacing_ = 0;
27   }
28
29   bool is_empty () const
30   {
31     return !staff_spacing_ && !note_spacings_.size ();
32   }
33   void clear ()
34   {
35     staff_spacing_ = 0;
36     note_spacings_.clear ();
37   }
38 };
39
40 class Separating_line_group_engraver : public Engraver
41 {
42 protected:
43   Item *break_item_;
44   Item *musical_item_;
45   Item *last_musical_item_;
46
47   Spacings current_spacings_;
48   Spacings last_spacings_;
49
50   Spanner *sep_span_;
51
52   virtual void acknowledge_grob (Grob_info);
53   virtual void process_music ();
54   virtual void finalize ();
55   virtual void stop_translation_timestep ();
56   virtual void start_translation_timestep ();
57 public:
58   TRANSLATOR_DECLARATIONS (Separating_line_group_engraver);
59 };
60
61 Separating_line_group_engraver::Separating_line_group_engraver ()
62 {
63   sep_span_ = 0;
64   break_item_ = 0;
65   musical_item_ = 0;
66 }
67
68 void
69 Separating_line_group_engraver::process_music ()
70 {
71
72   if (!sep_span_)
73     {
74       sep_span_ = make_spanner ("SeparatingGroupSpanner", SCM_EOL);
75
76       sep_span_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));
77     }
78 }
79 void
80 Separating_line_group_engraver::finalize ()
81 {
82   if (!sep_span_)
83     return;
84
85   SCM ccol = get_property ("currentCommandColumn");
86   Grob *column = unsmob_grob (ccol);
87
88   sep_span_->set_bound (RIGHT, unsmob_grob (ccol));
89   sep_span_ = 0;
90
91   for (int i = 0; i < last_spacings_.note_spacings_.size (); i++)
92     {
93       Pointer_group_interface::add_grob (last_spacings_.note_spacings_[i],
94                                          ly_symbol2scm ("right-items"),
95                                          column);
96     }
97
98   if (last_spacings_.staff_spacing_
99       && last_spacings_.staff_spacing_->get_column () == column)
100     {
101       last_spacings_.staff_spacing_->suicide ();
102     }
103 }
104
105 void
106 Separating_line_group_engraver::acknowledge_grob (Grob_info i)
107 {
108   Item *it = dynamic_cast<Item *> (i.grob ());
109   if (!it)
110     return;
111   if (it->get_parent (X_AXIS)
112       && it->get_parent (X_AXIS)
113       ->has_extent_callback (Axis_group_interface::group_extent_callback_proc, X_AXIS))
114     return;
115
116   if (to_boolean (it->get_property ("no-spacing-rods")))
117     return;
118
119   if (Note_spacing::has_interface (it))
120     {
121       current_spacings_.note_spacings_.push (it);
122       return;
123     }
124
125   bool ib = Item::is_breakable (it);
126   Item *&p_ref_ (ib ? break_item_
127                  : musical_item_);
128
129   if (!p_ref_)
130     {
131       p_ref_ = make_item ("SeparationItem", SCM_EOL);
132
133       if (ib)
134         {
135           p_ref_->set_property ("breakable", SCM_BOOL_T);
136           context ()->set_property ("breakableSeparationItem", p_ref_->self_scm ());
137         }
138
139       if (to_boolean (get_property ("createSpacing"))
140           && p_ref_ == break_item_)
141         {
142           Item *it = make_item ("StaffSpacing", SCM_EOL);
143           current_spacings_.staff_spacing_ = it;
144           it->set_property ("left-items", scm_cons (break_item_->self_scm (), SCM_EOL));
145
146           if (int i = last_spacings_.note_spacings_.size ())
147             {
148               for (; i--;)
149                 Pointer_group_interface::add_grob (last_spacings_.note_spacings_[i],
150                                                    ly_symbol2scm ("right-items"),
151                                                    break_item_);
152             }
153           else if (last_spacings_.staff_spacing_)
154             {
155               last_spacings_.staff_spacing_->set_property ("right-items",
156                                                            scm_cons (break_item_->self_scm (), SCM_EOL));
157             }
158         }
159     }
160
161   if (Accidental_placement::has_interface (it))
162     Separation_item::add_conditional_item (p_ref_, it);
163   else
164     Separation_item::add_item (p_ref_, it);
165 }
166
167 void
168 Separating_line_group_engraver::start_translation_timestep ()
169 {
170   if (break_item_)
171     context ()->unset_property (ly_symbol2scm ("breakableSeparationItem"));
172   break_item_ = 0;
173 }
174
175 void
176 Separating_line_group_engraver::stop_translation_timestep ()
177 {
178   if (break_item_)
179     {
180       Separating_group_spanner::add_spacing_unit (sep_span_, break_item_);
181     }
182
183   if (Item *sp = current_spacings_.staff_spacing_)
184     {
185       /*
186         TODO: should really look at the left-items of following
187         note-spacing grobs.
188       */
189       if (musical_item_)
190         Pointer_group_interface::add_grob (sp, ly_symbol2scm ("right-items"),
191                                            musical_item_);
192     }
193
194   if (!current_spacings_.is_empty ())
195     {
196       last_spacings_ = current_spacings_;
197     }
198
199   current_spacings_.clear ();
200
201   if (musical_item_)
202     {
203       Separating_group_spanner::add_spacing_unit (sep_span_, musical_item_);
204     }
205
206   musical_item_ = 0;
207 }
208
209 ADD_TRANSLATOR (Separating_line_group_engraver,
210                 /* descr */ "Generates objects for computing spacing parameters.",
211                 /* creats*/ "SeparationItem SeparatingGroupSpanner StaffSpacing",
212                 /* accepts */ "",
213                 /* acks  */ "item-interface",
214                 /* reads */ "createSpacing",
215                 /* write */ "breakableSeparationItem");