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