]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
lilypond-1.3.107
[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--2000 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 "local-key-item.hh"
16 #include "bar.hh"
17 #include "note-head.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "debug.hh"
20 #include "engraver.hh"
21 #include "direction.hh"
22 #include "side-position-interface.hh"
23 #include "item.hh"
24 #include "custos.hh"
25
26 /// where is c-0 in the staff?
27 class Clef_engraver : public  Engraver
28 {
29 public:
30   VIRTUAL_COPY_CONS (Translator);
31   Clef_engraver ();
32
33   Direction octave_dir_;
34
35 protected:
36   virtual void do_process_music ();
37   virtual void do_pre_move_processing ();
38   virtual void do_creation_processing ();
39   virtual void do_post_move_processing ();
40   virtual void acknowledge_element (Score_element_info);
41
42 private:
43   Item * clef_p_;
44   Item * octavate_p_;
45
46   SCM prev_glyph_;
47   SCM prev_cpos_;
48   SCM prev_octavation_;
49   void create_clef ();
50   void set_central_c (SCM, SCM, SCM);
51   void set_glyph ();
52 };
53
54 Clef_engraver::Clef_engraver ()
55 {
56   clef_p_ = 0;
57   octave_dir_ = CENTER;
58   octavate_p_ = 0;
59
60   /*
61     will trigger a clef at the start since #f != '()
62    */
63   prev_cpos_ = prev_glyph_ = SCM_BOOL_F;
64 }
65
66 void
67 Clef_engraver::set_central_c (SCM glyph,SCM clefpos, SCM octavation)
68 {
69   prev_cpos_ = clefpos;
70   prev_glyph_ = glyph;
71   prev_octavation_ = octavation;
72
73   SCM p = get_property ("clefPitches");
74   int c0_position =  0;
75   if (gh_list_p (p))
76     {
77       SCM found = scm_assoc (glyph, p);
78       if (found == SCM_BOOL_F)
79         {
80           c0_position =0;
81         }
82       else
83         {
84           c0_position =  gh_scm2int (gh_cdr (found));
85
86           if (gh_number_p (octavation))
87               c0_position -= gh_scm2int (octavation);
88       
89           if (gh_number_p (clefpos))
90             c0_position += gh_scm2int (clefpos);
91         }
92       
93     }
94   daddy_trans_l_->set_property ("centralCPosition", gh_int2scm (c0_position));
95 }
96
97 void
98 Clef_engraver::set_glyph ()
99 {
100   SCM glyph_sym = ly_symbol2scm ("glyph");
101   SCM glyph = get_property ("clefGlyph");
102
103   SCM basic = ly_symbol2scm ("Clef");
104   
105   daddy_trans_l_->execute_single_pushpop_property (basic, glyph_sym, SCM_UNDEFINED);
106   daddy_trans_l_->execute_single_pushpop_property (basic, glyph_sym, glyph);
107 }
108
109 /** 
110   Generate a clef at the start of a measure. (when you see a Bar,
111   ie. a breakpoint) 
112   */
113 void
114 Clef_engraver::acknowledge_element (Score_element_info info)
115 {
116   Item * item =dynamic_cast <Item *> (info.elem_l_);
117   if (item)
118     {
119       if (Bar::has_interface (info.elem_l_)
120           && gh_string_p (get_property ("clefGlyph")))
121         create_clef ();
122       
123
124       if (Note_head::has_interface (item)
125           || Local_key_item::has_interface (item)
126           || Custos::has_interface (item)
127           )
128         {
129           int p = int (Staff_symbol_referencer::position_f (item))
130             + gh_scm2int (get_property ("centralCPosition"));
131           Staff_symbol_referencer::set_position (item, p);
132         }
133       else if (Key_item::has_interface (item))
134         {
135           /*
136             Key_item adapts its formatting to make sure that the
137             accidentals stay in the upper half of the staff. It needs
138             to know c0-pos for this.  (?)
139           */
140
141           item->set_elt_property ("c0-position", get_property ("centralCPosition"));
142         }
143     } 
144 }
145
146 void
147 Clef_engraver::do_creation_processing ()
148 {
149 }
150
151
152
153 void
154 Clef_engraver::create_clef ()
155 {
156   if (!clef_p_)
157     {
158       Item *c= new Item (get_property ("Clef"));
159       announce_element (c, 0);
160
161       Staff_symbol_referencer::set_interface (c);
162       
163       clef_p_ = c;
164     }
165   Staff_symbol_referencer::set_position (clef_p_,
166                                          gh_scm2int (get_property ("clefPosition")));
167
168   SCM oct =  get_property("clefOctavation");
169   if (gh_number_p (oct) && gh_scm2int (oct))
170     {
171       Item * g = new Item (get_property ("OctavateEight"));
172
173       Side_position::add_support (g,clef_p_);      
174
175       g->set_parent (clef_p_, Y_AXIS);
176       g->set_parent (clef_p_, X_AXIS);
177
178       g->set_elt_property ("direction", gh_int2scm (sign (gh_scm2int (oct))));
179       octavate_p_ = g;
180       announce_element (octavate_p_, 0);
181     }
182 }
183
184 void
185 Clef_engraver::do_process_music ()
186 {
187   SCM glyph = get_property ("clefGlyph");
188   SCM clefpos = get_property ("clefPosition");
189   SCM octavation = get_property ("clefOctavation");
190   
191   if (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     {
195       set_glyph();
196       set_central_c (glyph, clefpos, octavation);
197         
198       create_clef ();
199
200       clef_p_->set_elt_property ("non-default", SCM_BOOL_T);
201     }
202 }
203
204 void
205 Clef_engraver::do_pre_move_processing ()
206 {
207   if (clef_p_)
208     {
209       SCM vis = 0; 
210       if (to_boolean (clef_p_->get_elt_property ("non-default")))
211         {
212           vis = get_property ("explicitClefVisibility");
213         }
214
215       if (vis)
216         {
217           clef_p_->set_elt_property ("visibility-lambda", vis);
218           if (octavate_p_)
219             octavate_p_->set_elt_property ("visibility-lambda", vis);
220         }
221       
222       typeset_element (clef_p_);
223       clef_p_ =0;
224
225       if (octavate_p_)
226         typeset_element (octavate_p_);
227
228       octavate_p_ = 0;
229     }
230 }
231
232 void
233 Clef_engraver::do_post_move_processing ()
234 {
235 }
236
237 ADD_THIS_TRANSLATOR (Clef_engraver);
238