]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
Run `make grand-replace'.
[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--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>,
7
8   Mats Bengtsson <matsb@s3.kth.se>
9 */
10
11 #include <cctype>
12 using namespace std;
13
14 #include "item.hh"
15 #include "context.hh"
16 #include "bar-line.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "engraver.hh"
19 #include "direction.hh"
20 #include "side-position-interface.hh"
21
22 #include "translator.icc"
23
24 class Clef_engraver : public Engraver
25 {
26 public:
27   TRANSLATOR_DECLARATIONS (Clef_engraver);
28
29   Direction octave_dir_;
30
31 protected:
32   void stop_translation_timestep ();
33   void process_music ();
34   DECLARE_ACKNOWLEDGER (bar_line);
35
36   virtual void derived_mark () const;
37 private:
38   Item *clef_;
39   Item *octavate_;
40
41   SCM prev_glyph_;
42   SCM prev_cpos_;
43   SCM prev_octavation_;
44   void create_clef ();
45   void set_glyph ();
46   void inspect_clef_properties ();
47 };
48
49 void
50 Clef_engraver::derived_mark () const
51 {
52   scm_gc_mark (prev_octavation_);
53   scm_gc_mark (prev_cpos_);
54   scm_gc_mark (prev_glyph_);
55 }
56
57 Clef_engraver::Clef_engraver ()
58 {
59   clef_ = 0;
60   octave_dir_ = CENTER;
61   octavate_ = 0;
62
63   /*
64     will trigger a clef at the start since #f != ' ()
65   */
66   prev_octavation_ = prev_cpos_ = prev_glyph_ = SCM_BOOL_F;
67 }
68
69 void
70 Clef_engraver::set_glyph ()
71 {
72   SCM glyph_sym = ly_symbol2scm ("glyph");
73   SCM basic = ly_symbol2scm ("Clef");
74
75   execute_pushpop_property (context (), basic, glyph_sym, SCM_UNDEFINED);
76   execute_pushpop_property (context (), basic, glyph_sym, get_property ("clefGlyph"));
77 }
78
79 /**
80    Generate a clef at the start of a measure. (when you see a Bar,
81    ie. a breakpoint)
82 */
83 void
84 Clef_engraver::acknowledge_bar_line (Grob_info info)
85 {
86   Item *item = dynamic_cast<Item *> (info.grob ());
87   if (item && scm_is_string (get_property ("clefGlyph")))
88     create_clef ();
89 }
90
91 void
92 Clef_engraver::create_clef ()
93 {
94   if (!clef_)
95     {
96       Item *c = make_item ("Clef", SCM_EOL);
97
98       clef_ = c;
99       SCM cpos = get_property ("clefPosition");
100
101       if (scm_is_number (cpos))
102         clef_->set_property ("staff-position", cpos);
103
104       SCM oct = get_property ("clefOctavation");
105       if (scm_is_number (oct) && scm_to_int (oct))
106         {
107           Item *g = make_item ("OctavateEight", SCM_EOL);
108
109           int abs_oct = scm_to_int (oct);
110           int dir = sign (abs_oct);
111           abs_oct = abs (abs_oct) + 1;
112
113           SCM txt = scm_number_to_string (scm_from_int (abs_oct),
114                                           scm_from_int (10));
115
116           g->set_property ("text",
117                            scm_list_n (ly_lily_module_constant ("vcenter-markup"),
118                                        txt, SCM_UNDEFINED));
119           Side_position_interface::add_support (g, clef_);
120
121           g->set_parent (clef_, Y_AXIS);
122           g->set_parent (clef_, X_AXIS);
123           g->set_property ("direction", scm_from_int (dir));
124           octavate_ = g;
125         }
126     }
127 }
128 void
129 Clef_engraver::process_music ()
130 {
131   inspect_clef_properties ();
132 }
133
134 void
135 Clef_engraver::inspect_clef_properties ()
136 {
137   SCM glyph = get_property ("clefGlyph");
138   SCM clefpos = get_property ("clefPosition");
139   SCM octavation = get_property ("clefOctavation");
140   SCM force_clef = get_property ("forceClef");
141
142   if (clefpos == SCM_EOL
143       || scm_equal_p (glyph, prev_glyph_) == SCM_BOOL_F
144       || scm_equal_p (clefpos, prev_cpos_) == SCM_BOOL_F
145       || scm_equal_p (octavation, prev_octavation_) == SCM_BOOL_F
146       || to_boolean (force_clef))
147     {
148       set_context_property_on_children (context (),
149                                         ly_symbol2scm ("localKeySignature"),
150                                         get_property ("keySignature"));
151       
152       set_glyph ();
153       if (prev_cpos_ != SCM_BOOL_F || to_boolean (get_property ("firstClef")))
154         create_clef ();
155
156       if (clef_)
157         clef_->set_property ("non-default", SCM_BOOL_T);
158
159       prev_cpos_ = clefpos;
160       prev_glyph_ = glyph;
161       prev_octavation_ = octavation;
162     }
163
164   if (to_boolean (force_clef))
165     {
166       SCM prev;
167       Context *w = context ()->where_defined (ly_symbol2scm ("forceClef"), &prev);
168       w->set_property ("forceClef", SCM_EOL);
169     }
170 }
171
172 void
173 Clef_engraver::stop_translation_timestep ()
174 {
175   if (clef_)
176     {
177       SCM vis = 0;
178       if (to_boolean (clef_->get_property ("non-default")))
179         vis = get_property ("explicitClefVisibility");
180
181       if (vis)
182         {
183           clef_->set_property ("break-visibility", vis);
184           if (octavate_)
185             octavate_->set_property ("break-visibility", vis);
186         }
187
188       clef_ = 0;
189
190       octavate_ = 0;
191     }
192 }
193
194 ADD_ACKNOWLEDGER (Clef_engraver, bar_line);
195 ADD_TRANSLATOR (Clef_engraver,
196                 /* doc */
197                 "Determine and set reference point for pitches.",
198
199                 /* create */
200                 "Clef "
201                 "OctavateEight ",
202
203                 /* read */
204                 "clefGlyph "
205                 "clefOctavation "
206                 "clefPosition "
207                 "explicitClefVisibility "
208                 "forceClef ",
209
210                 /* write */
211                 ""
212                 );