]> git.donarmstrong.com Git - lilypond.git/blob - lily/vertical-align-engraver.cc
Merge with git+ssh://jneem@git.sv.gnu.org/srv/git/lilypond.git
[lilypond.git] / lily / vertical-align-engraver.cc
1 /*
2   vertical-align-engraver.cc -- implement Vertical_align_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "context.hh"
10 #include "paper-column.hh"
11 #include "align-interface.hh"
12 #include "span-bar.hh"
13 #include "axis-group-interface.hh"
14 #include "engraver.hh"
15 #include "spanner.hh"
16 #include "pointer-group-interface.hh"
17 #include "grob-array.hh"
18
19 #include "translator.icc"
20
21 class Vertical_align_engraver : public Engraver
22 {
23   Spanner *valign_;
24   bool qualifies (Grob_info) const;
25   SCM id_to_group_hashtab_;
26
27 public:
28   TRANSLATOR_DECLARATIONS (Vertical_align_engraver);
29   DECLARE_ACKNOWLEDGER (axis_group);
30
31 protected:
32   virtual void derived_mark () const;
33   void process_music ();
34   virtual void finalize ();
35   virtual void initialize ();
36 };
37
38 ADD_ACKNOWLEDGER (Vertical_align_engraver, axis_group);
39 ADD_TRANSLATOR (Vertical_align_engraver,
40                 "Catch groups (staffs, lyrics lines, etc.) and stack "
41                 "them vertically.",
42                 /* create */ "VerticalAlignment",
43                 /* read */ "alignAboveContext alignBelowContext",
44                 /* write */ "");
45
46 Vertical_align_engraver::Vertical_align_engraver ()
47 {
48   valign_ = 0;
49   id_to_group_hashtab_ = SCM_EOL;
50 }
51
52 void
53 Vertical_align_engraver::derived_mark () const
54 {
55   scm_gc_mark (id_to_group_hashtab_);
56 }
57
58 void
59 Vertical_align_engraver::initialize ()
60 {
61   id_to_group_hashtab_ = scm_c_make_hash_table (11);
62 }
63
64 void
65 Vertical_align_engraver::process_music ()
66 {
67   if (!valign_)
68     {
69       valign_ = make_spanner ("VerticalAlignment", SCM_EOL);
70       valign_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));
71       Align_interface::set_ordered (valign_);
72     }
73 }
74
75 void
76 Vertical_align_engraver::finalize ()
77 {
78   if (valign_)
79     {
80       valign_->set_bound (RIGHT, unsmob_grob (get_property ("currentCommandColumn")));
81       valign_ = 0;
82     }
83 }
84
85 bool
86 Vertical_align_engraver::qualifies (Grob_info i) const
87 {
88   int sz = i.origin_contexts ((Translator *)this).size ();
89
90   return sz > 0 && Axis_group_interface::has_interface (i.grob ())
91     && !i.grob ()->get_parent (Y_AXIS)
92     && Axis_group_interface::has_axis (i.grob (), Y_AXIS);
93 }
94
95 void
96 Vertical_align_engraver::acknowledge_axis_group (Grob_info i)
97 {
98   if (qualifies (i))
99     {
100       string id = i.context ()->id_string ();
101
102       scm_hash_set_x (id_to_group_hashtab_, scm_makfrom0str (id.c_str ()),
103                       i.grob ()->self_scm ());
104
105       SCM before_id = i.context ()->get_property ("alignAboveContext");
106       SCM after_id = i.context ()->get_property ("alignBelowContext");
107
108       SCM before = scm_hash_ref (id_to_group_hashtab_, before_id, SCM_BOOL_F);
109       SCM after = scm_hash_ref (id_to_group_hashtab_, after_id, SCM_BOOL_F);
110
111       Grob *before_grob = unsmob_grob (before);
112       Grob *after_grob = unsmob_grob (after);
113
114       Align_interface::add_element (valign_, i.grob ());
115         
116       if (before_grob || after_grob)
117         {
118           Grob_array *ga = unsmob_grob_array (valign_->get_object ("elements"));
119           vector<Grob*> &arr = ga->array_reference ();
120
121           Grob *added = arr.back ();
122           arr.pop_back ();
123           for (vsize i = 0; i < arr.size (); i++)
124             {
125               if (arr[i] == before_grob)
126                 {
127                   arr.insert (arr.begin () + i, added);
128                   break;
129                 }
130               else if (arr[i] == after_grob)
131                 {
132                   arr.insert (arr.begin () + i + 1, added);
133                   break;
134                 }
135             }
136         }
137     }
138 }