]> git.donarmstrong.com Git - lilypond.git/blob - lily/ambitus-engraver.cc
Use a `define-builtin-markup-command' macro for builtin markups, which
[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 "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 (on
93    * which lily does not call start_translation_timestep ()).
94    * Otherwise, if a voice begins with a rest, the ambitus grob will
95    * be placed after the rest.
96    */
97   if (!ambitus_)
98     create_ambitus ();
99 }
100
101 void
102 Ambitus_engraver::stop_translation_timestep ()
103 {
104   if (ambitus_ && !is_typeset_)
105     {
106       /*
107        * Evaluate middleCPosition not until now, since otherwise we
108        * may then oversee a clef that is defined in a staff context if
109        * we are in a voice context; middleCPosition would then be
110        * assumed to be 0.
111        */
112       start_c0_ = robust_scm2int (get_property ("middleCPosition"), 0);
113       start_key_sig_ = get_property ("keySignature");
114
115       is_typeset_ = true;
116     }
117 }
118
119 void
120 Ambitus_engraver::acknowledge_note_head (Grob_info info)
121 {
122   Stream_event *nr = info.event_cause ();
123   if (nr && nr->in_event_class ("note-event"))
124     {
125       Pitch pitch = *unsmob_pitch (nr->get_property ("pitch"));
126       pitch_interval_.add_point (pitch);
127     }
128 }
129
130 void
131 Ambitus_engraver::finalize ()
132 {
133   if (ambitus_ && !pitch_interval_.is_empty ())
134     {
135       Direction d = DOWN;
136       do
137         {
138           Pitch p = pitch_interval_[d];
139           heads_[d]->set_property ("staff-position",
140                                    scm_from_int (start_c0_
141                                                  + p.steps ()));
142
143           SCM handle = scm_assoc (scm_cons (scm_from_int (p.get_octave ()),
144                                             scm_from_int (p.get_notename ())),
145                                   start_key_sig_);
146
147           if (handle == SCM_BOOL_F)
148             handle = scm_assoc (scm_from_int (p.get_notename ()),
149                                 start_key_sig_);
150
151           int sig_alter = (handle != SCM_BOOL_F)
152             ? scm_to_int (scm_cdr (handle)) : 0;
153
154           if (sig_alter == p.get_alteration ())
155             {
156               accidentals_[d]->suicide ();
157               heads_[d]->set_object ("accidental-grob", SCM_EOL);
158             }
159           else
160             {
161               SCM l = scm_list_1 (scm_from_int (p.get_alteration ()));
162               accidentals_[d]->set_property ("accidentals", l);
163             }
164         }
165       while (flip (&d) != DOWN);
166
167
168       Pointer_group_interface::add_grob (ambitus_, ly_symbol2scm ("note-heads"), heads_[DOWN]);
169       Pointer_group_interface::add_grob (ambitus_, ly_symbol2scm ("note-heads"), heads_[UP]);
170     }
171   else
172     {
173       Direction d = DOWN;
174       do
175         {
176           accidentals_[d]->suicide ();
177           heads_[d]->suicide ();
178         }
179       while (flip (&d) != DOWN);
180
181       ambitus_->suicide ();
182     }
183 }
184
185 ADD_ACKNOWLEDGER (Ambitus_engraver, note_head);
186 ADD_TRANSLATOR (Ambitus_engraver,
187                 /* doc */ "",
188                 /* create */
189                 "Ambitus "
190                 "AmbitusLine "
191                 "AmbitusNoteHead "
192                 "AmbitusAccidental",
193                 /* read */ "",
194                 /* write */ "");