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