]> git.donarmstrong.com Git - lilypond.git/blob - lily/ambitus-engraver.cc
Merge commit 'origin/dev/jneeman' into systems-per-page
[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--2009 Juergen Reuter <reuter@ipd.uka.de>
7
8   Han-Wen Nienhuys <hanwen@xs4all.nl
9 */
10
11 #include "engraver.hh"
12
13 #include "accidental-placement.hh"
14 #include "axis-group-interface.hh"
15 #include "item.hh"
16 #include "note-head.hh"
17 #include "pitch-interval.hh"
18 #include "pointer-group-interface.hh"
19 #include "protected-scm.hh"
20 #include "side-position-interface.hh"
21 #include "separation-item.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "stream-event.hh"
24
25 #include "translator.icc"
26
27 class Ambitus_engraver : public Engraver
28 {
29 public:
30   TRANSLATOR_DECLARATIONS (Ambitus_engraver);
31 protected:
32   DECLARE_ACKNOWLEDGER (note_head);
33
34   void process_music ();
35   void stop_translation_timestep ();
36   virtual void finalize ();
37   virtual void derived_mark () const;
38
39 private:
40   void create_ambitus ();
41   Item *ambitus_;
42   Item *group_;
43   Drul_array<Item *> heads_;
44   Drul_array<Item *> accidentals_;
45   Drul_array<Stream_event *> causes_;
46   Pitch_interval pitch_interval_;
47   bool is_typeset_;
48   int start_c0_;
49   SCM start_key_sig_;
50 };
51
52 void
53 Ambitus_engraver::derived_mark () const
54 {
55   scm_gc_mark (start_key_sig_);
56 }
57
58 void
59 Ambitus_engraver::create_ambitus ()
60 {
61   ambitus_ = make_item ("AmbitusLine", SCM_EOL);
62   group_ = make_item ("Ambitus", SCM_EOL);
63   Direction d = DOWN;
64   do
65     {
66       heads_[d] = make_item ("AmbitusNoteHead", SCM_EOL);
67       accidentals_[d] = make_item ("AmbitusAccidental", SCM_EOL);
68       accidentals_[d]->set_parent (heads_[d], Y_AXIS);
69       heads_[d]->set_object ("accidental-grob",
70                              accidentals_[d]->self_scm ());
71       Axis_group_interface::add_element (group_, heads_[d]);
72       Axis_group_interface::add_element (group_, accidentals_[d]);
73     }
74   while (flip (&d) != DOWN);
75
76   ambitus_->set_parent (heads_[DOWN], X_AXIS);
77   Axis_group_interface::add_element (group_, ambitus_);
78
79   is_typeset_ = false;
80 }
81
82 Ambitus_engraver::Ambitus_engraver ()
83 {
84   ambitus_ = 0;
85   heads_[LEFT] = heads_[RIGHT] = 0;
86   accidentals_[LEFT] = accidentals_[RIGHT] = 0;
87   group_ = 0;
88   is_typeset_ = false;
89   start_key_sig_ = SCM_EOL;
90 }
91
92 void
93 Ambitus_engraver::process_music ()
94 {
95   /*
96    * Ensure that ambitus is created in the very first timestep
97    */
98   if (!ambitus_)
99     create_ambitus ();
100 }
101
102 void
103 Ambitus_engraver::stop_translation_timestep ()
104 {
105   if (ambitus_ && !is_typeset_)
106     {
107       /*
108        * Evaluate middleCPosition not until now, since otherwise we
109        * may then oversee a clef that is defined in a staff context if
110        * we are in a voice context; middleCPosition would then be
111        * assumed to be 0.
112        */
113       start_c0_ = robust_scm2int (get_property ("middleCPosition"), 0);
114       start_key_sig_ = get_property ("keySignature");
115
116       is_typeset_ = true;
117     }
118 }
119
120 void
121 Ambitus_engraver::acknowledge_note_head (Grob_info info)
122 {
123   Stream_event *nr = info.event_cause ();
124   SCM p = nr->get_property ("pitch");
125   /*
126     If the engraver is added to a percussion context,
127     filter out unpitched note heads.
128   */
129   if (!unsmob_pitch (p))
130     return;
131   if (nr && nr->in_event_class ("note-event"))
132     {
133       Pitch pitch = *unsmob_pitch (p);
134       Drul_array<bool> expands = pitch_interval_.add_point (pitch);
135       if (expands[UP])
136         causes_[UP] = nr;
137       if (expands[DOWN])
138         causes_[DOWN] = nr;
139     }
140 }
141
142 void
143 Ambitus_engraver::finalize ()
144 {
145   if (ambitus_ && !pitch_interval_.is_empty ())
146     {
147       Grob *accidental_placement =
148         make_item ("AccidentalPlacement",
149                    accidentals_[DOWN]->self_scm ());
150
151       Direction d = DOWN;
152       do
153         {
154           Pitch p = pitch_interval_[d];
155           heads_[d]->set_property ("cause", causes_[d]->self_scm());
156           heads_[d]->set_property ("staff-position",
157                                    scm_from_int (start_c0_
158                                                  + p.steps ()));
159
160           SCM handle = scm_assoc (scm_cons (scm_from_int (p.get_octave ()),
161                                             scm_from_int (p.get_notename ())),
162                                   start_key_sig_);
163
164           if (handle == SCM_BOOL_F)
165             handle = scm_assoc (scm_from_int (p.get_notename ()),
166                                 start_key_sig_);
167
168           Rational sig_alter = (handle != SCM_BOOL_F)
169             ? robust_scm2rational (scm_cdr (handle), Rational (0)) : Rational (0);
170
171           if (sig_alter == p.get_alteration ())
172             {
173               accidentals_[d]->suicide ();
174               heads_[d]->set_object ("accidental-grob", SCM_EOL);
175             }
176           else
177             {
178               accidentals_[d]->set_property ("alteration", ly_rational2scm (p.get_alteration ()));
179             }
180           Separation_item::add_conditional_item (heads_[d], accidental_placement);
181           Accidental_placement::add_accidental (accidental_placement, accidentals_[d]);
182         }
183       while (flip (&d) != DOWN);
184
185
186       Pointer_group_interface::add_grob (ambitus_, ly_symbol2scm ("note-heads"), heads_[DOWN]);
187       Pointer_group_interface::add_grob (ambitus_, ly_symbol2scm ("note-heads"), heads_[UP]);
188       Axis_group_interface::add_element (group_, accidental_placement);
189     }
190   else
191     {
192       Direction d = DOWN;
193       do
194         {
195           accidentals_[d]->suicide ();
196           heads_[d]->suicide ();
197         }
198       while (flip (&d) != DOWN);
199
200       ambitus_->suicide ();
201     }
202 }
203
204 ADD_ACKNOWLEDGER (Ambitus_engraver, note_head);
205 ADD_TRANSLATOR (Ambitus_engraver,
206                 /* doc */
207                 "",
208
209                 /* create */
210                 "AccidentalPlacement "
211                 "Ambitus "
212                 "AmbitusAccidental "
213                 "AmbitusLine "
214                 "AmbitusNoteHead ",
215
216                 /* read */
217                 "",
218
219                 /* write */
220                 ""
221                 );