]> git.donarmstrong.com Git - lilypond.git/blob - lily/vertical-align-engraver.cc
650669f8c54768e41791ac488f18ae529960227c
[lilypond.git] / lily / vertical-align-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "context.hh"
21 #include "paper-column.hh"
22 #include "align-interface.hh"
23 #include "axis-group-interface.hh"
24 #include "engraver.hh"
25 #include "international.hh"
26 #include "spanner.hh"
27 #include "pointer-group-interface.hh"
28 #include "grob-array.hh"
29
30 #include "translator.icc"
31
32 class Vertical_align_engraver : public Engraver
33 {
34   Spanner *valign_;
35   bool qualifies (Grob_info) const;
36   SCM id_to_group_hashtab_;
37
38 public:
39   TRANSLATOR_DECLARATIONS (Vertical_align_engraver);
40   DECLARE_ACKNOWLEDGER (axis_group);
41
42 protected:
43   virtual void derived_mark () const;
44   void process_music ();
45   virtual void finalize ();
46   virtual void initialize ();
47
48   bool top_level_;
49 };
50
51 ADD_ACKNOWLEDGER (Vertical_align_engraver, axis_group);
52 ADD_TRANSLATOR (Vertical_align_engraver,
53                 /* doc */
54                 "Catch groups (staves, lyrics lines, etc.) and stack them"
55                 " vertically.",
56
57                 /* create */
58                 "VerticalAlignment ",
59
60                 /* read */
61                 "alignAboveContext "
62                 "alignBelowContext "
63                 "hasAxisGroup ",
64
65                 /* write */
66                 ""
67                );
68
69 Vertical_align_engraver::Vertical_align_engraver ()
70 {
71   valign_ = 0;
72   id_to_group_hashtab_ = SCM_EOL;
73   top_level_ = false;
74 }
75
76 void
77 Vertical_align_engraver::derived_mark () const
78 {
79   scm_gc_mark (id_to_group_hashtab_);
80 }
81
82 void
83 Vertical_align_engraver::initialize ()
84 {
85   id_to_group_hashtab_ = scm_c_make_hash_table (11);
86 }
87
88 void
89 Vertical_align_engraver::process_music ()
90 {
91   if (!valign_ && !scm_is_null (id_to_group_hashtab_))
92     {
93       if (to_boolean (get_property ("hasAxisGroup")))
94         {
95           warning (_ ("Ignoring Vertical_align_engraver in VerticalAxisGroup"));
96           id_to_group_hashtab_ = SCM_EOL;
97           return;
98         }
99       
100       top_level_ = to_boolean (get_property ("topLevelAlignment"));
101
102       valign_ = make_spanner (top_level_ ? "VerticalAlignment" : "StaffGrouper", SCM_EOL);
103       valign_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));
104       Align_interface::set_ordered (valign_);
105     }
106 }
107
108 void
109 Vertical_align_engraver::finalize ()
110 {
111   if (valign_)
112     {
113       valign_->set_bound (RIGHT, unsmob_grob (get_property ("currentCommandColumn")));
114       valign_ = 0;
115     }
116 }
117
118 bool
119 Vertical_align_engraver::qualifies (Grob_info i) const
120 {
121   int sz = i.origin_contexts ((Translator *)this).size ();
122
123   return sz > 0 && Axis_group_interface::has_interface (i.grob ())
124          && !i.grob ()->get_parent (Y_AXIS)
125          && !to_boolean (i.grob ()->get_property ("no-alignment"))
126          && Axis_group_interface::has_axis (i.grob (), Y_AXIS);
127 }
128
129 void
130 Vertical_align_engraver::acknowledge_axis_group (Grob_info i)
131 {
132   if (scm_is_null (id_to_group_hashtab_))
133     return;
134
135   if (top_level_ && qualifies (i))
136     {
137       string id = i.context ()->id_string ();
138
139       scm_hash_set_x (id_to_group_hashtab_, ly_string2scm (id),
140                       i.grob ()->self_scm ());
141
142       SCM before_id = i.context ()->get_property ("alignAboveContext");
143       SCM after_id = i.context ()->get_property ("alignBelowContext");
144
145       SCM before = scm_hash_ref (id_to_group_hashtab_, before_id, SCM_BOOL_F);
146       SCM after = scm_hash_ref (id_to_group_hashtab_, after_id, SCM_BOOL_F);
147
148       Grob *before_grob = unsmob_grob (before);
149       Grob *after_grob = unsmob_grob (after);
150
151       Align_interface::add_element (valign_, i.grob ());
152
153       if (before_grob || after_grob)
154         {
155           Grob_array *ga = unsmob_grob_array (valign_->get_object ("elements"));
156           vector<Grob *> &arr = ga->array_reference ();
157
158           Grob *added = arr.back ();
159           arr.pop_back ();
160           for (vsize i = 0; i < arr.size (); i++)
161             {
162               if (arr[i] == before_grob)
163                 {
164                   arr.insert (arr.begin () + i, added);
165
166                   /* Only set staff affinity if it already has one.  That way we won't
167                      set staff-affinity on things that don't want it (like staves). */
168                   if (scm_is_number (added->get_property ("staff-affinity")))
169                     added->set_property ("staff-affinity", scm_from_int (DOWN));
170                   break;
171                 }
172               else if (arr[i] == after_grob)
173                 {
174                   arr.insert (arr.begin () + i + 1, added);
175                   if (scm_is_number (added->get_property ("staff-affinity")))
176                     added->set_property ("staff-affinity", scm_from_int (UP));
177                   break;
178                 }
179             }
180         }
181     }
182   else if (qualifies (i))
183     {
184       Pointer_group_interface::add_grob (valign_, ly_symbol2scm ("elements"), i.grob ());
185       if (!unsmob_grob (i.grob ()->get_object ("staff-grouper")))
186         i.grob ()->set_object ("staff-grouper", valign_->self_scm ());
187     }
188 }