]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
release: 1.5.0
[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 "bar.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "debug.hh"
18 #include "engraver.hh"
19 #include "direction.hh"
20 #include "side-position-interface.hh"
21 #include "item.hh"
22
23
24 class Clef_engraver : public  Engraver
25 {
26 public:
27   VIRTUAL_COPY_CONS (Translator);
28   Clef_engraver ();
29
30   Direction octave_dir_;
31
32 protected:
33   virtual void stop_translation_timestep ();
34   virtual void start_translation_timestep ();
35   virtual void process_music ();
36   virtual void acknowledge_grob (Grob_info);
37   virtual void do_creation_processing ();
38 private:
39   Item * clef_p_;
40   Item * octavate_p_;
41
42   SCM prev_glyph_;
43   SCM prev_cpos_;
44   SCM prev_octavation_;
45   void create_clef ();
46   void set_glyph ();
47   void inspect_clef_properties ();
48 };
49
50 Clef_engraver::Clef_engraver ()
51 {
52   clef_p_ = 0;
53   octave_dir_ = CENTER;
54   octavate_p_ = 0;
55
56   /*
57     will trigger a clef at the start since #f != ' ()
58    */
59   prev_cpos_ = prev_glyph_ = SCM_BOOL_F;
60 }
61
62 void
63 Clef_engraver::set_glyph ()
64 {
65   SCM glyph_sym = ly_symbol2scm ("glyph-name");
66   SCM glyph = get_property ("clefGlyph");
67
68   SCM basic = ly_symbol2scm ("Clef");
69   
70   daddy_trans_l_->execute_single_pushpop_property (basic, glyph_sym, SCM_UNDEFINED);
71   daddy_trans_l_->execute_single_pushpop_property (basic, glyph_sym, glyph);
72 }
73
74 /** 
75   Generate a clef at the start of a measure. (when you see a Bar,
76   ie. a breakpoint) 
77   */
78 void
79 Clef_engraver::acknowledge_grob (Grob_info info)
80 {
81   Item * item =dynamic_cast <Item *> (info.elem_l_);
82   if (item)
83     {
84       if (Bar::has_interface (info.elem_l_)
85           && gh_string_p (get_property ("clefGlyph")))
86         create_clef ();
87
88       if (Key_item::has_interface (item))
89         {
90           /*
91             Key_item adapts its formatting to make sure that the
92             accidentals stay in the upper half of the staff. It needs
93             to know c0-pos for this. (?)
94           */
95
96           item->set_grob_property ("c0-position", get_property ("centralCPosition"));
97         }
98     } 
99 }
100
101 void
102 Clef_engraver::create_clef ()
103 {
104   if (!clef_p_)
105     {
106       Item *c= new Item (get_property ("Clef"));
107       announce_grob (c, 0);
108
109       Staff_symbol_referencer::set_interface (c);
110       
111       clef_p_ = c;
112     }
113   Staff_symbol_referencer::set_position (clef_p_,
114                                          gh_scm2int (get_property ("clefPosition")));
115
116   SCM oct =  get_property ("clefOctavation");
117   if (gh_number_p (oct) && gh_scm2int (oct))
118     {
119       Item * g = new Item (get_property ("OctavateEight"));
120
121       Side_position_interface::add_support (g,clef_p_);      
122
123       g->set_parent (clef_p_, Y_AXIS);
124       g->set_parent (clef_p_, X_AXIS);
125
126       g->set_grob_property ("direction", gh_int2scm (sign (gh_scm2int (oct))));
127       octavate_p_ = g;
128       announce_grob (octavate_p_, 0);
129     }
130 }
131
132 void
133 Clef_engraver::process_music ()
134 {
135   inspect_clef_properties ();
136 }
137
138 /*
139   this must be done in creation_proc() since grace notes will be
140   processed before Clef_engraver::prcoess_music()
141
142   Grace notes and clef changes are still broken.
143 */
144 void
145 Clef_engraver::do_creation_processing ()
146 {
147   inspect_clef_properties ();
148 }
149
150 void
151 Clef_engraver::inspect_clef_properties ()
152 {
153   SCM glyph = get_property ("clefGlyph");
154   SCM clefpos = get_property ("clefPosition");
155   SCM octavation = get_property ("clefOctavation");
156   SCM force_clef = get_property ("forceClef");
157   
158   if (clefpos == SCM_EOL
159       || scm_equal_p (glyph, prev_glyph_) == SCM_BOOL_F
160       || scm_equal_p (clefpos, prev_cpos_) == SCM_BOOL_F
161       || scm_equal_p (octavation, prev_octavation_) == SCM_BOOL_F
162       || to_boolean (force_clef)
163 )
164     {
165       set_glyph ();
166       create_clef ();
167
168       clef_p_->set_grob_property ("non-default", SCM_BOOL_T);
169
170       prev_cpos_ = clefpos;
171       prev_glyph_ = glyph;
172       prev_octavation_ = octavation;
173     }
174
175   if (to_boolean (force_clef))
176     {
177       Translator_group * w = daddy_trans_l_->where_defined (ly_symbol2scm ("forceClef"));
178       w->set_property ("forceClef", SCM_EOL);
179     }
180 }
181
182
183 void
184 Clef_engraver::stop_translation_timestep ()
185 {
186   if (clef_p_)
187     {
188       SCM vis = 0; 
189       if (to_boolean (clef_p_->get_grob_property ("non-default")))
190         {
191           vis = get_property ("explicitClefVisibility");
192         }
193
194       if (vis)
195         {
196           clef_p_->set_grob_property ("visibility-lambda", vis);
197           if (octavate_p_)
198             octavate_p_->set_grob_property ("visibility-lambda", vis);
199         }
200       
201       typeset_grob (clef_p_);
202       clef_p_ =0;
203
204       if (octavate_p_)
205         typeset_grob (octavate_p_);
206
207       octavate_p_ = 0;
208     }
209 }
210
211 void
212 Clef_engraver::start_translation_timestep ()
213 {
214 }
215
216 ADD_THIS_TRANSLATOR (Clef_engraver);
217