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