]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
release: 1.3.55
[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 "staff-symbol-referencer.hh"
14 #include "bar.hh"
15 #include "clef-item.hh"
16 #include "debug.hh"
17 #include "command-request.hh"
18 #include "timing-translator.hh"
19 #include "note-head.hh"
20 #include "key-item.hh"
21 #include "local-key-item.hh"
22 #include "array.hh"
23 #include "engraver.hh"
24 #include "direction.hh"
25 #include "side-position-interface.hh"
26 #include "text-item.hh"
27
28 /// where is c-0 in the staff?
29 class Clef_engraver : public  Engraver {
30   Clef_item * clef_p_;
31   Text_item * octavate_p_;
32   Clef_change_req * clef_req_l_;
33   
34   void create_clef();
35   bool set_type (String);
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 bool do_try_music (Music*);
42   virtual void acknowledge_element (Score_element_info);
43 public:
44   VIRTUAL_COPY_CONS(Translator);
45   int c0_position_i_;
46   int clef_position_i_;
47                                 // junkme.
48   Direction octave_dir_;
49   SCM clef_glyph_;              // no need for protection. Always referenced somewhere else.
50    
51   Clef_engraver();
52
53   SCM basic_properties_;
54   Protected_scm current_settings_;
55 };
56
57
58 Clef_engraver::Clef_engraver()
59 {
60   current_settings_ = SCM_EOL;
61   basic_properties_ = SCM_EOL;
62   
63   clef_glyph_ = SCM_EOL;
64   clef_p_ = 0;
65   clef_req_l_ = 0;
66   c0_position_i_ = 0;
67   clef_position_i_ = 0;
68   octave_dir_ = CENTER;
69   octavate_p_ = 0;
70 }
71
72 bool
73 Clef_engraver::set_type (String s)
74 {
75   if (s.right_str(2) == "_8") // Down one octave
76     {
77       octave_dir_ = DOWN;
78       s = s.left_str(s.length_i() - 2);
79     }
80   else if (s.right_str(2) == "^8") // Up one octave
81     {
82       octave_dir_ = UP;
83       s = s.left_str(s.length_i() - 2);
84     }
85   else
86     octave_dir_ = CENTER;
87
88   SCM c = get_property ("supportedClefTypes");
89   SCM p = get_property ("clefPitches");
90   
91   if (gh_list_p (c))
92     {
93       SCM found = scm_assoc (ly_str02scm (s.ch_C()), c);
94       if (found == SCM_BOOL_F)
95         return false;
96       
97       clef_glyph_  = gh_cadr (found);
98       SCM pos  = gh_caddr (found);
99
100       clef_position_i_ = gh_scm2int (pos);
101
102       found = scm_assoc (clef_glyph_, p);
103       if (found == SCM_BOOL_F)
104         return false;
105
106       c0_position_i_ = clef_position_i_ + gh_scm2int (gh_cdr (found));
107     }
108
109   c0_position_i_ -= (int) octave_dir_ * 7;
110
111
112   current_settings_ = gh_cons (gh_cons (ly_symbol2scm ("glyph"), clef_glyph_), basic_properties_);
113   current_settings_ =
114     gh_cons (gh_cons (ly_symbol2scm ("c0-position"),
115                       gh_int2scm (c0_position_i_)),
116              current_settings_);
117   
118   return true;
119 }
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   if (dynamic_cast<Bar*>(info.elem_l_)
130       && gh_string_p (clef_glyph_))
131     create_clef();
132
133   /* ugh; should make Clef_referenced baseclass */
134   Item * it_l =dynamic_cast <Item *> (info.elem_l_);
135   if (it_l)
136     {
137       if (dynamic_cast<Note_head*>(it_l)
138           || dynamic_cast<Local_key_item*> (it_l)
139           )
140           
141         {
142           Staff_symbol_referencer_interface si (it_l);
143           si.set_position (int (si.position_f ()) + c0_position_i_);
144         }
145       else if (Key_item *k = dynamic_cast<Key_item*>(it_l))
146         {
147           k->set_elt_property ("c0-position", gh_int2scm (c0_position_i_));
148         }
149     } 
150 }
151
152 void
153 Clef_engraver::do_creation_processing()
154 {
155   basic_properties_ = get_property ("basicClefItemProperties");
156   
157   SCM def = get_property ("defaultClef");
158   if (gh_string_p (def))
159     {
160       set_type (ly_scm2string (def));
161       create_clef ();
162       clef_p_->set_elt_property ("non-default", SCM_BOOL_T);
163     }
164 }
165
166 bool
167 Clef_engraver::do_try_music (Music * r_l)
168 {
169   if (Clef_change_req *cl = dynamic_cast <Clef_change_req *> (r_l))
170     {
171       clef_req_l_ = cl;
172       if (!set_type (cl->clef_str_))
173         cl->error (_ ("unknown clef type"));
174
175       return true;
176     }
177   else
178     return false;
179
180 }
181
182 void
183 Clef_engraver::create_clef()
184 {
185   if (!clef_p_)
186     {
187       Clef_item *c= new Clef_item ( current_settings_);
188       announce_element (Score_element_info (c, clef_req_l_));
189
190       Staff_symbol_referencer_interface si(c);
191       si.set_interface ();
192       
193       clef_p_ = c;
194     }
195   Staff_symbol_referencer_interface si(clef_p_);
196   clef_p_->set_elt_property ("glyph", clef_glyph_);
197   si.set_position (clef_position_i_);
198   if (octave_dir_)
199     {
200       Text_item * g = new Text_item (get_property ("basicOctavateEightProperties"));
201       Side_position_interface spi (g);
202       spi.set_axis (Y_AXIS);
203       spi.add_support (clef_p_);
204       g->set_parent (clef_p_, Y_AXIS);
205       g->set_parent (clef_p_, X_AXIS);
206       g->add_offset_callback (Side_position_interface::aligned_on_self, X_AXIS);
207       g->add_offset_callback (Side_position_interface::centered_on_parent, X_AXIS);
208       g->set_elt_property ("direction", gh_int2scm (octave_dir_));
209       octavate_p_ = g;
210       announce_element (Score_element_info (octavate_p_, clef_req_l_));
211     }
212 }
213
214
215 void
216 Clef_engraver::do_process_music()
217 {
218   if (clef_req_l_)
219     {
220       create_clef();
221       clef_p_->set_elt_property ("non-default", SCM_BOOL_T);
222     }
223 }
224
225 void
226 Clef_engraver::do_pre_move_processing()
227 {
228   if (clef_p_)
229     {
230       SCM vis = 0; 
231       if(to_boolean (clef_p_->get_elt_property("non-default")))
232         {
233           vis = ly_symbol2scm ("all-visible");
234           vis = scm_eval (vis);
235         }
236
237       if (vis)
238         {
239           clef_p_->set_elt_property("visibility-lambda", vis);
240           if (octavate_p_)
241             octavate_p_->set_elt_property("visibility-lambda", vis);
242         }
243       
244       typeset_element (clef_p_);
245       clef_p_ =0;
246
247       if (octavate_p_)
248         typeset_element(octavate_p_);
249
250       octavate_p_ = 0;
251     }
252 }
253
254 void
255 Clef_engraver::do_post_move_processing()
256 {
257   clef_req_l_ = 0;
258 }
259
260 ADD_THIS_TRANSLATOR(Clef_engraver);
261