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