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