]> git.donarmstrong.com Git - lilypond.git/blob - lily/ambitus-engraver.cc
6590b0a1cbf7c5013d209d348125b9ab0f4198eb
[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--2006 Juergen Reuter <reuter@ipd.uka.de>
7
8   Han-Wen Nienhuys <hanwen@xs4all.nl
9 */
10
11 #include "engraver.hh"
12
13 #include "axis-group-interface.hh"
14 #include "note-head.hh"
15 #include "pitch-interval.hh"
16 #include "pointer-group-interface.hh"
17 #include "protected-scm.hh"
18 #include "side-position-interface.hh"
19 #include "staff-symbol-referencer.hh" 
20 #include "stream-event.hh"
21
22 #include "translator.icc"
23
24 class Ambitus_engraver : public Engraver
25 {
26 public:
27   TRANSLATOR_DECLARATIONS (Ambitus_engraver);
28   void process_music ();
29   void acknowledge_note_head (Grob_info);
30   void stop_translation_timestep ();
31   virtual void finalize ();
32   virtual void derived_mark () const;
33
34 private:
35   void create_ambitus ();
36   Item *ambitus_;
37   Item *group_;
38   Drul_array<Item *> heads_;
39   Drul_array<Item *> accidentals_;
40   Pitch_interval pitch_interval_;
41   bool is_typeset_;
42   int start_c0_;
43   SCM start_key_sig_;
44 };
45
46 void
47 Ambitus_engraver::derived_mark () const
48 {
49   scm_gc_mark (start_key_sig_);
50 }
51
52 void
53 Ambitus_engraver::create_ambitus ()
54 {
55   ambitus_ = make_item ("AmbitusLine", SCM_EOL);
56   group_ = make_item ("Ambitus", SCM_EOL);
57   Direction d = DOWN;
58   do
59     {
60       heads_[d] = make_item ("AmbitusNoteHead", SCM_EOL);
61       accidentals_[d] = make_item ("AmbitusAccidental", SCM_EOL);
62       accidentals_[d]->set_parent (heads_[d], Y_AXIS);
63       heads_[d]->set_object ("accidental-grob",
64                              accidentals_[d]->self_scm ());
65       Axis_group_interface::add_element (group_, heads_[d]);
66       Axis_group_interface::add_element (group_, accidentals_[d]);
67       Side_position_interface::add_support (accidentals_[d], heads_[d]);
68     }
69   while (flip (&d) != DOWN);
70
71   ambitus_->set_parent (heads_[DOWN], X_AXIS);
72   Axis_group_interface::add_element (group_, ambitus_);
73
74   is_typeset_ = false;
75 }
76
77 Ambitus_engraver::Ambitus_engraver ()
78 {
79   ambitus_ = 0;
80   heads_[LEFT] = heads_[RIGHT] = 0;
81   accidentals_[LEFT] = accidentals_[RIGHT] = 0;
82   group_ = 0;
83   is_typeset_ = false;
84   start_key_sig_ = SCM_EOL;
85 }
86
87 void
88 Ambitus_engraver::process_music ()
89 {
90   /*
91    * Ensure that ambitus is created in the very first timestep (on
92    * which lily does not call start_translation_timestep ()).
93    * Otherwise, if a voice begins with a rest, the ambitus grob will
94    * be placed after the rest.
95    */
96   if (!ambitus_)
97     create_ambitus ();
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       is_typeset_ = true;
115     }
116 }
117
118 void
119 Ambitus_engraver::acknowledge_note_head (Grob_info info)
120 {
121   Stream_event *nr = info.event_cause ();
122   if (nr && nr->in_event_class ("note-event"))
123     {
124       Pitch pitch = *unsmob_pitch (nr->get_property ("pitch"));
125       pitch_interval_.add_point (pitch);
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)
151             ? scm_to_int (scm_cdr (handle)) : 0;
152
153           if (sig_alter == p.get_alteration ())
154             {
155               accidentals_[d]->suicide ();
156               heads_[d]->set_object ("accidental-grob", SCM_EOL);
157             }
158           else
159             {
160               SCM l = scm_list_1 (scm_from_int (p.get_alteration ()));
161               accidentals_[d]->set_property ("accidentals", l);
162             }
163         }
164       while (flip (&d) != DOWN);
165
166
167       Pointer_group_interface::add_grob (ambitus_, ly_symbol2scm ("note-heads"), heads_[DOWN]);
168       Pointer_group_interface::add_grob (ambitus_, ly_symbol2scm ("note-heads"), heads_[UP]);
169     }
170   else
171     {
172       Direction d = DOWN;
173       do
174         {
175           accidentals_[d]->suicide ();
176           heads_[d]->suicide ();
177         }
178       while (flip (&d) != DOWN);
179
180       ambitus_->suicide ();
181     }
182 }
183
184 ADD_ACKNOWLEDGER (Ambitus_engraver, note_head);
185 ADD_TRANSLATOR (Ambitus_engraver,
186                 /* doc */ "",
187                 /* create */
188                 "Ambitus "
189                 "AmbitusLine "
190                 "AmbitusNoteHead "
191                 "AmbitusAccidental",
192
193                 /* accept */ "",
194                 /* read */ "",
195                 /* write */ "");