]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
release: 1.3.145
[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 /// where is c-0 in the staff?
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
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 };
49
50 Clef_engraver::Clef_engraver ()
51 {
52   clef_p_ = 0;
53   octave_dir_ = CENTER;
54   octavate_p_ = 0;
55
56   /*
57     will trigger a clef at the start since #f != ' ()
58    */
59   prev_cpos_ = prev_glyph_ = SCM_BOOL_F;
60 }
61
62 void
63 Clef_engraver::set_central_c (SCM glyph,SCM clefpos, SCM octavation)
64 {
65   prev_cpos_ = clefpos;
66   prev_glyph_ = glyph;
67   prev_octavation_ = octavation;
68
69   SCM p = get_property ("clefPitches");
70   int c0_position =  0;
71   if (gh_list_p (p))
72     {
73       SCM found = scm_assoc (glyph, p);
74       if (found == SCM_BOOL_F)
75         {
76           c0_position =0;
77         }
78       else
79         {
80           c0_position =  gh_scm2int (gh_cdr (found));
81
82           if (gh_number_p (octavation))
83               c0_position -= gh_scm2int (octavation);
84       
85           if (gh_number_p (clefpos))
86             c0_position += gh_scm2int (clefpos);
87         }
88       
89     }
90   daddy_trans_l_->set_property ("centralCPosition", gh_int2scm (c0_position));
91 }
92
93 void
94 Clef_engraver::set_glyph ()
95 {
96   SCM glyph_sym = ly_symbol2scm ("glyph-name");
97   SCM glyph = get_property ("clefGlyph");
98
99   SCM basic = ly_symbol2scm ("Clef");
100   
101   daddy_trans_l_->execute_single_pushpop_property (basic, glyph_sym, SCM_UNDEFINED);
102   daddy_trans_l_->execute_single_pushpop_property (basic, glyph_sym, glyph);
103 }
104
105 /** 
106   Generate a clef at the start of a measure. (when you see a Bar,
107   ie. a breakpoint) 
108   */
109 void
110 Clef_engraver::acknowledge_grob (Grob_info info)
111 {
112   Item * item =dynamic_cast <Item *> (info.elem_l_);
113   if (item)
114     {
115       if (Bar::has_interface (info.elem_l_)
116           && gh_string_p (get_property ("clefGlyph")))
117         create_clef ();
118
119       if (Key_item::has_interface (item))
120         {
121           /*
122             Key_item adapts its formatting to make sure that the
123             accidentals stay in the upper half of the staff. It needs
124             to know c0-pos for this. (?)
125           */
126
127           item->set_grob_property ("c0-position", get_property ("centralCPosition"));
128         }
129     } 
130 }
131
132 void
133 Clef_engraver::create_clef ()
134 {
135   if (!clef_p_)
136     {
137       Item *c= new Item (get_property ("Clef"));
138       announce_grob (c, 0);
139
140       Staff_symbol_referencer::set_interface (c);
141       
142       clef_p_ = c;
143     }
144   Staff_symbol_referencer::set_position (clef_p_,
145                                          gh_scm2int (get_property ("clefPosition")));
146
147   SCM oct =  get_property ("clefOctavation");
148   if (gh_number_p (oct) && gh_scm2int (oct))
149     {
150       Item * g = new Item (get_property ("OctavateEight"));
151
152       Side_position_interface::add_support (g,clef_p_);      
153
154       g->set_parent (clef_p_, Y_AXIS);
155       g->set_parent (clef_p_, X_AXIS);
156
157       g->set_grob_property ("direction", gh_int2scm (sign (gh_scm2int (oct))));
158       octavate_p_ = g;
159       announce_grob (octavate_p_, 0);
160     }
161 }
162
163 void
164 Clef_engraver::process_music ()
165 {
166   SCM glyph = get_property ("clefGlyph");
167   SCM clefpos = get_property ("clefPosition");
168   SCM octavation = get_property ("clefOctavation");
169   SCM force_clef = get_property ("forceClef");
170   
171   if (clefpos == SCM_EOL
172       || scm_equal_p (glyph, prev_glyph_) == SCM_BOOL_F
173       || scm_equal_p (clefpos, prev_cpos_) == SCM_BOOL_F
174       || scm_equal_p (octavation, prev_octavation_) == SCM_BOOL_F
175       || to_boolean (force_clef)
176 )
177     {
178       set_glyph ();
179       set_central_c (glyph, clefpos, octavation);
180         
181       create_clef ();
182
183       clef_p_->set_grob_property ("non-default", SCM_BOOL_T);
184     }
185
186   if (to_boolean (force_clef))
187     {
188       Translator_group * w = daddy_trans_l_->where_defined (ly_symbol2scm ("forceClef"));
189       w->set_property ("forceClef", SCM_EOL);
190     }
191 }
192
193 void
194 Clef_engraver::stop_translation_timestep ()
195 {
196   if (clef_p_)
197     {
198       SCM vis = 0; 
199       if (to_boolean (clef_p_->get_grob_property ("non-default")))
200         {
201           vis = get_property ("explicitClefVisibility");
202         }
203
204       if (vis)
205         {
206           clef_p_->set_grob_property ("visibility-lambda", vis);
207           if (octavate_p_)
208             octavate_p_->set_grob_property ("visibility-lambda", vis);
209         }
210       
211       typeset_grob (clef_p_);
212       clef_p_ =0;
213
214       if (octavate_p_)
215         typeset_grob (octavate_p_);
216
217       octavate_p_ = 0;
218     }
219 }
220
221 void
222 Clef_engraver::start_translation_timestep ()
223 {
224 }
225
226 ADD_THIS_TRANSLATOR (Clef_engraver);
227