]> git.donarmstrong.com Git - lilypond.git/blob - lily/ambitus-engraver.cc
* lily/include/grob-info.hh (class Grob_info): make data member
[lilypond.git] / lily / ambitus-engraver.cc
1 /*
2   ambitus-engraver.cc -- implement Ambitus_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2002--2005 Juergen Reuter <reuter@ipd.uka.de>
7
8   Han-Wen Nienhuys <hanwen@xs4all.nl
9 */
10
11 #include "engraver.hh"
12 #include "note-head.hh"
13 #include "pitch-interval.hh"
14 #include "protected-scm.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "axis-group-interface.hh"
17 #include "side-position-interface.hh"
18
19 class Ambitus_engraver : public Engraver
20 {
21 public:
22   TRANSLATOR_DECLARATIONS (Ambitus_engraver);
23   virtual void process_music ();
24   virtual void acknowledge_grob (Grob_info);
25   virtual void stop_translation_timestep ();
26   virtual void finalize ();
27   virtual void derived_mark () const;
28
29 private:
30   void create_ambitus ();
31   Item *ambitus_;
32   Item *group_;
33   Drul_array<Item *> heads_;
34   Drul_array<Item *> accidentals_;
35   Pitch_interval pitch_interval_;
36   bool is_typeset_;
37   int start_c0_;
38   SCM start_key_sig_;
39 };
40
41 void
42 Ambitus_engraver::derived_mark () const
43 {
44   scm_gc_mark (start_key_sig_);
45 }
46
47 void
48 Ambitus_engraver::create_ambitus ()
49 {
50   ambitus_ = make_item ("AmbitusLine", SCM_EOL);
51   group_ = make_item ("Ambitus", SCM_EOL);
52   Direction d = DOWN;
53   do
54     {
55       heads_[d] = make_item ("AmbitusNoteHead", SCM_EOL);
56       accidentals_[d] = make_item ("AmbitusAccidental", SCM_EOL);
57       accidentals_[d]->set_parent (heads_[d], Y_AXIS);
58       heads_[d]->set_property ("accidental-grob", accidentals_[d]->self_scm ());
59       Axis_group_interface::add_element (group_, heads_[d]);
60       Axis_group_interface::add_element (group_, accidentals_[d]);
61       Side_position_interface::add_support (accidentals_[d], heads_[d]);
62     }
63   while (flip (&d) != DOWN);
64
65   ambitus_->set_parent (heads_[DOWN], X_AXIS);
66   Axis_group_interface::add_element (group_, ambitus_);
67
68   is_typeset_ = false;
69 }
70
71 Ambitus_engraver::Ambitus_engraver ()
72 {
73   ambitus_ = 0;
74   heads_[LEFT] = heads_[RIGHT] = 0;
75   accidentals_[LEFT] = accidentals_[RIGHT] = 0;
76   group_ = 0;
77   is_typeset_ = false;
78   start_key_sig_ = SCM_EOL;
79 }
80
81 void
82 Ambitus_engraver::process_music ()
83 {
84   /*
85    * Ensure that ambitus is created in the very first timestep (on
86    * which lily does not call start_translation_timestep ()).
87    * Otherwise, if a voice begins with a rest, the ambitus grob will
88    * be placed after the rest.
89    */
90   if (!ambitus_)
91     {
92       create_ambitus ();
93     }
94 }
95
96 void
97 Ambitus_engraver::stop_translation_timestep ()
98 {
99   if (ambitus_ && !is_typeset_)
100     {
101       /*
102        * Evaluate middleCPosition not until now, since otherwise we
103        * may then oversee a clef that is defined in a staff context if
104        * we are in a voice context; middleCPosition would then be
105        * assumed to be 0.
106        */
107       start_c0_ = robust_scm2int (get_property ("middleCPosition"), 0);
108       start_key_sig_ = get_property ("keySignature");
109
110       is_typeset_ = true;
111     }
112 }
113
114 void
115 Ambitus_engraver::acknowledge_grob (Grob_info info)
116 {
117   Item *item = dynamic_cast<Item *> (info.grob ());
118   if (item)
119     {
120       if (Note_head::has_interface (info.grob ()))
121         {
122           Music *nr = info.music_cause ();
123           if (nr && nr->is_mus_type ("note-event"))
124             {
125               Pitch pitch = *unsmob_pitch (nr->get_property ("pitch"));
126               pitch_interval_.add_point (pitch);
127             }
128         }
129     }
130 }
131
132 void
133 Ambitus_engraver::finalize ()
134 {
135   if (ambitus_ && !pitch_interval_.is_empty ())
136     {
137       Direction d = DOWN;
138       do
139         {
140           Pitch p = pitch_interval_[d];
141           heads_[d]->set_property ("staff-position",
142                                    scm_from_int (start_c0_
143                                                  + p.steps ()));
144
145           SCM handle = scm_assoc (scm_cons (scm_from_int (p.get_octave ()),
146                                             scm_from_int (p.get_notename ())),
147                                   start_key_sig_);
148
149           if (handle == SCM_BOOL_F)
150             handle = scm_assoc (scm_from_int (p.get_notename ()),
151                                 start_key_sig_);
152
153           int sig_alter = (handle != SCM_BOOL_F) ? scm_to_int (scm_cdr (handle)) : 0;
154           if (sig_alter == p.get_alteration ())
155             {
156               accidentals_[d]->suicide ();
157               heads_[d]->set_property ("accidental-grob", SCM_EOL);
158             }
159           else
160             {
161               accidentals_[d]->set_property ("accidentals",
162                                              scm_list_1 (scm_from_int (p.get_alteration ())));
163             }
164         }
165       while (flip (&d) != DOWN);
166
167       ambitus_->set_property ("note-heads", scm_list_2 (heads_[DOWN]->self_scm (),
168                                                         heads_[UP]->self_scm ()));
169     }
170   else
171     {
172       Direction d = DOWN;
173       do
174         {
175           accidentals_[d]->suicide ();
176           heads_[d]->suicide ();
177         }
178       while (flip (&d) != DOWN);
179
180       ambitus_->suicide ();
181     }
182 }
183
184 ADD_TRANSLATOR (Ambitus_engraver,
185                 /* descr */ "",
186                 /* creats*/ "Ambitus AmbitusLine AmbitusNoteHead AmbitusAccidental",
187                 /* accepts */ "",
188                 /* acks  */ "note-head-interface",
189                 /* reads */ "",
190                 /* write */ "");