]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / clef-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Mats Bengtsson <matsb@s3.kth.se>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <cctype>
22 using namespace std;
23
24 #include "item.hh"
25 #include "context.hh"
26 #include "staff-symbol-referencer.hh"
27 #include "engraver.hh"
28 #include "direction.hh"
29 #include "side-position-interface.hh"
30
31 #include "translator.icc"
32
33 class Clef_engraver : public Engraver
34 {
35 public:
36   TRANSLATOR_DECLARATIONS (Clef_engraver);
37
38 protected:
39   void stop_translation_timestep ();
40   void process_music ();
41   DECLARE_ACKNOWLEDGER (bar_line);
42
43   virtual void derived_mark () const;
44 private:
45   Item *clef_;
46   Item *modifier_;
47
48   SCM prev_glyph_;
49   SCM prev_cpos_;
50   SCM prev_transposition_;
51   void create_clef ();
52   void set_glyph ();
53   void inspect_clef_properties ();
54 };
55
56 void
57 Clef_engraver::derived_mark () const
58 {
59   scm_gc_mark (prev_transposition_);
60   scm_gc_mark (prev_cpos_);
61   scm_gc_mark (prev_glyph_);
62 }
63
64 Clef_engraver::Clef_engraver ()
65 {
66   clef_ = 0;
67   modifier_ = 0;
68
69   /*
70     will trigger a clef at the start since #f != ' ()
71   */
72   prev_transposition_ = prev_cpos_ = prev_glyph_ = SCM_BOOL_F;
73 }
74
75 void
76 Clef_engraver::set_glyph ()
77 {
78   SCM glyph_sym = ly_symbol2scm ("glyph");
79   SCM basic = ly_symbol2scm ("Clef");
80
81   execute_pushpop_property (context (), basic, glyph_sym, SCM_UNDEFINED);
82   execute_pushpop_property (context (), basic, glyph_sym, get_property ("clefGlyph"));
83 }
84
85 /**
86    Generate a clef at the start of a measure. (when you see a Bar,
87    ie. a breakpoint)
88 */
89 void
90 Clef_engraver::acknowledge_bar_line (Grob_info info)
91 {
92   Item *item = info.item ();
93   if (item && scm_is_string (get_property ("clefGlyph")))
94     create_clef ();
95 }
96
97 void
98 Clef_engraver::create_clef ()
99 {
100   if (!clef_)
101     {
102       Item *c = make_item ("Clef", SCM_EOL);
103
104       clef_ = c;
105       SCM cpos = get_property ("clefPosition");
106
107       if (scm_is_number (cpos))
108         clef_->set_property ("staff-position", cpos);
109
110       SCM transp = get_property ("clefTransposition");
111       if (scm_is_number (transp) && scm_to_int (transp))
112         {
113           Item *g = make_item ("ClefModifier", SCM_EOL);
114
115           int abs_transp = scm_to_int (transp);
116           int dir = sign (abs_transp);
117           abs_transp = abs (abs_transp) + 1;
118
119           SCM txt = scm_number_to_string (scm_from_int (abs_transp),
120                                           scm_from_int (10));
121
122           SCM style = get_property ("clefTranspositionStyle");
123
124           SCM formatter = get_property ("clefTranspositionFormatter");
125           if (ly_is_procedure (formatter))
126             g->set_property ("text", scm_call_2 (formatter, txt, style));
127
128           Side_position_interface::add_support (g, clef_);
129
130           g->set_parent (clef_, Y_AXIS);
131           g->set_parent (clef_, X_AXIS);
132           g->set_property ("direction", scm_from_int (dir));
133           modifier_ = g;
134         }
135     }
136 }
137 void
138 Clef_engraver::process_music ()
139 {
140   inspect_clef_properties ();
141 }
142
143 static void apply_on_children (Context *context, SCM fun)
144 {
145   scm_call_1 (fun, context->self_scm ());
146   for (SCM s = context->children_contexts ();
147        scm_is_pair (s); s = scm_cdr (s))
148     apply_on_children (unsmob_context (scm_car (s)), fun);
149 }
150
151 void
152 Clef_engraver::inspect_clef_properties ()
153 {
154   SCM glyph = get_property ("clefGlyph");
155   SCM clefpos = get_property ("clefPosition");
156   SCM transposition = get_property ("clefTransposition");
157   SCM force_clef = get_property ("forceClef");
158
159   if (clefpos == SCM_EOL
160       || scm_equal_p (glyph, prev_glyph_) == SCM_BOOL_F
161       || scm_equal_p (clefpos, prev_cpos_) == SCM_BOOL_F
162       || scm_equal_p (transposition, prev_transposition_) == SCM_BOOL_F
163       || to_boolean (force_clef))
164     {
165       apply_on_children (context (),
166                          ly_lily_module_constant ("invalidate-alterations"));
167
168       set_glyph ();
169       if (prev_cpos_ != SCM_BOOL_F || to_boolean (get_property ("firstClef")))
170         create_clef ();
171
172       if (clef_)
173         clef_->set_property ("non-default", SCM_BOOL_T);
174
175       prev_cpos_ = clefpos;
176       prev_glyph_ = glyph;
177       prev_transposition_ = transposition;
178     }
179
180   if (to_boolean (force_clef))
181     {
182       SCM prev;
183       Context *w = context ()->where_defined (ly_symbol2scm ("forceClef"), &prev);
184       w->set_property ("forceClef", SCM_EOL);
185     }
186 }
187
188 void
189 Clef_engraver::stop_translation_timestep ()
190 {
191   if (clef_)
192     {
193       SCM vis = 0;
194       if (to_boolean (clef_->get_property ("non-default")))
195         vis = get_property ("explicitClefVisibility");
196
197       if (vis)
198         clef_->set_property ("break-visibility", vis);
199
200       clef_ = 0;
201
202       modifier_ = 0;
203     }
204 }
205
206 ADD_ACKNOWLEDGER (Clef_engraver, bar_line);
207 ADD_TRANSLATOR (Clef_engraver,
208                 /* doc */
209                 "Determine and set reference point for pitches.",
210
211                 /* create */
212                 "Clef "
213                 "ClefModifier ",
214
215                 /* read */
216                 "clefGlyph "
217                 "clefTransposition "
218                 "clefTranspositionStyle "
219                 "clefPosition "
220                 "explicitClefVisibility "
221                 "forceClef ",
222
223                 /* write */
224                 ""
225                );