]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
lilypond-1.3.96
[lilypond.git] / lily / clef-engraver.cc
1 /*
2
3   clef-engraver.cc -- implement Clef_engraver
4
5   source file of the GNU LilyPond music typesetter
6
7   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>,
8
9   Mats Bengtsson <matsb@s3.kth.se>
10 */
11
12 #include <ctype.h>
13
14 #include "translator-group.hh"
15 #include "key-item.hh"
16 #include "local-key-item.hh"
17 #include "bar.hh"
18 #include "note-head.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "debug.hh"
21 #include "command-request.hh"
22 #include "engraver.hh"
23 #include "direction.hh"
24 #include "side-position-interface.hh"
25 #include "item.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   bool  first_b_;
36
37 protected:
38   virtual void do_process_music ();
39   virtual void do_pre_move_processing ();
40   virtual void do_creation_processing ();
41   virtual void do_post_move_processing ();
42   virtual bool do_try_music (Music*);
43   virtual void acknowledge_element (Score_element_info);
44
45 private:
46   Item * clef_p_;
47   Item * octavate_p_;
48   Clef_change_req * clef_req_l_;
49   
50   void create_clef ();
51   bool set_type (String);
52 };
53
54
55 Clef_engraver::Clef_engraver ()
56 {
57   first_b_ = true;
58   clef_p_ = 0;
59   clef_req_l_ = 0;
60   octave_dir_ = CENTER;
61   octavate_p_ = 0;
62 }
63
64 bool
65 Clef_engraver::set_type (String s)
66 {
67   if (s.right_str (2) == "_8") // Down one octave
68     {
69       octave_dir_ = DOWN;
70       s = s.left_str (s.length_i () - 2);
71     }
72   else if (s.right_str (2) == "^8") // Up one octave
73     {
74       octave_dir_ = UP;
75       s = s.left_str (s.length_i () - 2);
76     }
77   else
78     octave_dir_ = CENTER;
79
80   SCM c = get_property ("supportedClefTypes");
81   SCM p = get_property ("clefPitches");
82   
83   if (gh_list_p (c))
84     {
85       SCM found = scm_assoc (ly_str02scm (s.ch_C ()), c);
86       if (found == SCM_BOOL_F)
87         return false;
88       
89       SCM glyph = gh_cadr (found);
90       SCM pos = gh_caddr (found);
91
92       daddy_trans_l_->set_property ("glyph", glyph);
93       daddy_trans_l_->set_property ("position", pos);
94
95       found = scm_assoc (glyph, p);
96       if (found == SCM_BOOL_F)
97         return false;
98
99       int c0_position = gh_scm2int (pos) + gh_scm2int (gh_cdr (found));
100       daddy_trans_l_->set_property ("c0-position", gh_int2scm (c0_position));
101     }
102
103   int c0_position = gh_scm2int (get_property ("c0-position"));
104   c0_position -= (int)octave_dir_ * 7;
105   daddy_trans_l_->set_property ("c0-position", gh_int2scm (c0_position));
106
107
108   SCM basic = ly_symbol2scm ("Clef");
109   SCM c0 = ly_symbol2scm ("c0-position");
110   SCM gl = ly_symbol2scm ("glyph");
111
112   daddy_trans_l_->execute_single_pushpop_property (basic, gl, SCM_UNDEFINED);
113   daddy_trans_l_->execute_single_pushpop_property (basic, c0, SCM_UNDEFINED);  
114   daddy_trans_l_->execute_single_pushpop_property (basic, gl,
115                                                    get_property ("glyph"));
116   daddy_trans_l_->execute_single_pushpop_property (basic, c0,
117                                                    get_property ("c0_position")
118                                                    );
119   return true;
120 }
121
122 /** 
123   Generate a clef at the start of a measure. (when you see a Bar,
124   ie. a breakpoint) 
125   */
126 void
127 Clef_engraver::acknowledge_element (Score_element_info info)
128 {
129   Item * item =dynamic_cast <Item *> (info.elem_l_);
130   if (item)
131     {
132       if (Bar::has_interface (info.elem_l_)
133           && gh_string_p (get_property ("glyph")))
134         create_clef ();
135       
136
137       if (Note_head::has_interface (item)
138           || Local_key_item::has_interface (item))
139         {
140           int p = int (Staff_symbol_referencer::position_f (item))
141             + gh_scm2int (get_property ("c0-position"));
142           Staff_symbol_referencer::set_position (item, p);
143         }
144       else if (Key_item::has_interface (item))
145         {
146           item->set_elt_property ("c0-position", get_property ("c0-position"));
147         }
148     } 
149 }
150
151 void
152 Clef_engraver::do_creation_processing ()
153 {
154   daddy_trans_l_->set_property ("position", gh_int2scm (0));
155   daddy_trans_l_->set_property ("glyph", SCM_EOL);
156   daddy_trans_l_->set_property ("c0-position", gh_int2scm (0));
157
158   SCM def = get_property ("defaultClef");
159   if (gh_string_p (def))
160     {
161       set_type (ly_scm2string (def));
162     }
163 }
164
165 bool
166 Clef_engraver::do_try_music (Music * r_l)
167 {
168   if (Clef_change_req *cl = dynamic_cast <Clef_change_req *> (r_l))
169     {
170       clef_req_l_ = cl;
171       if (!set_type (cl->clef_str_))
172         cl->origin ()->warning (_ ("unknown clef type"));
173
174       return true;
175     }
176   return false;
177 }
178
179 void
180 Clef_engraver::create_clef ()
181 {
182   if (!clef_p_)
183     {
184       Item *c= new Item (get_property ("Clef"));
185       announce_element (c, clef_req_l_);
186
187       Staff_symbol_referencer::set_interface (c);
188       
189       clef_p_ = c;
190     }
191   Staff_symbol_referencer::set_position (clef_p_,
192                                          gh_scm2int (get_property ("position")
193                                                      ));
194   if (octave_dir_)
195     {
196       Item * g = new Item (get_property ("OctavateEight"));
197
198       Side_position::add_support (g,clef_p_);      
199
200       g->set_parent (clef_p_, Y_AXIS);
201       g->set_parent (clef_p_, X_AXIS);
202
203       g->set_elt_property ("direction", gh_int2scm (octave_dir_));
204       octavate_p_ = g;
205       announce_element (octavate_p_, clef_req_l_);
206     }
207 }
208
209 void
210 Clef_engraver::do_process_music ()
211 {
212   if (clef_req_l_ || first_b_)
213     {
214       create_clef ();
215       clef_p_->set_elt_property ("non-default", SCM_BOOL_T);
216     }
217 }
218
219 void
220 Clef_engraver::do_pre_move_processing ()
221 {
222   if (clef_p_)
223     {
224       SCM vis = 0; 
225       if (to_boolean (clef_p_->get_elt_property ("non-default")))
226         {
227           vis = get_property ("explicitClefVisibility");
228         }
229
230       if (vis)
231         {
232           clef_p_->set_elt_property ("visibility-lambda", vis);
233           if (octavate_p_)
234             octavate_p_->set_elt_property ("visibility-lambda", vis);
235         }
236       
237       typeset_element (clef_p_);
238       clef_p_ =0;
239
240       if (octavate_p_)
241         typeset_element (octavate_p_);
242
243       octavate_p_ = 0;
244     }
245
246   first_b_ = 0;
247 }
248
249 void
250 Clef_engraver::do_post_move_processing ()
251 {
252   clef_req_l_ = 0;
253 }
254
255 ADD_THIS_TRANSLATOR (Clef_engraver);
256