]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
release: 1.3.150
[lilypond.git] / lily / clef-engraver.cc
1 /*
2   clef-engraver.cc -- implement Clef_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>,
7
8   Mats Bengtsson <matsb@s3.kth.se>
9 */
10
11 #include <ctype.h>
12
13 #include "translator-group.hh"
14 #include "key-item.hh"
15 #include "bar.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "debug.hh"
18 #include "engraver.hh"
19 #include "direction.hh"
20 #include "side-position-interface.hh"
21 #include "item.hh"
22
23
24 class Clef_engraver : public  Engraver
25 {
26 public:
27   VIRTUAL_COPY_CONS (Translator);
28   Clef_engraver ();
29
30   Direction octave_dir_;
31
32 protected:
33   virtual void stop_translation_timestep ();
34   virtual void start_translation_timestep ();
35   virtual void process_music ();
36   virtual void acknowledge_grob (Grob_info);
37   virtual void do_creation_processing ();
38 private:
39   Item * clef_p_;
40   Item * octavate_p_;
41
42   SCM prev_glyph_;
43   SCM prev_cpos_;
44   SCM prev_octavation_;
45   void create_clef ();
46   void set_central_c (SCM, SCM, SCM);
47   void set_glyph ();
48   void inspect_clef_properties ();
49 };
50
51 Clef_engraver::Clef_engraver ()
52 {
53   clef_p_ = 0;
54   octave_dir_ = CENTER;
55   octavate_p_ = 0;
56
57   /*
58     will trigger a clef at the start since #f != ' ()
59    */
60   prev_cpos_ = prev_glyph_ = SCM_BOOL_F;
61 }
62
63 void
64 Clef_engraver::set_central_c (SCM glyph,SCM clefpos, SCM octavation)
65 {
66   prev_cpos_ = clefpos;
67   prev_glyph_ = glyph;
68   prev_octavation_ = octavation;
69
70   SCM p = get_property ("clefPitches");
71   int c0_position =  0;
72   if (gh_list_p (p))
73     {
74       SCM found = scm_assoc (glyph, p);
75       if (found == SCM_BOOL_F)
76         {
77           c0_position =0;
78         }
79       else
80         {
81           c0_position =  gh_scm2int (gh_cdr (found));
82
83           if (gh_number_p (octavation))
84               c0_position -= gh_scm2int (octavation);
85       
86           if (gh_number_p (clefpos))
87             c0_position += gh_scm2int (clefpos);
88         }
89       
90     }
91   daddy_trans_l_->set_property ("centralCPosition", gh_int2scm (c0_position));
92 }
93
94 void
95 Clef_engraver::set_glyph ()
96 {
97   SCM glyph_sym = ly_symbol2scm ("glyph-name");
98   SCM glyph = get_property ("clefGlyph");
99
100   SCM basic = ly_symbol2scm ("Clef");
101   
102   daddy_trans_l_->execute_single_pushpop_property (basic, glyph_sym, SCM_UNDEFINED);
103   daddy_trans_l_->execute_single_pushpop_property (basic, glyph_sym, glyph);
104 }
105
106 /** 
107   Generate a clef at the start of a measure. (when you see a Bar,
108   ie. a breakpoint) 
109   */
110 void
111 Clef_engraver::acknowledge_grob (Grob_info info)
112 {
113   Item * item =dynamic_cast <Item *> (info.elem_l_);
114   if (item)
115     {
116       if (Bar::has_interface (info.elem_l_)
117           && gh_string_p (get_property ("clefGlyph")))
118         create_clef ();
119
120       if (Key_item::has_interface (item))
121         {
122           /*
123             Key_item adapts its formatting to make sure that the
124             accidentals stay in the upper half of the staff. It needs
125             to know c0-pos for this. (?)
126           */
127
128           item->set_grob_property ("c0-position", get_property ("centralCPosition"));
129         }
130     } 
131 }
132
133 void
134 Clef_engraver::create_clef ()
135 {
136   if (!clef_p_)
137     {
138       Item *c= new Item (get_property ("Clef"));
139       announce_grob (c, 0);
140
141       Staff_symbol_referencer::set_interface (c);
142       
143       clef_p_ = c;
144     }
145   Staff_symbol_referencer::set_position (clef_p_,
146                                          gh_scm2int (get_property ("clefPosition")));
147
148   SCM oct =  get_property ("clefOctavation");
149   if (gh_number_p (oct) && gh_scm2int (oct))
150     {
151       Item * g = new Item (get_property ("OctavateEight"));
152
153       Side_position_interface::add_support (g,clef_p_);      
154
155       g->set_parent (clef_p_, Y_AXIS);
156       g->set_parent (clef_p_, X_AXIS);
157
158       g->set_grob_property ("direction", gh_int2scm (sign (gh_scm2int (oct))));
159       octavate_p_ = g;
160       announce_grob (octavate_p_, 0);
161     }
162 }
163
164 void
165 Clef_engraver::process_music ()
166 {
167   inspect_clef_properties ();
168 }
169
170 /*
171   this must be done in creation_proc() since grace notes will be
172   processed before Clef_engraver::prcoess_music()
173
174   Grace notes and clef changes are still broken.
175 */
176 void
177 Clef_engraver::do_creation_processing ()
178 {
179   inspect_clef_properties ();
180 }
181
182 void
183 Clef_engraver::inspect_clef_properties ()
184 {
185   SCM glyph = get_property ("clefGlyph");
186   SCM clefpos = get_property ("clefPosition");
187   SCM octavation = get_property ("clefOctavation");
188   SCM force_clef = get_property ("forceClef");
189   
190   if (clefpos == SCM_EOL
191       || scm_equal_p (glyph, prev_glyph_) == SCM_BOOL_F
192       || scm_equal_p (clefpos, prev_cpos_) == SCM_BOOL_F
193       || scm_equal_p (octavation, prev_octavation_) == SCM_BOOL_F
194       || to_boolean (force_clef)
195 )
196     {
197       set_glyph ();
198       set_central_c (glyph, clefpos, octavation);
199         
200       create_clef ();
201
202       clef_p_->set_grob_property ("non-default", SCM_BOOL_T);
203     }
204
205   if (to_boolean (force_clef))
206     {
207       Translator_group * w = daddy_trans_l_->where_defined (ly_symbol2scm ("forceClef"));
208       w->set_property ("forceClef", SCM_EOL);
209     }
210 }
211
212 void
213 Clef_engraver::stop_translation_timestep ()
214 {
215   if (clef_p_)
216     {
217       SCM vis = 0; 
218       if (to_boolean (clef_p_->get_grob_property ("non-default")))
219         {
220           vis = get_property ("explicitClefVisibility");
221         }
222
223       if (vis)
224         {
225           clef_p_->set_grob_property ("visibility-lambda", vis);
226           if (octavate_p_)
227             octavate_p_->set_grob_property ("visibility-lambda", vis);
228         }
229       
230       typeset_grob (clef_p_);
231       clef_p_ =0;
232
233       if (octavate_p_)
234         typeset_grob (octavate_p_);
235
236       octavate_p_ = 0;
237     }
238 }
239
240 void
241 Clef_engraver::start_translation_timestep ()
242 {
243 }
244
245 ADD_THIS_TRANSLATOR (Clef_engraver);
246