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