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