]> git.donarmstrong.com Git - lilypond.git/blob - lily/axis-group-engraver.cc
Issue 4997/6: Use Preinit for font metrics
[lilypond.git] / lily / axis-group-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2015 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 "engraver.hh"
21
22 #include "axis-group-interface.hh"
23 #include "hara-kiri-group-spanner.hh"
24 #include "pointer-group-interface.hh"
25 #include "context.hh"
26 #include "international.hh"
27 #include "spanner.hh"
28 #include "warn.hh"
29
30 #include "translator.icc"
31
32 /**
33    Put stuff in a Spanner with an Axis_group_interface.
34    Use as last element of a context.
35 */
36 class Axis_group_engraver : public Engraver
37 {
38 protected:
39   bool active_;
40   Spanner *staffline_;
41   SCM interesting_;
42   vector<Grob *> elts_;
43   void process_music ();
44   virtual void initialize ();
45   virtual void finalize ();
46   void acknowledge_grob (Grob_info);
47   void process_acknowledged ();
48   virtual Spanner *get_spanner ();
49   virtual void add_element (Grob *);
50   virtual bool must_be_last () const;
51   virtual void derived_mark () const;
52
53 public:
54   TRANSLATOR_DECLARATIONS (Axis_group_engraver);
55 };
56
57
58 Axis_group_engraver::Axis_group_engraver ()
59 {
60   staffline_ = 0;
61   interesting_ = SCM_EOL;
62   active_ = false;
63 }
64
65 void
66 Axis_group_engraver::initialize ()
67 {
68   active_ = !to_boolean (get_property ("hasAxisGroup"));
69   if (active_)
70     context ()->set_property ("hasAxisGroup", SCM_BOOL_T);
71 }
72
73 void
74 Axis_group_engraver::derived_mark () const
75 {
76   scm_gc_mark (interesting_);
77 }
78
79
80 bool
81 Axis_group_engraver::must_be_last () const
82 {
83   return true;
84 }
85
86 void
87 Axis_group_engraver::process_music ()
88 {
89   if (!staffline_ && active_)
90     {
91       staffline_ = get_spanner ();
92       Grob *it = unsmob<Grob> (get_property ("currentCommandColumn"));
93       staffline_->set_bound (LEFT, it);
94     }
95   interesting_ = get_property ("keepAliveInterfaces");
96 }
97
98 Spanner *
99 Axis_group_engraver::get_spanner ()
100 {
101   return make_spanner ("VerticalAxisGroup", SCM_EOL);
102 }
103
104 void
105 Axis_group_engraver::finalize ()
106 {
107   if (staffline_)
108     {
109       Grob *it = unsmob<Grob> (get_property ("currentCommandColumn"));
110       staffline_->set_bound (RIGHT, it);
111
112       Pointer_group_interface::set_ordered (staffline_, ly_symbol2scm ("elements"), false);
113     }
114 }
115
116 void
117 Axis_group_engraver::acknowledge_grob (Grob_info i)
118 {
119   if (!staffline_)
120     return;
121
122   elts_.push_back (i.grob ());
123
124   if (to_boolean (staffline_->get_property ("remove-empty")))
125     {
126       for (SCM s = interesting_; scm_is_pair (s); s = scm_cdr (s))
127         {
128           if (i.grob ()->internal_has_interface (scm_car (s)))
129             {
130               Hara_kiri_group_spanner::add_interesting_item (staffline_, i.grob ());
131               break;
132             }
133         }
134     }
135 }
136
137 /*
138   maybe should check if our parent is set, because we now get a
139   cyclic parent relationship if we have two Axis_group_engravers in
140   the context.  */
141 void
142 Axis_group_engraver::process_acknowledged ()
143 {
144   if (!staffline_)
145     return;
146
147   for (vsize i = 0; i < elts_.size (); i++)
148     {
149       if (!unsmob<Grob> (elts_[i]->get_object ("axis-group-parent-Y")))
150         {
151           if (staffline_->get_parent (Y_AXIS)
152               && staffline_->get_parent (Y_AXIS) == elts_[i])
153             {
154               staffline_->warning (_ ("Axis_group_engraver: vertical group already has a parent"));
155               staffline_->warning (_ ("are there two Axis_group_engravers?"));
156               staffline_->warning (_ ("removing this vertical group"));
157               staffline_->suicide ();
158               staffline_ = 0;
159               break;
160             }
161           add_element (elts_[i]);
162         }
163     }
164   elts_.clear ();
165 }
166
167 void
168 Axis_group_engraver::add_element (Grob *e)
169 {
170   Axis_group_interface::add_element (staffline_, e);
171 }
172
173
174 void
175 Axis_group_engraver::boot ()
176 {
177   ADD_ACKNOWLEDGER (Axis_group_engraver, grob);
178 }
179
180 ADD_TRANSLATOR (Axis_group_engraver,
181                 /* doc */
182                 "Group all objects created in this context in a"
183                 " @code{VerticalAxisGroup} spanner.",
184
185                 /* create */
186                 "VerticalAxisGroup ",
187
188                 /* read */
189                 "currentCommandColumn "
190                 "keepAliveInterfaces "
191                 "hasAxisGroup ",
192                 
193                 /* write */
194                 "hasAxisGroup "
195                );