]> git.donarmstrong.com Git - lilypond.git/blob - lily/clef-engraver.cc
Issue 4947: Link notes to dynamics in Dynamic_performer rather than
[lilypond.git] / lily / clef-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 #include "lily-imports.hh"
33
34 class Clef_engraver : public Engraver
35 {
36 public:
37   TRANSLATOR_DECLARATIONS (Clef_engraver);
38
39 protected:
40   void stop_translation_timestep ();
41   void process_music ();
42   void acknowledge_bar_line (Grob_info);
43
44   virtual void derived_mark () const;
45 private:
46   Item *clef_;
47   Item *modifier_;
48
49   SCM prev_glyph_;
50   SCM prev_cpos_;
51   SCM prev_transposition_;
52   void create_clef ();
53   void set_glyph ();
54   void inspect_clef_properties ();
55 };
56
57 void
58 Clef_engraver::derived_mark () const
59 {
60   scm_gc_mark (prev_transposition_);
61   scm_gc_mark (prev_cpos_);
62   scm_gc_mark (prev_glyph_);
63 }
64
65 Clef_engraver::Clef_engraver ()
66 {
67   clef_ = 0;
68   modifier_ = 0;
69
70   /*
71     will trigger a clef at the start since #f != ' ()
72   */
73   prev_transposition_ = prev_cpos_ = prev_glyph_ = SCM_BOOL_F;
74 }
75
76 void
77 Clef_engraver::set_glyph ()
78 {
79   SCM glyph_sym = ly_symbol2scm ("glyph");
80   SCM basic = ly_symbol2scm ("Clef");
81
82   execute_pushpop_property (context (), basic, glyph_sym, SCM_UNDEFINED);
83   execute_pushpop_property (context (), basic, glyph_sym, get_property ("clefGlyph"));
84 }
85
86 /**
87    Generate a clef at the start of a measure. (when you see a Bar,
88    ie. a breakpoint)
89 */
90 void
91 Clef_engraver::acknowledge_bar_line (Grob_info info)
92 {
93   Item *item = info.item ();
94   if (item && scm_is_string (get_property ("clefGlyph")))
95     create_clef ();
96 }
97
98 void
99 Clef_engraver::create_clef ()
100 {
101   if (!clef_)
102     {
103       Item *c = make_item ("Clef", SCM_EOL);
104
105       clef_ = c;
106       SCM cpos = get_property ("clefPosition");
107
108       if (scm_is_number (cpos))
109         clef_->set_property ("staff-position", cpos);
110
111       SCM transp = get_property ("clefTransposition");
112       if (scm_is_number (transp) && scm_to_int (transp))
113         {
114           Item *g = make_item ("ClefModifier", SCM_EOL);
115
116           int abs_transp = scm_to_int (transp);
117           int dir = sign (abs_transp);
118           abs_transp = abs (abs_transp) + 1;
119
120           SCM txt = scm_number_to_string (scm_from_int (abs_transp),
121                                           scm_from_int (10));
122
123           SCM style = get_property ("clefTranspositionStyle");
124
125           SCM formatter = get_property ("clefTranspositionFormatter");
126           if (ly_is_procedure (formatter))
127             g->set_property ("text", scm_call_2 (formatter, txt, style));
128
129           Side_position_interface::add_support (g, clef_);
130
131           g->set_parent (clef_, Y_AXIS);
132           g->set_parent (clef_, X_AXIS);
133           g->set_property ("direction", scm_from_int (dir));
134           modifier_ = g;
135         }
136     }
137 }
138 void
139 Clef_engraver::process_music ()
140 {
141   inspect_clef_properties ();
142 }
143
144 static void apply_on_children (Context *context, SCM fun)
145 {
146   scm_call_1 (fun, context->self_scm ());
147   for (SCM s = context->children_contexts ();
148        scm_is_pair (s); s = scm_cdr (s))
149     apply_on_children (unsmob<Context> (scm_car (s)), fun);
150 }
151
152 void
153 Clef_engraver::inspect_clef_properties ()
154 {
155   SCM glyph = get_property ("clefGlyph");
156   SCM clefpos = get_property ("clefPosition");
157   SCM transposition = get_property ("clefTransposition");
158   SCM force_clef = get_property ("forceClef");
159
160   if (scm_is_null (clefpos)
161       || !ly_is_equal (glyph, prev_glyph_)
162       || !ly_is_equal (clefpos, prev_cpos_)
163       || !ly_is_equal (transposition, prev_transposition_)
164       || to_boolean (force_clef))
165     {
166       apply_on_children (context (), Lily::invalidate_alterations);
167
168       set_glyph ();
169       if (scm_is_true (prev_cpos_) || 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 void
207 Clef_engraver::boot ()
208 {
209   ADD_ACKNOWLEDGER (Clef_engraver, bar_line);
210 }
211
212 ADD_TRANSLATOR (Clef_engraver,
213                 /* doc */
214                 "Determine and set reference point for pitches.",
215
216                 /* create */
217                 "Clef "
218                 "ClefModifier ",
219
220                 /* read */
221                 "clefGlyph "
222                 "clefTransposition "
223                 "clefTranspositionStyle "
224                 "clefPosition "
225                 "explicitClefVisibility "
226                 "forceClef ",
227
228                 /* write */
229                 ""
230                );