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