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