]> git.donarmstrong.com Git - lilypond.git/blob - lily/ambitus-engraver.cc
Fix some bugs in the dynamic engraver and PostScript backend
[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
21 #include "translator.icc"
22
23 class Ambitus_engraver : public Engraver
24 {
25 public:
26   TRANSLATOR_DECLARATIONS (Ambitus_engraver);
27   void process_music ();
28   void acknowledge_note_head (Grob_info);
29   void stop_translation_timestep ();
30   virtual void finalize ();
31   virtual void derived_mark () const;
32
33 private:
34   void create_ambitus ();
35   Item *ambitus_;
36   Item *group_;
37   Drul_array<Item *> heads_;
38   Drul_array<Item *> accidentals_;
39   Pitch_interval pitch_interval_;
40   bool is_typeset_;
41   int start_c0_;
42   SCM start_key_sig_;
43 };
44
45 void
46 Ambitus_engraver::derived_mark () const
47 {
48   scm_gc_mark (start_key_sig_);
49 }
50
51 void
52 Ambitus_engraver::create_ambitus ()
53 {
54   ambitus_ = make_item ("AmbitusLine", SCM_EOL);
55   group_ = make_item ("Ambitus", SCM_EOL);
56   Direction d = DOWN;
57   do
58     {
59       heads_[d] = make_item ("AmbitusNoteHead", SCM_EOL);
60       accidentals_[d] = make_item ("AmbitusAccidental", SCM_EOL);
61       accidentals_[d]->set_parent (heads_[d], Y_AXIS);
62       heads_[d]->set_object ("accidental-grob",
63                              accidentals_[d]->self_scm ());
64       Axis_group_interface::add_element (group_, heads_[d]);
65       Axis_group_interface::add_element (group_, accidentals_[d]);
66       Side_position_interface::add_support (accidentals_[d], heads_[d]);
67     }
68   while (flip (&d) != DOWN);
69
70   ambitus_->set_parent (heads_[DOWN], X_AXIS);
71   Axis_group_interface::add_element (group_, ambitus_);
72
73   is_typeset_ = false;
74 }
75
76 Ambitus_engraver::Ambitus_engraver ()
77 {
78   ambitus_ = 0;
79   heads_[LEFT] = heads_[RIGHT] = 0;
80   accidentals_[LEFT] = accidentals_[RIGHT] = 0;
81   group_ = 0;
82   is_typeset_ = false;
83   start_key_sig_ = SCM_EOL;
84 }
85
86 void
87 Ambitus_engraver::process_music ()
88 {
89   /*
90    * Ensure that ambitus is created in the very first timestep (on
91    * which lily does not call start_translation_timestep ()).
92    * Otherwise, if a voice begins with a rest, the ambitus grob will
93    * be placed after the rest.
94    */
95   if (!ambitus_)
96     create_ambitus ();
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_note_head (Grob_info info)
119 {
120   Music *nr = info.music_cause ();
121   if (nr && nr->is_mus_type ("note-event"))
122     {
123       Pitch pitch = *unsmob_pitch (nr->get_property ("pitch"));
124       pitch_interval_.add_point (pitch);
125     }
126 }
127
128 void
129 Ambitus_engraver::finalize ()
130 {
131   if (ambitus_ && !pitch_interval_.is_empty ())
132     {
133       Direction d = DOWN;
134       do
135         {
136           Pitch p = pitch_interval_[d];
137           heads_[d]->set_property ("staff-position",
138                                    scm_from_int (start_c0_
139                                                  + p.steps ()));
140
141           SCM handle = scm_assoc (scm_cons (scm_from_int (p.get_octave ()),
142                                             scm_from_int (p.get_notename ())),
143                                   start_key_sig_);
144
145           if (handle == SCM_BOOL_F)
146             handle = scm_assoc (scm_from_int (p.get_notename ()),
147                                 start_key_sig_);
148
149           int sig_alter = (handle != SCM_BOOL_F)
150             ? scm_to_int (scm_cdr (handle)) : 0;
151
152           if (sig_alter == p.get_alteration ())
153             {
154               accidentals_[d]->suicide ();
155               heads_[d]->set_object ("accidental-grob", SCM_EOL);
156             }
157           else
158             {
159               SCM l = scm_list_1 (scm_from_int (p.get_alteration ()));
160               accidentals_[d]->set_property ("accidentals", l);
161             }
162         }
163       while (flip (&d) != DOWN);
164
165
166       Pointer_group_interface::add_grob (ambitus_, ly_symbol2scm ("note-heads"), heads_[DOWN]);
167       Pointer_group_interface::add_grob (ambitus_, ly_symbol2scm ("note-heads"), heads_[UP]);
168     }
169   else
170     {
171       Direction d = DOWN;
172       do
173         {
174           accidentals_[d]->suicide ();
175           heads_[d]->suicide ();
176         }
177       while (flip (&d) != DOWN);
178
179       ambitus_->suicide ();
180     }
181 }
182
183 ADD_ACKNOWLEDGER (Ambitus_engraver, note_head);
184 ADD_TRANSLATOR (Ambitus_engraver,
185                 /* doc */ "",
186                 /* create */ "Ambitus AmbitusLine AmbitusNoteHead AmbitusAccidental",
187                 /* accept */ "",
188                 /* read */ "",
189                 /* write */ "");