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