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