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