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