]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
release: 1.3.131
[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 "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 stop_translation_timestep ();
37   virtual void start_translation_timestep ();
38   virtual void create_grobs ();
39   virtual void acknowledge_grob (Grob_info);
40
41 private:
42   Item * clef_p_;
43   Item * octavate_p_;
44
45   SCM prev_glyph_;
46   SCM prev_cpos_;
47   SCM prev_octavation_;
48   void create_clef ();
49   void set_central_c (SCM, SCM, SCM);
50   void set_glyph ();
51 };
52
53 Clef_engraver::Clef_engraver ()
54 {
55   clef_p_ = 0;
56   octave_dir_ = CENTER;
57   octavate_p_ = 0;
58
59   /*
60     will trigger a clef at the start since #f != '()
61    */
62   prev_cpos_ = prev_glyph_ = SCM_BOOL_F;
63 }
64
65 void
66 Clef_engraver::set_central_c (SCM glyph,SCM clefpos, SCM octavation)
67 {
68   prev_cpos_ = clefpos;
69   prev_glyph_ = glyph;
70   prev_octavation_ = octavation;
71
72   SCM p = get_property ("clefPitches");
73   int c0_position =  0;
74   if (gh_list_p (p))
75     {
76       SCM found = scm_assoc (glyph, p);
77       if (found == SCM_BOOL_F)
78         {
79           c0_position =0;
80         }
81       else
82         {
83           c0_position =  gh_scm2int (gh_cdr (found));
84
85           if (gh_number_p (octavation))
86               c0_position -= gh_scm2int (octavation);
87       
88           if (gh_number_p (clefpos))
89             c0_position += gh_scm2int (clefpos);
90         }
91       
92     }
93   daddy_trans_l_->set_property ("centralCPosition", gh_int2scm (c0_position));
94 }
95
96 void
97 Clef_engraver::set_glyph ()
98 {
99   SCM glyph_sym = ly_symbol2scm ("glyph-name");
100   SCM glyph = get_property ("clefGlyph");
101
102   SCM basic = ly_symbol2scm ("Clef");
103   
104   daddy_trans_l_->execute_single_pushpop_property (basic, glyph_sym, SCM_UNDEFINED);
105   daddy_trans_l_->execute_single_pushpop_property (basic, glyph_sym, glyph);
106 }
107
108 /** 
109   Generate a clef at the start of a measure. (when you see a Bar,
110   ie. a breakpoint) 
111   */
112 void
113 Clef_engraver::acknowledge_grob (Grob_info info)
114 {
115   create_grobs ();
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_grob_property ("c0-position", get_property ("centralCPosition"));
142         }
143     } 
144 }
145
146 void
147 Clef_engraver::create_clef ()
148 {
149   if (!clef_p_)
150     {
151       Item *c= new Item (get_property ("Clef"));
152       announce_grob (c, 0);
153
154       Staff_symbol_referencer::set_interface (c);
155       
156       clef_p_ = c;
157     }
158   Staff_symbol_referencer::set_position (clef_p_,
159                                          gh_scm2int (get_property ("clefPosition")));
160
161   SCM oct =  get_property("clefOctavation");
162   if (gh_number_p (oct) && gh_scm2int (oct))
163     {
164       Item * g = new Item (get_property ("OctavateEight"));
165
166       Side_position::add_support (g,clef_p_);      
167
168       g->set_parent (clef_p_, Y_AXIS);
169       g->set_parent (clef_p_, X_AXIS);
170
171       g->set_grob_property ("direction", gh_int2scm (sign (gh_scm2int (oct))));
172       octavate_p_ = g;
173       announce_grob (octavate_p_, 0);
174     }
175 }
176
177 void
178 Clef_engraver::create_grobs ()
179 {
180   SCM glyph = get_property ("clefGlyph");
181   SCM clefpos = get_property ("clefPosition");
182   SCM octavation = get_property ("clefOctavation");
183   SCM force_clef = get_property ("forceClef");
184   
185   if (clefpos == SCM_EOL
186       || scm_equal_p (glyph, prev_glyph_) == SCM_BOOL_F
187       || scm_equal_p (clefpos, prev_cpos_) == SCM_BOOL_F
188       || scm_equal_p (octavation, prev_octavation_) == SCM_BOOL_F
189       || to_boolean (force_clef)
190       )
191     {
192       set_glyph();
193       set_central_c (glyph, clefpos, octavation);
194         
195       create_clef ();
196
197       clef_p_->set_grob_property ("non-default", SCM_BOOL_T);
198     }
199
200   if (to_boolean (force_clef))
201     {
202       Translator_group * w = daddy_trans_l_->where_defined (ly_symbol2scm ("forceClef"));
203       w->set_property ("forceClef", SCM_EOL);
204     }
205 }
206
207 void
208 Clef_engraver::stop_translation_timestep ()
209 {
210   if (clef_p_)
211     {
212       SCM vis = 0; 
213       if (to_boolean (clef_p_->get_grob_property ("non-default")))
214         {
215           vis = get_property ("explicitClefVisibility");
216         }
217
218       if (vis)
219         {
220           clef_p_->set_grob_property ("visibility-lambda", vis);
221           if (octavate_p_)
222             octavate_p_->set_grob_property ("visibility-lambda", vis);
223         }
224       
225       typeset_grob (clef_p_);
226       clef_p_ =0;
227
228       if (octavate_p_)
229         typeset_grob (octavate_p_);
230
231       octavate_p_ = 0;
232     }
233 }
234
235 void
236 Clef_engraver::start_translation_timestep ()
237 {
238 }
239
240 ADD_THIS_TRANSLATOR (Clef_engraver);
241